global(6403).c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /* includes ----------------------------------------------------------*/
  2. #include "global.h"
  3. /* typedef -----------------------------------------------------------*/
  4. /* define ------------------------------------------------------------*/
  5. /* macro -------------------------------------------------------------*/
  6. /* variables ---------------------------------------------------------*/
  7. global_par s_global_par;
  8. /* function prototypes -----------------------------------------------*/
  9. // 定义指令周期数,用于实现1ms的延时
  10. // 假设时钟频率为72MHz,每个指令周期为1/72MHz = 13.889ns
  11. // 因此,1ms需要72000个指令周期
  12. #define INSTRUCTION_CYCLES_PER_MS 72000
  13. // 延时函数
  14. void global_delay_ms(uint32_t ms) {
  15. uint32_t i;
  16. uint32_t cycles = ms * INSTRUCTION_CYCLES_PER_MS; // 计算需要消耗的指令周期数
  17. // 消耗指令周期
  18. for (i = 0; i < cycles; ++i) {
  19. __NOP(); // 使用NOP指令来消耗一个指令周期
  20. }
  21. }
  22. /**
  23. * @brief 中控板初始化
  24. * @param p_param_boot : 参数区指针
  25. * @param p_rs485RecDate:485接收数据区的指针
  26. * @note NONE
  27. * @retval 无
  28. */
  29. void centralCtrSys_Init(param_boot *p_param_boot, global_par *p_global_par){
  30. uint8_t recBack = 0;
  31. rs485RecDate *p_rs485RecDate = &s_rs485RecDate;
  32. // 1. 从flash中读出VIN
  33. Read_ParamArea(); // 读出参数区的所有数据
  34. // 2. 与车通信获取vin数据(读取所有寄存器的值)
  35. // modbus_read_holding_registers(0x01, 0x0000, 67);
  36. // global_delay_ms(1000); // 延时3s
  37. // 3. vin码比对
  38. if(s_comData.vinRecSuccess == 1){ // 成功获取到车辆数据
  39. if(memcmp(p_param_boot->vin, p_rs485RecDate->VIN, 26) != 0){ // 比对不成功
  40. // 备份新的VIN码
  41. memcpy(p_param_boot->vin, p_rs485RecDate->VIN, 26);
  42. Write_paramArea();
  43. // 置位中控板迁移事件
  44. BIT_SET(p_global_par->ctrEvent, CtrlPanelMigration);
  45. }
  46. }
  47. // 4. 初始化4g模块 4g模块在这里不好处理 放在任务中,围栏数据也一样
  48. // 5. 读取96位(或者说是3个32位的字)的唯一ID
  49. GetUniqueID(s_messageDate.devId);
  50. // 6. 陀螺仪的初始化
  51. recBack = atk_ms6050_init();
  52. if(recBack != 0){
  53. // printf("ATK-MS6050 init failed!\r\n");
  54. BIT_SET(s_comData.Malfunction, gyroscope); // 置位陀螺仪数据异常故障
  55. }
  56. // printf("ATK-MS6050 init\r\n");
  57. recBack = atk_ms6050_dmp_init();
  58. if(recBack != 0){
  59. // printf("ATK-MS6050 DMP init failed!\r\n");
  60. BIT_SET(s_comData.Malfunction, gyroscope); // 置位陀螺仪数据异常故障
  61. }
  62. // printf("ATK-MS6050 DMP init!\r\n");
  63. // 7. 外部flash初始化
  64. norflash_init();
  65. }
  66. // 车本体故障判断
  67. void fault_car(rs485RecDate *p_rs485RecDate, comData *p_comData){
  68. uint16_t errorLevel = 0;
  69. /* 尝试获取互斥量,等待无限长时间 */
  70. if(osMutexAcquire(mutex_rs485RecDateHandle, 1000) == osOK)
  71. {
  72. errorLevel = p_rs485RecDate->alarmLevel;
  73. /* 访问完成,释放互斥量 */
  74. osMutexRelease(mutex_rs485RecDateHandle);
  75. }
  76. // 存在车故障
  77. if(errorLevel != 0){
  78. BIT_SET(s_comData.Malfunction, carError); // 置位车故障
  79. }else{
  80. BIT_CLEAR(s_comData.Malfunction, carError); // 清除车故障
  81. }
  82. }
  83. /**
  84. * @brief 中控板故障检测
  85. * @param p_global_par:global_par的全局参数指针
  86. * @note 周期为1s1次 清除除电池故障的其他故障
  87. * @retval 无
  88. */
  89. void faultDetection(global_par *p_global_par){
  90. // 参数定义
  91. static uint16_t timesCnt_gps = 0; // gps记时
  92. static uint8_t errorCnt_gyro = 0; // 陀螺仪数据故障滤波次数
  93. static uint16_t timesOut_ota = 0; // OTA超时计数
  94. static uint16_t timesCnt_runFence; // 超运行围栏计数
  95. static uint16_t timesCnt_outSpeed; // 超速计时器
  96. comData *p_comData = &s_comData;
  97. param_boot *p_param_boot = &s_param_boot;
  98. // 开启超级权限 不判断所有故障
  99. if(p_global_par->superUser == 1){
  100. p_comData->Malfunction = 0; // 清除所有故障
  101. return;
  102. }
  103. // 1. 车本体故障判断
  104. fault_car(&s_rs485RecDate, &s_comData); // 车故障判断
  105. // 2. 超出围栏故障判断
  106. if(p_comData->fenceStatus == 1){
  107. if(timesCnt_runFence < p_param_boot->fenceBreach_Timeout){
  108. timesCnt_runFence++;
  109. }else{
  110. timesCnt_runFence = p_param_boot->fenceBreach_Timeout;
  111. BIT_SET(s_comData.Malfunction, fence);
  112. }
  113. }else{
  114. timesCnt_runFence = 0;
  115. BIT_CLEAR(s_comData.Malfunction, fence);
  116. }
  117. // 3. GPS信号丢失
  118. // 获取gps失败,但陀螺仪数据正常的情况下,一分钟之后置位gps故障
  119. // 获取gps失败,陀螺仪数据异常,直接置位gps故障
  120. if((p_global_par->positionErrorCnt >= 3) && (BIT_CHECK(s_comData.Malfunction, fence) == 0)){
  121. if(timesCnt_gps < FILTER_TIME_GPS){ // 4分钟滤波时间
  122. timesCnt_gps++;
  123. }else{
  124. timesCnt_gps = FILTER_TIME_GPS;
  125. BIT_SET(s_comData.Malfunction, positionError);
  126. }
  127. }else if((p_global_par->positionErrorCnt >= 3) && (BIT_CHECK(s_comData.Malfunction, fence) == 1)){
  128. timesCnt_gps = 0;
  129. BIT_SET(s_comData.Malfunction, positionError);
  130. }else{
  131. timesCnt_gps = 0;
  132. }
  133. if(p_global_par->positionErrorCnt == 0){ // 清除gps故障
  134. BIT_CLEAR(s_comData.Malfunction, positionError);
  135. }
  136. // 4. MQTT通信异常
  137. if(p_global_par->mqttTimeoutCnt >= PUBLISH_TIME_MS){
  138. BIT_SET(s_comData.Malfunction, errorMqtt); // 置位MQTT故障
  139. }
  140. if(p_global_par->mqttTimeoutCnt == 0){ // 清除MQTT故障
  141. BIT_CLEAR(s_comData.Malfunction, errorMqtt);
  142. }
  143. // 5. 4G模块初始化故障
  144. if(p_global_par->InitFaultFlag_4G == 1){
  145. BIT_SET(s_comData.Malfunction, init_4G_error); // 置位4g模块初始化故障
  146. }else{
  147. BIT_CLEAR(s_comData.Malfunction, init_4G_error); // 清除4g模块初始化故障
  148. }
  149. // 6. 485通信故障
  150. if(p_global_par->timeoutCnt_485 >= TIMEOUT_485){
  151. BIT_SET(s_comData.Malfunction, com485); // 置位485通信故障
  152. }
  153. if(p_global_par->timeoutCnt_485 == 0){
  154. BIT_CLEAR(s_comData.Malfunction, com485); // 置位485通信故障
  155. }
  156. // 7. 陀螺仪数据故障-- 获取的姿态角数据全部为0
  157. if(p_global_par->gyroDataFaultFlag == 1){
  158. if(errorCnt_gyro < FILTER_TIME_gyro){
  159. errorCnt_gyro++;
  160. }else{
  161. errorCnt_gyro = FILTER_TIME_gyro;
  162. BIT_SET(s_comData.Malfunction, gyroscope); // 置位陀螺仪数据异常故障
  163. }
  164. }else{
  165. errorCnt_gyro = 0;
  166. BIT_CLEAR(s_comData.Malfunction, gyroscope);
  167. }
  168. // 8. 超速故障
  169. if(s_rs485RecDate.vehicleSpeed > s_param_boot.speed_limit){
  170. if(timesCnt_outSpeed < s_param_boot.overspeed_Timeout){
  171. timesCnt_outSpeed++;
  172. }else{
  173. timesCnt_outSpeed = s_param_boot.overspeed_Timeout;
  174. BIT_SET(s_comData.Malfunction, outSpeed); // 置位超速故障
  175. }
  176. }else{
  177. timesCnt_outSpeed = 0;
  178. BIT_CLEAR(s_comData.Malfunction, outSpeed); // 清零超速故障
  179. }
  180. // 9. 升级故障--开始升级后,开始记时,2分钟还未升级成功的话,置位升级故障,
  181. // 退出升级,此次升级失败,旧版本运行,清除升级状态变量、标志、数据,重启释放
  182. // OTA开始后任务周期为200ms
  183. if(p_global_par->otaUpgradeStartFlag == 1){
  184. if(timesOut_ota < TIME_OUT_OTA){
  185. timesOut_ota++;
  186. }else{
  187. timesOut_ota = TIME_OUT_OTA;
  188. BIT_SET(s_comData.Malfunction, OTA_fault); // 置位OTA异常故障
  189. }
  190. }
  191. // 10. 订阅主题失败
  192. if(p_global_par->subscribe_fail == 1){
  193. BIT_SET(s_comData.Malfunction, subscribeFail); // 置位订阅主题失败故障
  194. }else{
  195. BIT_CLEAR(s_comData.Malfunction, subscribeFail); // 清除订阅主题失败故障
  196. }
  197. // 11. 发布消息失败
  198. if((p_global_par->publish_fail == 1) || (s_global_par.reconnect_server_flag == 1)){
  199. BIT_SET(s_comData.Malfunction, publishFail); // 置位发布消息失败故障
  200. }else{
  201. BIT_CLEAR(s_comData.Malfunction, publishFail); // 清除发布消息失败故障
  202. }
  203. // 12. 获取定位信息失败
  204. if(p_global_par->get_location_error == 1){
  205. BIT_SET(s_comData.Malfunction, getLocationError); // 置位获取定位信息失败故障
  206. }else{
  207. BIT_CLEAR(s_comData.Malfunction, getLocationError); // 清除获取定位信息失败故障
  208. }
  209. }
  210. /**
  211. * @brief 控制蜂鸣器响停
  212. * @param count 当前计数
  213. * @param on_threshold 响的阈值
  214. * @param off_threshold 停的阈值
  215. * @retval 无
  216. */
  217. void beep_control(uint8_t count, uint8_t on_threshold, uint8_t off_threshold) {
  218. if (count < on_threshold) {
  219. GPIO_BEEP(GPIO_PIN_SET);
  220. } else if (count < off_threshold) {
  221. GPIO_BEEP(GPIO_PIN_RESET);
  222. }
  223. }
  224. /**
  225. * @brief 控制蜂鸣器响应
  226. * @note 根据不同条件控制蜂鸣器的响停 进入周期为100ms
  227. * @param 无
  228. * @retval 无
  229. */
  230. void control_beep_response(void) {
  231. global_par *p_global_par = &s_global_par;
  232. // 蜂鸣器计数器
  233. static uint8_t beep_count_fence = 0;
  234. // static uint8_t beep_count_fault = 0;
  235. static uint8_t beep_count_ota = 0;
  236. // 超出围栏 500ms频率响应
  237. if (BIT_CHECK(s_comData.Malfunction, fence)) {
  238. beep_control(beep_count_fence, 5, 10);
  239. beep_count_fence = (beep_count_fence >= 10) ? 0 : beep_count_fence + 1;
  240. }
  241. // OTA升级 2s的响应频率
  242. else if (p_global_par->otaUpgradeStartFlag == 1) {
  243. beep_control(beep_count_ota, 20, 40);
  244. beep_count_ota = (beep_count_ota >= 40) ? 0 : beep_count_ota + 1;
  245. }
  246. // 不存在蜂鸣器响应,蜂鸣器应关闭
  247. else{
  248. GPIO_BEEP(GPIO_PIN_RESET);
  249. }
  250. }
  251. // 格式转换函数
  252. void formatDateTimeAndMalfunction(uint16_t year, uint8_t month, uint8_t day,
  253. uint8_t hour, uint8_t minute, uint8_t sec,
  254. uint32_t malfunction, char *output) {
  255. // 确保提供的output数组足够大
  256. // 格式化字符串为 YYYYMMDD-HHMM:0xXXXXXXXX 格式
  257. sprintf(output, "%04u%02u%02u-%02u%02u%02u:0x%08X,",
  258. year, month, day, hour, minute, sec, malfunction);
  259. output[29] = '\0'; // 确保字符串以空字符结尾
  260. }
  261. /**
  262. * @brief 历史故障存储
  263. * @note 此函数1s1次
  264. * @param 无
  265. * @retval 无
  266. */
  267. void storeFaultRecord(void) {
  268. uint16_t id = 0;
  269. char storeFaultData[30] = {0};
  270. static uint32_t fault_log = 0; // 用来同当前故障数据对比,看是否产生故障变化
  271. static uint16_t timeCnt_delay = 0; // 存在故障
  272. id = norflash_read_id(); /* 读取FLASH ID */
  273. if((id == 0) || (id == 0XFFFF)) { // 检测不到FLASH芯片
  274. return;
  275. }
  276. // 检查是否存在故障
  277. if (s_comData.Malfunction == 0) {
  278. return; // 如果没有故障,直接返回
  279. }
  280. // 检查是否产生故障变化或者到达计时时间
  281. if((fault_log == s_comData.Malfunction) && (timeCnt_delay < FAULT_RECORD_TIME)){
  282. timeCnt_delay++;
  283. return; // 如果没有故障变化或者未到达计时时间,直接返回
  284. }else{
  285. // 更新计数值和故障log
  286. timeCnt_delay = 0;
  287. fault_log = s_comData.Malfunction;
  288. }
  289. // 存储格式转换成 这样的20240513-172203:0x0000000f,字符串数据
  290. formatDateTimeAndMalfunction(s_nmea_utc_time.year,s_nmea_utc_time.month,s_nmea_utc_time.date,
  291. s_nmea_utc_time.hour,s_nmea_utc_time.min,s_nmea_utc_time.sec,s_comData.Malfunction, storeFaultData);
  292. // 写入数据
  293. norflash_write((uint8_t *)storeFaultData, s_param_boot.nextFaultAddr, strlen(storeFaultData));
  294. // 更新索引
  295. s_param_boot.faultRecordIndex = (s_param_boot.faultRecordIndex < MAX_RECORDS) ? (s_param_boot.faultRecordIndex + 1) : 0 ;
  296. // 更新地址
  297. s_param_boot.nextFaultAddr += strlen(storeFaultData);
  298. if(s_param_boot.faultRecordIndex == 0){
  299. s_param_boot.nextFaultAddr = 0;
  300. }
  301. // 保存最新的索引和地址数据
  302. Write_paramArea();
  303. }
  304. /**
  305. * @brief 更新保存的时间信息
  306. * @note 此函数1s1次
  307. * @param 无
  308. * @retval 无
  309. */
  310. void refreshSavedTime(void){
  311. static uint8_t timesCnt = 0;
  312. // 未获取到时间数据,则不保存
  313. if(s_global_par.time_stamp_flag != 1){
  314. return;
  315. }
  316. s_param_boot.year = s_nmea_utc_time.year;
  317. s_param_boot.month = s_nmea_utc_time.month;
  318. s_param_boot.date = s_nmea_utc_time.date;
  319. s_param_boot.hour = s_nmea_utc_time.hour;
  320. s_param_boot.min = s_nmea_utc_time.min;
  321. s_param_boot.sec = s_nmea_utc_time.sec;
  322. // 10s保存一次时间数据,发生故障时保存一次,固件更新时保存一次
  323. if(timesCnt < times_10s){
  324. timesCnt++;
  325. }else{
  326. timesCnt = 0;
  327. Write_paramArea();
  328. }
  329. }