AN3281 Application note

Similar documents
STM8 I 2 C optimized examples

P-NUCLEO-USB001. STM32 Nucleo pack for USB Type-C and Power Delivery. Features. Description

Using the GPIOs and interrupt controller to drive LEDs on STM8 Nucleo-64 boards

P-NUCLEO-USB001. STM32 Nucleo pack for USB Type-C and Power Delivery Data brief. Features. Description

AN4045 Application note

Bluetooth low energy profiles for the X-CUBE-BLE1 expansion for STM32Cube. Description

AN2672 Application note

AN4311 Application note

STSW-BLUENRG1-DK. BlueNRG-1, BlueNRG-2 DK SW package

L9958 Evaluation board for high current (8.6A) DC and Stepper Motors. Description

RN0084 Release note. ST-LINK/V2 firmware upgrade. About this release note

Motor control power board based on the SLLIMM-nano 2 nd series

AN4777 Application note

NUCLEO-L433RC-P NUCLEO-L452RE-P

STEVAL-IPMnM2N. Motor control power board based on the SLLIMM-nano 2 nd series. Description. Features. RoHS compliant

Getting started with X-CUBE-LED channel LED driver software expansion based on LED1642GW for STM32Cube

Getting started with the digital MEMS microphone expansion board based on MP34DT01-M for STM32 Nucleo

Dynamic Electro-Thermal simulator for VIPower products. Description

UM0792 User manual. Demonstration firmware for the DMX-512 communication protocol transmitter based on the STM32F103Zx.

AN3996 Application Note

AN2737 Application note Basic in-application programming example using the STM8 I 2 C and SPI peripherals Introduction

AN5123 Application note

UM2092 User manual. Basic metrology firmware for the STM32F103RD and the STPM32 devices. Introduction

AN4491 Application note

AN2667 Application note

NUCLEO-L496ZG. STM32 Nucleo-144 board. Features. Description

AN3279 Application Note

STSW-STWBCFWDT. STWBC firmware downloader tool. Description. Features

UM2167 User manual. OrCAD PSpice model usage instructions

AN4872 Application note

USB Type-C and Power Delivery Nucleo pack with NUCLEO-F072RB expansion board based on STUSB1602. Description

Description. January 2019 DB2381 Rev 3 1/5

TN1235 Technical note

Description. July 2016 DocID Rev 2 1/6

STSW-BNRGUI. BlueNRG GUI SW package. Data brief. Features. Description

AN4696 Application note

AN4820 Application note

Getting started with STEVAL-IDB007V1 and STEVAL-IDB008V1 evaluation boards

P-NUCLEO-6180X2. Proximity, gesture, ambient light sensor expansion board based on VL6180X for STM32L053R8. Description. Features

Getting started with the X-CUBE-IKA02A1 multifunctional software expansion for STM32Cube

Getting started with osxmotiongc gyroscope calibration library for X-CUBE-MEMS1 expansion for STM32Cube

AN5179. RS232 communications with a terminal using the STM8 Nucleo-64 boards. Application note. Introduction

Description of STM8 LIN software package (STSW-STM8A-LIN) release 4.1. Table 1. Release information. STM8 LIN package

AN3980 Application note

STSW-BNRG-Mesh. Mesh over Bluetooth low energy. Features. Description. Applications

AN4782 Application note

ST25DV-DISCOVERY. Discovery kit for the ST25DV04K dynamic NFC/RFID tag. Features

AN2261 APPLICATION NOTE

UM2330 User manual. ST8500 boot. Introduction

EVAL6494L. Demonstration board for L6494L gate driver. Description. Features

USB Type-C and Power Delivery Nucleo pack with NUCLEO-F072RB expansion board based on the STUSB1602

Getting started with the STM32 Nucleo pack for USB Type-C and Power Delivery with the Nucleo-F072RB board and the STUSB1602

AN5224. STM32 DMAMUX: the DMA request router. Application note. Introduction

AN4515 Application note

X-NUCLEO-53L0A1. Ranging and gesture detection sensor expansion board based on VL53L0X for STM32 Nucleo. Description. Features

AN2676 Application note

AN5181. Building a thermometer using the STM8 Nucleo-64 boards. Application note. Introduction

Description SPC564A-DISP. March 2014 DocID Rev 3 1/5

AN4113 Application note

STM32SnippetsL0. STM32L0xx Snippets firmware package. Features. Description

AN4749 Application note

Getting started with X-NUCLEO-53L1A1 long distance ranging ToF sensor expansion board based on VL53L1X for STM32 Nucleo

A scalable approach to your body, networking and security platforms. Description

AN5153 Application note

300 ma triple DC-DC converter for powering AMOLED displays. Description. Table 1: Device summary Negative voltage. Auxiliary positive voltage

AN4943 Application note

Getting started with MotionPM real-time pedometer library in X CUBE-MEMS1 expansion for STM32Cube

P-NUCLEO-IKA02A1. STM32 Nucleo pack: electrochemical toxic gas sensor expansion board with CO sensor. Description. Features

TN1234 Technical note

P-NUCLEO-53L1A1. VL53L1X nucleo pack with X-NUCLEO-53L1A1 expansion board and STM32F401RE nucleo board. Description. Features

UM2051 User manual. Getting started with the STM32 Nucleo pack for USB Type-C and Power Delivery. Introduction

AN2781 Application note

EVALKIT-VL6180X. VL6180X plug-in and STM32 F401RE Nucleo board explorer kit. Description. Features

UM2119 User manual. Graphical user interface (GUI) for EVAL-L9907-H. Introduction

EV-VND7040AJ. VND7040AJ evaluation board. Features. Applications

UM1844 User manual. EVAL-L9958 Graphical user interface (GUI) Introduction

UM2495. High performance HF reader / NFC initiator IC software expansion for NUCLEO-8S208RB. User manual. Introduction

X-NUCLEO-53L1A1. Long distance ranging Time-of-Flight sensor expansion board based on VL53L1X for STM32 Nucleo. Description.

ST33F1M, ST33F1M0, ST33F896, ST33F768, ST33F640, ST33F512

EVLKSTCOMET10-1. STCOMET smart meter system-on-chip development kit. Features

ST10F271B/E, ST10F272B/E Errata sheet

EVALKITSTKNX. Miniature transceiver STKNX evaluation and development kit. Features

SPC58EHx,SPC58NHx. A scalable approach for high-end body, networking and security platforms for Automotive. Description. Features

P-NUCLEO-53L0A1. VL53L0X Nucleo pack with ranging, gesture detection sensor expansion board and STM32F401RE. Description. Features

AN4624 Application note

UM2276. Getting started with MotionSD standing vs sitting desk detection library in X-CUBE-MEMS1 expansion for STM32Cube. User manual.

UM2194. Getting started with MotionAW activity recognition for wrist library in X-CUBE-MEMS1 expansion for STM32Cube. User manual.

AN4274 Application note

UM2343 Application note

X-NUCLEO-53L0A1 ranging and gesture detection sensor expansion board based on VL53L0X for STM32 Nucleo

AN4666 Application note

Getting started with MotionMC magnetometer calibration library in X-CUBE-MEMS1 expansion for STM32Cube

STM32L4R9I-EVAL. Evaluation board with STM32L4R9AI MCU. Features

UM2045 User manual. Getting started with the X-CUBE-NFC3 near field communication transceiver software expansion for STM32Cube.

Getting started with osxmotionmc magnetometer calibration library for X-CUBE-MEMS1 expansion for STM32Cube

How to configure the BlueNRG-1 and BlueNRG-2 devices in network coprocessor mode. Main components Bluetooth Low Energy wireless system-on-chip

EV-AUTO-LDOS. L5150GJ-L5300AH7-L4995AK Evaluation Board. Applications. Features. Description

ST Payment Secure Solution Java Card platform with up to 100 Kbytes of user NVM for AMEX payment applications

Getting started with the X-CUBE-NFC5 high performance HF reader / NFC initiator IC software expansion for STM32Cube

Getting started with MotionPM real-time pedometer library in X-CUBE-MEMS1 expansion for STM32Cube

Transcription:

Application note STM8 8-bit MCUs I 2 C optimized examples Introduction This document describes how to use the following I 2 C optimized examples: Hardware configuration example of a common I 2 C bus Master firmware examples in polling mode Master firmware examples with interrupt Slave firmware examples. Reference documents I 2 C-bus specification, version 2.1, January 2000, NXP STM8S series and STM8AF series 8-bit microcontrollers reference manual (RM0016) STM8L051/L052 Value Line, STM8L151/L152, STM8L162, STM8AL31, STM8AL3L MCU lines reference manual (RM0031) STM8L101xx microcontroller family reference manual (RM0013) STM8TL5xxx microcontroller family reference manual (RM0312). May 2016 DocID17985 Rev 3 1/23 www.st.com 1

Contents AN3281 Contents 1 Hardware configuration example of a common I 2 C bus............ 5 2 Software of I 2 C firmware examples............................. 6 3 Master firmware examples in polling mode...................... 7 3.1 Application layer example in polling mode......................... 7 3.2 Data link layer example in polling mode........................... 7 3.2.1 Functions predefined to control the data flow on master firmware in polling mode (to be customized)............................. 7 4 Master firmware examples with interrupt....................... 14 4.1 Master application layer example with interrupt.................... 14 4.2 Master data link layer example with interrupt...................... 14 4.2.1 Functions predefined to control the data flow on master firmware with interrupt (to be customized).............................. 14 5 Slave firmware examples with interrupt........................ 18 5.1 Slave application layer example with interrupt..................... 18 5.2 Slave data link layer example with interrupt....................... 18 5.2.1 Functions predefined to control the data flow on slave firmware with interrupt (to be customized).............................. 18 5.3 Data link layer flowchart...................................... 21 6 Revision history........................................... 2/23 DocID17985 Rev 3

List of tables List of tables Table 1. Document revision history................................................. DocID17985 Rev 3 3/23 3

List of figures AN3281 List of figures Figure 1. Hardware configuration example of a common I 2 C bus............................ 5 Figure 2. N-data byte write sequences preceded by a one-command byte..................... 8 Figure 3. One-datum or command byte write sequence................................... 8 Figure 4. Flowchart of data-write sequences made by the I2C_WriteRegister() function................................................................. 9 Figure 5. N-data byte read sequences preceded by a one-command byte.................... 10 Figure 6. N-data byte random read sequences (without any command)...................... 11 Figure 7. Flowchart of data read sequences made by the I2C_RandomRead() function......... 12 Figure 8. I 2 C state machine flowchart................................................ 16 Figure 9. Data link layer flowchart................................................... 21 4/23 DocID17985 Rev 3

Hardware configuration example of a common I 2 C bus 1 Hardware configuration example of a common I 2 C bus The firmware examples provided within this application note illustrate the basics of the I 2 C communication protocol on the STM8 microcontrollers. In these examples, the I 2 C peripheral is used to communicate between two STM8 devices. The I 2 C can also be reused and customized to fit a specific application which requires an I 2 C communication with another device using the I 2 C protocol. In these examples, the master and slave work together and transmit data through the bus. At all times, the I 2 C protocol is respected (see the I 2 C-bus specification). Figure 1 shows the hardware configuration that must be followed. Figure 1. Hardware configuration example of a common I 2 C bus 1. Legend: V CC = supply voltage, typically ranging from 1.8 V to 5 V SDA = Serial data (I 2 C data line) SCL = Serial clock (I 2 C clock line) Rp1, Rp2 = Pull-up resistor used to set the bus idle voltage to V CC. Also called the I 2 C termination. Rs1, Rs2 = Optional 100 Ohm serial resistor used to ease differentiation between master and slave when analyzing communication waveforms on the oscilloscope. These resistors must be placed on one extremity of the bus (on the master or slave side). DocID17985 Rev 3 5/23

Software of I 2 C firmware examples 2 Software of I 2 C firmware examples The software of all I 2 C firmware examples is divided into two basic levels: An application layer (main.c) - which is an example of how to implement all the I 2 C procedures. It must be replaced by the usercode in the final application. A data link layer (I2C_xxx.c) - which manages the data flow process and hardware control. The user should not change the software at this level. All processes at data link level are managed by a set of predefined functions contained in the data link layer. These functions are called from the application level. DocID17985 Rev 3 6/23

Master firmware examples in polling mode 3 Master firmware examples in polling mode 3.1 Application layer example in polling mode This layer simulates an I 2 C memory access with an offset command. It should be used with the example of the provided I 2 C slave. After peripheral initialization, the program runs in a testing loop. This sends a succession of bytes to be stored in the slave memory and then reads them back. After each loop, all sent and received values are compared for integrity checking purposes. The first byte of every message is used as a memory offset command for the data storage register. All read and write procedures performed on the I 2 C bus are managed by calling dedicated functions from the data link layer. Their execution times are guarded by a timeout which is serviced by a dedicated timer. This timeout is reset at the start of every testing loop and is checked at the end of every loop. If the timer counter reaches 0, it means that one I 2 C communication is stuck. 3.2 Data link layer example in polling mode All I 2 C activities of the data link layer (except errors) are performed and checked by polling. Errors are handled by the I 2 C interrupt service. The specific functions for I 2 C flow control are predefined in this part of the firmware. They are called by the application layer to control all I 2 C processes and they can be customized by the user according to the application needs. The address of the slave is fixed in form of a compilation parameter. These procedures follow specific processes which cover all the known I 2 C errata issues (see Figure 4 and Figure 7). 3.2.1 Functions predefined to control the data flow on master firmware in polling mode (to be customized) I2C_WriteRegister function This function sends an offset/command byte followed by a defined number of data bytes from a specific data field. This function can also be used to send one byte if it is called with zero number of data bytes. In the example in Figure 2, the first (command) byte is interpreted as an offset from which data is stored in the slave device. Prototype void Function I2C_WriteRegister (u8 offset_command, u8 number_of_data_bytes, u8 *data_field_address). Parameters offset_command: first byte to send during communication. Can be used as offset, command, or first datum. number_of_data_bytes: number of bytes to be sent. Value from 0 to 255. *data_field_address: pointer to first address of data to be sent. DocID17985 Rev 3 7/23

Master firmware examples in polling mode Return value Figure 2. N-data byte write sequences preceded by a one-command byte 1. Legend: S = start, P = stop, H = high, L = low Figure 3. One-datum or command byte write sequence 1. Legend: S = start, P = stop, H = high, L = low DocID17985 Rev 3 8/23

Master firmware examples in polling mode Figure 4. Flowchart of data-write sequences made by the I2C_WriteRegister() function 1. Legend: SB = start bit, RW = read/write bit DocID17985 Rev 3 9/23

Master firmware examples in polling mode I2C_ReadRegister function This function sends one byte (offset_command value) to the slave. It then restarts the bus and continues communication by reading a defined number of data bytes. Bytes are stored in a specific data field starting from a specified address. The offset_command value depends on the slave device interpretation. In this example, it is used as a memory offset. It can be used in other applications as a command for specific I 2 C peripherals. Prototype void Function I2C_ReadRegister (u8 offset_command, u8 number_of_data_bytes, u8 *data_field_address). Parameters offset_command: first byte to send during communication. Can be used as offset, command, or first datum. number_of_data_bytes: number of bytes to read. Value from 0 to 255. *data_field_address: pointer to first address of data to store received data. Return value Figure 5. N-data byte read sequences preceded by a one-command byte 1. Legend: S = start, P = stop, H = high, L = low 2. Stop is not mandatory in this sequence and can be skipped by defining the NO_RESTART constant in the driver header file (I2C_master_poll.h). DocID17985 Rev 3 10/23

Master firmware examples in polling mode I2C_RandomRead function This function reads directly the requested data from the slave. No offset or command byte is written previously. It can be used as a standard or continuous read (where autoincrementation of addresses is available on the slave side). Received bytes are stored in data fields starting from specified data field addresses. Prototype void Function I2C_RandomRead (u8 number_of_data_bytes, u8 *data_field_address). Parameters number_of_data_bytes: number of bytes to read. Value from 0 to 255. *data_field_address: pointer to first address to store received data. Return value Figure 6. N-data byte random read sequences (without any command) 1. Legend: S = start, P = stop, H = high, L = low DocID17985 Rev 3 11/23

Master firmware examples in polling mode Figure 7. Flowchart of data read sequences made by the I2C_RandomRead() function 1. Legend: SB = start bit, RW = read/write bit DocID17985 Rev 3 12/23

Master firmware examples in polling mode The I 2 C read register function is the succession of one I 2 C write register function call and one I 2 C random read function call (see Figure 4: Flowchart of data-write sequences made by the I2C_WriteRegister() function and Figure 7: Flowchart of data read sequences made by the I2C_RandomRead() function). DocID17985 Rev 3 13/23

Master firmware examples with interrupt 4 Master firmware examples with interrupt 4.1 Master application layer example with interrupt The function and purpose of this layer is the same as for polling mode. After peripheral initialization, the program stays in a testing loop. This sends a succession of bytes to be stored into the slave memory and then reads back from the slave. After each loop, all sent and received values are compared for integrity checking purposes. For more details, please refer to Section 3.1: Application layer example in polling mode. 4.2 Master data link layer example with interrupt All I 2 C activities of the data link layer are handled by the I 2 C interrupt service. This interrupt routine is managed by the internal state machine (see Figure 8 on page 16). The procedures included in this flowchart follow specific processes which cover all known I 2 C errata issues (see Figure 4 on page 9 and Figure 7 on page 12). It is highly recommended not to change this layer to ensure that the application handles specific states on the I 2 C bus. The specific functions for I 2 C flow control are predefined in this part of the firmware. They are called by the application layer to control all I 2 C processes and can be customized by the user according to the application needs. 4.2.1 Functions predefined to control the data flow on master firmware with interrupt (to be customized) I2C_WriteRegister function This function sets up and starts the state machine to perform an I 2 C write process. It returns 1 when the process is started or 0 when the peripheral or line is busy. Prototype u8 I2C_WriteRegister (u16 SlaveAdd, u8 AddType, u8 NoStop, u8 NumByteToWrite, u8 *DataBuffer). Parameters SlaveAdd: unsigned short number address of the slave. AddType: 7-bit (SEV_BIT_ADDRESS) or 10-bit addressing (TEN_BIT_ADDRESS). NoStop: stop is/is not performed after the transmission (STOP; NOSTOP). NumByteToWrite: number of bytes to be sent. DataBuffer: first data buffer address. Return value 0 is returned if the write process is not started due to other I 2 C operations. 1 is returned if the write process is started. DocID17985 Rev 3 14/23

Master firmware examples with interrupt I2C_ReadRegister function This function sets up and starts the state machine to perform an I 2 C read process. It returns 1 when the process is started or 0 when the peripheral or line is busy. Prototype u8 I2C_ReadRegister(u16 SlaveAdd, u8 AddType, u8 NoStop, u8 NumByteToRead, u8 *DataBuffer); Parameters SlaveAdd: unsigned short number address of the slave. AddType: 7-bit (SEV_BIT_ADDRESS) or 10-bit addressing (TEN_BIT_ADDRESS). NoStop: stop is/is not performed before the transmission (used for 10-bit addressing. mode when the complete address or header is sent depending on the STOP or NOSTOP flag). NumByteToRead: number of bytes to be received. DataBuffer: first data buffer address. Return value 0 is returned if the read process is not started due to other I 2 C operations. 1 is returned if the read process is started. ErrProc function This function is called from I 2 C interrupt routines each time an error is detected. It can be customized according to the application needs. Prototype void ErrProc (void). Parameters Return value DocID17985 Rev 3 15/23

Master firmware examples with interrupt Figure 8. I 2 C state machine flowchart 1. Legend: SB = start bit, W = write, R = read 2. The text in blue indicates the parts of this State machine which must be protected from interrupt by software disabling (see the device errata sheet). Note: For a 10-bit address random read, a WriteRegister function call (without data and STOP) should be performed before a ReadRegister function call. DocID17985 Rev 3 16/23

Master firmware examples with interrupt Example // Send 10-bit slave address I 2 C_WriteRegister (0x3F0,TEN_BIT_ADDRESS,NOSTOP,0,Buff); // Read data from slave I 2 C_ReadRegister (0x3F0,TEN_BIT_ADDRESS,STOP,3,Buff); DocID17985 Rev 3 17/23

Slave firmware examples with interrupt 5 Slave firmware examples with interrupt 5.1 Slave application layer example with interrupt This layer simulates an I 2 C memory with an offset command example. The first datum received is interpreted as a command (memory offset). Interaction with the data link layer is made using specific customizable callback functions (see Section 5.2.1: Functions predefined to control the data flow on slave firmware with interrupt (to be customized)). These functions can be modified depending on the application needs. 5.2 Slave data link layer example with interrupt All I 2 C activities of the data link layer are handled by the I 2 C interrupt service. All procedures in this interrupt service follow specific processes which cover all known I 2 C errata issues (see Figure 4 and Figure 7). It is highly recommended not to change this layer to ensure that the application handles specific states on the I 2 C bus. 5.2.1 Functions predefined to control the data flow on slave firmware with interrupt (to be customized) I2C_transaction_begin This function is called every time a transaction with the slave begins (slave address recognized). Prototype void I2C_transaction_begin (void). Parameters Return value DocID17985 Rev 3 18/23

Slave firmware examples with interrupt I2C_transaction_end This function is called every time a transaction with the slave ends (stop or Nack detected). Prototype void I2C_transaction_end (void). Parameters Return value I2C_byte_received This function is called every time a byte is received by the I 2 C peripheral. This example stores data in the memory. Prototype void I2C_byte_received (u8 u8_rxdata). Parameters Return value I2C_byte_write This function is called every time a byte needs to be sent. It must return a u8 value which corresponds to the byte to be written on the I 2 C line. In this example, the function returns selected stored data from the memory. Prototype u8 I2C_byte_write(void). Parameters Return value To be customized according to the application needs. The returned value is the datum which is written on the I 2 C line. DocID17985 Rev 3 19/23

Slave firmware examples with interrupt ErrProc function This function is called from the I 2 C interrupt routines each time an error is detected. It can be customized according to the application needs. Prototype void ErrProc (void). Parameters Return value DocID17985 Rev 3 20/23

Slave firmware examples with interrupt 5.3 Data link layer flowchart Figure 9. Data link layer flowchart DocID17985 Rev 3 21/23

Revision history 6 Revision history Table 1. Document revision history Date Revision Changes 20-Oct-2010 1 Initial release 20-Nov-2012 2 09-May-2016 3 Document updated to include STM8AL and STM8TL5 devices. Added Table 1: Applicable products. Updated Reference documents. Modified Figure 8 and Figure 9. Added Section 5: Slave firmware examples with interrupt. DocID17985 Rev 3 /23

IMPORTANT NOTICE PLEASE READ CAREFULLY STMicroelectronics NV and its subsidiaries ( ST ) reserve the right to make changes, corrections, enhancements, modifications, and improvements to ST products and/or to this document at any time without notice. Purchasers should obtain the latest relevant information on ST products before placing orders. ST products are sold pursuant to ST s terms and conditions of sale in place at the time of order acknowledgement. Purchasers are solely responsible for the choice, selection, and use of ST products and ST assumes no liability for application assistance or the design of Purchasers products. No license, express or implied, to any intellectual property right is granted by ST herein. Resale of ST products with provisions different from the information set forth herein shall void any warranty granted by ST for such product. ST and the ST logo are trademarks of ST. All other product or service names are the property of their respective owners. Information in this document supersedes and replaces information previously supplied in any prior versions of this document. 2016 STMicroelectronics All rights reserved DocID17985 Rev 3 23/23 23