### Initialize HAL Library and System Clock Source: https://context7.com/stmicroelectronics/stm32g4xx-hal-driver/llms.txt Call HAL_Init() at the start of your application before using any HAL functions. This example also shows system clock configuration and peripheral initialization. ```c #include "stm32g4xx_hal.h" int main(void) { /* Initialize the HAL Library */ HAL_StatusTypeDef status = HAL_Init(); if (status != HAL_OK) { /* Initialization Error */ while (1); } /* Configure the system clock */ SystemClock_Config(); /* Initialize peripherals */ MX_GPIO_Init(); MX_USART2_UART_Init(); while (1) { /* Main application loop */ } } ``` ```c /* SysTick Handler - must be called from SysTick_IRQHandler */ void HAL_IncTick(void) { uwTick += uwTickFreq; /* Increment the tick counter */ } ``` ```c /* Get current tick value in milliseconds */ uint32_t current_tick = HAL_GetTick(); ``` ```c /* Blocking delay in milliseconds */ HAL_Delay(1000); /* Delay for 1 second */ ``` ```c /* Get HAL version */ uint32_t hal_version = HAL_GetHalVersion(); /* Format: 0xXYZR (X=major, Y=minor, Z=sub, R=release) */ ``` ```c /* Get device identifiers */ uint32_t revision_id = HAL_GetREVID(); uint32_t device_id = HAL_GetDEVID(); ``` -------------------------------- ### Calibrate and Start ADC Conversion (HAL_ADCEx_Calibration_Start, HAL_ADC_Start) Source: https://context7.com/stmicroelectronics/stm32g4xx-hal-driver/llms.txt Performs ADC calibration before the first conversion and then starts a single conversion using polling. The result is retrieved using HAL_ADC_GetValue. Ensure the ADC is stopped afterwards with HAL_ADC_Stop. ```c /* ADC Calibration (recommended before first conversion) */ HAL_ADCEx_Calibration_Start(&hadc1, ADC_SINGLE_ENDED); /* Single conversion (polling) */ HAL_ADC_Start(&hadc1); if (HAL_ADC_PollForConversion(&hadc1, 100) == HAL_OK) { uint32_t adc_value = HAL_ADC_GetValue(&hadc1); float voltage = (float)adc_value * 3.3f / 4096.0f; /* 12-bit, 3.3V reference */ } HAL_ADC_Stop(&hadc1); ``` -------------------------------- ### Start DMA for Continuous ADC Sampling (HAL_ADC_Start_DMA) Source: https://context7.com/stmicroelectronics/stm32g4xx-hal-driver/llms.txt Configures and starts DMA for continuous ADC sampling, transferring conversion results to a specified buffer. The buffer size should match the number of samples to be acquired. ```c /* DMA mode for continuous sampling */ uint32_t adc_buffer[100]; HAL_ADC_Start_DMA(&hadc1, adc_buffer, 100); ``` -------------------------------- ### Configure Timer for Input Capture Source: https://context7.com/stmicroelectronics/stm32g4xx-hal-driver/llms.txt Sets up TIM3 Channel 2 for input capture to measure frequency. Ensure HAL_TIM_IC_ConfigChannel returns HAL_OK and start the capture. ```c /* Input Capture for frequency measurement */ TIM_IC_InitTypeDef sConfigIC = {0}; sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_RISING; sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI; sConfigIC.ICPrescaler = TIM_ICPSC_DIV1; sConfigIC.ICFilter = 0; HAL_TIM_IC_ConfigChannel(&htim3, &sConfigIC, TIM_CHANNEL_2); HAL_TIM_IC_Start_IT(&htim3, TIM_CHANNEL_2); ``` -------------------------------- ### Configure Timer for Encoder Mode Source: https://context7.com/stmicroelectronics/stm32g4xx-hal-driver/llms.txt Initializes TIM3 for encoder mode (TI12) and starts the encoder interface. Ensure HAL_TIM_Encoder_Init returns HAL_OK. ```c /* Encoder Mode */ TIM_Encoder_InitTypeDef sEncoderConfig = {0}; sEncoderConfig.EncoderMode = TIM_ENCODERMODE_TI12; sEncoderConfig.IC1Polarity = TIM_ICPOLARITY_RISING; sEncoderConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI; sEncoderConfig.IC1Prescaler = TIM_ICPSC_DIV1; sEncoderConfig.IC1Filter = 0; sEncoderConfig.IC2Polarity = TIM_ICPOLARITY_RISING; sEncoderConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI; sEncoderConfig.IC2Prescaler = TIM_ICPSC_DIV1; sEncoderConfig.IC2Filter = 0; HAL_TIM_Encoder_Init(&htim3, &sEncoderConfig); HAL_TIM_Encoder_Start(&htim3, TIM_CHANNEL_ALL); ``` -------------------------------- ### Start Continuous ADC Conversion with Interrupt (HAL_ADC_Start_IT) Source: https://context7.com/stmicroelectronics/stm32g4xx-hal-driver/llms.txt Initiates continuous ADC conversions where the HAL_ADC_ConvCpltCallback is invoked upon completion of each conversion. ```c /* Continuous conversion with interrupt */ HAL_ADC_Start_IT(&hadc1); ``` -------------------------------- ### Start UART Transmission with DMA Source: https://context7.com/stmicroelectronics/stm32g4xx-hal-driver/llms.txt Initiates a UART transmission using DMA. Ensure the DMA channel is linked to the UART peripheral and the data buffer is prepared. ```c /* Start UART transmission with DMA */ HAI_UART_Transmit_DMA(&huart2, tx_buffer, tx_size); ``` -------------------------------- ### ADC Conversion with LL Drivers Source: https://context7.com/stmicroelectronics/stm32g4xx-hal-driver/llms.txt Enables the ADC, starts a regular conversion, waits for End Of Conversion (EOC), and reads the 12-bit conversion data using LL functions. LL drivers are suitable for time-critical operations. ```c /* ADC using LL */ LL_ADC_Enable(ADC1); while (!LL_ADC_IsActiveFlag_ADRDY(ADC1)); LL_ADC_REG_StartConversion(ADC1); while (!LL_ADC_IsActiveFlag_EOC(ADC1)); uint32_t adc_value = LL_ADC_REG_ReadConversionData12(ADC1); ``` -------------------------------- ### Get TIM Channel State Source: https://github.com/stmicroelectronics/stm32g4xx-hal-driver/blob/master/Release_Notes.html Macros to get the current state of all channels or a specific channel of a TIM peripheral. Used for querying the operational status. ```c #define TIM_CHANNEL_STATE_GET(__HANDLE__) ... ``` ```c #define TIM_CHANNEL_N_STATE_GET(__HANDLE__) ... ``` -------------------------------- ### Check TIM Slave Instance Source: https://github.com/stmicroelectronics/stm32g4xx-hal-driver/blob/master/Release_Notes.html Macro to validate if a TIM instance can be used as a slave in synchronization configurations, checking the TIM_SMCR_MSM bit. Added to improve TIM synchronization setup. ```c IS_TIM_SLAVE_INSTANCE(TIMx) ``` -------------------------------- ### Perform DMA Memory-to-Memory Transfer (Interrupt) Source: https://context7.com/stmicroelectronics/stm32g4xx-hal-driver/llms.txt Initiates a memory-to-memory DMA transfer using interrupts. Register callback functions for transfer complete and error events before starting the transfer. ```c /* Interrupt mode transfer */ HAL_DMA_RegisterCallback(&hdma_memtomem, HAL_DMA_XFER_CPLT_CB_ID, DMA_TransferComplete); HAL_DMA_RegisterCallback(&hdma_memtomem, HAL_DMA_XFER_ERROR_CB_ID, DMA_TransferError); HAI_DMA_Start_IT(&hdma_memtomem, (uint32_t)src_buffer, (uint32_t)dst_buffer, 256); ``` -------------------------------- ### Initialize UART Peripheral (HAL_UART_Init) Source: https://context7.com/stmicroelectronics/stm32g4xx-hal-driver/llms.txt Initializes the UART peripheral with specified parameters. Ensure the corresponding peripheral and GPIO clocks are enabled in HAL_UART_MspInit. ```c #include "stm32g4xx_hal.h" UART_HandleTypeDef huart2; /* UART Initialization */ void MX_USART2_UART_Init(void) { huart2.Instance = USART2; huart2.Init.BaudRate = 115200; huart2.Init.WordLength = UART_WORDLENGTH_8B; huart2.Init.StopBits = UART_STOPBITS_1; huart2.Init.Parity = UART_PARITY_NONE; huart2.Init.Mode = UART_MODE_TX_RX; huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE; huart2.Init.OverSampling = UART_OVERSAMPLING_16; huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE; huart2.Init.ClockPrescaler = UART_PRESCALER_DIV1; if (HAL_UART_Init(&huart2) != HAL_OK) { Error_Handler(); } } ``` -------------------------------- ### Configure GPIO Pins for Input, Output, and Alternate Functions Source: https://context7.com/stmicroelectronics/stm32g4xx-hal-driver/llms.txt Use GPIO_InitTypeDef to configure pin mode, pull-up/down, speed, and alternate functions. Ensure the corresponding GPIO port clock is enabled before initialization. ```c #include "stm32g4xx_hal.h" /* GPIO Initialization Structure */ GPIO_InitTypeDef GPIO_InitStruct = {0}; /* Enable GPIO port clocks */ __HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE(); ``` ```c /* Configure PA5 as output push-pull (LED) */ GPIO_InitStruct.Pin = GPIO_PIN_5; 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); ``` ```c /* Configure PC13 as input with pull-up (Button) */ GPIO_InitStruct.Pin = GPIO_PIN_13; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_PULLUP; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); ``` ```c /* Configure PA9 for USART1_TX (alternate function) */ GPIO_InitStruct.Pin = GPIO_PIN_9; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; GPIO_InitStruct.Alternate = GPIO_AF7_USART1; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); ``` ```c /* Configure PB0 as external interrupt (rising edge) */ GPIO_InitStruct.Pin = GPIO_PIN_0; GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING; GPIO_InitStruct.Pull = GPIO_PULLDOWN; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); ``` ```c /* GPIO Operations */ HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET); /* Set pin high */ HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET); /* Set pin low */ HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5); /* Toggle pin */ ``` ```c GPIO_PinState state = HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13); /* Read pin state */ ``` ```c /* Lock GPIO configuration (prevents further changes until reset) */ HAL_GPIO_LockPin(GPIOA, GPIO_PIN_5); ``` ```c /* EXTI callback - called when interrupt triggers */ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { if (GPIO_Pin == GPIO_PIN_0) { /* Handle PB0 interrupt */ HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5); } } ``` -------------------------------- ### Get Encoder Counter Value Source: https://context7.com/stmicroelectronics/stm32g4xx-hal-driver/llms.txt Retrieves the current count from the timer configured in encoder mode. The value is cast to a signed 16-bit integer. ```c int32_t encoder_count = (int16_t)__HAL_TIM_GET_COUNTER(&htim3); ``` -------------------------------- ### Initialize Timer for PWM Generation Source: https://context7.com/stmicroelectronics/stm32g4xx-hal-driver/llms.txt Configures TIM3 for PWM generation on Channel 1. Sets up a 1 kHz PWM frequency with a 50% duty cycle initially. Ensure HAL_TIM_PWM_Init and HAL_TIM_PWM_ConfigChannel return HAL_OK. ```c /* PWM Configuration (TIM3 Channel 1) */ void MX_TIM3_PWM_Init(void) { TIM_OC_InitTypeDef sConfigOC = {0}; htim3.Instance = TIM3; htim3.Init.Prescaler = 170 - 1; /* 1 MHz timer clock */ htim3.Init.CounterMode = TIM_COUNTERMODE_UP; htim3.Init.Period = 1000 - 1; /* 1 kHz PWM frequency */ htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE; if (HAL_TIM_PWM_Init(&htim3) != HAL_OK) { Error_Handler(); } sConfigOC.OCMode = TIM_OCMODE_PWM1; sConfigOC.Pulse = 500; /* 50% duty cycle */ sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_1) != HAL_OK) { Error_Handler(); } } /* Start PWM output */ HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1); ``` -------------------------------- ### Initialize ADC1 (HAL_ADC_Init) Source: https://context7.com/stmicroelectronics/stm32g4xx-hal-driver/llms.txt Initializes the ADC1 peripheral with specified configurations for clock prescaler, resolution, data alignment, scan mode, and conversion modes. Ensure Error_Handler is defined for error management. ```c #include "stm32g4xx_hal.h" ADC_HandleTypeDef hadc1; /* ADC Initialization */ void MX_ADC1_Init(void) { ADC_ChannelConfTypeDef sConfig = {0}; hadc1.Instance = ADC1; hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV4; hadc1.Init.Resolution = ADC_RESOLUTION_12B; hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; hadc1.Init.GainCompensation = 0; hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE; hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV; hadc1.Init.LowPowerAutoWait = DISABLE; hadc1.Init.ContinuousConvMode = DISABLE; hadc1.Init.NbrOfConversion = 1; hadc1.Init.DiscontinuousConvMode = DISABLE; hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START; hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; hadc1.Init.DMAContinuousRequests = DISABLE; hadc1.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN; hadc1.Init.OversamplingMode = DISABLE; if (HAL_ADC_Init(&hadc1) != HAL_OK) { Error_Handler(); } /* Configure ADC channel (PA0 = ADC1_IN1) */ sConfig.Channel = ADC_CHANNEL_1; sConfig.Rank = ADC_REGULAR_RANK_1; sConfig.SamplingTime = ADC_SAMPLETIME_47CYCLES_5; sConfig.SingleDiff = ADC_SINGLE_ENDED; sConfig.OffsetNumber = ADC_OFFSET_NONE; sConfig.Offset = 0; if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) { Error_Handler(); } } ``` -------------------------------- ### Update HAL_HRTIM_DLLCalibrationStart() and HAL_HRTIM_DLLCalibrationStart_IT() Source: https://github.com/stmicroelectronics/stm32g4xx-hal-driver/blob/master/Release_Notes.html These APIs are updated by setting the HRTIM state to ready to avoid unstable behavior of TIMER E. This ensures proper DLL calibration. ```c HAL_HRTIM_DLLCalibrationStart(HRTIM_HandleTypeDef * hhrtim, HRTIM_Waveform_TypeDef *Waveform) ``` ```c HAL_HRTIM_DLLCalibrationStart_IT(HRTIM_HandleTypeDef * hhrtim, HRTIM_Waveform_TypeDef *Waveform) ``` -------------------------------- ### Configure GPIO Pins with LL Drivers Source: https://context7.com/stmicroelectronics/stm32g4xx-hal-driver/llms.txt Configures a GPIO pin for output with push-pull, low speed, and no pull-up/pull-down using LL functions. This is faster than HAL for time-critical operations. ```c /* GPIO Configuration using LL */ LL_GPIO_SetPinMode(GPIOA, LL_GPIO_PIN_5, LL_GPIO_MODE_OUTPUT); LL_GPIO_SetPinOutputType(GPIOA, LL_GPIO_PIN_5, LL_GPIO_OUTPUT_PUSHPULL); LL_GPIO_SetPinSpeed(GPIOA, LL_GPIO_PIN_5, LL_GPIO_SPEED_FREQ_LOW); LL_GPIO_SetPinPull(GPIOA, LL_GPIO_PIN_5, LL_GPIO_PULL_NO); ``` -------------------------------- ### Get TIM Update Interrupt Flag Copy Source: https://github.com/stmicroelectronics/stm32g4xx-hal-driver/blob/master/Release_Notes.html Use this macro to retrieve the status of the Update Interrupt Flag (UIF) copy. This is useful for checking the UIF status in specific scenarios. ```c #define __HAL_TIM_GET_UIFCPY(__HANDLE__) ... ``` -------------------------------- ### Initialize Timer Base for 1ms Interrupt Source: https://context7.com/stmicroelectronics/stm32g4xx-hal-driver/llms.txt Configures TIM2 for a 1ms interrupt period. Ensure HAL_TIM_Base_Init returns HAL_OK before proceeding. ```c #include "stm32g4xx_hal.h" TIM_HandleTypeDef htim2; TIM_HandleTypeDef htim3; /* Timer Base Initialization (1ms interrupt) */ void MX_TIM2_Init(void) { htim2.Instance = TIM2; htim2.Init.Prescaler = 170 - 1; /* 170 MHz / 170 = 1 MHz */ htim2.Init.CounterMode = TIM_COUNTERMODE_UP; htim2.Init.Period = 1000 - 1; /* 1 MHz / 1000 = 1 kHz (1ms) */ htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE; if (HAL_TIM_Base_Init(&htim2) != HAL_OK) { Error_Handler(); } } /* Start timer in interrupt mode */ HAL_TIM_Base_Start_IT(&htim2); ``` -------------------------------- ### Get System Clock Frequencies Source: https://context7.com/stmicroelectronics/stm32g4xx-hal-driver/llms.txt Retrieves the current frequencies of the system clock (SYSCLK), AHB clock (HCLK), APB1 clock (PCLK1), and APB2 clock (PCLK2). These functions are useful for debugging and configuration. ```c /* Get system clock frequencies */ uint32_t sysclk = HAL_RCC_GetSysClockFreq(); /* System clock frequency */ uint32_t hclk = HAL_RCC_GetHCLKFreq(); /* AHB clock frequency */ uint32_t pclk1 = HAL_RCC_GetPCLK1Freq(); /* APB1 clock frequency */ uint32_t pclk2 = HAL_RCC_GetPCLK2Freq(); /* APB2 clock frequency */ ``` -------------------------------- ### Initialize LL SPI for 8-bit Data Source: https://github.com/stmicroelectronics/stm32g4xx-hal-driver/blob/master/Release_Notes.html Use this function to initialize the SPI peripheral in LL mode, setting the FRXTH bit for proper 8-bit data transfer. Ensure SPI is configured. ```c LL_SPI_Init(SPIx, &SPI_InitStruct); ``` -------------------------------- ### MSP Initialization for UART (HAL_UART_MspInit) Source: https://context7.com/stmicroelectronics/stm32g4xx-hal-driver/llms.txt Configures the microcontroller specific peripherals (clocks and GPIOs) for UART communication. This function is called automatically by HAL_UART_Init. ```c /* MSP Initialization (called by HAL_UART_Init) */ void HAL_UART_MspInit(UART_HandleTypeDef *huart) { GPIO_InitTypeDef GPIO_InitStruct = {0}; if (huart->Instance == USART2) { __HAL_RCC_USART2_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE(); /* PA2 -> USART2_TX, PA3 -> USART2_RX */ GPIO_InitStruct.Pin = GPIO_PIN_2 | GPIO_PIN_3; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; GPIO_InitStruct.Alternate = GPIO_AF7_USART2; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /* Enable USART2 interrupt */ HAL_NVIC_SetPriority(USART2_IRQn, 0, 0); HAL_NVIC_EnableIRQ(USART2_IRQn); } } ``` -------------------------------- ### UART Extended Reception to IDLE Event (Interrupt) Source: https://context7.com/stmicroelectronics/stm32g4xx-hal-driver/llms.txt Starts UART reception that completes upon detecting an IDLE line event, allowing for variable-length data reception. This is useful for protocols where message length is not fixed. ```c /* Reception to IDLE event (variable length) */ HAL_UARTEx_ReceiveToIdle_IT(&huart2, rx_buffer, sizeof(rx_buffer)); ``` -------------------------------- ### Initialize DMA Memory-to-Memory Transfer Source: https://context7.com/stmicroelectronics/stm32g4xx-hal-driver/llms.txt Configures DMA controller for memory-to-memory transfers. Ensure DMA and DMAMUX clocks are enabled before initialization. ```c #include "stm32g4xx_hal.h" DMA_HandleTypeDef hdma_memtomem; /* DMA Memory-to-Memory Initialization */ void MX_DMA_Init(void) { __HAL_RCC_DMAMUX1_CLK_ENABLE(); __HAL_RCC_DMA1_CLK_ENABLE(); hdma_memtomem.Instance = DMA1_Channel1; hdma_memtomem.Init.Request = DMA_REQUEST_MEM2MEM; hdma_memtomem.Init.Direction = DMA_MEMORY_TO_MEMORY; hdma_memtomem.Init.PeriphInc = DMA_PINC_ENABLE; hdma_memtomem.Init.MemInc = DMA_MINC_ENABLE; hdma_memtomem.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD; hdma_memtomem.Init.MemDataAlignment = DMA_MDATAALIGN_WORD; hdma_memtomem.Init.Mode = DMA_NORMAL; hdma_memtomem.Init.Priority = DMA_PRIORITY_HIGH; if (HAL_DMA_Init(&hdma_memtomem) != HAL_OK) { Error_Handler(); } /* Configure DMA interrupt */ HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0); HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn); } ``` -------------------------------- ### Enable Peripheral Clocks with LL Drivers Source: https://context7.com/stmicroelectronics/stm32g4xx-hal-driver/llms.txt Enables clocks for AHB2 and APB1 peripherals using Low-Level (LL) driver functions. Ensure necessary includes are present. ```c #include "stm32g4xx_ll_gpio.h" #include "stm32g4xx_ll_usart.h" #include "stm32g4xx_ll_rcc.h" #include "stm32g4xx_ll_bus.h" /* Enable peripheral clocks using LL */ LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOA); LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_USART2); ``` -------------------------------- ### Add Lock and Unlock Handle Process in HAL_HRTIM_SimpleOCChannelConfig() Source: https://github.com/stmicroelectronics/stm32g4xx-hal-driver/blob/master/Release_Notes.html A lock and unlock handle process has been added to the HAL_HRTIM_SimpleOCChannelConfig() API. This ensures thread-safe access to the HRTIM handle during simple Output Compare channel configuration. ```c HAL_HRTIM_SimpleOCChannelConfig(HRTIM_HandleTypeDef * hhrtim, HRTIM_Waveform_TypeDef *Waveform, uint32_t Channel, HRTIM_SimpleOC_Config_TypeDef *SimpleOC_Config) ``` -------------------------------- ### Correct SAI SPDIF Formula Source: https://github.com/stmicroelectronics/stm32g4xx-hal-driver/blob/master/Release_Notes.html Corrected the formula used in HAL_SAI_Init for SPDIF mode to ensure accurate configuration. ```c HAL_SAI_Init(SAI_HandleTypeDef *hsai); ``` -------------------------------- ### HAL/LL SMARTCARD Updates Source: https://github.com/stmicroelectronics/stm32g4xx-hal-driver/blob/master/Release_Notes.html Fixes for SMARTCARD initialization and typos in state definitions. ```APIDOC ## HAL/LL SMARTCARD Update ### Description Fixed invalid initialization of SMARTCARD configuration by removing the FIFO mode configuration and fixed typos in SMARTCARD State definition description. ### Method N/A ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response #### Success Response (200) N/A #### Response Example N/A ### Fixes - Fixed invalid initialization of SMARTCARD configuration by removing the FIFO mode configuration. - Fixed typos in SMARTCARD State definition description. - Optimized stack usage for multiple APIs. ``` -------------------------------- ### IWDG Default Timeout Calculation (C) Source: https://github.com/stmicroelectronics/stm32g4xx-hal-driver/blob/master/Release_Notes.html The default IWDG timeout calculation (HAL_IWDG_DEFAULT_TIMEOUT) now includes the LSI startup time for more accurate timeout management. ```c HAL_IWDG_DEFAULT_TIMEOUT ``` -------------------------------- ### Enable SMBUS Wake Up Capability (C) Source: https://github.com/stmicroelectronics/stm32g4xx-hal-driver/blob/master/Release_Notes.html Use HAL_SMBUSEx_EnableWakeUp() to enable the wake-up capability for the SMBUS peripheral. Ensure proper initialization of the SMBUSEx peripheral before calling this function. ```c HAL_SMBUSEx_EnableWakeUp() ``` -------------------------------- ### Enable Peripheral Clocks Source: https://context7.com/stmicroelectronics/stm32g4xx-hal-driver/llms.txt Enables the clock for various peripherals. This must be done before using any peripheral. ```c /* Enable peripheral clocks */ __HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE(); __HAL_RCC_USART2_CLK_ENABLE(); __HAL_RCC_SPI1_CLK_ENABLE(); __HAL_RCC_I2C1_CLK_ENABLE(); __HAL_RCC_ADC12_CLK_ENABLE(); __HAL_RCC_TIM2_CLK_ENABLE(); __HAL_RCC_DMA1_CLK_ENABLE(); ``` -------------------------------- ### ADC Conversion Complete Callback (HAL_ADC_ConvCpltCallback) Source: https://context7.com/stmicroelectronics/stm32g4xx-hal-driver/llms.txt This callback is executed when a complete ADC conversion is finished. It is essential for processing the conversion results, especially in interrupt or DMA modes. Ensure the correct ADC instance is checked. ```c /* ADC Callbacks */ void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc) { if (hadc->Instance == ADC1) { uint32_t value = HAL_ADC_GetValue(hadc); /* Process conversion result */ } } ``` -------------------------------- ### Check I2C Device Ready Source: https://context7.com/stmicroelectronics/stm32g4xx-hal-driver/llms.txt Checks if a device on the I2C bus is ready and responsive. This is useful for verifying device presence before attempting communication. The EEPROM_ADDR should be the 7-bit address left-shifted by 1. ```c #define EEPROM_ADDR 0xA0 /* 7-bit address << 1 */ /* Check if device is ready */ HAL_StatusTypeDef status = HAL_I2C_IsDeviceReady(&hi2c1, EEPROM_ADDR, 3, 1000); if (status == HAL_OK) { /* Device is connected */ } ``` -------------------------------- ### Configure and Read Internal Temperature Sensor (ADC) Source: https://context7.com/stmicroelectronics/stm32g4xx-hal-driver/llms.txt Configures an ADC channel to read the internal temperature sensor, performs a single conversion, and retrieves the raw value. Further conversion to temperature requires calibration values from the datasheet. ```c /* Read internal temperature sensor */ ADC_ChannelConfTypeDef sConfig = {0}; sConfig.Channel = ADC_CHANNEL_TEMPSENSOR_ADC1; sConfig.Rank = ADC_REGULAR_RANK_1; sConfig.SamplingTime = ADC_SAMPLETIME_640CYCLES_5; HAI_ADC_ConfigChannel(&hadc1, &sConfig); HAI_ADC_Start(&hadc1); HAI_ADC_PollForConversion(&hadc1, 100); uint32_t temp_raw = HAL_ADC_GetValue(&hadc1); /* Convert to temperature using calibration values from datasheet */ ``` -------------------------------- ### HAL WWDG Update Source: https://github.com/stmicroelectronics/stm32g4xx-hal-driver/blob/master/Release_Notes.html Updated the HAL WWDG driver description. ```APIDOC ## HAL WWDG Update ### Description Updated HAL driver description. ### Method N/A ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response #### Success Response (200) N/A #### Response Example N/A ``` -------------------------------- ### Control GPIO Output Pins with LL Drivers Source: https://context7.com/stmicroelectronics/stm32g4xx-hal-driver/llms.txt Sets, resets, and toggles a GPIO output pin using LL functions. Also demonstrates reading the input pin state. ```c /* GPIO Operations using LL (faster than HAL) */ LL_GPIO_SetOutputPin(GPIOA, LL_GPIO_PIN_5); LL_GPIO_ResetOutputPin(GPIOA, LL_GPIO_PIN_5); LL_GPIO_TogglePin(GPIOA, LL_GPIO_PIN_5); uint32_t pin_state = LL_GPIO_IsInputPinSet(GPIOA, LL_GPIO_PIN_5); ``` -------------------------------- ### Configure DMA Burst for TIM Peripheral Source: https://github.com/stmicroelectronics/stm32g4xx-hal-driver/blob/master/Release_Notes.html APIs for configuring DMA burst transfers to or from the TIM peripheral. Use HAL_TIM_DMABurst_MultiWriteStart for memory-to-peripheral transfers and HAL_TIM_DMABurst_MultiReadStart for peripheral-to-memory transfers. ```c HAL_TIM_DMABurst_MultiWriteStart(TIM_HandleTypeDef *htim, uint32_t Instance, uint32_t Channel, uint32_t *pData, uint32_t Length); HAL_TIM_DMABurst_MultiReadStart(TIM_HandleTypeDef *htim, uint32_t Instance, uint32_t Channel, uint32_t *pData, uint32_t Length); ``` -------------------------------- ### Update HAL_LPTIM_Init() for Digital Filter on External Clock Source: https://github.com/stmicroelectronics/stm32g4xx-hal-driver/blob/master/Release_Notes.html The HAL_LPTIM_Init() API is updated to apply a digital filter for the external clock source for all LPTIM clock sources. This improves the stability of LPTIM operation with external clocks. ```c HAL_LPTIM_Init(LPTIM_HandleTypeDef *hlptim) ``` -------------------------------- ### HAL/LL USB Updates Source: https://github.com/stmicroelectronics/stm32g4xx-hal-driver/blob/master/Release_Notes.html Fixes for USB ISO IN double buffer mode transfer and PMA rx count descriptor. ```APIDOC ## HAL/LL USB Update ### Description Fixes for USB ISO IN double buffer mode transfer and PMA rx count descriptor, along with added instructions before reading the RX count register. ### Method N/A ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response #### Success Response (200) N/A #### Response Example N/A ### Fixes - Fixed USB ISO IN double buffer mode Transfer. - Fixed PMA rx count descriptor. - Added few instructions before reading the RX count register. ``` -------------------------------- ### ADC Half Conversion Complete Callback (HAL_ADC_ConvHalfCpltCallback) Source: https://context7.com/stmicroelectronics/stm32g4xx-hal-driver/llms.txt This callback is invoked when half of the DMA buffer has been filled during continuous ADC sampling. It allows for processing the first half of the buffer while the second half is being filled. ```c void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef *hadc) { /* Half of DMA buffer filled - process first half */ } ``` -------------------------------- ### Update TIM Synchronization Master Configuration Source: https://github.com/stmicroelectronics/stm32g4xx-hal-driver/blob/master/Release_Notes.html Modified HAL_TIMEx_MasterConfigSynchronization() to avoid functional errors and assert fails when using certain TIM instances as input triggers. Replaced IS_TIM_SYNCHRO_INSTANCE with IS_TIM_MASTER_INSTANCE and added IS_TIM_SLAVE_INSTANCE. ```c HAL_TIMEx_MasterConfigSynchronization(TIM_HandleTypeDef *htim, TIMEx_MasterConfigTypeDef *sMasterConfig); ``` -------------------------------- ### Check COMP Blanking Source Instance Source: https://github.com/stmicroelectronics/stm32g4xx-hal-driver/blob/master/Release_Notes.html Macro to validate the COMP blanking source instance. This check is updated to support STM32G491/STM32G4A1 devices with 4 COMP instances. ```c IS_COMP_BLANKINGSRC_INSTANCE(handle, Instance) ``` -------------------------------- ### HAL/LL UART Updates Source: https://github.com/stmicroelectronics/stm32g4xx-hal-driver/blob/master/Release_Notes.html Enhancements to the UART driver, including new reception APIs for idle events and fixes for configuration issues. ```APIDOC ## HAL/LL UART Update ### Description Enhanced reception for idle services (ReceptionToIdle) with a new field in UART_HandleTypeDef to identify reception type and added UART Reception Event Callback registration. New APIs for reception specific to Idle transfer in different modes have been added. ### Method N/A (API descriptions) ### Endpoint N/A ### Parameters #### Request Body - **HAL_UART_RxTypeTypeDef** (enum) - Optional - Field added to UART_HandleTypeDef to identify the type of ongoing reception. ### Request Example N/A ### Response #### Success Response (200) N/A #### Response Example N/A ### New APIs - **HAL_UARTEx_ReceiveToIdle()**: Receive data in blocking mode until expected amount is received or an IDLE event occurs. - **HAL_UARTEx_ReceiveToIdle_IT()**: Receive data in interrupt mode until expected amount is received or an IDLE event occurs. - **HAL_UARTEx_ReceiveToIdle_DMA()**: Receive data in DMA mode until expected amount is received or an IDLE event occurs. ### Updated APIs - **HAL_UART_Receive()**, **HAL_UART_Receive_IT()**, **HAL_UART_Receive_DMA()**: Updated to support the new ReceptionToIdle enhancement. ### Fixes - Fixed invalid initialization of UART configuration by removing FIFO mode configuration. - Fixed typos in UART State definition description. - Optimized stack usage for multiple APIs. ``` -------------------------------- ### Configure USART with LL Drivers Source: https://context7.com/stmicroelectronics/stm32g4xx-hal-driver/llms.txt Configures USART2 with specified baud rate, data width, stop bits, parity, and transfer direction using LL functions. LL drivers offer optimized performance. ```c /* USART Configuration using LL */ LL_USART_SetBaudRate(USART2, LL_RCC_GetUSARTClockFreq(LL_RCC_USART2_CLKSOURCE), LL_USART_PRESCALER_DIV1, LL_USART_OVERSAMPLING_16, 115200); LL_USART_SetDataWidth(USART2, LL_USART_DATAWIDTH_8B); LL_USART_SetStopBitsLength(USART2, LL_USART_STOPBITS_1); LL_USART_SetParity(USART2, LL_USART_PARITY_NONE); LL_USART_SetTransferDirection(USART2, LL_USART_DIRECTION_TX_RX); LL_USART_Enable(USART2); ``` -------------------------------- ### Perform DMA Memory-to-Memory Transfer (Polling) Source: https://context7.com/stmicroelectronics/stm32g4xx-hal-driver/llms.txt Initiates a memory-to-memory DMA transfer and waits for its completion using polling. Ensure the DMA is initialized and source/destination buffers are prepared. ```c /* Memory-to-memory transfer */ uint32_t src_buffer[256]; uint32_t dst_buffer[256]; /* Fill source buffer */ for (int i = 0; i < 256; i++) src_buffer[i] = i; /* Polling mode transfer */ HAL_DMA_Start(&hdma_memtomem, (uint32_t)src_buffer, (uint32_t)dst_buffer, 256); HAL_DMA_PollForTransfer(&hdma_memtomem, HAL_DMA_FULL_TRANSFER, HAL_MAX_DELAY); ``` -------------------------------- ### Initialize I2C1 Peripheral Source: https://context7.com/stmicroelectronics/stm32g4xx-hal-driver/llms.txt Configures the I2C1 peripheral for master mode with a timing of 100 kHz at 170 MHz, 7-bit addressing, and no stretch mode. Ensure Error_Handler is defined for error management. ```c #include "stm32g4xx_hal.h" I2C_HandleTypeDef hi2c1; /* I2C Initialization */ void MX_I2C1_Init(void) { hi2c1.Instance = I2C1; hi2c1.Init.Timing = 0x10909CEC; /* 100 kHz @ 170 MHz */ hi2c1.Init.OwnAddress1 = 0; hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; if (HAL_I2C_Init(&hi2c1) != HAL_OK) { Error_Handler(); } } ``` -------------------------------- ### HAL/LL IRDA Updates Source: https://github.com/stmicroelectronics/stm32g4xx-hal-driver/blob/master/Release_Notes.html Fixes for IRDA state definition typos and stack usage optimization. ```APIDOC ## HAL/LL IRDA Update ### Description Fixed typos in IRDA State definition description and optimized stack usage for multiple APIs. ### Method N/A ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response #### Success Response (200) N/A #### Response Example N/A ### Fixes - Fixed typos in IRDA State definition description. - Optimized stack usage for multiple APIs. ``` -------------------------------- ### Check TIM Master Instance Source: https://github.com/stmicroelectronics/stm32g4xx-hal-driver/blob/master/Release_Notes.html Macro to validate if a TIM instance can be used as a master in synchronization configurations. Replaces IS_TIM_SYNCHRO_INSTANCE for improved accuracy. ```c IS_TIM_MASTER_INSTANCE(TIMx) ``` -------------------------------- ### Initialize SPI1 Peripheral Source: https://context7.com/stmicroelectronics/stm32g4xx-hal-driver/llms.txt Configures the SPI1 peripheral for master mode with 8-bit data size, low clock polarity, first edge phase, software NSS, and a baud rate prescaler of 16. Ensure Error_Handler is defined for error management. ```c #include "stm32g4xx_hal.h" SPI_HandleTypeDef hspi1; /* SPI Initialization */ void MX_SPI1_Init(void) { hspi1.Instance = SPI1; hspi1.Init.Mode = SPI_MODE_MASTER; hspi1.Init.Direction = SPI_DIRECTION_2LINES; hspi1.Init.DataSize = SPI_DATASIZE_8BIT; hspi1.Init.CLKPolarity = SPI_POLARITY_LOW; hspi1.Init.CLKPhase = SPI_PHASE_1EDGE; hspi1.Init.NSS = SPI_NSS_SOFT; hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16; hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB; hspi1.Init.TIMode = SPI_TIMODE_DISABLE; hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; hspi1.Init.NSSPMode = SPI_NSS_PULSE_DISABLE; if (HAL_SPI_Init(&hspi1) != HAL_OK) { Error_Handler(); } } ``` -------------------------------- ### Update LL_RTC_BKP_SetRegister() and LL_RTC_BKP_GetRegister() for MISRA Compliance Source: https://github.com/stmicroelectronics/stm32g4xx-hal-driver/blob/master/Release_Notes.html These APIs are updated to comply with rules 2.2 and 13.2 of MISRA C:2012. This ensures better code quality and adherence to coding standards for backup register access. ```c LL_RTC_BKP_SetRegister(RTC_TypeDef *RTCx, uint32_t BKP_Register, uint32_t Value) ``` ```c LL_RTC_BKP_GetRegister(RTC_TypeDef *RTCx, uint32_t BKP_Register) ``` -------------------------------- ### Configure System Clock to 170 MHz Source: https://context7.com/stmicroelectronics/stm32g4xx-hal-driver/llms.txt Sets up the system clock to 170 MHz using HSE and PLL. This configuration involves setting voltage scaling, initializing the HSE oscillator, and configuring the PLL parameters. ```c #include "stm32g4xx_hal.h" /* System Clock Configuration (170 MHz using HSE + PLL) */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; /* Configure the main internal regulator output voltage */ HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1_BOOST); /* Configure LSE Drive */ HAL_PWR_EnableBkUpAccess(); __HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW); /* Initialize HSE Oscillator and PLL */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV2; /* 8 MHz / 2 = 4 MHz */ RCC_OscInitStruct.PLL.PLLN = 85; /* 4 MHz * 85 = 340 MHz */ RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2; RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2; /* 340 MHz / 2 = 170 MHz */ if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /* Configure CPU, AHB and APB 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_8) != HAL_OK) { Error_Handler(); } } ``` -------------------------------- ### Update CRYP_GCMCCM_SetHeaderPhase() API for AAD Support Source: https://github.com/stmicroelectronics/stm32g4xx-hal-driver/blob/master/Release_Notes.html This API is updated to support AAD with all possible byte sizes, not only multiples of 4 bytes. It involves a new parameter HeaderWidthUnit in the CRYP_ConfigTypeDef structure. ```c CRYP_GCMCCM_SetHeaderPhase(CRYP_HandleTypeDef *hcryp, uint32_t HeaderWidthUnit) ``` -------------------------------- ### Update SPI Initialization for Slave Mode Source: https://github.com/stmicroelectronics/stm32g4xx-hal-driver/blob/master/Release_Notes.html Modified HAL_SPI_Init to prevent setting the BaudRatePrescaler in Slave Motorola Mode and to use bit-masks for SPI configuration, improving robustness. ```c HAL_SPI_Init(SPI_HandleTypeDef *hspi); ``` -------------------------------- ### Enable SMBUS Fast Mode Plus Source: https://github.com/stmicroelectronics/stm32g4xx-hal-driver/blob/master/Release_Notes.html Call this function to enable Fast Mode Plus (Fm+) for SMBUS communication, ensuring compliance with SMBUS rev 3. Ensure SMBUS is initialized. ```c HAL_SMBUSEx_EnableFastModePlus(&hsmbus1); ``` -------------------------------- ### UART Receive Until Idle (DMA) Source: https://github.com/stmicroelectronics/stm32g4xx-hal-driver/blob/master/Release_Notes.html Use this API to receive data in DMA mode until the expected number of bytes is received or an IDLE event occurs. DMA must be configured for UART reception. ```c HAL_UARTEx_ReceiveToIdle_DMA(&huart1, pData, Size); ``` -------------------------------- ### UART Receive Until Idle (Blocking) Source: https://github.com/stmicroelectronics/stm32g4xx-hal-driver/blob/master/Release_Notes.html Use this API to receive data in blocking mode until the expected number of bytes is received or an IDLE event occurs. Ensure the UART peripheral is initialized. ```c HAL_UARTEx_ReceiveToIdle(&huart1, pData, Size, Timeout); ``` -------------------------------- ### HAL SMBUS Update Source: https://github.com/stmicroelectronics/stm32g4xx-hal-driver/blob/master/Release_Notes.html Added support for Fast Mode Plus to comply with SMBUS revision 3. ```APIDOC ## HAL SMBUS Update ### Description Support for Fast Mode Plus to be SMBUS rev 3 compliant. ### Method N/A (API descriptions) ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response #### Success Response (200) N/A #### Response Example N/A ### New APIs - **HAL_SMBUSEx_EnableFastModePlus()**: Enables Fast Mode Plus. - **HAL_SMBUSEx_DisableFastModePlus()**: Disables Fast Mode Plus. ``` -------------------------------- ### Update HRTIM Event Counter Constants Source: https://github.com/stmicroelectronics/stm32g4xx-hal-driver/blob/master/Release_Notes.html Renamed HAL and LL constants related to HRTIM event counters to better reflect their functionality and align with CMSIS definitions. Note potential compatibility break with older CubeMX versions. ```c HRTIM_EVENTCOUNTER_... LL_HRTIM_EVENT_COUNTER_... ``` -------------------------------- ### Clock Security System (CSS) Callback Source: https://context7.com/stmicroelectronics/stm32g4xx-hal-driver/llms.txt Callback function executed when the Clock Security System detects a failure in the HSE clock. This function should implement error handling or switch to a backup clock. ```c /* CSS Callback */ void HAL_RCC_CSSCallback(void) { /* Clock failure detected - switch to safe clock or handle error */ } ``` -------------------------------- ### Fix NAND Page Calculation for 8-bit Memories (C) Source: https://github.com/stmicroelectronics/stm32g4xx-hal-driver/blob/master/Release_Notes.html This update fixes an issue with page calculation in HAL_NAND_Write_Page_16b and HAL_NAND_Read_Page_16b APIs for 8-bit memories. ```c HAL_NAND_Write_Page_16b ``` ```c HAL_NAND_Read_Page_16b ``` -------------------------------- ### Add IS_CRYP_BUFFERSIZE() Macro for CRYP Buffer Size Check Source: https://github.com/stmicroelectronics/stm32g4xx-hal-driver/blob/master/Release_Notes.html This macro is used to validate the buffer size for CRYP operations. Ensure this macro is used before processing data to prevent potential issues. ```c #define IS_CRYP_BUFFERSIZE(SIZE) (((SIZE) >= CRYP_MIN_INTERFACE_SIZE) && ((SIZE) <= CRYP_MAX_INTERFACE_SIZE)) ``` -------------------------------- ### Configure MCO Output Source: https://context7.com/stmicroelectronics/stm32g4xx-hal-driver/llms.txt Configures the Microcontroller Clock Output (MCO) to output a selected clock source. Useful for debugging clock signals. ```c /* MCO output configuration (for debugging clock) */ HAI_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_SYSCLK, RCC_MCODIV_8); ``` -------------------------------- ### LL SPI Update Source: https://github.com/stmicroelectronics/stm32g4xx-hal-driver/blob/master/Release_Notes.html Updated the LL SPI driver to set the FRXTH bit for 8-bit data. ```APIDOC ## LL SPI Update ### Description Updated to set the FRXTH bit for 8bit data for LL_SPI_Init() API. ### Method N/A (API descriptions) ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response #### Success Response (200) N/A #### Response Example N/A ### Updated APIs - **LL_SPI_Init()**: Updated to set the FRXTH bit for 8bit data. ``` -------------------------------- ### Flash Memory Programming with HAL Source: https://context7.com/stmicroelectronics/stm32g4xx-hal-driver/llms.txt Unlocks, erases, programs, and locks the internal flash memory using HAL functions. Includes error handling and option byte configuration. ```c #include "stm32g4xx_hal.h" /* Unlock Flash for programming */ HAL_FLASH_Unlock(); /* Erase Flash pages */ FLASH_EraseInitTypeDef EraseInitStruct = {0}; uint32_t PageError = 0; EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES; EraseInitStruct.Banks = FLASH_BANK_1; EraseInitStruct.Page = 127; /* Last page (for data storage) */ EraseInitStruct.NbPages = 1; if (HAL_FLASHEx_Erase(&EraseInitStruct, &PageError) != HAL_OK) { /* Erase error */ uint32_t error = HAL_FLASH_GetError(); HAL_FLASH_Lock(); Error_Handler(); } /* Program Flash (64-bit double word) */ uint32_t address = 0x0803F800; /* Page 127 start address */ uint64_t data = 0x0123456789ABCDEF; if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, address, data) != HAL_OK) { uint32_t error = HAL_FLASH_GetError(); HAL_FLASH_Lock(); Error_Handler(); } /* Program multiple values */ uint64_t data_array[] = {0x1111111111111111, 0x2222222222222222, 0x3333333333333333}; for (int i = 0; i < 3; i++) { HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, address + (i * 8), data_array[i]); } /* Lock Flash after programming */ HAL_FLASH_Lock(); /* Read Flash data */ uint64_t read_data = *(__IO uint64_t *)address; /* Option bytes configuration */ HAL_FLASH_OB_Unlock(); FLASH_OBProgramInitTypeDef OBInit = {0}; HAL_FLASHEx_OBGetConfig(&OBInit); /* Configure read protection level */ OBInit.OptionType = OPTIONBYTE_RDP; OBInit.RDPLevel = OB_RDP_LEVEL_1; HAL_FLASHEx_OBProgram(&OBInit); /* Launch option bytes (causes system reset) */ HAL_FLASH_OB_Launch(); HAL_FLASH_OB_Lock(); ``` -------------------------------- ### Update CRYP_GCMCCM_SetPayloadPhase_IT() API Source: https://github.com/stmicroelectronics/stm32g4xx-hal-driver/blob/master/Release_Notes.html This API is updated to support data encryption and decryption with lengths not only multiples of 16 bytes. It is used in interrupt mode. ```c CRYP_GCMCCM_SetPayloadPhase_IT(CRYP_HandleTypeDef *hcryp) ``` -------------------------------- ### Enable TIM Update Interrupt Flag Remap Source: https://github.com/stmicroelectronics/stm32g4xx-hal-driver/blob/master/Release_Notes.html Use this macro to enable the Update Interrupt Flag Remap feature for TIM peripherals. Ensure TIM module is properly initialized before use. ```c #define __HAL_TIM_UIFREMAP_ENABLE(__HANDLE__) ... ``` -------------------------------- ### Correct HRTIM Output Set/Reset Constant Names Source: https://github.com/stmicroelectronics/stm32g4xx-hal-driver/blob/master/Release_Notes.html Several HRTIM output set and reset constant names have been corrected to comply with the Timer Events Mapping specified in the reference manual. Incorrect constants have been removed and new ones added. ```c /* Removed constants: HRTIM_OUTPUTSET_TIMCEV1_TIMACMP1 HRTIM_OUTPUTSET_TIMEEV5_TIMDCMP2 HRTIM_OUTPUTRESET_TIMCEV1_TIMACMP1 HRTIM_OUTPUTRESET_TIMEEV5_TIMDCMP2 */ ``` ```c #define HRTIM_OUTPUTSET_TIMEEV5_TIMCCMP2 ((uint32_t)0x00000000U) /*!< Event E on Timer E output set */ ``` ```c #define HRTIM_OUTPUTSET_TIMCEV2_TIMACMP3 ((uint32_t)0x00000000U) /*!< Event C on Timer A output set */ ``` ```c #define HRTIM_OUTPUTRESET_TIMCEV2_TIMACMP3 ((uint32_t)0x00000000U) /*!< Event C on Timer A output reset */ ``` ```c #define HRTIM_OUTPUTRESET_TIMEEV5_TIMCCMP2 ((uint32_t)0x00000000U) /*!< Event E on Timer E output reset */ ``` ```c #define HRTIM_OUTPUTSET_TIMCEV2_TIMACMP2 ((uint32_t)0x00000000U) /*!< Event C on Timer A output set */ ``` ```c #define HRTIM_OUTPUTRESET_TIMCEV2_TIMACMP2 ((uint32_t)0x00000000U) /*!< Event C on Timer A output reset */ ```