00001
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057 #ifdef __GNUC__
00058 #include <avr32/io.h>
00059 #elif __ICCAVR32__
00060 #include <avr32/ioap7000.h>
00061 #else
00062 #error No known compiler used
00063 #endif
00064 #include "spi_at32ap7000.h"
00065 #include "gpio.h"
00066
00067
00068
00070 #define CPUHZ 20000000
00071
00072 #define BUFFERSIZE 64
00073
00074
00075 #define TIMEOUT CPUHZ/200
00076
00077 #define SPI_BITS 8
00078
00079 #define SPI_SLAVE_SPEED 160000
00080
00081 #define SPI_MASTER_SPEED 125000
00082
00083
00084 #ifdef __ICCAVR32__
00085 #define AVR32_SPI0_MISO_0_PIN 0
00086 #define AVR32_SPI0_MISO_0_FUNCTION 0
00087 #define AVR32_SPI0_MOSI_0_PIN 1
00088 #define AVR32_SPI0_MOSI_0_FUNCTION 0
00089 #define AVR32_SPI0_NPCS_0_PIN 3
00090 #define AVR32_SPI0_NPCS_0_FUNCTION 0
00091 #define AVR32_SPI0_NPCS_1_PIN 4
00092 #define AVR32_SPI0_NPCS_1_FUNCTION 0
00093 #define AVR32_SPI0_NPCS_2_PIN 5
00094 #define AVR32_SPI0_NPCS_2_FUNCTION 0
00095 #define AVR32_SPI0_NPCS_3_PIN 20
00096 #define AVR32_SPI0_NPCS_3_FUNCTION 1
00097 #define AVR32_SPI0_SCK_0_PIN 2
00098 #define AVR32_SPI0_SCK_0_FUNCTION 0
00099 #define AVR32_SPI1_MISO_0_PIN 32
00100 #define AVR32_SPI1_MISO_0_FUNCTION 1
00101 #define AVR32_SPI1_MOSI_0_PIN 33
00102 #define AVR32_SPI1_MOSI_0_FUNCTION 1
00103 #define AVR32_SPI1_NPCS_3_PIN 27
00104 #define AVR32_SPI1_NPCS_3_FUNCTION 0
00105 #define AVR32_SPI1_NPCS_0_PIN 34
00106 #define AVR32_SPI1_NPCS_0_FUNCTION 1
00107 #define AVR32_SPI1_NPCS_1_PIN 35
00108 #define AVR32_SPI1_NPCS_1_FUNCTION 1
00109 #define AVR32_SPI1_NPCS_2_PIN 36
00110 #define AVR32_SPI1_NPCS_2_FUNCTION 1
00111 #define AVR32_SPI1_SCK_0_PIN 37
00112 #define AVR32_SPI1_SCK_0_FUNCTION 1
00113 #endif
00114
00116 typedef unsigned char avr32_piomap_t[][2];
00117
00118 void init_spiMaster(volatile avr32_spi_t *spi, long cpuHz);
00119 void init_spiSlave(volatile avr32_spi_t *spi, long cpuHz);
00120 void rgb_setColor(volatile avr32_pio_t *pio, unsigned char color);
00121 void rgb_blinkColor(volatile avr32_pio_t *pio, unsigned char color);
00122 void spi_masterSend(volatile avr32_spi_t *spi, char *string);
00123 void spi_slaveReceive_and_compare(volatile avr32_spi_t *spi, char *string);
00124
00130 void init_spiMaster(volatile avr32_spi_t *spi, long cpuHz)
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
00145 spi_initMaster(spi, &spiOptions);
00146
00147
00148 spi_selectionMode(spi, 0, 0, 0);
00149
00150
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 }
00161
00162
00168 void init_spiSlave(volatile avr32_spi_t *spi, long cpuHz)
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
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 }
00194
00195
00207 void rgb_setColor(volatile avr32_pio_t *pio, unsigned char color)
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 }
00233
00234
00246 void rgb_blinkColor(volatile avr32_pio_t *pio, unsigned char color)
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 }
00267
00268
00274 void spi_masterSend(volatile avr32_spi_t *spi, char *string)
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 }
00293
00294
00301 void spi_slaveReceiveAndCompare(volatile avr32_spi_t *spi, char *string)
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
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
00336 do {
00337 if ((receiveBuffer[index++] & 0x00FF)
00338 != ((*textStringPtr++) & 0x00FF)) {
00339 ++error;
00340 }
00341 } while (*textStringPtr != '\n');
00342
00343
00344
00345
00346
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 }
00360
00361
00366 int main(void)
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
00385 pioa->idr = 0xFFFFffff;
00386 piob->idr = 0xFFFFffff;
00387 pioc->idr = 0xFFFFffff;
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
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
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
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
00443
00444
00445
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
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 }