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

ref: 9cb6ab52fc2b86956e5f74a8630a3aaf79411f29 stm32h747i-disco-usb-image-viewer/include/registers.h -rw-r--r-- 3.9 KiB
9cb6ab52 — Rutherther feat: vos0, system clock at 480 MHz 3 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <stdint.h>

#ifndef REGISTERS_H
#define REGISTERS_H

/**
 * @brief Writes data on the given position in register.
 * @param[in,out] reg The address to change.
 * @param[in] data The data to write on pos.
 * @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.
 */
inline void __attribute__((always_inline))
reg_write_bits_pos(volatile uint32_t *reg, uint32_t data, uint8_t pos,
                   uint32_t mask) {
  uint32_t val = *reg;
  val &= ~(mask << pos);
  val |= (data & mask) << pos;
  *reg = val;
}

/**
 * @brief Writes data into the register.
 * @param[in,out] reg The address to change.
 * @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.
 */
inline void __attribute__((always_inline)) reg_write_bits(volatile uint32_t *reg, uint32_t data,
                                  uint32_t mask) {
  uint32_t val = *reg;
  val &= ~(mask);
  val |= (data & mask);
  *reg = val;
}

/**
 * @brief Write ones on the given position in register.
 * @param[in,out] reg The address to change.
 * @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.
 */
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.
 */
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.
 * @details Toggles bits where one is written to the mask, but shifted by @see pos.
 * @param[in,out] reg Address of the register to toggle bits in.
 * @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.
 */
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.
 * @details Toggles bits where one is written to the mask.
 * @param[in,out] reg Address of the register to toggle bits in.
 * @param[in] mask The mask saying what bits to toggle (1).
 */
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.
 * @param[in,out] reg The address to change.
 * @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.
 */
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.
 */
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.
 * @details The obtained value starts from @see pos, and is anded with @see mask.
 * @param[in,out] reg The register address to read
 * @param[in] pos The position to shift data by.
 * @param[in] mask The mask to apply on the resulting data
 * @return The read data.
 */
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
Do not follow this link