STMicroelectronics AN1800 Str71x bspi used as a uart Datasheet

AN1800
APPLICATION NOTE
STR71x BSPI
USED AS A UART
INTRODUCTION
This document presents a standard communication interface between a STR71x microcontroller and a PC. This communication is done through the STR71x BSPI peripheral and a serial
port of the PC using the RS232 protocol.
Rev. 3
AN1800/0405
1/7
1
STR71x BSPI USED AS A UART
1 BSPI COMMUNICATION
The main features of the BSPI communication peripheral are summarized below. Refer to the
STR71x reference manual for more details.
1.1 MAIN FEATURES
The Buffered Serial Peripheral Interface (BSPI) offers a flexible means of full-duplex data exchange with external equipment requiring an industry standard synchronous serial data
format.
The BSPI allows a very wide range of baud rates with different baud rates for transmission and
reception.
For BSPI, only four signals are needed, the first for transmission (MOSI), the second for reception (MISO), the third is the clock (SCLK) and the other is slave select (SS).
1.2 BAUD RATES
Transmission and reception can be driven by their own baud rate generator. However be
aware that to communicate correctly, the receiver must have a reception baud rate strictly
equal to the transmission baud rate of the transmitter. As long as this condition is met, a wide
range of baud rates is possible.
1.3 FRAMES
The length of a data word can be 8 or 16 bits, these bits are transmitted Most Significant Bit
first.
2/7
2
STR71x BSPI USED AS A UART
2 RS232 COMMUNICATION WITH A PC
2.1 MAIN FEATURES
The electrical and protocol characteristics of RS232 are different from those of the STR71X
BSPI peripheral. In RS232 communication, high level is typically +7V and low level is typically
-7V, while the BSPI peripheral works with (0, +3.3V).
Furthermore, the polarities are different. A ’1’ bit coming from the BSPI corresponds to a ’0’ bit
in RS232, and a ’0’ bit to a ’1’ bit. So it is necessary to implement a conversion between the PC
and the BSPI peripheral. In this application ST3232 is used for this purpose.
Also the BSPI transmits Most Significant Bit first but the PC receives the Least Significant Bit
first. That’s why we must invert the order before transmitting the data.
2.2 PC CONFIGURATION
The PC will be used as a terminal. The description below refers to the Windows environment.
This terminal application is called HyperTerminal.
Under Windows, open the “HyperTerminal” application. To configure it, go to the communication parameters menu. The options of this window must be the same as the ones defined with
your STR71x.
After selecting the right serial communication port, select the same baud rate as the one configured in the microcontroller. As the PC accepts only one baud rate, transmission and reception baud rates will have the same value. Data word length will be 7 bits, and choose to use 1
stop bit. “Flow control” can be either Xon/Xoff or none. The PC is then correctly configured.
3/7
STR71x BSPI USED AS A UART
3 STR71X CONFIGURATION
This application was implemented with a STR71X. This microcontroller uses a 16 MHz external clock (quartz). A description of the baud rate selection is given later in this application
note.
3.1 GENERAL INITIALIZATION
Four pins of the STR71x are used:
■
the MOSI pin (Master Output Slave Input).
■
the MISO pin (Master Input, Slave Output).
■
the SCLK clock pin.
■
the SS slave select pin.
The MOSI, MISO and SCLK pins must be initialized as Push-Pull (please refer to the Reference Manual).
3.2 BSPI CONFIGURATION
The peripheral is configured as a master by using function BSPI_MasterEnable, and the Baud
Rate is fixed by using function BSPI_ClockDividerConfig (please refer to the STR71x Software Library User Manual for more information).
3.2.1 EXAMPLE
Then the baud rates must be selected. We should always have the same baud rates in the PC
and in the BSPI.
4/7
Baud Rate
Clock Divider
19200
0xCF
38400
0x67
57600
0x40
115200
0x20
STR71x BSPI USED AS A UART
4 COMMUNICATION BETWEEN STR71x AND PC VIA RS232
In this application note the software modules are developed in C language, with RVDK environment, using the STR71x software library from STMicroelectronics.
It is of course only an example. It is up to you to adapt it to your specific application.
4.1 GENERAL DESCRIPTION
In this application, an STR71x is connected to a PC. The communication is performed using
the “HyperTerminal” application of Windows. Characters are sent by the STR71x in ASCII
code to the PC with a baudrate set to 19200 bps (ClockDivider = 0xCF) and 1 stop bit.
The first part of the program performs the initialization of the STR71x, core and the BSPI peripheral.
4.2 HARDWARE
The STR71x BSPI peripheral cannot be directly connected to a PC, as it uses the RS232 protocol.
The conversion between BSPI and RS232 can be done using ST3232. An overview schematic is presented in the figure below:
Figure 1. Hardware Overview
STR71x
RS232 interface
GND
TD
RD
GND
GND
IN
OUT
OUT
IN
VCC
MISO
MOSI
SS
ST3232
Be sure that the three main devices (PC, STR71x, ST3232) have the same electrical reference (GND).
5/7
STR71x BSPI USED AS A UART
4.3 SOFTWARE
Figure 2. Application flowchart
BEGIN
Configure the BSPI pin to Push-Pull
Initialize BSPI peripheral
{
...
GPIO_Config (GPIO0, 0x0077, GPIO_AF_PP);
BSPI_Init (BSPI1) ;
Configure the clock divider
BSPI_ClockDividerConfig (BSPI1, 0xCF);
Enable the BSPI peripheral
BSPI_Enable (BSPI1 , ENABLE);
Select BSPI master mode
BSPI_MasterEnable (BSPI1, ENABLE);
Configure the clock
Enable capturing the first data sample on the
first edge of SCK or the second edge
Set the word length
Send word (invert the order before the transmission of the data)
BSPI_ClkActiveHigh(BSPI1, DISABLE);
BSPI_ClkFEdge(BSPI1, ENABLE);
BSPI_8bLEn(BSPI1, ENABLE);
BSPI_WordSend(BSPI1, 0x41);
...
}
END
6/7
STR71x BSPI USED AS A UART
“THE PRESENT NOTE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS WITH INFORMATION
REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. AS A RESULT, STMICROELECTRONICS
SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO
ANY CLAIMS ARISING FROM THE CONTENT OF SUCH A NOTE AND/OR THE USE MADE BY CUSTOMERS OF
THE INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.”
Information furnished is believed to be accurate and reliable. However, STMicroelectronics assumes no responsibility for the consequences
of use of such information nor for any infringement of patents or other rights of third parties which may result from its use. No license is granted
by implication or otherwise under any patent or patent rights of STMicroelectronics. Specifications mentioned in this publication are subject
to change without notice. This publication supersedes and replaces all information previously supplied. STMicroelectronics products are not
authorized for use as critical components in life support devices or systems without express written approval of STMicroelectronics.
The ST logo is a registered trademark of STMicroelectronics.
All other names are the property of their respective owners
© 2005 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
7/7
Similar pages