Rev 1293 Rev 1298
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
19 #define MEASURE_DELAY 6000 // Delay to a next measurement 19 #define MEASURE_DELAY 6000 // Delay to a next measurement
20 #define RESPONSE_DELAY 100 // Reaction time after receiving a command 20 #define RESPONSE_DELAY 100 // Reaction time after receiving a command
21 #define SAFETY_COUNT 90 // Time of one emergency cycle 21 #define SAFETY_COUNT 90 // Time of one emergency cycle
22 #define SEND_DELAY 50 // Time between two characters on RS232 22 #define SEND_DELAY 50 // Time between two characters on RS232
23   23  
24 #define DOME PIN_B4 // Dome controll port 24 #define DOME PIN_B4 // Dome controll port
25 #define HEATING PIN_B3 // Heating for defrosting 25 #define HEATING PIN_B3 // Heating for defrosting
26   26  
27   27  
28 char VER[4]=VERSION; // Buffer for concatenate of a version string 28 char VER[4]=VERSION; // Buffer for concatenate of a version string
29   29  
30 int8 heat; // Status variables 30 int8 heat; // Status variables
31 int8 open; 31 int8 open;
32   32  
33 inline void toggle_dome(void) // Wire exercise 33 inline void toggle_dome(void) // Wire exercise
34 { 34 {
35 if (open>0) 35 if (open>0)
36 {output_toggle(DOME);} // Toggle = Open Dome 36 {output_toggle(DOME);} // Toggle = Open Dome
37 else 37 else
38 {output_high(DOME);} // Do not toggle = Close Dome 38 {output_high(DOME);} // Do not toggle = Close Dome
39 } 39 }
40   40  
41 void delay(int16 cycles) // Wire exercise with delay 41 void delay(int16 cycles) // Wire exercise with delay
42 { 42 {
43 int16 i; 43 int16 i;
44   44  
45 for(i=0; i<cycles; i++) {toggle_dome(); delay_us(100);} 45 for(i=0; i<cycles; i++) {toggle_dome(); delay_us(100);}
46 } 46 }
47   47  
48 void welcome(void) // Welcome message 48 void welcome(void) // Welcome message
49 { 49 {
50 char REV[50]=ID; // Buffer for concatenate of a version string 50 char REV[50]=ID; // Buffer for concatenate of a version string
51   51  
52 if (REV[strlen(REV)-1]=='$') REV[strlen(REV)-1]=0; 52 if (REV[strlen(REV)-1]=='$') REV[strlen(REV)-1]=0;
53 printf("\n\r# Mrakomer %s (C) 2007 KAKL\n\r",VER); // Welcome message 53 printf("\n\r# Mrakomer %s (C) 2007 KAKL\n\r",VER); // Welcome message
54 printf("#%s\n\r",&REV[4]); 54 printf("#%s\n\r",&REV[4]);
55 printf("# <sequence> <ambient[1/100 C]> <sky[1/100 C]> "); 55 printf("# <sequence> <ambient[1/100 C]> <sky[1/100 C]> ");
56 printf("<heating[s]> <dome[s]> <check>\n\r\n\r"); 56 printf("<heating[s]> <dome[s]> <check>\n\r\n\r");
57 } 57 }
58   58  
59   59  
60 #include "smb.c" // System Management Bus driver 60 #include "smb.c" // System Management Bus driver
61   61  
62   62  
63 // Read sensor's RAM 63 // Read sensor's RAM
64 // Returns temperature in °K 64 // Returns temperature in °K
65 int16 ReadTemp(int8 addr, int8 select) 65 int16 ReadTemp(int8 addr, int8 select)
66 { 66 {
67 unsigned char arr[6]; // Buffer for the sent bytes 67 unsigned char arr[6]; // Buffer for the sent bytes
68 int8 crc; // Readed CRC 68 int8 crc; // Readed CRC
69 int16 temp; // Readed temperature 69 int16 temp; // Readed temperature
70   70  
71 addr<<=1; 71 addr<<=1;
72   72  
73 SMB_STOP_bit(); //If slave send NACK stop comunication 73 SMB_STOP_bit(); //If slave send NACK stop comunication
74 SMB_START_bit(); //Start condition 74 SMB_START_bit(); //Start condition
75 SMB_TX_byte(addr); 75 SMB_TX_byte(addr);
76 SMB_TX_byte(RAM_Access|select); 76 SMB_TX_byte(RAM_Access|select);
77 SMB_START_bit(); //Repeated Start condition 77 SMB_START_bit(); //Repeated Start condition
78 SMB_TX_byte(addr); 78 SMB_TX_byte(addr);
79 arr[2]=SMB_RX_byte(ACK); //Read low data,master must send ACK 79 arr[2]=SMB_RX_byte(ACK); //Read low data,master must send ACK
80 arr[1]=SMB_RX_byte(ACK); //Read high data,master must send ACK 80 arr[1]=SMB_RX_byte(ACK); //Read high data,master must send ACK
81 temp=make16(arr[1],arr[2]); 81 temp=make16(arr[1],arr[2]);
82 crc=SMB_RX_byte(NACK); //Read PEC byte, master must send NACK 82 crc=SMB_RX_byte(NACK); //Read PEC byte, master must send NACK
83 SMB_STOP_bit(); //Stop condition 83 SMB_STOP_bit(); //Stop condition
84   84  
85 arr[5]=addr; 85 arr[5]=addr;
86 arr[4]=RAM_Access|select; 86 arr[4]=RAM_Access|select;
87 arr[3]=addr; 87 arr[3]=addr;
88 arr[0]=0; 88 arr[0]=0;
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
119 heat=0; 110 heat=0;
120 open=0; 111 open=0;
121 repeat=TRUE; 112 repeat=TRUE;
122   113  
123 welcome(); 114 welcome();
124   115  
125 tempa=ReadTemp(SA, RAM_Tamb); // Dummy read 116 tempa=ReadTemp(SA, RAM_Tamb); // Dummy read
126 temp=ReadTemp(SA, RAM_Tobj1); 117 temp=ReadTemp(SA, RAM_Tobj1);
127   118  
128 delay_ms(1000); 119 delay_ms(1000);
129 //---WDT 120 //---WDT
130 restart_wdt(); 121 restart_wdt();
131   122  
132 while(TRUE) // Main Loop 123 while(TRUE) // Main Loop
133 { 124 {
134 safety_counter=SAFETY_COUNT; // Heating and Dome Count Down 125 safety_counter=SAFETY_COUNT; // Heating and Dome Count Down
135 do 126 do
136 { 127 {
137 if (safety_counter<SAFETY_COUNT) safety_counter++; 128 if (safety_counter<SAFETY_COUNT) safety_counter++;
138   129  
139 delay(RESPONSE_DELAY); 130 delay(RESPONSE_DELAY);
140   131  
141 if (safety_counter>=SAFETY_COUNT) 132 if (safety_counter>=SAFETY_COUNT)
142 { 133 {
143 if (heat>0) heat--; 134 if (heat>0) heat--;
144 if (open>0) open--; 135 if (open>0) open--;
145   136  
146 if (heat>0) { output_high(HEATING); } else { output_low(HEATING); } 137 if (heat>0) { output_high(HEATING); } else { output_low(HEATING); }
147   138  
148 safety_counter=0; 139 safety_counter=0;
149 //---WDT 140 //---WDT
150 restart_wdt(); 141 restart_wdt();
151 } 142 }
152 } while (!kbhit()&&!repeat); 143 } while (!kbhit()&&!repeat);
153   144  
154 //---WDT 145 //---WDT
155 restart_wdt(); 146 restart_wdt();
156 { // Retrieve command 147 { // Retrieve command
157 char ch='k'; 148 char ch='k';
158   149  
159 if(kbhit()) ch=getc(); 150 if(kbhit()) ch=getc();
160   151  
161 switch (ch) 152 switch (ch)
162 { 153 {
163 case 'h': 154 case 'h':
164 heat=MAXHEAT; // Need heating 155 heat=MAXHEAT; // Need heating
165 break; 156 break;
166   157  
167 case 'c': 158 case 'c':
168 heat=0; // Need colder 159 heat=0; // Need colder
169 break; 160 break;
170   161  
171 case 'o': 162 case 'o':
172 open=MAXOPEN; // Open the dome 163 open=MAXOPEN; // Open the dome
173 break; 164 break;
174   165  
175 case 'x': 166 case 'x':
176 open=MAXOPEN; // Open the dome 167 open=MAXOPEN; // Open the dome
177 heat=MAXHEAT; // Need heating 168 heat=MAXHEAT; // Need heating
178 break; 169 break;
179   170  
180 case 'l': 171 case 'l':
181 open=0; // Lock the dome 172 open=0; // Lock the dome
182 break; 173 break;
183   174  
184 case 'i': 175 case 'i':
185 if (open==0) welcome(); // Information about version, etc... 176 if (open==0) welcome(); // Information about version, etc...
186 break; // Only when dome is closed 177 break; // Only when dome is closed
187   178  
188 case 'r': 179 case 'r':
189 repeat=TRUE; // Repeated measure mode 180 repeat=TRUE; // Repeated measure mode
190 break; 181 break;
191   182  
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  
203 seq++; // Increment the number of measurement 194 seq++; // Increment the number of measurement
204   195  
205 tempa=ReadTemp(SA, RAM_Tamb); // Read temperatures from sensor 196 tempa=ReadTemp(SA, RAM_Tamb); // Read temperatures from sensor
206 temp=ReadTemp(SA, RAM_Tobj1); 197 temp=ReadTemp(SA, RAM_Tobj1);
207   198  
208 ta=tempa*2-27315; // °K -> °C 199 ta=tempa*2-27315; // °K -> °C
209 to=temp*2-27315; 200 to=temp*2-27315;
210   201  
211 { // printf 202 { // printf
212 char output[8]; // Output buffer 203 char output[8]; // Output buffer
213 int8 j; // String pointer 204 int8 j; // String pointer
214 int8 check=0; // Checksum is calculated between '$' and '*' 205 int8 check=0; // Checksum is calculated between '$' and '*'
215   206  
216 delay(SEND_DELAY); 207 delay(SEND_DELAY);
217 putc('$'); 208 putc('$');
218 delay(SEND_DELAY); 209 delay(SEND_DELAY);
219 sprintf(output,"M%s ",VER); 210 sprintf(output,"M%s ",VER);
220 j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j]); check^=output[j++]; } 211 j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j]); check^=output[j++]; }
221 sprintf(output,"%Lu ", seq); 212 sprintf(output,"%Lu ", seq);
222 j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j]); check^=output[j++]; } 213 j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j]); check^=output[j++]; }
223 sprintf(output,"%Ld ", ta); 214 sprintf(output,"%Ld ", ta);
224 j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j]); check^=output[j++]; } 215 j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j]); check^=output[j++]; }
225 sprintf(output,"%Ld ", to); 216 sprintf(output,"%Ld ", to);
226 j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j]); check^=output[j++]; } 217 j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j]); check^=output[j++]; }
227 sprintf(output,"%u ", heat); 218 sprintf(output,"%u ", heat);
228 j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j]); check^=output[j++]; } 219 j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j]); check^=output[j++]; }
229 sprintf(output,"%u ", open); 220 sprintf(output,"%u ", open);
230 j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j]); check^=output[j++]; } 221 j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j]); check^=output[j++]; }
231 sprintf(output,"*%X\n\r\0", check); 222 sprintf(output,"*%X\n\r\0", check);
232 j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j++]); } 223 j=0; while(output[j]!=0) { delay(SEND_DELAY); putc(output[j++]); }
233 delay(SEND_DELAY); 224 delay(SEND_DELAY);
234 } 225 }
235   226  
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"