embedded code copied from jankae/VNA and adjusted for STM32G4

This commit is contained in:
Jan Käberich 2020-08-24 19:06:50 +02:00
parent 7af204b349
commit 30d4ebe37b
215 changed files with 186208 additions and 0 deletions

View file

@ -0,0 +1,61 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* File Name : app_freertos.c
* Description : Code for freertos applications
******************************************************************************
* @attention
*
* <h2><center>&copy; 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 */
/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* 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 ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */
/* USER CODE END Variables */
/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */
/* USER CODE END FunctionPrototypes */
/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */
/* USER CODE END Application */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

View file

@ -0,0 +1,714 @@
/* 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 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 */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "cmsis_os.h"
#include "usbpd.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "App.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 ---------------------------------------------------------*/
I2C_HandleTypeDef hi2c2;
SPI_HandleTypeDef hspi1;
SPI_HandleTypeDef hspi2;
TIM_HandleTypeDef htim1;
UART_HandleTypeDef huart3;
PCD_HandleTypeDef hpcd_USB_FS;
osThreadId defaultTaskHandle;
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_DMA_Init(void);
static void MX_I2C2_Init(void);
static void MX_SPI1_Init(void);
static void MX_SPI2_Init(void);
static void MX_UCPD1_Init(void);
static void MX_USART3_UART_Init(void);
static void MX_USB_PCD_Init(void);
static void MX_TIM1_Init(void);
void StartDefaultTask(void const * argument);
/* USER CODE BEGIN PFP */
/* 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_DMA_Init();
MX_I2C2_Init();
MX_SPI1_Init();
MX_SPI2_Init();
MX_UCPD1_Init();
MX_USART3_UART_Init();
MX_USB_PCD_Init();
MX_TIM1_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* USBPD initialisation ---------------------------------*/
MX_USBPD_Init();
/* USER CODE BEGIN RTOS_MUTEX */
/* add mutexes, ... */
/* USER CODE END RTOS_MUTEX */
/* USER CODE BEGIN RTOS_SEMAPHORES */
/* add semaphores, ... */
/* USER CODE END RTOS_SEMAPHORES */
/* USER CODE BEGIN RTOS_TIMERS */
/* start timers, add new ones, ... */
/* USER CODE END RTOS_TIMERS */
/* USER CODE BEGIN RTOS_QUEUES */
/* add queues, ... */
/* USER CODE END RTOS_QUEUES */
/* Create the thread(s) */
/* definition and creation of defaultTask */
osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 128);
defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);
/* USER CODE BEGIN RTOS_THREADS */
/* add threads, ... */
/* USER CODE END RTOS_THREADS */
/* Start scheduler */
osKernelStart();
/* We should never get here as control is now taken by the scheduler */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* 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};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
/** Configure the main internal regulator output voltage
*/
HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV2;
RCC_OscInitStruct.PLL.PLLN = 36;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV6;
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB busses 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_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_7) != HAL_OK)
{
Error_Handler();
}
/** Initializes the peripherals clocks
*/
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART3|RCC_PERIPHCLK_I2C2
|RCC_PERIPHCLK_USB;
PeriphClkInit.Usart3ClockSelection = RCC_USART3CLKSOURCE_PCLK1;
PeriphClkInit.I2c2ClockSelection = RCC_I2C2CLKSOURCE_PCLK1;
PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_PLL;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
Error_Handler();
}
HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_HSI, RCC_MCODIV_1);
}
/**
* @brief I2C2 Initialization Function
* @param None
* @retval None
*/
static void MX_I2C2_Init(void)
{
/* USER CODE BEGIN I2C2_Init 0 */
/* USER CODE END I2C2_Init 0 */
/* USER CODE BEGIN I2C2_Init 1 */
/* USER CODE END I2C2_Init 1 */
hi2c2.Instance = I2C2;
hi2c2.Init.Timing = 0x20B0D9FF;
hi2c2.Init.OwnAddress1 = 0;
hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c2.Init.OwnAddress2 = 0;
hi2c2.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if (HAL_I2C_Init(&hi2c2) != HAL_OK)
{
Error_Handler();
}
/** Configure Analogue filter
*/
if (HAL_I2CEx_ConfigAnalogFilter(&hi2c2, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
{
Error_Handler();
}
/** Configure Digital filter
*/
if (HAL_I2CEx_ConfigDigitalFilter(&hi2c2, 0) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN I2C2_Init 2 */
/* USER CODE END I2C2_Init 2 */
}
/**
* @brief SPI1 Initialization Function
* @param None
* @retval None
*/
static void MX_SPI1_Init(void)
{
/* USER CODE BEGIN SPI1_Init 0 */
/* USER CODE END SPI1_Init 0 */
/* USER CODE BEGIN SPI1_Init 1 */
/* USER CODE END SPI1_Init 1 */
/* SPI1 parameter configuration*/
hspi1.Instance = SPI1;
hspi1.Init.Mode = SPI_MODE_MASTER;
hspi1.Init.Direction = SPI_DIRECTION_2LINES;
hspi1.Init.DataSize = SPI_DATASIZE_4BIT;
hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
hspi1.Init.NSS = SPI_NSS_SOFT;
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64;
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi1.Init.CRCPolynomial = 7;
hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
hspi1.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;
if (HAL_SPI_Init(&hspi1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN SPI1_Init 2 */
/* USER CODE END SPI1_Init 2 */
}
/**
* @brief SPI2 Initialization Function
* @param None
* @retval None
*/
static void MX_SPI2_Init(void)
{
/* USER CODE BEGIN SPI2_Init 0 */
/* USER CODE END SPI2_Init 0 */
/* USER CODE BEGIN SPI2_Init 1 */
/* USER CODE END SPI2_Init 1 */
/* SPI2 parameter configuration*/
hspi2.Instance = SPI2;
hspi2.Init.Mode = SPI_MODE_MASTER;
hspi2.Init.Direction = SPI_DIRECTION_2LINES;
hspi2.Init.DataSize = SPI_DATASIZE_4BIT;
hspi2.Init.CLKPolarity = SPI_POLARITY_LOW;
hspi2.Init.CLKPhase = SPI_PHASE_1EDGE;
hspi2.Init.NSS = SPI_NSS_SOFT;
hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64;
hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi2.Init.TIMode = SPI_TIMODE_DISABLE;
hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi2.Init.CRCPolynomial = 7;
hspi2.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
hspi2.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;
if (HAL_SPI_Init(&hspi2) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN SPI2_Init 2 */
/* USER CODE END SPI2_Init 2 */
}
/**
* @brief TIM1 Initialization Function
* @param None
* @retval None
*/
static void MX_TIM1_Init(void)
{
/* USER CODE BEGIN TIM1_Init 0 */
/* USER CODE END TIM1_Init 0 */
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0};
/* USER CODE BEGIN TIM1_Init 1 */
/* USER CODE END TIM1_Init 1 */
htim1.Instance = TIM1;
htim1.Init.Prescaler = 143;
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
htim1.Init.Period = 65535;
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim1.Init.RepetitionCounter = 0;
htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
sBreakDeadTimeConfig.BreakAFMode = TIM_BREAK_AFMODE_INPUT;
sBreakDeadTimeConfig.Break2AFMode = TIM_BREAK_AFMODE_INPUT;
if (HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM1_Init 2 */
/* USER CODE END TIM1_Init 2 */
}
/**
* @brief UCPD1 Initialization Function
* @param None
* @retval None
*/
static void MX_UCPD1_Init(void)
{
/* USER CODE BEGIN UCPD1_Init 0 */
/* USER CODE END UCPD1_Init 0 */
/* Peripheral clock enable */
LL_APB1_GRP2_EnableClock(LL_APB1_GRP2_PERIPH_UCPD1);
/* UCPD1 DMA Init */
/* UCPD1_RX Init */
LL_DMA_SetPeriphRequest(DMA1, LL_DMA_CHANNEL_1, LL_DMAMUX_REQ_UCPD1_RX);
LL_DMA_SetDataTransferDirection(DMA1, LL_DMA_CHANNEL_1, LL_DMA_DIRECTION_PERIPH_TO_MEMORY);
LL_DMA_SetChannelPriorityLevel(DMA1, LL_DMA_CHANNEL_1, LL_DMA_PRIORITY_LOW);
LL_DMA_SetMode(DMA1, LL_DMA_CHANNEL_1, LL_DMA_MODE_NORMAL);
LL_DMA_SetPeriphIncMode(DMA1, LL_DMA_CHANNEL_1, LL_DMA_PERIPH_NOINCREMENT);
LL_DMA_SetMemoryIncMode(DMA1, LL_DMA_CHANNEL_1, LL_DMA_MEMORY_INCREMENT);
LL_DMA_SetPeriphSize(DMA1, LL_DMA_CHANNEL_1, LL_DMA_PDATAALIGN_BYTE);
LL_DMA_SetMemorySize(DMA1, LL_DMA_CHANNEL_1, LL_DMA_MDATAALIGN_BYTE);
/* UCPD1_TX Init */
LL_DMA_SetPeriphRequest(DMA1, LL_DMA_CHANNEL_2, LL_DMAMUX_REQ_UCPD1_TX);
LL_DMA_SetDataTransferDirection(DMA1, LL_DMA_CHANNEL_2, LL_DMA_DIRECTION_MEMORY_TO_PERIPH);
LL_DMA_SetChannelPriorityLevel(DMA1, LL_DMA_CHANNEL_2, LL_DMA_PRIORITY_LOW);
LL_DMA_SetMode(DMA1, LL_DMA_CHANNEL_2, LL_DMA_MODE_NORMAL);
LL_DMA_SetPeriphIncMode(DMA1, LL_DMA_CHANNEL_2, LL_DMA_PERIPH_NOINCREMENT);
LL_DMA_SetMemoryIncMode(DMA1, LL_DMA_CHANNEL_2, LL_DMA_MEMORY_INCREMENT);
LL_DMA_SetPeriphSize(DMA1, LL_DMA_CHANNEL_2, LL_DMA_PDATAALIGN_BYTE);
LL_DMA_SetMemorySize(DMA1, LL_DMA_CHANNEL_2, LL_DMA_MDATAALIGN_BYTE);
/* UCPD1 interrupt Init */
NVIC_SetPriority(UCPD1_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),5, 0));
NVIC_EnableIRQ(UCPD1_IRQn);
/* USER CODE BEGIN UCPD1_Init 1 */
/* USER CODE END UCPD1_Init 1 */
/* USER CODE BEGIN UCPD1_Init 2 */
/* USER CODE END UCPD1_Init 2 */
}
/**
* @brief USART3 Initialization Function
* @param None
* @retval None
*/
static void MX_USART3_UART_Init(void)
{
/* USER CODE BEGIN USART3_Init 0 */
/* USER CODE END USART3_Init 0 */
/* USER CODE BEGIN USART3_Init 1 */
/* USER CODE END USART3_Init 1 */
huart3.Instance = USART3;
huart3.Init.BaudRate = 115200;
huart3.Init.WordLength = UART_WORDLENGTH_7B;
huart3.Init.StopBits = UART_STOPBITS_1;
huart3.Init.Parity = UART_PARITY_NONE;
huart3.Init.Mode = UART_MODE_TX_RX;
huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart3.Init.OverSampling = UART_OVERSAMPLING_16;
huart3.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart3.Init.ClockPrescaler = UART_PRESCALER_DIV1;
huart3.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_HalfDuplex_Init(&huart3) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_SetTxFifoThreshold(&huart3, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_SetRxFifoThreshold(&huart3, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_DisableFifoMode(&huart3) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART3_Init 2 */
/* USER CODE END USART3_Init 2 */
}
/**
* @brief USB Initialization Function
* @param None
* @retval None
*/
static void MX_USB_PCD_Init(void)
{
/* USER CODE BEGIN USB_Init 0 */
/* USER CODE END USB_Init 0 */
/* USER CODE BEGIN USB_Init 1 */
/* USER CODE END USB_Init 1 */
hpcd_USB_FS.Instance = USB;
hpcd_USB_FS.Init.dev_endpoints = 8;
hpcd_USB_FS.Init.speed = PCD_SPEED_FULL;
hpcd_USB_FS.Init.phy_itface = PCD_PHY_EMBEDDED;
hpcd_USB_FS.Init.Sof_enable = DISABLE;
hpcd_USB_FS.Init.low_power_enable = DISABLE;
hpcd_USB_FS.Init.lpm_enable = DISABLE;
hpcd_USB_FS.Init.battery_charging_enable = DISABLE;
if (HAL_PCD_Init(&hpcd_USB_FS) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USB_Init 2 */
/* USER CODE END USB_Init 2 */
}
/**
* Enable DMA controller clock
*/
static void MX_DMA_Init(void)
{
/* DMA controller clock enable */
__HAL_RCC_DMAMUX1_CLK_ENABLE();
__HAL_RCC_DMA1_CLK_ENABLE();
/* DMA interrupt init */
/* DMA1_Channel1_IRQn interrupt configuration */
NVIC_SetPriority(DMA1_Channel1_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),5, 0));
NVIC_EnableIRQ(DMA1_Channel1_IRQn);
/* DMA1_Channel2_IRQn interrupt configuration */
NVIC_SetPriority(DMA1_Channel2_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),5, 0));
NVIC_EnableIRQ(DMA1_Channel2_IRQn);
}
/**
* @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_GPIOF_CLK_ENABLE();
__HAL_RCC_GPIOG_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(FPGA_INIT_B_GPIO_Port, FPGA_INIT_B_Pin, GPIO_PIN_RESET);
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOA, FPGA_AUX1_Pin|FPGA_AUX2_Pin|FPGA_AUX3_Pin|FPGA_CS_Pin
|LED1_Pin, GPIO_PIN_RESET);
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOB, FLASH_CS_Pin|FPGA_PROGRAM_B_Pin|EN_6V_Pin|FPGA_RESET_Pin, GPIO_PIN_RESET);
/*Configure GPIO pin : FPGA_INIT_B_Pin */
GPIO_InitStruct.Pin = FPGA_INIT_B_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(FPGA_INIT_B_GPIO_Port, &GPIO_InitStruct);
/*Configure GPIO pin : PG10 */
GPIO_InitStruct.Pin = GPIO_PIN_10;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF0_MCO;
HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
/*Configure GPIO pins : FPGA_AUX1_Pin FPGA_AUX2_Pin FPGA_AUX3_Pin FPGA_CS_Pin
LED1_Pin */
GPIO_InitStruct.Pin = FPGA_AUX1_Pin|FPGA_AUX2_Pin|FPGA_AUX3_Pin|FPGA_CS_Pin
|LED1_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/*Configure GPIO pins : FLASH_CS_Pin FPGA_PROGRAM_B_Pin EN_6V_Pin FPGA_RESET_Pin */
GPIO_InitStruct.Pin = FLASH_CS_Pin|FPGA_PROGRAM_B_Pin|EN_6V_Pin|FPGA_RESET_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/*Configure GPIO pin : FPGA_INTR_Pin */
GPIO_InitStruct.Pin = FPGA_INTR_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(FPGA_INTR_GPIO_Port, &GPIO_InitStruct);
/*Configure GPIO pin : FPGA_DONE_Pin */
GPIO_InitStruct.Pin = FPGA_DONE_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(FPGA_DONE_GPIO_Port, &GPIO_InitStruct);
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/* USER CODE BEGIN Header_StartDefaultTask */
/**
* @brief Function implementing the defaultTask thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartDefaultTask */
void StartDefaultTask(void const * argument)
{
/* USER CODE BEGIN 5 */
App_Start();
/* Infinite loop */
for(;;)
{
osDelay(1);
}
/* USER CODE END 5 */
}
/**
* @brief Period elapsed callback in non blocking mode
* @note This function is called when TIM17 interrupt took place, inside
* HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
* a global variable "uwTick" used as application time base.
* @param htim : TIM handle
* @retval None
*/
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
/* USER CODE BEGIN Callback 0 */
/* USER CODE END Callback 0 */
if (htim->Instance == TIM17) {
HAL_IncTick();
}
/* USER CODE BEGIN Callback 1 */
/* USER CODE END Callback 1 */
}
/**
* @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****/

View file

@ -0,0 +1,455 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* File Name : stm32g4xx_hal_msp.c
* Description : This file provides code for the MSP Initialization
* and de-Initialization codes.
******************************************************************************
* @attention
*
* <h2><center>&copy; 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 */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN TD */
/* USER CODE END TD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN Define */
/* USER CODE END Define */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN Macro */
/* USER CODE END Macro */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* External functions --------------------------------------------------------*/
/* USER CODE BEGIN ExternalFunctions */
/* USER CODE END ExternalFunctions */
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* Initializes the Global MSP.
*/
void HAL_MspInit(void)
{
/* USER CODE BEGIN MspInit 0 */
/* USER CODE END MspInit 0 */
__HAL_RCC_SYSCFG_CLK_ENABLE();
__HAL_RCC_PWR_CLK_ENABLE();
/* System interrupt init*/
/* PendSV_IRQn interrupt configuration */
HAL_NVIC_SetPriority(PendSV_IRQn, 15, 0);
/* USER CODE BEGIN MspInit 1 */
/* USER CODE END MspInit 1 */
}
/**
* @brief I2C MSP Initialization
* This function configures the hardware resources used in this example
* @param hi2c: I2C handle pointer
* @retval None
*/
void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(hi2c->Instance==I2C2)
{
/* USER CODE BEGIN I2C2_MspInit 0 */
/* USER CODE END I2C2_MspInit 0 */
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/**I2C2 GPIO Configuration
PC4 ------> I2C2_SCL
PA8 ------> I2C2_SDA
*/
GPIO_InitStruct.Pin = GPIO_PIN_4;
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF4_I2C2;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_8;
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF4_I2C2;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* Peripheral clock enable */
__HAL_RCC_I2C2_CLK_ENABLE();
/* USER CODE BEGIN I2C2_MspInit 1 */
/* USER CODE END I2C2_MspInit 1 */
}
}
/**
* @brief I2C MSP De-Initialization
* This function freeze the hardware resources used in this example
* @param hi2c: I2C handle pointer
* @retval None
*/
void HAL_I2C_MspDeInit(I2C_HandleTypeDef* hi2c)
{
if(hi2c->Instance==I2C2)
{
/* USER CODE BEGIN I2C2_MspDeInit 0 */
/* USER CODE END I2C2_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_I2C2_CLK_DISABLE();
/**I2C2 GPIO Configuration
PC4 ------> I2C2_SCL
PA8 ------> I2C2_SDA
*/
HAL_GPIO_DeInit(GPIOC, GPIO_PIN_4);
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_8);
/* USER CODE BEGIN I2C2_MspDeInit 1 */
/* USER CODE END I2C2_MspDeInit 1 */
}
}
/**
* @brief SPI MSP Initialization
* This function configures the hardware resources used in this example
* @param hspi: SPI handle pointer
* @retval None
*/
void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(hspi->Instance==SPI1)
{
/* USER CODE BEGIN SPI1_MspInit 0 */
/* USER CODE END SPI1_MspInit 0 */
/* Peripheral clock enable */
__HAL_RCC_SPI1_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/**SPI1 GPIO Configuration
PA5 ------> SPI1_SCK
PA6 ------> SPI1_MISO
PA7 ------> SPI1_MOSI
*/
GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* USER CODE BEGIN SPI1_MspInit 1 */
/* USER CODE END SPI1_MspInit 1 */
}
else if(hspi->Instance==SPI2)
{
/* USER CODE BEGIN SPI2_MspInit 0 */
/* USER CODE END SPI2_MspInit 0 */
/* Peripheral clock enable */
__HAL_RCC_SPI2_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/**SPI2 GPIO Configuration
PB13 ------> SPI2_SCK
PB15 ------> SPI2_MOSI
*/
GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_15;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* USER CODE BEGIN SPI2_MspInit 1 */
/* USER CODE END SPI2_MspInit 1 */
}
}
/**
* @brief SPI MSP De-Initialization
* This function freeze the hardware resources used in this example
* @param hspi: SPI handle pointer
* @retval None
*/
void HAL_SPI_MspDeInit(SPI_HandleTypeDef* hspi)
{
if(hspi->Instance==SPI1)
{
/* USER CODE BEGIN SPI1_MspDeInit 0 */
/* USER CODE END SPI1_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_SPI1_CLK_DISABLE();
/**SPI1 GPIO Configuration
PA5 ------> SPI1_SCK
PA6 ------> SPI1_MISO
PA7 ------> SPI1_MOSI
*/
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7);
/* USER CODE BEGIN SPI1_MspDeInit 1 */
/* USER CODE END SPI1_MspDeInit 1 */
}
else if(hspi->Instance==SPI2)
{
/* USER CODE BEGIN SPI2_MspDeInit 0 */
/* USER CODE END SPI2_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_SPI2_CLK_DISABLE();
/**SPI2 GPIO Configuration
PB13 ------> SPI2_SCK
PB15 ------> SPI2_MOSI
*/
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_13|GPIO_PIN_15);
/* USER CODE BEGIN SPI2_MspDeInit 1 */
/* USER CODE END SPI2_MspDeInit 1 */
}
}
/**
* @brief TIM_Base MSP Initialization
* This function configures the hardware resources used in this example
* @param htim_base: TIM_Base handle pointer
* @retval None
*/
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base)
{
if(htim_base->Instance==TIM1)
{
/* USER CODE BEGIN TIM1_MspInit 0 */
/* USER CODE END TIM1_MspInit 0 */
/* Peripheral clock enable */
__HAL_RCC_TIM1_CLK_ENABLE();
/* TIM1 interrupt Init */
HAL_NVIC_SetPriority(TIM1_TRG_COM_TIM17_IRQn, 5, 0);
HAL_NVIC_EnableIRQ(TIM1_TRG_COM_TIM17_IRQn);
/* USER CODE BEGIN TIM1_MspInit 1 */
/* USER CODE END TIM1_MspInit 1 */
}
}
/**
* @brief TIM_Base MSP De-Initialization
* This function freeze the hardware resources used in this example
* @param htim_base: TIM_Base handle pointer
* @retval None
*/
void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* htim_base)
{
if(htim_base->Instance==TIM1)
{
/* USER CODE BEGIN TIM1_MspDeInit 0 */
/* USER CODE END TIM1_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_TIM1_CLK_DISABLE();
/* TIM1 interrupt DeInit */
HAL_NVIC_DisableIRQ(TIM1_TRG_COM_TIM17_IRQn);
/* USER CODE BEGIN TIM1_MspDeInit 1 */
/* USER CODE END TIM1_MspDeInit 1 */
}
}
/**
* @brief UART MSP Initialization
* This function configures the hardware resources used in this example
* @param huart: UART handle pointer
* @retval None
*/
void HAL_UART_MspInit(UART_HandleTypeDef* huart)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(huart->Instance==USART3)
{
/* USER CODE BEGIN USART3_MspInit 0 */
/* USER CODE END USART3_MspInit 0 */
/* Peripheral clock enable */
__HAL_RCC_USART3_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
/**USART3 GPIO Configuration
PC10 ------> USART3_TX
*/
GPIO_InitStruct.Pin = GPIO_PIN_10;
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF7_USART3;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
/* USER CODE BEGIN USART3_MspInit 1 */
/* USER CODE END USART3_MspInit 1 */
}
}
/**
* @brief UART MSP De-Initialization
* This function freeze the hardware resources used in this example
* @param huart: UART handle pointer
* @retval None
*/
void HAL_UART_MspDeInit(UART_HandleTypeDef* huart)
{
if(huart->Instance==USART3)
{
/* USER CODE BEGIN USART3_MspDeInit 0 */
/* USER CODE END USART3_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_USART3_CLK_DISABLE();
/**USART3 GPIO Configuration
PC10 ------> USART3_TX
*/
HAL_GPIO_DeInit(GPIOC, GPIO_PIN_10);
/* USER CODE BEGIN USART3_MspDeInit 1 */
/* USER CODE END USART3_MspDeInit 1 */
}
}
/**
* @brief PCD MSP Initialization
* This function configures the hardware resources used in this example
* @param hpcd: PCD handle pointer
* @retval None
*/
void HAL_PCD_MspInit(PCD_HandleTypeDef* hpcd)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(hpcd->Instance==USB)
{
/* USER CODE BEGIN USB_MspInit 0 */
/* USER CODE END USB_MspInit 0 */
__HAL_RCC_GPIOA_CLK_ENABLE();
/**USB GPIO Configuration
PA11 ------> USB_DM
PA12 ------> USB_DP
*/
GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* Peripheral clock enable */
__HAL_RCC_USB_CLK_ENABLE();
/* USB interrupt Init */
HAL_NVIC_SetPriority(USB_HP_IRQn, 5, 0);
HAL_NVIC_EnableIRQ(USB_HP_IRQn);
HAL_NVIC_SetPriority(USB_LP_IRQn, 5, 0);
HAL_NVIC_EnableIRQ(USB_LP_IRQn);
/* USER CODE BEGIN USB_MspInit 1 */
/* USER CODE END USB_MspInit 1 */
}
}
/**
* @brief PCD MSP De-Initialization
* This function freeze the hardware resources used in this example
* @param hpcd: PCD handle pointer
* @retval None
*/
void HAL_PCD_MspDeInit(PCD_HandleTypeDef* hpcd)
{
if(hpcd->Instance==USB)
{
/* USER CODE BEGIN USB_MspDeInit 0 */
/* USER CODE END USB_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_USB_CLK_DISABLE();
/**USB GPIO Configuration
PA11 ------> USB_DM
PA12 ------> USB_DP
*/
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_11|GPIO_PIN_12);
/* USB interrupt DeInit */
HAL_NVIC_DisableIRQ(USB_HP_IRQn);
HAL_NVIC_DisableIRQ(USB_LP_IRQn);
/* USER CODE BEGIN USB_MspDeInit 1 */
/* USER CODE END USB_MspDeInit 1 */
}
}
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

View file

@ -0,0 +1,114 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file stm32g4xx_hal_timebase_TIM.c
* @brief HAL time base based on the hardware TIM.
******************************************************************************
* @attention
*
* <h2><center>&copy; 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 */
/* Includes ------------------------------------------------------------------*/
#include "stm32g4xx_hal.h"
#include "stm32g4xx_hal_tim.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
TIM_HandleTypeDef htim17;
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/**
* @brief This function configures the TIM17 as a time base source.
* The time source is configured to have 1ms time base with a dedicated
* Tick interrupt priority.
* @note This function is called automatically at the beginning of program after
* reset by HAL_Init() or at any time when clock is configured, by HAL_RCC_ClockConfig().
* @param TickPriority: Tick interrupt priority.
* @retval HAL status
*/
HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
{
RCC_ClkInitTypeDef clkconfig;
uint32_t uwTimclock = 0;
uint32_t uwPrescalerValue = 0;
uint32_t pFLatency;
/*Configure the TIM17 IRQ priority */
HAL_NVIC_SetPriority(TIM1_TRG_COM_TIM17_IRQn, TickPriority ,0);
/* Enable the TIM17 global Interrupt */
HAL_NVIC_EnableIRQ(TIM1_TRG_COM_TIM17_IRQn);
/* Enable TIM17 clock */
__HAL_RCC_TIM17_CLK_ENABLE();
/* Get clock configuration */
HAL_RCC_GetClockConfig(&clkconfig, &pFLatency);
/* Compute TIM17 clock */
uwTimclock = HAL_RCC_GetPCLK2Freq();
/* Compute the prescaler value to have TIM17 counter clock equal to 1MHz */
uwPrescalerValue = (uint32_t) ((uwTimclock / 1000000) - 1);
/* Initialize TIM17 */
htim17.Instance = TIM17;
/* Initialize TIMx peripheral as follow:
+ Period = [(TIM17CLK/1000) - 1]. to have a (1/1000) s time base.
+ Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock.
+ ClockDivision = 0
+ Counter direction = Up
*/
htim17.Init.Period = (1000000 / 1000) - 1;
htim17.Init.Prescaler = uwPrescalerValue;
htim17.Init.ClockDivision = 0;
htim17.Init.CounterMode = TIM_COUNTERMODE_UP;
if(HAL_TIM_Base_Init(&htim17) == HAL_OK)
{
/* Start the TIM time Base generation in interrupt mode */
return HAL_TIM_Base_Start_IT(&htim17);
}
/* Return function status */
return HAL_ERROR;
}
/**
* @brief Suspend Tick increment.
* @note Disable the tick increment by disabling TIM17 update interrupt.
* @param None
* @retval None
*/
void HAL_SuspendTick(void)
{
/* Disable TIM17 update Interrupt */
__HAL_TIM_DISABLE_IT(&htim17, TIM_IT_UPDATE);
}
/**
* @brief Resume Tick increment.
* @note Enable the tick increment by Enabling TIM17 update interrupt.
* @param None
* @retval None
*/
void HAL_ResumeTick(void)
{
/* Enable TIM17 Update interrupt */
__HAL_TIM_ENABLE_IT(&htim17, TIM_IT_UPDATE);
}
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

View file

@ -0,0 +1,251 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file stm32g4xx_it.c
* @brief Interrupt Service Routines.
******************************************************************************
* @attention
*
* <h2><center>&copy; 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 */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32g4xx_it.h"
#include "cmsis_os.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN TD */
/* USER CODE END TD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/* External variables --------------------------------------------------------*/
extern TIM_HandleTypeDef htim1;
extern PCD_HandleTypeDef hpcd_USB_FS;
extern TIM_HandleTypeDef htim17;
/* USER CODE BEGIN EV */
/* USER CODE END EV */
/******************************************************************************/
/* Cortex-M4 Processor Interruption and Exception Handlers */
/******************************************************************************/
/**
* @brief This function handles Non maskable interrupt.
*/
void NMI_Handler(void)
{
/* USER CODE BEGIN NonMaskableInt_IRQn 0 */
/* USER CODE END NonMaskableInt_IRQn 0 */
/* USER CODE BEGIN NonMaskableInt_IRQn 1 */
/* USER CODE END NonMaskableInt_IRQn 1 */
}
/**
* @brief This function handles Hard fault interrupt.
*/
void HardFault_Handler(void)
{
/* USER CODE BEGIN HardFault_IRQn 0 */
/* USER CODE END HardFault_IRQn 0 */
while (1)
{
/* USER CODE BEGIN W1_HardFault_IRQn 0 */
/* USER CODE END W1_HardFault_IRQn 0 */
}
}
/**
* @brief This function handles Memory management fault.
*/
void MemManage_Handler(void)
{
/* USER CODE BEGIN MemoryManagement_IRQn 0 */
/* USER CODE END MemoryManagement_IRQn 0 */
while (1)
{
/* USER CODE BEGIN W1_MemoryManagement_IRQn 0 */
/* USER CODE END W1_MemoryManagement_IRQn 0 */
}
}
/**
* @brief This function handles Prefetch fault, memory access fault.
*/
void BusFault_Handler(void)
{
/* USER CODE BEGIN BusFault_IRQn 0 */
/* USER CODE END BusFault_IRQn 0 */
while (1)
{
/* USER CODE BEGIN W1_BusFault_IRQn 0 */
/* USER CODE END W1_BusFault_IRQn 0 */
}
}
/**
* @brief This function handles Undefined instruction or illegal state.
*/
void UsageFault_Handler(void)
{
/* USER CODE BEGIN UsageFault_IRQn 0 */
/* USER CODE END UsageFault_IRQn 0 */
while (1)
{
/* USER CODE BEGIN W1_UsageFault_IRQn 0 */
/* USER CODE END W1_UsageFault_IRQn 0 */
}
}
/**
* @brief This function handles Debug monitor.
*/
void DebugMon_Handler(void)
{
/* USER CODE BEGIN DebugMonitor_IRQn 0 */
/* USER CODE END DebugMonitor_IRQn 0 */
/* USER CODE BEGIN DebugMonitor_IRQn 1 */
/* USER CODE END DebugMonitor_IRQn 1 */
}
/******************************************************************************/
/* STM32G4xx Peripheral Interrupt Handlers */
/* Add here the Interrupt Handlers for the used peripherals. */
/* For the available peripheral interrupt handler names, */
/* please refer to the startup file (startup_stm32g4xx.s). */
/******************************************************************************/
/**
* @brief This function handles DMA1 channel1 global interrupt.
*/
void DMA1_Channel1_IRQHandler(void)
{
/* USER CODE BEGIN DMA1_Channel1_IRQn 0 */
/* USER CODE END DMA1_Channel1_IRQn 0 */
/* USER CODE BEGIN DMA1_Channel1_IRQn 1 */
/* USER CODE END DMA1_Channel1_IRQn 1 */
}
/**
* @brief This function handles DMA1 channel2 global interrupt.
*/
void DMA1_Channel2_IRQHandler(void)
{
/* USER CODE BEGIN DMA1_Channel2_IRQn 0 */
/* USER CODE END DMA1_Channel2_IRQn 0 */
/* USER CODE BEGIN DMA1_Channel2_IRQn 1 */
/* USER CODE END DMA1_Channel2_IRQn 1 */
}
/**
* @brief This function handles USB high priority interrupt remap.
*/
void USB_HP_IRQHandler(void)
{
/* USER CODE BEGIN USB_HP_IRQn 0 */
/* USER CODE END USB_HP_IRQn 0 */
HAL_PCD_IRQHandler(&hpcd_USB_FS);
/* USER CODE BEGIN USB_HP_IRQn 1 */
/* USER CODE END USB_HP_IRQn 1 */
}
/**
* @brief This function handles USB low priority interrupt remap.
*/
void USB_LP_IRQHandler(void)
{
/* USER CODE BEGIN USB_LP_IRQn 0 */
/* USER CODE END USB_LP_IRQn 0 */
HAL_PCD_IRQHandler(&hpcd_USB_FS);
/* USER CODE BEGIN USB_LP_IRQn 1 */
/* USER CODE END USB_LP_IRQn 1 */
}
/**
* @brief This function handles TIM1 trigger and commutation interrupts and TIM17 global interrupt.
*/
void TIM1_TRG_COM_TIM17_IRQHandler(void)
{
/* USER CODE BEGIN TIM1_TRG_COM_TIM17_IRQn 0 */
/* USER CODE END TIM1_TRG_COM_TIM17_IRQn 0 */
HAL_TIM_IRQHandler(&htim1);
HAL_TIM_IRQHandler(&htim17);
/* USER CODE BEGIN TIM1_TRG_COM_TIM17_IRQn 1 */
/* USER CODE END TIM1_TRG_COM_TIM17_IRQn 1 */
}
/**
* @brief This function handles UCPD1 interrupt / UCPD1 wake-up interrupt through EXTI line 43.
*/
void UCPD1_IRQHandler(void)
{
/* USER CODE BEGIN UCPD1_IRQn 0 */
/* USER CODE END UCPD1_IRQn 0 */
/* USER CODE BEGIN UCPD1_IRQn 1 */
/* USER CODE END UCPD1_IRQn 1 */
}
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

View file

@ -0,0 +1,204 @@
/**
*****************************************************************************
**
** File : syscalls.c
**
** Abstract : System Workbench Minimal System calls file
**
** For more information about which c-functions
** need which of these lowlevel functions
** please consult the Newlib libc-manual
**
** Environment : System Workbench for MCU
**
** Distribution: The file is distributed as is, without any warranty
** of any kind.
**
*****************************************************************************
**
** <h2><center>&copy; COPYRIGHT(c) 2014 Ac6</center></h2>
**
** Redistribution and use in source and binary forms, with or without modification,
** are permitted provided that the following conditions are met:
** 1. Redistributions of source code must retain the above copyright notice,
** this list of conditions and the following disclaimer.
** 2. Redistributions in binary form must reproduce the above copyright notice,
** this list of conditions and the following disclaimer in the documentation
** and/or other materials provided with the distribution.
** 3. Neither the name of Ac6 nor the names of its contributors
** may be used to endorse or promote products derived from this software
** without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
** AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
** DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
** SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
** CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
** OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**
*****************************************************************************
*/
/* Includes */
#include <sys/stat.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <signal.h>
#include <time.h>
#include <sys/time.h>
#include <sys/times.h>
/* Variables */
//#undef errno
extern int errno;
extern int __io_putchar(int ch) __attribute__((weak));
extern int __io_getchar(void) __attribute__((weak));
register char * stack_ptr asm("sp");
char *__env[1] = { 0 };
char **environ = __env;
/* Functions */
void initialise_monitor_handles()
{
}
int _getpid(void)
{
return 1;
}
int _kill(int pid, int sig)
{
errno = EINVAL;
return -1;
}
void _exit (int status)
{
_kill(status, -1);
while (1) {} /* Make sure we hang here */
}
__attribute__((weak)) int _read(int file, char *ptr, int len)
{
int DataIdx;
for (DataIdx = 0; DataIdx < len; DataIdx++)
{
*ptr++ = __io_getchar();
}
return len;
}
__attribute__((weak)) int _write(int file, char *ptr, int len)
{
int DataIdx;
for (DataIdx = 0; DataIdx < len; DataIdx++)
{
__io_putchar(*ptr++);
}
return len;
}
caddr_t _sbrk(int incr)
{
extern char end asm("end");
static char *heap_end;
char *prev_heap_end;
if (heap_end == 0)
heap_end = &end;
prev_heap_end = heap_end;
if (heap_end + incr > stack_ptr)
{
// write(1, "Heap and stack collision\n", 25);
// abort();
errno = ENOMEM;
return (caddr_t) -1;
}
heap_end += incr;
return (caddr_t) prev_heap_end;
}
int _close(int file)
{
return -1;
}
int _fstat(int file, struct stat *st)
{
st->st_mode = S_IFCHR;
return 0;
}
int _isatty(int file)
{
return 1;
}
int _lseek(int file, int ptr, int dir)
{
return 0;
}
int _open(char *path, int flags, ...)
{
/* Pretend like we always fail */
return -1;
}
int _wait(int *status)
{
errno = ECHILD;
return -1;
}
int _unlink(char *name)
{
errno = ENOENT;
return -1;
}
int _times(struct tms *buf)
{
return -1;
}
int _stat(char *file, struct stat *st)
{
st->st_mode = S_IFCHR;
return 0;
}
int _link(char *old, char *new)
{
errno = EMLINK;
return -1;
}
int _fork(void)
{
errno = EAGAIN;
return -1;
}
int _execve(char *name, char **argv, char **env)
{
errno = ENOMEM;
return -1;
}

View file

@ -0,0 +1,271 @@
/**
******************************************************************************
* @file system_stm32g4xx.c
* @author MCD Application Team
* @brief CMSIS Cortex-M4 Device Peripheral Access Layer System Source File
*
* This file provides two functions and one global variable to be called from
* user application:
* - SystemInit(): This function is called at startup just after reset and
* before branch to main program. This call is made inside
* the "startup_stm32g4xx.s" file.
*
* - SystemCoreClock variable: Contains the core clock (HCLK), it can be used
* by the user application to setup the SysTick
* timer or configure other parameters.
*
* - SystemCoreClockUpdate(): Updates the variable SystemCoreClock and must
* be called whenever the core clock is changed
* during program execution.
*
* After each device reset the HSI (16 MHz) is used as system clock source.
* Then SystemInit() function is called, in "startup_stm32g4xx.s" file, to
* configure the system clock before to branch to main program.
*
* This file configures the system clock as follows:
*=============================================================================
*-----------------------------------------------------------------------------
* System Clock source | HSI
*-----------------------------------------------------------------------------
* SYSCLK(Hz) | 16000000
*-----------------------------------------------------------------------------
* HCLK(Hz) | 16000000
*-----------------------------------------------------------------------------
* AHB Prescaler | 1
*-----------------------------------------------------------------------------
* APB1 Prescaler | 1
*-----------------------------------------------------------------------------
* APB2 Prescaler | 1
*-----------------------------------------------------------------------------
* PLL_M | 1
*-----------------------------------------------------------------------------
* PLL_N | 16
*-----------------------------------------------------------------------------
* PLL_P | 7
*-----------------------------------------------------------------------------
* PLL_Q | 2
*-----------------------------------------------------------------------------
* PLL_R | 2
*-----------------------------------------------------------------------------
* Require 48MHz for RNG | Disabled
*-----------------------------------------------------------------------------
*=============================================================================
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2017 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
*
******************************************************************************
*/
/** @addtogroup CMSIS
* @{
*/
/** @addtogroup stm32g4xx_system
* @{
*/
/** @addtogroup STM32G4xx_System_Private_Includes
* @{
*/
#include "stm32g4xx.h"
#if !defined (HSE_VALUE)
#define HSE_VALUE 8000000U /*!< Value of the External oscillator in Hz */
#endif /* HSE_VALUE */
#if !defined (HSI_VALUE)
#define HSI_VALUE 16000000U /*!< Value of the Internal oscillator in Hz*/
#endif /* HSI_VALUE */
/**
* @}
*/
/** @addtogroup STM32G4xx_System_Private_TypesDefinitions
* @{
*/
/**
* @}
*/
/** @addtogroup STM32G4xx_System_Private_Defines
* @{
*/
/************************* Miscellaneous Configuration ************************/
/*!< Uncomment the following line if you need to relocate your vector Table in
Internal SRAM. */
/* #define VECT_TAB_SRAM */
#define VECT_TAB_OFFSET 0x00UL /*!< Vector Table base offset field.
This value must be a multiple of 0x200. */
/******************************************************************************/
/**
* @}
*/
/** @addtogroup STM32G4xx_System_Private_Macros
* @{
*/
/**
* @}
*/
/** @addtogroup STM32G4xx_System_Private_Variables
* @{
*/
/* The SystemCoreClock variable is updated in three ways:
1) by calling CMSIS function SystemCoreClockUpdate()
2) by calling HAL API function HAL_RCC_GetHCLKFreq()
3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
Note: If you use this function to configure the system clock; then there
is no need to call the 2 first functions listed above, since SystemCoreClock
variable is updated automatically.
*/
uint32_t SystemCoreClock = HSI_VALUE;
const uint8_t AHBPrescTable[16] = {0U, 0U, 0U, 0U, 0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U, 6U, 7U, 8U, 9U};
const uint8_t APBPrescTable[8] = {0U, 0U, 0U, 0U, 1U, 2U, 3U, 4U};
/**
* @}
*/
/** @addtogroup STM32G4xx_System_Private_FunctionPrototypes
* @{
*/
/**
* @}
*/
/** @addtogroup STM32G4xx_System_Private_Functions
* @{
*/
/**
* @brief Setup the microcontroller system.
* @param None
* @retval None
*/
void SystemInit(void)
{
/* FPU settings ------------------------------------------------------------*/
#if (__FPU_PRESENT == 1) && (__FPU_USED == 1)
SCB->CPACR |= ((3UL << (10*2))|(3UL << (11*2))); /* set CP10 and CP11 Full Access */
#endif
/* Configure the Vector Table location add offset address ------------------*/
#ifdef VECT_TAB_SRAM
SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */
#else
SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */
#endif
}
/**
* @brief Update SystemCoreClock variable according to Clock Register Values.
* The SystemCoreClock variable contains the core clock (HCLK), it can
* be used by the user application to setup the SysTick timer or configure
* other parameters.
*
* @note Each time the core clock (HCLK) changes, this function must be called
* to update SystemCoreClock variable value. Otherwise, any configuration
* based on this variable will be incorrect.
*
* @note - The system frequency computed by this function is not the real
* frequency in the chip. It is calculated based on the predefined
* constant and the selected clock source:
*
* - If SYSCLK source is HSI, SystemCoreClock will contain the HSI_VALUE(**)
*
* - If SYSCLK source is HSE, SystemCoreClock will contain the HSE_VALUE(***)
*
* - If SYSCLK source is PLL, SystemCoreClock will contain the HSE_VALUE(***)
* or HSI_VALUE(*) multiplied/divided by the PLL factors.
*
* (**) HSI_VALUE is a constant defined in stm32g4xx_hal.h file (default value
* 16 MHz) but the real value may vary depending on the variations
* in voltage and temperature.
*
* (***) HSE_VALUE is a constant defined in stm32g4xx_hal.h file (default value
* 8 MHz), user has to ensure that HSE_VALUE is same as the real
* frequency of the crystal used. Otherwise, this function may
* have wrong result.
*
* - The result of this function could be not correct when using fractional
* value for HSE crystal.
*
* @param None
* @retval None
*/
void SystemCoreClockUpdate(void)
{
uint32_t tmp, pllvco, pllr, pllsource, pllm;
/* Get SYSCLK source -------------------------------------------------------*/
switch (RCC->CFGR & RCC_CFGR_SWS)
{
case 0x04: /* HSI used as system clock source */
SystemCoreClock = HSI_VALUE;
break;
case 0x08: /* HSE used as system clock source */
SystemCoreClock = HSE_VALUE;
break;
case 0x0C: /* PLL used as system clock source */
/* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLLM) * PLLN
SYSCLK = PLL_VCO / PLLR
*/
pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC);
pllm = ((RCC->PLLCFGR & RCC_PLLCFGR_PLLM) >> 4) + 1U ;
if (pllsource == 0x02UL) /* HSI used as PLL clock source */
{
pllvco = (HSI_VALUE / pllm);
}
else /* HSE used as PLL clock source */
{
pllvco = (HSE_VALUE / pllm);
}
pllvco = pllvco * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 8);
pllr = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLR) >> 25) + 1U) * 2U;
SystemCoreClock = pllvco/pllr;
break;
default:
break;
}
/* Compute HCLK clock frequency --------------------------------------------*/
/* Get HCLK prescaler */
tmp = AHBPrescTable[((RCC->CFGR & RCC_CFGR_HPRE) >> 4)];
/* HCLK clock frequency */
SystemCoreClock >>= tmp;
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

View file

@ -0,0 +1,70 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file usbpd.c
* @author MCD Application Team
* @brief This file contains the device define.
******************************************************************************
* @attention
*
* <h2><center>&copy; 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 */
/* Includes ------------------------------------------------------------------*/
#include "usbpd.h"
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* Global variables ---------------------------------------------------------*/
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* USBPD init function */
void MX_USBPD_Init(void)
{
/* Global Init of USBPD HW */
USBPD_HW_IF_GlobalHwInit();
/* Initialize the Device Policy Manager */
if(USBPD_OK != USBPD_DPM_InitCore())
{
while(1);
}
/* Initialise the DPM application */
if (USBPD_OK != USBPD_DPM_UserInit())
{
while(1);
}
/* USER CODE BEGIN 3 */
/* USER CODE END 3 */
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @}
*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

View file

@ -0,0 +1,350 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file usbpd_dpm_core.c
* @author MCD Application Team
* @brief USBPD dpm core file
******************************************************************************
* @attention
*
* <h2><center>&copy; 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 __USBPD_DPM_CORE_C
/* Includes ------------------------------------------------------------------*/
#include "usbpd_core.h"
#include "usbpd_trace.h"
#include "usbpd_dpm_core.h"
#include "usbpd_dpm_user.h"
#include "usbpd_dpm_conf.h"
#include "cmsis_os.h"
/* Private enum */
enum
{
USBPD_THREAD_PORT_0 = USBPD_PORT_0,
#if USBPD_PORT_COUNT == 2
USBPD_THREAD_PORT_1 = USBPD_PORT_1,
#endif
USBPD_THREAD_CAD
};
/* Generic STM32 prototypes */
extern uint32_t HAL_GetTick(void);
/* Private function prototypes -----------------------------------------------*/
void USBPD_DPM_CADCallback(uint8_t PortNum, USBPD_CAD_EVENT State, CCxPin_TypeDef Cc);
void USBPD_PE_Task(void const *argument);
void USBPD_CAD_Task(void const *argument);
static void USBPD_PE_TaskWakeUp(uint8_t PortNum);
static void USBPD_DPM_CADTaskWakeUp(void);
/* Private typedef -----------------------------------------------------------*/
osThreadDef(PE_0, USBPD_PE_Task, osPriorityHigh, 0, 200);
osThreadDef(PE_1, USBPD_PE_Task, osPriorityHigh, 0, 200);
/* Private define ------------------------------------------------------------*/
#define MAX_TREAD_POWER (USBPD_PORT_COUNT + 1)
#define OSTHREAD_PE(__VAL__) __VAL__==USBPD_PORT_0?osThread(PE_0):osThread(PE_1)
/* Private macro -------------------------------------------------------------*/
#define CHECK_PE_FUNCTION_CALL(_function_) if(USBPD_OK != _function_) {return USBPD_ERROR;}
#define CHECK_CAD_FUNCTION_CALL(_function_) if(USBPD_CAD_OK != _function_) {return USBPD_ERROR;}
#if defined(_DEBUG_TRACE)
#define DPM_CORE_DEBUG_TRACE(_PORTNUM_, __MESSAGE__) USBPD_TRACE_Add(USBPD_TRACE_DEBUG, _PORTNUM_, 0u, (uint8_t *)(__MESSAGE__), sizeof(__MESSAGE__) - 1u);
#else
#define DPM_CORE_DEBUG_TRACE(_PORTNUM_, __MESSAGE__)
#endif /* _DEBUG_TRACE */
/* Private variables ---------------------------------------------------------*/
static uint32_t DPM_Sleep_time[MAX_TREAD_POWER];
static osThreadId DPM_Thread_Table[MAX_TREAD_POWER];
osMessageQDef(queuePE, 2, uint16_t);
osMessageQDef(queueCAD, 1, uint16_t);
static osMessageQId PEQueueId[USBPD_PORT_COUNT], CADQueueId;
USBPD_ParamsTypeDef DPM_Params[USBPD_PORT_COUNT];
/* Private functions ---------------------------------------------------------*/
static void DPM_ManageAttachedState(uint8_t PortNum, USBPD_CAD_EVENT State, CCxPin_TypeDef Cc);
/**
* @brief Initialize the core stack (port power role, PWR_IF, CAD and PE Init procedures)
* @retval USBPD status
*/
USBPD_StatusTypeDef USBPD_DPM_InitCore(void)
{
/* variable to get dynamique memory allocated by usbpd stack */
uint32_t stack_dynamemsize;
static const USBPD_PE_Callbacks dpmCallbacks =
{
NULL,
NULL,
USBPD_DPM_EvaluatePowerRoleSwap,
USBPD_DPM_Notification,
NULL,
USBPD_DPM_GetDataInfo,
USBPD_DPM_SetDataInfo,
NULL,
USBPD_DPM_SNK_EvaluateCapabilities,
NULL,
USBPD_PE_TaskWakeUp,
NULL,
NULL,
NULL,
USBPD_DPM_EvaluateDataRoleSwap,
USBPD_DPM_IsPowerReady
};
static const USBPD_CAD_Callbacks CAD_cbs = { USBPD_DPM_CADCallback, USBPD_DPM_CADTaskWakeUp };
/* Check the lib selected */
if( USBPD_TRUE != USBPD_PE_CheckLIB(_LIB_ID))
{
return USBPD_ERROR;
}
/* to get how much memory are dynamically allocated by the stack
the memory return is corresponding to 2 ports so if the application
managed only one port divide the value return by 2 */
stack_dynamemsize = USBPD_PE_GetMemoryConsumption();
/* done to avoid warning */
stack_dynamemsize--;
/* Variable to be sure that DPM is correctly initialized */
DPM_Params[USBPD_PORT_0].DPM_Initialized = USBPD_FALSE;
#if USBPD_PORT_COUNT == 2
DPM_Params[USBPD_PORT_1].DPM_Initialized = USBPD_FALSE;
#endif /* USBPD_PORT_COUNT == 2 */
/* check the stack settings */
DPM_Params[USBPD_PORT_0].PE_SpecRevision = DPM_Settings[USBPD_PORT_0].PE_SpecRevision;
DPM_Params[USBPD_PORT_0].PE_PowerRole = DPM_Settings[USBPD_PORT_0].PE_DefaultRole;
DPM_Params[USBPD_PORT_0].PE_SwapOngoing = USBPD_FALSE;
DPM_Params[USBPD_PORT_0].ActiveCCIs = CCNONE;
DPM_Params[USBPD_PORT_0].VconnCCIs = CCNONE;
DPM_Params[USBPD_PORT_0].VconnStatus = USBPD_FALSE;
#if USBPD_PORT_COUNT == 2
DPM_Params[USBPD_PORT_1].PE_SpecRevision = DPM_Settings[USBPD_PORT_1].PE_SpecRevision;
DPM_Params[USBPD_PORT_1].PE_PowerRole = DPM_Settings[USBPD_PORT_1].PE_DefaultRole;
DPM_Params[USBPD_PORT_1].PE_SwapOngoing = USBPD_FALSE;
DPM_Params[USBPD_PORT_1].ActiveCCIs = CCNONE;
DPM_Params[USBPD_PORT_1].VconnCCIs = CCNONE;
DPM_Params[USBPD_PORT_1].VconnStatus = USBPD_FALSE;
#endif /* USBPD_PORT_COUNT == 2 */
/* CAD SET UP : Port 0 */
CHECK_CAD_FUNCTION_CALL(USBPD_CAD_Init(USBPD_PORT_0, (USBPD_CAD_Callbacks *)&CAD_cbs, (USBPD_SettingsTypeDef *)&DPM_Settings[USBPD_PORT_0], &DPM_Params[USBPD_PORT_0]));
#if USBPD_PORT_COUNT == 2
CHECK_CAD_FUNCTION_CALL(USBPD_CAD_Init(USBPD_PORT_1, (USBPD_CAD_Callbacks *)&CAD_cbs, (USBPD_SettingsTypeDef *)&DPM_Settings[USBPD_PORT_1], &DPM_Params[USBPD_PORT_1]));
#endif /* USBPD_PORT_COUNT == 2 */
/* PE SET UP : Port 0 */
CHECK_PE_FUNCTION_CALL(USBPD_PE_Init(USBPD_PORT_0, (USBPD_SettingsTypeDef *)&DPM_Settings[USBPD_PORT_0], &DPM_Params[USBPD_PORT_0], &dpmCallbacks));
#if USBPD_PORT_COUNT == 2
CHECK_PE_FUNCTION_CALL(USBPD_PE_Init(USBPD_PORT_1, (USBPD_SettingsTypeDef *)&DPM_Settings[USBPD_PORT_1], &DPM_Params[USBPD_PORT_1], &dpmCallbacks));
#endif /* USBPD_PORT_COUNT == 2 */
/* DPM is correctly initialized */
DPM_Params[USBPD_PORT_0].DPM_Initialized = USBPD_TRUE;
#if USBPD_PORT_COUNT == 2
DPM_Params[USBPD_PORT_1].DPM_Initialized = USBPD_TRUE;
#endif /* USBPD_PORT_COUNT == 2 */
/* Enable CAD on Port 0 */
USBPD_CAD_PortEnable(USBPD_PORT_0, USBPD_CAD_ENABLE);
#if USBPD_PORT_COUNT == 2
USBPD_CAD_PortEnable(USBPD_PORT_1, USBPD_CAD_ENABLE);
#endif /* USBPD_PORT_COUNT == 2 */
return USBPD_OK;
}
/**
* @brief Initialize the OS parts (task, queue,... )
* @retval USBPD status
*/
USBPD_StatusTypeDef USBPD_DPM_InitOS(void)
{
osThreadDef(CAD, USBPD_CAD_Task, osPriorityRealtime, 0, 300);
if((DPM_Thread_Table[USBPD_THREAD_CAD] = osThreadCreate(osThread(CAD), NULL)) == NULL)
{
return USBPD_ERROR;
}
CADQueueId = osMessageCreate(osMessageQ(queueCAD), NULL);
/* Create the queue corresponding to PE task */
PEQueueId[0] = osMessageCreate(osMessageQ(queuePE), NULL);
#if USBPD_PORT_COUNT == 2
PEQueueId[1] = osMessageCreate(osMessageQ(queuePE), NULL);
#endif /* USBPD_PORT_COUNT == 2 */
/* PE task to be created on attachment */
DPM_Thread_Table[USBPD_THREAD_PORT_0] = NULL;
#if USBPD_PORT_COUNT == 2
DPM_Thread_Table[USBPD_THREAD_PORT_1] = NULL;
#endif /* USBPD_PORT_COUNT == 2 */
return USBPD_OK;
}
/**
* @brief Initialize the OS parts (port power role, PWR_IF, CAD and PE Init procedures)
* @retval None
*/
void USBPD_DPM_Run(void)
{
osKernelStart();
}
/**
* @brief Initialize DPM (port power role, PWR_IF, CAD and PE Init procedures)
* @retval USBPD status
*/
void USBPD_DPM_TimerCounter(void)
{
/* Call PE/PRL timers functions only if DPM is initialized */
if (USBPD_TRUE == DPM_Params[USBPD_PORT_0].DPM_Initialized)
{
USBPD_DPM_UserTimerCounter(USBPD_PORT_0);
USBPD_PE_TimerCounter(USBPD_PORT_0);
USBPD_PRL_TimerCounter(USBPD_PORT_0);
}
#if USBPD_PORT_COUNT==2
if (USBPD_TRUE == DPM_Params[USBPD_PORT_1].DPM_Initialized)
{
USBPD_DPM_UserTimerCounter(USBPD_PORT_1);
USBPD_PE_TimerCounter(USBPD_PORT_1);
USBPD_PRL_TimerCounter(USBPD_PORT_1);
}
#endif /* USBPD_PORT_COUNT == 2 */
}
/**
* @brief WakeUp PE task
* @param PortNum port number
* @retval None
*/
static void USBPD_PE_TaskWakeUp(uint8_t PortNum)
{
osMessagePut(PEQueueId[PortNum], 0xFFFF, 0);
}
/**
* @brief WakeUp CAD task
* @retval None
*/
static void USBPD_DPM_CADTaskWakeUp(void)
{
osMessagePut(CADQueueId, 0xFFFF, 0);
}
/**
* @brief Main task for PE layer
* @param argument Not used
* @retval None
*/
void USBPD_PE_Task(void const *argument)
{
uint8_t _port = (uint32_t)argument;
for(;;)
{
DPM_Sleep_time[_port] =
USBPD_PE_StateMachine_SNK(_port);
osMessageGet(PEQueueId[_port], DPM_Sleep_time[_port]);
}
}
/**
* @brief Main task for CAD layer
* @param argument Not used
* @retval None
*/
void USBPD_CAD_Task(void const *argument)
{
for(;;)
{
DPM_Sleep_time[USBPD_THREAD_CAD] = USBPD_CAD_Process();
osMessageGet(CADQueueId, DPM_Sleep_time[USBPD_THREAD_CAD]);
}
}
/**
* @brief CallBack reporting events on a specified port from CAD layer.
* @param PortNum The handle of the port
* @param State CAD state
* @param Cc The Communication Channel for the USBPD communication
* @retval None
*/
void USBPD_DPM_CADCallback(uint8_t PortNum, USBPD_CAD_EVENT State, CCxPin_TypeDef Cc)
{
switch(State)
{
case USBPD_CAD_EVENT_ATTEMC :
{
DPM_ManageAttachedState(PortNum, State, Cc);
break;
}
case USBPD_CAD_EVENT_ATTACHED :
DPM_ManageAttachedState(PortNum, State, Cc);
break;
case USBPD_CAD_EVENT_DETACHED :
case USBPD_CAD_EVENT_EMC :
{
/* The ufp is detached */
(void)USBPD_PE_IsCableConnected(PortNum, 0);
/* Terminate PE task */
if (DPM_Thread_Table[PortNum] != NULL)
{
osThreadTerminate(DPM_Thread_Table[PortNum]);
DPM_Thread_Table[PortNum] = NULL;
}
USBPD_DPM_UserCableDetection(PortNum, State);
DPM_Params[PortNum].ActiveCCIs = CCNONE;
DPM_Params[PortNum].PE_Power = USBPD_POWER_NO;
break;
}
default :
/* nothing to do */
break;
}
}
static void DPM_ManageAttachedState(uint8_t PortNum, USBPD_CAD_EVENT State, CCxPin_TypeDef Cc)
{
DPM_Params[PortNum].ActiveCCIs = Cc;
(void)USBPD_PE_IsCableConnected(PortNum, 1);
USBPD_DPM_UserCableDetection(PortNum, State);
/* Create PE task */
if (DPM_Thread_Table[PortNum] == NULL)
{
DPM_Thread_Table[PortNum] = osThreadCreate(OSTHREAD_PE(PortNum), (void *)((uint32_t)PortNum));
if (DPM_Thread_Table[PortNum] == NULL)
{
/* should not occurr. May be an issue with FreeRTOS heap size too small */
while(1);
}
}
}
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

View file

@ -0,0 +1,405 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file usbpd_dpm_user.c
* @author MCD Application Team
* @brief USBPD DPM user code
******************************************************************************
* @attention
*
* <h2><center>&copy; 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 USBPD_DPM_USER_C
/* Includes ------------------------------------------------------------------*/
#include "usbpd_core.h"
#include "usbpd_pdo_defs.h"
#include "usbpd_dpm_core.h"
#include "usbpd_dpm_conf.h"
#include "usbpd_dpm_user.h"
#include "usbpd_vdm_user.h"
#include "usbpd_trace.h"
#include "usbpd_pwr_if.h"
#include "string.h"
#include "cmsis_os.h"
#include "usbpd_pwr_user.h"
/** @addtogroup STM32_USBPD_APPLICATION
* @{
*/
/** @addtogroup STM32_USBPD_APPLICATION_DPM_USER
* @{
*/
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN Private_Typedef */
/* USER CODE END Private_Typedef */
/* Private define ------------------------------------------------------------*/
/** @defgroup USBPD_USER_PRIVATE_DEFINES USBPD USER Private Defines
* @{
*/
/* USER CODE BEGIN Private_Define */
/* USER CODE END Private_Define */
/**
* @}
*/
/* Private macro -------------------------------------------------------------*/
/** @defgroup USBPD_USER_PRIVATE_MACROS USBPD USER Private Macros
* @{
*/
/* USER CODE BEGIN Private_Macro */
/* USER CODE END Private_Macro */
/**
* @}
*/
/* Private variables ---------------------------------------------------------*/
/** @defgroup USBPD_USER_PRIVATE_VARIABLES USBPD USER Private Variables
* @{
*/
/* USER CODE BEGIN Private_Variables */
/* USER CODE END Private_Variables */
/**
* @}
*/
/* Private function prototypes -----------------------------------------------*/
/** @defgroup USBPD_USER_PRIVATE_FUNCTIONS USBPD USER Private Functions
* @{
*/
/* USER CODE BEGIN USBPD_USER_PRIVATE_FUNCTIONS_Prototypes */
/* USER CODE END USBPD_USER_PRIVATE_FUNCTIONS_Prototypes */
/**
* @}
*/
/* Exported functions ------- ------------------------------------------------*/
/** @defgroup USBPD_USER_EXPORTED_FUNCTIONS USBPD USER Exported Functions
* @{
*/
/* USER CODE BEGIN USBPD_USER_EXPORTED_FUNCTIONS */
/* USER CODE END USBPD_USER_EXPORTED_FUNCTIONS */
/** @defgroup USBPD_USER_EXPORTED_FUNCTIONS_GROUP1 USBPD USER Exported Functions called by DPM CORE
* @{
*/
/* USER CODE BEGIN USBPD_USER_EXPORTED_FUNCTIONS_GROUP1 */
/* USER CODE END USBPD_USER_EXPORTED_FUNCTIONS_GROUP1 */
/**
* @brief Initialize DPM (port power role, PWR_IF, CAD and PE Init procedures)
* @retval USBPD Status
*/
USBPD_StatusTypeDef USBPD_DPM_UserInit(void)
{
/* USER CODE BEGIN USBPD_DPM_UserInit */
return USBPD_OK;
/* USER CODE END USBPD_DPM_UserInit */
}
/**
* @brief User delay implementation which is OS dependant
* @param Time time in ms
* @retval None
*/
void USBPD_DPM_WaitForTime(uint32_t Time)
{
/* USER CODE BEGIN USBPD_DPM_WaitForTime */
#ifdef _RTOS
osDelay(Time);
#else
HAL_Delay(Time);
#endif
/* USER CODE END USBPD_DPM_WaitForTime */
}
/**
* @brief User processing time, it is recommended to avoid blocking task for long time
* @param argument DPM User event
* @retval None
*/
void USBPD_DPM_UserExecute(void const *argument)
{
/* USER CODE BEGIN USBPD_DPM_UserExecute */
/* USER CODE END USBPD_DPM_UserExecute */
}
/**
* @brief UserCableDetection reporting events on a specified port from CAD layer.
* @param PortNum The handle of the port
* @param State CAD state
* @retval None
*/
void USBPD_DPM_UserCableDetection(uint8_t PortNum, USBPD_CAD_EVENT State)
{
/* USER CODE BEGIN USBPD_DPM_UserCableDetection */
/* USER CODE END USBPD_DPM_UserCableDetection */
}
/**
* @brief function used to manage user timer.
* @param PortNum Port number
* @retval None
*/
void USBPD_DPM_UserTimerCounter(uint8_t PortNum)
{
/* USER CODE BEGIN USBPD_DPM_UserTimerCounter */
/* USER CODE END USBPD_DPM_UserTimerCounter */
}
/**
* @}
*/
/** @defgroup USBPD_USER_EXPORTED_FUNCTIONS_GROUP2 USBPD USER Exported Callbacks functions called by PE
* @{
*/
/**
* @brief Callback function called by PE layer when HardReset message received from PRL
* @param PortNum The current port number
* @param CurrentRole the current role
* @param Status status on hard reset event
* @retval None
*/
void USBPD_DPM_HardReset(uint8_t PortNum, USBPD_PortPowerRole_TypeDef CurrentRole, USBPD_HR_Status_TypeDef Status)
{
/* USER CODE BEGIN USBPD_DPM_HardReset */
/* USER CODE END USBPD_DPM_HardReset */
}
/**
* @brief Request the DPM to setup the new power level.
* @param PortNum The current port number
* @retval USBPD status
*/
USBPD_StatusTypeDef USBPD_DPM_SetupNewPower(uint8_t PortNum)
{
/* USER CODE BEGIN USBPD_DPM_SetupNewPower */
return USBPD_PWR_IF_SetProfile(PortNum);
/* USER CODE END USBPD_DPM_SetupNewPower */
}
/**
* @brief Evaluate the swap request from PE.
* @param PortNum The current port number
* @retval USBPD_ACCEPT, USBPD_WAIT, USBPD_REJECT
*/
USBPD_StatusTypeDef USBPD_DPM_EvaluatePowerRoleSwap(uint8_t PortNum)
{
/* USER CODE BEGIN USBPD_DPM_EvaluatePowerRoleSwap */
return USBPD_REJECT;
/* USER CODE END USBPD_DPM_EvaluatePowerRoleSwap */
}
/**
* @brief Callback function called by PE to inform DPM about PE event.
* @param PortNum The current port number
* @param EventVal @ref USBPD_NotifyEventValue_TypeDef
* @retval None
*/
void USBPD_DPM_Notification(uint8_t PortNum, USBPD_NotifyEventValue_TypeDef EventVal)
{
/* USER CODE BEGIN USBPD_DPM_Notification */
/* USER CODE END USBPD_DPM_Notification */
}
/**
* @brief DPM callback to allow PE to retrieve information from DPM/PWR_IF.
* @param PortNum Port number
* @param DataId Type of data to be updated in DPM based on @ref USBPD_CORE_DataInfoType_TypeDef
* @param Ptr Pointer on address where DPM data should be written (u8 pointer)
* @param Size Pointer on nb of u8 written by DPM
* @retval None
*/
void USBPD_DPM_GetDataInfo(uint8_t PortNum, USBPD_CORE_DataInfoType_TypeDef DataId, uint8_t *Ptr, uint32_t *Size)
{
/* USER CODE BEGIN USBPD_DPM_GetDataInfo */
/* USER CODE END USBPD_DPM_GetDataInfo */
}
/**
* @brief DPM callback to allow PE to update information in DPM/PWR_IF.
* @param PortNum Port number
* @param DataId Type of data to be updated in DPM based on @ref USBPD_CORE_DataInfoType_TypeDef
* @param Ptr Pointer on the data
* @param Size Nb of bytes to be updated in DPM
* @retval None
*/
void USBPD_DPM_SetDataInfo(uint8_t PortNum, USBPD_CORE_DataInfoType_TypeDef DataId, uint8_t *Ptr, uint32_t Size)
{
/* USER CODE BEGIN USBPD_DPM_SetDataInfo */
/* USER CODE END USBPD_DPM_SetDataInfo */
}
/**
* @brief Evaluate received Request Message from Sink port
* @param PortNum Port number
* @param PtrPowerObject Pointer on the power data object
* @retval USBPD status : USBPD_ACCEPT, USBPD_REJECT, USBPD_WAIT, USBPD_GOTOMIN
*/
USBPD_StatusTypeDef USBPD_DPM_EvaluateRequest(uint8_t PortNum, USBPD_CORE_PDO_Type_TypeDef *PtrPowerObject)
{
/* USER CODE BEGIN USBPD_DPM_EvaluateRequest */
return USBPD_REJECT;
/* USER CODE END USBPD_DPM_EvaluateRequest */
}
/**
* @brief Evaluate received Capabilities Message from Source port and prepare the request message
* @param PortNum Port number
* @param PtrRequestData Pointer on selected request data object
* @param PtrPowerObjectType Pointer on the power data object
* @retval None
*/
void USBPD_DPM_SNK_EvaluateCapabilities(uint8_t PortNum, uint32_t *PtrRequestData, USBPD_CORE_PDO_Type_TypeDef *PtrPowerObjectType)
{
/* USER CODE BEGIN USBPD_DPM_SNK_EvaluateCapabilities */
/* USER CODE END USBPD_DPM_SNK_EvaluateCapabilities */
}
/**
* @brief Power role swap status update
* @param PortNum Port number
* @param CurrentRole the current role
* @param Status status on power role swap event
*/
void USBPD_DPM_PowerRoleSwap(uint8_t PortNum, USBPD_PortPowerRole_TypeDef CurrentRole, USBPD_PRS_Status_TypeDef Status)
{
/* USER CODE BEGIN USBPD_DPM_PowerRoleSwap */
/* USER CODE END USBPD_DPM_PowerRoleSwap */
}
/**
* @brief Callback to be used by PE to evaluate a Vconn swap
* @param PortNum Port number
* @retval USBPD_ACCEPT, USBPD_REJECT, USBPD_WAIT
*/
USBPD_StatusTypeDef USBPD_DPM_EvaluateVconnSwap(uint8_t PortNum)
{
/* USER CODE BEGIN USBPD_DPM_EvaluateVconnSwap */
return USBPD_REJECT;
/* USER CODE END USBPD_DPM_EvaluateVconnSwap */
}
/**
* @brief Callback to be used by PE to manage VConn
* @param PortNum Port number
* @param State Enable or Disable VConn on CC lines
* @retval USBPD_ACCEPT, USBPD_REJECT
*/
USBPD_StatusTypeDef USBPD_DPM_PE_VconnPwr(uint8_t PortNum, USBPD_FunctionalState State)
{
/* USER CODE BEGIN USBPD_DPM_PE_VconnPwr */
return USBPD_ERROR;
/* USER CODE END USBPD_DPM_PE_VconnPwr */
}
/**
* @brief DPM callback to allow PE to forward extended message information.
* @param PortNum Port number
* @param MsgType Type of message to be handled in DPM
* This parameter can be one of the following values:
* @arg @ref USBPD_EXT_SECURITY_REQUEST Security Request extended message
* @arg @ref USBPD_EXT_SECURITY_RESPONSE Security Response extended message
* @param ptrData Pointer on address Extended Message data could be read (u8 pointer)
* @param DataSize Nb of u8 that compose Extended message
* @retval None
*/
void USBPD_DPM_ExtendedMessageReceived(uint8_t PortNum, USBPD_ExtendedMsg_TypeDef MsgType, uint8_t *ptrData, uint16_t DataSize)
{
/* USER CODE BEGIN USBPD_DPM_ExtendedMessageReceived */
/* USER CODE END USBPD_DPM_ExtendedMessageReceived */
}
/**
* @brief DPM callback used to know user choice about Data Role Swap.
* @param PortNum Port number
* @retval USBPD_REJECT, UBPD_ACCEPT
*/
USBPD_StatusTypeDef USBPD_DPM_EvaluateDataRoleSwap(uint8_t PortNum)
{
/* USER CODE BEGIN USBPD_DPM_EvaluateDataRoleSwap */
return USBPD_REJECT;
/* USER CODE END USBPD_DPM_EvaluateDataRoleSwap */
}
/**
* @brief Callback to be used by PE to check is VBUS is ready or present
* @param PortNum Port number
* @param Vsafe Vsafe status based on @ref USBPD_VSAFE_StatusTypeDef
* @retval USBPD_DISABLE or USBPD_ENABLE
*/
USBPD_FunctionalState USBPD_DPM_IsPowerReady(uint8_t PortNum, USBPD_VSAFE_StatusTypeDef Vsafe)
{
/* USER CODE BEGIN USBPD_DPM_IsPowerReady */
return ((USBPD_OK == USBPD_PWR_IF_SupplyReady(PortNum, Vsafe)) ? USBPD_ENABLE : USBPD_DISABLE);
/* USER CODE END USBPD_DPM_IsPowerReady */
}
/**
* @}
*/
/** @defgroup USBPD_USER_EXPORTED_FUNCTIONS_GROUP3 USBPD USER Functions PD messages requests
* @{
*/
/**
* @}
*/
/** @addtogroup USBPD_USER_PRIVATE_FUNCTIONS
* @{
*/
/* USER CODE BEGIN USBPD_USER_PRIVATE_FUNCTIONS */
/* USER CODE END USBPD_USER_PRIVATE_FUNCTIONS */
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

View file

@ -0,0 +1,341 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file usbpd_pwr_if.c
* @author MCD Application Team
* @brief This file contains power interface control functions.
******************************************************************************
* @attention
*
* <h2><center>&copy; 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 __USBPD_PWR_IF_C
/* Includes ------------------------------------------------------------------*/
#include "usbpd_pwr_if.h"
#include "usbpd_hw_if.h"
#include "usbpd_pwr_if.h"
#include "usbpd_dpm_core.h"
#include "usbpd_dpm_conf.h"
#include "usbpd_pdo_defs.h"
#include "usbpd_core.h"
#include "usbpd_trace.h"
/* USER CODE BEGIN Include */
/* USER CODE END Include */
/** @addtogroup STM32_USBPD_APPLICATION
* @{
*/
/** @addtogroup STM32_USBPD_APPLICATION_POWER_IF
* @{
*/
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN Private_Typedef */
/* USER CODE END Private_Typedef */
/* Private define ------------------------------------------------------------*/
/** @addtogroup STM32_USBPD_APPLICATION_POWER_IF_Private_Defines
* @{
*/
/* USER CODE BEGIN Private_Define */
/* USER CODE END Private_Define */
/**
* @}
*/
/* Private macros ------------------------------------------------------------*/
/** @addtogroup STM32_USBPD_APPLICATION_POWER_IF_Private_Macros
* @{
*/
/* USER CODE BEGIN Private_Macro */
/* USER CODE END Private_Macro */
/**
* @}
*/
/* Private variables ---------------------------------------------------------*/
/** @addtogroup STM32_USBPD_APPLICATION_POWER_IF_Private_Variables
* @{
*/
/* USER CODE BEGIN Private_Variables */
/* USER CODE END Private_Variables */
/**
* @}
*/
/* Private function prototypes -----------------------------------------------*/
/** @addtogroup STM32_USBPD_APPLICATION_POWER_IF_Private_Functions
* @{
*/
/* USER CODE BEGIN USBPD_USER_PRIVATE_FUNCTIONS_Prototypes */
/* Functions to initialize Source PDOs */
uint32_t _PWR_SRCFixedPDO(float _C_, float _V_,
USBPD_CORE_PDO_PeakCurr_TypeDef _PK_,
USBPD_CORE_PDO_DRDataSupport_TypeDef DRDSupport,
USBPD_CORE_PDO_USBCommCapable_TypeDef UsbCommCapable,
USBPD_CORE_PDO_ExtPowered_TypeDef ExtPower,
USBPD_CORE_PDO_USBSuspendSupport_TypeDef UsbSuspendSupport,
USBPD_CORE_PDO_DRPowerSupport_TypeDef DRPSupport);
uint32_t _PWR_SRCVariablePDO(float _MAXV_, float _MINV_, float _C_);
uint32_t _PWR_SRCBatteryPDO(float _MAXV_,float _MINV_,float _PWR_);
/* Functions to initialize Sink PDOs */
uint32_t _PWR_SNKFixedPDO(float _C_, float _V_,
USBPD_CORE_PDO_DRDataSupport_TypeDef DRDSupport,
USBPD_CORE_PDO_USBCommCapable_TypeDef UsbCommCapable,
USBPD_CORE_PDO_ExtPowered_TypeDef ExtPower,
USBPD_CORE_PDO_HigherCapability_TypeDef HigherCapab,
USBPD_CORE_PDO_DRPowerSupport_TypeDef DRPSupport);
uint32_t _PWR_SNKVariablePDO(float _MAXV_,float _MINV_,float _C_);
uint32_t _PWR_SNKBatteryPDO(float _MAXV_,float _MINV_,float _PWR_);
/* USER CODE END USBPD_USER_PRIVATE_FUNCTIONS_Prototypes */
/**
* @}
*/
/** @addtogroup STM32_USBPD_APPLICATION_POWER_IF_Exported_Functions
* @{
*/
/**
* @brief Initialize structures and variables related to power board profiles
* used by Sink and Source, for all available ports.
* @retval USBPD status
*/
USBPD_StatusTypeDef USBPD_PWR_IF_Init(void)
{
/* USER CODE BEGIN USBPD_PWR_IF_Init */
return USBPD_ERROR;
/* USER CODE END USBPD_PWR_IF_Init */
}
/**
* @brief Sets the required power profile, now it works only with Fixed ones
* @param PortNum Port number
* @retval USBPD status
*/
USBPD_StatusTypeDef USBPD_PWR_IF_SetProfile(uint8_t PortNum)
{
/* USER CODE BEGIN USBPD_PWR_IF_SetProfile */
return USBPD_ERROR;
/* USER CODE END USBPD_PWR_IF_SetProfile */
}
/**
* @brief Resets the Power Board
* @retval USBPD status
*/
USBPD_StatusTypeDef USBPD_PWR_IF_PowerResetGlobal(void)
{
/* USER CODE BEGIN USBPD_PWR_IF_PowerResetGlobal */
return USBPD_ERROR;
/* USER CODE END USBPD_PWR_IF_PowerResetGlobal */
}
/**
* @brief Resets the Power on a specified port
* @param PortNum Port number
* @retval USBPD status
*/
USBPD_StatusTypeDef USBPD_PWR_IF_PowerReset(uint8_t PortNum)
{
/* USER CODE BEGIN USBPD_PWR_IF_PowerReset */
return USBPD_ERROR;
/* USER CODE END USBPD_PWR_IF_PowerReset */
}
/**
* @brief Checks if the power on a specified port is ready
* @param PortNum Port number
* @param Vsafe Vsafe status based on @ref USBPD_VSAFE_StatusTypeDef
* @retval USBPD status
*/
USBPD_StatusTypeDef USBPD_PWR_IF_SupplyReady(uint8_t PortNum, USBPD_VSAFE_StatusTypeDef Vsafe)
{
/* USER CODE BEGIN USBPD_PWR_IF_SupplyReady */
return USBPD_ERROR;
/* USER CODE END USBPD_PWR_IF_SupplyReady */
}
/**
* @brief Enables VBUS power on a specified port
* @param PortNum Port number
* @retval USBPD status
*/
USBPD_StatusTypeDef USBPD_PWR_IF_VBUSEnable(uint8_t PortNum)
{
/* USER CODE BEGIN USBPD_PWR_IF_VBUSEnable */
return USBPD_ERROR;
/* USER CODE END USBPD_PWR_IF_VBUSEnable */
}
/**
* @brief Disbale VBUS/VCONN the power on a specified port
* @param PortNum Port number
* @retval USBPD status
*/
USBPD_StatusTypeDef USBPD_PWR_IF_VBUSDisable(uint8_t PortNum)
{
/* USER CODE BEGIN USBPD_PWR_IF_VBUSDisable */
return USBPD_ERROR;
/* USER CODE END USBPD_PWR_IF_VBUSDisable */
}
/**
* @brief Disable the SNK to stop current consumption
* @param PortNum Port number
* @retval USBPD status
*/
USBPD_StatusTypeDef USBPD_PWR_IF_SNKDisable(uint8_t PortNum)
{
/* USER CODE BEGIN USBPD_PWR_IF_SNKDisable */
return USBPD_ERROR;
/* USER CODE END USBPD_PWR_IF_SNKDisable */
}
/**
* @brief Initialize power on a specified port
* @param PortNum Port number
* @retval USBPD status
*/
USBPD_StatusTypeDef USBPD_PWR_IF_InitPower(uint8_t PortNum)
{
return USBPD_OK;
}
/**
* @brief Checks if the power on a specified port is enabled
* @param PortNum Port number
* @retval USBPD_ENABLE or USBPD_DISABLE
*/
USBPD_FunctionalState USBPD_PWR_IF_VBUSIsEnabled(uint8_t PortNum)
{
/* Get the Status of the port */
return USBPD_PORT_IsValid(PortNum) ? (USBPD_FunctionalState)HW_IF_PWR_VBUSIsEnabled(PortNum) : USBPD_DISABLE;
}
/**
* @brief Reads the voltage and the current on a specified port
* @param PortNum Port number
* @param pVoltage: The Voltage in mV
* @param pCurrent: The Current in mA
* @retval USBPD_ERROR or USBPD_OK
*/
USBPD_StatusTypeDef USBPD_PWR_IF_ReadVA(uint8_t PortNum, uint16_t *pVoltage, uint16_t *pCurrent)
{
/* USER CODE BEGIN USBPD_PWR_IF_ReadVA */
return USBPD_ERROR;
/* USER CODE END USBPD_PWR_IF_ReadVA */
}
/**
* @brief Enables the VConn on the port.
* @param PortNum Port number
* @param CC Specifies the CCx to be selected based on @ref CCxPin_TypeDef structure
* @retval USBPD status
*/
USBPD_StatusTypeDef USBPD_PWR_IF_Enable_VConn(uint8_t PortNum, CCxPin_TypeDef CC)
{
/* USER CODE BEGIN USBPD_PWR_IF_Enable_VConn */
return USBPD_ERROR;
/* USER CODE END USBPD_PWR_IF_Enable_VConn */
}
/**
* @brief Disable the VConn on the port.
* @param PortNum Port number
* @param CC Specifies the CCx to be selected based on @ref CCxPin_TypeDef structure
* @retval USBPD status
*/
USBPD_StatusTypeDef USBPD_PWR_IF_Disable_VConn(uint8_t PortNum, CCxPin_TypeDef CC)
{
/* USER CODE BEGIN USBPD_PWR_IF_Disable_VConn */
return USBPD_ERROR;
/* USER CODE END USBPD_PWR_IF_Disable_VConn */
}
/**
* @brief Allow PDO data reading from PWR_IF storage.
* @param PortNum Port number
* @param DataId Type of data to be read from PWR_IF
* This parameter can be one of the following values:
* @arg @ref USBPD_CORE_DATATYPE_SRC_PDO Source PDO reading requested
* @arg @ref USBPD_CORE_DATATYPE_SNK_PDO Sink PDO reading requested
* @param Ptr Pointer on address where PDO values should be written (u8 pointer)
* @param Size Pointer on nb of u32 written by PWR_IF (nb of PDOs)
* @retval None
*/
void USBPD_PWR_IF_GetPortPDOs(uint8_t PortNum, USBPD_CORE_DataInfoType_TypeDef DataId, uint8_t *Ptr, uint32_t *Size)
{
/* USER CODE BEGIN USBPD_PWR_IF_GetPortPDOs */
/* USER CODE END USBPD_PWR_IF_GetPortPDOs */
}
/**
* @brief Find out SRC PDO pointed out by a position provided in a Request DO (from Sink).
* @param PortNum Port number
* @param RdoPosition RDO Position in list of provided PDO
* @param Pdo Pointer on PDO value pointed out by RDO position (u32 pointer)
* @retval Status of search
* USBPD_OK : Src PDO found for requested DO position (output Pdo parameter is set)
* USBPD_FAIL : Position is not compliant with current Src PDO for this port (no corresponding PDO value)
*/
USBPD_StatusTypeDef USBPD_PWR_IF_SearchRequestedPDO(uint8_t PortNum, uint32_t RdoPosition, uint32_t *Pdo)
{
/* USER CODE BEGIN USBPD_PWR_IF_SearchRequestedPDO */
return USBPD_FAIL;
/* USER CODE END USBPD_PWR_IF_SearchRequestedPDO */
}
/**
* @brief the function is called in case of critical issue is detected to switch in safety mode.
* @retval None
*/
void USBPD_PWR_IF_Alarm()
{
/* USER CODE BEGIN USBPD_PWR_IF_Alarm */
/* USER CODE END USBPD_PWR_IF_Alarm */
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

View file

@ -0,0 +1,457 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file usbpd_pwr_user.c
* @author MCD Application Team
* @brief USBPD PWR user code
******************************************************************************
* @attention
*
* <h2><center>&copy; 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 */
/* Includes ------------------------------------------------------------------*/
#include "usbpd_pwr_user.h"
#include "stm32g4xx_hal.h"
/* USER CODE BEGIN include */
/* USER CODE END include */
/** @addtogroup BSP
* @{
*/
/** @addtogroup POWER
* @{
*/
/** @defgroup POWER_Private_Typedef Private Typedef
* @{
*/
/* USER CODE BEGIN POWER_Private_Typedef */
/* USER CODE END POWER_Private_Typedef */
/**
* @}
*/
/** @defgroup POWER_Private_Constants Private Constants
* @{
*/
/* USER CODE BEGIN POWER_Private_Constants */
/* USER CODE END POWER_Private_Constants */
/**
* @}
*/
/** @defgroup POWER_Private_Macros Private Macros
* @{
*/
/* USER CODE BEGIN POWER_Private_Macros */
/* USER CODE END POWER_Private_Macros */
/**
* @}
*/
/** @defgroup POWER_Private_Variables Private Variables
* @{
*/
/* USER CODE BEGIN POWER_Private_Variables */
/* USER CODE END POWER_Private_Variables */
/**
* @}
*/
/** @defgroup POWER_Private_Functions Private Functions
* @{
*/
/* USER CODE BEGIN POWER_Private_Prototypes */
/* USER CODE END POWER_Private_Prototypes */
/**
* @}
*/
/** @defgroup POWER_Exported_Variables Exported Variables
* @{
*/
/* USER CODE BEGIN POWER_Exported_Variables */
/* USER CODE END POWER_Exported_Variables */
/**
* @}
*/
/** @addtogroup POWER_Exported_Functions
* @{
*/
/**
* @brief Initialize the hardware resources used by the Type-C power delivery (PD)
* controller.
* @param PortId Type-C port identifier
* This parameter can be take one of the following values:
* @arg TYPE_C_PORT_1
* @arg TYPE_C_PORT_2
* @retval PD controller status
*/
PWR_StatusTypeDef BSP_PWR_VBUSInit(uint32_t PortId)
{
/* USER CODE BEGIN BSP_PWR_VBUSInit */
return PWR_ERROR;
/* USER CODE END BSP_PWR_VBUSInit */
}
/**
* @brief Release the hardware resources used by the Type-C power delivery (PD)
* controller.
* @param PortId Type-C port identifier
* This parameter can be take one of the following values:
* @arg TYPE_C_PORT_1
* @arg TYPE_C_PORT_2
* @retval PD controller status
*/
PWR_StatusTypeDef BSP_PWR_VBUSDeInit(uint32_t PortId)
{
/* USER CODE BEGIN BSP_PWR_VBUSDeInit */
return PWR_ERROR;
/* USER CODE END BSP_PWR_VBUSDeInit */
}
/**
* @brief Enable power supply over VBUS.
* @param PortId Type-C port identifier
* This parameter can be take one of the following values:
* @arg TYPE_C_PORT_1
* @retval PD controller status
*/
PWR_StatusTypeDef BSP_PWR_VBUSOn(uint32_t PortId)
{
/* USER CODE BEGIN BSP_PWR_VBUSOn */
return PWR_ERROR;
/* USER CODE END BSP_PWR_VBUSOn */
}
/**
* @brief Disable power supply over VBUS.
* @param PortId Type-C port identifier
* This parameter can be take one of the following values:
* @arg TYPE_C_PORT_1
* @retval PD controller status
*/
PWR_StatusTypeDef BSP_PWR_VBUSOff(uint32_t PortId)
{
/* USER CODE BEGIN BSP_PWR_VBUSOff */
return PWR_ERROR;
/* USER CODE END BSP_PWR_VBUSOff */
}
/**
* @brief Set a fixed/variable PDO and manage the power control.
* @param PortId Type-C port identifier
* This parameter can be take one of the following values:
* @arg TYPE_C_PORT_1
* @param VbusTargetInmv the vbus Target (in mV)
* @param OperatingCurrent the Operating Current (in mA)
* @param MaxOperatingCurrent the Max Operating Current (in mA)
* @retval PD controller status
*/
PWR_StatusTypeDef BSP_PWR_VBUSSetVoltage_Fixed(uint32_t PortId,
uint32_t VbusTargetInmv,
uint32_t OperatingCurrent,
uint32_t MaxOperatingCurrent)
{
/* USER CODE BEGIN BSP_PWR_VBUSSetVoltage_Fixed */
return PWR_ERROR;
/* USER CODE END BSP_PWR_VBUSSetVoltage_Fixed */
}
/**
* @brief Set a fixed/variable PDO and manage the power control.
* @param PortId Type-C port identifier
* This parameter can be take one of the following values:
* @arg TYPE_C_PORT_1
* @param VbusTargetMinInmv the vbus Target min (in mV)
* @param VbusTargetMaxInmv the vbus Target max (in mV)
* @param OperatingCurrent the Operating Current (in mA)
* @param MaxOperatingCurrent the Max Operating Current (in mA)
* @retval PD controller status
*/
PWR_StatusTypeDef BSP_PWR_VBUSSetVoltage_Variable(uint32_t PortId,
uint32_t VbusTargetMinInmv,
uint32_t VbusTargetMaxInmv,
uint32_t OperatingCurrent,
uint32_t MaxOperatingCurrent)
{
/* USER CODE BEGIN BSP_PWR_VBUSSetVoltage_Variable */
return PWR_ERROR;
/* USER CODE BEGIN BSP_PWR_VBUSSetVoltage_Variable */
}
/**
* @brief Set a Battery PDO and manage the power control.
* @param PortId Type-C port identifier
* This parameter can be take one of the following values:
* @arg TYPE_C_PORT_1
* @param VbusTargetMin the vbus Target min (in mV)
* @param VbusTargetMax the vbus Target max (in mV)
* @param OperatingPower the Operating Power (in mW)
* @param MaxOperatingPower the Max Operating Power (in mW)
* @retval PD controller status
*/
PWR_StatusTypeDef BSP_PWR_VBUSSetVoltage_Battery(uint32_t PortId,
uint32_t VbusTargetMin,
uint32_t VbusTargetMax,
uint32_t OperatingPower,
uint32_t MaxOperatingPower)
{
/* USER CODE BEGIN BSP_PWR_VBUSSetVoltage_Battery */
return PWR_ERROR;
/* USER CODE BEGIN BSP_PWR_VBUSSetVoltage_Battery */
}
/**
* @brief Set a APDO and manage the power control.
* @param PortId Type-C port identifier
* This parameter can be take one of the following values:
* @arg TYPE_C_PORT_1
* @param VbusTargetInmv the vbus Target (in mV)
* @param OperatingCurrent the Operating current (in mA)
* @param Delta Delta between with previous APDO (in mV), 0 means APDO start
* @retval PD controller status
*/
PWR_StatusTypeDef BSP_PWR_VBUSSetVoltage_APDO(uint32_t PortId,
uint32_t VbusTargetInmv,
uint32_t OperatingCurrent,
int32_t Delta)
{
/* USER CODE BEGIN BSP_PWR_VBUSSetVoltage_APDO */
return PWR_ERROR;
/* USER CODE BEGIN BSP_PWR_VBUSSetVoltage_APDO */
}
/**
* @brief Get actual voltage level measured on the VBUS line.
* @param PortId Type-C port identifier
* This parameter can be take one of the following values:
* @arg TYPE_C_PORT_1
* @arg TYPE_C_PORT_2
* @retval Voltage measured voltage level (in mV)
*/
uint32_t BSP_PWR_VBUSGetVoltage(uint32_t PortId)
{
/* USER CODE BEGIN BSP_PWR_VBUSGetVoltage */
return 0;
/* USER CODE END BSP_PWR_VBUSGetVoltage */
}
/**
* @brief Get actual current level measured on the VBUS line.
* @param PortId Type-C port identifier
* This parameter can be take one of the following values:
* @arg TYPE_C_PORT_1
* @arg TYPE_C_PORT_2
* @retval Current measured current level (in mA)
*/
int32_t BSP_PWR_VBUSGetCurrent(uint32_t PortId)
{
/* USER CODE BEGIN BSP_PWR_VBUSGetCurrent */
return 0;
/* USER CODE END BSP_PWR_VBUSGetCurrent */
}
/**
* @brief Initialize VCONN sourcing.
* @param PortId Type-C port identifier
* This parameter can be take one of the following values:
* @arg TYPE_C_PORT_1
* @arg TYPE_C_PORT_2
* @param CCPinId Type-C CC pin identifier
* This parameter can be take one of the following values:
* @arg TYPE_C_CC1
* @arg TYPE_C_CC2
* @retval PD controller status
*/
PWR_StatusTypeDef BSP_PWR_VCONNInit(uint32_t PortId,
uint32_t CCPinId)
{
/* USER CODE BEGIN BSP_PWR_VCONNInit */
return PWR_ERROR;
/* USER CODE END BSP_PWR_VCONNInit */
}
/**
* @brief Un-Initialize VCONN sourcing.
* @param PortId Type-C port identifier
* This parameter can be take one of the following values:
* @arg TYPE_C_PORT_1
* @arg TYPE_C_PORT_2
* @param CCPinId Type-C CC pin identifier
* This parameter can be take one of the following values:
* @arg TYPE_C_CC1
* @arg TYPE_C_CC2
* @retval PD controller status
*/
PWR_StatusTypeDef BSP_PWR_VCONNDeInit(uint32_t PortId,
uint32_t CCPinId)
{
/* USER CODE BEGIN BSP_PWR_VCONNDeInit */
return PWR_ERROR;
/* USER CODE END BSP_PWR_VCONNDeInit */
}
/**
* @brief Enable VCONN sourcing.
* @param PortId Type-C port identifier
* This parameter can be take one of the following values:
* @arg TYPE_C_PORT_1
* @arg TYPE_C_PORT_2
* @param CCPinId Type-C CC pin identifier
* This parameter can be take one of the following values:
* @arg TYPE_C_CC1
* @arg TYPE_C_CC2
* @retval PD controller status
*/
PWR_StatusTypeDef BSP_PWR_VCONNOn(uint32_t PortId,
uint32_t CCPinId)
{
/* USER CODE BEGIN BSP_PWR_VCONNOn */
return PWR_ERROR;
/* USER CODE END BSP_PWR_VCONNOn */
}
/**
* @brief Disable VCONN sourcing.
* @param PortId Type-C port identifier
* This parameter can be take one of the following values:
* @arg TYPE_C_PORT_1
* @arg TYPE_C_PORT_2
* @param CCPinId CC pin identifier
* This parameter can be take one of the following values:
* @arg TYPE_C_CC1
* @arg TYPE_C_CC2
* @retval PD controller status
*/
PWR_StatusTypeDef BSP_PWR_VCONNOff(uint32_t PortId,
uint32_t CCPinId)
{
/* USER CODE BEGIN BSP_PWR_VCONNOff */
return PWR_ERROR;
/* USER CODE END BSP_PWR_VCONNOff */
}
/**
* @brief Set the VBUS disconnection voltage threshold.
* @note Callback funtion registered through BSP_PWR_RegisterVBUSDetectCallback
* function call is invoked when VBUS falls below programmed threshold.
* @note By default VBUS disconnection threshold is set to 3.3V
* @param PortId Type-C port identifier
* This parameter can be take one of the following values:
* @arg TYPE_C_PORT_1
* @arg TYPE_C_PORT_2
* @param VoltageThreshold: VBUS disconnection voltage threshold (in mV)
* @retval PD controller status
*/
void BSP_PWR_SetVBUSDisconnectionThreshold(uint32_t PortId,
uint32_t VoltageThreshold)
{
/* USER CODE BEGIN BSP_PWR_SetVBUSDisconnectionThreshold */
/* USER CODE END BSP_PWR_SetVBUSDisconnectionThreshold */
}
/**
* @brief Register USB Type-C Current callback function.
* @note Callback function invoked when VBUS rises above 4V (VBUS present) or
* when VBUS falls below programmed threshold (VBUS absent).
* @note Callback funtion is un-registered when callback function pointer
* argument is NULL.
* @param PortId Type-C port identifier
* This parameter can be take one of the following values:
* @arg TYPE_C_PORT_1
* @arg TYPE_C_PORT_2
* @param pfnVBUSDetectCallback callback function pointer
* @retval 0 success else fail
*/
PWR_StatusTypeDef BSP_PWR_RegisterVBUSDetectCallback(uint32_t PortId,
PWR_VBUSDetectCallbackFunc * pfnVBUSDetectCallback)
{
/* USER CODE BEGIN BSP_PWR_RegisterVBUSDetectCallback */
return PWR_ERROR;
/* USER CODE END BSP_PWR_RegisterVBUSDetectCallback */
}
/**
* @brief Get actual VBUS status.
* @param PortId Type-C port identifier
* This parameter can be take one of the following values:
* @arg TYPE_C_PORT_1
* @retval VBUS status (1: On, 0: Off)
*/
uint8_t BSP_PWR_VBUSIsOn(uint32_t PortId)
{
/* USER CODE BEGIN BSP_PWR_VBUSIsOn */
return 0;
/* USER CODE END BSP_PWR_VBUSIsOn */
}
/**
* @brief Get actual VCONN status.
* @param PortId Type-C port identifier
* This parameter can be take one of the following values:
* @arg TYPE_C_PORT_1
* @param CCPinId Type-C CC pin identifier
* This parameter can be take one of the following values:
* @arg TYPE_C_CC1
* @arg TYPE_C_CC2
* @retval VCONN status (1: On, 0: Off)
*/
uint8_t BSP_PWR_VCONNIsOn(uint32_t PortId,
uint32_t CCPinId)
{
/* USER CODE BEGIN BSP_PWR_VCONNIsOn */
return 0;
/* USER CODE END BSP_PWR_VCONNIsOn */
}
/**
* @}
*/
/** @addtogroup POWER_Private_Functions
* @{
*/
/* USER CODE BEGIN POWER_Private_Functions */
/* USER CODE END POWER_Private_Functions */
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

View file

@ -0,0 +1,470 @@
/**
******************************************************************************
* @file usbpd_vdm_user.c
* @author MCD Application Team
* @brief USBPD provider demo file
******************************************************************************
* @attention
*
* <h2><center>&copy; 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
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "usbpd_core.h"
#include "usbpd_dpm_conf.h"
#include "usbpd_vdm_user.h"
#include "usbpd_dpm_user.h"
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/** @addtogroup STM32_USBPD_APPLICATION
* @{
*/
/** @addtogroup STM32_USBPD_APPLICATION_VDM_USER
* @{
*/
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN Private_define */
/* USER CODE END Private_define */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN Private_typedef */
/* USER CODE END Private_typedef */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN Private_macro */
/* USER CODE END Private_macro */
/* Private function prototypes -----------------------------------------------*/
static USBPD_StatusTypeDef USBPD_VDM_DiscoverIdentity(uint8_t PortNum, USBPD_DiscoveryIdentity_TypeDef *pIdentity);
static USBPD_StatusTypeDef USBPD_VDM_DiscoverSVIDs(uint8_t PortNum, uint16_t **p_SVID_Info, uint8_t *nb);
static USBPD_StatusTypeDef USBPD_VDM_DiscoverModes(uint8_t PortNum, uint16_t SVID, uint32_t **p_ModeInfo, uint8_t *nbMode);
static USBPD_StatusTypeDef USBPD_VDM_ModeEnter(uint8_t PortNum, uint16_t SVID, uint32_t ModeIndex);
static USBPD_StatusTypeDef USBPD_VDM_ModeExit(uint8_t PortNum, uint16_t SVID, uint32_t ModeIndex);
static void USBPD_VDM_SendAttention(uint8_t PortNum, uint8_t *NbData, uint32_t *VDO);
static void USBPD_VDM_ReceiveAttention(uint8_t PortNum, uint8_t NbData, uint32_t VDO);
static USBPD_StatusTypeDef USBPD_VDM_ReceiveSpecific(uint8_t PortNum, USBPD_VDM_Command_Typedef VDMCommand, uint8_t *pNbData, uint32_t *pVDO);
static void USBPD_VDM_InformIdentity(uint8_t PortNum, USBPD_SOPType_TypeDef SOPType, USBPD_VDM_CommandType_Typedef CommandStatus, USBPD_DiscoveryIdentity_TypeDef *pIdentity);
static void USBPD_VDM_InformSVID(uint8_t PortNum, USBPD_SOPType_TypeDef SOPType, USBPD_VDM_CommandType_Typedef CommandStatus, USBPD_SVIDInfo_TypeDef *pListSVID);
static void USBPD_VDM_InformMode(uint8_t PortNum, USBPD_SOPType_TypeDef SOPType, USBPD_VDM_CommandType_Typedef CommandStatus, USBPD_ModeInfo_TypeDef *pModesInfo);
static void USBPD_VDM_InformModeEnter(uint8_t PortNum, USBPD_SOPType_TypeDef SOPType, USBPD_VDM_CommandType_Typedef CommandStatus, uint16_t SVID, uint32_t ModeIndex);
static void USBPD_VDM_InformModeExit(uint8_t PortNum, USBPD_SOPType_TypeDef SOPType, USBPD_VDM_CommandType_Typedef CommandStatus, uint16_t SVID, uint32_t ModeIndex);
static void USBPD_VDM_InformSpecific(uint8_t PortNum, USBPD_VDM_Command_Typedef VDMCommand, uint8_t *NbData, uint32_t *VDO);
static void USBPD_VDM_SendSpecific(uint8_t PortNum, USBPD_VDM_Command_Typedef VDMCommand, uint8_t *NbData, uint32_t *VDO);
static void USBPD_VDM_SendUVDM(uint8_t PortNum, USBPD_UVDMHeader_TypeDef *pUVDM_Header, uint8_t *pNbData, uint32_t *pVDO);
static USBPD_StatusTypeDef USBPD_VDM_ReceiveUVDM(uint8_t PortNum, USBPD_UVDMHeader_TypeDef UVDM_Header, uint8_t *pNbData, uint32_t *pVDO);
/* USER CODE BEGIN Private_prototypes */
/* USER CODE END Private_prototypes */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Private_variables */
const USBPD_VDM_Callbacks vdmCallbacks =
{
USBPD_VDM_DiscoverIdentity,
USBPD_VDM_DiscoverSVIDs,
USBPD_VDM_DiscoverModes,
USBPD_VDM_ModeEnter,
USBPD_VDM_ModeExit,
USBPD_VDM_InformIdentity,
USBPD_VDM_InformSVID,
USBPD_VDM_InformMode,
USBPD_VDM_InformModeEnter,
USBPD_VDM_InformModeExit,
USBPD_VDM_SendAttention,
USBPD_VDM_ReceiveAttention,
USBPD_VDM_SendSpecific,
USBPD_VDM_ReceiveSpecific,
USBPD_VDM_InformSpecific,
USBPD_VDM_SendUVDM,
USBPD_VDM_ReceiveUVDM,
};
/* USER CODE END Private_variables */
/* Private functions ---------------------------------------------------------*/
/**
* @brief VDM Discovery identity callback
* @note Function is called to get Discovery identity information linked to the device and answer
* to SVDM Discovery identity init message sent by port partner
* @param PortNum current port number
* @param pIdentity Pointer on @ref USBPD_DiscoveryIdentity_TypeDef structure
* @retval USBPD status: @ref USBPD_ACK or @ref USBPD_BUSY
*/
static USBPD_StatusTypeDef USBPD_VDM_DiscoverIdentity(uint8_t PortNum, USBPD_DiscoveryIdentity_TypeDef *pIdentity)
{
/* USER CODE BEGIN USBPD_VDM_DiscoverIdentity */
return USBPD_NAK;
/* USER CODE END USBPD_VDM_DiscoverIdentity */
}
/**
* @brief VDM Discover SVID callback
* @note Function is called to retrieve SVID supported by device and answer
* to SVDM Discovery SVID init message sent by port partner
* @param PortNum current port number
* @param p_SVID_Info Pointer on @ref USBPD_SVIDInfo_TypeDef structure
* @param pNbSVID Pointer on number of SVID
* @retval USBPD status @ref USBPD_BUSY or @ref USBPD_ACK or @ref USBPD_NAK
*/
static USBPD_StatusTypeDef USBPD_VDM_DiscoverSVIDs(uint8_t PortNum, uint16_t **p_SVID_Info, uint8_t *pNbSVID)
{
/* USER CODE BEGIN USBPD_VDM_DiscoverSVIDs */
return USBPD_NAK;
/* USER CODE END USBPD_VDM_DiscoverSVIDs */
}
/**
* @brief VDM Discover Mode callback (report all the modes supported by SVID)
* @note Function is called to report all the modes supported by selected SVID and answer
* to SVDM Discovery Mode init message sent by port partner
* @param PortNum current port number
* @param SVID SVID value
* @param p_ModeTab Pointer on the mode value
* @param NumberOfMode Number of mode available
* @retval USBPD status
*/
static USBPD_StatusTypeDef USBPD_VDM_DiscoverModes(uint8_t PortNum, uint16_t SVID, uint32_t **p_ModeTab, uint8_t *NumberOfMode)
{
/* USER CODE BEGIN USBPD_VDM_DiscoverModes */
return USBPD_NAK;
/* USER CODE END USBPD_VDM_DiscoverModes */
}
/**
* @brief VDM Mode enter callback
* @note Function is called to check if device can enter in the mode received for the selected SVID in the
* SVDM enter mode init message sent by port partner
* @param PortNum current port number
* @param SVID SVID value
* @param ModeIndex Index of the mode to be entered
* @retval USBPD status @ref USBPD_ACK/@ref USBPD_NAK
*/
static USBPD_StatusTypeDef USBPD_VDM_ModeEnter(uint8_t PortNum, uint16_t SVID, uint32_t ModeIndex)
{
/* USER CODE BEGIN USBPD_VDM_ModeEnter */
return USBPD_NAK;
/* USER CODE END USBPD_VDM_ModeEnter */
}
/**
* @brief VDM Mode exit callback
* @note Function is called to check if device can exit from the mode received for the selected SVID in the
* SVDM exit mode init message sent by port partner
* @param PortNum current port number
* @param SVID SVID value
* @param ModeIndex Index of the mode to be exited
* @retval USBPD status @ref USBPD_ACK/@ref USBPD_NAK
*/
static USBPD_StatusTypeDef USBPD_VDM_ModeExit(uint8_t PortNum, uint16_t SVID, uint32_t ModeIndex)
{
/* USER CODE BEGIN USBPD_VDM_ModeExit */
return USBPD_NAK;
/* USER CODE END USBPD_VDM_ModeExit */
}
/**
* @brief Send VDM Attention message callback
* @note Function is called when device wants to send a SVDM attention message to port partner
* (for instance DP status can be filled through this function)
* @param PortNum current port number
* @param pNbData Pointer of number of VDO to send
* @param pVDO Pointer of VDO to send
* @retval None
*/
static void USBPD_VDM_SendAttention(uint8_t PortNum, uint8_t *pNbData, uint32_t *pVDO)
{
/* USER CODE BEGIN USBPD_VDM_SendAttention */
/* USER CODE END USBPD_VDM_SendAttention */
}
/**
* @brief Receive VDM Attention callback
* @note Function is called when a SVDM attention init message has been received from port partner
* (for instance, save DP status data through this function)
* @param PortNum current port number
* @param NbData Number of received VDO
* @param VDO Received VDO
* @retval None
*/
static void USBPD_VDM_ReceiveAttention(uint8_t PortNum, uint8_t NbData, uint32_t VDO)
{
/* USER CODE BEGIN USBPD_VDM_ReceiveAttention */
/* USER CODE END USBPD_VDM_ReceiveAttention */
}
/**
* @brief VDM Receive Specific message callback
* @note Function is called to answer to a SVDM specific init message received by port partner.
* (for instance, retrieve DP status or DP configure data through this function)
* @param PortNum Current port number
* @param VDMCommand VDM command based on @ref USBPD_VDM_Command_Typedef
* @param pNbData Pointer of number of received VDO and used for the answer
* @param pVDO Pointer of received VDO and use for the answer
* @retval USBPD Status
*/
static USBPD_StatusTypeDef USBPD_VDM_ReceiveSpecific(uint8_t PortNum, USBPD_VDM_Command_Typedef VDMCommand, uint8_t *pNbData, uint32_t *pVDO)
{
/* USER CODE BEGIN USBPD_VDM_ReceiveSpecific */
return USBPD_NAK;
/* USER CODE END USBPD_VDM_ReceiveSpecific */
}
/**
* @brief Inform identity callback
* @note Function is called to save Identity information received in Discovery identity from port partner
(answer to SVDM discovery identity sent by device)
* @param PortNum current port number
* @param SOPType SOP type
* @param CommandStatus Command status based on @ref USBPD_VDM_CommandType_Typedef
* @param pIdentity Pointer on the discovery identity information based on @ref USBPD_DiscoveryIdentity_TypeDef
* @retval None
*/
static void USBPD_VDM_InformIdentity(uint8_t PortNum, USBPD_SOPType_TypeDef SOPType, USBPD_VDM_CommandType_Typedef CommandStatus, USBPD_DiscoveryIdentity_TypeDef *pIdentity)
{
/* USER CODE BEGIN USBPD_VDM_InformIdentity */
switch(CommandStatus)
{
case SVDM_RESPONDER_ACK :
break;
case SVDM_RESPONDER_NAK :
break;
case SVDM_RESPONDER_BUSY :
break;
default :
break;
}
/* USER CODE END USBPD_VDM_InformIdentity */
}
/**
* @brief Inform SVID callback
* @note Function is called to save list of SVID received in Discovery SVID from port partner
(answer to SVDM discovery SVID sent by device)
* @param PortNum current port number
* @param SOPType SOP type
* @param CommandStatus Command status based on @ref USBPD_VDM_CommandType_Typedef
* @param pListSVID Pointer of list of SVID based on @ref USBPD_SVIDInfo_TypeDef
* @retval None
*/
static void USBPD_VDM_InformSVID(uint8_t PortNum, USBPD_SOPType_TypeDef SOPType, USBPD_VDM_CommandType_Typedef CommandStatus, USBPD_SVIDInfo_TypeDef *pListSVID)
{
/* USER CODE BEGIN USBPD_VDM_InformSVID */
switch(CommandStatus)
{
case SVDM_RESPONDER_ACK :
break;
case SVDM_RESPONDER_NAK :
break;
case SVDM_RESPONDER_BUSY :
break;
default :
break;
}
/* USER CODE END USBPD_VDM_InformSVID */
}
/**
* @brief Inform Mode callback ( received in Discovery Modes ACK)
* @note Function is called to save list of modes linked to SVID received in Discovery mode from port partner
(answer to SVDM discovery mode sent by device)
* @param PortNum current port number
* @param SOPType SOP type
* @param CommandStatus Command status based on @ref USBPD_VDM_CommandType_Typedef
* @param pModesInfo Pointer of Modes info based on @ref USBPD_ModeInfo_TypeDef
* @retval None
*/
static void USBPD_VDM_InformMode(uint8_t PortNum, USBPD_SOPType_TypeDef SOPType, USBPD_VDM_CommandType_Typedef CommandStatus, USBPD_ModeInfo_TypeDef *pModesInfo)
{
/* USER CODE BEGIN USBPD_VDM_InformMode */
switch(CommandStatus)
{
case SVDM_RESPONDER_ACK :
break;
case SVDM_RESPONDER_NAK :
break;
case SVDM_RESPONDER_BUSY :
break;
default :
break;
}
/* USER CODE END USBPD_VDM_InformMode */
}
/**
* @brief Inform Mode enter callback
* @note Function is called to inform if port partner accepted or not to enter in the mode
* specified in the SVDM enter mode sent by the device
* @param PortNum current port number
* @param SOPType SOP type
* @param CommandStatus Command status based on @ref USBPD_VDM_CommandType_Typedef
* @param SVID SVID ID
* @param ModeIndex Index of the mode to be entered
* @retval None
*/
static void USBPD_VDM_InformModeEnter(uint8_t PortNum, USBPD_SOPType_TypeDef SOPType, USBPD_VDM_CommandType_Typedef CommandStatus, uint16_t SVID, uint32_t ModeIndex)
{
/* USER CODE BEGIN USBPD_VDM_InformModeEnter */
switch(CommandStatus)
{
case SVDM_RESPONDER_ACK :
break;
case SVDM_RESPONDER_NAK :
break;
case SVDM_RESPONDER_BUSY :
/* retry in 50ms */
break;
default :
break;
}
/* USER CODE END USBPD_VDM_InformModeEnter */
}
/**
* @brief Inform Exit enter callback
* @param PortNum current port number
* @param SVID SVID ID
* @param ModeIndex Index of the mode to be entered
* @retval None
*/
static void USBPD_VDM_InformModeExit(uint8_t PortNum, USBPD_SOPType_TypeDef SOPType, USBPD_VDM_CommandType_Typedef CommandStatus, uint16_t SVID, uint32_t ModeIndex)
{
/* USER CODE BEGIN USBPD_VDM_InformModeExit */
switch(CommandStatus)
{
case SVDM_RESPONDER_ACK :
break;
case SVDM_RESPONDER_NAK :
break;
case SVDM_RESPONDER_BUSY :
/* retry in 50ms */
break;
default :
break;
}
/* USER CODE END USBPD_VDM_InformModeExit */
}
/**
* @brief VDM Send Specific message callback
* @note Function is called when device wants to send a SVDM specific init message to port partner
* (for instance DP status or DP configure can be filled through this function)
* @param PortNum current port number
* @param VDMCommand VDM command based on @ref USBPD_VDM_Command_Typedef
* @param pNbData Pointer of number of VDO to send
* @param pVDO Pointer of VDO to send
* @retval None
*/
static void USBPD_VDM_SendSpecific(uint8_t PortNum, USBPD_VDM_Command_Typedef VDMCommand, uint8_t *pNbData, uint32_t *pVDO)
{
/* USER CODE BEGIN USBPD_VDM_SendSpecific */
/* USER CODE END USBPD_VDM_SendSpecific */
}
/**
* @brief VDM Specific message callback to inform user of reception of VDM specific message
* @note Function is called when answer from SVDM specific init message has been received by the device
* (for instance, save DP status and DP configure data through this function)
* @param PortNum current port number
* @param VDMCommand VDM command based on @ref USBPD_VDM_Command_Typedef
* @param pNbData Pointer of number of received VDO
* @param pVDO Pointer of received VDO
* @retval None
*/
static void USBPD_VDM_InformSpecific(uint8_t PortNum, USBPD_VDM_Command_Typedef VDMCommand, uint8_t *pNbData, uint32_t *pVDO)
{
/* USER CODE BEGIN USBPD_VDM_InformSpecific */
/* USER CODE END USBPD_VDM_InformSpecific */
}
/**
* @brief VDM Send Unstructured message callback
* @param PortNum current port number
* @param pUVDM_Header Pointer on UVDM header based on @ref USBPD_UVDMHeader_TypeDef
* @param pNbData Pointer of number of VDO to send
* @param pVDO Pointer of VDO to send
* @retval None
*/
static void USBPD_VDM_SendUVDM(uint8_t PortNum, USBPD_UVDMHeader_TypeDef *pUVDM_Header, uint8_t *pNbData, uint32_t *pVDO)
{
/* USER CODE BEGIN USBPD_VDM_SendUVDM */
/* USER CODE END USBPD_VDM_SendUVDM */
}
/**
* @brief Unstructured VDM message callback to inform user of reception of UVDM message
* @param PortNum current port number
* @param UVDM_Header UVDM header based on @ref USBPD_UVDMHeader_TypeDef
* @param pNbData Pointer of number of received VDO
* @param pVDO Pointer of received VDO
* @retval USBPD Status
*/
static USBPD_StatusTypeDef USBPD_VDM_ReceiveUVDM(uint8_t PortNum, USBPD_UVDMHeader_TypeDef UVDM_Header, uint8_t *pNbData, uint32_t *pVDO)
{
/* USER CODE BEGIN USBPD_VDM_ReceiveUVDM */
return USBPD_ERROR;
/* USER CODE END USBPD_VDM_ReceiveUVDM */
}
/* USER CODE BEGIN Private_functions */
/* USER CODE END Private_functions */
/* Exported functions ---------------------------------------------------------*/
/**
* @brief VDM Initialization function
* @param PortNum Index of current used port
* @retval status
*/
USBPD_StatusTypeDef USBPD_VDM_UserInit(uint8_t PortNum)
{
/* USER CODE BEGIN USBPD_VDM_UserInit */
return USBPD_OK;
/* USER CODE END USBPD_VDM_UserInit */
}
/**
* @brief VDM Reset function
* @param PortNum Index of current used port
* @retval status
*/
void USBPD_VDM_UserReset(uint8_t PortNum)
{
/* USER CODE BEGIN USBPD_VDM_UserReset */
/* USER CODE END USBPD_VDM_UserReset */
}
/* USER CODE BEGIN Exported_functions */
/* USER CODE END Exported_functions */
/**
* @}
*/
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/