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
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130 #include <sys/thread.h>
00131 #include <sys/heap.h>
00132 #include <cfg/memory.h>
00133 #include <cfg/os.h>
00134 #include <cfg/arch/avr.h>
00135 #include <cfg/arch.h>
00136
00141
00142 #ifdef NUTXMEM_SIZE
00143
00146 #define NUTMEM_END (uint16_t)(NUTXMEM_START + (uint16_t)NUTXMEM_SIZE - 1U)
00147
00148 #else
00149
00155 #define NUTMEM_END (uint16_t)(NUTMEM_START + (uint16_t)NUTMEM_SIZE - 1U)
00156
00157 #endif
00158
00159 #ifdef NUTMEM_RESERVED
00160
00170 uint8_t nutmem_onchip[NUTMEM_RESERVED];
00171 #endif
00172
00173
00174 #if defined(__GNUC__) && defined(__AVR_ENHANCED__)
00175 uint8_t idle_sleep_mode = SLEEP_MODE_NONE;
00176
00177
00178 #if defined(SMCR)
00179 #define AVR_SLEEP_CTRL_REG SMCR
00180 #else
00181 #define AVR_SLEEP_CTRL_REG MCUCR
00182 #endif
00183
00184 #endif
00185
00186
00187
00188
00189
00190 #if defined(RTL_EESK_BIT) && defined(__GNUC__)
00191
00192 #ifndef RTL_BASE_ADDR
00193 #define RTL_BASE_ADDR 0x8300
00194 #endif
00195 #define NIC_CR _MMIO_BYTE(RTL_BASE_ADDR)
00196 #define NIC_EE _MMIO_BYTE(RTL_BASE_ADDR + 1)
00197
00198 #if (RTL_EEMU_AVRPORT == AVRPORTB)
00199 #define RTL_EEMU_PORT PORTB
00200 #define RTL_EEMU_DDR DDRB
00201
00202 #elif (RTL_EEMU_AVRPORT == AVRPORTD)
00203 #define RTL_EEMU_PORT PORTD
00204 #define RTL_EEMU_DDR DDRD
00205
00206 #elif (RTL_EEMU_AVRPORT == AVRPORTE)
00207 #define RTL_EEMU_PORT PORTE
00208 #define RTL_EEMU_DDR DDRE
00209
00210 #elif (RTL_EEMU_AVRPORT == AVRPORTF)
00211 #define RTL_EEMU_PORT PORTF
00212 #define RTL_EEMU_DDR DDRF
00213
00214 #else
00215 #define RTL_EE_MEMBUS
00216 #define RTL_EEMU_PORT PORTC
00217 #define RTL_EEMU_DDR DDRC
00218
00219 #endif
00220
00221 #if (RTL_EEDO_AVRPORT == AVRPORTB)
00222 #define RTL_EEDO_PORT PORTB
00223 #define RTL_EEDO_DDR DDRB
00224
00225 #elif (RTL_EEDO_AVRPORT == AVRPORTD)
00226 #define RTL_EEDO_PORT PORTD
00227 #define RTL_EEDO_DDR DDRD
00228
00229 #elif (RTL_EEDO_AVRPORT == AVRPORTE)
00230 #define RTL_EEDO_PORT PORTE
00231 #define RTL_EEDO_DDR DDRE
00232
00233 #elif (RTL_EEDO_AVRPORT == AVRPORTF)
00234 #define RTL_EEDO_PORT PORTF
00235 #define RTL_EEDO_DDR DDRF
00236
00237 #else
00238 #define RTL_EE_MEMBUS
00239 #define RTL_EEDO_PORT PORTC
00240 #define RTL_EEDO_DDR DDRC
00241
00242 #endif
00243
00244 #if (RTL_EESK_AVRPORT == AVRPORTB)
00245 #define RTL_EESK_PIN PINB
00246 #define RTL_EESK_DDR DDRB
00247
00248 #elif (RTL_EESK_AVRPORT == AVRPORTD)
00249 #define RTL_EESK_PIN PIND
00250 #define RTL_EESK_DDR DDRD
00251
00252 #elif (RTL_EESK_AVRPORT == AVRPORTE)
00253 #define RTL_EESK_PIN PINE
00254 #define RTL_EESK_DDR DDRE
00255
00256 #elif (RTL_EESK_AVRPORT == AVRPORTF)
00257 #define RTL_EESK_PIN PINF
00258 #define RTL_EESK_DDR DDRF
00259
00260 #else
00261 #define RTL_EE_MEMBUS
00262 #define RTL_EESK_PIN PINC
00263 #define RTL_EESK_DDR DDRC
00264
00265 #endif
00266 #endif
00267
00268 #ifdef __GNUC__
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290 void NutInit(void) __attribute__ ((naked)) __attribute__ ((section(".init8")));
00291 extern void NutAppMain(void *arg) __attribute__ ((noreturn));
00292 #else
00293 extern void main(void *);
00294 #endif
00295
00296
00297
00298
00299 #if defined(__GNUC__) && defined(NUTXMEM_SIZE)
00300
00301
00302
00303
00304 static void NutInitXRAM(void) __attribute__ ((naked, section(".init1"), used));
00305 void NutInitXRAM(void)
00306 {
00307 #if defined(__AVR_AT90CAN128__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega2561__)
00308
00309
00310
00311
00312 #ifdef NUT_3WAITSTATES
00313 XMCRA = _BV(SRE) | _BV(SRL2) | _BV(SRW00) | _BV(SRW10) | _BV(SRW11);
00314 #else
00315 XMCRA = _BV(SRE) | _BV(SRW10);
00316 #endif
00317
00318 #elif defined(__AVR_ATmega128__)
00319
00320 MCUCR = _BV(SRE) | _BV(SRW10);
00321
00322
00323
00324
00325
00326 #ifdef NUT_3WAITSTATES
00327 XMCRA |= _BV(SRL2) | _BV(SRW00) | _BV(SRW11);
00328 XMCRB = 0;
00329 #endif
00330
00331 #else
00332 MCUCR = _BV(SRE) | _BV(SRW);
00333 #endif
00334 }
00335
00336 #endif
00337
00338
00339 #if defined(RTL_EESK_BIT) && defined(__GNUC__) && defined(NUTXMEM_SIZE)
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351 static void FakeNicEeprom(void) __attribute__ ((naked, section(".init1"), used));
00352 void FakeNicEeprom(void)
00353 {
00354
00355
00356
00357
00358 #ifdef RTL_EEMU_BIT
00359 sbi(RTL_EEMU_PORT, RTL_EEMU_BIT);
00360 sbi(RTL_EEMU_DDR, RTL_EEMU_BIT);
00361 #endif
00362 sbi(RTL_EEDO_PORT, RTL_EEDO_BIT);
00363 sbi(RTL_EEDO_DDR, RTL_EEDO_BIT);
00364
00365
00366 NIC_CR = 0xE1;
00367 NIC_EE = 0x40;
00368
00369
00370 #ifdef RTL_EE_MEMBUS
00371 cbi(MCUCR, SRE);
00372 #endif
00373
00374
00375
00376
00377
00378
00379 __asm__ __volatile__("\n"
00380 "EmuLoop: " "\n"
00381 " ldi r24, 0 " "\n"
00382 " ldi r25, 0 " "\n"
00383 " sbis %0, %1 " "\n"
00384 " rjmp EmuClkClr " "\n"
00385 "EmuClkSet: " "\n"
00386 " adiw r24, 1 " "\n"
00387 " breq EmuDone " "\n"
00388 " sbis %0, %1 " "\n"
00389 " rjmp EmuLoop " "\n"
00390 " rjmp EmuClkSet " "\n"
00391 "EmuClkClr: " "\n"
00392 " adiw r24, 1 " "\n"
00393 " breq EmuDone " "\n"
00394 " sbic %0, %1 " "\n"
00395 " rjmp EmuLoop " "\n"
00396 " rjmp EmuClkClr " "\n"
00397 "EmuDone: \n\t"
00398 :
00399 :"I"(_SFR_IO_ADDR(RTL_EESK_PIN)),
00400 "I"(RTL_EESK_BIT)
00401 :"r24", "r25");
00402
00403
00404 #ifdef RTL_EE_MEMBUS
00405 sbi(MCUCR, SRE);
00406 #endif
00407
00408
00409 #ifdef RTL_EEMU_BIT
00410 cbi(RTL_EEMU_PORT, RTL_EEMU_BIT);
00411 cbi(RTL_EEMU_DDR, RTL_EEMU_BIT);
00412 #endif
00413 cbi(RTL_EEDO_PORT, RTL_EEDO_BIT);
00414 cbi(RTL_EEDO_DDR, RTL_EEDO_BIT);
00415 }
00416
00417 #endif
00418
00430 #if defined(__GNUC__) && defined(__AVR_ENHANCED__)
00431 uint8_t NutThreadSetSleepMode(uint8_t mode)
00432 {
00433 uint8_t old_mode = idle_sleep_mode;
00434 idle_sleep_mode = mode;
00435 return old_mode;
00436 }
00437 #endif
00438
00444 THREAD(NutIdle, arg)
00445 {
00446 #if defined(__GNUC__) && defined(__AVR_ENHANCED__)
00447 uint8_t sleep_mode;
00448 #endif
00449 #ifdef IDLE_HEARTBEAT_BIT
00450 uint8_t beat = 0;
00451 #endif
00452
00453
00454 NutTimerInit();
00455
00456
00457 NutThreadCreate("main", main, 0, NUT_THREAD_MAINSTACK);
00458
00459
00460
00461
00462
00463
00464 NutThreadSetPriority(254);
00465 for (;;) {
00466 NutThreadYield();
00467 NutThreadDestroy();
00468
00469 #ifdef IDLE_HEARTBEAT_BIT
00470 if ((beat = !beat) == 0) {
00471
00472 cbi(IDLE_HEARTBEAT_PORT, IDLE_HEARTBEAT_BIT);
00473 }
00474 else {
00475 sbi(IDLE_HEARTBEAT_PORT, IDLE_HEARTBEAT_BIT);
00476 }
00477 sbi(IDLE_HEARTBEAT_DDR, IDLE_HEARTBEAT_BIT);
00478 #endif
00479
00480 #if defined(__GNUC__) && defined(__AVR_ENHANCED__)
00481 if (idle_sleep_mode != SLEEP_MODE_NONE) {
00482 sleep_mode = AVR_SLEEP_CTRL_REG & _SLEEP_MODE_MASK;
00483 set_sleep_mode(idle_sleep_mode);
00484 #ifdef IDLE_THREAD_ADC_OFF
00485 uint8_t adc = bit_is_set(ADCSR, ADEN);
00486 cbi(ADCSR, ADEN);
00487 #endif
00488 #ifdef IDLE_THREAD_BUSKEEPER_OFF
00489 uint8_t bitkeeper = bit_is_set(XMCRB, XMBK);
00490 cbi(XMCRB, XMBK);
00491 #endif
00492
00493
00494 AVR_SLEEP_CTRL_REG |= _BV(SE);
00495 __asm__ __volatile__ ("sleep" "\n\t" :: );
00496 AVR_SLEEP_CTRL_REG &= ~_BV(SE);
00497 #ifdef IDLE_THREAD_ADC_OFF
00498 if (bitkeeper) {
00499 sbi(XMCRB, XMBK);
00500 }
00501 #endif
00502 #ifdef IDLE_THREAD_BUSKEEPER_OFF
00503 if (adc) {
00504 sbi(ADCSR, ADEN);
00505 }
00506 #endif
00507 set_sleep_mode(sleep_mode);
00508 }
00509 #endif
00510 }
00511 }
00512
00513 #if defined(__GNUC__)
00514 static void NutInitSP(void) __attribute__ ((naked, section (".init5"), used));
00515 void NutInitSP(void)
00516 {
00517 #if defined (__AVR_AT90CAN128__)
00518
00519 #else
00520
00521
00522
00523 SP = (uint16_t)(NUTMEM_END);
00524 #endif
00525 }
00526 #endif
00527
00528 #if defined(__GNUC__)
00529 static void NutInitHeap(void) __attribute__ ((naked, section (".init5"), used));
00530 #endif
00531 void NutInitHeap()
00532 {
00533 #if defined (NUTMEM_STACKHEAP)
00534 NutStackAdd((void *) NUTMEM_START, NUTMEM_STACKHEAP);
00535 #endif
00536
00537
00538
00539
00540
00541
00542 if ((uint16_t)NUTMEM_END - (uint16_t) (&__heap_start) > 384) {
00543 NutHeapAdd(&__heap_start, (uint16_t) NUTMEM_END - 256 - (uint16_t) (&__heap_start));
00544 }
00545 }
00546
00547 #if defined(__GNUC__)
00548 static void NutCustomInit(void) __attribute__ ((naked, section (".init1"), used));
00549 #endif
00550
00559 void NutCustomInit(void)
00560
00561
00562
00563 #if defined(MMNET02)
00564 {
00565 volatile uint8_t *breg = (uint8_t *)((size_t)-1 & ~0xFF);
00566
00567 *(breg + 1) = 0x01;
00568
00569
00570 outp(7, UBRR);
00571 outp(7, UBRR1L);
00572 }
00573
00574
00575
00576 #elif defined(ARTHERNET1)
00577 {
00578
00579
00580
00581
00582
00583 MCUCR = _BV(SRE);
00584 XMCRA |= _BV(SRL0) | _BV(SRW01) | _BV(SRW00);
00585 XMCRB = 0;
00586
00587 *((volatile uint8_t *)(ARTHERCPLDSTART)) = 0x10;
00588 *((volatile uint8_t *)(ARTHERCPLDSPI)) = 0xFF;
00589
00590
00591 outp(25, UBRR);
00592 outp(25, UBRR1L);
00593 }
00594
00595
00596
00597 #elif defined(XNUT_100) || defined(XNUT_105)
00598 {
00599 PORTB = 0x35;
00600 DDRB = 0x3F;
00601 PORTD = 0xE8;
00602 DDRD = 0xB0;
00603 PORTE = 0x0E;
00604 DDRE = 0x02;
00605 PORTF = 0xF0;
00606 DDRF = 0x0F;
00607 PORTG = 0x1F;
00608 DDRG = 0x07;
00609
00610 ACSR |= _BV(ACD);
00611
00612
00613 TWSR = 0;
00614 TWBR = (NUT_CPU_FREQ / 100000UL - 16) / 2;
00615
00616
00617 #if NUT_CPU_FREQ == 14745600
00618 UBRR0L = (NUT_CPU_FREQ / (16 * 9600UL)) - 1;
00619 UBRR1L = (NUT_CPU_FREQ / (16 * 9600UL)) - 1;
00620 #else
00621 sbi(UCSR0A, U2X0);
00622 sbi(UCSR1A, U2X1);
00623 UBRR0L = (NUT_CPU_FREQ / (8 * 9600UL)) - 1;
00624 UBRR1L = (NUT_CPU_FREQ / (8 * 9600UL)) - 1;
00625 #endif
00626 }
00627
00628
00629
00630 #else
00631 {
00632
00633 outp(7, UBRR);
00634 #ifdef __AVR_ENHANCED__
00635 outp(7, UBRR1L);
00636 #endif
00637 }
00638 #endif
00639
00672 void NutInit(void)
00673 {
00674
00675
00676
00677
00678 #ifdef NUTDEBUG
00679
00680 outp(BV(RXEN) | BV(TXEN), UCR);
00681 #endif
00682
00683 #ifndef __GNUC__
00684 NutCustomInit();
00685
00686
00687
00688
00689 SP = (uint16_t)(NUTMEM_END);
00690
00691
00692
00693 NutInitHeap();
00694 #endif
00695
00696
00697
00698
00699 if (NutLoadConfig()) {
00700 strcpy(confos.hostname, "ethernut");
00701 NutSaveConfig();
00702 }
00703
00704
00705
00706 NutThreadCreate("idle", NutIdle, 0, NUT_THREAD_IDLESTACK);
00707 }
00708