Rev 1293 Rev 1298
Line 1... Line 1...
1 /**** IR Mrakomer 4 ****/ 1 /**** IR Mrakomer 4 ****/
2 #define VERSION "4.0" 2 #define VERSION "4.0"
3 #define ID "$Id: irmrak4.c 1293 2009-01-11 22:53:46Z kakl $" 3 #define ID "$Id: irmrak4.c 1298 2009-01-16 23:40:59Z kakl $"
4   4  
5 #include "irmrak4.h" 5 #include "irmrak4.h"
6   6  
7 #bit CREN = 0x18.4 // USART registers 7 #bit CREN = 0x18.4 // USART registers
8 #bit SPEN = 0x18.7 8 #bit SPEN = 0x18.7
9 #bit OERR = 0x18.1 9 #bit OERR = 0x18.1
10 #bit FERR = 0x18.2 10 #bit FERR = 0x18.2
11   11  
12 #include <string.h> 12 #include <string.h>
13 #include "bloader.c" // Boot Loader driver 13 //!!! #include "bloader.c" // Boot Loader driver
14   14  
15 #CASE // Case sensitive compiler 15 #CASE // Case sensitive compiler
16   16  
17 #define MAXHEAT 20 // Number of cycles for heating 17 #define MAXHEAT 20 // Number of cycles for heating
18 #define MAXOPEN 20 // Number of cycles for dome open 18 #define MAXOPEN 20 // Number of cycles for dome open
Line 89... Line 89...
89 if (crc != PEC_calculation(arr)) temp=0; // Calculate and check CRC 89 if (crc != PEC_calculation(arr)) temp=0; // Calculate and check CRC
90   90  
91 return temp; 91 return temp;
92 } 92 }
93   93  
-   94  
94 /*-----------------------------------------------------------------------*/ 95 /*-------------------------------- MAIN --------------------------------------*/
95 void main() 96 void real_main()
96 { 97 {
97 unsigned int16 seq, temp, tempa; 98 unsigned int16 seq, temp, tempa;
98 signed int16 ta, to; 99 signed int16 ta, to;
99 int8 safety_counter; 100 int8 safety_counter;
100 int1 repeat; 101 int1 repeat;
101   102  
102 output_high(DOME); // Close Dome 103 output_high(DOME); // Close Dome
103 output_low(HEATING); // Heating off 104 output_low(HEATING); // Heating off
104 setup_wdt(WDT_2304MS); // Setup Watch Dog -  
105 setup_adc_ports(NO_ANALOGS); -  
106 setup_adc(ADC_OFF); -  
107 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); -  
108 setup_timer_1(T1_DISABLED); -  
109 setup_timer_2(T2_DISABLED,0,1); -  
110 setup_comparator(NC_NC_NC_NC); -  
111 setup_vref(FALSE); -  
112 // setup_oscillator(OSC_4MHZ|OSC_INTRC,+2); // Pokud je nutna kalibrace RCosc -  
113 setup_oscillator(OSC_8MHZ|OSC_INTRC); -  
114   105  
115 delay_ms(1000); 106 delay_ms(1000);
116 restart_wdt(); 107 restart_wdt();
117   108  
118 seq=0; // Variables initiation 109 seq=0; // Variables initiation
Line 192... Line 183...
192 case 's': 183 case 's':
193 repeat=FALSE; // Single measure mode 184 repeat=FALSE; // Single measure mode
194 break; 185 break;
195   186  
196 case 'u': 187 case 'u':
197 load_program(); // Update firmware 188 // load_program(); // Update firmware
198 } 189 }
199 } 190 }
200 // while(kbhit()) getc(); // Flush USART buffer 191 // while(kbhit()) getc(); // Flush USART buffer
201 CREN=0; CREN=1; // Reinitialise USART 192 CREN=0; CREN=1; // Reinitialise USART
202   193  
Line 236... Line 227...
236 delay(MEASURE_DELAY); // Delay to a next measurement 227 delay(MEASURE_DELAY); // Delay to a next measurement
237 //---WDT 228 //---WDT
238 restart_wdt(); 229 restart_wdt();
239 } 230 }
240 } 231 }
-   232  
-   233  
-   234 /*------------------- BOOT LOADER --------------------------------------------*/
-   235 #define LOADER_RESERVED getenv("PROGRAM_MEMORY")-getenv("FLASH_ERASE_SIZE")-800
-   236 #define BUFFER_LEN_LOD 46
-   237  
-   238 #ORG LOADER_RESERVED,getenv("PROGRAM_MEMORY")-201 auto=0 default
-   239  
-   240 unsigned int atoi_b16(char *s) { // Convert two hex characters to a int8
-   241 unsigned int result = 0;
-   242 int i;
-   243  
-   244 for (i=0; i<2; i++,s++) {
-   245 if (*s >= 'A')
-   246 result = 16*result + (*s) - 'A' + 10;
-   247 else
-   248 result = 16*result + (*s) - '0';
-   249 }
-   250  
-   251 return(result);
-   252 }
-   253  
-   254 void assert(int1 Condition, int8 ErrorCode)
-   255 {
-   256 if(Condition)
-   257 {
-   258 putchar('E');
-   259 putchar(ErrorCode+'1');
-   260 reset_cpu();
-   261 }
-   262 }
-   263  
-   264 void pause()
-   265 {
-   266 int16 timeout;
-   267  
-   268 for(timeout=0; timeout<65535; timeout++); // Delay cca 300ms
-   269 }
-   270  
-   271 boot_loader()
-   272 {
-   273 int buffidx;
-   274 char buffer[BUFFER_LEN_LOD];
-   275  
-   276 int8 checksum, line_type;
-   277 int16 l_addr,h_addr=0;
-   278 int32 addr;
-   279 #if getenv("FLASH_ERASE_SIZE")>2
-   280 int32 next_addr;
-   281 #endif
-   282  
-   283 //!!! #error ble getenv("FLASH_ERASE_SIZE") getenv("FLASH_WRITE_SIZE")
-   284  
-   285 int8 dataidx, i, count;
-   286 union program_data {
-   287 int8 i8[16];
-   288 int16 i16[8];
-   289 } data;
-   290  
-   291 putchar('@');
-   292  
-   293 //!!!nesmaze obsluhu preruseni
-   294 for(i=getenv("FLASH_ERASE_SIZE")+1;i<LOADER_RESERVED;i+=getenv("FLASH_ERASE_SIZE"))
-   295 erase_program_eeprom(i);
-   296  
-   297 putchar('@');
-   298  
-   299 while(TRUE)
-   300 {
-   301 //---WDT
-   302 //!!! musi fungovat watchdog
-   303 while (getc()!=':') restart_wdt(); // Only process data blocks that starts with ':'
-   304  
-   305 buffidx = 0; // Read into the buffer until 'x' is received or buffer is full
-   306 do
-   307 {
-   308 buffer[buffidx] = getc();
-   309 } while ( (buffer[buffidx++] != 'x') && (buffidx < BUFFER_LEN_LOD) );
-   310 assert(buffidx == BUFFER_LEN_LOD,1); // Overrun buffer?
-   311  
-   312 //---WDT
-   313 restart_wdt();
-   314  
-   315 checksum = 0; // Sum the bytes to find the check sum value
-   316 for (i=0; i<(buffidx-3); i+=2)
-   317 checksum += atoi_b16 (&buffer[i]);
-   318 checksum = 0xFF - checksum + 1;
-   319 assert(checksum != atoi_b16 (&buffer[buffidx-3]),2); // Bad CheckSum?
-   320  
-   321 count = atoi_b16 (&buffer[0]); // Get the number of bytes from the buffer
-   322  
-   323 // Get the lower 16 bits of address
-   324 l_addr = make16(atoi_b16(&buffer[2]),atoi_b16(&buffer[4]));
-   325  
-   326 line_type = atoi_b16 (&buffer[6]);
-   327  
-   328 addr = make32(h_addr,l_addr);
-   329  
-   330 addr /= 2; // PIC16 uses word addresses
-   331  
-   332 // If the line type is 1, then data is done being sent
-   333 if (line_type == 1)
-   334 {
-   335 putchar('#');
-   336 reset_cpu();
-   337 }
-   338  
-   339 assert (line_type == 4,4);
-   340  
-   341  
-   342 //!!! pozor, nevypalilo by to obsluhu preruseni
-   343 if (addr > 3 || addr < LOADER_RESERVED)
-   344 {
-   345  
-   346 if (line_type == 0)
-   347 {
-   348 for (i=0,next_addr=addr;i<8;i++)
-   349 data.i16[i]=read_program_eeprom(next_addr++);
-   350 // Loops through all of the data and stores it in data
-   351 // The last 2 bytes are the check sum, hence buffidx-3
-   352 for (i=8,dataidx=0; i < buffidx-3; i += 2)
-   353 data.i8[dataidx++]=atoi_b16(&buffer[i]);
-   354  
-   355 write_program_memory(addr, data.i8, count);
-   356 }
-   357 putchar('*');
-   358 }
-   359 }
-   360 }
-   361  
-   362 #ORG default
-   363  
-   364 #ORG getenv("PROGRAM_MEMORY")-200,getenv("PROGRAM_MEMORY")-1
-   365 void main()
-   366 {
-   367 int8 timeout;
-   368  
-   369 disable_interrupts(GLOBAL);
-   370 setup_wdt(WDT_2304MS); // Setup Watch Dog
-   371 setup_adc_ports(NO_ANALOGS);
-   372 setup_adc(ADC_OFF);
-   373 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
-   374 setup_timer_1(T1_DISABLED);
-   375 setup_timer_2(T2_DISABLED,0,1);
-   376 setup_comparator(NC_NC_NC_NC);
-   377 setup_vref(FALSE);
-   378 setup_oscillator(OSC_8MHZ|OSC_INTRC);
-   379  
-   380 /*
-   381 for(timeout=0; timeout<(3*20); timeout++) //cca 20s
-   382 if (kbhit())
-   383 {
-   384 if (getc()=='u') if (getc()=='f') boot_loader(); // Update Firmware starter
-   385 pause();
-   386 CREN=0; CREN=1;
-   387 restart_wdt();
-   388 };
-   389 */
-   390 real_main();
-   391 }
-   392  
-   393 #include "dbloader.c"