Link Search Menu Expand Document

MCHP

FreeRTOS basic

This example application demonstrates context switching between four tasks of different priorites. Two tasks run periodically while the other two tasks are event driven.

Description

This demonstration creates four tasks each of differen priority. Task1 has the lowest priority, followed by Task2, Task3 and Task4 which has the highest priority. Task1 and Task2 run periodically.

Task3 blocks until a character is received on UART terminal. Task3 registers a read callback with the UART PLIB and blcoks on a UART receive semaphore. The semaphore is given from the registered callback which is called when a character is receved on the terminal.

Task4 blocks until a user switch is pressed. Task4 registers a change notification callback for the switch press event with the GPIO peripheral and then blocks on the switch press semaphore. The semaphore is given from the registered callback which is called when the switch is pressed.

All the tasks print messages on the UART terminal to indicate tasks entry/exit. Since all the tasks use the same UART peripheral library, a mutex is used to guard the shared resource (UART ring buffer).

  • Task1: Task1 is configured for priority 1 (lowest of all the application tasks). The task1 runs for about 100 ticks and then blocks for 10 milliseconds. Each time task1 is run, it prints a message on the UART console “Tsk1-P1 <-“ where, the <- symbol indicates that task1 is running. Just before blocking it prints another message on the UART console “Tsk1-P1 ->” where, the -> symbol indicates that task1 is about to put itself into a blocked state.
  • Task2: Task2 is configured for priority 2. The task2 runs for about 10 ticks and then blocks for 250 milliseconds. Each time task2 is run, it prints a message on the UART console “Tsk2-P2 <-“ where, the <- symbol indicates that task2 is running. Just before blocking it prints another message on the UART console “Tsk2-P2 ->” where, the -> symbol indicates that task2 is about to put itself into a blocked state.
  • Task3: Task3 is configured for priority 3. It remains blocked on a semaphore which is released when a character is entered on the UART console. Once active, the task reads out the received characters and toggles LED if letter ‘L’ or ‘l’ is pressed. The task then runs for about 50 ticks, before again blocking itself until a new character is received. Each time task3 is run, it prints a message on the UART console “Tsk3-P3 <-“ where, the <- symbol indicates that task2 is running. Just before blocking it prints another message on the UART console “Tsk3-P3 ->” where, the -> symbol indicates that task3 is about to put itself into a blocked state.
  • Task4: Task4 is configured for priority 4 (highest of all the application tasks). It remains blocked on a semaphore which is released when a user switch is pressed. Once active, the task runs for about 10 ticks, before again blocking itself until the switch is pressed again. Each time task4 is run, it prints a message on the UART console “Tsk4-P4 <-“ where, the <- symbol indicates that task4 is running. Just before blocking it prints another message on the UART console “Tsk4-P4 ->” where, the -> symbol indicates that task4 is about to put itself into a blocked state.

Note: Task4 is not created for [PIC32MK MCJ Curiosity Pro Board](https://www.microchip.com/en-us/development-tool/DT100113)

The following figure shows possible context switching between the tasks.

output

Downloading and building the application

To clone or download this application from Github, go to the main page of this repository and then click Clone button to clone this repository or download as zip file. This content can also be downloaded using content manager by following these instructions.

Path of the application within the repository is apps/rtos/freertos/basic_freertos/firmware .

To build the application, refer to the following table and open the project using its IDE.

Project Name Description
pic32mk_gp_db.X MPLABX project for PIC32MK GP Development Kit
pic32mk_mcj_curiosity_pro.X MPLABX project for PIC32MK MCJ Curiosity Pro Board
pic32mk_mcm_curiosity_pro.X MPLABX project for PIC32MK MCM Curiosity Pro Board
   

Setting up the hardware

The following table shows the target hardware for the application projects.

Project Name Board
pic32mk_gp_db.X PIC32MK GP Development Kit
pic32mk_mcj_curiosity_pro.X [PIC32MK MCJ Curiosity Pro Board](https://www.microchip.com/en-us/development-tool/DT100113)
pic32mk_mcm_curiosity_pro.X PIC32MK MCM Curiosity Pro Board
   

Setting up PIC32MK GP Development Kit

  • Connect micro USB cable to the ‘Debug USB’ connector(J12) on the board to the computer
  • Connect a micro USB cable to the USART-USB port J25. This will enumerate as a COM port on the PC

Setting up [PIC32MK MCJ Curiosity Pro Board](https://www.microchip.com/en-us/development-tool/DT100113)

  • Connect micro USB cable to the ‘Debug USB’ connector(J1) on the board to the computer. This will enumerate as a COM port on the PC

Setting up PIC32MK MCM Curiosity Pro Board

  • Connect a micro USB cable to the USB DEBUG port J500
  • Connect a micro USB cable to the USB to UART port J400. This will enumerate as a COM port on the PC

Running the Application

  1. Open the Terminal application (Ex.:Tera Term) on the computer.
  2. Configure the serial port settings as follows:

    Baud : 115200

    Data : 8 Bits

    Parity : None

    Stop : 1 Bit

    Flow Control : None

  3. Build and program the application using its IDE.
  4. Observe the following output on the terminal.

    output

  5. Enter any character on the terminal to run task3. Notice how task1 and task2 are pre-empted by task3 as task3 is of higher priority than task1 and task2. Pressing character ‘l’ or “L’ toggles the on board LED. Notice how task1 and task2 are not run when characters are entered continuously on the terminal.

    output

    output

  6. Press the user switch on the board to run task4. Notice how task4 preempts all other tasks as it is of highest priority.

    output

Refer to the following table for LED and Switch name:



Copyright © 2020 Microchip Technology.