Rename firmware folder
This commit is contained in:
58
firmware/Inc/adc.h
Normal file
58
firmware/Inc/adc.h
Normal file
@@ -0,0 +1,58 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* File Name : ADC.h
|
||||
* Description : This file provides code for the configuration
|
||||
* of the ADC instances.
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* <h2><center>© Copyright (c) 2020 STMicroelectronics.
|
||||
* All rights reserved.</center></h2>
|
||||
*
|
||||
* This software component is licensed by ST under Ultimate Liberty license
|
||||
* SLA0044, the "License"; You may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at:
|
||||
* www.st.com/SLA0044
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __adc_H
|
||||
#define __adc_H
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "init.h"
|
||||
|
||||
/* USER CODE BEGIN Includes */
|
||||
|
||||
/* USER CODE END Includes */
|
||||
|
||||
extern ADC_HandleTypeDef hadc;
|
||||
|
||||
/* USER CODE BEGIN Private defines */
|
||||
|
||||
/* USER CODE END Private defines */
|
||||
|
||||
void MX_ADC_Init(void);
|
||||
|
||||
/* USER CODE BEGIN Prototypes */
|
||||
|
||||
/* USER CODE END Prototypes */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif /*__ adc_H */
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|
||||
57
firmware/Inc/gpio.h
Normal file
57
firmware/Inc/gpio.h
Normal file
@@ -0,0 +1,57 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* File Name : gpio.h
|
||||
* Description : This file contains all the functions prototypes for
|
||||
* the gpio
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* <h2><center>© Copyright (c) 2020 STMicroelectronics.
|
||||
* All rights reserved.</center></h2>
|
||||
*
|
||||
* This software component is licensed by ST under Ultimate Liberty license
|
||||
* SLA0044, the "License"; You may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at:
|
||||
* www.st.com/SLA0044
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __gpio_H
|
||||
#define __gpio_H
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "init.h"
|
||||
|
||||
/* USER CODE BEGIN Includes */
|
||||
|
||||
/* USER CODE END Includes */
|
||||
|
||||
/* USER CODE BEGIN Private defines */
|
||||
|
||||
/* USER CODE END Private defines */
|
||||
|
||||
void MX_GPIO_Init(void);
|
||||
|
||||
/* USER CODE BEGIN Prototypes */
|
||||
|
||||
/* USER CODE END Prototypes */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif /*__ pinoutConfig_H */
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|
||||
86
firmware/Inc/init.h
Normal file
86
firmware/Inc/init.h
Normal file
@@ -0,0 +1,86 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : init.h
|
||||
* @brief : Header for init.c file.
|
||||
* This file contains the common defines of the application.
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* <h2><center>© Copyright (c) 2020 STMicroelectronics.
|
||||
* All rights reserved.</center></h2>
|
||||
*
|
||||
* This software component is licensed by ST under Ultimate Liberty license
|
||||
* SLA0044, the "License"; You may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at:
|
||||
* www.st.com/SLA0044
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
/* USER CODE END Header */
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __INIT_H
|
||||
#define __INIT_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "stm32f0xx_hal.h"
|
||||
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
|
||||
/* USER CODE END Includes */
|
||||
|
||||
/* Exported types ------------------------------------------------------------*/
|
||||
/* USER CODE BEGIN ET */
|
||||
|
||||
/* USER CODE END ET */
|
||||
|
||||
/* Exported constants --------------------------------------------------------*/
|
||||
/* USER CODE BEGIN EC */
|
||||
|
||||
/* USER CODE END EC */
|
||||
|
||||
/* Exported macro ------------------------------------------------------------*/
|
||||
/* USER CODE BEGIN EM */
|
||||
|
||||
/* USER CODE END EM */
|
||||
|
||||
/* Exported functions prototypes ---------------------------------------------*/
|
||||
void Error_Handler(void);
|
||||
void init(void);
|
||||
|
||||
/* USER CODE BEGIN EFP */
|
||||
|
||||
/* USER CODE END EFP */
|
||||
|
||||
/* Private defines -----------------------------------------------------------*/
|
||||
#define LDR1_Pin GPIO_PIN_0
|
||||
#define LDR1_GPIO_Port GPIOA
|
||||
#define LDR2_Pin GPIO_PIN_1
|
||||
#define LDR2_GPIO_Port GPIOA
|
||||
#define LDR3_Pin GPIO_PIN_2
|
||||
#define LDR3_GPIO_Port GPIOA
|
||||
#define RED_LED_Pin GPIO_PIN_7
|
||||
#define RED_LED_GPIO_Port GPIOA
|
||||
#define GREEN_LED_Pin GPIO_PIN_0
|
||||
#define GREEN_LED_GPIO_Port GPIOB
|
||||
#define BLUE_LED_Pin GPIO_PIN_1
|
||||
#define BLUE_LED_GPIO_Port GPIOB
|
||||
#define BOOT_Pin GPIO_PIN_8
|
||||
#define BOOT_GPIO_Port GPIOB
|
||||
/* USER CODE BEGIN Private defines */
|
||||
|
||||
/* USER CODE END Private defines */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __INIT_H */
|
||||
|
||||
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|
||||
46
firmware/Inc/light_sensors.hpp
Normal file
46
firmware/Inc/light_sensors.hpp
Normal file
@@ -0,0 +1,46 @@
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <utility>
|
||||
|
||||
#include "adc.h"
|
||||
#include "utils.hpp"
|
||||
|
||||
class LightSensors {
|
||||
public:
|
||||
static constexpr auto MAX_VALUE = uint16_t{4095};
|
||||
|
||||
static inline const std::array<uint16_t, 3>& getValues()
|
||||
{
|
||||
sampleLightSensors();
|
||||
return m_adcValues;
|
||||
}
|
||||
|
||||
template<size_t Idx>
|
||||
static inline const int16_t& getValue()
|
||||
{
|
||||
static_assert(Idx < m_adcValues.size(), "Invalid light sensor index");
|
||||
|
||||
sampleLightSensors();
|
||||
return m_adcValues[Idx];
|
||||
}
|
||||
|
||||
private:
|
||||
static std::array<uint16_t, 3> m_adcValues;
|
||||
|
||||
static void sampleLightSensors()
|
||||
{
|
||||
HAL_ADC_Start(&hadc);
|
||||
|
||||
util::for_constexpr(
|
||||
[](const auto& idx) {
|
||||
constexpr auto i = idx.value;
|
||||
HAL_ADC_PollForConversion(&hadc, 1000);
|
||||
m_adcValues[i] = HAL_ADC_GetValue(&hadc);
|
||||
},
|
||||
std::make_index_sequence<m_adcValues.size()>{});
|
||||
|
||||
HAL_ADC_Stop(&hadc);
|
||||
}
|
||||
};
|
||||
319
firmware/Inc/stm32f0xx_hal_conf.h
Normal file
319
firmware/Inc/stm32f0xx_hal_conf.h
Normal file
@@ -0,0 +1,319 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file stm32f0xx_hal_conf.h
|
||||
* @brief HAL configuration file.
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* <h2><center>© Copyright (c) 2016 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
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __STM32F0xx_HAL_CONF_H
|
||||
#define __STM32F0xx_HAL_CONF_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Exported types ------------------------------------------------------------*/
|
||||
/* Exported constants --------------------------------------------------------*/
|
||||
|
||||
/* ########################## Module Selection ############################## */
|
||||
/**
|
||||
* @brief This is the list of modules to be used in the HAL driver
|
||||
*/
|
||||
#define HAL_MODULE_ENABLED
|
||||
#define HAL_ADC_MODULE_ENABLED
|
||||
/*#define HAL_CRYP_MODULE_ENABLED */
|
||||
/*#define HAL_CAN_MODULE_ENABLED */
|
||||
/*#define HAL_CEC_MODULE_ENABLED */
|
||||
/*#define HAL_COMP_MODULE_ENABLED */
|
||||
/*#define HAL_CRC_MODULE_ENABLED */
|
||||
/*#define HAL_CRYP_MODULE_ENABLED */
|
||||
/*#define HAL_TSC_MODULE_ENABLED */
|
||||
/*#define HAL_DAC_MODULE_ENABLED */
|
||||
/*#define HAL_I2S_MODULE_ENABLED */
|
||||
/*#define HAL_IWDG_MODULE_ENABLED */
|
||||
/*#define HAL_LCD_MODULE_ENABLED */
|
||||
/*#define HAL_LPTIM_MODULE_ENABLED */
|
||||
/*#define HAL_RNG_MODULE_ENABLED */
|
||||
/*#define HAL_RTC_MODULE_ENABLED */
|
||||
/*#define HAL_SPI_MODULE_ENABLED */
|
||||
/*#define HAL_TIM_MODULE_ENABLED */
|
||||
/*#define HAL_UART_MODULE_ENABLED */
|
||||
/*#define HAL_USART_MODULE_ENABLED */
|
||||
/*#define HAL_IRDA_MODULE_ENABLED */
|
||||
/*#define HAL_SMARTCARD_MODULE_ENABLED */
|
||||
/*#define HAL_SMBUS_MODULE_ENABLED */
|
||||
/*#define HAL_WWDG_MODULE_ENABLED */
|
||||
#define HAL_PCD_MODULE_ENABLED
|
||||
#define HAL_CORTEX_MODULE_ENABLED
|
||||
#define HAL_DMA_MODULE_ENABLED
|
||||
#define HAL_FLASH_MODULE_ENABLED
|
||||
#define HAL_GPIO_MODULE_ENABLED
|
||||
#define HAL_EXTI_MODULE_ENABLED
|
||||
#define HAL_PWR_MODULE_ENABLED
|
||||
#define HAL_RCC_MODULE_ENABLED
|
||||
#define HAL_I2C_MODULE_ENABLED
|
||||
|
||||
/* ########################## HSE/HSI Values adaptation ##################### */
|
||||
/**
|
||||
* @brief Adjust the value of External High Speed oscillator (HSE) used in your application.
|
||||
* This value is used by the RCC HAL module to compute the system frequency
|
||||
* (when HSE is used as system clock source, directly or through the PLL).
|
||||
*/
|
||||
#if !defined (HSE_VALUE)
|
||||
#define HSE_VALUE ((uint32_t)8000000) /*!< Value of the External oscillator in Hz */
|
||||
#endif /* HSE_VALUE */
|
||||
|
||||
/**
|
||||
* @brief In the following line adjust the External High Speed oscillator (HSE) Startup
|
||||
* Timeout value
|
||||
*/
|
||||
#if !defined (HSE_STARTUP_TIMEOUT)
|
||||
#define HSE_STARTUP_TIMEOUT ((uint32_t)100) /*!< Time out for HSE start up, in ms */
|
||||
#endif /* HSE_STARTUP_TIMEOUT */
|
||||
|
||||
/**
|
||||
* @brief Internal High Speed oscillator (HSI) value.
|
||||
* This value is used by the RCC HAL module to compute the system frequency
|
||||
* (when HSI is used as system clock source, directly or through the PLL).
|
||||
*/
|
||||
#if !defined (HSI_VALUE)
|
||||
#define HSI_VALUE ((uint32_t)8000000) /*!< Value of the Internal oscillator in Hz*/
|
||||
#endif /* HSI_VALUE */
|
||||
|
||||
/**
|
||||
* @brief In the following line adjust the Internal High Speed oscillator (HSI) Startup
|
||||
* Timeout value
|
||||
*/
|
||||
#if !defined (HSI_STARTUP_TIMEOUT)
|
||||
#define HSI_STARTUP_TIMEOUT ((uint32_t)5000) /*!< Time out for HSI start up */
|
||||
#endif /* HSI_STARTUP_TIMEOUT */
|
||||
|
||||
/**
|
||||
* @brief Internal High Speed oscillator for ADC (HSI14) value.
|
||||
*/
|
||||
#if !defined (HSI14_VALUE)
|
||||
#define HSI14_VALUE ((uint32_t)14000000) /*!< Value of the Internal High Speed oscillator for ADC in Hz.
|
||||
The real value may vary depending on the variations
|
||||
in voltage and temperature. */
|
||||
#endif /* HSI14_VALUE */
|
||||
|
||||
/**
|
||||
* @brief Internal High Speed oscillator for USB (HSI48) value.
|
||||
*/
|
||||
#if !defined (HSI48_VALUE)
|
||||
#define HSI48_VALUE ((uint32_t)48000000) /*!< Value of the Internal High Speed oscillator for USB in Hz.
|
||||
The real value may vary depending on the variations
|
||||
in voltage and temperature. */
|
||||
#endif /* HSI48_VALUE */
|
||||
|
||||
/**
|
||||
* @brief Internal Low Speed oscillator (LSI) value.
|
||||
*/
|
||||
#if !defined (LSI_VALUE)
|
||||
#define LSI_VALUE ((uint32_t)40000)
|
||||
#endif /* LSI_VALUE */ /*!< Value of the Internal Low Speed oscillator in Hz
|
||||
The real value may vary depending on the variations
|
||||
in voltage and temperature. */
|
||||
/**
|
||||
* @brief External Low Speed oscillator (LSI) value.
|
||||
*/
|
||||
#if !defined (LSE_VALUE)
|
||||
#define LSE_VALUE ((uint32_t)32768) /*!< Value of the External Low Speed oscillator in Hz */
|
||||
#endif /* LSE_VALUE */
|
||||
|
||||
#if !defined (LSE_STARTUP_TIMEOUT)
|
||||
#define LSE_STARTUP_TIMEOUT ((uint32_t)5000) /*!< Time out for LSE start up, in ms */
|
||||
#endif /* LSE_STARTUP_TIMEOUT */
|
||||
|
||||
/* Tip: To avoid modifying this file each time you need to use different HSE,
|
||||
=== you can define the HSE value in your toolchain compiler preprocessor. */
|
||||
|
||||
/* ########################### System Configuration ######################### */
|
||||
/**
|
||||
* @brief This is the HAL system configuration section
|
||||
*/
|
||||
#define VDD_VALUE ((uint32_t)3300) /*!< Value of VDD in mv */
|
||||
#define TICK_INT_PRIORITY ((uint32_t)0) /*!< tick interrupt priority (lowest by default) */
|
||||
/* Warning: Must be set to higher priority for HAL_Delay() */
|
||||
/* and HAL_GetTick() usage under interrupt context */
|
||||
#define USE_RTOS 0
|
||||
#define PREFETCH_ENABLE 1
|
||||
#define INSTRUCTION_CACHE_ENABLE 0
|
||||
#define DATA_CACHE_ENABLE 0
|
||||
#define USE_SPI_CRC 0U
|
||||
|
||||
#define USE_HAL_ADC_REGISTER_CALLBACKS 0U /* ADC register callback disabled */
|
||||
#define USE_HAL_CAN_REGISTER_CALLBACKS 0U /* CAN register callback disabled */
|
||||
#define USE_HAL_COMP_REGISTER_CALLBACKS 0U /* COMP register callback disabled */
|
||||
#define USE_HAL_CEC_REGISTER_CALLBACKS 0U /* CEC register callback disabled */
|
||||
#define USE_HAL_DAC_REGISTER_CALLBACKS 0U /* DAC register callback disabled */
|
||||
#define USE_HAL_I2C_REGISTER_CALLBACKS 0U /* I2C register callback disabled */
|
||||
#define USE_HAL_SMBUS_REGISTER_CALLBACKS 0U /* SMBUS register callback disabled */
|
||||
#define USE_HAL_UART_REGISTER_CALLBACKS 0U /* UART register callback disabled */
|
||||
#define USE_HAL_USART_REGISTER_CALLBACKS 0U /* USART register callback disabled */
|
||||
#define USE_HAL_IRDA_REGISTER_CALLBACKS 0U /* IRDA register callback disabled */
|
||||
#define USE_HAL_SMARTCARD_REGISTER_CALLBACKS 0U /* SMARTCARD register callback disabled */
|
||||
#define USE_HAL_WWDG_REGISTER_CALLBACKS 0U /* WWDG register callback disabled */
|
||||
#define USE_HAL_RTC_REGISTER_CALLBACKS 0U /* RTC register callback disabled */
|
||||
#define USE_HAL_SPI_REGISTER_CALLBACKS 0U /* SPI register callback disabled */
|
||||
#define USE_HAL_I2S_REGISTER_CALLBACKS 0U /* I2S register callback disabled */
|
||||
#define USE_HAL_TIM_REGISTER_CALLBACKS 0U /* TIM register callback disabled */
|
||||
#define USE_HAL_TSC_REGISTER_CALLBACKS 0U /* TSC register callback disabled */
|
||||
#define USE_HAL_PCD_REGISTER_CALLBACKS 0U /* PCD register callback disabled */
|
||||
|
||||
/* ########################## Assert Selection ############################## */
|
||||
/**
|
||||
* @brief Uncomment the line below to expanse the "assert_param" macro in the
|
||||
* HAL drivers code
|
||||
*/
|
||||
/* #define USE_FULL_ASSERT 1U */
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
/**
|
||||
* @brief Include module's header file
|
||||
*/
|
||||
|
||||
#ifdef HAL_RCC_MODULE_ENABLED
|
||||
#include "stm32f0xx_hal_rcc.h"
|
||||
#endif /* HAL_RCC_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_GPIO_MODULE_ENABLED
|
||||
#include "stm32f0xx_hal_gpio.h"
|
||||
#endif /* HAL_GPIO_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_EXTI_MODULE_ENABLED
|
||||
#include "stm32f0xx_hal_exti.h"
|
||||
#endif /* HAL_EXTI_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_DMA_MODULE_ENABLED
|
||||
#include "stm32f0xx_hal_dma.h"
|
||||
#endif /* HAL_DMA_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_CORTEX_MODULE_ENABLED
|
||||
#include "stm32f0xx_hal_cortex.h"
|
||||
#endif /* HAL_CORTEX_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_ADC_MODULE_ENABLED
|
||||
#include "stm32f0xx_hal_adc.h"
|
||||
#endif /* HAL_ADC_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_CAN_MODULE_ENABLED
|
||||
#include "stm32f0xx_hal_can.h"
|
||||
#endif /* HAL_CAN_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_CEC_MODULE_ENABLED
|
||||
#include "stm32f0xx_hal_cec.h"
|
||||
#endif /* HAL_CEC_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_COMP_MODULE_ENABLED
|
||||
#include "stm32f0xx_hal_comp.h"
|
||||
#endif /* HAL_COMP_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_CRC_MODULE_ENABLED
|
||||
#include "stm32f0xx_hal_crc.h"
|
||||
#endif /* HAL_CRC_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_DAC_MODULE_ENABLED
|
||||
#include "stm32f0xx_hal_dac.h"
|
||||
#endif /* HAL_DAC_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_FLASH_MODULE_ENABLED
|
||||
#include "stm32f0xx_hal_flash.h"
|
||||
#endif /* HAL_FLASH_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_I2C_MODULE_ENABLED
|
||||
#include "stm32f0xx_hal_i2c.h"
|
||||
#endif /* HAL_I2C_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_I2S_MODULE_ENABLED
|
||||
#include "stm32f0xx_hal_i2s.h"
|
||||
#endif /* HAL_I2S_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_IRDA_MODULE_ENABLED
|
||||
#include "stm32f0xx_hal_irda.h"
|
||||
#endif /* HAL_IRDA_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_IWDG_MODULE_ENABLED
|
||||
#include "stm32f0xx_hal_iwdg.h"
|
||||
#endif /* HAL_IWDG_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_PCD_MODULE_ENABLED
|
||||
#include "stm32f0xx_hal_pcd.h"
|
||||
#endif /* HAL_PCD_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_PWR_MODULE_ENABLED
|
||||
#include "stm32f0xx_hal_pwr.h"
|
||||
#endif /* HAL_PWR_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_RTC_MODULE_ENABLED
|
||||
#include "stm32f0xx_hal_rtc.h"
|
||||
#endif /* HAL_RTC_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_SMARTCARD_MODULE_ENABLED
|
||||
#include "stm32f0xx_hal_smartcard.h"
|
||||
#endif /* HAL_SMARTCARD_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_SMBUS_MODULE_ENABLED
|
||||
#include "stm32f0xx_hal_smbus.h"
|
||||
#endif /* HAL_SMBUS_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_SPI_MODULE_ENABLED
|
||||
#include "stm32f0xx_hal_spi.h"
|
||||
#endif /* HAL_SPI_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_TIM_MODULE_ENABLED
|
||||
#include "stm32f0xx_hal_tim.h"
|
||||
#endif /* HAL_TIM_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_TSC_MODULE_ENABLED
|
||||
#include "stm32f0xx_hal_tsc.h"
|
||||
#endif /* HAL_TSC_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_UART_MODULE_ENABLED
|
||||
#include "stm32f0xx_hal_uart.h"
|
||||
#endif /* HAL_UART_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_USART_MODULE_ENABLED
|
||||
#include "stm32f0xx_hal_usart.h"
|
||||
#endif /* HAL_USART_MODULE_ENABLED */
|
||||
|
||||
#ifdef HAL_WWDG_MODULE_ENABLED
|
||||
#include "stm32f0xx_hal_wwdg.h"
|
||||
#endif /* HAL_WWDG_MODULE_ENABLED */
|
||||
|
||||
/* Exported macro ------------------------------------------------------------*/
|
||||
#ifdef USE_FULL_ASSERT
|
||||
/**
|
||||
* @brief The assert_param macro is used for function's parameters check.
|
||||
* @param expr If expr is false, it calls assert_failed function
|
||||
* which reports the name of the source file and the source
|
||||
* line number of the call that failed.
|
||||
* If expr is true, it returns no value.
|
||||
* @retval None
|
||||
*/
|
||||
#define assert_param(expr) ((expr) ? (void)0U : assert_failed((uint8_t *)__FILE__, __LINE__))
|
||||
/* Exported functions ------------------------------------------------------- */
|
||||
void assert_failed(uint8_t* file, uint32_t line);
|
||||
#else
|
||||
#define assert_param(expr) ((void)0U)
|
||||
#endif /* USE_FULL_ASSERT */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __STM32F0xx_HAL_CONF_H */
|
||||
|
||||
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|
||||
66
firmware/Inc/stm32f0xx_it.h
Normal file
66
firmware/Inc/stm32f0xx_it.h
Normal file
@@ -0,0 +1,66 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file stm32f0xx_it.h
|
||||
* @brief This file contains the headers of the interrupt handlers.
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* <h2><center>© Copyright (c) 2020 STMicroelectronics.
|
||||
* All rights reserved.</center></h2>
|
||||
*
|
||||
* This software component is licensed by ST under Ultimate Liberty license
|
||||
* SLA0044, the "License"; You may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at:
|
||||
* www.st.com/SLA0044
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
/* USER CODE END Header */
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __STM32F0xx_IT_H
|
||||
#define __STM32F0xx_IT_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
|
||||
/* USER CODE END Includes */
|
||||
|
||||
/* Exported types ------------------------------------------------------------*/
|
||||
/* USER CODE BEGIN ET */
|
||||
|
||||
/* USER CODE END ET */
|
||||
|
||||
/* Exported constants --------------------------------------------------------*/
|
||||
/* USER CODE BEGIN EC */
|
||||
|
||||
/* USER CODE END EC */
|
||||
|
||||
/* Exported macro ------------------------------------------------------------*/
|
||||
/* USER CODE BEGIN EM */
|
||||
|
||||
/* USER CODE END EM */
|
||||
|
||||
/* Exported functions prototypes ---------------------------------------------*/
|
||||
void NMI_Handler(void);
|
||||
void HardFault_Handler(void);
|
||||
void SVC_Handler(void);
|
||||
void PendSV_Handler(void);
|
||||
void SysTick_Handler(void);
|
||||
void USB_IRQHandler(void);
|
||||
/* USER CODE BEGIN EFP */
|
||||
|
||||
/* USER CODE END EFP */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __STM32F0xx_IT_H */
|
||||
|
||||
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|
||||
202
firmware/Inc/terminal.hpp
Normal file
202
firmware/Inc/terminal.hpp
Normal file
@@ -0,0 +1,202 @@
|
||||
#pragma once
|
||||
|
||||
#include <cctype>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
|
||||
#include "light_sensors.hpp"
|
||||
|
||||
namespace detail {
|
||||
|
||||
constexpr auto ENDL = "\r\n";
|
||||
constexpr auto HELP_CMD = "help";
|
||||
constexpr auto READ_CMD = "read";
|
||||
constexpr auto READ_CSV_CMD = "read csv";
|
||||
constexpr auto RANGE_CMD = "range";
|
||||
|
||||
constexpr auto VERSION_CMD = "version";
|
||||
constexpr auto VERSION = "1.2";
|
||||
|
||||
static inline bool substringEquals(const char* str1, const char* str2, const size_t& size)
|
||||
{
|
||||
return (std::strncmp(str1, str2, size) == 0);
|
||||
}
|
||||
|
||||
static inline bool stringEquals(const char* str1, const char* str2, const size_t& size)
|
||||
{
|
||||
if(size == std::strlen(str2)) {
|
||||
return substringEquals(str1, str2, size);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template<class Uart>
|
||||
class Terminal {
|
||||
public:
|
||||
static void init()
|
||||
{
|
||||
m_serial.init();
|
||||
|
||||
m_serial << detail::ENDL;
|
||||
printVersion();
|
||||
m_serial << detail::ENDL << "$ ";
|
||||
}
|
||||
|
||||
static void callback()
|
||||
{
|
||||
if(receiveInput()) {
|
||||
parseInput();
|
||||
}
|
||||
m_serial.flushTx();
|
||||
}
|
||||
|
||||
private:
|
||||
static constexpr auto INPUT_BUFFER_SIZE = 128;
|
||||
static constexpr auto BACKSPACE = uint8_t{0x7f};
|
||||
static constexpr auto CTRL_C = uint8_t{0x03};
|
||||
|
||||
static Uart m_serial;
|
||||
static char m_inputBuffer[INPUT_BUFFER_SIZE];
|
||||
static uint16_t m_inputSize;
|
||||
static LightSensors m_lightSensors;
|
||||
|
||||
static bool receiveInput()
|
||||
{
|
||||
uint8_t inputByte;
|
||||
|
||||
while(m_serial.rxByte(inputByte)) {
|
||||
if(std::isprint(inputByte) || inputByte == CTRL_C) {
|
||||
m_inputBuffer[m_inputSize++] = inputByte;
|
||||
|
||||
// Handle Ctrl + C
|
||||
if(inputByte == CTRL_C) {
|
||||
m_serial << "^C" << detail::ENDL;
|
||||
return true;
|
||||
}
|
||||
// Echo
|
||||
else {
|
||||
m_serial << static_cast<char>(inputByte);
|
||||
}
|
||||
}
|
||||
|
||||
// Handle backspace
|
||||
if(inputByte == BACKSPACE && m_inputSize > 0) {
|
||||
m_serial << "\b \b";
|
||||
--m_inputSize;
|
||||
}
|
||||
// Handle line terminator
|
||||
else if(inputByte == '\r' || inputByte == '\n') {
|
||||
// Consume possible second line terminator
|
||||
if(m_serial.peek(inputByte) && (inputByte == '\r' || inputByte == '\n')) {
|
||||
m_serial.rxByte(inputByte);
|
||||
}
|
||||
m_serial << detail::ENDL;
|
||||
return true;
|
||||
}
|
||||
|
||||
if(m_inputSize >= INPUT_BUFFER_SIZE) {
|
||||
m_serial << detail::ENDL << "WARNING: Terminal input buffer overflow!" << detail::ENDL;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static void parseInput()
|
||||
{
|
||||
if(m_inputSize) {
|
||||
if(m_inputBuffer[m_inputSize - 1] == CTRL_C) {
|
||||
handleCtrlC();
|
||||
}
|
||||
else {
|
||||
if(detail::substringEquals(m_inputBuffer, detail::HELP_CMD, m_inputSize)) {
|
||||
printHelp();
|
||||
}
|
||||
else if(detail::substringEquals(m_inputBuffer, detail::READ_CMD, m_inputSize)) {
|
||||
readSensors();
|
||||
}
|
||||
else if(detail::stringEquals(m_inputBuffer, detail::READ_CSV_CMD, m_inputSize)) {
|
||||
readSensorsCsv();
|
||||
}
|
||||
else if(detail::stringEquals(m_inputBuffer, detail::RANGE_CMD, m_inputSize)) {
|
||||
getSensorsRange();
|
||||
}
|
||||
else if(detail::substringEquals(m_inputBuffer, detail::VERSION_CMD, m_inputSize)) {
|
||||
printVersion();
|
||||
}
|
||||
else {
|
||||
printUnknown();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m_inputSize = 0;
|
||||
m_serial << "$ ";
|
||||
}
|
||||
|
||||
static void handleCtrlC() { m_serial << "Abort!" << detail::ENDL; }
|
||||
|
||||
static void printHelp()
|
||||
{
|
||||
m_serial << "AdaptiveBrightness command overview: " << detail::ENDL;
|
||||
m_serial << detail::HELP_CMD << " .......: prints this help message" << detail::ENDL;
|
||||
m_serial << detail::READ_CMD << " .......: reads and displays all LDR values" << detail::ENDL;
|
||||
m_serial << detail::READ_CSV_CMD << " ...: read LDR values as csv" << detail::ENDL;
|
||||
m_serial << detail::RANGE_CMD << " ......: print LDR sensor range" << detail::ENDL;
|
||||
m_serial << detail::VERSION_CMD << " ....: displays firmware version" << detail::ENDL;
|
||||
}
|
||||
|
||||
static void readSensors()
|
||||
{
|
||||
const auto sensorValues = m_lightSensors.getValues();
|
||||
m_serial << "Sensor values: " << detail::ENDL;
|
||||
for(size_t i = 0; i < sensorValues.size(); ++i) {
|
||||
m_serial << "LDR";
|
||||
m_serial.txNumber(i + 1);
|
||||
m_serial << ": ";
|
||||
m_serial.template txNumber<uint16_t, 10, 4, ' '>(sensorValues[i]);
|
||||
m_serial << " - ";
|
||||
m_serial.template txNumber<uint8_t, 10, 3, ' '>(sensorValues[i] * 100 / m_lightSensors.MAX_VALUE);
|
||||
m_serial << "%" << detail::ENDL;
|
||||
}
|
||||
}
|
||||
|
||||
static void readSensorsCsv()
|
||||
{
|
||||
const auto sensorValues = m_lightSensors.getValues();
|
||||
for(size_t i = 0; i < sensorValues.size(); ++i) {
|
||||
m_serial.txNumber(sensorValues[i]);
|
||||
m_serial << ((i + 1 == sensorValues.size()) ? "" : ",");
|
||||
}
|
||||
m_serial << detail::ENDL;
|
||||
}
|
||||
|
||||
static void getSensorsRange()
|
||||
{
|
||||
m_serial << "0,";
|
||||
m_serial.txNumber(m_lightSensors.MAX_VALUE);
|
||||
m_serial << detail::ENDL;
|
||||
}
|
||||
|
||||
static void printVersion() { m_serial << "AdaptiveBrightness v" << detail::VERSION << detail::ENDL; }
|
||||
|
||||
static void printUnknown()
|
||||
{
|
||||
m_serial << "Unknown command \"";
|
||||
for(uint16_t i = 0; i < m_inputSize; ++i) {
|
||||
m_serial << static_cast<char>(m_inputBuffer[i]);
|
||||
}
|
||||
m_serial << "\"" << detail::ENDL;
|
||||
}
|
||||
};
|
||||
|
||||
template<class Uart>
|
||||
char Terminal<Uart>::m_inputBuffer[INPUT_BUFFER_SIZE];
|
||||
|
||||
template<class Uart>
|
||||
uint16_t Terminal<Uart>::m_inputSize = 0;
|
||||
329
firmware/Inc/uart.hpp
Normal file
329
firmware/Inc/uart.hpp
Normal file
@@ -0,0 +1,329 @@
|
||||
#pragma once
|
||||
|
||||
#include <limits>
|
||||
#include <type_traits>
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include "uart_config.hpp"
|
||||
#include "uart_vcp.hpp"
|
||||
|
||||
namespace uart {
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<typename...>
|
||||
struct always_false : std::false_type {
|
||||
};
|
||||
template<typename... Ts>
|
||||
inline constexpr auto always_false_v = always_false<Ts...>::value;
|
||||
|
||||
template<typename T, T Limit, size_t Base>
|
||||
static constexpr size_t cntDigits()
|
||||
{
|
||||
T num = Limit;
|
||||
size_t cnt = 0;
|
||||
|
||||
do {
|
||||
num /= Base;
|
||||
++cnt;
|
||||
} while(num > 0);
|
||||
|
||||
return cnt;
|
||||
}
|
||||
|
||||
template<typename T, size_t Base>
|
||||
static constexpr size_t maxNumDigits()
|
||||
{
|
||||
constexpr T MinVal = std::numeric_limits<T>::min();
|
||||
constexpr T MaxVal = std::numeric_limits<T>::max();
|
||||
|
||||
constexpr T MinDigits = cntDigits<T, MinVal, Base>();
|
||||
constexpr T MaxDigits = cntDigits<T, MaxVal, Base>();
|
||||
|
||||
return (MinDigits < MaxDigits) ? MaxDigits : MinDigits;
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template<class Driver>
|
||||
class Uart {
|
||||
public:
|
||||
// Constructing a uart object does not initialize the driver to allow different specializations with the same
|
||||
// back-end to exists at the same time
|
||||
// Note that init must be called every time when switching specializations with the same back-end
|
||||
Uart() = default;
|
||||
|
||||
// Moving and copying uart objects is not supported
|
||||
Uart(const Uart&) = delete;
|
||||
Uart(Uart&&) = delete;
|
||||
Uart& operator=(const Uart&) = delete;
|
||||
Uart& operator=(Uart&&) = delete;
|
||||
|
||||
// Before using the uart init must be called
|
||||
static void init() { Driver::init(); }
|
||||
|
||||
static void txByte(const uint8_t& byte) { Driver::txByte(byte); }
|
||||
|
||||
static bool rxByte(uint8_t& byte) { return Driver::rxByte(byte); }
|
||||
|
||||
static bool peek(uint8_t& byte) { return Driver::peek(byte); }
|
||||
|
||||
static bool peek() { return Driver::peek(); }
|
||||
|
||||
static void flushTx() { Driver::flushTx(); }
|
||||
|
||||
static void txString(const char* str)
|
||||
{
|
||||
static_assert(Driver::DATA_BITS == DataBits::EIGHT, "Strings are only supported with 8 data bits");
|
||||
|
||||
while(char ch = *str++)
|
||||
txByte(ch);
|
||||
}
|
||||
|
||||
template<typename T, size_t Base = 10, size_t Padding = 0, char PadChar = '0', bool LowerCase = true>
|
||||
static void txNumber(const T& val)
|
||||
{
|
||||
static_assert(std::is_integral_v<T>, "Only supported on integral types");
|
||||
static_assert(Base >= 2, "Numbers with base less than 2 make no sense");
|
||||
static_assert(Base <= 16, "Numbers with base higher than 16 are not supported");
|
||||
static_assert(Padding <= detail::maxNumDigits<T, Base>(), "Cannot pad more than maximum length of number");
|
||||
|
||||
constexpr char AlphaChar = (LowerCase) ? 'a' : 'A';
|
||||
constexpr size_t NumDigits = detail::maxNumDigits<T, Base>();
|
||||
|
||||
T digits = val;
|
||||
|
||||
if(digits < 0) {
|
||||
digits = -digits;
|
||||
txByte('-');
|
||||
}
|
||||
|
||||
uint8_t buffer[NumDigits];
|
||||
uint8_t* bufEnd = buffer + NumDigits - 1;
|
||||
|
||||
do {
|
||||
const uint8_t lastDigit = digits % Base;
|
||||
*bufEnd-- = (lastDigit < 10) ? ('0' + lastDigit) : (AlphaChar + lastDigit - 10);
|
||||
digits /= Base;
|
||||
} while(digits > 0);
|
||||
|
||||
if(Padding > 0) {
|
||||
size_t strLen = buffer + NumDigits - (bufEnd + 1);
|
||||
|
||||
if(Padding > strLen) {
|
||||
for(size_t i = Padding; i > strLen && bufEnd >= buffer; --i) {
|
||||
*bufEnd-- = PadChar;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for(uint8_t* buf = bufEnd + 1; buf < buffer + NumDigits; ++buf) {
|
||||
txByte(*buf);
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
// Output stream overloads
|
||||
|
||||
Uart& operator<<(const char* str)
|
||||
{
|
||||
txString(str);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Uart& operator<<(const char& val)
|
||||
{
|
||||
txByte(val);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Uart& operator<<(const signed char& val)
|
||||
{
|
||||
txNumber(val);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Uart& operator<<(const unsigned char& val)
|
||||
{
|
||||
txNumber(val);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Uart& operator<<(const short& val)
|
||||
{
|
||||
txNumber(val);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Uart& operator<<(const unsigned short& val)
|
||||
{
|
||||
txNumber(val);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Uart& operator<<(const int& val)
|
||||
{
|
||||
txNumber(val);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Uart& operator<<(const unsigned int& val)
|
||||
{
|
||||
txNumber(val);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Uart& operator<<(const long& val)
|
||||
{
|
||||
txNumber(val);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Uart& operator<<(const unsigned long& val)
|
||||
{
|
||||
txNumber(val);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Uart& operator<<(const long long& val)
|
||||
{
|
||||
txNumber(val);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Uart& operator<<(const unsigned long long& val)
|
||||
{
|
||||
txNumber(val);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename... Ts>
|
||||
Uart& operator<<(float) const
|
||||
{
|
||||
static_assert(detail::always_false_v<Ts...>, "Not supported by hardware");
|
||||
}
|
||||
|
||||
template<typename... Ts>
|
||||
Uart& operator<<(double) const
|
||||
{
|
||||
static_assert(detail::always_false_v<Ts...>, "Not supported by hardware");
|
||||
}
|
||||
|
||||
template<typename... Ts>
|
||||
Uart& operator<<(long double) const
|
||||
{
|
||||
static_assert(detail::always_false_v<Ts...>, "Not supported by hardware");
|
||||
}
|
||||
|
||||
Uart& operator<<(const bool& val)
|
||||
{
|
||||
txString(val ? "true" : "false");
|
||||
return *this;
|
||||
}
|
||||
|
||||
Uart& operator<<(const void* val)
|
||||
{
|
||||
txString("0x");
|
||||
txNumber<uint32_t, 16, 4, '0', false>(reinterpret_cast<uint32_t>(val));
|
||||
return *this;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
// Input stream overloads
|
||||
|
||||
template<typename... Ts>
|
||||
Uart& operator>>(char&) const
|
||||
{
|
||||
static_assert(detail::always_false_v<Ts...>, "Not implemented");
|
||||
}
|
||||
|
||||
template<typename... Ts>
|
||||
Uart& operator>>(unsigned char&) const
|
||||
{
|
||||
static_assert(detail::always_false_v<Ts...>, "Not implemented");
|
||||
}
|
||||
|
||||
template<typename... Ts>
|
||||
Uart& operator>>(short&) const
|
||||
{
|
||||
static_assert(detail::always_false_v<Ts...>, "Not implemented");
|
||||
}
|
||||
|
||||
template<typename... Ts>
|
||||
Uart& operator>>(unsigned short&) const
|
||||
{
|
||||
static_assert(detail::always_false_v<Ts...>, "Not implemented");
|
||||
}
|
||||
|
||||
template<typename... Ts>
|
||||
Uart& operator>>(int&) const
|
||||
{
|
||||
static_assert(detail::always_false_v<Ts...>, "Not implemented");
|
||||
}
|
||||
|
||||
template<typename... Ts>
|
||||
Uart& operator>>(unsigned int&) const
|
||||
{
|
||||
static_assert(detail::always_false_v<Ts...>, "Not implemented");
|
||||
}
|
||||
|
||||
template<typename... Ts>
|
||||
Uart& operator>>(long&) const
|
||||
{
|
||||
static_assert(detail::always_false_v<Ts...>, "Not implemented");
|
||||
}
|
||||
|
||||
template<typename... Ts>
|
||||
Uart& operator>>(unsigned long&) const
|
||||
{
|
||||
static_assert(detail::always_false_v<Ts...>, "Not implemented");
|
||||
}
|
||||
|
||||
template<typename... Ts>
|
||||
Uart& operator>>(long long&) const
|
||||
{
|
||||
static_assert(detail::always_false_v<Ts...>, "Not implemented");
|
||||
}
|
||||
|
||||
template<typename... Ts>
|
||||
Uart& operator>>(unsigned long long&) const
|
||||
{
|
||||
static_assert(detail::always_false_v<Ts...>, "Not implemented");
|
||||
}
|
||||
|
||||
template<typename... Ts>
|
||||
Uart& operator>>(float&) const
|
||||
{
|
||||
static_assert(detail::always_false_v<Ts...>, "Not supported by hardware");
|
||||
}
|
||||
|
||||
template<typename... Ts>
|
||||
Uart& operator>>(double&) const
|
||||
{
|
||||
static_assert(detail::always_false_v<Ts...>, "Not supported by hardware");
|
||||
}
|
||||
|
||||
template<typename... Ts>
|
||||
Uart& operator>>(long double&) const
|
||||
{
|
||||
static_assert(detail::always_false_v<Ts...>, "Not supported by hardware");
|
||||
}
|
||||
|
||||
template<typename... Ts>
|
||||
Uart& operator>>(bool&) const
|
||||
{
|
||||
static_assert(detail::always_false_v<Ts...>, "Not implemented");
|
||||
}
|
||||
|
||||
template<typename... Ts>
|
||||
Uart& operator>>(const void*&) const
|
||||
{
|
||||
static_assert(detail::always_false_v<Ts...>, "Not implemented");
|
||||
}
|
||||
};
|
||||
|
||||
template<typename cfg = Config<>>
|
||||
using Vcp = Uart<detail::VirtualComPort<cfg>>;
|
||||
|
||||
} // namespace uart
|
||||
32
firmware/Inc/uart_config.hpp
Normal file
32
firmware/Inc/uart_config.hpp
Normal file
@@ -0,0 +1,32 @@
|
||||
#pragma once
|
||||
|
||||
namespace uart {
|
||||
|
||||
enum class DataBits {
|
||||
FIVE,
|
||||
SIX,
|
||||
SEVEN,
|
||||
EIGHT,
|
||||
NINE,
|
||||
};
|
||||
|
||||
enum class StopBits {
|
||||
ONE,
|
||||
TWO,
|
||||
};
|
||||
|
||||
enum class Parity {
|
||||
NONE,
|
||||
ODD,
|
||||
EVEN,
|
||||
};
|
||||
|
||||
template<uint32_t baudRate = 9600, DataBits dataBits = DataBits::EIGHT, Parity parity = Parity::NONE, StopBits stopBits = StopBits::ONE>
|
||||
struct Config {
|
||||
static constexpr auto BAUD_RATE = baudRate;
|
||||
static constexpr auto DATA_BITS = dataBits;
|
||||
static constexpr auto PARITY = parity;
|
||||
static constexpr auto STOP_BITS = stopBits;
|
||||
};
|
||||
|
||||
} // namespace uart
|
||||
252
firmware/Inc/uart_vcp.hpp
Normal file
252
firmware/Inc/uart_vcp.hpp
Normal file
@@ -0,0 +1,252 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
|
||||
#include "gpio.h"
|
||||
#include "usbd_cdc.h"
|
||||
#include "usbd_def.h"
|
||||
|
||||
extern "C" {
|
||||
extern USBD_CDC_ItfTypeDef USBD_Interface_fops_FS;
|
||||
extern USBD_HandleTypeDef hUsbDeviceFS;
|
||||
}
|
||||
|
||||
namespace uart::detail {
|
||||
|
||||
template<size_t Size>
|
||||
struct RingBuffer {
|
||||
volatile size_t head;
|
||||
volatile size_t tail;
|
||||
volatile uint8_t data[Size];
|
||||
};
|
||||
|
||||
template<size_t Size>
|
||||
struct Buffer {
|
||||
volatile size_t size;
|
||||
volatile uint8_t data[Size];
|
||||
};
|
||||
|
||||
template<class cfg>
|
||||
class VirtualComPort {
|
||||
public:
|
||||
static constexpr auto DATA_BITS = cfg::DATA_BITS;
|
||||
|
||||
[[gnu::always_inline]] static void init()
|
||||
{
|
||||
USBD_Interface_fops_FS.Init = CdcInit;
|
||||
USBD_Interface_fops_FS.DeInit = CdcDeInit;
|
||||
USBD_Interface_fops_FS.Control = CdcControl;
|
||||
USBD_Interface_fops_FS.Receive = CdcReceive;
|
||||
}
|
||||
|
||||
[[gnu::always_inline]] static bool rxByte(uint8_t& byte)
|
||||
{
|
||||
if(m_rxBuffer.head == m_rxBuffer.tail)
|
||||
return false;
|
||||
|
||||
m_reading = true;
|
||||
const size_t newTail = (m_rxBuffer.tail + 1) % RX_BUFFER_SIZE;
|
||||
byte = m_rxBuffer.data[newTail];
|
||||
m_rxBuffer.tail = newTail;
|
||||
m_reading = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
[[gnu::always_inline]] static void txByte(const uint8_t& byte)
|
||||
{
|
||||
if(m_txBuffer.size == TX_BUFFER_SIZE) {
|
||||
flushTx();
|
||||
}
|
||||
m_txBuffer.data[m_txBuffer.size++] = byte;
|
||||
}
|
||||
|
||||
[[gnu::always_inline]] static bool peek(uint8_t& byte)
|
||||
{
|
||||
if(m_rxBuffer.head == m_rxBuffer.tail)
|
||||
return false;
|
||||
|
||||
m_reading = true;
|
||||
const size_t newTail = (m_rxBuffer.tail + 1) % RX_BUFFER_SIZE;
|
||||
byte = m_rxBuffer.data[newTail];
|
||||
m_reading = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
[[gnu::always_inline]] static void flushTx()
|
||||
{
|
||||
if(m_txBuffer.size == 0)
|
||||
return;
|
||||
|
||||
constexpr auto usbReady = []() {
|
||||
USBD_CDC_HandleTypeDef* hcdc = static_cast<USBD_CDC_HandleTypeDef*>(hUsbDeviceFS.pClassData);
|
||||
return hcdc->TxState != 0;
|
||||
};
|
||||
|
||||
#ifdef INFO_LEDS
|
||||
HAL_GPIO_WritePin(GREEN_LED_GPIO_Port, GREEN_LED_Pin, GPIO_PIN_SET);
|
||||
HAL_GPIO_WritePin(BLUE_LED_GPIO_Port, BLUE_LED_Pin, GPIO_PIN_RESET);
|
||||
#endif
|
||||
|
||||
while(usbReady())
|
||||
;
|
||||
|
||||
std::memcpy(const_cast<uint8_t*>(m_usbAsyncTxBuffer.data), const_cast<const uint8_t*>(m_txBuffer.data), m_txBuffer.size);
|
||||
m_usbAsyncTxBuffer.size = m_txBuffer.size;
|
||||
m_txBuffer.size = 0;
|
||||
|
||||
USBD_CDC_SetTxBuffer(&hUsbDeviceFS, const_cast<uint8_t*>(m_usbAsyncTxBuffer.data), m_usbAsyncTxBuffer.size);
|
||||
USBD_CDC_TransmitPacket(&hUsbDeviceFS);
|
||||
|
||||
#ifdef INFO_LEDS
|
||||
HAL_GPIO_WritePin(BLUE_LED_GPIO_Port, BLUE_LED_Pin, GPIO_PIN_SET);
|
||||
#endif
|
||||
}
|
||||
|
||||
private:
|
||||
static constexpr auto TX_BUFFER_SIZE = CDC_DATA_FS_OUT_PACKET_SIZE;
|
||||
static constexpr auto RX_BUFFER_SIZE = CDC_DATA_FS_IN_PACKET_SIZE;
|
||||
|
||||
static Buffer<TX_BUFFER_SIZE> m_txBuffer;
|
||||
static Buffer<TX_BUFFER_SIZE> m_usbAsyncTxBuffer;
|
||||
|
||||
static RingBuffer<RX_BUFFER_SIZE> m_rxBuffer;
|
||||
static Buffer<RX_BUFFER_SIZE> m_usbAsyncRxBuffer;
|
||||
static volatile bool m_reading;
|
||||
|
||||
static int8_t CdcInit()
|
||||
{
|
||||
#ifdef INFO_LEDS
|
||||
HAL_GPIO_WritePin(RED_LED_GPIO_Port, RED_LED_Pin, GPIO_PIN_SET);
|
||||
HAL_GPIO_WritePin(GREEN_LED_GPIO_Port, GREEN_LED_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(BLUE_LED_GPIO_Port, BLUE_LED_Pin, GPIO_PIN_SET);
|
||||
#endif
|
||||
|
||||
USBD_CDC_SetTxBuffer(&hUsbDeviceFS, const_cast<uint8_t*>(m_txBuffer.data), 0);
|
||||
USBD_CDC_SetRxBuffer(&hUsbDeviceFS, const_cast<uint8_t*>(m_usbAsyncRxBuffer.data));
|
||||
return USBD_OK;
|
||||
}
|
||||
|
||||
static int8_t CdcDeInit()
|
||||
{
|
||||
#ifdef INFO_LEDS
|
||||
HAL_GPIO_WritePin(RED_LED_GPIO_Port, RED_LED_Pin, GPIO_PIN_RESET);
|
||||
HAL_GPIO_WritePin(GREEN_LED_GPIO_Port, GREEN_LED_Pin, GPIO_PIN_SET);
|
||||
HAL_GPIO_WritePin(BLUE_LED_GPIO_Port, BLUE_LED_Pin, GPIO_PIN_SET);
|
||||
#endif
|
||||
|
||||
m_txBuffer.size = 0;
|
||||
m_usbAsyncTxBuffer.size = 0;
|
||||
m_rxBuffer.head = 0;
|
||||
m_rxBuffer.tail = 0;
|
||||
m_usbAsyncRxBuffer.size = 0;
|
||||
m_reading = false;
|
||||
return USBD_OK;
|
||||
}
|
||||
|
||||
static int8_t CdcControl(uint8_t cmd, [[maybe_unused]] uint8_t* buf, [[maybe_unused]] uint16_t length)
|
||||
{
|
||||
switch(cmd) {
|
||||
case CDC_SEND_ENCAPSULATED_COMMAND:
|
||||
break;
|
||||
case CDC_GET_ENCAPSULATED_RESPONSE:
|
||||
break;
|
||||
case CDC_SET_COMM_FEATURE:
|
||||
break;
|
||||
case CDC_GET_COMM_FEATURE:
|
||||
break;
|
||||
case CDC_CLEAR_COMM_FEATURE:
|
||||
break;
|
||||
|
||||
case CDC_SET_LINE_CODING:
|
||||
/*******************************************************************************/
|
||||
/* Line Coding Structure */
|
||||
/*-----------------------------------------------------------------------------*/
|
||||
/* Offset | Field | Size | Value | Description */
|
||||
/* 0 | dwDTERate | 4 | Number |Data terminal rate, in bits per second*/
|
||||
/* 4 | bCharFormat | 1 | Number | Stop bits */
|
||||
/* 0 - 1 Stop bit */
|
||||
/* 1 - 1.5 Stop bits */
|
||||
/* 2 - 2 Stop bits */
|
||||
/* 5 | bParityType | 1 | Number | Parity */
|
||||
/* 0 - None */
|
||||
/* 1 - Odd */
|
||||
/* 2 - Even */
|
||||
/* 3 - Mark */
|
||||
/* 4 - Space */
|
||||
/* 6 | bDataBits | 1 | Number Data bits (5, 6, 7, 8 or 16). */
|
||||
/*******************************************************************************/
|
||||
break;
|
||||
|
||||
case CDC_GET_LINE_CODING:
|
||||
break;
|
||||
|
||||
case CDC_SET_CONTROL_LINE_STATE:
|
||||
break;
|
||||
|
||||
case CDC_SEND_BREAK:
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return USBD_OK;
|
||||
}
|
||||
|
||||
static int8_t CdcReceive([[maybe_unused]] uint8_t* buf, uint32_t* length)
|
||||
{
|
||||
#ifdef INFO_LEDS
|
||||
HAL_GPIO_WritePin(GREEN_LED_GPIO_Port, GREEN_LED_Pin, GPIO_PIN_SET);
|
||||
HAL_GPIO_WritePin(RED_LED_GPIO_Port, RED_LED_Pin, GPIO_PIN_RESET);
|
||||
#endif
|
||||
|
||||
if(USBD_CDC_ReceivePacket(&hUsbDeviceFS) != USBD_OK)
|
||||
return USBD_FAIL;
|
||||
|
||||
for(uint32_t i = 0; i < *length; ++i) {
|
||||
rxHandler(m_usbAsyncRxBuffer.data[i]);
|
||||
}
|
||||
|
||||
#ifdef INFO_LEDS
|
||||
HAL_GPIO_WritePin(RED_LED_GPIO_Port, RED_LED_Pin, GPIO_PIN_SET);
|
||||
#endif
|
||||
return USBD_OK;
|
||||
}
|
||||
|
||||
[[gnu::always_inline]] static inline void rxHandler(const volatile uint8_t& data)
|
||||
{
|
||||
const size_t newHead = (m_rxBuffer.head + 1) % RX_BUFFER_SIZE;
|
||||
|
||||
// Overflow, but tail is being read
|
||||
if(newHead == m_rxBuffer.tail && m_reading) {
|
||||
// Throw away the data, because it cannot be received safely
|
||||
return;
|
||||
}
|
||||
// Overflow, overwrite oldest data
|
||||
else if(newHead == m_rxBuffer.tail) {
|
||||
const size_t newTail = (m_rxBuffer.tail + 1) % RX_BUFFER_SIZE;
|
||||
m_rxBuffer.tail = newTail;
|
||||
}
|
||||
|
||||
m_rxBuffer.data[newHead] = data;
|
||||
m_rxBuffer.head = newHead;
|
||||
}
|
||||
};
|
||||
|
||||
template<class cfg>
|
||||
Buffer<VirtualComPort<cfg>::TX_BUFFER_SIZE> VirtualComPort<cfg>::m_txBuffer = {0, {0}};
|
||||
|
||||
template<class cfg>
|
||||
Buffer<VirtualComPort<cfg>::TX_BUFFER_SIZE> VirtualComPort<cfg>::m_usbAsyncTxBuffer = {0, {0}};
|
||||
|
||||
template<class cfg>
|
||||
RingBuffer<VirtualComPort<cfg>::RX_BUFFER_SIZE> VirtualComPort<cfg>::m_rxBuffer = {0, 0, {0}};
|
||||
|
||||
template<class cfg>
|
||||
Buffer<VirtualComPort<cfg>::RX_BUFFER_SIZE> VirtualComPort<cfg>::m_usbAsyncRxBuffer = {0, {0}};
|
||||
|
||||
template<class cfg>
|
||||
volatile bool VirtualComPort<cfg>::m_reading = false;
|
||||
|
||||
} // namespace uart::detail
|
||||
105
firmware/Inc/usb_device.h
Normal file
105
firmware/Inc/usb_device.h
Normal file
@@ -0,0 +1,105 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : usb_device.h
|
||||
* @version : v2.0_Cube
|
||||
* @brief : Header for usb_device.c file.
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* <h2><center>© Copyright (c) 2020 STMicroelectronics.
|
||||
* All rights reserved.</center></h2>
|
||||
*
|
||||
* This software component is licensed by ST under Ultimate Liberty license
|
||||
* SLA0044, the "License"; You may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at:
|
||||
* www.st.com/SLA0044
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
/* USER CODE END Header */
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __USB_DEVICE__H__
|
||||
#define __USB_DEVICE__H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "stm32f0xx.h"
|
||||
#include "stm32f0xx_hal.h"
|
||||
#include "usbd_def.h"
|
||||
|
||||
/* USER CODE BEGIN INCLUDE */
|
||||
|
||||
/* USER CODE END INCLUDE */
|
||||
|
||||
/** @addtogroup USBD_OTG_DRIVER
|
||||
* @{
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_DEVICE USBD_DEVICE
|
||||
* @brief Device file for Usb otg low level driver.
|
||||
* @{
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_DEVICE_Exported_Variables USBD_DEVICE_Exported_Variables
|
||||
* @brief Public variables.
|
||||
* @{
|
||||
*/
|
||||
|
||||
/* Private variables ---------------------------------------------------------*/
|
||||
/* USER CODE BEGIN PV */
|
||||
|
||||
/* USER CODE END PV */
|
||||
|
||||
/* Private function prototypes -----------------------------------------------*/
|
||||
/* USER CODE BEGIN PFP */
|
||||
|
||||
/* USER CODE END PFP */
|
||||
|
||||
/*
|
||||
* -- Insert your variables declaration here --
|
||||
*/
|
||||
/* USER CODE BEGIN VARIABLES */
|
||||
|
||||
/* USER CODE END VARIABLES */
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_DEVICE_Exported_FunctionsPrototype USBD_DEVICE_Exported_FunctionsPrototype
|
||||
* @brief Declaration of public functions for Usb device.
|
||||
* @{
|
||||
*/
|
||||
|
||||
/** USB Device initialization function. */
|
||||
void MX_USB_DEVICE_Init(void);
|
||||
|
||||
/*
|
||||
* -- Insert functions declaration here --
|
||||
*/
|
||||
/* USER CODE BEGIN FD */
|
||||
|
||||
/* USER CODE END FD */
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __USB_DEVICE__H__ */
|
||||
|
||||
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|
||||
115
firmware/Inc/usbd_cdc_if.h
Normal file
115
firmware/Inc/usbd_cdc_if.h
Normal file
@@ -0,0 +1,115 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : usbd_cdc_if.h
|
||||
* @version : v2.0_Cube
|
||||
* @brief : Header for usbd_cdc_if.c file.
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* <h2><center>© Copyright (c) 2020 STMicroelectronics.
|
||||
* All rights reserved.</center></h2>
|
||||
*
|
||||
* This software component is licensed by ST under Ultimate Liberty license
|
||||
* SLA0044, the "License"; You may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at:
|
||||
* www.st.com/SLA0044
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
/* USER CODE END Header */
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __USBD_CDC_IF_H__
|
||||
#define __USBD_CDC_IF_H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "usbd_cdc.h"
|
||||
|
||||
/* USER CODE BEGIN INCLUDE */
|
||||
|
||||
/* USER CODE END INCLUDE */
|
||||
|
||||
/** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY
|
||||
* @brief For Usb device.
|
||||
* @{
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_CDC_IF USBD_CDC_IF
|
||||
* @brief Usb VCP device module
|
||||
* @{
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_CDC_IF_Exported_Defines USBD_CDC_IF_Exported_Defines
|
||||
* @brief Defines.
|
||||
* @{
|
||||
*/
|
||||
/* USER CODE BEGIN EXPORTED_DEFINES */
|
||||
|
||||
/* USER CODE END EXPORTED_DEFINES */
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_CDC_IF_Exported_Types USBD_CDC_IF_Exported_Types
|
||||
* @brief Types.
|
||||
* @{
|
||||
*/
|
||||
|
||||
/* USER CODE BEGIN EXPORTED_TYPES */
|
||||
|
||||
/* USER CODE END EXPORTED_TYPES */
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_CDC_IF_Exported_Macros USBD_CDC_IF_Exported_Macros
|
||||
* @brief Aliases.
|
||||
* @{
|
||||
*/
|
||||
|
||||
/* USER CODE BEGIN EXPORTED_MACRO */
|
||||
|
||||
/* USER CODE END EXPORTED_MACRO */
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_CDC_IF_Exported_Variables USBD_CDC_IF_Exported_Variables
|
||||
* @brief Public variables.
|
||||
* @{
|
||||
*/
|
||||
|
||||
/** CDC Interface callback. */
|
||||
extern USBD_CDC_ItfTypeDef USBD_Interface_fops_FS;
|
||||
|
||||
/* USER CODE BEGIN EXPORTED_VARIABLES */
|
||||
|
||||
/* USER CODE END EXPORTED_FUNCTIONS */
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __USBD_CDC_IF_H__ */
|
||||
|
||||
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|
||||
173
firmware/Inc/usbd_conf.h
Normal file
173
firmware/Inc/usbd_conf.h
Normal file
@@ -0,0 +1,173 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : usbd_conf.h
|
||||
* @version : v2.0_Cube
|
||||
* @brief : Header for usbd_conf.c file.
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* <h2><center>© Copyright (c) 2020 STMicroelectronics.
|
||||
* All rights reserved.</center></h2>
|
||||
*
|
||||
* This software component is licensed by ST under Ultimate Liberty license
|
||||
* SLA0044, the "License"; You may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at:
|
||||
* www.st.com/SLA0044
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
/* USER CODE END Header */
|
||||
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __USBD_CONF__H__
|
||||
#define __USBD_CONF__H__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "init.h"
|
||||
#include "stm32f0xx.h"
|
||||
#include "stm32f0xx_hal.h"
|
||||
|
||||
/* USER CODE BEGIN INCLUDE */
|
||||
|
||||
/* USER CODE END INCLUDE */
|
||||
|
||||
/** @addtogroup USBD_OTG_DRIVER
|
||||
* @{
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_CONF USBD_CONF
|
||||
* @brief Configuration file for Usb otg low level driver.
|
||||
* @{
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_CONF_Exported_Variables USBD_CONF_Exported_Variables
|
||||
* @brief Public variables.
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_CONF_Exported_Defines USBD_CONF_Exported_Defines
|
||||
* @brief Defines for configuration of the Usb device.
|
||||
* @{
|
||||
*/
|
||||
|
||||
/*---------- -----------*/
|
||||
#define USBD_MAX_NUM_INTERFACES 1
|
||||
/*---------- -----------*/
|
||||
#define USBD_MAX_NUM_CONFIGURATION 1
|
||||
/*---------- -----------*/
|
||||
#define USBD_MAX_STR_DESC_SIZ 512
|
||||
/*---------- -----------*/
|
||||
#define USBD_DEBUG_LEVEL 0
|
||||
/*---------- -----------*/
|
||||
#define USBD_SELF_POWERED 1
|
||||
/*---------- -----------*/
|
||||
#define MAX_STATIC_ALLOC_SIZE 512
|
||||
|
||||
/****************************************/
|
||||
/* #define for FS and HS identification */
|
||||
#define DEVICE_FS 0
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_CONF_Exported_Macros USBD_CONF_Exported_Macros
|
||||
* @brief Aliases.
|
||||
* @{
|
||||
*/
|
||||
|
||||
/* Memory management macros */
|
||||
|
||||
/** Alias for memory allocation. */
|
||||
#define USBD_malloc (uint32_t *)USBD_static_malloc
|
||||
|
||||
/** Alias for memory release. */
|
||||
#define USBD_free USBD_static_free
|
||||
|
||||
/** Alias for memory set. */
|
||||
#define USBD_memset /* Not used */
|
||||
|
||||
/** Alias for memory copy. */
|
||||
#define USBD_memcpy /* Not used */
|
||||
|
||||
/** Alias for delay. */
|
||||
#define USBD_Delay HAL_Delay
|
||||
|
||||
/* DEBUG macros */
|
||||
|
||||
#if (USBD_DEBUG_LEVEL > 0)
|
||||
#define USBD_UsrLog(...) printf(__VA_ARGS__);\
|
||||
printf("\n");
|
||||
#else
|
||||
#define USBD_UsrLog(...)
|
||||
#endif
|
||||
|
||||
#if (USBD_DEBUG_LEVEL > 1)
|
||||
|
||||
#define USBD_ErrLog(...) printf("ERROR: ") ;\
|
||||
printf(__VA_ARGS__);\
|
||||
printf("\n");
|
||||
#else
|
||||
#define USBD_ErrLog(...)
|
||||
#endif
|
||||
|
||||
#if (USBD_DEBUG_LEVEL > 2)
|
||||
#define USBD_DbgLog(...) printf("DEBUG : ") ;\
|
||||
printf(__VA_ARGS__);\
|
||||
printf("\n");
|
||||
#else
|
||||
#define USBD_DbgLog(...)
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_CONF_Exported_Types USBD_CONF_Exported_Types
|
||||
* @brief Types.
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_CONF_Exported_FunctionsPrototype USBD_CONF_Exported_FunctionsPrototype
|
||||
* @brief Declaration of public functions for Usb device.
|
||||
* @{
|
||||
*/
|
||||
|
||||
/* Exported functions -------------------------------------------------------*/
|
||||
void *USBD_static_malloc(uint32_t size);
|
||||
void USBD_static_free(void *p);
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __USBD_CONF__H__ */
|
||||
|
||||
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|
||||
145
firmware/Inc/usbd_desc.h
Normal file
145
firmware/Inc/usbd_desc.h
Normal file
@@ -0,0 +1,145 @@
|
||||
/* USER CODE BEGIN Header */
|
||||
/**
|
||||
******************************************************************************
|
||||
* @file : usbd_desc.c
|
||||
* @version : v2.0_Cube
|
||||
* @brief : Header for usbd_conf.c file.
|
||||
******************************************************************************
|
||||
* @attention
|
||||
*
|
||||
* <h2><center>© Copyright (c) 2020 STMicroelectronics.
|
||||
* All rights reserved.</center></h2>
|
||||
*
|
||||
* This software component is licensed by ST under Ultimate Liberty license
|
||||
* SLA0044, the "License"; You may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at:
|
||||
* www.st.com/SLA0044
|
||||
*
|
||||
******************************************************************************
|
||||
*/
|
||||
/* USER CODE END Header */
|
||||
/* Define to prevent recursive inclusion -------------------------------------*/
|
||||
#ifndef __USBD_DESC__C__
|
||||
#define __USBD_DESC__C__
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Includes ------------------------------------------------------------------*/
|
||||
#include "usbd_def.h"
|
||||
|
||||
/* USER CODE BEGIN INCLUDE */
|
||||
|
||||
/* USER CODE END INCLUDE */
|
||||
|
||||
/** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY
|
||||
* @{
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_DESC USBD_DESC
|
||||
* @brief Usb device descriptors module.
|
||||
* @{
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_DESC_Exported_Constants USBD_DESC_Exported_Constants
|
||||
* @brief Constants.
|
||||
* @{
|
||||
*/
|
||||
#define DEVICE_ID1 (UID_BASE)
|
||||
#define DEVICE_ID2 (UID_BASE + 0x4)
|
||||
#define DEVICE_ID3 (UID_BASE + 0x8)
|
||||
|
||||
#define USB_SIZ_STRING_SERIAL 0x1A
|
||||
|
||||
/* USER CODE BEGIN EXPORTED_CONSTANTS */
|
||||
|
||||
/* USER CODE END EXPORTED_CONSTANTS */
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_DESC_Exported_Defines USBD_DESC_Exported_Defines
|
||||
* @brief Defines.
|
||||
* @{
|
||||
*/
|
||||
|
||||
/* USER CODE BEGIN EXPORTED_DEFINES */
|
||||
|
||||
/* USER CODE END EXPORTED_DEFINES */
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_DESC_Exported_TypesDefinitions USBD_DESC_Exported_TypesDefinitions
|
||||
* @brief Types.
|
||||
* @{
|
||||
*/
|
||||
|
||||
/* USER CODE BEGIN EXPORTED_TYPES */
|
||||
|
||||
/* USER CODE END EXPORTED_TYPES */
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_DESC_Exported_Macros USBD_DESC_Exported_Macros
|
||||
* @brief Aliases.
|
||||
* @{
|
||||
*/
|
||||
|
||||
/* USER CODE BEGIN EXPORTED_MACRO */
|
||||
|
||||
/* USER CODE END EXPORTED_MACRO */
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_DESC_Exported_Variables USBD_DESC_Exported_Variables
|
||||
* @brief Public variables.
|
||||
* @{
|
||||
*/
|
||||
|
||||
/** Descriptor for the Usb device. */
|
||||
extern USBD_DescriptorsTypeDef FS_Desc;
|
||||
|
||||
/* USER CODE BEGIN EXPORTED_VARIABLES */
|
||||
|
||||
/* USER CODE END EXPORTED_VARIABLES */
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/** @defgroup USBD_DESC_Exported_FunctionsPrototype USBD_DESC_Exported_FunctionsPrototype
|
||||
* @brief Public functions declaration.
|
||||
* @{
|
||||
*/
|
||||
|
||||
/* USER CODE BEGIN EXPORTED_FUNCTIONS */
|
||||
|
||||
/* USER CODE END EXPORTED_FUNCTIONS */
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __USBD_DESC__C__ */
|
||||
|
||||
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|
||||
21
firmware/Inc/utils.hpp
Normal file
21
firmware/Inc/utils.hpp
Normal file
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
namespace util {
|
||||
|
||||
template<typename Fn, size_t... Ints>
|
||||
auto for_constexpr(Fn&& func, std::index_sequence<Ints...>)
|
||||
{
|
||||
if constexpr(std::is_void_v<std::invoke_result_t<Fn, std::integral_constant<size_t, 0>>>) {
|
||||
(func(std::integral_constant<size_t, Ints>{}), ...);
|
||||
}
|
||||
else {
|
||||
if((func(std::integral_constant<size_t, Ints>{}) && ...))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace util
|
||||
Reference in New Issue
Block a user