audio_dac_basic.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /* Copyright 2016-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. #include "audio.h"
  18. #include "ch.h"
  19. #include "hal.h"
  20. /*
  21. Audio Driver: DAC
  22. which utilizes both channels of the DAC unit many STM32 are equipped with to output a modulated square-wave, from precomputed samples stored in a buffer, which is passed to the hardware through DMA
  23. this driver can either be used to drive to separate speakers, wired to A4+Gnd and A5+Gnd, which allows two tones to be played simultaneously
  24. OR
  25. one speaker wired to A4+A5 with the AUDIO_PIN_ALT_AS_NEGATIVE define set - see docs/feature_audio
  26. */
  27. #if !defined(AUDIO_PIN)
  28. # pragma message "Audio feature enabled, but no suitable pin selected as AUDIO_PIN - see docs/feature_audio under 'ARM (DAC basic)' for available options."
  29. // TODO: make this an 'error' instead; go through a breaking change, and add AUDIO_PIN A5 to all keyboards currently using AUDIO on STM32 based boards? - for now: set the define here
  30. # define AUDIO_PIN A5
  31. #endif
  32. // check configuration for ONE speaker, connected to both DAC pins
  33. #if defined(AUDIO_PIN_ALT_AS_NEGATIVE) && !defined(AUDIO_PIN_ALT)
  34. # error "Audio feature: AUDIO_PIN_ALT_AS_NEGATIVE set, but no pin configured as AUDIO_PIN_ALT"
  35. #endif
  36. #ifndef AUDIO_PIN_ALT
  37. // no ALT pin defined is valid, but the c-ifs below need some value set
  38. # define AUDIO_PIN_ALT -1
  39. #endif
  40. #if !defined(AUDIO_STATE_TIMER)
  41. # define AUDIO_STATE_TIMER GPTD8
  42. #endif
  43. // square-wave
  44. static const dacsample_t dac_buffer_1[AUDIO_DAC_BUFFER_SIZE] = {
  45. // First half is max, second half is 0
  46. [0 ... AUDIO_DAC_BUFFER_SIZE / 2 - 1] = AUDIO_DAC_SAMPLE_MAX,
  47. [AUDIO_DAC_BUFFER_SIZE / 2 ... AUDIO_DAC_BUFFER_SIZE - 1] = 0,
  48. };
  49. // square-wave
  50. static const dacsample_t dac_buffer_2[AUDIO_DAC_BUFFER_SIZE] = {
  51. // opposite of dac_buffer above
  52. [0 ... AUDIO_DAC_BUFFER_SIZE / 2 - 1] = 0,
  53. [AUDIO_DAC_BUFFER_SIZE / 2 ... AUDIO_DAC_BUFFER_SIZE - 1] = AUDIO_DAC_SAMPLE_MAX,
  54. };
  55. GPTConfig gpt6cfg1 = {.frequency = AUDIO_DAC_SAMPLE_RATE,
  56. .callback = NULL,
  57. .cr2 = TIM_CR2_MMS_1, /* MMS = 010 = TRGO on Update Event. */
  58. .dier = 0U};
  59. GPTConfig gpt7cfg1 = {.frequency = AUDIO_DAC_SAMPLE_RATE,
  60. .callback = NULL,
  61. .cr2 = TIM_CR2_MMS_1, /* MMS = 010 = TRGO on Update Event. */
  62. .dier = 0U};
  63. static void gpt_audio_state_cb(GPTDriver *gptp);
  64. GPTConfig gptStateUpdateCfg = {.frequency = 10,
  65. .callback = gpt_audio_state_cb,
  66. .cr2 = TIM_CR2_MMS_1, /* MMS = 010 = TRGO on Update Event. */
  67. .dier = 0U};
  68. static const DACConfig dac_conf_ch1 = {.init = AUDIO_DAC_OFF_VALUE, .datamode = DAC_DHRM_12BIT_RIGHT};
  69. static const DACConfig dac_conf_ch2 = {.init = AUDIO_DAC_OFF_VALUE, .datamode = DAC_DHRM_12BIT_RIGHT};
  70. /**
  71. * @note The DAC_TRG(0) here selects the Timer 6 TRGO event, which is triggered
  72. * on the rising edge after 3 APB1 clock cycles, causing our gpt6cfg1.frequency
  73. * to be a third of what we expect.
  74. *
  75. * Here are all the values for DAC_TRG (TSEL in the ref manual)
  76. * TIM15_TRGO 0b011
  77. * TIM2_TRGO 0b100
  78. * TIM3_TRGO 0b001
  79. * TIM6_TRGO 0b000
  80. * TIM7_TRGO 0b010
  81. * EXTI9 0b110
  82. * SWTRIG 0b111
  83. */
  84. static const DACConversionGroup dac_conv_grp_ch1 = {.num_channels = 1U, .trigger = DAC_TRG(0b000)};
  85. static const DACConversionGroup dac_conv_grp_ch2 = {.num_channels = 1U, .trigger = DAC_TRG(0b010)};
  86. void channel_1_start(void) {
  87. gptStart(&GPTD6, &gpt6cfg1);
  88. gptStartContinuous(&GPTD6, 2U);
  89. palSetPadMode(GPIOA, 4, PAL_MODE_INPUT_ANALOG);
  90. }
  91. void channel_1_stop(void) {
  92. gptStopTimer(&GPTD6);
  93. palSetPadMode(GPIOA, 4, PAL_MODE_OUTPUT_PUSHPULL);
  94. palSetPad(GPIOA, 4);
  95. }
  96. static float channel_1_frequency = 0.0f;
  97. void channel_1_set_frequency(float freq) {
  98. channel_1_frequency = freq;
  99. channel_1_stop();
  100. if (freq <= 0.0) // a pause/rest has freq=0
  101. return;
  102. gpt6cfg1.frequency = 2 * freq * AUDIO_DAC_BUFFER_SIZE;
  103. channel_1_start();
  104. }
  105. float channel_1_get_frequency(void) {
  106. return channel_1_frequency;
  107. }
  108. void channel_2_start(void) {
  109. gptStart(&GPTD7, &gpt7cfg1);
  110. gptStartContinuous(&GPTD7, 2U);
  111. palSetPadMode(GPIOA, 5, PAL_MODE_INPUT_ANALOG);
  112. }
  113. void channel_2_stop(void) {
  114. gptStopTimer(&GPTD7);
  115. palSetPadMode(GPIOA, 5, PAL_MODE_OUTPUT_PUSHPULL);
  116. palSetPad(GPIOA, 5);
  117. }
  118. static float channel_2_frequency = 0.0f;
  119. void channel_2_set_frequency(float freq) {
  120. channel_2_frequency = freq;
  121. channel_2_stop();
  122. if (freq <= 0.0) // a pause/rest has freq=0
  123. return;
  124. gpt7cfg1.frequency = 2 * freq * AUDIO_DAC_BUFFER_SIZE;
  125. channel_2_start();
  126. }
  127. float channel_2_get_frequency(void) {
  128. return channel_2_frequency;
  129. }
  130. static void gpt_audio_state_cb(GPTDriver *gptp) {
  131. if (audio_update_state()) {
  132. #if defined(AUDIO_PIN_ALT_AS_NEGATIVE)
  133. // one piezo/speaker connected to both audio pins, the generated square-waves are inverted
  134. channel_1_set_frequency(audio_get_processed_frequency(0));
  135. channel_2_set_frequency(audio_get_processed_frequency(0));
  136. #else // two separate audio outputs/speakers
  137. // primary speaker on A4, optional secondary on A5
  138. if (AUDIO_PIN == A4) {
  139. channel_1_set_frequency(audio_get_processed_frequency(0));
  140. if (AUDIO_PIN_ALT == A5) {
  141. if (audio_get_number_of_active_tones() > 1) {
  142. channel_2_set_frequency(audio_get_processed_frequency(1));
  143. } else {
  144. channel_2_stop();
  145. }
  146. }
  147. }
  148. // primary speaker on A5, optional secondary on A4
  149. if (AUDIO_PIN == A5) {
  150. channel_2_set_frequency(audio_get_processed_frequency(0));
  151. if (AUDIO_PIN_ALT == A4) {
  152. if (audio_get_number_of_active_tones() > 1) {
  153. channel_1_set_frequency(audio_get_processed_frequency(1));
  154. } else {
  155. channel_1_stop();
  156. }
  157. }
  158. }
  159. #endif
  160. }
  161. }
  162. void audio_driver_initialize() {
  163. if ((AUDIO_PIN == A4) || (AUDIO_PIN_ALT == A4)) {
  164. palSetPadMode(GPIOA, 4, PAL_MODE_INPUT_ANALOG);
  165. dacStart(&DACD1, &dac_conf_ch1);
  166. // initial setup of the dac-triggering timer is still required, even
  167. // though it gets reconfigured and restarted later on
  168. gptStart(&GPTD6, &gpt6cfg1);
  169. }
  170. if ((AUDIO_PIN == A5) || (AUDIO_PIN_ALT == A5)) {
  171. palSetPadMode(GPIOA, 5, PAL_MODE_INPUT_ANALOG);
  172. dacStart(&DACD2, &dac_conf_ch2);
  173. gptStart(&GPTD7, &gpt7cfg1);
  174. }
  175. /* enable the output buffer, to directly drive external loads with no additional circuitry
  176. *
  177. * see: AN4566 Application note: Extending the DAC performance of STM32 microcontrollers
  178. * Note: Buffer-Off bit -> has to be set 0 to enable the output buffer
  179. * Note: enabling the output buffer imparts an additional dc-offset of a couple mV
  180. *
  181. * this is done here, reaching directly into the stm32 registers since chibios has not implemented BOFF handling yet
  182. * (see: chibios/os/hal/ports/STM32/todo.txt '- BOFF handling in DACv1.'
  183. */
  184. DACD1.params->dac->CR &= ~DAC_CR_BOFF1;
  185. DACD2.params->dac->CR &= ~DAC_CR_BOFF2;
  186. // start state-updater
  187. gptStart(&AUDIO_STATE_TIMER, &gptStateUpdateCfg);
  188. }
  189. void audio_driver_stop(void) {
  190. if ((AUDIO_PIN == A4) || (AUDIO_PIN_ALT == A4)) {
  191. gptStopTimer(&GPTD6);
  192. // stop the ongoing conversion and put the output in a known state
  193. dacStopConversion(&DACD1);
  194. dacPutChannelX(&DACD1, 0, AUDIO_DAC_OFF_VALUE);
  195. }
  196. if ((AUDIO_PIN == A5) || (AUDIO_PIN_ALT == A5)) {
  197. gptStopTimer(&GPTD7);
  198. dacStopConversion(&DACD2);
  199. dacPutChannelX(&DACD2, 0, AUDIO_DAC_OFF_VALUE);
  200. }
  201. gptStopTimer(&AUDIO_STATE_TIMER);
  202. }
  203. void audio_driver_start(void) {
  204. if ((AUDIO_PIN == A4) || (AUDIO_PIN_ALT == A4)) {
  205. dacStartConversion(&DACD1, &dac_conv_grp_ch1, (dacsample_t *)dac_buffer_1, AUDIO_DAC_BUFFER_SIZE);
  206. }
  207. if ((AUDIO_PIN == A5) || (AUDIO_PIN_ALT == A5)) {
  208. dacStartConversion(&DACD2, &dac_conv_grp_ch2, (dacsample_t *)dac_buffer_2, AUDIO_DAC_BUFFER_SIZE);
  209. }
  210. gptStartContinuous(&AUDIO_STATE_TIMER, 2U);
  211. }