本帖最后由 shipeng1989 于 2023-12-18 15:29 编辑
为了实现串口接收不定长数据到消息队列,前段时间我在网上找到一个串口数据接收逻辑感觉很完美,于是直接拿来使用。他是这样做的:
1.定义一个消息队列队列单元设置4个字节用来保存指针数据,队列长度随意;
2.当接收到串口数据后先申请动态内存;
3.再将接收到的数据copy到申请的内存中;
4.将申请到的内存地址放入队列中;
5.在任务中从容的处理消息队列。
以下是源代码:
- #define USE_FREERTOS_MALLOC 0
- #if USE_FREERTOS_MALLOC
- #define my_malloc(x) pvPortMalloc(x)
- #define my_free(x) vPortFree(x)
- #else
- #define my_malloc(x) malloc(x)
- #define my_free(x) free(x)
- #endif
- typedef struct
- {
- uint8_t length;
- uint8_t DatArea[];
- }UsartReceiveData_t;
- void USART1_IRQHandler(void)
- {
- static uint8_t rx_index = 0;
- if(USART_ReadIntFlag(USART1, USART_INT_IDLE))
- {
- USART1->DATA;//CLEAR USART IDLE FLAG
- BaseType_t xHigherPriorityTaskWoken = pdFALSE;
- uint8_t rx_len = UPDATE==StatusSystemEnum ? rx_index : sizeof(DMA_USART1_RxBuf) - DMA1_Channel5->CHNDATA_B.NDATA;
- UsartReceiveData_t *RxStruct = (UsartReceiveData_t*)my_malloc(rx_len+1);
- if (NULL!=RxStruct)
- {
- memcpy(RxStruct->DatArea,DMA_USART1_RxBuf,rx_len);RxStruct->length = rx_len;
- if (NULL==U1RxQueue || pdPASS!=xQueueSendFromISR(U1RxQueue, (uint8_t*)&RxStruct, &xHigherPriorityTaskWoken))printf("U1RxQueue sending failed!");
- else portYIELD_FROM_ISR(xHigherPriorityTaskWoken);//Request a context switch (Start task switching after interrupt)
- }
- else printf("USART1 RxStruct my_malloc failed!");
- DMA_Disable(DMA1_Channel5);
- DMA_ConfigDataNumber(DMA1_Channel5, sizeof(DMA_USART1_RxBuf));
- DMA_Enable(DMA1_Channel5);
- rx_index = 0;
- }
- else if(USART_ReadIntFlag(USART1, USART_INT_RXBNE))//Only for DWIN UPDATE,See "main.c" on line:501
- {
- uint8_t buf8 = USART1->DATA;
- DMA_USART1_RxBuf[rx_index++] = buf8;
- if ((UART_DWIN->STS & USART_FLAG_TXBE) != (uint16_t)RESET)UART_DWIN->DATA_B.DATA = buf8;
- }
- }
当用malloc函数时一切安好,后面听小人言pvPortMalloc函数更安全于是就听信了谗言结果我就是那个宋高宗,软件分分钟就崩溃了。后经百般检索了解到pvPortMalloc函数不能在中断中调用,不知各位高人有无**之法?
|