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