5. Peripheral Example Applications

The DA145xx Software Development Kit (SDK) includes a set of engineering examples, which demonstrate the use of the drivers provided with the SDK to access the main peripheral devices of DA145xx.

5.1. Introduction

The peripheral example applications demonstrate the peripheral connectivity capabilities of the DA145xx such as the interface with SPI Flash and I2C EEPROM memories as well as the use of chip peripherals such as timers, the quadrature decoder, and the ADC. If applicable, user interaction is done via a UART terminal.

The following examples are provided:

  • Blinky Example: How to initialize DA145xx and make a LED blinking

  • UART (UART1, UART2) Example: How to configure, initiate, and send to the UART interface

  • SPI Flash Memory Example: How to initiate, read, write and erase an SPI Flash memory

  • I2C EEPROM Example: How to initiate, read, write and erase an EEPROM memory

  • Quadrature Decoder Example: How to configure and read from the quadrature decoder peripheral. The Wakeup Timer setup to respond to GPIO activity is also demonstrated in this example

  • Systick Example: How to use the Systick timer to generate an interrupt periodically. A LED changes its state upon each interrupt

  • TIMER0 (PWM0, PWM1) Example: How to configure TIMER0 to produce PWM signals. A melody is produced on an externally connected buzzer

  • TIMER0 general Example: How to configure TIMER0 to count a specified amount of time and generate an interrupt. LED is blinking every interrupt

  • TIMER2 (PWM2, PWM3, PWM4) Example: How to configure TIMER2 to produce PWM signals. LEDs are changing light brightness in this example

  • Battery Example: How to read the battery indication level, with the ADC

5.2. Software Description

The peripheral example applications are available in directory target_apps\peripheral_examples\ of the DA145xx Software Development Kit.

The implementation of the drivers is available in directory sdk\platform\driver. To use the DA145xx peripheral drivers:

  • Add the driver’s source code file (e.g. spi\spi.c) to the project

  • Include the driver’s header file (e.g. spi\spi.h) whenever the driver’s API is needed

  • Add the driver folder path to the Include Paths (C/C++ tab of Keil Target Options)

Each of the example projects includes the following files:

main.c: Includes both the main and test functions

user_periph_setup.c: Includes the system initialization and GPIO configuration functions for a peripheral that is about to be presented

user_periph_setup.h: Defines the hardware configuration, such as GPIO assignment for a peripheral that is about to be presented

uart_utils.c, .h: Introduces functions to print byte, word, double word and string variables. It calls functions from the UART driver.

5.3. Getting Started

It is assumed that the user is familiar with the use of the DA145xx Development Kit as described in the related document.

Before you run a desired peripheral example, configure the DA145xx development board accordingly, which depends on the required hardware configuration. Each example contains a subsection that describes the GPIO assignment of the DA145xx development board.

Note

Unless it is explicitly mentioned, all the following peripheral examples run on the 376-18-B Hardware Development Kit (HDK).

5.4. Configuring the UART terminal port on a DA145xx HDK

The DA145xx UART2 port will be used to print messages to a connected UART terminal application (e.g. Real Term, Tera Term).

Table 9 HDK jumper configuration for UART2 terminal port (used by most applications)

Target chip

jumper configuration for UART2 TX pin

jumper configuration for UART2 RX pin

DA1458x

P0_4: Connect J1.17 to J1.18

P0_5: Connect J1.15 to J1.16

DA1453x

P0_6: Connect J1.17 to J2.27

P0_7: Connect J1.15 to J2.28

A UART terminal application (e.g. Real Term, Tera Term) should be used for user interaction with each peripheral example project. All examples use the following COM port settings:

  • Baud rate: 115200

  • Data: 8-bit

  • Parity: none

  • Stop: 1 bit

  • Flow control: none

The procedure of discovering the COM port number of a DA145xx HDK is described in subsequent paragraphs.

5.4.1. Connecting to a DA145xx HDK

When the DA145xx HDK is connected via USB with a Windows machine (e.g. laptop) a dual serial port device should be discovered in the Device Manager (Ports COM & LPT1). The user must select the serial port with the smaller number (Figure 38) to provide it to a terminal console application.

../_images/Figure_10.jpg

Figure 38 DA145xx HDK Virtual COM Port

5.5. Configuring the SWD Interface on a DA145xx HDK

The DA145xx SWD interface should be used to download the code to system RAM and debug the downloaded application.

Table 10 HDK SWD jumper configuration for all peripheral examples

Target chip

SW_CLK jumper configuration

SW_DIO jumper configuration

DA1458x

Connect J1.21 to J1.22

Connect J1.23 to J1.24

DA1453x

5.6. Blinky: Your First DA145xx Application Example

Blinky is a simple application example that demonstrates the basic initialization of DA145xx with which a LED starts to blink.

The Keil project is located in the <sdk_root_directory>\projects\target_apps\peripheral_examples\blinky.

5.6.1. Hardware Configuration

The SWD configuration described in section Section 5.5 is used to download the code to RAM.

Table 11 Blinky example HDK jumper configuration

Target chip

jumper configuration for LED pin

DA1458x

P1_0: Connect J8.3 to J8.4

DA1453x

P0_9: Connect J8.3 to J8.4

5.6.2. Running the Example

Once the user has built and loaded the example project to the HDK, the LED will start to blink and the following output Figure 39 from program will be visible in the console.

../_images/blinky.jpg

Figure 39 Blinky Demo

5.7. UART Example

The UART example demonstrates how to configure, initiate, and send-receive characters using the UART1 and UART2 ports in three different modes:

  • Blocking mode

  • Interrupt mode

  • DMA mode

The Keil project is located in the <sdk_root_directory>\projects\target_apps\peripheral_examples\uart.

5.7.1. Hardware Configuration

Connect the UART terminal as described in section Section 5.4.

The SWD configuration described in section Section 5.5 is used to download the code to RAM.

The UART1/2 ports use the following HDK jumper configuration.

Table 12 HDK jumper configuration for UART1/2 Example

Target chip

jumper configuration for UART1/2 TX pin

jumper configuration for UART1/2 RX pin

DA1458x

P0_4: Connect J1.17 to J1.18

P0_5: Connect J1.15 to J1.16

DA1453x

P0_6: Connect J1.17 to J2.27

P0_7: Connect J1.15 to J2.28

5.7.2. Running the Example

Once the user has built and loaded the example project to the HDK, the console will display the message shown in Figure 40, Figure 41, Figure 42, Figure 43.

  • The uart_print_example() function uses print_hword(), print_word() and printf_string() functions to print the message on the console.

  • The uart_send_blocking_example() - uart_receive_blocking_example() functions use the UART driver in blocking mode to send-receive messages.

  • The uart_send_interrupt_example() - uart_receive_interrupt_example() functions use the UART driver in interrupt mode to print messages on the console.

  • The uart_send_dma_example() - uart_receive_dma_example() functions use the UART driver in DMA mode to send-receive messages.

  • The last example is the uart_loopback_interrupt_example(), where every received character is echoed back to the sender.

../_images/uart1_send.jpg

Figure 40 UART1 Send

../_images/uart1_receive.jpg

Figure 41 UART1 Receive

../_images/uart2_send.jpg

Figure 42 UART2 Send

../_images/uart2_receive.jpg

Figure 43 UART2 Receive

5.8. SPI Flash Memory Example

The SPI Flash memory example demonstrates how to initiate, read, write and erase an SPI Flash memory with the SPI Flash driver.

The Keil project is located in the <sdk_root_directory>\projects\target_apps\peripheral_examples\spi\spi_flash.

5.8.1. Hardware Configuration

Connect the UART terminal as described in section Section 5.4.

The SWD configuration described in section Section 5.5 is used to download the code to RAM.

The SPI flash memory example uses the following HDK jumper configuration.

Table 13 SPI flash memory example HDK jumper configuration for DA1458x

GPIO

Function

jumper configuration

P0_0

SPI_CLK

Connect J1.5 to J1.6

P0_3

SPI_CS

Connect J1.7 to J1.8

P0_5

SPI_DI

Connect J1.9 to J1.10

P0_6

SPI_DO

Connect J1.3 to J1.4

Table 14 SPI flash memory example HDK jumper configuration for DA1453x

GPIO

Function

jumper configuration

P0_4

SPI_CLK

Connect J1.5 to J1.6

P0_1

SPI_CS

Connect J1.7 to J1.8

P0_3

SPI_DI

Connect J1.9 to J1.10

P0_0

SPI_DO

Connect J1.3 to J1.4

5.8.2. Running the Example

Once the user has built and loaded the example project to the HDK, a series of read and write operations will be performed on the SPI Flash memory as shown in Figure 44, Figure 45, Figure 46, Figure 47, Figure 48, Figure 49, Figure 50.

The user also has to enter the characteristics of the SPI Flash in the header file: <sdk_root_directory>\projects\target_apps\peripheral_examples\spi\spi_flash\include\user_periph_setup.h.

The predefined characteristics are:

#define SPI_MS_MODE             SPI_MS_MODE_MASTER
#define SPI_CP_MODE             SPI_CP_MODE_0
#define SPI_WSZ                 SPI_MODE_8BIT
#define SPI_CS                  SPI_CS_0

#define SPI_FLASH_DEV_SIZE      (256 * 1024)

The spi_test() function performs the following tests:

  1. The GPIO pins used for the SPI Flash and the SPI module are initialized.

  2. The SPI Flash device memory is erased. To erase a device on which protection has been activated, the full test must be run once.

  3. The contents of the SPI Flash are read and printed to the console.

  4. The JEDEC ID is read and the device is detected, if a corresponding entry is found in the supported device list.

  5. The Manufacturer/Device ID and the Unique ID are read, if the device is known to be supported.

  6. The SPI Flash device memory turned into low power mode.

  7. The SPI Flash device memory is written with spi_flash_page_program().

  8. The contents of the SPI Flash are read and printed to the console with spi_flash_read_data().

  9. The SPI Flash device memory is written with spi_flash_page_program_buffer().

  10. The contents of the SPI Flash are read and printed to the console with spi_flash_read_data_buffer().

  11. The SPI Flash device memory is written with spi_flash_page_program_dma().

  12. The contents of the SPI Flash are read and printed to the console with spi_flash_read_data_dma().

  13. The SPI Flash device memory turned into high performance mode.

  14. The SPI Flash device memory is written with spi_flash_page_program().

  15. The contents of the SPI Flash are read and printed to the console with spi_flash_read_data().

  16. The SPI Flash device memory is written with spi_flash_page_program_buffer().

  17. The contents of the SPI Flash are read and printed to the console with spi_flash_read_data_buffer().

  18. The SPI Flash device memory is written with spi_flash_page_program_dma().

  19. The contents of the SPI Flash are read and printed to the console with spi_flash_read_data_dma().

../_images/SPI_Flash_erase.jpg

Figure 44 SPI Flash Erase

../_images/SPI_Flash_read_write_in_low_power_mode.jpg

Figure 45 SPI Flash Read Write in Low Power Mode

../_images/SPI_Flash_read_write_buffer_in_low_power_mode.jpg

Figure 46 SPI Flash Read Write Buffer in Low Power Mode

../_images/SPI_Flash_read_write_dma_in_low_power_mode.jpg

Figure 47 SPI Flash Read Write DMA in Low Power Mode

../_images/SPI_Flash_read_write_in_high_performance_mode.jpg

Figure 48 SPI Flash Read Write in High Performance Mode

../_images/SPI_Flash_read_write_buffer_in_high_performance_mode.jpg

Figure 49 SPI Flash Read Write Buffer in High Performance Mode

../_images/SPI_Flash_read_write_dma_in_high_performance_mode.jpg

Figure 50 SPI Flash Read Write DMA in High Performance Mode

In case you use a supported external SPI Flash device other than W25X10, the results of the SPI memory protection features test may differ. The user can change the test procedure by editing the function spi_test. The SPI Flash driver API is described in detail in API documentation.

5.9. I2C EEPROM Example

The I2C EEPROM example demonstrates how to initiate, read, write and erase an I2C EEPROM memory.

There in no I2C EEPROM chip populated on the HDK. Therefore, this example will work if the user connects an external one.

The Keil project is located in the <sdk_root_directory>\projects\target_apps\peripheral_examples\i2c\i2c_eeprom.

5.9.1. Hardware Configuration

Connect the UART terminal as described in section Section 5.4.

The SWD configuration described in section Section 5.5 is used to download the code to RAM.

Table 15 HDK jumper configuration for I2C EEPROM example

Target chip

jumper configuration for SCL pin

jumper configuration for SDA pin

DA1458x

P2_3: Connect J2.24 to I2C EEPROM SCL

P2_1: Connect J2.22 to I2C EEPROM SDA

DA1453x

P0_3: Connect J2.24 to I2C EEPROM SCL

P0_1: Connect J2.22 to I2C EEPROM SDA

An external I2C EEPROM must be connected to the HDK using the pins shown in Table 15. If a different selection of GPIO pins is needed, the user should edit the I2C_GPIO_PORT, I2C_SCL_PIN and I2C_SDA_PORT, I2C_SDA_PIN defines in user_periph_setup.h.

5.9.2. Running the Example

Once the user has built and loaded the example project to the HDK, a series of read and writes operations will be performed on the I2C EEPROM, as shown in Figure 51, Figure 52.

The predefined I2C EEPROM characteristics are:

#define I2C_EEPROM_SIZE    0x20000              // EEPROM size in bytes
#define I2C_EEPROM_PAGE    256                  // EEPROM page size in bytes
#define I2C_SLAVE_ADDRESS  0x50                 // Set slave device address
#define I2C_SPEED_MODE     I2C_SPEED_FAST       // Speed mode: I2C_SPEED_STANDARD (100 kbits/s), I2C_SPEED_FAST (400 kbits/s)
#define I2C_ADDRESS_MODE   I2C_ADDRESSING_7B    // Addressing mode: {I2C_ADDRESSING_7B, I2C_ADDRESSING_10B}
#define I2C_ADDRESS_SIZE   I2C_2BYTES_ADDR      // Address width: {I2C_1BYTE_ADDR, I2C_2BYTES_ADDR, I2C_3BYTES_ADDR}

The i2c_test() function performs the following tests:

  1. Initializes the GPIO pins used for the I2C EEPROM and the I2C module.

  2. Writes 256 bytes of data to the I2C EEPROM.

  3. Reads the contents of the I2C EEPROM.

  4. Writes and reads bytes 0x5A, 0x6A, 0x7A and 0xFF at addresses 22, 0, 255 and 30 respectively.

  5. Reads the contents of the I2C EEPROM.

  6. Releases the configured GPIO pins and the I2C module.

This is shown in detail in Figure 51, Figure 52.

../_images/I2C_EEPROM_read_write.jpg

Figure 51 I2C EEPROM Read Write

../_images/I2C_EEPROM_verify.jpg

Figure 52 I2C EEPROM Verify

The user can change the test procedure by editing the function i2c_test(). The I2C EEPROM driver API is described in detail in API documentation.

5.10. Quadrature Decoder Example

The Quadrature decoder example demonstrates how to configure and read from the quadrature decoder peripheral. The Wakeup Timer setup for responding to GPIO activity is also demonstrated in this example.

There in no Quadrature encoder module populated on the HDK. Therefore, this example will work if the user connects an external one.

The Keil project is located in the <sdk_root_directory>\projects\target_apps\peripheral_examples\quadrature_decoder.

5.10.1. Hardware Configuration

Connect the UART terminal as described in section Section 5.4.

Note

The DA1453x example uses P08 as the UART2 TX, instead of P06. The P08 pin is the J2.29 (noted as P28 in J2 connector).

The SWD configuration described in section Section 5.5 is used to download the code to RAM.

The connection settings required by the Quadrature decoder example are shown in Table 16.

Table 16 HDK jumper configuration Quadrature decoder

Target chip

jumper configuration for CHX_A PIN

jumper configuration for CHX_B PIN

jumper configuration for WKUP1 PIN

jumper configuration for WKUP2 PIN

DA1458x

P0_0: Connect J2.5 to encoder

P0_1 : Connect J2.6 to encoder

P0_6 :Connect J19.1 to J19.2

P1_1: Connect J19.4 to J19.5

DA1453x

P0_6: Connect J2.27 to encoder

P0_7 : Connect J2.28 to encoder

P0_11: Connect J19.2 to J19.3

P0_1: Connect J2.22 to J19.4

  • The quadrature encoder CHX_A and CHX_B pins have to be connected to P0_0 and P0_1 for DA1458x and to P0_8 and P0_9 for DA1453x.

  • The common terminal of the quadrature encoder must be connected to ground.

  • The WKUP1_PIN is connected with the SW2 button on the HDK and WKUP2_PIN is connected to the SW3 button on the HDK.

5.10.2. Running the Example

Once the user has built and loaded the example project to the HDK, the console will display the screen shown in Figure 53.

../_images/Quadrature_decoder_example.jpg

Figure 53 Quadrature Decoder Example

If the rotary encoder is activated (e.g. the mouse wheel is scrolled and the rotary encoder is attached to a mouse) the quadrature decoder-wakeup timer interrupt will be triggered, and after each trigger the terminal screen will report the axes relative coordinates. In this configuration, only the X channel terminals are configured and connected, the console will display the screen shown in Figure 54.

../_images/Quadrature_decoder_isr_only_reports.jpg

Figure 54 Quadrature Decoder ISR-Only Reports

If at any time the SW3 button is pressed (the user should make sure that the correct jumper configuration for buttons SW3 and SW2 is selected, as described in Section 5.10.1), polling of the relative coordinates will be enabled. Then the terminal window will start polling the quadrature decoder driver (see Figure 55). If the quadrature decoder is activated, a mixture of ISR and polling generated reports are displayed (see Figure 56).

../_images/Quadrature_decoder_polling_only_reports.jpg

Figure 55 Quadrature Decoder Polling-Only Reports

../_images/Quadrature_decoder_polling_and_isr_reports.jpg

Figure 56 Quadrature Decoder Polling and ISR Reports

Click the SW3 button again to stop polling. To terminate the test, one can press SW2 at any time. The message “Quadrature Decoder Test terminated!” will be printed to the connected UART terminal.

If the count of events needs to be changed before an interrupt is triggered, the parameter QDEC_EVENTS_COUNT_TO_INT in user_periph_setup.h can be modified accordingly. In the same file, one can change the clock divisor of the quadrature decoder by altering the parameter QDEC_CLOCK_DIVIDER.

#define QDEC_CLOCK_DIVIDER                   (1)
#define QDEC_EVENTS_COUNT_TO_INT             (1)

5.11. Systick Example

The Systick example demonstrates how to use the systick timer to generate an interrupt periodically. LED is changing its state upon each interrupt.

The Keil project is located in the <sdk_root_directory>\projects\target_apps\peripheral_examples\systick.

5.11.1. Hardware Configuration

This example does not use the UART terminal.

The debug configuration described in section Section 5.5 is used to download the code to RAM.

Table 17 HDK jumper configuration for Systick example

Target chip

jumper configuration for LED pin

DA1458x

P1_0: Connect J8.3 to J8.4

DA1453x

P0_9: Connect J8.3 to J8.4

5.11.2. Running the Example

Once the user has built and loaded the example project to the HDK, the LED will start to blink every 1 second.

5.12. TIMER0 (PWM0, PWM1) Example

The TIMER0 (PWM0, PWM1) example demonstrates how to configure TIMER0 to produce PWM signals. A melody is produced on an externally connected buzzer.

The Keil project is located in the <sdk_root_directory>\projects\target_apps\peripheral_examples\timer0\timer0_pwm.

5.12.1. Hardware Configuration

Connect the UART terminal as described in section Section 5.4.

The SWD configuration described in section Section 5.5 is used to download the code to RAM.

Table 18 HDK jumper configuration for Timer0 PWM example

Target chip

jumper configuration for PWM0 pin

jumper configuration for PWM1 pin

DA1458x

P0_2: Connect buzzer between J2.40 and J2.7

P0_3: Connect buzzer between J2.40 and J2.8

DA1453x

P0_9: Connect buzzer between J2.40 and J2.30

P0_8: Connect buzzer between J2.40 and J2.29

5.12.2. Running the Example

Once the user has built and loaded the example project to the HDK, the PWM0 and PWM1 signals will become active and start producing an audible melody if a buzzer is connected to P0_2 - P0_3 for DA1458x targets or P0_9 - P0_8 for DA1453x target. While the melody is playing, stars are being drawn in the terminal window on each beat Figure 57.

../_images/timer0_pwm.jpg

Figure 57 Timer0 PWM

5.13. TIMER0 General Example

The TIMER0 general example demonstrates how to configure TIMER0 to count a specified amount of time and generate an interrupt. A LED is changing state upon each timer interrupt.

The Keil project is located in the <sdk_root_directory>\projects\target_apps\peripheral_examples\timer0\timer0_general.

5.13.1. Hardware Configuration

Connect the UART terminal as described in section Section 5.4.

Table 19 HDK jumper configuration for Timer0 example

Target chip

jumper configuration for LED pin

DA1458x

P1_0: Connect J8.3 to J8.4

DA1453x

P0_9: Connect J8.3 to J8.4

5.13.2. Running the Example

Once the user has built and loaded the example project to the HDK, the LED will be changing its state every second until the end of the test. The duration of the test (expressed in seconds) can be set by user and it will also be printed in the console. See Figure 58.

../_images/timer0_general.jpg

Figure 58 Timer0 General

5.14. TIMER2 (PWM2, PWM3, PWM4) Example

The TIMER2 (PWM2, PWM3, PWM4) example demonstrates how to configure TIMER2 to produce PWM signals. The PWM outputs are used to change the brightness of the LEDs in this example.

The Keil project is located in the <sdk_root_directory>\projects\target_apps\peripheral_examples\timer2\timer2_pwm.

5.14.1. Hardware Configuration

Connect the UART terminal as described in section Section 5.4.

The SWD configuration described in section Section 5.5 is used to download the code to RAM.

Table 20 HDK jumper configuration for Timer2 PWM example

Target chip

jumper configuration for PWM2 pin

jumper configuration for PWM3 pin

jumper configuration for PWM4 pin

DA1458x

P1_2: J2.15

P0_7: J2.12

P1_0: J2.13

DA1453x

P0_8: J2.29

P0_9: J2.20

P0_7 : J2.28

The user can use LED segment array as a visual indication for signals PWM2, PWM3 and PWM4. The brightness of the LED segments is then directly influenced by the duty cycle of the PWM signals.

5.14.2. Running the Example

Once the user has built the Timer2 (PWM2, PWM3, PWM4) example and loaded to HDK, the PWM2, PWM3 and PWM4 signals will become active. If a LED segment array is connected to the PWM pins. The brightness of the LEDs will be changing automatically because each PWM duty cycle will change in a loop function. The screen shown in Figure 59 will appear.

../_images/timer2_pwm.jpg

Figure 59 Timer2 PWM

5.15. Battery Example

The Battery example demonstrates how to read the battery level with the use of the ADC.

The Keil project is located in the <sdk_root_directory>\projects\target_apps\peripheral_examples\adc\batt_lvl.

5.15.1. Hardware Configuration

Connect the UART terminal as described in section Section 5.4.

The SWD configuration described in section Section 5.5 is used to download the code to RAM.

The user has to install a coin cell battery on the board.

5.15.2. Running the Example

Once the user has built and loaded the example project to the HDK, the ADC is configured to provide a measurement of the battery level. If device operates in boost mode, the example will report the percentage left of an alkaline cell battery on the terminal screen (see Figure 60) – if not, the percentage left of a CR2032 cell battery will be reported instead (see Figure 61).

../_images/battery_boost.png

Figure 60 Battery Example – Boost Mode

../_images/battery.png

Figure 61 Battery Example