acc_integration_stm32.c File Reference
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "main.h"
#include "acc_integration.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 RTC_MAX_TIME_MS   (24*60*60*1000)
 Maximum RTC time before wrapping occurs. More...
 

Functions

static void disable_interrupts (void)
 
static void enable_interrupts (void)
 
static uint32_t rtc_time_to_tick (RTC_TimeTypeDef *time)
 Convert RTC time to RTC ticks. More...
 
static void rtc_tick_to_time (uint32_t tick, RTC_TimeTypeDef *time)
 Convert RTC ticks to RTC time. More...
 
static uint32_t get_rtc_tick (void)
 Get RTC ticks based on current RTC time. More...
 
static void update_system_tick (uint32_t rtc_ticks_pre_sleep, uint32_t rtc_ticks_post_sleep)
 Update system tick based on RTC ticks during sleep. More...
 
static void rtc_set_next_wakeup_time (void)
 Function for setting the next wakeup time from the RTC interrupt. More...
 
static void enable_gpio_port_clock (GPIO_TypeDef *gpio_bank, bool enable)
 Set the port clock status for a given GPIO bank. More...
 
static bool get_gpio_port_clock (GPIO_TypeDef *gpio_bank)
 Get the port clock status for a given GPIO bank. More...
 
static void save_gpio_bank (GPIO_TypeDef *gpio_bank, gpio_config_t *config)
 Save all registers for a given GPIO bank including the original GPIO port clock. More...
 
static void restore_gpio_bank (GPIO_TypeDef *gpio_bank, gpio_config_t *config)
 Restore all registers for a given GPIO bank. More...
 
static void gpio_suspend (void)
 Suspend GPIO driver. More...
 
static void gpio_resume (void)
 Resume GPIO driver. More...
 
void HAL_RTC_AlarmAEventCallback (RTC_HandleTypeDef *rtc)
 IRQ Handler for RTC Alarm. More...
 
void HAL_RTCEx_WakeUpTimerEventCallback (RTC_HandleTypeDef *rtc)
 IRQ Handler for RTC Wakeup. More...
 
static void acc_integration_enable_wake_up (uint32_t time_usec)
 
static void acc_integration_disable_wake_up (void)
 
void acc_integration_sleep_ms (uint32_t time_msec)
 Sleep for a specified number of milliseconds. More...
 
void acc_integration_sleep_us (uint32_t time_usec)
 Sleep for a specified number of microseconds. More...
 
void acc_integration_set_periodic_wakeup (uint32_t time_msec)
 Set up a periodic timer used to wake up the system from sleep. More...
 
static void acc_integration_prepare_stop_1 (void)
 
static void acc_integration_resume_stop_1 (void)
 
void acc_integration_sleep_until_periodic_wakeup (void)
 Put the system in sleep until the periodic timer triggers. More...
 
uint32_t acc_integration_get_time (void)
 Get current time. More...
 
void * acc_integration_mem_alloc (size_t size)
 Allocate dynamic memory. More...
 
void * acc_integration_mem_calloc (size_t nmemb, size_t size)
 Allocate dynamic memory. More...
 
void acc_integration_mem_free (void *ptr)
 Free dynamic memory. More...
 

Variables

volatile bool rtc_alarm_triggered = false
 Set to true when RTC alarm interrupt has triggered. More...
 
volatile bool rtc_wakeup_triggered = false
 Set to true when RTC wakeup interrupt has triggered. More...
 
static uint32_t periodic_sleep_time_ms = 0
 Variable that holds the periodic wakeup time. More...
 
static gpio_config_t saved_gpio_status [3]
 GPIO status of the system before going to sleep. More...
 
RTC_HandleTypeDef MODULE_RTC_HANDLE
 Handles for the blocks to control. More...
 
SPI_HandleTypeDef A121_SPI_HANDLE
 
I2C_HandleTypeDef MODULE_I2C_HANDLE
 
UART_HandleTypeDef MODULE_UART1_HANDLE
 
UART_HandleTypeDef MODULE_UART2_HANDLE
 
__IO uint32_t uwTick
 
static RCC_OscInitTypeDef prepare_saved_rcc_oscinitstruct
 
static RCC_ClkInitTypeDef prepare_saved_rcc_clkinitstruct
 
static uint32_t prepare_saved_flatency
 
static HAL_UART_StateTypeDef prepare_saved_uart1_rx_state
 
static HAL_I2C_StateTypeDef prepare_saved_i2c_state
 

Macro Definition Documentation

◆ RTC_MAX_TIME_MS

#define RTC_MAX_TIME_MS   (24*60*60*1000)

Maximum RTC time before wrapping occurs.

Definition at line 19 of file acc_integration_stm32.c.

Function Documentation

◆ acc_integration_disable_wake_up()

static void acc_integration_disable_wake_up ( void  )
static

Definition at line 492 of file acc_integration_stm32.c.

◆ acc_integration_enable_wake_up()

static void acc_integration_enable_wake_up ( uint32_t  time_usec)
static

Definition at line 456 of file acc_integration_stm32.c.

◆ acc_integration_get_time()

uint32_t acc_integration_get_time ( void  )

Get current time.

It is important that this value wraps correctly and uses all bits. I.e. it should count upwards to 2^32 - 1 and then 0 again.

Returns
Current time as milliseconds

Definition at line 626 of file acc_integration_stm32.c.

◆ acc_integration_mem_alloc()

void* acc_integration_mem_alloc ( size_t  size)

Allocate dynamic memory.

Parameters
[in]sizeThe bytesize of the reuested memory block
Returns
Returns either NULL or a unique pointer to a memory block

Definition at line 632 of file acc_integration_stm32.c.

◆ acc_integration_mem_calloc()

void* acc_integration_mem_calloc ( size_t  nmemb,
size_t  size 
)

Allocate dynamic memory.

Allocate an array of nmemb elements of size bytes each.

Parameters
[in]nmembThe number of elements in the array
[in]sizeThe bytesize of the element
Returns
Returns either NULL or a unique pointer to a memory block

Definition at line 638 of file acc_integration_stm32.c.

◆ acc_integration_mem_free()

void acc_integration_mem_free ( void *  ptr)

Free dynamic memory.

Parameters
[in]ptrA pointer to the memory space to be freed

Definition at line 644 of file acc_integration_stm32.c.

◆ acc_integration_prepare_stop_1()

static void acc_integration_prepare_stop_1 ( void  )
static

Definition at line 538 of file acc_integration_stm32.c.

◆ acc_integration_resume_stop_1()

static void acc_integration_resume_stop_1 ( void  )
static

Definition at line 556 of file acc_integration_stm32.c.

◆ acc_integration_set_periodic_wakeup()

void acc_integration_set_periodic_wakeup ( uint32_t  time_msec)

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

This function will start a periodic timer with the specified time. This is useful when the drift of the wakeup interval should be kept at a minimum.

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

Parameters
time_msecTime in milliseconds

Definition at line 531 of file acc_integration_stm32.c.

◆ acc_integration_sleep_ms()

void acc_integration_sleep_ms ( uint32_t  time_msec)

Sleep for a specified number of milliseconds.

Parameters
time_msecTime in milliseconds to sleep

Definition at line 501 of file acc_integration_stm32.c.

◆ acc_integration_sleep_until_periodic_wakeup()

void acc_integration_sleep_until_periodic_wakeup ( void  )

Put the system in sleep until the periodic timer triggers.

The periodic timer must be started using acc_integration_set_periodic_wakeup prior to invoking this function. The target specific implementation of this function will determine the sleep depth based on the set sleep interval and it will be a trade-off between wake-up latency and power consumption.

Definition at line 590 of file acc_integration_stm32.c.

◆ acc_integration_sleep_us()

void acc_integration_sleep_us ( uint32_t  time_usec)

Sleep for a specified number of microseconds.

Parameters
time_usecTime in microseconds to sleep

Definition at line 507 of file acc_integration_stm32.c.

◆ disable_interrupts()

static void disable_interrupts ( void  )
inlinestatic

Definition at line 55 of file acc_integration_stm32.c.

◆ enable_gpio_port_clock()

static void enable_gpio_port_clock ( GPIO_TypeDef *  gpio_bank,
bool  enable 
)
static

Set the port clock status for a given GPIO bank.

Parameters
[in]gpio_bankGPIO port clock to set
[in]enableTrue to enable clock, False to disable

Definition at line 248 of file acc_integration_stm32.c.

◆ enable_interrupts()

static void enable_interrupts ( void  )
inlinestatic

Definition at line 63 of file acc_integration_stm32.c.

◆ get_gpio_port_clock()

static bool get_gpio_port_clock ( GPIO_TypeDef *  gpio_bank)
static

Get the port clock status for a given GPIO bank.

Parameters
[in]gpio_bankGPIO bank to save
Returns
True if the GPIO port clock is enabled

Definition at line 292 of file acc_integration_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 160 of file acc_integration_stm32.c.

◆ gpio_resume()

static void gpio_resume ( void  )
static

Resume GPIO driver.

Returns
Status

Definition at line 424 of file acc_integration_stm32.c.

◆ gpio_suspend()

static void gpio_suspend ( void  )
static

Suspend GPIO driver.

Set all GPIO pins in the lowest power consuming state according to AN4899 and disable all the port clocks.

Definition at line 362 of file acc_integration_stm32.c.

◆ HAL_RTC_AlarmAEventCallback()

void HAL_RTC_AlarmAEventCallback ( RTC_HandleTypeDef *  rtc)

IRQ Handler for RTC Alarm.

Definition at line 436 of file acc_integration_stm32.c.

◆ HAL_RTCEx_WakeUpTimerEventCallback()

void HAL_RTCEx_WakeUpTimerEventCallback ( RTC_HandleTypeDef *  rtc)

IRQ Handler for RTC Wakeup.

Definition at line 448 of file acc_integration_stm32.c.

◆ restore_gpio_bank()

static void restore_gpio_bank ( GPIO_TypeDef *  gpio_bank,
gpio_config_t config 
)
static

Restore all registers for a given GPIO bank.

Parameters
[in]gpio_bankGPIO bank to restore
[in]configVariable that contains all the saved GPIO bank registers

Definition at line 338 of file acc_integration_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 208 of file acc_integration_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 133 of file acc_integration_stm32.c.

◆ rtc_time_to_tick()

static uint32_t rtc_time_to_tick ( RTC_TimeTypeDef *  time)
static

Convert RTC time to RTC ticks.

Parameters
[in]timeRTC time
Returns
rtc ticks in ms

Definition at line 102 of file acc_integration_stm32.c.

◆ save_gpio_bank()

static void save_gpio_bank ( GPIO_TypeDef *  gpio_bank,
gpio_config_t config 
)
static

Save all registers for a given GPIO bank including the original GPIO port clock.

The GPIO port clock needs to be enabled before calling this function

Parameters
[in]gpio_bankGPIO bank to save
[out]configVariable for storing the GPIO bank registers

Definition at line 321 of file acc_integration_stm32.c.

◆ update_system_tick()

static void update_system_tick ( uint32_t  rtc_ticks_pre_sleep,
uint32_t  rtc_ticks_post_sleep 
)
static

Update system tick based on RTC ticks during sleep.

Parameters
[in]rtc_ticks_pre_sleeprtc ticks before sleep was entered
[in]rtc_ticks_post_sleeprtc ticks after sleep was exited

Definition at line 191 of file acc_integration_stm32.c.

Variable Documentation

◆ A121_SPI_HANDLE

SPI_HandleTypeDef A121_SPI_HANDLE

◆ MODULE_I2C_HANDLE

I2C_HandleTypeDef MODULE_I2C_HANDLE

◆ MODULE_RTC_HANDLE

RTC_HandleTypeDef MODULE_RTC_HANDLE

Handles for the blocks to control.

◆ MODULE_UART1_HANDLE

UART_HandleTypeDef MODULE_UART1_HANDLE

◆ MODULE_UART2_HANDLE

UART_HandleTypeDef MODULE_UART2_HANDLE

◆ periodic_sleep_time_ms

uint32_t periodic_sleep_time_ms = 0
static

Variable that holds the periodic wakeup time.

Definition at line 37 of file acc_integration_stm32.c.

◆ prepare_saved_flatency

uint32_t prepare_saved_flatency
static

Definition at line 90 of file acc_integration_stm32.c.

◆ prepare_saved_i2c_state

HAL_I2C_StateTypeDef prepare_saved_i2c_state
static

Definition at line 93 of file acc_integration_stm32.c.

◆ prepare_saved_rcc_clkinitstruct

RCC_ClkInitTypeDef prepare_saved_rcc_clkinitstruct
static

Definition at line 89 of file acc_integration_stm32.c.

◆ prepare_saved_rcc_oscinitstruct

RCC_OscInitTypeDef prepare_saved_rcc_oscinitstruct
static

Definition at line 88 of file acc_integration_stm32.c.

◆ prepare_saved_uart1_rx_state

HAL_UART_StateTypeDef prepare_saved_uart1_rx_state
static

Definition at line 92 of file acc_integration_stm32.c.

◆ rtc_alarm_triggered

volatile bool rtc_alarm_triggered = false

Set to true when RTC alarm interrupt has triggered.

Definition at line 25 of file acc_integration_stm32.c.

◆ rtc_wakeup_triggered

volatile bool rtc_wakeup_triggered = false

Set to true when RTC wakeup interrupt has triggered.

Definition at line 31 of file acc_integration_stm32.c.

◆ saved_gpio_status

gpio_config_t saved_gpio_status[3]
static

GPIO status of the system before going to sleep.

Definition at line 74 of file acc_integration_stm32.c.

◆ uwTick

__IO uint32_t uwTick