MAX32660 Peripheral Driver API
Peripheral Driver API for the MAX32660
mxc_sys.h
1
6/******************************************************************************
7 *
8 * Copyright (C) 2022-2023 Maxim Integrated Products, Inc. (now owned by
9 * Analog Devices, Inc.),
10 * Copyright (C) 2023-2024 Analog Devices, Inc.
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 *
24 ******************************************************************************/
25
26#ifndef LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32660_MXC_SYS_H_
27#define LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32660_MXC_SYS_H_
28
29#include "mxc_device.h"
30#include "gcr_regs.h"
31
32#ifdef __cplusplus
33extern "C" {
34#endif
35
44typedef enum {
60 /* RESET1 Below this line we add 32 to separate RESET0 and RESET1 */
63
65typedef enum {
88 /* PCLKDIS1 Below this line we add 32 to separate PCLKDIS0 and PCLKDIS1 */
94
96typedef enum {
102
103typedef enum {
104 MXC_SYS_CLOCK_DIV_1 = MXC_S_GCR_CLK_CTRL_PSC_DIV1,
105 MXC_SYS_CLOCK_DIV_2 = MXC_S_GCR_CLK_CTRL_PSC_DIV2,
106 MXC_SYS_CLOCK_DIV_4 = MXC_S_GCR_CLK_CTRL_PSC_DIV4,
107 MXC_SYS_CLOCK_DIV_8 = MXC_S_GCR_CLK_CTRL_PSC_DIV8,
108 MXC_SYS_CLOCK_DIV_16 = MXC_S_GCR_CLK_CTRL_PSC_DIV16,
109 MXC_SYS_CLOCK_DIV_32 = MXC_S_GCR_CLK_CTRL_PSC_DIV32,
110 MXC_SYS_CLOCK_DIV_64 = MXC_S_GCR_CLK_CTRL_PSC_DIV64,
111 MXC_SYS_CLOCK_DIV_128 = MXC_S_GCR_CLK_CTRL_PSC_DIV128
112} mxc_sys_system_clock_div_t;
113
114#define MXC_SYS_USN_LEN 8
115
116/***** Function Prototypes *****/
117
118typedef struct {
119 int ie_status;
120 int in_critical;
121} mxc_crit_state_t;
122
123static mxc_crit_state_t _state = { .ie_status = (int)0xFFFFFFFF, .in_critical = 0 };
124
125static inline void _mxc_crit_get_state(void)
126{
127#ifndef __riscv
128 /*
129 On ARM M the 0th bit of the Priority Mask register indicates
130 whether interrupts are enabled or not.
131
132 0 = enabled
133 1 = disabled
134 */
135 uint32_t primask = __get_PRIMASK();
136 _state.ie_status = (primask == 0);
137#else
138 /*
139 On RISC-V bit position 3 (Machine Interrupt Enable) of the
140 mstatus register indicates whether interrupts are enabled.
141
142 0 = disabled
143 1 = enabled
144 */
145 uint32_t mstatus = get_mstatus();
146 _state.ie_status = ((mstatus & (1 << 3)) != 0);
147#endif
148}
149
161static inline void MXC_SYS_Crit_Enter(void)
162{
163 _mxc_crit_get_state();
164 if (_state.ie_status)
165 __disable_irq();
166 _state.in_critical = 1;
167}
168
173static inline void MXC_SYS_Crit_Exit(void)
174{
175 if (_state.ie_status) {
176 __enable_irq();
177 }
178 _state.in_critical = 0;
179 _mxc_crit_get_state();
180 /*
181 ^ Reset the state again to prevent edge case
182 where interrupts get disabled, then Crit_Exit() gets
183 called, which would inadvertently re-enable interrupts
184 from old state.
185 */
186}
187
193static inline int MXC_SYS_In_Crit_Section(void)
194{
195 return _state.in_critical;
196}
197
198// clang-format off
212#define MXC_CRITICAL(code) {\
213 MXC_SYS_Crit_Enter();\
214 code;\
215 MXC_SYS_Crit_Exit();\
216}
217// clang-format on
218
226int MXC_SYS_GetUSN(uint8_t *usn, int len, int part);
227
234
240
246
252
258
265
272
279
284void MXC_SYS_SetClockDiv(mxc_sys_system_clock_div_t div);
285
290mxc_sys_system_clock_div_t MXC_SYS_GetClockDiv(void);
291
297int MXC_SYS_Clock_Timeout(uint32_t ready);
298
304
312
313#ifdef __cplusplus
314}
315#endif
316
317#endif // LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32660_MXC_SYS_H_
Registers, Bit Masks and Bit Positions for the GCR Peripheral Module.
#define MXC_S_GCR_CLK_CTRL_PSC_DIV4
Definition: gcr_regs.h:202
#define MXC_S_GCR_CLK_CTRL_PSC_DIV64
Definition: gcr_regs.h:210
#define MXC_V_GCR_CLK_CTRL_CLKSEL_HIRC
Definition: gcr_regs.h:216
#define MXC_S_GCR_CLK_CTRL_PSC_DIV8
Definition: gcr_regs.h:204
#define MXC_V_GCR_CLK_CTRL_CLKSEL_NANORING
Definition: gcr_regs.h:218
#define MXC_S_GCR_CLK_CTRL_PSC_DIV32
Definition: gcr_regs.h:208
#define MXC_S_GCR_CLK_CTRL_PSC_DIV1
Definition: gcr_regs.h:198
#define MXC_S_GCR_CLK_CTRL_PSC_DIV2
Definition: gcr_regs.h:200
#define MXC_S_GCR_CLK_CTRL_PSC_DIV128
Definition: gcr_regs.h:212
#define MXC_S_GCR_CLK_CTRL_PSC_DIV16
Definition: gcr_regs.h:206
#define MXC_V_GCR_CLK_CTRL_CLKSEL_HFXIN
Definition: gcr_regs.h:220
#define MXC_F_GCR_PCLK_DIS0_SPI1D_POS
Definition: gcr_regs.h:284
#define MXC_F_GCR_PCLK_DIS0_GPIO0D_POS
Definition: gcr_regs.h:275
#define MXC_F_GCR_PCLK_DIS0_UART1D_POS
Definition: gcr_regs.h:290
#define MXC_F_GCR_PCLK_DIS0_TIMER1D_POS
Definition: gcr_regs.h:299
#define MXC_F_GCR_PCLK_DIS0_SPI0D_POS
Definition: gcr_regs.h:281
#define MXC_F_GCR_PCLK_DIS0_UART0D_POS
Definition: gcr_regs.h:287
#define MXC_F_GCR_PCLK_DIS0_DMAD_POS
Definition: gcr_regs.h:278
#define MXC_F_GCR_PCLK_DIS0_I2C0D_POS
Definition: gcr_regs.h:293
#define MXC_F_GCR_PCLK_DIS0_TIMER0D_POS
Definition: gcr_regs.h:296
#define MXC_F_GCR_PCLK_DIS0_I2C1D_POS
Definition: gcr_regs.h:305
#define MXC_F_GCR_PCLK_DIS0_TIMER2D_POS
Definition: gcr_regs.h:302
#define MXC_F_GCR_PCLK_DIS1_ICCD_POS
Definition: gcr_regs.h:381
#define MXC_F_GCR_PCLK_DIS1_FLCD_POS
Definition: gcr_regs.h:378
#define MXC_F_GCR_RST0_TIMER2_POS
Definition: gcr_regs.h:157
#define MXC_F_GCR_RST0_SPI1_POS
Definition: gcr_regs.h:169
#define MXC_F_GCR_RST0_UART0_POS
Definition: gcr_regs.h:160
#define MXC_F_GCR_RST0_TIMER1_POS
Definition: gcr_regs.h:154
#define MXC_F_GCR_RST0_SOFT_POS
Definition: gcr_regs.h:178
#define MXC_F_GCR_RST0_WDT0_POS
Definition: gcr_regs.h:145
#define MXC_F_GCR_RST0_I2C0_POS
Definition: gcr_regs.h:172
#define MXC_F_GCR_RST0_UART1_POS
Definition: gcr_regs.h:163
#define MXC_F_GCR_RST0_SYSTEM_POS
Definition: gcr_regs.h:184
#define MXC_F_GCR_RST0_RTC_POS
Definition: gcr_regs.h:175
#define MXC_F_GCR_RST0_PERIPH_POS
Definition: gcr_regs.h:181
#define MXC_F_GCR_RST0_GPIO0_POS
Definition: gcr_regs.h:148
#define MXC_F_GCR_RST0_SPI0_POS
Definition: gcr_regs.h:166
#define MXC_F_GCR_RST0_TIMER0_POS
Definition: gcr_regs.h:151
#define MXC_F_GCR_RST0_DMA_POS
Definition: gcr_regs.h:142
#define MXC_F_GCR_RST1_I2C1_POS
Definition: gcr_regs.h:367
int MXC_SYS_LockDAP_Permanent(void)
This function PERMANENTLY locks the Debug Access Port.
void MXC_SYS_ClockDisable(mxc_sys_periph_clock_t clock)
Disables the selected peripheral clock.
void MXC_SYS_ClockEnable(mxc_sys_periph_clock_t clock)
Enables the selected peripheral clock.
static void MXC_SYS_Crit_Exit(void)
Exit a critical section of code from MXC_SYS_Crit_Enter.
Definition: mxc_sys.h:173
static int MXC_SYS_In_Crit_Section(void)
Polls whether code is currently executing from a critical section.
Definition: mxc_sys.h:193
static void MXC_SYS_Crit_Enter(void)
Enter a critical section of code that cannot be interrupted. Call MXC_SYS_Crit_Exit to exit the criti...
Definition: mxc_sys.h:161
mxc_sys_periph_clock_t
System clock disable enumeration. Used in MXC_SYS_ClockDisable and MXC_SYS_ClockEnable functions.
Definition: mxc_sys.h:65
void MXC_SYS_RTCClockEnable(void)
Enables the 32kHz oscillator.
void MXC_SYS_SetClockDiv(mxc_sys_system_clock_div_t div)
Set the system clock divider.
int MXC_SYS_ClockSourceDisable(mxc_sys_system_clock_t clock)
Disable System Clock Source.
int MXC_SYS_RTCClockDisable(void)
Disables the 32kHz oscillator.
int MXC_SYS_IsClockEnabled(mxc_sys_periph_clock_t clock)
Determines if the selected peripheral clock is enabled.
int MXC_SYS_GetUSN(uint8_t *usn, int len, int part)
Reads the device USN.
mxc_sys_reset_t
System reset0 and reset1 enumeration. Used in MXC_SYS_PeriphReset0 function.
Definition: mxc_sys.h:44
void MXC_SYS_Reset_Periph(mxc_sys_reset_t reset)
Reset the peripherals and/or CPU in the rstr0 or rstr1 register.
mxc_sys_system_clock_t
Enumeration to select System Clock source.
Definition: mxc_sys.h:96
int MXC_SYS_ClockSourceEnable(mxc_sys_system_clock_t clock)
Enable System Clock Source without switching to it.
int MXC_SYS_Clock_Timeout(uint32_t ready)
Wait for a clock to enable with timeout.
int MXC_SYS_Clock_Select(mxc_sys_system_clock_t clock)
Select the system clock.
mxc_sys_system_clock_div_t MXC_SYS_GetClockDiv(void)
Get the system clock divider.
@ MXC_SYS_PERIPH_CLOCK_UART1
Definition: mxc_sys.h:76
@ MXC_SYS_PERIPH_CLOCK_UART0
Definition: mxc_sys.h:74
@ MXC_SYS_PERIPH_CLOCK_TMR2
Definition: mxc_sys.h:84
@ MXC_SYS_PERIPH_CLOCK_ICACHE
Definition: mxc_sys.h:91
@ MXC_SYS_PERIPH_CLOCK_SPI0
Definition: mxc_sys.h:70
@ MXC_SYS_PERIPH_CLOCK_TMR0
Definition: mxc_sys.h:80
@ MXC_SYS_PERIPH_CLOCK_FLCD
Definition: mxc_sys.h:89
@ MXC_SYS_PERIPH_CLOCK_SPI1
Definition: mxc_sys.h:72
@ MXC_SYS_PERIPH_CLOCK_DMA
Definition: mxc_sys.h:68
@ MXC_SYS_PERIPH_CLOCK_I2C1
Definition: mxc_sys.h:86
@ MXC_SYS_PERIPH_CLOCK_GPIO0
Definition: mxc_sys.h:66
@ MXC_SYS_PERIPH_CLOCK_TMR1
Definition: mxc_sys.h:82
@ MXC_SYS_PERIPH_CLOCK_I2C0
Definition: mxc_sys.h:78
@ MXC_SYS_RESET0_GPIO0
Definition: mxc_sys.h:47
@ MXC_SYS_RESET0_I2C0
Definition: mxc_sys.h:55
@ MXC_SYS_RESET0_SYSTEM
Definition: mxc_sys.h:59
@ MXC_SYS_RESET0_SRST
Definition: mxc_sys.h:57
@ MXC_SYS_RESET0_SPI1
Definition: mxc_sys.h:54
@ MXC_SYS_RESET0_RTC
Definition: mxc_sys.h:56
@ MXC_SYS_RESET0_SPI0
Definition: mxc_sys.h:53
@ MXC_SYS_RESET0_DMA
Definition: mxc_sys.h:45
@ MXC_SYS_RESET0_UART1
Definition: mxc_sys.h:52
@ MXC_SYS_RESET0_WDT0
Definition: mxc_sys.h:46
@ MXC_SYS_RESET0_TIMER2
Definition: mxc_sys.h:50
@ MXC_SYS_RESET1_I2C1
Definition: mxc_sys.h:61
@ MXC_SYS_RESET0_UART0
Definition: mxc_sys.h:51
@ MXC_SYS_RESET0_PRST
Definition: mxc_sys.h:58
@ MXC_SYS_RESET0_TIMER1
Definition: mxc_sys.h:49
@ MXC_SYS_RESET0_TIMER0
Definition: mxc_sys.h:48
@ MXC_SYS_CLOCK_HFXIN
Definition: mxc_sys.h:98
@ MXC_SYS_CLOCK_HIRC
Definition: mxc_sys.h:100
@ MXC_SYS_CLOCK_HFXIN_DIGITAL
Definition: mxc_sys.h:99
@ MXC_SYS_CLOCK_NANORING
Definition: mxc_sys.h:97