1 //========================================================================
2 // ####ECOSGPLCOPYRIGHTBEGIN####
3 // -------------------------------------------
4 // This file is part of eCos, the Embedded Configurable Operating System.
5 // Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
7 // eCos is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU General Public License as published by the Free
9 // Software Foundation; either version 2 or (at your option) any later
12 // eCos is distributed in the hope that it will be useful, but WITHOUT
13 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 // You should have received a copy of the GNU General Public License
18 // along with eCos; if not, write to the Free Software Foundation, Inc.,
19 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 // As a special exception, if other files instantiate templates or use
22 // macros or inline functions from this file, or you compile this file
23 // and link it with other works to produce a work based on this file,
24 // this file does not by itself cause the resulting work to be covered by
25 // the GNU General Public License. However the source code for this file
26 // must still be made available in accordance with section (3) of the GNU
27 // General Public License v2.
29 // This exception does not invalidate any other reasons why a work based
30 // on this file might be covered by the GNU General Public License.
31 // -------------------------------------------
32 // ####ECOSGPLCOPYRIGHTEND####
33 //========================================================================
34 #include <cyg/hal/io.h>
35 #include <cyg/hal/system.h>
40 #include "addresses.h"
41 #include "bootloader.h"
43 //#include <phi_network_support.h>
45 static char FIRMWARE_FILE
[] = "/ram/firmware.phi";
46 static char BOOTLOADER_FILE
[] = "/ram/bootloader.phi";
47 static char FPGA_FILE
[] = "/ram/fpga.phi";
48 static char IP_FILE
[] = "/config/ip";
49 static char MAC_FILE
[] = "/config/mac";
50 static const int WRITE_BUF_SIZE
= 4096;
51 static char WRITE_BUF
[WRITE_BUF_SIZE
];
56 static void writeMac(cyg_uint8 mac
[6]);
57 static void writeFile(const char *fileName
, const char *string
);
58 static bool hasMacAddress();
59 static void printMACAddress();
70 bool getChar(char *key
)
75 /* Watch stdin (fd 0) to see when it has input. */
79 retval
= select(1, &rfds
, NULL
, NULL
, NULL
);
80 /* Don't rely on the value of tv now! */
83 if (FD_ISSET(ser
, &rfds
))
85 if (read(ser
, key
, 1) == 1)
94 bool waitChar1(int seconds
, char *key
)
99 static char chr
[1024];
101 /* Watch stdin (fd 0) to see when it has input. */
104 /* Wait this long seconds. */
108 retval
= select(1, &rfds
, NULL
, NULL
, &tv
);
109 /* Don't rely on the value of tv now! */
112 if (FD_ISSET(ser
, &rfds
))
113 { // linux, for win see getChar
114 if (read(ser
, chr
, 1024) > 0)
127 bool waitChar(int seconds
, char *key
)
133 /* Watch stdin (fd 0) to see when it has input. */
137 /* Wait this long seconds. */
141 retval
= select(1, &rfds
, NULL
, NULL
, &tv
);
142 /* Don't rely on the value of tv now! */
145 if (FD_ISSET(ser
, &rfds
))
147 if (read(ser
, key
, 1) == 1)
158 fprintf(ser_fp
, "Resetting\r\n");
161 IOWR(REMOTE_UPDATE_BASE
, 0x20, 0x1);
166 ser
= open(UART_0_NAME
, O_RDWR
|O_SYNC
|O_NONBLOCK
);
169 diag_printf("Serial device problems %s\r\n", UART_0_NAME
);
173 ser_fp
= fdopen(ser
, "r+");
177 diag_printf("Serial device problems %s\r\n", UART_0_NAME
);
184 fprintf(ser_fp
, "Formatting JFFS2...\r\n");
188 if ((stat
= flash_init(0)) != 0)
190 fprintf(ser_fp
, "Flash Error flash_init: \r\n");
193 #ifdef CYGHWR_IO_FLASH_BLOCK_LOCKING
194 if ((stat
= flash_unlock((void *) EXT_FLASH_BASE
, EXT_FLASH_SPAN
,
195 (void **) &err_addr
)) != 0)
197 fprintf(ser_fp
, "Flash Error flash_unlock: %d\r\n", stat
);
201 fprintf(ser_fp
, "Formatting 0x%08x bytes\r\n", JFFS2_LENGTH
);
202 if ((stat
= flash_erase((void *) (EXT_FLASH_BASE
+ JFFS2_OFFSET
), JFFS2_LENGTH
,
203 (void **) &err_addr
)) != 0)
205 fprintf(ser_fp
, "Flash Error flash_erase: %d\r\n", stat
);
208 fprintf(ser_fp
, "Flash formatted successfully\r\n");
212 int firmwareFile
, fpgaFile
;
214 static bool expect(const char *string
, bool resetOnFailure
= true)
216 // fprintf(ser_fp, "Expecting \"%s\"\r\n", string);
217 for (size_t i
= 0; i
< strlen(string
); i
++)
220 if (read(firmwareFile
, &t
, 1) != 1)
222 fprintf(ser_fp
, "Error: reading firmware file %d, expecting \"%s\"\r\n",
230 lseek(firmwareFile
, -1, SEEK_CUR
);
239 "Unexpected data in firmware file while expecting \"%s\"\r\n",
245 lseek(firmwareFile
, -1, SEEK_CUR
);
253 /* read integer which is terminated by whitespace or eof */
261 if (i
>= sizeof(buf
))
263 fprintf(ser_fp
, "Error: reading string. Too long %d\r\n", (int) i
);
269 actual
= read(firmwareFile
, &t
, 1);
272 fprintf(ser_fp
, "Error: reading integer %d\r\n", errno
);
277 if (!isspace((int) t
))
286 else if (actual
== 0)
292 fprintf(ser_fp
, "Error: reading integer\r\n");
300 void appendPadding(char* buffer
, int start
, int length
)
303 for (i
= 0; i
< length
; i
++)
305 buffer
[start
+ i
] = 0xFF;
309 static void upgradeBootloader()
311 /* Do we have a pending bootloader update? */
312 if ((firmwareFile
= open(BOOTLOADER_FILE
, O_RDONLY
)) > 0)
314 fprintf(ser_fp
, "Single shot bootloader update in progress\r\n");
316 /* if (!expect("ZylinPhiBootloader\r\n", false))
319 fprintf(ser_fp, "Corrupt bootloader image uploaded. Safely aborting bootloader update.\r\n");
320 remove(BOOTLOADER_FILE);
324 cyg_uint8
*bootloaderAddr
= (cyg_uint8
*) (EXT_FLASH_BASE
325 + FACTORY_FPGA_OFFSET
);
328 if (stat(BOOTLOADER_FILE
, &results
) == 0)
330 fprintf(ser_fp
, "size %ld\r\n", results
.st_size
);
334 bool hasMac
= hasMacAddress();
338 fprintf(ser_fp
, "Erasing flash...");
339 if ((stat
= flash_erase((void *) (bootloaderAddr
), APPLICATION_FPGA_OFFSET
340 - FACTORY_FPGA_OFFSET
, (void **) &err_addr
)) != 0)
342 fprintf(ser_fp
, "Error: erasing bootloader %p: %d\r\n", err_addr
, stat
);
345 fprintf(ser_fp
, "done\r\n");
349 while ((actual
= read(firmwareFile
, buf
, sizeof(buf
))) > 0)
353 int rem
= actual
% LENGTH
;
357 appendPadding(buf
, actual
, rem
);
361 = FLASH_PROGRAM(bootloaderAddr
, buf
, actual
+ rem
, (void **)&err_addr
))
364 fprintf(ser_fp
, "Error: writing bootloader data at %p: %d\r\n", err_addr
,
369 bootloaderAddr
+= actual
;
376 fprintf(ser_fp
, "Error: catastrophic failure. Bootloader corrupt %d.\r\n",
378 remove(BOOTLOADER_FILE
);
384 // FIX!!! This won't work. If we ever type the wrong mac number, we're screwed.
385 // fprintf(ser_fp, "Updating mac address");
389 fprintf(ser_fp
, "Bootloader successfully updated.\r\n");
390 remove(BOOTLOADER_FILE
);
395 static void upgradeFirmware()
397 /* Do we have a pending firmware update? */
398 if ((firmwareFile
= open(FIRMWARE_FILE
, O_RDONLY
)) > 0)
400 fprintf(ser_fp
, "Firmware update in progress\r\n");
402 /* if (!expect("ZylinPhiBootloader\r\n", false))
405 fprintf(ser_fp, "Corrupt bootloader image uploaded. Safely aborting bootloader update.\r\n");
406 remove(BOOTLOADER_FILE);
410 cyg_uint8
*firmwareAddr
= (cyg_uint8
*) (EXT_FLASH_BASE
411 + APPLICATION_FPGA_OFFSET
);
414 if (stat(FIRMWARE_FILE
, &results
) == 0)
416 fprintf(ser_fp
, "size %ld\r\n", results
.st_size
);
420 bool hasMac
= hasMacAddress();
424 fprintf(ser_fp
, "Erasing flash...");
425 if ((stat
= flash_erase((void *) (firmwareAddr
), BOOTLOADER_OFFSET
426 - APPLICATION_FPGA_OFFSET
, (void **) &err_addr
)) != 0)
428 fprintf(ser_fp
, "Error: erasing firmware %p: %d\r\n", err_addr
, stat
);
431 fprintf(ser_fp
, "done\r\n");
435 while ((actual
= read(firmwareFile
, buf
, sizeof(buf
))) > 0)
439 int rem
= actual
% LENGTH
;
443 appendPadding(buf
, actual
, rem
);
447 = FLASH_PROGRAM(firmwareAddr
, buf
, actual
+ rem
, (void **)&err_addr
))
450 fprintf(ser_fp
, "Error: writing bootloader data at %p: %d\r\n", err_addr
,
455 firmwareAddr
+= actual
;
462 fprintf(ser_fp
, "Error: catastrophic failure. Bootloader corrupt %d.\r\n",
464 remove(FIRMWARE_FILE
);
470 // FIX!!! This won't work. If we ever type the wrong mac number, we're screwed.
471 // fprintf(ser_fp, "Updating mac address");
475 fprintf(ser_fp
, "Firmware successfully updated.\r\n");
476 remove(FIRMWARE_FILE
);
481 static void upgradeFPGA(int start
, int stop
)
483 /* Do we have a pending FPGA update? */
484 if ((fpgaFile
= open(FPGA_FILE
, O_RDONLY
)) > 0)
486 fprintf(ser_fp
, "FPGA update in progress\r\n");
488 /* if (!expect("ZylinPhiBootloader\r\n", false))
491 fprintf(ser_fp, "Corrupt bootloader image uploaded. Safely aborting bootloader update.\r\n");
492 remove(BOOTLOADER_FILE);
496 cyg_uint8
*fpgaAddr
= (cyg_uint8
*) (EXT_FLASH_BASE
+ start
);
499 if (stat(FPGA_FILE
, &results
) == 0)
501 fprintf(ser_fp
, "size %ld\r\n", results
.st_size
);
505 bool hasMac
= hasMacAddress();
509 fprintf(ser_fp
, "Erasing flash...from %0x size %0x", fpgaAddr
, stop
- start
);
510 if ((stat
= flash_erase((void *) (fpgaAddr
), stop
- start
,
511 (void **) &err_addr
)) != 0)
513 fprintf(ser_fp
, "Error: erasing fpga %p: %d\r\n", err_addr
, stat
);
516 fprintf(ser_fp
, "done\r\n");
520 while ((actual
= read(fpgaFile
, buf
, sizeof(buf
))) > 0)
525 int rem
= actual
% LENGTH
;
529 appendPadding(buf
, actual
, rem
);
532 fprintf(ser_fp
, "%d\r\n", fpgaAddr
);
534 = FLASH_PROGRAM(fpgaAddr
, buf
, actual
+ rem
, (void **)&err_addr
))
537 fprintf(ser_fp
, "Error: writing fpga data at %p: %d\r\n", err_addr
, stat
);
548 fprintf(ser_fp
, "Error: catastrophic failure. fpga corrupt %d.\r\n", errno
);
555 // FIX!!! This won't work. If we ever type the wrong mac number, we're screwed.
556 // fprintf(ser_fp, "Updating mac address");
560 fprintf(ser_fp
, "FPGA successfully updated. %d\r\n", actual
);
566 // Read a string in from serial port and reset if
567 // anything goes wrong.
568 static void readLine(char *buffer
,
569 // Including terminating \0
580 fprintf(ser_fp
, "\r\nCtrl-c pressed\r\n");
586 fprintf(ser_fp
, "\r\nEmpty string not allowed\r\n");
589 buffer
[index
] = '\0';
590 fprintf(ser_fp
, "\r\n");
597 fprintf(ser_fp
, "%c %c", c
, c
);
601 if (index
>= (maxLen
- 1))
603 fprintf(ser_fp
, "\r\nString too long\r\n");
606 fprintf(ser_fp
, "%c", c
);
614 static void getFileName(char *name
, int maxLen
)
616 readLine(name
, maxLen
);
619 static void wrongMAC()
621 fprintf(ser_fp
, "Wrong MAC address syntax\r\n");
625 static int getMacAddress(char* buffer
)
630 int fd
= open(MAC_FILE
, O_RDONLY
);
633 fprintf(ser_fp
, "Could not open %s\r\n", MAC_FILE
);
637 for (int i
= 0; i
< 12; i
++)
641 actual
= read(fd
, &c
, 1);
644 fprintf(ser_fp
, "\r\nFailed while reading %s\r\n", MAC_FILE
);
652 fprintf(ser_fp
, "\r\n");
657 static cyg_uint8
* transformMacAddress(char* buffer
, cyg_uint8 mac_addr
[6])
666 if (strlen(buffer
) != 12)
671 for (size_t i
= 0; i
< strlen(buffer
); i
++)
674 if (('0' <= c
&& c
<= '9') || ('a' <= c
&& c
<= 'f') || ('A' <= c
&& c
678 sscanf(&c
, "%x", &val
);
679 mac_addr
[i
/ 2] += (val
<< (4 * ((i
+ 1) % 2)));
691 static bool hasMacAddress()
694 cyg_uint8
* mac
= (cyg_uint8
*) (EXT_FLASH_BASE
+ FACTORY_FPGA_OFFSET
- 6);
696 cyg_uint8 ret
= 0xFF;
697 for(int i
= 0; i
< 6; i
++)
705 static void printMACAddress()
709 cyg_uint8
* mac
= (cyg_uint8
*) (EXT_FLASH_BASE
+ FACTORY_FPGA_OFFSET
- 6);
710 fprintf(ser_fp
, "Mac address %02x:%02x:%02x:%02x:%02x:%02x\r\n", mac
[0],
711 mac
[1], mac
[2], mac
[3], mac
[4], mac
[5]);
714 fprintf(ser_fp
, "Mac address not set\r\n");
717 static void changeMac()
722 fprintf(ser_fp
, "Enter the mac address in the format XXXXXXXXXXXX \r\n");
723 readLine(mac
, sizeof(mac
));
725 transformMacAddress(mac
, ui_mac
);
726 fprintf(ser_fp
, "Mac address %02x:%02x:%02x:%02x:%02x:%02x\r\n", ui_mac
[0],
727 ui_mac
[1], ui_mac
[2], ui_mac
[3], ui_mac
[4], ui_mac
[5]);
732 cyg_uint8
*macAddr
= (cyg_uint8
*) (EXT_FLASH_BASE
+ FACTORY_FPGA_OFFSET
- 6);
734 #ifdef CYGHWR_IO_FLASH_BLOCK_LOCKING
735 if ((stat
= flash_unlock((void *) macAddr
, 6,
736 (void **) &err_addr
)) != 0)
738 fprintf(ser_fp
, "Flash Error flash_unlock: %d\r\n", stat
);
742 if ((stat
= flash_erase((void *) (macAddr
), 6, (void **) &err_addr
)) != 0)
744 printf("Error: erasing bootloader %p: %d\n", err_addr
, stat
);
747 printf("erasing done\n");
749 if ((stat
= FLASH_PROGRAM(macAddr
, ui_mac
, 6, (void **)&err_addr
))
752 printf("Error: writing bootloader data at %p: %d\n", err_addr
, stat
);
758 static void writeFile(const char *fileName
, const char *string
)
760 if (strlen(string
) == 0)
766 int fd
= creat(fileName
, O_CREAT
| O_TRUNC
);
769 fprintf(ser_fp
, "unable to create %s\r\n", fileName
);
773 write(fd
, string
, strlen(string
));
779 static void enterParameter()
784 fprintf(ser_fp
, "Enter file name: ");
785 readLine(name
, sizeof(name
));
786 fprintf(ser_fp
, "Enter parameter: ");
787 readLine(param
, sizeof(param
));
788 writeFile(name
, param
);
792 static void showParameter()
796 fprintf(ser_fp
, "Enter filename: ");
797 readLine(name
, sizeof(name
));
800 if ((param
= open(name
, O_RDONLY
)) < 0)
802 fprintf(ser_fp
, "Could not open %s\r\n", name
);
805 fprintf(ser_fp
, "Displaying up to 1024 bytes of that parameter\r\n");
806 for (int i
= 0; i
< 1024; i
++)
810 actual
= read(param
, &c
, 1);
813 fprintf(ser_fp
, "\r\nFailed while reading %s\r\n", name
);
818 fprintf(ser_fp
, "%c", c
);
820 fprintf(ser_fp
, "\r\n");
824 static void changeIP()
829 "\r\nEnter ip, mask and gateway(optional) (x.x.x.x,y.y.y.y[,z.z.z.z]): ");
830 readLine(ip
, sizeof(ip
));
831 writeFile(IP_FILE
, ip
);
834 static void ymodemUpload(const char *fileName
)
838 fprintf(ser_fp
, "Start Ymodem upload of %s\r\n", fileName
);
840 connection_info_t connection
;
841 memset(&connection
, 0, sizeof(connection
));
842 connection
.mode
= xyzModem_ymodem
;
844 fprintf(ser_fp
, "connection over %s\r\n", fileName
);
846 if (xyzModem_stream_open(&connection
, &err
) == 0)
848 firmwareFile
= creat(fileName
, O_TRUNC
| O_CREAT
);
849 if (firmwareFile
< 0)
853 // close yModem connection so we can see error message in HyperTerminal
855 xyzModem_stream_close(&moreError
);
857 fprintf(ser_fp
, "Could not create firmware file %d\r\n", t
);
863 bool abortedByWrite
= false;
865 /* make sure we don't write too small blocks as this will
866 * increase memory usage catastrophically when reading the file
874 actual
= xyzModem_stream_read(WRITE_BUF
+ pos
, sizeof(WRITE_BUF
)
881 if (((actual
== 0) && (pos
> 0)) || ((actual
+ pos
)
882 > (WRITE_BUF_SIZE
- 0x100)))
884 int written
= write(firmwareFile
, WRITE_BUF
, actual
+ pos
);
885 if (written
< (actual
+ pos
))
887 fprintf(ser_fp
, "Writing %s failed %d\r\n", fileName
, errno
);
888 abortedByWrite
= true;
904 xyzModem_stream_close(&moreError
);
906 if ((!abortedByWrite
) && (actual
== 0) && (err
== 0))
908 fprintf(ser_fp
, "\r\nFirmware successfully uploaded\r\n");
919 fprintf(ser_fp
, "\r\nFirmware upload failed: %s\r\n", xyzModem_error(err
));
925 static void selectFile(char *fileName
)
928 char *names
[10]; //max 10 files
929 DIR* fs
= opendir("/config");
931 for (index
= 0; index
< 10;)
933 struct dirent
*entry
= readdir(fs
);
937 len
= strlen(entry
->d_name
);
938 if (len
> 4 && (entry
->d_name
[len
- 4] == '.' && ((entry
->d_name
[len
939 - 3] == 'p' && entry
->d_name
[len
- 2] == 'h'
940 && entry
->d_name
[len
- 1] == 'i') || (entry
->d_name
[len
- 3]
941 == 'z' && entry
->d_name
[len
- 2] == 'p' && entry
->d_name
[len
944 fprintf(ser_fp
, "%d. %s\r\n", index
, entry
->d_name
);
945 names
[index
] = (char *) malloc(strlen(entry
->d_name
) + 1);
946 if (names
[index
] == NULL
)
948 fprintf(ser_fp
, "ERROR, not enough memory. Resetting...");
951 strcpy(names
[index
], entry
->d_name
);
955 fprintf(ser_fp
, "Type in the index of the file you want to select (0 - %d)", index
958 if (!(key
>= '0' && key
<= '9' || key
- '0' > index
- 1))
960 fprintf(ser_fp
, "No such app\r\n");
963 fprintf(ser_fp
, "\r\n");
965 strcpy(fileName
, "/config/");
966 strcat(fileName
, names
[key
- '0']);
972 err
= cyg_flash_init(NULL
);
975 fprintf(ser_fp
, "cyg_flash_init error %d\r\n ", err
);
978 cyg_flashaddr_t err_address
;
980 #ifdef CYGHWR_IO_FLASH_BLOCK_LOCKING
981 if ((err
= flash_unlock((void *) EXT_FLASH_BASE
, EXT_FLASH_SPAN
,
982 (void **) &err_address
)) != 0)
984 fprintf(ser_fp
, "Flash Error flash_unlock: %d\r\n", err
);
989 // std::ostringstream s;
990 // s << "/dev/flash/0/" << JFFS2_OFFSET << "," << JFFS2_LENGTH;
992 std::string fis
= "/dev/flash/0/";
995 sprintf(number
, "0X%0x", JFFS2_OFFSET
);
998 sprintf(number
, "0X%0x", JFFS2_LENGTH
);
1000 fprintf(ser_fp
, "%s\r\n", fis
.c_str());
1002 err
= mount(fis
.c_str(), "/config", "jffs2");
1006 fprintf(ser_fp
, "JFFS2 mounting error %d\r\n", err
);
1009 fprintf(ser_fp
, "mounted jffs2\r\n");
1016 err
= mount("", "/ram", "ramfs");
1020 fprintf(ser_fp
, "RAMFS mounting error %d\r\n", err
);
1023 fprintf(ser_fp
, "mounted ramfs just fine\r\n");
1024 fprintf(ser_fp
, "testing file write/read/delete\r\n");
1025 FILE* pf
= fopen("/ram/tralala.txt", "w+");
1028 fprintf(ser_fp
, "unable to create/open file\r\n");
1034 void printAvailableRAM()
1036 struct mallinfo info
;
1038 fprintf(ser_fp
, "Available RAM: %d\r\n", info
.fordblks
);
1043 char fileName
[NAME_MAX
];
1045 fprintf(ser_fp
, "Bootloader. Copyright FSF 2006-2010 All rights reserved\r\n");
1046 fprintf(ser_fp
, "Version unknown %s %s\r\n", __DATE__
, __TIME__
);
1048 // cyg_exception_handler_t *old;
1049 // cyg_addrword_t oldData;
1050 // cyg_exception_set_handler(CYGNUM_HAL_VECTOR_UNDEF_INSTRUCTION, exception_handler, 0, &old, &oldData);
1051 // cyg_exception_set_handler(CYGNUM_HAL_VECTOR_ABORT_PREFETCH, exception_handler, 0, &old, &oldData);
1052 // cyg_exception_set_handler( CYGNUM_HAL_VECTOR_ABORT_DATA, exception_handler, 0, &old, &oldData);
1059 printAvailableRAM();
1061 //extern void phi_init_all_network_interfaces(void);
1063 // phi_init_all_network_interfaces();
1068 fprintf(ser_fp
, "Press <space> for advanced help\r\n");
1069 if (hasMacAddress())
1071 fprintf(ser_fp
, "Press <i> to set static IP address\r\n");
1072 fprintf(ser_fp
, "Press <enter> to start Ymodem upload of firmware\r\n");
1076 fprintf(ser_fp
, "Press <m> to set MAC address\r\n");
1079 //use default firmware file name
1080 strcpy(fileName
, FIRMWARE_FILE
);
1082 waitMoreChar
: if (waitChar(2, &key
))
1087 selectFile(fileName
);
1104 fprintf(ser_fp
, "File name: ");
1105 getFileName(fileName
, sizeof(fileName
));
1106 ymodemUpload(fileName
);
1109 fprintf(ser_fp
, "Default firmware file update\r\n");
1110 ymodemUpload(FIRMWARE_FILE
); //fall through
1113 fprintf(ser_fp
, "Single shot bootloader update\r\n");
1114 ymodemUpload(BOOTLOADER_FILE
); //fall through
1117 fprintf(ser_fp
, "FPGA image update\r\n");
1118 ymodemUpload(FPGA_FILE
); //fall through
1121 fprintf(ser_fp
, "Press <F> format flash\r\n");
1123 "Press <E> to start Ymodem upload of firmware to a specified file name\r\n");
1124 fprintf(ser_fp
, "Press <Y> to start single shot update of bootloader\r\n");
1125 fprintf(ser_fp
, "Press <A> to start update of FPGA image\r\n");
1126 fprintf(ser_fp
, "Press <P> set parameter\r\n");
1127 fprintf(ser_fp
, "Press <D> show parameter\r\n");
1131 /* ignore unknown keys... */
1134 if(!hasMacAddress())
1136 if ((key
== 'M') || (key
== 'm'))
1143 upgradeFPGA(FACTORY_FPGA_OFFSET
, APPLICATION_FPGA_OFFSET
);
1144 upgradeBootloader();
1147 if (!hasMacAddress())
1149 /* do not allow running application without mac address */
1159 /********************************************************************************
1160 * Function: CycloneIII_Reconfig
1161 * Purpose: Uses the ALT_REMOTE_UPDATE megafunction to reconfigure a Cyclone III FPGA.
1163 * remote_update_base - base address of the remote update controller
1164 * flash_base - base address of flash device
1165 * reconfig_offset - offset in flash from which to reconfigure
1166 * watchdog_timeout - 29-bit watchdog timeout value
1167 * width_of_flash - data-width of flash device
1168 * Returns: 0 ( but never exits since it reconfigures the FPGA )
1169 ****************************************************************************/
1170 int CycloneIIIReconfig(int remote_update_base
, int flash_base
,
1171 int reconfig_offset
, int watchdog_timeout
, int width_of_flash
)
1173 int offset_shift
, addr
;
1175 // Obtain upper 12 bits of 29-bit watchdog timeout value
1176 watchdog_timeout
= watchdog_timeout
>> 17;
1177 // Only enable the watchdog timer if its timeout value is greater than 0.
1178 if (watchdog_timeout
> 0)
1180 // Set the watchdog timeout value
1181 IOWR( remote_update_base
, 0x2, watchdog_timeout
);
1185 // Disable the watchdog timer
1186 IOWR( remote_update_base
, 0x3, 0 );
1189 tmp
= IORD( remote_update_base
, 0x0 );
1191 tmp
= IORD( remote_update_base
, 0xF );
1193 // Calculate how much to shift the reconfig offset location:
1194 // width_of_flash == 8->offset_shift = 2.
1195 // width_of_flash == 16->offset_shift = 3
1196 offset_shift
= ((width_of_flash
/ 8) + 1);
1197 // Write the offset of the desired reconfiguration image in flash
1198 IOWR( remote_update_base
, 0x4, reconfig_offset
>> offset_shift
);
1200 addr
= IORD( remote_update_base
, 0x4);
1201 // Perform the reconfiguration by setting bit 0 in the
1202 // control/status register
1203 IOWR( remote_update_base
, 0x20, 0x1 );
1209 int tmp
= IORD(REMOTE_UPDATE_BASE
, 0x0);
1217 fprintf(ser_fp
, "Main\r\n");
1223 fprintf(ser_fp
, "Reconfigure and reboot\r\n");
1225 CycloneIIIReconfig(REMOTE_UPDATE_BASE
, EXT_FLASH_BASE
,
1226 APPLICATION_FPGA_OFFSET
, 0, 16);
1228 // launch application!
1229 fprintf(ser_fp
, "Jump to deflate application\r\n");
1231 cyg_interrupt_disable();
1232 ((void(*)(void)) (EXT_FLASH_BASE
+ DEFLATOR_OFFSET
))();