From 930c45b89c06776e3f24156356fc29ab996e9e13 Mon Sep 17 00:00:00 2001 From: Adrien Hoffet Date: Thu, 22 Apr 2021 16:22:11 +0200 Subject: [PATCH 1/2] Add LED welcome and basic mic response --- firmware/audio_shield_firmware/Core/Inc/led.h | 73 ++++++++ firmware/audio_shield_firmware/Core/Src/led.c | 142 +++++++++++++++ .../audio_shield_firmware/Core/Src/main.c | 170 +++++++++++------- .../audio_shield_firmware/Debug/sources.mk | 1 + .../STM32F4xx_HAL_Driver/Src/stm32f4xx_hal.c | 4 +- .../audio_shield_firmware.ioc | 12 +- 6 files changed, 331 insertions(+), 71 deletions(-) create mode 100644 firmware/audio_shield_firmware/Core/Inc/led.h create mode 100644 firmware/audio_shield_firmware/Core/Src/led.c diff --git a/firmware/audio_shield_firmware/Core/Inc/led.h b/firmware/audio_shield_firmware/Core/Inc/led.h new file mode 100644 index 00000000..ba246f8c --- /dev/null +++ b/firmware/audio_shield_firmware/Core/Inc/led.h @@ -0,0 +1,73 @@ +/** + * || ____ _ __ + * +------+ / __ )(_) /_______________ _____ ___ + * | 0xBC | / __ / / __/ ___/ ___/ __ `/_ / / _ \ + * +------+ / /_/ / / /_/ /__/ / / /_/ / / /_/ __/ + * || || /_____/_/\__/\___/_/ \__,_/ /___/\___/ + * + * Crazyflie control firmware + * + * Copyright (C) 2011-2012 Bitcraze AB + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, in version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * Motors.h - Motor driver header file + * + * The motors PWM ratio is a value on 16 bits (from 0 to 0xFFFF) + * the functions of the driver will make the conversions to the actual PWM + * precision (ie. if the precision is 8bits 0xFFFF and 0xFF00 are equivalents). + * + * The precision is set in number of bits by the define MOTORS_PWM_BITS + * The timer prescaler is set by MOTORS_PWM_PRESCALE + */ +#ifndef __led_H__ +#define __led_H__ + +#include +#include +#include "stm32f4xx_hal.h" +#include "main.h" + + +/******** Defines ********/ + +extern TIM_HandleTypeDef htim1; + +/*** Public interface ***/ + +/** + * led Initialization. Configures two output compare channels in PWM mode + * with one of them inverted to increase power output. + */ +void ledInit(); + + +/** + * Test of the motor modules. The test will spin each motor very short in + * the sequence M1 to M4. + */ +bool ledTest(void); + +/** + * Set led ratio/power. + */ +void ledSetRatio(uint16_t ratio, uint8_t led); + +void ledSetMaxCount(uint16_t max); + +/** + * Set led frequency in hertz. + */ +void ledSetPSC(uint16_t freq); + +#endif /* __MOTORS_H__ */ diff --git a/firmware/audio_shield_firmware/Core/Src/led.c b/firmware/audio_shield_firmware/Core/Src/led.c new file mode 100644 index 00000000..333b205c --- /dev/null +++ b/firmware/audio_shield_firmware/Core/Src/led.c @@ -0,0 +1,142 @@ +/** + * || ____ _ __ + * +------+ / __ )(_) /_______________ _____ ___ + * | 0xBC | / __ / / __/ ___/ ___/ __ `/_ / / _ \ + * +------+ / /_/ / / /_/ /__/ / / /_/ / / /_/ __/ + * || || /_____/_/\__/\___/_/ \__,_/ /___/\___/ + * + * Crazyflie control firmware + * + * Copyright (C) 2011-2012 Bitcraze AB + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, in version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * led.c - led/Buzzer driver + * + * This code mainly interfacing the PWM peripheral lib of ST. + * + * + * Modified by Adrien Hoffet to embed led buzzer into the audiodeck V3 + * + */ + +#include + +#include "led.h" + +// HW defines +//#define led_TIM_PERIF RCC_APB1Periph_TIM5 +#define LED_TIM TIM1 +#define LED_hTIM htim1 +#define LED_TIM_DBG DBGMCU_TIM1_STOP +#define LED_TIM_SETCOMPARE_1(VAL) LED_TIM->CCR3 = VAL; +#define LED_TIM_SETCOMPARE_2(VAL) LED_TIM->CCR1 = VAL; +#define LED_TIM_SETCOMPARE_3(VAL) LED_TIM->CCR2 = VAL; +#define LED_TIM_SETCOMPARE_4(VAL) LED_TIM->CCR4 = VAL; +#define LED_TIM_SETARR(VAL) LED_TIM->ARR = VAL; +#define LED_TIM_GETCOMPARE LED_TIM->CCR3; +#define LED_TIM_SETPSC(VAL) LED_TIM->PSC = VAL; + +/* + Removed since init is done by cubeMX + #define LED_GPIO_POS_PERIF RCC_AHB1Periph_GPIOA + #define LED_GPIO_POS_PORT GPIOA + #define LED_GPIO_POS_PIN GPIO_Pin_2 // TIM5_CH3 + #define LED_GPIO_AF_POS_PIN GPIO_PinSource2 + #define LED_GPIO_AF_POS GPIO_AF_TIM5 + + #define LED_GPIO_NEG_PERIF RCC_AHB1Periph_GPIOA + #define LED_GPIO_NEG_PORT GPIOA + #define LED_GPIO_NEG_PIN GPIO_Pin_3 // TIM5_CH4 + #define LED_GPIO_AF_NEG_PIN GPIO_PinSource3 + #define LED_GPIO_AF_NEG GPIO_AF_TIM5 + */ + +#define LED_PWM_BITS (8) +#define LED_PWM_PERIOD ((1<PSC = psc; +} diff --git a/firmware/audio_shield_firmware/Core/Src/main.c b/firmware/audio_shield_firmware/Core/Src/main.c index 6e53ad89..e9aea927 100644 --- a/firmware/audio_shield_firmware/Core/Src/main.c +++ b/firmware/audio_shield_firmware/Core/Src/main.c @@ -32,6 +32,7 @@ #include "tukey_window.h" #include "buzzer.h" #include "sound.h" +#include "led.h" /* USER CODE END Includes */ @@ -67,7 +68,6 @@ #define FFTSIZE_SENT 32 // number of frequency bins to select #define FFTSIZE_HALF 16 // for filter_snr = 3, number of bins to send before buzzer bin - // processing #define N_PROP_FACTORS 30 #define DF (32000.0f/FFTSIZE) @@ -138,7 +138,11 @@ uint16_t current_frequency = 0; //#define BUZZER_CHANGE_BY_TIMER typedef enum { - BUZZER_IDLE, BUZZER_RECORD, BUZZER_PLAY_NEXT, BUZZER_CHOOSE_NEXT, BUZZER_STOP + BUZZER_IDLE, + BUZZER_RECORD, + BUZZER_PLAY_NEXT, + BUZZER_CHOOSE_NEXT, + BUZZER_STOP } state_note_t; uint8_t flag_spi_recieved = 0; @@ -180,7 +184,7 @@ uint8_t init_stage_iir = 1; uint16_t n_added = 0; // counter of how many samples were averaged. float prop_freq = 0; arm_rfft_fast_instance_f32 rfft_instance; -int16_t * tapering_window; +int16_t *tapering_window; // debugging uint8_t retval = 0; @@ -296,9 +300,9 @@ int main(void) { MX_TIM2_Init(); MX_I2S1_Init(); MX_SPI2_Init(); + MX_TIM5_Init(); MX_TIM1_Init(); MX_TIM3_Init(); - MX_TIM5_Init(); /* USER CODE BEGIN 2 */ // Start DMAs @@ -327,6 +331,20 @@ int main(void) { piezoSetRatio(BUZZER_ARR / 10); HAL_TIM_Base_Start(&htim5); + ledInit(); + + ledSetMaxCount(100); + for (uint8_t i = 1; i <= 4; i++) { + for (uint8_t j = 0; j < 100; j++) { + ledSetRatio(j, i); + HAL_Delay(1); + } + for (uint8_t j = 100; j > 0; j--) { + ledSetRatio(j, i); + HAL_Delay(1); + } + ledSetRatio(0, i); + } /* USER CODE END 2 */ @@ -339,10 +357,17 @@ int main(void) { /* USER CODE BEGIN 3 */ read_rx_buffer(); - if(buzzer_idx == 0 && state_note_sm != BUZZER_IDLE){ + if (buzzer_idx == 0 && state_note_sm != BUZZER_IDLE) { state_note_sm = BUZZER_STOP; } + // TODO: Upgrade led reactions + ledSetMaxCount(2000); + ledSetRatio((uint16_t) abs(dma_1[0]), 1); + ledSetRatio((uint16_t) abs(dma_1[1]), 2); + ledSetRatio((uint16_t) abs(dma_3[0]), 3); + ledSetRatio((uint16_t) abs(dma_3[1]), 4); + switch (state_note_sm) { case BUZZER_IDLE: @@ -359,12 +384,14 @@ int main(void) { } break; - case BUZZER_PLAY_NEXT: ; + case BUZZER_PLAY_NEXT: + ; // TODO(FD) for readability, create function that takes // current_melody[note_index] as in put and plays // the given note. - freq_list_t next_note = freq_list_tim[melodies[melody_index].notes[note_index]]; + freq_list_t next_note = + freq_list_tim[melodies[melody_index].notes[note_index]]; current_frequency = next_note.f; //HAL_TIM_Base_Init(&htim5); @@ -408,10 +435,14 @@ int main(void) { } else { for (int i = 0; i < N_ACTUAL_SAMPLES / 2; i++) { amplitude_avg[i] = (1 - IIR_ALPHA) * amplitude_avg[i] - + IIR_ALPHA * (abs_value_squared(&mic0_f[i * 2]) - + abs_value_squared(&mic1_f[i * 2]) - + abs_value_squared(&mic2_f[i * 2]) - + abs_value_squared(&mic3_f[i * 2])); + + IIR_ALPHA + * (abs_value_squared(&mic0_f[i * 2]) + + abs_value_squared( + &mic1_f[i * 2]) + + abs_value_squared( + &mic2_f[i * 2]) + + abs_value_squared( + &mic3_f[i * 2])); } } flag_fft_processing = 0; @@ -429,22 +460,29 @@ int main(void) { uint8_t i_array = 0; if (f_avg_counter >= 0) { for (int i_fbin = 0; i_fbin < FFTSIZE_SENT; i_fbin++) { - mics_f_sum[i_array++] += mic0_f[2 * selected_indices[i_fbin]]; - mics_f_sum[i_array++] += mic1_f[2 * selected_indices[i_fbin]]; - mics_f_sum[i_array++] += mic2_f[2 * selected_indices[i_fbin]]; - mics_f_sum[i_array++] += mic3_f[2 * selected_indices[i_fbin]]; - mics_f_sum[i_array++] += mic0_f[2 * selected_indices[i_fbin] + 1]; - mics_f_sum[i_array++] += mic1_f[2 * selected_indices[i_fbin] + 1]; - mics_f_sum[i_array++] += mic2_f[2 * selected_indices[i_fbin] + 1]; - mics_f_sum[i_array++] += mic3_f[2 * selected_indices[i_fbin] + 1]; + mics_f_sum[i_array++] += + mic0_f[2 * selected_indices[i_fbin]]; + mics_f_sum[i_array++] += + mic1_f[2 * selected_indices[i_fbin]]; + mics_f_sum[i_array++] += + mic2_f[2 * selected_indices[i_fbin]]; + mics_f_sum[i_array++] += + mic3_f[2 * selected_indices[i_fbin]]; + mics_f_sum[i_array++] += mic0_f[2 * selected_indices[i_fbin] + + 1]; + mics_f_sum[i_array++] += mic1_f[2 * selected_indices[i_fbin] + + 1]; + mics_f_sum[i_array++] += mic2_f[2 * selected_indices[i_fbin] + + 1]; + mics_f_sum[i_array++] += mic3_f[2 * selected_indices[i_fbin] + + 1]; } f_avg_counter++; fill_tx_buffer(); - } else{ + } else { f_avg_counter++; } - // TODO(FD) check that this still works. #ifdef BUZZER_CHANGE_BY_TIMER if((note_tickstart + NOTE_LENGTH) < HAL_GetTick()){ @@ -462,7 +500,8 @@ int main(void) { } #endif break; - case BUZZER_CHOOSE_NEXT: ; + case BUZZER_CHOOSE_NEXT: + ; note_index++; // point to next element and get its value. @@ -478,7 +517,7 @@ int main(void) { state_note_sm = BUZZER_PLAY_NEXT; } else { - state_note_sm = BUZZER_PLAY_NEXT; + state_note_sm = BUZZER_PLAY_NEXT; } break; case BUZZER_STOP: @@ -677,9 +716,9 @@ static void MX_TIM1_Init(void) { /* USER CODE END TIM1_Init 1 */ htim1.Instance = TIM1; - htim1.Init.Prescaler = 0; + htim1.Init.Prescaler = 1000; htim1.Init.CounterMode = TIM_COUNTERMODE_UP; - htim1.Init.Period = 65535; + htim1.Init.Period = 1000; htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; htim1.Init.RepetitionCounter = 0; htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; @@ -950,16 +989,17 @@ static void MX_GPIO_Init(void) { /* USER CODE BEGIN 4 */ #ifdef DCNotchActivated static inline int16_t DCNotch(int16_t x, uint8_t filter_index) { - static int16_t x_prev[4] = {0, 0, 0, 0}; - static int16_t y_prev[4] = {0, 0, 0, 0}; - if (filter_index == 10) { - memset(x_prev, 0x00, sizeof(x_prev)); - memset(y_prev, 0x00, sizeof(y_prev)); - } - - y_prev[filter_index] = (((int32_t)y_prev[filter_index] * 0x00007999) >> 16) - x_prev[filter_index] + x; - x_prev[filter_index] = x; - return y_prev[filter_index]; + static int16_t x_prev[4] = { 0, 0, 0, 0 }; + static int16_t y_prev[4] = { 0, 0, 0, 0 }; + if (filter_index == 10) { + memset(x_prev, 0x00, sizeof(x_prev)); + memset(y_prev, 0x00, sizeof(y_prev)); + } + + y_prev[filter_index] = (((int32_t) y_prev[filter_index] * 0x00007999) >> 16) + - x_prev[filter_index] + x; + x_prev[filter_index] = x; + return y_prev[filter_index]; } #endif @@ -976,18 +1016,21 @@ void inline process(int16_t *pIn, float *pOut1, float *pOut2, uint16_t size) { for (uint16_t i = 0; i < size; i += 1) { if (window_type > 0) { window_value = (float) tapering_window[i] / MAX_INT16; - } - else { + } else { window_value = 1.0; } #ifdef DCNotchActivated - if(pIn == dma_1){ - *pOut1++ = (float) DCNotch(*pIn++, 1) / MAX_INT16 * window_value; - *pOut2++ = (float) DCNotch(*pIn++, 2) / MAX_INT16 * window_value; - }else{ // pIn == dma_3 - *pOut1++ = (float) DCNotch(*pIn++, 3) / MAX_INT16 * window_value; - *pOut2++ = (float) DCNotch(*pIn++, 4) / MAX_INT16 * window_value; + if (pIn == dma_1) { + *pOut1++ = (float) DCNotch(*pIn++, 1) / MAX_INT16 + * window_value; + *pOut2++ = (float) DCNotch(*pIn++, 2) / MAX_INT16 + * window_value; + } else { // pIn == dma_3 + *pOut1++ = (float) DCNotch(*pIn++, 3) / MAX_INT16 + * window_value; + *pOut2++ = (float) DCNotch(*pIn++, 4) / MAX_INT16 + * window_value; }; #else // not DCNotchActivated *pOut1++ = (float) *pIn++ / MAX_INT16 * window_value; @@ -1013,7 +1056,7 @@ void inline process(int16_t *pIn, float *pOut1, float *pOut2, uint16_t size) { void frequency_bin_selection(uint16_t *selected_indices) { uint16_t freq_idx; - freq_idx = (uint16_t) round( current_frequency / DF); + freq_idx = (uint16_t) round(current_frequency / DF); // return fixed frequency bins if (filter_snr_enable == 3) { @@ -1023,7 +1066,7 @@ void frequency_bin_selection(uint16_t *selected_indices) { start_i = freq_idx - FFTSIZE_HALF; } - for (int i = start_i ; i < start_i + FFTSIZE_SENT + 1; i++) { + for (int i = start_i; i < start_i + FFTSIZE_SENT + 1; i++) { selected_indices[i - start_i] = i; } return; @@ -1174,13 +1217,13 @@ void fill_tx_buffer() { // NOTE: cannot do this inplace because we call fill_tx_buffer // multiple times on the same buffer. /*int i_array = 0; - float averaged_value; - for (int i = 0; i < N_MICS * 2 * FFTSIZE_SENT; i++) { - averaged_value = mics_f_sum[i]/f_avg_counter; - memcpy(&spi_tx_buffer[i_array], &averaged_value, sizeof(mics_f_sum[i])); - i_array += sizeof(mics_f_sum[i]); - } -*/ + float averaged_value; + for (int i = 0; i < N_MICS * 2 * FFTSIZE_SENT; i++) { + averaged_value = mics_f_sum[i]/f_avg_counter; + memcpy(&spi_tx_buffer[i_array], &averaged_value, sizeof(mics_f_sum[i])); + i_array += sizeof(mics_f_sum[i]); + } + */ memcpy(&spi_tx_buffer[0], mics_f_sum, sizeof(mics_f_sum)); int i_array = sizeof(mics_f_sum); @@ -1216,17 +1259,17 @@ void read_rx_buffer() { if (param_array[N_MOTORS + 7] != window_type) { window_type = param_array[N_MOTORS + 7]; switch (window_type) { - case 1: - tapering_window = hann_window; - break; - case 2: - tapering_window = flattop_window; - break; - case 3: - tapering_window = tukey_window; - break; - default: - break; + case 1: + tapering_window = hann_window; + break; + case 2: + tapering_window = flattop_window; + break; + case 3: + tapering_window = tukey_window; + break; + default: + break; } // reset the DC notch filter DCNotch(0, 10); @@ -1249,7 +1292,6 @@ float abs_value_squared(float real_imag[]) { return (real_imag[0] * real_imag[0] + real_imag[1] * real_imag[1]); } - /* USER CODE END 4 */ /** diff --git a/firmware/audio_shield_firmware/Debug/sources.mk b/firmware/audio_shield_firmware/Debug/sources.mk index 4c1aa9ff..282583f6 100644 --- a/firmware/audio_shield_firmware/Debug/sources.mk +++ b/firmware/audio_shield_firmware/Debug/sources.mk @@ -1,5 +1,6 @@ ################################################################################ # Automatically-generated file. Do not edit! +# Toolchain: GNU Tools for STM32 (9-2020-q2-update) ################################################################################ ELF_SRCS := diff --git a/firmware/audio_shield_firmware/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal.c b/firmware/audio_shield_firmware/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal.c index 89b6bb99..ca6311d1 100644 --- a/firmware/audio_shield_firmware/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal.c +++ b/firmware/audio_shield_firmware/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal.c @@ -50,11 +50,11 @@ * @{ */ /** - * @brief STM32F4xx HAL Driver version number V1.7.11 + * @brief STM32F4xx HAL Driver version number V1.7.12 */ #define __STM32F4xx_HAL_VERSION_MAIN (0x01U) /*!< [31:24] main version */ #define __STM32F4xx_HAL_VERSION_SUB1 (0x07U) /*!< [23:16] sub1 version */ -#define __STM32F4xx_HAL_VERSION_SUB2 (0x0BU) /*!< [15:8] sub2 version */ +#define __STM32F4xx_HAL_VERSION_SUB2 (0x0CU) /*!< [15:8] sub2 version */ #define __STM32F4xx_HAL_VERSION_RC (0x00U) /*!< [7:0] release candidate */ #define __STM32F4xx_HAL_VERSION ((__STM32F4xx_HAL_VERSION_MAIN << 24U)\ |(__STM32F4xx_HAL_VERSION_SUB1 << 16U)\ diff --git a/firmware/audio_shield_firmware/audio_shield_firmware.ioc b/firmware/audio_shield_firmware/audio_shield_firmware.ioc index ee2c5625..e51e4892 100644 --- a/firmware/audio_shield_firmware/audio_shield_firmware.ioc +++ b/firmware/audio_shield_firmware/audio_shield_firmware.ioc @@ -18,7 +18,7 @@ PB10.GPIO_PuPd=GPIO_NOPULL SPI2.VirtualType=VM_SLAVE PB10.Mode=Full_Duplex_Slave VP_TIM5_VS_ClockSourceINT.Signal=TIM5_VS_ClockSourceINT -TIM1.IPParameters=Channel-PWM Generation1 CH1,Channel-PWM Generation2 CH2,Channel-PWM Generation3 CH3,Channel-PWM Generation4 CH4 +TIM1.IPParameters=Channel-PWM Generation1 CH1,Channel-PWM Generation2 CH2,Channel-PWM Generation3 CH3,Channel-PWM Generation4 CH4,Period,Prescaler RCC.PLLCLKFreq_Value=84000000 PC10.Signal=I2S3_CK PA14.GPIO_Label=TCK @@ -51,6 +51,7 @@ Mcu.IP3=NVIC Mcu.IP0=DMA Dma.SPI3_RX.0.MemInc=DMA_MINC_ENABLE Mcu.IP1=I2S1 +TIM1.Prescaler=1000 Mcu.UserConstants= PA4.Mode=Half_Duplex_Master PC1.GPIOParameters=GPIO_PuPd @@ -108,8 +109,8 @@ Dma.SPI3_RX.0.MemDataAlignment=DMA_MDATAALIGN_HALFWORD PA15.Mode=Half_Duplex_Master NVIC.SysTick_IRQn=true\:0\:0\:true\:false\:true\:true\:true Dma.SPI2_RX.2.PeriphInc=DMA_PINC_DISABLE -ProjectManager.FirmwarePackage=STM32Cube FW_F4 V1.26.0 -MxDb.Version=DB.6.0.20 +ProjectManager.FirmwarePackage=STM32Cube FW_F4 V1.26.1 +MxDb.Version=DB.6.0.21 I2S3.ErrorAudioFreq=-0.26 % ProjectManager.BackupPrevious=false PC14-OSC32_IN.Mode=LSE-External-Oscillator @@ -157,6 +158,7 @@ Dma.Request3=SPI2_TX NVIC.BusFault_IRQn=true\:0\:0\:false\:false\:true\:true\:false NVIC.DMA1_Stream0_IRQn=true\:0\:0\:false\:false\:true\:false\:true SPI2.Direction=SPI_DIRECTION_2LINES +TIM1.Period=1000 PB10.GPIOParameters=GPIO_PuPd RCC.SAIBFreq_Value=96000000 Dma.Request0=SPI3_RX @@ -231,10 +233,10 @@ PB0.Mode=Half_Duplex_Master RCC.PLLSAIPCLKFreq_Value=96000000 board=NUCLEO-F446RE RCC.VCOOutputFreq_Value=336000000 -ProjectManager.LastFirmware=false +ProjectManager.LastFirmware=true Dma.SPI2_TX.3.Instance=DMA1_Stream4 RCC.APB2Freq_Value=84000000 -MxCube.Version=6.2.0 +MxCube.Version=6.2.1 SH.S_TIM1_CH2.0=TIM1_CH2,PWM Generation2 CH2 VP_TIM2_VS_ClockSourceINT.Mode=Internal RCC.PLLI2SPCLKFreq_Value=96000000 From 016eb73b6e18453f601fa018b6123e1f3c84a6d0 Mon Sep 17 00:00:00 2001 From: Adrien Hoffet Date: Thu, 22 Apr 2021 16:34:26 +0200 Subject: [PATCH 2/2] Green LED stops when no spi since more than 3s --- firmware/audio_shield_firmware/Core/Src/main.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/firmware/audio_shield_firmware/Core/Src/main.c b/firmware/audio_shield_firmware/Core/Src/main.c index e9aea927..7e2a9099 100644 --- a/firmware/audio_shield_firmware/Core/Src/main.c +++ b/firmware/audio_shield_firmware/Core/Src/main.c @@ -187,6 +187,7 @@ arm_rfft_fast_instance_f32 rfft_instance; int16_t *tapering_window; // debugging +uint32_t last_update_spi = 0; uint8_t retval = 0; uint32_t counter_error = 0; uint32_t counter_ok = 0; @@ -250,7 +251,7 @@ void HAL_SPI_ErrorCallback(SPI_HandleTypeDef *hspi) { void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi) { - HAL_GPIO_TogglePin(PC3_GPIO_Port, PC3_Pin); + last_update_spi = HAL_GetTick(); if ((spi_rx_buffer[SPI_N_BYTES - 1] != CHECKSUM_VALUE)) { STOPCHRONO; @@ -319,8 +320,8 @@ int main(void) { // Super important! We need to wait until the bus is idle, otherwise // there is a random shift in the spi_rx_buffer and spi_tx_buffer. - while (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_12) == GPIO_PIN_RESET) { - }; +// while (HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_12) == GPIO_PIN_RESET) { +// }; retval = HAL_SPI_TransmitReceive_DMA(&hspi2, spi_tx_buffer, spi_rx_buffer, SPI_N_BYTES); @@ -361,9 +362,14 @@ int main(void) { state_note_sm = BUZZER_STOP; } + // Monitoring // TODO: Upgrade led reactions ledSetMaxCount(2000); - ledSetRatio((uint16_t) abs(dma_1[0]), 1); + if(last_update_spi > (HAL_GetTick() - 3000)){ + ledSetRatio((uint16_t) abs(dma_1[0]), 1); + } else{ + ledSetRatio(0, 1); + } ledSetRatio((uint16_t) abs(dma_1[1]), 2); ledSetRatio((uint16_t) abs(dma_3[0]), 3); ledSetRatio((uint16_t) abs(dma_3[1]), 4);