微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 无线和射频 > TI无线射频设计 > cc1310 运行tirtos,在串口接收中断里采用event_post不能唤醒任务

cc1310 运行tirtos,在串口接收中断里采用event_post不能唤醒任务

时间:12-23 整理:3721RD 点击:

cc1310 运行tirtos,在串口接收中断里采用event_post不能唤醒任务

/* uart 中断接收函数 */
static void uart_rx_callback(UART_Handle h, void *buf, size_t count){
Event_post(uart_event_handle, UART_RX_DATA_DONE_EVENT);
}

任务:

while(1){

events = Event_pend(uart_event_handle, 0,
UART_RX_DATA_DONE_EVENT | UART_TX_DATA_EVENT,
BIOS_WAIT_FOREVER); 

}

我在资料上看到event_post不能再hwi里调用,不知道应该怎么解决。

你可以参照UART Echo示例代码。

另外,针对你的代码,可否贴出你对UART的初始化和callback函数的注册代码。

建议有信号量试一下。

这是我的uart的配置和callback,

我在

events = Event_pend(uart_event_handle, 0,
UART_RX_DATA_DONE_EVENT | UART_TX_DATA_EVENT,
10); 

调用event_pend,如果是 BIOS_WAIT_FOREVER 这线程就不会被唤醒

/*
** uart_task.c
**
*/
/* XDCtools Header files */
#include <xdc/std.h>
#include <xdc/runtime/System.h>
#include <xdc/runtime/Error.h>
#include <xdc/runtime/IHeap.h>
#include <xdc/runtime/Memory.h>

/* BIOS Header files */
#include <ti/sysbios/BIOS.h>
#include <ti/sysbios/knl/Task.h>
#include <ti/sysbios/knl/Semaphore.h>
#include <ti/sysbios/knl/Event.h>
#include <ti/sysbios/knl/Clock.h>
//#include <ti/sysbios/knl/Mailbox.h>
#include <ti/sysbios/knl/Queue.h>
#include <ti/sysbios/heaps/HeapBuf.h>

/* drivers */
#include "Board.h"
#include <ti/drivers/UART.h>
#include <ti/drivers/uart/UARTCC26XX.h>

/* std files */
#include <string.h>

/* task file */
#include "task/uart_task.h"
#include "task/task_config.h"

/* protocol */
#include "protocol/protocol.h"

/* base file*/
#include "base/error_code.h"
#include "base/type.h"
#include "base/const.h"

#include "struct.h"
#include "event_id.h"

/* logger */
#include "logger/logger.h"

/* task */
static Task_Struct uart_task_struct;
static mm_uint8 uart_task_stack[UART_TASK_STACK_SIZE];

/* handle */
static UART_Handle uart_handle;

/*事件*/
Event_Handle uart_event_handle = 0;
static Event_Struct uart_event_stru;


/* 串口的中断接收缓冲 1Byte */
//static mm_uint8 uart_rx_tmp_buffer[32];

static mm_buffer_item* uart_rx_buffer_item;
static mm_buffer_item* uart_tx_buffer_item;

/* uart 接收发送队列 */
static Queue_Struct uart_rx_queue_stru;
Queue_Handle uart_rx_queue_handle;
static Queue_Struct uart_tx_queue_stru;
Queue_Handle uart_tx_queue_handle;

/* 接收缓冲区 */
#define HEAP_BUFFER_COUNT             1

static mm_buffer_item uart_rx_heap_buffer[UART_HEAP_BUFFER_COUNT];
static HeapBuf_Struct uart_rx_heap_buf_stru;
static HeapBuf_Handle uart_rx_heap_buf_handle;
IHeap_Handle uart_rx_heap_handle;

static mm_buffer_item uart_tx_heap_buffer[UART_HEAP_BUFFER_COUNT];
static HeapBuf_Struct uart_tx_heap_buf_stru;
static HeapBuf_Handle uart_tx_heap_buf_handle;
IHeap_Handle uart_tx_heap_handle;

/* 事件 */
extern Event_Handle service_event_handle;

static mm_bool is_running = mm_true;

/* uart 中断接收函数 */
static void uart_rx_callback(UART_Handle h, void *buf, size_t count){
    /*
    if(uart_rx_buffer_item->data.lenght < BUFFER_MAX_SIZE-1){
        //uart_rx_buffer_item->data.payload[uart_rx_buffer_item->data.lenght++] = ((mm_uint8 *)buf)[0];
        memcpy(&uart_rx_buffer_item->data.payload[uart_rx_buffer_item->data.lenght], 
               (mm_uint8 *)buf, count);
        uart_rx_buffer_item->data.lenght += count;
    }
    else
        uart_rx_buffer_item->data.lenght = 0;
    */
    
    uart_rx_buffer_item->data.lenght += count;
    if(check_uart_protocol_valid(uart_rx_buffer_item->data.payload, 
                                 &uart_rx_buffer_item->data.lenght) == MM_ERR_OK){
        Event_post(uart_event_handle, UART_RX_DATA_DONE_EVENT);
    }
    else {
        if(uart_rx_buffer_item->data.lenght >= sizeof(uart_rx_buffer_item->data.payload)){
            UART_read(uart_handle, uart_rx_buffer_item->data.payload, 
                      sizeof(uart_rx_buffer_item->data.payload));
            uart_rx_buffer_item->data.lenght = 0;
        }
        else{
            UART_read(uart_handle, uart_rx_buffer_item->data.payload + uart_rx_buffer_item->data.lenght, 
                      sizeof(uart_rx_buffer_item->data.payload) - uart_rx_buffer_item->data.lenght);
        }
    }
    
    
}

/* uart收发线程 */
static void uart_task(UArg arg0, UArg arg1){
    mm_uint32 events = 0;
    Task_Stat statbuf;
    
    LOG_D("uart_task start\n");
    
    /* 初始化串口 */
    System_printf("start init uart\n");
    UART_Params uart_params;
    UART_Params_init(&uart_params);
    uart_params.readMode = UART_MODE_CALLBACK;
    uart_params.writeMode = UART_MODE_BLOCKING;
    uart_params.readCallback = uart_rx_callback;
    uart_params.writeCallback = 0;
    uart_params.readReturnMode = UART_RETURN_FULL;
    uart_params.readDataMode = UART_DATA_BINARY;
    uart_params.writeDataMode = UART_DATA_BINARY;
    uart_params.readEcho = UART_ECHO_OFF;
    uart_params.baudRate = UART_BAUD;
    uart_params.dataLength = UART_LEN_8;
    uart_params.stopBits = UART_STOP_ONE;
    uart_params.parityType = UART_PAR_NONE;
    uart_handle = UART_open(Board_UART0, &uart_params);
    if(uart_handle == NULL)
        LOG_E("Error opening the UART");
    
    UART_control(uart_handle, UARTCC26XX_CMD_RETURN_PARTIAL_ENABLE, NULL);
    
    uart_rx_buffer_item = Memory_alloc(uart_rx_heap_handle, sizeof(mm_buffer), 0, NULL);
    if(!uart_rx_buffer_item){
        LOG_E("Memory_alloc Failed");
    }
    uart_rx_buffer_item->data.lenght = 0;
    UART_read(uart_handle, uart_rx_buffer_item->data.payload, 
              sizeof(uart_rx_buffer_item->data.payload));
    
    while(is_running){
        Task_stat(Task_self(), &statbuf);
        if (statbuf.used > (statbuf.stackSize * 9 / 10)){
            LOG_D("uart_task Over 90%% of task's stack is in use = %d, size = %d\n", statbuf.used, statbuf.stackSize);
        }
        
        events = Event_pend(uart_event_handle, 0, 
                            UART_RX_DATA_DONE_EVENT | UART_TX_DATA_EVENT, 
                            10); // BIOS_WAIT_FOREVER

        /* 接收串口数据*/
        if(events & UART_RX_DATA_DONE_EVENT){
            LOG_MD(uart_rx_buffer_item->data.payload, uart_rx_buffer_item->data.lenght, 
                   "uart rx ok, lenght=%d, msg=\n", uart_rx_buffer_item->data.lenght);

            Queue_enqueue(uart_rx_queue_handle, &uart_rx_buffer_item->_elem);
            uart_rx_buffer_item = NULL;
            Event_post(uart_event_handle, UART_RX_DATA_EVENT);
            Event_post(service_event_handle, SERVICE_EVENT_UART_DATA_TX_RX);
            
            uart_rx_buffer_item = Memory_alloc(uart_rx_heap_handle, sizeof(mm_buffer), 0, NULL);
            if(!uart_rx_buffer_item){
                LOG_E("Memory_alloc Failed");
            }

            uart_rx_buffer_item->data.lenght = 0;
            UART_read(uart_handle, uart_rx_buffer_item->data.payload, 
                      sizeof(uart_rx_buffer_item->data.payload));            
        }
        
        /* 发送数据 */
        if(events & UART_TX_DATA_EVENT){
            while(Queue_empty(uart_tx_queue_handle) != TRUE){
                uart_tx_buffer_item = Queue_dequeue(uart_tx_queue_handle);
                if(uart_tx_buffer_item){
                    UART_write(uart_handle, uart_tx_buffer_item->data.payload, uart_tx_buffer_item->data.lenght);

                    LOG_MD(uart_tx_buffer_item->data.payload, uart_tx_buffer_item->data.lenght, 
                           "uart tx ok, lenght=%d, msg=\n", uart_tx_buffer_item->data.lenght);
                    
                    Memory_free(uart_tx_heap_handle, uart_tx_buffer_item, sizeof(mm_buffer_item));
                    uart_tx_buffer_item = NULL;

                    if(service_event_handle){ // 发送事件
                        Event_post(service_event_handle, SERVICE_EVENT_UART_DATA_TX_RX);
                    }
                }
            }
        }
    }
}


void init_uart_task(void){
    /* 初始化 uart_task */
    Error_Block eb;
    Task_Params task_params;

    /* 初始化 err */
    Error_init(&eb);
    
    /* 初始化事件 */
    Event_Params event_param;
    Event_Params_init(&event_param);
    Event_construct(&uart_event_stru, &event_param);
    uart_event_handle = Event_handle(&uart_event_stru);

    /* 初始化消息队列 */
    Queue_Params queue_params;
    Queue_Params_init(&queue_params);
    Queue_construct(&uart_rx_queue_stru, &queue_params);
    uart_rx_queue_handle = Queue_handle(&uart_rx_queue_stru);
    Queue_construct(&uart_tx_queue_stru, &queue_params);
    uart_tx_queue_handle = Queue_handle(&uart_tx_queue_stru);

    /* 初始化内存heap */
    HeapBuf_Params heap_buf_params;
    HeapBuf_Params_init(&heap_buf_params);
    heap_buf_params.blockSize = sizeof(mm_buffer_item);
    heap_buf_params.numBlocks = UART_HEAP_BUFFER_COUNT;
    heap_buf_params.align = 4;
    heap_buf_params.buf = uart_rx_heap_buffer;
    heap_buf_params.bufSize = sizeof(uart_rx_heap_buffer);
    HeapBuf_construct(&uart_rx_heap_buf_stru, &heap_buf_params, NULL);
    uart_rx_heap_buf_handle = HeapBuf_handle(&uart_rx_heap_buf_stru);
    uart_rx_heap_handle = HeapBuf_Handle_upCast(uart_rx_heap_buf_handle);

    HeapBuf_Params_init(&heap_buf_params);
    heap_buf_params.blockSize = sizeof(mm_buffer_item);
    heap_buf_params.numBlocks = UART_HEAP_BUFFER_COUNT;
    heap_buf_params.align = 4;
    heap_buf_params.buf = uart_tx_heap_buffer;
    heap_buf_params.bufSize = sizeof(uart_tx_heap_buffer); 
    HeapBuf_construct(&uart_tx_heap_buf_stru, &heap_buf_params, NULL);
    uart_tx_heap_buf_handle = HeapBuf_handle(&uart_tx_heap_buf_stru);
    uart_tx_heap_handle = HeapBuf_Handle_upCast(uart_tx_heap_buf_handle);
    
    Task_Params_init(&task_params);
    task_params.stackSize = sizeof(uart_task_stack);
    task_params.stack = &uart_task_stack;
    task_params.priority = UART_TASK_PRIORITY;
    Task_construct(&uart_task_struct, (Task_FuncPtr)uart_task, &task_params, &eb);
}

void stop_uart_task(void){
    is_running = mm_false;
}

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top