hal_mt.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. void gpio_mt_run(uint32_t tim)
  26. {
  27. MT_Run(tim);
  28. }
  29. void MT_process(void)
  30. {
  31. }
  32. void MT_TimerCounter(void* t)
  33. {
  34. static uint32_t low_level = 0;
  35. static uint32_t hight_level = 0;
  36. if(mt_time>0)
  37. {
  38. if(mt_time >=HeartTime_Interval)
  39. {
  40. mt_time -=HeartTime_Interval;
  41. if(hight_level != MT_DUTY_CYCLE_H)
  42. {
  43. nrf_gpio_pin_write(PIN_MT_EN,1);
  44. hight_level++;
  45. }
  46. else if(low_level != MT_DUTY_CYCLE_L)
  47. {
  48. low_level++;
  49. nrf_gpio_pin_write(PIN_MT_EN,0);
  50. }
  51. if(hight_level == MT_DUTY_CYCLE_H && low_level == MT_DUTY_CYCLE_L)
  52. {
  53. low_level = 0;
  54. hight_level = 0;
  55. }
  56. }
  57. else
  58. {
  59. mt_time=0;
  60. low_level = 0;
  61. hight_level = 0;
  62. }
  63. if(mt_time==0)
  64. {
  65. nrf_gpio_pin_write(PIN_MT_EN,0);
  66. Process_SetHoldOn(MT_process,0);
  67. }
  68. }
  69. }
  70. void cb_BLE_Client_R_SHOCK(void* handle)
  71. {
  72. BLE_Client_Rx_t *target = handle;
  73. uint16_t t = ((uint16_t)(target->pDat[0])<<8) | ((uint16_t)(target->pDat[1])<<0);
  74. if(mFlash.isHost)
  75. {
  76. if(target->pDat[2]==1){
  77. MT_Run(t);
  78. }
  79. else if(target->pDat[2]==0){
  80. MT_Run(t);
  81. BLE_Host_Tx_Send(0,BLE_SHOCK,target->pDat,target->datLen);
  82. }
  83. else{
  84. BLE_Host_Tx_Send(0,BLE_SHOCK,target->pDat,target->datLen);
  85. }
  86. }
  87. else
  88. {
  89. if((target->pDat[2]==2)||(target->pDat[2]==0))
  90. {
  91. MT_Run(t);
  92. }
  93. }
  94. DEBUG_LOG("MT RUN:%d \n",t);
  95. }
  96. void MT_Init(void)
  97. {
  98. nrf_gpio_cfg_output(PIN_MT_EN); nrf_gpio_pin_write(PIN_MT_EN,0); MT_Run(100);
  99. TIME_Regist(MT_TimerCounter);
  100. Process_Start(0,"MT_process",MT_process);
  101. Process_SetHoldOn(MT_process,1);
  102. BLE_Client_Rx_Regist(BLE_SHOCK,cb_BLE_Client_R_SHOCK);
  103. }