This file gives an example of using the SPI bus driver for both slave and master mode. It sends a text string to a slave in master mode, and in slave mode it receives data and try to compare this data with an expected string.
LEDs on STK1000 is used to give feedback to the user. The switches on STK1000 is used to change mode and send text strings.
Definition in file spi_example.c.
#include <avr32/io.h>
#include "spi_at32ap7000.h"
#include "gpio.h"
Go to the source code of this file.
Defines | |
#define | BUFFERSIZE 64 |
Number of bytes in the receive buffer when operating in slave mode. | |
#define | CPUHZ 20000000 |
CPU core speed in Hz. | |
#define | SPI_BITS 8 |
Number of bits in each SPI package. | |
#define | SPI_MASTER_SPEED 125000 |
SPI master speed in Hz. | |
#define | SPI_SLAVE_SPEED 160000 |
SPI slave speed in Hz. | |
#define | TIMEOUT CPUHZ/200 |
A adjustable delay avoiding multiple requests on the switches. | |
Typedefs | |
typedef unsigned char | avr32_piomap_t [][2] |
Map over PIO for setup of peripherals. | |
Functions | |
void | init_spiMaster (volatile avr32_spi_t *spi, long cpuHz) |
initialise SPI in master mode | |
void | init_spiSlave (volatile avr32_spi_t *spi, long cpuHz) |
initialise SPI in slave mode | |
int | main (void) |
Main function, executing starts here. | |
void | rgb_blinkColor (volatile avr32_pio_t *pio, unsigned char color) |
Function which blinks the RGB leds in a given color. | |
void | rgb_setColor (volatile avr32_pio_t *pio, unsigned char color) |
Function to set the color of the RGB LEDs. | |
void | spi_masterSend (volatile avr32_spi_t *spi, char *string) |
Send a text string to the SPI. | |
void | spi_slaveReceive_and_compare (volatile avr32_spi_t *spi, char *string) |
void | spi_slaveReceiveAndCompare (volatile avr32_spi_t *spi, char *string) |
Receive a text and compare it to excpected text. |
#define BUFFERSIZE 64 |
Number of bytes in the receive buffer when operating in slave mode.
Definition at line 72 of file spi_example.c.
Referenced by spi_slaveReceiveAndCompare().
#define CPUHZ 20000000 |
#define SPI_BITS 8 |
Number of bits in each SPI package.
Definition at line 77 of file spi_example.c.
Referenced by init_spiMaster(), and init_spiSlave().
#define SPI_MASTER_SPEED 125000 |
SPI master speed in Hz.
Definition at line 81 of file spi_example.c.
Referenced by init_spiMaster().
#define SPI_SLAVE_SPEED 160000 |
#define TIMEOUT CPUHZ/200 |
A adjustable delay avoiding multiple requests on the switches.
Definition at line 75 of file spi_example.c.
Referenced by main(), and rgb_blinkColor().
typedef unsigned char avr32_piomap_t[][2] |
void init_spiMaster | ( | volatile avr32_spi_t * | spi, | |
long | cpuHz | |||
) |
initialise SPI in master mode
spi | Pointer to the correct avr32_spi_t struct | |
cpuHz | CPU clock frequency in Hz |
Definition at line 130 of file spi_example.c.
References spi_options_t::baudrate, spi_options_t::bits, spi_options_t::modfdis, spi_options_t::reg, rgb_setColor(), spi_options_t::spck_delay, SPI_BITS, spi_enable(), spi_initMaster(), SPI_MASTER_SPEED, spi_options_t::spi_mode, spi_selectChip(), spi_selectionMode(), spi_setupChipReg(), spi_options_t::stay_act, and spi_options_t::trans_delay.
Referenced by main().
00131 { 00132 volatile avr32_pio_t *pioc = &AVR32_PIOC; 00133 spi_options_t spiOptions; 00134 00135 spiOptions.reg = 0; 00136 spiOptions.baudrate = SPI_MASTER_SPEED; 00137 spiOptions.bits = SPI_BITS; 00138 spiOptions.spck_delay = 0; 00139 spiOptions.trans_delay = 4; 00140 spiOptions.stay_act = 0; 00141 spiOptions.spi_mode = 1; 00142 spiOptions.modfdis = 0; 00143 00144 /* Initialize as master */ 00145 spi_initMaster(spi, &spiOptions); 00146 00147 /* Set master mode; variable_ps, pcs_decode, delay */ 00148 spi_selectionMode(spi, 0, 0, 0); 00149 00150 /* Select slave chip 0 (SPI_NPCS0) */ 00151 spi_selectChip(spi, 0); 00152 00153 spi_setupChipReg(spi, &spiOptions, cpuHz); 00154 00155 spi_enable(spi); 00156 00157 pioc->sodr = (1<<AVR32_PIO_P7)|(1<<AVR32_PIO_P0); 00158 pioc->codr = (1<<AVR32_PIO_P6)|(1<<AVR32_PIO_P1); 00159 rgb_setColor(pioc, 0); 00160 }
void init_spiSlave | ( | volatile avr32_spi_t * | spi, | |
long | cpuHz | |||
) |
initialise SPI in slave mode
spi | Pointer to the correct avr32_spi_t struct | |
cpuHz | CPU clock frequency in Hz |
Definition at line 168 of file spi_example.c.
References spi_options_t::baudrate, spi_options_t::bits, spi_options_t::modfdis, spi_options_t::reg, rgb_setColor(), spi_options_t::spck_delay, SPI_BITS, spi_enable(), spi_initSlave(), spi_options_t::spi_mode, spi_setupChipReg(), SPI_SLAVE_SPEED, spi_options_t::stay_act, and spi_options_t::trans_delay.
Referenced by main().
00169 { 00170 volatile avr32_pio_t *pioc = &AVR32_PIOC; 00171 00172 spi_options_t spiOptions; 00173 00174 spiOptions.reg = 0; 00175 spiOptions.baudrate = SPI_SLAVE_SPEED; 00176 spiOptions.bits = SPI_BITS; 00177 spiOptions.spck_delay = 0; 00178 spiOptions.trans_delay = 4; 00179 spiOptions.stay_act = 0; 00180 spiOptions.spi_mode = 1; 00181 spiOptions.modfdis = 0; 00182 00183 /* Initialize as slave; bits, spi_mode */ 00184 spi_initSlave(spi, 8, 1); 00185 00186 spi_setupChipReg(spi, &spiOptions, cpuHz); 00187 00188 spi_enable(spi); 00189 00190 pioc->sodr = (1<<AVR32_PIO_P6)|(1<<AVR32_PIO_P0); 00191 pioc->codr = (1<<AVR32_PIO_P7)|(1<<AVR32_PIO_P1); 00192 rgb_setColor(pioc, 0); 00193 }
int main | ( | void | ) |
Main function, executing starts here.
Definition at line 366 of file spi_example.c.
References CPUHZ, init_spiMaster(), init_spiSlave(), rgb_setColor(), spi_masterSend(), spi_readRegisterFullCheck(), spi_slaveReceiveAndCompare(), and TIMEOUT.
00367 { 00368 volatile avr32_pio_t *pioa = &AVR32_PIOA; 00369 volatile avr32_pio_t *piob = &AVR32_PIOB; 00370 volatile avr32_pio_t *pioc = &AVR32_PIOC; 00371 volatile avr32_spi_t *spi = &AVR32_SPI0; 00372 char masterMode = 0; 00373 00374 gpio_map_t spi_piomap = { \ 00375 {AVR32_SPI0_SCK_0_PIN, AVR32_SPI0_SCK_0_FUNCTION}, \ 00376 {AVR32_SPI0_MISO_0_PIN, AVR32_SPI0_MISO_0_FUNCTION}, \ 00377 {AVR32_SPI0_MOSI_0_PIN, AVR32_SPI0_MOSI_0_FUNCTION}, \ 00378 {AVR32_SPI0_NPCS_0_PIN, AVR32_SPI0_NPCS_0_FUNCTION}, \ 00379 {AVR32_SPI0_NPCS_1_PIN, AVR32_SPI0_NPCS_1_FUNCTION}, \ 00380 {AVR32_SPI0_NPCS_2_PIN, AVR32_SPI0_NPCS_2_FUNCTION}, \ 00381 {AVR32_SPI0_NPCS_3_PIN, AVR32_SPI0_NPCS_3_FUNCTION}, \ 00382 }; 00383 00384 /* Disable all interrupts on PIO */ 00385 pioa->idr = 0xFFFFffff; 00386 piob->idr = 0xFFFFffff; 00387 pioc->idr = 0xFFFFffff; 00388 00389 /* Enable PIO on PIOB and clear all bits 00390 * This is used for status information on LEDS and signals from switches 00391 * 00392 * LED0 - system is on and ready 00393 * LED1 - system is receiving/sending data on SPI 00394 * LED3 - system is comparing received data from SPI with expected data 00395 * LED6 - system is in slave mode 00396 * LED7 - system is in master mode 00397 * 00398 * PIOB: P0 to P7 is used for switches 00399 * PIOC: P0 to P7 is used for LEDs 00400 * PIOC: P8 to P13 is used for RGB LEDs 00401 */ 00402 piob->per = 0x000000FF; 00403 piob->odr = 0x000000FF; 00404 piob->puer = 0x000000FF; 00405 00406 pioc->per = 0x0000FFFF; 00407 pioc->oer = 0x0000FFFF; 00408 pioc->ower = 0x0000FFFF; 00409 pioc->codr = 0x0000FFFF; 00410 00411 /* Init PIO */ 00412 gpio_enable_module(spi_piomap, 7); 00413 00414 pioc->codr = 0x000000FF; 00415 pioc->sodr = 0x000000AA; 00416 rgb_setColor(pioc, 1); 00417 00418 int buttonTimer = 0; 00419 char *textString = "Atmel AVR32 SPI test application\r\n"; 00420 char *textStringAlt = "AVR32 SPI Atmel test application\r\n"; 00421 char *textStringToLong = "This string is far to long to be used " \ 00422 "in Atmel AVR32 SPI test application\r\n"; 00423 00424 rgb_setColor(pioc, 0); 00425 init_spiSlave(spi, CPUHZ); 00426 00427 for (;;) { 00428 /* SW7 is used to switch between master and slave mode */ 00429 if ((piob->pdsr & AVR32_PIO_P7_MASK) == 0 00430 && buttonTimer <= 0) { 00431 buttonTimer = TIMEOUT; 00432 00433 if (masterMode == 1) { 00434 init_spiSlave(spi, CPUHZ); 00435 masterMode = 0; 00436 } else { 00437 init_spiMaster(spi, CPUHZ); 00438 masterMode = 1; 00439 } 00440 } 00441 00442 /* Actions for master mode 00443 * SW5 sends "Atmel AVR32 SPI test application\r\n" on SPI 00444 * SW4 sends "AVR32 SPI Atmel test application\r\n" on SPI 00445 * SW3 sends "This string is far to long to be used in Atmel AVR32 SPI test application\r\n" 00446 */ 00447 if (masterMode == 1) { 00448 if ((piob->pdsr & AVR32_PIO_P5_MASK) == 0 00449 && buttonTimer <= 0) { 00450 buttonTimer = TIMEOUT; 00451 spi_masterSend(spi, textString); 00452 } else if ((piob->pdsr & AVR32_PIO_P4_MASK) == 0 00453 && buttonTimer <= 0) { 00454 buttonTimer = TIMEOUT; 00455 spi_masterSend(spi, textStringAlt); 00456 } else if ((piob->pdsr & AVR32_PIO_P3_MASK) == 0 00457 && buttonTimer <= 0) { 00458 buttonTimer = TIMEOUT; 00459 spi_masterSend(spi, textStringToLong); 00460 } 00461 00462 /* Slave mode polls if there is an incoming message on SPI */ 00463 } else { 00464 if (spi_readRegisterFullCheck(spi) == 1) { 00465 spi_slaveReceiveAndCompare(spi, textString); 00466 } 00467 } 00468 00469 if (buttonTimer > 0) { 00470 --buttonTimer; 00471 } 00472 } 00473 }
void rgb_blinkColor | ( | volatile avr32_pio_t * | pio, | |
unsigned char | color | |||
) |
Function which blinks the RGB leds in a given color.
pio | Pointer to an avr32_pio_t struct for RBG blinking | |
color | The color of the LED when active
|
Definition at line 246 of file spi_example.c.
References rgb_setColor(), and TIMEOUT.
Referenced by spi_slaveReceiveAndCompare().
00247 { 00248 volatile int pauseTimer = TIMEOUT/4; 00249 volatile int pauseRepeat = 10; 00250 00251 do{ 00252 do{ 00253 --pauseTimer; 00254 } while (pauseTimer > 0); 00255 00256 pauseTimer = TIMEOUT/4; 00257 00258 if ((pauseRepeat % 2) == 1) { 00259 rgb_setColor(pio, 0); 00260 } else { 00261 rgb_setColor(pio, color); 00262 } 00263 00264 --pauseRepeat; 00265 } while (pauseRepeat > 0); 00266 }
void rgb_setColor | ( | volatile avr32_pio_t * | pio, | |
unsigned char | color | |||
) |
Function to set the color of the RGB LEDs.
pio | Pointer to an avr32_pio_t struct for RGB blinking | |
color | The color of the LED
|
Definition at line 207 of file spi_example.c.
Referenced by init_spiMaster(), init_spiSlave(), main(), rgb_blinkColor(), and spi_slaveReceiveAndCompare().
00208 { 00209 pio->codr = 0x00003F00; 00210 00211 switch(color) { 00212 case 0: 00213 break; 00214 case 1: 00215 pio->sodr = 0x00000300; 00216 break; 00217 case 2: 00218 pio->sodr = 0x00000C00; 00219 break; 00220 case 3: 00221 pio->sodr = 0x00003000; 00222 break; 00223 case 4: 00224 pio->sodr = 0x00000F00; 00225 break; 00226 case 5: 00227 pio->sodr = 0x00003F00; 00228 break; 00229 default: 00230 break; 00231 } 00232 }
void spi_masterSend | ( | volatile avr32_spi_t * | spi, | |
char * | string | |||
) |
Send a text string to the SPI.
spi | Pointer to the correct avr32_spi_t struct | |
string | The text string to send on the SPI buss, terminated with a \n |
Definition at line 274 of file spi_example.c.
References SPI_OK, and spi_write().
Referenced by main().
00275 { 00276 volatile avr32_pio_t *pioc = &AVR32_PIOC; 00277 int error; 00278 char *textStringPtr = string; 00279 00280 pioc->codr = (1<<AVR32_PIO_P0); 00281 pioc->sodr = (1<<AVR32_PIO_P1); 00282 00283 do { 00284 error = spi_write(spi, (unsigned short) *textStringPtr); 00285 } while (*textStringPtr++ != '\n'); 00286 00287 pioc->codr = (1<<AVR32_PIO_P1); 00288 pioc->sodr = (1<<AVR32_PIO_P0); 00289 00290 if (error != SPI_OK) { 00291 } 00292 }
void spi_slaveReceive_and_compare | ( | volatile avr32_spi_t * | spi, | |
char * | string | |||
) |
void spi_slaveReceiveAndCompare | ( | volatile avr32_spi_t * | spi, | |
char * | string | |||
) |
Receive a text and compare it to excpected text.
spi | Pointer to the correct avr32_spi_t struct | |
string | The text string which the received data is compared with. String is terminated with a |
Definition at line 301 of file spi_example.c.
References BUFFERSIZE, rgb_blinkColor(), rgb_setColor(), SPI_OK, and spi_read().
Referenced by main().
00302 { 00303 int error = 0; 00304 int index = 0; 00305 int receivedChars = 0; 00306 char *textStringPtr = string; 00307 unsigned short receiveBuffer[BUFFERSIZE]; 00308 volatile avr32_pio_t *pioc = &AVR32_PIOC; 00309 00310 pioc->sodr = AVR32_PIO_P1_MASK|AVR32_PIO_P3_MASK; 00311 rgb_setColor(pioc, 4); 00312 00313 do { 00314 int errVal = SPI_OK; 00315 00316 errVal = spi_read(spi, &receiveBuffer[index]); 00317 00318 if (errVal == SPI_OK) { 00319 ++index; 00320 ++receivedChars; 00321 } 00322 00323 /* break on buffer overflow */ 00324 if (receivedChars > BUFFERSIZE) { 00325 error = BUFFERSIZE + 1; 00326 break; 00327 } 00328 00329 } while (receiveBuffer[index - 1] != '\n'); 00330 00331 index = 0; 00332 pioc->codr = AVR32_PIO_P1_MASK; 00333 rgb_blinkColor(pioc, 4); 00334 00335 /* compare received buffer with expected text string */ 00336 do { 00337 if ((receiveBuffer[index++] & 0x00FF) 00338 != ((*textStringPtr++) & 0x00FF)) { 00339 ++error; 00340 } 00341 } while (*textStringPtr != '\n'); 00342 00343 /* print result on RGB LEDs 00344 * error > BUFFERSIZE - buffer overflow 00345 * error > 0 - string mismatch 00346 * error = 0 - no error 00347 */ 00348 if (error > BUFFERSIZE) { 00349 rgb_blinkColor(pioc, 1); 00350 rgb_setColor(pioc, 1); 00351 } 00352 else if (error > 0) { 00353 rgb_setColor(pioc, 1); 00354 } else { 00355 rgb_setColor(pioc, 2); 00356 } 00357 00358 pioc->codr = (1<<AVR32_PIO_P3); 00359 }