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