Digital Power Starter Kit 3 Firmware
dsPIC33C Boost Converter Voltage Mode Control Example
p33c_uart.h
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 /*
23  * File: p33c_uart.h
24  * Author: M91406
25  * Comments: device driver library providing basic UART features
26  * Revision history:
27  * 1.0 initial release
28  */
29 
30 // This is a guard condition so that contents of this file are not included
31 // more than once.
32 #ifndef P33C_UART_DRIVER_INTERFACE_H
33 #define P33C_UART_DRIVER_INTERFACE_H
34 
35 #include <xc.h> // include processor files - each processor file is guarded.
36 #include <stdint.h> // include standard integer data types
37 #include <stdbool.h> // include standard boolean data types
38 #include <stddef.h> // include standard definition data types
39 
40 #include "p33c_mpuart.h" // include UART SFR abstraction layer driver for flexible port access
41 #include "p33c_osc.h" // Include system oscillator driver for timing calculation
42 #include "p33c_dma.h" // Include DMA peripheral driver for hardware data transfer
43 #include "p33c_gpio.h" // Include GPIO instance SFR abstraction header file
44 #include "p33c_pps.h" // Inlcude peripheral pin select driver header file
45 
46 /* ********************************************************************************************* *
47  * DRIVER PROPRIETARY DEFINES OF DRIVER FUNCTIONS
48  * ********************************************************************************************* */
49 
50 enum UART_DATA_BITS_e {
51  UART_DATABITS_7 = 7, // Asynchronous 7-bit UART
52  UART_DATABITS_8 = 8, // Asynchronous 8-bit UART
53 }; // UART data bit length
54 typedef enum UART_DATA_BITS_e UART_DATA_BITS_t; // UART data bit length data type
55 
56 enum UART_PARITY_e {
57  UART_PARITY_NONE = 0, // No data parity mode
58  UART_PARITY_ODD = 1, // Odd data parity mode
59  UART_PARITY_EVEN = 2 // Even data parity mode
60 }; // UART data parity mode
61 typedef enum UART_PARITY_e UART_PARITY_t; // UART data parity mode data type
62 
63 enum UART_STOP_BITS_e {
64  UART_STOPBITS_1 = 1, // Single stop bit mode
65  UART_STOPBITS_15 = 2, // 1 1/2 stop bit mode
66  UART_STOPBITS_2 = 3 // Dual stop bit mode
67 }; // UART number of stop bits
68 typedef enum UART_STOP_BITS_e UART_STOP_BITS_t; // UART number of stop bits data type
69 
70 enum UART_FLOWCONRTOL_e {
71  UART_FLOWCONRTOL_NONE = 0, // No Flow control mechanism is used
72  UART_FLOWCONRTOL_HARDWARE = 1, // Flow control mode using RTS/CTS
73  UART_FLOWCONRTOL_XON_XOFF = 2 // FLow control mode using XOn/XOff
74 }; // UART flow control selection
75 typedef enum UART_FLOWCONRTOL_e UART_FLOWCONRTOL_t; // UART flow control selection data type
76 
77 enum UART_BAUDRATE_e {
78  UART_BAUDRATE_75 = 75, // Baud rate = 75 of signal units per second
79  UART_BAUDRATE_150 = 150, // Baud rate = 150 of signal units per second
80  UART_BAUDRATE_300 = 300, // Baud rate = 300 of signal units per second
81  UART_BAUDRATE_600 = 600, // Baud rate = 600 of signal units per second
82  UART_BAUDRATE_1200 = 1200, // Baud rate = 1200 of signal units per second
83  UART_BAUDRATE_2400 = 2400, // Baud rate = 2400 of signal units per second
84  UART_BAUDRATE_4800 = 4800, // Baud rate = 4800 of signal units per second
85  UART_BAUDRATE_7200 = 7200, // Baud rate = 7200 of signal units per second
86  UART_BAUDRATE_9600 = 9600, // Baud rate = 9800 of signal units per second
87  UART_BAUDRATE_14400 = 14400, // Baud rate = 14400 of signal units per second
88  UART_BAUDRATE_19200 = 19200, // Baud rate = 19200 of signal units per second
89  UART_BAUDRATE_38400 = 38400, // Baud rate = 38400 of signal units per second
90  UART_BAUDRATE_56000 = 56000, // Baud rate = 56000 of signal units per second
91  UART_BAUDRATE_57600 = 57600, // Baud rate = 57600 of signal units per second
92  UART_BAUDRATE_115200 = 115200, // Baud rate = 115200 of signal units per second
93  UART_BAUDRATE_128000 = 128000 // Baud rate = 128000 of signal units per second
94 }; // UART baud rate selection
95 typedef enum UART_BAUDRATE_e UART_BAUDRATE_t; // UART baud rate selection data type
96 
98  unsigned ready : 1; // Bit 0: READY bit indicating that interface has been configured
99  unsigned busy : 1; // Bit 1: BUSY bit indicating that interface is occupied by internal procedure
100  unsigned port_open : 1; // Bit 2: PORT_OPEN bit indicating if UART port is open or closed
101  unsigned : 1; // Bit 3: (reserved)
102  unsigned : 1; // Bit 4: (reserved)
103  unsigned : 1; // Bit 5: (reserved)
104  unsigned : 1; // Bit 6: (reserved)
105  unsigned : 1; // Bit 7: (reserved)
106 
107  unsigned : 1; // Bit 8: (reserved)
108  unsigned : 1; // Bit 9: (reserved)
109  unsigned : 1; // Bit 10: (reserved)
110  unsigned : 1; // Bit 11: (reserved)
111  unsigned : 1; // Bit 12: (reserved)
112  unsigned rx_use_dma: 1; // Bit 13: Flag identifying if DMA support should be used for the receive channel
113  unsigned tx_use_dma: 1; // Bit 14: Flag identifying if DMA support should be used for the transmit channel
114  unsigned : 1; // Bit 15: (reserved)
115 };
117 
119  volatile uint16_t dma;
120  volatile struct GPIO_PORT_PIN_s io;
121 };
123 
125  volatile struct UART_INSTANCE_STATUS_s status;
126  volatile struct UART_INTERFACE_CHANNEL_s RxD;
127  volatile struct UART_INTERFACE_CHANNEL_s TxD;
128  volatile uint16_t uart;
129 }; // UART interface data object
130 typedef struct UART_INTERFACE_s UART_INTERFACE_t; // UART interface data object
131 
132 
134  {
135  union {
136  struct{
137  volatile bool buffer_empty : 1; // Bit 0: Buffer empty status bit
138  volatile bool buffer_full : 1; // Bit 1: Buffer full status bit
139  volatile unsigned : 6; // Bit 7-2: (reserved)
140  volatile bool buffer_overun: 1; // Bit 8: Buffer Overrun status bit
141  volatile unsigned : 6; // Bit 14-9: (reserved)
142  volatile bool msg_complete : 1; // Bit 15: Message Complete
143  } __attribute__((packed)) bits;
144  volatile uint16_t value;
145  };
146 };
148 
150  volatile struct UART_DATBUF_STATUS_s status; // Status word of user buffer
151  volatile uint8_t* buffer; // Pointer to user RECEIVE buffer
152  volatile uint16_t size; // Absolute size of buffer array in <byte>
153  volatile uint16_t data_size; // size of recently loaded data array in <byte>
154  volatile uint16_t pointer; // Pointer to next free cell of buffer array
155  volatile uint16_t package_size; // User-defined data package size sent at a time (Tx only, 0=single byte transmission)
156  };
158 
160  volatile struct UART_DATA_BUFFER_s rx_buffer; // User RECEIVE buffer
161  volatile struct UART_DATA_BUFFER_s tx_buffer; // User TRANSMIT buffer
162  };
164 
165 //#define UART_RX_BUFFER_SIZE 64
166 //extern volatile uint8_t __attribute__((near)) rx_buffer[];
167 //extern volatile uint16_t rx_buffer_size;
168 
169 //#define UART_TX_BUFFER_SIZE 500
170 //extern volatile uint8_t __attribute__((near)) tx_buffer[];
171 //extern volatile uint16_t tx_buffer_size;
172 
173 
174 /* ********************************************************************************************* *
175  * API FUNCTION PROTOTYPES
176  * ********************************************************************************************* */
177 extern volatile uint16_t p33c_UartPort_Initialize(volatile struct UART_INTERFACE_s* port);
178 
179 
180 extern volatile uint16_t p33c_UartPort_OpenPort(
181  volatile uint16_t port_no, volatile enum UART_BAUDRATE_e baud,
182  volatile enum UART_DATA_BITS_e data_bits, volatile enum UART_PARITY_e parity,
183  volatile enum UART_STOP_BITS_e stop_bits, volatile enum UART_FLOWCONRTOL_e flow_control);
184 
185 extern volatile uint16_t p33c_UartPort_Open(volatile uint16_t port_no);
186 extern volatile uint16_t p33c_UartPort_Close(volatile uint16_t port_no);
187 
188 extern volatile uint16_t p33c_UartPort_SetBaudrate(
189  volatile uint16_t port_no,
190  volatile uint32_t baud
191  );
192 extern volatile uint32_t p33c_UartPort_GetBaudrate(volatile uint16_t port_no);
193 
194 extern volatile char p33c_UartPort_ReadByte(volatile uint16_t port_no);
195 extern volatile uint16_t p33c_UartPort_WriteByte(volatile uint16_t port_no, volatile char byte);
196 extern volatile uint16_t p33c_UartPort_ClearDMAFlags(volatile struct UART_INTERFACE_s* port);
197 
198 /***********************************************************************************
199  * DMA DATA TRANSFER FUNCTIONS
200  **********************************************************************************/
201 extern volatile uint16_t p33c_UartPort_SendBufferDma(
202  volatile struct UART_INTERFACE_s* port, volatile uint16_t start_addr,
203  volatile uint16_t length
204  );
205 
206 extern volatile uint16_t p33c_UartPort_RecieveBufferDma(
207  volatile struct UART_INTERFACE_s* port, volatile uint16_t start_addr,
208  volatile uint16_t length
209  );
210 
211 extern volatile struct P33C_DMA_INSTANCE_s dmaInstanceConfigRxD;
212 extern volatile struct P33C_DMA_INSTANCE_s dmaInstanceConfigTxD;
213 extern volatile struct P33C_UART_INSTANCE_s uart_instance;
214 #endif /* P33C_UART_DRIVER_INTERFACE_H */
215 
216 // end of file
volatile struct UART_INSTANCE_STATUS_s status
Status word of the UART object.
Definition: p33c_uart.h:125
volatile uint16_t size
Definition: p33c_uart.h:152
volatile struct UART_DATA_BUFFER_s tx_buffer
Definition: p33c_uart.h:161
volatile bool msg_complete
Definition: p33c_uart.h:142
volatile uint16_t pointer
Definition: p33c_uart.h:154
volatile bool buffer_overun
Definition: p33c_uart.h:140
volatile struct UART_DATA_BUFFER_s rx_buffer
Definition: p33c_uart.h:160
volatile uint16_t uart
UART instance of interface.
Definition: p33c_uart.h:128
volatile bool buffer_empty
Definition: p33c_uart.h:137
volatile struct UART_INTERFACE_CHANNEL_s TxD
UART Receive Channel Configuration.
Definition: p33c_uart.h:127
volatile uint16_t dma
DMA instance of interface.
Definition: p33c_uart.h:119
volatile struct UART_DATBUF_STATUS_s status
Definition: p33c_uart.h:150
volatile uint16_t value
Definition: p33c_uart.h:144
volatile uint8_t * buffer
Definition: p33c_uart.h:151
volatile uint16_t package_size
Definition: p33c_uart.h:155
volatile struct GPIO_PORT_PIN_s io
GPIO configuration.
Definition: p33c_uart.h:120
volatile uint16_t data_size
Definition: p33c_uart.h:153
volatile struct UART_INTERFACE_CHANNEL_s RxD
UART Receive Channel Configuration.
Definition: p33c_uart.h:126
volatile bool buffer_full
Definition: p33c_uart.h:138