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