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