Application note STM8A SPI application examples Introduction This application note is one of a set of application notes giving examples of how to use the various blocks of the STM8A microcontroller family and the STM8 firmware library. It is intended to provide three practical application examples of the use of the SPI peripheral in the STM8A device. The examples are: STM8A SPI in full-duplex communication with an LCD display. STM8A SPI in full-duplex communication with a MicroSD (MSD) card. STM8A SPI in full-duplex communication with an STM8A USART. The SPI is an extremely flexible peripheral which can simultaneously manage more than one type of communication and concurrently be a master and a slave in the same application. For further information on the STM8A family features, pinout, electrical characteristics, mechanical data and ordering information, please refer to the STM8AF61xx, STM8AF51xx datasheet. This document, its associated firmware, and other such application notes are written to accompany the STM8 firmware library. Please contact STMicroelectronics for information on the STM8 firmware library. April 2008 Rev 2 1/10 www.st.com
Contents AN2673 Contents 1 Example 1: STM8A SPI in full-duplex communication with an LCD display.................................................... 3 1.1 Overview.................................................. 3 1.2 Hardware description......................................... 3 1.3 Firmware description......................................... 4 2 Example 2: STM8A SPI in full-duplex communication with an MSD card....................................................... 5 2.1 Overview.................................................. 5 2.2 Hardware description......................................... 5 2.3 Firmware description......................................... 6 3 Example 3: STM8A SPI in full-duplex communication with an STM8A USART.................................................... 7 3.1 Overview.................................................. 7 3.2 Hardware description......................................... 7 3.3 Firmware description......................................... 8 4 Revision history............................................ 9 2/10
Example 1: STM8A SPI in full-duplex communication with an LCD display 1 Example 1: STM8A SPI in full-duplex communication with an LCD display 1.1 Overview This section describes how to set an STM8A SPI in full-duplex communication using software slave management with an LCD display. This type of communication management allows master to slave and slave to master adjustment without any hardware modification. Effectively, hardware constraints are removed in such devices. In the current example, the device SPI is the master and an LCD display is the slave. 1.2 Hardware description Figure 1 shows a typical connection, in full-duplex mode, between the STM8A SPI and an HXM122032-GB1 TM LCD display. The data output (MOSI) and clock (SCK) lines of the STM8A SPI are connected respectevely to the data input (SID) and clock (CLK) lines of the LCD display. However, the NSS pin is kept unconnected when using software slave management. Communication between the device and LCD display is thus managed using only the MOSI and CLK pins. A potentiometer can be used to adjust the contrast of the LCD display. Figure 1. STM8A SPI in full-duplex communication with an LCD display +5 V Potentiometer +5 V Key 4.7 kω PC0 Master SPI_MOSI VD SID Slave 100 nf SPI_SCK CLK 100 Ω LD1, LD2, LD3 and LD4 PH3:0 STM8A PFO +5 V CS A K LCD display HXM122032-GB1 600 Ω ai15025a 3/10
Example 1: STM8A SPI in full-duplex communication with an LCD display AN2673 1.3 Firmware description The STM8 firmware library includes the SPI driver which supports all SPI communications through a set of functions. The current example is provided as SPI example 1 in the STM8 firmware library. By default, the LCD display is the slave. A data buffer of commands is sent from the SPI master (STM8A) to the SPI slave. The LCD executes the commands and displays data on the screen. The following actions are then performed: The LCD display is cleared The ST logo is displayed The LCD prints output data Note: The key button can be used to turn the board LEDs on/off. 4/10
Example 2: STM8A SPI in full-duplex communication with an MSD card 2 Example 2: STM8A SPI in full-duplex communication with an MSD card 2.1 Overview This section describes how to set an STM8A SPI in full-duplex communication using software slave management with an MSD card. This type of communication can also be achieved through the LCD display of example 1, as both an LCD display and an MSD card can be managed by SPI protocol. The current example focusses on SPI management of an MSD card. 2.2 Hardware description Figure 2 shows a typical connection, between the STM8A SPI and the PJS008-2000 TM MSD card through an MAX3378EEUD TM level translator. Figure 2. STM8A SPI in full-duplex communication with an MSD card SPI_MOSI SPI_MISO SPI_SCK PE5 PE4 STM8A IOVCC_4 IOVCC_2 IOVCC_3 IOVCC_1 +5 V IOVL_1 IOVL_2 IOVL_4 IOVL_3 Level translator MAX33 78EEUD 10 kω +3.3 V 4.7 kω Pin 2 Pin 3 Pin 7 Pin 5 Pin 10 MSD connector PJS008-2000 ai15026a 5/10
Example 2: STM8A SPI in full-duplex communication with an MSD card AN2673 2.3 Firmware description The current example is provided as SPI example 2 in the STM8 firmware library. By default the MSD card is the slave. An external interrupt signals that the MSD card is plugged and has been detected. This initiates STM8A SPI-MSD card comunication and the following actions are performed: Buffer block_tx is filled with the value 0x00 which is sent from the STM8A SPI to the MSD card. The data received are stored in the MSD card block 0. The MSD card block 0 is read and stored in buffer block_rx. These read data in buffer block Rx are then compared with the written values in buffer block_tx. The results of this comparison are stored in the "TransferStatus1" variable. Several blocks that have been writen by buffer block_tx and read by buffer block_rx, are compared to check that all data have been correctly exchanged. The results of these comparisons are stored in the "TransferStatus2" and "TransferStatus3" variables. Severel messages are displayed on the LCD display to guide the user through the different steps. Refer to Figure 1 for LCD connections. 6/10
Example 3: STM8A SPI in full-duplex communication with an STM8A USART 3 Example 3: STM8A SPI in full-duplex communication with an STM8A USART 3.1 Overview This section describes how to set an STM8A SPI in full-duplex communication with an STM8A USART using software slave management to control the key button. DataRx and DataTx are displayed on an LCD screen. 3.2 Hardware description Figure 3 shows a typical connection between the STM8A SPI and the STM8A USART. The data input (MISO), data output (MOSI) and clock (SCK) lines from the device SPI are connected respectively to the USART_Rx, USART_Tx and clock (USART_CLK) of the device USART. The NSS pin is kept unconnected due to software slave management. Figure 3. STM8A SPI in full-duplex synchronous communication with an STM8A USART SPI_MOSI SPI_MISO SPI_SCK USART_CK USART_Rx USART_Tx STM8A 7/10
Example 3: STM8A SPI in full-duplex communication with an STM8A USART AN2673 3.3 Firmware description The current example is provided as SPI example 3 in the STM8 firmware library. The STM8A SPI is set as both a master and a slave in the same application. To print interactive messages, the STM8A SPI is set as the master and the LCD display is set as the slave. To communicate with the USART in synchronous mode, the STM8A SPI is set as the slave and the STM8A USART is set as the master. When the key button (see Figure 1) is pressed by the user an interrupt is generated and the following actions are performed: The STM8A USART sends data (DataTx) to the STM8A SPI. The device SPI receives data (DataTx) from the device USART The device SPI sends data (DataTx), that can be received from the device USART, to the LCD display. The LCD prints data that can be sent by the device USART on line 1 and prints data that can be received by the device SPI on line 2. Data (DataTx) to be sent are incremented after each event on the key button. Note: Refer to Figure 1 for the LCD connections. 8/10
Revision history 4 Revision history Table 1. Document revision history Date Revision Changes 14-Mar-2008 1 Initial release 08-Apr-2008 2 Removed all references to the standard version of the STM8 9/10
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 herein 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 assumes 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. If 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 whatsoever 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 PARTICULAR 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 AUTHORIZE REPRESENTATIVE OF ST, ST PRODUCTS ARE NOT DESIGNED, AUTHORIZED OR WARRANTED FOR USE IN MILITARY, 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. 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 whatsoever, 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. 2008 STMicroelectronics - All rights reserved STMicroelectronics group of companies Australia - Belgium - Brazil - Canada - China - Czech Republic - Finland - France - Germany - Hong Kong - India - Israel - Italy - Japan - Malaysia - Malta - Morocco - Singapore - Spain - Sweden - Switzerland - United Kingdom - United States of America www.st.com 10/10