Nut/OS  4.10.3
API Reference
ethernut1.c
Go to the documentation of this file.
00001 /*
00002  * Copyright 2010 by egnite GmbH
00003  *
00004  * All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  *
00010  * 1. Redistributions of source code must retain the above copyright
00011  *    notice, this list of conditions and the following disclaimer.
00012  * 2. Redistributions in binary form must reproduce the above copyright
00013  *    notice, this list of conditions and the following disclaimer in the
00014  *    documentation and/or other materials provided with the distribution.
00015  * 3. Neither the name of the copyright holders nor the names of
00016  *    contributors may be used to endorse or promote products derived
00017  *    from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00020  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00021  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00022  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00023  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00024  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00025  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00026  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00027  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00028  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00029  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00030  * SUCH DAMAGE.
00031  *
00032  * For additional information see http://www.ethernut.de/
00033  */
00034 
00035 /*
00036  * \file arch/avr/board/ethernut1.c
00037  * \brief Ethernut 1 board initialization.
00038  *
00039  * \verbatim
00040  * $Id$
00041  * \endverbatim
00042  */
00043 
00044 #include <cfg/memory.h>
00045 #include <cfg/arch/avr.h>
00046 
00047 #include <arch/avr.h>
00048 
00049 #if defined(RTL_EESK_BIT) && defined(__GNUC__) && defined(NUTXMEM_SIZE)
00050 
00051 /*
00052  * Macros used for RTL8019AS EEPROM Emulation.
00053  * See FakeNicEeprom().
00054  */
00055 #ifndef RTL_BASE_ADDR
00056 #define RTL_BASE_ADDR 0x8300
00057 #endif
00058 #define NIC_CR  _MMIO_BYTE(RTL_BASE_ADDR)
00059 #define NIC_EE  _MMIO_BYTE(RTL_BASE_ADDR + 1)
00060 
00061 #if (RTL_EEMU_AVRPORT == AVRPORTB)
00062 #define RTL_EEMU_PORT   PORTB
00063 #define RTL_EEMU_DDR    DDRB
00064 
00065 #elif (RTL_EEMU_AVRPORT == AVRPORTD)
00066 #define RTL_EEMU_PORT   PORTD
00067 #define RTL_EEMU_DDR    DDRD
00068 
00069 #elif (RTL_EEMU_AVRPORT == AVRPORTE)
00070 #define RTL_EEMU_PORT   PORTE
00071 #define RTL_EEMU_DDR    DDRE
00072 
00073 #elif (RTL_EEMU_AVRPORT == AVRPORTF)
00074 #define RTL_EEMU_PORT   PORTF
00075 #define RTL_EEMU_DDR    DDRF
00076 
00077 #else
00078 #define RTL_EE_MEMBUS
00079 #define RTL_EEMU_PORT   PORTC
00080 #define RTL_EEMU_DDR    DDRC
00081 
00082 #endif /* RTL_EEMU_AVRPORT */
00083 
00084 #if (RTL_EEDO_AVRPORT == AVRPORTB)
00085 #define RTL_EEDO_PORT   PORTB
00086 #define RTL_EEDO_DDR    DDRB
00087 
00088 #elif (RTL_EEDO_AVRPORT == AVRPORTD)
00089 #define RTL_EEDO_PORT   PORTD
00090 #define RTL_EEDO_DDR    DDRD
00091 
00092 #elif (RTL_EEDO_AVRPORT == AVRPORTE)
00093 #define RTL_EEDO_PORT   PORTE
00094 #define RTL_EEDO_DDR    DDRE
00095 
00096 #elif (RTL_EEDO_AVRPORT == AVRPORTF)
00097 #define RTL_EEDO_PORT   PORTF
00098 #define RTL_EEDO_DDR    DDRF
00099 
00100 #else
00101 #define RTL_EE_MEMBUS
00102 #define RTL_EEDO_PORT   PORTC
00103 #define RTL_EEDO_DDR    DDRC
00104 
00105 #endif /* RTL_EEDO_AVRPORT */
00106 
00107 #if (RTL_EESK_AVRPORT == AVRPORTB)
00108 #define RTL_EESK_PIN    PINB
00109 #define RTL_EESK_DDR    DDRB
00110 
00111 #elif (RTL_EESK_AVRPORT == AVRPORTD)
00112 #define RTL_EESK_PIN    PIND
00113 #define RTL_EESK_DDR    DDRD
00114 
00115 #elif (RTL_EESK_AVRPORT == AVRPORTE)
00116 #define RTL_EESK_PIN    PINE
00117 #define RTL_EESK_DDR    DDRE
00118 
00119 #elif (RTL_EESK_AVRPORT == AVRPORTF)
00120 #define RTL_EESK_PIN    PINF
00121 #define RTL_EESK_DDR    DDRF
00122 
00123 #else
00124 #define RTL_EE_MEMBUS
00125 #define RTL_EESK_PIN    PINC
00126 #define RTL_EESK_DDR    DDRC
00127 
00128 #endif /* RTL_EESK_AVRPORT */
00129 
00130 
00131 /*
00132  * Before using extended memory, we need to run the RTL8019AS EEPROM emulation.
00133  * Not doing this may put this controller in a bad state, where it interferes
00134  * the data/address bus.
00135  *
00136  * This function has to be called as early as possible but after the external
00137  * memory interface has been enabled.
00138  *
00139  * The following part is used by the GCC environment only. For ICCAVR it has
00140  * been included in the C startup file.
00141  */
00142 static void FakeNicEeprom(void) __attribute__ ((naked, section(".init1"), used));
00143 void FakeNicEeprom(void)
00144 {
00145     /*
00146      * Prepare the EEPROM emulation port bits. Configure the EEDO
00147      * and the EEMU lines as outputs and set both lines to high.
00148      */
00149 #ifdef RTL_EEMU_BIT
00150     sbi(RTL_EEMU_PORT, RTL_EEMU_BIT);
00151     sbi(RTL_EEMU_DDR, RTL_EEMU_BIT);
00152 #endif
00153     sbi(RTL_EEDO_PORT, RTL_EEDO_BIT);
00154     sbi(RTL_EEDO_DDR, RTL_EEDO_BIT);
00155 
00156     /* Force the chip to re-read the EEPROM contents. */
00157     NIC_CR = 0xE1;
00158     NIC_EE = 0x40;
00159 
00160     /* No external memory access beyond this point. */
00161 #ifdef RTL_EE_MEMBUS
00162     cbi(MCUCR, SRE);
00163 #endif
00164 
00165     /*
00166      * Loop until the chip stops toggling our EESK input. We do it in
00167      * assembly language to make sure, that no external RAM is used
00168      * for the counter variable.
00169      */
00170     __asm__ __volatile__("\n"   /* */
00171                          "EmuLoop:               " "\n" /* */
00172                          "        ldi  r24, 0    " "\n" /* Clear counter. */
00173                          "        ldi  r25, 0    " "\n" /* */
00174                          "        sbis %0, %1    " "\n" /* Check if EESK set. */
00175                          "        rjmp EmuClkClr " "\n" /* */
00176                          "EmuClkSet:             " "\n" /* */
00177                          "        adiw r24, 1    " "\n" /* Count loops with EESK set. */
00178                          "        breq EmuDone   " "\n" /* Exit loop on counter overflow. */
00179                          "        sbis %0, %1    " "\n" /* Test if EESK is still set. */
00180                          "        rjmp EmuLoop   " "\n" /* EESK changed, do another loop. */
00181                          "        rjmp EmuClkSet " "\n" /* Continue waiting. */
00182                          "EmuClkClr:             " "\n" /* */
00183                          "        adiw r24, 1    " "\n" /* Count loops with EESK clear. */
00184                          "        breq EmuDone   " "\n" /* Exit loop on counter overflow. */
00185                          "        sbic %0, %1    " "\n" /* Test if EESK is still clear. */
00186                          "        rjmp EmuLoop   " "\n" /* EESK changed, do another loop. */
00187                          "        rjmp EmuClkClr " "\n" /* Continue waiting. */
00188                          "EmuDone:               \n\t"  /* */
00189                          :      /* No outputs. */
00190                          :"I"(_SFR_IO_ADDR(RTL_EESK_PIN)), /* Emulation port. */
00191                           "I"(RTL_EESK_BIT)                 /* EESK bit. */
00192                          :"r24", "r25");
00193 
00194     /* Enable memory interface. */
00195 #ifdef RTL_EE_MEMBUS
00196     sbi(MCUCR, SRE);
00197 #endif
00198 
00199     /* Reset port outputs to default. */
00200 #ifdef RTL_EEMU_BIT
00201     cbi(RTL_EEMU_PORT, RTL_EEMU_BIT);
00202     cbi(RTL_EEMU_DDR, RTL_EEMU_BIT);
00203 #endif
00204     cbi(RTL_EEDO_PORT, RTL_EEDO_BIT);
00205     cbi(RTL_EEDO_DDR, RTL_EEDO_BIT);
00206 }
00207 
00208 #endif /* RTL_EESK_BIT && __GNUC__ && NUTXMEM_SIZE */
00209 
00213 void NutBoardInit(void)
00214 {
00215 }
00216