主题中讨论的其他器件:CC2340R5
工具与软件:
您好!
我有一个接收 UART 数据并需要恢复任务的应用程序。 处理完数据后、它需要回复用户数据是否正确、然后暂停任务。 为什么 UART2_WRITE 和 UART2_READ 不能在同一个任务中一起使用?
我已经尝试集成 user_uart2callback 示例、但似乎也不起作用。
完整代码
/*
* Copyright (c) 2020, Texas Instruments Incorporated
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of Texas Instruments Incorporated nor the names of
* its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* ======== uart2callback.c ========
*/
/* POSIX Header files */
#include <pthread.h>
/* For usleep() */
#include <unistd.h>
#include <stdint.h>
#include <stddef.h>
#include <string.h>
/* POSIX Header files */
#include <semaphore.h>
/* Driver Header files */
#include <ti/drivers/GPIO.h>
#include <ti/drivers/UART2.h>
/* Driver configuration */
#include "ti_drivers_config.h"
/* RTOS header files */
#include <FreeRTOS.h>
#include <task.h>
//#include <inc_task.h>
//INC_TASK_H
#define USER_STACK_SIZE 1024
#define UART_MAX_READ_SIZE 250
static sem_t sem;
static volatile size_t numBytesRead, UART_status = 0;
UART2_Handle uart;
UART2_Params uartParams;
TaskHandle_t user_xHandle;
pthread_mutex_t LEDBlink_mutex;//pthread_mutex_lock ,pthread_mutex_unlock
uint8_t uartReadBuffer[250] = {0};
void callbackFxn(UART2_Handle handle, void *buffer, size_t count, void *userArg, int_fast16_t status);
void Open_UART_FUN()
{
char input;
const char echoPrompt[] = "Echoing characters:\r\n";
uint32_t status = UART2_STATUS_SUCCESS;
/* Create a UART in CALLBACK read mode */
UART2_Params_init(&uartParams);
uartParams.readMode = UART2_Mode_CALLBACK;
uartParams.readCallback = callbackFxn;
uartParams.baudRate = 115200;
uart = UART2_open(CONFIG_UART2_0, &uartParams);
if (uart == NULL)
{
/* UART2_open() failed */
while (1) {}
}
/* Turn on user LED to indicate successful initialization */
//GPIO_write(CONFIG_GPIO_LED_0, CONFIG_GPIO_LED_ON);
/* Pass NULL for bytesWritten since it's not used in this example */
UART2_write(uart, echoPrompt, sizeof(echoPrompt), NULL);
UART2_read(uart, uartReadBuffer, UART_MAX_READ_SIZE, NULL);
}
/*******************************************************************************
*/
/* Task to be created. */
void vTaskCode( void * pvParameters )
{
/* The parameter value is expected to be 1 as 1 is passed in the
pvParameters value in the call to xTaskCreate() below. */
for( ;; )
{
/* Task code goes here. */
GPIO_toggle(CONFIG_GPIO_LED_0);
// UART2_write(uart, (char*)"OK\r\n", strlen("OK\r\n"), NULL);
UART2_read(uart, uartReadBuffer, UART_MAX_READ_SIZE, NULL);
vTaskSuspend(NULL);
}
}
/* Function that creates a task. */
void vOtherFunction( void )
{
BaseType_t xReturned;
/* Configure the LED pin */
GPIO_setConfig(CONFIG_GPIO_LED_0, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW);
/* Turn on user LED to indicate successful initialization */
GPIO_write(CONFIG_GPIO_LED_0, CONFIG_GPIO_LED_ON);
/* Create the task, storing the handle. */
xReturned = xTaskCreate(
vTaskCode, /* Function that implements the task. */
"user_app", /* Text name for the task. */
USER_STACK_SIZE, /* Stack size in words, not bytes. */
( void * ) 1, /* Parameter passed into the task. */
tskIDLE_PRIORITY,/* Priority at which the task is created. */
&user_xHandle ); /* Used to pass out the created task's handle. */
}
/*
* ======== callbackFxn ========
*/
void callbackFxn(UART2_Handle handle, void *buffer, size_t count, void *userArg, int_fast16_t status)
{
if (status != UART2_STATUS_SUCCESS)
{
/* RX error occured in UART2_read() */
while (1) {}
}
numBytesRead = count;
//thread_mutex_lock(&LEDBlink_mutex);
UART_status = 1;
//hread_mutex_unlock(&LEDBlink_mutex);
memcpy(uartReadBuffer,buffer,numBytesRead);
BaseType_t xYieldRequired; //xYieldRequired
// Resume the suspended task.
xYieldRequired = xTaskResumeFromISR(user_xHandle);
// We should switch context so the ISR returns to a different task.
// NOTE: How this is done depends on the port you are using. Check
// the documentation and examples for your port.
portYIELD_FROM_ISR(xYieldRequired);
}
UART2 LED 成功
void vTaskCode( void * pvParameters )
{
/* The parameter value is expected to be 1 as 1 is passed in the
pvParameters value in the call to xTaskCreate() below. */
for( ;; )
{
/* Task code goes here. */
GPIO_toggle(CONFIG_GPIO_LED_0);
// UART2_write(uart, (char*)"OK\r\n", strlen("OK\r\n"), NULL);
UART2_read(uart, uartReadBuffer, UART_MAX_READ_SIZE, NULL);
vTaskSuspend(NULL);
}
}
UART2 LED 失败
void vTaskCode( void * pvParameters )
{
/* The parameter value is expected to be 1 as 1 is passed in the
pvParameters value in the call to xTaskCreate() below. */
for( ;; )
{
/* Task code goes here. */
GPIO_toggle(CONFIG_GPIO_LED_0);
UART2_write(uart, (char*)"OK\r\n", strlen("OK\r\n"), NULL);
UART2_read(uart, uartReadBuffer, UART_MAX_READ_SIZE, NULL);
vTaskSuspend(NULL);
}
}




