encoder.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  1. /*
  2. * @Description:
  3. * @Version: 2.0
  4. * @Author: Seven
  5. * @Date: 2022-08-22 09:55:26
  6. * @LastEditors: Seven
  7. * @LastEditTime: 2023-04-28 18:19:10
  8. */
  9. #include "encoder.h"
  10. #include "includes.h"
  11. GPIO_TypeDef_t g_encoder_cfg[] = {
  12. {GPIOC,GPIO_Pin_6, EXTI_Line6},
  13. {GPIOD,GPIO_Pin_12,EXTI_Line12},
  14. {GPIOC,GPIO_Pin_7, EXTI_Line7},
  15. {GPIOD,GPIO_Pin_13,EXTI_Line13},
  16. {GPIOB,GPIO_Pin_10,EXTI_Line10},
  17. {GPIOD,GPIO_Pin_14,EXTI_Line14},
  18. {GPIOC,GPIO_Pin_9, EXTI_Line9},
  19. {GPIOD,GPIO_Pin_15,EXTI_Line15},
  20. };
  21. uint8_t pulse_index[4] = {1,3,5,7};//脉冲索引
  22. uint8_t gunsignal_index[4] = {0,2,4,6};//枪信号索引
  23. exti_info_t exti_info[EXTI_LINE_MAX];//中断线对应信息
  24. gun_gpio_info_t gun_gpio_info[ENCODER_TAX_NUM][ENCODER_GUN_NUM];//枪引脚定义信息
  25. encoder_record_info_t encoder_record_info[ENCODER_TAX_NUM][ENCODER_GUN_NUM];//编码器记录信息
  26. encoder_eeprom_t encoder_eeprom[ENCODER_TAX_NUM][ENCODER_GUN_NUM]; //编码器存储数据
  27. pulse_up_info_t pulse_up_data[ENCODER_TAX_NUM][ENCODER_GUN_NUM][ENCODER_SAVE_GROUP_NUM]; //脉冲上报信息存储
  28. encoder_gunnum_ee_t encoder_gunnum_ee[ENCODER_TAX_NUM][ENCODER_GUN_NUM];//抬枪次数存储
  29. gunnum_time_t gunnum_time[ENCODER_TAX_NUM][ENCODER_GUN_NUM];//空抬枪计时
  30. //写eeprom计时
  31. timeout_t eeprom_write_time =
  32. {
  33. 0xff,
  34. 0,
  35. (1*1000),
  36. };
  37. void encoder_data_init(void)//编码器数据初始化
  38. {
  39. memset(&encoder_record_info[0][0],0,sizeof(encoder_record_info_t));
  40. memset(&gun_gpio_info[0][0],0,sizeof(gun_gpio_info_t));
  41. memset(&encoder_eeprom[0][0],0,sizeof(encoder_eeprom_t));
  42. }
  43. //编码器引脚驱动初始化
  44. void encoder_gpio_init(void)
  45. {
  46. GPIO_InitTypeDef GPIO_InitStructure;
  47. EXTI_InitTypeDef EXTI_InitStructure;
  48. NVIC_InitTypeDef NVIC_InitStructure;
  49. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD,ENABLE);//使能PORTA,PORTC时钟
  50. GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);//关闭jtag,使能SWD,可以用SWD模式调试
  51. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz,这里不用传参,直接写死用最大速度50MHZ
  52. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉输入
  53. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  54. GPIO_Init(GPIOB, &GPIO_InitStructure); //初始化GPIO
  55. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz,这里不用传参,直接写死用最大速度50MHZ
  56. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉输入
  57. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_9;
  58. GPIO_Init(GPIOC, &GPIO_InitStructure); //初始化GPIO
  59. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz,这里不用传参,直接写死用最大速度50MHZ
  60. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉输入
  61. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
  62. GPIO_Init(GPIOD, &GPIO_InitStructure); //初始化GPIO
  63. //中断配置
  64. RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); //使能复用功能时钟
  65. //GPIOE.2 中断线以及中断初始化配置 上升沿触发
  66. GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource10);
  67. GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource6);
  68. GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource7);
  69. GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource9);
  70. GPIO_EXTILineConfig(GPIO_PortSourceGPIOD, GPIO_PinSource12);
  71. GPIO_EXTILineConfig(GPIO_PortSourceGPIOD, GPIO_PinSource13);
  72. GPIO_EXTILineConfig(GPIO_PortSourceGPIOD, GPIO_PinSource14);
  73. GPIO_EXTILineConfig(GPIO_PortSourceGPIOD, GPIO_PinSource15);
  74. EXTI_InitStructure.EXTI_Line = EXTI_Line6|EXTI_Line7|EXTI_Line9|EXTI_Line10|EXTI_Line12|EXTI_Line13|EXTI_Line14|EXTI_Line15;
  75. EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  76. EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling; //上升沿和下降沿触发
  77. EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  78. EXTI_Init(&EXTI_InitStructure); //根据EXTI_InitStruct中指定的参数初始化外设EXTI寄存器
  79. //设置中断优先级
  80. NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn; //使能中断通道
  81. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00; //抢占优先级2,
  82. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00; //子优先级3
  83. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断通道
  84. NVIC_Init(&NVIC_InitStructure);
  85. EXTI_ClearITPendingBit(EXTI_Line10|EXTI_Line12|EXTI_Line13|EXTI_Line14|EXTI_Line15); //清除LINE11上的中断标志位
  86. EXTI_ClearITPendingBit(EXTI_Line6|EXTI_Line7|EXTI_Line9);
  87. }
  88. //枪引脚对应关系初始化
  89. void gun_gpio_init(void)
  90. {
  91. uint8_t i,j;
  92. uint8_t pulse,gunsignal;
  93. for(i = 0; i < ENCODER_TAX_NUM; i++)
  94. {
  95. for(j = 0; j < ENCODER_GUN_NUM; j++)
  96. {
  97. pulse = pulse_index[i*2+j];
  98. gunsignal = gunsignal_index[i*2+j];
  99. //枪信号
  100. gun_gpio_info[i][j].gunsignal.GPIOx = g_encoder_cfg[gunsignal].GPIOx;
  101. gun_gpio_info[i][j].gunsignal.GPIO_Pin = g_encoder_cfg[gunsignal].GPIO_Pin;
  102. gun_gpio_info[i][j].gunsignal.EXTI_Line = g_encoder_cfg[gunsignal].EXTI_Line;
  103. //脉冲信号
  104. gun_gpio_info[i][j].pulse.GPIOx = g_encoder_cfg[pulse].GPIOx;
  105. gun_gpio_info[i][j].pulse.GPIO_Pin = g_encoder_cfg[pulse].GPIO_Pin;
  106. gun_gpio_info[i][j].pulse.EXTI_Line = g_encoder_cfg[pulse].EXTI_Line;
  107. }
  108. }
  109. }
  110. //中断线对应关系初始化
  111. void exti_relation_init(void)
  112. {
  113. uint8_t i;
  114. uint8_t m,n;
  115. for(i = 0; i < 8; i++)
  116. {
  117. for(m = 0; m < ENCODER_TAX_NUM; m++)
  118. {
  119. for(n = 0; n < ENCODER_GUN_NUM; n++)
  120. {
  121. if(g_encoder_cfg[i].EXTI_Line == gun_gpio_info[m][n].gunsignal.EXTI_Line)
  122. {
  123. exti_info[i].tax = m;
  124. exti_info[i].gun = n;
  125. exti_info[i].mode = GUN_GPIO;
  126. exti_info[i].timeout = GUNSIGNAL_IRQ_TIME;
  127. break;
  128. }
  129. else if(g_encoder_cfg[i].EXTI_Line == gun_gpio_info[m][n].pulse.EXTI_Line)
  130. {
  131. exti_info[i].tax = m;
  132. exti_info[i].gun = n;
  133. exti_info[i].mode = PULSE_GPIO;
  134. exti_info[i].timeout = PULSE_IRQ_TIME;
  135. break;
  136. }
  137. }
  138. }
  139. }
  140. }
  141. //获取编码器相应引脚电平
  142. uint8_t get_encoder_bit(uint8_t tax, uint8_t gun, uint8_t mode)
  143. {
  144. uint8_t state = 0;
  145. if(mode == GUN_GPIO)
  146. {
  147. state = GPIO_ReadInputDataBit(gun_gpio_info[tax][gun].gunsignal.GPIOx,gun_gpio_info[tax][gun].gunsignal.GPIO_Pin);
  148. }
  149. else if(mode == PULSE_GPIO)
  150. {
  151. state = GPIO_ReadInputDataBit(gun_gpio_info[tax][gun].pulse.GPIOx,gun_gpio_info[tax][gun].pulse.GPIO_Pin);
  152. }
  153. return state;
  154. }
  155. //上电判断抬枪状态
  156. void gunsignal_init(void)
  157. {
  158. int i,j;
  159. for(i = 0; i < ENCODER_TAX_NUM; i++)
  160. {
  161. for(j = 0; j < ENCODER_GUN_NUM; j++)
  162. {
  163. encoder_record_info[i][j].gunsignal_irq.last_level = get_encoder_bit(i,j,GUN_GPIO);
  164. encoder_record_info[i][j].pulse_irq.last_level = get_encoder_bit(i,j,PULSE_GPIO);
  165. if(!encoder_record_info[i][j].gunsignal_irq.last_level)//低电平抬枪
  166. {
  167. encoder_record_info[i][j].encoder_count_info.gun_signal = GUN_UP;
  168. timeout_setValue(&encoder_record_info[i][j].pulse_stop_time,PULSE_GUN_END_TIME,1); //有抬枪信号定时
  169. printf_debug(ENCODER_INIT_DEBUG, "tax:%d,gun:%d,抬枪\n",i+1,j+1);
  170. }
  171. else
  172. {
  173. printf_debug(ENCODER_INIT_DEBUG, "tax:%d,gun:%d,挂枪\n",i+1,j+1);
  174. }
  175. // encoder_record_info[i][j].gunup_irq.last_level = encoder_record_info[i][j].encoder_count_info.gun_signal;
  176. }
  177. }
  178. }
  179. //编码器存储数据初始化
  180. void encoder_eeprom_init(void)
  181. {
  182. int i,j;
  183. uint8_t num = 0, err1,err2;
  184. uint8_t err3,err4;
  185. uint8_t tax1_temp[ENCODER_GUN_NUM*16] = {0};
  186. uint8_t tax2_temp[ENCODER_GUN_NUM*16] = {0};
  187. uint8_t tax1_temp_de[ENCODER_GUN_NUM*8] = {0};
  188. uint8_t tax2_temp_de[ENCODER_GUN_NUM*8] = {0};
  189. uint8_t tax1_temp_gun[ENCODER_GUN_NUM*8] = {0};
  190. uint8_t tax2_temp_gun[ENCODER_GUN_NUM*8] = {0};
  191. uint8_t tax1_temp_gun_de[ENCODER_GUN_NUM*4] = {0};
  192. uint8_t tax2_temp_gun_de[ENCODER_GUN_NUM*4] = {0};
  193. move:
  194. AT24CXX_Read(EEPROM_ENCODER_TAX1_ADDR,(uint8_t *)tax1_temp,ENCODER_GUN_NUM*16);//读编码器报税口1数据
  195. AT24CXX_Read(EEPROM_ENCODER_TAX2_ADDR,(uint8_t *)tax2_temp,ENCODER_GUN_NUM*16);//读编码器报税口2数据
  196. data_dump_debug(ENCODER_INIT_DATA_DEBUG, "tax1 read eeprom",tax1_temp,ENCODER_GUN_NUM*16);
  197. data_dump_debug(ENCODER_INIT_DATA_DEBUG, "tax2 read eeprom",tax2_temp,ENCODER_GUN_NUM*16);
  198. AT24CXX_Read(EEPROM_ENCODER_TAX1_GUNNUM_ADDR,(uint8_t *)tax1_temp_gun,ENCODER_GUN_NUM*8);//读编码器报税口1空抬枪次数
  199. AT24CXX_Read(EEPROM_ENCODER_TAX2_GUNNUM_ADDR,(uint8_t *)tax2_temp_gun,ENCODER_GUN_NUM*8);//读编码器报税口2空抬枪次数
  200. //解码
  201. err1 = HM_decode(tax1_temp,tax1_temp_de,ENCODER_GUN_NUM*16);
  202. err2 = HM_decode(tax2_temp,tax2_temp_de,ENCODER_GUN_NUM*16);
  203. err3 = HM_decode(tax1_temp_gun,tax1_temp_gun_de,ENCODER_GUN_NUM*8);
  204. err4 = HM_decode(tax2_temp_gun,tax2_temp_gun_de,ENCODER_GUN_NUM*8);
  205. if(err1 || err2 || err3 || err4)
  206. {
  207. num++;
  208. printf("encoder data err\n");
  209. if(num < 3)
  210. {
  211. goto move;
  212. }
  213. else//解码错误,清零
  214. {
  215. if(err1)//编码器数据
  216. {
  217. memset(tax1_temp,0,ENCODER_GUN_NUM*16);
  218. memset(tax1_temp_de,0,ENCODER_GUN_NUM*8);
  219. AT24CXX_Write(EEPROM_ENCODER_TAX1_ADDR,(uint8_t *)tax1_temp,ENCODER_GUN_NUM*16);
  220. }
  221. if(err2)
  222. {
  223. memset(tax2_temp,0,ENCODER_GUN_NUM*16);
  224. memset(tax2_temp_de,0,ENCODER_GUN_NUM*8);
  225. AT24CXX_Write(EEPROM_ENCODER_TAX2_ADDR,(uint8_t *)tax2_temp,ENCODER_GUN_NUM*16);
  226. }
  227. if(err3)//空抬枪数据
  228. {
  229. memset(tax1_temp_gun,0,ENCODER_GUN_NUM*8);
  230. memset(tax1_temp_gun_de,0,ENCODER_GUN_NUM*4);
  231. AT24CXX_Write(EEPROM_ENCODER_TAX1_GUNNUM_ADDR,(uint8_t *)tax1_temp_gun,ENCODER_GUN_NUM*8);
  232. }
  233. if(err4)
  234. {
  235. memset(tax2_temp_gun,0,ENCODER_GUN_NUM*8);
  236. memset(tax2_temp_gun_de,0,ENCODER_GUN_NUM*4);
  237. AT24CXX_Write(EEPROM_ENCODER_TAX2_GUNNUM_ADDR,(uint8_t *)tax2_temp_gun,ENCODER_GUN_NUM*8);
  238. }
  239. }
  240. }
  241. data_dump_debug(ENCODER_INIT_DATA_DEBUG, "tax1 decode",tax1_temp_de,ENCODER_GUN_NUM*8);
  242. data_dump_debug(ENCODER_INIT_DATA_DEBUG, "tax2 decode",tax2_temp_de,ENCODER_GUN_NUM*8);
  243. //eeprom存储数据初始化
  244. for(i = 0; i < ENCODER_TAX_NUM; i++)
  245. {
  246. for(j = 0; j < ENCODER_GUN_NUM; j++)
  247. {
  248. if(i == 0)
  249. {
  250. memcpy((uint8_t *)&encoder_eeprom[i][j].id, tax1_temp_de+(j*8), 8);
  251. memcpy((uint8_t *)&encoder_gunnum_ee[i][j].gunup_num,tax1_temp_gun_de+(j*4),4);//抬枪次数
  252. encoder_record_info[i][j].encoder_count_info.gunup_num = encoder_gunnum_ee[i][j].gunup_num;
  253. }
  254. else if(i == 1)
  255. {
  256. memcpy((uint8_t *)&encoder_eeprom[i][j].id, tax2_temp_de+(j*8), 8);
  257. memcpy((uint8_t *)&encoder_gunnum_ee[i][j].gunup_num,tax2_temp_gun_de+(j*4),4);//抬枪次数
  258. encoder_record_info[i][j].encoder_count_info.gunup_num = encoder_gunnum_ee[i][j].gunup_num;
  259. }
  260. printf_debug(ENCODER_INIT_DEBUG, "tax:%d,gun:%d,id=%u,total pulse=%u\n",i+1,j+1,encoder_eeprom[i][j].id,encoder_eeprom[i][j].total_pulse);
  261. printf_debug(ENCODER_INIT_DEBUG, "tax:%d,gun:%d,gun num=%u\n",i+1,j+1,encoder_gunnum_ee[i][j].gunup_num);
  262. }
  263. }
  264. }
  265. //编码器初始化
  266. void encoder_init(void)
  267. {
  268. encoder_data_init();
  269. encoder_gpio_init(); //编码器引脚驱动初始化
  270. gun_gpio_init(); //枪引脚对应关系初始化
  271. exti_relation_init(); //中断线对应关系初始化
  272. gunsignal_init(); //上电抬抢信号读取
  273. encoder_eeprom_init(); //编码器存储信息初始化
  274. }
  275. //脉冲数记录函数
  276. void pulse_save_handle(uint8_t tax, uint8_t gun, uint8_t mode)
  277. {
  278. uint8_t write_group = 0;
  279. uint32_t id = 0;
  280. uint32_t total_pulse = 0;
  281. uint32_t single_pulse = 0;
  282. if(encoder_record_info[tax][gun].encoder_count_info.pulse_count)//脉冲数不为0
  283. {
  284. single_pulse = encoder_record_info[tax][gun].encoder_count_info.pulse_count;
  285. encoder_record_info[tax][gun].encoder_count_info.pulse_count = 0;
  286. //总脉冲数
  287. // encoder_eeprom[tax][gun].total_pulse += encoder_record_info[tax][gun].encoder_count_info.pulse_count;
  288. //组号从0开始,id从1开始
  289. if(encoder_record_info[tax][gun].encoder_count_info.write_id == 0)//上电后第一次写入
  290. {
  291. // //读取存储的id
  292. encoder_record_info[tax][gun].encoder_count_info.write_id = encoder_eeprom[tax][gun].id;
  293. encoder_record_info[tax][gun].encoder_count_info.read_id = encoder_eeprom[tax][gun].id;
  294. encoder_record_info[tax][gun].encoder_count_info.read_first_valie = 1;
  295. write_group = 0;
  296. }
  297. else
  298. {
  299. //新数据组号+1
  300. write_group = (encoder_record_info[tax][gun].encoder_count_info.write_group+1)%ENCODER_SAVE_GROUP_NUM; //写入的组号
  301. }
  302. // id = encoder_record_info[tax][gun].encoder_count_info.write_id+1;//记录id
  303. id = encoder_eeprom[tax][gun].id + 1;//记录id
  304. total_pulse = encoder_eeprom[tax][gun].total_pulse+single_pulse;//总脉冲
  305. //上报信息存储
  306. pulse_up_data[tax][gun][write_group].id = id;
  307. pulse_up_data[tax][gun][write_group].pulse_mode = mode;
  308. pulse_up_data[tax][gun][write_group].single_pulse_num = single_pulse;
  309. pulse_up_data[tax][gun][write_group].total_pulse_num = total_pulse;
  310. pulse_up_data[tax][gun][write_group].time = encoder_gunnum_ee[tax][gun].gunup_num;//encoder_gunnum_ee[tax][gun].gunup_num;//RTC_GetCounter(); //时间
  311. //记录数据存储
  312. encoder_record_info[tax][gun].encoder_count_info.write_group = write_group;
  313. // encoder_record_info[tax][gun].encoder_count_info.write_id = id;
  314. encoder_eeprom[tax][gun].id = id;
  315. encoder_eeprom[tax][gun].total_pulse = total_pulse;
  316. encoder_record_info[tax][gun].encoder_count_info.eeprom_write_flag = 1; //写eeprom标记置1
  317. printf_debug(ENCODER_NEW_DEBUG, "t= %d,g= %d, group=%u,id=%u,mode=%d,pulse=%u,total=%u,time=%u\n",tax+1,gun+1,write_group,pulse_up_data[tax][gun][write_group].id,pulse_up_data[tax][gun][write_group].pulse_mode,\
  318. pulse_up_data[tax][gun][write_group].single_pulse_num,pulse_up_data[tax][gun][write_group].total_pulse_num,pulse_up_data[tax][gun][write_group].time);
  319. }
  320. else//脉冲数为0,空抬枪
  321. {
  322. if(mode == PULSE_END_MODE_GUN)//挂枪产生的交易结算才计算空抬枪次数
  323. {
  324. if((gunnum_time[tax][gun].gundown_time - gunnum_time[tax][gun].gunup_time) <= GUN_NUM_TIME)
  325. {
  326. printf("gun time short\n");
  327. }
  328. else
  329. {
  330. encoder_record_info[tax][gun].encoder_count_info.gunup_num++;//抬枪次数++
  331. encoder_record_info[tax][gun].encoder_count_info.eeprom_write_gunup_flag = 1;//写eeprom标记
  332. printf("gun time long\n");
  333. }
  334. }
  335. }
  336. }
  337. //交易完成处理函数
  338. void encoder_record_end_handle(void)
  339. {
  340. uint8_t i,j;
  341. for(i = 0; i < ENCODER_TAX_NUM; i++)
  342. {
  343. for(j = 0; j < ENCODER_GUN_NUM; j++)
  344. {
  345. //脉冲停止计数定时超时
  346. if(timeout_isOut(&encoder_record_info[i][j].pulse_stop_time))
  347. {
  348. pulse_save_handle(i,j,PULSE_END_MODE_LOGIC);
  349. //抬枪信号设置为无效
  350. encoder_record_info[i][j].gunsignal_irq.gun_signal_is_work = GUN_SIGNAL_DISABLE;
  351. printf("tax:%d,gun:%d,pulse timeout\n",i+1,j+1);
  352. // printf("tax:%d,gun:%d,is work:%d\n",i+1,j+1,encoder_record_info[i][j].gunsignal_irq.gun_signal_is_work);
  353. }
  354. //产生挂枪中断
  355. if(encoder_record_info[i][j].gunsignal_irq.gun_down_irq)
  356. {
  357. encoder_record_info[i][j].gunsignal_irq.gun_down_irq = 0;
  358. pulse_save_handle(i,j,PULSE_END_MODE_GUN);
  359. }
  360. }
  361. }
  362. }
  363. //中断内函数
  364. void encoder_irq_handle(uint8_t exti)
  365. {
  366. uint8_t tax,gun;
  367. tax = exti_info[exti].tax;
  368. gun = exti_info[exti].gun;
  369. if(exti_info[exti].mode == GUN_GPIO)
  370. {
  371. timeout_setValue(&encoder_record_info[tax][gun].gunsignal_irq.wave_time,GUNSIGNAL_IRQ_TIME,1);
  372. }
  373. else if(exti_info[exti].mode == PULSE_GPIO)
  374. {
  375. timeout_setValue_us(&encoder_record_info[tax][gun].pulse_irq.wave_time,PULSE_IRQ_TIME,1);
  376. }
  377. }
  378. //抬枪处理函数
  379. void gun_up_handle(uint8_t tax, uint8_t gun)
  380. {
  381. encoder_record_info[tax][gun].gunsignal_irq.gun_signal_is_work = GUN_SIGNAL_ENABLE; //枪信号标记为可用
  382. encoder_record_info[tax][gun].encoder_count_info.gun_signal = GUN_UP; //枪状态为抬枪
  383. timeout_setValue(&encoder_record_info[tax][gun].pulse_stop_time,PULSE_GUN_END_TIME,1); //有抬枪信号定时
  384. //抬枪计时
  385. gunnum_time[tax][gun].gunup_time = TickCounter;
  386. // timeout_setValue(&encoder_record_info[tax][gun].gunup_irq.wave_time,GUNUP_NUM_IRQ_TIME,1);//记录抬枪次数
  387. }
  388. //挂枪处理函数
  389. void gun_down_handle(uint8_t tax, uint8_t gun)
  390. {
  391. timeout_stop(&encoder_record_info[tax][gun].pulse_stop_time);
  392. encoder_record_info[tax][gun].gunsignal_irq.gun_down_irq++; //挂枪中断个数加一
  393. encoder_record_info[tax][gun].gunsignal_irq.gun_signal_is_work = GUN_SIGNAL_ENABLE; //枪信号标记为可用
  394. encoder_record_info[tax][gun].encoder_count_info.gun_signal = GUN_DOWN; //枪状态为挂枪
  395. gunnum_time[tax][gun].gundown_time = TickCounter;
  396. // timeout_setValue(&encoder_record_info[tax][gun].gunup_irq.wave_time,GUNUP_NUM_IRQ_TIME,1);//记录抬枪次数
  397. }
  398. //脉冲上升沿处理函数
  399. void pulse_up_handle(uint8_t tax, uint8_t gun)
  400. {
  401. encoder_record_info[tax][gun].encoder_count_info.pulse_count++;
  402. if(encoder_record_info[tax][gun].encoder_count_info.gun_signal == GUN_DOWN)
  403. {
  404. encoder_record_info[tax][gun].gunsignal_irq.gun_signal_is_work = GUN_SIGNAL_DISABLE;
  405. timeout_setValue(&encoder_record_info[tax][gun].pulse_stop_time,PULSE_NOGUN_END_TIME,1); //无抬枪信号定时
  406. }
  407. else
  408. {
  409. if(encoder_record_info[tax][gun].gunsignal_irq.gun_signal_is_work == GUN_SIGNAL_DISABLE)//抬枪信号不可用
  410. {
  411. timeout_setValue(&encoder_record_info[tax][gun].pulse_stop_time,PULSE_NOGUN_END_TIME,1); //无抬枪信号定时
  412. }
  413. else
  414. {
  415. timeout_setValue(&encoder_record_info[tax][gun].pulse_stop_time,PULSE_GUN_END_TIME,1); //有抬枪信号定时
  416. }
  417. }
  418. }
  419. //抬枪信号滤波函数
  420. void gunsignal_irq_delay_handle(void)
  421. {
  422. uint8_t i,j;
  423. uint8_t current_level;
  424. for(i = 0; i < ENCODER_TAX_NUM; i++)
  425. {
  426. for(j = 0; j < ENCODER_GUN_NUM; j++)
  427. {
  428. if(timeout_isOut(&encoder_record_info[i][j].gunsignal_irq.wave_time))
  429. {
  430. current_level = get_encoder_bit(i,j,GUN_GPIO);//当次有效电平
  431. if(current_level != encoder_record_info[i][j].gunsignal_irq.last_level)
  432. {
  433. //当次电平和上次电平不同
  434. if(current_level)//高电平
  435. {
  436. //当次为高电平,认为是上升沿,挂枪
  437. gun_down_handle(i,j);
  438. printf_debug(ENCODER_GUN_DEBUG, "t=%d, g=%d,gun down\n",i+1,j+1);
  439. }
  440. else
  441. {
  442. //当次为低电平,认为是下降沿,抬枪
  443. gun_up_handle(i,j);
  444. printf_debug(ENCODER_GUN_DEBUG, "t=%d, g=%d,gun up\n",i+1,j+1);
  445. }
  446. }
  447. encoder_record_info[i][j].gunsignal_irq.last_level = current_level;
  448. }
  449. }
  450. }
  451. }
  452. //脉冲信号中断处理函数
  453. void pulse_irq_delay_handle(void)
  454. {
  455. uint8_t i,j;
  456. uint8_t current_level;
  457. for(i = 0; i < ENCODER_TAX_NUM; i++)
  458. {
  459. for(j = 0; j < ENCODER_GUN_NUM; j++)
  460. {
  461. if(timeout_isOut_us(&encoder_record_info[i][j].pulse_irq.wave_time))
  462. {
  463. current_level = get_encoder_bit(i,j,PULSE_GPIO);//当次有效电平
  464. if(current_level != encoder_record_info[i][j].pulse_irq.last_level)
  465. {
  466. //当次电平和上次电平不同
  467. if(current_level)//高电平
  468. {
  469. //当次为高电平,认为是上升沿
  470. pulse_up_handle(i,j);
  471. }
  472. else
  473. {
  474. //当次为低电平,认为是下降沿
  475. }
  476. }
  477. encoder_record_info[i][j].pulse_irq.last_level = current_level;
  478. }
  479. }
  480. }
  481. }
  482. /*
  483. //抬枪次数滤波函数
  484. void gunnum_irq_delay_handle(void)
  485. {
  486. uint8_t i,j;
  487. uint8_t current_level;
  488. for(i = 0; i < ENCODER_TAX_NUM; i++)
  489. {
  490. for(j = 0; j < ENCODER_GUN_NUM; j++)
  491. {
  492. if(timeout_isOut(&encoder_record_info[i][j].gunup_irq.wave_time))
  493. {
  494. current_level = encoder_record_info[i][j].encoder_count_info.gun_signal;//当次枪状态
  495. if(current_level != encoder_record_info[i][j].gunup_irq.last_level)
  496. {
  497. //当次电平和上次电平不同
  498. if(current_level==GUN_UP)//抬枪
  499. {
  500. encoder_record_info[i][j].encoder_count_info.gunup_num++;//抬枪次数++
  501. encoder_record_info[i][j].encoder_count_info.eeprom_write_gunup_flag = 1;//写eeprom标记
  502. }
  503. else
  504. {
  505. }
  506. }
  507. encoder_record_info[i][j].gunup_irq.last_level = current_level;
  508. }
  509. }
  510. }
  511. }
  512. */
  513. uint8_t Tax=0,Gun=0;
  514. //编码器写eeprom
  515. void encoder_eeprom_write_handle(void)
  516. {
  517. uint8_t temp[8] = {0};
  518. uint8_t temp_en[16] = {0};
  519. uint32_t eeprom_addr = 0;
  520. uint32_t id = 0;
  521. if(timeout_isOut(&eeprom_write_time))
  522. {
  523. if(encoder_record_info[Tax][Gun].encoder_count_info.eeprom_write_flag)
  524. {
  525. id = encoder_eeprom[Tax][Gun].id;//记录当前写入的id
  526. //保存总脉冲数(eeprom)
  527. memcpy(temp,&encoder_eeprom[Tax][Gun].id,8);
  528. HM_encode(temp,temp_en,8);
  529. if(Tax==0)
  530. {
  531. eeprom_addr = EEPROM_ENCODER_TAX1_ADDR + Gun*16;
  532. }
  533. else if(Tax==1)
  534. {
  535. eeprom_addr = EEPROM_ENCODER_TAX2_ADDR + Gun*16;
  536. }
  537. AT24CXX_Write(eeprom_addr,(uint8_t *)temp_en,16);
  538. encoder_record_info[Tax][Gun].encoder_count_info.write_id = id;//记录当前写入的id
  539. data_dump_debug(ENCODER_EEPROM_DATA_DEBUG, "write eeprom", temp_en,16);
  540. printf_debug(ENCODER_EEPROM_DEBUG, "写eeprom:addr=0x%x, tax=%d, gun=%d, id=%u, total pulse=%u\n",eeprom_addr,Tax+1,Gun+1,encoder_eeprom[Tax][Gun].id,encoder_eeprom[Tax][Gun].total_pulse);
  541. encoder_record_info[Tax][Gun].encoder_count_info.eeprom_write_flag = 0;
  542. }
  543. //存储空抬枪次数
  544. if(encoder_record_info[Tax][Gun].encoder_count_info.eeprom_write_gunup_flag)
  545. {
  546. encoder_gunnum_ee[Tax][Gun].gunup_num = encoder_record_info[Tax][Gun].encoder_count_info.gunup_num;
  547. //保存总脉冲数(eeprom)
  548. memcpy(temp,(uint8_t *)&encoder_gunnum_ee[Tax][Gun].gunup_num,4);
  549. HM_encode(temp,temp_en,4);
  550. if(Tax==0)
  551. {
  552. eeprom_addr = EEPROM_ENCODER_TAX1_GUNNUM_ADDR + Gun*8;
  553. }
  554. else if(Tax==1)
  555. {
  556. eeprom_addr = EEPROM_ENCODER_TAX2_GUNNUM_ADDR + Gun*8;
  557. }
  558. AT24CXX_Write(eeprom_addr,(uint8_t *)temp_en,8);
  559. printf_debug(1,"写gun num:addr=0x%x, tax=%d, gun=%d, gun num=%u\n",eeprom_addr,Tax+1,Gun+1,encoder_gunnum_ee[Tax][Gun].gunup_num);
  560. encoder_record_info[Tax][Gun].encoder_count_info.eeprom_write_gunup_flag = 0;
  561. }
  562. Gun = (Gun+1)%ENCODER_GUN_NUM;
  563. if(Gun==0)
  564. {
  565. Tax = (Tax+1)%ENCODER_TAX_NUM;
  566. }
  567. }
  568. }
  569. //是否正在加油检测(一个报税口)
  570. //返回:1 正在加油,0 没有加油
  571. uint8_t tax_oil_judge_single(uint8_t tax)
  572. {
  573. uint8_t i,ret = 0;
  574. for(i = 0; i < ENCODER_GUN_NUM; i++)
  575. {
  576. if(encoder_record_info[tax][i].encoder_count_info.pulse_count)
  577. {
  578. ret = 1;
  579. }
  580. }
  581. return ret;
  582. }
  583. //是否正在加油检测(所有枪)
  584. uint8_t tax_oil_judge_all(void)
  585. {
  586. uint8_t tax1,tax2;
  587. uint8_t gun_signal = 0;
  588. tax1 = tax_oil_judge_single(TAX1);
  589. tax2 = tax_oil_judge_single(TAX2);
  590. gun_signal = (tax2<<4)|tax1;
  591. return gun_signal;
  592. }
  593. //抬枪信号检测
  594. uint8_t Gun_Signal_Check(uint8_t tax)
  595. {
  596. int i;
  597. uint8_t gun_signal = 0;
  598. for(i = 0; i < ENCODER_GUN_NUM; i++)
  599. {
  600. if(encoder_record_info[tax][i].gunsignal_irq.gun_signal_is_work == GUN_SIGNAL_DISABLE)//抬枪信号不可用
  601. {
  602. //抬枪信号不可用,脉冲计数不为零,认为当前是抬枪
  603. if(encoder_record_info[tax][i].encoder_count_info.pulse_count)
  604. {
  605. gun_signal |= 1<<i;
  606. }
  607. }
  608. else
  609. {
  610. //抬枪信号可用,抬枪信号为抬枪状态或脉冲计数不为零,认为当前是抬枪
  611. if((encoder_record_info[tax][i].encoder_count_info.gun_signal == GUN_UP) || encoder_record_info[tax][i].encoder_count_info.pulse_count)
  612. {
  613. gun_signal |= 1<<i;
  614. }
  615. }
  616. if(encoder_record_info[tax][i].gunsignal_irq.wave_time.flag)//抬枪信号滤波过程,也不读报税口
  617. {
  618. gun_signal |= 1<<i;
  619. }
  620. }
  621. return gun_signal;
  622. }
  623. //全部抬枪信号判断
  624. uint8_t Gun_Signal_Check_all(void)
  625. {
  626. uint8_t tax1,tax2;
  627. uint8_t gun_signal = 0;
  628. tax1 = Gun_Signal_Check(TAX1);
  629. tax2 = Gun_Signal_Check(TAX2);
  630. gun_signal = (tax2<<4)|tax1;
  631. return gun_signal;
  632. }
  633. //读取时的组号判断
  634. //返回:0 无新数据;1 有新数据。group为有新数据时读取的组号
  635. uint8_t read_group_check(uint8_t tax, uint8_t gun, uint8_t *group, uint8_t order)
  636. {
  637. uint8_t ret = 0;
  638. if(order == 0 || order == 2)//默认读取
  639. {
  640. if(encoder_record_info[tax][gun].encoder_count_info.write_id == encoder_record_info[tax][gun].encoder_count_info.read_id) //无新数据
  641. {
  642. //无新数据,返回最后一笔的交易
  643. *group = encoder_record_info[tax][gun].encoder_count_info.read_group;
  644. ret = 0;
  645. }
  646. else
  647. {
  648. if(encoder_record_info[tax][gun].encoder_count_info.write_id - encoder_record_info[tax][gun].encoder_count_info.read_id > ENCODER_SAVE_GROUP_NUM)
  649. {
  650. //已经有一轮数据未读取,从当前写入组的下一组开始读取
  651. *group = (encoder_record_info[tax][gun].encoder_count_info.write_group+1)%ENCODER_SAVE_GROUP_NUM;
  652. if(encoder_record_info[tax][gun].encoder_count_info.read_first_valie)
  653. {
  654. encoder_record_info[tax][gun].encoder_count_info.read_first_valie = 0;
  655. }
  656. }
  657. else //接着上次读取组号开始读取
  658. {
  659. if(encoder_record_info[tax][gun].encoder_count_info.read_first_valie)//上电读取的第一笔有效数据
  660. {
  661. encoder_record_info[tax][gun].encoder_count_info.read_first_valie = 0;
  662. *group = 0;
  663. }
  664. else
  665. *group = (encoder_record_info[tax][gun].encoder_count_info.read_group+1)%ENCODER_SAVE_GROUP_NUM;
  666. }
  667. ret = 1;
  668. }
  669. encoder_record_info[tax][gun].encoder_count_info.read_group = *group;
  670. encoder_record_info[tax][gun].encoder_count_info.read_id = pulse_up_data[tax][gun][*group].id;
  671. }
  672. else if(order == 1 || order == 3)//读取最新交易
  673. {
  674. if(encoder_record_info[tax][gun].encoder_count_info.write_id == encoder_record_info[tax][gun].encoder_count_info.read_id) //无新数据
  675. {
  676. ret = 0;
  677. }
  678. else
  679. {
  680. ret = 1;
  681. }
  682. *group = encoder_record_info[tax][gun].encoder_count_info.write_group; //新交易为上次写入的组号
  683. encoder_record_info[tax][gun].encoder_count_info.read_group = *group;
  684. encoder_record_info[tax][gun].encoder_count_info.read_id = pulse_up_data[tax][gun][*group].id;
  685. }
  686. return ret;
  687. }
  688. //编码器返回参数(0和1)
  689. uint8_t coll_encoder_return_0_1(uint8_t tax, uint8_t gun, uint8_t order, uint8_t *data)
  690. {
  691. uint8_t len = 0;
  692. uint8_t read_group = 0;
  693. uint8_t new_pulse = 0;
  694. encoder_up_gun_t *encoder_up = (encoder_up_gun_t *)data;
  695. encoder_up->order = order;
  696. encoder_up->tax1_gunstatus = Gun_Signal_Check(TAX1);
  697. encoder_up->tax2_gunstatus = Gun_Signal_Check(TAX2);
  698. //读取编码器的参数
  699. read_encoder_para.tax = tax;
  700. read_encoder_para.gunstatus = Gun_Signal_Check_all();
  701. read_encoder_para.flag = 1;
  702. timeout_setValue(&read_encoder_para.tt_encoder,ENCODER_GUNSTATUS_TIMEOUT,1);
  703. //判断有无新纪录,及查找读取的组号
  704. new_pulse = read_group_check(tax-1,gun-1,&read_group,order);
  705. printf_debug(MSG_ENCODER_DOWN_TYPE3, "read group:%d, new pulse:%d\n",read_group,new_pulse);
  706. encoder_up->new_recode_status = new_pulse;
  707. encoder_up->id = pulse_up_data[tax-1][gun-1][read_group].id;
  708. encoder_up->id_mode = (pulse_up_data[tax-1][gun-1][read_group].pulse_mode)|0x80;//记录产生方式最高位置1
  709. encoder_up->single_pulse_num = pulse_up_data[tax-1][gun-1][read_group].single_pulse_num;
  710. encoder_up->total_pulse_num = pulse_up_data[tax-1][gun-1][read_group].total_pulse_num;
  711. encoder_up->time = encoder_gunnum_ee[tax-1][gun-1].gunup_num;//encoder_gunnum_ee[tax-1][gun-1].gunup_num;//pulse_up_data[tax-1][gun-1][read_group].time;//抬枪次数
  712. len = sizeof(encoder_up_gun_t);
  713. return len;
  714. }
  715. //编码器返回数据(指令2和3)
  716. uint8_t coll_encoder_return_2_3(uint8_t tax, uint8_t gun, uint8_t order, uint8_t *data)
  717. {
  718. uint8_t len = 0;
  719. uint8_t read_group = 0;
  720. uint8_t new_recode_gun = 0;
  721. uint8_t new_recode_tax = 0; //新交易标记
  722. uint8_t mode = 0; //产生记录方式
  723. uint8_t temp[20] = {0};
  724. uint8_t i;
  725. encoder_up_tax_head_t *encoder_head = (encoder_up_tax_head_t *)data;
  726. encoder_up_tax_data_t *encoder_data = (encoder_up_tax_data_t *)temp;
  727. encoder_head->order = order;
  728. encoder_head->tax1_gunstatus = Gun_Signal_Check(TAX1);
  729. encoder_head->tax2_gunstatus = Gun_Signal_Check(TAX2);
  730. //读取编码器的参数
  731. read_encoder_para.tax = tax;
  732. read_encoder_para.gunstatus = Gun_Signal_Check_all();
  733. read_encoder_para.flag = 1;
  734. timeout_setValue(&read_encoder_para.tt_encoder,ENCODER_GUNSTATUS_TIMEOUT,1);
  735. for(i = 0; i < ENCODER_GUN_NUM; i++)
  736. {
  737. new_recode_gun = read_group_check(tax-1, i, &read_group, order);
  738. if(new_recode_gun)
  739. {
  740. new_recode_tax |= 1<<i;
  741. }
  742. encoder_data->id = pulse_up_data[tax-1][i][read_group].id;
  743. encoder_data->single_pulse_num = pulse_up_data[tax-1][i][read_group].single_pulse_num;
  744. encoder_data->total_pulse_num = pulse_up_data[tax-1][i][read_group].total_pulse_num;
  745. encoder_data->time = encoder_gunnum_ee[tax-1][i].gunup_num;//pulse_up_data[tax-1][i][read_group].time;//抬枪次数
  746. if(pulse_up_data[tax-1][i][read_group].pulse_mode)
  747. {
  748. mode |= 1<<i;
  749. }
  750. memcpy(encoder_head->info+(i*sizeof(encoder_up_tax_data_t)),temp,sizeof(encoder_up_tax_data_t));
  751. }
  752. encoder_head->new_recode_status = new_recode_tax;
  753. encoder_head->id_mode = mode|0x80;//记录产生方式最高位置1
  754. encoder_head->gun_num = ENCODER_GUN_NUM;
  755. len = ENCODER_UP_HEAD_NUM + ENCODER_GUN_NUM*sizeof(encoder_up_tax_data_t);
  756. return len;
  757. }
  758. //设备复位判断,编码器没有计数,且已经写eeprom
  759. //返回:0 可以复位,1 不能复位
  760. uint8_t Reset_encoder_judge(void)
  761. {
  762. uint8_t ret = 0;
  763. uint8_t tax1 = 0, tax2 = 0;
  764. uint8_t eeprom = 0;
  765. uint8_t i,j;
  766. tax1 = tax_oil_judge_single(TAX1);
  767. tax2 = tax_oil_judge_single(TAX2);
  768. for(i = 0; i < ENCODER_TAX_NUM; i++)
  769. {
  770. for(j = 0; j < ENCODER_GUN_NUM; j++)
  771. {
  772. if(encoder_record_info[i][j].encoder_count_info.eeprom_write_flag == 1)
  773. {
  774. eeprom = 1;
  775. }
  776. }
  777. }
  778. if(tax1==0 && tax2==0 && eeprom==0)
  779. ret = 0;
  780. else
  781. ret = 1;
  782. return ret;
  783. }