audio_dac_basic.c 9.2 KB

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