HPM SDK
HPMicro Software Development Kit
hpm_hall_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_HALL_DRV_H
9 #define HPM_HALL_DRV_H
10 
11 #include "hpm_common.h"
12 #include "hpm_hall_regs.h"
13 
22 #define HALL_EVENT_WDOG_FLAG_MASK (1U << 31)
23 #define HALL_EVENT_PHUPT_FLAG_MASK (1U << 30)
24 #define HALL_EVENT_PHPRE_FLAG_MASK (1U << 29)
25 #define HALL_EVENT_PHDLYEN_FLAG_MASK (1U << 28)
26 #define HALL_EVENT_U_FLAG_MASK (1U << 23)
27 #define HALL_EVENT_V_FLAG_MASK (1U << 22)
28 #define HALL_EVENT_W_FLAG_MASK (1U << 21)
29 #define HALL_UVW_STAT_MASK (HALL_COUNT_U_USTAT_MASK | HALL_COUNT_U_VSTAT_MASK | HALL_COUNT_U_WSTAT_MASK)
30 #define HALL_U_STAT_MASK HALL_COUNT_U_USTAT_MASK
31 #define HALL_V_STAT_MASK HALL_COUNT_U_VSTAT_MASK
32 #define HALL_W_STAT_MASK HALL_COUNT_U_WSTAT_MASK
33 
38 typedef enum hall_count_delay_start {
42 
47 typedef enum hall_rotate_direction {
51 
56 typedef enum hall_counter_type {
62 
63 #ifdef __cplusplus
64 extern "C" {
65 #endif
66 
73 static inline void hall_wdog_enable(HALL_Type *hall_x, uint32_t timeout)
74 {
75  hall_x->WDGCFG = HALL_WDGCFG_WDGTO_SET(timeout)
76  | HALL_WDGCFG_WDGEN_SET(true);
77 }
78 
84 static inline void hall_wdog_disable(HALL_Type *hall_x)
85 {
86  hall_x->WDGCFG = HALL_WDGCFG_WDGEN_SET(false);
87 }
88 
96 static inline void hall_phase_config(HALL_Type *hall_x, uint32_t delay_count,
97  hall_count_delay_start_t delay_start)
98 {
99  hall_x->PHCFG = HALL_PHCFG_DLYSEL_SET(delay_start)
100  | HALL_PHCFG_DLYCNT_SET(delay_count);
101 }
102 
109 static inline void hall_pre_uvw_transition_config(HALL_Type *hall_x, uint32_t counter)
110 {
111  hall_x->UVWCFG = HALL_UVWCFG_PRECNT_SET(counter);
112 }
113 
127 static inline void hall_trigger_output_event_enable(HALL_Type *hall_x,
128  uint32_t event_mask)
129 {
130  hall_x->TRGOEN = (hall_x->TRGOEN & ~event_mask) | event_mask;
131 }
132 
146 static inline void hall_trigger_output_event_disable(HALL_Type *hall_x,
147  uint32_t event_mask)
148 {
149  hall_x->TRGOEN &= ~event_mask;
150 }
151 
166  uint32_t event_mask)
167 {
168  hall_x->READEN = (hall_x->READEN & ~event_mask) | event_mask;
169 }
170 
185  uint32_t event_mask)
186 {
187  hall_x->READEN &= ~event_mask;
188 }
189 
203 static inline void hall_clear_status(HALL_Type *hall_x, uint32_t mask)
204 {
205  hall_x->SR = mask;
206 }
207 
221 static inline uint32_t hall_get_status(HALL_Type *hall_x)
222 {
223  return hall_x->SR;
224 }
225 
239 static inline void hall_irq_enable(HALL_Type *hall_x, uint32_t mask)
240 {
241  hall_x->IRQEN = (hall_x->IRQEN & ~mask) | mask;
242 }
243 
257 static inline void hall_irq_disable(HALL_Type *hall_x, uint32_t mask)
258 {
259  hall_x->IRQEN &= ~mask;
260 }
261 
275 static inline void hall_dma_request_enable(HALL_Type *hall_x, uint32_t mask)
276 {
277  hall_x->DMAEN = (hall_x->DMAEN & ~mask) | mask;
278 }
279 
293 static inline void hall_dma_request_disable(HALL_Type *hall_x, uint32_t mask)
294 {
295  hall_x->DMAEN &= ~mask;
296 }
297 
305 {
307 }
308 
315 static inline uint32_t hall_get_current_uvw_stat(HALL_Type *hall_x)
316 {
318 }
319 
327 static inline uint32_t hall_get_current_count(HALL_Type *hall_x,
328  hall_counter_type_t type)
329 {
330  return *(&hall_x->COUNT[HALL_COUNT_CURRENT].W + type) & HALL_COUNT_U_UCNT_MASK;
331 }
332 
340 static inline uint32_t hall_get_count_on_read_event(HALL_Type *hall_x,
341  hall_counter_type_t type)
342 {
343  return *(&hall_x->COUNT[HALL_COUNT_READ].W + type);
344 }
345 
353 static inline uint32_t hall_get_count_on_snap0_event(HALL_Type *hall_x,
354  hall_counter_type_t type)
355 {
356  return *(&hall_x->COUNT[HALL_COUNT_SNAP0].W + type);
357 }
358 
366 static inline uint32_t hall_get_count_on_snap1_event(HALL_Type *hall_x,
367  hall_counter_type_t type)
368 {
369  return *(&hall_x->COUNT[HALL_COUNT_SNAP1].W + type);
370 }
371 
378 static inline uint32_t hall_get_u_history0(HALL_Type *hall_x)
379 {
380  return hall_x->HIS[0].HIS0;
381 }
382 
389 static inline uint32_t hall_get_u_history1(HALL_Type *hall_x)
390 {
391  return hall_x->HIS[0].HIS1;
392 }
393 
400 static inline uint32_t hall_get_v_history0(HALL_Type *hall_x)
401 {
402  return hall_x->HIS[1].HIS0;
403 }
404 
411 static inline uint32_t hall_get_v_history1(HALL_Type *hall_x)
412 {
413  return hall_x->HIS[1].HIS1;
414 }
415 
422 static inline uint32_t hall_get_w_history0(HALL_Type *hall_x)
423 {
424  return hall_x->HIS[2].HIS0;
425 }
426 
433 static inline uint32_t hall_get_w_history1(HALL_Type *hall_x)
434 {
435  return hall_x->HIS[2].HIS1;
436 }
437 
444 static inline void hall_load_count_to_read_registers(HALL_Type *hall_x)
445 {
446  hall_x->CR |= HALL_CR_READ_MASK;
447 }
448 
454 static inline void hall_snap_enable(HALL_Type *hall_x)
455 {
456  hall_x->CR |= HALL_CR_SNAPEN_SET(1);
457 }
458 
464 static inline void hall_snap_disable(HALL_Type *hall_x)
465 {
466  hall_x->CR &= ~HALL_CR_SNAPEN_MASK;
467 }
468 
474 static inline void hall_counter_reset_assert(HALL_Type *hall_x)
475 {
476  hall_x->CR |= HALL_CR_RSTCNT_MASK;
477 }
478 
484 static inline void hall_counter_reset_release(HALL_Type *hall_x)
485 {
486  hall_x->CR &= ~HALL_CR_RSTCNT_MASK;
487 }
488 
489 #ifdef __cplusplus
490 }
491 #endif
496 #endif /* HPM_HALL_DRV_H */
#define HALL_PHCFG_DLYSEL_SET(x)
Definition: hpm_hall_regs.h:77
#define HALL_CR_READ_MASK
Definition: hpm_hall_regs.h:42
#define HALL_COUNT_READ
Definition: hpm_hall_regs.h:578
#define HALL_UVWCFG_PRECNT_SET(x)
Definition: hpm_hall_regs.h:119
#define HALL_WDGCFG_WDGTO_SET(x)
Definition: hpm_hall_regs.h:108
#define HALL_WDGCFG_WDGEN_SET(x)
Definition: hpm_hall_regs.h:98
#define HALL_COUNT_SNAP0
Definition: hpm_hall_regs.h:579
#define HALL_CR_SNAPEN_SET(x)
Definition: hpm_hall_regs.h:54
#define HALL_CR_RSTCNT_MASK
Definition: hpm_hall_regs.h:62
#define HALL_COUNT_U_UCNT_MASK
Definition: hpm_hall_regs.h:540
#define HALL_COUNT_U_DIR_GET(x)
Definition: hpm_hall_regs.h:506
#define HALL_COUNT_CURRENT
Definition: hpm_hall_regs.h:577
#define HALL_PHCFG_DLYCNT_SET(x)
Definition: hpm_hall_regs.h:87
#define HALL_CR_SNAPEN_MASK
Definition: hpm_hall_regs.h:52
#define HALL_COUNT_SNAP1
Definition: hpm_hall_regs.h:580
#define HALL_COUNT_U_WSTAT_SHIFT
Definition: hpm_hall_regs.h:532
static uint32_t hall_get_w_history0(HALL_Type *hall_x)
get the history count of w when w signal transition from 0 to 1
Definition: hpm_hall_drv.h:422
static void hall_irq_enable(HALL_Type *hall_x, uint32_t mask)
enable irq
Definition: hpm_hall_drv.h:239
static uint32_t hall_get_status(HALL_Type *hall_x)
get status register
Definition: hpm_hall_drv.h:221
static void hall_snap_disable(HALL_Type *hall_x)
disable hall snap
Definition: hpm_hall_drv.h:464
static void hall_counter_reset_assert(HALL_Type *hall_x)
reset all counter and related snapshots assert
Definition: hpm_hall_drv.h:474
enum hall_counter_type hall_counter_type_t
counter type config
static void hall_load_read_trigger_event_enable(HALL_Type *hall_x, uint32_t event_mask)
enable hardware read event
Definition: hpm_hall_drv.h:165
static uint32_t hall_get_current_count(HALL_Type *hall_x, hall_counter_type_t type)
get current count U or V or W
Definition: hpm_hall_drv.h:327
static uint32_t hall_get_count_on_read_event(HALL_Type *hall_x, hall_counter_type_t type)
get count when read event generated
Definition: hpm_hall_drv.h:340
static void hall_clear_status(HALL_Type *hall_x, uint32_t mask)
clear status register
Definition: hpm_hall_drv.h:203
static uint32_t hall_get_v_history1(HALL_Type *hall_x)
get the history count of v when v signal transition from 1 to 0
Definition: hpm_hall_drv.h:411
enum hall_rotate_direction hall_rotate_direction_t
return value of motor movement direction
static uint32_t hall_get_current_uvw_stat(HALL_Type *hall_x)
get three bits indicate UVW state
Definition: hpm_hall_drv.h:315
static void hall_snap_enable(HALL_Type *hall_x)
enable hall snap
Definition: hpm_hall_drv.h:454
static void hall_wdog_enable(HALL_Type *hall_x, uint32_t timeout)
enable the watchdog
Definition: hpm_hall_drv.h:73
static void hall_dma_request_disable(HALL_Type *hall_x, uint32_t mask)
disable dma request
Definition: hpm_hall_drv.h:293
enum hall_count_delay_start hall_count_delay_start_t
select delay start time
static uint32_t hall_get_count_on_snap1_event(HALL_Type *hall_x, hall_counter_type_t type)
get count when snap1 event generated
Definition: hpm_hall_drv.h:366
static uint32_t hall_get_w_history1(HALL_Type *hall_x)
get the history count of w when w signal transition from 1 to 0
Definition: hpm_hall_drv.h:433
static void hall_wdog_disable(HALL_Type *hall_x)
disable the watchdog
Definition: hpm_hall_drv.h:84
static uint32_t hall_get_u_history0(HALL_Type *hall_x)
get the history count of u when u signal transition from 0 to 1
Definition: hpm_hall_drv.h:378
static void hall_irq_disable(HALL_Type *hall_x, uint32_t mask)
disable irq
Definition: hpm_hall_drv.h:257
static hall_rotate_direction_t hall_get_rotate_direction(HALL_Type *hall_x)
get rotate direction
Definition: hpm_hall_drv.h:304
hall_rotate_direction
return value of motor movement direction
Definition: hpm_hall_drv.h:47
#define HALL_UVW_STAT_MASK
Definition: hpm_hall_drv.h:29
hall_count_delay_start
select delay start time
Definition: hpm_hall_drv.h:38
static void hall_counter_reset_release(HALL_Type *hall_x)
reset all counter and related snapshots release
Definition: hpm_hall_drv.h:484
static void hall_trigger_output_event_disable(HALL_Type *hall_x, uint32_t event_mask)
disable trigger event mask
Definition: hpm_hall_drv.h:146
static uint32_t hall_get_count_on_snap0_event(HALL_Type *hall_x, hall_counter_type_t type)
get count when snap0 event generated
Definition: hpm_hall_drv.h:353
static void hall_load_count_to_read_registers(HALL_Type *hall_x)
load ucnt, vcnt, wcnt and tmrcnt into their read registers. Hardware auto-clear;
Definition: hpm_hall_drv.h:444
static void hall_trigger_output_event_enable(HALL_Type *hall_x, uint32_t event_mask)
enable trigger event mask
Definition: hpm_hall_drv.h:127
static uint32_t hall_get_v_history0(HALL_Type *hall_x)
get the history count of v when v signal transition from 0 to 1
Definition: hpm_hall_drv.h:400
static void hall_pre_uvw_transition_config(HALL_Type *hall_x, uint32_t counter)
early trigger configuration
Definition: hpm_hall_drv.h:109
static void hall_load_read_trigger_event_disable(HALL_Type *hall_x, uint32_t event_mask)
disable hardware read event
Definition: hpm_hall_drv.h:184
static uint32_t hall_get_u_history1(HALL_Type *hall_x)
get the history count of u when u signal transition from 1 to 0
Definition: hpm_hall_drv.h:389
hall_counter_type
counter type config
Definition: hpm_hall_drv.h:56
static void hall_dma_request_enable(HALL_Type *hall_x, uint32_t mask)
enable dma request
Definition: hpm_hall_drv.h:275
static void hall_phase_config(HALL_Type *hall_x, uint32_t delay_count, hall_count_delay_start_t delay_start)
delay a certain number of clock cycles after receiving a trigger event
Definition: hpm_hall_drv.h:96
@ hall_rotate_direction_forward
Definition: hpm_hall_drv.h:48
@ hall_rotate_direction_reversed
Definition: hpm_hall_drv.h:49
@ hall_count_delay_start_after_uvw_toggle
Definition: hpm_hall_drv.h:39
@ hall_count_delay_start_after_pre_trigger
Definition: hpm_hall_drv.h:40
@ hall_counter_type_u
Definition: hpm_hall_drv.h:59
@ hall_counter_type_v
Definition: hpm_hall_drv.h:58
@ hall_counter_type_timer
Definition: hpm_hall_drv.h:60
@ hall_counter_type_w
Definition: hpm_hall_drv.h:57
Definition: hpm_hall_regs.h:12
__RW uint32_t IRQEN
Definition: hpm_hall_regs.h:22
__RW uint32_t PHCFG
Definition: hpm_hall_regs.h:14
__RW uint32_t CR
Definition: hpm_hall_regs.h:13
__RW uint32_t WDGCFG
Definition: hpm_hall_regs.h:15
__RW uint32_t TRGOEN
Definition: hpm_hall_regs.h:17
struct HALL_Type::@382 HIS[3]
__RW uint32_t DMAEN
Definition: hpm_hall_regs.h:20
__R uint32_t HIS1
Definition: hpm_hall_regs.h:31
__RW uint32_t UVWCFG
Definition: hpm_hall_regs.h:16
struct HALL_Type::@381 COUNT[4]
__RW uint32_t READEN
Definition: hpm_hall_regs.h:18
__RW uint32_t SR
Definition: hpm_hall_regs.h:21
__R uint32_t U
Definition: hpm_hall_regs.h:26
__R uint32_t W
Definition: hpm_hall_regs.h:24
__R uint32_t HIS0
Definition: hpm_hall_regs.h:30