MPLAB® Harmony v3 Drivers on SAM C21 Using FreeRTOS™: Step 5
Add Application Code to the Project
The application is already developed and is available in the following files:
- app_sensor_thread.c,
- app_sensor_thread.h,
- app_eeprom_thread.c,
- app_eeprom_thread.h,
- app_user_input_thread.c, and
- app_user_input_thread.h.
They are available under:
<your unzip folder>/samc21_getting_started_freertos/dev_files/sam_c21_xpro.
The application files app_sensor_thread.c, app_eeprom_thread.c, and app_user_input_thread.c contain the application logic. They also contain placeholders that you will populate with the necessary code in the next step.
Go to the samc21_getting_started_freertos/dev_files/sam_c21_xpro folder and copy the pre-developed files:
- app_sensor_thread.c,
- app_sensor_thread.h,
- app_eeprom_thread.c,
- app_eeprom_thread.h,
- app_user_input_thread.c, and
- app_user_input_thread.h.
Paste and replace (overwrite) the files of your project available at <Your project folder>/samc21_getting_started_freertos/firmware/src with the copied files.
The main_c21.c file calls the SYS_Tasks() routine, which creates the sensor, EEPROM, and user input threads and makes a call to FreeRTOS Application Programming Interface (API) vTaskStartScheduler() to start the scheduler.
The app_sensor_thread.h, app_eeprom_thread.h, and app_user_input_thread.h files define the states and data structures of these application threads.
The sensor application thread in app_sensor_thread.c, EEPROM application thread in app_eeprom_thread.c, and user input application thread in app_user_input_thread.c act as the three clients to the synchronous Universal Synchronous Asynchronous Receiver Transmitter (USART) driver instance 0.
Open app_sensor_thread.c and add application code referring to the comments in the screenshots as shown in the following steps.
a. Open the I²C driver instance 0, which is associated with SERCOM2. The call to DRV_I2C_Open() API will associate the sensor client with the I²C driver instance 0. The returned handle will be used by the application in all the subsequent calls to the driver to read temperature values from the sensor.
b. Set the transfer parameters for the sensor application thread client after a valid handle to the driver is obtained. The transfer parameter sets the I²C clock speed to 100 kHz for this client.
c. Open the USART synchronous driver instance 0 (which is associated with SERCOM4). The call to DRV_USART_Open() API will return a handle to the USART driver instance 0. The returned handle will be used by the sensor application thread in all the subsequent calls to the driver to print the temperature values on the serial terminal for this client.
d. Submit a call to the FreeRTOS API vTaskDelay() to yield CPU control to other threads in the application for a duration equal to the temperature sampling time of the application (one second).
e. Submit a blocking I²C request to read temperature from the sensor. The calling thread will be blocked until the request is completed thereby allowing other threads to run.
f. Communicate the temperature ready event and the temperature value to the EEPROM thread using the RTOS queue. The EEPROM task unblocks when an event/data is available in the RTOS queue.
g. Print the latest temperature value by making a call to DRV_USART_WriteBuffer() API, which submits a blocking USART write request.
Open app_eeprom_thread.c and add application code referring to the comments in the screenshots as shown in the following steps.
a. Associate the second client (EEPROM application thread client), with the I²C driver instance 0. This is done by opening the I²C driver instance 0 again. The call to DRV_I2C_Open () API will now associate the EEPROM application thread client with the I²C driver instance 0. The returned handle will be used by the application in all the subsequent calls (related to the EEPROM application thread client) to the driver to write/read temperature values to/from EEPROM.
b. Like the sensor application thread client, set up the transfer parameters for the EEPROM application thread client after a valid handle to the driver is obtained. The transfer parameters set the I²C clock speed to 100 kHz for this client.
c. Associate second client (EEPROM application thread client) to USART synchronous driver instance 0 (which is associated with SERCOM4). The call to DRV_USART_Open() API will return a handle to the USART driver instance 0. The returned handle will be used by the EEPROM application thread in all the subsequent calls to the driver to print the temperature values from the EEPROM on the serial terminal.
d. Block the EEPROM application thread until a request is available in the RTOS queue. The scheduler will unblock the EEPROM thread when an event/data is available in the RTOS queue. Depending on the event received in the RTOS queue, the EEPROM task either writes or reads the temperature value from the EEPROM.
e. Submit blocking I²C requests to write temperature from the sensor to EEPROM. The calling thread will be blocked until the request is completed thereby allowing other threads to run.
{
/* Check if EEPROM has completed the write operation */
while (false == DRV_I2C_WriteTransfer(app_eepromData.i2cHandle, APP_EEPROM_I2C_SLAVE_ADDR, (void *)&dummyData, 1));
}
f. Submit blocking I²C request to read the temperature from the EEPROM to submit for printing on the serial terminal. The calling thread will be blocked until the request is completed thereby allowing other threads to run.
APP_EEPROM_I2C_SLAVE_ADDR, app_eepromData.i2cTxBuffer, 1,\
app_eepromData.i2cRxBuffer, 5))
g. Print the logged temperature value from EEPROM by making a call to DRV_USART_WriteBuffer() API by calling the EPPROM application thread function APP_EEPROM_PrintTemperature(), which submits a blocking USART write request.
Open app_user_input_thread.c and add application code referring to the comments in the screenshots as shown in the following steps.
a. Associate the third client (user input application thread client) to USART synchronous driver instance 0 (which is associated with SERCOM4). The call to DRV_USART_Open() API will return a handle to the USART driver instance 0. The returned handle will be used by the user input application thread in all the subsequent calls to the driver to read a user keypress on the serial terminal.
b. Submit a blocking USART read request. The user input thread will be unblocked when a character is received on the USART. After unblocking, it communicates the request to read data from EEPROM to the EEPROM thread via the RTOS queue.
You have used FreeRTOS to implement your application. You are now ready to build and run your RTOS-based application!