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