driver_chibios_dac_basic.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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, 5, 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) { return channel_1_frequency; }
  106. void channel_2_start(void) {
  107. gptStart(&GPTD7, &gpt7cfg1);
  108. gptStartContinuous(&GPTD7, 2U);
  109. palSetPadMode(GPIOA, 5, PAL_MODE_INPUT_ANALOG);
  110. }
  111. void channel_2_stop(void) {
  112. gptStopTimer(&GPTD7);
  113. palSetPadMode(GPIOA, 5, PAL_MODE_OUTPUT_PUSHPULL);
  114. palSetPad(GPIOA, 5);
  115. }
  116. static float channel_2_frequency = 0.0f;
  117. void channel_2_set_frequency(float freq) {
  118. channel_2_frequency = freq;
  119. channel_2_stop();
  120. if (freq <= 0.0) // a pause/rest has freq=0
  121. return;
  122. gpt7cfg1.frequency = 2 * freq * AUDIO_DAC_BUFFER_SIZE;
  123. channel_2_start();
  124. }
  125. float channel_2_get_frequency(void) { return channel_2_frequency; }
  126. static void gpt_audio_state_cb(GPTDriver *gptp) {
  127. if (audio_update_state()) {
  128. #if defined(AUDIO_PIN_ALT_AS_NEGATIVE)
  129. // one piezo/speaker connected to both audio pins, the generated square-waves are inverted
  130. channel_1_set_frequency(audio_get_processed_frequency(0));
  131. channel_2_set_frequency(audio_get_processed_frequency(0));
  132. #else // two separate audio outputs/speakers
  133. // primary speaker on A4, optional secondary on A5
  134. if (AUDIO_PIN == A4) {
  135. channel_1_set_frequency(audio_get_processed_frequency(0));
  136. if (AUDIO_PIN_ALT == A5) {
  137. if (audio_get_number_of_active_tones() > 1) {
  138. channel_2_set_frequency(audio_get_processed_frequency(1));
  139. } else {
  140. channel_2_stop();
  141. }
  142. }
  143. }
  144. // primary speaker on A5, optional secondary on A4
  145. if (AUDIO_PIN == A5) {
  146. channel_2_set_frequency(audio_get_processed_frequency(0));
  147. if (AUDIO_PIN_ALT == A4) {
  148. if (audio_get_number_of_active_tones() > 1) {
  149. channel_1_set_frequency(audio_get_processed_frequency(1));
  150. } else {
  151. channel_1_stop();
  152. }
  153. }
  154. }
  155. #endif
  156. }
  157. }
  158. void audio_driver_initialize() {
  159. if ((AUDIO_PIN == A4) || (AUDIO_PIN_ALT == A4)) {
  160. palSetPadMode(GPIOA, 4, PAL_MODE_INPUT_ANALOG);
  161. dacStart(&DACD1, &dac_conf_ch1);
  162. // initial setup of the dac-triggering timer is still required, even
  163. // though it gets reconfigured and restarted later on
  164. gptStart(&GPTD6, &gpt6cfg1);
  165. }
  166. if ((AUDIO_PIN == A5) || (AUDIO_PIN_ALT == A5)) {
  167. palSetPadMode(GPIOA, 5, PAL_MODE_INPUT_ANALOG);
  168. dacStart(&DACD2, &dac_conf_ch2);
  169. gptStart(&GPTD7, &gpt7cfg1);
  170. }
  171. /* enable the output buffer, to directly drive external loads with no additional circuitry
  172. *
  173. * see: AN4566 Application note: Extending the DAC performance of STM32 microcontrollers
  174. * Note: Buffer-Off bit -> has to be set 0 to enable the output buffer
  175. * Note: enabling the output buffer imparts an additional dc-offset of a couple mV
  176. *
  177. * this is done here, reaching directly into the stm32 registers since chibios has not implemented BOFF handling yet
  178. * (see: chibios/os/hal/ports/STM32/todo.txt '- BOFF handling in DACv1.'
  179. */
  180. DACD1.params->dac->CR &= ~DAC_CR_BOFF1;
  181. DACD2.params->dac->CR &= ~DAC_CR_BOFF2;
  182. // start state-updater
  183. gptStart(&AUDIO_STATE_TIMER, &gptStateUpdateCfg);
  184. }
  185. void audio_driver_stop(void) {
  186. if ((AUDIO_PIN == A4) || (AUDIO_PIN_ALT == A4)) {
  187. gptStopTimer(&GPTD6);
  188. // stop the ongoing conversion and put the output in a known state
  189. dacStopConversion(&DACD1);
  190. dacPutChannelX(&DACD1, 0, AUDIO_DAC_OFF_VALUE);
  191. }
  192. if ((AUDIO_PIN == A5) || (AUDIO_PIN_ALT == A5)) {
  193. gptStopTimer(&GPTD7);
  194. dacStopConversion(&DACD2);
  195. dacPutChannelX(&DACD2, 0, AUDIO_DAC_OFF_VALUE);
  196. }
  197. gptStopTimer(&AUDIO_STATE_TIMER);
  198. }
  199. void audio_driver_start(void) {
  200. if ((AUDIO_PIN == A4) || (AUDIO_PIN_ALT == A4)) {
  201. dacStartConversion(&DACD1, &dac_conv_grp_ch1, (dacsample_t *)dac_buffer_1, AUDIO_DAC_BUFFER_SIZE);
  202. }
  203. if ((AUDIO_PIN == A5) || (AUDIO_PIN_ALT == A5)) {
  204. dacStartConversion(&DACD2, &dac_conv_grp_ch2, (dacsample_t *)dac_buffer_2, AUDIO_DAC_BUFFER_SIZE);
  205. }
  206. gptStartContinuous(&AUDIO_STATE_TIMER, 2U);
  207. }