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
00131
00132
00133
00134
00135
00136 #include <sys/thread.h>
00137 #include <sys/heap.h>
00138 #include <cfg/memory.h>
00139 #include <cfg/os.h>
00140 #include <cfg/arch/avr.h>
00141 #include <cfg/arch.h>
00142
00147
00148 #ifdef NUTXMEM_SIZE
00149
00152 #define NUTMEM_END (uint16_t)(NUTXMEM_START + (uint16_t)NUTXMEM_SIZE - 1U)
00153
00154 #else
00155
00161 #define NUTMEM_END (uint16_t)(NUTMEM_START + (uint16_t)NUTMEM_SIZE - 1U)
00162
00163 #endif
00164
00165 #ifdef NUTMEM_RESERVED
00166
00176 uint8_t nutmem_onchip[NUTMEM_RESERVED];
00177 #endif
00178
00179
00180 #if defined(__GNUC__) && defined(__AVR_ENHANCED__)
00181 uint8_t idle_sleep_mode = SLEEP_MODE_NONE;
00182
00183
00184 #if defined(SMCR)
00185 #define AVR_SLEEP_CTRL_REG SMCR
00186 #else
00187 #define AVR_SLEEP_CTRL_REG MCUCR
00188 #endif
00189
00190 #endif
00191
00192
00193
00194
00195
00196 #if defined(RTL_EESK_BIT) && defined(__GNUC__)
00197
00198 #ifndef RTL_BASE_ADDR
00199 #define RTL_BASE_ADDR 0x8300
00200 #endif
00201 #define NIC_CR _MMIO_BYTE(RTL_BASE_ADDR)
00202 #define NIC_EE _MMIO_BYTE(RTL_BASE_ADDR + 1)
00203
00204 #if (RTL_EEMU_AVRPORT == AVRPORTB)
00205 #define RTL_EEMU_PORT PORTB
00206 #define RTL_EEMU_DDR DDRB
00207
00208 #elif (RTL_EEMU_AVRPORT == AVRPORTD)
00209 #define RTL_EEMU_PORT PORTD
00210 #define RTL_EEMU_DDR DDRD
00211
00212 #elif (RTL_EEMU_AVRPORT == AVRPORTE)
00213 #define RTL_EEMU_PORT PORTE
00214 #define RTL_EEMU_DDR DDRE
00215
00216 #elif (RTL_EEMU_AVRPORT == AVRPORTF)
00217 #define RTL_EEMU_PORT PORTF
00218 #define RTL_EEMU_DDR DDRF
00219
00220 #else
00221 #define RTL_EE_MEMBUS
00222 #define RTL_EEMU_PORT PORTC
00223 #define RTL_EEMU_DDR DDRC
00224
00225 #endif
00226
00227 #if (RTL_EEDO_AVRPORT == AVRPORTB)
00228 #define RTL_EEDO_PORT PORTB
00229 #define RTL_EEDO_DDR DDRB
00230
00231 #elif (RTL_EEDO_AVRPORT == AVRPORTD)
00232 #define RTL_EEDO_PORT PORTD
00233 #define RTL_EEDO_DDR DDRD
00234
00235 #elif (RTL_EEDO_AVRPORT == AVRPORTE)
00236 #define RTL_EEDO_PORT PORTE
00237 #define RTL_EEDO_DDR DDRE
00238
00239 #elif (RTL_EEDO_AVRPORT == AVRPORTF)
00240 #define RTL_EEDO_PORT PORTF
00241 #define RTL_EEDO_DDR DDRF
00242
00243 #else
00244 #define RTL_EE_MEMBUS
00245 #define RTL_EEDO_PORT PORTC
00246 #define RTL_EEDO_DDR DDRC
00247
00248 #endif
00249
00250 #if (RTL_EESK_AVRPORT == AVRPORTB)
00251 #define RTL_EESK_PIN PINB
00252 #define RTL_EESK_DDR DDRB
00253
00254 #elif (RTL_EESK_AVRPORT == AVRPORTD)
00255 #define RTL_EESK_PIN PIND
00256 #define RTL_EESK_DDR DDRD
00257
00258 #elif (RTL_EESK_AVRPORT == AVRPORTE)
00259 #define RTL_EESK_PIN PINE
00260 #define RTL_EESK_DDR DDRE
00261
00262 #elif (RTL_EESK_AVRPORT == AVRPORTF)
00263 #define RTL_EESK_PIN PINF
00264 #define RTL_EESK_DDR DDRF
00265
00266 #else
00267 #define RTL_EE_MEMBUS
00268 #define RTL_EESK_PIN PINC
00269 #define RTL_EESK_DDR DDRC
00270
00271 #endif
00272 #endif
00273
00274 #ifdef __GNUC__
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296 void NutInit(void) __attribute__ ((naked)) __attribute__ ((section(".init8")));
00297 extern void NutAppMain(void *arg) __attribute__ ((noreturn));
00298 #else
00299 extern void main(void *);
00300 #endif
00301
00302
00303
00304
00305 #if defined(__GNUC__) && defined(NUTXMEM_SIZE)
00306
00307
00308
00309
00310 static void NutInitXRAM(void) __attribute__ ((naked, section(".init1"), used));
00311 void NutInitXRAM(void)
00312 {
00313 #if defined(__AVR_AT90CAN128__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega2561__)
00314
00315
00316
00317
00318 #ifdef NUT_3WAITSTATES
00319 XMCRA = _BV(SRE) | _BV(SRL2) | _BV(SRW00) | _BV(SRW10) | _BV(SRW11);
00320 #else
00321 XMCRA = _BV(SRE) | _BV(SRW10);
00322 #endif
00323
00324 #elif defined(__AVR_ATmega128__)
00325
00326 MCUCR = _BV(SRE) | _BV(SRW10);
00327
00328
00329
00330
00331
00332 #ifdef NUT_3WAITSTATES
00333 XMCRA |= _BV(SRL2) | _BV(SRW00) | _BV(SRW11);
00334 XMCRB = 0;
00335 #endif
00336
00337 #else
00338 MCUCR = _BV(SRE) | _BV(SRW);
00339 #endif
00340 }
00341
00342 #endif
00343
00344
00345 #if defined(RTL_EESK_BIT) && defined(__GNUC__) && defined(NUTXMEM_SIZE)
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357 static void FakeNicEeprom(void) __attribute__ ((naked, section(".init1"), used));
00358 void FakeNicEeprom(void)
00359 {
00360
00361
00362
00363
00364 #ifdef RTL_EEMU_BIT
00365 sbi(RTL_EEMU_PORT, RTL_EEMU_BIT);
00366 sbi(RTL_EEMU_DDR, RTL_EEMU_BIT);
00367 #endif
00368 sbi(RTL_EEDO_PORT, RTL_EEDO_BIT);
00369 sbi(RTL_EEDO_DDR, RTL_EEDO_BIT);
00370
00371
00372 NIC_CR = 0xE1;
00373 NIC_EE = 0x40;
00374
00375
00376 #ifdef RTL_EE_MEMBUS
00377 cbi(MCUCR, SRE);
00378 #endif
00379
00380
00381
00382
00383
00384
00385 __asm__ __volatile__("\n"
00386 "EmuLoop: " "\n"
00387 " ldi r24, 0 " "\n"
00388 " ldi r25, 0 " "\n"
00389 " sbis %0, %1 " "\n"
00390 " rjmp EmuClkClr " "\n"
00391 "EmuClkSet: " "\n"
00392 " adiw r24, 1 " "\n"
00393 " breq EmuDone " "\n"
00394 " sbis %0, %1 " "\n"
00395 " rjmp EmuLoop " "\n"
00396 " rjmp EmuClkSet " "\n"
00397 "EmuClkClr: " "\n"
00398 " adiw r24, 1 " "\n"
00399 " breq EmuDone " "\n"
00400 " sbic %0, %1 " "\n"
00401 " rjmp EmuLoop " "\n"
00402 " rjmp EmuClkClr " "\n"
00403 "EmuDone: \n\t"
00404 :
00405 :"I"(_SFR_IO_ADDR(RTL_EESK_PIN)),
00406 "I"(RTL_EESK_BIT)
00407 :"r24", "r25");
00408
00409
00410 #ifdef RTL_EE_MEMBUS
00411 sbi(MCUCR, SRE);
00412 #endif
00413
00414
00415 #ifdef RTL_EEMU_BIT
00416 cbi(RTL_EEMU_PORT, RTL_EEMU_BIT);
00417 cbi(RTL_EEMU_DDR, RTL_EEMU_BIT);
00418 #endif
00419 cbi(RTL_EEDO_PORT, RTL_EEDO_BIT);
00420 cbi(RTL_EEDO_DDR, RTL_EEDO_BIT);
00421 }
00422
00423 #endif
00424
00436 #if defined(__GNUC__) && defined(__AVR_ENHANCED__)
00437 uint8_t NutThreadSetSleepMode(uint8_t mode)
00438 {
00439 uint8_t old_mode = idle_sleep_mode;
00440 idle_sleep_mode = mode;
00441 return old_mode;
00442 }
00443 #endif
00444
00450 THREAD(NutIdle, arg)
00451 {
00452 #if defined(__GNUC__) && defined(__AVR_ENHANCED__)
00453 uint8_t sleep_mode;
00454 #endif
00455 #ifdef IDLE_HEARTBEAT_BIT
00456 uint8_t beat = 0;
00457 #endif
00458
00459
00460 NutTimerInit();
00461
00462
00463 NutThreadCreate("main", main, 0, NUT_THREAD_MAINSTACK);
00464
00465
00466
00467
00468
00469
00470 NutThreadSetPriority(254);
00471 for (;;) {
00472 NutThreadYield();
00473 NutThreadDestroy();
00474
00475 #ifdef IDLE_HEARTBEAT_BIT
00476 if ((beat = !beat) == 0) {
00477
00478 cbi(IDLE_HEARTBEAT_PORT, IDLE_HEARTBEAT_BIT);
00479 }
00480 else {
00481 sbi(IDLE_HEARTBEAT_PORT, IDLE_HEARTBEAT_BIT);
00482 }
00483 sbi(IDLE_HEARTBEAT_DDR, IDLE_HEARTBEAT_BIT);
00484 #endif
00485
00486 #if defined(__GNUC__) && defined(__AVR_ENHANCED__)
00487 if (idle_sleep_mode != SLEEP_MODE_NONE) {
00488 sleep_mode = AVR_SLEEP_CTRL_REG & _SLEEP_MODE_MASK;
00489 set_sleep_mode(idle_sleep_mode);
00490 #ifdef IDLE_THREAD_ADC_OFF
00491 uint8_t adc = bit_is_set(ADCSR, ADEN);
00492 cbi(ADCSR, ADEN);
00493 #endif
00494 #ifdef IDLE_THREAD_BUSKEEPER_OFF
00495 uint8_t bitkeeper = bit_is_set(XMCRB, XMBK);
00496 cbi(XMCRB, XMBK);
00497 #endif
00498
00499
00500 AVR_SLEEP_CTRL_REG |= _BV(SE);
00501 __asm__ __volatile__ ("sleep" "\n\t" :: );
00502 AVR_SLEEP_CTRL_REG &= ~_BV(SE);
00503 #ifdef IDLE_THREAD_ADC_OFF
00504 if (bitkeeper) {
00505 sbi(XMCRB, XMBK);
00506 }
00507 #endif
00508 #ifdef IDLE_THREAD_BUSKEEPER_OFF
00509 if (adc) {
00510 sbi(ADCSR, ADEN);
00511 }
00512 #endif
00513 set_sleep_mode(sleep_mode);
00514 }
00515 #endif
00516 }
00517 }
00518
00519 #if defined(__GNUC__)
00520 static void NutInitSP(void) __attribute__ ((naked, section (".init5"), used));
00521 void NutInitSP(void)
00522 {
00523 #if defined (__AVR_AT90CAN128__)
00524
00525 #else
00526
00527
00528
00529 SP = (uint16_t)(NUTMEM_END);
00530 #endif
00531 }
00532 #endif
00533
00534 #if defined(__GNUC__)
00535 static void NutInitHeap(void) __attribute__ ((naked, section (".init5"), used));
00536 #endif
00537 void NutInitHeap()
00538 {
00539 #if defined (NUTMEM_STACKHEAP)
00540 NutStackAdd((void *) NUTMEM_START, NUTMEM_STACKHEAP);
00541 #endif
00542
00543
00544
00545
00546
00547
00548 if ((uint16_t)NUTMEM_END - (uint16_t) (&__heap_start) > 384) {
00549 NutHeapAdd(&__heap_start, (uint16_t) NUTMEM_END - 256 - (uint16_t) (&__heap_start));
00550 }
00551 }
00552
00553 #if defined(__GNUC__)
00554 static void NutCustomInit(void) __attribute__ ((naked, section (".init1"), used));
00555 #endif
00556
00565 void NutCustomInit(void)
00566
00567
00568
00569 #if defined(MMNET02) || defined(MMNET03) || defined(MMNET04) ||\
00570 defined(MMNET102) || defined(MMNET103) || defined(MMNET104)
00571 {
00572 volatile uint8_t *breg = (uint8_t *)((size_t)-1 & ~0xFF);
00573
00574 *(breg + 1) = 0x01;
00575
00576
00577 outp(7, UBRR);
00578 outp(7, UBRR1L);
00579 }
00580
00581
00582
00583 #elif defined(ARTHERNET1)
00584 {
00585
00586
00587
00588
00589
00590 MCUCR = _BV(SRE);
00591 XMCRA |= _BV(SRL0) | _BV(SRW01) | _BV(SRW00);
00592 XMCRB = 0;
00593
00594 *((volatile uint8_t *)(ARTHERCPLDSTART)) = 0x10;
00595 *((volatile uint8_t *)(ARTHERCPLDSPI)) = 0xFF;
00596
00597
00598 outp(25, UBRR);
00599 outp(25, UBRR1L);
00600 }
00601
00602
00603
00604 #elif defined(XNUT_100) || defined(XNUT_105)
00605 {
00606 PORTB = 0x35;
00607 DDRB = 0x3F;
00608 PORTD = 0xE8;
00609 DDRD = 0xB0;
00610 PORTE = 0x0E;
00611 DDRE = 0x02;
00612 PORTF = 0xF0;
00613 DDRF = 0x0F;
00614 PORTG = 0x1F;
00615 DDRG = 0x07;
00616
00617 ACSR |= _BV(ACD);
00618
00619
00620 TWSR = 0;
00621 TWBR = (NUT_CPU_FREQ / 100000UL - 16) / 2;
00622
00623
00624 #if NUT_CPU_FREQ == 14745600
00625 UBRR0L = (NUT_CPU_FREQ / (16 * 9600UL)) - 1;
00626 UBRR1L = (NUT_CPU_FREQ / (16 * 9600UL)) - 1;
00627 #else
00628 sbi(UCSR0A, U2X0);
00629 sbi(UCSR1A, U2X1);
00630 UBRR0L = (NUT_CPU_FREQ / (8 * 9600UL)) - 1;
00631 UBRR1L = (NUT_CPU_FREQ / (8 * 9600UL)) - 1;
00632 #endif
00633 }
00634
00635
00636
00637 #else
00638 {
00639
00640 outp(7, UBRR);
00641 #ifdef __AVR_ENHANCED__
00642 outp(7, UBRR1L);
00643 #endif
00644 }
00645 #endif
00646
00679 void NutInit(void)
00680 {
00681
00682
00683
00684
00685 #ifdef NUTDEBUG
00686
00687 outp(BV(RXEN) | BV(TXEN), UCR);
00688 #endif
00689
00690 #ifndef __GNUC__
00691 NutCustomInit();
00692
00693
00694
00695
00696 SP = (uint16_t)(NUTMEM_END);
00697
00698
00699
00700 NutInitHeap();
00701 #endif
00702
00703
00704
00705
00706 if (NutLoadConfig()) {
00707 strcpy(confos.hostname, "ethernut");
00708 NutSaveConfig();
00709 }
00710
00711
00712
00713 NutThreadCreate("idle", NutIdle, 0, NUT_THREAD_IDLESTACK);
00714 }
00715