2531 views|3 replies

1662

Posts

0

Resources
The OP
 

Zhongke Bluexun AB32VG1 development board based on RT-Thread system--the use of serial port devices--sharing [Copy link]

 

Reprinted from: https://blog.bruceou.cn/2021/03/4-watchdog/770/

https://blog.bruceou.cn/2021/03/3-use-of-serial-port-equipment/766/

Development environment:
RT-Thread version: 4.0.3
Operating system: Windows 10
RT-Thread Studio version: 2.0.0
Development board MCU: AB5301A

In RTT, peripherals exist as devices. When creating a project, AB32VG1 serial port 0 is used as the system debugging serial port. If there is a serial port module that needs to communicate with the microcontroller, you can initialize another serial port. If there is no driver, the first step is to write the driver. For the AB32VG1 development board, the UART driver device has been written. You only need to open the corresponding device.

1. Simple use of serial port devices


1.1 Realize serial port transmission and reception


UART0 has been used as a download and debug serial port, so here we use UART1. Let's take a look at the circuit first.


As can be seen from the circuit diagram, PA3 and PA4 are used here, so it is necessary to connect PA3 and PA4 to the PC via USB to serial port and use the serial port debugging assistant to view the sending and receiving information.

This project enables UART0 and UART1 by default, and the UART devices can also be seen through the finish terminal.


Just write the application code here. Create task.c and task.h files in applications.

[task.c]

/**
 ******************************************************************************
 * @File        task.c
 * @author       BruceOu
 * @LIB version     V3.5.0
 * @version       V1.0
 * @date        2021-01-10
 * @blog        https://blog.bruceou.cn/
 * @official Accounts  嵌入式实验楼
 * @brief        RTT任务
 ******************************************************************************
 */
/*Includes**********************************************************************/
#include "task.h"

struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT; /* 初始化配置参数 */

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

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

  return RT_EOK;
}

/**
 * @brief serial_thread_entry
 * @param parameter
 * @retval None
 */
static void serial_thread_entry(void *parameter)
{
  char ch;

  while (1)
  {
    /* 从串口读取一个字节的数据,没有读取到则等待接收信号量 */
    while (rt_device_read(serial, -1, &ch, 1) != 1)
    {
      /* 阻塞等待接收信号量,等到信号量后再次读取数据 */
      rt_sem_take(&rx_sem, RT_WAITING_FOREVER);
    }
    /* 读取到的数据输出 */
    rt_kprintf("%c",ch);
  }
}

/**
 * @brief thread_serial
 * @param None
 * @retval ret
 */
int thread_serial(void)
{
  rt_err_t ret = RT_EOK;
  char uart_name[RT_NAME_MAX];
  char str[] = "hello RT-Thread!\r\n";

  rt_strncpy(uart_name, SAMPLE_UART_NAME, RT_NAME_MAX);

  /* 查找系统中的串口设备 */
  serial = rt_device_find(uart_name);
  if (!serial)
  {
    rt_kprintf("find %s failed!\n", uart_name);
    return RT_ERROR;
  }
  /* 修改串口配置参数 */
  config.baud_rate = BAUD_RATE_9600;    //修改波特率为 9600
  config.data_bits = DATA_BITS_8;      //数据位 8
  config.stop_bits = STOP_BITS_1;      //停止位 1
  config.bufsz   = 64;          //修改缓冲区 buff size 为 128
  config.parity  = PARITY_NONE;      //无奇偶校验位

  /* 控制串口设备。通过控制接口传入命令控制字,与控制参数 */
  rt_device_control(serial, RT_DEVICE_CTRL_CONFIG, &config);

  /* 初始化信号量 */
  rt_sem_init(&rx_sem, "rx_sem", 0, RT_IPC_FLAG_FIFO);
  /* 以中断接收及轮询发送模式打开串口设备 */
  rt_device_open(serial, RT_DEVICE_FLAG_INT_RX);
  /* 设置接收回调函数 */
  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(thread_serial, uart device sample);


[task.h]

#ifndef _TASK_H_
#define _TASK_H_

/* Standard includes. */
#include <stdio.h>

/* rtthread includes. */
#include <rtdevice.h>
#include <board.h>

#define SAMPLE_UART_NAME         "uart1"

int thread_serial(void);

#endif


The code is very simple. It creates a thread and uses interrupt reception and polling data in the thread. The main functions are as follows:

rt_device_find() //Find devicert_device_open
() //Open devicert_device_read
() //Read datart_device_write
() //Write datart_device_control
() //Control devicert_device_set_rx_indicate
() //Set receive callback functionrt_device_set_tx_complete
() //Set send completion callback
functionrt_device_close() //Close device1.2

Experimental phenomenon


Compile and download. Enter thread_serial in the finish terminal:


Use the serial port debugging assistant to view the output information of uart1.

You can also send data through the serial port debugging assistant, and the finish terminal can see the received data.

For detailed usage of UART application, please go to RTT Documentation Center.

UART Devices

2. Serial port device optimization


Those who are familiar with RTT know that all devices in RTT are turned on and off through macro definitions, but AB32VG1 does not have this part perfect. So how can we turn on and off UART devices through configuration like other BSPs? Let's explain them one by one.

First of all, we need to know that the UART initialization of AB32VG1 calls the rt_hw_usart_init() function in drv_uart.c.

Here uart_config defines the base address of UART and determines the size of UART. Therefore, the definition of this variable needs to be modified as follows:


In fact, a few macros are added, and the enumeration is also modified as follows:

In addition, you need to modify the UART interrupt function:

Next we can pass

#define BSP_USING_UART0
#define BSP_USING_UART1

controls the opening and closing of the UART device. It is not over yet. To realize the configuration of the board, you need to modify the Kconfig file of the board. The modified content is as follows:

Now the modification is complete, and you can use the configuration just now. Here is the RT-Thread Studio configuration, open the RT-Thread Setting, and you can configure the UART.

As for why UART0 cannot be unconfigured, it is because UART0 is used as a debugging serial port and has been forcibly configured.

Of course, you can also use the ENV tool to configure it. Enter the project directory, open the env console, and enter the menuconfig command to open its interface. Enter the following options in sequence:

→ Hardware Drivers Config → On-chip Peripheral → Enable UART

to enable UART1, save and exit.

The result is the same as adding a serial port device in RT-Thread Studio. The following macro definition will be added to rtconfig.h.

OK, that's it for the optimization of the serial port device. Now the UART device can be turned on and off.

This post is from Domestic Chip Exchange

Latest reply

I tried it and got an error, assertion failed at function:rt_object_init, why is this?  Details Published on 2023-2-14 16:50

赞赏

1

查看全部赞赏

 
 

1942

Posts

2

Resources
2
 

RTThread's tools are very convenient to use!

This post is from Domestic Chip Exchange
 
 
 

55

Posts

0

Resources
3
 

Do you consider the author's feelings when copying?

This post is from Domestic Chip Exchange
 
 
 

7

Posts

0

Resources
4
 

I tried it and got an error, assertion failed at function:rt_object_init, why is this?


This post is from Domestic Chip Exchange
 
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list