ReFlash.fth (5319B)
1 \ Reflash.fth - handles reflashing the system 2 3 (( 4 Copyright (c) 2004, 2006, 2008, 2010 5 MicroProcessor Engineering 6 133 Hill Lane 7 Southampton SO15 5AF 8 England 9 10 tel: +44 (0)23 8063 1441 11 fax: +44 (0)23 8033 9691 12 net: mpe@mpeforth.com 13 tech-support@mpeforth.com 14 web: www.mpeforth.com 15 16 From North America, our telephone and fax numbers are: 17 011 44 23 8063 1441 18 011 44 23 8033 9691 19 20 21 To do 22 ===== 23 24 Change history 25 ============== 26 20100125 SFP002 Cortex conversion from LPC23xx code. 27 Added FlashSize setting. 28 20060701 SFP001 Added CopyFlash. 29 )) 30 31 32 \ ========== 33 \ *! reflashstm32 34 \ *T STM32 Reflashing 35 \ ========== 36 \ *S Introduction 37 \ *P If you destroy the application in the internal Flash, you 38 \ ** must use the ST ISP loader to reload an Intel Hex file. 39 \ ** A suitable baud rate is 38400 baud. 40 41 \ *P Once the Forth system is running again, you can use the word 42 \ ** *\fo{REFLASH ( -- )} to download a new binary image. 43 \ ** Note that *\fo{REFLASH} only handles binary memory image 44 \ ** files. These should transferred using the XModem 128 45 \ ** (checksum) protocol. If you are using AIDE with the file 46 \ ** server enabled, a file selection dialog will appear 47 \ ** automatically after you have executed *\fo{REFLASH}. 48 49 \ *P The STM32 can only be completely reflashed from an 50 \ ** application by a program running from RAM. A separate 51 \ ** application built by a control file *\i{Reprog/Reprog*.ctl} 52 \ ** is used to do this. A binary image, *\fo{REPROG*.IMG}, is 53 \ ** inserted into the application. When required, the code is 54 \ ** copied into the internal RAM and executed from RAM. 55 56 \ *P Return is by rebooting the system. The contents of the 57 \ ** internal RAM and Flash are destroyed, therefore any data 58 \ ** that must be preserved should be saved externally to the 59 \ ** chip. Alternatively, modify the reprogramming code to 60 \ ** use the last sector to hold data to be 61 \ ** preserved. 62 63 64 \ *************************** 65 \ *S Code in main application 66 \ *************************** 67 \ *P The layout of the RAM code is defined in the control file 68 \ ** for the RAM application. This defines the first cell as 69 \ ** the Forth word to execute. 70 71 create reprog.img \ -- addr 72 \ *G The start address of the reprogramming code 73 data-file REPROGSTM32F0.IMG equ /reprog \ -- len 74 \ *G Generic STM32: The length of the reprogramming code loaded 75 \ ** into the Flash dictionary. 76 77 $2000:0000 equ reprogrun \ -- addr 78 \ *G The run time address of the reprogramming code. 79 \ ** This *\b{must} match the start address of the *\fo{CDATA} 80 \ ** section defined in *\i{ReprogSTM32.ctl}, and that section 81 \ ** *\b{must not} overlap the run-time stacks and user area. 82 83 variable ReflashUART \ -- addr 84 \ *G Holds 0 or a UART base address. If set to 0, the reflash 85 \ ** code will use the default device for the XModem transfer. 86 \ ** Otherwise it will use the value here as the base address 87 \ ** of the UART to use. Since the same binary reprogramming 88 \ ** code is used for many systems, it is foolish to make any 89 \ ** assumptions about the default device. 90 91 useUSART1? [if] 92 console-port 1 = [if] 93 _USART1 ReflashUART ! 94 \ *G Set *\fo{ReflashUART} for USART1 if *\fo{console-port}=1. 95 [then] 96 [then] 97 useUSART2? [if] 98 console-port 2 = [if] 99 _USART2 ReflashUART ! 100 \ *G Set *\fo{ReflashUART} for USART2 if *\fo{console-port}=2. 101 [then] 102 [then] 103 useUSART3? [if] 104 console-port 3 = [if] 105 _USART3 ReflashUART ! 106 \ *G Set *\fo{ReflashUART} for USART3 if *\fo{console-port}=3. 107 [then] 108 [then] 109 110 \ *P The start of the RAM code block holds data about the 111 \ ** running Forth. 112 \ *( 113 \ *B Cell 0 - xt of *\fo{ReprogFlash} to run, 114 \ *B Cell 1 - xt of *\fo{CopyFlash} to run, 115 \ *B Cell 2 - 0 for default UART or base address of another UART, 116 \ *B Cell 3 - clock speed in Hz of the calling Forth, 117 \ *B Cell 4 - Flash size in bytes, 118 \ *B Cell 5 - size of each page in the Flash. 119 \ *) 120 121 [undefined] KeepPages [if] 122 0 equ KeepPages \ -- u 123 \ *G Set this non-zero for the number of pages at the end of Flash 124 \ ** that are reserved for configuration data. Often set to 1 or 2 125 \ ** by systems that use PowerNet. If not already defined, we use 126 \ ** the value here. 127 [then] 128 /FlashPage KeepPages * equ /Keep \ -- len 129 \ *G Number of bytes to keep at the end of Flash for configuration 130 \ ** data. 131 132 : callit \ xt -- 133 \ *G Load the reprogramming code and execute the xt. 134 [defined] dfi [if] dfi [then] \ disable FIQ 135 di \ disable IRQ [and FIQ] 136 ReflashUART @ \ get device selection BEFORE copy 137 reprog.img reprogrun /reprog cmove \ copy code to RAM 138 reprogrun tuck 2 cells + ! \ set device base address 139 system-speed over 3 cells + ! \ set device clock speed 140 /Flash /Keep - over 4 cells + ! \ set Flash size 141 /FlashPage swap 5 cells + ! \ set Flash page size 142 execute 143 [defined] reboot [if] reboot [else] ExcVecs 4 + @ execute [then] 144 ; 145 146 : reflash \ -- ; no exit 147 \ *G Copies the reprogramming code to the run-time address 148 \ ** and executes it. 149 reprog.img @ callit 150 ; 151 152 : CopyFlash \ src dest len -- 153 \ *G Copy the flash. The parameters are as for *\fo{CMOVE}. 154 \ ** The process is repeated until there are no errors, and 155 \ ** the system is then rebooted. 156 reprog.img cell + @ callit 157 ; 158 159 160 \ ====== 161 \ *> ### 162 \ ====== 163