ST AN2737 Application note

AN2737

Application note

Basic in-application programming example using the STM8 I2C and SPI peripherals

Introduction

This application note is one of a set of application notes giving examples of how to use the various blocks of the STM8 microcontroller family and the STM8A and STM8S firmware libraries. It is intended to provide two practical application examples of how to perform basic in application programming (IAP) using either the STM8 I2C or SPI peripherals.

The STM8A and STM8S firmware libraries make the STM8 behave like an EEPROM which can be programmed and/or accessed by a standard EEPROM protocol via the I2C/SPI interface. The examples of this application note, use the PC-based AardvarkTM host adapter (on the I2C and SPI bus) to communicate with the STM8.

The two examples provided are:

The Aardvark host adapter communicating with the STM8 I²C peripheral using interrupts in 7-bit addressing mode.

The Aardvark host adapter communicating with the STM8 SPI peripheral in polling mode.

For further information on the STM8A and STM8S family features, pinout, electrical characteristics, mechanical data and ordering information, please refer to the STM8A and STM8S datasheets which are available on st.com:

Further details on the Aardvark host adapter and its software can be found on

www.totalphase.com.

The STM8A and STM8S firmware libraries are available on st.com.

March 2009

Rev 3

1/16

www.st.com

Contents

AN2737

 

 

Contents

1

IAP example with I2C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

3

 

1.1

Hardware description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

3

 

1.2

Firmware description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

3

 

1.3

Read and write command examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

4

1.3.1 Packet description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1.3.2 Current read command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 1.3.3 Random read command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 1.3.4 Current write command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 1.3.5 Random write command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

2

IAP example with SPI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

11

 

2.1

Hardware description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

11

 

2.2

Firmware description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

11

 

2.3

Read and write commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

12

2.3.1 Packet description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 2.3.2 Read command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 2.3.3 Write command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

3

Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

15

2/16

ST AN2737 Application note

AN2737

IAP example with I2C

1 IAP example with I2C

This section describes how to perform basic IAP with the STM8 I²C peripheral. The STM8 device behaves like a standard I²C EEPROM slave which can communicate with any master. In this application note, the Aardvark PC host adapter is used as the I²C master.

1.1Hardware description

Figure 1 shows a typical hardware connection between the Aardvark host adapter as master and the STM8 I²C peripheral as slave.

The Aardvark host adapter and the STM8 I²C are connected together via the data (SDA) and clock (SCL) pins.

A 4.7 kΩ pull-up resistor must be connected to both the SDA and SCL lines

Note:

If the STM8/128-EVAL board is used, the pull-up resistors are present by default.

Figure 1. Hardware connection between the Ardvark host adapter the STM8 I2C

 

Master

 

Slave

 

 

 

 

USB

SCL

Flash program

PC + Aardvark

R/W

Aardvark

I2C

memory and

software

host adapter

SDA

data EEPROM

 

 

 

 

 

 

 

STM8 device

1.2Firmware description

The current firmware is provided in the ZIP file attached to this AN.

It includes:

The I²C driver that supports all basic I²C communication.

The Flash, GPIO, I2C and LCD screen drivers.

STM8A and STM8S firmware libraries

The firmware uses 7-bit addressing mode which has been validated at 100 kHz and 400 kHz for the I2C SCL line frequency.

The STM8 address has been set to A2h using the ‘I2C own address register’. As bit 0 of this register is used for read/write operation selection, the value 51h must be entered in the Aardvark software.

3/16

IAP example with I2C

AN2737

The firmware implements byte, word, and block programming features offered by the STM8 Flash program memory and data EEPROM.

Block programming has to be executed from the RAM memory. Consequently, at the beginning of the code, the Flash function section is copied to the RAM memory. To copy and link some code to the RAM memory using the Cosmic linker, the steps below must be followed:

Define the section to be linked in RAM as a movable segment using the linker option ‘- ic’.

At the begining of the code, execute the ‘_fctcpy’ function which copies the code from the Flash to the RAM memory.

For more details about movable codes, please refer to the COSMIC “C Cross Compiler User’s Guide for ST Microelectronics STM8” available on www.cosmicsoftware.com.

1.3Read and write command examples

The firmware allows read or write operations in the Flash program memory (from address 8000h up to 27FFFh) or in the data EEPROM (from address 4000h up to 47FFh) memory of the STM8.

Current and random options are available for both read and write operations:

The random command allows data to be read/writen at a specific address.

The current command allows data to be read/writen at the current address pointer. The default current address pointer value (after reset) is 4000h (start of the data EEPROM). So, for example, after writing (or reading) 10 bytes to the memory, the current address pointer value becomes 400Ah. The address is passed on 3 bytes with the most significant bit (MSB) first.

Note:

The firmware follows standard serial I²C EEPROM protocol rules.

 

Before starting, the firmware example must be loaded with STVD and executed using the

 

run command. The evaluation board LCD displays a welcome message ‘STM8 IAP-I2C

 

Demo’.

1.3.1 Packet description

The different packets composing an I2C command are:

START: Start signal which is generated by the master.

ADD SLV DEVICE-W: Slave device address which is sent by the master with the direction bit set to write.

ACK-S: Acknowledge signal which is generated by the slave.

ACK-M: Acknowledge signal which is generated by the master.

ACKF: Acknowledge failure or ‘no acknowledge’ signal which are both generated by the master. A ‘no acknowledge’ signal is generated by the master to notify the slave device that the last read byte has been correctly received which marks the end of a read operation.

STOP: Stop signal which is generated by the master.

4/16

AN2737

IAP example with I2C

1.3.2Current read command

The current read command allows data from the Flash program memory of the STM8 to be read, starting from the current address. This address is set to 4000h after a firmware reset.

The packet sequence to achieve a ‘current read’ is summarized in Figure 2.

Figure 2. Current read packet sequence

START

ADD SLV DEVICE-R

ACK-S

Bytes read from STM8

ACK-M

STOP

ACKF

 

 

 

 

 

 

 

 

 

 

 

The following points explain how to read different bytes using the Aardvark software, starting from the reset address (4000h):

1.Select the master tabsheet.

2.Set the bitrate to 100 kHz or 400 kHz using the ‘bitrate’ field and the ‘set’ button.

3.Set the slave address to 51h (corresponding to the firmware address A2h) in the slave address field.

4.Uncheck the ‘10-bit addressing mode’ option because the firmware uses 7-bit addressing mode only.

5.Uncheck the ‘no stop’ option.

6.Enter the number of bytes to be read in the ‘number of bytesfield. A value from 1 to 255 can be entered.

7.Press the ‘master read’ button.

8.The number of bytes to be read can be changed by pressing the master read button again.

The read values are displayed in the transaction log window of the Aardvark software. At the end of this command, the LCD displays the message ‘reading done’.

Figure 3 illustrates points 1 to 8 of a current read command.

5/16

Loading...
+ 11 hidden pages