这里只贴出部分代码,以备后续参考。
1 /********************************************************* 2 FILE NAME perimeter.c 3 DESCRIPTION 4 Copyright: Galaxy Industry Ltd. 5 All Rights Reserved 6 Author: Milo lu 7 *********************************************************/ 8 9 //================ 10 User includes 11 ==================/ 12 #include "Global.h" 13 14 #include"IO_Port.h" 15 16 #include"McuInit.h" 17 18 #include "eeprom.h" 19 20 #include"CachedDataControl.h" 21 22 #include"gsmcomms.h" 23 24 #include"TCPIP.h" 25 26 #include"R232_Init.h" 27 28 #include"Perimeter.h" 29 30 extern flag_e flag; 31 32 extern GsenserVariable GsenserTmp; 33 34 extern u16 System_Time_msec,UsartA2_Tx_Disable_T; 35 36 extern u16 Tx_Head_index,Tx_Tail_index; 37 38 //============================== 39 Public variable declaration 40 ================================/ 41 Usart2StateTypeDef usart2_state = USART2_IDLE; 42 43 usart2_flag usart2_fcmd; 44 45 SensorTypeDef ps_sr_status; 46 47 48 //============================== 49 Private variable declaration 50 ================================/ 51 static IgnitionStateReportTypeDef ignition_report = REPORT_UNKNOWN; 52 53 static Sri900nConnectivityTypeDef sri900n_connection = PLUG; 54 55 static u16 usart2_time_ms; 56 static u8 DH_Input_Index; 57 58 static s8 DH_Package[16]; 59 60 //cmd 61 static s8 usart2_cmd[][3]={ 62 {0xB5,0x78,0x01}, 63 {0xB5,0x77,0x02}, 64 {0xB5,0x76,0x03}, 65 {0xB5,0x75,0x04}, 66 {0xB5,0x74,0x05}, 67 {0xB5,0x73,0x06}, 68 {0xB5,0x00,0x07}, 69 {0xB5,0xCC,0}}; 70 71 //=========== get state ============= 72 Sri900nConnectivityTypeDef get_sri900n_connection_state(void) 73 { 74 return sri900n_connection; 75 } 76 77 Usart2StateTypeDef get_usart2_state(void) 78 { 79 return usart2_state; 80 } 81 //========== set state ============= 82 void set_usart2_state(Usart2StateTypeDef state) 83 { 84 usart2_state = state; 85 } 86 87 //========================================= 88 function name: DH_Package_Checksum 89 description 90 parameters: array datalength 91 return: checksum 92 =========================================/ 93 static u8 DH_Package_Checksum(s8* arrary,short datalength) 94 { 95 u8 checksum =0; 96 u8 i; 97 s8 *data = arrary; 98 for(i=0;i<datalength;i++) 99 { 100 checksum += *data++; 101 } 102 return checksum; 103 } 104 105 //========================================================= 106 function name:usartA2_Send_Package 107 discription:send two-dimentional-array 108 parameters:a pointer to an array: 109 return: none 110 ============================================================/ 111 void usartA2_Send_Package(s8(*v)[]) 112 { 113 s8 *pt; 114 u8 checksum; 115 116 TxA2_PowerON; 117 118 Tx_Head_index = 0; 119 Tx_Tail_index = 0; 120 121 checksum = 0; 122 pt = *(v); 123 for(int i=0;i<3;i++) 124 { 125 if(usartA2_Send_Char(*pt)); 126 checksum += *pt++; 127 } 128 129 usartA2_Send_Char(checksum); 130 131 if((UCA2IE&UCTXIE)==0) 132 { 133 UCA2IE|=UCTXIE; 134 UCA2IFG |= UCTXIFG; 135 } 136 } 137 138 //============================================ 139 function name:Is_DH_Package_Received 140 description: 141 Complate datas have received 142 parameters: none 143 return: 1 data received 144 0 data unreceived 145 ==============================================/ 146 HAL_StatusTypeDef usartA2_reception(void) 147 { 148 u8 checksum; 149 150 HAL_StatusTypeDef option_state = HAL_BUSY; 151 152 if(usartA2_Received_Count()) 153 { 154 DH_Package[DH_Input_Index++] = usartA2_Read_Char(); 155 if(DH_Package[0]==0xA4) 156 { 157 if(DH_Input_Index >= 4) 158 { 159 DH_Package[DH_Input_Index] = 0; 160 checksum = DH_Package_Checksum(DH_Package,3); 161 if(checksum == DH_Package[3]) 162 { 163 option_state = HAL_OK; 164 } 165 DH_Input_Index = 0; 166 } 167 } 168 else 169 { 170 DH_Input_Index = 0; 171 } 172 } 173 return option_state; 174 } 175 176 //============================================ 177 description: 178 byte0 = header 179 byte1-2 information 180 byte3 crc 181 ==============================================/ 182 bool usartA2_decryption(s8 *pt,u8 id) 183 { 184 bool result=false; 185 u8 i; 186 if(pt[1]==0xcc) //transmit response 187 { 188 if(pt[2]==(id+1)) 189 { 190 usart2_fcmd.value&=~(1<<id); 191 result = true; 192 } 193 } 194 else //status report 195 { 196 //byte0 = header, 197 byte1:bit7 arm/disarm, bit6=ps on/off,bit5=ps installation status,bit4= sr on/off 198 bit3 = ignition on/off, bit2-bit0 = gs level. 199 byte2: 0x11=panic,0x22=ignition alarm,0x33=shock alert,0x44=tilt alert,0x55=ps alert/ 200 if(BIT7&pt[1]) //arm state 201 { 202 if(get_security_state()!=ARM_STATE) 203 { 204 set_security_state(ARM_STATE); 205 if(get_tcpip_state()!=TCPIP_ON_LINE) 206 { 207 flag.GPRSRetryCount=3; 208 } 209 flag.flag2|= f2gprs_duration_3mins; 210 flag.fReportStatus |= fAlarmArmStatus; 211 } 212 } 213 else if(get_security_state()!=DISARM_STATE)//disarm state 214 { 215 set_security_state(DISARM_STATE); 216 if(get_tcpip_state()!=TCPIP_ON_LINE) 217 { 218 flag.GPRSRetryCount=3; 219 } 220 flag.flag2|= f2gprs_duration_3mins; 221 flag.fReportStatus |= fAlarmArmStatus; 222 } 223 224 if(BIT5&pt[1]) //ps installed 225 { 226 if(BIT6&pt[1]) //ps on 227 { 228 if(ps_sr_status.ps_status!=INSTALLED_AND_ENABLED) 229 { 230 ps_sr_status.ps_status = INSTALLED_AND_ENABLED; 231 flag.fReportStatus |= fPerimeterSirenStatus; 232 } 233 } 234 else //ps off 235 { 236 if(ps_sr_status.ps_status!=INSTALLED_BUT_DISABLED) 237 { 238 ps_sr_status.ps_status = INSTALLED_BUT_DISABLED; 239 flag.fReportStatus |= fPerimeterSirenStatus; 240 } 241 } 242 } 243 else //ps support but no installed 244 { 245 if(ps_sr_status.ps_status!=SUPPORT_BUT_NOT_INSTALLED) 246 { 247 ps_sr_status.ps_status = SUPPORT_BUT_NOT_INSTALLED; 248 flag.fReportStatus|=fPerimeterSirenStatus; 249 } 250 } 251 252 if(BIT4&pt[1]) //sr installed and enable 253 { 254 if(ps_sr_status.sr_status!=INSTALLED_AND_ENABLED) 255 { 256 ps_sr_status.sr_status = INSTALLED_AND_ENABLED; 257 flag.fReportStatus |= fPerimeterSirenStatus; 258 } 259 } 260 else //sr installed but disable 261 { 262 if(ps_sr_status.sr_status!=INSTALLED_BUT_DISABLED) 263 { 264 ps_sr_status.sr_status = INSTALLED_BUT_DISABLED; 265 flag.fReportStatus |= fPerimeterSirenStatus; 266 } 267 } 268 269 if(BIT3&pt[1]) //ignition status 270 { 271 ignition_report = REPORT_ON; 272 } 273 else 274 { 275 ignition_report = REPORT_OFF; 276 } 277 278 i= 0x07&pt[1]; //gs level 279 if((i!=GsenserTmp.SetLevel)&&(5 >= i)&&(i!=0)) 280 { 281 GsenserTmp.SetLevel = i; 282 EEReadModifyWrite(1,eeGsenserTmpSetLevel,&GsenserTmp.SetLevel,1); 283 flag.fReportStatus |= fGsenserSensitivityStatus; 284 } 285 switch(pt[2]) 286 { 287 case 0x11: //panic 288 { 289 flag.Alarm_flag |= fPanicAlarmAlert; 290 break; 291 } 292 case 0x22: //ignition alarm 293 { 294 flag.Alarm_flag |= fIgnitionAlerts; 295 break; 296 } 297 case 0x33: //shock alert 298 { 299 CachedDataTypeDef gsensor_class = StatusAlertDataBytes(ShockAlarmAlertID); 300 loading_data_bytes(gsensor_class); 301 if(get_tcpip_state()!=TCPIP_ON_LINE) 302 { 303 flag.GPRSRetryCount=3; 304 } 305 flag.flag2|= f2gprs_duration_15mins; 306 flag.Alarm_flag |= fShock_Alarm; 307 flag.flag0 |= fGsenserStrikeOccur; //led turn on for 1500ms 308 break; 309 } 310 case 0x44: //tilt alert 311 { 312 CachedDataTypeDef gsensor_class = StatusAlertDataBytes(ShockAlarmAlertID); 313 loading_data_bytes(gsensor_class); 314 if(get_tcpip_state()!=TCPIP_ON_LINE) 315 { 316 flag.GPRSRetryCount=3; 317 } 318 flag.flag2|= f2gprs_duration_15mins; 319 flag.Alarm_flag |= fTilt_Alarm; 320 flag.flag0 |= fGsenserStrikeOccur; //led turn on for 1500ms 321 break; 322 } 323 case 0x55: //ps alert 324 { 325 if(get_tcpip_state()!=TCPIP_ON_LINE) 326 { 327 flag.GPRSRetryCount=3; 328 } 329 flag.flag2|= f2gprs_duration_15mins; 330 flag.Alarm_flag |= fPerimeterTriggerAlarm; 331 flag.flag0 |= fGsenserStrikeOccur; //led turn on for 1500ms 332 break; 333 } 334 default: 335 break; 336 } 337 usart2_cmd[7][2]=pt[2]; 338 usartA2_Send_Package(usart2_cmd+7);//status response 339 } 340 return result; 341 } 342 343 static void sri900_connnecting_status(void) 344 { 345 static u16 debouncing_time_msec; 346 if(f0ignition_off&flag.flag0) 347 { 348 if(ignition_report == REPORT_OFF) 349 { 350 if(sri900n_connection == UNPLUG) 351 sri900n_connection = PLUG; 352 debouncing_time_msec = System_Time_msec; 353 } 354 else if(sri900n_connection == PLUG) 355 { 356 if(System_Time_msec - debouncing_time_msec >= 3*seconds) 357 { 358 ignition_report = REPORT_UNKNOWN; 359 sri900n_connection = UNPLUG; 360 } 361 } 362 } 363 else 364 { 365 if(ignition_report == REPORT_ON) 366 { 367 if(sri900n_connection == UNPLUG) 368 sri900n_connection = PLUG; 369 debouncing_time_msec = System_Time_msec; 370 } 371 else if(sri900n_connection == PLUG) 372 { 373 if(System_Time_msec - debouncing_time_msec >= 3*seconds) 374 { 375 ignition_report = REPORT_UNKNOWN; 376 sri900n_connection = UNPLUG; 377 } 378 } 379 } 380 if((IsTxA2_PowerON)&&((UCA2IE&UCTXIE)==0)) 381 { 382 if(System_Time_msec - UsartA2_Tx_Disable_T >= 10) 383 { 384 TxA2_PowerOFF; 385 } 386 } 387 else UsartA2_Tx_Disable_T = System_Time_msec; 388 } 389 //========== usart2 control ============ 390 void usartA2_controler(void) 391 { 392 u8 i,j; 393 //UsartA2OptionTypeDef opt; 394 static u8 usart2_resend_cnt,sequence_cmd = 0; 395 switch(usart2_state) 396 { 397 case USART2_IDLE: 398 { 399 if(f0system_init_ok&flag.flag0) 400 { 401 set_usart2_state(USART2_INIT); 402 } 403 break; 404 } 405 case USART2_INIT: 406 { 407 if(!(f0usart2_init_ok&flag.flag0)) 408 { 409 usartA2_Init(BR9600); 410 } 411 usart2_resend_cnt = 0; 412 set_usart2_state(USART2_TRANSMIT); 413 break; 414 } 415 case USART2_TRANSMIT: 416 { 417 sequence_cmd = 0xFF; 418 if(usart2_fcmd.value) //cmd request 419 { 420 j=BIT0; 421 for(i=0;i<8;i++) //check which cmd send first 422 { 423 if(j&usart2_fcmd.value) 424 { 425 sequence_cmd = i; 426 break; 427 } 428 j<<=1; 429 } 430 usart2_cmd[6][1] = GsenserTmp.SetLevel; 431 usartA2_Send_Package(usart2_cmd+sequence_cmd); 432 DH_Input_Index = 0; 433 } 434 else 435 { 436 usart2_resend_cnt = 0; 437 } 438 usart2_time_ms = System_Time_msec; 439 set_usart2_state(USART2_STANDBY); 440 break; 441 } 442 case USART2_STANDBY: 443 { 444 if(usartA2_reception()==HAL_OK)//any data receive via RS232 445 { 446 if(usartA2_decryption(DH_Package,sequence_cmd)==true)//Does responsed? 447 { 448 usart2_resend_cnt = 0; 449 set_usart2_state(USART2_TRANSMIT);//check again, does any other request? 450 } 451 } 452 else if(usart2_fcmd.value) //request need to send? 453 { 454 if((System_Time_msec - usart2_time_ms >= 1*seconds)||(usart2_resend_cnt == 0)) 455 { 456 if(usart2_resend_cnt++ >= 5) 457 { 458 usart2_fcmd.value &=~ (1<<sequence_cmd); 459 usart2_resend_cnt = 0; 460 } 461 set_usart2_state(USART2_TRANSMIT); 462 } 463 } 464 465 break; 466 } 467 468 default: 469 { 470 set_usart2_state(USART2_TRANSMIT); 471 break; 472 } 473 } 474 475 sri900_connnecting_status(); 476 } 477 478 InstallationStatusTypeDef get_ps_status(void) 479 { 480 return ps_sr_status.ps_status; 481 } 482 InstallationStatusTypeDef get_sr_status(void) 483 { 484 return ps_sr_status.sr_status; 485 } 486 //============== end file =================== 487