Microchip® Advanced Software Framework

 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
FreeRTOS demo Co-routines

The demo Co-routines 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 Co-routines, see:

The demo Co-routines 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 demo_co_routines_init (void)
 Initialize Co-routines and resources for demo. More...
 
void vApplicationIdleHook (void)
 This idle task hook will schedule a co-routine each time it is called. More...
 

Co-routines configuration

#define UART_CO_ROUTINE_PRIORITY   (configMAX_CO_ROUTINE_PRIORITIES - 1)
 
#define MAIN_CO_ROUTINE_PRIORITY   (configMAX_CO_ROUTINE_PRIORITIES - 2)
 
#define MAIN_CO_ROUTINE_DELAY   (100 / portTICK_RATE_MS)
 
#define GRAPH_CO_ROUTINE_PRIORITY   (configMAX_CO_ROUTINE_PRIORITIES - 3)
 
#define GRAPH_CO_ROUTINE_DELAY   (50 / portTICK_RATE_MS)
 
#define TERMINAL_CO_ROUTINE_PRIORITY   (configMAX_CO_ROUTINE_PRIORITIES - 3)
 
#define ABOUT_CO_ROUTINE_PRIORITY   (configMAX_CO_ROUTINE_PRIORITIES - 3)
 

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,
  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) - (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 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 Co-routine. More...
 
static gfx_coord_t x1 = 0
 x coord for graph Co-routine 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 QueueHandle_t terminal_in_queue
 Queue for incoming terminal characters. More...
 
static QueueHandle_t graph_co_routine_queue
 Queue for sync graph Co-routine. More...
 
static QueueHandle_t terminal_co_routine_queue
 Queue for sync terminal Co-routine. More...
 
static QueueHandle_t about_co_routine_queue
 Queue for sync about Co-routine. More...
 
static OLED1_CREATE_INSTANCE (oled1, OLED1_EXT_HEADER)
 Instance for OLED1 Xplained Pro LED and button driver. More...
 

Co-routines for demo

static void main_co_routine (CoRoutineHandle_t xHandle, UBaseType_t uxIndex)
 Main demo Co-routine. More...
 
static void graph_co_routine (CoRoutineHandle_t xHandle, UBaseType_t uxIndex)
 Graph Co-routine. More...
 
static void terminal_co_routine (CoRoutineHandle_t xHandle, UBaseType_t uxIndex)
 Terminal Co-routine. More...
 
static void about_co_routine (CoRoutineHandle_t xHandle, UBaseType_t uxIndex)
 About Co-routine. More...
 
static void uart_co_routine (CoRoutineHandle_t xHandle, UBaseType_t uxIndex)
 UART Co-routine. More...
 

#define ABOUT_CO_ROUTINE_PRIORITY   (configMAX_CO_ROUTINE_PRIORITIES - 3)

Referenced by demo_co_routines_init().

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

Height of area in which to draw content.

Referenced by about_co_routine(), graph_co_routine(), and main_co_routine().

#define CANVAS_WIDTH   (GFX_MONO_LCD_WIDTH)

Width of area in which to draw content.

Referenced by about_co_routine(), graph_co_routine(), and terminal_co_routine().

#define GRAPH_CO_ROUTINE_DELAY   (50 / portTICK_RATE_MS)

Referenced by graph_co_routine().

#define GRAPH_CO_ROUTINE_PRIORITY   (configMAX_CO_ROUTINE_PRIORITIES - 3)

Referenced by demo_co_routines_init().

#define MAIN_CO_ROUTINE_DELAY   (100 / portTICK_RATE_MS)

Referenced by main_co_routine().

#define MAIN_CO_ROUTINE_PRIORITY   (configMAX_CO_ROUTINE_PRIORITIES - 2)

Referenced by demo_co_routines_init().

#define MENU_HEIGHT   8

Height of menu bar.

Referenced by main_co_routine().

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

Width per menu item.

Referenced by main_co_routine().

#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_co_routine(), and uart_co_routine().

#define TERMINAL_CO_ROUTINE_PRIORITY   (configMAX_CO_ROUTINE_PRIORITIES - 3)

Referenced by demo_co_routines_init().

#define TERMINAL_COLUMNS   (CANVAS_WIDTH / SYSFONT_WIDTH)

Character columns on display.

Referenced by about_co_routine(), terminal_co_routine(), and uart_co_routine().

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

Character lines on display.

Referenced by terminal_co_routine().

#define UART_CO_ROUTINE_PRIORITY   (configMAX_CO_ROUTINE_PRIORITIES - 1)

Referenced by demo_co_routines_init().

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 
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_co_routine ( CoRoutineHandle_t  xHandle,
UBaseType_t  uxIndex 
)
static

About Co-routine.

This Co-routine prints a short text about the demo, with a simple zooming animation.

Parameters
xHandleType by which co-routines are referenced.
uxIndexParameter for the Co-routine.

References about_co_routine_queue, about_text, c, CANVAS_HEIGHT, CANVAS_WIDTH, crEND, crQUEUE_RECEIVE, crSTART, gfx_mono_draw_char(), i, OLED1_LED2_ID, oled1_set_led_state(), pdPASS, sysfont, and TERMINAL_COLUMNS.

Referenced by demo_co_routines_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 crQUEUE_SEND_FROM_ISR, data, pdFALSE, and terminal_in_queue.

Referenced by demo_co_routines_init().

static void graph_co_routine ( CoRoutineHandle_t  xHandle,
UBaseType_t  uxIndex 
)
static

Graph Co-routine.

This Co-routine 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
xHandleType by which co-routines are referenced.
uxIndexParameter for the Co-routine.

References CANVAS_HEIGHT, CANVAS_WIDTH, crDELAY, crEND, crQUEUE_RECEIVE, crSTART, gfx_mono_draw_line, gfx_mono_draw_pixel, gfx_mono_draw_vertical_line, GFX_PIXEL_CLR, GFX_PIXEL_SET, GRAPH_CO_ROUTINE_DELAY, graph_co_routine_queue, graph_noise, OLED1_LED1_ID, oled1_set_led_state(), pdPASS, and x1.

Referenced by demo_co_routines_init().

static void main_co_routine ( CoRoutineHandle_t  xHandle,
UBaseType_t  uxIndex 
)
static

Main demo Co-routine.

This Co-routine keeps track of which screen the user has selected, which 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
xHandleType by which co-routines are referenced.
uxIndexParameter for the Co-routine.

References about_co_routine_queue, Assert, CANVAS_HEIGHT, crDELAY, crEND, crQUEUE_SEND, crSTART, 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, graph_co_routine_queue, i, MAIN_CO_ROUTINE_DELAY, MENU_HEIGHT, MENU_ITEM_ABOUT, MENU_ITEM_GRAPH, MENU_ITEM_TERMINAL, MENU_ITEM_WIDTH, menu_items_text, MENU_NUM_ITEMS, OLED1_BUTTON1_ID, OLED1_BUTTON2_ID, OLED1_BUTTON3_ID, oled1_get_button_state(), OLED1_LED3_ID, oled1_set_led_state(), pdPASS, result, sysfont, terminal_co_routine_queue, and x1.

Referenced by demo_co_routines_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_co_routine ( CoRoutineHandle_t  xHandle,
UBaseType_t  uxIndex 
)
static

Terminal Co-routine.

This Co-routine prints the terminal text buffer to the display.

Parameters
xHandleType by which co-routines are referenced.
uxIndexParameter for the Co-routine.

References CANVAS_WIDTH, crEND, crQUEUE_RECEIVE, crSTART, gfx_mono_draw_char(), gfx_mono_draw_filled_rect, GFX_PIXEL_CLR, OLED1_LED2_ID, oled1_set_led_state(), pdPASS, sysfont, terminal_buffer, TERMINAL_BUFFER_LINES, terminal_co_routine_queue, TERMINAL_COLUMNS, terminal_line_offset, and TERMINAL_LINES.

Referenced by demo_co_routines_init().

static void uart_co_routine ( CoRoutineHandle_t  xHandle,
UBaseType_t  uxIndex 
)
static

UART Co-routine.

This Co-routine 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_co_routine().

Parameters
xHandleType by which co-routines are referenced.
uxIndexParameter for the Co-routine.

References crEND, crQUEUE_RECEIVE, crSTART, OLED1_LED1_ID, oled1_set_led_state(), pdPASS, terminal_buffer, TERMINAL_BUFFER_LINES, TERMINAL_COLUMNS, terminal_in_queue, and terminal_line_offset.

Referenced by demo_co_routines_init().

void vApplicationIdleHook ( void  )

This idle task hook will schedule a co-routine each time it is called.

References vCoRoutineSchedule().

Referenced by portTASK_FUNCTION().

QueueHandle_t about_co_routine_queue
static

Queue for sync about Co-routine.

Referenced by about_co_routine(), demo_co_routines_init(), and main_co_routine().

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_co_routine().

struct usart_module cdc_usart
static
QueueHandle_t graph_co_routine_queue
static

Queue for sync graph Co-routine.

Referenced by demo_co_routines_init(), graph_co_routine(), and main_co_routine().

uint8_t graph_noise = 128
static

Pseudo-random noise for graph Co-routine.

Referenced by graph_co_routine().

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

Labels for menu items.

Referenced by main_co_routine().

uint8_t terminal_buffer[TERMINAL_BUFFER_LINES][TERMINAL_BUFFER_COLUMNS]
static

Buffer for terminal text.

Referenced by terminal_co_routine(), and uart_co_routine().

QueueHandle_t terminal_co_routine_queue
static

Queue for sync terminal Co-routine.

Referenced by demo_co_routines_init(), main_co_routine(), and terminal_co_routine().

QueueHandle_t terminal_in_queue
static

Queue for incoming terminal characters.

Referenced by cdc_rx_handler(), demo_co_routines_init(), and uart_co_routine().

uint8_t terminal_line_offset
static

Index of latest terminal line (first to be printed)

Referenced by terminal_co_routine(), and uart_co_routine().