MAX32675 Peripheral Driver API
Peripheral Driver API for the MAX32675
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Modules
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_MAX32675_MXC_SYS_H_
27#define LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32675_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 {
66 /* RESET1 Below this line we add 32 to separate RESET0 and RESET1 */
73 /* LPGCR RESET Below this line we add 64 to separate LPGCR and GCR */
78
80typedef enum {
109 /* PCLKDIS1 Below this line we add 32 to separate PCLKDIS0 and PCLKDIS1 */
128 /* LPGCR PCLKDIS Below this line we add 64 to seperate GCR and LPGCR registers */
134
136typedef enum {
150
151typedef enum {
152 MXC_SYS_CLOCK_DIV_1 = MXC_S_GCR_CLKCTRL_SYSCLK_DIV_DIV1,
153 MXC_SYS_CLOCK_DIV_2 = MXC_S_GCR_CLKCTRL_SYSCLK_DIV_DIV2,
154 MXC_SYS_CLOCK_DIV_4 = MXC_S_GCR_CLKCTRL_SYSCLK_DIV_DIV4,
155 MXC_SYS_CLOCK_DIV_8 = MXC_S_GCR_CLKCTRL_SYSCLK_DIV_DIV8,
156 MXC_SYS_CLOCK_DIV_16 = MXC_S_GCR_CLKCTRL_SYSCLK_DIV_DIV16,
157 MXC_SYS_CLOCK_DIV_32 = MXC_S_GCR_CLKCTRL_SYSCLK_DIV_DIV32,
158 MXC_SYS_CLOCK_DIV_64 = MXC_S_GCR_CLKCTRL_SYSCLK_DIV_DIV64,
159 MXC_SYS_CLOCK_DIV_128 = MXC_S_GCR_CLKCTRL_SYSCLK_DIV_DIV128
160} mxc_sys_system_clock_div_t;
161
162#define MXC_SYS_USN_CHECKSUM_LEN 16 // Length of the USN + padding for checksum compute
163#define MXC_SYS_USN_CSUM_FIELD_LEN 2 // Size of the checksum field in the USN
164#define MXC_SYS_USN_LEN 13 // Size of the USN including the checksum
165
166/***** Function Prototypes *****/
167
168typedef struct {
169 int ie_status;
170 int in_critical;
171} mxc_crit_state_t;
172
173static mxc_crit_state_t _state = { .ie_status = (int)0xFFFFFFFF, .in_critical = 0 };
174
175static inline void _mxc_crit_get_state(void)
176{
177#ifndef __riscv
178 /*
179 On ARM M the 0th bit of the Priority Mask register indicates
180 whether interrupts are enabled or not.
181
182 0 = enabled
183 1 = disabled
184 */
185 uint32_t primask = __get_PRIMASK();
186 _state.ie_status = (primask == 0);
187#else
188 /*
189 On RISC-V bit position 3 (Machine Interrupt Enable) of the
190 mstatus register indicates whether interrupts are enabled.
191
192 0 = disabled
193 1 = enabled
194 */
195 uint32_t mstatus = get_mstatus();
196 _state.ie_status = ((mstatus & (1 << 3)) != 0);
197#endif
198}
199
211static inline void MXC_SYS_Crit_Enter(void)
212{
213 _mxc_crit_get_state();
214 if (_state.ie_status)
215 __disable_irq();
216 _state.in_critical = 1;
217}
218
223static inline void MXC_SYS_Crit_Exit(void)
224{
225 if (_state.ie_status) {
226 __enable_irq();
227 }
228 _state.in_critical = 0;
229 _mxc_crit_get_state();
230 /*
231 ^ Reset the state again to prevent edge case
232 where interrupts get disabled, then Crit_Exit() gets
233 called, which would inadvertently re-enable interrupts
234 from old state.
235 */
236}
237
243static inline int MXC_SYS_In_Crit_Section(void)
244{
245 return _state.in_critical;
246}
247
248// clang-format off
262#define MXC_CRITICAL(code) {\
263 MXC_SYS_Crit_Enter();\
264 code;\
265 MXC_SYS_Crit_Exit();\
266}
267// clang-format on
268
275int MXC_SYS_GetUSN(uint8_t *usn, uint8_t *checksum);
276
283
289
295
301
307
314
321
329
334void MXC_SYS_SetClockDiv(mxc_sys_system_clock_div_t div);
335
340mxc_sys_system_clock_div_t MXC_SYS_GetClockDiv(void);
341
347int MXC_SYS_Clock_Timeout(uint32_t ready);
348
354
362
363#ifdef __cplusplus
364}
365#endif
366
367#endif // LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32675_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:250
#define MXC_V_GCR_CLKCTRL_SYSCLK_SEL_EXTCLK
Definition: gcr_regs.h:266
#define MXC_V_GCR_CLKCTRL_SYSCLK_SEL_IBRO
Definition: gcr_regs.h:262
#define MXC_S_GCR_CLKCTRL_SYSCLK_DIV_DIV1
Definition: gcr_regs.h:238
#define MXC_S_GCR_CLKCTRL_SYSCLK_DIV_DIV128
Definition: gcr_regs.h:252
#define MXC_V_GCR_CLKCTRL_SYSCLK_SEL_IPO
Definition: gcr_regs.h:260
#define MXC_V_GCR_CLKCTRL_SYSCLK_SEL_INRO
Definition: gcr_regs.h:258
#define MXC_S_GCR_CLKCTRL_SYSCLK_DIV_DIV2
Definition: gcr_regs.h:240
#define MXC_V_GCR_CLKCTRL_SYSCLK_SEL_ERFO
Definition: gcr_regs.h:256
#define MXC_S_GCR_CLKCTRL_SYSCLK_DIV_DIV8
Definition: gcr_regs.h:244
#define MXC_V_GCR_CLKCTRL_SYSCLK_SEL_ERTCO
Definition: gcr_regs.h:264
#define MXC_S_GCR_CLKCTRL_SYSCLK_DIV_DIV32
Definition: gcr_regs.h:248
#define MXC_S_GCR_CLKCTRL_SYSCLK_DIV_DIV16
Definition: gcr_regs.h:246
#define MXC_S_GCR_CLKCTRL_SYSCLK_DIV_DIV4
Definition: gcr_regs.h:242
#define MXC_F_GCR_PCLKDIS0_UART1_POS
Definition: gcr_regs.h:414
#define MXC_F_GCR_PCLKDIS0_GPIO1_POS
Definition: gcr_regs.h:396
#define MXC_F_GCR_PCLKDIS0_SPI1_POS
Definition: gcr_regs.h:405
#define MXC_F_GCR_PCLKDIS0_DMA_POS
Definition: gcr_regs.h:399
#define MXC_F_GCR_PCLKDIS0_I2C0_POS
Definition: gcr_regs.h:417
#define MXC_F_GCR_PCLKDIS0_SPI2_POS
Definition: gcr_regs.h:408
#define MXC_F_GCR_PCLKDIS0_GPIO0_POS
Definition: gcr_regs.h:393
#define MXC_F_GCR_PCLKDIS0_TMR0_POS
Definition: gcr_regs.h:420
#define MXC_F_GCR_PCLKDIS0_TMR1_POS
Definition: gcr_regs.h:423
#define MXC_F_GCR_PCLKDIS0_UART0_POS
Definition: gcr_regs.h:411
#define MXC_F_GCR_PCLKDIS0_TMR2_POS
Definition: gcr_regs.h:426
#define MXC_F_GCR_PCLKDIS0_SPI0_POS
Definition: gcr_regs.h:402
#define MXC_F_GCR_PCLKDIS0_TMR3_POS
Definition: gcr_regs.h:429
#define MXC_F_GCR_PCLKDIS0_I2C1_POS
Definition: gcr_regs.h:432
#define MXC_F_GCR_PCLKDIS1_ICC0_POS
Definition: gcr_regs.h:547
#define MXC_F_GCR_PCLKDIS1_WWDT1_POS
Definition: gcr_regs.h:544
#define MXC_F_GCR_PCLKDIS1_WWDT0_POS
Definition: gcr_regs.h:541
#define MXC_F_GCR_PCLKDIS1_TRNG_POS
Definition: gcr_regs.h:538
#define MXC_F_GCR_PCLKDIS1_CRC_POS
Definition: gcr_regs.h:550
#define MXC_F_GCR_PCLKDIS1_I2S_POS
Definition: gcr_regs.h:559
#define MXC_F_GCR_PCLKDIS1_UART2_POS
Definition: gcr_regs.h:535
#define MXC_F_GCR_PCLKDIS1_I2C2_POS
Definition: gcr_regs.h:556
#define MXC_F_GCR_PCLKDIS1_AES_POS
Definition: gcr_regs.h:553
#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:218
#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:215
#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_RTC_POS
Definition: gcr_regs.h:209
#define MXC_F_GCR_RST0_SYS_POS
Definition: gcr_regs.h:224
#define MXC_F_GCR_RST0_PERIPH_POS
Definition: gcr_regs.h:221
#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:518
#define MXC_F_GCR_RST1_WDT1_POS
Definition: gcr_regs.h:506
#define MXC_F_GCR_RST1_CRC_POS
Definition: gcr_regs.h:509
#define MXC_F_GCR_RST1_AES_POS
Definition: gcr_regs.h:512
#define MXC_F_GCR_RST1_I2S_POS
Definition: gcr_regs.h:521
#define MXC_F_GCR_RST1_I2C1_POS
Definition: gcr_regs.h:503
#define MXC_F_MCR_CLKDIS_LPUART0_POS
Definition: mcr_regs.h:161
#define MXC_F_MCR_CLKDIS_LPTMR0_POS
Definition: mcr_regs.h:155
#define MXC_F_MCR_RST_LPTMR0
Definition: mcr_regs.h:104
#define MXC_F_MCR_RST_LPUART0
Definition: mcr_regs.h:110
#define MXC_F_MCR_RST_LPTMR1
Definition: mcr_regs.h:107
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:223
static int MXC_SYS_In_Crit_Section(void)
Polls whether code is currently executing from a critical section.
Definition: mxc_sys.h:243
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:211
mxc_sys_periph_clock_t
System clock disable enumeration. Used in MXC_SYS_ClockDisable and MXC_SYS_ClockEnable functions.
Definition: mxc_sys.h:80
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.
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:136
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:95
@ MXC_SYS_PERIPH_CLOCK_UART3
Definition: mxc_sys.h:131
@ MXC_SYS_PERIPH_CLOCK_UART0
Definition: mxc_sys.h:93
@ MXC_SYS_PERIPH_CLOCK_TMR4
Definition: mxc_sys.h:129
@ MXC_SYS_PERIPH_CLOCK_TMR2
Definition: mxc_sys.h:103
@ MXC_SYS_PERIPH_CLOCK_ICACHE
Definition: mxc_sys.h:118
@ MXC_SYS_PERIPH_CLOCK_SPI2
Definition: mxc_sys.h:91
@ MXC_SYS_PERIPH_CLOCK_GPIO1
Definition: mxc_sys.h:83
@ MXC_SYS_PERIPH_CLOCK_TMR3
Definition: mxc_sys.h:105
@ MXC_SYS_PERIPH_CLOCK_SPI0
Definition: mxc_sys.h:87
@ MXC_SYS_PERIPH_CLOCK_CRC
Definition: mxc_sys.h:120
@ MXC_SYS_PERIPH_CLOCK_I2S
Definition: mxc_sys.h:126
@ MXC_SYS_PERIPH_CLOCK_TMR0
Definition: mxc_sys.h:99
@ MXC_SYS_PERIPH_CLOCK_I2C2
Definition: mxc_sys.h:124
@ MXC_SYS_PERIPH_CLOCK_UART2
Definition: mxc_sys.h:110
@ MXC_SYS_PERIPH_CLOCK_WDT0
Definition: mxc_sys.h:114
@ MXC_SYS_PERIPH_CLOCK_SPI1
Definition: mxc_sys.h:89
@ MXC_SYS_PERIPH_CLOCK_TRNG
Definition: mxc_sys.h:112
@ MXC_SYS_PERIPH_CLOCK_DMA
Definition: mxc_sys.h:85
@ MXC_SYS_PERIPH_CLOCK_WDT1
Definition: mxc_sys.h:116
@ MXC_SYS_PERIPH_CLOCK_AES
Definition: mxc_sys.h:122
@ MXC_SYS_PERIPH_CLOCK_I2C1
Definition: mxc_sys.h:107
@ MXC_SYS_PERIPH_CLOCK_GPIO0
Definition: mxc_sys.h:81
@ MXC_SYS_PERIPH_CLOCK_TMR1
Definition: mxc_sys.h:101
@ MXC_SYS_PERIPH_CLOCK_I2C0
Definition: mxc_sys.h:97
@ 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:76
@ MXC_SYS_RESET0_TMR3
Definition: mxc_sys.h:53
@ MXC_SYS_RESET1_AES
Definition: mxc_sys.h:69
@ MXC_SYS_RESET0_SRST
Definition: mxc_sys.h:63
@ 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_RESET0_TRNG
Definition: mxc_sys.h:61
@ MXC_SYS_RESET_TMR4
Definition: mxc_sys.h:74
@ 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:62
@ MXC_SYS_RESET0_SYS
Definition: mxc_sys.h:65
@ MXC_SYS_RESET_TMR5
Definition: mxc_sys.h:75
@ MXC_SYS_RESET0_WDT0
Definition: mxc_sys.h:47
@ MXC_SYS_RESET1_I2S
Definition: mxc_sys.h:72
@ MXC_SYS_RESET1_I2C1
Definition: mxc_sys.h:67
@ MXC_SYS_RESET1_CRC
Definition: mxc_sys.h:70
@ MXC_SYS_RESET0_UART0
Definition: mxc_sys.h:54
@ MXC_SYS_RESET0_PRST
Definition: mxc_sys.h:64
@ MXC_SYS_RESET0_GPIO1
Definition: mxc_sys.h:49
@ MXC_SYS_RESET1_WDT1
Definition: mxc_sys.h:68
@ MXC_SYS_RESET1_I2C2
Definition: mxc_sys.h:71
@ 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:147
@ MXC_SYS_CLOCK_INRO
Definition: mxc_sys.h:143
@ MXC_SYS_CLOCK_IBRO
Definition: mxc_sys.h:139
@ MXC_SYS_CLOCK_ERTCO
Definition: mxc_sys.h:145
@ MXC_SYS_CLOCK_IPO
Definition: mxc_sys.h:137
@ MXC_SYS_CLOCK_ERFO
Definition: mxc_sys.h:141
Registers, Bit Masks and Bit Positions for the MCR Peripheral Module.