HPM SDK
HPMicro Software Development Kit
hpm_gptmr_drv.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021 HPMicro
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #ifndef HPM_GPTMR_DRV_H
9 #define HPM_GPTMR_DRV_H
10 #include "hpm_common.h"
11 #include "hpm_gptmr_regs.h"
12 
23 #define GPTMR_CH_CMP_IRQ_MASK(ch, cmp) (1 << (ch * 4 + 2 + cmp))
24 #define GPTMR_CH_CAP_IRQ_MASK(ch) (1 << (ch * 4 + 1))
25 #define GPTMR_CH_RLD_IRQ_MASK(ch) (1 << (ch * 4))
26 
30 #define GPTMR_CH_CMP_STAT_MASK(ch, cmp) (1 << (ch * 4 + 2 + cmp))
31 #define GPTMR_CH_CAP_STAT_MASK(ch) (1 << (ch * 4 + 1))
32 #define GPTMR_CH_RLD_STAT_MASK(ch) (1 << (ch * 4))
33 
37 #define GPTMR_CH_GCR_SWSYNCT_MASK(ch) (1 << ch)
38 
42 #define GPTMR_CH_CMP_COUNT (2U)
43 
47 typedef enum gptmr_synci_edge {
53 
57 typedef enum gptmr_work_mode {
64 
75 
79 typedef enum gptmr_counter_type {
86 
90 typedef struct gptmr_channel_config {
95  uint32_t reload;
102 
103 #ifdef __cplusplus
104 extern "C" {
105 #endif
106 
116 static inline void gptmr_channel_enable(GPTMR_Type *ptr, uint8_t ch_index, bool enable)
117 {
118  ptr->CHANNEL[ch_index].CR = (ptr->CHANNEL[ch_index].CR
120  | GPTMR_CHANNEL_CR_CMPEN_SET(enable);
121 }
122 
129 static inline void gptmr_channel_reset_count(GPTMR_Type *ptr, uint8_t ch_index)
130 {
131  ptr->CHANNEL[ch_index].CR |= GPTMR_CHANNEL_CR_CNTRST_MASK;
132  ptr->CHANNEL[ch_index].CR &= ~GPTMR_CHANNEL_CR_CNTRST_MASK;
133 }
134 
142 static inline void gptmr_channel_update_count(GPTMR_Type *ptr,
143  uint8_t ch_index,
144  uint32_t value)
145 {
146  if (value > 0) {
147  value--;
148  }
150  ptr->CHANNEL[ch_index].CR |= GPTMR_CHANNEL_CR_CNTUPT_MASK;
151 }
152 
161  uint8_t ch_index,
162  gptmr_synci_edge_t edge)
163 {
164  ptr->CHANNEL[ch_index].CR = (ptr->CHANNEL[ch_index].CR
167 }
168 
179  uint8_t ch_index,
180  bool enable)
181 {
182  ptr->CHANNEL[ch_index].CR = (ptr->CHANNEL[ch_index].CR
184 }
185 
193 static inline uint32_t gptmr_channel_get_counter(GPTMR_Type *ptr,
194  uint8_t ch_index,
195  gptmr_counter_type_t capture)
196 {
197  uint32_t value;
198  switch (capture) {
201  break;
204  break;
207  break;
210  break;
211  default:
213  break;
214  }
215  return value;
216 }
217 
224 static inline void gptmr_trigger_channel_software_sync(GPTMR_Type *ptr, uint32_t ch_index_mask)
225 {
226  ptr->GCR = ch_index_mask;
227 }
228 
235 static inline void gptmr_enable_irq(GPTMR_Type *ptr, uint32_t irq_mask)
236 {
237  ptr->IRQEN |= irq_mask;
238 }
239 
246 static inline void gptmr_disable_irq(GPTMR_Type *ptr, uint32_t irq_mask)
247 {
248  ptr->IRQEN &= ~irq_mask;
249 }
250 
257 static inline bool gptmr_check_status(GPTMR_Type *ptr, uint32_t mask)
258 {
259  return (ptr->SR & mask) == mask;
260 }
261 
268 static inline void gptmr_clear_status(GPTMR_Type *ptr, uint32_t mask)
269 {
270  ptr->SR = mask;
271 }
272 
279 static inline uint32_t gptmr_get_status(GPTMR_Type *ptr)
280 {
281  return ptr->SR;
282 }
283 
290 static inline void gptmr_start_counter(GPTMR_Type *ptr, uint8_t ch_index)
291 {
292  ptr->CHANNEL[ch_index].CR |= GPTMR_CHANNEL_CR_CEN_MASK;
293 }
294 
301 static inline void gptmr_stop_counter(GPTMR_Type *ptr, uint8_t ch_index)
302 {
303  ptr->CHANNEL[ch_index].CR &= ~GPTMR_CHANNEL_CR_CEN_MASK;
304 }
305 
312 static inline void gptmr_enable_cmp_output(GPTMR_Type *ptr, uint8_t ch_index)
313 {
314  ptr->CHANNEL[ch_index].CR |= GPTMR_CHANNEL_CR_CMPEN_MASK;
315 }
316 
323 static inline void gptmr_disable_cmp_output(GPTMR_Type *ptr, uint8_t ch_index)
324 {
325  ptr->CHANNEL[ch_index].CR &= ~GPTMR_CHANNEL_CR_CMPEN_MASK;
326 }
327 
335 static inline void gptmr_channel_set_capmode(GPTMR_Type *ptr, uint8_t ch_index, gptmr_work_mode_t mode)
336 {
337  ptr->CHANNEL[ch_index].CR = (ptr->CHANNEL[ch_index].CR & ~GPTMR_CHANNEL_CR_CAPMODE_MASK) | GPTMR_CHANNEL_CR_CAPMODE_SET(mode);
338 }
339 
347 static inline gptmr_work_mode_t gptmr_channel_get_capmode(GPTMR_Type *ptr, uint8_t ch_index)
348 {
349  return GPTMR_CHANNEL_CR_CAPMODE_GET(ptr->CHANNEL[ch_index].CR);
350 }
351 
360 static inline void gptmr_update_cmp(GPTMR_Type *ptr, uint8_t ch_index, uint8_t cmp_index, uint32_t cmp)
361 {
362  if (cmp > 0) {
363  cmp--;
364  }
365  ptr->CHANNEL[ch_index].CMP[cmp_index] = GPTMR_CHANNEL_CMP_CMP_SET(cmp);
366 }
367 
375 static inline uint32_t gptmr_channel_get_reload(GPTMR_Type *ptr, uint8_t ch_index)
376 {
377  return ptr->CHANNEL[ch_index].RLD;
378 }
379 
387 static inline void gptmr_channel_config_update_reload(GPTMR_Type *ptr, uint8_t ch_index, uint32_t reload)
388 {
389  if (reload > 0) {
390  reload--;
391  }
392  ptr->CHANNEL[ch_index].RLD = GPTMR_CHANNEL_RLD_RLD_SET(reload);
393 }
394 
403 {
404  return GPTMR_CHANNEL_CR_DMASEL_GET(ptr->CHANNEL[ch_index].CR);
405 }
406 
420  uint8_t ch_index,
421  gptmr_channel_config_t *config,
422  bool enable);
423 
431 
436 #ifdef __cplusplus
437 }
438 #endif
439 
440 #endif /* HPM_GPTMR_DRV_H */
uint32_t hpm_stat_t
Definition: hpm_common.h:119
static uint32_t gptmr_get_status(GPTMR_Type *ptr)
gptmr get status
Definition: hpm_gptmr_drv.h:279
static bool gptmr_check_status(GPTMR_Type *ptr, uint32_t mask)
gptmr check status
Definition: hpm_gptmr_drv.h:257
static uint32_t gptmr_channel_get_counter(GPTMR_Type *ptr, uint8_t ch_index, gptmr_counter_type_t capture)
gptmr channel get counter value
Definition: hpm_gptmr_drv.h:193
gptmr_dma_request_event
GPTMR DMA request event.
Definition: hpm_gptmr_drv.h:68
static uint32_t gptmr_channel_get_reload(GPTMR_Type *ptr, uint8_t ch_index)
gptmr channel get reload
Definition: hpm_gptmr_drv.h:375
struct gptmr_channel_config gptmr_channel_config_t
GPTMR channel config.
static void gptmr_channel_enable_dma_request(GPTMR_Type *ptr, uint8_t ch_index, bool enable)
gptmr channel enable dma request
Definition: hpm_gptmr_drv.h:178
enum gptmr_dma_request_event gptmr_dma_request_event_t
GPTMR DMA request event.
static void gptmr_channel_enable(GPTMR_Type *ptr, uint8_t ch_index, bool enable)
gptmr channel enable
Definition: hpm_gptmr_drv.h:116
gptmr_synci_edge
GPTMR synci valid edge.
Definition: hpm_gptmr_drv.h:47
enum gptmr_work_mode gptmr_work_mode_t
GPTMR work mode.
static gptmr_work_mode_t gptmr_channel_get_capmode(GPTMR_Type *ptr, uint8_t ch_index)
gptmr channel get capmode
Definition: hpm_gptmr_drv.h:347
static void gptmr_enable_irq(GPTMR_Type *ptr, uint32_t irq_mask)
gptmr enable irq
Definition: hpm_gptmr_drv.h:235
static void gptmr_start_counter(GPTMR_Type *ptr, uint8_t ch_index)
gptmr channel start counter
Definition: hpm_gptmr_drv.h:290
static void gptmr_trigger_channel_software_sync(GPTMR_Type *ptr, uint32_t ch_index_mask)
gptmr trigger channel software sync
Definition: hpm_gptmr_drv.h:224
gptmr_work_mode
GPTMR work mode.
Definition: hpm_gptmr_drv.h:57
enum gptmr_synci_edge gptmr_synci_edge_t
GPTMR synci valid edge.
static void gptmr_update_cmp(GPTMR_Type *ptr, uint8_t ch_index, uint8_t cmp_index, uint32_t cmp)
gptmr channel update comparator
Definition: hpm_gptmr_drv.h:360
void gptmr_channel_get_default_config(GPTMR_Type *ptr, gptmr_channel_config_t *config)
gptmr channel get default config
Definition: hpm_gptmr_drv.c:10
static void gptmr_disable_cmp_output(GPTMR_Type *ptr, uint8_t ch_index)
gptmr channel disable compare output
Definition: hpm_gptmr_drv.h:323
static gptmr_dma_request_event_t gptmr_channel_get_dma_request_event(GPTMR_Type *ptr, uint8_t ch_index)
gptmr channel get dma request event
Definition: hpm_gptmr_drv.h:402
hpm_stat_t gptmr_channel_config(GPTMR_Type *ptr, uint8_t ch_index, gptmr_channel_config_t *config, bool enable)
gptmr channel config
Definition: hpm_gptmr_drv.c:27
static void gptmr_channel_config_update_reload(GPTMR_Type *ptr, uint8_t ch_index, uint32_t reload)
gptmr channel update reload
Definition: hpm_gptmr_drv.h:387
static void gptmr_channel_select_synci_valid_edge(GPTMR_Type *ptr, uint8_t ch_index, gptmr_synci_edge_t edge)
gptmr channel slect synci valid edge
Definition: hpm_gptmr_drv.h:160
static void gptmr_channel_set_capmode(GPTMR_Type *ptr, uint8_t ch_index, gptmr_work_mode_t mode)
gptmr channel set capmode
Definition: hpm_gptmr_drv.h:335
static void gptmr_stop_counter(GPTMR_Type *ptr, uint8_t ch_index)
gptmr channel stop counter
Definition: hpm_gptmr_drv.h:301
static void gptmr_enable_cmp_output(GPTMR_Type *ptr, uint8_t ch_index)
gptmr channel enable compare output
Definition: hpm_gptmr_drv.h:312
enum gptmr_counter_type gptmr_counter_type_t
GPTMR counter type.
#define GPTMR_CH_CMP_COUNT
GPTMR one channel support output comparator count.
Definition: hpm_gptmr_drv.h:42
static void gptmr_clear_status(GPTMR_Type *ptr, uint32_t mask)
gptmr clear status
Definition: hpm_gptmr_drv.h:268
gptmr_counter_type
GPTMR counter type.
Definition: hpm_gptmr_drv.h:79
static void gptmr_channel_reset_count(GPTMR_Type *ptr, uint8_t ch_index)
gptmr channel reset counter
Definition: hpm_gptmr_drv.h:129
static void gptmr_disable_irq(GPTMR_Type *ptr, uint32_t irq_mask)
gptmr disable irq
Definition: hpm_gptmr_drv.h:246
static void gptmr_channel_update_count(GPTMR_Type *ptr, uint8_t ch_index, uint32_t value)
gptmr channel update counter
Definition: hpm_gptmr_drv.h:142
@ gptmr_dma_request_on_cmp1
Definition: hpm_gptmr_drv.h:70
@ gptmr_dma_request_on_cmp0
Definition: hpm_gptmr_drv.h:69
@ gptmr_dma_request_on_input_signal_toggle
Definition: hpm_gptmr_drv.h:71
@ gptmr_dma_request_on_reload
Definition: hpm_gptmr_drv.h:72
@ gptmr_dma_request_disabled
Definition: hpm_gptmr_drv.h:73
@ gptmr_synci_edge_falling
Definition: hpm_gptmr_drv.h:49
@ gptmr_synci_edge_both
Definition: hpm_gptmr_drv.h:51
@ gptmr_synci_edge_none
Definition: hpm_gptmr_drv.h:48
@ gptmr_synci_edge_rising
Definition: hpm_gptmr_drv.h:50
@ gptmr_work_mode_capture_at_rising_edge
Definition: hpm_gptmr_drv.h:59
@ gptmr_work_mode_no_capture
Definition: hpm_gptmr_drv.h:58
@ gptmr_work_mode_capture_at_both_edge
Definition: hpm_gptmr_drv.h:61
@ gptmr_work_mode_measure_width
Definition: hpm_gptmr_drv.h:62
@ gptmr_work_mode_capture_at_falling_edge
Definition: hpm_gptmr_drv.h:60
@ gptmr_counter_type_rising_edge
Definition: hpm_gptmr_drv.h:80
@ gptmr_counter_type_measured_duty_cycle
Definition: hpm_gptmr_drv.h:83
@ gptmr_counter_type_normal
Definition: hpm_gptmr_drv.h:84
@ gptmr_counter_type_measured_period
Definition: hpm_gptmr_drv.h:82
@ gptmr_counter_type_falling_edge
Definition: hpm_gptmr_drv.h:81
#define GPTMR_CHANNEL_RLD_RLD_SET(x)
Definition: hpm_gptmr_regs.h:234
#define GPTMR_CHANNEL_CAPNEG_CAPNEG_SHIFT
Definition: hpm_gptmr_regs.h:265
#define GPTMR_CHANNEL_CNTUPTVAL_CNTUPTVAL_SET(x)
Definition: hpm_gptmr_regs.h:245
#define GPTMR_CHANNEL_CR_CMPEN_MASK
Definition: hpm_gptmr_regs.h:151
#define GPTMR_CHANNEL_CAPPRD_CAPPRD_SHIFT
Definition: hpm_gptmr_regs.h:275
#define GPTMR_CHANNEL_CAPPOS_CAPPOS_SHIFT
Definition: hpm_gptmr_regs.h:255
#define GPTMR_CHANNEL_CNT_COUNTER_SHIFT
Definition: hpm_gptmr_regs.h:295
#define GPTMR_CHANNEL_CR_DMAEN_SET(x)
Definition: hpm_gptmr_regs.h:177
#define GPTMR_CHANNEL_CMP_CMP_SET(x)
Definition: hpm_gptmr_regs.h:223
#define GPTMR_CHANNEL_CR_CEN_MASK
Definition: hpm_gptmr_regs.h:128
#define GPTMR_CHANNEL_CR_DMASEL_GET(x)
Definition: hpm_gptmr_regs.h:168
#define GPTMR_CHANNEL_CR_CAPMODE_GET(x)
Definition: hpm_gptmr_regs.h:213
#define GPTMR_CHANNEL_CAPNEG_CAPNEG_MASK
Definition: hpm_gptmr_regs.h:264
#define GPTMR_CHANNEL_CAPDTY_MEAS_HIGH_SHIFT
Definition: hpm_gptmr_regs.h:285
#define GPTMR_CHANNEL_CAPPOS_CAPPOS_MASK
Definition: hpm_gptmr_regs.h:254
#define GPTMR_CHANNEL_CR_CNTUPT_MASK
Definition: hpm_gptmr_regs.h:40
#define GPTMR_CHANNEL_CR_SYNCIFEN_MASK
Definition: hpm_gptmr_regs.h:108
#define GPTMR_CHANNEL_CR_CAPMODE_SET(x)
Definition: hpm_gptmr_regs.h:212
#define GPTMR_CHANNEL_CNT_COUNTER_MASK
Definition: hpm_gptmr_regs.h:294
#define GPTMR_CHANNEL_CR_CMPEN_SET(x)
Definition: hpm_gptmr_regs.h:153
#define GPTMR_CHANNEL_CR_DMAEN_MASK
Definition: hpm_gptmr_regs.h:175
#define GPTMR_CHANNEL_CR_CAPMODE_MASK
Definition: hpm_gptmr_regs.h:210
#define GPTMR_CHANNEL_CR_SYNCIREN_MASK
Definition: hpm_gptmr_regs.h:118
#define GPTMR_CHANNEL_CR_CNTRST_MASK
Definition: hpm_gptmr_regs.h:87
#define GPTMR_CHANNEL_CAPPRD_CAPPRD_MASK
Definition: hpm_gptmr_regs.h:274
#define GPTMR_CHANNEL_CAPDTY_MEAS_HIGH_MASK
Definition: hpm_gptmr_regs.h:284
Definition: hpm_gptmr_regs.h:12
__RW uint32_t CNTUPTVAL
Definition: hpm_gptmr_regs.h:17
__R uint32_t CAPNEG
Definition: hpm_gptmr_regs.h:20
struct GPTMR_Type::@412 CHANNEL[4]
__R uint32_t CAPPRD
Definition: hpm_gptmr_regs.h:21
__RW uint32_t SR
Definition: hpm_gptmr_regs.h:27
__R uint32_t CAPPOS
Definition: hpm_gptmr_regs.h:19
__RW uint32_t CR
Definition: hpm_gptmr_regs.h:14
__R uint32_t CNT
Definition: hpm_gptmr_regs.h:23
__RW uint32_t IRQEN
Definition: hpm_gptmr_regs.h:28
__RW uint32_t RLD
Definition: hpm_gptmr_regs.h:16
__R uint32_t CAPDTY
Definition: hpm_gptmr_regs.h:22
__RW uint32_t CMP[2]
Definition: hpm_gptmr_regs.h:15
__RW uint32_t GCR
Definition: hpm_gptmr_regs.h:29
GPTMR channel config.
Definition: hpm_gptmr_drv.h:90
gptmr_synci_edge_t synci_edge
Definition: hpm_gptmr_drv.h:93
bool enable_software_sync
Definition: hpm_gptmr_drv.h:99
gptmr_dma_request_event_t dma_request_event
Definition: hpm_gptmr_drv.h:92
bool cmp_initial_polarity_high
Definition: hpm_gptmr_drv.h:96
bool enable_sync_follow_previous_channel
Definition: hpm_gptmr_drv.h:98
bool enable_cmp_output
Definition: hpm_gptmr_drv.h:97
uint32_t cmp[(2U)]
Definition: hpm_gptmr_drv.h:94
uint32_t reload
Definition: hpm_gptmr_drv.h:95
gptmr_work_mode_t mode
Definition: hpm_gptmr_drv.h:91
bool debug_mode
Definition: hpm_gptmr_drv.h:100