Microchip® Advanced Software Framework

 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
FreeRTOS demo tasks

The demo tasks demonstrate basic use of FreeRTOS, with inter-task communication using queues and mutexes.

For details on how the demo works, see Introduction.

For detailed information on the tasks, see:

The demo tasks depend on the following drivers:

Functions

static void cdc_rx_handler (uint8_t instance)
 Interrupt handler for reception from EDBG Virtual COM Port. More...
 
void demotasks_init (void)
 Initialize tasks and resources for demo. More...
 

Variables

bool volatile tickless_enable
 Global variable to control tickless operation. More...
 

Task configuration

#define UART_TASK_PRIORITY   (tskIDLE_PRIORITY + 3)
 
#define UART_TASK_DELAY   (10 / portTICK_RATE_MS)
 
#define MAIN_TASK_PRIORITY   (tskIDLE_PRIORITY + 2)
 
#define MAIN_TASK_DELAY   (100 / portTICK_RATE_MS)
 
#define GRAPH_TASK_PRIORITY   (tskIDLE_PRIORITY + 1)
 
#define GRAPH_TASK_DELAY   (50 / portTICK_RATE_MS)
 
#define TERMINAL_TASK_PRIORITY   (tskIDLE_PRIORITY + 1)
 
#define TERMINAL_TASK_DELAY   (1000 / portTICK_RATE_MS)
 
#define ABOUT_TASK_PRIORITY   (tskIDLE_PRIORITY + 1)
 
#define ABOUT_TASK_DELAY   (33 / portTICK_RATE_MS)
 

Menu and display configuration

enum  menu_items {
  MENU_ITEM_GRAPH,
  MENU_ITEM_TERMINAL,
  MENU_ITEM_ABOUT,
  MENU_NUM_ITEMS,
  MENU_ITEM_GRAPH,
  MENU_ITEM_TERMINAL,
  MENU_ITEM_ABOUT,
  MENU_NUM_ITEMS
}
 Available selections in menu. More...
 
#define MENU_HEIGHT   8
 Height of menu bar. More...
 
#define MENU_ITEM_WIDTH   (((GFX_MONO_LCD_WIDTH - (MENU_NUM_ITEMS - 1))) / MENU_NUM_ITEMS)
 Width per menu item. More...
 
#define CANVAS_HEIGHT   ((GFX_MONO_LCD_HEIGHT / 2) - (MENU_HEIGHT + 1))
 Height of area in which to draw content. More...
 
#define CANVAS_WIDTH   (GFX_MONO_LCD_WIDTH)
 Width of area in which to draw content. More...
 
#define CANVAS_GRAPH_Y_OFFSET   (GFX_MONO_LCD_HEIGHT / 2)
 Offset of Y-coordinate for display buffer of graph. More...
 
#define TERMINAL_LINES   (1 + ((CANVAS_HEIGHT - SYSFONT_HEIGHT) / (SYSFONT_HEIGHT + 1)))
 Character lines on display. More...
 
#define TERMINAL_COLUMNS   (CANVAS_WIDTH / SYSFONT_WIDTH)
 Character columns on display. More...
 
#define TERMINAL_BUFFER_LINES   (1 + TERMINAL_LINES)
 Character lines in terminal buffer. More...
 
#define TERMINAL_BUFFER_COLUMNS   (1 + TERMINAL_COLUMNS)
 Character columns in terminal buffer. More...
 

Global constants and variables

static const char menu_items_text [MENU_NUM_ITEMS][6]
 Labels for menu items. More...
 
static const char about_text []
 Text to display on about screen when tickless operation is disabled. More...
 
static const char about_text_tickless []
 Text to display on about screen when tickless operation is enabled. More...
 
static struct usart_module cdc_usart
 Instance for Embedded Debugger (EDBG) Virtual COM Port driver. More...
 
static uint8_t graph_noise = 128
 Pseudo-random noise for graph task. More...
 
static uint8_t terminal_buffer [TERMINAL_BUFFER_LINES][TERMINAL_BUFFER_COLUMNS]
 Buffer for terminal text. More...
 
static uint8_t terminal_line_offset
 Index of latest terminal line (first to be printed) More...
 
static xQueueHandle terminal_in_queue
 Queue for incoming terminal characters. More...
 
static xSemaphoreHandle display_mutex
 Semaphore to signal busy display. More...
 
static xSemaphoreHandle terminal_mutex
 Semaphore to signal busy terminal buffer. More...
 
static xTaskHandle terminal_task_handle
 Handle for terminal output task. More...
 
static xTaskHandle about_task_handle
 Handle for about screen task. More...
 
static OLED1_CREATE_INSTANCE (oled1, OLED1_EXT_HEADER)
 Instance for OLED1 Xplained Pro LED and button driver. More...
 

Tasks for demo

static void main_task (void *params)
 Main demo task. More...
 
static void graph_task (void *params)
 Graph task. More...
 
static void terminal_task (void *params)
 Terminal task. More...
 
static void about_task (void *params)
 About task. More...
 
static void uart_task (void *params)
 UART task. More...
 

Task configuration

#define UART_TASK_PRIORITY   (tskIDLE_PRIORITY + 3)
 
#define UART_TASK_DELAY   (10 / portTICK_RATE_MS)
 
#define MAIN_TASK_PRIORITY   (tskIDLE_PRIORITY + 2)
 
#define MAIN_TASK_DELAY   (100 / portTICK_RATE_MS)
 
#define GRAPH_TASK_PRIORITY   (tskIDLE_PRIORITY + 1)
 
#define GRAPH_TASK_DELAY   (50 / portTICK_RATE_MS)
 
#define TERMINAL_TASK_PRIORITY   (tskIDLE_PRIORITY + 1)
 
#define TERMINAL_TASK_DELAY   (1000 / portTICK_RATE_MS)
 
#define ABOUT_TASK_PRIORITY   (tskIDLE_PRIORITY + 1)
 
#define ABOUT_TASK_DELAY   (33 / portTICK_RATE_MS)
 

Menu and display configuration

enum  menu_items {
  MENU_ITEM_GRAPH,
  MENU_ITEM_TERMINAL,
  MENU_ITEM_ABOUT,
  MENU_NUM_ITEMS,
  MENU_ITEM_GRAPH,
  MENU_ITEM_TERMINAL,
  MENU_ITEM_ABOUT,
  MENU_NUM_ITEMS
}
 Available selections in menu. More...
 
#define MENU_HEIGHT   8
 Height of menu bar. More...
 
#define MENU_ITEM_WIDTH   (((GFX_MONO_LCD_WIDTH - (MENU_NUM_ITEMS - 1))) / MENU_NUM_ITEMS)
 Width per menu item. More...
 
#define CANVAS_HEIGHT   ((GFX_MONO_LCD_HEIGHT / 2) - (MENU_HEIGHT + 1))
 Height of area in which to draw content. More...
 
#define CANVAS_WIDTH   (GFX_MONO_LCD_WIDTH)
 Width of area in which to draw content. More...
 
#define CANVAS_GRAPH_Y_OFFSET   (GFX_MONO_LCD_HEIGHT / 2)
 Offset of Y-coordinate for display buffer of graph. More...
 
#define TERMINAL_LINES   (1 + ((CANVAS_HEIGHT - SYSFONT_HEIGHT) / (SYSFONT_HEIGHT + 1)))
 Character lines on display. More...
 
#define TERMINAL_COLUMNS   (CANVAS_WIDTH / SYSFONT_WIDTH)
 Character columns on display. More...
 
#define TERMINAL_BUFFER_LINES   (1 + TERMINAL_LINES)
 Character lines in terminal buffer. More...
 
#define TERMINAL_BUFFER_COLUMNS   (1 + TERMINAL_COLUMNS)
 Character columns in terminal buffer. More...
 

Global constants and variables

static const char menu_items_text [MENU_NUM_ITEMS][6]
 Labels for menu items. More...
 
static const char about_text []
 Text to display on about screen. More...
 
static struct usart_module cdc_usart
 Instance for Embedded Debugger (EDBG) Virtual COM Port driver. More...
 
static uint8_t graph_noise = 128
 Pseudo-random noise for graph task. More...
 
static uint8_t terminal_buffer [TERMINAL_BUFFER_LINES][TERMINAL_BUFFER_COLUMNS]
 Buffer for terminal text. More...
 
static uint8_t terminal_line_offset
 Index of latest terminal line (first to be printed) More...
 
static xQueueHandle terminal_in_queue
 Queue for incoming terminal characters. More...
 
static xSemaphoreHandle display_mutex
 Semaphore to signal busy display. More...
 
static xSemaphoreHandle terminal_mutex
 Semaphore to signal busy terminal buffer. More...
 
static xTaskHandle terminal_task_handle
 Handle for terminal output task. More...
 
static xTaskHandle about_task_handle
 Handle for about screen task. More...
 

#define ABOUT_TASK_DELAY   (33 / portTICK_RATE_MS)

Referenced by about_task().

#define ABOUT_TASK_DELAY   (33 / portTICK_RATE_MS)

Referenced by about_task().

#define ABOUT_TASK_PRIORITY   (tskIDLE_PRIORITY + 1)

Referenced by demotasks_init().

#define ABOUT_TASK_PRIORITY   (tskIDLE_PRIORITY + 1)
#define CANVAS_GRAPH_Y_OFFSET   (GFX_MONO_LCD_HEIGHT / 2)

Offset of Y-coordinate for display buffer of graph.

Referenced by graph_task(), and main_task().

#define CANVAS_GRAPH_Y_OFFSET   (GFX_MONO_LCD_HEIGHT / 2)

Offset of Y-coordinate for display buffer of graph.

Referenced by graph_task(), and main_task().

#define CANVAS_HEIGHT   ((GFX_MONO_LCD_HEIGHT / 2) - (MENU_HEIGHT + 1))

Height of area in which to draw content.

Referenced by about_task(), graph_task(), and main_task().

#define CANVAS_HEIGHT   ((GFX_MONO_LCD_HEIGHT / 2) - (MENU_HEIGHT + 1))

Height of area in which to draw content.

Referenced by about_task(), graph_task(), and main_task().

#define CANVAS_WIDTH   (GFX_MONO_LCD_WIDTH)

Width of area in which to draw content.

Referenced by about_task(), graph_task(), and terminal_task().

#define CANVAS_WIDTH   (GFX_MONO_LCD_WIDTH)

Width of area in which to draw content.

Referenced by about_task(), graph_task(), and terminal_task().

#define GRAPH_TASK_DELAY   (50 / portTICK_RATE_MS)

Referenced by graph_task().

#define GRAPH_TASK_DELAY   (50 / portTICK_RATE_MS)

Referenced by graph_task().

#define GRAPH_TASK_PRIORITY   (tskIDLE_PRIORITY + 1)

Referenced by demotasks_init().

#define GRAPH_TASK_PRIORITY   (tskIDLE_PRIORITY + 1)
#define MAIN_TASK_DELAY   (100 / portTICK_RATE_MS)

Referenced by main_task().

#define MAIN_TASK_DELAY   (100 / portTICK_RATE_MS)

Referenced by main_task().

#define MAIN_TASK_PRIORITY   (tskIDLE_PRIORITY + 2)

Referenced by demotasks_init().

#define MAIN_TASK_PRIORITY   (tskIDLE_PRIORITY + 2)
#define MENU_HEIGHT   8

Height of menu bar.

Referenced by main_task().

#define MENU_HEIGHT   8

Height of menu bar.

Referenced by main_task().

#define MENU_ITEM_WIDTH   (((GFX_MONO_LCD_WIDTH - (MENU_NUM_ITEMS - 1))) / MENU_NUM_ITEMS)

Width per menu item.

Referenced by main_task().

#define MENU_ITEM_WIDTH   (((GFX_MONO_LCD_WIDTH - (MENU_NUM_ITEMS - 1))) / MENU_NUM_ITEMS)

Width per menu item.

Referenced by main_task().

#define TERMINAL_BUFFER_COLUMNS   (1 + TERMINAL_COLUMNS)

Character columns in terminal buffer.

#define TERMINAL_BUFFER_COLUMNS   (1 + TERMINAL_COLUMNS)

Character columns in terminal buffer.

#define TERMINAL_BUFFER_LINES   (1 + TERMINAL_LINES)

Character lines in terminal buffer.

Referenced by terminal_task(), and uart_task().

#define TERMINAL_BUFFER_LINES   (1 + TERMINAL_LINES)

Character lines in terminal buffer.

Referenced by terminal_task(), and uart_task().

#define TERMINAL_COLUMNS   (CANVAS_WIDTH / SYSFONT_WIDTH)

Character columns on display.

Referenced by about_task(), terminal_task(), and uart_task().

#define TERMINAL_COLUMNS   (CANVAS_WIDTH / SYSFONT_WIDTH)

Character columns on display.

Referenced by about_task(), terminal_task(), and uart_task().

#define TERMINAL_LINES   (1 + ((CANVAS_HEIGHT - SYSFONT_HEIGHT) / (SYSFONT_HEIGHT + 1)))

Character lines on display.

Referenced by terminal_task().

#define TERMINAL_LINES   (1 + ((CANVAS_HEIGHT - SYSFONT_HEIGHT) / (SYSFONT_HEIGHT + 1)))

Character lines on display.

Referenced by terminal_task().

#define TERMINAL_TASK_DELAY   (1000 / portTICK_RATE_MS)

Referenced by terminal_task().

#define TERMINAL_TASK_DELAY   (1000 / portTICK_RATE_MS)

Referenced by terminal_task().

#define TERMINAL_TASK_PRIORITY   (tskIDLE_PRIORITY + 1)
#define TERMINAL_TASK_PRIORITY   (tskIDLE_PRIORITY + 1)

Referenced by demotasks_init().

#define UART_TASK_DELAY   (10 / portTICK_RATE_MS)

Referenced by uart_task().

#define UART_TASK_DELAY   (10 / portTICK_RATE_MS)

Referenced by uart_task().

#define UART_TASK_PRIORITY   (tskIDLE_PRIORITY + 3)

Referenced by demotasks_init().

#define UART_TASK_PRIORITY   (tskIDLE_PRIORITY + 3)

enum menu_items

Available selections in menu.

Enumerator
MENU_ITEM_GRAPH 
MENU_ITEM_TERMINAL 
MENU_ITEM_ABOUT 
MENU_NUM_ITEMS 
MENU_ITEM_GRAPH 
MENU_ITEM_TERMINAL 
MENU_ITEM_ABOUT 
MENU_NUM_ITEMS 
enum menu_items

Available selections in menu.

Enumerator
MENU_ITEM_GRAPH 
MENU_ITEM_TERMINAL 
MENU_ITEM_ABOUT 
MENU_NUM_ITEMS 
MENU_ITEM_GRAPH 
MENU_ITEM_TERMINAL 
MENU_ITEM_ABOUT 
MENU_NUM_ITEMS 

static void about_task ( void *  params)
static

About task.

This task prints a short text about the demo, with a simple zooming animation.

Parameters
paramsParameters for the task. (Not used.)

References ABOUT_TASK_DELAY, about_text, about_text_tickless, CANVAS_HEIGHT, CANVAS_WIDTH, display_mutex, gfx_mono_draw_char(), i, NULL, OLED1_LED2_ID, oled1_set_led_state(), sysfont, TERMINAL_COLUMNS, tickless_enable, vTaskDelay(), vTaskSuspend(), xSemaphoreGive, and xSemaphoreTake.

Referenced by demotasks_init().

static void cdc_rx_handler ( uint8_t  instance)
static

Interrupt handler for reception from EDBG Virtual COM Port.

UART interrupt handler for reception on EDBG CDC UART.

This function is based on the interrupt handler of the SERCOM USART callback driver (_usart_interrupt_handler()). It has been modified to only handle the receive interrupt and to push the received data directly into the queue for terminal characters (terminal_in_queue), and echo the character back to the sender.

Parameters
instanceInstance number of SERCOM that generated interrupt.

References data, NULL, terminal_in_queue, and xQueueSendFromISR.

Referenced by demotasks_init().

static void graph_task ( void *  params)
static

Graph task.

This task runs in the background to draw a pseudo-random graph to a dedicated display buffer. If the user selects a different screen than the graph, it will continue to update even though it is not visible until the graph screen is selected again.

Parameters
paramsParameters for the task. (Not used.)

References CANVAS_GRAPH_Y_OFFSET, CANVAS_HEIGHT, CANVAS_WIDTH, display_mutex, gfx_mono_draw_line, gfx_mono_draw_pixel, gfx_mono_draw_vertical_line, GFX_PIXEL_CLR, GFX_PIXEL_SET, graph_noise, GRAPH_TASK_DELAY, OLED1_LED1_ID, oled1_set_led_state(), vTaskDelay(), xSemaphoreGive, and xSemaphoreTake.

Referenced by demotasks_init().

static void main_task ( void *  params)
static

Main demo task.

This task keeps track of which screen the user has selected, which tasks to resume/suspend to draw the selected screen, and also draws the menu bar.

The menu bar shows which screens the user can select by clicking the corresponding buttons on the OLED1 Xplained Pro:

Parameters
paramsParameters for the task. (Not used.)

References about_task_handle, CANVAS_GRAPH_Y_OFFSET, CANVAS_HEIGHT, display_mutex, gfx_mono_draw_filled_rect, gfx_mono_draw_horizontal_line, gfx_mono_draw_rect, gfx_mono_draw_string(), gfx_mono_draw_vertical_line, GFX_MONO_LCD_HEIGHT, GFX_MONO_LCD_WIDTH, GFX_PIXEL_CLR, GFX_PIXEL_SET, i, MAIN_TASK_DELAY, MENU_HEIGHT, MENU_ITEM_ABOUT, MENU_ITEM_GRAPH, MENU_ITEM_TERMINAL, MENU_ITEM_WIDTH, menu_items_text, MENU_NUM_ITEMS, NULL, OLED1_BUTTON1_ID, OLED1_BUTTON2_ID, OLED1_BUTTON3_ID, oled1_get_button_state(), OLED1_LED3_ID, oled1_set_led_state(), ssd1306_set_display_start_line_address(), sysfont, terminal_task_handle, vTaskDelay(), vTaskResume(), vTaskSuspend(), xSemaphoreGive, and xSemaphoreTake.

Referenced by demotasks_init().

static OLED1_CREATE_INSTANCE ( oled1  ,
OLED1_EXT_HEADER   
)
static

Instance for OLED1 Xplained Pro LED and button driver.

The extension header to use is configured with OLED1_EXT_HEADER.

static void terminal_task ( void *  params)
static
static void uart_task ( void *  params)
static

UART task.

This task runs in the background to handle the queued, incoming terminal characters and write them to the terminal text buffer. It does not print anything to the display – that is done by terminal_task().

Parameters
paramsParameters for the task. (Not used.)

References OLED1_LED1_ID, oled1_set_led_state(), terminal_buffer, TERMINAL_BUFFER_LINES, TERMINAL_COLUMNS, terminal_in_queue, terminal_line_offset, terminal_mutex, UART_TASK_DELAY, vTaskDelay(), xQueueReceive, xSemaphoreGive, and xSemaphoreTake.

Referenced by demotasks_init().

xTaskHandle about_task_handle
static

Handle for about screen task.

Referenced by main_task().

xTaskHandle about_task_handle
static

Handle for about screen task.

Referenced by demotasks_init(), and main_task().

const char about_text[]
static
Initial value:
=
"FreeRTOS "tskKERNEL_VERSION_NUMBER" demo."
" "
"Use CDC at 9.6 kBaud."
#define tskKERNEL_VERSION_NUMBER
Definition: freertos-7.5.2/Source/include/task.h:83

Text to display on about screen when tickless operation is disabled.

Referenced by about_task().

const char about_text[]
static
Initial value:
=
"FreeRTOS "tskKERNEL_VERSION_NUMBER" demo."
" "
"Use CDC at 9.6 kBaud."
#define tskKERNEL_VERSION_NUMBER
Definition: freertos-7.5.2/Source/include/task.h:83

Text to display on about screen.

Referenced by about_task().

const char about_text_tickless[]
static
Initial value:
=
"FreeRTOS "tskKERNEL_VERSION_NUMBER" demo."
" TickLess "
"Use CDC at 9.6 kBaud."
#define tskKERNEL_VERSION_NUMBER
Definition: freertos-7.5.2/Source/include/task.h:83

Text to display on about screen when tickless operation is enabled.

Referenced by about_task().

struct usart_module cdc_usart
static
struct usart_module cdc_usart
static
xSemaphoreHandle display_mutex
static

Semaphore to signal busy display.

Referenced by about_task(), graph_task(), main_task(), and terminal_task().

xSemaphoreHandle display_mutex
static

Semaphore to signal busy display.

Referenced by about_task(), demotasks_init(), graph_task(), main_task(), and terminal_task().

uint8_t graph_noise = 128
static

Pseudo-random noise for graph task.

Referenced by graph_task().

uint8_t graph_noise = 128
static

Pseudo-random noise for graph task.

Referenced by graph_task().

const char menu_items_text[MENU_NUM_ITEMS][6]
static
Initial value:
= {
"Graph",
"Term.",
"About",
}

Labels for menu items.

Referenced by main_task().

const char menu_items_text[MENU_NUM_ITEMS][6]
static
Initial value:
= {
"Graph",
"Term.",
"About",
}

Labels for menu items.

Referenced by main_task().

uint8_t terminal_buffer[TERMINAL_BUFFER_LINES][TERMINAL_BUFFER_COLUMNS]
static

Buffer for terminal text.

Referenced by terminal_task(), and uart_task().

uint8_t terminal_buffer[TERMINAL_BUFFER_LINES][TERMINAL_BUFFER_COLUMNS]
static

Buffer for terminal text.

Referenced by terminal_task(), and uart_task().

xQueueHandle terminal_in_queue
static

Queue for incoming terminal characters.

Referenced by cdc_rx_handler(), and uart_task().

xQueueHandle terminal_in_queue
static

Queue for incoming terminal characters.

Referenced by cdc_rx_handler(), demotasks_init(), and uart_task().

uint8_t terminal_line_offset
static

Index of latest terminal line (first to be printed)

Referenced by terminal_task(), and uart_task().

uint8_t terminal_line_offset
static

Index of latest terminal line (first to be printed)

Referenced by terminal_task(), and uart_task().

xSemaphoreHandle terminal_mutex
static

Semaphore to signal busy terminal buffer.

Referenced by terminal_task(), and uart_task().

xSemaphoreHandle terminal_mutex
static

Semaphore to signal busy terminal buffer.

Referenced by demotasks_init(), terminal_task(), and uart_task().

xTaskHandle terminal_task_handle
static

Handle for terminal output task.

Referenced by main_task().

xTaskHandle terminal_task_handle
static

Handle for terminal output task.

Referenced by demotasks_init(), and main_task().

bool volatile tickless_enable

Global variable to control tickless operation.

Referenced by about_task(), and vPortSuppressTicksAndSleep().