1 /*
2  * Copyright (c) 2017-2019, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * *  Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * *  Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * *  Neither the name of Texas Instruments Incorporated nor the names of
17  *    its contributors may be used to endorse or promote products derived
18  *    from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 /*!*****************************************************************************
33  *  @file       CaptureCC32XX.h
34  *  @brief      Capture driver interface for CC32XX devices
35  *
36  *  # Operation #
37  *  This driver uses a general purpose timer hardware peripheral to implement
38  *  the capture functionality. The capture driver only uses half of a timer
39  *  peripheral (16-bit timer). This is not a software limitation but due to the
40  *  general purpose timer hardware implementation. For CC32XX devices, the
41  *  system clock is 80 MHz. A 16-bit timer peripheral has 24-bits of
42  *  resolution when the prescaler register is used as an 8-bit linear extension.
43  *
44  *  # Resource Allocation #
45  *  Each general purpose timer block contains two timers, Timer A and Timer B,
46  *  that can be configured to operate independently. This behavior is managed
47  *  through a set of resource allocation APIs. For example, the
48  *  TimerCC32XX_allocateTimerResource API will allocate a timer for exclusive
49  *  use. Any attempt to re-allocate this resource by the TimerCC32XX, PWMCC32XX,
50  *  or CaptureCC32xx drivers will result in a false value being returned from
51  *  the allocation API. To free a timer resource, the
52  *  TimerCC32XX_freeTimerResource is used. The application is not responsible
53  *  for calling these allocation APIs directly.
54  *
55  *  # Capture Modes #
56  *  This device implementation only works with the following  modes for
57  *  #Capture_Mode :
58  *    - #Capture_RISING_EDGE
59  *    - #Capture_FALLING_EDGE
60  *    - #Capture_ANY_EDGE
61  *  All other modes will fail.
62  *******************************************************************************
63  */
64 #ifndef ti_drivers_capture_CaptureCC32XX__include
65 #define ti_drivers_capture_CaptureCC32XX__include
66 
67 #include <stdbool.h>
68 #include <stdint.h>
69 
70 #include <ti/drivers/Capture.h>
71 #include <ti/drivers/Power.h>
72 #include <ti/drivers/dpl/HwiP.h>
73 #include <ti/devices/cc32xx/inc/hw_ints.h>
74 #include <ti/devices/cc32xx/inc/hw_ocp_shared.h>
75 
76 #ifdef __cplusplus
77 extern "C"
78 {
79 #endif
80 
81 /*! \cond */
82 /*
83  *  Capture port/pin defines for pin configuration.
84  *
85  *  The timer id (0, 1, 2, or 3) is stored in bits 31 - 30
86  *  The timer half (1 = A, 2 = B) is stored in bits 29 - 28
87  *  The interrupt number is stored in bits 27 - 20
88  *  The GPIO port (0, 1, 2, or 3) is stored in bits 19 - 16
89  *  The GPIO pad offset is stored in bits 15 - 4
90  *  The pin mode is stored in bits 3 - 0
91  *
92  *    31 - 30    29 - 28    27 - 20    19 - 16        15 - 4         3 - 0
93  *  --------------------------------------------------------------------------
94  *  | TimerId | Timer half | IntNum | GPIO Port | GPIO Pad Offset | Pin Mode |
95  *  --------------------------------------------------------------------------
96  *
97  *  The CC32XX has fixed GPIO assignments and pin modes for a given pin.
98  *  A Capture pin mode for a given pin has a fixed timer/timer-half.
99  */
100 #define CaptureCC32XX_T0A    (0x10000000 | (INT_TIMERA0A << 20))
101 #define CaptureCC32XX_T0B    (0x20000000 | (INT_TIMERA0B << 20))
102 #define CaptureCC32XX_T1A    (0x50000000 | (INT_TIMERA1A << 20))
103 #define CaptureCC32XX_T1B    (0x60000000 | (INT_TIMERA1B << 20))
104 #define CaptureCC32XX_T2A    (0x90000000 | (INT_TIMERA2A << 20))
105 #define CaptureCC32XX_T2B    (0xA0000000 | (INT_TIMERA2B << 20))
106 #define CaptureCC32XX_T3A    (0xD0000000 | (INT_TIMERA3A << 20))
107 #define CaptureCC32XX_T3B    (0xE0000000 | (INT_TIMERA3B << 20))
108 
109 #define CaptureCC32XX_GPIO0  (0x00000000 | (OCP_SHARED_O_GPIO_PAD_CONFIG_0 << 4))
110 #define CaptureCC32XX_GPIO1  (0x00000000 | (OCP_SHARED_O_GPIO_PAD_CONFIG_1 << 4))
111 #define CaptureCC32XX_GPIO2  (0x00000000 | (OCP_SHARED_O_GPIO_PAD_CONFIG_2 << 4))
112 #define CaptureCC32XX_GPIO5  (0x00000000 | (OCP_SHARED_O_GPIO_PAD_CONFIG_5 << 4))
113 #define CaptureCC32XX_GPIO6  (0x00000000 | (OCP_SHARED_O_GPIO_PAD_CONFIG_6 << 4))
114 #define CaptureCC32XX_GPIO8  (0x00010000 | (OCP_SHARED_O_GPIO_PAD_CONFIG_8 << 4))
115 #define CaptureCC32XX_GPIO9  (0x00010000 | (OCP_SHARED_O_GPIO_PAD_CONFIG_9 << 4))
116 #define CaptureCC32XX_GPIO10 (0x00010000 | (OCP_SHARED_O_GPIO_PAD_CONFIG_10 << 4))
117 #define CaptureCC32XX_GPIO11 (0x00010000 | (OCP_SHARED_O_GPIO_PAD_CONFIG_11 << 4))
118 #define CaptureCC32XX_GPIO12 (0x00010000 | (OCP_SHARED_O_GPIO_PAD_CONFIG_12 << 4))
119 #define CaptureCC32XX_GPIO13 (0x00010000 | (OCP_SHARED_O_GPIO_PAD_CONFIG_13 << 4))
120 #define CaptureCC32XX_GPIO14 (0x00010000 | (OCP_SHARED_O_GPIO_PAD_CONFIG_14 << 4))
121 #define CaptureCC32XX_GPIO15 (0x00010000 | (OCP_SHARED_O_GPIO_PAD_CONFIG_15 << 4))
122 #define CaptureCC32XX_GPIO16 (0x00020000 | (OCP_SHARED_O_GPIO_PAD_CONFIG_16 << 4))
123 #define CaptureCC32XX_GPIO22 (0x00020000 | (OCP_SHARED_O_GPIO_PAD_CONFIG_22 << 4))
124 #define CaptureCC32XX_GPIO24 (0x00030000 | (OCP_SHARED_O_GPIO_PAD_CONFIG_24 << 4))
125 #define CaptureCC32XX_GPIO30 (0x00030000 | (OCP_SHARED_O_GPIO_PAD_CONFIG_30 << 4))
126 /*! \endcond */
127 
128 /*!
129  *  \defgroup capturePinIdentifiersCC32XX CaptureCC32XX_HWAttrs 'capturePin'
130  *            field options.
131  *  @{
132  */
133 /*!
134  *  @name PIN 01, GPIO10, uses Timer0B for Capture.
135  *  @{
136  */
137 #define CaptureCC32XX_PIN_01  CaptureCC32XX_T0B | CaptureCC32XX_GPIO10 | 0xC /*!< @hideinitializer */
138 /*! @} */
139 /*!
140  *  @name PIN 02, GPIO11, uses Timer1A for Capture.
141  *  @{
142  */
143 #define CaptureCC32XX_PIN_02  CaptureCC32XX_T1A | CaptureCC32XX_GPIO11 | 0xC /*!< @hideinitializer */
144 /*! @} */
145 /*!
146  *  @name PIN 03, GPIO12, uses Timer1B for Capture.
147  *  @{
148  */
149 #define CaptureCC32XX_PIN_03  CaptureCC32XX_T1B | CaptureCC32XX_GPIO12 | 0xC /*!< @hideinitializer */
150 /*! @} */
151 /*!
152  *  @name PIN 04, GPIO13, uses Timer2A for Capture.
153  *  @{
154  */
155 #define CaptureCC32XX_PIN_04  CaptureCC32XX_T2A | CaptureCC32XX_GPIO13 | 0xC /*!< @hideinitializer */
156 /*! @} */
157 /*!
158  *  @name PIN 05, GPIO14, uses Timer2B for Capture.
159  *  @{
160  */
161 #define CaptureCC32XX_PIN_05  CaptureCC32XX_T2B | CaptureCC32XX_GPIO14 | 0xC /*!< @hideinitializer */
162 /*! @} */
163 /*!
164  *  @name PIN 06, GPIO15, uses Timer3A for Capture.
165  *  @{
166  */
167 #define CaptureCC32XX_PIN_06  CaptureCC32XX_T3A | CaptureCC32XX_GPIO15 | 0xD /*!< @hideinitializer */
168 /*! @} */
169 /*!
170  *  @name PIN 07, GPIO16, uses Timer3B for Capture.
171  *  @{
172  */
173 #define CaptureCC32XX_PIN_07  CaptureCC32XX_T3B | CaptureCC32XX_GPIO16 | 0xD /*!< @hideinitializer */
174 /*! @} */
175 /*!
176  *  @name PIN 15, GPIO22, uses Timer2A for Capture.
177  *  @{
178  */
179 #define CaptureCC32XX_PIN_15  CaptureCC32XX_T2A | CaptureCC32XX_GPIO22 | 0x5  /*!< @hideinitializer */
180 /*! @} */
181 /*!
182  *  @name PIN 17, GPIO24 (TDO), uses Timer3A for Capture.
183  *  @{
184  */
185 #define CaptureCC32XX_PIN_17  CaptureCC32XX_T3A | CaptureCC32XX_GPIO24 | 0x4  /*!< @hideinitializer */
186 /*! @} */
187 /*!
188  *  @name PIN 50, GPIO0, uses Timer0A for Capture.
189  *  @{
190  */
191 #define CaptureCC32XX_PIN_50  CaptureCC32XX_T0A | CaptureCC32XX_GPIO0 | 0x7  /*!< @hideinitializer */
192 /*! @} */
193 /*!
194  *  @name PIN 53, GPIO30, uses Timer2B for Capture.
195  *  @{
196  */
197 #define CaptureCC32XX_PIN_53  CaptureCC32XX_T2B | CaptureCC32XX_GPIO30 | 0x4  /*!< @hideinitializer */
198 /*! @} */
199 /*!
200  *  @name PIN 55, GPIO1, uses Timer0B for Capture.
201  *  @{
202  */
203 #define CaptureCC32XX_PIN_55  CaptureCC32XX_T0B | CaptureCC32XX_GPIO1 | 0x7  /*!< @hideinitializer */
204 /*! @} */
205 /*!
206  *  @name PIN 57, GPIO2, uses Timer1A for Capture.
207  *  @{
208  */
209 #define CaptureCC32XX_PIN_57  CaptureCC32XX_T1A | CaptureCC32XX_GPIO2 | 0x7  /*!< @hideinitializer */
210 /*! @} */
211 /*!
212  *  @name PIN 60, GPIO5, uses Timer2B for Capture.
213  *  @{
214  */
215 #define CaptureCC32XX_PIN_60  CaptureCC32XX_T2B | CaptureCC32XX_GPIO5 | 0x7  /*!< @hideinitializer */
216 /*! @} */
217 /*!
218  *  @name PIN 61, GPIO6, uses Timer3A for Capture.
219  *  @{
220  */
221 #define CaptureCC32XX_PIN_61  CaptureCC32XX_T3A | CaptureCC32XX_GPIO6 | 0x7  /*!< @hideinitializer */
222 /*! @} */
223 /*!
224  *  @name PIN 63, GPIO8, uses Timer3A for Capture.
225  *  @{
226  */
227 #define CaptureCC32XX_PIN_63  CaptureCC32XX_T3A | CaptureCC32XX_GPIO8 | 0xC  /*!< @hideinitializer */
228 /*! @} */
229 /*!
230  *  @name PIN 64, GPIO9, uses Timer0A for Capture.
231  *  @{
232  */
233 #define CaptureCC32XX_PIN_64  CaptureCC32XX_T0A | CaptureCC32XX_GPIO9 | 0xC  /*!< @hideinitializer */
234 /*! @} */
235 /*! @} */
236 
237 extern const Capture_FxnTable CaptureCC32XX_fxnTable;
238 
239 /*!
240  *  @brief CaptureCC32XX Hardware Attributes
241  *
242  *  Timer hardware attributes that tell the CaptureCC32XX driver specific hardware
243  *  configurations and interrupt/priority settings.
244  *
245  *  A sample structure is shown below:
246  *  @code
247  *  const CaptureCC32XX_HWAttrs captureCC32XXHWAttrs[] =
248  *  {
249  *      {
250  *          .capturePin = CaptureCC32XX_PIN_04,
251  *          .intPriority = ~0
252  *      },
253  *      {
254  *          .capturePin = CaptureCC32XX_PIN_05,
255  *          .intPriority = ~0
256  *      },
257  *  };
258  *  @endcode
259  */
260 typedef struct {
261     /*!  Specifies the input pin for the capture event. There are 17
262          pins available as inputs for capture functionality. Each
263          available pin must map to a specific general purpose timer
264          hardware instance. By specifying this attribute, a fixed
265          16-bit timer peripheral is used. */
266     uint32_t             capturePin;
267 
268     /*! The interrupt priority. */
269     uint32_t             intPriority;
270 } CaptureCC32XX_HWAttrs;
271 
272 /*!
273  *  @brief CaptureCC32XX_Object
274  *
275  *  The application must not access any member variables of this structure!
276  */
277 typedef struct {
278     HwiP_Handle           hwiHandle;
279     Power_NotifyObj       notifyObj;
280     Capture_CallBackFxn   callBack;
281     Capture_PeriodUnits   periodUnits;
282     uint32_t              mode;
283     uint32_t              timer;
284     uint32_t              previousCount;
285     bool                  isRunning;
286 } CaptureCC32XX_Object;
287 
288 #ifdef __cplusplus
289 }
290 #endif
291 
292 #endif /* ti_drivers_capture_CaptureCC32XX__include */
293