|
本帖最后由 wo4fisher 于 2018-9-3 20:51 编辑
1、colibri_350共有两个USART外设,在板上USART0连接到了PA9(Tx)&PA10(Rx),然后排针TTL电平引出,USART1在CPU引脚PA2(Tx)&PA3(Rx)引出到ch340...
图中左上角第一列排针从上到下第一脚是PA10,第二脚是PA9;最下边usb口作为调试器和ch340(usb转ttl)共用。
2、“printf("HelloWorld\r\n");”走起
运行固件库中的 04_USART_Printf 例程,前提是在前边修改了LED脚位和cpu时钟选择基础上。
源程序对usart的定义:
- /* eval board low layer COM */
- #define COMn 1U
- /* definition for COM1, connected to USART0 */
- #define EVAL_COM1 USART0
- #define EVAL_COM1_CLK RCU_USART0
- #define EVAL_COM1_TX_PIN GPIO_PIN_9
- #define EVAL_COM1_RX_PIN GPIO_PIN_10
- #define EVAL_COM_GPIO_PORT GPIOA
- #define EVAL_COM_GPIO_CLK RCU_GPIOA
- #define EVAL_COM_AF GPIO_AF_1
复制代码
- static rcu_periph_enum COM_CLK[COMn] = {EVAL_COM1_CLK};
- static uint32_t COM_TX_PIN[COMn] = {EVAL_COM1_TX_PIN};
- static uint32_t COM_RX_PIN[COMn] = {EVAL_COM1_RX_PIN};
复制代码
串口初始化相关代码:
- void gd_eval_com_init(uint32_t com)
- {
- uint32_t COM_ID;
-
- if(EVAL_COM1 == com){
- COM_ID = 0U;
- }else{
- }
- /* enable COM GPIO clock */
- rcu_periph_clock_enable(EVAL_COM_GPIO_CLK);
- /* enable USART clock */
- rcu_periph_clock_enable(COM_CLK[COM_ID]);
- /* connect port to USARTx_Tx */
- gpio_af_set(EVAL_COM_GPIO_PORT, EVAL_COM_AF, COM_TX_PIN[COM_ID]); [/size]
- [size=4]
- /* connect port to USARTx_Rx */
- gpio_af_set(EVAL_COM_GPIO_PORT, EVAL_COM_AF, COM_RX_PIN[COM_ID]);
- /* configure USART Tx as alternate function push-pull */
- gpio_mode_set(EVAL_COM_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, COM_TX_PIN[COM_ID]);
- gpio_output_options_set(EVAL_COM_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, COM_TX_PIN[COM_ID]);
- /* configure USART Rx as alternate function push-pull */
- gpio_mode_set(EVAL_COM_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, COM_RX_PIN[COM_ID]);
- gpio_output_options_set(EVAL_COM_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, COM_RX_PIN[COM_ID]);
- /* USART configure */
- usart_deinit(com);
- usart_baudrate_set(com, 115200U);
- usart_receive_config(com, USART_RECEIVE_ENABLE);
- usart_transmit_config(com, USART_TRANSMIT_ENABLE);
- usart_enable(com);
- }
复制代码
编译,一切正常,下载代码到板子上,复位运行(事先用另一个usb转ttl的Rx脚连接PA9,GND相连)
结果如下:
3、另一个USART呢
我们知道,colibri板上的cpu有两个usart,并且都已引出,那么下边就来在2的基础上实现两个串口都能正常输出。
很简单,在原来基础上,再增加一路就可以了。
usart相关定义:
- /* eval board low layer COM */
- #define COMn 2U
- /* definition for COM0, connected to USART0 */
- #define EVAL_COM0 USART0
- #define EVAL_COM0_CLK RCU_USART0
- #define EVAL_COM0_TX_PIN GPIO_PIN_9
- #define EVAL_COM0_RX_PIN GPIO_PIN_10
- #define EVAL_COM0_GPIO_PORT GPIOA //
- #define EVAL_COM0_GPIO_CLK RCU_GPIOA //
- #define EVAL_COM0_AF GPIO_AF_1
- #define EVAL_COM1 USART1
- #define EVAL_COM1_CLK RCU_USART1
- #define EVAL_COM1_TX_PIN GPIO_PIN_2
- #define EVAL_COM1_RX_PIN GPIO_PIN_3
- #define EVAL_COM1_GPIO_PORT GPIOA //
- #define EVAL_COM1_GPIO_CLK RCU_GPIOA //
- #define EVAL_COM1_AF GPIO_AF_1
复制代码
- static rcu_periph_enum COM_CLK[COMn] = {EVAL_COM0_CLK,EVAL_COM1_CLK};
- static uint32_t COM_TX_PIN[COMn] = {EVAL_COM0_TX_PIN,EVAL_COM1_TX_PIN};
- static uint32_t COM_RX_PIN[COMn] = {EVAL_COM0_RX_PIN,EVAL_COM1_RX_PIN};
复制代码
串口初始化函数:
- void gd_eval_com_init(uint32_t com)
- {
- uint32_t COM_ID;
-
- if(EVAL_COM0 == com){
- COM_ID = 0U;
- /* enable COM GPIO clock */
- rcu_periph_clock_enable(EVAL_COM0_GPIO_CLK);
- /* enable USART clock */
- rcu_periph_clock_enable(COM_CLK[COM_ID]);
- /* connect port to USARTx_Tx */
- gpio_af_set(EVAL_COM0_GPIO_PORT, EVAL_COM0_AF, COM_TX_PIN[COM_ID]);
- /* connect port to USARTx_Rx */
- gpio_af_set(EVAL_COM0_GPIO_PORT, EVAL_COM0_AF, COM_RX_PIN[COM_ID]);
- /* configure USART Tx as alternate function push-pull */
- gpio_mode_set(EVAL_COM0_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, COM_TX_PIN[COM_ID]);
- gpio_output_options_set(EVAL_COM0_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, COM_TX_PIN[COM_ID]);
- /* configure USART Rx as alternate function push-pull */
- gpio_mode_set(EVAL_COM0_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, COM_RX_PIN[COM_ID]);
- gpio_output_options_set(EVAL_COM0_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, COM_RX_PIN[COM_ID]);
- /* USART configure */
- usart_deinit(com);
- usart_baudrate_set(com, 115200U);
- usart_receive_config(com, USART_RECEIVE_ENABLE);
- usart_transmit_config(com, USART_TRANSMIT_ENABLE);
- usart_enable(com);
- }
- else if(EVAL_COM1 == com){
- COM_ID = 1U;
- /* enable COM GPIO clock */
- rcu_periph_clock_enable(EVAL_COM1_GPIO_CLK);
- /* enable USART clock */
- rcu_periph_clock_enable(COM_CLK[COM_ID]);
- /* connect port to USARTx_Tx */
- gpio_af_set(EVAL_COM1_GPIO_PORT, EVAL_COM1_AF, COM_TX_PIN[COM_ID]);
- /* connect port to USARTx_Rx */
- gpio_af_set(EVAL_COM1_GPIO_PORT, EVAL_COM1_AF, COM_RX_PIN[COM_ID]);
- /* configure USART Tx as alternate function push-pull */
- gpio_mode_set(EVAL_COM1_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, COM_TX_PIN[COM_ID]);
- gpio_output_options_set(EVAL_COM1_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, COM_TX_PIN[COM_ID]);
- /* configure USART Rx as alternate function push-pull */
- gpio_mode_set(EVAL_COM1_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, COM_RX_PIN[COM_ID]);
- gpio_output_options_set(EVAL_COM1_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, COM_RX_PIN[COM_ID]);
- /* USART configure */
- usart_deinit(com);
- usart_baudrate_set(com, 115200U);
- usart_receive_config(com, USART_RECEIVE_ENABLE);
- usart_transmit_config(com, USART_TRANSMIT_ENABLE);
- usart_enable(com);
- }
- }
复制代码
:代码未作优化。
初始化的时候只要使用 gd_eval_com_init(EVAL_COM0)或者 gd_eval_com_init(EVAL_COM1)就可以实现USART0和USART1的切换。当然如果是使用printf的话,需要事先将fputc()函数中的相关代码对应到USART0或者USART1。
这样两个USART就实现了数据的发送。
基于篇幅,下一篇,USART1+DMA(收、发)+各种中断+modbus客户端实现。
|
|