00001
00022 #include <avr32/io.h>
00023 #include "settings.h"
00024 #include "usart.h"
00025 #include "pdc.h"
00026
00027 #ifdef __GNUC__
00028 #include "interrupt_gcc.h"
00029 #elif __IAR__
00030 #include "interrupt_iar.h"
00031 #endif
00032
00033 #define RGB_OFF 0
00034 #define RGB_RED 1
00035 #define RGB_GREEN 2
00036 #define RGB_BLUE 3
00037 #define RGB_ORANGE 4
00038 #define RGB_WHITE 5
00039
00040 #define MODE_POLLED 0
00041 #define MODE_PDC 1
00042 #define MODE_INT 2
00043
00044
00045 char * string;
00046 int stringSize;
00047 char * stringPDC;
00048 int stringSizePDC;
00049 volatile avr32_pio_t * pioa;
00050 volatile avr32_pio_t * piob;
00051 volatile avr32_usart_t * usart0;
00052 unsigned char runningleds_status;
00053
00054 void delay()
00055 {
00056 unsigned int timeout = TIMEOUT;
00057
00058 do {
00059 --timeout;
00060 } while ( timeout > 0 );
00061 }
00062
00063
00064 void runningleds()
00065 {
00066 runningleds_status <<= 1;
00067 runningleds_status ^= ( runningleds_status ^ 0x80 ) >> 7;
00068 piob->codr = 0x000000FF;
00069 piob->sodr = (long)runningleds_status;
00070 }
00071
00082 void rgb_setColor( unsigned char color )
00083 {
00084 piob->codr = 0x003F0000;
00085
00086 switch( color ) {
00087 case RGB_OFF:
00088 break;
00089 case RGB_RED:
00090 piob->sodr = 0x00030000;
00091 break;
00092 case RGB_GREEN:
00093 piob->sodr = 0x000C0000;
00094 break;
00095 case RGB_BLUE:
00096 piob->sodr = 0x00300000;
00097 break;
00098 case RGB_ORANGE:
00099 piob->sodr = 0x000F0000;
00100 break;
00101 case RGB_WHITE:
00102 piob->sodr = 0x003F0000;
00103 break;
00104 default:
00105 break;
00106 }
00107 }
00108
00109
00110 int main( int argc, char * argv[] )
00111 {
00112 struct usart_options_t usartOptions;
00113
00114
00115 pioa = (volatile avr32_pio_t *) AVR32_PIOA_ADDRESS;
00116 piob = (volatile avr32_pio_t *) AVR32_PIOB_ADDRESS;
00117
00118
00119 usart0 = (volatile avr32_usart_t *) AVR32_USART0_ADDRESS;
00120
00121
00122 pioa->idr = 0xFFFFffff;
00123 piob->idr = 0xFFFFffff;
00124
00125
00126
00127
00128
00129 pioa->pdr = 0x0C000000;
00130 pioa->asr = 0x0C000000;
00131
00132
00133
00134
00135
00136 piob->per = 0x000000FF;
00137 piob->oer = 0x00FF00FF;
00138 piob->odr = 0x0000FF00;
00139 piob->ifer = 0x0000FF00;
00140 piob->codr = 0xFFFFffff;
00141 piob->ower = 0x00FF00FF;
00142
00143
00144 usartOptions.baudrate = USART_BAUD;
00145 usartOptions.charlength = USART_BITS;
00146 usartOptions.paritytype = USART_NO_PARITY;
00147 usartOptions.stopbits = USART_1_STOPBIT;
00148 usartOptions.channelmode = USART_NORMAL_CHMODE;
00149
00150
00151 usart_init2( usart0, &usartOptions, CPUHZ );
00152
00153 string = "POL: AVR32 PDC application demo\r\n";
00154 stringPDC = "PDC: AVR32 PDC application demo\r\n";
00155
00156 stringSize = 33;
00157 stringSizePDC = 33;
00158
00159 pdc_flushCache( string, stringSize );
00160 pdc_flushCache( stringPDC, stringSizePDC );
00161
00162 int mode = MODE_POLLED;
00163 rgb_setColor(RGB_RED);
00164 runningleds_status = 0;
00165
00166 for( ;; ) {
00167
00168
00169
00170
00171
00172
00173 if( (piob->pdsr & (1<<AVR32_PIO_P15)) == 0 ) {
00174 rgb_setColor(RGB_RED);
00175 mode = MODE_POLLED;
00176 } else if( (piob->pdsr & (1<<AVR32_PIO_P14)) == 0 ) {
00177 rgb_setColor(RGB_ORANGE);
00178 mode = MODE_PDC;
00179 } else if( (piob->pdsr & (1<<AVR32_PIO_P13)) == 0 ) {
00180 rgb_setColor(RGB_GREEN);
00181 mode = MODE_INT;
00182 }
00183
00184
00185 if( mode == MODE_POLLED ) {
00186 usart_writeLine( usart0, string );
00187 }
00188 else if( mode == MODE_PDC ) {
00189 if( pdc_txBytesLeft( (void *) usart0 ) == 0 ) {
00190 pdc_setTxBuf( (void *) usart0,
00191 pdc_translatePtr( stringPDC ),
00192 stringSizePDC,
00193 0,
00194 0 );
00195 }
00196 }
00197 else if( mode == MODE_INT ) {
00198 int interrupts_on = 1;
00199
00200 start_interrupts();
00201
00202 do {
00203
00204 if( (piob->pdsr & (1<<AVR32_PIO_P15)) == 0 ) {
00205 rgb_setColor(RGB_RED);
00206 mode = MODE_POLLED;
00207 interrupts_on = 0;
00208 } else if( (piob->pdsr & (1<<AVR32_PIO_P14)) == 0 ) {
00209 rgb_setColor(RGB_ORANGE);
00210 mode = MODE_PDC;
00211 interrupts_on = 0;
00212 }
00213
00214 runningleds();
00215 delay();
00216 } while( interrupts_on == 1 );
00217
00218 stop_interrupts();
00219 }
00220
00221 runningleds();
00222 delay();
00223 }
00224 }
00225