~ruther/stm32h747i-disco-usb-image-viewer

cb4fa519d2c542d36e961a7982e079cc5e6eb00b — Rutherther 4 months ago a42a0f9
fix: ensure register access are inline
2 files changed, 39 insertions(+), 48 deletions(-)

M include/registers.h
D src/registers.c
M include/registers.h => include/registers.h +39 -9
@@ 10,7 10,12 @@
 * @param[in] pos The position to start writing to. First @see data param is shifted by this.
 * @param[in] mask The mask to and the data with, ie. if you want only two bits, set to 0x3.
 */
extern inline void reg_write_bits_pos(volatile uint32_t *reg, uint32_t data, uint8_t pos, uint32_t mask);
inline void __attribute__((always_inline))
reg_write_bits_pos(volatile uint32_t *reg, uint32_t data, uint8_t pos,
                   uint32_t mask) {
  *reg &= ~(mask << pos);
  *reg |= (data & mask) << pos;
}

/**
 * @brief Writes data into the register.


@@ 18,7 23,11 @@ extern inline void reg_write_bits_pos(volatile uint32_t *reg, uint32_t data, uin
 * @param[in] data The data to write to the whole register.
 * @param[in] mask The mask to and the data with, ie. if you want only two bits, set to 0x3.
 */
extern inline void reg_write_bits(volatile uint32_t *reg, uint32_t data, uint32_t mask);
inline void __attribute__((always_inline)) reg_write_bits(volatile uint32_t *reg, uint32_t data,
                                  uint32_t mask) {
  *reg &= ~(mask);
  *reg |= (data & mask);
}

/**
 * @brief Write ones on the given position in register.


@@ 26,14 35,20 @@ extern inline void reg_write_bits(volatile uint32_t *reg, uint32_t data, uint32_
 * @param[in] pos The position to start writing to. First @see mask param is shifted by this.
 * @param[in] mask The mask of bits to write ones to, ie. if you want only two bits, set to 0x3.
 */
extern inline void reg_set_bits_pos(volatile uint32_t *reg, uint8_t pos, uint32_t mask);
inline void __attribute__((always_inline)) reg_set_bits_pos(volatile uint32_t *reg, uint8_t pos,
                                    uint32_t mask) {
  *reg |= mask << pos;
}

/**
 * @brief Write ones into register.
 * @param[in,out] reg The address to change.
 * @param[in] mask The mask of bits to write ones to.
 */
extern inline void reg_set_bits(volatile uint32_t *reg, uint32_t mask);
inline void __attribute__((always_inline)) reg_set_bits(volatile uint32_t *reg,
                                                        uint32_t mask) {
  *reg |= mask;
}

/**
 * @brief Toggle bits in the register, starting from @see pos.


@@ 42,7 57,10 @@ extern inline void reg_set_bits(volatile uint32_t *reg, uint32_t mask);
 * @param[in] pos The position to shift the @see mask by.
 * @param[in] mask The mask saying what bits to toggle (1), from the @see pos.
 */
extern inline void reg_toggle_bits_pos(volatile uint32_t *reg, uint8_t pos, uint32_t mask);
inline void __attribute__((always_inline)) reg_toggle_bits_pos(volatile uint32_t *reg, uint8_t pos,
                                       uint32_t mask) {
  *reg ^= (mask << pos);
}

/**
 * @brief Toggle bits in the register.


@@ 50,7 68,10 @@ extern inline void reg_toggle_bits_pos(volatile uint32_t *reg, uint8_t pos, uint
 * @param[in,out] reg Address of the register to toggle bits in.
 * @param[in] mask The mask saying what bits to toggle (1).
 */
extern inline void reg_toggle_bits(volatile uint32_t *reg, uint32_t mask);
inline void __attribute__((always_inline))
reg_toggle_bits(volatile uint32_t *reg, uint32_t mask) {
  *reg ^= mask;
}

/**
 * @brief Writes zeros on the given position in register.


@@ 58,14 79,20 @@ extern inline void reg_toggle_bits(volatile uint32_t *reg, uint32_t mask);
 * @param[in] pos The position to start writing to. First the mask is shifted by this.
 * @param[in] mask The mask of bits to write ones to, ie. if you want only two bits, set to 0x3.
 */
extern inline void reg_clear_bits_pos(volatile uint32_t *reg, uint8_t pos, uint32_t mask);
inline void __attribute__((always_inline))
reg_clear_bits_pos(volatile uint32_t *reg, uint8_t pos, uint32_t mask) {
  *reg ^= ~(mask << pos);
}

/**
 * @brief Write zeros on into register.
 * @param[in,out] reg The address to change.
 * @param[in] mask The mask of bits to write ones to.
 */
extern inline void reg_clear_bits(volatile uint32_t *reg, uint32_t mask);
inline void __attribute__((always_inline))
reg_clear_bits(volatile uint32_t *reg, uint32_t mask) {
  *reg &= ~mask;
}

/**
 * @brief Get bits from the register on the given position.


@@ 75,6 102,9 @@ extern inline void reg_clear_bits(volatile uint32_t *reg, uint32_t mask);
 * @param[in] mask The mask to apply on the resulting data
 * @return The read data.
 */
extern inline uint32_t reg_read_bits_pos(volatile uint32_t *reg, uint8_t pos, uint32_t mask);
inline uint32_t __attribute__((always_inline))
reg_read_bits_pos(volatile uint32_t *reg, uint8_t pos, uint32_t mask) {
  return ((*reg) >> pos) & mask;
}

#endif // REGISTERS_H

D src/registers.c => src/registers.c +0 -39
@@ 1,39 0,0 @@
#include <stdint.h>

void reg_write_bits_pos(volatile uint32_t *reg, uint32_t data, uint8_t pos, uint32_t mask) {
  *reg &= ~(mask << pos);
  *reg |= (data & mask) << pos;
}

void reg_write_bits(volatile uint32_t *reg, uint32_t data, uint32_t mask) {
  *reg &= ~(mask);
  *reg |= (data & mask);
}

void reg_set_bits_pos(volatile uint32_t *reg, uint8_t pos, uint32_t mask) {
  *reg |= mask << pos;
}

void reg_set_bits(volatile uint32_t *reg, uint32_t mask) {
  *reg |= mask;
}

void reg_toggle_bits_pos(volatile uint32_t *reg, uint8_t pos, uint32_t mask) {
  *reg ^= (mask << pos);
}

void reg_toggle_bits(volatile uint32_t *reg, uint32_t mask) {
  *reg ^= mask;
}

uint32_t reg_read_bits_pos(volatile uint32_t *reg, uint8_t pos, uint32_t mask) {
  return ((*reg) >> pos) & mask;
}

void reg_clear_bits_pos(volatile uint32_t *reg, uint8_t pos, uint32_t mask) {
  *reg ^= ~(mask << pos);
}

void reg_clear_bits(volatile uint32_t *reg, uint8_t pos, uint32_t mask) {
  *reg &= ~mask;
}

Do not follow this link