ethernut5.c
Go to the documentation of this file.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 #include <arch/arm.h>
00045
00046 #ifndef PMM_RST_BASE
00047
00048 #define PMM_RST_BASE PIOB_BASE
00049 #endif
00050
00051 #ifndef PMM_RST_PIN
00052
00053 #define PMM_RST_PIN 8
00054 #endif
00055
00056
00057
00058 #define PMM_REG_ENA 0
00059 #define PMM_REG_DIS 1
00060 #define PMM_REG_STA 2
00061
00062
00063 #define PMM_CPUPWR 0x01
00064 #define PMM_VBUSI 0x02
00065 #define PMM_VBUSO 0x04
00066 #define PMM_MMCPWR 0x08
00067 #define PMM_RS232 0x10
00068 #define PMM_ETHCLK 0x20
00069 #define PMM_ETHRST 0x40
00070 #define PMM_LED 0x80
00071 #define PMM_ALARM 0x80
00072
00080 static void BootLoopDelay(int n)
00081 {
00082 while (n--) {
00083 _NOP();
00084 }
00085 }
00086
00092 static void BootMicroDelay(int us)
00093 {
00094 while (us--) {
00095 BootLoopDelay(200);
00096 }
00097 }
00098
00107 static void BootMilliDelay(int ms)
00108 {
00109 while (ms--) {
00110 BootMicroDelay(1000);
00111 }
00112 }
00113
00117 static void PmmInit(void)
00118 {
00119 #if defined(PMM_RST_BASE) && defined(PMM_RST_PIN)
00120
00121 outr(PMM_RST_BASE + PIO_SODR_OFF, _BV(PMM_RST_PIN));
00122 outr(PMM_RST_BASE + PIO_PER_OFF, _BV(PMM_RST_PIN));
00123 outr(PMM_RST_BASE + PIO_OER_OFF, _BV(PMM_RST_PIN));
00124 BootMilliDelay(1);
00125
00126 outr(PMM_RST_BASE + PIO_CODR_OFF, _BV(PMM_RST_PIN));
00127 BootMilliDelay(100);
00128 #endif
00129
00130 outr(PIOA_ASR, _BV(PA23_TWD_A) | _BV(PA24_TWCK_A));
00131 outr(PIOA_PDR, _BV(PA23_TWD_A) | _BV(PA24_TWCK_A));
00132
00133 outr(PIOA_MDER, _BV(PA23_TWD_A) | _BV(PA24_TWCK_A));
00134
00135 outr(PMC_PCER, _BV(TWI_ID));
00136
00137 outr(TWI_IDR, 0xFFFFFFFF);
00138 outr(TWI_CR, TWI_SWRST);
00139
00140 outr(TWI_CR, TWI_MSEN | TWI_SVDIS);
00141
00142 outr(TWI_CWGR, (7 << TWI_CKDIV_LSB) | (128 << TWI_CHDIV_LSB) | (128 << TWI_CLDIV_LSB));
00143 }
00144
00153 static int PmmWriteReg(unsigned int reg, unsigned int val)
00154 {
00155 volatile int tmo;
00156
00157 outr(TWI_MMR, 0x22 << TWI_DADR_LSB);
00158 outr(TWI_CR, TWI_START);
00159 outr(TWI_THR, reg);
00160 for (tmo = 0; (inr(TWI_SR) & TWI_TXRDY) == 0; tmo++) {
00161 if (tmo > 100000) {
00162 return -1;
00163 }
00164 }
00165 outr(TWI_CR, TWI_STOP);
00166 outr(TWI_THR, val);
00167 for (tmo = 0; (inr(TWI_SR) & TWI_TXCOMP) == 0; tmo++) {
00168 if (tmo > 100000) {
00169 return -1;
00170 }
00171 }
00172 return 0;
00173 }
00174
00178 static void PmmPhyReset(void)
00179 {
00180
00181 outr(PIOA_ODR, _BV(14) | _BV(15) | _BV(17));
00182 outr(PIOA_PUER, _BV(14) | _BV(15) | _BV(17));
00183 outr(PIOA_PER, _BV(14) | _BV(15) | _BV(17));
00184
00185
00186 outr(PIOA_ODR, _BV(18));
00187 outr(PIOA_PUDR, _BV(18));
00188 outr(PIOA_PER, _BV(18));
00189
00190 BootMilliDelay(10);
00191 PmmWriteReg(PMM_REG_ENA, PMM_ETHRST);
00192 BootMilliDelay(1);
00193 PmmWriteReg(PMM_REG_DIS, PMM_ETHRST);
00194 BootMilliDelay(10);
00195 }
00196
00200 void NutBoardInit(void)
00201 {
00202 PmmInit();
00203 PmmPhyReset();
00204 }