1 /**
2  * @file
3  * @brief   System level header file.
4  */
5 
6 /*******************************************************************************
7  * Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved.
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a
10  * copy of this software and associated documentation files (the "Software"),
11  * to deal in the Software without restriction, including without limitation
12  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13  * and/or sell copies of the Software, and to permit persons to whom the
14  * Software is furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be included
17  * in all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22  * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
23  * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  * OTHER DEALINGS IN THE SOFTWARE.
26  *
27  * Except as contained in this notice, the name of Maxim Integrated
28  * Products, Inc. shall not be used except as stated in the Maxim Integrated
29  * Products, Inc. Branding Policy.
30  *
31  * The mere transfer of this software does not imply any licenses
32  * of trade secrets, proprietary technology, copyrights, patents,
33  * trademarks, maskwork rights, or any other form of intellectual
34  * property whatsoever. Maxim Integrated Products, Inc. retains all
35  * ownership rights.
36  *
37  * $Date: 2019-04-15 09:35:40 -0500 (Mon, 15 Apr 2019) $
38  * $Revision: 42499 $
39  *
40  ******************************************************************************/
41 
42 #ifndef _MXC_SYS_H_
43 #define _MXC_SYS_H_
44 
45 #include "mxc_config.h"
46 #include "uart_regs.h"
47 #include "i2c_regs.h"
48 #include "gcr_regs.h"
49 #include "tmr_regs.h"
50 #include "icc_regs.h"
51 #include "spi17y_regs.h"
52 #include "spimss_regs.h"
53 #include "gpio.h"
54 #include "flc_regs.h"
55 #include "dma.h"
56 #include "wdt_regs.h"
57 
58 #ifdef __cplusplus
59 extern "C" {
60 #endif
61 #if defined ( __CC_ARM ) /* Suppressing the warning: "enum value is out of range of int" for Keil */
62 #pragma push
63 #pragma diag_suppress 66
64 #endif /* __CC_ARM */
65 
66 /** @brief System reset0 enumeration. Used in SYS_PeriphReset0 function */
67 typedef enum {
68     SYS_RESET0_DMA       = MXC_F_GCR_RSTR0_DMA,         /**< Reset DMA */
69     SYS_RESET0_WDT       = MXC_F_GCR_RSTR0_WDT,         /**< Reset WDT */
70     SYS_RESET0_GPIO0     = MXC_F_GCR_RSTR0_GPIO0,       /**< Reset GPIO0 */
71     SYS_RESET0_TIMER0    = MXC_F_GCR_RSTR0_TIMER0,      /**< Reset TIMER0 */
72     SYS_RESET0_TIMER1    = MXC_F_GCR_RSTR0_TIMER1,      /**< Reset TIMER1 */
73     SYS_RESET0_TIMER2    = MXC_F_GCR_RSTR0_TIMER2,      /**< Reset TIMER2 */
74     SYS_RESET0_UART0     = MXC_F_GCR_RSTR0_UART0,       /**< Reset UART0 */
75     SYS_RESET0_UART1     = MXC_F_GCR_RSTR0_UART1,       /**< Reset UART1 */
76     SYS_RESET0_SPI0      = MXC_F_GCR_RSTR0_SPI0,        /**< Reset SPI0 */
77     SYS_RESET0_SPI1      = MXC_F_GCR_RSTR0_SPI1,        /**< Reset SPI1 */
78     SYS_RESET0_I2C0      = MXC_F_GCR_RSTR0_I2C0,        /**< Reset I2C0 */
79     SYS_RESET0_RTC       = MXC_F_GCR_RSTR0_RTC,         /**< Reset RTC */
80     SYS_RESET0_SRST      = MXC_F_GCR_RSTR0_SRST,        /**< Soft reset */
81     SYS_RESET0_PRST      = MXC_F_GCR_RSTR0_PRST,        /**< Peripheral reset */
82     SYS_RESET0_SYSTEM    = MXC_F_GCR_RSTR0_SYSTEM,      /**< System reset */
83 } sys_reset0_t;
84 
85 /** @brief System reset1 enumeration. Used in SYS_PeriphReset1 function */
86 typedef enum {
87     SYS_RESET1_I2C1      = MXC_F_GCR_RSTR1_I2C1,        /**< Reset I2C1 */
88 } sys_reset1_t;
89 
90 /** @brief System clock disable enumeration. Used in SYS_ClockDisable and SYS_ClockEnable functions */
91 typedef enum {
92     SYS_PERIPH_CLOCK_GPIO0   = MXC_F_GCR_PERCKCN0_GPIO0D,     /**< Disable MXC_F_GCR_PERCKCN0_GPIO0D clock */
93     SYS_PERIPH_CLOCK_DMA     = MXC_F_GCR_PERCKCN0_DMAD,       /**< Disable MXC_F_GCR_PERCKCN0_DMAD clock */
94     SYS_PERIPH_CLOCK_SPI17Y  = MXC_F_GCR_PERCKCN0_SPI0D,      /**< Disable MXC_F_GCR_PERCKCN0_SPI0D clock */
95     SYS_PERIPH_CLOCK_SPIMSS  = MXC_F_GCR_PERCKCN0_SPI1D,      /**< Disable MXC_F_GCR_PERCKCN0_SPI1D clock */
96     SYS_PERIPH_CLOCK_UART0   = MXC_F_GCR_PERCKCN0_UART0D,     /**< Disable MXC_F_GCR_PERCKCN0_UART0D clock */
97     SYS_PERIPH_CLOCK_UART1   = MXC_F_GCR_PERCKCN0_UART1D,     /**< Disable MXC_F_GCR_PERCKCN0_UART1D clock */
98     SYS_PERIPH_CLOCK_I2C0    = MXC_F_GCR_PERCKCN0_I2C0D,      /**< Disable MXC_F_GCR_PERCKCN0_I2C0D clock */
99     SYS_PERIPH_CLOCK_T0      = MXC_F_GCR_PERCKCN0_T0D,        /**< Disable MXC_F_GCR_PERCKCN0_T0D clock */
100     SYS_PERIPH_CLOCK_T1      = MXC_F_GCR_PERCKCN0_T1D,        /**< Disable MXC_F_GCR_PERCKCN0_T1D clock */
101     SYS_PERIPH_CLOCK_T2      = MXC_F_GCR_PERCKCN0_T2D,        /**< Disable MXC_F_GCR_PERCKCN0_T2D clock */
102     SYS_PERIPH_CLOCK_I2C1    = MXC_F_GCR_PERCKCN0_I2C1D,      /**< Disable MXC_F_GCR_PERCKCN0_I2C1D clock */
103 } sys_periph_clock_t;
104 
105 /** @brief Clock source */
106 typedef enum {
107     SYS_CLOCK_NANORING      = MXC_V_GCR_CLKCN_CLKSEL_NANORING, /**< 8KHz nanoring on MAX32660 */
108     SYS_CLOCK_HFXIN         = MXC_V_GCR_CLKCN_CLKSEL_HFXIN,    /**< 32KHz on MAX32660 */
109     SYS_CLOCK_HFXIN_DIGITAL = 0x9,                             /**< External Clock Input*/
110     SYS_CLOCK_HIRC          = MXC_V_GCR_CLKCN_CLKSEL_HIRC,     /**< High Frequency Internal Oscillator */
111 } sys_system_clock_t;
112 
113 typedef void* sys_cfg_t;
114 
115 typedef sys_cfg_t sys_cfg_i2c_t;
116 typedef sys_cfg_t sys_cfg_flc_t;
117 typedef sys_cfg_t sys_cfg_wdt_t;
118 
119 /** @brief Map control */
120 typedef enum {
121     MAP_A,
122     MAP_B,
123     MAP_C,
124 } sys_map_t;
125 
126 /** @brief UART Flow control */
127 typedef enum {
128     UART_FLOW_DISABLE,
129     UART_FLOW_ENABLE,
130 } sys_uart_flow_t;
131 
132 /** @brief UART system configuration object */
133 typedef struct {
134     sys_map_t map;
135     sys_uart_flow_t flow_flag;
136 } sys_cfg_uart_t;
137 
138 /** @brief SPI17Y system configuration object */
139 typedef struct {
140     sys_map_t map;
141 } sys_cfg_spi17y_t;
142 
143 /** @brief SPIMSS system configuration object */
144 typedef struct {
145     sys_map_t map;
146 } sys_cfg_spimss_t;
147 
148 /** @brief I2S system configuration object */
149 typedef struct {
150     sys_map_t map;
151     dma_reqsel_t dma_reqsel_tx;
152     dma_reqsel_t dma_reqsel_rx;
153 } sys_cfg_i2s_t;
154 
155 /** @brief TIMER system configuration object */
156 typedef struct {
157    int out_en;
158 } sys_cfg_tmr_t;
159 
160 /** @brief Real Time Clock system configuration object */
161 typedef struct {
162     mxc_tmr_regs_t* tmr;
163 }  sys_cfg_rtc_t;
164 
165 
166 /** @brief Pulse Train System Configuration Object */
167 typedef gpio_cfg_t sys_cfg_pt_t;
168 
169 #if defined ( __CC_ARM ) /* Restore the warning: "enum is out of int range" for Keil */
170 #pragma pop
171 #endif /* __CC_ARM */
172 /***** Function Prototypes *****/
173 /**
174  * @brief      Selects the system clock and enables it once ready
175  * @param      clock   Enumeration for desired clock.
176  * @param      tmr     Optional tmr pointer for timeout. NULL if undesired.
177  *
178  * @returns    #E_NO_ERROR is clock is succesfully selected
179  */
180 int SYS_Clock_Select(sys_system_clock_t clock, mxc_tmr_regs_t* tmr);
181 
182 /**
183  * @brief      Enables the selected peripheral clock.
184  * @param      clock   Enumeration for desired clock.
185  */
186 void SYS_ClockEnable(sys_periph_clock_t clock);
187 
188 /**
189  * @brief      Disables the selected peripheral clock.
190  * @param      clock   Enumeration for desired clock.
191  */
192 void SYS_ClockDisable(sys_periph_clock_t clock);
193 
194 /**
195  * @brief      Enables the external 32k oscillator.
196  * @param      sys_cfg system configuration object
197  *
198  * @returns    #E_NO_ERROR is successful, appropriate error otherwise
199  */
200 int SYS_ClockEnable_X32K(sys_cfg_rtc_t *sys_cfg);
201 
202 /**
203  * @brief      Disables the external 32k oscillator.
204  *
205  * @returns    #E_NO_ERROR is successful, appropriate error otherwise
206  */
207 int SYS_ClockDisable_X32K(void);
208 
209 /**
210  * @brief      System level initialization for UART module.
211  * @param      uart      Pointer to UART module registers
212  * @param      sys_cfg   System configuration object
213  *
214  * @returns    #E_NO_ERROR if successful, appropriate error otherwise
215  */
216 int SYS_UART_Init(mxc_uart_regs_t *uart, const sys_cfg_uart_t* sys_cfg);
217 
218 /**
219  * @brief      System level shutdown for UART module
220  * @param      uart  Pointer to UART module registers
221  *
222  * @return     #E_NO_ERROR if successful, appropriate error otherwise
223  */
224 int SYS_UART_Shutdown(mxc_uart_regs_t *uart);
225 
226 /**
227  * @brief      System level initialization for I2C module.
228  * @param      i2c  Pointer to I2C module registers
229  * @param      sys_cfg  System configuration object
230  *
231  * @returns    #E_NO_ERROR if successful, appropriate error otherwise
232  */
233 int SYS_I2C_Init(mxc_i2c_regs_t *i2c,  const sys_cfg_i2c_t* sys_cfg);
234 
235 /**
236  * @brief      System level Shutdown for I2C module.
237  * @param      i2c    Pointer to I2C module registers
238  *
239  * @returns    #E_NO_ERROR if successful, appropriate error otherwise
240  */
241 int SYS_I2C_Shutdown(mxc_i2c_regs_t *i2c);
242 
243 /**
244  * @brief      Init DMA system settings
245  *
246  * @returns    #E_NO_ERROR if successful, appropriate error otherwise
247  */
248 int SYS_DMA_Init(void);
249 
250 /**
251  * @brief      Shutdown DMA system specific settings
252  *
253  * @returns    #E_NO_ERROR if successful, appropriate error otherwise
254  */
255 int SYS_DMA_Shutdown(void);
256 
257 /**
258  * @brief Get the frequency of the I2C module source clock
259  * @param      spim  Unused, pointer to I2C module registers
260  *
261  * @returns    frequency in Hz
262  */
263 unsigned SYS_I2C_GetFreq(mxc_i2c_regs_t *i2c);
264 
265 /**
266  * @brief Get the frequency of the Timer module source clock.
267  * @params     tmr  Unused, pointer to timer module registers
268  *
269  * @returns    frequency in Hz
270  */
271 unsigned SYS_TMR_GetFreq(mxc_tmr_regs_t *tmr);
272 
273 /**
274  * @brief Reset the peripherals and/or CPU in the rstr0 register.
275  * @param      Enumeration for what to reset. Can reset multiple items at once.
276  */
277 void SYS_Reset0(sys_reset0_t reset);
278 
279 /**
280  * @brief Reset the peripherals and/or CPU in the rstr1 register.
281  * @param      Enumeration for what to reset. Can reset multiple items at once.
282  */
283 void SYS_Reset1(sys_reset1_t reset);
284 
285 /**
286  * @brief Clear Cache and Line buffer.
287  */
288 void SYS_Flash_Operation(void);
289 
290 /**
291  * @brief      Init TMR system settings
292  * @param      tmr Pointer to timer module registers
293  * @param      sys_cfg System configuration object
294  *
295  * @returns    #E_NO_ERROR if successful, appropriate error otherwise
296  */
297 int SYS_TMR_Init(mxc_tmr_regs_t *tmr, const sys_cfg_tmr_t* sys_cfg);
298 
299 /**
300  * @brief      Init flash system settings
301  * @param      sys_cfg System configuration object
302  *
303  * @returns    #E_NO_ERROR if successful, appropriate error otherwise
304  */
305 int SYS_FLC_Init(const sys_cfg_flc_t* sys_cfg);
306 
307 /**
308  * @brief      Shutdown flash system specific settings
309  *
310  * @returns    #E_NO_ERROR if successful, appropriate error otherwise
311  */
312 int SYS_FLC_Shutdown(void);
313 
314 /**
315  * @brief System level initialization for SPI17Y module.
316  * @param      spi pointer to spi module registers
317  * @param      sys_cfg System configuration object
318  *
319  * @returns    E_NO_ERROR if successful, appropriate error otherwise
320  */
321 int SYS_SPI17Y_Init( mxc_spi17y_regs_t *spi, const sys_cfg_spi17y_t* sys_cfg);
322 
323 /**
324  * @brief System level shutdown for SPI17Y module
325  * @param      pointer to spi module registers
326  *
327  * @returns    E_NO_ERROR if successful, appropriate error otherwise
328  */
329 int SYS_SPI17Y_Shutdown(mxc_spi17y_regs_t *spi);
330 
331 /**
332  * @brief System level initialization for SPIMSS module.
333  * @param      spi pointer to spi module registers
334  * @param      sys_cfg System configuration object
335  *
336  * @returns    E_NO_ERROR if successful, appropriate error otherwise
337  */
338 int SYS_SPIMSS_Init(mxc_spimss_regs_t *spi, const sys_cfg_spimss_t* sys_cfg);
339 
340 /**
341  * @brief      System level shutdown for SPIMSS module
342  * @param      pointer to spi module registers
343  *
344  * @returns    E_NO_ERROR if everything is successful
345  */
346 int SYS_SPIMSS_Shutdown(mxc_spimss_regs_t *spi);
347 
348 /**
349  * @brief      Shutdown Timer system specific settings
350  * @param      tmr pointer to timer module registers
351  *
352  * @returns    #E_NO_ERROR if successful, appropriate error otherwise
353  */
354 int SYS_TMR_Shutdown(mxc_tmr_regs_t *tmr);
355 
356 /**
357  * @brief      System level initialization for I2S Module
358  * @param      sys_cfg System configuration object
359  *
360  * @returns    #E_NO_ERROR if successful, appropriate error otherwise
361  */
362 int SYS_I2S_Init(const sys_cfg_i2s_t* sys_cfg);
363 
364 /**
365  * @brief System level shutdown of I2S module
366  *
367  * @returns    #E_NO_ERROR if everything is successful
368  */
369 int SYS_I2S_Shutdown(void);
370 
371 /**
372  * @brief      Get the frequency of the I2S module source clock
373  * @param      spimss  Pointer to I2S module registers
374  *
375  * @returns    frequency in Hz
376  */
377 int SYS_I2S_GetFreq(mxc_spimss_regs_t *spimss);
378 
379 /**
380  * @brief      Init system settings for RTC square wave output.
381  * @param      sys_cfg  System configuration object
382  *
383  * @returns    #E_NO_ERROR if successful, appropriate error otherwise
384  */
385 int SYS_RTC_SqwavInit(const sys_cfg_rtc_t* sys_cfg);
386 
387 
388 /**
389  * @brief      System Tick Configuration Helper
390  *
391  *             The function enables selection of the external clock source for
392  *             the System Tick Timer. It initializes the System Timer and its
393  *             interrupt, and starts the System Tick Timer. Counter is in free
394  *             running mode to generate periodic interrupts.
395  *
396  * @param      ticks    Number of ticks between two interrupts.
397  * @param      clk_src  Selects between default SystemClock or External Clock.
398  *                      - 0 Use external clock source
399  * @param tmr  Optional tmr pointer for timeout. NULL if undesired.
400  *                      - 1 SystemClock
401  *
402  * @return     #E_NO_ERROR  Function succeeded, of #E_INVALID if an invalid value is requested
403  */
404 int SYS_SysTick_Config(uint32_t ticks, int clk_src, mxc_tmr_regs_t* tmr);
405 
406 /**
407  * @brief      Disable System Tick timer
408  */
409 void SYS_SysTick_Disable(void);
410 
411 /**
412  * @brief      Delay a requested number of SysTick Timer Ticks.
413  * @param      ticks  Number of System Ticks to delay.
414  * @note       This delay function is based on the clock used for the SysTick
415  *             timer if the SysTick timer is enabled. If the SysTick timer is
416  *             not enabled, the current SysTick registers are saved and the
417  *             timer will use the SystemClock as the source for the delay. The
418  *             delay is measured in clock ticks and is not based on the SysTick
419  *             interval.
420  *
421  * @return     #E_NO_ERROR if everything is successful
422  */
423 int SYS_SysTick_Delay(uint32_t ticks);
424 
425 /**
426  * @brief      Get the frequency of the SysTick Timer
427  *
428  * @return     frequency in Hz
429  */
430 uint32_t SYS_SysTick_GetFreq(void);
431 
432 /**
433  * @brief      Delay a requested number of microseconds.
434  * @param      us    Number of microseconds to delay.
435  * @note       Calls SYS_SysTick_Delay().
436  */
437 void SYS_SysTick_DelayUs(uint32_t us);
438 
439 /**
440  * @brief      Init WDT system settings
441  * @param      wdt     watchdog registers
442  * @param      sys_cfg System configuration object
443  */
444 int SYS_WDT_Init(mxc_wdt_regs_t* wdt, const sys_cfg_wdt_t* sys_cfg);
445 #ifdef __cplusplus
446 }
447 #endif
448 
449 #endif /* _MXC_SYS_H_*/
450 
451