Rev Author Line No. Line
2804 kakl 1 /**** BootLoader for PIC16F887
2  
2809 kakl 3 After Reset PIC run this script with number of ttyUSBn:
4 echo uf > /dev/ttyUSB$1
5 sleep 5
6 ascii-xfr -s -v -l 100 ./bltest.hex > /dev/ttyUSB$1
2804 kakl 7 */
8  
9 #define ID "$Id: bloader.c 2809 2013-03-10 06:31:00Z 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);
154 putchar('@'); //Start Erase
155  
156 //Erase program. Do not erase "jump to main" and BootLoader.
157 {
158 int8 i;
159 for(i=0;i<32;i++)buffer[i]=0xFF;
160 }
161 for(addr=FLASH_BLOCK_SIZE;addr<LOADER_RESERVED+FLASH_BLOCK_SIZE;addr+=FLASH_BLOCK_SIZE)
162 {
163 write_program_memory(addr, &buffer[0], 32);
164 putchar('.');
165 restart_wdt();
166 }
167  
168 putchar('!'); //Erase completed
169  
170 //---WDT
171 while(!kbhit()) restart_wdt(); //Wait for HEX
172 putc('\r'); putc('\n');
173  
174 while(TRUE)
175 {
176 //---WDT
177 while (getc()!=':') restart_wdt(); // Only process data blocks that starts with ':'
178 putchar(':');
179  
180 buffidx = 0; // Read into the buffer until CR is received or buffer is full
181 do
182 {
183 buffer[buffidx] = getc();
184 putc(buffer[buffidx]);
185 } while ( (buffer[buffidx++] != '\r') && (buffidx < BUFFER_LEN_LOD) );
186 assert(buffidx == BUFFER_LEN_LOD, ERR_BUFFER_OVERRUN); // Error 1 - Buffer Overrun
187 buffidx--;
188  
189 //---WDT
190 restart_wdt();
191  
192 checksum = 0; // Sum the bytes to find the check sum value
193 for (i=0; i<(buffidx); i+=2)
194 {
195 checksum += atoi_b16 (&buffer[i]);
196 }
197 assert(checksum != 0, ERR_CHECKSUM); // Error 2 - Bad CheckSum
198  
199 // Get the lower 16 bits of address
200 l_addr = make16(atoi_b16(&buffer[2]),atoi_b16(&buffer[4]));
201  
202 line_type = atoi_b16 (&buffer[6]);
203  
204 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  
207 addr = make32(h_addr,l_addr);
208  
209 addr /= 2; // PIC16 uses word addresses
210  
211 // If the line type is 1 then END
212 if (line_type == 1)
213 {
214 putchar('#');
215 reset_cpu();
216 }
217  
218 assert (line_type != 0, ERR_UNSUPORTED_LINETYPE); // Error 4 - Unsuported Line type
219  
220 {
221 // Read old program memory content
222 for (i=0,next_addr=addr;i<8;i++)
223 data.i16[i]=read_program_eeprom(next_addr++);
224 // Loops through all of the data and stores it in data
225 // The last 2 bytes are the check sum, hence buffidx-2
226 for (i=8,dataidx=0; i < (buffidx-2); i += 2)
227 data.i8[dataidx++]=atoi_b16(&buffer[i]);
228  
229 if (addr == 0)
230 {
231 // Write 8 words to the Loader location (jump to the main())
232 addr=LOADER_RESERVED;
233 write_program_memory(addr, &data.i8[0], 16);
234 putchar('%');
235 }
236 else
237 if ( (addr > 7) && (addr <= (LOADER_RESERVED-16)) ) // Do not overwrite BootLoader
238 {
239 // Write program
240 write_program_memory(addr, &data.i8[0], 16);
241 putchar('$');
242 }
243 else putchar('.'); // Possibly there was prevented write to the location of BootLoader
244 putc('\r'); putc('\n');
245  
246 //---WDT
247 restart_wdt();
248 }
249 }
250 }
251  
252  
253 void main()
254 {
255 int8 timeout;
256  
257 disable_interrupts(GLOBAL);
258 setup_wdt(WDT_2304MS); // Setup Watch Dog
259 setup_adc_ports(NO_ANALOGS);
260 setup_adc(ADC_OFF);
261 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
262 setup_timer_1(T1_DISABLED);
263 setup_timer_2(T2_DISABLED,0,1);
264 setup_comparator(NC_NC_NC_NC);
265 setup_vref(FALSE);
266 setup_oscillator(OSC_8MHZ|OSC_INTRC);
267  
268 for(timeout=0; timeout<255; timeout++) //cca 50s
269 {
270 if (kbhit())
271 if (getc()=='u') // Send "uf" for Update Firmware
272 {
273 putchar('*');
274 if (getc()=='f')
275 {
276 restart_wdt();
277 boot_loader(); // Update Firmware starter
278 }
279 }
280 else break;
281 putchar('u'); putchar('f'); putchar('?');
282 pause();
283 restart_wdt();
284 };
285  
286 restart_wdt();
287 goto_address(LOADER_RESERVED); // Jump to the location where is the jump to the main
288 }
289 #ORG default // End of BootLoader