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