3. Analyzing The Demonstration Example
This section analyzes an application example which demonstrates using the SPI adapters. The example is based on the freertos_retarget sample code found in the SDK. It adds an additional freeRTOS task which is responsible for controlling an external SPI module, connected on SPI1 bus. It also enables the wake-up (WKUP) timer for handling external events. Both synchronous and asynchronous SPI operations are demonstrated.
3.1. Application Structure
The key goal of this demonstration is for the device to perform a few SPI operations following an event. For demonstration purposes the K1 button on the Pro DevKit has been configured as a wake-up input pin. For more information on how to configure and set a pin for handling external events, please read the External Interruption tutorial. At each external event, produced at every K1 button press, a dedicated callback function named
wkup_cb()is triggered. In this function, a freeRTOS signal named
signal_MCP4822is sent towards the
prvSPITask_MCP_4822application task to unblock its execution.
Depending on the value of the
SPI_ASYNC_ENmacro, a synchronous or an asynchronous SPI write operation is performed. A 2-byte data is sent over the SPI bus to update the analog output value of the MCP4822 DAC module. Following a successful write operation, the written data are printed on the serial console and the analog output value of the selected channel is randomly updated. Otherwise, a debugging message indicating the failure of the current operation is displayed on the serial console.
It is essential that developers must not call asynchronous related APIs without guaranteeing that the previous asynchronous transaction is finished. To ensure this, after triggering an asynchronous SPI write transaction, the calling task blocks its execution until the arrival of a freeRTOS signal, indicating the end of the current SPI operation.
The selected DAC module consists of a 2-byte register, in which the first four significant bits (MSBits) control the entire behavior of the module. The provided demonstration example exhibits a few macros that can be used for easily configuring the control as well as data bits of the DAC’s control register. For more information on the control bits, read the
Serial Interfacesection in the manufacturer datasheet.
The MCP4822 DAC module has been selected for demonstration purposes only. Providing complete drivers for this module is out of the scope of this tutorial.