HPM SDK
HPMicro Software Development Kit
hpm_mcan_soc.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2024-2025 HPMicro
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #ifndef HPM_MCAN_SOC_H
9 #define HPM_MCAN_SOC_H
10 
11 #include <stdint.h>
12 #include "hpm_mcan_regs.h"
13 #include "hpm_soc.h"
14 #include "hpm_soc_feature.h"
15 
19 typedef struct {
20  uint32_t ram_base;
21  uint32_t ram_size;
23 
24 #define MCAN_SOC_TSU_SRC_TWO_STAGES (1U)
25 
26 #define HPM_MCAN_EXT_TBSEL_NUM (4U)
27 #define HPM_MCAN_TBSEL_BASE (0xF02FF000UL)
28 #define HPM_MCAN_TBSEL (*(volatile uint32_t *)HPM_MCAN_TBSEL_BASE)
29 #define HPM_MCAN_TBSEL_BITWIDTH (6U)
30 #define HPM_MCAN_TBSEL_MASK ((1UL << HPM_MCAN_TBSEL_BITWIDTH) - 1UL)
31 #define HPM_MCAN_TBSEL0_SHIFT (8U)
32 
33 #define MCAN_MSG_BUF_BASE_VALID_START (0xF0200000UL) /* This is the start address of AHB_RAM */
34 #define MCAN_MSG_BUG_SIZE_MAX (32UL * 1024UL) /* This is the total size of AHB_RAM */
35 #define MCAN_MSG_BUF_BASE_VALID_END (MCAN_MSG_BUF_BASE_VALID_START + MCAN_MSG_BUG_SIZE_MAX)
36 
37 #define MCAN_MSG_BUF_ALIGNMENT_SIZE (4U)
38 
42 #define MCAN_MSG_BUF_SIZE_IN_WORDS (640U)
43 #define MCAN_IP_SLOT_SIZE (0x4000U)
44 
48 #define MCAN_TSU_EXT_TIMEBASE_SRC_MIN (0U)
49 #define MCAN_TSU_EXT_TIMEBASE_SRC_TBSEL_0 (MCAN_TSU_EXT_TIMEBASE_SRC_MIN)
50 #define MCAN_TSU_EXT_TIMEBASE_SRC_TBSEL_1 (1U)
51 #define MCAN_TSU_EXT_TIMEBASE_SRC_TBSEL_2 (2U)
52 #define MCAN_TSU_EXT_TIMEBASE_SRC_TBSEL_3 (3U)
53 #define MCAN_TSU_EXT_TIMEBASE_SRC_MAX (MCAN_TSU_EXT_TIMEBASE_SRC_TBSEL_3)
54 
58 #define MCAN_TSU_TBSEL_PTPC0 (0x20)
59 #define MCAN_TSU_TBSEL_MCAN0 (0x00)
60 #define MCAN_TSU_TBSEL_MCAN1 (0x01)
61 #define MCAN_TSU_TBSEL_MCAN2 (0x02)
62 #define MCAN_TSU_TBSEL_MCAN3 (0x03)
63 #define MCAN_TSU_TBSEL_MCAN4 (0x04)
64 #define MCAN_TSU_TBSEL_MCAN5 (0x05)
65 #define MCAN_TSU_TBSEL_MCAN6 (0x06)
66 #define MCAN_TSU_TBSEL_MCAN7 (0x07)
67 
69 
70 #ifdef __cpluspus
71 extern "C" {
72 #endif
73 
79 static inline uint32_t mcan_get_instance_from_base(MCAN_Type *base)
80 {
81  uint32_t index = ((uint32_t) base - HPM_MCAN0_BASE) / MCAN_IP_SLOT_SIZE;
82  return index;
83 }
84 
90 static inline void mcan_set_tsu_ext_timebase_src(MCAN_Type *ptr, uint8_t src)
91 {
92  if (src < HPM_MCAN_EXT_TBSEL_NUM) {
94  }
95 }
96 
104 static inline void mcan_set_tsu_tbsel_option(MCAN_Type *ptr, uint8_t ext_tbsel, uint8_t tbsel_option)
105 {
106  (void) ptr;
107  if (ext_tbsel < HPM_MCAN_EXT_TBSEL_NUM) {
108  uint32_t tbsel_shift = (ext_tbsel * HPM_MCAN_TBSEL_BITWIDTH) + HPM_MCAN_TBSEL0_SHIFT;
109  uint32_t tbsel_mask = HPM_MCAN_TBSEL_MASK << tbsel_shift;
110  HPM_MCAN_TBSEL = (HPM_MCAN_TBSEL & ~tbsel_mask) | (((uint32_t) tbsel_option << tbsel_shift) & tbsel_mask);
111  }
112 }
113 
118 static inline void mcan_enable_standby_pin(MCAN_Type *ptr)
119 {
121 }
122 
127 static inline void mcan_disable_standby_pin(MCAN_Type *ptr)
128 {
130 }
131 
140 {
141  if ((attr == NULL) || \
143  (attr->ram_base >= MCAN_MSG_BUF_BASE_VALID_END) || \
144  (attr->ram_size > MCAN_MSG_BUG_SIZE_MAX) || \
145  (attr->ram_base + attr->ram_size > MCAN_MSG_BUF_BASE_VALID_END) || \
146  (attr->ram_base % MCAN_MSG_BUF_ALIGNMENT_SIZE != 0U) || \
147  (attr->ram_size % MCAN_MSG_BUF_ALIGNMENT_SIZE != 0)) {
149  }
150  uint32_t instance = mcan_get_instance_from_base(ptr);
151  if (instance >= MCAN_SOC_MAX_COUNT) {
153  }
154 
155  mcan_soc_msg_buf_attr[instance].ram_base = attr->ram_base;
156  mcan_soc_msg_buf_attr[instance].ram_size = attr->ram_size;
157 
158  return status_success;
159 }
165 static inline uint32_t mcan_get_ram_base(MCAN_Type *ptr)
166 {
167  (void) ptr;
168  /* NOTE: As the `mcan_get_ram_offset` returns the actual offset in AHB RAM, this function always returns
169  * the base address of the AHB_RAM
170  */
172 }
173 
179 static inline uint32_t mcan_get_ram_offset(MCAN_Type *ptr)
180 {
181  uint32_t instance = mcan_get_instance_from_base(ptr);
182  return (mcan_soc_msg_buf_attr[instance].ram_base - MCAN_MSG_BUF_BASE_VALID_START);
183 }
184 
190 static inline uint32_t mcan_get_ram_size(MCAN_Type *ptr)
191 {
192  uint32_t instance = mcan_get_instance_from_base(ptr);
193  return mcan_soc_msg_buf_attr[instance].ram_size;
194 }
195 
196 #ifdef __cpluspus
197 }
198 #endif
199 
200 #endif /* HPM_MCAN_SOC_H */
#define MCAN_SOC_MAX_COUNT
Definition: hpm_soc_feature.h:184
mcan_msg_buf_attr_t mcan_soc_msg_buf_attr[(4U)]
#define HPM_MCAN0_BASE
Definition: hpm_soc.h:220
#define MCAN_GLB_CTL_M_CAN_STBY_MASK
Definition: hpm_mcan_regs.h:2874
#define MCAN_GLB_CTL_TSU_TBIN_SEL_SET(x)
Definition: hpm_mcan_regs.h:2908
#define MCAN_GLB_CTL_TSU_TBIN_SEL_MASK
Definition: hpm_mcan_regs.h:2906
#define MCAN_MSG_BUG_SIZE_MAX
Definition: hpm_mcan_soc.h:34
static uint32_t mcan_get_ram_offset(MCAN_Type *ptr)
Get the MCAN RAM offset in the dedicated/shared RAM for.
Definition: hpm_mcan_soc.h:179
static void mcan_set_tsu_tbsel_option(MCAN_Type *ptr, uint8_t ext_tbsel, uint8_t tbsel_option)
Set the Source for specified external timebase.
Definition: hpm_mcan_soc.h:104
static hpm_stat_t mcan_set_msg_buf_attr(MCAN_Type *ptr, const mcan_msg_buf_attr_t *attr)
Set the attribute of the Message Buffer for specified MCAN.
Definition: hpm_mcan_soc.h:139
#define HPM_MCAN_TBSEL_MASK
Definition: hpm_mcan_soc.h:30
#define MCAN_MSG_BUF_BASE_VALID_END
Definition: hpm_mcan_soc.h:35
#define MCAN_MSG_BUF_ALIGNMENT_SIZE
Definition: hpm_mcan_soc.h:37
#define MCAN_MSG_BUF_BASE_VALID_START
Definition: hpm_mcan_soc.h:33
#define HPM_MCAN_TBSEL
Definition: hpm_mcan_soc.h:28
#define HPM_MCAN_TBSEL_BITWIDTH
Definition: hpm_mcan_soc.h:29
#define MCAN_IP_SLOT_SIZE
Definition: hpm_mcan_soc.h:43
static uint32_t mcan_get_instance_from_base(MCAN_Type *base)
Get the MCAN instance index from base address.
Definition: hpm_mcan_soc.h:79
static void mcan_set_tsu_ext_timebase_src(MCAN_Type *ptr, uint8_t src)
Set External Timebase Source for MCAN TSU.
Definition: hpm_mcan_soc.h:90
static void mcan_disable_standby_pin(MCAN_Type *ptr)
Disable Standby pin for MCAN.
Definition: hpm_mcan_soc.h:127
static void mcan_enable_standby_pin(MCAN_Type *ptr)
Enable Standby Pin for MCAN.
Definition: hpm_mcan_soc.h:118
#define HPM_MCAN_TBSEL0_SHIFT
Definition: hpm_mcan_soc.h:31
static uint32_t mcan_get_ram_base(MCAN_Type *ptr)
Get RAM base for MCAN.
Definition: hpm_mcan_soc.h:165
static uint32_t mcan_get_ram_size(MCAN_Type *ptr)
Get MCAN RAM size.
Definition: hpm_mcan_soc.h:190
#define HPM_MCAN_EXT_TBSEL_NUM
Definition: hpm_mcan_soc.h:26
uint32_t hpm_stat_t
Definition: hpm_common.h:126
@ status_invalid_argument
Definition: hpm_common.h:182
@ status_success
Definition: hpm_common.h:180
Definition: hpm_mcan_regs.h:12
__RW uint32_t GLB_CTL
Definition: hpm_mcan_regs.h:74
The Attribute of Message Attribute.
Definition: hpm_mcan_soc.h:19
uint32_t ram_base
Definition: hpm_mcan_soc.h:20
uint32_t ram_size
Definition: hpm_mcan_soc.h:21