hal_mt.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include "hal_mt.h"
  2. #include "nrf_gpio.h"
  3. #include "usr_config.h"
  4. #include "bsp_time.h"
  5. #include "system.h"
  6. #include "hal_ble_client.h"
  7. #include "hal_ble_host.h"
  8. #include "app_flash.h"
  9. #define MT_DUTY_CYCLE_H 8
  10. #define MT_DUTY_CYCLE_L 2
  11. /************************ º¯ÊýÉùÃ÷ ***********************************/
  12. void MT_process(void);
  13. /************************ ±äÁ¿ ***********************************/
  14. /************************ º¯Êý¶¨Òå ***********************************/
  15. static uint32_t mt_time = 0;
  16. void MT_Run(uint32_t tim)
  17. {
  18. DEBUG_LOG("MT MT_Run:%d \n",tim);
  19. if(tim>0){
  20. mt_time = tim;
  21. nrf_gpio_pin_write(PIN_MT_EN,1);
  22. Process_SetHoldOn(MT_process,1);
  23. }
  24. }
  25. bool MT_GetState(void)
  26. {
  27. if( 0 != mt_time)return true;
  28. return false;
  29. }
  30. void gpio_mt_run(uint32_t tim)
  31. {
  32. MT_Run(tim);
  33. }
  34. void MT_process(void)
  35. {
  36. }
  37. void MT_TimerCounter(void* t)
  38. {
  39. static uint32_t low_level = 0;
  40. static uint32_t hight_level = 0;
  41. if(mt_time>0)
  42. {
  43. if(mt_time >=HeartTime_Interval)
  44. {
  45. mt_time -=HeartTime_Interval;
  46. if(hight_level != MT_DUTY_CYCLE_H)
  47. {
  48. nrf_gpio_pin_write(PIN_MT_EN,1);
  49. hight_level++;
  50. }
  51. else if(low_level != MT_DUTY_CYCLE_L)
  52. {
  53. low_level++;
  54. nrf_gpio_pin_write(PIN_MT_EN,0);
  55. }
  56. if(hight_level == MT_DUTY_CYCLE_H && low_level == MT_DUTY_CYCLE_L)
  57. {
  58. low_level = 0;
  59. hight_level = 0;
  60. }
  61. }
  62. else
  63. {
  64. mt_time=0;
  65. low_level = 0;
  66. hight_level = 0;
  67. }
  68. if(mt_time==0)
  69. {
  70. nrf_gpio_pin_write(PIN_MT_EN,0);
  71. Process_SetHoldOn(MT_process,0);
  72. }
  73. }
  74. }
  75. void cb_BLE_Client_R_SHOCK(void* handle)
  76. {
  77. BLE_Client_Rx_t *target = handle;
  78. uint16_t t = ((uint16_t)(target->pDat[0])<<8) | ((uint16_t)(target->pDat[1])<<0);
  79. if(mFlash.isHost)
  80. {
  81. if(target->pDat[2]==1){
  82. MT_Run(t);
  83. }
  84. else if(target->pDat[2]==0){
  85. MT_Run(t);
  86. BLE_Host_Tx_Send(0,BLE_SHOCK,target->pDat,target->datLen);
  87. }
  88. else{
  89. BLE_Host_Tx_Send(0,BLE_SHOCK,target->pDat,target->datLen);
  90. }
  91. }
  92. else
  93. {
  94. if((target->pDat[2]==2)||(target->pDat[2]==0))
  95. {
  96. MT_Run(t);
  97. }
  98. }
  99. DEBUG_LOG("MT RUN:%d \n",t);
  100. }
  101. void MT_Init(void)
  102. {
  103. nrf_gpio_cfg_output(PIN_MT_EN); nrf_gpio_pin_write(PIN_MT_EN,0); MT_Run(200);
  104. TIME_Regist(MT_TimerCounter);
  105. Process_Start(0,"MT",MT_process);
  106. Process_SetHoldOn(MT_process,1);
  107. BLE_Client_Rx_Regist(BLE_SHOCK,cb_BLE_Client_R_SHOCK);
  108. }