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 typedef struct {
63 
64 
65 #ifdef __cplusplus
66 extern "C" {
67 #endif
68 
75 static inline void esc_core_enable_clock(ESC_Type *ptr, bool enable)
76 {
77  if (enable) {
79  } else {
81  }
82 }
83 
90 static inline void esc_phy_enable_clock(ESC_Type *ptr, bool enable)
91 {
92  if (enable) {
94  } else {
96  }
97 }
98 
106 {
107  uint32_t gpr_cfg0 = ptr->GPR_CFG0;
108 
109  if (config->eeprom_emulation) {
110  gpr_cfg0 |= ESC_GPR_CFG0_EEPROM_EMU_MASK;
112  } else {
113  gpr_cfg0 &= ~ESC_GPR_CFG0_EEPROM_EMU_MASK;
114  gpr_cfg0 |= ESC_GPR_CFG0_I2C_SCLK_EN_MASK;
115  if (config->eeprom_size_over_16kbit) {
116  gpr_cfg0 |= ESC_GPR_CFG0_PROM_SIZE_MASK;
117  } else {
118  gpr_cfg0 &= ~ESC_GPR_CFG0_PROM_SIZE_MASK;
119  }
120  }
121  ptr->GPR_CFG0 = gpr_cfg0;
122  esc_core_enable_clock(ptr, config->core_clock_en);
123  esc_phy_enable_clock(ptr, config->phy_refclk_en);
124 }
125 
134 static inline void esc_config_ctrl_signal_function(ESC_Type *ptr, uint8_t index, esc_ctrl_signal_function_t func, bool invert)
135 {
136  ptr->IO_CFG[index] = ESC_IO_CFG_FUNC_ALT_SET(func) | ESC_IO_CFG_INVERT_SET(invert);
137 }
138 
147 static inline void esc_config_nmii_link_source(ESC_Type *ptr, bool link0_from_io, bool link1_from_io, bool link2_from_io)
148 {
149  if (link0_from_io) {
151  } else {
153  ptr->GPR_CFG2 |= ESC_GPR_CFG2_NMII_LINK0_GPR_MASK; /* config GRP to indicate LINK0 is invalid by default */
154  }
155 
156  if (link1_from_io) {
158  } else {
160  ptr->GPR_CFG2 |= ESC_GPR_CFG2_NMII_LINK1_GPR_MASK; /* config GRP to indicate LINK1 is invalid by default */
161  }
162 
163  if (link2_from_io) {
165  } else {
167  ptr->GPR_CFG2 |= ESC_GPR_CFG2_NMII_LINK2_GPR_MASK; /* config GRP to indicate LINK2 is invalid by default */
168  }
169 }
170 
171 /* config ESC reset request source: ESC core or GRP_REG value */
178 static inline void esc_config_reset_source(ESC_Type *ptr, bool reset_from_ecat_core)
179 {
180  if (reset_from_ecat_core) {
182  } else {
184  }
185 }
186 
193 static inline void esc_pdi_reset(ESC_Type *ptr)
194 {
195  ptr->ESC_RST_PDI = 0x52; /* R */
196  ptr->ESC_RST_PDI = 0x45; /* E */
197  ptr->ESC_RST_PDI = 0x53; /* S */
198 }
199 
206 static inline void esc_set_phy_offset(ESC_Type *ptr, uint8_t offset)
207 {
209 }
210 
217 {
219 }
220 
227 {
229 }
230 
239 hpm_stat_t esc_mdio_read(ESC_Type *ptr, uint8_t phy_addr, uint8_t reg_addr, uint16_t *data);
240 
249 hpm_stat_t esc_mdio_write(ESC_Type *ptr, uint8_t phy_addr, uint8_t reg_addr, uint16_t data);
250 
262 
269 static inline uint8_t esc_get_eeprom_cmd(ESC_Type *ptr)
270 {
272 }
273 
282 static inline void esc_eeprom_emulation_ack(ESC_Type *ptr, esc_eeprom_cmd_t cmd, bool ack_err, bool crc_err)
283 {
284  uint16_t temp = ESC_EEPROM_CTRL_STAT_CMD_SET(cmd);
285  if (ack_err) {
287  }
288  if (crc_err) {
290  }
291 
292  ptr->EEPROM_CTRL_STAT = temp;
293 }
294 
301 static inline uint32_t esc_get_eeprom_byte_address(ESC_Type *ptr)
302 {
303  return (ptr->EEPROM_ADDR) << 1U;
304 }
305 
312 static inline uint32_t esc_get_eeprom_word_address(ESC_Type *ptr)
313 {
314  return ptr->EEPROM_ADDR;
315 }
316 
323 static inline uint64_t esc_read_eeprom_data(ESC_Type *ptr)
324 {
325  return ptr->EEPROM_DATA;
326 }
327 
334 static inline void esc_write_eeprom_data(ESC_Type *ptr, uint64_t data)
335 {
336  ptr->EEPROM_DATA = data;
337 }
338 
345 static inline void esc_config_latch0_source(ESC_Type *ptr, bool latch0_from_ntm)
346 {
347  if (latch0_from_ntm) {
349  } else {
351  }
352 }
353 
360 static inline void esc_config_latch1_source(ESC_Type *ptr, bool latch0_from_trigmux)
361 {
362  if (latch0_from_trigmux) {
364  } else {
366  }
367 }
368 
369 #if defined(HPM_IP_FEATURE_ESC_SYNC_IRQ_MASK) && HPM_IP_FEATURE_ESC_SYNC_IRQ_MASK
379 static inline void esc_enable_sync_irq_to_pdi_irq(ESC_Type *ptr, bool sync0_irq, bool sync1_irq)
380 {
381  ptr->GPR_CFG0 = (ptr->GPR_CFG0 & ~(ESC_GPR_CFG0_SYNC0_PDI_IRQEN_MASK | ESC_GPR_CFG0_SYNC1_PDI_IRQEN_MASK))
382  | (ESC_GPR_CFG0_SYNC0_PDI_IRQEN_SET(sync0_irq))
383  | (ESC_GPR_CFG0_SYNC1_PDI_IRQEN_SET(sync1_irq));
384 }
385 #endif
386 
387 #if defined(HPM_IP_FEATURE_ESC_PORT_DIS) && HPM_IP_FEATURE_ESC_PORT_DIS
398 static inline void esc_change_port_description(ESC_Type *ptr, bool port0, bool port1, bool port2)
399 {
400  ptr->GPR_CFG0 = (ptr->GPR_CFG0 & ~(ESC_GPR_CFG0_PORT0_DIS_MASK | ESC_GPR_CFG0_PORT1_DIS_MASK | ESC_GPR_CFG0_PORT2_DIS_MASK))
401  | (ESC_GPR_CFG0_PORT0_DIS_SET(port0))
402  | (ESC_GPR_CFG0_PORT1_DIS_SET(port1))
403  | (ESC_GPR_CFG0_PORT2_DIS_SET(port2));
404 }
405 #endif
406 
407 #ifdef __cplusplus
408 }
409 #endif
413 #endif /* HPM_ESC_DRV_H */
uint32_t hpm_stat_t
Definition: hpm_common.h:126
#define MAKE_STATUS(group, code)
Definition: hpm_common.h:135
@ status_group_esc
Definition: hpm_common.h:166
static void esc_core_enable_clock(ESC_Type *ptr, bool enable)
ESC peripheral clock.
Definition: hpm_esc_drv.h:75
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:323
static void esc_enable_pdi_access_mii_management(ESC_Type *ptr)
ESC enable PDI to access MII management.
Definition: hpm_esc_drv.h:216
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:312
static void esc_config_latch0_source(ESC_Type *ptr, bool latch0_from_ntm)
ESC config latch0 signal source.
Definition: hpm_esc_drv.h:345
static uint32_t esc_get_eeprom_byte_address(ESC_Type *ptr)
ESC get eeprom byte address.
Definition: hpm_esc_drv.h:301
static void esc_config_latch1_source(ESC_Type *ptr, bool latch0_from_trigmux)
ESC config latch1 signal source.
Definition: hpm_esc_drv.h:360
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:193
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:269
hpm_stat_t esc_check_eeprom_loading(ESC_Type *ptr)
ESC check eeprom loading data status.
Definition: hpm_esc_drv.c:114
static void esc_phy_enable_clock(ESC_Type *ptr, bool enable)
ESC PHY clock.
Definition: hpm_esc_drv.h:90
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:334
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:178
static void esc_disable_pdi_access_mii_management(ESC_Type *ptr)
ESC disable PDI to access MII management.
Definition: hpm_esc_drv.h:226
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:105
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:134
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:282
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:147
static void esc_set_phy_offset(ESC_Type *ptr, uint8_t offset)
ESC set phy offset.
Definition: hpm_esc_drv.h:206
@ 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
@ latch_source_from_ntm
Definition: hpm_esc_drv.h:33
@ latch_source_from_trigger_mux
Definition: hpm_esc_drv.h:34
#define ESC_GPR_CFG2_NMII_LINK1_FROM_IO_MASK
Definition: hpm_esc_regs.h:4034
#define ESC_EEPROM_CTRL_STAT_CMD_SET(x)
Definition: hpm_esc_regs.h:1807
#define ESC_GPR_CFG2_NMII_LINK2_GPR_MASK
Definition: hpm_esc_regs.h:4025
#define ESC_GPR_CFG0_CLK100_EN_MASK
Definition: hpm_esc_regs.h:3891
#define ESC_IO_CFG_INVERT_SET(x)
Definition: hpm_esc_regs.h:4459
#define ESC_PHY_CFG0_PHY_OFFSET_VAL_MASK
Definition: hpm_esc_regs.h:4081
#define ESC_GPR_CFG0_EEPROM_EMU_MASK
Definition: hpm_esc_regs.h:3901
#define ESC_GPR_CFG2_NMII_LINK1_GPR_MASK
Definition: hpm_esc_regs.h:4043
#define ESC_GPR_CFG1_LATCH1_FROM_IO_MASK
Definition: hpm_esc_regs.h:3978
#define ESC_GPR_CFG1_LATCH0_FROM_IO_MASK
Definition: hpm_esc_regs.h:3988
#define ESC_GPR_CFG2_NMII_LINK2_FROM_IO_MASK
Definition: hpm_esc_regs.h:4016
#define ESC_GPR_CFG2_NMII_LINK0_GPR_MASK
Definition: hpm_esc_regs.h:4061
#define ESC_GPR_CFG2_NMII_LINK0_FROM_IO_MASK
Definition: hpm_esc_regs.h:4052
#define ESC_PHY_CFG1_REFCK_25M_OE_MASK
Definition: hpm_esc_regs.h:4164
#define ESC_EEPROM_CTRL_STAT_CMD_GET(x)
Definition: hpm_esc_regs.h:1808
#define ESC_MIIM_PDI_ACC_STAT_ACC_MASK
Definition: hpm_esc_regs.h:2107
#define ESC_IO_CFG_FUNC_ALT_SET(x)
Definition: hpm_esc_regs.h:4478
#define ESC_GPR_CFG0_I2C_SCLK_EN_MASK
Definition: hpm_esc_regs.h:3910
#define ESC_EEPROM_CTRL_STAT_ERR_ACK_CMD_MASK
Definition: hpm_esc_regs.h:1758
#define ESC_PHY_CFG0_PHY_OFFSET_VAL_SHIFT
Definition: hpm_esc_regs.h:4082
#define ESC_EEPROM_CTRL_STAT_CKSM_ERR_MASK
Definition: hpm_esc_regs.h:1785
#define ESC_GPR_CFG0_PROM_SIZE_MASK
Definition: hpm_esc_regs.h:3922
#define ESC_GPR_CFG1_RSTO_OVRD_ENJ_MASK
Definition: hpm_esc_regs.h:4006
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 uint32_t IO_CFG[9]
Definition: hpm_esc_regs.h:202
__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 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:57
bool phy_refclk_en
Definition: hpm_esc_drv.h:61
bool eeprom_size_over_16kbit
Definition: hpm_esc_drv.h:59
bool eeprom_emulation
Definition: hpm_esc_drv.h:58
bool core_clock_en
Definition: hpm_esc_drv.h:60