Files
2020-09-26 15:34:18 -04:00

1235 lines
38 KiB
C

/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
ADC_HandleTypeDef hadc3;
SPI_HandleTypeDef hspi4;
TIM_HandleTypeDef htim6;
/* USER CODE BEGIN PV */
uint8_t LEDMode = 0;
bool LEDDesign_PendingChange = false;
uint8_t LEDData[64][3] = { 0 }; // 64 LEDs for a 8x8 WS2812B array, in GRB format
uint8_t LEDData_WS2812B[66][3][3] = { 0 }; // For the LEDData Arrays, index 64 and 65
// will always be zeroes, in order to have
// SPI transmit a RESET signal of 76.8 μs,
// which is greater than the minimum 50 μs
// needed for the WS2812B ICs to signify
// the end of the data
uint32_t WS2812BConvertedData;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_SPI4_Init(void);
static void MX_TIM6_Init(void);
static void MX_ADC3_Init(void);
/* USER CODE BEGIN PFP */
uint8_t getSoundLevel(void);
void LEDDesign_Off(void);
void LEDDesign_ColorWhite(void);
void LEDDesign_ColorBlue(void);
void LEDDesign_ColorGreen(void);
void LEDDesign_ColorRed(void);
void LEDDesign_Crazy(void);
void LEDDesign_Smile(void);
void LEDDesign_Smile_Audio(void);
void LEDDesign_SuperCrazy(void);
uint8_t lookupLED(uint8_t column, uint8_t row);
void setLED(uint8_t pixelNumber, uint8_t redLevel, uint8_t greenLevel, uint8_t blueLevel);
void updateWS2812BData(void);
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_SPI4_Init();
MX_TIM6_Init();
MX_ADC3_Init();
/* USER CODE BEGIN 2 */
updateWS2812BData();
HAL_SPI_Transmit_IT(&hspi4, (uint8_t*) &LEDData, (uint16_t) 66 * 3 * 3);
HAL_ADC_Start(&hadc3);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1) {
if(LEDDesign_PendingChange){
LEDDesign_Off();
}else{
switch (LEDMode) {
case 0:
LEDDesign_Smile();
break;
case 1:
LEDDesign_Smile_Audio();
break;
case 2:
LEDDesign_Crazy();
break;
case 3:
LEDDesign_SuperCrazy();
break;
case 4:
LEDDesign_ColorWhite();
break;
case 5:
LEDDesign_ColorRed();
break;
case 6:
LEDDesign_ColorGreen();
break;
case 7:
LEDDesign_ColorBlue();
break;
default:
LEDDesign_Off();
}
}
updateWS2812BData();
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 4;
RCC_OscInitStruct.PLL.PLLN = 160;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 7;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief ADC3 Initialization Function
* @param None
* @retval None
*/
static void MX_ADC3_Init(void)
{
/* USER CODE BEGIN ADC3_Init 0 */
/* USER CODE END ADC3_Init 0 */
ADC_ChannelConfTypeDef sConfig = {0};
/* USER CODE BEGIN ADC3_Init 1 */
/* USER CODE END ADC3_Init 1 */
/** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
*/
hadc3.Instance = ADC3;
hadc3.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
hadc3.Init.Resolution = ADC_RESOLUTION_8B;
hadc3.Init.ScanConvMode = DISABLE;
hadc3.Init.ContinuousConvMode = DISABLE;
hadc3.Init.DiscontinuousConvMode = DISABLE;
hadc3.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc3.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc3.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc3.Init.NbrOfConversion = 1;
hadc3.Init.DMAContinuousRequests = DISABLE;
hadc3.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
if (HAL_ADC_Init(&hadc3) != HAL_OK)
{
Error_Handler();
}
/** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_4;
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
if (HAL_ADC_ConfigChannel(&hadc3, &sConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN ADC3_Init 2 */
/* USER CODE END ADC3_Init 2 */
}
/**
* @brief SPI4 Initialization Function
* @param None
* @retval None
*/
static void MX_SPI4_Init(void)
{
/* USER CODE BEGIN SPI4_Init 0 */
/* USER CODE END SPI4_Init 0 */
/* USER CODE BEGIN SPI4_Init 1 */
/* USER CODE END SPI4_Init 1 */
/* SPI4 parameter configuration*/
hspi4.Instance = SPI4;
hspi4.Init.Mode = SPI_MODE_MASTER;
hspi4.Init.Direction = SPI_DIRECTION_2LINES;
hspi4.Init.DataSize = SPI_DATASIZE_8BIT;
hspi4.Init.CLKPolarity = SPI_POLARITY_LOW;
hspi4.Init.CLKPhase = SPI_PHASE_1EDGE;
hspi4.Init.NSS = SPI_NSS_SOFT;
hspi4.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;
hspi4.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi4.Init.TIMode = SPI_TIMODE_DISABLE;
hspi4.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi4.Init.CRCPolynomial = 10;
if (HAL_SPI_Init(&hspi4) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN SPI4_Init 2 */
/* USER CODE END SPI4_Init 2 */
}
/**
* @brief TIM6 Initialization Function
* @param None
* @retval None
*/
static void MX_TIM6_Init(void)
{
/* USER CODE BEGIN TIM6_Init 0 */
/* USER CODE END TIM6_Init 0 */
TIM_MasterConfigTypeDef sMasterConfig = {0};
/* USER CODE BEGIN TIM6_Init 1 */
/* USER CODE END TIM6_Init 1 */
htim6.Instance = TIM6;
htim6.Init.Prescaler = 4000;
htim6.Init.CounterMode = TIM_COUNTERMODE_UP;
htim6.Init.Period = 10000;
htim6.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
if (HAL_TIM_Base_Init(&htim6) != HAL_OK)
{
Error_Handler();
}
if (HAL_TIM_OnePulse_Init(&htim6, TIM_OPMODE_SINGLE) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim6, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM6_Init 2 */
/* USER CODE END TIM6_Init 2 */
}
/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOE_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOF_CLK_ENABLE();
__HAL_RCC_GPIOH_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOG_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOC, NCS_MEMS_SPI_Pin|CSX_Pin|OTG_FS_PSO_Pin, GPIO_PIN_RESET);
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(ACP_RST_GPIO_Port, ACP_RST_Pin, GPIO_PIN_RESET);
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOD, RDX_Pin|WRX_DCX_Pin, GPIO_PIN_RESET);
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOG, LD3_Pin|LD4_Pin, GPIO_PIN_RESET);
/*Configure GPIO pins : A0_Pin A1_Pin A2_Pin A3_Pin
A4_Pin A5_Pin SDNRAS_Pin A6_Pin
A7_Pin A8_Pin A9_Pin */
GPIO_InitStruct.Pin = A0_Pin|A1_Pin|A2_Pin|A3_Pin
|A4_Pin|A5_Pin|SDNRAS_Pin|A6_Pin
|A7_Pin|A8_Pin|A9_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF12_FMC;
HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
/*Configure GPIO pins : SPI5_SCK_Pin SPI5_MISO_Pin SPI5_MOSI_Pin */
GPIO_InitStruct.Pin = SPI5_SCK_Pin|SPI5_MISO_Pin|SPI5_MOSI_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF5_SPI5;
HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
/*Configure GPIO pin : ENABLE_Pin */
GPIO_InitStruct.Pin = ENABLE_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF14_LTDC;
HAL_GPIO_Init(ENABLE_GPIO_Port, &GPIO_InitStruct);
/*Configure GPIO pin : SDNWE_Pin */
GPIO_InitStruct.Pin = SDNWE_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF12_FMC;
HAL_GPIO_Init(SDNWE_GPIO_Port, &GPIO_InitStruct);
/*Configure GPIO pins : NCS_MEMS_SPI_Pin CSX_Pin OTG_FS_PSO_Pin */
GPIO_InitStruct.Pin = NCS_MEMS_SPI_Pin|CSX_Pin|OTG_FS_PSO_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
/*Configure GPIO pin : B1_Pin */
GPIO_InitStruct.Pin = B1_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
/*Configure GPIO pins : MEMS_INT1_Pin MEMS_INT2_Pin TP_INT1_Pin */
GPIO_InitStruct.Pin = MEMS_INT1_Pin|MEMS_INT2_Pin|TP_INT1_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/*Configure GPIO pins : B5_Pin VSYNC_Pin G2_Pin R4_Pin
R5_Pin */
GPIO_InitStruct.Pin = B5_Pin|VSYNC_Pin|G2_Pin|R4_Pin
|R5_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF14_LTDC;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/*Configure GPIO pin : ACP_RST_Pin */
GPIO_InitStruct.Pin = ACP_RST_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(ACP_RST_GPIO_Port, &GPIO_InitStruct);
/*Configure GPIO pin : OTG_FS_OC_Pin */
GPIO_InitStruct.Pin = OTG_FS_OC_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(OTG_FS_OC_GPIO_Port, &GPIO_InitStruct);
/*Configure GPIO pins : R3_Pin R6_Pin */
GPIO_InitStruct.Pin = R3_Pin|R6_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF9_LTDC;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/*Configure GPIO pin : BOOT1_Pin */
GPIO_InitStruct.Pin = BOOT1_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(BOOT1_GPIO_Port, &GPIO_InitStruct);
/*Configure GPIO pins : A10_Pin A11_Pin BA0_Pin BA1_Pin
SDCLK_Pin SDNCAS_Pin */
GPIO_InitStruct.Pin = A10_Pin|A11_Pin|BA0_Pin|BA1_Pin
|SDCLK_Pin|SDNCAS_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF12_FMC;
HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
/*Configure GPIO pins : D4_Pin D5_Pin D6_Pin D7_Pin
D8_Pin D9_Pin D10_Pin D11_Pin
D12_Pin NBL0_Pin NBL1_Pin */
GPIO_InitStruct.Pin = D4_Pin|D5_Pin|D6_Pin|D7_Pin
|D8_Pin|D9_Pin|D10_Pin|D11_Pin
|D12_Pin|NBL0_Pin|NBL1_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF12_FMC;
HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
/*Configure GPIO pins : G4_Pin G5_Pin B6_Pin B7_Pin */
GPIO_InitStruct.Pin = G4_Pin|G5_Pin|B6_Pin|B7_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF14_LTDC;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/*Configure GPIO pins : OTG_HS_ID_Pin OTG_HS_DM_Pin OTG_HS_DP_Pin */
GPIO_InitStruct.Pin = OTG_HS_ID_Pin|OTG_HS_DM_Pin|OTG_HS_DP_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF12_OTG_HS_FS;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/*Configure GPIO pin : VBUS_HS_Pin */
GPIO_InitStruct.Pin = VBUS_HS_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(VBUS_HS_GPIO_Port, &GPIO_InitStruct);
/*Configure GPIO pins : D13_Pin D14_Pin D15_Pin D0_Pin
D1_Pin D2_Pin D3_Pin */
GPIO_InitStruct.Pin = D13_Pin|D14_Pin|D15_Pin|D0_Pin
|D1_Pin|D2_Pin|D3_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF12_FMC;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
/*Configure GPIO pin : TE_Pin */
GPIO_InitStruct.Pin = TE_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(TE_GPIO_Port, &GPIO_InitStruct);
/*Configure GPIO pins : RDX_Pin WRX_DCX_Pin */
GPIO_InitStruct.Pin = RDX_Pin|WRX_DCX_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
/*Configure GPIO pins : R7_Pin DOTCLK_Pin B3_Pin */
GPIO_InitStruct.Pin = R7_Pin|DOTCLK_Pin|B3_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF14_LTDC;
HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
/*Configure GPIO pins : HSYNC_Pin G6_Pin R2_Pin */
GPIO_InitStruct.Pin = HSYNC_Pin|G6_Pin|R2_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF14_LTDC;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
/*Configure GPIO pin : I2C3_SDA_Pin */
GPIO_InitStruct.Pin = I2C3_SDA_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF4_I2C3;
HAL_GPIO_Init(I2C3_SDA_GPIO_Port, &GPIO_InitStruct);
/*Configure GPIO pin : I2C3_SCL_Pin */
GPIO_InitStruct.Pin = I2C3_SCL_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF4_I2C3;
HAL_GPIO_Init(I2C3_SCL_GPIO_Port, &GPIO_InitStruct);
/*Configure GPIO pins : STLINK_RX_Pin STLINK_TX_Pin */
GPIO_InitStruct.Pin = STLINK_RX_Pin|STLINK_TX_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/*Configure GPIO pins : G7_Pin B2_Pin */
GPIO_InitStruct.Pin = G7_Pin|B2_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF14_LTDC;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
/*Configure GPIO pins : G3_Pin B4_Pin */
GPIO_InitStruct.Pin = G3_Pin|B4_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF9_LTDC;
HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
/*Configure GPIO pins : LD3_Pin LD4_Pin */
GPIO_InitStruct.Pin = LD3_Pin|LD4_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
/*Configure GPIO pins : SDCKE1_Pin SDNE1_Pin */
GPIO_InitStruct.Pin = SDCKE1_Pin|SDNE1_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF12_FMC;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* EXTI interrupt init*/
HAL_NVIC_SetPriority(EXTI0_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(EXTI0_IRQn);
}
/* USER CODE BEGIN 4 */
uint8_t getSoundLevel(void){
// Adjustment / Calibration
static uint8_t cutoff = 90;
// By order of process
uint8_t samples[8];
uint16_t estimatedSoundLevel = 0;
static uint16_t averagedReturnValue = 0;
uint16_t returnValue;
for(uint8_t i = 0; i < sizeof(samples); ++i){
HAL_ADC_Start(&hadc3);
HAL_ADC_PollForConversion(&hadc3, (uint32_t) 20);
samples[i] = HAL_ADC_GetValue(&hadc3);
}
for(uint8_t i = 0; i < sizeof(samples); ++i){
estimatedSoundLevel += samples[i];
}
estimatedSoundLevel /= sizeof(samples);
if(estimatedSoundLevel <= cutoff){
returnValue = 0;
}else{
returnValue = (uint16_t) (2 * (estimatedSoundLevel - cutoff));
}
averagedReturnValue += ((int32_t) returnValue - (int32_t) averagedReturnValue) / 3;
if(averagedReturnValue >= 0xFF){
return 0xFF;
}else if(averagedReturnValue <= 10){
return 0x00;
}else{
return averagedReturnValue;
}
}
void LEDDesign_Off(void){
for(uint8_t i = 0; i < 64; ++i){
for(uint8_t j = 0; j < 3; ++j){
LEDData[i][j] = 0x00;
}
}
}
void LEDDesign_ColorWhite(void){
for(uint8_t i = 0; i < 64; ++i){
for(uint8_t j = 0; j < 3; ++j){
LEDData[i][j] = 0xFF;
}
}
}
void LEDDesign_ColorBlue(void){
for(uint8_t i = 0; i < 64; ++i){
LEDData[i][0] = 0x00;
}
for(uint8_t i = 0; i < 64; ++i){
LEDData[i][1] = 0x00;
}
for(uint8_t i = 0; i < 64; ++i){
LEDData[i][2] = 0xFF;
}
}
void LEDDesign_ColorGreen(void){
for(uint8_t i = 0; i < 64; ++i){
LEDData[i][0] = 0xFF;
}
for(uint8_t i = 0; i < 64; ++i){
LEDData[i][1] = 0x00;
}
for(uint8_t i = 0; i < 64; ++i){
LEDData[i][2] = 0x00;
}
}
void LEDDesign_ColorRed(void){
for(uint8_t i = 0; i < 64; ++i){
LEDData[i][0] = 0x00;
}
for(uint8_t i = 0; i < 64; ++i){
LEDData[i][1] = 0xFF;
}
for(uint8_t i = 0; i < 64; ++i){
LEDData[i][2] = 0x00;
}
}
void LEDDesign_Crazy(void){
HAL_Delay(50);
for(uint8_t i = 0; i < 64; ++i){
for(uint8_t j = 0; j < 3; ++j){
LEDData[i][j] = (uint8_t) (0xFF * (((float) rand()) / RAND_MAX));
}
}
}
void LEDDesign_Smile(void){
setLED(lookupLED(0,0), 0x00, 0x00, 0x00);
setLED(lookupLED(0,1), 0x00, 0x00, 0x00);
setLED(lookupLED(0,2), 0x00, 0x00, 0x00);
setLED(lookupLED(0,3), 0x00, 0x00, 0x7F);
setLED(lookupLED(0,4), 0x00, 0x00, 0x00);
setLED(lookupLED(0,5), 0x00, 0x00, 0x00);
setLED(lookupLED(0,6), 0x00, 0x00, 0x00);
setLED(lookupLED(0,7), 0x00, 0x00, 0x00);
setLED(lookupLED(1,0), 0x00, 0x00, 0x00);
setLED(lookupLED(1,1), 0x00, 0x00, 0x00);
setLED(lookupLED(1,2), 0x00, 0x00, 0x7F);
setLED(lookupLED(1,3), 0x00, 0x00, 0x00);
setLED(lookupLED(1,4), 0x00, 0x00, 0x00);
setLED(lookupLED(1,5), 0x00, 0x00, 0x00);
setLED(lookupLED(1,6), 0x00, 0x00, 0x00);
setLED(lookupLED(1,7), 0x00, 0x00, 0x00);
setLED(lookupLED(2,0), 0x00, 0x00, 0x00);
setLED(lookupLED(2,1), 0x00, 0x00, 0x00);
setLED(lookupLED(2,2), 0x00, 0x00, 0x7F);
setLED(lookupLED(2,3), 0x00, 0x00, 0x00);
setLED(lookupLED(2,4), 0x00, 0x00, 0x00);
setLED(lookupLED(2,5), 0x00, 0x00, 0x00);
setLED(lookupLED(2,6), 0x00, 0x00, 0x00);
setLED(lookupLED(2,7), 0x00, 0x00, 0x00);
setLED(lookupLED(3,0), 0x00, 0x00, 0x00);
setLED(lookupLED(3,1), 0x00, 0x00, 0x00);
setLED(lookupLED(3,2), 0x00, 0x00, 0x7F);
setLED(lookupLED(3,3), 0x00, 0x00, 0x00);
setLED(lookupLED(3,4), 0x00, 0x00, 0x00);
setLED(lookupLED(3,5), 0x00, 0x00, 0x00);
setLED(lookupLED(3,6), 0x00, 0x00, 0x00);
setLED(lookupLED(3,7), 0x00, 0x00, 0x00);
setLED(lookupLED(4,0), 0x00, 0x00, 0x00);
setLED(lookupLED(4,1), 0x00, 0x00, 0x00);
setLED(lookupLED(4,2), 0x00, 0x00, 0x7F);
setLED(lookupLED(4,3), 0x00, 0x00, 0x00);
setLED(lookupLED(4,4), 0x00, 0x00, 0x00);
setLED(lookupLED(4,5), 0x00, 0x00, 0x00);
setLED(lookupLED(4,6), 0x00, 0x00, 0x00);
setLED(lookupLED(4,7), 0x00, 0x00, 0x00);
setLED(lookupLED(5,0), 0x00, 0x00, 0x00);
setLED(lookupLED(5,1), 0x00, 0x00, 0x00);
setLED(lookupLED(5,2), 0x00, 0x00, 0x7F);
setLED(lookupLED(5,3), 0x00, 0x00, 0x00);
setLED(lookupLED(5,4), 0x00, 0x00, 0x00);
setLED(lookupLED(5,5), 0x00, 0x00, 0x00);
setLED(lookupLED(5,6), 0x00, 0x00, 0x00);
setLED(lookupLED(5,7), 0x00, 0x00, 0x00);
setLED(lookupLED(6,0), 0x00, 0x00, 0x00);
setLED(lookupLED(6,1), 0x00, 0x00, 0x00);
setLED(lookupLED(6,2), 0x00, 0x00, 0x7F);
setLED(lookupLED(6,3), 0x00, 0x00, 0x00);
setLED(lookupLED(6,4), 0x00, 0x00, 0x00);
setLED(lookupLED(6,5), 0x00, 0x00, 0x00);
setLED(lookupLED(6,6), 0x00, 0x00, 0x00);
setLED(lookupLED(6,7), 0x00, 0x00, 0x00);
setLED(lookupLED(7,0), 0x00, 0x00, 0x00);
setLED(lookupLED(7,1), 0x00, 0x00, 0x00);
setLED(lookupLED(7,2), 0x00, 0x00, 0x00);
setLED(lookupLED(7,3), 0x00, 0x00, 0x7F);
setLED(lookupLED(7,4), 0x00, 0x00, 0x00);
setLED(lookupLED(7,5), 0x00, 0x00, 0x00);
setLED(lookupLED(7,6), 0x00, 0x00, 0x00);
setLED(lookupLED(7,7), 0x00, 0x00, 0x00);
}
void LEDDesign_Smile_Audio(void){
uint8_t currentSoundLevel = getSoundLevel();
setLED(lookupLED(0,0), 0x00, 0x00, 0x00);
setLED(lookupLED(0,1), 0x00, 0x00, 0x00);
setLED(lookupLED(0,2), 0x00, 0x00, currentSoundLevel * 0.25);
setLED(lookupLED(0,3), 0x00, 0x00, 0x7F + (currentSoundLevel * 0.5));
setLED(lookupLED(0,4), 0x00, 0x00, currentSoundLevel * 0.25);
setLED(lookupLED(0,5), 0x00, 0x00, 0x00);
setLED(lookupLED(0,6), 0x00, 0x00, 0x00);
setLED(lookupLED(0,7), 0x00, 0x00, 0x00);
setLED(lookupLED(1,0), 0x00, 0x00, 0x00);
setLED(lookupLED(1,1), 0x00, 0x00, currentSoundLevel * 0.5);
setLED(lookupLED(1,2), 0x00, 0x00, 0x7F + (currentSoundLevel * 0.5));
setLED(lookupLED(1,3), 0x00, 0x00, currentSoundLevel);
setLED(lookupLED(1,4), 0x00, 0x00, currentSoundLevel);
setLED(lookupLED(1,5), 0x00, 0x00, currentSoundLevel * 0.5);
setLED(lookupLED(1,6), 0x00, 0x00, 0x00);
setLED(lookupLED(1,7), 0x00, 0x00, 0x00);
setLED(lookupLED(2,0), 0x00, 0x00, 0x00);
setLED(lookupLED(2,1), 0x00, 0x00, currentSoundLevel * 0.5);
setLED(lookupLED(2,2), 0x00, 0x00, 0x7F + (currentSoundLevel * 0.5));
setLED(lookupLED(2,3), 0x00, 0x00, currentSoundLevel);
setLED(lookupLED(2,4), 0x00, 0x00, currentSoundLevel);
setLED(lookupLED(2,5), 0x00, 0x00, currentSoundLevel * 0.5);
setLED(lookupLED(2,6), 0x00, 0x00, 0x00);
setLED(lookupLED(2,7), 0x00, 0x00, 0x00);
setLED(lookupLED(3,0), 0x00, 0x00, currentSoundLevel * 0.25);
setLED(lookupLED(3,1), 0x00, 0x00, currentSoundLevel * 0.5);
setLED(lookupLED(3,2), 0x00, 0x00, 0x7F + (currentSoundLevel * 0.5));
setLED(lookupLED(3,3), 0x00, 0x00, currentSoundLevel);
setLED(lookupLED(3,4), 0x00, 0x00, currentSoundLevel);
setLED(lookupLED(3,5), 0x00, 0x00, currentSoundLevel * 0.5);
setLED(lookupLED(3,6), 0x00, 0x00, currentSoundLevel * 0.25);
setLED(lookupLED(3,7), 0x00, 0x00, 0x00);
setLED(lookupLED(4,0), 0x00, 0x00, currentSoundLevel * 0.25);
setLED(lookupLED(4,1), 0x00, 0x00, currentSoundLevel * 0.5);
setLED(lookupLED(4,2), 0x00, 0x00, 0x7F + (currentSoundLevel * 0.5));
setLED(lookupLED(4,3), 0x00, 0x00, currentSoundLevel);
setLED(lookupLED(4,4), 0x00, 0x00, currentSoundLevel);
setLED(lookupLED(4,5), 0x00, 0x00, currentSoundLevel * 0.5);
setLED(lookupLED(4,6), 0x00, 0x00, currentSoundLevel * 0.25);
setLED(lookupLED(4,7), 0x00, 0x00, 0x00);
setLED(lookupLED(5,0), 0x00, 0x00, 0x00);
setLED(lookupLED(5,1), 0x00, 0x00, currentSoundLevel * 0.5);
setLED(lookupLED(5,2), 0x00, 0x00, 0x7F + (currentSoundLevel * 0.5));
setLED(lookupLED(5,3), 0x00, 0x00, currentSoundLevel);
setLED(lookupLED(5,4), 0x00, 0x00, currentSoundLevel);
setLED(lookupLED(5,5), 0x00, 0x00, currentSoundLevel * 0.5);
setLED(lookupLED(5,6), 0x00, 0x00, 0x00);
setLED(lookupLED(5,7), 0x00, 0x00, 0x00);
setLED(lookupLED(6,0), 0x00, 0x00, 0x00);
setLED(lookupLED(6,1), 0x00, 0x00, currentSoundLevel * 0.5);
setLED(lookupLED(6,2), 0x00, 0x00, 0x7F + (currentSoundLevel * 0.5));
setLED(lookupLED(6,3), 0x00, 0x00, currentSoundLevel);
setLED(lookupLED(6,4), 0x00, 0x00, currentSoundLevel);
setLED(lookupLED(6,5), 0x00, 0x00, currentSoundLevel * 0.5);
setLED(lookupLED(6,6), 0x00, 0x00, 0x00);
setLED(lookupLED(6,7), 0x00, 0x00, 0x00);
setLED(lookupLED(7,0), 0x00, 0x00, 0x00);
setLED(lookupLED(7,1), 0x00, 0x00, 0x00);
setLED(lookupLED(7,2), 0x00, 0x00, currentSoundLevel * 0.25);
setLED(lookupLED(7,3), 0x00, 0x00, 0x7F + (currentSoundLevel * 0.5));
setLED(lookupLED(7,4), 0x00, 0x00, currentSoundLevel * 0.25);
setLED(lookupLED(7,5), 0x00, 0x00, 0x00);
setLED(lookupLED(7,6), 0x00, 0x00, 0x00);
setLED(lookupLED(7,7), 0x00, 0x00, 0x00);
}
void LEDDesign_SuperCrazy(void){
HAL_Delay(50);
uint8_t randomByte = (uint8_t) (0xFF * (((float) rand()) / RAND_MAX));
for(uint8_t i = 0; i < 64; ++i){
LEDData[i][0] = randomByte;
}
randomByte = (uint8_t) (0xFF * (((float) rand()) / RAND_MAX));
for(uint8_t i = 0; i < 64; ++i){
LEDData[i][1] = randomByte;
}
randomByte = (uint8_t) (0xFF * (((float) rand()) / RAND_MAX));
for(uint8_t i = 0; i < 64; ++i){
LEDData[i][2] = randomByte;
}
}
uint8_t lookupLED(uint8_t column, uint8_t row){
switch(column){
case 0:
switch(row){
case 0:
return 0;
break;
case 1:
return 15;
break;
case 2:
return 16;
break;
case 3:
return 31;
break;
case 4:
return 32;
break;
case 5:
return 47;
break;
case 6:
return 48;
break;
case 7:
return 63;
break;
}
break;
case 1:
switch(row){
case 0:
return 1;
break;
case 1:
return 14;
break;
case 2:
return 17;
break;
case 3:
return 30;
break;
case 4:
return 33;
break;
case 5:
return 46;
break;
case 6:
return 49;
break;
case 7:
return 62;
break;
}
break;
case 2:
switch(row){
case 0:
return 2;
break;
case 1:
return 13;
break;
case 2:
return 18;
break;
case 3:
return 29;
break;
case 4:
return 34;
break;
case 5:
return 45;
break;
case 6:
return 50;
break;
case 7:
return 61;
break;
}
break;
case 3:
switch(row){
case 0:
return 3;
break;
case 1:
return 12;
break;
case 2:
return 19;
break;
case 3:
return 28;
break;
case 4:
return 35;
break;
case 5:
return 44;
break;
case 6:
return 51;
break;
case 7:
return 60;
break;
}
break;
case 4:
switch(row){
case 0:
return 4;
break;
case 1:
return 11;
break;
case 2:
return 20;
break;
case 3:
return 27;
break;
case 4:
return 36;
break;
case 5:
return 43;
break;
case 6:
return 52;
break;
case 7:
return 59;
break;
}
break;
case 5:
switch(row){
case 0:
return 5;
break;
case 1:
return 10;
break;
case 2:
return 21;
break;
case 3:
return 26;
break;
case 4:
return 37;
break;
case 5:
return 42;
break;
case 6:
return 53;
break;
case 7:
return 58;
break;
}
break;
case 6:
switch(row){
case 0:
return 6;
break;
case 1:
return 9;
break;
case 2:
return 22;
break;
case 3:
return 25;
break;
case 4:
return 38;
break;
case 5:
return 41;
break;
case 6:
return 54;
break;
case 7:
return 57;
break;
}
break;
case 7:
switch(row){
case 0:
return 7;
break;
case 1:
return 8;
break;
case 2:
return 23;
break;
case 3:
return 24;
break;
case 4:
return 39;
break;
case 5:
return 40;
break;
case 6:
return 55;
break;
case 7:
return 56;
break;
}
break;
}
return 0;
}
void setLED(uint8_t pixelNumber, uint8_t redLevel, uint8_t greenLevel, uint8_t blueLevel){
LEDData[pixelNumber][0] = greenLevel;
LEDData[pixelNumber][1] = redLevel;
LEDData[pixelNumber][2] = blueLevel;
}
void updateWS2812BData(void){
uint8_t byteToConvert;
for (uint8_t i = 0; i < 64; ++i) {
for (uint8_t j = 0; j < 3; ++j) {
byteToConvert = LEDData[i][j];
switch((byteToConvert & 0xF0) >> 4){
case 0x00:
WS2812BConvertedData = 0x00924000;
break;
case 0x01:
WS2812BConvertedData = 0x00926000;
break;
case 0x02:
WS2812BConvertedData = 0x00934000;
break;
case 0x03:
WS2812BConvertedData = 0x00936000;
break;
case 0x04:
WS2812BConvertedData = 0x009A4000;
break;
case 0x05:
WS2812BConvertedData = 0x009A6000;
break;
case 0x06:
WS2812BConvertedData = 0x009B4000;
break;
case 0x07:
WS2812BConvertedData = 0x009B6000;
break;
case 0x08:
WS2812BConvertedData = 0x00D24000;
break;
case 0x09:
WS2812BConvertedData = 0x00D26000;
break;
case 0x0A:
WS2812BConvertedData = 0x00D34000;
break;
case 0x0B:
WS2812BConvertedData = 0x00D36000;
break;
case 0x0C:
WS2812BConvertedData = 0x00DA4000;
break;
case 0x0D:
WS2812BConvertedData = 0x00DA6000;
break;
case 0x0E:
WS2812BConvertedData = 0x00DB4000;
break;
default: // 0x0F
WS2812BConvertedData = 0x00DB6000;
}
switch(byteToConvert & 0x0F){
case 0x00:
WS2812BConvertedData |= 0x00000924;
break;
case 0x01:
WS2812BConvertedData |= 0x00000926;
break;
case 0x02:
WS2812BConvertedData |= 0x00000934;
break;
case 0x03:
WS2812BConvertedData |= 0x00000936;
break;
case 0x04:
WS2812BConvertedData |= 0x000009A4;
break;
case 0x05:
WS2812BConvertedData |= 0x000009A6;
break;
case 0x06:
WS2812BConvertedData |= 0x000009B4;
break;
case 0x07:
WS2812BConvertedData |= 0x000009B6;
break;
case 0x08:
WS2812BConvertedData |= 0x00000D24;
break;
case 0x09:
WS2812BConvertedData |= 0x00000D26;
break;
case 0x0A:
WS2812BConvertedData |= 0x00000D34;
break;
case 0x0B:
WS2812BConvertedData |= 0x00000D36;
break;
case 0x0C:
WS2812BConvertedData |= 0x00000DA4;
break;
case 0x0D:
WS2812BConvertedData |= 0x00000DA6;
break;
case 0x0E:
WS2812BConvertedData |= 0x00000DB4;
break;
default: // 0x0F
WS2812BConvertedData |= 0x00000DB6;
}
LEDData_WS2812B[i][j][0] = (WS2812BConvertedData & 0x00FF0000) >> 16;
LEDData_WS2812B[i][j][1] = (WS2812BConvertedData & 0x0000FF00) >> 8;
LEDData_WS2812B[i][j][2] = WS2812BConvertedData & 0x000000FF;
}
}
}
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
/* USER CODE END Error_Handler_Debug */
}
#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,
tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/