It this lesson we do a simple FreeRTOS based project for PSoC6. You don't need any prior experiences with RTOS or even BareMetal programming.
This project is kind of a blinking LED project, but you can change the blinking time by sending numbers from the PC to MCU via UART.
- (A) Blinking first LED
- (B) Blinking second LED
- (C) Changing Blinking time
(A1) Create an Empty App and name it "lsn0BlinkedFreeRTOS"
(A2) Quick Panel -> Library Manager -> Add Library
(A2.1) Core -> freertos
(A2.2) Peripheral -> retarget-io(A2.3) OK and then Update
(A3) Add FREERTOS and RTOS_AWARE to in the Makefile
COMPONENTS=FREERTOS RTOS_AWARE
(A4) Copy the FreeRTOSConfig.h file from the following directory into the root directory of the lsn0BlinkedFreeRTOS project:../mtb_shared/freertos/release-x/Source/portable/COMPONENT_CM4(A4.1) Remove the #warring line from FreeRTOSConfig.h
#warning This is a template. Copy this file to your project and remove this line. Refer to FreeRTOS README.md for usage details.
(A5) In the main.c file, add the required header files:
#include "FreeRTOS.h"
#include "task.h"
#include "cy_retarget_io.h"
(A6) Initialize retarget-io and CYBSP_USER_LED and CYBSP_USER_LED2
result = cyhal_gpio_init(CYBSP_USER_LED2, CYHAL_GPIO_DIR_OUTPUT, CYHAL_GPIO_DRIVE_STRONG, CYBSP_LED_STATE_OFF);
result = cyhal_gpio_init(CYBSP_USER_LED, CYHAL_GPIO_DIR_OUTPUT, CYHAL_GPIO_DRIVE_STRONG, CYBSP_LED_STATE_OFF);
result = cy_retarget_io_init(P5_1, P5_0, 115200);
(A7) Write a function to blink CYBSP_USER_LED every 100ms.
void blinkLED(void *arg)
{
(void)arg;
for(;;)
{
cyhal_gpio_toggle(CYBSP_USER_LED);
printf("LED is blinking ... \r\n");
vTaskDelay(myDelay);
}
}
(A8) Create a task for blinkLED function
xTaskCreate(blinkLED, "blink task", 1024, NULL, 5, NULL);
(A9) Call the task start Scheduler
vTaskStartScheduler();
(A10) Run the application and see the LED will blink every 100ms.
(B1) Create another function to blink CYBSP_USER_LED2
void blinkLED2(void *arg)
{
(void)arg;
for(;;)
{
cyhal_gpio_toggle(CYBSP_USER_LED2);
printf("LED2 is blinking ... \r\n");
vTaskDelay(500);
}
}
(B2) Create another task for blinkLED2 function
xTaskCreate(blinkLED2, "blink task 2", 1024, NULL, 5, NULL);
(B3) Run the application and two LEDs will blink, one every 100ms and the other every 500ms
(C1) Create a function to read from UART the character you input by keyboard
void watchButtons(void *arg){
uint8_t read_data;
for(;;){
printf("\n");
if (CY_RSLT_SUCCESS == cyhal_uart_getc(&cy_retarget_io_uart_obj, &read_data, 0))
{
//Alternate Connect
if(read_data == '0')
{
printf("Blinking time is 100\n");
myDelay = 100;
}
// Default Connect
else if(read_data == '1')
{
printf("Blinking time is 250\n");
myDelay = 250;
}
else if(read_data == '2')
{
printf("Blinking time is 1000\n");
myDelay = 1000;
}
}
}
}
(C1) Create a task for above function
xTaskCreate(watchButtons, "watch_buttons_task", 1024, NULL, 4, NULL);
(C2) Run the application and then chage the blinking time of first LED by sending 0, 1, 2 to the board via UART.
This was a lesson-0 for RTOS just to have a clue what RTOS is. It's just the begging an RTOS will be explain thoroughly in next lessons.
Comments
Please log in or sign up to comment.