1.4 FreeRTOS Basic
This example application demonstrates context switching between four tasks of different priorities. Two tasks run periodically while the other two tasks are event driven.
Description
This demonstration creates four tasks each of different 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 blocks on a UART receive semaphore. The semaphore is given from the registered callback which is called when a character is received 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.
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 | Description |
---|---|
pic32mk_gp_db.X | PIC32MK GP Development Kit |
pic32mk_mcj_curiosity_pro.X | PIC32MK MCJ Curiosity Pro Board |
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
- 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
- Open the Terminal application (Ex.:Tera Term) on the computer.
- Configure the serial port settings as follows:
- Baud : 115200
- Data : 8 Bits
- Parity : None
- Stop : 1 Bit
- Flow Control : None
- Build and program the application using its IDE.
- Observe the following output on the terminal.
- 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.
- Press the user switch on the board to run task4. Notice how task4 preempts all other tasks as it is of highest priority.
Board | Switch Name | LED Name |
---|---|---|
PIC32MK GP Development Kit | S1 | LED1 |
PIC32MK MCJ Curiosity Pro Board | SW200 | LED1 |
PIC32MK MCM Curiosity Pro Board | SW1 | LED1 |