add MP45DT02 testing function

This commit is contained in:
2025-03-13 19:33:50 +02:00
parent 0f1d8b71d7
commit 3300c5eebd
16 changed files with 8528 additions and 51 deletions

56
Core/Src/MP45DT02.c Normal file
View File

@@ -0,0 +1,56 @@
#include "memory.h"
#include "main.h"
#include "MP45DT02.h"
#include "lcd.h"
#define SAMPLE_AMOUNT 256
extern I2S_HandleTypeDef hi2s2;
void MP45DT02_run_test(void)
{
DISPLAY_CLEAR;
display_write_data_seq("MP45DT02 Mic");
uint16_t sample_buffer[SAMPLE_AMOUNT];
memset(sample_buffer, 0, 2 * SAMPLE_AMOUNT);
for (size_t t = 0; t < 5; t++) {
HAL_I2S_Receive(&hi2s2, sample_buffer, SAMPLE_AMOUNT, 1000);
for (size_t i = 0; i < SAMPLE_AMOUNT / 2; i++)
if (((uint32_t *) sample_buffer)[i]) {
DISPLAY_CLEAR;
display_write_data_seq("MP45DT02 Mic");
if (!t) {
DISPLAY_SET_CURSOR(1, 1);
display_write_data_seq("OK");
} else {
DISPLAY_SET_CURSOR(1, 0);
display_write_data_byte('0' + t);
display_write_data_seq(" full 0 samples");
}
return;
}
// if ended up here, then no samples equaled to 1; that is not a good sign
DISPLAY_CLEAR;
display_write_data_seq("MP45DT02 Mic");
DISPLAY_SET_CURSOR(1, 0);
display_write_data_byte('1' + t);
display_write_data_seq("/5: no 1 sampled");
HAL_Delay(1000);
}
// t = 5, not a single 1 was recorded; microphone might be broken
DISPLAY_CLEAR;
display_write_data_seq("MP45DT02 Mic");
DISPLAY_SET_CURSOR(1, 0);
display_write_data_seq("No good samples!");
}

View File

@@ -64,6 +64,6 @@ void SST25VF016B_run_test(void)
DISPLAY_SET_CURSOR(1, 1);
display_write_data_seq("OK");
DISPLAY_SET_CURSOR(1, 8);
display_write_data_seq((char) rx_buffer);
display_write_data_seq((char *) rx_buffer);
}
}

View File

@@ -30,6 +30,8 @@
#include "CS43L22.h"
#include "SST25VF016B.h"
#include "LIS302DL.h"
#include "DHT11.h"
#include "MP45DT02.h"
/* USER CODE END Includes */
@@ -56,8 +58,12 @@ ADC_HandleTypeDef hadc1;
I2C_HandleTypeDef hi2c1;
I2S_HandleTypeDef hi2s2;
SPI_HandleTypeDef hspi1;
TIM_HandleTypeDef htim2;
/* USER CODE BEGIN PV */
void ((*executors[])(void)) = {
@@ -68,7 +74,8 @@ void ((*executors[])(void)) = {
CS43L22_run_test,
SST25VF016B_run_test,
LIS302DL_run_test,
LIS302DL_run_test_dynamic
LIS302DL_run_test_dynamic,
MP45DT02_run_test
};
void ((*cleanup_functions[])(void)) = {
@@ -79,10 +86,21 @@ void ((*cleanup_functions[])(void)) = {
CS43L22_cleanup,
NULL,
NULL,
NULL,
NULL
};
int delay_between_runs[] = {250, 250, -1, -1, -1, -1, -1, 200};
int delay_between_runs[] = {
250,
250,
-1,
-1,
-1,
-1,
-1,
200,
-1
};
size_t current_executor_id = 0;
@@ -96,6 +114,8 @@ static void MX_GPIO_Init(void);
static void MX_ADC1_Init(void);
static void MX_I2C1_Init(void);
static void MX_SPI1_Init(void);
static void MX_TIM2_Init(void);
static void MX_I2S2_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
@@ -106,7 +126,7 @@ static void MX_SPI1_Init(void);
void switch_to_next_test(void)
{
current_executor_id += 1;
current_executor_id %= 8;
current_executor_id %= 9;
}
void buttons_switch_to_interrupt(void)
@@ -263,9 +283,15 @@ int main(void)
MX_ADC1_Init();
MX_I2C1_Init();
MX_SPI1_Init();
MX_TIM2_Init();
MX_I2S2_Init();
/* USER CODE BEGIN 2 */
buttons_interrupt_enabled = 0;
HAL_TIM_Base_Start(&htim2);
TIM2->CNT = 0;
HAL_TIM_Base_Stop(&htim2);
GPIOD->ODR = 0x1000;
display_init();
@@ -317,6 +343,14 @@ void SystemClock_Config(void)
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Macro to configure the PLL multiplication factor
*/
__HAL_RCC_PLL_PLLM_CONFIG(16);
/** Macro to configure the PLL clock source
*/
__HAL_RCC_PLL_PLLSOURCE_CONFIG(RCC_PLLSOURCE_HSI);
/** Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
@@ -329,6 +363,7 @@ void SystemClock_Config(void)
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
@@ -435,6 +470,40 @@ static void MX_I2C1_Init(void)
}
/**
* @brief I2S2 Initialization Function
* @param None
* @retval None
*/
static void MX_I2S2_Init(void)
{
/* USER CODE BEGIN I2S2_Init 0 */
/* USER CODE END I2S2_Init 0 */
/* USER CODE BEGIN I2S2_Init 1 */
/* USER CODE END I2S2_Init 1 */
hi2s2.Instance = SPI2;
hi2s2.Init.Mode = I2S_MODE_MASTER_RX;
hi2s2.Init.Standard = I2S_STANDARD_PHILIPS;
hi2s2.Init.DataFormat = I2S_DATAFORMAT_16B;
hi2s2.Init.MCLKOutput = I2S_MCLKOUTPUT_DISABLE;
hi2s2.Init.AudioFreq = I2S_AUDIOFREQ_48K;
hi2s2.Init.CPOL = I2S_CPOL_LOW;
hi2s2.Init.ClockSource = I2S_CLOCK_PLL;
hi2s2.Init.FullDuplexMode = I2S_FULLDUPLEXMODE_DISABLE;
if (HAL_I2S_Init(&hi2s2) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN I2S2_Init 2 */
/* USER CODE END I2S2_Init 2 */
}
/**
* @brief SPI1 Initialization Function
* @param None
@@ -473,6 +542,51 @@ static void MX_SPI1_Init(void)
}
/**
* @brief TIM2 Initialization Function
* @param None
* @retval None
*/
static void MX_TIM2_Init(void)
{
/* USER CODE BEGIN TIM2_Init 0 */
/* USER CODE END TIM2_Init 0 */
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
/* USER CODE BEGIN TIM2_Init 1 */
/* USER CODE END TIM2_Init 1 */
htim2.Instance = TIM2;
htim2.Init.Prescaler = 0;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = 0xffffffff;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM2_Init 2 */
/* USER CODE END TIM2_Init 2 */
}
/**
* @brief GPIO Initialization Function
* @param None
@@ -485,19 +599,19 @@ static void MX_GPIO_Init(void)
/* USER CODE END MX_GPIO_Init_1 */
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOE_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_7|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12
|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15, GPIO_PIN_RESET);
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15
|GPIO_PIN_4|GPIO_PIN_7, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14
|GPIO_PIN_15|GPIO_PIN_4|GPIO_PIN_7, GPIO_PIN_RESET);
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_7, GPIO_PIN_RESET);
@@ -517,10 +631,10 @@ static void MX_GPIO_Init(void)
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
/*Configure GPIO pins : PD12 PD13 PD14 PD15
PD4 PD7 */
GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15
|GPIO_PIN_4|GPIO_PIN_7;
/*Configure GPIO pins : PD11 PD12 PD13 PD14
PD15 PD4 PD7 */
GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14
|GPIO_PIN_15|GPIO_PIN_4|GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;

View File

@@ -208,6 +208,96 @@ void HAL_I2C_MspDeInit(I2C_HandleTypeDef* hi2c)
}
/**
* @brief I2S MSP Initialization
* This function configures the hardware resources used in this example
* @param hi2s: I2S handle pointer
* @retval None
*/
void HAL_I2S_MspInit(I2S_HandleTypeDef* hi2s)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
if(hi2s->Instance==SPI2)
{
/* USER CODE BEGIN SPI2_MspInit 0 */
/* USER CODE END SPI2_MspInit 0 */
/** Initializes the peripherals clock
*/
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_I2S;
PeriphClkInitStruct.PLLI2S.PLLI2SN = 192;
PeriphClkInitStruct.PLLI2S.PLLI2SR = 2;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
{
Error_Handler();
}
/* Peripheral clock enable */
__HAL_RCC_SPI2_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/**I2S2 GPIO Configuration
PC3 ------> I2S2_SD
PB10 ------> I2S2_CK
PB12 ------> I2S2_WS
*/
GPIO_InitStruct.Pin = GPIO_PIN_3;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_12;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* USER CODE BEGIN SPI2_MspInit 1 */
/* USER CODE END SPI2_MspInit 1 */
}
}
/**
* @brief I2S MSP De-Initialization
* This function freeze the hardware resources used in this example
* @param hi2s: I2S handle pointer
* @retval None
*/
void HAL_I2S_MspDeInit(I2S_HandleTypeDef* hi2s)
{
if(hi2s->Instance==SPI2)
{
/* USER CODE BEGIN SPI2_MspDeInit 0 */
/* USER CODE END SPI2_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_SPI2_CLK_DISABLE();
/**I2S2 GPIO Configuration
PC3 ------> I2S2_SD
PB10 ------> I2S2_CK
PB12 ------> I2S2_WS
*/
HAL_GPIO_DeInit(GPIOC, GPIO_PIN_3);
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_10|GPIO_PIN_12);
/* USER CODE BEGIN SPI2_MspDeInit 1 */
/* USER CODE END SPI2_MspDeInit 1 */
}
}
/**
* @brief SPI MSP Initialization
* This function configures the hardware resources used in this example
@@ -286,6 +376,51 @@ void HAL_SPI_MspDeInit(SPI_HandleTypeDef* hspi)
}
/**
* @brief TIM_Base MSP Initialization
* This function configures the hardware resources used in this example
* @param htim_base: TIM_Base handle pointer
* @retval None
*/
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base)
{
if(htim_base->Instance==TIM2)
{
/* USER CODE BEGIN TIM2_MspInit 0 */
/* USER CODE END TIM2_MspInit 0 */
/* Peripheral clock enable */
__HAL_RCC_TIM2_CLK_ENABLE();
/* USER CODE BEGIN TIM2_MspInit 1 */
/* USER CODE END TIM2_MspInit 1 */
}
}
/**
* @brief TIM_Base MSP De-Initialization
* This function freeze the hardware resources used in this example
* @param htim_base: TIM_Base handle pointer
* @retval None
*/
void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* htim_base)
{
if(htim_base->Instance==TIM2)
{
/* USER CODE BEGIN TIM2_MspDeInit 0 */
/* USER CODE END TIM2_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_TIM2_CLK_DISABLE();
/* USER CODE BEGIN TIM2_MspDeInit 1 */
/* USER CODE END TIM2_MspDeInit 1 */
}
}
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */