In this section, implements by adding the code to communicate with PC to LED0.1 second cycle blinking program created with Generate new project (FreeRTOS(Kernel Only)) using UART mode of SCI(Serial Communication Interface)
Serialize means
Line up requirement in serial (continuously)
An appropriate data structure as implementation is queue
This method is effective when there are multiple tasks to use the hardware for one hardware.
As typical examples, explain the serialize of print debug.
Receive string transmission requirement from multiple tasks and run into the transmission function of SCI
Since the transmission function of SCI is non-blocking call, the process itself has not completed when the function ends.
Process complete is the format notified by callback function
After the transmission function of SCI ends, take semaphore and is in wait state.
Give semaphore when callback transmission complete notice of SCI, and release wait state.
#include"task_function.h"/* Start user code for import. Do not edit comment generated here */#include"r_sci_rx_if.h"#include"r_sci_rx_pinset.h"#include"platform.h"#include<string.h>voidsci_callback(void*arg);
staticsci_hdl_tsci_handle;
staticsigned portBASE_TYPExHigherPriorityTaskWoken;
externQueueHandle_tqueue_handle_1;
externSemaphoreHandle_tsemaphore_handle_1;
/* End user code. Do not edit comment generated here */voidprint_task(void*pvParameters)
{
/* Start user code for function. Do not edit comment generated here */sci_cfg_tmy_sci_config;
staticcharstring[256];
/* Set up the configuration data structure for asynchronous (UART) operation. */my_sci_config.async.baud_rate=115200;
my_sci_config.async.clk_src=SCI_CLK_INT;
my_sci_config.async.data_size=SCI_DATA_8BIT;
my_sci_config.async.parity_en=SCI_PARITY_OFF;
my_sci_config.async.parity_type=SCI_EVEN_PARITY;
my_sci_config.async.stop_bits=SCI_STOPBITS_1;
my_sci_config.async.int_priority=15; /* disable 0 - low 1 - 15 high */R_SCI_Open(SCI_CH2, SCI_MODE_ASYNC, &my_sci_config, sci_callback, &sci_handle);
R_SCI_PinSet_SCI2();
while(1)
{
xQueueReceive(queue_handle_1, string, portMAX_DELAY);
R_SCI_Send(sci_handle, (uint8_t*)string, strlen(string));
xSemaphoreTake( semaphore_handle_1, portMAX_DELAY );
}
/* End user code. Do not edit comment generated here */
}
/* Start user code for other. Do not edit comment generated here */voidsci_callback(void*arg)
{
xHigherPriorityTaskWoken=pdFALSE;
xSemaphoreGiveFromISR(semaphore_handle_1, &xHigherPriorityTaskWoken);
portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
}
/* End user code. Do not edit comment generated here */
task_2.c coding
#include"task_function.h"/* Start user code for import. Do not edit comment generated here */#include"platform.h"externQueueHandle_tqueue_handle_1;
/* End user code. Do not edit comment generated here */voidtask_2(void*pvParameters)
{
/* Start user code for function. Do not edit comment generated here */charstring[256];
while(1)
{
vTaskDelay(1000);
sprintf(string, "task_2: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n");
xQueueSend(queue_handle_1, string, portMAX_DELAY);
}
/* End user code. Do not edit comment generated here */
}
/* Start user code for other. Do not edit comment generated here *//* End user code. Do not edit comment generated here */
task_3.c coding
#include"task_function.h"/* Start user code for import. Do not edit comment generated here */#include"platform.h"externQueueHandle_tqueue_handle_1;
/* End user code. Do not edit comment generated here */voidtask_3(void*pvParameters)
{
/* Start user code for function. Do not edit comment generated here */charstring[256];
while(1)
{
vTaskDelay(1000);
sprintf(string, "task_3: bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\n");
xQueueSend(queue_handle_1, string, portMAX_DELAY);
}
/* End user code. Do not edit comment generated here */
}
/* Start user code for other. Do not edit comment generated here *//* End user code. Do not edit comment generated here */
Check that the print outputs from task_2 and task_3 are outputted without running into each other.
If R_SCI_Send() is called directly without xQueueSend() from task_2 and task_3, the transmission of task_3 is mixed into that of task_2, accordingly, it is not possible to output without running into each other as described above.