Digital Power Starter Kit 3 Firmware
dsPIC33C Boost Converter Voltage Mode Control Example
p33c_pps.c
1 /* Software License Agreement
2  * ************************************************************************************************
3  *
4  * Software License Agreement
5  *
6  * Copyright © 2021 Microchip Technology Inc. All rights reserved. Microchip licenses to you the
7  * right to use, modify, copy and distribute Software only when embedded on a Microchip
8  * microcontroller or digital signal controller, which is integrated into your product or third
9  * party product (pursuant to the sublicense terms in the accompanying license agreement).
10  *
11  * You should refer to the license agreement accompanying this Software for additional information
12  * regarding your rights and obligations.
13  *
14  * SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR
15  * IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE, NON-INFRINGEMENT
16  * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROCHIP OR ITS LICENSORS BE LIABLE OR
17  * OBLIGATED UNDER CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR
18  * OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES INCLUDING BUT NOT
19  * LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE OR CONSEQUENTIAL DAMAGES, LOST PROFITS
20  * OR LOST DATA, COST OF PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY
21  * THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
22  *
23  * ***********************************************************************************************/
24 
25 // Include Header Files
26 #include "p33c_pps.h"
27 
28 
29 /* ************************************************************************************************
30  * @file p33c_pps.c
31  * @brief Generic Peripheral Pin Select (PPS) Driver Module
32  * @see p33c_pps.h
33  *
34  * @details
35  * Some low-pin count devices have the capability to assign digital functions to a range of
36  * free configurable pins (RPx). This driver module offers functions to read from and write to
37  * the configuration registers, which will connect a digital peripheral bock with a selected
38  * pin.
39  *
40  * @ref dsPIC33F Reference Manual, Section 10: I/O Ports, Chapter 10.3: Peripheral Multiplexing
41  *
42  * Author: M91406
43  * Version: 1.3
44  *
45  * History:
46  * 11/04/2008 First Release
47  * 08/25/2009 Added definitions for new peripherals
48  * 02/05/2012 Format transfer
49  * 04/11/2016 Added definitions and compile switches for dsPIC33EP GS
50  * 05/29/2019 Added RPx pin configuration lock/unlock sequence for dsPIC33C
51  * 10/14/2020 Removed support for dsPIC33F and dsPIC33E
52  * 06/17/2021 Synchronized all function names across PRAL drivers
53  * ***********************************************************************************************/
54 
55 /*************************************************************************************************
56  * @fn uint16_t p33c_PPS_LockIO(void)
57  * @ingroup lib-layer-pral-functions-public-pps
58  * @brief Locks the Peripheral Pin Select Configuration registers against accidental changes
59  * @return
60  * unsigned integer
61  * 1: Success
62  * 0: Error
63  *
64  * @details
65  * This inline-assembly routine locks the Port Multiplexing Configuration registers by keeping
66  * the required number of cycles during the Lock pocess. This function has to be called once
67  * before digital functions are mapped to a specific pin. Once called, a series of assignments
68  * can be executed.
69  *
70  * @note If bit IOL1WAY in the Oscillator Configuration Bits (FOSC) is set, only one pin assignment
71  * operation will be allowed. all following calls of Unlock() or Lock() will be ignored.
72  *
73  * @see p33c_pps.h
74  *
75  ************************************************************************************************/
76 volatile uint16_t p33c_PPS_LockIO(void){
77 
78  __builtin_write_RPCON(0x0800); // lock PPS
79  return(RPCONbits.IOLOCK);
80 
81 }
82 
83 /*************************************************************************************************
84  * @fn uint16_t p33c_PPS_UnlockIO(void)
85  * @ingroup lib-layer-pral-functions-public-pps
86  * @brief Unlocks the Peripheral Pin Select Configuration registers to enable changes
87  * @return
88  * unsigned integer
89  * 1: Success
90  * 0: Error
91  *
92  * @details
93  * This inline-assembly routine unlocks the Port Multiplexing Configuration registers by keeping
94  * the required number of cycles during the unlock process. This function has to be called once
95  * after digital functions have been mapped to a specific pin, to prevent accidental changes.
96  *
97  * @note If bit IOL1WAY in the Oscillator Configuration Bits (FOSC) is set, only one pin assignment
98  * operation will be allowed. all following calls of Unlock() or Lock() will be ignored.
99  *
100  * @see p33c_pps.h
101  *
102  ************************************************************************************************/
103 volatile uint16_t p33c_PPS_UnlockIO(void){
104 
105  __builtin_write_RPCON(0x0000); // unlock PPS
106  return(1 - RPCONbits.IOLOCK);
107 
108 }
109 
110 /*************************************************************************************************
111  * @fn uint16_t p33c_PPS_RemapOutput(volatile uint8_t pinno, volatile uint8_t peripheral)
112  * @ingroup lib-layer-pral-functions-public-pps
113  * @brief Assigns a digital function output to a pin
114  * @param pinno: Index number of the RPx-pin of type uint8_t, which should be assigned to the function
115  * @param peripheral: Peripheral/Function ID of type uint8_t, which should be assigned to the pin
116  * @return
117  * unsigned integer
118  * 1: Success
119  * 0: Error
120  *
121  * @details
122  * Any supported digital function output (e.g. UART TxD) can be assigned to one of the RPx pins
123  * of the MCU/DSC. To assign a function output to a pin, call
124  *
125  * p33c_PPS_RemapOutput([RP-NUMBER], [FUNCTION])
126  *
127  * @code{.c}
128  * retval |= p33c_PPS_RemapOutput(PIN_RP9 , PPSOUT_SYNCO1); // Assign RP9 to PWMSyncClkOutput
129  * @endcode
130  *
131  * @see p33c_pps.h
132  *
133  *************************************************************************************************/
134 volatile uint16_t p33c_PPS_RemapOutput(volatile uint8_t pinno, volatile uint8_t peripheral){
135 
136  volatile uint16_t retval = 0;
137  volatile uint8_t *regptr;
138  volatile uint8_t pin_offset=0;
139 
140  pin_offset = (pinno - RP_PINNO_MIN); // Determine pin-offset
141  regptr = (volatile uint8_t *)&RPOR0; // get register block base address
142  regptr += (volatile uint8_t)pin_offset; // add offset
143  *regptr = (volatile uint8_t)peripheral; // copy configuration into register location
144  retval = (bool)(*regptr == (volatile uint8_t)peripheral); // Check if contents have been written correctly
145 
146  return (retval);
147 
148 }
149 
150 /*************************************************************************************************
151  * @fn uint16_t p33c_PPS_RemapInput(volatile uint8_t pinno, volatile uint8_t *peripheral)
152  * @ingroup lib-layer-pral-functions-public-pps
153  * @brief Assigns a pin to a digital function input
154  * @param pinno: Index number of the RPx-pin of type uint8_t, which should be assigned to the function
155  * @param peripheral: Pointer to peripheral/function of type uint8_t, which should be assigned to the pin
156  * @return
157  * unsigned integer
158  * 1: Success
159  * 0: Error
160  *
161  * @details
162  * Any RPx pin can be assigned to a supported digital function input (e.g. UART RxD). To assign
163  * a pin to a function input, call
164  *
165  * p33c_PPS_RemapInput([RP-NUMBER], [FUNCTION])
166  *
167  * @code{.c}
168  * retval |= p33c_PPS_RemapInput(PIN_RP10, PPSIN_U1RX); // Assign RP10 to UART1 RxD
169  * @endcode
170  *
171  * @see p33SMPS_pps.h, FOSC, IOL1WAY, IOL1WAY_ON, IOL1WAY_OFF, smpsPPS_LockIO, smpsPPS_UnlockIO, smpsPPS_RemapOutput,
172  * smpsPPS_UnmapInput, smpsPPS_UnmapOutput
173  *
174  * ***********************************************************************************************/
175 volatile uint16_t p33c_PPS_RemapInput(volatile uint8_t pinno, volatile uint8_t *peripheral)
176 {
177 
178  // Map selected pin function
179  *peripheral = pinno;
180 
181  return 1;
182 
183 }
184 
185 /*************************************************************************************************
186  * @fn uint16_t p33c_PPS_UnmapOutput(volatile uint8_t pinno)
187  * @ingroup lib-layer-pral-functions-public-pps
188  * @brief Disconnects a pin from a digital function output
189  * @param pinno: Index number of the RPx-pin of type uint8_t, which should be assigned to the function
190  * @return
191  * unsigned integer
192  * 1: Success
193  * 0: Error
194  *
195  * @details
196  * An existing assignment between any RPx pin and a supported digital function output will be
197  * dissolved.
198  *
199  * p33c_PPS_UnmapOutput([RP-NUMBER])
200  *
201  * @code{.c}
202  * retval |= p33c_PPS_UnmapOutput(PIN_RP10); // Dissolve RP10 assignment
203  * @endcode
204  *
205  * @see p33c_pps.h
206  *
207  ************************************************************************************************/
208 volatile uint16_t p33c_PPS_UnmapOutput(volatile uint8_t pinno)
209 {
210  volatile uint16_t retval=0;
211 
212  // Unmap selected pin function
213  retval = p33c_PPS_RemapOutput(pinno, PPSPIN_NULL);
214  return retval;
215 
216 }
217 
218 /************************************************************************************************
219  * @fn uint16_t p33c_PPS_UnmapInput(volatile uint8_t *peripheral)
220  * @ingroup lib-layer-pral-functions-public-pps
221  * @brief Disconnects a pin from a digital function input
222  * @param peripheral: Pointer to peripheral of type uint8_t, which should be assigned to the pin
223  * @return
224  * unsigned integer
225  * 1: Success
226  * 0: Error
227  *
228  * @details
229  * An existing assignment between any RPx pin and a supported digital function input will be
230  * dissolved.
231  *
232  * p33c_PPS_UnmapInput([RP-NUMBER])
233  *
234  * @code{.c}
235  * retval |= p33c_PPS_UnmapInput(PIN_RP10); // Dissolve RP10 assignment
236  * @endcode
237  *
238  * @see p33c_pps.h
239  *
240  ************************************************************************************************/
241 volatile uint16_t p33c_PPS_UnmapInput(volatile uint8_t *peripheral)
242 {
243  volatile uint16_t retval=0;
244 
245  // Unmap selected pin function
246  retval = p33c_PPS_RemapInput(PPSPIN_NULL, peripheral);
247  return retval;
248 
249 }
250 
251 
252 // end of file