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