00001 /*@{*//*@}*/ 00013 00014 /******************************************************************************/ 00015 /* Includes (#include) */ 00016 #include <avr/io.h> 00017 #include <avr/wdt.h> 00018 #include <avr/pgmspace.h> 00019 #include <avr/interrupt.h> 00020 00021 /* Own headers */ 00022 #include "xtypes.h" 00023 #include "global.h" 00024 00025 #include "fan.h" 00026 #include "relay.h" 00027 00028 #define EXPORT 00029 #include "safety.h" 00030 #undef EXPORT 00031 00032 /******************************************************************************/ 00033 /* Constants (#define) *//*@{*/ 00034 /* End: Constants (#define) *//*@}*/ 00035 /******************************************************************************/ 00036 00037 /******************************************************************************/ 00038 /* Macro definitions (#define) *//*@{*/ 00039 /* End: Macro definitions (#define) *//*@}*/ 00040 /******************************************************************************/ 00041 00042 /******************************************************************************/ 00043 /* Basic types (typedef) *//*@{*/ 00044 /* End: Basic types (typedef) *//*@}*/ 00045 /******************************************************************************/ 00046 00047 /******************************************************************************/ 00048 /* Local constants (const) *//*@{*/ 00049 /* End: Local constants (const) *//*@}*/ 00050 /******************************************************************************/ 00051 00052 /******************************************************************************/ 00053 /* Local variables *//*@{*/ 00054 00056 STATIC E_SAFETY_CONDITION mg_eCondition = COND_OK; 00057 00059 STATIC UINT16 mg_u16MemIdx = 0U; 00060 00062 STATIC UINT32 mg_u32CalcCRC = 0xFFFFFFFFUL; 00063 00066 STATIC UINT32 mg_u32ApplCRC = 0x00000000UL; 00067 00068 /* End: Local variables *//*@}*/ 00069 /******************************************************************************/ 00070 00071 /******************************************************************************/ 00072 /* Global variables *//*@{*/ 00073 /* End: Global variables *//*@}*/ 00074 /******************************************************************************/ 00075 00076 /******************************************************************************/ 00077 /* Prototype declarations */ 00078 /* End: Prototype declarations */ 00079 /******************************************************************************/ 00080 00081 /******************************************************************************/ 00082 /* Local functions *//*@{*/ 00083 /* End: Local functions *//*@}*/ 00084 /******************************************************************************/ 00085 00086 /******************************************************************************/ 00087 /* Global functions *//*@{*/ 00088 00089 /* ****************************************************************************/ 00098 /* ****************************************************************************/ 00099 void safety_Init (void) 00100 { 00101 fan_Set(OFF); 00102 mg_u16MemIdx = 0U; 00103 mg_u32CalcCRC = 0xFFFFFFFFUL; 00104 00105 /* read CRC */ 00106 mg_u32ApplCRC = pgm_read_word(MEMPOS_CRC+2U); 00107 mg_u32ApplCRC <<= 16U; 00108 mg_u32ApplCRC += pgm_read_word(MEMPOS_CRC); 00109 00110 /* activate watchdog */ 00111 wdt_enable(WDTO_500MS); 00112 wdt_reset(); 00113 } 00114 00115 00116 /* ****************************************************************************/ 00125 /* ****************************************************************************/ 00126 void safety_SetCondition (E_SAFETY_CONDITION eCond) 00127 { 00128 mg_eCondition |= eCond; 00129 00130 if((mg_eCondition & COND_TEMP_ERR) != 0U) 00131 { 00132 fan_Set(ON); 00133 } 00134 } 00135 00136 00137 /* ****************************************************************************/ 00146 /* ****************************************************************************/ 00147 void safety_ClearCondition (E_SAFETY_CONDITION eCond) 00148 { 00149 mg_eCondition &= ~eCond; 00150 } 00151 00152 00153 /* ****************************************************************************/ 00160 /* ****************************************************************************/ 00161 UINT32 safety_GetCRC(void) 00162 { 00163 return (mg_u32ApplCRC); 00164 } 00165 00166 00167 /* ****************************************************************************/ 00174 /* ****************************************************************************/ 00175 E_SAFETY_CONDITION safety_GetCondition(void) 00176 { 00177 return (mg_eCondition); 00178 } 00179 00180 00181 /* ****************************************************************************/ 00198 /* ****************************************************************************/ 00199 void safety_MemoryTest (void) 00200 { 00201 UINT8 u8Help; 00202 UINT8 u8Idx; 00203 00204 u8Help = pgm_read_byte(mg_u16MemIdx); 00205 00206 /* Calculate CRC */ 00207 for(u8Idx = 8U; u8Idx; u8Idx--) 00208 { 00209 if(((mg_u32CalcCRC & 0x80000000) ? 1U : 0U) != (u8Help & 1U)) 00210 { 00211 mg_u32CalcCRC = (mg_u32CalcCRC << 1U) ^ CRC32POLY; 00212 } 00213 else 00214 { 00215 mg_u32CalcCRC <<= 1U; 00216 } 00217 u8Help >>= 1U; 00218 } 00219 00220 mg_u16MemIdx++; 00221 00222 if(mg_u16MemIdx >= MEMPOS_CRC) 00223 { 00224 mg_u32CalcCRC ^= 0xFFFFFFFFUL; 00225 00226 /* compare CRC values */ 00227 if(mg_u32ApplCRC != mg_u32CalcCRC) 00228 { 00229 safety_SafeMode(); /* this function never returns */ 00230 } 00231 00232 mg_u16MemIdx = 0U; 00233 mg_u32CalcCRC = 0xFFFFFFFFUL; 00234 } 00235 } 00236 00237 00238 /* ****************************************************************************/ 00252 /* ****************************************************************************/ 00253 void safety_SafeMode (void) 00254 { 00255 mg_eCondition |= COND_VOLT_WARN; 00256 mg_eCondition |= COND_CURR_WARN; 00257 mg_eCondition |= COND_TEMP_WARN; 00258 mg_eCondition |= COND_VOLT_ERR; 00259 mg_eCondition |= COND_CURR_ERR; 00260 mg_eCondition |= COND_TEMP_ERR; 00261 cli(); 00262 while(1U) /* loop forever */ 00263 { 00264 relay_Enable(FALSE); 00265 fan_Set(ON); 00266 wdt_reset(); /* watchdog must be triggered to prevent from restarting */ 00267 } 00268 } 00269 00270 00271 /* End: Global functions *//*@}*/ 00272 /******************************************************************************/ 00273 00274 /**** Last line of code ****/