#include “FreeRTOS.h”
#include “task.h”
BaseType_t xTaskCreate( TaskFunction_t pvTaskCode,
const char * const pcName,
unsigned short usStackDepth,
void *pvParameters,
UBaseType_t uxPriority,
TaskHandle_t *pxCreatedTask );
void vTaskDelete( TaskHandle_t pxTask );
TaskHandle_t xTaskCreateStatic( TaskFunction_t pvTaskCode,
const char * const pcName,
uint32_t ulStackDepth,
void *pvParameters,
UBaseType_t uxPriority,
StackType_t * const puxStackBuffer,
StaticTask_t * const pxTaskBuffer );
vTaskStartScheduler();
void vTaskDelay( TickType_t xTicksToDelay );
void vTaskDelayUntil( TickType_t *pxPreviousWakeTime, TickType_t xTimeIncrement );
void vApplicationIdleHook( void );
#include "demo.h"
static volatile uint32_t g_idle_timer;
static void task_led0(void *param)
{
TickType_t xLastWakeTime;
const TickType_t delay_ms = pdMS_TO_TICKS(250);
#if configUSE_IDLE_HOOK == 1
uint32_t last_idle_timer = 0;
uint32_t diff_idle_timer = 0;
#endif
uart_send("task_led0 start!\n", 0);
while(1)
{
LED0_FLIP;
#if configUSE_IDLE_HOOK == 1
diff_idle_timer = g_idle_timer - last_idle_timer;
if(last_idle_timer != g_idle_timer)
{
last_idle_timer = g_idle_timer;
}
uart_send_hex((uint8_t *)&diff_idle_timer, 4);
uart_send("\n", 0);
#endif
vTaskDelayUntil(&xLastWakeTime, delay_ms);
}
}
#if 0
static void task_led1(void *param)
{
TickType_t xLastWakeTime;
const TickType_t delay_ms = pdMS_TO_TICKS(500);
while(1)
{
LED1_FLIP;
vTaskDelayUntil(&xLastWakeTime, delay_ms);
}
}
#endif
static void task_uart(void *param)
{
char *string = (char *)param;
TickType_t xLastWakeTime;
const TickType_t delay_ms = pdMS_TO_TICKS(1000);
while(1)
{
uart_send((uint8_t *)string, strlen(string));
vTaskDelayUntil(&xLastWakeTime, delay_ms);
}
}
#if 0
void task_uart2(void *param)
{
char *string = (char *)param;
uart_init();
TickType_t xLastWakeTime;
const TickType_t delay_ms = pdMS_TO_TICKS(3000);
while(1)
{
uart_send((uint8_t *)string, strlen(string));
vTaskDelayUntil(&xLastWakeTime, delay_ms);
}
}
#endif
void demo_01(void)
{
xTaskCreate(task_led0, "led0", 32, NULL, 1, NULL);
xTaskCreate(task_uart, "uart1", 128, "uart1 is running!\n", 2, NULL);
vTaskStartScheduler();
while(1)
{
;
}
}
void vApplicationIdleHook(void)
{
g_idle_timer ++;
}
QueueHandle_t xQueueCreate( UBaseType_t uxQueueLength,
UBaseType_t uxItemSize );
void vQueueDelete( TaskHandle_t pxQueueToDelete );
UBaseType_t uxQueueMessagesWaiting( const QueueHandle_t xQueue );
BaseType_t xQueueReceive( QueueHandle_t xQueue,
void *pvBuffer,
TickType_t xTicksToWait );
BaseType_t xQueueSend( QueueHandle_t xQueue,
const void * pvItemToQueue,
TickType_t xTicksToWait );
BaseType_t xQueueSendToFront( QueueHandle_t xQueue,
const void * pvItemToQueue,
TickType_t xTicksToWait );
BaseType_t xQueueSendToBack( QueueHandle_t xQueue,
const void * pvItemToQueue,
TickType_t xTicksToWait );
#include "demo.h"
static QueueHandle_t g_queue;
static volatile uint32_t g_tick_timer;
static void task_cmd(void *param)
{
(void)param;
BaseType_t status;
const TickType_t tick_wait = pdMS_TO_TICKS(100);
uint8_t str[32] = {0};
uint32_t len;
while(1)
{
len = uart_recv(str, 32);
if(len == 0)
{
continue;
}
status = xQueueSendToBack(g_queue, &str, tick_wait);
if(status != pdPASS)
{
uart_send("send to queue failed!\n", 0);
}
else
{
uart_send("send to the cmd : ", 0);
uart_send(str, 1);
uart_send("\n", 0);
}
}
}
#if 0
static void task_parse(void *param)
{
(void)param;
BaseType_t status;
const TickType_t tick_wait = pdMS_TO_TICKS(500);
uint8_t cmd;
while(1)
{
if(uxQueueMessagesWaiting(g_queue) != 0)
{
uart_send("queue should have been empty!\n", 0);
}
status = xQueueReceive(g_queue, &cmd, tick_wait);
if(status != pdPASS)
{
continue;
}
uart_send("recv the cmd : ", 0);
uart_send(&cmd, 1);
uart_send("\n", 0);
switch(cmd)
{
case 0x31:
{
LED1_ON;
}
break;
case 0x32:
{
LED1_OFF;
}
break;
case 0x33:
{
LED1_FLIP;
}
break;
default:
{
}
}
}
}
#else
static void task_parse(void *param)
{
uint32_t period = *(uint32_t *)param / 2;
BaseType_t status;
const TickType_t tick_wait = pdMS_TO_TICKS(period);
uint8_t cmd;
while(1)
{
#if configUSE_IDLE_HOOK == 1
uart_send("tick_timer = ", 0);
uart_send_hex((uint8_t *)&g_tick_timer, 4);
uart_send("\n", 0);
#endif
if(uxQueueMessagesWaiting(g_queue) != 0)
{
uart_send("queue should have been empty!\n", 0);
}
status = xQueueReceive(g_queue, &cmd, tick_wait);
if(status != pdPASS)
{
}
else
{
uart_send("recv the cmd : ", 0);
uart_send(&cmd, 1);
uart_send("\n", 0);
LED1_OFF;
}
switch(cmd)
{
case 0x31:
{
LED1_ON;
}
break;
case 0x32:
{
LED1_OFF;
}
break;
case 0x33:
{
LED1_FLIP;
}
break;
default:
{
}
}
}
}
#endif
void demo_02(void)
{
uint32_t period = 1000;
g_queue = xQueueCreate(3, sizeof(uint8_t));
if(g_queue != NULL)
{
xTaskCreate(task_cmd, "cmd", 128, NULL, 1, NULL);
xTaskCreate(task_parse, "parse", 128, &period, 2, NULL);
}
vTaskStartScheduler();
while(1)
{
;
}
}
void vApplicationTickHook(void)
{
g_tick_timer ++;
}
TimerHandle_t xTimerCreate( const char *pcTimerName,
const TickType_t xTimerPeriod,
const UBaseType_t uxAutoReload,
void * const pvTimerID,
TimerCallbackFunction_t pxCallbackFunction );
BaseType_t xTimerDelete( TimerHandle_t xTimer, TickType_t xTicksToWait );
BaseType_t xTimerStart( TimerHandle_t xTimer, TickType_t xTicksToWait );
BaseType_t xTimerStop( TimerHandle_t xTimer, TickType_t xTicksToWait );
BaseType_t xTimerChangePeriod( TimerHandle_t xTimer,
TickType_t xNewPeriod,
TickType_t xTicksToWait );
TickType_t xTimerGetPeriod( TimerHandle_t xTimer );
BaseType_t xTimerReset( TimerHandle_t xTimer, TickType_t xTicksToWait );
TickType_t xTicksToWait = pdMS_TO_TICKS( xTimeInMs );;
#include "demo.h"
static void timer_callback(xTimerHandle timer);
static xTimerHandle g_timer;
static xTimerHandle g_timer_ont_shot;
static void uart_parse(void *param)
{
(void)param;
const TickType_t tick_wait = pdMS_TO_TICKS(0);
uint8_t str[32] = {0};
uint32_t len;
uint32_t peroid_div2 = 0;
while(1)
{
len = uart_recv(str, 32);
if(len != 1)
{
continue;
}
switch(str[0])
{
case 0x31:
{
peroid_div2 = 100;
}
break;
case 0x32:
{
peroid_div2 = 500;
}
break;
case 0x33:
{
peroid_div2 = 2000;
}
break;
default:
{
LED0_OFF;
LED1_OFF;
xTimerStop(g_timer, tick_wait);
xTimerStop(g_timer_ont_shot, tick_wait);
uart_send("Turn off the backlight manually!\n", 0);
continue;
}
}
LED0_ON;
uart_send("Turn on the backlight manually!\n", 0);
xTimerReset(g_timer_ont_shot, tick_wait);
xTimerChangePeriod(g_timer, pdMS_TO_TICKS(peroid_div2), tick_wait);
}
}
void demo_03(void)
{
BaseType_t st_timer, st_timer_one_shot;
g_timer = xTimerCreate("blinkled", pdMS_TO_TICKS(500), pdTRUE, 0, timer_callback);
g_timer_ont_shot = xTimerCreate("backlight", pdMS_TO_TICKS(5000), pdFALSE, 0, timer_callback);
if(g_timer != NULL && g_timer_ont_shot != NULL)
{
st_timer = xTimerStart(g_timer, 0);
st_timer_one_shot = xTimerStart(g_timer_ont_shot, 0);
if(st_timer != pdFALSE && st_timer_one_shot != pdFALSE)
{
LED0_ON;
uart_send("The backlight is on!\n", 0);
xTaskCreate(uart_parse, "uart", 128, NULL, 1, NULL);
vTaskStartScheduler();
}
}
while(1)
{
;
}
}
static void timer_callback(xTimerHandle timer)
{
if(timer == g_timer)
{
LED1_FLIP;
}
else if(timer == g_timer_ont_shot)
{
LED0_OFF;
uart_send("The backlight turn off automatically!\n", 0);
}
else
{
uart_send("It won't get here\n", 0);
}
}
SemaphoreHandle_t xSemaphoreCreateMutex( void );
BaseType_t xSemaphoreTake( SemaphoreHandle_t xSemaphore, TickType_t xTicksToWait );
BaseType_t xSemaphoreGive( SemaphoreHandle_t xSemaphore );
#include "demo.h"
static SemaphoreHandle_t g_mutex;
static void task_uart(void *param)
{
char *string = (char *)param;
uart_init();
TickType_t xLastWakeTime;
const TickType_t delay_ms = pdMS_TO_TICKS(20);
while(1)
{
xSemaphoreTake(g_mutex, portMAX_DELAY);
uart_send((uint8_t *)string, strlen(string));
xSemaphoreGive(g_mutex);
vTaskDelayUntil(&xLastWakeTime, delay_ms);
}
}
void demo_04(void)
{
g_mutex = xSemaphoreCreateMutex();
if(g_mutex != NULL)
{
xTaskCreate(task_uart, "uart1", 128, ""
"----------------------------------------------------"
"----------------------------------------------------"
"----------------------------------------------------"
"----------------------------------------------------"
"\n", 1, NULL);
xTaskCreate(task_uart, "uart2", 128, "uart2 is running!\n", 2, NULL);
vTaskStartScheduler();
}
while(1)
{
;
}
}