如何在中断中使用pvPortMalloc函数
本帖最后由 shipeng 于 2023-12-8 09:41 编辑<p>为了实现串口接收不定长数据到消息队列,前段时间我在网上找到一个串口数据接收逻辑感觉很完美,于是直接拿来使用。他是这样做的:</p>
<p>1.定义一个消息队列队列单元设置4个字节用来保存指针数据,队列深度随意;</p>
<p>2.当接收到串口数据后先申请动态内存;</p>
<p>3.再将接收到的数据copy到申请的内存中;</p>
<p>4.申请的内存地址放入消息队列中;</p>
<p>5.在任务中从容的处理消息队列。</p>
<p>以下是源代码:</p>
<pre>
<code class="language-cpp">
#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;
QueueHandle_t U1RxQueue = xQueueCreate(4, 16);
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 = buf8;
if ((UART_DWIN->STS & USART_FLAG_TXBE) != (uint16_t)RESET)UART_DWIN->DATA_B.DATA = buf8;
}
}
</code></pre>
<p>当用malloc函数时一切安好,后面听小人言pvPortMalloc函数更安全于是就听信了谗言结果我就是那个宋高宗,软件分分钟就崩溃了。后经百般检索了解到pvPortMalloc函数不能在中断中调用,不知各位高人有无破解之法?</p>
<p>中断里不要用</p>
页:
[1]