Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
RT-Thread
M2354
【NuMaker-M2354试用】USCI测试
发布于 2021-12-22 22:38:58 浏览:777
订阅该版
时间:2021年12月22日 作者:lianghai **1.测试环境** MDK V5.26.2.0 Nu-Link_Keil_Driver 3.07.7246r ENV V1.2.0 Python V2.7.13 rt-thread-v4.0.4 USB转TTL **2.测试任务** 本次测试任务为新唐 NuMaker-M2354 USCI模块 **3.资源介绍** 翻阅新唐M2354数据手册 USCI可配置为UART,SPI,I2C三种工作模式,本次选择测试配置其为UART功能 ![image.png](https://oss-club.rt-thread.org/uploads/20211222/8ef82da4d96124445257afc80583d47a.png) USCI-UART ![image.png](https://oss-club.rt-thread.org/uploads/20211222/2a0c78e2f3c2a922ce248a4d13c3a74d.png.webp) 查看开发板原理图,发现PD1、PD2空闲 ![image.png](https://oss-club.rt-thread.org/uploads/20211222/0454ea39c9b9646243e68cb98a3456be.png.webp) ![image.png](https://oss-club.rt-thread.org/uploads/20211222/fd9c384ad94c0aefe13df416acd0215f.png) ![image.png](https://oss-club.rt-thread.org/uploads/20211222/1f5e4d1aacab5668d0010aa374c08091.png) 硬件连接 ![image.png](https://oss-club.rt-thread.org/uploads/20211222/d3efdf78f56d007e6a7a9749016ffe40.jpg.webp) **4.代码** nu_usci_uart.c ``` uint32_t UUART_Open(UUART_T* uuart, uint32_t u32baudrate) { uint32_t u32PCLKFreq, u32PDSCnt, u32DSCnt, u32ClkDiv; uint32_t u32Tmp, u32Tmp2, u32Min, u32MinClkDiv, u32MinDSCnt; uint32_t u32Div; /* Get PCLK frequency */ if((uuart == UUART0) || (uuart == UUART0_NS)) { u32PCLKFreq = CLK_GetPCLK0Freq(); } else { u32PCLKFreq = CLK_GetPCLK1Freq(); } /* Calculate baud rate divider */ u32Div = u32PCLKFreq / u32baudrate; u32Tmp = (u32PCLKFreq / u32Div) - u32baudrate; u32Tmp2 = u32baudrate - (u32PCLKFreq / (u32Div + 1ul)); if(u32Tmp >= u32Tmp2) u32Div = u32Div + 1ul; if(u32Div >= 65536ul) { /* Set the smallest baud rate that USCI_UART can generate */ u32PDSCnt = 0x4ul; u32MinDSCnt = 0x10ul; u32MinClkDiv = 0x400ul; } else { u32Tmp = 0x400ul * 0x10ul; for(u32PDSCnt = 1ul; u32PDSCnt <= 0x04ul; u32PDSCnt++) { if(u32Div <= (u32Tmp * u32PDSCnt)) break; } if(u32PDSCnt > 0x4ul) u32PDSCnt = 0x4ul; u32Div = u32Div / u32PDSCnt; /* Find best solution */ u32Min = (uint32_t) - 1; u32MinDSCnt = 0ul; u32MinClkDiv = 0ul; u32Tmp = 0ul; for(u32DSCnt = 6ul; u32DSCnt <= 0x10ul; u32DSCnt++) /* DSCNT could be 0x5~0xF */ { u32ClkDiv = u32Div / u32DSCnt; if(u32ClkDiv > 0x400ul) { u32ClkDiv = 0x400ul; u32Tmp = u32Div - (u32ClkDiv * u32DSCnt); u32Tmp2 = u32Tmp + 1ul; } else { u32Tmp = u32Div - (u32ClkDiv * u32DSCnt); u32Tmp2 = ((u32ClkDiv + 1ul) * u32DSCnt) - u32Div; } if(u32Tmp >= u32Tmp2) { u32ClkDiv = u32ClkDiv + 1ul; } else u32Tmp2 = u32Tmp; if(u32Tmp2 < u32Min) { u32Min = u32Tmp2; u32MinDSCnt = u32DSCnt; u32MinClkDiv = u32ClkDiv; /* Break when get good results */ if(u32Min == 0ul) { break; } } } } /* Enable USCI_UART protocol */ uuart->CTL &= ~UUART_CTL_FUNMODE_Msk; uuart->CTL = 2ul << UUART_CTL_FUNMODE_Pos; /* Set USCI_UART line configuration */ uuart->LINECTL = UUART_WORD_LEN_8 | UUART_LINECTL_LSB_Msk; uuart->DATIN0 = (2ul << UUART_DATIN0_EDGEDET_Pos); /* Set falling edge detection */ /* Set USCI_UART baud rate */ uuart->BRGEN = ((u32MinClkDiv - 1ul) << UUART_BRGEN_CLKDIV_Pos) | ((u32MinDSCnt - 1ul) << UUART_BRGEN_DSCNT_Pos) | ((u32PDSCnt - 1ul) << UUART_BRGEN_PDSCNT_Pos); uuart->PROTCTL |= UUART_PROTCTL_PROTEN_Msk; return (u32PCLKFreq / u32PDSCnt / u32MinDSCnt / u32MinClkDiv); } uint32_t UUART_SetLine_Config(UUART_T* uuart, uint32_t u32baudrate, uint32_t u32data_width, uint32_t u32parity, uint32_t u32stop_bits) { uint32_t u32PCLKFreq, u32PDSCnt, u32DSCnt, u32ClkDiv; uint32_t u32Tmp, u32Tmp2, u32Min, u32MinClkDiv, u32MinDSCnt; uint32_t u32Div; /* Get PCLK frequency */ if((uuart == UUART0) || (uuart == UUART0_NS)) { u32PCLKFreq = CLK_GetPCLK0Freq(); } else /* UUART1 */ { u32PCLKFreq = CLK_GetPCLK1Freq(); } if(u32baudrate != 0ul) { /* Calculate baud rate divider */ u32Div = u32PCLKFreq / u32baudrate; u32Tmp = (u32PCLKFreq / u32Div) - u32baudrate; u32Tmp2 = u32baudrate - (u32PCLKFreq / (u32Div + 1ul)); if(u32Tmp >= u32Tmp2) u32Div = u32Div + 1ul; if(u32Div >= 65536ul) { /* Set the smallest baud rate that USCI_UART can generate */ u32PDSCnt = 0x4ul; u32MinDSCnt = 0x10ul; u32MinClkDiv = 0x400ul; } else { u32Tmp = 0x400ul * 0x10ul; for(u32PDSCnt = 1ul; u32PDSCnt <= 0x04ul; u32PDSCnt++) { if(u32Div <= (u32Tmp * u32PDSCnt)) break; } if(u32PDSCnt > 0x4ul) u32PDSCnt = 0x4ul; u32Div = u32Div / u32PDSCnt; /* Find best solution */ u32Min = (uint32_t) - 1; u32MinDSCnt = 0ul; u32MinClkDiv = 0ul; for(u32DSCnt = 6ul; u32DSCnt <= 0x10ul; u32DSCnt++) /* DSCNT could be 0x5~0xF */ { u32ClkDiv = u32Div / u32DSCnt; if(u32ClkDiv > 0x400ul) { u32ClkDiv = 0x400ul; u32Tmp = u32Div - (u32ClkDiv * u32DSCnt); u32Tmp2 = u32Tmp + 1ul; } else { u32Tmp = u32Div - (u32ClkDiv * u32DSCnt); u32Tmp2 = ((u32ClkDiv + 1ul) * u32DSCnt) - u32Div; } if(u32Tmp >= u32Tmp2) { u32ClkDiv = u32ClkDiv + 1ul; } else u32Tmp2 = u32Tmp; if(u32Tmp2 < u32Min) { u32Min = u32Tmp2; u32MinDSCnt = u32DSCnt; u32MinClkDiv = u32ClkDiv; /* Break when get good results */ if(u32Min == 0ul) { break; } } } } /* Set USCI_UART baud rate */ uuart->BRGEN = ((u32MinClkDiv - 1ul) << UUART_BRGEN_CLKDIV_Pos) | ((u32MinDSCnt - 1ul) << UUART_BRGEN_DSCNT_Pos) | ((u32PDSCnt - 1ul) << UUART_BRGEN_PDSCNT_Pos); } else { u32PDSCnt = ((uuart->BRGEN & UUART_BRGEN_PDSCNT_Msk) >> UUART_BRGEN_PDSCNT_Pos) + 1ul; u32MinDSCnt = ((uuart->BRGEN & UUART_BRGEN_DSCNT_Msk) >> UUART_BRGEN_DSCNT_Pos) + 1ul; u32MinClkDiv = ((uuart->BRGEN & UUART_BRGEN_CLKDIV_Msk) >> UUART_BRGEN_CLKDIV_Pos) + 1ul; } /* Set USCI_UART line configuration */ uuart->LINECTL = (uuart->LINECTL & ~UUART_LINECTL_DWIDTH_Msk) | u32data_width; uuart->PROTCTL = (uuart->PROTCTL & ~(UUART_PROTCTL_STICKEN_Msk | UUART_PROTCTL_EVENPARITY_Msk | UUART_PROTCTL_PARITYEN_Msk)) | u32parity; uuart->PROTCTL = (uuart->PROTCTL & ~UUART_PROTCTL_STOPB_Msk) | u32stop_bits; return (u32PCLKFreq / u32PDSCnt / u32MinDSCnt / u32MinClkDiv); } uint32_t UUART_Write(UUART_T* uuart, uint8_t pu8TxBuf[], uint32_t u32WriteBytes) { uint32_t u32Count, u32delayno; for(u32Count = 0ul; u32Count != u32WriteBytes; u32Count++) { u32delayno = 0ul; while((uuart->BUFSTS & UUART_BUFSTS_TXEMPTY_Msk) == 0ul) /* Wait Tx empty */ { u32delayno++; if(u32delayno >= 0x40000000ul) { break; } } if(u32delayno >= 0x40000000ul) { break; } uuart->TXDAT = (uint8_t)pu8TxBuf[u32Count]; /* Send USCI_UART Data to buffer */ } return u32Count; } ``` main.c ``` uint8_t uuart0_send[]="RT-thread nuvoton test\r\n"; uint32_t ret1,ret2; int main(int argc, char **argv) { #if defined(RT_USING_PIN) int counter = 100; /* set LEDR pin mode to output */ rt_pin_mode(LEDR, PIN_MODE_OUTPUT); SYS->GPD_MFPL &= ~(SYS_GPD_MFPL_PD1MFP_Msk | SYS_GPD_MFPL_PD2MFP_Msk); SYS->GPD_MFPL |= (SYS_GPD_MFPL_PD1MFP_USCI0_DAT0 | SYS_GPD_MFPL_PD2MFP_USCI0_DAT1); ret1=UUART_SetLine_Config(UUART0,BAUD_RATE_115200,8,0,1); ret2=UUART_Open(UUART0,BAUD_RATE_115200); while (counter--) { rt_pin_write(LEDR, PIN_HIGH); rt_thread_mdelay(500); rt_pin_write(LEDR, PIN_LOW); rt_thread_mdelay(500); // usr_trng=nu_trng_rand(); UUART_Write(UUART0,uuart0_send,sizeof(uuart0_send)); } #endif return 0; } ``` main.c添加头文件 ``` #include "nu_usci_uart.h" ``` **5.结果** ![image.png](https://oss-club.rt-thread.org/uploads/20211222/b4713cecff5acb0438d81fcced4a9645.png.webp) **6.心得** RT-Thread提供的BSP很全面,驱动都已经封装好了,技术手册也讲得很清楚,调试的时候可以对照技术手册查看寄存器基本都能找到问题,后面闲再对其他模块测试分享出来
1
条评论
默认排序
按发布时间排序
登录
注册新账号
关于作者
Bin角的最后一根
这家伙很懒,什么也没写!
文章
7
回答
0
被采纳
0
关注TA
发私信
相关文章
1
RT-THREAD在STM32H747平台上移植lwip
2
正点原子miniSTM32开发板读写sdcard
3
反馈rtt串口驱动对低功耗串口lpuart1不兼容的问题
4
Keil MDK 移植 RT-Thread Nano
5
RT1061/1052 带 RTT + LWIP和LPSPI,有什么坑要注意吗?
6
RT thread HID 如何收发数据
7
求一份基于RTT系统封装好的STM32F1系列的FLASH操作程序
8
RT-Thread修改项目名称之后不能下载
9
rt-studio编译c++
10
有木有移植rt-thread(nano)到riscv 32位MCU上
推荐文章
1
RT-Thread应用项目汇总
2
玩转RT-Thread系列教程
3
国产MCU移植系列教程汇总,欢迎查看!
4
机器人操作系统 (ROS2) 和 RT-Thread 通信
5
五分钟玩转RT-Thread新社区
6
【技术三千问】之《玩转ART-Pi》,看这篇就够了!干货汇总
7
关于STM32H7开发板上使用SDIO接口驱动SD卡挂载文件系统的问题总结
8
STM32的“GPU”——DMA2D实例详解
9
RT-Thread隐藏的宝藏之completion
10
【ART-PI】RT-Thread 开启RTC 与 Alarm组件
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
FinSH
ART-Pi
USB
DMA
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
FAL
rt-smart
ESP8266
I2C_IIC
UART
WIZnet_W5500
ota在线升级
freemodbus
PWM
flash
cubemx
packages_软件包
BSP
潘多拉开发板_Pandora
定时器
ADC
flashDB
GD32
socket
中断
编译报错
Debug
SFUD
rt_mq_消息队列_msg_queue
msh
keil_MDK
ulog
C++_cpp
MicroPython
本月问答贡献
踩姑娘的小蘑菇
4
个答案
1
次被采纳
红枫
4
个答案
1
次被采纳
张世争
4
个答案
1
次被采纳
Ryan_CW
4
个答案
1
次被采纳
xiaorui
1
个答案
1
次被采纳
本月文章贡献
catcatbing
3
篇文章
5
次点赞
qq1078249029
2
篇文章
2
次点赞
xnosky
2
篇文章
1
次点赞
Woshizhapuren
1
篇文章
5
次点赞
YZRD
1
篇文章
2
次点赞
回到
顶部
发布
问题
投诉
建议
回到
底部