/* Includes ------------------------------------------------------------------*/ #include "cube_hal.h" /* USER CODE BEGIN Includes */ #include <string.h> #include "hal_types.h" #include "hci.h" #include "eddystone_beacon.h" #include "stm32_bluenrg_ble.h" #include "stm32xx_it.h" #include "component.h" #include "SensorTile_accelero.h" #include "SensorTile_audio_in.h" #include "arm_math.h" #include "arm_const_structs.h" /* USER CODE END Includes */ /** @addtogroup X-CUBE-BLE1_Applications * @{ */ /** @defgroup Beacon * @{ */ /** @defgroup MAIN * @{ */ /* Private variables ---------------------------------------------------------*/ /* USER CODE BEGIN PV */ /* Private variables ---------------------------------------------------------*/ /* USER CODE END PV */ /* Private define ------------------------------------------------------------*/ #define AUDIO_CHANNELS 1 #define AUDIO_SAMPLING_FREQUENCY 16000 #define AUDIO_IN_BUF_LEN (AUDIO_CHANNELS*AUDIO_SAMPLING_FREQUENCY/1000) //16byte #define FFT_LEN 2048 //2048/16 = 128 /* Private macro -------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ uint16_t PCM_Buffer[AUDIO_IN_BUF_LEN]; extern volatile float mic1_results[]; static void *LSM303AGR_X_0_handle = NULL; /*--------------------fft----------------------------*/ //FFT输入数据 static float32_t fft_data[FFT_LEN]; extern float32_t fft_data1[FFT_LEN]; //取模 static float32_t fft_mod[FFT_LEN/2]; uint32_t fftSize = FFT_LEN/2; uint32_t ifftFlag = 0; uint32_t doBitReverse = 1; int ii = 0; uint8_t adv[22]; uint8_t advIndex = 0; uint16_t maxValues[5]; uint16_t maxFqc[5]; uint8_t doFFT = 0; /** @defgroup MAIN_Private_Function_Prototypes * @{ */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); void BlueNRG_Init(void); void MX_GPIO_Init(void); void fft(void); /* USER CODE BEGIN PFP */ /* Private function prototypes -----------------------------------------------*/ /* USER CODE END PFP */ /** * @} */ /* USER CODE BEGIN 0 */ void EnterStopMode(void); /* USER CODE END 0 */ /** * @brief Main function to show how to use the BlueNRG Bluetooth Low Energy * expansion board to implement a Eddystone Beacon device. * * @param None * @retval None */ SensorAxes_t acceleration; void initAcceleroSensor(void) { if (BSP_ACCELERO_Init( LSM303AGR_X_0, &LSM303AGR_X_0_handle ) != COMPONENT_OK) { // while(1); } BSP_ACCELERO_Sensor_Enable( LSM303AGR_X_0_handle ); } void Accelero_Sensor_Handler( void *handle ) { uint8_t id; uint8_t status; BSP_ACCELERO_Get_Instance( handle, &id ); BSP_ACCELERO_IsInitialized( handle, &status ); if ( BSP_ACCELERO_Get_Axes( handle, &acceleration ) == COMPONENT_ERROR ) { acceleration.AXIS_X = 0; acceleration.AXIS_Y = 0; acceleration.AXIS_Z = 0; } else { } } int main(void) { //HAL_EnableDBGStopMode(); /* MCU Configuration----------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* Configure the system clock */ SystemClock_Config(); initAcceleroSensor(); /* Initialize the BlueNRG SPI */ BNRG_SPI_Init(); /* Initialize the BlueNRG HCI */ HCI_Init(); /* Reset BlueNRG hardware */ BlueNRG_RST(); /* Init BlueNRG protocol */ BlueNRG_Init(); // sprintf(buf,"\r\nhello:%d!",i); // UpdateAdvStr(buf); /* Initialize beacon services */ if (EDDYSTONE_BEACON_TYPE & EDDYSTONE_UID_BEACON_TYPE) { EddystoneUID_Start(); } if (EDDYSTONE_BEACON_TYPE & EDDYSTONE_URL_BEACON_TYPE) { EddystoneURL_Start(); } /* Configure Audio Input peripheral - DFSDM */ BSP_AUDIO_IN_Init(AUDIO_SAMPLING_FREQUENCY, 16, AUDIO_CHANNELS); /* Start Microphone acquisition */ BSP_AUDIO_IN_Record(PCM_Buffer,0); /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ // __WFI(); HCI_Process(); // EnterStopMode(); // // HAL_Delay(100); // Accelero_Sensor_Handler(LSM303AGR_X_0_handle); //// sprintf(buf,"%d,%d,%d",acceleration.AXIS_X,acceleration.AXIS_Y,acceleration.AXIS_Z); //// sprintf(buf,"x:%d,y:%d,z:%d",acceleration.AXIS_X,acceleration.AXIS_Y,acceleration.AXIS_Z); // adv[0] = acceleration.AXIS_X >> 8; // adv[1] = acceleration.AXIS_X & 0xff; // adv[2] = acceleration.AXIS_Y >> 8; // adv[3] = acceleration.AXIS_Y & 0xff; // adv[4] = acceleration.AXIS_Z >> 8; // adv[5] = acceleration.AXIS_Z & 0xff; // UpdateAdvStr(adv); } } void fft(void) { uint8_t i; float32_t maxValue; uint32_t maxIndex; /* Process the data through the CFFT/CIFFT module */ arm_cfft_f32(&arm_cfft_sR_f32_len1024, fft_data, ifftFlag, doBitReverse); arm_cmplx_mag_f32(fft_data, fft_mod, fftSize); advIndex = 0; //提取最大的5个数和索引 for(i=0;i<5;i++) { arm_max_f32(fft_mod, fftSize / 2, &maxValue, &maxIndex); //直流分量 if(maxIndex == 0) { maxValues[i] = (int)(maxValue / FFT_LEN); } else { maxValues[i] = (int)(maxValue / FFT_LEN / 2); } //频率 maxFqc[i] = maxIndex * AUDIO_SAMPLING_FREQUENCY/fftSize; //添加到广播数据 adv[advIndex++] = maxValues[i] >> 8; adv[advIndex++] = maxValues[i]; adv[advIndex++] = maxFqc[i] >> 8; adv[advIndex++] = maxFqc[i]; //最大值置0,为了下一次取最大值(除当前最大值以外的最大值) fft_mod[maxIndex] = 0; } //更新广播数据 UpdateAdvStr(adv); } //填充音频数据到fft数组 void FillInFFT(void) { static uint32_t IndexOut = 0; uint32_t indexIn; //如果上一次FFT未完成则退出 if(doFFT) return; for(indexIn=0;indexIn<AUDIO_IN_BUF_LEN;indexIn++) { //实部 fft_data[IndexOut++] = (int16_t)PCM_Buffer[indexIn];//fft_data1[IndexOut];;//(int16_t)PCM_Buffer[indexIn]; //虚部 fft_data[IndexOut++] = 0; } //数据填满后进行FFT转换 if(IndexOut==FFT_LEN) { doFFT = 1; //FFT转换 fft(); // memset(fft_mod,0,sizeof(fft_mod)/sizeof(fft_mod[0])); // memset(fft_data,0,sizeof(fft_data)/sizeof(fft_data[0])); IndexOut=0; doFFT = 0; } } /** * @brief Transfer Complete user callback, called by BSP functions. * @param None * @retval None */ void BSP_AUDIO_IN_TransferComplete_CallBack(void) { FillInFFT(); } /** * @brief Half Transfer Complete user callback, called by BSP functions. * @param None * @retval None */ void BSP_AUDIO_IN_HalfTransfer_CallBack(void) { FillInFFT(); } #ifdef USE_FULL_ASSERT /** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @param file: pointer to the source file name * @param line: assert_param error line source number * @retval None */ void assert_failed(uint8_t* file, uint32_t line) { /* USER CODE BEGIN 6 */ /* User can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* USER CODE END 6 */ } #endif void EnterStopMode(void) { __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU); HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI); } /** * @brief EXTI line detection callback. * @param uint16_t GPIO_Pin Specifies the pins connected EXTI line * @retval None */ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { HCI_Isr(); } |