audio_pwm_hardware.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright 2022 Stefan Kerkmann
  2. // Copyright 2020 Jack Humbert
  3. // Copyright 2020 JohSchneider
  4. // SPDX-License-Identifier: GPL-2.0-or-later
  5. // Audio Driver: PWM the duty-cycle is always kept at 50%, and the pwm-period is
  6. // adjusted to match the frequency of a note to be played back. This driver uses
  7. // the chibios-PWM system to produce a square-wave on specific output pins that
  8. // are connected to the PWM hardware. The hardware directly toggles the pin via
  9. // its alternate function. see your MCUs data-sheet for which pin can be driven
  10. // by what timer - looking for TIMx_CHy and the corresponding alternate
  11. // function.
  12. #include "audio.h"
  13. #include "gpio.h"
  14. #if !defined(AUDIO_PIN)
  15. # error "Audio feature enabled, but no pin selected - see docs/feature_audio under the ARM PWM settings"
  16. #endif
  17. #if !defined(AUDIO_PWM_COUNTER_FREQUENCY)
  18. # define AUDIO_PWM_COUNTER_FREQUENCY 100000
  19. #endif
  20. #ifndef AUDIO_PWM_COMPLEMENTARY_OUTPUT
  21. # define AUDIO_PWM_OUTPUT_MODE PWM_OUTPUT_ACTIVE_HIGH
  22. #else
  23. # define AUDIO_PWM_OUTPUT_MODE PWM_COMPLEMENTARY_OUTPUT_ACTIVE_HIGH
  24. #endif
  25. extern bool playing_note;
  26. extern bool playing_melody;
  27. extern uint8_t note_timbre;
  28. static PWMConfig pwmCFG = {.frequency = AUDIO_PWM_COUNTER_FREQUENCY, /* PWM clock frequency */
  29. .period = 2,
  30. .callback = NULL,
  31. .channels = {[(AUDIO_PWM_CHANNEL - 1)] = {.mode = AUDIO_PWM_OUTPUT_MODE, .callback = NULL}}};
  32. static float channel_1_frequency = 0.0f;
  33. void channel_1_set_frequency(float freq) {
  34. channel_1_frequency = freq;
  35. if (freq <= 0.0) {
  36. // a pause/rest has freq=0
  37. return;
  38. }
  39. pwmcnt_t period = (pwmCFG.frequency / freq);
  40. chSysLockFromISR();
  41. pwmChangePeriodI(&AUDIO_PWM_DRIVER, period);
  42. pwmEnableChannelI(&AUDIO_PWM_DRIVER, AUDIO_PWM_CHANNEL - 1,
  43. // adjust the duty-cycle so that the output is for 'note_timbre' duration HIGH
  44. PWM_PERCENTAGE_TO_WIDTH(&AUDIO_PWM_DRIVER, (100 - note_timbre) * 100));
  45. chSysUnlockFromISR();
  46. }
  47. float channel_1_get_frequency(void) {
  48. return channel_1_frequency;
  49. }
  50. void channel_1_start(void) {
  51. pwmStop(&AUDIO_PWM_DRIVER);
  52. pwmStart(&AUDIO_PWM_DRIVER, &pwmCFG);
  53. }
  54. void channel_1_stop(void) {
  55. pwmStop(&AUDIO_PWM_DRIVER);
  56. }
  57. static virtual_timer_t audio_vt;
  58. static void audio_callback(virtual_timer_t *vtp, void *p);
  59. // a regular timer task, that checks the note to be currently played and updates
  60. // the pwm to output that frequency.
  61. static void audio_callback(virtual_timer_t *vtp, void *p) {
  62. float freq; // TODO: freq_alt
  63. if (audio_update_state()) {
  64. freq = audio_get_processed_frequency(0); // freq_alt would be index=1
  65. channel_1_set_frequency(freq);
  66. }
  67. chSysLockFromISR();
  68. chVTSetI(&audio_vt, TIME_MS2I(16), audio_callback, NULL);
  69. chSysUnlockFromISR();
  70. }
  71. void audio_driver_initialize_impl(void) {
  72. pwmStart(&AUDIO_PWM_DRIVER, &pwmCFG);
  73. // connect the AUDIO_PIN to the PWM hardware
  74. #if defined(USE_GPIOV1) // STM32F103C8, RP2040
  75. palSetLineMode(AUDIO_PIN, AUDIO_PWM_PAL_MODE);
  76. #else // GPIOv2 (or GPIOv3 for f4xx, which is the same/compatible at this command)
  77. palSetLineMode(AUDIO_PIN, PAL_MODE_ALTERNATE(AUDIO_PWM_PAL_MODE));
  78. #endif
  79. chVTObjectInit(&audio_vt);
  80. }
  81. void audio_driver_start_impl(void) {
  82. channel_1_stop();
  83. channel_1_start();
  84. if ((playing_note || playing_melody) && !chVTIsArmed(&audio_vt)) {
  85. // a whole note is one beat, which is - per definition in
  86. // musical_notes.h - set to 64 the longest note is
  87. // BREAVE_DOT=128+64=192, the shortest SIXTEENTH=4 the tempo (which
  88. // might vary!) is in bpm (beats per minute) therefore: if the timer
  89. // ticks away at 64Hz (~16.6ms) audio_update_state is called just often
  90. // enough to not miss any notes
  91. chVTSet(&audio_vt, TIME_MS2I(16), audio_callback, NULL);
  92. }
  93. }
  94. void audio_driver_stop_impl(void) {
  95. channel_1_stop();
  96. chVTReset(&audio_vt);
  97. }