-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflizzer_tracker_hal.c
313 lines (243 loc) · 10.6 KB
/
flizzer_tracker_hal.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#include "flizzer_tracker_hal.h"
#include "flizzer_tracker.h"
void sound_engine_dma_isr(void* ctx) {
SoundEngine* sound_engine = (SoundEngine*)ctx;
// sound_engine->counter++;
// half of transfer
if(LL_DMA_IsActiveFlag_HT1(DMA1)) {
LL_DMA_ClearFlag_HT1(DMA1);
// fill first half of buffer
uint16_t* audio_buffer = sound_engine->audio_buffer;
uint32_t audio_buffer_length = sound_engine->audio_buffer_size / 2;
sound_engine_fill_buffer(sound_engine, audio_buffer, audio_buffer_length);
}
// transfer complete
if(LL_DMA_IsActiveFlag_TC1(DMA1)) {
LL_DMA_ClearFlag_TC1(DMA1);
// fill second half of buffer
uint32_t audio_buffer_length = sound_engine->audio_buffer_size / 2;
uint16_t* audio_buffer = &sound_engine->audio_buffer[audio_buffer_length];
sound_engine_fill_buffer(sound_engine, audio_buffer, audio_buffer_length);
}
}
void tracker_engine_timer_isr(
void* ctx) // the tracker engine interrupt is of higher priority than sound engine one so it can run at the middle of filling the buffer, thus allowing sample-accurate tight effect timing
{
TrackerEngine* tracker_engine = (TrackerEngine*)ctx;
// tracker_engine->counter++;
if(LL_TIM_IsActiveFlag_UPDATE(TRACKER_ENGINE_TIMER)) {
LL_TIM_ClearFlag_UPDATE(TRACKER_ENGINE_TIMER);
tracker_engine_advance_tick(tracker_engine);
}
}
void sound_engine_PWM_timer_init(bool external_audio_output) // external audio on pin PA6
{
if(external_audio_output) {
/*if(furi_hal_speaker_is_mine()) {
furi_hal_speaker_release();
}*/
//LL_TIM_DisableAllOutputs(SPEAKER_PWM_TIMER);
//LL_TIM_DisableCounter(SPEAKER_PWM_TIMER);
if(!(furi_hal_speaker_is_mine())) {
if(furi_hal_speaker_acquire(1000)) {
LL_TIM_DisableAllOutputs(SPEAKER_PWM_TIMER);
LL_TIM_DisableCounter(SPEAKER_PWM_TIMER);
LL_TIM_InitTypeDef TIM_InitStruct = {0};
LL_TIM_OC_InitTypeDef TIM_OC_InitStruct = {0};
TIM_InitStruct.Prescaler = 0;
TIM_InitStruct.Autoreload =
1023; // 10-bit PWM resolution at around 60 kHz PWM rate
TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
LL_TIM_Init(SPEAKER_PWM_TIMER, &TIM_InitStruct);
TIM_OC_InitStruct.OCMode = LL_TIM_OCMODE_PWM1;
TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_ENABLE;
TIM_OC_InitStruct.CompareValue = 0;
LL_TIM_OC_Init(SPEAKER_PWM_TIMER, SPEAKER_PWM_TIMER_CHANNEL, &TIM_OC_InitStruct);
SPEAKER_PWM_TIMER->CNT = 0;
LL_TIM_EnableAllOutputs(SPEAKER_PWM_TIMER);
LL_TIM_EnableCounter(SPEAKER_PWM_TIMER);
}
}
furi_hal_gpio_init(&gpio_ext_pa6, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
LL_TIM_InitTypeDef TIM_InitStruct = {0};
LL_TIM_OC_InitTypeDef TIM_OC_InitStruct = {0};
TIM_InitStruct.Prescaler = 0;
TIM_InitStruct.Autoreload = 1023; // 10-bit PWM resolution at around 60 kHz PWM rate
TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
LL_TIM_Init(SPEAKER_PWM_TIMER, &TIM_InitStruct);
TIM_OC_InitStruct.OCMode = LL_TIM_OCMODE_PWM1;
TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_ENABLE;
TIM_OC_InitStruct.CompareValue = 0;
LL_TIM_OC_Init(SPEAKER_PWM_TIMER, SPEAKER_PWM_TIMER_CHANNEL, &TIM_OC_InitStruct);
SPEAKER_PWM_TIMER->CNT = 0;
LL_TIM_EnableAllOutputs(SPEAKER_PWM_TIMER);
LL_TIM_EnableCounter(SPEAKER_PWM_TIMER);
furi_hal_gpio_init(&gpio_ext_pa6, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
//furi_hal_gpio_init_ex(&gpio_ext_pa6, GpioModeAltFunctionPushPull, GpioPullNo, GpioSpeedLow, GpioAltFn14TIM16);
}
else {
if(!(furi_hal_speaker_is_mine())) {
if(furi_hal_speaker_acquire(1000)) {
LL_TIM_DisableAllOutputs(SPEAKER_PWM_TIMER);
LL_TIM_DisableCounter(SPEAKER_PWM_TIMER);
LL_TIM_InitTypeDef TIM_InitStruct = {0};
LL_TIM_OC_InitTypeDef TIM_OC_InitStruct = {0};
TIM_InitStruct.Prescaler = 0;
TIM_InitStruct.Autoreload =
1023; // 10-bit PWM resolution at around 60 kHz PWM rate
TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
LL_TIM_Init(SPEAKER_PWM_TIMER, &TIM_InitStruct);
TIM_OC_InitStruct.OCMode = LL_TIM_OCMODE_PWM1;
TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_ENABLE;
TIM_OC_InitStruct.CompareValue = 0;
LL_TIM_OC_Init(SPEAKER_PWM_TIMER, SPEAKER_PWM_TIMER_CHANNEL, &TIM_OC_InitStruct);
SPEAKER_PWM_TIMER->CNT = 0;
LL_TIM_EnableAllOutputs(SPEAKER_PWM_TIMER);
LL_TIM_EnableCounter(SPEAKER_PWM_TIMER);
}
}
furi_hal_gpio_init(&gpio_ext_pa6, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
//furi_hal_gpio_init_ex(&gpio_ext_pa6, GpioModeAltFunctionPushPull, GpioPullNo, GpioSpeedLow, GpioAltFn14TIM16);
}
furi_hal_gpio_init_ex(
&gpio_ext_pa6, GpioModeAltFunctionPushPull, GpioPullNo, GpioSpeedLow, GpioAltFn14TIM16);
}
void sound_engine_set_audio_output(bool external_audio_output) {
if(external_audio_output) {
furi_hal_gpio_init_ex(
&gpio_ext_pa6, GpioModeAltFunctionPushPull, GpioPullNo, GpioSpeedLow, GpioAltFn14TIM16);
if(furi_hal_speaker_is_mine()) {
furi_hal_speaker_release();
}
}
else {
if(!(furi_hal_speaker_is_mine())) {
bool unu = furi_hal_speaker_acquire(1000);
UNUSED(unu);
}
furi_hal_gpio_init(&gpio_ext_pa6, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
}
}
void sound_engine_timer_init(uint32_t sample_rate) // external audio on pin PA6
{
if(!furi_hal_bus_is_enabled(FuriHalBusTIM1)) {
furi_hal_bus_enable(FuriHalBusTIM1);
}
LL_TIM_InitTypeDef TIM_InitStruct = {0};
LL_TIM_OC_InitTypeDef TIM_OC_InitStruct = {0};
TIM_InitStruct.Prescaler = 0;
TIM_InitStruct.Autoreload =
TIMER_BASE_CLOCK / sample_rate - 1; // to support various sample rates
TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
LL_TIM_Init(SAMPLE_RATE_TIMER, &TIM_InitStruct);
TIM_OC_InitStruct.OCMode = LL_TIM_OCMODE_PWM1;
TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_ENABLE;
LL_TIM_OC_Init(SAMPLE_RATE_TIMER, SPEAKER_PWM_TIMER_CHANNEL, &TIM_OC_InitStruct);
LL_TIM_EnableAllOutputs(SAMPLE_RATE_TIMER);
SAMPLE_RATE_TIMER->CNT = 0;
}
void tracker_engine_timer_init(uint8_t rate) // 0-255 hz
{
if(!furi_hal_bus_is_enabled(FuriHalBusTIM2)) {
furi_hal_bus_enable(FuriHalBusTIM2);
}
LL_TIM_InitTypeDef TIM_InitStruct = {0};
LL_TIM_OC_InitTypeDef TIM_OC_InitStruct = {0};
TIM_InitStruct.Prescaler = 0; // using 32-bit timer
TIM_InitStruct.Autoreload =
(uint32_t)TIMER_BASE_CLOCK / (uint32_t)rate - 1; // to support various tracker engine rates
TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
LL_TIM_Init(TRACKER_ENGINE_TIMER, &TIM_InitStruct);
TIM_OC_InitStruct.OCMode = LL_TIM_OCMODE_PWM1;
TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_ENABLE;
LL_TIM_OC_Init(TRACKER_ENGINE_TIMER, SPEAKER_PWM_TIMER_CHANNEL, &TIM_OC_InitStruct);
LL_TIM_EnableIT_UPDATE(TRACKER_ENGINE_TIMER);
TRACKER_ENGINE_TIMER->CNT = 0;
}
void tracker_engine_set_rate(uint8_t rate) {
if(!furi_hal_bus_is_enabled(FuriHalBusTIM2)) {
furi_hal_bus_enable(FuriHalBusTIM2);
}
LL_TIM_InitTypeDef TIM_InitStruct = {0};
TIM_InitStruct.Prescaler = 0; // using 32-bit timer
TIM_InitStruct.Autoreload =
(uint32_t)TIMER_BASE_CLOCK / (uint32_t)rate - 1; // to support various tracker engine rates
TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
LL_TIM_Init(TRACKER_ENGINE_TIMER, &TIM_InitStruct);
TRACKER_ENGINE_TIMER->CNT = 0;
}
void tracker_engine_init_hardware(uint8_t rate) {
tracker_engine_timer_init(rate);
}
void sound_engine_dma_init(uint32_t address, uint32_t size) {
uint32_t dma_dst = (uint32_t) & (SPEAKER_PWM_TIMER->CCR1);
LL_DMA_ConfigAddresses(DMA_INSTANCE, address, dma_dst, LL_DMA_DIRECTION_MEMORY_TO_PERIPH);
LL_DMA_SetDataLength(DMA_INSTANCE, size);
LL_DMA_SetPeriphRequest(DMA_INSTANCE, LL_DMAMUX_REQ_TIM1_UP);
LL_DMA_SetDataTransferDirection(DMA_INSTANCE, LL_DMA_DIRECTION_MEMORY_TO_PERIPH);
LL_DMA_SetChannelPriorityLevel(DMA_INSTANCE, LL_DMA_PRIORITY_VERYHIGH);
LL_DMA_SetMode(DMA_INSTANCE, LL_DMA_MODE_CIRCULAR);
LL_DMA_SetPeriphIncMode(DMA_INSTANCE, LL_DMA_PERIPH_NOINCREMENT);
LL_DMA_SetMemoryIncMode(DMA_INSTANCE, LL_DMA_MEMORY_INCREMENT);
LL_DMA_SetPeriphSize(DMA_INSTANCE, LL_DMA_PDATAALIGN_HALFWORD);
LL_DMA_SetMemorySize(DMA_INSTANCE, LL_DMA_MDATAALIGN_HALFWORD);
LL_DMA_EnableIT_TC(DMA_INSTANCE);
LL_DMA_EnableIT_HT(DMA_INSTANCE);
}
void sound_engine_init_hardware(
uint32_t sample_rate,
bool external_audio_output,
uint16_t* audio_buffer,
uint32_t audio_buffer_size) {
sound_engine_dma_init((uint32_t)audio_buffer, audio_buffer_size);
sound_engine_timer_init(sample_rate);
sound_engine_PWM_timer_init(external_audio_output);
}
void sound_engine_dma_start() {
LL_DMA_EnableChannel(DMA_INSTANCE);
LL_TIM_EnableDMAReq_UPDATE(SAMPLE_RATE_TIMER);
}
void sound_engine_dma_stop() {
LL_DMA_DisableChannel(DMA_INSTANCE);
}
void sound_engine_start() {
SAMPLE_RATE_TIMER->CNT = 0;
LL_TIM_EnableCounter(SAMPLE_RATE_TIMER);
sound_engine_dma_start();
}
void sound_engine_stop() {
LL_TIM_DisableAllOutputs(SAMPLE_RATE_TIMER);
LL_TIM_DisableCounter(SAMPLE_RATE_TIMER);
sound_engine_dma_stop();
}
void sound_engine_deinit_timer() {
LL_TIM_DisableAllOutputs(SAMPLE_RATE_TIMER);
LL_TIM_DisableAllOutputs(SPEAKER_PWM_TIMER);
LL_TIM_DisableCounter(SPEAKER_PWM_TIMER);
if(furi_hal_speaker_is_mine()) {
furi_hal_speaker_release();
}
if(furi_hal_bus_is_enabled(FuriHalBusTIM2)) {
furi_hal_bus_disable(FuriHalBusTIM2);
}
if(furi_hal_bus_is_enabled(FuriHalBusTIM1)) {
furi_hal_bus_disable(FuriHalBusTIM1);
}
}
void tracker_engine_start() {
TRACKER_ENGINE_TIMER->CNT = 0;
LL_TIM_EnableAllOutputs(TRACKER_ENGINE_TIMER);
LL_TIM_EnableCounter(TRACKER_ENGINE_TIMER);
}
void tracker_engine_stop() {
LL_TIM_DisableAllOutputs(TRACKER_ENGINE_TIMER);
LL_TIM_DisableCounter(TRACKER_ENGINE_TIMER);
}
void play() {
tracker_engine_start();
sound_engine_start();
}
void stop() {
sound_engine_stop();
tracker_engine_stop();
}