| Digital Power Starter Kit 3 Firmware dsPIC33C Boost Converter Voltage Mode Control Example |

p33c_mpuart.c
1 /* Microchip Technology Inc. and its subsidiaries. You may use this software
2 * and any derivatives exclusively with Microchip products.
3 *
4 * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
5 * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED
6 * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A
7 * PARTICULAR PURPOSE, OR ITS INTERACTION WITH MICROCHIP PRODUCTS, COMBINATION
8 * WITH ANY OTHER PRODUCTS, OR USE IN ANY APPLICATION.
9 *
10 * IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
11 * INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
12 * WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS
13 * BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE
14 * FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS
15 * IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF
16 * ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
17 *
18 * MICROCHIP PROVIDES THIS SOFTWARE CONDITIONALLY UPON YOUR ACCEPTANCE OF THESE
19 * TERMS.
20 */
21
22 // Include standard header files
23 #include <xc.h> // include processor files - each processor file is guarded.
24 #include <stdint.h> // include standard integer data types
25 #include <stdbool.h> // include standard boolean data types
26 #include <stddef.h> // include standard definition data types
27
28 #include "p33c_mpuart.h"
29
30 #if defined (U8MODE)
31 volatile uint16_t* p33c_MpUartPort_Handles[8]={
32 &U1MODE, &U2MODE, &U3MODE, &U4MODE,
33 &U5MODE, &U6MODE, &U7MODE, &U8MODE
34 };
35 #elif defined (U7MODE)
36 volatile uint16_t* p33c_MpUartPort_Handles[7]={
37 &U1MODE, &U2MODE, &U3MODE, &U4MODE,
38 &U5MODE, &U6MODE, &U7MODE
39 };
40 #elif defined (U6MODE)
41 volatile uint16_t* p33c_MpUartPort_Handles[6]={
42 &U1MODE, &U2MODE, &U3MODE, &U4MODE,
43 &U5MODE, &U6MODE
44 };
45 #elif defined (U5MODE)
46 volatile uint16_t* p33c_MpUartPort_Handles[5]={
47 &U1MODE, &U2MODE, &U3MODE, &U4MODE,
48 &U5MODE
49 };
50 #elif defined (U4MODE)
51 volatile uint16_t* p33c_MpUartPort_Handles[4]={
52 &U1MODE, &U2MODE, &U3MODE, &U4MODE
53 };
54 #elif defined (U3MODE)
55 volatile uint16_t* p33c_MpUartPort_Handles[3]={
56 &U1MODE, &U2MODE, &U3MODE
57 };
58 #elif defined (U2MODE)
59 volatile uint16_t* p33c_MpUartPort_Handles[2]={
60 &U1MODE, &U2MODE
61 };
62 #elif defined (U1MODE)
63 volatile uint16_t* p33c_MpUartPort_Handles[1]={
64 &U1MODE
65 };
66 #else
67 #pragma message "selected device has no supported UART ports"
68 #endif
69
70
71 /*********************************************************************************
72 * @fn struct P33C_UART_PORT_s p33c_MpUartPort_ConfigRead(volatile uint16_t uartInstance)
73 * @ingroup lib-layer-pral-functions-public-uart
74 * @brief Disposes a given UART port by resetting all its registers to default
75 * @param uartInstance Instance of the UART port (e.g. 1 = UART1, 2=UART2, etc.) of type unsigned integer
76 * @return Generic UART port Special Function Register set of type struct P33C_UART_PORT_s
77 *
78 * @details
79 * This function copies the contents of all UART ports registers of the
80 * specified generator instance (e.g. UART2) to a user variable of type
81 * P33C_UART_PORT_t. This 'virtual' UART configuration can then, for
82 * example, be analyzed and/or modified in user code and applied to another
83 * UART port using function p33c_MpUartPort_ConfigWrite(...).
84 *
85 *********************************************************************************/
86
87 volatile struct P33C_UART_INSTANCE_s p33c_MpUartPort_ConfigRead(volatile uint16_t uartInstance)
88 {
89 volatile struct P33C_UART_INSTANCE_s* uart;
90
91 // Set pointer to memory address of desired UART instance
92 uart = p33c_MpUartPort_GetHandle(uartInstance);
93
94 return(*uart);
95
96 }
97
98 /*********************************************************************************
99 * @fn uint16_t p33c_MpUartPort_ConfigWrite(
100 volatile uint16_t uartInstance,
101 volatile struct P33C_UART_PORT_s uartConfig)
102 * @ingroup lib-layer-pral-functions-public-uart
103 * @brief Disposes the specified UART port by resetting all its registers to default
104 * @param uartInstance Instance of the UART port of type unsigned integer (e.g. 1=UART1, 2=UART2, etc.)
105 * @param uartConfig Generic UART port Special Function Register set of type struct P33C_UART_PORT_s
106 * @return 0 = failure, disposing UART port was not successful
107 * @return 1 = success, disposing UART port was successful
108 *
109 * @details
110 * This function writes a user-defined UART port configuration of type
111 * P33C_UART_PORT_t to the given UART port peripheral instance (e.g. UART2).
112 *
113 *********************************************************************************/
114
115 volatile uint16_t p33c_MpUartPort_ConfigWrite(
116 volatile uint16_t uartInstance,
117 volatile struct P33C_UART_INSTANCE_s uartConfig
118 )
119 {
120 volatile uint16_t retval=1;
121 volatile struct P33C_UART_INSTANCE_s* uart;
122
123 // Set pointer to memory address of desired PWM instance
124 uart = p33c_MpUartPort_GetHandle(uartInstance);
125 *uart = uartConfig;
126
127 // Verifying configuration process
128 retval &= (bool)(uart->UxMODE.value == uartConfig.UxMODE.value);
129
130 return(retval);
131
132 }
133
134 /*********************************************************************************
135 * @fn uint16_t p33c_MpUartPort_Initialize(volatile uint16_t uartInstance)
136 * @ingroup lib-layer-pral-functions-public-uart
137 * @brief Initializes a given UART port by resetting all its registers to default
138 * @param uartInstance Instance of the UART port of type unsigned integer (e.g. 1=UART1, 2=UART2, etc.)
139 * @return 0 = failure, disposing UART port was not successful
140 * @return 1 = success, disposing UART port was successful
141 *
142 * @details
143 * This function initializes the specified UART port with default
144 * input clock settings and high resolution mode enabled. All other
145 * Special Function Registers (SFR) are reset to their RESET default
146 * values.
147 *
148 *********************************************************************************/
149
150 volatile uint16_t p33c_MpUartPort_Initialize(volatile uint16_t uartInstance)
151 {
152 volatile uint16_t retval=1;
153 volatile struct P33C_UART_INSTANCE_s* uart;
154
155 // Set pointer to memory address of desired PWM instance
156 uart = p33c_MpUartPort_GetHandle(uartInstance);
157
158 // Disable the UART port
159 retval &= p33c_MpUartPort_Disable(uart);
160
161 // Reset all SFRs to default
162 retval &= p33c_MpUartPort_ConfigWrite(uartInstance, uartConfigClear);
163
164 // Verifying configuration process
165 retval &= (bool)(uart->UxMODE.value == 0);
166
167 return(retval);
168 }
169
170
171 /*********************************************************************************
172 * @fn uint16_t p33c_MpUartPort_Dispose(volatile uint16_t uartInstance)
173 * @ingroup lib-layer-pral-functions-public-uart
174 * @brief Disposes the specified UART port by resetting all its registers to default
175 * @param uartInstance Instance of the UART port of type unsigned integer (e.g. 1=UART1, 2=UART2, etc.)
176 * @return 0 = failure, disposing UART port was not successful
177 * @return 1 = success, disposing UART port was successful
178 *
179 * @details
180 * This function disables the specified UART port and clears all
181 * its Special Function Registers (SFR) to their RESET default values.
182 *
183 * ********************************************************************************/
184
185 volatile uint16_t p33c_MpUartPort_Dispose(volatile uint16_t uartInstance)
186 {
187 volatile uint16_t retval=1;
188 volatile struct P33C_UART_INSTANCE_s* uart;
189
190 // Set pointer to memory address of desired PWM instance
191 uart = p33c_MpUartPort_GetHandle(uartInstance);
192
193 // Clear all registers of uartInstance
194 retval &= p33c_MpUartPort_ConfigWrite(uartInstance, uartConfigClear);
195
196 // Verifying configuration process
197 retval &= (bool)(uart->UxMODE.value == 0);
198
199 return(retval);
200 }
201
202 /*********************************************************************************
203 * @fn uint16_t p33c_MpUartPort_Enable(volatile struct P33C_UART_PORT_s* uart)
204 * @ingroup lib-layer-pral-functions-public-uart
205 * @brief Enables the specified UART port
206 * @param uart Pointer to UART peripheral instance register set of type struct P33C_UART_PORT_s
207 * @return 0 = failure, enabling UART port was not successful
208 * @return 1 = success, enabling UART port was successful
209 *
210 * @details
211 * This function enables the UART port. It is expected that RX and/or TX
212 * have been enabled previously as part of the peripheral configuration.
213 *
214 * @note:
215 * if RX and TX are not enabled, this function will still enable the
216 * specified UART instance but will return an error to indicate that
217 * no communication will be possible.
218 *
219 *********************************************************************************/
220
221 volatile uint16_t p33c_MpUartPort_Enable(volatile struct P33C_UART_INSTANCE_s* uart)
222 {
223 volatile uint16_t retval=1;
224
225 // Set UART port enable bit (expecting RX and/or TX to be set previously)
226 uart->UxMODE.bits.UARTEN = true;
227 retval &= (bool)((uart->UxMODE.bits.UARTEN) &&
228 ((uart->UxMODE.bits.UTXEN) || (uart->UxMODE.bits.URXEN)));
229
230 return(retval);
231
232 }
233
234 /*********************************************************************************
235 * @fn uint16_t p33c_MpUartPort_Disable(volatile struct P33C_UART_PORT_s* uartInstance)
236 * @ingroup lib-layer-pral-functions-public-uart
237 * @brief Disables the specified UART port
238 * @param uartInstance Pointer to UART peripheral instance register set of type struct P33C_UART_PORT_s
239 * @return 0 = failure, disabling UART port was not successful
240 * @return 1 = success, disabling UART port was successful
241 *
242 * @details
243 * This function disables the UART port. All communication will stop immediately.
244 *
245 *********************************************************************************/
246
247 volatile uint16_t p33c_MpUartPort_Disable(volatile struct P33C_UART_INSTANCE_s* uartInstance)
248 {
249 volatile uint16_t retval=1;
250
251 // Set UART port enable bit (allowing RX and/or TX to remain set)
252 uartInstance->UxMODE.bits.UARTEN = false;
253 retval &= (bool)(!uartInstance->UxMODE.bits.UARTEN);
254
255 return(retval);
256
257 }
258
259 /*********************************************************************************
260 * volatile uint16_t p33c_MpUartPort_GetInstance(volatile struct P33C_UART_PORT_s* uart)
261 * @ingroup lib-layer-pral-functions-public-uart
262 * @brief Get the UART port instance index of a known PWM object pointer address
263 * @param uart Pointer to UART port peripheral instance register set of type struct P33C_UART_PORT_s
264 * @return index of UART instance of type unsigned integer
265 *
266 * @details
267 * This function returns the non-zero UART instance index of the UART port of
268 * the UART Special Function Register set referenced by the pointer address of
269 * function parameter uart. If the memory address does not match any available
270 * UART instance start address, this function returns '0'.
271 *
272 *********************************************************************************/
273 volatile uint16_t p33c_MpUartPort_GetInstance(volatile struct P33C_UART_INSTANCE_s* uart)
274 {
275 volatile uint16_t retval=1;
276
277 // Null-pointer protection
278 if (uart == NULL)
279 return(0);
280
281 // Capture Instance: set pointer to memory address of desired PWM instance
282 retval = (volatile uint16_t)
283 (((volatile uint16_t)&uart->UxMODE - (volatile uint16_t)P33C_UART_SFR_START_ADDR) / P33C_UARTPORT_SFR_OFFSET) + 1;
284
285 if (retval > P33C_UART_COUNT)
286 return(0); // UART port not member of a valid group
287
288 return(retval);
289 }
290
291 /* ********************************************************************************************* *
292 * UART PORT CONFIGURATION TEMPLATES
293 * ********************************************************************************************* */
294
295 /*********************************************************************************
296 * @var struct P33C_UART_PORT_s uartConfigClear
297 * @ingroup lib-layer-pral-properties-private-uart
298 * @brief Default RESET configuration of one UART instance SFRs
299 *
300 * @details
301 * Default configuration of the UART port SFRs with all its registers
302 * being reset to their default state when the device comes out of RESET.
303 * Programmers can use this template to reset (dispose) a previously used
304 * UART port when it's not used anymore or to secure a known startup
305 * condition before writing individual configurations to its SFRs.
306 *
307 ********************************************************************************/
308
309 volatile struct P33C_UART_INSTANCE_s uartConfigClear = {
310
311 .UxMODE.value = 0x0000, // UARTEN=0, USIDL=0, WAKE=0, RXBIMD=0, BRKOVR=0, UTXBRK=0, BRGH=0, ABAUD=0, UTXEN=0, URXEN=0, MOD=0
312 .UxMODEH.value = 0x0000, // SLPEN=0, ACTIVE=0, BCLKMOD=0, BCLKSEL=0, HALFDPLX=0, RUNOVF=0, URXINV=0, STSEL=0, C0EN=0, UTXINV=0, FLO=0
313 .UxSTA.value = 0x0000, // TXMTIE=0, PERIE=0, ABDOVE=0, CERIE=0, FERIE=0, RXBKIE=0, OERIE=0, TXCIE=0, TRMT=0, PERR=0, ABDOVF=0, CERIF=0, FERR=0, RXBKIF=0, OERR=0, TXCIF=0
314 .UxSTAH.value = 0x0000, // UTXISEL2=0, UTXISEL1=0, UTXISEL0=0, URXISEL2=0, URXISEL1=0, URXISEL0=0, TXWRE=0, STPMD=0, UTXBE=0, UTXBF=0, RIDLE=0, XON=0, URXBE=0, URXBF=0
315 .UxBRG.value = 0x0000, // BRG=0
316 .UxBRGH.value = 0x0000, // BRG[19:16]=0
317 .UxRXREG.value = 0x0000, // RXREG[7:0]=0
318 .UxTXREG.value = 0x0000, // TXREG[7:0]=0
319 .UxP1.value = 0x0000, // P1[7:0]=0
320 .UxP2.value = 0x0000, // P2[7:0]=0
321 .UxP3.value = 0x0000, // P3[7:0]=0
322 .UxP3H.value = 0x0000, // P3[7:0]=0
323 .UxTXCHK.value = 0x0000, // TXCHK[7:0]=0
324 .UxRXCHK.value = 0x0000, // RXCHK[7:0]=0
325 .UxSCCON.value = 0x0000, // TXRPT1=0, TXRPT0=0, CONV=0, T0PD=0, PRTCL=0,
326 .UxSCINT.value = 0x0000, // RXRPTIF=0, TXRPTIF=0, BTCIF=0, WTCIF=0, GTCIF=0, RXRPTIE=0, TXRPTIE=0, BTCIE=0, WTCIE=0, GTCIE=0,
327 .UxINT.value = 0x0000 // WUIF=0, ABDIF=0, ABDIE=0,
328 };
329
330 /*********************************************************************************
331 * @var struct P33C_UART_PORT_s uartConfigSerialRxTx
332 * @ingroup lib-layer-pral-properties-private-uart
333 * @brief Default UART configuration of one UART instance for standard serial communication without hardware handshake
334 *
335 * @details
336 * Default configuration of a UART port SFRs with all its registers
337 * being set for a simple, default UART operation with RX and TX channels
338 * enabled but without hardware handshake.
339 * Programmers can use this template to quickly set a standard UART port
340 * up for simple pure RX/TX operation without handshake. If specific,
341 * additional features are required, these registers must be written after
342 * this configuration template has been loaded to prevent they are
343 * accidentally overwritten.
344 *
345 * @note
346 * All configuration templates initialize SFRs with ENABLE bits being
347 * cleared, requiring the user enables the peripheral instance separately.
348 *
349 * @note
350 * The UART instance will be configured with RX and TX enabled using the
351 * high resolution mode with fractional Baud Rate generation. The default
352 * input clock is FOSC/2 (FP)
353 *
354 ********************************************************************************/
355
356 volatile struct P33C_UART_INSTANCE_s uartConfigSerialRxTx = {
357
358 .UxMODE.value = 0x00B0, // UARTEN=0, USIDL=0, WAKE=0, RXBIMD=0, BRKOVR=0, UTXBRK=0, BRGH=1, ABAUD=0, UTXEN=1, URXEN=1, MOD=0
359 .UxMODEH.value = 0x0880, // SLPEN=0, ACTIVE=0, BCLKMOD=1, BCLKSEL=0, HALFDPLX=0, RUNOVF=1, URXINV=0, STSEL=0, C0EN=0, UTXINV=0, FLO=0
360 .UxSTA.value = 0x0000, // TXMTIE=0, PERIE=0, ABDOVE=0, CERIE=0, FERIE=0, RXBKIE=0, OERIE=0, TXCIE=0, TRMT=0, PERR=0, ABDOVF=0, CERIF=0, FERR=0, RXBKIF=0, OERR=0, TXCIF=0
361 .UxSTAH.value = 0x0000, // UTXISEL2=0, UTXISEL1=0, UTXISEL0=0, URXISEL2=0, URXISEL1=0, URXISEL0=0, TXWRE=0, STPMD=0, UTXBE=0, UTXBF=0, RIDLE=0, XON=0, URXBE=0, URXBF=0
362 .UxBRG.value = 0x0000, // BRG=0
363 .UxBRGH.value = 0x0000, // BRG[19:16]=0
364 .UxRXREG.value = 0x0000, // RXREG[7:0]=0
365 .UxTXREG.value = 0x0000, // TXREG[7:0]=0
366 .UxP1.value = 0x0000, // P1[7:0]=0
367 .UxP2.value = 0x0000, // P2[7:0]=0
368 .UxP3.value = 0x0000, // P3[7:0]=0
369 .UxP3H.value = 0x0000, // P3[7:0]=0
370 .UxTXCHK.value = 0x0000, // TXCHK[7:0]=0
371 .UxRXCHK.value = 0x0000, // RXCHK[7:0]=0
372 .UxSCCON.value = 0x0000, // TXRPT1=0, TXRPT0=0, CONV=0, T0PD=0, PRTCL=0,
373 .UxSCINT.value = 0x0000, // RXRPTIF=0, TXRPTIF=0, BTCIF=0, WTCIF=0, GTCIF=0, RXRPTIE=0, TXRPTIE=0, BTCIE=0, WTCIE=0, GTCIE=0,
374 .UxINT.value = 0x0000 // WUIF=0, ABDIF=0, ABDIE=0,
375 };
376
377
378
379
380 // end of file
P33C_UART_INSTANCE_s::UxMODE
union P33C_UART_INSTANCE_s::@147 UxMODE
P33C_UART_INSTANCE_s::value
uint16_t value
Definition: p33c_mpuart.h:88
P33C_UART_INSTANCE_s::bits
struct tagU1MODEBITS bits
Definition: p33c_mpuart.h:87
P33C_UART_INSTANCE_s
Definition: p33c_mpuart.h:85