i2c_application_system_stm32.c File Reference
#include <stdbool.h>
#include <stddef.h>
#include "main.h"
#include "acc_integration.h"
#include "acc_reg_protocol.h"
#include "i2c_application_system.h"

Go to the source code of this file.

Data Structures

struct  gpio_config_t
 GPIO config status, maps directly to the GPIO registers. More...
 

Macros

#define I2C_SLAVE_BUFFER_SIZE   4
 
#define WAIT_FOR_IDLE_RETRIES   10U
 
#define WAIT_FOR_IDLE_RETRY_INTERNAL_MS   10U
 
#define GPIO_BANK_COUNT   3U
 

Functions

static void disable_interrupts (void)
 Disable interrupts. More...
 
static void enable_interrupts (void)
 Enable interrupts. More...
 
static void prepare_power_down (void)
 Prepare power down state. More...
 
static void resume_power_down (void)
 Resume power down state. More...
 
static uint32_t get_rtc_tick (void)
 Get RTC ticks based on current RTC time. More...
 
static void rtc_tick_to_time (uint32_t tick, RTC_TimeTypeDef *time)
 Convert RTC ticks to RTC time. More...
 
static void rtc_set_next_wakeup_time (void)
 Function for setting the next wakeup time from the RTC interrupt. More...
 
static void wait_for_i2c_idle (void)
 Wait for I2C interface to be idle. More...
 
static void prepare_register_data (I2C_HandleTypeDef *hi2c)
 Helper function to prepare transmit of register data. More...
 
void i2c_application_system_init (void)
 Init the system. More...
 
void i2c_application_system_reset (void)
 Reset the system. More...
 
void i2c_application_system_wait_for_interrupt (void)
 Wait for interrupt to occur. More...
 
bool i2c_application_system_test_wakeup_pin (void)
 Check if wakeup pin is high. More...
 
void i2c_application_system_set_ready_pin (bool enable)
 Set the ready pin state. More...
 
void i2c_application_system_setup_generic_gpio_pin (bool enable)
 Setup the generic gpio pin. More...
 
void i2c_application_system_set_generic_gpio_pin (bool enable)
 Set the generic gpio pin output state. More...
 
void i2c_application_enter_low_power_state (void)
 Make the MCU enter its low power state. More...
 
void i2c_application_set_periodic_wakeup (uint32_t period_ms)
 Set up a periodic timer used to wake up the system from sleep. More...
 
bool i2c_application_is_periodic_wakeup (void)
 Test if a periodic wake up has occurred. More...
 
void HAL_RTCEx_AlarmBEventCallback (RTC_HandleTypeDef *rtc)
 IRQ Handler for RTC B Alarm. More...
 
void HAL_I2C_AddrCallback (I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, uint16_t AddrMatchCode)
 
void HAL_I2C_SlaveTxCpltCallback (I2C_HandleTypeDef *hi2c)
 
void HAL_I2C_SlaveRxCpltCallback (I2C_HandleTypeDef *hi2c)
 
void HAL_I2C_ListenCpltCallback (I2C_HandleTypeDef *hi2c)
 
void HAL_I2C_ErrorCallback (I2C_HandleTypeDef *hi2c)
 

Variables

SPI_HandleTypeDef A121_SPI_HANDLE
 
I2C_HandleTypeDef MODULE_I2C_HANDLE
 
UART_HandleTypeDef MODULE_UART1_HANDLE
 
UART_HandleTypeDef MODULE_UART2_HANDLE
 
RTC_HandleTypeDef MODULE_RTC_HANDLE
 
static RCC_OscInitTypeDef low_power_saved_rcc_oscinitstruct
 
static RCC_ClkInitTypeDef low_power_saved_rcc_clkinitstruct
 
static uint32_t low_power_saved_flatency
 
static gpio_config_t low_power_saved_gpio_bank [3U] = { 0 }
 
static GPIO_TypeDef * gpio_banks [3U] = { GPIOA, GPIOB, GPIOH }
 
static uint8_t i2c_slave_buffer [4]
 
static volatile bool i2c_idle = true
 
static uint32_t rtc_period_time_ms = 0
 
static volatile bool rtc_wakeup_triggered = false
 

Macro Definition Documentation

◆ GPIO_BANK_COUNT

#define GPIO_BANK_COUNT   3U

Definition at line 19 of file i2c_application_system_stm32.c.

◆ I2C_SLAVE_BUFFER_SIZE

#define I2C_SLAVE_BUFFER_SIZE   4

Definition at line 16 of file i2c_application_system_stm32.c.

◆ WAIT_FOR_IDLE_RETRIES

#define WAIT_FOR_IDLE_RETRIES   10U

Definition at line 17 of file i2c_application_system_stm32.c.

◆ WAIT_FOR_IDLE_RETRY_INTERNAL_MS

#define WAIT_FOR_IDLE_RETRY_INTERNAL_MS   10U

Definition at line 18 of file i2c_application_system_stm32.c.

Function Documentation

◆ disable_interrupts()

static void disable_interrupts ( void  )
inlinestatic

Disable interrupts.

Definition at line 245 of file i2c_application_system_stm32.c.

◆ enable_interrupts()

static void enable_interrupts ( void  )
inlinestatic

Enable interrupts.

Definition at line 253 of file i2c_application_system_stm32.c.

◆ get_rtc_tick()

static uint32_t get_rtc_tick ( void  )
static

Get RTC ticks based on current RTC time.

Returns
The current RTC ticks in ms

Definition at line 354 of file i2c_application_system_stm32.c.

◆ HAL_I2C_AddrCallback()

void HAL_I2C_AddrCallback ( I2C_HandleTypeDef *  hi2c,
uint8_t  TransferDirection,
uint16_t  AddrMatchCode 
)

Calling this function with wrong parameters will return HAL_ERROR Calling this function when the i2c module is in the wrong state return HAL_BUSY

Definition at line 497 of file i2c_application_system_stm32.c.

◆ HAL_I2C_ErrorCallback()

void HAL_I2C_ErrorCallback ( I2C_HandleTypeDef *  hi2c)

The data NACK is the only valid error code

Definition at line 590 of file i2c_application_system_stm32.c.

◆ HAL_I2C_ListenCpltCallback()

void HAL_I2C_ListenCpltCallback ( I2C_HandleTypeDef *  hi2c)

Calling this function with wrong parameters will return HAL_ERROR Calling this function when the i2c module is in the wrong state return HAL_BUSY

Definition at line 574 of file i2c_application_system_stm32.c.

◆ HAL_I2C_SlaveRxCpltCallback()

void HAL_I2C_SlaveRxCpltCallback ( I2C_HandleTypeDef *  hi2c)

Calling this function with wrong parameters will return HAL_ERROR Calling this function when the i2c module is in the wrong state return HAL_BUSY

Definition at line 537 of file i2c_application_system_stm32.c.

◆ HAL_I2C_SlaveTxCpltCallback()

void HAL_I2C_SlaveTxCpltCallback ( I2C_HandleTypeDef *  hi2c)

Definition at line 530 of file i2c_application_system_stm32.c.

◆ HAL_RTCEx_AlarmBEventCallback()

void HAL_RTCEx_AlarmBEventCallback ( RTC_HandleTypeDef *  rtc)

IRQ Handler for RTC B Alarm.

Definition at line 237 of file i2c_application_system_stm32.c.

◆ i2c_application_enter_low_power_state()

void i2c_application_enter_low_power_state ( void  )

Make the MCU enter its low power state.

This function is called by the i2c application when the MCU should power down.

Wait for the i2c interface to be idle, if not idle after retries, power down anyway!

Definition at line 192 of file i2c_application_system_stm32.c.

◆ i2c_application_is_periodic_wakeup()

bool i2c_application_is_periodic_wakeup ( void  )

Test if a periodic wake up has occurred.

Returns
true if a periodic wake up has occurred

Definition at line 223 of file i2c_application_system_stm32.c.

◆ i2c_application_set_periodic_wakeup()

void i2c_application_set_periodic_wakeup ( uint32_t  period_ms)

Set up a periodic timer used to wake up the system from sleep.

If the time_msec is set to zero the periodic wakeup will be disabled.

Parameters
period_msPeriod time in milliseconds

Definition at line 216 of file i2c_application_system_stm32.c.

◆ i2c_application_system_init()

void i2c_application_system_init ( void  )

Init the system.

This function is called by the i2c application during initialization.

Calling this function with wrong parameters will return HAL_ERROR Calling this function when the i2c module is in the wrong state return HAL_BUSY

Definition at line 110 of file i2c_application_system_stm32.c.

◆ i2c_application_system_reset()

void i2c_application_system_reset ( void  )

Reset the system.

This function is called by the i2c application when the system should be restarted.

Wait for the i2c interface to be idle, if not idle after retries, reset anyway!

Definition at line 129 of file i2c_application_system_stm32.c.

◆ i2c_application_system_set_generic_gpio_pin()

void i2c_application_system_set_generic_gpio_pin ( bool  enable)

Set the generic gpio pin output state.

This function is used to set the output state of the generic gpio pin

Parameters
[in]enablepin output is set to high level when enable is true

Definition at line 184 of file i2c_application_system_stm32.c.

◆ i2c_application_system_set_ready_pin()

void i2c_application_system_set_ready_pin ( bool  enable)

Set the ready pin state.

This function is used to set the state of the ready pin

Parameters
[in]enablepin is set to high level when enable is true

Definition at line 155 of file i2c_application_system_stm32.c.

◆ i2c_application_system_setup_generic_gpio_pin()

void i2c_application_system_setup_generic_gpio_pin ( bool  enable)

Setup the generic gpio pin.

This function is used to setup the generic gpio pin

Parameters
[in]enablepin is set to output driver when enable is true

Definition at line 163 of file i2c_application_system_stm32.c.

◆ i2c_application_system_test_wakeup_pin()

bool i2c_application_system_test_wakeup_pin ( void  )

Check if wakeup pin is high.

This function is used to test if the system should power down

Returns
true if wakeup pin is high

Definition at line 147 of file i2c_application_system_stm32.c.

◆ i2c_application_system_wait_for_interrupt()

void i2c_application_system_wait_for_interrupt ( void  )

Wait for interrupt to occur.

Definition at line 141 of file i2c_application_system_stm32.c.

◆ prepare_power_down()

static void prepare_power_down ( void  )
inlinestatic

Prepare power down state.

Definition at line 261 of file i2c_application_system_stm32.c.

◆ prepare_register_data()

static void prepare_register_data ( I2C_HandleTypeDef *  hi2c)
static

Helper function to prepare transmit of register data.

Parameters
[in]hi2cThe I2C handle

Calling this function with wrong parameters will return HAL_ERROR Calling this function when the i2c module is in the wrong state return HAL_BUSY

Definition at line 472 of file i2c_application_system_stm32.c.

◆ resume_power_down()

static void resume_power_down ( void  )
inlinestatic

Resume power down state.

Calling this function with wrong parameters will return HAL_ERROR Calling this function when the i2c module is in the wrong state return HAL_BUSY

Definition at line 313 of file i2c_application_system_stm32.c.

◆ rtc_set_next_wakeup_time()

static void rtc_set_next_wakeup_time ( void  )
static

Function for setting the next wakeup time from the RTC interrupt.

Definition at line 420 of file i2c_application_system_stm32.c.

◆ rtc_tick_to_time()

static void rtc_tick_to_time ( uint32_t  tick,
RTC_TimeTypeDef *  time 
)
static

Convert RTC ticks to RTC time.

Parameters
[in]tickrtc ticks in ms
[out]timeRTC time

Definition at line 398 of file i2c_application_system_stm32.c.

◆ wait_for_i2c_idle()

static void wait_for_i2c_idle ( void  )
static

Wait for I2C interface to be idle.

Definition at line 452 of file i2c_application_system_stm32.c.

Variable Documentation

◆ A121_SPI_HANDLE

SPI_HandleTypeDef A121_SPI_HANDLE

◆ gpio_banks

GPIO_TypeDef* gpio_banks[3U] = { GPIOA, GPIOB, GPIOH }
static

Definition at line 41 of file i2c_application_system_stm32.c.

◆ i2c_idle

volatile bool i2c_idle = true
static

Definition at line 44 of file i2c_application_system_stm32.c.

◆ i2c_slave_buffer

uint8_t i2c_slave_buffer[4]
static

Definition at line 43 of file i2c_application_system_stm32.c.

◆ low_power_saved_flatency

uint32_t low_power_saved_flatency
static

Definition at line 39 of file i2c_application_system_stm32.c.

◆ low_power_saved_gpio_bank

gpio_config_t low_power_saved_gpio_bank[3U] = { 0 }
static

Definition at line 40 of file i2c_application_system_stm32.c.

◆ low_power_saved_rcc_clkinitstruct

RCC_ClkInitTypeDef low_power_saved_rcc_clkinitstruct
static

Definition at line 38 of file i2c_application_system_stm32.c.

◆ low_power_saved_rcc_oscinitstruct

RCC_OscInitTypeDef low_power_saved_rcc_oscinitstruct
static

Definition at line 37 of file i2c_application_system_stm32.c.

◆ MODULE_I2C_HANDLE

I2C_HandleTypeDef MODULE_I2C_HANDLE

◆ MODULE_RTC_HANDLE

RTC_HandleTypeDef MODULE_RTC_HANDLE

◆ MODULE_UART1_HANDLE

UART_HandleTypeDef MODULE_UART1_HANDLE

◆ MODULE_UART2_HANDLE

UART_HandleTypeDef MODULE_UART2_HANDLE

◆ rtc_period_time_ms

uint32_t rtc_period_time_ms = 0
static

Definition at line 46 of file i2c_application_system_stm32.c.

◆ rtc_wakeup_triggered

volatile bool rtc_wakeup_triggered = false
static

Definition at line 47 of file i2c_application_system_stm32.c.