drv_trigger.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #include "drv_trigger.h"
  2. #include "bsp_time.h"
  3. #include "selfcheck.h"
  4. static volatile uint32_t trg = 0;
  5. static uint16_t dat = 0;
  6. static uint8_t dat_dex = 15;
  7. void drv_TrigNBit(uint8_t cnt,uint8_t flag)
  8. {
  9. int16_t _cnt = cnt-10;
  10. while(_cnt>0){
  11. _cnt = _cnt-30;
  12. if(flag>0){
  13. dat |= (0x0001<<dat_dex);
  14. }else{
  15. dat &= ~(0x0001<<dat_dex);
  16. }
  17. if(dat_dex>0) dat_dex--;
  18. }
  19. // SEGGER_RTT_printf(0,"dat=%04X(%d)\n",dat,dat_dex);
  20. }
  21. uint32_t drv_GetChargeTrig(void){
  22. return trg;
  23. }
  24. void drv_TrigProcess(void* t)
  25. {
  26. static uint8_t state = 0;
  27. static uint8_t cnt[2];
  28. static uint32_t trg_b = 0;
  29. static uint32_t dt_b = 0;
  30. uint32_t dt = *((uint32_t*)t);
  31. trg = nrf_gpio_pin_read(PIN_CHARGING);
  32. if(dt - dt_b > 1)
  33. {
  34. // SEGGER_RTT_printf(0,"-------------------------------------------->\r\n");
  35. }
  36. if(dt-dt_b==1){
  37. if(trg==0&&trg_b==0){ //低
  38. cnt[0]++;
  39. }else if(trg>0&&trg_b>0){ //高
  40. cnt[1]++;
  41. }else if(trg==0&&trg_b>0){ //高变低
  42. // SEGGER_RTT_printf(0,"H->L:cnt[0]=%d,cnt[1]=%d \n",cnt[0],cnt[1]);
  43. if(cnt[1]>37&&cnt[1]<53 && state==2){ //结束
  44. state = 0;
  45. if(cnt[0]>50) drv_TrigNBit(cnt[0]-50,0);
  46. uint8_t trg_dat = (uint8_t)(dat>>8);
  47. uint8_t trg_dat_f = ~((uint8_t)(dat>>0));
  48. //调用触发函数
  49. if(trg_dat>0&&trg_dat<4&&trg_dat==trg_dat_f){
  50. selfcheck_trigger_set_order(trg_dat);
  51. // SEGGER_RTT_printf(0,"******** selfcheck_trigger_set_order **********\n");
  52. }
  53. // SEGGER_RTT_printf(0,"trg_dat=%02X,trg_dat_f=%02X \n\n",trg_dat,trg_dat_f);
  54. // SEGGER_RTT_printf(0,"******** stop **********\n");
  55. }
  56. if(state==2){ //接收数据
  57. drv_TrigNBit(cnt[0],0);
  58. drv_TrigNBit(cnt[1],1);
  59. // SEGGER_RTT_printf(0,"******** data **********\n");
  60. }
  61. if(cnt[0]>37&&cnt[0]<53&&cnt[1]>38&&cnt[1]<53 && state==1){ //开始
  62. state = 2;
  63. dat = 0;
  64. dat_dex = 15;
  65. // SEGGER_RTT_printf(0,"******** start **********\n");
  66. }
  67. if(cnt[0]>90&&cnt[0]<110&&cnt[1]>10&&cnt[1]<180){ //准备开始
  68. state = 1;
  69. // SEGGER_RTT_printf(0,"******** ready **********\n");
  70. }
  71. cnt[0] = 0;cnt[1] = 0;
  72. }
  73. }
  74. trg_b = trg;
  75. dt_b = dt;
  76. }
  77. void drv_TriggerInit(void)
  78. {
  79. nrf_gpio_cfg_input(PIN_CHARGING,NRF_GPIO_PIN_NOPULL);
  80. TIME_Regist(drv_TrigProcess);
  81. }