Data Structures | Macros | Enumerations | Functions | Variables
i2c_application_system_stm32.c File Reference
`#include <inttypes.h>`
`#include <stdbool.h>`
`#include <stddef.h>`
`#include <stdio.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
#define | I2C_ADDRESS_FLOATING (0x52)
#define | I2C_ADDRESS_LOW (0x51)
#define | I2C_ADDRESS_HIGH (0x53)

## Enumerations
enum | pin_state_t { PIN_STATE_LOW, PIN_STATE_HIGH, PIN_STATE_FLOATING }

## 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...
static uint32_t | get_i2c_address (void)
| Helper function to get i2c address from I2C_ADDRESS pin pin state. More...
static pin_state_t | get_pin_state (GPIO_TypeDef *gpio_port, uint32_t gpio_pin)
| Helper function to the state (HIGH/LOW/FLOATING) of a gpio pin. 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 21 of file i2c_application_system_stm32.c.

## ◆ I2C_ADDRESS_FLOATING

#define I2C_ADDRESS_FLOATING (0x52)

Definition at line 23 of file i2c_application_system_stm32.c.

## ◆ I2C_ADDRESS_HIGH

#define I2C_ADDRESS_HIGH (0x53)

Definition at line 25 of file i2c_application_system_stm32.c.

## ◆ I2C_ADDRESS_LOW

#define I2C_ADDRESS_LOW (0x51)

Definition at line 24 of file i2c_application_system_stm32.c.

## ◆ I2C_SLAVE_BUFFER_SIZE

#define I2C_SLAVE_BUFFER_SIZE 4

Definition at line 18 of file i2c_application_system_stm32.c.

## ◆ WAIT_FOR_IDLE_RETRIES

#define WAIT_FOR_IDLE_RETRIES 10U

Definition at line 19 of file i2c_application_system_stm32.c.

## ◆ WAIT_FOR_IDLE_RETRY_INTERNAL_MS

#define WAIT_FOR_IDLE_RETRY_INTERNAL_MS 10U

Definition at line 20 of file i2c_application_system_stm32.c.

## Enumeration Type Documentation

## ◆ pin_state_t

enum pin_state_t

Enumerator
PIN_STATE_LOW |
PIN_STATE_HIGH |
PIN_STATE_FLOATING |

Definition at line 27 of file i2c_application_system_stm32.c.

## Function Documentation

## ◆ disable_interrupts()

static void disable_interrupts | ( | void |  | ) |
| inline static

Disable interrupts.

Definition at line 268 of file i2c_application_system_stm32.c.

## ◆ enable_interrupts()

static void enable_interrupts | ( | void |  | ) |
| inline static

Enable interrupts.

Definition at line 275 of file i2c_application_system_stm32.c.

## ◆ get_i2c_address()

static uint32_t get_i2c_address | ( | void |  | ) |
| static

Helper function to get i2c address from I2C_ADDRESS pin pin state.

Returns
  The i2c address to use

Definition at line 503 of file i2c_application_system_stm32.c.

## ◆ get_pin_state()

static pin_state_t get_pin_state | ( | GPIO_TypeDef * | gpio_port,
|  | uint32_t | gpio_pin
| ) |  |
| static

Helper function to the state (HIGH/LOW/FLOATING) of a gpio pin.

Parameters

  [in] | gpio_port | The GPIO port
  [in] | gpio_pin | The GPIO pin

Returns
  The pin state

Definition at line 525 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 373 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 569 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 652 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 637 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 604 of file i2c_application_system_stm32.c.

## ◆ HAL_I2C_SlaveTxCpltCallback()

void HAL_I2C_SlaveTxCpltCallback | ( | I2C_HandleTypeDef * | hi2c | ) |

Definition at line 598 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 262 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 215 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 244 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_ms | Period time in milliseconds

Definition at line 238 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 130 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 158 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] | enable | pin output is set to high level when enable is true

Definition at line 208 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] | enable | pin is set to high level when enable is true

Definition at line 181 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] | enable | pin is set to output driver when enable is true

Definition at line 188 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 174 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 169 of file i2c_application_system_stm32.c.

## ◆ prepare_power_down()

static void prepare_power_down | ( | void |  | ) |
| inline static

Prepare power down state.

Definition at line 282 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] | hi2c | The 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 487 of file i2c_application_system_stm32.c.

## ◆ resume_power_down()

static void resume_power_down | ( | void |  | ) |
| inline static

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 333 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 437 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] | tick | rtc ticks in ms
  [out] | time | RTC time

Definition at line 416 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 468 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 54 of file i2c_application_system_stm32.c.

## ◆ i2c_idle

volatile bool i2c_idle = true
| static

Definition at line 57 of file i2c_application_system_stm32.c.

## ◆ i2c_slave_buffer

uint8_t i2c_slave_buffer[4]
| static

Definition at line 56 of file i2c_application_system_stm32.c.

## ◆ low_power_saved_flatency

uint32_t low_power_saved_flatency
| static

Definition at line 52 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 53 of file i2c_application_system_stm32.c.

## ◆ low_power_saved_rcc_clkinitstruct

RCC_ClkInitTypeDef low_power_saved_rcc_clkinitstruct
| static

Definition at line 51 of file i2c_application_system_stm32.c.

## ◆ low_power_saved_rcc_oscinitstruct

RCC_OscInitTypeDef low_power_saved_rcc_oscinitstruct
| static

Definition at line 50 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 59 of file i2c_application_system_stm32.c.

## ◆ rtc_wakeup_triggered

volatile bool rtc_wakeup_triggered = false
| static

Definition at line 60 of file i2c_application_system_stm32.c.