MPLAB® Harmony v3 Drivers and System Services on SAM E70/S70/V70/V71: Step 5
Add Application Code to the Project
The application is already developed and is available in the files main_e70.c, app_sensor.c, app_eeprom.c, and usart_common.c under <your unzip folder>/getting_started_drv/dev_files/sam_e70_xult. The application files app_sensor.c and app_eeprom.c contain the application logic. They also contain placeholders that you will populate with the necessary code in the next step.
- Go to the getting_started_drv/dev_files/sam_e70_xult folder and copy the pre-developed files main_e70.c, app_sensor.c, app_eeprom.c, usart_common.c, app_sensor.h, app_eeprom.h, and usart_common.h.
- Paste and replace (overwrite) the files of your project available at <your project folder>/getting_started_drv/firmware/src with the copied files.
- Add the application file usart_common.c to your project.
For an MPLAB® Harmony v3 application, MPLAB Code Configurator (MCC) generates the application template files (app_sensor.c, app_sensor.h, app_eeprom.c, app_eeprom.h, and main_e70.c). The main_e70.c file calls the SYS_Tasks() routine which runs the sensor and the Electrically Erasable Programmable Read-Only Memory (EEPROM) application tasks (and driver/middleware tasks if added to the project).
The app_sensor.h and app_eeprom.h files define the states of the application. Try to relate the states with the state diagram shown at the beginning of this lab.
The app_sensor.h and app_eeprom.h files also define the application data structure APP_SENSOR_DATA and APP_EEPROM_DATA.
Open app_sensor.c and add application code by following the steps below.
Open the I²C driver instance 0 (which is associated with TWIHS0). The call to DRV_I2C_Open() Application Programming Interface (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 (related to the sensor client) to the driver.
Set the transfer parameters for the sensor 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.
Register an event handler (callback) with the I²C driver for the sensor client. The event handler is called by the I²C driver when any request submitted by the sensor application client is completed.
APP_SENSOR_I2CEventHandler, 0);
Register a periodic callback with the Timer System Service for every 1000 milliseconds.
(1000*APP_SENSOR_SAMPLING_RATE_IN_HZ), SYS_TIME_PERIODIC);
Open the Universal Synchronous Asynchronous Receiver Transmitter (USART) driver instance 0 (which is associated with USART1). 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 application (by sensor task and EEPROM task) in all the subsequent calls to the driver.
Register an event handler (callback) with the USART driver. The event handler is called by the USART driver when any request submitted by the sensor or EEPROM application tasks is completed.
Set a flag in the periodic timer event handler. The sensor task will read the temperature when the flag is set and also toggle an LED.
LED_Toggle();
When the periodic timer expires, submit an I²C transfer to read the temperature sensor value using the I²C driver write-then-read Application Programming Interface (API). The I²C driver calls back the sensor event handler (registered in step 6.3 above) when the submitted request is complete.
APP_SENSOR_I2C_SLAVE_ADDR,
(void*)app_sensorData.i2cTxBuffer, 1,
(void *) app_sensorData.i2cRxBuffer, 2,
&app_sensorData.transferHandle );
In the I²C event handler for sensor client, set a flag to indicate that the I²C read (temperature sensor value) is completed.
Print the latest temperature value and notify the EEPROM application task to log the temperature value to EEPROM.
(int8_t *)"Temperature = %d F\r\n",
app_sensorData.temperature);
APP_EEPROM_SetTempWriteRequest(app_sensorData.temperature);
Open app_eeprom.c and add the application code by following the below steps.
Associate the second client (the EEPROM 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 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 client) to the driver.
Like the sensor client, set up the transfer parameters for the EEPROM client after a valid handle to the driver is obtained. The transfer parameters set the I²C clock speed to 400 kHz for this client.
Like the sensor client, register an event handler (callback) with the I²C driver for the EEPROM client. The event handler would be called by the I²C driver when any request submitted by the EEPROM application client is completed.
Submit a USART read request to receive a character on the serial terminal. When the user enters a character, a USART interrupt occurs. In the USART event handler the application sets the usartReadRequest flag to true.
When the user enters a character on the serial terminal, submit an I²C transfer to read back the written temperature sensor values from the EEPROM, using the write-then-read API. The I²C driver calls back the EEPROM event handler (APP_EEPROM_I2CEventHandler) when the submitted request is complete.
APP_EEPROM_I2C_SLAVE_ADDR, app_eepromData.i2cTxBuffer,
1, app_eepromData.i2cRxBuffer, 5, &app_eepromData.transferHandle);
In the I²C event handler of the EEPROM client, update the status to indicate that the I²C read/write (corresponding to EEPROM read/write) is completed.
Print the temperature values read from the EEPROM and submit a USART read request to receive subsequent character on the serial terminal.
DRV_USART_ReadBufferAdd(usartHandle, (void*)app_eepromData.usartRxBuffer, 1, &usartReadBufferHandle);
You are now ready to build the code!