pdc_example.c

Go to the documentation of this file.
00001 /*This file has been prepared for Doxygen automatic documentation generation.*/
00022 /* Copyright (c) 2006, Atmel Corporation All rights reserved.
00023  *
00024  * Redistribution and use in source and binary forms, with or without
00025  * modification, are permitted provided that the following conditions are met:
00026  *
00027  * 1. Redistributions of source code must retain the above copyright notice,
00028  * this list of conditions and the following disclaimer.
00029  *
00030  * 2. Redistributions in binary form must reproduce the above copyright notice,
00031  * this list of conditions and the following disclaimer in the documentation
00032  * and/or other materials provided with the distribution.
00033  *
00034  * 3. The name of ATMEL may not be used to endorse or promote products derived
00035  * from this software without specific prior written permission.
00036  *
00037  * THIS SOFTWARE IS PROVIDED BY ATMEL ``AS IS'' AND ANY EXPRESS OR IMPLIED
00038  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00039  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND
00040  * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
00041  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00042  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00043  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00044  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00045  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00046  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00047  */
00048 
00049 #ifdef __GNUC__
00050 #include <avr32/io.h>
00051 #elif __ICCAVR32__
00052 #include <avr32/ioap7000.h>
00053 #else
00054 #error No known compiler used
00055 #endif
00056 #include "settings.h"
00057 #include "usart.h"
00058 #include "pdc.h"
00059 
00060 #ifdef __GNUC__
00061 #include "interrupt_gcc.h"
00062 #elif __ICCAVR32__
00063 #include "interrupt_iar.h"
00064 #if __PART__ == __AP7000__
00065 #define AVR32_USART0_CLK_0_PIN             10
00066 #define AVR32_USART0_CLK_0_FUNCTION        1
00067 #define AVR32_USART0_CTS_0_PIN             7
00068 #define AVR32_USART0_CTS_0_FUNCTION        1
00069 #define AVR32_USART0_RTS_0_PIN             6
00070 #define AVR32_USART0_RTS_0_FUNCTION        1
00071 #define AVR32_USART0_RXD_0_PIN             8
00072 #define AVR32_USART0_RXD_0_FUNCTION        1
00073 #define AVR32_USART0_TXD_0_PIN             9
00074 #define AVR32_USART0_TXD_0_FUNCTION        1
00075 #define AVR32_USART1_CLK_0_PIN             16
00076 #define AVR32_USART1_CLK_0_FUNCTION        0
00077 #define AVR32_USART1_CTS_0_PIN             20
00078 #define AVR32_USART1_CTS_0_FUNCTION        0
00079 #define AVR32_USART1_RTS_0_PIN             19
00080 #define AVR32_USART1_RTS_0_FUNCTION        0
00081 #define AVR32_USART1_RXD_0_PIN             17
00082 #define AVR32_USART1_RXD_0_FUNCTION        0
00083 #define AVR32_USART1_TXD_0_PIN             18
00084 #define AVR32_USART1_TXD_0_FUNCTION        0
00085 #define AVR32_USART2_CLK_0_PIN             60
00086 #define AVR32_USART2_CLK_0_FUNCTION        1
00087 #define AVR32_USART2_CTS_0_PIN             61
00088 #define AVR32_USART2_CTS_0_FUNCTION        1
00089 #define AVR32_USART2_RTS_0_PIN             62
00090 #define AVR32_USART2_RTS_0_FUNCTION        1
00091 #define AVR32_USART2_RXD_0_PIN             58
00092 #define AVR32_USART2_RXD_0_FUNCTION        1
00093 #define AVR32_USART2_TXD_0_PIN             59
00094 #define AVR32_USART2_TXD_0_FUNCTION        1
00095 #define AVR32_USART3_CLK_0_PIN             51
00096 #define AVR32_USART3_CLK_0_FUNCTION        1
00097 #define AVR32_USART3_CTS_0_PIN             47
00098 #define AVR32_USART3_CTS_0_FUNCTION        1
00099 #define AVR32_USART3_RTS_0_PIN             48
00100 #define AVR32_USART3_RTS_0_FUNCTION        1
00101 #define AVR32_USART3_RXD_0_PIN             50
00102 #define AVR32_USART3_RXD_0_FUNCTION        1
00103 #define AVR32_USART3_TXD_0_PIN             49
00104 #define AVR32_USART3_TXD_0_FUNCTION        1
00105 #else
00106 #error No PIO definitions for the IAR compiler and the cpu chosen
00107 #endif /* __AVR32_AP7000__ */
00108 #endif /* __ICCAVR32__ */
00109 
00110 #define RGB_OFF     0
00111 #define RGB_RED     1
00112 #define RGB_GREEN   2
00113 #define RGB_BLUE    3
00114 #define RGB_ORANGE  4
00115 #define RGB_WHITE   5
00116 
00117 #define MODE_POLLED 0
00118 #define MODE_PDC    1
00119 #define MODE_INT    2
00120 
00122 typedef unsigned char avr32_piomap_t[][2];
00123 
00124 int pio_enable_module(avr32_piomap_t piomap, unsigned int size);
00125 
00126 
00129 void delay(int delay)
00130 {
00131     unsigned int timeout = delay * TIMEOUT;
00132 
00133     do {
00134         --timeout;
00135     } while (timeout > 0);
00136 }
00137 
00138 
00150 void rgb_setColor(volatile avr32_pio_t * pio, unsigned char color)
00151 {
00152     pio->codr = 0x00003F00;
00153 
00154     switch(color) {
00155         case RGB_OFF:
00156             break;
00157         case RGB_RED:
00158             pio->sodr = 0x00000300;
00159             break;
00160         case RGB_GREEN:
00161             pio->sodr = 0x00000C00;
00162             break;
00163         case RGB_BLUE:
00164             pio->sodr = 0x00003000;
00165             break;
00166         case RGB_ORANGE:
00167             pio->sodr = 0x00000F00;
00168             break;
00169         case RGB_WHITE:
00170             pio->sodr = 0x00003F00;
00171             break;
00172         default:
00173             break;
00174     }
00175 }
00176 
00177 
00180 void do_interrupt_mode(volatile avr32_pio_t *piob,
00181         volatile avr32_pio_t *pioc,
00182         int *mode, unsigned char *leds)
00183 {
00184     start_interrupts();
00185 
00186     for (;;) {
00187         /* SW7 and SW6 disables interrupts */
00188         if ((piob->pdsr & AVR32_PIO_P7_MASK) == 0) {
00189             rgb_setColor(pioc, RGB_RED);
00190             *mode = MODE_POLLED;
00191             goto stop_int;
00192         } else if ((piob->pdsr & AVR32_PIO_P6_MASK) == 0) {
00193             rgb_setColor(pioc, RGB_GREEN);
00194             *mode = MODE_PDC;
00195             goto stop_int;
00196         }
00197 
00198         *leds <<= 1;
00199         *leds ^= (*leds ^ 0x80) >> 7;
00200         pioc->codr = 0x000000FF;
00201         pioc->sodr = (long) *leds;
00202         delay(10);
00203     }
00204 
00205 stop_int:
00206     stop_interrupts();
00207 }
00208 
00209 
00213 int main()
00214 {
00215     /* Initialize interrupts */
00216     init_usart_interrupts();
00217 
00218     avr32_piomap_t usart_piomap = {
00219         {AVR32_USART1_RXD_0_PIN, AVR32_USART1_RXD_0_FUNCTION}, \
00220         {AVR32_USART1_TXD_0_PIN, AVR32_USART1_TXD_0_FUNCTION}, \
00221     };
00222 
00223     char * string;
00224     int stringSize;
00225 #ifdef __GNUC__
00226     char * stringPDC __attribute__((aligned(32)));
00227 #elif __ICCAVR32__
00228     char * stringPDC;
00229 #else
00230 #error Unknown compiler used, stringPDC must be alligned to work as intended
00231 #endif
00232     int stringSizePDC;
00233     volatile avr32_pio_t * piob;
00234     volatile avr32_pio_t * pioc;
00235     volatile struct avr32_usart_t * usart;
00236     struct usart_options_t usartOptions;
00237 
00238     /* Select PIO */
00239     piob = &AVR32_PIOB;
00240     pioc = &AVR32_PIOC;
00241 
00242     /* Select USART */
00243     usart = &USART_MODULE;
00244 
00245     /* disable all interrupts on PIO */
00246     piob->idr = 0xFFFFffff;
00247     pioc->idr = 0xFFFFffff;
00248 
00249     /* Enable USART on PIOA */
00250     pio_enable_module(usart_piomap, 2);
00251 
00252     /* Enable PIOB as debug output
00253      * Output: PC0 => PC15
00254      *
00255      * Enable PIOC as debug input
00256      * Input: PB0 => BP7
00257      */
00258     piob->per = 0x000000FF;
00259     piob->odr = 0x000000FF;
00260     piob->puer = 0x000000FF;
00261 
00262     pioc->per = 0x0000FFFF;
00263     pioc->oer = 0x0000FFFF;
00264     pioc->ower = 0x0000FFFF;
00265     pioc->odsr = 0x0000FFFF;
00266 
00267     /* Set options for the USART */
00268     usartOptions.baudrate = USART_BAUD;
00269     usartOptions.charlength = USART_BITS;
00270     usartOptions.paritytype = USART_NO_PARITY;
00271     usartOptions.stopbits = USART_1_STOPBIT;
00272     usartOptions.channelmode = USART_NORMAL_CHMODE;
00273 
00274     /* Initialize USART in RS232 mode */
00275     usart_linit(usart, &usartOptions, CPUHZ);
00276 
00277     string = "POL: AVR32 PDC application demo\r\n";
00278     stringPDC = "PDC: AVR32 PDC application demo\r\n";
00279 
00280     stringSize = 33;
00281     stringSizePDC = 33;
00282 
00283     pdc_flushCache(string, stringSize/4);
00284     pdc_flushCache(stringPDC, stringSizePDC/4);
00285 
00286     int mode = MODE_POLLED;
00287     rgb_setColor(pioc, RGB_RED);
00288 
00289     unsigned char leds = 0;
00290 
00291     for (;;) {
00292         leds <<= 1;
00293         leds ^= (leds ^ 0x80) >> 7;
00294         pioc->codr = 0x000000FF;
00295         pioc->sodr = (long) leds;
00296 
00297         /* Actions
00298          * SW7 change mode to POLL
00299          * SW6 change mode to PDC
00300          * SW5 change mode to PDC + Interrupt
00301          */
00302         if ((piob->pdsr & AVR32_PIO_P7_MASK) == 0) {
00303             rgb_setColor(pioc, RGB_RED);
00304             mode = MODE_POLLED;
00305         } else if ((piob->pdsr & AVR32_PIO_P6_MASK) == 0) {
00306             rgb_setColor(pioc, RGB_GREEN);
00307             mode = MODE_PDC;
00308         } else if ((piob->pdsr & AVR32_PIO_P5_MASK) == 0) {
00309             rgb_setColor(pioc, RGB_BLUE);
00310             mode = MODE_INT;
00311         }
00312 
00313 
00314         if (mode == MODE_POLLED) {
00315             usart_lwriteLine(usart, string);
00316         }
00317         else if (mode == MODE_PDC) {
00318             if (pdc_txBytesLeft((void *) usart) == 0) {
00319                 pdc_setTxBuf((void *) usart,
00320                         pdc_translatePtr(stringPDC),
00321                         stringSizePDC,
00322                         pdc_translatePtr(stringPDC),
00323                         stringSizePDC);
00324             }
00325         }
00326         else if (mode == MODE_INT) {
00327             do_interrupt_mode(piob, pioc, &mode, &leds);
00328         }
00329 
00330         delay(10);
00331     }
00332 }
00333 
00334 
00344 int pio_enable_module(avr32_piomap_t piomap, unsigned int size)
00345 {
00346     int i;
00347     volatile struct avr32_pio_t *pio;
00348 
00349     /* get the base address for the port */
00350     switch(**piomap/32) {
00351 
00352         case 0:
00353             pio = &AVR32_PIOA;
00354             break;
00355         case 1:
00356             pio = &AVR32_PIOB;
00357             break;
00358         case 2:
00359             pio = &AVR32_PIOC;
00360             break;
00361         case 3:
00362             pio = &AVR32_PIOD;
00363             break;
00364         case 4:
00365             pio = &AVR32_PIOE;
00366             break;
00367         default :
00368             return USART_ERROR_ARGUMENT;
00369 
00370     }
00371 
00372     for (i = 0; i < size; i++){
00373 
00374         pio->pdr |= (1<<((**piomap) % 32));
00375         pio->pudr |= (1<<((**piomap) % 32));
00376 
00377         switch(*(*piomap+1)){
00378             case 0:
00379                 pio->asr |= (1<<((**piomap) % 32));
00380                 break;
00381             case 1:
00382                 pio->bsr |= (1<<((**piomap) % 32));
00383                 break;
00384             default:
00385                 return USART_ERROR_ARGUMENT;
00386         }
00387 
00388         ++piomap;
00389 
00390     }
00391 
00392     return USART_OK;
00393 }
00394 

Generated on Wed May 10 15:03:11 2006 for AVR32108 - Peripheral Direct Memory Access Driver by  doxygen 1.4.6-NO