audio_pwm_hardware.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* Copyright 2020 Jack Humbert
  2. * Copyright 2020 JohSchneider
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /*
  18. Audio Driver: PWM
  19. the duty-cycle is always kept at 50%, and the pwm-period is adjusted to match the frequency of a note to be played back.
  20. this driver uses the chibios-PWM system to produce a square-wave on specific output pins that are connected to the PWM hardware.
  21. The hardware directly toggles the pin via its alternate function. see your MCUs data-sheet for which pin can be driven by what timer - looking for TIMx_CHy and the corresponding alternate function.
  22. */
  23. #include "audio.h"
  24. #include "ch.h"
  25. #include "hal.h"
  26. #if !defined(AUDIO_PIN)
  27. # error "Audio feature enabled, but no pin selected - see docs/feature_audio under the ARM PWM settings"
  28. #endif
  29. extern bool playing_note;
  30. extern bool playing_melody;
  31. extern uint8_t note_timbre;
  32. static PWMConfig pwmCFG = {
  33. .frequency = 100000, /* PWM clock frequency */
  34. // CHIBIOS-BUG? can't set the initial period to <2, or the pwm (hard or software) takes ~130ms with .frequency=500000 for a pwmChangePeriod to take effect; with no output=silence in the meantime
  35. .period = 2, /* initial PWM period (in ticks) 1S (1/10kHz=0.1mS 0.1ms*10000 ticks=1S) */
  36. .callback = NULL, /* no callback, the hardware directly toggles the pin */
  37. .channels =
  38. {
  39. #if AUDIO_PWM_CHANNEL == 4
  40. {PWM_OUTPUT_DISABLED, NULL}, /* channel 0 -> TIMx_CH1 */
  41. {PWM_OUTPUT_DISABLED, NULL}, /* channel 1 -> TIMx_CH2 */
  42. {PWM_OUTPUT_DISABLED, NULL}, /* channel 2 -> TIMx_CH3 */
  43. {PWM_OUTPUT_ACTIVE_HIGH, NULL} /* channel 3 -> TIMx_CH4 */
  44. #elif AUDIO_PWM_CHANNEL == 3
  45. {PWM_OUTPUT_DISABLED, NULL},
  46. {PWM_OUTPUT_DISABLED, NULL},
  47. {PWM_OUTPUT_ACTIVE_HIGH, NULL}, /* TIMx_CH3 */
  48. {PWM_OUTPUT_DISABLED, NULL}
  49. #elif AUDIO_PWM_CHANNEL == 2
  50. {PWM_OUTPUT_DISABLED, NULL},
  51. {PWM_OUTPUT_ACTIVE_HIGH, NULL}, /* TIMx_CH2 */
  52. {PWM_OUTPUT_DISABLED, NULL},
  53. {PWM_OUTPUT_DISABLED, NULL}
  54. #else /*fallback to CH1 */
  55. {PWM_OUTPUT_ACTIVE_HIGH, NULL}, /* TIMx_CH1 */
  56. {PWM_OUTPUT_DISABLED, NULL},
  57. {PWM_OUTPUT_DISABLED, NULL},
  58. {PWM_OUTPUT_DISABLED, NULL}
  59. #endif
  60. },
  61. };
  62. static float channel_1_frequency = 0.0f;
  63. void channel_1_set_frequency(float freq) {
  64. channel_1_frequency = freq;
  65. if (freq <= 0.0) // a pause/rest has freq=0
  66. return;
  67. pwmcnt_t period = (pwmCFG.frequency / freq);
  68. pwmChangePeriod(&AUDIO_PWM_DRIVER, period);
  69. pwmEnableChannel(&AUDIO_PWM_DRIVER, AUDIO_PWM_CHANNEL - 1,
  70. // adjust the duty-cycle so that the output is for 'note_timbre' duration HIGH
  71. PWM_PERCENTAGE_TO_WIDTH(&AUDIO_PWM_DRIVER, (100 - note_timbre) * 100));
  72. }
  73. float channel_1_get_frequency(void) { return channel_1_frequency; }
  74. void channel_1_start(void) {
  75. pwmStop(&AUDIO_PWM_DRIVER);
  76. pwmStart(&AUDIO_PWM_DRIVER, &pwmCFG);
  77. }
  78. void channel_1_stop(void) { pwmStop(&AUDIO_PWM_DRIVER); }
  79. static void gpt_callback(GPTDriver *gptp);
  80. GPTConfig gptCFG = {
  81. /* a whole note is one beat, which is - per definition in musical_notes.h - set to 64
  82. the longest note is BREAVE_DOT=128+64=192, the shortest SIXTEENTH=4
  83. the tempo (which might vary!) is in bpm (beats per minute)
  84. therefore: if the timer ticks away at .frequency = (60*64)Hz,
  85. and the .interval counts from 64 downwards - audio_update_state is
  86. called just often enough to not miss any notes
  87. */
  88. .frequency = 60 * 64,
  89. .callback = gpt_callback,
  90. };
  91. void audio_driver_initialize(void) {
  92. pwmStart(&AUDIO_PWM_DRIVER, &pwmCFG);
  93. // connect the AUDIO_PIN to the PWM hardware
  94. #if defined(USE_GPIOV1) // STM32F103C8
  95. palSetLineMode(AUDIO_PIN, PAL_MODE_ALTERNATE_PUSHPULL);
  96. #else // GPIOv2 (or GPIOv3 for f4xx, which is the same/compatible at this command)
  97. palSetLineMode(AUDIO_PIN, PAL_MODE_ALTERNATE(AUDIO_PWM_PAL_MODE));
  98. #endif
  99. gptStart(&AUDIO_STATE_TIMER, &gptCFG);
  100. }
  101. void audio_driver_start(void) {
  102. channel_1_stop();
  103. channel_1_start();
  104. if (playing_note || playing_melody) {
  105. gptStartContinuous(&AUDIO_STATE_TIMER, 64);
  106. }
  107. }
  108. void audio_driver_stop(void) {
  109. channel_1_stop();
  110. gptStopTimer(&AUDIO_STATE_TIMER);
  111. }
  112. /* a regular timer task, that checks the note to be currently played
  113. * and updates the pwm to output that frequency
  114. */
  115. static void gpt_callback(GPTDriver *gptp) {
  116. float freq; // TODO: freq_alt
  117. if (audio_update_state()) {
  118. freq = audio_get_processed_frequency(0); // freq_alt would be index=1
  119. channel_1_set_frequency(freq);
  120. }
  121. }