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