00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00096 #define NUT_DEPRECATED
00097
00098 #include <cfg/os.h>
00099 #include <cfg/clock.h>
00100
00101 #include <dev/debug.h>
00102 #include <sys/device.h>
00103 #include <sys/file.h>
00104 #include <sys/timer.h>
00105
00106 #define static
00107
00112
00113 #if defined(DBGU_BASE)
00114 static NUTFILE dbgfile;
00115 #else
00116 static NUTFILE dbgfile0;
00117 static NUTFILE dbgfile1;
00118 #endif
00119
00120 #if defined(DBGU_BASE)
00121
00128 static int DebugIOCtl(NUTDEVICE * dev, int req, void *conf)
00129 {
00130 if(req == UART_SETSPEED) {
00131 #if defined(AT91_PLL_MAINCK)
00132 outr(DBGU_BRGR, (At91GetMasterClock() / (8 * (*((uint32_t *)conf))) + 1) / 2);
00133 #else
00134 outr(DBGU_BRGR, (NutGetCpuClock() / (8 * (*((uint32_t *)conf))) + 1) / 2);
00135 #endif
00136 return 0;
00137 }
00138 return -1;
00139 }
00140
00146 static int DebugInit(NUTDEVICE * dev)
00147 {
00148 #if defined (MCU_AT91SAM7X) || defined (MCU_AT91SAM7S256) || defined (MCU_AT91SAM7SE512)
00149
00150 #if defined (MCU_AT91SAM7X)
00151 outr(PIOA_PDR, _BV(27) | _BV(28));
00152 #elif defined (MCU_AT91SAM7S256) || defined (MCU_AT91SAM7SE512)
00153 outr(PIOA_PDR, _BV(PA9_DRXD_A) | _BV(PA10_DTXD_A));
00154 #endif
00155
00156 outr(DBGU_CR, US_RSTRX | US_RSTTX | US_RXDIS | US_TXDIS);
00157
00158 outr(DBGU_IDR, 0xFFFFFFFF);
00159
00160 outr(DBGU_BRGR, (NutGetCpuClock() / (8 * (115200)) + 1) / 2);
00161
00162 outr(DBGU_MR, US_CHMODE_NORMAL | US_CHRL_8 | US_PAR_NO | US_NBSTOP_1);
00163
00164 outr(DBGU_CR, US_RXEN | US_TXEN);
00165 #endif
00166
00167 return 0;
00168 }
00169
00170 #else
00171
00179 static int Debug0IOCtl(NUTDEVICE * dev, int req, void *conf)
00180 {
00181 if(req == UART_SETSPEED) {
00182 outr(US0_BRGR, (NutGetCpuClock() / (8 * (*((uint32_t *)conf))) + 1) / 2);
00183 return 0;
00184 }
00185 return -1;
00186 }
00187
00195 static int Debug1IOCtl(NUTDEVICE * dev, int req, void *conf)
00196 {
00197 if(req == UART_SETSPEED) {
00198 outr(US1_BRGR, (NutGetCpuClock() / (8 * (*((uint32_t *)conf))) + 1) / 2);
00199 return 0;
00200 }
00201 return -1;
00202 }
00203
00209 static int Debug0Init(NUTDEVICE * dev)
00210 {
00211 #if defined (MCU_AT91R40008)
00212
00213 outr(PS_PCER, _BV(US0_ID));
00214
00215 outr(PIO_PDR, _BV(14) | _BV(15));
00216 #elif defined (MCU_AT91SAM7X) || defined (MCU_AT91SAM9260) || defined (MCU_AT91SAM7S256) || defined (MCU_AT91SAM7SE512) || defined(MCU_AT91SAM9XE512)
00217
00218 outr(PMC_PCER, _BV(US0_ID));
00219
00220 #if defined (MCU_AT91SAM7S256) || defined (MCU_AT91SAM7SE512)
00221 outr(PIOA_PDR, _BV(PA5_RXD0_A) | _BV(PA6_TXD0_A));
00222 #else
00223 outr(PIOA_PDR, _BV(0) | _BV(1));
00224 #endif
00225 #endif
00226
00227 outr(US0_CR, US_RSTRX | US_RSTTX | US_RXDIS | US_TXDIS);
00228
00229 outr(US0_IDR, 0xFFFFFFFF);
00230 #if defined (US0_RCR) && defined(US0_TCR)
00231
00232 outr(US0_RCR, 0);
00233 outr(US0_TCR, 0);
00234 #endif
00235
00236 outr(US0_BRGR, (NutGetCpuClock() / (8 * (115200)) + 1) / 2);
00237
00238 outr(US0_MR, US_CHMODE_NORMAL | US_CHRL_8 | US_PAR_NO | US_NBSTOP_1);
00239
00240 outr(US0_CR, US_RXEN | US_TXEN);
00241
00242 return 0;
00243 }
00244
00250 static int Debug1Init(NUTDEVICE * dev)
00251 {
00252 #if defined (MCU_AT91R40008)
00253
00254 outr(PS_PCER, _BV(US1_ID));
00255
00256 outr(PIO_PDR, _BV(21) | _BV(22));
00257 #elif defined (MCU_AT91SAM7X) || defined (MCU_AT91SAM9260) || defined (MCU_AT91SAM7S256) || defined (MCU_AT91SAM7SE512) || defined(MCU_AT91SAM9XE512)
00258
00259 outr(PMC_PCER, _BV(US1_ID));
00260
00261 #if defined (MCU_AT91SAM7S256) || defined (MCU_AT91SAM7SE512)
00262 outr(PIOA_PDR, _BV(PA21_RXD1_A) | _BV(PA22_TXD1_A));
00263 #else
00264 outr(PIOA_PDR, _BV(5) | _BV(6));
00265 #endif
00266 #endif
00267
00268 outr(US1_CR, US_RSTRX | US_RSTTX | US_RXDIS | US_TXDIS);
00269
00270 outr(US1_IDR, 0xFFFFFFFF);
00271 #if defined (US1_RCR) && defined(US1_TCR)
00272
00273 outr(US1_RCR, 0);
00274 outr(US1_TCR, 0);
00275 #endif
00276
00277 outr(US1_BRGR, (NutGetCpuClock() / (8 * (115200)) + 1) / 2);
00278
00279 outr(US1_MR, US_CHMODE_NORMAL | US_CHRL_8 | US_PAR_NO | US_NBSTOP_1);
00280
00281 outr(US1_CR, US_RXEN | US_TXEN);
00282
00283 return 0;
00284 }
00285
00286 #endif
00287
00288
00295 static void DebugPut(CONST NUTDEVICE * dev, char ch)
00296 {
00297 if (ch == '\n') {
00298 while ((inr(dev->dev_base + US_CSR_OFF) & US_TXRDY) == 0);
00299 outr(dev->dev_base + US_THR_OFF, '\r');
00300 }
00301 while ((inr(dev->dev_base + US_CSR_OFF) & US_TXRDY) == 0);
00302 outr(dev->dev_base + US_THR_OFF, ch);
00303 }
00304
00313 static int DebugWrite(NUTFILE * fp, CONST void *buffer, int len)
00314 {
00315 int c = len;
00316 CONST char *cp = buffer;
00317
00318 while (c--) {
00319 DebugPut(fp->nf_dev, *cp++);
00320 }
00321 return len;
00322 }
00323
00329 static NUTFILE *DebugOpen(NUTDEVICE * dev, CONST char *name, int mode, int acc)
00330 {
00331 NUTFILE *fp = (NUTFILE *) (dev->dev_dcb);
00332
00333 fp->nf_next = 0;
00334 fp->nf_dev = dev;
00335 fp->nf_fcb = 0;
00336
00337 return fp;
00338 }
00339
00345 static int DebugClose(NUTFILE * fp)
00346 {
00347 return 0;
00348 }
00349
00350 #if defined(DBGU_BASE)
00351
00355 NUTDEVICE devDebug = {
00356 0,
00357 {'d', 'b', 'g', 'u', 0, 0, 0, 0, 0}
00358 ,
00359 0,
00360 DBGU_BASE,
00361 0,
00362 0,
00363 &dbgfile,
00364 DebugInit,
00365 DebugIOCtl,
00366 0,
00367 DebugWrite,
00368 DebugOpen,
00369 DebugClose,
00370 0
00371 };
00372
00373 #else
00374
00378 NUTDEVICE devDebug0 = {
00379 0,
00380 {'u', 'a', 'r', 't', '0', 0, 0, 0, 0}
00381 ,
00382 0,
00383 USART0_BASE,
00384 0,
00385 0,
00386 &dbgfile0,
00387 Debug0Init,
00388 Debug0IOCtl,
00389 0,
00390 DebugWrite,
00391 DebugOpen,
00392 DebugClose,
00393 0
00394 };
00395
00399 NUTDEVICE devDebug1 = {
00400 0,
00401 {'u', 'a', 'r', 't', '1', 0, 0, 0, 0}
00402 ,
00403 0,
00404 USART1_BASE,
00405 0,
00406 0,
00407 &dbgfile1,
00408 Debug1Init,
00409 Debug1IOCtl,
00410 0,
00411 DebugWrite,
00412 DebugOpen,
00413 DebugClose,
00414 0
00415 };
00416
00417 #endif
00418