23 #include "./hal/hal_power.h" 30 #define CNT1_BASE (0x100) 31 #define CNT2_BASE (0x200) 32 #define CNT3_BASE (0x300) 34 #define CNT_CAPTURE_SHIFTIN_MODE 0x0 35 #define CNT_WAVEFORM_SHITOUT_MODE 0x1 36 #define CNT_COUNT_MODE 0x0 37 #define CNT_SHIFT_MODE 0x2 38 #define CNT_SWITCH_DISABLE 0x0 39 #define CNT_SWITCH_ENABLE 0x4 41 #define CNT_CAPTURE_MODE (CNT_CAPTURE_SHIFTIN_MODE | CNT_COUNT_MODE) 42 #define CNT_WAVAFORM_MODE (CNT_WAVEFORM_SHITOUT_MODE | CNT_COUNT_MODE) 43 #define CNT_SHIFTIN_MODE (CNT_CAPTURE_SHIFTIN_MODE | CNT_SHIFT_MODE) 44 #define CNT_SHIFTOUT_MODE (CNT_WAVEFORM_SHITOUT_MODE | CNT_SHIFT_MODE) 47 #define CNT_ENABLE 0x1UL 48 #define CNT_CLK_INV_ENABLE 0x1000UL 49 #define CNT_CLK_ENABLE 0x2000UL 50 #define CNT_CLK_DIV8 0 51 #define CNT_CLK_DIV4 1 52 #define CNT_CLK_DIV2 2 54 #define CNT_EXT_CLK0 4 55 #define CNT_EXT_CLK1 5 56 #define CNT_EXT_CLK2 6 60 #define CNT_COUNTER_CLK0 7 61 #define CNT_COUNTER_CLK1 8 62 #define CNT_COUNTER_CLK2 9 65 #define CNT_EXT_DIN_A 1 66 #define CNT_EXT_DIN_B 2 67 #define CNT_GLOBAL_START_TRIGGER 3 68 #define CNT_SINGLE_START_TRIGGER 4 69 #define CNT_GLOBAL_STOP_TRIGGER 5 70 #define CNT_SINGLE_STOP_TRIGGER 6 71 #define CNT_INNER_DIN0 11 72 #define CNT_INNER_DIN1 12 73 #define CNT_INNER_DIN2 13 74 #define CNT_INNER_DIN3 14 75 #define CNT_START_SIGNLE_SHIFT 0 76 #define CNT_STOP_SIGNLE_SHIFT 8 77 #define CNT_DIN0_SHIFT 16 78 #define CNT_DIN1_SHIFT 24 79 #define CNT_RISING_EDGE 0 80 #define CNT_FALLING_EDGE 0x10UL 81 #define CNT_BOTH_EDGE 0x20UL 84 #define CNT_SNAP_SHADOW_MASK 0x1UL 85 #define CNT_SNAP_CAPTURE_A_MASK 0x2UL 86 #define CNT_SNAP_CAPTURE_B_MASK 0x4UL 87 #define CNT_SNAP_SHIFTIN_DATA_MASK 0x8UL 88 #define CNT_SNAP_CLEAR_MASK 0x10000UL 91 #define CNT_A2_KEEP_VALUE 0x1UL 92 #define CNT_A2_RESET_VALUE 0x0UL 93 #define CNT_A2_STOP_COUNTER 0x2UL 94 #define CNT_A2_RESTART_COUNTER 0x0UL 95 #define CNT_B2_KEEP_VALUE 0x4UL 96 #define CNT_B2_RESET_VALUE 0x0UL 97 #define CNT_B2_STOP_COUNTER 0x8UL 98 #define CNT_B2_RESTART_COUNTER 0x0UL 99 #define CNT_DOUT_A_RESET_VALUE_SHIFT 4 100 #define CNT_DOUT_B_RESET_VALUE_SHIFT 5 103 #define CNT_OUT_BYPASS_A 0x10000UL //Bypass pin A operation(AND, OR and NOT) 104 #define CNT_OUT_BYPASS_B 0x20000UL //Bypass pin B operation(AND, OR and NOT) 105 #define CNT_OPT_A_AND 0x0 106 #define CNT_OPT_A_OR 0x1 107 #define CNT_OPT_A_XOR 0x2 108 #define CNT_OPT_B_AND 0x0 109 #define CNT_OPT_B_OR 0x10 110 #define CNT_OPT_B_XOR 0x20 113 #define CNT_IN_BYPASS_A 0x1UL 114 #define CNT_IN_BYPASS_B 0x2UL 117 #define CNT_SHIFTMODE_POINT_EN 0x10000 120 #define CNT_WAVEFORM_SHIFTOUT_CNT_SHIFT 0 121 #define CNT_CAPTURE_SHIFTIN_CNT_SHIFT 8 122 #define CNT_WAVEFORM_SHIFTOUT_AUTO_ENABLE 0x10000UL 123 #define CNT_CAPTURE_SHIFTIN_AUTO_ENABLE 0x1000000UL 128 #define CNT2_SHIFT 16 129 #define CNT3_SHIFT 24 130 #define CNT_A_UPDATE 1 131 #define CNT_B_UPDATE 0x2 132 #define CNT_OVERFLOW 0x4 133 #define CNT_SHIFTIN_COMPLETE 0x8 134 #define CNT_SHIFTOUT_COMPLETE 0x10 135 #define CNT_WAVEFORM_STOP 0x20 //counter value reach a2 or b2 136 #define CNT_SHIFTIN_CAPTURE_END 0x40 //need enable auto switch 137 #define CNT_SHIFTOUT_WAVEFORM_END 0x80 //need enable auto swtich 140 #define CNT_RESET_CNT0 0x00000010UL 141 #define CNT_RESET_CNT1 0x00000020UL 142 #define CNT_RESET_CNT2 0x00000040UL 143 #define CNT_RESET_CNT3 0x00000080UL 146 #define CNT0_BUS_B 0x1 147 #define CNT1_BUS_B 0x2 148 #define CNT2_BUS_B 0x4 149 #define CNT3_BUS_B 0x8 153 #define FLIP_BIT(val, mask) \ 155 uint32_t _tmp = (~val) & mask; \ 159 #define DUMP_REG(reg, base) PRINTD(DBG_TRACE, #reg " %08x (%x)\n", RD_WORD(reg + base), base) 184 CNT_ERR_INVALID_PARAM = -1,
185 CNT_ERR_TIMEOUT = -2,
194 void (*cb)(
void *arg, uint32_t status);
198 void (*cb)(
void *arg, uint32_t status);
208 struct pm_module pmd;
213 typedef void (*CNT_ISR_FUN)(
void*, uint32_t);
215 static INLINE
void cnt_enable_clk(uint32_t base)
217 uint32_t reg = RD_WORD(COUNTER_ALL_APB_REG_ENABLE_C0 + base);
218 reg |= CNT_CLK_ENABLE;
219 WR_WORD(COUNTER_ALL_APB_REG_ENABLE_C0 + base, reg);
221 static INLINE
void cnt_disable_clk(uint32_t base)
223 uint32_t reg = RD_WORD(COUNTER_ALL_APB_REG_ENABLE_C0 + base);
224 reg &= ~CNT_CLK_ENABLE;
225 WR_WORD(COUNTER_ALL_APB_REG_ENABLE_C0 + base, reg);
227 static INLINE
void cnt_set_clk(uint32_t base, uint32_t val)
229 uint32_t reg = RD_WORD(COUNTER_ALL_APB_REG_ENABLE_C0 + base);
231 WR_WORD(COUNTER_ALL_APB_REG_ENABLE_C0 + base, reg);
234 static INLINE
void cnt_set_mode(uint32_t base, uint32_t mode)
236 WR_WORD(COUNTER_ALL_APB_REG_MODE_SEL_C0 + base, mode);
238 static INLINE
void cnt_set_src_edge(uint32_t base, uint32_t src_edge)
240 WR_WORD(COUNTER_ALL_APB_REG_SRC_SEL_EDGE_C0 + base, src_edge);
242 static INLINE
void cnt_set_target_ctrl(uint32_t base, uint32_t ctrl)
244 WR_WORD(COUNTER_ALL_APB_REG_TARGET_REG_CTRL_C0 + base, ctrl);
246 static INLINE
void cnt_set_soft_trigger(uint32_t base, uint32_t trigger)
248 WR_WORD(COUNTER_ALL_APB_REG_SOFT_TRIGGER_CTRL_C0 + base, trigger);
250 static INLINE
void cnt_set_target_a(uint32_t base, uint32_t a0, uint32_t a1, uint32_t a2)
252 WR_WORD(COUNTER_ALL_APB_REG_TARGET_REG_A0_C0 + base, a0);
253 WR_WORD(COUNTER_ALL_APB_REG_TARGET_REG_A1_C0 + base, a1);
254 WR_WORD(COUNTER_ALL_APB_REG_TARGET_REG_A2_C0 + base, a2);
256 static INLINE
void cnt_set_target_b(uint32_t base, uint32_t b0, uint32_t b1, uint32_t b2)
258 WR_WORD(COUNTER_ALL_APB_REG_TARGET_REG_B0_C0 + base, b0);
259 WR_WORD(COUNTER_ALL_APB_REG_TARGET_REG_B1_C0 + base, b1);
260 WR_WORD(COUNTER_ALL_APB_REG_TARGET_REG_B2_C0 + base, b2);
262 static INLINE
void cnt_set_out_bypass(uint32_t base, uint32_t val)
264 WR_WORD(COUNTER_ALL_APB_REG_IR_DOUT_BYPASS_C0 + base, val);
266 static INLINE
void cnt_set_in_bypass(uint32_t base, uint32_t val)
268 WR_WORD(COUNTER_ALL_APB_REG_IR_DIN_BYPASS_C0 + base, val);
270 static INLINE
void cnt_set_input_mux(uint32_t base, uint32_t val)
272 WR_WORD(COUNTER_ALL_APB_REG_MUX_SEL_C0 + base, val);
274 static INLINE
void cnt_set_shiftmode_ctrl(uint32_t base, uint32_t val)
276 WR_WORD(COUNTER_ALL_APB_REG_SHIFTMODE_CTRL_C0 + base, val);
280 static INLINE
void cnt_set_shiftin_bit_num(uint32_t base, uint32_t n)
282 WR_WORD(COUNTER_ALL_APB_REG_SHIFTIN_DATA_CTRL_BITCNTS_C0 + base, n);
286 static INLINE
void cnt_set_shiftmode_point_en(uint32_t base, uint32_t val)
288 WR_WORD(COUNTER_ALL_APB_REG_SHIFTMODE_POINT_EN_C0 + base, val);
291 static INLINE
void cnt_set_one_bit_count(uint32_t base, uint32_t val)
293 WR_WORD(COUNTER_ALL_APB_REG_SWITCH_MODE_ONEBIT_CNTS_C0 + base, val);
296 static INLINE
void cnt_set_one_cycle_value(uint32_t base, uint32_t val)
298 WR_WORD(COUNTER_ALL_APB_REG_IR_DIN_ONECYCLE_VALUE_A_C0 + base, val);
300 static INLINE
void cnt_config_switch_mode(uint32_t base, uint32_t val)
302 WR_WORD(COUNTER_ALL_APB_REG_WAVEFORM_MODE_AUTOMATIC_C0 + base, val);
305 static INLINE uint32_t cnt_get_shiftin_update(uint32_t base)
307 return RD_WORD(COUNTER_ALL_APB_REG_SHIFTIN_DATABITS_UPDATED_C0 + base);
310 static INLINE uint32_t cnt_get_shiftin_data(uint32_t base)
312 return RD_WORD(COUNTER_ALL_APB_REG_SHIFTIN_DATA_C0 + base);
315 static INLINE
void cnt_set_shiftout_bit_num(uint32_t base, uint32_t n)
317 WR_WORD(COUNTER_ALL_APB_REG_SHIFTOUT_DATA_CTRL_BITCNTS_C0 + base, n);
319 static INLINE
void cnt_set_shiftout_data(uint32_t base, uint32_t val)
321 WR_WORD(COUNTER_ALL_APB_REG_SHIFTOUT_DATA_C0 + base, val);
323 static INLINE
void cnt_set_shiftout_data_valid(uint32_t base, uint32_t val)
325 WR_WORD(COUNTER_ALL_APB_REG_SHIFTOUT_DATA_VALID_C0 + base, val);
328 static INLINE uint32_t cnt_get_shadow_reg(uint32_t base)
330 return RD_WORD(COUNTER_ALL_APB_REG_SHADOW_REG_C0 + base);
332 static INLINE uint32_t cnt_get_capture_reg_a0(uint32_t base)
334 return RD_WORD(COUNTER_ALL_APB_REG_CAPTURE_REG_A0_C0 + base);
336 static INLINE uint32_t cnt_get_capture_reg_a1(uint32_t base)
338 return RD_WORD(COUNTER_ALL_APB_REG_CAPTURE_REG_A1_C0 + base);
340 static INLINE uint32_t cnt_get_capture_reg_a2(uint32_t base)
342 return RD_WORD(COUNTER_ALL_APB_REG_CAPTURE_REG_A2_C0 + base);
344 static INLINE uint32_t cnt_get_capture_reg_b0(uint32_t base)
346 return RD_WORD(COUNTER_ALL_APB_REG_CAPTURE_REG_B0_C0 + base);
348 static INLINE uint32_t cnt_get_capture_reg_b1(uint32_t base)
350 return RD_WORD(COUNTER_ALL_APB_REG_CAPTURE_REG_B1_C0 + base);
352 static INLINE uint32_t cnt_get_capture_reg_b2(uint32_t base)
354 return RD_WORD(COUNTER_ALL_APB_REG_CAPTURE_REG_B2_C0 + base);
356 static INLINE uint32_t cnt_get_capture_status(uint32_t base)
358 return RD_WORD(COUNTER_ALL_APB_REG_CAPTURE_REG_STATUS_C0 + base);
360 static INLINE
void cnt_set_capture_overflow(uint32_t base, uint32_t val)
362 WR_WORD(COUNTER_ALL_APB_REG_CAPTURE_REG_OVERFLOW_CTRL_C0 + base, val);
364 static INLINE uint32_t cnt_get_din_mux()
366 return RD_WORD(COUNTER_ALL_APB_REG_DIN_MUX_SEL);
368 static INLINE uint32_t cnt_get_dout_mux()
370 return RD_WORD(COUNTER_ALL_APB_REG_DOUT_MUX_SEL);
372 static INLINE
void cnt_set_din_mux(uint32_t val)
374 WR_WORD(COUNTER_ALL_APB_REG_DIN_MUX_SEL, val);
376 static INLINE
void cnt_set_dout_mux(uint32_t val)
378 WR_WORD(COUNTER_ALL_APB_REG_DOUT_MUX_SEL, val);
380 static INLINE
void cnt_set_dout_enable_ctrl(uint32_t base, uint32_t val)
382 WR_WORD(COUNTER_ALL_APB_REG_DOUT_ENABLE_CTRL + base, val);
384 static INLINE uint32_t cnt_get_dout_enable_ctrl(uint32_t base)
386 return RD_WORD(COUNTER_ALL_APB_REG_DOUT_ENABLE_CTRL + base);
388 static INLINE
void cnt_enable(uint32_t base)
390 uint32_t reg = RD_WORD(COUNTER_ALL_APB_REG_ENABLE_C0 + base);
393 WR_WORD(COUNTER_ALL_APB_REG_ENABLE_C0 + base, reg);
395 static INLINE
void cnt_disable(uint32_t base)
397 uint32_t reg = RD_WORD(COUNTER_ALL_APB_REG_ENABLE_C0 + base);
399 WR_WORD(COUNTER_ALL_APB_REG_ENABLE_C0 + base, reg);
401 static INLINE
void cnt_trigger_start(uint32_t base)
404 WR_WORD(COUNTER_ALL_APB_REG_SINGLE_START_TRIGGER_C0 + base, 1);
405 WR_WORD(COUNTER_ALL_APB_REG_SINGLE_START_TRIGGER_C0 + base, 0);
407 static INLINE
void cnt_trigger_stop(uint32_t base)
410 WR_WORD(COUNTER_ALL_APB_REG_SINGLE_STOP_TRIGGER_C0 + base, 1);
411 WR_WORD(COUNTER_ALL_APB_REG_SINGLE_STOP_TRIGGER_C0 + base, 0);
413 static INLINE
void cnt_trigger_clear(uint32_t base)
416 WR_WORD(COUNTER_ALL_APB_REG_SINGLE_CLEAR_TRIGGER_C0 + base, 1);
417 WR_WORD(COUNTER_ALL_APB_REG_SINGLE_CLEAR_TRIGGER_C0 + base, 0);
419 static INLINE
void cnt_trigger_reset(uint32_t base)
422 WR_WORD(COUNTER_ALL_APB_REG_SINGLE_RESET_TRIGGER_C0 + base, 1);
423 WR_WORD(COUNTER_ALL_APB_REG_SINGLE_RESET_TRIGGER_C0 + base, 0);
425 static INLINE
void cnt_set_snap_ctrl(uint32_t base, uint32_t mask)
427 uint32_t snap = RD_WORD(COUNTER_ALL_APB_REG_CTRL_SNAP_C0 + base);
428 FLIP_BIT(snap, mask);
429 WR_WORD(COUNTER_ALL_APB_REG_CTRL_SNAP_C0 + base, snap);
431 static INLINE
void cnt_reset(
int id)
434 reg = RD_WORD(GLOBAL_REG_RESET_CTRL_4);
435 uint32_t mask = 0x1UL << (4 + id);
437 WR_WORD(GLOBAL_REG_RESET_CTRL_4, reg);
446 WR_WORD(GLOBAL_REG_RESET_CTRL_4, reg | mask);
449 static INLINE
void cnt_reset_pclk(
void)
452 reg = RD_WORD(GLOBAL_REG_RESET_CTRL_5);
454 reg &= ~GLOBAL_REG_RESET_CTRL_5_CTL_RESET_5_D0_COUNTER_TOP_PCLK_RSTN_REG;
455 WR_WORD(GLOBAL_REG_RESET_CTRL_4, reg);
457 reg |= GLOBAL_REG_RESET_CTRL_5_CTL_RESET_5_D0_COUNTER_TOP_PCLK_RSTN_REG;
458 WR_WORD(GLOBAL_REG_RESET_CTRL_4, reg);
460 static INLINE uint32_t cnt_snap_status(uint32_t base)
462 return RD_WORD(COUNTER_ALL_APB_REG_SNAP_STATUS_C0 + base);
464 static INLINE uint32_t cnt_intr_status(
void)
466 return RD_WORD(COUNTER_ALL_APB_REG_INTR_STATUS);
468 static INLINE uint32_t cnt_intr_mask_status(
void)
470 return RD_WORD(COUNTER_ALL_APB_REG_INTR_MASK_STATUS);
472 static INLINE
void cnt_intr_clear(uint32_t val)
474 WR_WORD(COUNTER_ALL_APB_REG_INTR_CLR, val);
476 static INLINE
void cnt_intr_set(uint32_t val)
478 WR_WORD(COUNTER_ALL_APB_REG_INTR_SET, val);
480 static INLINE
void cnt_intr_mask_set(uint32_t val)
482 WR_WORD(COUNTER_ALL_APB_REG_INTR_MASK_SET, val);
484 static INLINE
void cnt_intr_mask_clear(uint32_t val)
486 WR_WORD(COUNTER_ALL_APB_REG_INTR_MASK_CLR, val);
489 static INLINE
void cnt_intr_sw_reset()
491 WR_WORD(COUNTER_ALL_APB_REG_INTR_SRESET, 1);
492 WR_WORD(COUNTER_ALL_APB_REG_INTR_SRESET, 0);
494 static INLINE
void cnt_trigger_global_start(
void)
496 WR_WORD(COUNTER_ALL_APB_REG_GLOBAL_START_TRIGGER, 1);
497 WR_WORD(COUNTER_ALL_APB_REG_GLOBAL_START_TRIGGER, 0);
499 static INLINE
void cnt_trigger_global_stop(
void)
501 WR_WORD(COUNTER_ALL_APB_REG_GLOBAL_STOP_TRIGGER, 1);
502 WR_WORD(COUNTER_ALL_APB_REG_GLOBAL_STOP_TRIGGER, 0);
504 static INLINE
void cnt_trigger_global_clear(
void)
506 WR_WORD(COUNTER_ALL_APB_REG_GLOBAL_CLEAR_TRIGGER, 1);
507 WR_WORD(COUNTER_ALL_APB_REG_GLOBAL_CLEAR_TRIGGER, 0);
509 static INLINE
void cnt_trigger_global_reset(
void)
511 WR_WORD(COUNTER_ALL_APB_REG_GLOBAL_RESET_TRIGGER, 1);
512 WR_WORD(COUNTER_ALL_APB_REG_GLOBAL_RESET_TRIGGER, 0);
514 void cnt_resume(cnt_dev_t *pd);
int hal_cnt_pin_chk(int port, int pin)
Check counter pinmux.
int hal_cnt_enable(cnt_dev_t *dev)
Enable counter.
int hal_cnt_internal_dout_pinmux(int inner_pin, int ext_port, int ext_pin)
Set dout pin mux.
int hal_cnt_sync(cnt_dev_t *dev, uint32_t mask)
Synchronize status registers manually. Will block until register finish update.
uint32_t hal_cnt_get_clk(cnt_dev_t *dev)
Get counter clock frequency.
cnt_int_pin
Counter internal pin.
Definition: hal_counter.h:170
void hal_cnt_close(cnt_dev_t *dev)
Close counter device.
int hal_cnt_intr_unmask(cnt_dev_t *dev)
Set interrupt unmask.
int hal_cnt_intr_mask(cnt_dev_t *dev)
Set interrupt mask.
cnt_dev_t * hal_cnt_open(int id, cnt_init_t *init)
Open counter device.
int hal_cnt_pin_mux_en(int port, int pin, int en)
Set pin mux.
int hal_cnt_internal_din_pinmux(int inner_pin, int ext_port, int ext_pin)
Set din pin mux.
int hal_cnt_disable(cnt_dev_t *dev)
Disable counter.
void hal_cnt_set_handler(cnt_dev_t *dev, CNT_ISR_FUN fun, void *arg)
Set ISR handler.