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