1 //*****************************************************************************
2 //
3 //! @file am_hal_tpiu.h
4 //!
5 //! @brief Support functions for the ARM TPIU module
6 //!
7 //! Provides support functions for configuring the ARM TPIU module
8 //!
9 //! @addtogroup tpiu3p TPIU - Trace Port Interface Unit
10 //! @ingroup apollo3p_hal
11 //! @{
12 //
13 //*****************************************************************************
14 
15 //*****************************************************************************
16 //
17 // Copyright (c) 2024, Ambiq Micro, Inc.
18 // All rights reserved.
19 //
20 // Redistribution and use in source and binary forms, with or without
21 // modification, are permitted provided that the following conditions are met:
22 //
23 // 1. Redistributions of source code must retain the above copyright notice,
24 // this list of conditions and the following disclaimer.
25 //
26 // 2. Redistributions in binary form must reproduce the above copyright
27 // notice, this list of conditions and the following disclaimer in the
28 // documentation and/or other materials provided with the distribution.
29 //
30 // 3. Neither the name of the copyright holder nor the names of its
31 // contributors may be used to endorse or promote products derived from this
32 // software without specific prior written permission.
33 //
34 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
35 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
38 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
39 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
40 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
41 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
42 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
43 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
44 // POSSIBILITY OF SUCH DAMAGE.
45 //
46 // This is part of revision release_sdk_3_2_0-dd5f40c14b of the AmbiqSuite Development Package.
47 //
48 //*****************************************************************************
49 #ifndef AM_HAL_TPIU_H
50 #define AM_HAL_TPIU_H
51 
52 #include <stdint.h>
53 
54 #ifdef __cplusplus
55 extern "C"
56 {
57 #endif
58 
59 //*****************************************************************************
60 //
61 //! TPIU bit rate defines.
62 //
63 //*****************************************************************************
64 #define AM_HAL_TPIU_BAUD_57600      (115200 / 2)
65 #define AM_HAL_TPIU_BAUD_115200     (115200 * 1)
66 #define AM_HAL_TPIU_BAUD_230400     (115200 * 2)
67 #define AM_HAL_TPIU_BAUD_460800     (115200 * 4)
68 #define AM_HAL_TPIU_BAUD_250000     (1000000 / 4)
69 #define AM_HAL_TPIU_BAUD_500000     (1000000 / 2)
70 #define AM_HAL_TPIU_BAUD_1M         (1000000 * 1)
71 #define AM_HAL_TPIU_BAUD_2M         (1000000 * 2)
72 #define AM_HAL_TPIU_BAUD_DEFAULT    (AM_HAL_TPIU_BAUD_1M)
73 
74 //*****************************************************************************
75 //
76 //! TPIU register defines.
77 //
78 //*****************************************************************************
79 #define AM_HAL_TPIU_SSPSR       0xE0040000  //!< Supported Parallel Port Sizes
80 #define AM_HAL_TPIU_CSPSR       0xE0040004  //!< Current Parallel Port Size
81 #define AM_HAL_TPIU_ACPR        0xE0040010  //!< Asynchronous Clock Prescaler
82 #define AM_HAL_TPIU_SPPR        0xE00400F0  //!< Selected Pin Protocol
83 #define AM_HAL_TPIU_TYPE        0xE0040FC8  //!< TPIU Type
84 
85 //*****************************************************************************
86 //
87 //! TPIU ACPR defines.
88 //
89 //*****************************************************************************
90 #define AM_HAL_TPIU_ACPR_SWOSCALER_M    0x0000FFFF  //!< SWO baud rate prescalar
91 
92 //*****************************************************************************
93 //
94 //! TPIU_SPPR TXMODE defines.
95 //
96 //*****************************************************************************
97 #define AM_HAL_TPIU_SPPR_PARALLEL       0x00000000
98 #define AM_HAL_TPIU_SPPR_MANCHESTER     0x00000001
99 #define AM_HAL_TPIU_SPPR_NRZ            0x00000002
100 
101 //*****************************************************************************
102 //
103 //! TPIU Type defines
104 //
105 //*****************************************************************************
106 #define AM_HAL_TPIU_TYPE_NRZVALID       0x00000800
107 #define AM_HAL_TPIU_TYPE_MANCVALID      0x00000400
108 #define AM_HAL_TPIU_TYPE_PTINVALID      0x00000200
109 #define AM_HAL_TPIU_TYPE_FIFOSZ_M       0x000001C0
110 
111 //*****************************************************************************
112 //
113 //! TPIU Clock defines
114 //
115 //*****************************************************************************
116 #define AM_HAL_TPIU_TRACECLKIN_6MHZ     AM_REG_MCUCTRL_TPIUCTRL_CLKSEL(0)
117 #define AM_HAL_TPIU_TRACECLKIN_3MHZ     AM_REG_MCUCTRL_TPIUCTRL_CLKSEL(1)
118 #define AM_HAL_TPIU_TRACECLKIN_1_5MHZ   AM_REG_MCUCTRL_TPIUCTRL_CLKSEL(2)
119 #define AM_HAL_TPIU_TRACECLKIN_750KHZ   AM_REG_MCUCTRL_TPIUCTRL_CLKSEL(3)
120 
121 //*****************************************************************************
122 //
123 //! @brief Structure used for configuring the TPIU
124 //
125 //*****************************************************************************
126 typedef struct
127 {
128     //
129     //! If ui32SetItmBaud is non-zero, the ITM frequency is set to the given
130     //!  frequency, and is based on a divide-by-8 HFRC TPIU clock.\n
131     //! If zero, other structure members are used to set the TPIU configuration.
132     //
133     uint32_t ui32SetItmBaud;
134 
135     //
136     //! MCU Control TRACECLKIN clock freq.
137     //!
138     //! Valid values for ui32TraceClkIn are:
139     //!     - AM_HAL_TPIU_TRACECLKIN_6MHZ
140     //!     - AM_HAL_TPIU_TRACECLKIN_3MHZ
141     //!     - AM_HAL_TPIU_TRACECLKIN_1_5MHZ
142     //!     - AM_HAL_TPIU_TRACECLKIN_750KHZ
143     //
144     uint32_t ui32TraceClkIn;
145 
146     //
147     //! Protocol to use for the TPIU
148     //!
149     //! Valid values for ui32PinProtocol are:
150     //!     - AM_HAL_TPIU_SPPR_PARALLEL
151     //!     - AM_HAL_TPIU_SPPR_MANCHESTER
152     //!     - AM_HAL_TPIU_SPPR_NRZ
153     //
154     uint32_t ui32PinProtocol;
155 
156     //
157     //! Desired width of the TPIU parallel port
158     //
159     uint32_t ui32ParallelPortSize;
160 
161     //
162     //! Desired Clock prescaler value
163     //
164     uint32_t ui32ClockPrescaler;
165 }
166 am_hal_tpiu_config_t;
167 
168 //*****************************************************************************
169 //
170 //! @brief Enable the clock to the TPIU module.
171 //!
172 //! This function enables the clock to the TPIU module.
173 //
174 //*****************************************************************************
175 extern void am_hal_tpiu_clock_enable(void);
176 
177 //*****************************************************************************
178 //
179 //! @brief Disable the clock to the TPIU module.
180 //!
181 //! This function disables the clock to the TPIU module.
182 //
183 //*****************************************************************************
184 extern void am_hal_tpiu_clock_disable(void);
185 
186 //*****************************************************************************
187 //
188 //! @brief Set the output port width of the TPIU
189 //!
190 //! @param ui32PortWidth - The desired port width (in bits)
191 //!
192 //! @details This function uses the TPIU_CSPSR register to set the desired output port
193 //! width of the TPIU.
194 //
195 //*****************************************************************************
196 extern void am_hal_tpiu_port_width_set(uint32_t ui32PortWidth);
197 
198 //*****************************************************************************
199 //
200 //! @brief Read the supported_output port width of the TPIU
201 //!
202 //! @details This function uses the \e TPIU_SSPSR register to set the supported output
203 //! port widths of the TPIU.
204 //!
205 //! @return Current width of the TPIU output port
206 //
207 //*****************************************************************************
208 extern uint32_t am_hal_tpiu_supported_port_width_get(void);
209 
210 //*****************************************************************************
211 //
212 //! @brief Read the output port width of the TPIU
213 //!
214 //! @details This function uses the \e TPIU_CSPSR register to set the desired output
215 //! port width of the TPIU.
216 //!
217 //! @return Current width of the TPIU output port
218 //
219 //*****************************************************************************
220 extern uint32_t am_hal_tpiu_port_width_get(void);
221 
222 //*****************************************************************************
223 //
224 //! @brief Configure the TPIU based on the values in the configuration struct.
225 //!
226 //! @details This function reads the provided configuration structure, and sets the
227 //! relevant TPIU registers to achieve the desired configuration.
228 //!
229 //! @param psConfig - pointer to an am_hal_tpiu_config_t structure containing
230 //! the desired configuration information.
231 //
232 //*****************************************************************************
233 extern void am_hal_tpiu_configure(am_hal_tpiu_config_t *psConfig);
234 
235 //*****************************************************************************
236 //
237 //! @brief Enables the TPIU
238 //!
239 //! @details This function enables the ARM TPIU by setting the TPIU registers and then
240 //! enabling the TPIU clock source in MCU control register.
241 //!
242 //! @param psConfig - Structure for configuration.
243 //!         - If ui32SetItmBaud, the other structure members are used to set the
244 //!             TPIU configuration.
245 //!         - But for simplicity, ui32SetItmBaud can be set to one of the
246 //!             following, in which case all other structure members are ignored.\n
247 //!             In this case, the given BAUD rate is based on a div-by-8 HFRC clock.
248 //!                   - AM_HAL_TPIU_BAUD_57600
249 //!                   - AM_HAL_TPIU_BAUD_115200
250 //!                   - AM_HAL_TPIU_BAUD_230400
251 //!                   - AM_HAL_TPIU_BAUD_460800
252 //!                   - AM_HAL_TPIU_BAUD_500000
253 //!                   - AM_HAL_TPIU_BAUD_1M
254 //
255 //*****************************************************************************
256 extern void am_hal_tpiu_enable(am_hal_tpiu_config_t *psConfig);
257 
258 //*****************************************************************************
259 //
260 //! @brief Disables the TPIU
261 //!
262 //! This function disables the ARM TPIU by disabling the TPIU clock source
263 //! in MCU control register.
264 //
265 //*****************************************************************************
266 extern void am_hal_tpiu_disable(void);
267 
268 #ifdef __cplusplus
269 }
270 #endif
271 
272 #endif // AM_HAL_TPIU_H
273 
274 //*****************************************************************************
275 //
276 // End Doxygen group.
277 //! @}
278 //
279 //*****************************************************************************
280