buzzer.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /*
  14. * Buzzer driver for Olimex LPC-P2148.
  15. * Uses the timer 1 for wave generation and a Virtual Timer for the sound
  16. * duration.
  17. * The driver also generates an event when the sound is done and the buzzer
  18. * goes silent.
  19. */
  20. #include "hal.h"
  21. #include "buzzer.h"
  22. EventSource BuzzerSilentEventSource;
  23. #define StartCounter(t) ((t)->TC_EMR = 0xF1, (t)->TC_TCR = 1)
  24. #define StopCounter(t) ((t)->TC_EMR = 0, (t)->TC_TCR = 2)
  25. /**
  26. * @brief Buzzer driver initialization.
  27. */
  28. void buzzInit(void) {
  29. chEvtInit(&BuzzerSilentEventSource);
  30. /*
  31. * Switches P0.12 and P0.13 to MAT1.0 and MAT1.1 functions.
  32. * Enables Timer1 clock.
  33. */
  34. PINSEL0 &= 0xF0FFFFFF;
  35. PINSEL0 |= 0x0A000000;
  36. PCONP = (PCONP & PCALL) | PCTIM1;
  37. /*
  38. * Timer setup.
  39. */
  40. TC *tc = T1Base;
  41. StopCounter(tc);
  42. tc->TC_CTCR = 0; /* Clock source is PCLK. */
  43. tc->TC_PR = 0; /* Prescaler disabled. */
  44. tc->TC_MCR = 2; /* Clear TC on match MR0. */
  45. }
  46. /**
  47. * @brief Stops the sound.
  48. *
  49. * @param[in] p pointer to the timer
  50. */
  51. static void stop(void *p) {
  52. StopCounter((TC *)p);
  53. chSysLockFromIsr();
  54. chEvtBroadcastI(&BuzzerSilentEventSource);
  55. chSysUnlockFromIsr();
  56. }
  57. /**
  58. * @brief Plays a tone asynchronously.
  59. *
  60. * @param[in] freq approximated tone frequency
  61. * @param[in] duration tone duration in systicks
  62. */
  63. void buzzPlay(uint32_t freq, systime_t duration) {
  64. static VirtualTimer bvt;
  65. TC *tc = T1Base;
  66. chSysLock();
  67. if (chVTIsArmedI(&bvt)) { /* If a sound is already being */
  68. chVTResetI(&bvt); /* played then aborts it. */
  69. StopCounter(tc);
  70. }
  71. tc->TC_MR0 = tc->TC_MR1 = (PCLK / (freq * 2));
  72. StartCounter(tc);
  73. chVTSetI(&bvt, duration, stop, tc);
  74. chSysUnlock();
  75. }
  76. /**
  77. * @brief Plays a tone.
  78. *
  79. * @param[in] freq approximated tone frequency
  80. * @param[in] duration tone duration in systicks
  81. */
  82. void buzzPlayWait(uint32_t freq, systime_t duration) {
  83. TC *tc = T1Base;
  84. StopCounter(tc);
  85. tc->TC_MR0 = tc->TC_MR1 = (PCLK / (freq * 2));
  86. StartCounter(tc);
  87. chThdSleep(duration);
  88. StopCounter(tc);
  89. }