### Configure Debug Freeze and Get Device IDs (C) Source: https://context7.com/stmicroelectronics/stm32wbxx-hal-driver/llms.txt Configures debug behavior, enabling debug in low-power modes and freezing specific peripherals when breakpoints are hit. It also retrieves the device revision and ID. This function requires the 'stm32wbxx_hal.h' header. ```c #include "stm32wbxx_hal.h" void Debug_Configuration(void) { /* Enable debug in low-power modes */ HAL_DBGMCU_EnableDBGSleepMode(); HAL_DBGMCU_EnableDBGStopMode(); HAL_DBGMCU_EnableDBGStandbyMode(); /* Freeze peripherals during debug breakpoints */ __HAL_DBGMCU_FREEZE_TIM1(); __HAL_DBGMCU_FREEZE_TIM2(); __HAL_DBGMCU_FREEZE_IWDG(); __HAL_DBGMCU_FREEZE_WWDG(); __HAL_DBGMCU_FREEZE_I2C1_TIMEOUT(); __HAL_DBGMCU_FREEZE_LPTIM1(); /* Get device identifiers */ uint32_t rev_id = HAL_GetREVID(); /* Revision ID */ uint32_t dev_id = HAL_GetDEVID(); /* Device ID */ } ``` -------------------------------- ### Get STM32WB Clock Frequencies using RCC HAL Source: https://context7.com/stmicroelectronics/stm32wbxx-hal-driver/llms.txt Retrieves the current system clock, AHB clock, APB1 clock, and APB2 clock frequencies using the RCC HAL driver. This function is useful for debugging and understanding the current clock configuration. Requires the 'stm32wbxx_hal.h' header. ```c #include "stm32wbxx_hal.h" void Get_Clock_Frequencies(void) { /* Get current clock frequencies */ uint32_t sysclk = HAL_RCC_GetSysClockFreq(); /* System clock */ uint32_t hclk = HAL_RCC_GetHCLKFreq(); /* AHB clock (CPU1) */ uint32_t pclk1 = HAL_RCC_GetPCLK1Freq(); /* APB1 clock */ uint32_t pclk2 = HAL_RCC_GetPCLK2Freq(); /* APB2 clock */ } ``` -------------------------------- ### HAL Core Initialization and System Functions (C) Source: https://context7.com/stmicroelectronics/stm32wbxx-hal-driver/llms.txt Initializes the HAL core, configures system clock, retrieves HAL version, and obtains unique device identifiers. Includes a main loop with a blocking delay and SysTick interrupt handling for tick management. Requires the `stm32wbxx_hal.h` header. ```c #include "stm32wbxx_hal.h" int main(void) { /* Initialize the HAL Library - configures Flash, Systick, NVIC */ if (HAL_Init() != HAL_OK) { /* Initialization Error */ Error_Handler(); } /* Configure the system clock */ SystemClock_Config(); /* Get HAL version (format: 0xXYZR where X=main, Y=sub1, Z=sub2, R=release) */ uint32_t hal_version = HAL_GetHalVersion(); /* Get unique device identifier (96-bit UID) */ uint32_t uid0 = HAL_GetUIDw0(); uint32_t uid1 = HAL_GetUIDw1(); uint32_t uid2 = HAL_GetUIDw2(); /* Main loop with HAL delay */ while (1) { HAL_Delay(1000); /* Delay 1 second (blocking) */ } } /* SysTick interrupt handler - must be called every 1ms */ void SysTick_Handler(void) { HAL_IncTick(); } /* Configure tick frequency (default 1kHz) */ void Configure_Tick(void) { HAL_SetTickFreq(HAL_TICK_FREQ_1KHZ); /* Options: 10Hz, 100Hz, 1kHz */ uint32_t current_tick = HAL_GetTick(); } ``` -------------------------------- ### Configure SYSCFG Memory Remap, I/O Compensation, and Security (C) Source: https://context7.com/stmicroelectronics/stm32wbxx-hal-driver/llms.txt Configures SYSCFG settings including memory remapping for boot modes, enabling Fast Mode Plus for I2C pins, activating the I/O analog switch booster, and setting security access for cryptographic peripherals. It also includes SRAM2 protection and parity error checking. ```c #include "stm32wbxx_hal.h" void SYSCFG_Configuration(void) { /* Memory remap - boot from different memory */ __HAL_SYSCFG_REMAPMEMORY_FLASH(); /* Boot from Flash (default) */ __HAL_SYSCFG_REMAPMEMORY_SYSTEMFLASH(); /* Boot from System Flash */ __HAL_SYSCFG_REMAPMEMORY_SRAM(); /* Boot from SRAM */ /* Get current boot configuration */ uint32_t boot_mode = __HAL_SYSCFG_GET_BOOT_MODE(); /* Enable Fast Mode Plus for I2C pins (up to 1MHz) */ __HAL_SYSCFG_FASTMODEPLUS_ENABLE(SYSCFG_FASTMODEPLUS_PB6); __HAL_SYSCFG_FASTMODEPLUS_ENABLE(SYSCFG_FASTMODEPLUS_PB7); /* Enable I/O analog switch booster */ HAL_SYSCFG_EnableIOBooster(); /* Enable security access to cryptographic peripherals */ HAL_SYSCFG_EnableSecurityAccess(HAL_SYSCFG_SECURE_ACCESS_AES2); HAL_SYSCFG_EnableSecurityAccess(HAL_SYSCFG_SECURE_ACCESS_PKA); HAL_SYSCFG_EnableSecurityAccess(HAL_SYSCFG_SECURE_ACCESS_RNG); /* SRAM2 protection */ __HAL_SYSCFG_SRAM2_WRP_0_31_ENABLE(SYSCFG_SRAM2WRP_PAGE0 | SYSCFG_SRAM2WRP_PAGE1); /* Check SRAM2 parity error flag */ if (__HAL_SYSCFG_GET_FLAG(SYSCFG_FLAG_SRAM2_PE)) { __HAL_SYSCFG_CLEAR_FLAG(); /* Clear parity error flag */ } } ``` -------------------------------- ### Configure and Enable VREFBUF (C) Source: https://context7.com/stmicroelectronics/stm32wbxx-hal-driver/llms.txt Configures the internal voltage reference buffer (VREFBUF) by setting the voltage scale and high-impedance mode. It then enables the VREFBUF, with an error check to ensure it's ready. This function requires the 'stm32wbxx_hal.h' header and a defined 'Error_Handler'. ```c #include "stm32wbxx_hal.h" void VREFBUF_Configuration(void) { /* Configure internal voltage reference buffer */ HAL_SYSCFG_VREFBUF_VoltageScalingConfig(SYSCFG_VREFBUF_VOLTAGE_SCALE0); HAL_SYSCFG_VREFBUF_HighImpedanceConfig(SYSCFG_VREFBUF_HIGH_IMPEDANCE_DISABLE); /* Enable VREFBUF */ if (HAL_SYSCFG_EnableVREFBUF() != HAL_OK) { /* VREFBUF not ready */ Error_Handler(); } } ``` -------------------------------- ### Initialize SPI Communication (C) Source: https://context7.com/stmicroelectronics/stm32wbxx-hal-driver/llms.txt Initializes the SPI peripheral in master mode with configurable parameters such as data size, clock polarity/phase, NSS management, and baud rate. It also configures the necessary GPIO pins for SPI communication and enables the SPI interrupt. ```c #include "stm32wbxx_hal.h" SPI_HandleTypeDef hspi1; uint8_t spi_tx_buffer[32]; uint8_t spi_rx_buffer[32]; void SPI_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; /* Software slave select */ 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.CRCPolynomial = 7; hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE; hspi1.Init.NSSPMode = SPI_NSS_PULSE_DISABLE; if (HAL_SPI_Init(&hspi1) != HAL_OK) { Error_Handler(); } } void HAL_SPI_MspInit(SPI_HandleTypeDef *hspi) { GPIO_InitTypeDef GPIO_InitStruct = {0}; if (hspi->Instance == SPI1) { __HAL_RCC_SPI1_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE(); /* PA5 = SCK, PA6 = MISO, PA7 = 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_VERY_HIGH; GPIO_InitStruct.Alternate = GPIO_AF5_SPI1; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /* PA4 = NSS (software controlled) */ GPIO_InitStruct.Pin = GPIO_PIN_4; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET); /* CS high (inactive) */ HAL_NVIC_SetPriority(SPI1_IRQn, 0, 0); HAL_NVIC_EnableIRQ(SPI1_IRQn); } } ``` -------------------------------- ### Initialize I2C Peripheral (C) Source: https://context7.com/stmicroelectronics/stm32wbxx-hal-driver/llms.txt Initializes the I2C1 peripheral with specified timing, addressing mode, and other configurations. This function must be called before any I2C operations. It relies on the HAL_I2C_Init function and requires the I2C1 clock and associated GPIO pins to be enabled. ```c #include "stm32wbxx_hal.h" I2C_HandleTypeDef hi2c1; #define EEPROM_ADDRESS 0xA0 /* 7-bit address shifted left */ #define SENSOR_ADDRESS 0x68 /* MPU6050 example */ void I2C_Init(void) { hi2c1.Instance = I2C1; hi2c1.Init.Timing = 0x10909CEC; /* 100kHz @ 64MHz, use CubeMX to calculate */ hi2c1.Init.OwnAddress1 = 0; hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; hi2c1.Init.OwnAddress2 = 0; hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK; hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; if (HAL_I2C_Init(&hi2c1) != HAL_OK) { Error_Handler(); } } ``` -------------------------------- ### Configure I2C MSP (C) Source: https://context7.com/stmicroelectronics/stm32wbxx-hal-driver/llms.txt Configures the Master Service Provider (MSP) for the I2C1 peripheral, enabling clocks for GPIOB and I2C1. It sets up GPIO pins PB6 (SCL) and PB7 (SDA) for alternate function open-drain mode and configures NVIC for I2C1 event and error interrupts. External pull-ups on SDA and SCL lines are required. ```c void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c) { GPIO_InitTypeDef GPIO_InitStruct = {0}; if (hi2c->Instance == I2C1) { __HAL_RCC_GPIOB_CLK_ENABLE(); __HAL_RCC_I2C1_CLK_ENABLE(); /* PB6 = SCL, PB7 = SDA */ GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_7; GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; GPIO_InitStruct.Pull = GPIO_NOPULL; /* External pull-ups required */ GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; GPIO_InitStruct.Alternate = GPIO_AF4_I2C1; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); HAL_NVIC_SetPriority(I2C1_EV_IRQn, 0, 0); HAL_NVIC_EnableIRQ(I2C1_EV_IRQn); HAL_NVIC_SetPriority(I2C1_ER_IRQn, 0, 0); HAL_NVIC_EnableIRQ(I2C1_ER_IRQn); } } ``` -------------------------------- ### GPIO Configuration and Control (C) Source: https://context7.com/stmicroelectronics/stm32wbxx-hal-driver/llms.txt Configures GPIO pins for input, output, and external interrupts. Enables clock for GPIO ports, sets pin modes (push-pull, open-drain, input), pull-up/down resistors, and speed. Also handles pin read/write, toggling, locking, and external interrupt callbacks. Requires `stm32wbxx_hal.h`. ```c #include "stm32wbxx_hal.h" GPIO_InitTypeDef GPIO_InitStruct = {0}; void GPIO_Configuration(void) { /* Enable GPIO port clocks */ __HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE(); /* 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); /* Configure PB4 as input with pull-up (button) */ GPIO_InitStruct.Pin = GPIO_PIN_4; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_PULLUP; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); /* Configure PC13 as external interrupt (falling edge) */ GPIO_InitStruct.Pin = GPIO_PIN_13; GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING; GPIO_InitStruct.Pull = GPIO_PULLUP; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); /* Enable and set EXTI line interrupt priority */ HAL_NVIC_SetPriority(EXTI15_10_IRQn, 2, 0); HAL_NVIC_EnableIRQ(EXTI15_10_IRQn); } void GPIO_Operations(void) { /* Write pin state */ HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET); HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET); /* Toggle pin */ HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5); /* Read pin state */ GPIO_PinState state = HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_4); if (state == GPIO_PIN_RESET) { /* Button pressed (active low) */ } /* Lock pin configuration (cannot be modified until reset) */ HAL_GPIO_LockPin(GPIOA, GPIO_PIN_5); /* Write multiple pins simultaneously */ HAL_GPIO_WriteMultipleStatePin(GPIOA, GPIO_PIN_6, GPIO_PIN_5); /* Reset PA6, Set PA5 */ } /* External interrupt callback */ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { if (GPIO_Pin == GPIO_PIN_13) { HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5); /* Toggle LED on button press */ } } /* EXTI interrupt handler */ void EXTI15_10_IRQHandler(void) { HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_13); } ``` -------------------------------- ### Configure Low Power Settings on STM32WB using PWR HAL Source: https://context7.com/stmicroelectronics/stm32wbxx-hal-driver/llms.txt Configures low power settings on the STM32WB microcontroller using the PWR HAL driver. This includes enabling the Power Control clock, controlling voltage scaling for reduced power consumption, and enabling Flash power-down during low-power sleep. Requires 'stm32wbxx_hal.h', 'stm32wbxx_hal_pwr.h', and 'stm32wbxx_hal_pwr_ex.h'. ```c #include "stm32wbxx_hal.h" #include "stm32wbxx_hal_pwr.h" #include "stm32wbxx_hal_pwr_ex.h" void Configure_Low_Power(void) { /* Enable Power Control clock */ __HAL_RCC_PWR_CLK_ENABLE(); /* Configure voltage scaling for lower power */ HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE2); /* Enable Flash power-down during sleep */ HAL_PWREx_EnableFlashPowerDown(PWR_FLASHPD_LPSLEEP); } ``` -------------------------------- ### Initialize UART Peripheral (C) Source: https://context7.com/stmicroelectronics/stm32wbxx-hal-driver/llms.txt Initializes the UART peripheral (USART1) with specified parameters including baud rate, word length, stop bits, parity, mode, and hardware flow control. It also configures the associated GPIO pins (PA9 for TX, PA10 for RX) and enables the UART interrupt. This function is typically called once during system startup. ```c #include "stm32wbxx_hal.h" UART_HandleTypeDef huart1; uint8_t rx_buffer[128]; uint8_t tx_buffer[] = "Hello STM32WB!\r\n"; void UART_Init(void) { huart1.Instance = USART1; huart1.Init.BaudRate = 115200; huart1.Init.WordLength = UART_WORDLENGTH_8B; huart1.Init.StopBits = UART_STOPBITS_1; huart1.Init.Parity = UART_PARITY_NONE; huart1.Init.Mode = UART_MODE_TX_RX; huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; huart1.Init.OverSampling = UART_OVERSAMPLING_16; huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE; huart1.Init.ClockPrescaler = UART_PRESCALER_DIV1; /* Advanced features (optional) */ huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT; if (HAL_UART_Init(&huart1) != HAL_OK) { Error_Handler(); } } /* MSP (MCU Support Package) initialization - called by HAL_UART_Init */ void HAL_UART_MspInit(UART_HandleTypeDef *huart) { GPIO_InitTypeDef GPIO_InitStruct = {0}; if (huart->Instance == USART1) { __HAL_RCC_USART1_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE(); /* PA9 = TX, PA10 = RX */ GPIO_InitStruct.Pin = GPIO_PIN_9 | GPIO_PIN_10; 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); /* Enable interrupt */ HAL_NVIC_SetPriority(USART1_IRQn, 0, 0); HAL_NVIC_EnableIRQ(USART1_IRQn); } } ``` -------------------------------- ### Perform I2C Master Operations (C) Source: https://context7.com/stmicroelectronics/stm32wbxx-hal-driver/llms.txt Demonstrates I2C master operations including checking device readiness, blocking transmit, blocking receive, and initiating an interrupt-driven transmit. It uses the SENSOR_ADDRESS for communication and specifies timeouts for blocking operations. The interrupt-driven transmit requires corresponding interrupt handlers and callbacks. ```c void I2C_Master_Operations(void) { uint8_t tx_data[4] = {0x01, 0x02, 0x03, 0x04}; uint8_t rx_data[6]; /* Check if device is ready */ if (HAL_I2C_IsDeviceReady(&hi2c1, SENSOR_ADDRESS << 1, 3, 100) == HAL_OK) { /* Device responded */ } /* Master transmit (blocking) */ HAL_I2C_Master_Transmit(&hi2c1, SENSOR_ADDRESS << 1, tx_data, 4, 1000); /* Master receive (blocking) */ HAL_I2C_Master_Receive(&hi2c1, SENSOR_ADDRESS << 1, rx_data, 6, 1000); /* Master transmit (interrupt mode) */ HAL_I2C_Master_Transmit_IT(&hi2c1, SENSOR_ADDRESS << 1, tx_data, 4); } ``` -------------------------------- ### Enter Standby Mode on STM32WB using PWR HAL Source: https://context7.com/stmicroelectronics/stm32wbxx-hal-driver/llms.txt Enables entering Standby Mode on the STM32WB microcontroller using the PWR HAL driver. This is the lowest power mode, where only the backup domain is powered. The device resets upon wakeup. Requires 'stm32wbxx_hal.h', 'stm32wbxx_hal_pwr.h', and 'stm32wbxx_hal_pwr_ex.h'. ```c #include "stm32wbxx_hal.h" #include "stm32wbxx_hal_pwr.h" #include "stm32wbxx_hal_pwr_ex.h" void Enter_Standby_Mode(void) { /* Standby mode - lowest power, only backup domain powered */ /* Clear wakeup flags */ __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU); /* Enable wakeup pin (e.g., WKUP1 = PA0) */ HAL_PWR_EnableWakeUpPin(PWR_WAKEUP_PIN1_HIGH); /* Enter Standby Mode - will reset on wakeup */ HAL_PWR_EnterSTANDBYMode(); /* Code never reaches here */ } ``` -------------------------------- ### Configure STM32WB System Clock using RCC HAL Source: https://context7.com/stmicroelectronics/stm32wbxx-hal-driver/llms.txt Configures the STM32WB microcontroller's system clock using the RCC HAL driver. It sets up HSE and PLL for a 64MHz system clock and configures bus dividers. Requires the 'stm32wbxx_hal.h' header. ```c #include "stm32wbxx_hal.h" void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; /* Configure HSE and PLL for 64MHz system clock */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_LSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.LSEState = RCC_LSE_ON; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV2; RCC_OscInitStruct.PLL.PLLN = 16; RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2; RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /* Configure system clock source and bus dividers */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2 | RCC_CLOCKTYPE_HCLK2 | RCC_CLOCKTYPE_HCLK4; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; RCC_ClkInitStruct.AHBCLK2Divider = RCC_SYSCLK_DIV2; /* CPU2 clock */ RCC_ClkInitStruct.AHBCLK4Divider = RCC_SYSCLK_DIV1; /* Shared SRAM */ /* Flash latency for 64MHz @ 1.2V */ if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK) { Error_Handler(); } } ``` -------------------------------- ### Perform SPI Data Transfers (C) Source: https://context7.com/stmicroelectronics/stm32wbxx-hal-driver/llms.txt Demonstrates various SPI data transfer methods including full-duplex, transmit-only, and receive-only operations using blocking mode. It utilizes defined macros for controlling the Chip Select (CS) line. ```c #define SPI_CS_LOW() HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET) #define SPI_CS_HIGH() HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET) void SPI_Transmit_Receive(void) { /* Prepare data */ spi_tx_buffer[0] = 0x8F; /* Read WHO_AM_I register (example) */ /* Full-duplex transfer (blocking) */ SPI_CS_LOW(); HAL_SPI_TransmitReceive(&hspi1, spi_tx_buffer, spi_rx_buffer, 2, 1000); SPI_CS_HIGH(); /* Transmit only (blocking) */ SPI_CS_LOW(); HAL_SPI_Transmit(&hspi1, spi_tx_buffer, 4, 1000); SPI_CS_HIGH(); /* Receive only (blocking) */ SPI_CS_LOW(); HAL_SPI_Receive(&hspi1, spi_rx_buffer, 8, 1000); SPI_CS_HIGH(); } ``` -------------------------------- ### SPI Interrupt-Driven Transfers and Callbacks (C) Source: https://context7.com/stmicroelectronics/stm32wbxx-hal-driver/llms.txt Implements SPI communication using interrupts for non-blocking operations. It includes functions to initiate interrupt-driven transfers and defines callback functions for transfer completion and error handling. ```c void SPI_Interrupt_Mode(void) { SPI_CS_LOW(); /* Start non-blocking full-duplex transfer */ HAL_SPI_TransmitReceive_IT(&hspi1, spi_tx_buffer, spi_rx_buffer, 16); } /* Interrupt handler */ void SPI1_IRQHandler(void) { HAL_SPI_IRQHandler(&hspi1); } /* Transfer complete callback */ void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi) { if (hspi->Instance == SPI1) { SPI_CS_HIGH(); /* Process received data */ } } void HAL_SPI_ErrorCallback(SPI_HandleTypeDef *hspi) { uint32_t error = HAL_SPI_GetError(hspi); SPI_CS_HIGH(); /* Handle error: OVR, MODF, CRC, FRE */ } ``` -------------------------------- ### Enter Stop Mode on STM32WB using PWR HAL Source: https://context7.com/stmicroelectronics/stm32wbxx-hal-driver/llms.txt Enables entering Stop Mode on the STM32WB microcontroller using the PWR HAL driver. In Stop Mode, all clocks are stopped, but SRAM and register contents are retained. Wakeup sources must be configured beforehand. Requires reconfiguring clocks after wakeup. Requires 'stm32wbxx_hal.h', 'stm32wbxx_hal_pwr.h', and 'stm32wbxx_hal_pwr_ex.h'. ```c #include "stm32wbxx_hal.h" #include "stm32wbxx_hal_pwr.h" #include "stm32wbxx_hal_pwr_ex.h" void Enter_Stop_Mode(void) { /* Stop mode - all clocks stopped, SRAM and register contents retained */ /* Configure wakeup source (EXTI, RTC alarm, etc.) before entering */ HAL_SuspendTick(); /* Enter Stop Mode 2 (lowest power with retention) */ HAL_PWREx_EnterSTOP2Mode(PWR_STOPENTRY_WFI); /* Execution resumes here after wakeup */ HAL_ResumeTick(); /* Reconfigure clocks after wakeup (MSI is default) */ SystemClock_Config(); } ``` -------------------------------- ### I2C Callbacks (C) Source: https://context7.com/stmicroelectronics/stm32wbxx-hal-driver/llms.txt Defines callback functions that are invoked by the HAL driver upon completion of I2C master transmission, master reception, or occurrence of an I2C error. These callbacks allow for asynchronous handling of I2C events, such as processing received data or reacting to errors like acknowledge failures. ```c /* Callbacks */ void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *hi2c) { /* Master transmission complete */ } void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *hi2c) { /* Master reception complete */ } void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c) { uint32_t error = HAL_I2C_GetError(hi2c); if (error & HAL_I2C_ERROR_AF) { /* Acknowledge failure - device not responding */ } } ``` -------------------------------- ### Enable/Disable STM32WB Peripheral Clocks using RCC HAL Source: https://context7.com/stmicroelectronics/stm32wbxx-hal-driver/llms.txt Demonstrates how to enable and disable clocks for various peripherals on the STM32WB microcontroller using the RCC HAL driver. This is crucial for power management and ensuring peripherals are active when needed. Requires the 'stm32wbxx_hal.h' header. ```c #include "stm32wbxx_hal.h" void Peripheral_Clock_Control(void) { /* Enable peripheral clocks */ __HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_USART1_CLK_ENABLE(); __HAL_RCC_I2C1_CLK_ENABLE(); __HAL_RCC_SPI1_CLK_ENABLE(); __HAL_RCC_ADC_CLK_ENABLE(); __HAL_RCC_TIM1_CLK_ENABLE(); __HAL_RCC_DMA1_CLK_ENABLE(); __HAL_RCC_RNG_CLK_ENABLE(); __HAL_RCC_AES1_CLK_ENABLE(); /* Disable to save power */ __HAL_RCC_SPI1_CLK_DISABLE(); } ``` -------------------------------- ### Perform I2C Memory Operations (C) Source: https://context7.com/stmicroelectronics/stm32wbxx-hal-driver/llms.txt Performs read and write operations on I2C EEPROM memory. It demonstrates writing data to a specific memory address (0x0010) using a 16-bit address format and then reading data back from the same address. A delay is included after writing to allow for the EEPROM's internal write cycle. ```c void I2C_Memory_Operations(void) { uint8_t eeprom_data[16]; uint8_t write_data[] = {0xAA, 0xBB, 0xCC, 0xDD}; /* Write to EEPROM memory at address 0x0010 (16-bit address) */ HAL_I2C_Mem_Write(&hi2c1, EEPROM_ADDRESS, 0x0010, I2C_MEMADD_SIZE_16BIT, write_data, sizeof(write_data), 1000); /* Wait for EEPROM write cycle (typically 5ms) */ HAL_Delay(5); /* Read from EEPROM memory */ HAL_I2C_Mem_Read(&hi2c1, EEPROM_ADDRESS, 0x0010, I2C_MEMADD_SIZE_16BIT, eeprom_data, 16, 1000); } ``` -------------------------------- ### UART Interrupt Mode Operations (C) Source: https://context7.com/stmicroelectronics/stm32wbxx-hal-driver/llms.txt Shows how to use UART in interrupt mode for non-blocking transmission and reception. It initiates transmit and receive operations using IT (Interrupt Transfer) functions. Callbacks are provided for transmission complete, reception complete, and error handling. ```c void UART_Interrupt_Mode(void) { /* Start non-blocking transmission */ HAL_UART_Transmit_IT(&huart1, tx_buffer, sizeof(tx_buffer) - 1); /* Start non-blocking reception */ HAL_UART_Receive_IT(&huart1, rx_buffer, 10); } /* Interrupt handler */ void USART1_IRQHandler(void) { HAL_UART_IRQHandler(&huart1); } /* TX complete callback */ void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) { if (huart->Instance == USART1) { /* Transmission complete - can start new transfer */ } } /* RX complete callback */ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { if (huart->Instance == USART1) { /* Process received data, restart reception */ HAL_UART_Receive_IT(&huart1, rx_buffer, 10); } } /* Error callback */ void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart) { uint32_t error = HAL_UART_GetError(huart); if (error & HAL_UART_ERROR_ORE) { /* Overrun error - clear and restart */ __HAL_UART_CLEAR_OREFLAG(huart); } } ``` -------------------------------- ### UART Polling Mode Operations (C) Source: https://context7.com/stmicroelectronics/stm32wbxx-hal-driver/llms.txt Demonstrates UART communication using polling mode. Includes blocking transmit and receive functions. The transmit function sends data with a timeout, and the receive function waits for a specified number of bytes with a timeout. Error handling for receive is included. ```c void UART_Polling_Mode(void) { /* Transmit (blocking) - timeout 1000ms */ HAL_UART_Transmit(&huart1, tx_buffer, sizeof(tx_buffer) - 1, 1000); /* Receive (blocking) - wait for 10 bytes, timeout 5000ms */ if (HAL_UART_Receive(&huart1, rx_buffer, 10, 5000) == HAL_OK) { /* Process received data */ } } ``` -------------------------------- ### I2C Interrupt Handlers (C) Source: https://context7.com/stmicroelectronics/stm32wbxx-hal-driver/llms.txt Provides the interrupt service routines (ISRs) for I2C1 events and errors. These handlers are crucial for non-blocking I2C operations (interrupt and DMA modes) and must be implemented to call the respective HAL driverIRQHandler functions. ```c /* Interrupt handlers */ void I2C1_EV_IRQHandler(void) { HAL_I2C_EV_IRQHandler(&hi2c1); } void I2C1_ER_IRQHandler(void) { HAL_I2C_ER_IRQHandler(&hi2c1); } ``` -------------------------------- ### Enter Sleep Mode on STM32WB using PWR HAL Source: https://context7.com/stmicroelectronics/stm32wbxx-hal-driver/llms.txt Enables entering Sleep Mode on the STM32WB microcontroller using the PWR HAL driver. In Sleep Mode, the CPU clock is stopped, but peripherals remain active. This function requires suspending and resuming the system tick. Requires 'stm32wbxx_hal.h' and 'stm32wbxx_hal_pwr.h'. ```c #include "stm32wbxx_hal.h" #include "stm32wbxx_hal_pwr.h" #include "stm32wbxx_hal_pwr_ex.h" void Enter_Sleep_Mode(void) { /* Sleep mode - CPU clock stopped, peripherals running */ HAL_SuspendTick(); /* Prevent SysTick from waking up */ /* Enter Sleep Mode, wake up with any interrupt */ HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI); HAL_ResumeTick(); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.