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