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