DOC PREVIEW
UNCC ECGR 4101 - Study Notes

This preview shows page 1 out of 4 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Joe JohnsonCOE, UNC Charlotte12-11-2004Using USART of Renesas M16C262 for SPI CommunicationSPI (Serial Peripheral Interface) is a standard communications protocol for connecting devices to a microcontroller. This document shows how to use a microcontroller’s uart controller to emulate SPI communications. The code is written for the Renesas (Mitsubishi) M16C262 MCU, but the concept can easily ported to any other device.SPI is a 4 wire serial communications bus. It has a relatively simple protocol which is similar to I2C or RS232 communications. Data is sent in chunks of 8-bits. The bus is bi-directional, so data can be sent and received simultaneously. Several devices can be attached to the bus. One of the devices is dedicated as the bus master and controls the chip select for all of the slave devices.The SPI signals are:- CSn (chip select) - Used by bus master to select the slave device for communications- SCLK - bus clock. It is driven by the transmitting device- SI - serial input for receiving data- SO - serial output for transmitting dataSPI communications can work in four different modes. The four modes are dictated by the SCLK polarity and the phase in which the data is sampled. For instance, the Chipcon CC2420 transceiver device is specified to operate in SPI mode 1. This means that the SCLK should idle low (CPOL= 0) and the data is sampled at the positive clock transition (CPHA = 1). The table below shows the four SPI modes of operation.SPI Mode CPOL CPHA0 0 01 0 12 1 03 1 1Some MCUs do not have a hardware SPI controller. One option for establishing SPI communications is to use available IO pins and write software to emulate the SPI protocol. This can lead to significant overhead of the data processing in the MCU. This is not recommended for high-speed or real-time systems. One way to overcome this problem is to use the UART hardware unit on the MCU for SPI communications. Most MCUs have built-in UART peripheral controllers. Many have multiple UARTs which can be used in either synchronous or asynchronous modes.The code written below shows how to use the UART of the Renesas (Mitsubishi) M16C262 MCU in synchronous mode to emulate the SPI mode 3 protocol. It was found that the uart clock was pulled up when the device is inactive. Therefore, it can only be used for modes 2 and 3 of operation. However, many devices do not care about the idle polarity of the SCLK so the mode of operation is solely dependent on the data sample polarity. For example, a MCU using SPI mode 3 can communicate with a device that is specified for SPI mode 1. This code is a modification of the code presented in the Renesas Application Note: Using the M16C/62 UART for SPI Communications[1] .//SPI definitions, using uart0 for SPI emulation#define CSn p6_0 #define SCLK p6_1#define SI p6_2#define SO p6_3/*****************************************************************************Name: init_SPIParameters: noneReturns: noneDescription: configure uart0 to be compatible with SPI MODE 3SPI MODE 3 = SCLK is normally high, posedge clocked dataNote: the cc2420 is specified to work in spi mode 1, however it was found that it can also work in spi mode 3. The only difference is that SCLK is normally low for spi mode 1.*****************************************************************************/void init_spi(void){ u0mr = 0x01; // internal clk, synchronous mode u0c0 = 0x90; // msb first, CKPOL=0, no CTS/RTS, clk is f1 u0c1 = 0x00; // no data reverse, rx & tx disabled u0brg = 0x2F; // data rate, really slow. This can be adjusted according to the required data rate}/*****************************************************************************Name: spi_txParameters: charReturns: noneDescription: transmits 8 bits of data to the spi busnote: enable the slave before running this routine, CSn = 0;*****************************************************************************/void spi_tx(char c){ te_u0c1 = 1; //enable tx uart_tx(c); uart_end();}/*****************************************************************************Name: spi_rxParameters: noneReturns: charDescription: receives 8 bits of data from the spi busnote: enable the slave before running this routine, CSn = 0;*****************************************************************************/char spi_rx(void){ char temp; te_u0c1 = 1; //enable tx uart_enable_rx(); temp = uart_rx(); uart_end(); return temp; }/*****************************************************************************UART support routines for SPI emulation*****************************************************************************///transmits a charactervoid uart_tx(char c){ while(!ti_u0c1); //wait for tx buffer to empty u0tbl = c; //write the char to the tx buffer}//receives a characterchar uart_rx(void){ uart_tx(0xFF); //dummy tx to force a rx while(!ri_u0c1); //wait for rx buffer to empty return(u0rbl); //return the char, low byte}void uart_enable_rx(void){ while(!ti_u0c1); //wait for tx buffer to empty while(!txept_u0c0); //wait for tx register to empty re_u0c1 = 1; //enable rx}void uart_end(void){ while(!ti_u0c1); //wait for tx buffer to empty while(!txept_u0c0); //wait for tx register to empty re_u0c1 = 0; //disable rx te_u0c1 = 0; //disable tx ckpol_u0c0 = 0; //let SCLK idle high}Sample program using the M16C262 to communicate with a Chipcon CC2420 ZigbeeTM transceiver via SPI. The MCU will initialize the CC2420 for data reception. It will read data from the CC2420 FIFO when data is present.// CC2420 port definitions#define FIFO p8_0#define FIFOP p8_3#define CCA#define SFD p8_5#define RESETn p8_1#define VREG_EN p8_2//--------------------- Global Variables ---------------------------------------char temp;int i = 0;char message[2][9] = { // LCD banner display{" TEMP = "},{" C "}};// 0, 1void main(void){//ports setuppd6_0 = 1; //output port, slave selectpd6_4 = 0; //input port, fifopd8_1 = 1; //output port, resetnpd8_2 = 1; //output port, vreg_enpd8_0 = 0; //input port, FIFOpd8_3 = 0; //input port, FIFOPpd8_5 = 0; //input port, SFDp6 = 0xEF; //set all pins, except FIFO, initially highpu15 = 1; //pullups on p6_4 - p6_7init_spi();init_disp();ENABLE_LEDSALL_LED_OFF//cc2420 setupVREG_EN = 1; for(i=0; i<1000; i++); //turn-on power to cc2420RESETn = 0; for(i=0; i<1000; i++) ; //reset the cc2420RESETn = 1; for(i=0; i<1000; i++); //reset releaseCSn =


View Full Document

UNCC ECGR 4101 - Study Notes

Documents in this Course
Load more
Download Study Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Study Notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Study Notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?