Nut/OS  4.10.3
API Reference
debug.c
Go to the documentation of this file.
00001 
00047 #include <cfg/os.h>
00048 #include <cfg/clock.h>
00049 
00050 #include <sys/atom.h>
00051 #include <sys/device.h>
00052 #include <sys/file.h>
00053 #include <sys/timer.h>
00054 #include <dev/gpio.h>
00055 #include <dev/uart.h>
00056 
00057 #include <avr32/io.h>
00058 #include <arch/avr32/gpio.h>
00059 #include <arch/avr32/pm.h>
00060 
00061 #if defined(AVR32_USART0_TXD_0_0_PIN)
00062 #define AVR32_USART0_TXD_PIN        AVR32_USART0_TXD_0_0_PIN
00063 #define AVR32_USART0_TXD_FUNCTION   AVR32_USART0_TXD_0_0_FUNCTION
00064 #elif defined(AVR32_USART0_TXD_0_PIN)
00065 #define AVR32_USART0_TXD_PIN        AVR32_USART0_TXD_0_PIN
00066 #define AVR32_USART0_TXD_FUNCTION   AVR32_USART0_TXD_0_FUNCTION
00067 #endif
00068 
00069 #if defined(AVR32_USART1_TXD_0_0_PIN)
00070 #define AVR32_USART1_TXD_PIN        AVR32_USART1_TXD_0_0_PIN
00071 #define AVR32_USART1_TXD_FUNCTION   AVR32_USART1_TXD_0_0_FUNCTION
00072 #elif defined(AVR32_USART1_TXD_0_PIN)
00073 #define AVR32_USART1_TXD_PIN        AVR32_USART1_TXD_0_PIN
00074 #define AVR32_USART1_TXD_FUNCTION   AVR32_USART1_TXD_0_FUNCTION
00075 #endif
00076 
00077 #if defined(AVR32_USART2_TXD_0_0_PIN)
00078 #define AVR32_USART2_TXD_PIN        AVR32_USART2_TXD_0_0_PIN
00079 #define AVR32_USART2_TXD_FUNCTION   AVR32_USART2_TXD_0_0_FUNCTION
00080 #elif defined(AVR32_USART2_TXD_0_PIN)
00081 #define AVR32_USART2_TXD_PIN        AVR32_USART2_TXD_0_PIN
00082 #define AVR32_USART2_TXD_FUNCTION   AVR32_USART2_TXD_0_FUNCTION
00083 #endif
00084 
00085 #if defined(AVR32_USART3_TXD_0_0_PIN)
00086 #define AVR32_USART3_TXD_PIN        AVR32_USART3_TXD_0_0_PIN
00087 #define AVR32_USART3_TXD_FUNCTION   AVR32_USART3_TXD_0_0_FUNCTION
00088 #elif defined(AVR32_USART3_TXD_0_PIN)
00089 #define AVR32_USART3_TXD_PIN        AVR32_USART3_TXD_0_PIN
00090 #define AVR32_USART3_TXD_FUNCTION   AVR32_USART3_TXD_0_FUNCTION
00091 #endif
00092 
00097 
00105 static int DebugIOCtl(NUTDEVICE * dev, int req, void *conf)
00106 {
00107     volatile avr32_usart_t *usart = (avr32_usart_t *) dev->dev_base;
00108 
00109     if (req == UART_SETSPEED) {
00110         const uint32_t baudrate = (*((uint32_t *) conf));
00111         const uint32_t pba_hz = NutArchClockGet(NUT_HWCLK_PERIPHERAL_A);
00112         unsigned int over = (pba_hz >= 16 * baudrate) ? 16 : 8;
00113         unsigned int cd_fp = ((1 << AVR32_USART_BRGR_FP_SIZE) * pba_hz + (over * baudrate) / 2) / (over * baudrate);
00114         unsigned int cd = cd_fp >> AVR32_USART_BRGR_FP_SIZE;
00115         unsigned int fp = cd_fp & ((1 << AVR32_USART_BRGR_FP_SIZE) - 1);
00116 
00117         if (cd < 1 || cd > (1 << AVR32_USART_BRGR_CD_SIZE) - 1)
00118             return -1;
00119 
00120         usart->mr = (usart->mr & ~(AVR32_USART_MR_USCLKS_MASK |
00121                                    AVR32_USART_MR_SYNC_MASK |
00122                                    AVR32_USART_MR_OVER_MASK)) |
00123             AVR32_USART_MR_USCLKS_MCK << AVR32_USART_MR_USCLKS_OFFSET |
00124             ((over == 16) ? AVR32_USART_MR_OVER_X16 : AVR32_USART_MR_OVER_X8) << AVR32_USART_MR_OVER_OFFSET;
00125 
00126         usart->brgr = cd << AVR32_USART_BRGR_CD_OFFSET | fp << AVR32_USART_BRGR_FP_OFFSET;
00127         return 0;
00128     }
00129     return -1;
00130 }
00131 
00137 static int DebugInit(NUTDEVICE * dev)
00138 {
00139     volatile avr32_usart_t *usart = (avr32_usart_t *) dev->dev_base;
00140 
00141     /* Disable all USART interrupts.
00142      ** Interrupts needed should be set explicitly on every reset. */
00143     NutEnterCritical();
00144     usart->idr = 0xFFFFFFFF;
00145     usart->csr;
00146     NutExitCritical();
00147 
00148     /* Reset mode and other registers that could cause unpredictable behavior after reset. */
00149     usart->mr = 0;
00150     usart->rtor = 0;
00151     usart->ttgr = 0;
00152 
00153     /* Shutdown TX and RX (will be re-enabled when setup has successfully completed),
00154      ** reset status bits and turn off DTR and RTS. */
00155     usart->cr = AVR32_USART_CR_RSTRX_MASK |
00156         AVR32_USART_CR_RSTTX_MASK | AVR32_USART_CR_RSTSTA_MASK | AVR32_USART_CR_RSTIT_MASK | AVR32_USART_CR_RSTNACK_MASK |
00157 #ifndef AVR32_USART_CR_DTRDIS_MASK
00158         AVR32_USART_CR_DTRDIS_MASK |
00159 #endif
00160         AVR32_USART_CR_RTSDIS_MASK;
00161 
00162     usart->mr |= (8 - 5) << AVR32_USART_MR_CHRL_OFFSET |        /* 8 bit character length */
00163         AVR32_USART_MR_PAR_NONE << AVR32_USART_MR_PAR_OFFSET |  /* No parity */
00164         AVR32_USART_MR_CHMODE_NORMAL << AVR32_USART_MR_CHMODE_OFFSET |  /* */
00165         AVR32_USART_MR_NBSTOP_1 << AVR32_USART_MR_NBSTOP_OFFSET;
00166 
00167     /* Set normal mode. */
00168     usart->mr = (usart->mr & ~AVR32_USART_MR_MODE_MASK) | AVR32_USART_MR_MODE_NORMAL << AVR32_USART_MR_MODE_OFFSET;
00169 
00170     /* Enable input and output. */
00171     usart->cr = AVR32_USART_CR_TXEN_MASK;
00172 
00173     return 0;
00174 }
00175 
00182 static void DebugPut(CONST NUTDEVICE * dev, char ch)
00183 {
00184     volatile avr32_usart_t *usart = (avr32_usart_t *) dev->dev_base;
00185 
00186     /* Prepend new line by carriage return */
00187     if (ch == '\n') {
00188         DebugPut(dev, '\r');
00189     }
00190 
00191     /* Wait for TX Ready. */
00192     while (!(usart->csr & AVR32_USART_CSR_TXRDY_MASK));
00193     /* Send out character */
00194     usart->thr = (ch << AVR32_USART_THR_TXCHR_OFFSET) & AVR32_USART_THR_TXCHR_MASK;
00195 
00196 }
00197 
00206 static int DebugWrite(NUTFILE * fp, CONST void *buffer, int len)
00207 {
00208     int c = len;
00209     CONST char *cp = buffer;
00210 
00211     while (c--) {
00212         DebugPut(fp->nf_dev, *cp++);
00213     }
00214     return len;
00215 }
00216 
00222 static NUTFILE *DebugOpen(NUTDEVICE * dev, CONST char *name, int mode, int acc)
00223 {
00224     NUTFILE *fp = (NUTFILE *) (dev->dev_dcb);
00225 
00226     fp->nf_next = 0;
00227     fp->nf_dev = dev;
00228     fp->nf_fcb = 0;
00229 
00230     return fp;
00231 }
00232 
00238 static int DebugClose(NUTFILE * fp)
00239 {
00240     return 0;
00241 }
00242 
00243 #ifdef AVR32_USART0_TXD_PIN
00244 static int Debug0Init(NUTDEVICE * dev)
00245 {
00246     /* Assign GPIO to USART. */
00247     gpio_enable_module_pin(AVR32_USART0_TXD_PIN, AVR32_USART0_TXD_FUNCTION);
00248 
00249     pm_enable_module(&AVR32_PM, AVR32_USART0_CLK_PBA);
00250 
00251     return DebugInit(dev);
00252 };
00253 
00254 static NUTFILE dbgfile0;
00255 
00259 NUTDEVICE devDebug0 = {
00260     0,                          
00261     {'u', 'a', 'r', 't', '0', 0, 0, 0, 0}
00262     ,                           
00263     0,                          
00264     (uintptr_t) (&AVR32_USART0),        
00265     0,                          
00266     0,                          
00267     &dbgfile0,                  
00268     Debug0Init,                 
00269     DebugIOCtl,                 
00270     0,                          
00271     DebugWrite,                 
00272     DebugOpen,                  
00273     DebugClose,                 
00274     0                           
00275 };
00276 #endif                          /* AVR32_USART0_TXD_PIN */
00277 
00278 #ifdef AVR32_USART1_TXD_PIN
00279 static int Debug1Init(NUTDEVICE * dev)
00280 {
00281     /* Assign GPIO to USART. */
00282     gpio_enable_module_pin(AVR32_USART1_TXD_PIN, AVR32_USART1_TXD_FUNCTION);
00283 
00284     pm_enable_module(&AVR32_PM, AVR32_USART1_CLK_PBA);
00285 
00286     return DebugInit(dev);
00287 };
00288 
00289 static NUTFILE dbgfile1;
00290 
00294 NUTDEVICE devDebug1 = {
00295     0,                          
00296     {'u', 'a', 'r', 't', '1', 0, 0, 0, 0}
00297     ,                           
00298     0,                          
00299     (uintptr_t) (&AVR32_USART1),        
00300     0,                          
00301     0,                          
00302     &dbgfile1,                  
00303     Debug1Init,                 
00304     DebugIOCtl,                 
00305     0,                          
00306     DebugWrite,                 
00307     DebugOpen,                  
00308     DebugClose,                 
00309     0                           
00310 };
00311 #endif                          /* AVR32_USART1_TXD_PIN */
00312 
00313 #ifdef AVR32_USART2_TXD_PIN
00314 static int Debug2Init(NUTDEVICE * dev)
00315 {
00316     /* Assign GPIO to USART. */
00317     gpio_enable_module_pin(AVR32_USART2_TXD_PIN, AVR32_USART2_TXD_FUNCTION);
00318 
00319     pm_enable_module(&AVR32_PM, AVR32_USART2_CLK_PBA);
00320 
00321     return DebugInit(dev);
00322 };
00323 
00324 static NUTFILE dbgfile2;
00325 
00329 NUTDEVICE devDebug2 = {
00330     0,                          
00331     {'u', 'a', 'r', 't', '2', 0, 0, 0, 0}
00332     ,                           
00333     0,                          
00334     (uintptr_t) (&AVR32_USART2),        
00335     0,                          
00336     0,                          
00337     &dbgfile2,                  
00338     Debug2Init,                 
00339     DebugIOCtl,                 
00340     0,                          
00341     DebugWrite,                 
00342     DebugOpen,                  
00343     DebugClose,                 
00344     0                           
00345 };
00346 #endif                          /* AVR32_USART2_TXD_PIN */
00347 
00348 #ifdef AVR32_USART3_TXD_PIN
00349 static int Debug3Init(NUTDEVICE * dev)
00350 {
00351     /* Assign GPIO to USART. */
00352     gpio_enable_module_pin(AVR32_USART3_TXD_PIN, AVR32_USART3_TXD_FUNCTION);
00353 
00354     pm_enable_module(&AVR32_PM, AVR32_USART3_CLK_PBA);
00355 
00356     return DebugInit(dev);
00357 };
00358 
00359 static NUTFILE dbgfile3;
00360 
00364 NUTDEVICE devDebug3 = {
00365     0,                          
00366     {'u', 'a', 'r', 't', '3', 0, 0, 0, 0}
00367     ,                           
00368     0,                          
00369     (uintptr_t) (&AVR32_USART3),        
00370     0,                          
00371     0,                          
00372     &dbgfile3,                  
00373     Debug3Init,                 
00374     DebugIOCtl,                 
00375     0,                          
00376     DebugWrite,                 
00377     DebugOpen,                  
00378     DebugClose,                 
00379     0                           
00380 };
00381 
00382 #endif                          /* AVR32_USART3_TXD_PIN */
00383