HPM SDK
HPMicro Software Development Kit
hpm_ptpc_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_PTPC_DRV_H
9 #define HPM_PTPC_DRV_H
10 
11 #include "hpm_common.h"
12 #include "hpm_ptpc_regs.h"
13 
21 #define PTPC_EVENT_COMPARE0_MASK PTPC_INT_STS_COMP_INT_STS0_MASK
22 #define PTPC_EVENT_CAPTURE0_MASK PTPC_INT_STS_CAPTURE_INT_STS0_MASK
23 #define PTPC_EVENT_PPS0_MASK PTPC_INT_STS_PPS_INT_STS0_MASK
24 #define PTPC_EVENT_COMPARE1_MASK PTPC_INT_STS_COMP_INT_STS1_MASK
25 #define PTPC_EVENT_CAPTURE1_MASK PTPC_INT_STS_CAPTURE_INT_STS1_MASK
26 #define PTPC_EVENT_PPS1_MASK PTPC_INT_STS_PPS_INT_STS1_MASK
27 
28 #define PTPC_MAX_NS_COUNTER (0x3B9ACA00UL)
29 
37 
48 
52 typedef enum ptpc_counting_mode {
56 
60 typedef struct {
64  bool capture_keep;
65  uint32_t src_frequency;
67 
68 #ifdef __cplusplus
69 extern "C" {
70 #endif
71 
79 static inline void ptpc_set_ns_counter_rollover(PTPC_Type *ptr, uint8_t index,
81 {
84 }
85 
92 static inline void ptpc_enable_capture_keep(PTPC_Type *ptr, uint8_t index)
93 {
95 }
96 
103 static inline void ptpc_disable_capture_keep(PTPC_Type *ptr, uint8_t index)
104 {
106 }
107 
115 static inline void ptpc_set_ns_counter_update_type(PTPC_Type *ptr, uint8_t index, bool coarse_update)
116 {
117  ptr->PTPC[index].CTRL0 = (ptr->PTPC[index].CTRL0 & ~PTPC_PTPC_CTRL0_FINE_COARSE_SEL_MASK)
118  | PTPC_PTPC_CTRL0_FINE_COARSE_SEL_SET(coarse_update);
119 }
120 
128 static inline void ptpc_set_ns_counter_step(PTPC_Type *ptr, uint8_t index, uint32_t ns_step)
129 {
130  ptr->PTPC[index].CTRL1 = PTPC_PTPC_CTRL1_SS_INCR_SET(ns_step);
131 }
132 
140 static inline void ptpc_set_second_update(PTPC_Type *ptr, uint8_t index, uint32_t sec)
141 {
143 }
144 
155 hpm_stat_t ptpc_set_ns_update(PTPC_Type *ptr, uint8_t index, uint32_t ns, ptpc_counting_mode mode);
156 
165 static inline uint32_t ptpc_get_timestamp_second(PTPC_Type *ptr, uint8_t index)
166 {
167  return PTPC_PTPC_TIMEH_TIMESTAMP_HIGH_GET(ptr->PTPC[index].TIMEH);
168 }
169 
178 static inline uint32_t ptpc_get_timestamp_ns(PTPC_Type *ptr, uint8_t index)
179 {
180  return PTPC_PTPC_TIMEL_TIMESTAMP_LOW_GET(ptr->PTPC[index].TIMEL);
181 }
182 
191 static inline void ptpc_config_compare(PTPC_Type *ptr, uint8_t index, uint32_t sec, uint32_t ns)
192 {
193  ptr->PTPC[index].TARH = PTPC_PTPC_TARH_TARGET_TIME_HIGH_SET(sec);
194  ptr->PTPC[index].TARL = PTPC_PTPC_TARL_TARGET_TIME_LOW_SET(ns);
195  ptr->PTPC[index].CTRL0 |= PTPC_PTPC_CTRL0_COMP_EN_MASK;
196 }
197 
205 static inline void ptpc_update_timestamp_second(PTPC_Type *ptr, uint8_t index, uint32_t sec)
206 {
208 }
209 
218 static inline void ptpc_update_timestamp_ns(PTPC_Type *ptr, uint8_t index, uint32_t ns, ptpc_counting_mode mode)
219 {
221 }
222 
229 static inline void ptpc_enable_timer(PTPC_Type *ptr, uint8_t index)
230 {
232 }
233 
240 static inline void ptpc_disable_timer(PTPC_Type *ptr, uint8_t index)
241 {
243 }
244 
252 static inline void ptpc_config_capture(PTPC_Type *ptr, uint8_t index,
254 {
255  ptr->PTPC[index].CTRL0 = (ptr->PTPC[index].CTRL0 & ~(PTPC_PTPC_CTRL0_CAPT_SNAP_POS_EN_MASK
257 }
258 
267 static inline uint32_t ptpc_get_capture_ns(PTPC_Type *ptr, uint8_t index)
268 {
270 }
271 
280 static inline uint32_t ptpc_get_capture_second(PTPC_Type *ptr, uint8_t index)
281 {
283 }
284 
291 static inline void ptpc_clear_irq_status(PTPC_Type *ptr, uint32_t mask)
292 {
293  ptr->INT_STS = mask;
294 }
295 
303 static inline uint32_t ptpc_get_irq_status(PTPC_Type *ptr)
304 {
305  return ptr->INT_STS;
306 }
307 
314 static inline void ptpc_irq_disable(PTPC_Type *ptr, uint32_t mask)
315 {
316  ptr->INT_EN &= ~mask;
317 }
318 
325 static inline void ptpc_irq_enable(PTPC_Type *ptr, uint32_t mask)
326 {
327  ptr->INT_EN |= mask;
328 }
329 
339 hpm_stat_t ptpc_init(PTPC_Type *ptr, uint8_t index, ptpc_config_t *config);
340 
350 hpm_stat_t ptpc_set_timer_output(PTPC_Type *ptr, uint8_t can_index, bool use_ptpc1);
351 
359 
367 void ptpc_init_timer(PTPC_Type *ptr, uint8_t index);
368 
380 hpm_stat_t ptpc_init_timer_with_initial(PTPC_Type *ptr, uint8_t index, uint32_t sec, uint32_t ns, ptpc_counting_mode mode);
381 
392 hpm_stat_t ptpc_update_timer(PTPC_Type *ptr, uint8_t index, uint32_t sec, uint32_t ns, ptpc_counting_mode mode);
393 
404 hpm_stat_t ptpc_set_pps(PTPC_Type *ptr, uint8_t index, uint8_t p);
405 
406 #ifdef __cplusplus
407 }
408 #endif
413 #endif /* HPM_PTPC_DRV_H */
#define PTPC_PTPC_TS_UPDTL_ADD_SUB_SET(x)
Definition: hpm_ptpc_regs.h:180
#define PTPC_PTPC_CAPT_SNAPH_CAPT_SNAP_HIGH_GET(x)
Definition: hpm_ptpc_regs.h:243
#define PTPC_PTPC_CTRL0_COMP_EN_MASK
Definition: hpm_ptpc_regs.h:86
#define PTPC_PTPC_TS_UPDTH_SEC_UPDATE_SET(x)
Definition: hpm_ptpc_regs.h:169
#define PTPC_PTPC_TIMEH_TIMESTAMP_HIGH_GET(x)
Definition: hpm_ptpc_regs.h:150
#define PTPC_PTPC_CTRL0_FINE_COARSE_SEL_MASK
Definition: hpm_ptpc_regs.h:117
#define PTPC_PTPC_TIMEL_TIMESTAMP_LOW_GET(x)
Definition: hpm_ptpc_regs.h:159
#define PTPC_PTPC_CAPT_SNAPL_CAPT_SNAP_LOW_GET(x)
Definition: hpm_ptpc_regs.h:253
#define PTPC_PTPC_CTRL0_CAPT_SNAP_POS_EN_MASK
Definition: hpm_ptpc_regs.h:67
#define PTPC_PTPC_CTRL0_CAPT_SNAP_NEG_EN_MASK
Definition: hpm_ptpc_regs.h:76
#define PTPC_PTPC_CTRL0_SUBSEC_DIGITAL_ROLLOVER_MASK
Definition: hpm_ptpc_regs.h:45
#define PTPC_PTPC_CTRL1_SS_INCR_SET(x)
Definition: hpm_ptpc_regs.h:140
#define PTPC_PTPC_TARH_TARGET_TIME_HIGH_SET(x)
Definition: hpm_ptpc_regs.h:211
#define PTPC_PTPC_CTRL0_SUBSEC_DIGITAL_ROLLOVER_SET(x)
Definition: hpm_ptpc_regs.h:47
#define PTPC_PTPC_CTRL0_FINE_COARSE_SEL_SET(x)
Definition: hpm_ptpc_regs.h:119
#define PTPC_PTPC_TS_UPDTL_NS_UPDATE_SET(x)
Definition: hpm_ptpc_regs.h:189
#define PTPC_PTPC_CTRL0_TIMER_ENABLE_MASK
Definition: hpm_ptpc_regs.h:126
#define PTPC_PTPC_CTRL0_CAPT_SNAP_KEEP_MASK
Definition: hpm_ptpc_regs.h:57
#define PTPC_PTPC_TARL_TARGET_TIME_LOW_SET(x)
Definition: hpm_ptpc_regs.h:221
uint32_t hpm_stat_t
Definition: hpm_common.h:123
void ptpc_get_default_config(PTPC_Type *ptr, ptpc_config_t *config)
Get default config.
Definition: hpm_ptpc_drv.c:13
static void ptpc_enable_timer(PTPC_Type *ptr, uint8_t index)
Enable timer.
Definition: hpm_ptpc_drv.h:229
hpm_stat_t ptpc_init(PTPC_Type *ptr, uint8_t index, ptpc_config_t *config)
Initialize PTPC module.
Definition: hpm_ptpc_drv.c:23
ptpc_counting_mode
Capture trigger types.
Definition: hpm_ptpc_drv.h:52
ptpc_capture_trigger_type
Capture trigger types.
Definition: hpm_ptpc_drv.h:41
static void ptpc_set_second_update(PTPC_Type *ptr, uint8_t index, uint32_t sec)
Set Second portion update value.
Definition: hpm_ptpc_drv.h:140
hpm_stat_t ptpc_init_timer_with_initial(PTPC_Type *ptr, uint8_t index, uint32_t sec, uint32_t ns, ptpc_counting_mode mode)
Initialize timer with initial values.
Definition: hpm_ptpc_drv.c:87
static void ptpc_enable_capture_keep(PTPC_Type *ptr, uint8_t index)
Enable capture keep.
Definition: hpm_ptpc_drv.h:92
static void ptpc_set_ns_counter_rollover(PTPC_Type *ptr, uint8_t index, ptpc_ns_counter_rollover_type_t rollover)
Configure ns counter rollover mode.
Definition: hpm_ptpc_drv.h:79
void ptpc_init_timer(PTPC_Type *ptr, uint8_t index)
Initialize timer to count from 0.
Definition: hpm_ptpc_drv.c:80
static void ptpc_set_ns_counter_step(PTPC_Type *ptr, uint8_t index, uint32_t ns_step)
Set ns counter increment step.
Definition: hpm_ptpc_drv.h:128
ptpc_ns_counter_rollover_type
Counter types.
Definition: hpm_ptpc_drv.h:33
enum ptpc_capture_trigger_type ptpc_capture_trigger_type_t
Capture trigger types.
static uint32_t ptpc_get_timestamp_second(PTPC_Type *ptr, uint8_t index)
Get current timestamp Second portion.
Definition: hpm_ptpc_drv.h:165
enum ptpc_ns_counter_rollover_type ptpc_ns_counter_rollover_type_t
Counter types.
static void ptpc_update_timestamp_ns(PTPC_Type *ptr, uint8_t index, uint32_t ns, ptpc_counting_mode mode)
Update Ns portion.
Definition: hpm_ptpc_drv.h:218
hpm_stat_t ptpc_set_ns_update(PTPC_Type *ptr, uint8_t index, uint32_t ns, ptpc_counting_mode mode)
Set ns portion update value.
Definition: hpm_ptpc_drv.c:60
static void ptpc_set_ns_counter_update_type(PTPC_Type *ptr, uint8_t index, bool coarse_update)
Update ns counter update type.
Definition: hpm_ptpc_drv.h:115
hpm_stat_t ptpc_set_pps(PTPC_Type *ptr, uint8_t index, uint8_t p)
Set period of pulse generated per second.
Definition: hpm_ptpc_drv.c:97
static void ptpc_disable_capture_keep(PTPC_Type *ptr, uint8_t index)
Disable capture keep.
Definition: hpm_ptpc_drv.h:103
hpm_stat_t ptpc_update_timer(PTPC_Type *ptr, uint8_t index, uint32_t sec, uint32_t ns, ptpc_counting_mode mode)
Update timestamp counter.
Definition: hpm_ptpc_drv.c:70
static void ptpc_disable_timer(PTPC_Type *ptr, uint8_t index)
Disable timer.
Definition: hpm_ptpc_drv.h:240
static void ptpc_config_capture(PTPC_Type *ptr, uint8_t index, ptpc_capture_trigger_type_t trigger)
Config capture trigger event.
Definition: hpm_ptpc_drv.h:252
static uint32_t ptpc_get_timestamp_ns(PTPC_Type *ptr, uint8_t index)
Get timestamp ns portion.
Definition: hpm_ptpc_drv.h:178
hpm_stat_t ptpc_set_timer_output(PTPC_Type *ptr, uint8_t can_index, bool use_ptpc1)
Set PTPC output destination.
Definition: hpm_ptpc_drv.c:51
static void ptpc_config_compare(PTPC_Type *ptr, uint8_t index, uint32_t sec, uint32_t ns)
Config compare.
Definition: hpm_ptpc_drv.h:191
static uint32_t ptpc_get_capture_ns(PTPC_Type *ptr, uint8_t index)
Get snapped ns value.
Definition: hpm_ptpc_drv.h:267
static void ptpc_irq_enable(PTPC_Type *ptr, uint32_t mask)
Enable interrupt with mask.
Definition: hpm_ptpc_drv.h:325
static void ptpc_clear_irq_status(PTPC_Type *ptr, uint32_t mask)
Clear interrupt status.
Definition: hpm_ptpc_drv.h:291
static uint32_t ptpc_get_irq_status(PTPC_Type *ptr)
Get interrupt status.
Definition: hpm_ptpc_drv.h:303
static uint32_t ptpc_get_capture_second(PTPC_Type *ptr, uint8_t index)
Get captured second.
Definition: hpm_ptpc_drv.h:280
static void ptpc_irq_disable(PTPC_Type *ptr, uint32_t mask)
Disable interrupt with mask.
Definition: hpm_ptpc_drv.h:314
static void ptpc_update_timestamp_second(PTPC_Type *ptr, uint8_t index, uint32_t sec)
Update Second portion.
Definition: hpm_ptpc_drv.h:205
@ ptpc_counting_decrement
Definition: hpm_ptpc_drv.h:54
@ ptpc_counting_increment
Definition: hpm_ptpc_drv.h:53
@ ptpc_capture_trigger_none
Definition: hpm_ptpc_drv.h:42
@ ptpc_capture_trigger_on_failing_edge
Definition: hpm_ptpc_drv.h:44
@ ptpc_capture_trigger_on_rising_edge
Definition: hpm_ptpc_drv.h:43
@ ptpc_capture_trigger_on_both_edges
Definition: hpm_ptpc_drv.h:45
@ ptpc_ns_counter_rollover_binary
Definition: hpm_ptpc_drv.h:34
@ ptpc_ns_counter_rollover_digital
Definition: hpm_ptpc_drv.h:35
Definition: hpm_ptpc_regs.h:12
__R uint32_t CAPT_SNAPH
Definition: hpm_ptpc_regs.h:25
__R uint32_t TIMEL
Definition: hpm_ptpc_regs.h:17
__RW uint32_t CAPT_SNAPL
Definition: hpm_ptpc_regs.h:26
__RW uint32_t CTRL0
Definition: hpm_ptpc_regs.h:14
__RW uint32_t TARH
Definition: hpm_ptpc_regs.h:21
__RW uint32_t TARL
Definition: hpm_ptpc_regs.h:22
__R uint32_t TIMEH
Definition: hpm_ptpc_regs.h:16
__RW uint32_t CTRL1
Definition: hpm_ptpc_regs.h:15
__RW uint32_t INT_EN
Definition: hpm_ptpc_regs.h:31
__RW uint32_t TS_UPDTL
Definition: hpm_ptpc_regs.h:19
__W uint32_t INT_STS
Definition: hpm_ptpc_regs.h:30
__RW uint32_t TS_UPDTH
Definition: hpm_ptpc_regs.h:18
struct PTPC_Type::@317 PTPC[2]
Timer config.
Definition: hpm_ptpc_drv.h:60
bool coarse_increment
Definition: hpm_ptpc_drv.h:63
bool capture_keep
Definition: hpm_ptpc_drv.h:64
uint32_t src_frequency
Definition: hpm_ptpc_drv.h:65
ptpc_capture_trigger_type_t capture_trigger
Definition: hpm_ptpc_drv.h:61
ptpc_ns_counter_rollover_type_t ns_rollover_mode
Definition: hpm_ptpc_drv.h:62