ST AN1445 Application note

AN1445
APPLICATION NOTE
USING THE ST7 SPI TO EMULATE A 16-BIT SLAVE
By Microcontroller Division Applications
INTRODUCTION
This application note describes how to emulate a 16-bit slave SPI using an ST7 microcon­troller with an on-chip 8-bit SPI.
bit 0 bit 1 bit 2 bit 3 bit 4 bit 5 bit 6 bit 7 bit 8 bit 9bit 10 bit 11bit 12bit 13bit 14bit 15
1 PRINCIPLE
The ST7 SPI cell has a double buffer for receiving data us ing two 8-bit registers: a read reg­ister and a shift register (see Figure 2.). The application software accesses the read register to retrieve the received data. The 8-bit shift register is managed by hardware to receive the 8 bits of each byte. As each bit is received, it is shifted into the s h ift register . During byte reception, the read regist er is no t cha nge d. It co ntains the pr evio usly r eceiv ed by te wh ich c an still b e read by soft wa re. At the en d o f b yte r ecep tion, the 8-bi t shift re gister is c op ied i nto the r ea d register.
This double buffering makes it possible to receive 16-bit words. At the end of reception of the first byte, the shift register is copied into the read register, the SPIF flag is set and an interrupt can be generated. The next in-coming byte will be received in the shift register while the first byte is available in the read regi ster. In order not to lose any bits, the software mus t be fast enough to read the first byte before the end of the reception of the second one.
Note: The SPISR (SPI Status Regi ster) is also called SP ICSR (SPI Contr ol/Status Re gister) depending on which ST7 microcontroller device you use. In this application note, we’ll use the name SPISR for the status register.
AN1445/1101 1/7
1
USING THE ST7 SPI TO EM ULATE A 16-BIT SLAVE
Figure 2. Data Register Block diagram
INTERNAL BUS
DATA REGISTER
READ REGISTER
MOSI MISO
SHIFT REGISTER
8-BIT
2 SOFTWARE
Figure 3. C code (COSMIC C Com piler) for the interrupt routine
@interrupt @nostack void SPI_Interrupt(void) { volatile TwoBytes twobytesDR;
if (ValBit(SPISR,SPIF)) // if a byte is received (SPIF=1) + first step to clear int flags {
twobytesDR.b_form.low=SPIDR; //1st byte storage while(ValBit(SPISR,SPIF));
twobytesDR.b_form.high=SPIDR; //2nd byte storage } else // then MODF flag caused the interrupt -> add your own code here {
SPICR=0xC4; // second step to clear MODF flag: write access to SPICR (init value) }
}
2/7
2
USING THE ST7 SPI TO EMULATE A 16-BIT SLA VE
Figure 4. Disassembled interrupt routine code
_SPI_Interrupt:
btjf _SPISR,#7,L501 ld a,_SPIDR ld _SPI_Interrupt$L-1,a
L311:
btjt _SPISR,#7,L311 ld a,_SPIDR ld _SPI_Interrupt$L-2,a iret
L501:
ld a,#196 ld _SPICR,a iret
where:
typedef unsigned char u8; /* unsigned 8 bit type definition */ typedef signed char s8; /* signed 8 bit type definition */ typedef unsigned int u16; / * unsigned 16 bit type definition */ typedef signed int s16; /* signed 16 bit type definition */ typedef unsigned long u32; / * unsigned 32 bit type definition */ typedef signed long s32; / * signed 32 bit type definition */
typedef union { /* unsigned 16 bit type for 8 & 16 */
u16 w_form; /* bit accesses: 16> var.w_form */ struct { /* 8> var.b_form.high/low */
u8 high, low;
} b_form;
} TwoBytes;
Note: On some devices, another flag called OVR (overrun) can also cause an SPI interrupt to occur. In this case, you will have to add some code to the interrupt routine to handle this.
3/7
Loading...
+ 4 hidden pages