| #include "utility/pdb.h" | #include "utility/pdb.h" | ||||
| #include "utility/dspinst.h" | #include "utility/dspinst.h" | ||||
| #define COEF_HPF_DCBLOCK 1048300 // DC Removal filter coefficient in S12.19 | |||||
| DMAMEM static uint16_t analog_rx_buffer[AUDIO_BLOCK_SAMPLES]; | DMAMEM static uint16_t analog_rx_buffer[AUDIO_BLOCK_SAMPLES]; | ||||
| audio_block_t * AudioInputAnalog::block_left = NULL; | audio_block_t * AudioInputAnalog::block_left = NULL; | ||||
| uint16_t AudioInputAnalog::block_offset = 0; | uint16_t AudioInputAnalog::block_offset = 0; | ||||
| int32_t AudioInputAnalog::hpf_y1 = 0; | int32_t AudioInputAnalog::hpf_y1 = 0; | ||||
| int32_t AudioInputAnalog::hpf_x1 = 0; | int32_t AudioInputAnalog::hpf_x1 = 0; | ||||
| int32_t AudioInputAnalog::a = 1048300; | |||||
| bool AudioInputAnalog::update_responsibility = false; | bool AudioInputAnalog::update_responsibility = false; | ||||
| DMAChannel AudioInputAnalog::dma(false); | DMAChannel AudioInputAnalog::dma(false); | ||||
| void AudioInputAnalog::init(uint8_t pin) | void AudioInputAnalog::init(uint8_t pin) | ||||
| { | { | ||||
| int32_t tmp; | int32_t tmp; | ||||
| a = 1048300; // DC Removal filter coefficient in S12.19 | |||||
| // Configure the ADC and run at least one software-triggered | // Configure the ADC and run at least one software-triggered | ||||
| // conversion. This completes the self calibration stuff and | // conversion. This completes the self calibration stuff and | ||||
| int32_t acc = tmp; | int32_t acc = tmp; | ||||
| acc += hpf_y1; | acc += hpf_y1; | ||||
| acc -= hpf_x1; | acc -= hpf_x1; | ||||
| hpf_y1 = FRACMUL_SHL(acc, a, 11); | |||||
| hpf_y1 = FRACMUL_SHL(acc, COEF_HPF_DCBLOCK, 11); | |||||
| hpf_x1 = tmp; | hpf_x1 = tmp; | ||||
| s = signed_saturate_rshift(hpf_y1, 16, 4); | s = signed_saturate_rshift(hpf_y1, 16, 4); | ||||
| *p++ = s; | *p++ = s; |
| static uint16_t block_offset; | static uint16_t block_offset; | ||||
| static int32_t hpf_y1; | static int32_t hpf_y1; | ||||
| static int32_t hpf_x1; | static int32_t hpf_x1; | ||||
| static int32_t a; | |||||
| static bool update_responsibility; | static bool update_responsibility; | ||||
| static DMAChannel dma; | |||||
| static void isr(void); | |||||
| static void init(uint8_t pin); | |||||
| static DMAChannel dma; | |||||
| static void isr(void); | |||||
| static void init(uint8_t pin); | |||||
| }; | }; | ||||
| #if defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__) | #if defined(__MK20DX256__) || defined(__MK64FX512__) || defined(__MK66FX1M0__) | ||||
| #define COEF_HPF_DCBLOCK 1048300 // DC Removal filter coefficient in S12.19 | |||||
| DMAMEM static uint16_t left_buffer[AUDIO_BLOCK_SAMPLES]; | DMAMEM static uint16_t left_buffer[AUDIO_BLOCK_SAMPLES]; | ||||
| DMAMEM static uint16_t right_buffer[AUDIO_BLOCK_SAMPLES]; | DMAMEM static uint16_t right_buffer[AUDIO_BLOCK_SAMPLES]; | ||||
| audio_block_t * AudioInputAnalogStereo::block_left = NULL; | audio_block_t * AudioInputAnalogStereo::block_left = NULL; | ||||
| uint16_t AudioInputAnalogStereo::offset_right = 0; | uint16_t AudioInputAnalogStereo::offset_right = 0; | ||||
| int32_t AudioInputAnalogStereo::hpf_y1[2] = { 0, 0 }; | int32_t AudioInputAnalogStereo::hpf_y1[2] = { 0, 0 }; | ||||
| int32_t AudioInputAnalogStereo::hpf_x1[2] = { 0, 0 }; | int32_t AudioInputAnalogStereo::hpf_x1[2] = { 0, 0 }; | ||||
| int32_t AudioInputAnalogStereo::a = 1048300; | |||||
| bool AudioInputAnalogStereo::update_responsibility = false; | bool AudioInputAnalogStereo::update_responsibility = false; | ||||
| DMAChannel AudioInputAnalogStereo::dma0(false); | DMAChannel AudioInputAnalogStereo::dma0(false); | ||||
| DMAChannel AudioInputAnalogStereo::dma1(false); | DMAChannel AudioInputAnalogStereo::dma1(false); | ||||
| int32_t acc = tmp; | int32_t acc = tmp; | ||||
| acc += hpf_y1[0]; | acc += hpf_y1[0]; | ||||
| acc -= hpf_x1[0]; | acc -= hpf_x1[0]; | ||||
| hpf_y1[0] = FRACMUL_SHL(acc, a, 11); | |||||
| hpf_y1[0] = FRACMUL_SHL(acc, COEF_HPF_DCBLOCK, 11); | |||||
| hpf_x1[0] = tmp; | hpf_x1[0] = tmp; | ||||
| s = signed_saturate_rshift(hpf_y1[0], 16, 4); | s = signed_saturate_rshift(hpf_y1[0], 16, 4); | ||||
| *p++ = s; | *p++ = s; | ||||
| int32_t acc = tmp; | int32_t acc = tmp; | ||||
| acc += hpf_y1[1]; | acc += hpf_y1[1]; | ||||
| acc -= hpf_x1[1]; | acc -= hpf_x1[1]; | ||||
| hpf_y1[1]= FRACMUL_SHL(acc, a, 11); | |||||
| hpf_y1[1]= FRACMUL_SHL(acc, COEF_HPF_DCBLOCK, 11); | |||||
| hpf_x1[1] = tmp; | hpf_x1[1] = tmp; | ||||
| s = signed_saturate_rshift(hpf_y1[1], 16, 4); | s = signed_saturate_rshift(hpf_y1[1], 16, 4); | ||||
| *p++ = s; | *p++ = s; |
| { | { | ||||
| public: | public: | ||||
| AudioInputAnalogStereo() : AudioStream(0, NULL) { | AudioInputAnalogStereo() : AudioStream(0, NULL) { | ||||
| init(A2, A3); | |||||
| } | |||||
| init(A2, A3); | |||||
| } | |||||
| AudioInputAnalogStereo(uint8_t pin0, uint8_t pin1) : AudioStream(0, NULL) { | AudioInputAnalogStereo(uint8_t pin0, uint8_t pin1) : AudioStream(0, NULL) { | ||||
| init(pin0, pin1); | |||||
| } | |||||
| init(pin0, pin1); | |||||
| } | |||||
| virtual void update(void); | virtual void update(void); | ||||
| private: | private: | ||||
| static audio_block_t *block_left; | static audio_block_t *block_left; | ||||
| static uint16_t offset_right; | static uint16_t offset_right; | ||||
| static int32_t hpf_y1[2]; | static int32_t hpf_y1[2]; | ||||
| static int32_t hpf_x1[2]; | static int32_t hpf_x1[2]; | ||||
| static int32_t a; | |||||
| static bool update_responsibility; | static bool update_responsibility; | ||||
| static DMAChannel dma0; | |||||
| static DMAChannel dma1; | |||||
| static void isr0(void); | |||||
| static void isr1(void); | |||||
| static void init(uint8_t pin0, uint8_t pin1); | |||||
| static DMAChannel dma0; | |||||
| static DMAChannel dma1; | |||||
| static void isr0(void); | |||||
| static void isr1(void); | |||||
| static void init(uint8_t pin0, uint8_t pin1); | |||||
| }; | }; | ||||
| #endif | #endif |