HPM SDK
HPMicro Software Development Kit
hpm_esc_drv.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2024 HPMicro
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 
9 #ifndef HPM_ESC_DRV_H
10 #define HPM_ESC_DRV_H
11 
12 #include "hpm_common.h"
13 #include "hpm_esc_regs.h"
14 
26 enum {
29 };
30 
31 
32 typedef enum {
36 
37 typedef enum {
38  esc_eeprom_idle_cmd = 0, /* clear error bits */
43 
44 typedef enum {
55 
56 /*
57  * @brief ESC IRQ mask
58  *
59  * @note The ESC(PDI) IRQ is always enabled, and this enum is only used to control sync0/sync1 and reset irq.
60  */
61 typedef enum {
68 
69 
70 typedef struct {
76 
77 
78 #ifdef __cplusplus
79 extern "C" {
80 #endif
81 
88 static inline void esc_core_enable_clock(ESC_Type *ptr, bool enable)
89 {
90  if (enable) {
92  } else {
94  }
95 }
96 
103 static inline void esc_phy_enable_clock(ESC_Type *ptr, bool enable)
104 {
105  if (enable) {
107  } else {
109  }
110 }
111 
119 {
120  uint32_t gpr_cfg0 = ptr->GPR_CFG0;
121 
122  if (config->eeprom_emulation) {
123  gpr_cfg0 |= ESC_GPR_CFG0_EEPROM_EMU_MASK;
125  } else {
126  gpr_cfg0 &= ~ESC_GPR_CFG0_EEPROM_EMU_MASK;
127  gpr_cfg0 |= ESC_GPR_CFG0_I2C_SCLK_EN_MASK;
128  if (config->eeprom_size_over_16kbit) {
129  gpr_cfg0 |= ESC_GPR_CFG0_PROM_SIZE_MASK;
130  } else {
131  gpr_cfg0 &= ~ESC_GPR_CFG0_PROM_SIZE_MASK;
132  }
133  }
134  ptr->GPR_CFG0 = gpr_cfg0;
135  esc_core_enable_clock(ptr, config->core_clock_en);
136  esc_phy_enable_clock(ptr, config->phy_refclk_en);
137 }
138 
147 static inline void esc_config_ctrl_signal_function(ESC_Type *ptr, uint8_t index, esc_ctrl_signal_function_t func, bool invert)
148 {
149  ptr->IO_CFG[index] = ESC_IO_CFG_FUNC_ALT_SET(func) | ESC_IO_CFG_INVERT_SET(invert);
150 }
151 
160 static inline void esc_config_nmii_link_source(ESC_Type *ptr, bool link0_from_io, bool link1_from_io, bool link2_from_io)
161 {
162  if (link0_from_io) {
164  } else {
166  ptr->GPR_CFG2 |= ESC_GPR_CFG2_NMII_LINK0_GPR_MASK; /* config GRP to indicate LINK0 is invalid by default */
167  }
168 
169  if (link1_from_io) {
171  } else {
173  ptr->GPR_CFG2 |= ESC_GPR_CFG2_NMII_LINK1_GPR_MASK; /* config GRP to indicate LINK1 is invalid by default */
174  }
175 
176  if (link2_from_io) {
178  } else {
180  ptr->GPR_CFG2 |= ESC_GPR_CFG2_NMII_LINK2_GPR_MASK; /* config GRP to indicate LINK2 is invalid by default */
181  }
182 }
183 
184 /* config ESC reset request source: ESC core or GRP_REG value */
191 static inline void esc_config_reset_source(ESC_Type *ptr, bool reset_from_ecat_core)
192 {
193  if (reset_from_ecat_core) {
195  } else {
197  }
198 }
199 
206 static inline void esc_pdi_reset(ESC_Type *ptr)
207 {
208  ptr->ESC_RST_PDI = 0x52; /* R */
209  ptr->ESC_RST_PDI = 0x45; /* E */
210  ptr->ESC_RST_PDI = 0x53; /* S */
211 }
212 
219 static inline void esc_set_phy_offset(ESC_Type *ptr, uint8_t offset)
220 {
222 }
223 
230 {
232 }
233 
240 {
242 }
243 
252 hpm_stat_t esc_mdio_read(ESC_Type *ptr, uint8_t phy_addr, uint8_t reg_addr, uint16_t *data);
253 
262 hpm_stat_t esc_mdio_write(ESC_Type *ptr, uint8_t phy_addr, uint8_t reg_addr, uint16_t data);
263 
275 
282 static inline uint8_t esc_get_eeprom_cmd(ESC_Type *ptr)
283 {
285 }
286 
295 static inline void esc_eeprom_emulation_ack(ESC_Type *ptr, esc_eeprom_cmd_t cmd, bool ack_err, bool crc_err)
296 {
297  uint16_t temp = ESC_EEPROM_CTRL_STAT_CMD_SET(cmd);
298  if (ack_err) {
300  }
301  if (crc_err) {
303  }
304 
305  ptr->EEPROM_CTRL_STAT = temp;
306 }
307 
314 static inline uint32_t esc_get_eeprom_byte_address(ESC_Type *ptr)
315 {
316  return (ptr->EEPROM_ADDR) << 1U;
317 }
318 
325 static inline uint32_t esc_get_eeprom_word_address(ESC_Type *ptr)
326 {
327  return ptr->EEPROM_ADDR;
328 }
329 
336 static inline uint64_t esc_read_eeprom_data(ESC_Type *ptr)
337 {
338  return ptr->EEPROM_DATA;
339 }
340 
347 static inline void esc_write_eeprom_data(ESC_Type *ptr, uint64_t data)
348 {
349  ptr->EEPROM_DATA = data;
350 }
351 
358 static inline void esc_config_latch0_source(ESC_Type *ptr, bool latch0_from_ntm)
359 {
360  if (latch0_from_ntm) {
362  } else {
364  }
365 }
366 
373 static inline void esc_config_latch1_source(ESC_Type *ptr, bool latch0_from_trigmux)
374 {
375  if (latch0_from_trigmux) {
377  } else {
379  }
380 }
381 
391 static inline void esc_enable_irq(ESC_Type *ptr, esc_irq_mask_t irq_mask)
392 {
393  ptr->GPR_CFG1 |= (irq_mask & esc_irq_mask_all);
394 }
395 
405 static inline void esc_disable_irq(ESC_Type *ptr, esc_irq_mask_t irq_mask)
406 {
407  ptr->GPR_CFG1 &= ~(irq_mask & esc_irq_mask_all);
408 }
409 
410 #if defined(HPM_IP_FEATURE_ESC_SYNC_IRQ_MASK) && HPM_IP_FEATURE_ESC_SYNC_IRQ_MASK
420 static inline void esc_enable_sync_irq_to_pdi_irq(ESC_Type *ptr, bool sync0_irq, bool sync1_irq)
421 {
423  | (ESC_GPR_CFG0_SYNC0_PDI_IRQEN_SET(sync0_irq))
424  | (ESC_GPR_CFG0_SYNC1_PDI_IRQEN_SET(sync1_irq));
425 }
426 #endif
427 
428 #if defined(HPM_IP_FEATURE_ESC_PORT_DIS) && HPM_IP_FEATURE_ESC_PORT_DIS
439 static inline void esc_change_port_description(ESC_Type *ptr, bool port0, bool port1, bool port2)
440 {
442  | (ESC_GPR_CFG0_PORT0_DIS_SET(port0))
443  | (ESC_GPR_CFG0_PORT1_DIS_SET(port1))
444  | (ESC_GPR_CFG0_PORT2_DIS_SET(port2));
445 }
446 #endif
447 
448 #ifdef __cplusplus
449 }
450 #endif
454 #endif /* HPM_ESC_DRV_H */
#define ESC_GPR_CFG2_NMII_LINK1_FROM_IO_MASK
Definition: hpm_esc_regs.h:4086
#define ESC_GPR_CFG1_SYNC0_IRQ_EN_MASK
Definition: hpm_esc_regs.h:3993
#define ESC_EEPROM_CTRL_STAT_CMD_SET(x)
Definition: hpm_esc_regs.h:1809
#define ESC_GPR_CFG0_PORT0_DIS_MASK
Definition: hpm_esc_regs.h:3914
#define ESC_GPR_CFG0_SYNC1_PDI_IRQEN_MASK
Definition: hpm_esc_regs.h:3943
#define ESC_GPR_CFG0_SYNC1_PDI_IRQEN_SET(x)
Definition: hpm_esc_regs.h:3945
#define ESC_GPR_CFG2_NMII_LINK2_GPR_MASK
Definition: hpm_esc_regs.h:4077
#define ESC_GPR_CFG0_CLK100_EN_MASK
Definition: hpm_esc_regs.h:3923
#define ESC_IO_CFG_INVERT_SET(x)
Definition: hpm_esc_regs.h:4535
#define ESC_PHY_CFG0_PHY_OFFSET_VAL_MASK
Definition: hpm_esc_regs.h:4133
#define ESC_GPR_CFG0_EEPROM_EMU_MASK
Definition: hpm_esc_regs.h:3933
#define ESC_GPR_CFG2_NMII_LINK1_GPR_MASK
Definition: hpm_esc_regs.h:4095
#define ESC_GPR_CFG1_LATCH1_FROM_IO_MASK
Definition: hpm_esc_regs.h:4030
#define ESC_GPR_CFG0_PORT0_DIS_SET(x)
Definition: hpm_esc_regs.h:3916
#define ESC_GPR_CFG1_SYNC1_IRQ_EN_MASK
Definition: hpm_esc_regs.h:3984
#define ESC_GPR_CFG1_LATCH0_FROM_IO_MASK
Definition: hpm_esc_regs.h:4040
#define ESC_GPR_CFG2_NMII_LINK2_FROM_IO_MASK
Definition: hpm_esc_regs.h:4068
#define ESC_GPR_CFG0_PORT2_DIS_MASK
Definition: hpm_esc_regs.h:3894
#define ESC_GPR_CFG2_NMII_LINK0_GPR_MASK
Definition: hpm_esc_regs.h:4113
#define ESC_GPR_CFG2_NMII_LINK0_FROM_IO_MASK
Definition: hpm_esc_regs.h:4104
#define ESC_GPR_CFG1_RSTO_IRQ_EN_MASK
Definition: hpm_esc_regs.h:4002
#define ESC_PHY_CFG1_REFCK_25M_OE_MASK
Definition: hpm_esc_regs.h:4216
#define ESC_EEPROM_CTRL_STAT_CMD_GET(x)
Definition: hpm_esc_regs.h:1810
#define ESC_MIIM_PDI_ACC_STAT_ACC_MASK
Definition: hpm_esc_regs.h:2109
#define ESC_IO_CFG_FUNC_ALT_SET(x)
Definition: hpm_esc_regs.h:4554
#define ESC_GPR_CFG0_I2C_SCLK_EN_MASK
Definition: hpm_esc_regs.h:3962
#define ESC_GPR_CFG0_SYNC0_PDI_IRQEN_MASK
Definition: hpm_esc_regs.h:3953
#define ESC_GPR_CFG0_PORT1_DIS_MASK
Definition: hpm_esc_regs.h:3904
#define ESC_GPR_CFG0_PORT2_DIS_SET(x)
Definition: hpm_esc_regs.h:3896
#define ESC_GPR_CFG0_SYNC0_PDI_IRQEN_SET(x)
Definition: hpm_esc_regs.h:3955
#define ESC_EEPROM_CTRL_STAT_ERR_ACK_CMD_MASK
Definition: hpm_esc_regs.h:1760
#define ESC_PHY_CFG0_PHY_OFFSET_VAL_SHIFT
Definition: hpm_esc_regs.h:4134
#define ESC_EEPROM_CTRL_STAT_CKSM_ERR_MASK
Definition: hpm_esc_regs.h:1787
#define ESC_GPR_CFG0_PORT1_DIS_SET(x)
Definition: hpm_esc_regs.h:3906
#define ESC_GPR_CFG0_PROM_SIZE_MASK
Definition: hpm_esc_regs.h:3974
#define ESC_GPR_CFG1_RSTO_OVRD_ENJ_MASK
Definition: hpm_esc_regs.h:4058
uint32_t hpm_stat_t
Definition: hpm_common.h:135
#define MAKE_STATUS(group, code)
Definition: hpm_common.h:144
@ status_group_esc
Definition: hpm_common.h:175
static void esc_core_enable_clock(ESC_Type *ptr, bool enable)
ESC peripheral clock.
Definition: hpm_esc_drv.h:88
static void esc_enable_irq(ESC_Type *ptr, esc_irq_mask_t irq_mask)
ESC enable specific IRQ sources.
Definition: hpm_esc_drv.h:391
static uint64_t esc_read_eeprom_data(ESC_Type *ptr)
ESC read eeprom data from register, this function is using in eeprom emulation function.
Definition: hpm_esc_drv.h:336
static void esc_enable_pdi_access_mii_management(ESC_Type *ptr)
ESC enable PDI to access MII management.
Definition: hpm_esc_drv.h:229
hpm_stat_t esc_mdio_write(ESC_Type *ptr, uint8_t phy_addr, uint8_t reg_addr, uint16_t data)
ESc write PHY register via ESC MII Management Interface.
Definition: hpm_esc_drv.c:66
esc_eeprom_cmd_t
Definition: hpm_esc_drv.h:37
esc_ctrl_signal_function_t
Definition: hpm_esc_drv.h:44
static uint32_t esc_get_eeprom_word_address(ESC_Type *ptr)
ESC get eeprom word(2 bytes) address.
Definition: hpm_esc_drv.h:325
static void esc_config_latch0_source(ESC_Type *ptr, bool latch0_from_ntm)
ESC config latch0 signal source.
Definition: hpm_esc_drv.h:358
static uint32_t esc_get_eeprom_byte_address(ESC_Type *ptr)
ESC get eeprom byte address.
Definition: hpm_esc_drv.h:314
static void esc_config_latch1_source(ESC_Type *ptr, bool latch0_from_trigmux)
ESC config latch1 signal source.
Definition: hpm_esc_drv.h:373
static void esc_pdi_reset(ESC_Type *ptr)
ESC generate reset signal to ESC_RESET interrupt and RESET_OUT pin.
Definition: hpm_esc_drv.h:206
static uint8_t esc_get_eeprom_cmd(ESC_Type *ptr)
ESC get eeprom cmd, this using in eeprom emulation function.
Definition: hpm_esc_drv.h:282
hpm_stat_t esc_check_eeprom_loading(ESC_Type *ptr)
ESC check eeprom loading data status.
Definition: hpm_esc_drv.c:114
esc_irq_mask_t
Definition: hpm_esc_drv.h:61
static void esc_phy_enable_clock(ESC_Type *ptr, bool enable)
ESC PHY clock.
Definition: hpm_esc_drv.h:103
static void esc_write_eeprom_data(ESC_Type *ptr, uint64_t data)
ESC write eeprom data to register, this function is using in eeprom emulation function.
Definition: hpm_esc_drv.h:347
hpm_stat_t esc_mdio_read(ESC_Type *ptr, uint8_t phy_addr, uint8_t reg_addr, uint16_t *data)
ESC read PHY register via ESC MII Management Interface.
Definition: hpm_esc_drv.c:14
static void esc_config_reset_source(ESC_Type *ptr, bool reset_from_ecat_core)
ESC config reset signal source.
Definition: hpm_esc_drv.h:191
static void esc_disable_pdi_access_mii_management(ESC_Type *ptr)
ESC disable PDI to access MII management.
Definition: hpm_esc_drv.h:239
static void esc_config_eeprom_and_clock(ESC_Type *ptr, esc_eeprom_clock_config_t *config)
ESC config eeprom attributes(emulation and size) and peripheral clock.
Definition: hpm_esc_drv.h:118
esc_latch_source_t
Definition: hpm_esc_drv.h:32
static void esc_config_ctrl_signal_function(ESC_Type *ptr, uint8_t index, esc_ctrl_signal_function_t func, bool invert)
ESC assign specific function to CTRL signal.
Definition: hpm_esc_drv.h:147
static void esc_eeprom_emulation_ack(ESC_Type *ptr, esc_eeprom_cmd_t cmd, bool ack_err, bool crc_err)
ESC ack eeprom cmd in eeprom emualtion function.
Definition: hpm_esc_drv.h:295
static void esc_config_nmii_link_source(ESC_Type *ptr, bool link0_from_io, bool link1_from_io, bool link2_from_io)
ESC config nmii_link signal source.
Definition: hpm_esc_drv.h:160
static void esc_set_phy_offset(ESC_Type *ptr, uint8_t offset)
ESC set phy offset.
Definition: hpm_esc_drv.h:219
static void esc_disable_irq(ESC_Type *ptr, esc_irq_mask_t irq_mask)
ESC disable specific IRQ sources.
Definition: hpm_esc_drv.h:405
@ status_esc_eeprom_ack_error
Definition: hpm_esc_drv.h:27
@ status_esc_eeprom_checksum_error
Definition: hpm_esc_drv.h:28
@ esc_eeprom_write_cmd
Definition: hpm_esc_drv.h:40
@ esc_eeprom_read_cmd
Definition: hpm_esc_drv.h:39
@ esc_eeprom_idle_cmd
Definition: hpm_esc_drv.h:38
@ esc_eeprom_reload_cmd
Definition: hpm_esc_drv.h:41
@ esc_ctrl_signal_func_alt_led_err
Definition: hpm_esc_drv.h:52
@ esc_ctrl_signal_func_alt_reset_out
Definition: hpm_esc_drv.h:53
@ esc_ctrl_signal_func_alt_link_act1
Definition: hpm_esc_drv.h:49
@ esc_ctrl_signal_func_alt_link_act0
Definition: hpm_esc_drv.h:48
@ esc_ctrl_signal_func_alt_nmii_link2
Definition: hpm_esc_drv.h:47
@ esc_ctrl_signal_func_alt_led_run
Definition: hpm_esc_drv.h:51
@ esc_ctrl_signal_func_alt_link_act2
Definition: hpm_esc_drv.h:50
@ esc_ctrl_signal_func_alt_nmii_link0
Definition: hpm_esc_drv.h:45
@ esc_ctrl_signal_func_alt_nmii_link1
Definition: hpm_esc_drv.h:46
@ esc_irq_mask_none
Definition: hpm_esc_drv.h:62
@ esc_irq_mask_all
Definition: hpm_esc_drv.h:66
@ esc_sync0_irq_mask
Definition: hpm_esc_drv.h:63
@ esc_reset_irq_mask
Definition: hpm_esc_drv.h:65
@ esc_sync1_irq_mask
Definition: hpm_esc_drv.h:64
@ latch_source_from_ntm
Definition: hpm_esc_drv.h:33
@ latch_source_from_trigger_mux
Definition: hpm_esc_drv.h:34
Definition: hpm_esc_regs.h:12
__RW uint32_t GPR_CFG2
Definition: hpm_esc_regs.h:186
__RW uint32_t EEPROM_ADDR
Definition: hpm_esc_regs.h:85
__RW uint8_t ESC_RST_PDI
Definition: hpm_esc_regs.h:32
__RW uint32_t PHY_CFG0
Definition: hpm_esc_regs.h:188
__RW uint16_t EEPROM_CTRL_STAT
Definition: hpm_esc_regs.h:84
__RW uint64_t EEPROM_DATA
Definition: hpm_esc_regs.h:86
__RW uint32_t GPR_CFG0
Definition: hpm_esc_regs.h:184
__RW uint32_t IO_CFG[9]
Definition: hpm_esc_regs.h:204
__RW uint32_t GPR_CFG1
Definition: hpm_esc_regs.h:185
__RW uint8_t MIIM_PDI_ACC_STAT
Definition: hpm_esc_regs.h:92
__RW uint32_t PHY_CFG1
Definition: hpm_esc_regs.h:189
Definition: hpm_esc_drv.h:70
bool phy_refclk_en
Definition: hpm_esc_drv.h:74
bool eeprom_size_over_16kbit
Definition: hpm_esc_drv.h:72
bool eeprom_emulation
Definition: hpm_esc_drv.h:71
bool core_clock_en
Definition: hpm_esc_drv.h:73