InPlay API
hal_qdec.h
1 
12 #ifndef HAL_QDEC_H
13 #define HAL_QDEC_H
14 #include <stdint.h>
15 #include "cmsis_os.h"
16 #include "./hal/hal_power.h"
17 
25 typedef enum {
27  QDEC0_ID = 0,
31 } qdec_id_t;
32 
33 enum qdec_error {
34  QDEC_ERR_OK = 0,
35  QDEC_ERR_INVALID_PARAM = -1,
36  QDEC_ERR_PINMUX = -2,
37  QDEC_ERR_BUSY = -3,
38  QDEC_ERR_MUTEX = -4,
39 };
40 
41 
42 
43 typedef struct qdec_init {
44  uint16_t report_intvl;
45  uint16_t sample_intvl;
46  int move_dir;
47  int prio;
48  void* arg;
49  void(*callback)(void*, int, uint32_t, int);
50 } qdec_init_t;
51 typedef struct qdec_cfg {
52  int8_t a_port;
53  int8_t a_pin;
54  int8_t b_port;
55  int8_t b_pin;
56  int8_t debounce;
57  int8_t idx_en;
58  int8_t idx_port;
59  int8_t idx_pin;
60  int8_t idx_pol;
61  int8_t led_en;
62  int8_t led_pol;
63  uint16_t led_on_time;
64 } qdec_cfg_t;
65 typedef struct qdec_idx_cfg {
66  int8_t rst_en;
67  uint8_t pattern_len;
68  uint8_t pattern[4];
69 } qdec_idx_cfg_t;
70 
71 static INLINE uint32_t qdec_intr_status(void)
72 {
73  return RD_WORD(QUADDEC_REG_INTERRUPT_STATUS);
74 }
75 static INLINE uint32_t qdec_intr_mask_status(void)
76 {
77  return RD_WORD(QUADDEC_REG_INTERRUPT_MASK_STATUS);
78 }
79 
80 static INLINE void qdec_intr_clear(uint32_t val)
81 {
82  WR_WORD(QUADDEC_REG_INTERRUPT_CLEAR, val);
83 }
84 static INLINE void qdec_intr_set(uint32_t val)
85 {
86  WR_WORD(QUADDEC_REG_INTERRUPT_SET, val);
87 }
88 static INLINE void qdec_intr_mask_clear(uint32_t val)
89 {
90  WR_WORD(QUADDEC_REG_INTERRUPT_MASK_CLEAR, val);
91 }
92 static INLINE void qdec_intr_mask_set(uint32_t val)
93 {
94  WR_WORD(QUADDEC_REG_INTERRUPT_MASK_SET, val);
95 }
96 
97 
98 
99 /*
100  * intval = led<<16 | smaple.
101  * sample: actual sample interval = (register value + 64) * 2 us
102  * led: actual early start interval = register value * 1 us.
103  * Register value 0 is invalid. If this field has an effectively longer time than sample_interval field, the LED will always be asserted.
104  * */
105 static INLINE void qdec_sample_interval(uint16_t intv)
106 {
107  uint32_t reg = RD_WORD(QUADDEC_REG_INTERVAL_SETTING);
108  reg &= ~QUADDEC_REG_INTERVAL_SETTING_CTL_SAMPLE_INTERVAL_M1;
109  reg |= intv;
110  WR_WORD(QUADDEC_REG_INTERVAL_SETTING,reg);
111 }
112 
113 //How many samples the counter value in the register will be updated (and an counter ready interrupt triggered).
114 //If value is 0, the counter will be updated every sample. Max value is 0xff
115 static INLINE void qdec_report_interval(uint32_t intv)
116 {
117  WR_WORD(QUADDEC_REG_REPORT_INTERVAL,intv);
118 }
119 
120 //Actual led on = register value * 1 us.
121 static INLINE void qdec_led_inertval(uint8_t id, uint32_t intv)
122 {
123  WR_WORD(QUADDEC_REG_INTERVAL_LED_SETTING_M0 + id*4,intv);
124 }
125 
126 static INLINE void qdec_counter_lmt(uint32_t lmt)
127 {
128 
129  WR_WORD(QUADDEC_REG_COUNTER_LMT,lmt);
130 }
131 
132 static INLINE void qdec_counter_init(uint32_t init)
133 {
134  WR_WORD(QUADDEC_REG_COUNTER_INIT,init);
135 }
136 
137 static INLINE void qdec_counter_clear(uint32_t val)
138 {
139  WR_WORD(QUADDEC_REG_COUNTER_CLEAR,val);
140 }
141 
142 //current sample counter value
143 static INLINE uint32_t qdec_curr_sample(int id)
144 {
145  return RD_WORD(QUADDEC_REG_SAMPLE_COUNTER0 + id*4)&0xff;
146 }
147 
148 //sample counter value from last report time
149 static INLINE uint32_t qdec_report_sample(int id)
150 {
151  return (RD_WORD(QUADDEC_REG_SAMPLE_COUNTER0 + id*4)>>16)&0xff;
152 }
153 
154 static INLINE uint32_t qdec_get_db_counter(int id)
155 {
156  return RD_WORD(QUADDEC_REG_DB_COUNTER_AND_CURRENT_INPUT0 + id*4);
157 }
158 
159 static INLINE void qdec_idx_ctrl(uint32_t val)
160 {
161  WR_WORD(QUADDEC_REG_INDEX_CTRL, val);
162 }
163 
164 static INLINE uint32_t qdec_get_index_counter(int id)
165 {
166  return RD_WORD(QUADDEC_REG_INDEX_COUNTER0 + id*4);
167 }
168 #if 0
169 static INLINE uint32_t qdec_get_misc_ctrl(void)
170 {
171  return RD_WORD(QUADDEC_REG_MISC_CTRL);
172 }
173 
174 static INLINE void qdec_set_misc_ctrl(uint32_t misc)
175 {
176  WR_WORD(QUADDEC_REG_MISC_CTRL, misc);
177 }
178 #endif
179 static INLINE void qdec_set_pin_a(int id, uint32_t a)
180 {
181  uint32_t reg = RD_WORD(QUADDEC_REG_INPUT_PIN_ASSIGNMENT0 + id*4);
182  reg &= ~QUADDEC_REG_INPUT_PIN_ASSIGNMENT0_CTL_PIN_A_SEL0;
183  reg |= a&0x3;
184  WR_WORD(QUADDEC_REG_INPUT_PIN_ASSIGNMENT0 + id*4, reg);
185 }
186 static INLINE void qdec_set_pin_b(int id, uint32_t b)
187 {
188  uint32_t reg = RD_WORD(QUADDEC_REG_INPUT_PIN_ASSIGNMENT0 + id*4);
189  reg &= ~QUADDEC_REG_INPUT_PIN_ASSIGNMENT0_CTL_PIN_B_SEL0;
190  reg |= (b&0x3)<<4;
191  WR_WORD(QUADDEC_REG_INPUT_PIN_ASSIGNMENT0 + id*4, reg);
192 }
193 static INLINE void qdec_set_pin_idx(int id, uint32_t idx)
194 {
195  uint32_t reg = RD_WORD(QUADDEC_REG_INPUT_PIN_ASSIGNMENT0 + id*4);
196  reg &= ~QUADDEC_REG_INPUT_PIN_ASSIGNMENT0_CTL_PIN_IDX_SEL0;
197  reg |= (idx&0x3)<<8;
198  WR_WORD(QUADDEC_REG_INPUT_PIN_ASSIGNMENT0 + id*4, reg);
199 }
200 
201 
202 
203 static INLINE void qdec_enable(int id)
204 {
205  uint32_t reg = RD_WORD(QUADDEC_REG_MISC_CTRL);
206  id &= 0x3;
207  reg |= 0x1UL<<(id+QUADDEC_REG_MISC_CTRL_CTL_QUADDEC_ENABLE_SHIFT);
208  WR_WORD(QUADDEC_REG_MISC_CTRL, reg);
209 
210 }
211 static INLINE void qdec_disable(int id)
212 {
213  uint32_t reg = RD_WORD(QUADDEC_REG_MISC_CTRL);
214  id &= 0x3;
215  uint32_t mask = 0x1UL<<(id+QUADDEC_REG_MISC_CTRL_CTL_QUADDEC_ENABLE_SHIFT);
216  reg &= ~mask;
217  WR_WORD(QUADDEC_REG_MISC_CTRL, reg);
218 
219 }
220 static INLINE void qdec_led_enable(int id)
221 {
222  uint32_t reg = RD_WORD(QUADDEC_REG_MISC_CTRL);
223  id &= 0x3;
224  reg |= 0x1UL<<(id+QUADDEC_REG_MISC_CTRL_CTL_LED_EN_SHIFT);
225  WR_WORD(QUADDEC_REG_MISC_CTRL, reg);
226 
227 }
228 static INLINE void qdec_led_disable(int id)
229 {
230  uint32_t reg = RD_WORD(QUADDEC_REG_MISC_CTRL);
231  id &= 0x3;
232  uint32_t mask = 0x1UL<<(id+QUADDEC_REG_MISC_CTRL_CTL_LED_EN_SHIFT);
233  reg &= ~mask;
234  WR_WORD(QUADDEC_REG_MISC_CTRL, reg);
235 
236 }
237 static INLINE void qdec_led_pol(int id, uint8_t pol)
238 {
239  uint32_t reg = RD_WORD(QUADDEC_REG_MISC_CTRL);
240  id &= 0x3;
241  reg &= ~(0x1UL<<(id+QUADDEC_REG_MISC_CTRL_CTL_LED_POLARITY_SHIFT));
242  reg |= (pol&0x1UL)<<(id+QUADDEC_REG_MISC_CTRL_CTL_LED_POLARITY_SHIFT);
243  WR_WORD(QUADDEC_REG_MISC_CTRL, reg);
244 }
245 static INLINE void qdec_idx_enable(int id)
246 {
247  uint32_t reg = RD_WORD(QUADDEC_REG_MISC_CTRL);
248  id &= 0x3;
249  reg |= 0x1UL<<(id+QUADDEC_REG_MISC_CTRL_CTL_INDEX_EN_SHIFT);
250  WR_WORD(QUADDEC_REG_MISC_CTRL, reg);
251 
252 }
253 static INLINE void qdec_idx_disable(int id)
254 {
255  uint32_t reg = RD_WORD(QUADDEC_REG_MISC_CTRL);
256  id &= 0x3;
257  uint32_t mask = 0x1UL<<(id+QUADDEC_REG_MISC_CTRL_CTL_INDEX_EN_SHIFT);
258  reg &= ~mask;
259  WR_WORD(QUADDEC_REG_MISC_CTRL, reg);
260 
261 }
262 static INLINE void qdec_idx_pol(int id, uint8_t pol)
263 {
264  uint32_t reg = RD_WORD(QUADDEC_REG_MISC_CTRL);
265  id &= 0x3;
266  reg &= ~(0x1UL<<(id+QUADDEC_REG_MISC_CTRL_CTL_INDEX_POLARITY_SHIFT));
267  reg |= (pol&0x1UL)<<(id+QUADDEC_REG_MISC_CTRL_CTL_INDEX_POLARITY_SHIFT);
268  WR_WORD(QUADDEC_REG_MISC_CTRL, reg);
269 }
270 static INLINE void qdec_debounce_enable(int id)
271 {
272  uint32_t reg = RD_WORD(QUADDEC_REG_MISC_CTRL);
273  id &= 0x3;
274  reg |= 0x1UL<<(id+QUADDEC_REG_MISC_CTRL_CTL_DEBOUNCE_EN_SHIFT);
275  WR_WORD(QUADDEC_REG_MISC_CTRL, reg);
276 
277 }
278 static INLINE void qdec_debounce_disable(int id)
279 {
280  uint32_t reg = RD_WORD(QUADDEC_REG_MISC_CTRL);
281  id &= 0x3;
282  uint32_t mask = 0x1UL<<(id+QUADDEC_REG_MISC_CTRL_CTL_DEBOUNCE_EN_SHIFT);
283  reg &= ~mask;
284  WR_WORD(QUADDEC_REG_MISC_CTRL, reg);
285 
286 }
287 static INLINE void qdec_clk_num_in_us(uint8_t num)
288 {
289  uint32_t reg = RD_WORD(QUADDEC_REG_MISC_CTRL);
290  reg &= ~QUADDEC_REG_MISC_CTRL_CTL_NUM_CLK_IN_US_M1;
291  reg |= (num&QUADDEC_REG_MISC_CTRL_CTL_NUM_CLK_IN_US_M1_MASK)<<QUADDEC_REG_MISC_CTRL_CTL_NUM_CLK_IN_US_M1_SHIFT;
292  WR_WORD(QUADDEC_REG_MISC_CTRL, reg);
293 
294 }
295 
296 
297 
306 int hal_qdec_open(qdec_init_t *init);
307 
308 
319 int hal_qdec_cfg(int id, qdec_cfg_t *cfg);
320 
321 
330 int hal_qdec_idx_cfg(qdec_idx_cfg_t *cfg);
331 
332 
341 int hal_qdec_enable(int id);
342 
343 
344 
353 int hal_qdec_disable(int id);
354 
362 void hal_qdec_close(void);
363 
364 
366 #endif
void hal_qdec_close(void)
Close qdec.
Max ID.
Definition: hal_qdec.h:30
int hal_qdec_disable(int id)
Disable qdec.
int hal_qdec_cfg(int id, qdec_cfg_t *cfg)
Config qdec.
ID 1.
Definition: hal_qdec.h:28
ID 2.
Definition: hal_qdec.h:29
int hal_qdec_open(qdec_init_t *init)
Open qdec device.
int hal_qdec_idx_cfg(qdec_idx_cfg_t *cfg)
Config qdec index signal.
ID 0.
Definition: hal_qdec.h:27
int hal_qdec_enable(int id)
Enable qdec.
qdec_id_t
Mouse device ID.
Definition: hal_qdec.h:26