MAX32672 Peripheral Driver API
Peripheral Driver API for the MAX32672
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Modules Pages
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_MAX32672_MXC_SYS_H_
27#define LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32672_MXC_SYS_H_
28
29#include "mxc_device.h"
30#include "gcr_regs.h"
31#include "mcr_regs.h"
32
33#ifdef __cplusplus
34extern "C" {
35#endif
36
45typedef enum {
68 /* RESET1 Below this line we add 32 to separate RESET0 and RESET1 */
75 /* LPGCR RESET Below this line we add 64 to separate LPGCR and GCR */
80
82typedef enum {
115 /* PCLKDIS1 Below this line we add 32 to separate PCLKDIS0 and PCLKDIS1 */
134 /* LPGCR PCLKDIS Below this line we add 64 to seperate GCR and LPGCR registers */
142
144typedef enum {
158
159typedef enum {
160 MXC_SYS_CLOCK_DIV_1 = MXC_S_GCR_CLKCTRL_SYSCLK_DIV_DIV1,
161 MXC_SYS_CLOCK_DIV_2 = MXC_S_GCR_CLKCTRL_SYSCLK_DIV_DIV2,
162 MXC_SYS_CLOCK_DIV_4 = MXC_S_GCR_CLKCTRL_SYSCLK_DIV_DIV4,
163 MXC_SYS_CLOCK_DIV_8 = MXC_S_GCR_CLKCTRL_SYSCLK_DIV_DIV8,
164 MXC_SYS_CLOCK_DIV_16 = MXC_S_GCR_CLKCTRL_SYSCLK_DIV_DIV16,
165 MXC_SYS_CLOCK_DIV_32 = MXC_S_GCR_CLKCTRL_SYSCLK_DIV_DIV32,
166 MXC_SYS_CLOCK_DIV_64 = MXC_S_GCR_CLKCTRL_SYSCLK_DIV_DIV64,
167 MXC_SYS_CLOCK_DIV_128 = MXC_S_GCR_CLKCTRL_SYSCLK_DIV_DIV128
168} mxc_sys_system_clock_div_t;
169
170#define MXC_SYS_USN_CHECKSUM_LEN 16 // Length of the USN + padding for checksum compute
171#define MXC_SYS_USN_CSUM_FIELD_LEN 2 // Size of the checksum field in the USN
172#define MXC_SYS_USN_LEN 13 // Size of the USN including the checksum
173
174/***** Function Prototypes *****/
175
176typedef struct {
177 int ie_status;
178 int in_critical;
179} mxc_crit_state_t;
180
181static mxc_crit_state_t _state = { .ie_status = (int)0xFFFFFFFF, .in_critical = 0 };
182
183static inline void _mxc_crit_get_state(void)
184{
185#ifndef __riscv
186 /*
187 On ARM M the 0th bit of the Priority Mask register indicates
188 whether interrupts are enabled or not.
189
190 0 = enabled
191 1 = disabled
192 */
193 uint32_t primask = __get_PRIMASK();
194 _state.ie_status = (primask == 0);
195#else
196 /*
197 On RISC-V bit position 3 (Machine Interrupt Enable) of the
198 mstatus register indicates whether interrupts are enabled.
199
200 0 = disabled
201 1 = enabled
202 */
203 uint32_t mstatus = get_mstatus();
204 _state.ie_status = ((mstatus & (1 << 3)) != 0);
205#endif
206}
207
219static inline void MXC_SYS_Crit_Enter(void)
220{
221 _mxc_crit_get_state();
222 if (_state.ie_status)
223 __disable_irq();
224 _state.in_critical = 1;
225}
226
231static inline void MXC_SYS_Crit_Exit(void)
232{
233 if (_state.ie_status) {
234 __enable_irq();
235 }
236 _state.in_critical = 0;
237 _mxc_crit_get_state();
238 /*
239 ^ Reset the state again to prevent edge case
240 where interrupts get disabled, then Crit_Exit() gets
241 called, which would inadvertently re-enable interrupts
242 from old state.
243 */
244}
245
251static inline int MXC_SYS_In_Crit_Section(void)
252{
253 return _state.in_critical;
254}
255
256// clang-format off
270#define MXC_CRITICAL(code) {\
271 MXC_SYS_Crit_Enter();\
272 code;\
273 MXC_SYS_Crit_Exit();\
274}
275// clang-format on
276
283int MXC_SYS_GetUSN(uint8_t *usn, uint8_t *checksum);
284
291
298
304
310
316
322
329
336
344
349void MXC_SYS_SetClockDiv(mxc_sys_system_clock_div_t div);
350
355mxc_sys_system_clock_div_t MXC_SYS_GetClockDiv(void);
356
362int MXC_SYS_Clock_Timeout(uint32_t ready);
363
369
377
378#ifdef __cplusplus
379}
380#endif
381
382#endif // LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32672_MXC_SYS_H_
Registers, Bit Masks and Bit Positions for the GCR Peripheral Module.
#define MXC_S_GCR_CLKCTRL_SYSCLK_DIV_DIV64
Definition: gcr_regs.h:253
#define MXC_V_GCR_CLKCTRL_SYSCLK_SEL_EXTCLK
Definition: gcr_regs.h:269
#define MXC_V_GCR_CLKCTRL_SYSCLK_SEL_IBRO
Definition: gcr_regs.h:265
#define MXC_S_GCR_CLKCTRL_SYSCLK_DIV_DIV1
Definition: gcr_regs.h:241
#define MXC_S_GCR_CLKCTRL_SYSCLK_DIV_DIV128
Definition: gcr_regs.h:255
#define MXC_V_GCR_CLKCTRL_SYSCLK_SEL_IPO
Definition: gcr_regs.h:263
#define MXC_V_GCR_CLKCTRL_SYSCLK_SEL_INRO
Definition: gcr_regs.h:261
#define MXC_S_GCR_CLKCTRL_SYSCLK_DIV_DIV2
Definition: gcr_regs.h:243
#define MXC_V_GCR_CLKCTRL_SYSCLK_SEL_ERFO
Definition: gcr_regs.h:259
#define MXC_S_GCR_CLKCTRL_SYSCLK_DIV_DIV8
Definition: gcr_regs.h:247
#define MXC_V_GCR_CLKCTRL_SYSCLK_SEL_ERTCO
Definition: gcr_regs.h:267
#define MXC_S_GCR_CLKCTRL_SYSCLK_DIV_DIV32
Definition: gcr_regs.h:251
#define MXC_S_GCR_CLKCTRL_SYSCLK_DIV_DIV16
Definition: gcr_regs.h:249
#define MXC_S_GCR_CLKCTRL_SYSCLK_DIV_DIV4
Definition: gcr_regs.h:245
#define MXC_F_GCR_PCLKDIS0_UART1_POS
Definition: gcr_regs.h:425
#define MXC_F_GCR_PCLKDIS0_GPIO1_POS
Definition: gcr_regs.h:407
#define MXC_F_GCR_PCLKDIS0_CTB_POS
Definition: gcr_regs.h:431
#define MXC_F_GCR_PCLKDIS0_SPI1_POS
Definition: gcr_regs.h:416
#define MXC_F_GCR_PCLKDIS0_DMA_POS
Definition: gcr_regs.h:410
#define MXC_F_GCR_PCLKDIS0_I2C0_POS
Definition: gcr_regs.h:428
#define MXC_F_GCR_PCLKDIS0_SPI2_POS
Definition: gcr_regs.h:419
#define MXC_F_GCR_PCLKDIS0_GPIO0_POS
Definition: gcr_regs.h:404
#define MXC_F_GCR_PCLKDIS0_TMR0_POS
Definition: gcr_regs.h:434
#define MXC_F_GCR_PCLKDIS0_TMR1_POS
Definition: gcr_regs.h:437
#define MXC_F_GCR_PCLKDIS0_ADC_POS
Definition: gcr_regs.h:446
#define MXC_F_GCR_PCLKDIS0_UART0_POS
Definition: gcr_regs.h:422
#define MXC_F_GCR_PCLKDIS0_TMR2_POS
Definition: gcr_regs.h:440
#define MXC_F_GCR_PCLKDIS0_SPI0_POS
Definition: gcr_regs.h:413
#define MXC_F_GCR_PCLKDIS0_TMR3_POS
Definition: gcr_regs.h:443
#define MXC_F_GCR_PCLKDIS0_I2C1_POS
Definition: gcr_regs.h:449
#define MXC_F_GCR_PCLKDIS1_ICC0_POS
Definition: gcr_regs.h:567
#define MXC_F_GCR_PCLKDIS1_TRNG_POS
Definition: gcr_regs.h:558
#define MXC_F_GCR_PCLKDIS1_I2S_POS
Definition: gcr_regs.h:576
#define MXC_F_GCR_PCLKDIS1_UART2_POS
Definition: gcr_regs.h:555
#define MXC_F_GCR_PCLKDIS1_WDT1_POS
Definition: gcr_regs.h:564
#define MXC_F_GCR_PCLKDIS1_WDT0_POS
Definition: gcr_regs.h:561
#define MXC_F_GCR_PCLKDIS1_QDEC_POS
Definition: gcr_regs.h:579
#define MXC_F_GCR_PCLKDIS1_I2C2_POS
Definition: gcr_regs.h:573
#define MXC_F_GCR_PCLKDIS1_AES_POS
Definition: gcr_regs.h:570
#define MXC_F_GCR_RST0_CTB_POS
Definition: gcr_regs.h:209
#define MXC_F_GCR_RST0_TMR2_POS
Definition: gcr_regs.h:185
#define MXC_F_GCR_RST0_GPIO1_POS
Definition: gcr_regs.h:176
#define MXC_F_GCR_RST0_SPI1_POS
Definition: gcr_regs.h:200
#define MXC_F_GCR_RST0_UART0_POS
Definition: gcr_regs.h:191
#define MXC_F_GCR_RST0_SOFT_POS
Definition: gcr_regs.h:221
#define MXC_F_GCR_RST0_WDT0_POS
Definition: gcr_regs.h:170
#define MXC_F_GCR_RST0_TRNG_POS
Definition: gcr_regs.h:212
#define MXC_F_GCR_RST0_UART2_POS
Definition: gcr_regs.h:218
#define MXC_F_GCR_RST0_I2C0_POS
Definition: gcr_regs.h:206
#define MXC_F_GCR_RST0_TMR3_POS
Definition: gcr_regs.h:188
#define MXC_F_GCR_RST0_UART1_POS
Definition: gcr_regs.h:194
#define MXC_F_GCR_RST0_TMR1_POS
Definition: gcr_regs.h:182
#define MXC_F_GCR_RST0_ADC_POS
Definition: gcr_regs.h:215
#define MXC_F_GCR_RST0_SYS_POS
Definition: gcr_regs.h:227
#define MXC_F_GCR_RST0_PERIPH_POS
Definition: gcr_regs.h:224
#define MXC_F_GCR_RST0_TMR0_POS
Definition: gcr_regs.h:179
#define MXC_F_GCR_RST0_GPIO0_POS
Definition: gcr_regs.h:173
#define MXC_F_GCR_RST0_SPI0_POS
Definition: gcr_regs.h:197
#define MXC_F_GCR_RST0_SPI2_POS
Definition: gcr_regs.h:203
#define MXC_F_GCR_RST0_DMA_POS
Definition: gcr_regs.h:167
#define MXC_F_GCR_RST1_I2C2_POS
Definition: gcr_regs.h:538
#define MXC_F_GCR_RST1_WDT1_POS
Definition: gcr_regs.h:529
#define MXC_F_GCR_RST1_AES_POS
Definition: gcr_regs.h:532
#define MXC_F_GCR_RST1_I2S_POS
Definition: gcr_regs.h:541
#define MXC_F_GCR_RST1_QDEC_POS
Definition: gcr_regs.h:544
#define MXC_F_GCR_RST1_I2C1_POS
Definition: gcr_regs.h:526
#define MXC_F_MCR_PCLKDIS_LPTMR1_POS
Definition: mcr_regs.h:212
#define MXC_F_MCR_PCLKDIS_LPTMR0_POS
Definition: mcr_regs.h:209
#define MXC_F_MCR_PCLKDIS_LPUART0_POS
Definition: mcr_regs.h:215
#define MXC_F_MCR_RST_LPTMR1_POS
Definition: mcr_regs.h:120
#define MXC_F_MCR_RST_LPUART0_POS
Definition: mcr_regs.h:123
#define MXC_F_MCR_RST_RTC_POS
Definition: mcr_regs.h:126
#define MXC_F_MCR_RST_LPTMR0_POS
Definition: mcr_regs.h:117
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.
int MXC_SYS_GetUSN(uint8_t *usn, uint8_t *checksum)
Reads the device USN and verifies the checksum.
static void MXC_SYS_Crit_Exit(void)
Exit a critical section of code from MXC_SYS_Crit_Enter.
Definition: mxc_sys.h:231
static int MXC_SYS_In_Crit_Section(void)
Polls whether code is currently executing from a critical section.
Definition: mxc_sys.h:251
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:219
mxc_sys_periph_clock_t
System clock disable enumeration. Used in MXC_SYS_ClockDisable and MXC_SYS_ClockEnable functions.
Definition: mxc_sys.h:82
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_GetRevision(void)
Gets design revision of the chip.
mxc_sys_reset_t
System reset0 and reset1 enumeration. Used in MXC_SYS_PeriphReset0 function.
Definition: mxc_sys.h:45
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:144
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:97
@ MXC_SYS_PERIPH_CLOCK_UART3
Definition: mxc_sys.h:139
@ MXC_SYS_PERIPH_CLOCK_ADC
Definition: mxc_sys.h:109
@ MXC_SYS_PERIPH_CLOCK_UART0
Definition: mxc_sys.h:95
@ MXC_SYS_PERIPH_CLOCK_TMR4
Definition: mxc_sys.h:135
@ MXC_SYS_PERIPH_CLOCK_TMR2
Definition: mxc_sys.h:105
@ MXC_SYS_PERIPH_CLOCK_ICACHE
Definition: mxc_sys.h:124
@ MXC_SYS_PERIPH_CLOCK_SPI2
Definition: mxc_sys.h:93
@ MXC_SYS_PERIPH_CLOCK_GPIO1
Definition: mxc_sys.h:85
@ MXC_SYS_PERIPH_CLOCK_TMR3
Definition: mxc_sys.h:107
@ MXC_SYS_PERIPH_CLOCK_SPI0
Definition: mxc_sys.h:89
@ MXC_SYS_PERIPH_CLOCK_I2S
Definition: mxc_sys.h:130
@ MXC_SYS_PERIPH_CLOCK_TMR0
Definition: mxc_sys.h:101
@ MXC_SYS_PERIPH_CLOCK_I2C2
Definition: mxc_sys.h:128
@ MXC_SYS_PERIPH_CLOCK_UART2
Definition: mxc_sys.h:116
@ MXC_SYS_PERIPH_CLOCK_WDT0
Definition: mxc_sys.h:120
@ MXC_SYS_PERIPH_CLOCK_SPI1
Definition: mxc_sys.h:91
@ MXC_SYS_PERIPH_CLOCK_CTB
Definition: mxc_sys.h:113
@ MXC_SYS_PERIPH_CLOCK_TRNG
Definition: mxc_sys.h:118
@ MXC_SYS_PERIPH_CLOCK_QDEC
Definition: mxc_sys.h:132
@ MXC_SYS_PERIPH_CLOCK_DMA
Definition: mxc_sys.h:87
@ MXC_SYS_PERIPH_CLOCK_WDT1
Definition: mxc_sys.h:122
@ MXC_SYS_PERIPH_CLOCK_AES
Definition: mxc_sys.h:126
@ MXC_SYS_PERIPH_CLOCK_I2C1
Definition: mxc_sys.h:111
@ MXC_SYS_PERIPH_CLOCK_GPIO0
Definition: mxc_sys.h:83
@ MXC_SYS_PERIPH_CLOCK_TMR1
Definition: mxc_sys.h:103
@ MXC_SYS_PERIPH_CLOCK_TMR5
Definition: mxc_sys.h:137
@ MXC_SYS_PERIPH_CLOCK_I2C0
Definition: mxc_sys.h:99
@ MXC_SYS_RESET0_GPIO0
Definition: mxc_sys.h:48
@ MXC_SYS_RESET0_I2C0
Definition: mxc_sys.h:59
@ MXC_SYS_RESET0_SPI2
Definition: mxc_sys.h:58
@ MXC_SYS_RESET0_TMR2
Definition: mxc_sys.h:52
@ MXC_SYS_RESET_UART3
Definition: mxc_sys.h:78
@ MXC_SYS_RESET0_CTB
Definition: mxc_sys.h:61
@ MXC_SYS_RESET0_TMR3
Definition: mxc_sys.h:53
@ MXC_SYS_RESET1_AES
Definition: mxc_sys.h:71
@ MXC_SYS_RESET0_SRST
Definition: mxc_sys.h:65
@ MXC_SYS_RESET0_SPI1
Definition: mxc_sys.h:57
@ MXC_SYS_RESET0_RTC
Definition: mxc_sys.h:60
@ MXC_SYS_RESET0_SPI0
Definition: mxc_sys.h:56
@ MXC_SYS_RESET1_QDEC
Definition: mxc_sys.h:74
@ MXC_SYS_RESET0_TRNG
Definition: mxc_sys.h:62
@ MXC_SYS_RESET_TMR4
Definition: mxc_sys.h:76
@ MXC_SYS_RESET0_DMA
Definition: mxc_sys.h:46
@ MXC_SYS_RESET0_UART1
Definition: mxc_sys.h:55
@ MXC_SYS_RESET0_UART2
Definition: mxc_sys.h:64
@ MXC_SYS_RESET0_SYS
Definition: mxc_sys.h:67
@ MXC_SYS_RESET_TMR5
Definition: mxc_sys.h:77
@ MXC_SYS_RESET0_WDT0
Definition: mxc_sys.h:47
@ MXC_SYS_RESET1_I2S
Definition: mxc_sys.h:73
@ MXC_SYS_RESET1_I2C1
Definition: mxc_sys.h:69
@ MXC_SYS_RESET0_ADC
Definition: mxc_sys.h:63
@ MXC_SYS_RESET0_UART0
Definition: mxc_sys.h:54
@ MXC_SYS_RESET0_PRST
Definition: mxc_sys.h:66
@ MXC_SYS_RESET0_GPIO1
Definition: mxc_sys.h:49
@ MXC_SYS_RESET1_WDT1
Definition: mxc_sys.h:70
@ MXC_SYS_RESET1_I2C2
Definition: mxc_sys.h:72
@ MXC_SYS_RESET0_TMR0
Definition: mxc_sys.h:50
@ MXC_SYS_RESET0_TMR1
Definition: mxc_sys.h:51
@ MXC_SYS_CLOCK_EXTCLK
Definition: mxc_sys.h:155
@ MXC_SYS_CLOCK_INRO
Definition: mxc_sys.h:151
@ MXC_SYS_CLOCK_IBRO
Definition: mxc_sys.h:147
@ MXC_SYS_CLOCK_ERTCO
Definition: mxc_sys.h:153
@ MXC_SYS_CLOCK_IPO
Definition: mxc_sys.h:145
@ MXC_SYS_CLOCK_ERFO
Definition: mxc_sys.h:149
Registers, Bit Masks and Bit Positions for the MCR Peripheral Module.