beep.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #include "gpio.h"
  2. #include "timer.h"
  3. #include "elog.h"
  4. #include "beep.h"
  5. #include "common.h"
  6. #include <stdio.h>
  7. #include <sys/time.h>
  8. #if 1
  9. #define LOGD log_d
  10. #define LOGE log_e
  11. #else
  12. #define LOGD printf
  13. #define LOGE printf
  14. #endif
  15. #if (CHIP_TYPE == CHIP_T113s)
  16. #define BEEP_GPIO (128 + 0)
  17. #else
  18. #define BEEP_GPIO 103 /* PD7=96+7 */
  19. #endif
  20. #define BEEP_ON_LEVEL 1
  21. #define BEEP_OFF_LEVEL 0
  22. static beep_para_t beep_para[BEEP_MODE_MAX]={
  23. #if 0
  24. {1000, 1000, 5}, //BEEP_MODE_WARN1
  25. {1000, 1000, 5}, //BEEP_MODE_WARN1
  26. {500, 500, 10}, //BEEP_MODE_ERROR
  27. {200, 200, 25}, //BEEP_MODE_FAULT
  28. {50, 50, -1}, //BEEP_MODE_FATAL
  29. #else
  30. {500, 500, 300}, //BEEP_MODE_WARN1
  31. {333, 333, 450}, //BEEP_MODE_WARN2
  32. {500, 500, 10}, //BEEP_MODE_ERROR
  33. {200, 200, 25}, //BEEP_MODE_FAULT
  34. {50, 50, -1}, //BEEP_MODE_FATAL
  35. #endif
  36. };
  37. typedef struct {
  38. int io;
  39. int tmr;
  40. uint32_t cnt;
  41. beep_para_t para;
  42. int mode;
  43. int stop_level;
  44. }beep_handle_t;
  45. static beep_handle_t bpHandle={0};
  46. static inline MiscInfo_t* get_misc(void)
  47. {
  48. return &__globalDeviceManage._globalDevInfo.misc;
  49. }
  50. static void beep_handler(void *arg, int last)
  51. {
  52. beep_handle_t *h=(beep_handle_t*)arg;
  53. if(h->para.on_ms>0 && h->para.off_ms>0) {
  54. h->cnt++;
  55. if(h->cnt%2) {
  56. gpio_set(h->io, BEEP_OFF_LEVEL);
  57. }
  58. else {
  59. gpio_set(h->io, BEEP_ON_LEVEL);
  60. }
  61. }
  62. if(last) {
  63. timer_stop(h->tmr);
  64. gpio_set(h->io, BEEP_OFF_LEVEL);
  65. }
  66. }
  67. /////////////////////////////////////////////
  68. int beep_init(void)
  69. {
  70. beep_handle_t *h=&bpHandle;
  71. h->io = gpio_init(BEEP_GPIO, GPIO_OUT);
  72. h->tmr = timer_init();
  73. gpio_set(h->io, BEEP_ON_LEVEL);
  74. sleep(1);
  75. gpio_set(h->io, BEEP_OFF_LEVEL);
  76. h->mode = -1;
  77. //beep_set(BEEP_MODE_FATAL);
  78. return 0;
  79. }
  80. int beep_deinit(void)
  81. {
  82. beep_handle_t *h=&bpHandle;
  83. gpio_deinit(h->io);
  84. timer_deinit(h->tmr);
  85. h->io = -1;
  86. h->tmr = -1;
  87. return 0;
  88. }
  89. int beep_set(int mode)
  90. {
  91. beep_handle_t *h=&bpHandle;
  92. h->mode = mode;
  93. return beep_set_ex(&beep_para[h->mode]);
  94. }
  95. int beep_set_ex(beep_para_t *para)
  96. {
  97. int r = 0;
  98. beep_handle_t *h=&bpHandle;
  99. MiscInfo_t* misc=get_misc();
  100. if(!misc->beep || !para || (para->on_ms<=0 && para->off_ms<=0) || para->times==0) {
  101. return -1;
  102. }
  103. h->cnt = 0;
  104. h->para = *para;
  105. if(h->para.on_ms>0) {
  106. gpio_set(h->io, BEEP_ON_LEVEL);
  107. if(h->para.off_ms>0) {
  108. h->para.times *= 2;
  109. }
  110. else {
  111. h->para.on_ms *= h->para.times;
  112. h->para.times = 1;
  113. }
  114. r = timer_start(h->tmr, h->para.on_ms, h->para.times, beep_handler, h);
  115. }
  116. else {
  117. gpio_set(h->io, BEEP_OFF_LEVEL);
  118. if(h->para.off_ms>0) {
  119. h->para.off_ms *= h->para.times;
  120. h->para.times = 1;
  121. r = timer_start(h->tmr, h->para.off_ms, h->para.times, beep_handler, h);
  122. }
  123. }
  124. return r;
  125. }
  126. int beep_reset(void)
  127. {
  128. beep_handle_t *h=&bpHandle;
  129. if(h->mode==-1) {
  130. return -1;
  131. }
  132. h->mode = -1;
  133. timer_stop(h->tmr);
  134. gpio_set(h->io, BEEP_OFF_LEVEL);
  135. return 0;
  136. }