Loading...
Searching...
No Matches
tim.h
1
8
9// ALLOW FORMATTING
10#ifndef TIM_H
11#define TIM_H
12
13#include "stm32f439xx.h"
14#include "stdint.h"
15#include "stdbool.h"
16
17// Macro definitions for pin config literals
18//
19
20#define TIM_CONFIG_DEFAULT \
21 (TIM_Config) { \
22 .ARPE = false, \
23 .DIR = TIM_DIR_UP, \
24 .OPM = false, \
25 .URS = TIM_URS_ANY, \
26 .UDIS = false, \
27 .CCDS = TIM_CCDS_UPDATE, \
28 .CCDE = {}, \
29 .UDE = false, \
30 .CCIE = {}, \
31 .UIE = false \
32 }
33
34// clang-format off
35
36#define TIM_CR1_CONFIG_MASK ( \
37 TIM_CR1_ARPE \
38 | TIM_CR1_DIR \
39 | TIM_CR1_OPM \
40 | TIM_CR1_URS \
41 | TIM_CR1_UDIS \
42)
43
44#define TIM_CR2_CONFIG_MASK ( \
45 TIM_CR2_CCDS \
46)
47
48#define TIM_DIER_CONFIG_MASK ( \
49 TIM_DIER_CC4DE \
50 | TIM_DIER_CC3DE \
51 | TIM_DIER_CC2DE \
52 | TIM_DIER_CC1DE \
53 | TIM_DIER_UDE \
54 | TIM_DIER_CC4IE \
55 | TIM_DIER_CC3IE \
56 | TIM_DIER_CC2IE \
57 | TIM_DIER_CC1IE \
58 | TIM_DIER_UDE \
59)
60
61#define TIM_CCMR_CONFIG_MASK 0xFF
62
63// clang-format on
64
71
76typedef enum {
77 TIM_CHANNEL1,
78 TIM_CHANNEL2,
79 TIM_CHANNEL3,
80 TIM_CHANNEL4
81} TIM_Channel;
82
87typedef enum {
88 TIM_DIR_UP,
89 TIM_DIR_DOWN
90} TIM_Direction;
91
96typedef enum {
97 TIM_URS_ANY,
98 TIM_URS_WRAP
99} TIM_UpdateSource;
100
105typedef enum {
106 TIM_CCDS_CCX,
107 TIM_CCDS_UPDATE
108} TIM_DmaSelect;
109
114typedef enum {
115 TIM_CCM_SELECT_OUTPUT,
116 TIM_CCM_SELECT_IN1,
117 TIM_CCM_SELECT_IN2,
118 TIM_CCM_SELECT_TRC
119} TIM_CCM_Selection;
120
125typedef enum {
126 TIM_CCM_OUTPUT_MODE_FROZEN,
127 TIM_CCM_OUTPUT_MODE_ACTIVE,
128 TIM_CCM_OUTPUT_MODE_INACTIVE,
129 TIM_CCM_OUTPUT_MODE_TOGGLE,
130 TIM_CCM_OUTPUT_MODE_FORCE_INACTIVE,
131 TIM_CCM_OUTPUT_MODE_FORCE_ACTIVE,
132 TIM_CCM_OUTPUT_MODE_PWM_1,
133 TIM_CCM_OUTPUT_MODE_PWM_2
134} TIM_CCM_OutputMode;
135
140typedef struct {
141 TIM_CCM_Selection S : 2;
142 bool FE : 1;
143 bool PE : 1;
144 TIM_CCM_OutputMode M : 3;
145 bool CE : 1;
147
152typedef struct {
153 bool ARPE;
154 TIM_Direction DIR;
155 bool OPM;
156 TIM_UpdateSource URS;
157 bool UDIS;
158 TIM_DmaSelect CCDS;
159 bool CCDE[4];
160 bool CCIE[4];
161 TIM_CCMR_Output OC[4];
162 bool UDE;
163 bool UIE;
164} TIM_Config;
165
170typedef struct TIM {
171 TIM_TypeDef *interface;
173 void (*startCounter)(struct TIM *tim);
174 bool (*pollUpdate)(struct TIM *tim);
175 bool (*setTimingPeriod)(struct TIM *tim, float period);
176 bool (*pollCompare)(struct TIM *tim, TIM_Channel channel);
177 bool (*setTimingPWM)(struct TIM *tim, TIM_Channel channel, float duty);
178 void (*updateConfig)(struct TIM *tim, TIM_Config *config);
179} TIM_t;
180
181TIM_t TIM_init(TIM_TypeDef *interface, TIM_Config *config);
182void TIM_startCounter(TIM_t *tim);
183void TIM_updateConfig(TIM_t *tim, TIM_Config *config);
184bool TIM_setTimingPWM(TIM_t *tim, TIM_Channel channel, float duty);
185bool TIM_pollCompare(TIM_t *tim, TIM_Channel channel);
186bool TIM_setTimingPeriod(TIM_t *tim, float period);
187bool TIM_pollUpdate(TIM_t *tim);
188
190#endif
void(* updateConfig)(struct TIM *tim, TIM_Config *config)
TIM configuration update method.
Definition tim.h:178
TIM_Config config
Configuration parameters for the TIM peripheral.
Definition tim.h:172
void(* startCounter)(struct TIM *tim)
TIM configuration update method.
Definition tim.h:173
TIM_TypeDef * interface
Pointer to TIM interface struct.
Definition tim.h:171
bool TIM_setTimingPeriod(TIM_t *tim, float period)
Definition tim.c:193
void TIM_updateConfig(TIM_t *tim, TIM_Config *config)
Update TIM peripheral configuration.
Definition tim.c:282
bool TIM_pollUpdate(TIM_t *tim)
Definition tim.c:175
TIM_t TIM_init(TIM_TypeDef *interface, TIM_Config *config)
Initialiser for an SPI device interface.
Definition tim.c:30
bool TIM_setTimingPWM(TIM_t *tim, TIM_Channel channel, float duty)
Definition tim.c:259
bool TIM_pollCompare(TIM_t *tim, TIM_Channel channel)
Definition tim.c:241
void TIM_startCounter(TIM_t *tim)
Definition tim.c:160
TIM configuration struct.
Definition tim.h:152
Struct definition for TIM interface. Provides the interface for API consumers to interact with the TI...
Definition tim.h:170