beep.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. * Copyright (c) 2006-2020, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-05-23 Administrator the first version
  9. */
  10. #include <rtdevice.h> //使用 RT-Thread 的设备需要包含此头文件
  11. #include <beep.h>
  12. #include <control.h>
  13. #include "hmi.h"
  14. #define DBG_ENABLE
  15. #define DBG_COLOR
  16. #define DBG_TAG "pwm"
  17. #define DBG_LEVEL DBG_LOG
  18. #include <rtdbg.h>
  19. #define HWTIMER_DEV_NAME "timer2" /* 定时器名称 */
  20. Device_Status_typedef device_status;
  21. struct rt_device_pwm *pwm_device = RT_NULL; //定义 pwm 设备指针
  22. struct rt_device_pwm *pwm_tim8 = RT_NULL; //定义 pwm 设备指针
  23. /* 定时器超时回调函数 */
  24. static rt_err_t timeout_cb(rt_device_t dev, rt_size_t size)
  25. {
  26. rt_kprintf("this is hwtimer timeout callback fucntion!\n");
  27. rt_kprintf("tick is :%d !\n", rt_tick_get());
  28. rt_pwm_disable(pwm_device, BEEP_PWM_CH);
  29. rt_device_close(dev);
  30. return 0;
  31. }
  32. rt_device_t hw_dev = RT_NULL; /* 定时器设备句柄 */
  33. static int hwtimer_sample(int argc, char *argv[])
  34. {
  35. rt_err_t ret = RT_EOK;
  36. rt_hwtimerval_t timeout_s; /* 定时器超时值 */
  37. rt_device_t hw_dev = RT_NULL; /* 定时器设备句柄 */
  38. rt_hwtimer_mode_t mode; /* 定时器模式 */
  39. rt_uint32_t freq = 10000; /* 计数频率 */
  40. /* 查找定时器设备 */
  41. hw_dev = rt_device_find(HWTIMER_DEV_NAME);
  42. if (hw_dev == RT_NULL)
  43. {
  44. rt_kprintf("hwtimer sample run failed! can't find %s device!\n", HWTIMER_DEV_NAME);
  45. return RT_ERROR;
  46. }
  47. /* 以读写方式打开设备 */
  48. ret = rt_device_open(hw_dev, RT_DEVICE_OFLAG_RDWR);
  49. if (ret != RT_EOK)
  50. {
  51. rt_kprintf("open %s device failed!\n", HWTIMER_DEV_NAME);
  52. return ret;
  53. }
  54. /* 设置超时回调函数 */
  55. rt_device_set_rx_indicate(hw_dev, timeout_cb);
  56. /* 设置计数频率(若未设置该项,默认为1Mhz 或 支持的最小计数频率) */
  57. rt_device_control(hw_dev, HWTIMER_CTRL_FREQ_SET, &freq);
  58. /* 设置模式为周期性定时器(若未设置,默认是HWTIMER_MODE_ONESHOT)*/
  59. mode = HWTIMER_MODE_PERIOD; //HWTIMER_MODE_ONESHOT;
  60. ret = rt_device_control(hw_dev, HWTIMER_CTRL_MODE_SET, &mode);
  61. if (ret != RT_EOK)
  62. {
  63. rt_kprintf("set mode failed! ret is :%d\n", ret);
  64. return ret;
  65. }
  66. /* 设置定时器超时值为5s并启动定时器 */
  67. timeout_s.sec = 10; /* 秒 */
  68. timeout_s.usec = 0; /* 微秒 */
  69. if (rt_device_write(hw_dev, 0, &timeout_s, sizeof(timeout_s)) != sizeof(timeout_s))
  70. {
  71. rt_kprintf("set timeout value failed\n");
  72. return RT_ERROR;
  73. }
  74. //beep_init();
  75. hwtimer2_pwm3_output(0,20000,1,1);
  76. return ret;
  77. }
  78. /*初始化蜂鸣器*/
  79. int beep_init(void)
  80. {
  81. /*查找PWM设备*/
  82. pwm_device = (struct rt_device_pwm *)rt_device_find(BEEP_PWM_DEVICE);
  83. if(pwm_device==RT_NULL)
  84. {
  85. rt_kprintf("pwm device %s not found!\n", BEEP_PWM_DEVICE);
  86. return -RT_ERROR;
  87. }
  88. return 0;
  89. }
  90. static int init_pwm3(void)
  91. {
  92. /*查找PWM设备*/
  93. pwm_device = (struct rt_device_pwm *)rt_device_find(BEEP_PWM_DEVICE);
  94. if(pwm_device==RT_NULL)
  95. {
  96. rt_kprintf("pwm device %s not found!\n", BEEP_PWM_DEVICE);
  97. return -RT_ERROR;
  98. }
  99. return 0;
  100. }
  101. static int init_tim2(void)
  102. {
  103. rt_err_t ret = RT_EOK;
  104. rt_hwtimerval_t timeout_s; /* 定时器超时值 */
  105. rt_device_t hw_dev = RT_NULL; /* 定时器设备句柄 */
  106. rt_hwtimer_mode_t mode; /* 定时器模式 */
  107. rt_uint32_t freq = 10000; /* 计数频率 */
  108. /* 查找定时器设备 */
  109. hw_dev = rt_device_find(HWTIMER_DEV_NAME);
  110. if (hw_dev == RT_NULL)
  111. {
  112. rt_kprintf("hwtimer sample run failed! can't find %s device!\n", HWTIMER_DEV_NAME);
  113. return RT_ERROR;
  114. }
  115. /* 以读写方式打开设备 */
  116. ret = rt_device_open(hw_dev, RT_DEVICE_OFLAG_RDWR);
  117. if (ret != RT_EOK)
  118. {
  119. rt_kprintf("open %s device failed!\n", HWTIMER_DEV_NAME);
  120. return ret;
  121. }
  122. /* 设置超时回调函数 */
  123. rt_device_set_rx_indicate(hw_dev, timeout_cb);
  124. /* 设置计数频率(若未设置该项,默认为1Mhz 或 支持的最小计数频率) */
  125. rt_device_control(hw_dev, HWTIMER_CTRL_FREQ_SET, &freq);
  126. /* 设置模式为周期性定时器(若未设置,默认是HWTIMER_MODE_ONESHOT)*/
  127. mode = HWTIMER_MODE_ONESHOT; //HWTIMER_MODE_ONESHOT;
  128. ret = rt_device_control(hw_dev, HWTIMER_CTRL_MODE_SET, &mode);
  129. if (ret != RT_EOK)
  130. {
  131. rt_kprintf("set mode failed! ret is :%d\n", ret);
  132. return ret;
  133. }
  134. /* 设置定时器超时值为5s并启动定时器 */
  135. timeout_s.sec = 1; /* 秒 */
  136. timeout_s.usec = 100000; /* 微秒 */ //100ms
  137. if (rt_device_write(hw_dev, 0, &timeout_s, sizeof(timeout_s)) != sizeof(timeout_s))
  138. {
  139. rt_kprintf("set timeout value failed\n");
  140. return RT_ERROR;
  141. }
  142. return 0;
  143. }
  144. int hwtimer2_pwm3_output(rt_uint32_t time,rt_uint32_t freq,uint8_t EN,uint8_t DIR)
  145. {
  146. init_pwm3();
  147. rt_pin_mode(Motor_EN, PIN_MODE_OUTPUT);
  148. rt_pin_mode(Motor_DIR, PIN_MODE_OUTPUT);
  149. rt_pin_write(Motor_EN, EN);
  150. rt_pin_write(Motor_DIR, DIR);
  151. rt_pwm_set(pwm_device, BEEP_PWM_CH, freq, freq/2);//channel,period,pulse
  152. if(init_tim2()==0)
  153. rt_pwm_enable(pwm_device, BEEP_PWM_CH); //使能蜂鸣器对应的 PWM 通道
  154. return 0;
  155. }
  156. static void stop_motor_86(void)
  157. {
  158. rt_kprintf("stop_motor_86!\n");
  159. rt_pwm_disable(pwm_device, BEEP_PWM_CH);
  160. }
  161. void pin_motor_86_bind(void)
  162. {
  163. rt_pin_mode(MOTOR_86, PIN_MODE_INPUT);
  164. /* 绑定中断,下降沿模式,回调函数名为 */
  165. rt_pin_attach_irq(MOTOR_86, PIN_IRQ_MODE_FALLING, stop_motor_86, RT_NULL);
  166. /* 使能中断 */
  167. rt_pin_irq_enable(MOTOR_86, PIN_IRQ_ENABLE);
  168. }
  169. void pin_motor_86_unbind(void)
  170. {
  171. rt_pin_detach_irq(MOTOR_86);
  172. }
  173. /*打开蜂鸣器*/
  174. int pwm_on(uint8_t pwn_ch,rt_uint32_t pwm_freq)
  175. {
  176. rt_pwm_set(pwm_device, pwn_ch, pwm_freq, pwm_freq/2);
  177. rt_pwm_enable(pwm_device, pwn_ch); //使能蜂鸣器对应的 PWM 通道
  178. return 0;
  179. }
  180. /*关闭蜂鸣器*/
  181. int pwm_off(uint8_t pwn_ch,rt_uint32_t pwm_freq)
  182. {
  183. rt_pwm_set(pwm_device, pwn_ch, pwm_freq, 0);
  184. rt_pwm_disable(pwm_device, pwn_ch); //使能蜂鸣器对应的 PWM 通道
  185. return 0;
  186. }
  187. int pwm8_init(void)
  188. {
  189. pwm_tim8 = (struct rt_device_pwm *)rt_device_find("pwm8");
  190. if(pwm_tim8==RT_NULL)
  191. {
  192. rt_kprintf("pwm device pwm8 not found!\n");
  193. return -RT_ERROR;
  194. }
  195. return 0;
  196. }
  197. /*打开蜂鸣器*/
  198. int beep_on(void)
  199. {
  200. rt_pwm_enable(pwm_device, BEEP_PWM_CH); //使能蜂鸣器对应的 PWM 通道
  201. return 0;
  202. }
  203. /*关闭蜂鸣器*/
  204. int beep_off(void)
  205. {
  206. rt_pwm_disable(pwm_device, BEEP_PWM_CH); //失能蜂鸣器对应的 PWM 通道
  207. return 0;
  208. }
  209. /*在蜂鸣器设定函数里需要设定 蜂鸣器的频率和音量。PWM 设备设定的 API 是设定 周期 和 脉宽 的,
  210. * 因此我们需要转换一下,将频率装换成周期,将音量大小(占空比)转换成脉宽。这里的周期的单位是 ns,
  211. * 因此转换成的 秒 之后还要乘以 10^9 。由于无源蜂鸣器一般是低电平触发的,因此音量越大对应的脉宽就要越小。
  212. * 然后,将转换后的数据传入 rt_pwm_set 来设置 PWM 设备的参数。
  213. *
  214. */
  215. void beep_set(uint32_t freq, uint32_t volume)
  216. {
  217. rt_uint32_t period, pulse;
  218. beep_init();
  219. /* 将频率转化为周期 周期单位:ns 频率单位:HZ */
  220. //period = 1000000000 / freq; //unit:ns 1/HZ*10^9 = ns
  221. period = freq; /* 周期为0.5ms,单位为纳秒500000ns-200k */
  222. /* 根据声音大小计算占空比 蜂鸣器低电平触发 */
  223. pulse = volume; //5000
  224. if(freq==0)
  225. {
  226. rt_pwm_disable(pwm_device, BEEP_PWM_CH);
  227. }else {
  228. /* 利用 PWM API 设定 周期和占空比 */
  229. rt_pwm_set(pwm_device, BEEP_PWM_CH, period, pulse);//channel,period,pulse
  230. rt_pwm_enable(pwm_device, BEEP_PWM_CH);
  231. }
  232. }
  233. void Beep_Tip(uint16_t volnum)
  234. {
  235. rt_pin_mode(Beep, PIN_MODE_OUTPUT);
  236. rt_pin_write(Beep, 1);
  237. rt_thread_mdelay(volnum);
  238. rt_pin_write(Beep, 0);
  239. }
  240. void Motor_Juandai(uint8_t on_off)
  241. {
  242. pwm8_init();
  243. rt_pwm_set(pwm_tim8, PC7_TIM3_CH, 200000, 100000);//channel,period,pulse
  244. //
  245. if(on_off==ON){
  246. rt_pwm_enable(pwm_tim8, PC7_TIM3_CH);
  247. }
  248. else {
  249. rt_pwm_disable(pwm_tim8, PC7_TIM3_CH);
  250. }
  251. }
  252. void Motor_Qingsao_R_PWM(uint8_t on_off)
  253. {
  254. pwm8_init();
  255. rt_pwm_set(pwm_tim8, PC7_TIM3_CH, 200000, 150000);//channel,period,pulse
  256. if(on_off==ON){
  257. rt_pwm_enable(pwm_tim8, PC7_TIM3_CH);
  258. }
  259. else {
  260. rt_pwm_disable(pwm_tim8, PC7_TIM3_CH);
  261. }
  262. }
  263. void Motor_Qingsao_L_PWM(uint8_t on_off)
  264. {
  265. pwm8_init();
  266. rt_pwm_set(pwm_tim8, PC8_TIM3_CH, 200000, 150000);//channel,period,pulse
  267. if(on_off==ON)
  268. {
  269. rt_pwm_enable(pwm_tim8, PC8_TIM3_CH);
  270. }
  271. else {
  272. rt_pwm_disable(pwm_tim8, PC8_TIM3_CH);
  273. }
  274. }
  275. void Motor_shake(uint8_t status_shake,uint16_t shake_sec)
  276. {
  277. rt_uint16_t turn_cnt = 0;
  278. beep_init();
  279. rt_pwm_set(pwm_device, PB1_TIM3_CH, 200000, 100000);//channel,period,pulse
  280. rt_pin_mode(LIMIT_90, PIN_MODE_INPUT);
  281. rt_pwm_enable(pwm_device, PB1_TIM3_CH);
  282. while(rt_pin_read(LIMIT_90)==0){
  283. rt_thread_delay(100);
  284. turn_cnt++;
  285. if(turn_cnt==shake_sec){
  286. LOG_E("电机异常,不转?限位异常,持续触发?");
  287. HMI_WRITE("sysTest.switchClear.val=%d",0);
  288. break;
  289. }
  290. }
  291. if(turn_cnt==shake_sec) return;
  292. turn_cnt = 0;
  293. while(rt_pin_read(LIMIT_90)==1){
  294. turn_cnt++;
  295. rt_thread_delay(50);
  296. if(turn_cnt==shake_sec){
  297. LOG_E("检测不到限位,超时退出?");
  298. break;
  299. }
  300. }
  301. //停
  302. rt_pwm_disable(pwm_device, PB1_TIM3_CH);
  303. HMI_WRITE("sysTest.switchClear.val=%d",0);
  304. }
  305. /* 导出到 msh 命令列表中 */
  306. MSH_CMD_EXPORT(hwtimer_sample, hwtimer sample);