Rev 2848 Rev 2896
1 /**** BootLoader for PIC16F887 1 /**** BootLoader for PIC16F887
2   2  
3 Ussage: 3 Ussage:
4 ascii-xfr -s -v -l 110 ./bltest.hex > /dev/ttyUSB0 4 ascii-xfr -s -v -l 110 ./bltest.hex > /dev/ttyUSB0
5 5
6 ascii-xfr is part of 'minicom' package 6 ascii-xfr is part of 'minicom' package
7   7  
8 Add "uf\n\r" to the first line of .HEX. or use this script: 8 Add "uf\n\r" to the first line of .HEX. or use this script:
9 echo uf > /dev/ttyUSB$1 9 echo uf > /dev/ttyUSB$1
10 ascii-xfr -s -v -l 100 ./bltest.hex > /dev/ttyUSB$1 10 ascii-xfr -s -v -l 100 ./bltest.hex > /dev/ttyUSB$1
11 11
12 Or add "uf\n\r" and add some dummy characters at end or begin of each line (for 100 ms delay) and use: 12 Or add "uf\n\r" and add some dummy characters at the end or begin of each line (for 100 ms delay) and use:
13 cp ./bltest.hex > /dev/ttyUSB0 13 cp ./bltest.hex > /dev/ttyUSB0
14 14
15 For adding characters you can use this: 15 For adding characters you can use this:
16 sed -i 's/^/pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp/' ./bltest.hex 16 sed -i 's/^/pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp/' ./bltest.hex
17 sed -i 1i"uf" ./bltest.hex 17 sed -i 1i"uf" ./bltest.hex
18 18
-   19 Windows script:
-   20 echo uf > com1
-   21 ping 1.1.1.1 -n 1 -w 100 > nul
-   22 FOR /F "tokens=1*" %%i in (bltest.hex) do @echo %%i > com1 & ping 1.1.1.1 -n 1 -w 100 > nul
19 */ 23 */
20   24  
21 #define ID "$Id: bloader.c 2848 2013-03-21 08:16:49Z kakl $" 25 #define ID "$Id: bloader.c 2896 2013-04-09 20:40:51Z kakl $"
22   26  
23 #CASE // Case sensitive compiler 27 #CASE // Case sensitive compiler
24   28  
25 #define ERR_BUFFER_OVERRUN 1 // Error 1 - Buffer Overrun 29 #define ERR_BUFFER_OVERRUN 1 // Error 1 - Buffer Overrun
26 #define ERR_CHECKSUM 2 // Error 2 - Bad CheckSum 30 #define ERR_CHECKSUM 2 // Error 2 - Bad CheckSum
27 #define ERR_TOO_MANY_BYTES 3 // Error 3 - Too many bytes in one line 31 #define ERR_TOO_MANY_BYTES 3 // Error 3 - Too many bytes in one line
28 #define ERR_UNSUPORTED_LINETYPE 4 // Error 4 - Unsuported Line type 32 #define ERR_UNSUPORTED_LINETYPE 4 // Error 4 - Unsuported Line type
29   33  
30 #define BUFFER_LEN_LOD 46 // Length of Working buffer for HEX 34 #define BUFFER_LEN_LOD 46 // Length of Working buffer for HEX
31   35  
32 #include "..\common\bloader_defs.h" 36 #include "..\common\bloader_defs.h"
33 #include "bloader.h" 37 #include "bloader.h"
34 #include <string.h> 38 #include <string.h>
35   39  
36 #INT_RDA 40 #INT_RDA
37 void rs232_handler() // Test of interrupt 41 void rs232_handler() // Test of interrupt
38 { 42 {
39 putchar(getc()); // Just echo for test 43 putchar(getc()); // Just echo for test
40 } 44 }
41   45  
42 void welcome(void) // Welcome message 46 void welcome(void) // Welcome message
43 { 47 {
44 char REV[50]=ID; // Buffer for concatenate of a version string 48 char REV[50]=ID; // Buffer for concatenate of a version string
45   49  
46 if (REV[strlen(REV)-1]=='$') REV[strlen(REV)-1]=0; 50 if (REV[strlen(REV)-1]=='$') REV[strlen(REV)-1]=0;
47 printf("\r\n\r\n# BLoader 887 (C) 2013 KAKL\r\n"); // Welcome message 51 printf("\r\n\r\n# BLoader 887 (C) 2013 KAKL\r\n"); // Welcome message
48 printf("#%s\r\n",&REV[4]); 52 printf("#%s\r\n",&REV[4]);
49 } 53 }
50   54  
51   55  
52 /*-------------------------------- MAIN --------------------------------------*/ 56 /*-------------------------------- MAIN --------------------------------------*/
53 #SEPARATE 57 #SEPARATE
54 void real_main() // Main of loaded program 58 void real_main() // Main of loaded program
55 { 59 {
56 int8 i=0; 60 int8 i=0;
57   61  
58 i=rs232_errors; // Just for compiler pleasure (supress Warning) 62 i=rs232_errors; // Just for compiler pleasure (supress Warning)
59 63
60 welcome(); 64 welcome();
61   65  
62 printf("# Reserved: %Lu\r\n", RESERVED_BLOCKS*FLASH_BLOCK_SIZE); 66 printf("# Reserved: %Lu\r\n", RESERVED_BLOCKS*FLASH_BLOCK_SIZE);
63 printf("# FLASH_ERASE_SIZE: %Lu\r\n",getenv("FLASH_ERASE_SIZE")); 67 printf("# FLASH_ERASE_SIZE: %Lu\r\n",getenv("FLASH_ERASE_SIZE"));
64 printf("# FLASH_WRITE_SIZE: %Lu\r\n",getenv("FLASH_WRITE_SIZE")); 68 printf("# FLASH_WRITE_SIZE: %Lu\r\n",getenv("FLASH_WRITE_SIZE"));
65 69
66 printf("# Boot Loader Test >>>\r\n# "); 70 printf("# Boot Loader Test >>>\r\n# ");
67 enable_interrupts(INT_RDA); 71 enable_interrupts(INT_RDA);
68 enable_interrupts(GLOBAL); 72 enable_interrupts(GLOBAL);
69 while(TRUE) 73 while(TRUE)
70 { 74 {
71 printf("%u|",i++); // Do something 75 printf("%u|",i++); // Do something
72 delay_ms(100); 76 delay_ms(100);
73 } 77 }
74 } 78 }
75   79  
76   80  
77 /*------------------- BOOT LOADER --------------------------------------------*/ 81 /*------------------- BOOT LOADER --------------------------------------------*/
78   82  
79 #BUILD(INTERRUPT=FLASH_BLOCK_SIZE) // Redirect Interrupt routine above first flash block 83 #BUILD(INTERRUPT=FLASH_BLOCK_SIZE) // Redirect Interrupt routine above first flash block
80 #ROM 0x0004={0,0,0,0,0,0,0,0,0,0,0,0} // 12x NOP from interrupt vector to interrupt routine 84 #ROM 0x0004={0,0,0,0,0,0,0,0,0,0,0,0} // 12x NOP from interrupt vector to interrupt routine
81   85  
82   86  
83 #ORG LOADER_RESERVED,LOADER_RESERVED+FLASH_BLOCK_SIZE-1 auto=0 87 #ORG LOADER_RESERVED,LOADER_RESERVED+FLASH_BLOCK_SIZE-1 auto=0
84 #SEPARATE 88 #SEPARATE
85 void dummy_main() // Main on the fix position. It will be overwriten by downloaded program reset vector. 89 void dummy_main() // Main on the fix position. It will be overwriten by downloaded program reset vector.
86 { 90 {
87 real_main(); 91 real_main();
88 } 92 }
89   93  
90 #ORG LOADER_RESERVED+FLASH_BLOCK_SIZE,getenv("PROGRAM_MEMORY")-1 auto=0 default //Start of BootLoader 94 #ORG LOADER_RESERVED+FLASH_BLOCK_SIZE,getenv("PROGRAM_MEMORY")-1 auto=0 default //Start of BootLoader
91   95  
92 #use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,ERRORS) //RS232 control routine for BootLoader 96 #use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,ERRORS) //RS232 control routine for BootLoader
93   97  
94 #SEPARATE 98 #SEPARATE
95 unsigned int8 atoi_b16(char *s) // Convert two hex characters to an int8 99 unsigned int8 atoi_b16(char *s) // Convert two hex characters to an int8
96 { 100 {
97 unsigned int8 result = 0; 101 unsigned int8 result = 0;
98 int i; 102 int i;
99   103  
100 for (i=0; i<2; i++,s++) { 104 for (i=0; i<2; i++,s++) {
101 if (*s >= 'A') 105 if (*s >= 'A')
102 result = 16*result + (*s) - 'A' + 10; 106 result = 16*result + (*s) - 'A' + 10;
103 else 107 else
104 result = 16*result + (*s) - '0'; 108 result = 16*result + (*s) - '0';
105 } 109 }
106   110  
107 return(result); 111 return(result);
108 } 112 }
109   113  
110 void assert(int1 Condition, int8 ErrorCode) // Send error number to the serial line 114 void assert(int1 Condition, int8 ErrorCode) // Send error number to the serial line
111 { 115 {
112 if(Condition) 116 if(Condition)
113 { 117 {
114 putchar('E'); 118 putchar('e');
115 putchar(ErrorCode+'0'); 119 putchar(ErrorCode+'0');
116 reset_cpu(); 120 reset_cpu();
117 } 121 }
118 } 122 }
119   123  
120 void pause() 124 void pause()
121 { 125 {
122 int16 timeout; 126 int16 timeout;
123   127  
124 for(timeout=0; timeout<65535; timeout++); // Delay cca 300ms 128 for(timeout=0; timeout<65535; timeout++); // Delay cca 300ms
125 } 129 }
126   130  
127 #SEPARATE 131 #SEPARATE
128 void boot_loader() // Loads a new program 132 void boot_loader() // Loads a new program
129 { 133 {
130 /* 134 /*
131 :100240001F2999000A300C1E232999006430A4004C 135 :100240001F2999000A300C1E232999006430A4004C
132 10 = 16 bytes 136 10 = 16 bytes
133 0249 = address 0x0120 (0x240/2 because of words) 137 0249 = address 0x0120 (0x240/2 because of words)
134 00 = data 138 00 = data
135 ...data... 139 ...data...
136 4C = checksum 140 4C = checksum
137   141  
138 :00000001FF 142 :00000001FF
139 00 = 0 bytes 143 00 = 0 bytes
140 0000 = address 0 144 0000 = address 0
141 01 = END 145 01 = END
142 FF = checksum 146 FF = checksum
143   147  
144 http://cs.wikipedia.org/wiki/Intel_HEX 148 http://cs.wikipedia.org/wiki/Intel_HEX
145 */ 149 */
146 int buffidx; 150 int buffidx;
147 char buffer[BUFFER_LEN_LOD]; // Buffer for HEX line 151 char buffer[BUFFER_LEN_LOD]; // Buffer for HEX line
148   152  
149 int8 checksum, num_of_bytes, line_type; // Extracted values from HEX line 153 int8 checksum, num_of_bytes, line_type; // Extracted values from HEX line
150 int16 l_addr,h_addr=0; 154 int16 l_addr,h_addr=0;
151 int16 addr; // Address of word in PIC 155 int16 addr; // Address of word in PIC
152 int32 next_addr; // Helper variable for for 156 int32 next_addr; // Helper variable for for
153   157  
154 int8 dataidx, i; // Buffer for program bytes and pointers 158 int8 dataidx, i; // Buffer for program bytes and pointers
155 union program_data { 159 union program_data {
156 int8 i8[16]; 160 int8 i8[16];
157 int16 i16[8]; 161 int16 i16[8];
158 } data; 162 } data;
159   163  
160 disable_interrupts(GLOBAL); 164 disable_interrupts(GLOBAL);
161 /* -  
162 putchar('@'); //Start Erase -  
163   165  
-   166 putchar('@'); //Start Erase
-   167 /*
164 //Erase program memory is not necessary. 168 //Erase program memory is not necessary.
165 { 169 {
166 int8 i; 170 int8 i;
167 for(i=0;i<32;i++)buffer[i]=0xFF; 171 for(i=0;i<32;i++)buffer[i]=0xFF;
168 } 172 }
169 for(addr=FLASH_BLOCK_SIZE;addr<LOADER_RESERVED+FLASH_BLOCK_SIZE;addr+=FLASH_BLOCK_SIZE) 173 for(addr=FLASH_BLOCK_SIZE;addr<LOADER_RESERVED+FLASH_BLOCK_SIZE;addr+=FLASH_BLOCK_SIZE)
170 { 174 {
171 write_program_memory(addr, &buffer[0], 32); 175 write_program_memory(addr, &buffer[0], 32);
172 putchar('.'); 176 putchar('.');
173 restart_wdt(); 177 restart_wdt();
174 } 178 }
175 */ 179 */
176 putchar('!'); //Erase completed 180 putchar('!'); //Erase completed
177   181  
178 //---WDT 182 //---WDT
179 while(!kbhit()) restart_wdt(); //Wait for HEX 183 while(!kbhit()) restart_wdt(); //Wait for HEX
180 putc('\r'); putc('\n'); 184 putc('\r'); putc('\n');
181 185
182 while(TRUE) 186 while(TRUE)
183 { 187 {
184 //---WDT 188 //---WDT
185 while (getc()!=':') restart_wdt(); // Only process data blocks that starts with ':' 189 while (getc()!=':') restart_wdt(); // Only process data blocks that starts with ':'
186 putchar(':'); 190 putchar(':');
187   191  
188 buffidx = 0; // Read into the buffer until CR is received or buffer is full 192 buffidx = 0; // Read into the buffer until CR is received or buffer is full
189 do 193 do
190 { 194 {
191 buffer[buffidx] = getc(); 195 buffer[buffidx] = getc();
192 putc(buffer[buffidx]); 196 putc(buffer[buffidx]);
193 } while ( (buffer[buffidx++] != '\r') && (buffidx < BUFFER_LEN_LOD) ); 197 } while ( (buffer[buffidx++] != '\r') && (buffidx < BUFFER_LEN_LOD) );
194 assert(buffidx == BUFFER_LEN_LOD, ERR_BUFFER_OVERRUN); // Error 1 - Buffer Overrun 198 assert(buffidx == BUFFER_LEN_LOD, ERR_BUFFER_OVERRUN); // Error 1 - Buffer Overrun
195 buffidx--; -  
196 199
197 //---WDT 200 //---WDT
198 restart_wdt(); 201 restart_wdt();
199   202  
200 checksum = 0; // Sum the bytes to find the check sum value 203 checksum = 0; // Sum the bytes to find the check sum value
201 for (i=0; i<(buffidx); i+=2) 204 for (i=0; i<(buffidx-3); i+=2)
202 { 205 {
203 checksum += atoi_b16 (&buffer[i]); 206 checksum += atoi_b16 (&buffer[i]);
-   207 //!!! printf(".%x",checksum);
204 } 208 }
205 assert(checksum != 0, ERR_CHECKSUM); // Error 2 - Bad CheckSum 209 assert(checksum != 0, ERR_CHECKSUM); // Error 2 - Bad CheckSum
206   210  
207 // Get the lower 16 bits of address 211 // Get the lower 16 bits of address
208 l_addr = make16(atoi_b16(&buffer[2]),atoi_b16(&buffer[4])); 212 l_addr = make16(atoi_b16(&buffer[2]),atoi_b16(&buffer[4]));
209   213  
210 line_type = atoi_b16 (&buffer[6]); 214 line_type = atoi_b16 (&buffer[6]);
211 215
212 num_of_bytes = atoi_b16 (&buffer[0]); 216 num_of_bytes = atoi_b16 (&buffer[0]);
213 assert (num_of_bytes > 16, ERR_TOO_MANY_BYTES); // Error 3 - Too many bytes in one line 217 assert (num_of_bytes > 16, ERR_TOO_MANY_BYTES); // Error 3 - Too many bytes in one line
214   218  
215 addr = make32(h_addr,l_addr); 219 addr = make32(h_addr,l_addr);
216   220  
217 addr /= 2; // PIC16 uses word addresses 221 addr /= 2; // PIC16 uses word addresses
218   222  
219 // If the line type is 1 then END 223 // If the line type is 1 then END
220 if (line_type == 1) 224 if (line_type == 1)
221 { 225 {
222 putchar('#'); 226 putchar('#');
223 reset_cpu(); 227 reset_cpu();
224 } 228 }
225   229  
226 assert (line_type != 0, ERR_UNSUPORTED_LINETYPE); // Error 4 - Unsuported Line type 230 assert (line_type != 0, ERR_UNSUPORTED_LINETYPE); // Error 4 - Unsuported Line type
227   231  
228 { 232 {
229 // Read old program memory content 233 // Read old program memory content
230 for (i=0,next_addr=addr;i<8;i++) 234 for (i=0,next_addr=addr;i<8;i++)
231 data.i16[i]=read_program_eeprom(next_addr++); 235 data.i16[i]=read_program_eeprom(next_addr++);
232 // Loops through all of the data and stores it in data 236 // Loops through all of the data and stores it in data
233 // The last 2 bytes are the check sum, hence buffidx-2 237 // The last 2 bytes are the check sum, hence buffidx-4
234 for (i=8,dataidx=0; i < (buffidx-2); i += 2) 238 for (i=8,dataidx=0; i < (buffidx-5); i += 2)
235 data.i8[dataidx++]=atoi_b16(&buffer[i]); 239 data.i8[dataidx++]=atoi_b16(&buffer[i]);
236   240  
237 if (addr == 0) 241 if (addr == 0)
238 { 242 {
239 // Write 8 words to the Loader location (jump to the main()) 243 // Write 8 words to the Loader location (jump to the main())
240 addr=LOADER_RESERVED; 244 addr=LOADER_RESERVED;
241 write_program_memory(addr, &data.i8[0], 16); // It works only with 16 !!! 245 write_program_memory(addr, &data.i8[0], 16); // It works only with 16 !!!
242 putchar('%'); 246 putchar('%');
243 } 247 }
244 else 248 else
245 if ( (addr > 7) && (addr <= (LOADER_RESERVED-16)) ) // Do not overwrite BootLoader 249 if ( (addr >= FLASH_BLOCK_SIZE) && (addr <= (LOADER_RESERVED-16)) ) // Do not overwrite BootLoader
246 { 250 {
247 // Write program 251 // Write program
248 write_program_memory(addr, &data.i8[0], 16); // It works only with 16 !!! 252 write_program_memory(addr, &data.i8[0], 16); // It works only with 16 !!!
249 putchar('$'); 253 putchar('$');
250 } 254 }
251 else putchar('.'); // Possibly there was prevented write to the location of BootLoader 255 else putchar('.'); // Possibly there was prevented write to the location of BootLoader
252 putc('\r'); putc('\n'); 256 putc('\r'); putc('\n');
253   257  
254 //---WDT 258 //---WDT
255 restart_wdt(); 259 restart_wdt();
256 } 260 }
257 } 261 }
258 } 262 }
259   263  
260   264  
261 void main() 265 void main()
262 { 266 {
263 int8 timeout; 267 int8 timeout;
264   268  
265 disable_interrupts(GLOBAL); 269 disable_interrupts(GLOBAL);
266 setup_wdt(WDT_2304MS); // Setup Watch Dog 270 setup_wdt(WDT_2304MS); // Setup Watch Dog
267 setup_adc_ports(NO_ANALOGS); 271 setup_adc_ports(NO_ANALOGS);
268 setup_adc(ADC_OFF); 272 setup_adc(ADC_OFF);
269 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); 273 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
270 setup_timer_1(T1_DISABLED); 274 setup_timer_1(T1_DISABLED);
271 setup_timer_2(T2_DISABLED,0,1); 275 setup_timer_2(T2_DISABLED,0,1);
272 setup_comparator(NC_NC_NC_NC); 276 setup_comparator(NC_NC_NC_NC);
273 setup_vref(FALSE); 277 setup_vref(FALSE);
274 setup_oscillator(OSC_8MHZ|OSC_INTRC); 278 setup_oscillator(OSC_8MHZ|OSC_INTRC);
275   279  
276 for(timeout=0; timeout<255; timeout++) //cca 50s 280 for(timeout=0; timeout<255; timeout++) //cca 50s
277 { 281 {
278 if (kbhit()) 282 if (kbhit())
279 if (getc()=='u') // Send "uf" for Update Firmware 283 if (getc()=='u') // Send "uf" for Update Firmware
280 { 284 {
281 putchar('*'); 285 putchar('*');
282 if (getc()=='f') 286 if (getc()=='f')
283 { 287 {
284 restart_wdt(); 288 restart_wdt();
285 boot_loader(); // Update Firmware starter 289 boot_loader(); // Update Firmware starter
286 } 290 }
287 } 291 }
288 else break; 292 else break;
289 putchar('u'); putchar('f'); putchar('?'); 293 putchar('u'); putchar('f'); putchar('?');
290 pause(); 294 pause();
291 restart_wdt(); 295 restart_wdt();
292 }; 296 };
293   297  
294 restart_wdt(); 298 restart_wdt();
295 goto_address(LOADER_RESERVED); // Jump to the location where is the jump to the main 299 goto_address(LOADER_RESERVED); // Jump to the location where is the jump to the main
296 } 300 }
297 #ORG default // End of BootLoader 301 #ORG default // End of BootLoader