![]() |
|
| If you can't view the Datasheet, Please click here to try to view without PDF Reader . |
|
|
| Datasheet File OCR Text: |
| march 2009 rev 3 1/16 AN2737 application note basic in-application programming example using the stm8 i 2 c and spi peripherals introduction this application note is one of a set of applicat ion 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 i 2 c or spi peripherals. the stm8a and stm8s firmware libraries ma ke the stm8 behave like an eeprom which can be programmed and/or accessed by a standard eeprom pr otocol via the i 2 c/spi interface. the examples of this applic ation note, use the pc-based aardvark tm host adapter (on the i 2 c and spi bus) to communicate with the stm8. the two examples provided are: the aardvark host adapter communicating with the stm8 i2c peripheral using interrupts in 7-bit addressing mode. the aardvark host adapter co mmunicating 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 . www.st.com
contents AN2737 2/16 contents 1 iap example with i 2 c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 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 AN2737 iap example with i 2 c 3/16 1 iap example with i 2 c this section describes how to perform basic iap with the stm8 i2c peripheral. the stm8 device behaves like a standard i2c eeprom slave which can communicate with any master. in this application note, the aardvark pc host adapter is used as the i2c master. 1.1 hardware description figure 1 shows a typical hardware connection between the aardvark host adapter as master and the stm8 i2c peripheral as slave. the aardvark host adapter and the stm8 i2c 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 i 2 c 1.2 firmware description the current firmware is provided in the zip file attached to this an. it includes: the i2c driver that supports all basic i2c communication. the flash, gpio, i 2 c 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 i 2 c scl line frequency. the stm8 address has been set to a2h using the ?i 2 c 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. r/w scl sda usb pc + aardvark software aardvark host adapter stm8 device i 2 c flash program memory and master slave data eeprom iap example with i 2 c AN2737 4/16 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 st m8? available on www.cosmicsoftware.com. 1.3 read 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 i2c 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 i 2 c 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. AN2737 iap example with i 2 c 5/16 1.3.2 current 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 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 bytes ? field. 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. start add slv device-r ack-s bytes read from stm8 stop ack-m ackf iap example with i 2 c AN2737 6/16 figure 3. reading current bytes using the aardvark software figure 4 shows the read values displayed in the transaction log window of the aardvark software. figure 4. values read by the aardvark software AN2737 iap example with i 2 c 7/16 1.3.3 random read command the random read command allows data from the flash program memory or data eeprom of the stm8 to be read, starting from any address in the range 8000h?027fffh or 4000h?47ffh respectively. the packet sequence to achieve a ?random read? with the aardvark software and the stm8 i 2 c is summarized in figure 5 . figure 5. random read packet sequence two steps are required to perform a random read with the aardvark host adapter software. step1 sends the first address to be read from the firmware. this is done as follows: 1. select the master tabsheet, set the slave address and the bitrate, and uncheck the 10- bit addressing mode option as per section 1.3.2: current read command on page 5 . 2. enter the ?start address? in the ?message window? text box. it corresponds to the first address to be read from the flash program memory. for example, enter ?00 a0 00 ? if you want to read from address a000h. 3. check the ?no stop? option. 4. press the ?master write? button to send the start address to the firmware. at the end of this command, the firmware has in itialized its internal pointer address to the address sent above. start add slv device-w ack-s start program address ack-s current read iap example with i 2 c AN2737 8/16 figure 6 illustrates points 2, 3, and 4 of step1. figure 6. step 1 of a random read using the aardvark software step2 reads the bytes as follows: 1. enter the number of bytes to read in the ?number of bytes ? field. values can be from 1 to 255. 2. uncheck the ?no stop ? option. 3. press the ?master read ? button. 4. the number of bytes to be read can be changed by pressing the master read button again. the read data are displayed in the transaction log window of the aardvark software. at the end of this command, the lcd displays the message ?reading done?. AN2737 iap example with i 2 c 9/16 1.3.4 current write command the current write command allows data to be written in the flash program memory or data eeprom of the stm8, starting from the curren t address which is se t to 4000h after a firmware reset. the packet sequence to achieve a ?current write? with the aardvark software and the stm8 i 2 c is summarized in figure 7 . figure 7. current write packet sequence the following points explain how to write current bytes using the aardvark software: 1. select the master tabsheet, set the slave address and the bitrate, and uncheck the 10- bit addressing as per section 1.3.2: current read command on page 5 . 2. enter the data to be writen in the flash program memory at the current address in the ?message window ? text box. 3. uncheck the ?no stop ? option. 4. press the ?master write ? button to send the data to the firmware. at the end of this command, the data are written in the flash program memory. they can be verified using the stvd memory window. the lcd displays the message ?writing ok?. 1.3.5 random write command the random write command allows data to be written in the flash program memory or data eeprom of the stm8, starting from any a ddress in the range 8000h?027fffh or 4000h?47ffh. the packet sequence to achieve a ?random write? with the aardvark software and the stm8 i 2 c is summarized in figure 8 figure 8. random write packet sequence two steps are required to perform a random write with the aardvark host adapter software. step1 sends the first address to be writen to the firmware. this is done as follows: 1. select the master tabsheet, set the slave address and the bitrate, and uncheck the 10- bit addressing mode option as per section 1.3.2: current read command on page 5 . 2. enter the ?start address ? in the ?message window ? text box. it corresponds to the first address to be writen to the flash program memory. 3. check the ?no stop ? option. 4. press the ?master write ? button to send the start address to the firmware. at the end of this command, the firmware has in itialized its internal pointer address to the address sent above. add slv device-w ack-s bytes sent by master ack-s stop start start add slv device-w ack-s ack-s current write start program address iap example with i 2 c AN2737 10/16 step2 writes the bytes as follows: 1. fill the data to be progra mmed in the ?message window ? . 2. uncheck the ?no stop ? option. 3. press the ?master write ? button. at the end of this command, the data are written in the flash program memory. they can be verified using the stvd memory window. the lcd displays the message ?writing ok?. AN2737 iap example with spi 11/16 2 iap example with spi this section describes how to perform basic iap with the stm8 spi peripheral. the stm8 device behaves like a standard spi eeprom slave which can communicate with any master. in this application note, the aardvark pc host adapter is used as the spi master. 2.1 hardware description figure 9 shows a typical hardware connection between the aardvark host adapter as master and the stm8 spi peripheral as slave. this type of communication uses hardware nss management for the slave. the following pins are connected together: aardvark mosi and stm8 spi mosi. aardvark miso and stm8 spi miso. aardvark sck and stm8 spi sck. aardvark ss and stm8 spi nss. note: when the stm8 evaluation board ( stm8/128-eval) is used, the r34 resistor has to be soldered on the r35 placement. this is to put the i/o of the micro-sd card level shifter in high impedance state. to use the micro-sd card, the r34 resistor has to be soldered back to its original place. figure 9. hardware connection between the ardvark host adapter and the stm8 spi 2.2 firmware description the current example is provided in the zip file attached to this an. it includes: the spi driver that supports all basic spi communications. the flash, clk, gpio and lcd screen drivers. the stm8 clock is set to 16 mhz. the baudrate is automatically set to f cpu/2 (because the stm8 spi peripheral is the slave). as a result, the maximum stm8 spi speed is 8 mhz. the firmware follows standard se rial spi eeprom protocol rules . however, not all features of a real serial spi eeprom are implemented here . for example, the firmware does not r/w aardvark host adapter usb mosi miso sck ss/nss pc + aardvark software spi slave master stm8 device flash program memory and data eeprom iap example with spi AN2737 12/16 support the status register management. consequently, it is not possible to read or write the status register. 2.3 read and write commands the current example has been validated at 4 mhz for read and write operations. the firmware allows reading or wr iting in the flash program me mory and data eeprom of the stm8 from address 8000h?027fffh and 4000h?47ffh respectively. before starting, the firmware example must be loaded with stvd in the stm8 device and executed using the run command. the evaluation board lcd displays a welcome message ?stm8 iap-spi demo?. no more messages are displayed during firmware execution. 2.3.1 packet description the different packets composing an spi command are: 1. opc-read : opcode (03h) that requests a read. 2. opc-wren: opcode (06h) that enables the write function. 3. opc-wrt : opcode (02h) that requests a write after a rising nss signal. 4. add: physical address of the memory which is used to start a read or write operation. 5. data-r: number of data to be read. it is used only for clocking (commonly these bytes have a value of 00h). 6. data-w: data to be programmed into the flash program memory or data eeprom. 2.3.2 read command the read command allows data from the flash program memory of the stm8 to be read, starting from any flash addressing range. the packet sequence to achieve a ?read? with the aardvark software and the stm8 spi is summarized in figure 10 . during this sequence, the nss signal is set to gnd by the master. figure 10. read packet sequence opc-read add data-r 00h flash program memory data read mosi line miso line 00h AN2737 iap example with spi 13/16 the following points explain how to read different bytes using the aardvark software: 1. select the master tabsheet. 2. set the bitrate to 4000 khz using the ?bitrate? field and the ?set? button. 3. select the polarity as rising/falling. 4. select the phase as sample/setup. 5. set the bit order to msb. 6. set the ss pin as active low (aardvark ss pin is connected to the stm8 nss). 7. for example, enter the following sequence: 03 00 40 00 00 00 00 00 00 00 00. the value 03 sets the ?read? flag. the three bytes 00, 40, and 00 are the ?read start address?. the seven ?00? bytes correspond to the number of bytes to be read. 8. press the ?send? button. figure 11 illustrates the different steps ent ered on the aardvark software. figure 11. reading bytes using the aardvark software figure 12 shows the read values displayed in the transaction log window of the aardvark software. figure 12. read values of the aardvark software iap example with spi AN2737 14/16 2.3.3 write command the write command allows data in the flash program memory of the stm8 to be written, starting from any flash addressing range. the packet sequence to achieve a ?write? with the aardvark software and stm8 spi is explained below and is summarized in figure 13 and figure 14 . as per section 2.3.2: read command on page 12 , select the master tabsheet, set the bitrate (for write operations up to 8 mhz can be chosen), select the rising/falling polarity and the sample/setup phase, and set the active low option for the ss pin. two steps are then required to carry out a write command. step 1: in the ?message window ? text box enter the opc-wren value (06h) and press ?send?. this byte is used to activate the write mode. step 2: in the ?message window ? text box enter opc-wrt add data-w and press ?send?. for example, 02 01 90 ff aa 55 bb 66 programs 4 bytes starting from address 0190ffh. figure 13. step 1 of the write packet sequence using aardvark software figure 14. step 2 of the write packet sequence using aardvark software in step 2, a maximun of 128 bytes can be programmed. when step 2 is finished, the nss signal rises up and the flash is programmed with the data-w bytes at the add address. at the end of this command, the data are written in the flash memory. they can be verified using the stvd memory window or by performing an spi read. opc-wren 00h mosi line miso line opc-wrt add data-w 00h 00h 0x00 (1 byte per byte programmed) mosi line miso line AN2737 revision history 15/16 3 revision history table 1. document revision history date revision changes 02-may-2008 1 initial release 13-nov-2008 2 changed document title. reformulated text of the introduction to improve understanding and readability. changed all references of the ?stm8 firmware? to the ? stm8a and stm8s firmwares?. changed all references of ?aardvark-i 2 c? and ?aardvark-spi? to ?aardvark?. 02-mar-2009 3 updated document title. current firmware is provided as a zip attachment wit h this application note (and not in the stm8 fi rmware libraries).throughout the document, firmware descriptions and examples have been updated to reflect the new source code. modified text throughout docum ent (without changing technical content) to improve clarity and readability. updated terminology: program memory replaced by flash program memory and data memory replaced by data eeprom. section 1.2: firmware description : added information concerning byte, word, and block programming features offered by the stm8. section 1.3: read and write command examples : added that read and write operations are possible from both the flash program memory and the data eeprom; provided address range for both; added note that the firmware follows standard serial i 2 c eeprom protocol rules. section 1.3.2: current read command : current address of the flash program memory after a firmware reset changed from 8000h to 4000h; updated frequency; updated and replaced figure 3 and figure 4 . section 1.3.3: random read command : clarified that random read commands are possible from both the flash program memory and data eeprom; provided address range for both; replaced example addresses in ?step 1?; updated and replaced figure 6 . section 1.3.4: current write command : clarified that current write commands are possible from both the flash program memory and data eeprom starting from t he current address (which was updated). section 1.3.5: random write command : clarified that random write commands are possible from both the flash program memory and data eeprom; provided address range for both. section 2.1: hardware description : added note about using the stm8/128-eval evaluation board. section 2.3: read and write commands : updated frequency for read and write operations. section 2.3.2: read command : updated figure 10 ; updated and replaced figure 11 and figure 12 . AN2737 16/16 please read carefully: information in this document is provided solely in connection with st products. stmicroelectronics nv and its subsidiaries (?st ?) reserve the right to make changes, corrections, modifications or improvements, to this document, and the products and services described he rein at any time, without notice. all st products are sold pursuant to st?s terms and conditions of sale. purchasers are solely responsible for the choice, selection and use of the st products and services described herein, and st as sumes no liability whatsoever relating to the choice, selection or use of the st products and services described herein. no license, express or implied, by estoppel or otherwise, to any intellectual property rights is granted under this document. i f any part of this document refers to any third party products or services it shall not be deemed a license grant by st for the use of such third party products or services, or any intellectual property contained therein or considered as a warranty covering the use in any manner whatsoev er of such third party products or services or any intellectual property contained therein. unless otherwise set forth in st?s terms and conditions of sale st disclaims any express or implied warranty with respect to the use and/or sale of st products including without limitation implied warranties of merchantability, fitness for a parti cular purpose (and their equivalents under the laws of any jurisdiction), or infringement of any patent, copyright or other intellectual property right. unless expressly approved in writing by an authorized st representative, st products are not recommended, authorized or warranted for use in milita ry, air craft, space, life saving, or life sustaining applications, nor in products or systems where failure or malfunction may result in personal injury, death, or severe property or environmental damage. st products which are not specified as "automotive grade" may only be used in automotive applications at user?s own risk. resale of st products with provisions different from the statements and/or technical features set forth in this document shall immediately void any warranty granted by st for the st product or service described herein and shall not create or extend in any manner whatsoev er, any liability of st. st and the st logo are trademarks or registered trademarks of st in various countries. information in this document supersedes and replaces all information previously supplied. the st logo is a registered trademark of stmicroelectronics. all other names are the property of their respective owners. ? 2009 stmicroelectronics - all rights reserved stmicroelectronics group of companies australia - belgium - brazil - canada - china - czech republic - finland - france - germany - hong kong - india - israel - ital y - japan - malaysia - malta - morocco - singapore - spain - sweden - switzerland - united kingdom - united states of america www.st.com |
|
Price & Availability of AN2737
|
|
|
|
|
All Rights Reserved © IC-ON-LINE 2003 - 2022 |
| [Add Bookmark] [Contact Us] [Link exchange] [Privacy policy] |
|
Mirror Sites : [www.datasheet.hk]
[www.maxim4u.com] [www.ic-on-line.cn]
[www.ic-on-line.com] [www.ic-on-line.net]
[www.alldatasheet.com.cn]
[www.gdcy.com]
[www.gdcy.net] |