isp2.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
00042
00043
00044
00045
00046 #include <stdio.h>
00047 #include <io.h>
00048 #include <sys/timer.h>
00049 #include <dev/debug.h>
00050 #include <dev/urom.h>
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074 #define ISPMOSI_PORT PORTE
00075 #define ISPMOSI_DDR DDRE
00076 #define ISPMOSI_BIT 0
00077
00078 #define ISPMISO_PORT PORTE
00079 #define ISPMISO_DDR DDRE
00080 #define ISPMISO_PIN PINE
00081 #define ISPMISO_BIT 1
00082
00083 #define ISPSCK_PORT PORTB
00084 #define ISPSCK_DDR DDRB
00085 #define ISPSCK_BIT 1
00086
00090 static uint8_t SpiByte(uint8_t c)
00091 {
00092 uint8_t i;
00093
00094 for(i = 0; i < 8; i++) {
00095 if(c & 0x80)
00096 sbi(ISPMOSI_PORT, ISPMOSI_BIT);
00097 else
00098 cbi(ISPMOSI_PORT, ISPMOSI_BIT);
00099 sbi(ISPSCK_PORT, ISPSCK_BIT);
00100 c <<= 1;
00101 if(bit_is_set(ISPMISO_PIN, ISPMISO_BIT))
00102 c++;
00103 cbi(ISPSCK_PORT, ISPSCK_BIT);
00104 }
00105 cbi(ISPMOSI_PORT, ISPMOSI_BIT);
00106
00107 return c;
00108 }
00109
00115 int SpiFlashEnable(void)
00116 {
00117 uint8_t i;
00118 uint8_t rc;
00119
00120 cbi(ISPMOSI_PORT, ISPMOSI_BIT);
00121 sbi(ISPMOSI_DDR, ISPMOSI_BIT);
00122
00123 cbi(ISPMISO_PORT, ISPMISO_BIT);
00124 cbi(ISPMISO_DDR, ISPMISO_BIT);
00125
00126 cbi(ISPSCK_PORT, ISPSCK_BIT);
00127 sbi(ISPSCK_DDR, ISPSCK_BIT);
00128
00129 for (i = 0; i < 32; i++) {
00130
00131
00132
00133
00134 SpiByte(0xAC);
00135 SpiByte(0x53);
00136 rc = SpiByte(0xFF);
00137 SpiByte(0xff);
00138
00139 if (rc == 0x53)
00140 return 0;
00141
00142
00143
00144
00145
00146
00147 sbi(ISPSCK_PORT, ISPSCK_BIT);
00148 cbi(ISPSCK_PORT, ISPSCK_BIT);
00149 }
00150 return -1;
00151 }
00152
00159 void SpiFlashId(uint8_t * id)
00160 {
00161 uint8_t i;
00162
00163 for (i = 0; i < 3; i++) {
00164 SpiByte(0x30);
00165 SpiByte(0x00);
00166 SpiByte(i);
00167 id[i] = SpiByte(0x00);
00168 }
00169 }
00170
00184 int SpiFlashWriteByte(uint8_t high, uint16_t addr, uint8_t data)
00185 {
00186 uint8_t d;
00187
00188 if (data != 0xff) {
00189 SpiByte(0x40 | high);
00190 SpiByte(addr >> 8);
00191 SpiByte(addr & 0xFF);
00192 SpiByte(data);
00193
00194
00195
00196
00197
00198
00199
00200 if (data == 0x7f)
00201 NutDelay(10);
00202 else {
00203 for (d = 0; d < 255; d++) {
00204
00205
00206
00207 SpiByte(0x20 | high);
00208 SpiByte(addr >> 8);
00209 SpiByte(addr & 0xFF);
00210 if (SpiByte(0xFF) == data)
00211 break;
00212 }
00213 if (d == 255) {
00214 return -1;
00215 }
00216 }
00217 }
00218 return 0;
00219 }
00220
00229 int SpiFlashWriteWord(uint16_t addr, uint16_t data)
00230 {
00231 if (SpiFlashWriteByte(0, addr, data & 0xFF))
00232 return -1;
00233 if (SpiFlashWriteByte(8, addr, data >> 8))
00234 return -1;
00235
00236 return 0;
00237 }
00238
00245 void SpiFlashErase(void)
00246 {
00247
00248
00249
00250 SpiByte(0xAC);
00251 SpiByte(0x80);
00252 SpiByte(0x00);
00253 SpiByte(0x00);
00254 NutDelay(50);
00255 }
00256
00257
00258 int main(void)
00259 {
00260 uint8_t id[3];
00261 uint32_t baud = 115200;
00262 char *filename = "UROM:sisp.bin";
00263 int val;
00264 uint16_t word;
00265 uint16_t addr;
00266 FILE *fp;
00267
00268
00269
00270
00271
00272 NutRegisterDevice(&devDebug1, 0, 0);
00273 NutRegisterDevice(&devUrom, 0, 0);
00274
00275
00276
00277
00278 freopen("uart1", "w", stdout);
00279 _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
00280 puts("\nISP2 1.0.1");
00281
00282
00283
00284
00285 fp = fopen(filename, "rb");
00286 if (fp == 0) {
00287 printf("ERROR: Failed to open %s\n", filename);
00288 for(;;);
00289 }
00290
00291
00292
00293
00294 printf("Enable programming...");
00295 if(SpiFlashEnable()) {
00296 puts("failed\n"
00297 "Make sure that the ISP adapter is connected to the\n"
00298 "Ethernut ISP port and that the MCU on the adapter\n"
00299 "is held in reset state.");
00300 for(;;);
00301 }
00302 puts("OK");
00303
00304
00305
00306
00307 printf("Reading signature... ");
00308 SpiFlashId(id);
00309 if(id[0] != 0x1E || id[1] != 0x91 || id[2] != 0x01) {
00310 printf("unexpected %02X%02X%02X\n", id[0], id[1], id[2]);
00311 }
00312 else {
00313 puts("OK");
00314 }
00315
00316
00317
00318
00319 printf("Erasing device... ");
00320 SpiFlashErase();
00321 puts("OK");
00322
00323 printf("Programming device");
00324 addr = 0;
00325 for (;;) {
00326 if((addr & 0xFF) == 0)
00327 putchar('.');
00328 if((val = fgetc(fp)) == EOF) {
00329 puts("OK");
00330 break;
00331 }
00332 word = ((uint8_t)fgetc(fp) << 8) + (uint8_t)val;
00333 if (SpiFlashWriteWord(addr, word)) {
00334 printf("failed at %04X\n", addr);
00335 break;
00336 }
00337 addr++;
00338 }
00339 for(;;);
00340 }