Add Application Code to the Project
The application is already developed and is available in the following files:
- common.h
- app_eeprom.h
- app_eeprom.c
- app_sensor.h
- app_sensor.c
- app_usb.h
- app_usb.c
- main.c
They are available under <your unzipped folder path>\getting_started_drivers_middleware\dev_files
The application files app_eeprom.c, app_sensor.c, and app_usb.c contain the application logic. They also contain placeholders that you will populate with the necessary code.
1
Go to the <your unzipped folder path>\getting_started_drivers_middleware\dev_files folder and copy the pre-developed files:
- common.h
- app_eeprom.h
- app_eeprom.c
- app_sensor.h
- app_sensor.c
- app_usb.h
- app_usb.c
- main.c
Paste and replace (overwrite) the files of your project at <your harmony 3 project folder path>\getting_started_drivers_middleware\firmware\src with the copied files.
3
For a Harmony v3 application, MPLAB® Code Configurator (MCC) generates the application template files app_eeprom.h, app_eeprom.c, app_sensor.h, app_sensor.c, app_usb.h, app_usb.c, main.c. The main.c file calls the SYS_Tasks() routine which runs the Sensor, EEPROM, and USB application tasks. It also calls the requisite Driver, System Service, and Middleware tasks if they're added to the project using MCC.
6
The functions APP_SENSOR_Tasks(), APP_EEPROM_Tasks(), and APP_USB_Tasks() in the files app_sensor.h, app_eeprom.h, and app_usb.h implement the application state machine.
8
Open app_sensor.c and add the application code as shown in the following steps.
a
Open the I²C Driver Instance 0 (which is associated with TWIHS0 Peripheral Library). 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 Sensor Task in all subsequent calls to the driver.
Line numbers are provided beside each code snippet. If you don't see line numbers in MPLAB X IDE, enable them by navigating to View > Show Line Numbers from the menu bar.
You can use Ctrl + G to jump to a line number on the editor.
9
Open app_eeprom.c and add the application code as shown in the following steps.
a
Associate the second I²C 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 then be used by the EEPROM Task in all subsequent calls to the driver.
b
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.
- The call to DRV_I2C_TransferSetup() overrides the baud rate set in the I²C driver configuration using MCC.
- I²C was configured to run at 400 kHz using MCC. While in the application, the Sensor Task has reconfigured it to run at 100 kHz and the EEPROM Task configured it to run at 400 kHz. This illustrates how the Harmony I²C driver handles the peripheral's configuration depending on the client accessing the peripheral.
e
The COMMON_APP_EEPROM_GetTemperature() function declared in common.h and defined in app_eeprom.c provides a common interface between tasks and is used in app_usb.c to fetch the last five temperature values from the EEPROM. When this is called, submit an I²C transfer request to read the last five temperature values from the EEPROM using the I²C driver write-then-read API. Once this completes, store the temperature values in the requisite buffer.
10
Open app_usb.c and add the application code as shown in the following steps.
c
Once the USB device is plugged in and the enumeration is complete, the previously configured callback will be called by the USB device layer. Set a flag to notify the state machine that the USB device is now configured and turn on the SAME70 Xplained board LED to show the same.
The LED on the SAM E70 Xplained Pro is active low, so, clear the pin to turn it on.
You are now ready to build the code!