资深版主
 
- 积分
- 1702
- 金钱
- 1702
- 注册时间
- 2012-5-10
- 在线时间
- 439 小时
|
发表于 2018-5-30 17:02:30
|
显示全部楼层
本帖最后由 xsx127 于 2018-5-30 17:04 编辑
希望你能看明白吧
1,串口中断内容(包含接收中断,发送中断)
if(USART_GetITStatus(UART4 , USART_IT_TXE) == SET)
{
/* The interrupt was caused by the THR becoming empty. Are there any
more characters to transmit?
Because FreeRTOS is not supposed to run with nested interrupts, put all OS
calls in a critical section . */
portENTER_CRITICAL();
retstatus = xQueueReceiveFromISR( xTxQueue, &cChar, &xHigherPriorityTaskWoken );
portEXIT_CRITICAL();
if (retstatus == pdTRUE)
{
/* A character was retrieved from the queue so can be sent to the THR now. */
USART_SendData(UART4, cChar);
}
else
{
/* Queue empty, nothing to send so turn off the Tx interrupt. */
USART_ITConfig(UART4, USART_IT_TXE, DISABLE);
}
}
if(USART_GetITStatus(UART4, USART_IT_RXNE) == SET)
{
/* The interrupt was caused by the receiver getting data. */
cChar = USART_ReceiveData(UART4);
/* Because FreeRTOS is not supposed to run with nested interrupts, put all OS
calls in a critical section . */
portENTER_CRITICAL();
xQueueSendFromISR(xRxQueue, &cChar, &xHigherPriorityTaskWoken);
portEXIT_CRITICAL();
}
2,一个任务的内容函数原型,任务内调用此函数即可
/***************************************************************************************************
*FunctionName:ReceiveDataFromQueue
*Description:从队列中读取有限长度的数据
*Input:queue -- 目标队列
* mutex -- 此队列的互斥量,可为null
* receivedstr -- 存放接收数据的地址
* len -- 接收的数据长度(长度为此队列单元数据大小的个数,与数据接收数据的字节长度无关)
* readSize -- 实际读取到的数据长度
* itemsize -- 队列单元数据的大小
* queueBlockTime -- 队列阻塞时间(超时,则中断接收,处理已经接收的数据)
* mutexBlockTime -- 互斥量阻塞时间
*Output:None
*Author:xsx,565170595@qq.com
*Data:2016年4月22日15:35:40
***************************************************************************************************/
unsigned char ReceiveDataFromQueue(xQueueHandle queue, xSemaphoreHandle mutex, void *receivedstr , unsigned short len ,
unsigned short * readSize, unsigned short itemsize, portTickType queueBlockTime, portTickType mutexBlockTime)
{
unsigned short i=0;
unsigned char *pdata = (unsigned char *)receivedstr;
unsigned char statues = pdFAIL;
if(queue == NULL)
return pdFAIL;
if(mutex != NULL)
{
if(pdFAIL == WaittingForMutex(mutex, mutexBlockTime))
return pdFAIL;
}
for(i=0; i<len; i++)
{
if(pdPASS == xQueueReceive(queue, pdata , queueBlockTime))
pdata += itemsize;
else
break;
}
if(i > 0)
statues = pdPASS;
if(readSize)
*readSize = i;
if(mutex != NULL)
GivexMutex(mutex);
return statues;
} |
|