Select Git revision
meson.build
Forked from
card10 / firmware
Source project has a limited visibility.
audio.c 8.97 KiB
#include <stdint.h>
#include <string.h>
#include "stm32f4xx_dac.h"
#include "nlr.h"
#include "misc.h"
#include "mpconfig.h"
#include "qstr.h"
#include "parse.h"
#include "obj.h"
#include "runtime.h"
#include "audio.h"
STATIC void TIM7_Config(uint freq) {
// TIM7 clock enable
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM7, ENABLE);
// reset TIM7
TIM_DeInit(TIM7);
// Compute the prescaler value so TIM7 triggers at freq-Hz
uint16_t period = (uint16_t) ((SystemCoreClock / 2) / freq) - 1;
// Time base configuration
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_Period = period; // timer triggers with this period
TIM_TimeBaseStructure.TIM_Prescaler = 0; // timer runs at SystemCoreClock / 2
TIM_TimeBaseStructure.TIM_ClockDivision = 0; // unused for TIM7
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; // unused for TIM7
TIM_TimeBaseInit(TIM7, &TIM_TimeBaseStructure);
// TIM7 TRGO selection
TIM_SelectOutputTrigger(TIM7, TIM_TRGOSource_Update);
// TIM7 enable counter
TIM_Cmd(TIM7, ENABLE);
}
/******************************************************************************/
// Micro Python bindings
typedef struct _pyb_audio_t {
mp_obj_base_t base;
uint dac_channel; // DAC_Channel_1 or DAC_Channel_2
DMA_Stream_TypeDef *dma_stream; // DMA1_Stream6 or DMA1_Stream7
} pyb_audio_t;
mp_obj_t pyb_audio_noise(mp_obj_t self_in, mp_obj_t freq) {
pyb_audio_t *self = self_in;
// set TIM7 to trigger the DAC at the given frequency
TIM7_Config(mp_obj_get_int(freq));
DAC_Cmd(self->dac_channel, DISABLE);
DAC_InitTypeDef DAC_InitStructure;
DAC_InitStructure.DAC_Trigger = DAC_Trigger_T7_TRGO;
DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_Noise;
DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_LFSRUnmask_Bits10_0;
DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
DAC_Init(self->dac_channel, &DAC_InitStructure);
DAC_Cmd(self->dac_channel, ENABLE);
if (self->dac_channel == DAC_Channel_1) {
DAC_SetChannel1Data(DAC_Align_12b_L, 0x7ff0);
} else {
DAC_SetChannel2Data(DAC_Align_12b_L, 0x7ff0);