1 /*
2  * Copyright (c) 2023 HPMicro
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #ifndef HPM_MMC_DRV_H
9 #define HPM_MMC_DRV_H
10 
11 #include "hpm_common.h"
12 #include "hpm_mtg_regs.h"
13 /**
14  * @brief MTG driver APIs
15  * @defgroup mtg_interface MTG driver APIs
16  * @ingroup motor_interfaces
17  * @{
18  */
19 
20 /**
21  * @brief define the struct of mtg lock values
22  *
23  */
24 typedef struct mtg_result {
25     int32_t rev; /* revolution */
26     uint32_t pos; /* postion */
27     int32_t vel; /* velocity */
28     int32_t acc; /* acceleration */
29     uint32_t time_stamp; /* time stamp */
30 } mtg_lock_value_t;
31 
32 /**
33  * @brief define the enum of the postion of the observed object
34  */
35 typedef enum mtg_evnet_object_postion {
36     event_source_before_filter = 1 << 0, /* before filter, also treat as input of mtg */
37     event_source_filter_output = 1 << 1, /* after filter, also treat as input of mtg's time compensation module */
38     event_source_tra0_output = 1 << 2, /* output of mtg's trajectory predictor0 */
39     event_source_tra1_output = 1 << 3, /* output of mtg's trajectory predictor1 */
40 } mtg_evnet_object_postion_t;
41 
42 /**
43  * @brief define the enum of the observed object
44  */
45 typedef enum mtg_event_object {
46     event_object_rev_pos = 1, /* location, revolution and position */
47     event_object_vel = 2, /* velocity */
48     event_object_acc = 4, /* acceleration */
49     event_object_pos = 8, /* postion, ignore revolution */
50 } mtg_event_object_t;
51 
52 /**
53  * @brief define the enum of the event detection mode
54  */
55 typedef enum mtg_event_mode {
56     event_mode_across = 1, /* across mode, change from greater than to less than, or opposite */
57     event_mode_hold = 2, /* hold mode, maintain within a range for a period of time */
58     event_mode_over_protect = 4, /* over protect mode, greater than or less than */
59     event_mode_time_match = 8, /* time match mode, when the synt's cnt value is equal to the preset */
60 } mtg_event_mode_t;
61 
62 /**
63  * @brief define the enum of the event detection direction
64  */
65 typedef enum mtg_event_dir {
66     event_dir_negative = 0,
67     event_dir_positive = 1,
68     event_dir_both = 2,
69 } mtg_event_dir_t;
70 
71 /**
72  * @brief define the enum of the event detection direction calculation mode
73  */
74 typedef enum mtg_event_dir_mode {
75     event_dir_mode_dy = 0, /* use first derivative of object */
76     event_dir_mode_y1_y0 = 1, /* use difference of current and previous */
77 } mtg_event_dir_mode_t;
78 
79 /**
80  * @brief define the enum of the event detection over protect mode
81  */
82 typedef enum mtg_event_over_mode_cmp {
83     event_over_cmp_mode_smaller = 0,
84     event_over_cmp_mode_bigger = 1,
85 } mtg_event_over_mode_cmp_t;
86 
87 /**
88  * @brief define the enum of the event trigger number
89  */
90 typedef enum mtg_event_trig_num {
91     event_trig_once = 0, /* event can only be triggered once */
92     event_trig_repeat = 1, /* event can be triggered multiple times */
93 } mtg_event_trig_num_t;
94 
95 /**
96  * @brief define the struct of the event setup parameter
97  */
98 typedef struct mtg_event_param {
99     bool enable;
100     bool irq_en; /* enable or disable the event irq */
101     mtg_event_object_t obj;
102     mtg_evnet_object_postion_t obj_postion;
103     mtg_event_mode_t mode;
104     mtg_event_dir_t dir;
105     mtg_event_dir_mode_t dir_mode;
106     mtg_event_over_mode_cmp_t cmp_mode;
107     mtg_event_trig_num_t trig_num;
108 
109     /* union struct of event presets registers, different meaning when different event mode */
110     union {
111         struct {
112             uint32_t cross_value_h; /* the high 32bit of cross_value, only need set when object is ${event_object_rev_pos} */
113             uint32_t cross_value_l; /* the low 32bit of cross_value */
114         } cross_param;
115         struct {
116             uint32_t hold_value_h; /* the high 32bit of hold_value, only need set when object is ${event_object_rev_pos} */
117             uint32_t hold_value_l; /* the low 32bit of hold_value */
118             uint32_t error_limit; /* the range is between ${hold value - error_limit} and ${hold value + error_limit} */
119             uint32_t hold_clock_cnt; /* the event will be triggered when match the hold value and error_limit and last for &{hold_clock_cnt} clocks */
120         } hold_param;
121         struct {
122             uint32_t limit_value_h; /* the high 32bit of limit_value */
123             uint32_t limit_value_l; /* the low 32bit of limit_value */
124         } over_protect_param;
125         struct {
126             uint32_t clock_count; /* the event will be triggered when synt clock match the value */
127         } time_param;
128         uint32_t preset[4];
129     } preset;
130 } mtg_event_param_t;
131 
132 /*
133  * @brief define the struct of the trajectory predictor limit
134  */
135 typedef struct mtg_tra_limit_param {
136     bool vel_step_limit_en; /* velocity step limit enable, when enable, the velocity variation per clock will be limited by vel_step_max and vel_step_min */
137     uint32_t vel_step_max; /* velocity step limit value max */
138     uint32_t vel_step_min;  /* velocity step limit value min */
139     bool pos_step_limit_en; /* postion step limit enable, when enable, the postion variation per clock will be limited by pos_step_max and pos_step_min */
140     uint32_t pos_step_max; /* postion step limit value max */
141     uint32_t pos_step_min; /* postion step limit value min */
142 } mtg_tra_limit_param_t;
143 
144 /**
145  * @brief define the enum of the software force one way mode
146  */
147 typedef enum mtg_software_force_one_way_mode {
148     sw_force_negative = 0,
149     sw_force_positive = 1,
150 } mtg_software_force_one_way_mode_t;
151 
152 /**
153  * @brief define the enum of the hardware force one way mode
154  */
155 typedef enum vel_one_way_mode {
156     bigger_or_eq_zero = 0,
157     smaller_or_eq_zero = 1,
158 } vel_one_way_mode_t;
159 
160 /**
161  * @brief define the struct of the hardware force one way mode
162  */
163 typedef struct mtg_hardware_force_one_way_mode {
164     uint32_t vel_limit_p;
165     uint32_t vel_limit_n;
166 } mtg_hardware_force_one_way_mode_t;
167 
168 /**
169  * @brief define the enum of the filter initialization mode
170  */
171 typedef enum mtg_filter_rev_init_mode {
172     rev_init_mode_from_first_input_value = 0, /* the first input value will be used by the mtg filter to initialize the filter */
173     rev_init_mode_from_register = 1, /* the value in the register will be used by the mtg filter to initialize the filter */
174 } mtg_filter_rev_init_mode_t;
175 
176 /**
177  * @brief define the enum of the filter judge mode
178  * It affects the judgment conditions for changes in the number of revolution
179  */
180 typedef enum mtg_filter_rev_judge_mode {
181     mtg_rev_judge_mode_new_sub_old = 0,
182     mtg_rev_judge_mode_encoder_lines = 1,
183 } mtg_filter_rev_judge_mode_t;
184 
185 /**
186  * @brief define the enum of the filter ff mode
187  */
188 typedef enum mtg_filter_ff_mode {
189     mtg_filter_ff_mode_from_register = 0,
190     mtg_filter_ff_mode_from_input = 1,
191 } mtg_filter_ff_mode_t;
192 
193 typedef struct mtg_filter_param {
194     bool enable; /* filter enable */
195     bool ff_en; /* filter feedforward enable */
196     bool init_en; /* filter first value initialization enable */
197     bool err_bypass_en; /* filter error bypass enable. when difference between filter input and output beyond the limit, filter output will be bypassed  */
198     bool err_init; /* filter error reset enable. when difference between filter input and output beyond the limit, the filter will reset */
199     bool timeout_en; /* filter timeout enable */
200     bool err_bypass_i_f_en;
201     bool err_bypass_f_i_en;
202     bool multi_err_irq_en;
203     bool acceleration_en; /* filter acceleration enable. when enable, the filter will output acceleration value */
204     mtg_filter_rev_init_mode_t rev_ini_mode;
205     mtg_filter_rev_judge_mode_t rev_judge_mode;
206     mtg_filter_ff_mode_t ff_mode;
207     int32_t rev_init_value; /* the initial value of revolution. only used when init_en == true */
208     int32_t vel_init_value; /* the initial value of velocity. only used when init_en == true */
209     int32_t acc_init_value; /* the initial value of acceleration. only used when init_en == true */
210     uint32_t pos_init_value; /* the initial value of postion. only used when init_en == true */
211     uint32_t filter_mot_sel;
212     uint32_t filter_stage_sel;
213     uint32_t filter_time_constant_tp;
214     uint32_t filter_time_constant_tz;
215     uint32_t filter_time_constant_tz_1;
216     uint32_t filter_zero_tz_sel;
217     uint32_t filter_gain;
218     uint32_t filter_stage_shift[2];
219     uint32_t filter_param_shift;
220     uint32_t filter_time_shift;
221     uint32_t filter_ff_shift;
222     uint32_t filter_error_limit_l;
223     uint32_t filter_error_limit_h;
224 } mtg_filter_param_t;
225 
226 /**
227  * @brief define the enum of the trajectory predictor time input source
228  */
229 typedef enum mtg_time_input_source {
230     mtg_time_input_from_filter = 1, /* the value in the filter will be used */
231     mtg_time_input_from_input = 2, /* the value in the input will be used */
232 } mtg_time_input_source_t;
233 
234 /**
235  * @brief define the struct of the trajectory predictor time initialization
236  */
237 typedef struct mtg_time_init_param {
238     bool enable;
239     uint32_t adjust_value; /* time compensation clocks */
240     uint8_t index;
241     mtg_time_input_source_t source;
242 } mtg_time_init_param_t;
243 
244 /**
245  * @brief define the struct of the trajectory predictor timeout
246  */
247 typedef struct mtg_timeout_param {
248     bool enable;
249     uint32_t timeout_clock_count;
250 } mtg_timeout_param_t;
251 
252 /**
253  * @brief define the enum of the trajectory predictor link event
254  * link the trigger source and the cmd object, it describes the
255  * trigger source
256  */
257 typedef enum mtg_link_cfg {
258     link_event0 = 0,
259     link_event1 = 1,
260     link_event2 = 2,
261     link_event3 = 3,
262     link_hw_trigger = 4,
263     link_sw_trigger = 5,
264     link_event_timeout = 6,
265 } mtg_link_cfg_t;
266 
267 /**
268  * @brief define the enum of the trajectory predictor command object
269  */
270 typedef enum {
271     cmd_object_rev = 1 << 0,
272     cmd_object_pos = 1 << 1,
273     cmd_object_vel = 1 << 2,
274     cmd_object_acc = 1 << 3,
275     cmd_object_jer = 1 << 4,
276 } mtg_tra_cmd_object_t;
277 
278 /**
279  * @brief define the enum of the trajectory predictor command mode
280  */
281 typedef enum {
282     cmd_mode_new_value = 0, /* when cmd exec, the preset value will replace the old value */
283     cmd_mode_old_delta = 1, /* when cmd exec, the preset value will be added to the old value */
284 } mtg_link_cmd_mode_t;
285 
286 /**
287  * @brief define the struct of the trajectory shift struct
288  */
289 typedef struct mtg_tra_shift {
290     uint8_t jerk_shift;
291     uint8_t acc_shift;
292     uint8_t vel_shift;
293 } mtg_tra_shift_t;
294 
295 /**
296  * @brief define the struct of the trajectory predictor command
297  */
298 typedef struct mtg_tra_cmd_cfg {
299     uint8_t index;
300     uint32_t object; /* cmd object, see ${mtg_tra_cmd_object_t} */
301     mtg_link_cmd_mode_t mode;
302     uint32_t rev_preset;
303     uint32_t pos_preset;
304     uint32_t vel_preset;
305     uint32_t acc_preset;
306     uint32_t jer_preset;
307 } mtg_tra_cmd_cfg_t;
308 
309 #ifdef __cplusplus
310 extern "C" {
311 #endif
312 
313 /**
314  * @brief MTG get trajectory predictor control register value
315  * @param [in] base - MTG base address
316  * @param [in] tra_index - trajectory predictor index
317  * @retval control register value
318  */
mtg_get_tra_control_status(MTG_Type * base,uint8_t tra_index)319 static inline uint32_t mtg_get_tra_control_status(MTG_Type *base, uint8_t tra_index)
320 {
321     assert(tra_index < 2);
322     return base->TRA[tra_index].CONTROL;
323 }
324 
325 /**
326  * @brief MTG get trajectory predictor commond control register value
327  * @param [in] base - MTG base address
328  * @param [in] tra_index - trajectory predictor index
329  * @param [in] cmd_index - trajectory predicotr command index
330  * @retval control register value
331  */
mtg_get_tra_cmd_control_status(MTG_Type * base,uint8_t tra_index,uint8_t cmd_index)332 static inline uint32_t mtg_get_tra_cmd_control_status(MTG_Type *base, uint8_t tra_index, uint8_t cmd_index)
333 {
334     assert((tra_index < 2) && (cmd_index < 4));
335     return base->TRA[tra_index].CMD[cmd_index].CONTROL;
336 }
337 
338 /**
339  * @brief MTG trigger trajectory predictor lock current values
340  * @param [in] base - MTG base address
341  * @param [in] tra_index - trajectory predictor index
342  */
mtg_trig_tra_lock(MTG_Type * base,uint8_t tra_index)343 static inline void mtg_trig_tra_lock(MTG_Type *base, uint8_t tra_index)
344 {
345     assert(tra_index < 2);
346     base->TRA[tra_index].CONTROL |= MTG_TRA_CONTROL_SW_LOCK_SET(1);
347 }
348 
349 /**
350  * @brief MTG clear trajectory predictor lock status
351  * @param [in] base - MTG base address
352  * @param [in] tra_index - trajectory predictor index
353  */
mtg_clear_tra_lock(MTG_Type * base,uint8_t tra_index)354 static inline void mtg_clear_tra_lock(MTG_Type *base, uint8_t tra_index)
355 {
356     assert(tra_index < 2);
357     base->TRA[tra_index].CONTROL &= ~MTG_TRA_CONTROL_SW_LOCK_MASK;
358 }
359 
360 /**
361  * @brief MTG get trajectory predictor revolution lock value
362  * @param [in] base - MTG base address
363  * @param [in] tra_index - trajectory predictor index
364  * @retval trajectory predictor revolution value
365  */
mtg_get_tra_rev_lock_value(MTG_Type * base,uint8_t tra_index)366 static inline int32_t mtg_get_tra_rev_lock_value(MTG_Type *base, uint8_t tra_index)
367 {
368     assert(tra_index < 2);
369     return base->TRA[tra_index].LOCK_REV;
370 }
371 
372 /**
373  * @brief MTG get trajectory predictor postion lock value
374  * @param [in] base - MTG base address
375  * @param [in] tra_index - trajectory predictor index
376  * @retval trajectory predictor postion value
377  */
mtg_get_tra_pos_lock_value(MTG_Type * base,uint8_t tra_index)378 static inline uint32_t mtg_get_tra_pos_lock_value(MTG_Type *base, uint8_t tra_index)
379 {
380     assert(tra_index < 2);
381     return base->TRA[tra_index].LOCK_POS;
382 }
383 
384 /**
385  * @brief MTG get trajectory predictor velocity lock value
386  * @param [in] base - MTG base address
387  * @param [in] tra_index - trajectory predictor index
388  * @retval trajectory predictor velocity value
389  */
mtg_get_tra_vel_lock_value(MTG_Type * base,uint8_t tra_index)390 static inline int32_t mtg_get_tra_vel_lock_value(MTG_Type *base, uint8_t tra_index)
391 {
392     assert(tra_index < 2);
393     return base->TRA[tra_index].LOCK_VEL;
394 }
395 
396 /**
397  * @brief MTG get trajectory predictor acceleration lock value
398  * @param [in] base - MTG base address
399  * @param [in] tra_index - trajectory predictor index
400  * @retval trajectory predictor acceleration value
401  */
mtg_get_tra_acc_lock_value(MTG_Type * base,uint8_t tra_index)402 static inline int32_t mtg_get_tra_acc_lock_value(MTG_Type *base, uint8_t tra_index)
403 {
404     assert(tra_index < 2);
405     return base->TRA[tra_index].LOCK_ACC;
406 }
407 
408 /**
409  * @brief MTG setup filter error bypass limit
410  * @param [in] base MTG base address
411  * @param [in] limit - bypass limit
412  */
mtg_filter_set_err_bypass_limit(MTG_Type * base,uint32_t limit)413 static inline void mtg_filter_set_err_bypass_limit(MTG_Type *base, uint32_t limit)
414 {
415     base->FILTER_ERROR_LIMIT_H = limit;
416 }
417 
418 /**
419  * @brief MTG setup hardware switch from bypass status to filter status's limit
420  *        When the difference between input and filter's pos result is smaller than the limit,
421  *        the filter will use the filter's result inside of the input.
422  * @param [in] base MTG base address
423  * @param [in] param - filter params
424  */
mtg_filter_set_bypass_switch_filter_limit(MTG_Type * base,uint32_t limit)425 static inline void mtg_filter_set_bypass_switch_filter_limit(MTG_Type *base, uint32_t limit)
426 {
427     base->FILTER_ERROR_LIMIT_L = limit;
428 }
429 
430 /**
431  * @brief MTG enable filter error bypass
432  *        When difference between input and filter's pos result is bigger than the limit,
433  *        the filter output will be bypass and input will be output to the filter's port.
434  * @param [in] base MTG base address
435  * @param [in] limit - bypass limit
436  */
mtg_filter_enable_bypass(MTG_Type * base)437 static inline void mtg_filter_enable_bypass(MTG_Type *base)
438 {
439     base->FILTER_CONTROL |= MTG_FILTER_CONTROL_ERR_BYPASS_F_I_EN_SET(1);
440     base->FILTER_CONTROL |= MTG_FILTER_CONTROL_ERR_BYPASS_EN_SET(1);
441 }
442 
443 /**
444  * @brief MTG disable filter error bypass
445  *        When difference between input and filter's pos result is bigger than the limit,
446  *        the filter output will be bypass and input will be output to the filter's port.
447  * @param [in] base MTG base address
448  * @param [in] limit - bypass limit
449  */
mtg_filter_disable_bypass(MTG_Type * base)450 static inline void mtg_filter_disable_bypass(MTG_Type *base)
451 {
452     base->FILTER_CONTROL &= ~MTG_FILTER_CONTROL_ERR_BYPASS_F_I_EN_MASK;
453     base->FILTER_CONTROL &= ~MTG_FILTER_CONTROL_ERR_BYPASS_EN_MASK;
454 }
455 
456 /**
457  * @brief MTG reset filter enable error reset
458  * @param [in] base MTG base address
459  */
mtg_filter_enable_reset_init(MTG_Type * base)460 static inline void mtg_filter_enable_reset_init(MTG_Type *base)
461 {
462     base->FILTER_CONTROL |= MTG_FILTER_CONTROL_ERR_INI_SET(1);
463 }
464 
465 /**
466  * @brief MTG reset filter disable error reset
467  * @param [in] base MTG base address
468  */
mtg_filter_disable_reset_init(MTG_Type * base)469 static inline void mtg_filter_disable_reset_init(MTG_Type *base)
470 {
471     base->FILTER_CONTROL &= ~MTG_FILTER_CONTROL_ERR_INI_MASK;
472 }
473 
474 /**
475  * @brief MTG enable hardware switch from bypass status to filter status's limit
476  *        When the difference between input and filter's pos result is smaller than the limit,
477  *        the filter will use the filter's result inside of the input.
478  *        The switch result can be defined by reading the switch status register.
479  * @param [in] base MTG base address
480  * @param [in] param - filter params
481  */
mtg_filter_switch_filter_result(MTG_Type * base)482 static inline void mtg_filter_switch_filter_result(MTG_Type *base)
483 {
484     base->FILTER_CONTROL |= MTG_FILTER_CONTROL_ERR_BYPASS_I_F_EN_SET(1);
485 }
486 
487 /**
488  * @brief MTG disable hardware switch from bypass status to filter status's limit
489  *        When the difference between input and filter's pos result is smaller than the limit,
490  *        the filter will use the filter's result inside of the input.
491  *        The switch result can be defined by reading the switch status register.
492  * @param [in] base MTG base address
493  * @param [in] param - filter params
494  */
mtg_filter_stop_auto_switch_filter_result(MTG_Type * base)495 static inline void mtg_filter_stop_auto_switch_filter_result(MTG_Type *base)
496 {
497     base->FILTER_CONTROL &= ~MTG_FILTER_CONTROL_ERR_BYPASS_I_F_EN_MASK;
498 }
499 
500 /**
501  * @brief MTG setup hardware switch from bypass status to filter status's limit
502  *        When the difference between input and filter's pos result is smaller than the limit,
503  *        the filter will use the filter's result inside of the input.
504  * @param [in] base - MTG base address
505  * @retval bypass status
506  */
mtg_get_err_bypass_status(MTG_Type * base)507 static inline bool mtg_get_err_bypass_status(MTG_Type *base)
508 {
509     return MTG_FILTER_CONTROL_ERR_BYPASS_STATUS_GET(base->FILTER_CONTROL) == 0 ? true : false;
510 }
511 
512 /**
513  * @brief MTG set time0 adjust value
514  * @param [in] base - MTG base address
515  * @param [in] value - adjust value
516  */
mtg_set_time0_adjust_value(MTG_Type * base,uint32_t value)517 static inline void mtg_set_time0_adjust_value(MTG_Type *base, uint32_t value)
518 {
519     base->FILTER_TIME0_SW_ADJUST = value;
520 }
521 
522 /**
523  * @brief MTG set time1 adjust value
524  * @param [in] base - MTG base address
525  * @param [in] value - adjust value
526  */
mtg_set_time1_adjust_value(MTG_Type * base,uint32_t value)527 static inline void mtg_set_time1_adjust_value(MTG_Type *base, uint32_t value)
528 {
529     base->FILTER_TIME1_SW_ADJUST = value;
530 }
531 
532 /**
533  * @brief MTG set time shift value
534  * @param [in] base - MTG base address
535  * @param [in] index - tra index
536  * @param [in] jer_shift - jerk shift
537  * @param [in] acc_shift - acceleration shift
538  * @param [in] vel_shift - velocity shift
539  */
mtg_tra_set_shift(MTG_Type * base,uint8_t index,uint8_t jer_shift,uint8_t acc_shift,uint8_t vel_shift)540 static inline void mtg_tra_set_shift(MTG_Type *base, uint8_t index, uint8_t jer_shift, uint8_t acc_shift, uint8_t vel_shift)
541 {
542     assert(index < 2);
543     base->TRA[index].SHIFT = MTG_TRA_SHIFT_JER_SHIFT_SET(jer_shift) |
544                              MTG_TRA_SHIFT_ACC_SHIFT_SET(acc_shift) |
545                              MTG_TRA_SHIFT_VEL_SHIFT_SET(vel_shift);
546 }
547 
548 /**
549  * @brief MTG global reset
550  * @param [in] base - MTG base address
551  */
mtg_set_global_reset(MTG_Type * base)552 static inline void mtg_set_global_reset(MTG_Type *base)
553 {
554     base->SW_GLB_RESET = 1;
555 }
556 
557 /**
558  * @brief MTG stop global reset
559  */
mtg_stop_global_reset(MTG_Type * base)560 static inline void mtg_stop_global_reset(MTG_Type *base)
561 {
562     base->SW_GLB_RESET = 0;
563 }
564 
565 /**
566  * @brief MTG predictor get lock result
567  * @param [in] base MTG base address
568  * @param [in] tra_index - trajectory predictor index
569  * @param [out] para mtg_lock_value_t
570  */
571 void mtg_get_tra_lock_result(MTG_Type *base, uint8_t tra_index, mtg_lock_value_t *para);
572 
573 /**
574  * @brief MTG setup event params
575  * @param [in] base MTG base address
576  * @param [in] event_index - event index
577  * @param [in] param - event params
578  * @param [out] setup status
579  */
580 hpm_stat_t mtg_setup_event(MTG_Type *base, uint8_t event_index, mtg_event_param_t *param);
581 
582 /**
583  * @brief MTG setup trajectory limit params
584  * @param [in] base MTG base address
585  * @param [in] tra_index - trajectory index
586  * @param [in] param - trajectory limit params
587  * @param [out] setup status
588  */
589 hpm_stat_t mtg_setup_tra_limit(MTG_Type *base, uint8_t tra_index, mtg_tra_limit_param_t *param);
590 
591 /**
592  * @brief MTG setup trajectory software one_way mode
593  * @param [in] base MTG base address
594  * @param [in] tra_index - trajectory index
595  * @param [in] param - 0:force + , 1:force -
596  * @param [out] setup status
597  */
598 void mtg_setup_tra_software_pos_one_way_mode(MTG_Type *base, uint8_t tra_index, mtg_software_force_one_way_mode_t param);
599 
600 /**
601  * @brief MTG setup trajectory hardware one_way mode
602  * @param [in] base MTG base address
603  * @param [in] tra_index - trajectory index
604  * @param [in] param - 0:force + , 1:force -
605  * @param [out] setup status
606  */
607 void mtg_setup_tra_hardware_pos_one_way_mode(MTG_Type *base, uint8_t tra_index, mtg_hardware_force_one_way_mode_t *param);
608 
609 /**
610  * @brief MTG disable trajectory postion one_way mode
611  * @param [in] base MTG base address
612  * @param [in] tra_index - trajectory index
613  */
614 void mtg_disable_tra_pos_one_way_mode(MTG_Type *base, uint8_t tra_index);
615 
616 /**
617  * @brief MTG setup trajectory velocity one_way mode
618  * @param [in] base MTG base address
619  * @param [in] tra_index - trajectory index
620  * @param [in] mode - 0:bigger_or_eq_zero , 1:smaller_or_eq_zero
621  */
622 void mtg_setup_tra_vel_one_way(MTG_Type *base, uint8_t tra_index, vel_one_way_mode_t mode, bool enable);
623 
624 /**
625  * @brief MTG setup filter
626  * @param [in] base MTG base address
627  * @param [in] param - filter params
628  */
629 void mtg_filter_get_default_filter_stage_param(mtg_filter_param_t *param);
630 
631 /**
632  * @brief MTG setup time compensation module
633  * @param [in] base MTG base address
634  * @param [in] param - time compensation params
635  */
636 void mtg_setup_time(MTG_Type *base, mtg_time_init_param_t *param);
637 
638 /**
639  * @brief MTG setup filter
640  * @param [in] base MTG base address
641  * @param [in] param - filter params
642  */
643 void mtg_setup_filter(MTG_Type *base, mtg_filter_param_t *param);
644 
645 /**
646  * @brief MTG setup timeout
647  * @param [in] base MTG base address
648  * @param [in] param - timeout params
649  */
650 void mtg_setup_timeout(MTG_Type *base, mtg_timeout_param_t *param);
651 
652 /**
653  * @brief MTG link trigger source and trajectory command
654  * @param [in] base MTG base address
655  * @param [in] tra_index - trajectory index
656  * @param [in] link_cfg - link config
657  * @param [in] cmd_cfg - command config
658  */
659 void mtg_setup_link_cfg(MTG_Type *base, uint8_t tra_index, mtg_link_cfg_t link_cfg, mtg_tra_cmd_cfg_t *cmd_cfg);
660 
661 /**
662  * @brief MTG software trigger trajectory command
663  * @param [in] base MTG base address
664  */
665 void mtg_soft_event_trigger(MTG_Type *base);
666 
667 /**
668  * @brief Get default trajectory shift
669  * @param [out] cfg pointer to the tra shift var
670  */
671 void mtg_get_default_tra_shift(mtg_tra_shift_t *cfg);
672 
673 /**
674  * @brief Get the event irq status
675  * @param [in] ptr the mtg base
676  * @param [in] idx the event index
677  */
mtg_get_irq_status(MTG_Type * ptr,uint8_t idx)678 static inline bool mtg_get_irq_status(MTG_Type *ptr, uint8_t idx)
679 {
680     return ((MTG_EVENT_CONTROL_EVENT_IRQ_GET(ptr->EVENT[idx].CONTROL) != 0) ? true : false);
681 }
682 
683 /**
684  * @brief Clear the event irq status
685  * @param [in] ptr the mtg base
686  * @param [in] idx the event index
687  */
mtg_clear_irq_status(MTG_Type * ptr,uint8_t idx)688 static inline void mtg_clear_irq_status(MTG_Type *ptr, uint8_t idx)
689 {
690     ptr->EVENT[idx].CONTROL |= MTG_EVENT_CONTROL_EVENT_IRQ_MASK;
691 }
692 
693 /**
694  * @brief calculate the vel preset
695  * @param [in] base MTG base address
696  * @param [in] clock MTG clock name
697  * @param [in] tra_index - trajectory index
698  * @param [in] speed - speed in r/s
699  */
700 int32_t mtg_calc_vel_preset(MTG_Type *base, clock_name_t clock, uint8_t tra_index, float speed);
701 
702 /**
703  * @brief calculate the acc preset
704  * @param [in] base MTG base address
705  * @param [in] clock MTG clock name
706  * @param [in] tra_index - trajectory index
707  * @param [in] acc - acc in r/s2
708  */
709 int32_t mtg_calc_acc_preset(MTG_Type *base, clock_name_t clock, uint8_t tra_index, float acc);
710 
711 /**
712  * @brief MTG link trigger source and trajectory command
713  * @param [in] base MTG base address
714  * @param [in] clock MTG clock name
715  * @param [in] tra_index - trajectory index
716  * @param [in] jer - jer in r/s3
717  */
718 int32_t mtg_calc_jer_preset(MTG_Type *base, clock_name_t clock, uint8_t tra_index, float jer);
719 
720 #ifdef __cplusplus
721 }
722 #endif
723 /**
724  * @}
725  */
726 #endif /* HPM_MTG_DRV_H */
727