1 /**
2  * @file
3  * @brief Pulse Train data types, definitions and function prototypes.
4  */
5 
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 /* Define to prevent redundant inclusion */
27 #ifndef LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX78002_PT_H_
28 #define LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX78002_PT_H_
29 
30 /* **** Includes **** */
31 
32 #include <stdio.h>
33 #include "pt.h"
34 #include "gcr_regs.h"
35 #include "pt_regs.h"
36 #include "ptg_regs.h"
37 #include "mxc_device.h"
38 #include "mxc_errors.h"
39 #include "mxc_assert.h"
40 #include "mxc_sys.h"
41 #include "mcr_regs.h"
42 
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46 
47 /**
48  * @defgroup pulsetrain Pulse Train Engine
49  * @ingroup periphlibs
50  * @brief This is the high level API for the pulse train engine.
51  * @{
52  */
53 
54 /**
55  * Structure type for pulse train mode configuration.
56  * @note       Do not use for square wave
57  */
58 typedef struct {
59     unsigned channel; /**< PT Channel to use */
60     uint32_t bps; /**< pulse train bit rate */
61     uint32_t pattern; /**< Output pattern to shift out, starts at LSB */
62     uint8_t
63         ptLength; /**< Number of bits in pulse train, 0 = 32bits, 1 = non valid , 2 = 2 bits, ... */
64     uint16_t loop; /**< Number of times to repeat the train, 0 = continuous */
65     uint16_t
66         loopDelay; /**< Delay between loops specified in bits Example: loopDelay = 4,  delays time  = time it takes to shift out 4 bits */
67 } mxc_pt_cfg_t;
68 /**
69  * Enumeration type for the system clock scale types
70  */
71 typedef enum {
72     MXC_PT_CLK_DIV1,
73     MXC_PT_CLK_DIV2,
74     MXC_PT_CLK_DIV4,
75     MXC_PT_CLK_DIV8,
76     MXC_PT_CLK_DIV16,
77     MXC_PT_CLK_DIV32,
78     MXC_PT_CLK_DIV64,
79     MXC_PT_CLK_DIV128,
80 } mxc_clk_scale_t;
81 
82 /**
83  * @brief      This function initializes the pulse trains to a known stopped
84  *             state and sets the global PT clock scale.
85  * @param      clk_scale  Scale the system clock for the global PT clock.
86  */
87 void MXC_PT_Init(mxc_clk_scale_t clk_scale);
88 
89 /**
90  * @brief      Shutdown the pulse train channel/channels.
91  * @details    Shutdown pulse train and if all pluse trains are shut down then turn off pulse train clock.
92  * @note       Shutdown pulse train channel/channels and delete config.
93  *
94  * @param      pts Pulse train channel to operate on.
95  *
96  * @return     #E_NO_ERROR if everything is successful, @ref MXC_Error_Codes
97  *             "error" if unsuccessful.
98  */
99 void MXC_PT_Shutdown(uint32_t pts);
100 
101 /**
102  * @brief      Configures the pulse train in the specified mode.
103  * @details    The parameters in the config structure must be set before calling
104  *             this function. This function should be used for configuring pulse
105  *             train mode only.
106  * @note       The pulse train cannot be running when this function is called.
107  *
108  * @param      cfg     Pointer to pulse train configuration.
109  *
110  * @return     #E_NO_ERROR if everything is successful, @ref MXC_Error_Codes
111  *             "error" if unsuccessful.
112  */
113 int MXC_PT_Config(mxc_pt_cfg_t *cfg);
114 
115 /**
116  * @brief   Configures the pulse train in the square wave mode.
117  * @details This function should be used for configuring square wave mode only.
118  * @note    The pulse train cannot be running when this function is called
119  *
120  * @param   channel Pulse train channel to operate on
121  * @param   freq    square wave output frequency in Hz
122  *
123  * @returns #E_NO_ERROR if everything is successful, \ref MXC_Error_Codes "error" if unsuccessful.
124  */
125 int MXC_PT_SqrWaveConfig(unsigned channel, uint32_t freq);
126 
127 /**
128  * @brief   Starts the pulse trains specified.
129  *
130  * @param   pts Pulse train pts to operate on.
131  */
132 void MXC_PT_Start(unsigned pts);
133 
134 /**
135  * @brief   Stops pulse trains.
136  *
137  * @param   pts Pulse train pts to stop.
138  */
139 void MXC_PT_Stop(unsigned pts);
140 
141 /**
142  * @brief      Determines if the pulse trains selected are running
143  *
144  * @param      pts   Set the bits of pulse trains to check Bit0-\>pt0,
145  *                   Bit1-\>pt1... etc.
146  *
147  * @return     0            All pulse trains are off.
148  * @return     \>0          At least one pulse train is on.
149  */
150 uint32_t MXC_PT_IsActive(uint32_t pts);
151 
152 /**
153  * @brief   Sets the pattern of the pulse train
154  *
155  * @param   pts Pulse train pts to operate on.
156  * @param   pattern Output pattern.
157  *
158  */
159 void MXC_PT_SetPattern(unsigned pts, uint32_t pattern);
160 
161 /**
162  * @brief      Enable interrupts for the pulse trains selected.
163  *
164  * @param      pts   Bit mask of which pulse trains to enable. Set the bit
165  *                   position of each pulse train to enable it. Bit0-\>pt0,
166  *                   Bit1-\>pt1... etc, 1 will enable the interrupt, 0 to leave
167  *                   a PT channel in its current state.
168  */
169 void MXC_PT_EnableInt(uint32_t pts);
170 
171 /**
172  * @brief      Disable interrupts for the pulse trains selected.
173  *
174  * @param      pts   Bit mask of what pulse trains to disable. Set the bit
175  *                   position of each pulse train to disable it. Bit0-\>pt0,
176  *                   Bit1-\>pt1... etc, 1 will disable the interrupt, 0 to leave
177  *                   a PT channel in its current state.
178  */
179 void MXC_PT_DisableInt(uint32_t pts);
180 
181 /**
182  * @brief      Gets the pulse trains's interrupt flags.
183  *
184  * @return     The Pulse Train Interrupt Flags, \ref MXC_PT_INTFL_Register Register
185  *             for details.
186  */
187 uint32_t MXC_PT_GetFlags(void);
188 
189 /**
190  * @brief      Clears the pulse train's interrupt flag.
191  *
192  * @param      mask  bits to clear, see \ref MXC_PT_INTFL_Register Register for details.
193  */
194 void MXC_PT_ClearFlags(uint32_t flags);
195 
196 /**
197  * @brief      Setup and enables a pulse train to restart after another pulse
198  *             train has exited its loop. Each pulse train can have up to two
199  *             restart triggers.
200  *
201  * @param      start         Pulse train channel to start.
202  * @param      stop          Pulse train channel to stop.
203  * @param      restartIndex  selects which restart trigger to set (0 or 1).
204  */
205 void MXC_PT_EnableRestart(unsigned start, unsigned stop, uint8_t restartIndex);
206 
207 /**
208  * @brief      Disable the restart for the specified pulse train
209  *
210  * @param      restartIndex  selects which restart trigger to disable (0 or 1)
211  */
212 void MXC_PT_DisableRestart(unsigned channel, uint8_t restartIndex);
213 
214 /**
215  * @brief      Resynchronize individual pulse trains together. Resync will stop
216  *             those resync_pts; others will be still running
217  *
218  * @param      pts  pulse train modules that need to be re-synced by bit
219  *                        number. Bit0-\>pt0, Bit1-\>pt1... etc.
220  */
221 void MXC_PT_Resync(uint32_t pts);
222 /**@} end of group pulsetrains*/
223 
224 #ifdef __cplusplus
225 }
226 #endif
227 
228 #endif // LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX78002_PT_H_
229