由于ES32F065板卡没有板载串口转USB芯片,需要自己外接一个USB串口模块进行测试。 串口初始化函数: <font size="4"><b>//串口GPIO初始化
static void uart_232_pin_init(void)
{
gpio_init_t x;
/* Initialize tx pin */
x.mode = GPIO_MODE_OUTPUT;
x.odos = GPIO_PUSH_PULL;
x.pupd = GPIO_PUSH_UP;
x.odrv = GPIO_OUT_DRIVE_NORMAL;
x.flt = GPIO_FILTER_DISABLE;
x.type = GPIO_TYPE_TTL;
x.func = GPIO_FUNC_3;
ald_gpio_init(GPIOB, GPIO_PIN_10, &x);
/* Initialize rx pin */
x.mode = GPIO_MODE_INPUT;
x.odos = GPIO_PUSH_PULL;
x.pupd = GPIO_PUSH_UP;
x.odrv = GPIO_OUT_DRIVE_NORMAL;
x.flt = GPIO_FILTER_DISABLE;
x.type = GPIO_TYPE_TTL;
x.func = GPIO_FUNC_3;
ald_gpio_init(GPIOB, GPIO_PIN_11, &x);
}
//串口初始化
void uart_232_module_init(uint32_t baud)
{
uart_232_pin_init();
memset(&h_uart, 0x0, sizeof(h_uart));
/* Initialize uart */
h_uart.perh = UART0;
h_uart.init.baud = baud;
h_uart.init.word_length = UART_WORD_LENGTH_8B;
h_uart.init.stop_bits = UART_STOP_BITS_1;
h_uart.init.parity = UART_PARITY_NONE;
h_uart.init.mode = UART_MODE_UART;
h_uart.init.fctl = UART_HW_FLOW_CTL_DISABLE;
ald_uart_init(&h_uart);
}
</b></font>
串口接收数据函数: <font size="4"><b>uart_232_status_t bsp_uart_232_recv(uint8_t *buf, uint16_t size)
{
if (buf == NULL)
return UART_232_ERROR;
if (ald_uart_recv(&h_uart, buf, size, 500) != OK)
return UART_232_ERROR;
return UART_232_OK;
}
</b></font>
串口写数据函数: <font size="4"><b>uart_232_status_t bsp_uart_232_send(uint8_t *buf, uint16_t size)
{
if (buf == NULL)
return UART_232_ERROR;
if (ald_uart_send(&h_uart, buf, size, 500) != OK)
return UART_232_ERROR;
return UART_232_OK;
}
</b></font>
添加修改main.c和mian.h,实现对串口的读写操作。 <font size="4"><b>#include "main.h"
unsigned char Sendbuf[16] = "es32f0654 is ok!";
int main()
{
led_module_init(); //LED初始化
bsp_key_init(); //按键初始化
uart_232_module_init(115200);
bsp_uart_232_send(Sendbuf,16);
while (1)
{
}
}
#ifndef __MAIN_H__
#define __MAIN_H__
#include "bsp_led.h"
#include "bsp_key.h"
#include "bsp_uart_232.h"
#endif
</b></font>
硬件连接后,打印数据如下:
|