基于STM32H7 UART空闲事件及DMA传输示例

有人使用stm32h7芯片的uart,想利用dma实现不定长的数据接收,实现过程似乎不太顺利。另外,刚好最近有人使用h7芯片做uart收发,当开启d-cache未做mpu配置,使用指令对d-cache做相应失效处理,发现通信结果异常。我这里简单演示下实现过程,分使用和不使用d-cache两种做法,以供参考。
这里使用uart4做发送,uart5做接收,都使用dma,均工作在normal模式。
在主循环里每隔一定时间让uart4发送一串数据出去,一共3串依次发送。每次发送的数据长度不一样,但不会超过20个字符。
让uart5基于dma做不定长的数据接收,将接收长度定义在20。同时开启uart5的空闲中断。在uart5的空闲中断里再次开启下一轮的dma接收准备。
关键api函数,uart4的发送函数和uart5用到的接收函数如下所示:
顺便提下,在h7系列的hal库里定义了好几个uart接收类型,使用时适当注意下。这里用的是下图划红线的关注idle事件的接收类型。
我在idle事件的中断回调函数里就做了一件事,为下次uart5的dma接收做准备。至于接收到的数据的后续处理就没做进一步操作了。
这里是对不定长数据进行接收,使用idle事件的中断比使用dma接收完成中断更方便些。
不开启d-cache时的主要测试代码如下:
#define buffer_size 20align_32bytes(uint8_t rxdata[buffer_size];)align_32bytes(char txdata1[]=a5a5lfjaf888;)align_32bytes(char txdata2[]=1234$%^&555;)align_32bytes(char txdata3[]=aa8$%^&*33333;)uint8_t tempcnt;int main(void){/* user code begin 1 *//* user code end 1 *//* enable i-cache---------------------------------------------------------*/   scb_enableicache();/* mcu configuration--------------------------------------------------------*//* reset of all peripherals, initializes the flash interface and the systick. */ hal_init();/* user code begin init *//* user code end init *//* configure the system clock */ systemclock_config();/* user code begin sysinit */  /* user code end sysinit *//* initialize all configured peripherals */ mx_gpio_init();  mx_dma_init(); mx_uart4_init();  mx_uart5_init();/* user code begin 2 */ hal_delay(5);  __hal_uart_clear_flag(&huart5, uart_clear_idlef);          hal_uartex_receivetoidle_dma(&huart5, rxdata,  buffer_size);/* user code end 2 *//* infinite loop *//* user code begin while */while (1) {/* user code end while */    /* user code begin 3 */ hal_delay (500); //延时长点方便观察调试窗口的数据变化    for (uint8_t i=0;i上面是没有启用d-cache的代码,若启用d-cache而又不想配置mpu,就得在适当地方添加对cache的失效操作或清除操作。调整后的代码如下:
int main(void){/* user code begin 1 *//* user code end 1 *//* enable i-cache---------------------------------------------------------*/ scb_enableicache();/* enable d-cache---------------------------------------------------------*/ scb_enabledcache(); //*****/* mcu configuration--------------------------------------------------------*//* reset of all peripherals, initializes the flash interface and the systick. */ hal_init();/* user code begin init *//* user code end init *//* configure the system clock */ systemclock_config();/* user code begin sysinit *//* user code end sysinit *//* initialize all configured peripherals */ mx_gpio_init();  mx_dma_init(); mx_uart4_init(); mx_uart5_init();/* user code begin 2 */  hal_delay(5);       __hal_uart_clear_flag(&huart5, uart_clear_idlef);         hal_uartex_receivetoidle_dma(&huart5, rxdata,  buffer_size);        /* user code end 2 *//* infinite loop *//* user code begin while */while (1) {/* user code end while *//* user code begin 3 */scb_invalidatedcache_by_addr(rxdata,buffer_size);//*** hal_delay (500); //延时长点方便观察调试窗口的数据变化for (uint8_t i=0;i 好,今天的话题就分享到这里。下次再聊。


诺基亚死追不放,誓要联想海外PC产品被禁售
如何使用分类器字符串来分析和描述具有特定光学特性的光线
iphone8什么时候上市?iphone8最新消息:三星S8最后忍痛放弃屏幕指纹方案,可能被iPhone8首发!
38款App违规被通报 豆瓣小红书在列
砥砺前行 菲菱科思携手推动深圳繁荣发展
基于STM32H7 UART空闲事件及DMA传输示例
ADAS是否能取代人类决策?
4G发牌众生相:面对千亿蛋糕运营商心思各异
直流电能表在意大利光伏蓄电池项目的应用
红米Note 5定档3月8日首卖 或1199元起售
叶鲜叶农残检测设备的操作方法及注意事项
360N5低价高配,OPPO学学人家,6G运存才900元!
Marvell发布业界最完整的802.11ax无线产品系列
美高森美宣布提供低成本IGLOO 2 FPGA评测工具套件
2028年飞行出租车将出现在洛杉矶纽约上空
海柔创新与全球顶尖物流集成商MHS达成战略合作
GaN外延生长方法及生长模式
有线电视放大器的作用是什么
人工智能打开定制动态驾驶新的时间一代
PK Sound品牌与广州极声行在中国达成战略合作关系