JQ8900的RTT[RT-thread]简易测试代码

-以下为示例代码

可以通过开发板上的按钮来对JQ8900进行操作,达到切换曲目的效果

并且可以自定义修改代码进行更多操作,优化了串口发送信息的方式。

/*
通过RTT例子修改
用于测试YSV0.7是否可用
*/

#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>


#define LED0_PIN    GET_PIN(B, 1)    //定位到野火开发板上两个RGB相关的寄存器上
#define LED0_PINE   GET_PIN(B, 0)
#ifndef KEY0_PIN_NUM
    #define KEY0_PIN_NUM      GET_PIN(A, 0) /* PA0 */
#endif
#ifndef KEY1_PIN_NUM
    #define KEY1_PIN_NUM      GET_PIN(C, 13) /* PC13 */
#endif
#define SAMPLE_UART_NAME       "uart2"

static rt_device_t serial;              /* 串口设备句柄 */
struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT; /* 配置参数 */


/* 用于接收消息的信号量 */
static struct rt_semaphore rx_sem;
static rt_device_t serial;




/* 接收数据回调函数 */
static rt_err_t uart_input(rt_device_t dev, rt_size_t size)
{
    /* 串口接收到数据后产生中断,调用此回调函数,然后发送接收信号量 */
    rt_sem_release(&rx_sem);

    return RT_EOK;
}

static void Re_music(void *args)
{
    static char buffer1[4]={0xAA,0x05,0x00,0xAF};
    rt_uint32_t tx_length;
    tx_length=4;
    int i = 0;
    while(i < 4){
    rt_device_write(serial,0,&buffer1[i],tx_length);
    i++;}
    rt_kprintf("上一条\n");
}
static void next_music(void *args)
{
    static char buffer1[4]={0xAA,0x06,0x00,0xB0};
    rt_uint32_t tx_length;
    tx_length=4;
    int i = 0;
    while(i < 4){
    rt_device_write(serial,0,&buffer1[i],tx_length);
    i++;}
    rt_kprintf("下一条\n");

}

static void serial_thread_entry(void *parameter)
{

  //  char ch;
    int jun = 1;
    while (1)
      
    {		
             /*发送缓冲区是固定为5个字节长度的*/
        static char uart2_tx_buffer[5]={0xAA,0x02,0x00,0xAC,0x05};
        rt_uint32_t tx_length;
        tx_length=5;
    rt_pin_mode(KEY0_PIN_NUM, PIN_MODE_INPUT_PULLUP);
    rt_pin_attach_irq(KEY0_PIN_NUM, PIN_IRQ_MODE_FALLING, Re_music, RT_NULL);
    rt_pin_irq_enable(KEY0_PIN_NUM, PIN_IRQ_ENABLE);


    rt_pin_mode(KEY1_PIN_NUM, PIN_MODE_INPUT_PULLUP);
    rt_pin_attach_irq(KEY1_PIN_NUM, PIN_IRQ_MODE_FALLING, next_music, RT_NULL);
    rt_pin_irq_enable(KEY1_PIN_NUM, PIN_IRQ_ENABLE);
        if(jun){
        rt_device_write(serial,0,&uart2_tx_buffer[0],tx_length);
        rt_device_write(serial,0,&uart2_tx_buffer[1],tx_length);
        rt_device_write(serial,0,&uart2_tx_buffer[2],tx_length);
        rt_device_write(serial,0,&uart2_tx_buffer[3],tx_length);
        rt_kprintf("测试成功!");}
        /* 从串口读取一个字节的数据,没有读取到则等待接收信号量 */					
             jun = 0;
    }
}

static int mdr1(int argc, char *argv[])
{
    rt_err_t ret = RT_EOK;
    char uart_name[RT_NAME_MAX];

    if (argc == 2)
    {
        rt_strncpy(uart_name, argv[1], RT_NAME_MAX);
    }
    else
    {
        rt_strncpy(uart_name, SAMPLE_UART_NAME, RT_NAME_MAX);
    }

    /* 查找系统中的串口设备 */
    serial = rt_device_find(uart_name);

    rt_device_control(serial, RT_DEVICE_CTRL_CONFIG, &config);
    if (!serial)
    {
        rt_kprintf("find %s failed!\n", uart_name);
        return RT_ERROR;
    }

    /* 初始化信号量 */
    rt_sem_init(&rx_sem, "rx_sem", 0, RT_IPC_FLAG_FIFO);
    /* 以中断接收及轮询发送模式打开串口设备 */
    rt_device_open(serial, RT_DEVICE_FLAG_INT_RX);
    config.baud_rate = BAUD_RATE_9600;    //配置串口通信为9600
    config.data_bits = DATA_BITS_8;
    config.stop_bits = STOP_BITS_1;
    config.parity = PARITY_NONE;
    /* 打开设备后才可修改串口配置参数 */
    rt_device_control(serial, RT_DEVICE_CTRL_CONFIG, &config);
    /* 设置接收回调函数 */
    rt_device_set_rx_indicate(serial, uart_input);
    /* 发送字符串 */
//    rt_device_write(serial, 0, str, (sizeof(str) - 1));

    /* 创建 serial 线程 */
    rt_thread_t thread = rt_thread_create("serial", serial_thread_entry, RT_NULL, 1024, 25, 10);
    /* 创建成功则启动线程 */
    if (thread != RT_NULL)
    {
        rt_thread_startup(thread);
    }
    else
    {
        ret = RT_ERROR;
    }

    return ret;
}

/* 导出到 msh 命令列表中 */
MSH_CMD_EXPORT(mdr1, uart JUN);

可以自行添加到RT-thread的BSP中,并烧录到开发板中。

发表评论

电子邮件地址不会被公开。 必填项已用*标注

14 + 14 =