Digital Power Starter Kit 3 Firmware
dsPIC33C Boost Converter Voltage Mode Control Example
p33c_uart.c
1 /*
2  * File: p33c_uart.c
3  * Author: M91406
4  *
5  * Created on May 29, 2021, 4:23 PM
6  */
7 
8 
9 #include <xc.h> // include processor files - each processor file is guarded.
10 #include <stdint.h> // include standard integer data types
11 #include <stdbool.h> // include standard boolean data types
12 #include <stddef.h> // include standard definition data types
13 
14 #include "uart.h"
15 
16 #if defined (U8MODE)
17 volatile uint16_t UartDmaTrg_Rx [] = {
18  DMATRG_UART1_RX, DMATRG_UART2_RX, DMATRG_UART3_RX, DMATRG_UART4_RX,
19  DMATRG_UART5_RX, DMATRG_UART6_RX, DMATRG_UART7_RX, DMATRG_UART8_RX
20 };
21 volatile uint16_t UartDmaTrg_Tx [] = {
22  DMATRG_UART1_TX, DMATRG_UART2_TX, DMATRG_UART3_TX, DMATRG_UART4_TX,
23  DMATRG_UART5_TX, DMATRG_UART6_TX, DMATRG_UART7_TX, DMATRG_UART8_TX
24 };
25 #elif defined (U7MODE)
26 volatile uint16_t UartDmaTrg_Rx [] = {
27  DMATRG_UART1_RX, DMATRG_UART2_RX, DMATRG_UART3_RX, DMATRG_UART4_RX,
28  DMATRG_UART5_RX, DMATRG_UART6_RX, DMATRG_UART7_RX
29 };
30 volatile uint16_t UartDmaTrg_Tx [] = {
31  DMATRG_UART1_TX, DMATRG_UART2_TX, DMATRG_UART3_TX, DMATRG_UART4_TX,
32  DMATRG_UART5_TX, DMATRG_UART6_TX, DMATRG_UART7_TX
33 };
34 #elif defined (U6MODE)
35 volatile uint16_t UartDmaTrg_Rx [] = {
36  DMATRG_UART1_RX, DMATRG_UART2_RX, DMATRG_UART3_RX, DMATRG_UART4_RX,
37  DMATRG_UART5_RX, DMATRG_UART6_RX
38 };
39 volatile uint16_t UartDmaTrg_Tx [] = {
40  DMATRG_UART1_TX, DMATRG_UART2_TX, DMATRG_UART3_TX, DMATRG_UART4_TX,
41  DMATRG_UART5_TX, DMATRG_UART6_TX
42 };
43 #elif defined (U5MODE)
44 volatile uint16_t UartDmaTrg_Rx [] = {
45  DMATRG_UART1_RX, DMATRG_UART2_RX, DMATRG_UART3_RX, DMATRG_UART4_RX,
46  DMATRG_UART5_RX
47 };
48 volatile uint16_t UartDmaTrg_Tx [] = {
49  DMATRG_UART1_TX, DMATRG_UART2_TX, DMATRG_UART3_TX, DMATRG_UART4_TX,
50  DMATRG_UART5_TX
51 };
52 #elif defined (U4MODE)
53 volatile uint16_t UartDmaTrg_Rx [] = {
54  DMATRG_UART1_RX, DMATRG_UART2_RX, DMATRG_UART3_RX, DMATRG_UART4_RX
55 };
56 volatile uint16_t UartDmaTrg_Tx [] = {
57  DMATRG_UART1_TX, DMATRG_UART2_TX, DMATRG_UART3_TX, DMATRG_UART4_TX
58 };
59 #elif defined (U3MODE)
60 volatile uint16_t UartDmaTrg_Rx [] = {
61  DMATRG_UART1_RX, DMATRG_UART2_RX, DMATRG_UART3_RX
62 };
63 volatile uint16_t UartDmaTrg_Tx [] = {
64  DMATRG_UART1_TX, DMATRG_UART2_TX, DMATRG_UART3_TX
65 };
66 #elif defined (U2MODE)
67 volatile uint16_t UartDmaTrg_Rx [] = {
68  DMATRG_UART1_RX, DMATRG_UART2_RX
69 };
70 volatile uint16_t UartDmaTrg_Tx [] = {
71  DMATRG_UART1_TX, DMATRG_UART2_TX
72 };
73 #elif defined (U1MODE)
74 volatile uint16_t UartDmaTrg_Rx [] = {
75  DMATRG_UART1_RX
76 };
77 volatile uint16_t UartDmaTrg_Tx [] = {
78  DMATRG_UART1_TX
79 };
80 #endif
81 
82 #define UartInterfase_GetDmaTriggerRx(x) (volatile uint16_t)UartDmaTrg_Rx[x-1]
83 #define UartInterfase_GetDmaTriggerTx(x) (volatile uint16_t)UartDmaTrg_Tx[x-1]
84 
85 #define UART_RXREG_ADDR_OFFSET (uint16_t)(&U1RXREG - &U1MODE)
86 #define UART_TXREG_ADDR_OFFSET (uint16_t)(&U1TXREG - &U1MODE)
87 
88 /*********************************************************************************
89  * @ingroup dev-layer-uart-properties-public
90  * @var rx_buffer
91  * @brief UART receive byte-data buffer
92  * @details
93  * UART receive data buffer to which the UART interface is writing to when
94  * new data is received.
95  **********************************************************************************/
96 //volatile uint8_t __attribute__((near)) rx_buffer[UART_RX_BUFFER_SIZE];
97 
98 /*********************************************************************************
99  * @ingroup dev-layer-uart-properties-public
100  * @var rx_buffer_size
101  * @brief UART receive data buffer size
102  * @details
103  * Size of the UART receiver data buffer in bytes
104  **********************************************************************************/
105 //volatile uint16_t rx_buffer_size = (sizeof(rx_buffer)/sizeof(rx_buffer[0]));
106 
107 /*********************************************************************************
108  * @ingroup dev-layer-uart-properties-public
109  * @var tx_buffer
110  * @brief UART transmit byte-data buffer
111  * @details
112  * UART transmit data buffer from which the UART interface is reading new
113  * data to be transmitted
114  **********************************************************************************/
115 //volatile uint8_t __attribute__((near)) tx_buffer[UART_TX_BUFFER_SIZE];
116 
117 /*********************************************************************************
118  * @ingroup dev-layer-uart-properties-public
119  * @var tx_buffer_size
120  * @brief UART transmit data buffer size
121  * @details
122  * Size of the UART transmit data buffer in bytes
123  **********************************************************************************/
124 //volatile uint16_t tx_buffer_size = (sizeof(tx_buffer)/sizeof(tx_buffer[0]));
125 
126 /*********************************************************************************
127  * @ingroup lib-layer-pral-functions-public-uart
128  * @fn uint16_t p33c_UartPort_Initialize(volatile struct UART_INTERFACE_s* port)
129  * @brief Initializes a UART interface with predefined user settings
130  * @param port Pointer to UART interface object of type UART_INTERFACE_s
131  * @return 0 = failure, Initializing UART interface was not successful
132  * @return 1 = success, Initializing UART interface was successful
133  * @details
134  * This function can be used to initialize a UART interface by handing over
135  * a predefined user-UART interface object.
136  *
137  **********************************************************************************/
138 
139 volatile uint16_t p33c_UartPort_Initialize(volatile struct UART_INTERFACE_s* port) {
140 
141  volatile uint16_t retval=1;
142  volatile struct P33C_GPIO_INSTANCE_s* gpio;
143  volatile struct P33C_DMA_MODULE_s* dmac;
144  volatile struct P33C_DMA_INSTANCE_s* dma;
145  volatile struct P33C_UART_INSTANCE_s* uart;
146 
147  // Check if object exists
148  if (port == NULL)
149  return(0);
150 
151  // Initialize device port pins assigning RX/TX
152  p33c_PPS_UnlockIO();
153  p33c_PPS_RemapInput(port->RxD.io.rpid, PPSIN_U1RX);
154  p33c_PPS_RemapOutput(port->TxD.io.rpid, PPSOUT_U1TX);
155  p33c_PPS_LockIO();
156 
157  // Set TRIS registers of RX/TX
158  gpio = p33c_GpioInstance_GetHandle(port->TxD.io.port);
159  if(gpio != NULL) {
160  gpio->TRISx.value &= ~(0x0001 << port->TxD.io.pin);
161  }
162  else { return(0); }
163 
164  gpio = p33c_GpioInstance_GetHandle(port->RxD.io.port);
165  if(gpio != NULL) {
166  gpio->TRISx.value |= (0x0001 << port->RxD.io.pin);
167  }
168  else { return(0); }
169 
170  // Initialize selected UART
171  uart = p33c_MpUartPort_GetHandle(port->uart);
172  if (uart != NULL)
173  {
174  retval &= p33c_MpUartPort_ConfigWrite(port->uart, uartConfigClear);
175 
176  uart->UxSTA.bits.TXMTIE = true; // Enable TX interrupt
177  uart->UxSTA.bits.RXBKIE = true; // Enable RX interrupt
178  uart->UxSTAH.bits.UTXISEL = 0; // Generate trigger when complete buffer is empty
179  uart->UxSTAH.bits.URXISEL = 0; // Generate trigger receive interrupt when there is 1 word or more in the buffer
180  }
181 
182  // Enable DMA support
183  if ((port->status.rx_use_dma) || (port->status.tx_use_dma))
184  {
185  // Check DMA module is used, enable if not
186  dmac = p33c_DmaModule_GetHandle();
187  if (dmac != NULL) {
188  if (!dmac->DmaCon.bits.DMAEN) {
189  p33c_DmaModule_ConfigWrite(dmaModuleConfigClear);
190  }
191  dmac->DmaCon.bits.DMAEN = true; // Enable DMA controller first
192  }
193  else { return(0); }
194 
195  // Initialize RxD DMA channel
196  if (port->status.rx_use_dma) {
197 
198  dma = p33c_DmaInstance_GetHandle(port->RxD.dma);
199 
200  if (dma != NULL) {
201  retval &= p33c_DmaInstance_ConfigWrite(port->RxD.dma, dmaInstanceConfigRxD);
202  dma->DMASRCx.value = (uint16_t)&(uart->UxRXREG); // Set fixed source address for Receive channel
203  dma->DMAINTx.bits.CHSEL = UartInterfase_GetDmaTriggerRx(port->uart); // DMA Channel Trigger Selection: UARTx Receiver
204  }
205  else { return(0); }
206  }
207  else { /* do nothing */ }
208 
209  // Initialize TxD DMA channel
210  if (port->status.tx_use_dma) {
211 
212  dma = p33c_DmaInstance_GetHandle(port->TxD.dma);
213 
214  if (dma != NULL) {
215  retval &= p33c_DmaInstance_ConfigWrite(port->TxD.dma, dmaInstanceConfigTxD);
216  dma->DMADSTx.value = (uint16_t)&(uart->UxTXREG); // Set fixed destination address for Transmit channel
217  dma->DMAINTx.bits.CHSEL = UartInterfase_GetDmaTriggerTx(port->uart); // DMA Channel Trigger Selection: UARTx Transmitter
218  }
219  else { return(0); }
220  }
221  else { /* do nothing */ }
222 
223  }
224 
225  // Set READY bit indicating successful configuration
226  port->status.ready = true;
227 
228  return(retval);
229 
230 }
231 
232 
233 /*********************************************************************************
234  * @ingroup lib-layer-pral-functions-public-uart
235  * @fn volatile uint16_t p33c_MpUartPort_OpenPort(volatile uint16_t port, volatile enum UART_BAUDRATE_e baud, volatile enum UART_DATA_BITS_e data_bits, volatile enum UART_PARITY_e parity, volatile enum UART_STOP_BITS_e stop_bits, volatile enum UART_FLOWCONRTOL_e flow_control)
236  * @brief Opens the specified UART port with the specified settings in standard RS232 mode
237  * @param 0 = port_no Index of UART instance of type unsigned integer
238  * @param 1 = baud Baud Rate in signal segments per second of type unsigned integer of the UART instance Baud Rate generator
239  * @param 2 = data_bits Data bit length
240  * @param 3 = parity Data parity type
241  * @param 4 = stop_bits Data bit length
242  * @param 5 = flow_control Flow control option
243  * @return 0 = failure, disabling UART port was not successful
244  * @return 1 = success, disabling UART port was successful
245  *
246  * @details
247  * This function opens a UART interface port with the respective settings
248  * specified by in the function parameters. No data send and receive function
249  * will be enabled by default.
250  *
251  **********************************************************************************/
252 
253 volatile uint16_t p33c_UartPort_OpenPort(
254  volatile uint16_t port_no, volatile enum UART_BAUDRATE_e baud,
255  volatile enum UART_DATA_BITS_e data_bits, volatile enum UART_PARITY_e parity,
256  volatile enum UART_STOP_BITS_e stop_bits, volatile enum UART_FLOWCONRTOL_e flow_control)
257 {
258 
259  volatile uint16_t retval=1;
260  volatile struct P33C_UART_INSTANCE_s* uart;
261 
262  // Check port number specified by user
263  if (port_no > P33C_UART_COUNT)
264  return(0);
265 
266  // Capture UART instance SFR block
267  uart = p33c_MpUartPort_GetHandle(port_no);
268 
269  // Check system oscillator settings
270  if (SystemFrequencies.fosc == 0)
271  return(0);
272 
273  // Selecting the data bits
274  switch(data_bits) {
275 
276  case UART_DATABITS_7:
277  uart->UxMODE.bits.MOD = 0b0001; // Enable 7-bit UART mode
278  break;
279 
280  case UART_DATABITS_8:
281 
282  // Selecting the data parity
283  switch(parity) {
284 
285  case UART_PARITY_NONE:
286  uart->UxMODE.bits.MOD = 0b0000; // Enable 8-bit UART mode, no parity
287  break;
288  case UART_PARITY_ODD:
289  uart->UxMODE.bits.MOD = 0b0010; // Enable 8-bit UART mode, odd parity
290  break;
291  case UART_PARITY_EVEN:
292  uart->UxMODE.bits.MOD = 0b0011; // Enable 8-bit UART mode, even parity
293  break;
294  default:
295  return(0);
296  }
297 
298  break;
299 
300  default:
301  return(0);
302  }
303 
304 
305  // Selecting the data bits
306  switch(stop_bits) {
307  case UART_STOPBITS_1:
308  uart->UxMODEH.bits.STSEL = 0b00; // 1 Stop bit sent, 1 checked at receive
309  break;
310  case UART_STOPBITS_15:
311  uart->UxMODEH.bits.STSEL = 0b01; // 1.5 Stop bits sent, 1.5 checked at receive
312  break;
313  case UART_STOPBITS_2:
314  uart->UxMODEH.bits.STSEL = 0b10; // 2 Stop bits sent, 2 checked at receive
315  break;
316  default:
317  return(0);
318  }
319 
320  // Selecting the data parity
321  switch(flow_control) {
322  case UART_FLOWCONRTOL_NONE:
323  uart->UxMODEH.bits.FLO = 0b00; // Flow control off
324  break;
325  case UART_FLOWCONRTOL_HARDWARE:
326  uart->UxMODEH.bits.FLO = 0b10; // RTS-DSR (for TX side)/CTS-DTR (for RX side) hardware flow control
327  break;
328  case UART_FLOWCONRTOL_XON_XOFF:
329  uart->UxMODEH.bits.FLO = 0b01; // XON/XOFF software flow control
330  break;
331  default:
332  return(0);
333  }
334 
335  // Set Baud Rate Generator Divider Mode to Fractional Divider
336  uart->UxMODEH.bits.BCLKSEL = 0b00; // Baud Clock Source Selection: FOSC/2 (=FP)
337  uart->UxMODEH.bits.BCLKMOD = 1; // Use fractional baud rate divider mode (highest resolution)
338 
339  // Calculate baud rate generator divider
340 
341  retval &= p33c_UartPort_SetBaudrate (port_no, baud);
342 
343  // Clear pending TX error flags
344  uart->UxSTA.bits.TXCIF = 0; // Clear Transmit Collision Interrupt Flag bit
345  uart->UxSTAH.bits.TXWRE = 0; // Clear TX Write Transmit Error Status bit
346 
347  uart->UxSTA.bits.OERR = 0; // Clear Receive Buffer Overflow Interrupt Flag bit
348  uart->UxSTA.bits.RXBKIF = 0;
349 
350  // Enable RX, TX and UART port instance
351  uart->UxSTAH.bits.UTXBE = 1; // Reset TX FIFO
352  uart->UxSTAH.bits.URXBE = 1; // Reset RX FIFO
353  uart->UxMODE.bits.UTXEN = true; // Enable Transmitter
354  uart->UxMODE.bits.URXEN = true; // Enable Receiver
355  uart->UxMODE.bits.UARTEN = true; // Enable UART Instance
356 
357  return(retval);
358 
359 }
360 
361 /*********************************************************************************
362  * @ingroup lib-layer-pral-functions-public-uart
363  * @fn volatile uint16_t p33c_UartPort_Open(volatile uint16_t port_no)
364  * @brief Opens a previously configured port
365  * @param port_no Index of UART peripheral instance of type unsigned integer
366  * @return 0 = failure, Opening UART port was not successful
367  * @return 1 = success, Opening UART port was successful
368  *
369  * @details
370  * Opens a previously configured UART peripheral instance by enabling the UART
371  * instance including receive and transmit channels.
372  * No further settings will be set. When you try to open a port for the first
373  * time, please use function 'p33c_UartPort_OpenPort(...)' specifying Baud Rate,
374  * Data Bits, Stop Bits, Parity and Flow Control.
375  *
376  **********************************************************************************/
377 
378 volatile uint16_t p33c_UartPort_Open(volatile uint16_t port_no) {
379 
380  volatile uint16_t retval=1;
381  volatile struct P33C_UART_INSTANCE_s* uart;
382 
383  // Check port number specified by user
384  if (port_no > P33C_UART_COUNT)
385  return(0);
386 
387  // Capture UART instance SFR block
388  uart = p33c_MpUartPort_GetHandle(port_no);
389  if (uart != NULL) {
390 
391  // Enable RX, TX and UART instance
392  uart->UxMODE.bits.UTXEN = true;
393  uart->UxMODE.bits.URXEN = true;
394  uart->UxMODE.bits.UARTEN = true;
395 
396  // Check if port opened successfully
397  retval = (bool) (
398  uart->UxMODE.bits.UTXEN &
399  uart->UxMODE.bits.URXEN &
400  uart->UxMODE.bits.URXEN
401  );
402  }
403  else { retval = 0; } // return error
404 
405  return(retval);
406 
407 }
408 
409 /*********************************************************************************
410  * @ingroup lib-layer-pral-functions-public-uart
411  * @fn volatile uint16_t p33c_UartPort_Close(volatile uint16_t port_no)
412  * @brief Closes a previously configured port
413  * @param port_no Index of UART peripheral instance of type unsigned integer
414  * @return 0 = failure, Closing UART port was not successful
415  * @return 1 = success, Closing UART port was successful
416  *
417  * @details
418  * Closes a previously opened UART peripheral instance by disabling the UART
419  * instance including receive and transmit channels. To reopen a closed port,
420  * please use function 'p33c_UartPort_Open', if no changes to settings have to
421  * be made, or function 'p33c_UartPort_OpenPort(...)' specifying Baud Rate,
422  * Data Bits, Stop Bits, Parity and Flow Control.
423  *
424  **********************************************************************************/
425 
426 volatile uint16_t p33c_UartPort_Close(volatile uint16_t port_no) {
427 
428  volatile uint16_t retval=1;
429  volatile struct P33C_UART_INSTANCE_s* uart;
430 
431  // Check port number specified by user
432  if (port_no > P33C_UART_COUNT)
433  return(0);
434 
435  // Capture UART instance SFR block
436  uart = p33c_MpUartPort_GetHandle(port_no);
437  if (uart != NULL) {
438 
439  // Enable RX, TX and UART instance
440  uart->UxMODE.bits.UTXEN = false;
441  uart->UxMODE.bits.URXEN = false;
442  uart->UxMODE.bits.UARTEN = false;
443 
444  // Clear status flag bits
445  uart->UxSTA.value &= 0x00FF; // Clear interrupt flag bits
446  uart->UxSTAH.bits.TXWRE = false; // Clear TX Write Transmit Error Status bit
447 
448  // Check if port closed successfully
449  retval = (1 - (bool)(
450  uart->UxMODE.bits.UTXEN &
451  uart->UxMODE.bits.URXEN &
452  uart->UxMODE.bits.URXEN
453  ));
454 
455  }
456  else { retval = 0; } // return error
457 
458  return(retval);
459 
460 }
461 
462 /*********************************************************************************
463  * @fn uint16_t p33c_UartPort_SetBaudrate(volatile uinat16_t port_no, volatile uint16_t baud)
464  * @ingroup lib-layer-pral-functions-public-uart
465  * @brief Sets the Baud Rate of the specified UART instance
466  * @param port_no Index of UART peripheral instance of type unsigned integer
467  * @param baud Baud Rate in signal segments per second of type unsigned integer of the UART peripheral instance Baud Rate generator
468  * @return 0 = failure, setting UART Baud Rate was not successful
469  * @return 1 = success, setting UART Baud Rate was successful
470  *
471  * @details
472  * This function writes the value specified by 'baud' to the Baud Rate
473  * register of the specified UART peripheral instance.
474  *
475  *********************************************************************************/
476 
477 volatile uint16_t p33c_UartPort_SetBaudrate(
478  volatile uint16_t port_no,
479  volatile uint32_t baud
480  )
481 {
482  volatile uint16_t retval=1;
483  volatile struct P33C_UART_INSTANCE_s* uart;
484  volatile uint32_t _brg=0;
485 
486  // Check port number specified by user
487  if (port_no > P33C_UART_COUNT)
488  return(0);
489 
490  // Capture UART instance SFR block
491  uart = p33c_MpUartPort_GetHandle(port_no);
492  if (uart != NULL) {
493 
494  // Update oscillator setting
495  retval &= p33c_Osc_GetFrequencies();
496 
497  // Check which baud rate option is set
498  _brg = (uint32_t)(SystemFrequencies.fp / baud);
499 
500  // IF legacy mode of baud clock generation is selected, divide baud clock setting by x,
501  // where x = 4 or 16 depending on the BRGH bit
502  if (!uart->UxMODEH.bits.BCLKMOD) {
503 
504  if (uart->UxMODE.bits.BRGH) { // If High Speed Baud Rate Clock Divider 4 is selected
505  _brg = ((_brg >> 2)-1); // divide by 4, subtract 1
506  }
507  else { // If Low Speed Baud Rate Clock Divider 16 is selected
508  _brg = ((_brg >> 4)-1); // divide by 16, subtract 1
509  }
510  }
511 
512  // Set UART port Baud Rate divisor bits
513  uart->UxBRG.value = (uint16_t)(_brg & 0x0000FFFF);
514  uart->UxBRGH.value = (uint16_t)((_brg & 0x00FF0000) >> 16);
515 
516  }
517  else { retval = 0; } // return error
518 
519 
520  return(retval);
521 
522 }
523 
524 /*********************************************************************************
525  * @fn volatile uint16_t p33c_UartPort_GetBaudrate(volatile uint16_t port_no)
526  * @ingroup lib-layer-pral-functions-public-uart
527  * @brief Gets the most recent Baud Rate of the specified UART interface
528  * @param port_no Index of UART peripheral instance of type unsigned integer
529  * @return Baud Rate in signal segments per second of type unsigned integer of the UART interface Baud Rate generator
530  *
531  * @details
532  * This function returns the value of the Baud Rate register of the
533  * specified UART peripheral instance in [Baud Per Second]
534  *
535  *********************************************************************************/
536 
537 volatile uint32_t p33c_UartPort_GetBaudrate(volatile uint16_t port_no)
538 {
539  volatile uint32_t retval=1;
540  volatile uint32_t _brg=0;
541  volatile uint32_t _baud=0;
542  volatile struct P33C_UART_INSTANCE_s* uart;
543 
544  // Check port number specified by user
545  if (port_no > P33C_UART_COUNT)
546  return(0);
547 
548  // Capture UART instance SFR block
549  uart = p33c_MpUartPort_GetHandle(port_no);
550 
551  // Update oscillator setting
552  retval &= p33c_Osc_GetFrequencies();
553 
554  // Read Baud Rate Register
555  _brg = (uart->UxBRGH.value);
556  _brg <<= 16;
557  _brg |= uart->UxBRG.value;
558 
559  // IF legacy mode of baud clock generation is selected, divide baud clock setting by x,
560  // where x = 4 or 16 depending on the BRGH bit
561  if (!uart->UxMODEH.bits.BCLKMOD) {
562 
563  if (uart->UxMODE.bits.BRGH) { // If High Speed Baud Rate Clock Divider 4 is selected
564  _brg = ((_brg+1) << 2); // add 1, multiply by 4
565  }
566  else { // If Low Speed Baud Rate Clock Divider 16 is selected
567  _brg = ((_brg+1) << 4); // add 1, multiply by 16
568  }
569  }
570 
571  // Check which baud rate option is set
572  if (_brg > 0) { // Uses fractional Baud Rate Generation
573  _baud = (uint32_t)(SystemFrequencies.fp / _brg);
574  retval = _baud;
575  }
576  else {
577  retval = 0;
578  }
579 
580  return(retval);
581 
582 }
583 
584 /*********************************************************************************
585  * volatile volatile uint16_t p33c_UartPort_WriteByte(volatile uint16_t port_no, volatile char byte)
586  * @ingroup lib-layer-pral-functions-public-uart
587  * @brief Sends a single via an opened UART interface
588  * @param port_no Index of UART peripheral instance of type unsigned integer
589  * @param byte Single data byte to be sent via specified UART interface object
590  * @return 0 = failure, setting UART Baud Rate was not successful
591  * @return 1 = success, setting UART Baud Rate was successful
592  *
593  * @details
594  * This function sends a single byte via the specified UART peripheral
595  * instance.
596  *
597  *********************************************************************************/
598 volatile uint16_t p33c_UartPort_WriteByte(volatile uint16_t port_no, volatile char byte)
599 {
600  volatile uint16_t retval=1;
601  volatile struct P33C_UART_INSTANCE_s* uart;
602 
603  // Null-pointer protection
604  uart = p33c_MpUartPort_GetHandle(port_no);
605  if (uart != NULL) {
606  uart->UxSTAH.bits.TXWRE = 0; // Clear transmit error
607  uart->UxTXREG.bits.TXREG = byte; // write byte to buffer
608  }
609  else { retval = 0; }
610 
611  return(retval);
612 }
613 
614 /*********************************************************************************
615  * volatile volatile uint16_t p33c_UartPort_ReadByte(volatile uint16_t port_no, volatile char byte)
616  * @ingroup lib-layer-pral-functions-public-uart
617  * @brief Reads a single byte from an opened UART interface
618  * @param port_no Pointer to UART peripheral instance of type unsigned integer
619  * @return Single data byte read from the specified UART interface
620  *
621  * @details
622  * This function reads a single byte from the specified UART interface.
623  *
624  *********************************************************************************/
625 volatile char p33c_UartPort_ReadByte(volatile uint16_t port_no)
626 {
627  volatile uint16_t retval=1;
628  volatile struct P33C_UART_INSTANCE_s* uart;
629 
630  // Null-pointer protection
631  uart = p33c_MpUartPort_GetHandle(port_no);
632  if (uart != NULL) {
633  retval = (char)(uart->UxRXREG.bits.RXREG);
634  }
635  else { retval = 0; }
636 
637  return(retval);
638 }
639 
640 /*********************************************************************************
641  * @fn uint16_t p33c_UartPort_SendBufferDma(volatile struct UART_INTERFACE_s* port)
642  * @ingroup lib-layer-pral-functions-public-uart
643  * @brief Sends complete user buffer via an opened UART interface using MDA
644  * @param port Pointer to UART interface object of type struct UART_INTERFACE_s
645  * @param start_addr Data buffer start address of type unsigned integer
646  * @param length Length of data buffer to be sent
647  * @return DMA buffer write to the specified UART interface
648  *
649  * @details
650  * This function reads a single byte from the specified UART interface.
651  *
652  **********************************************************************************/
653 
654 volatile uint16_t p33c_UartPort_SendBufferDma(
655  volatile struct UART_INTERFACE_s* port,
656  volatile uint16_t start_addr,
657  volatile uint16_t length
658  )
659 {
660 
661  volatile uint16_t retval=1;
662  volatile uint16_t timeout=0;
663  volatile struct P33C_UART_INSTANCE_s* uart;
664  volatile struct P33C_DMA_INSTANCE_s* dma;
665 
666  // Exit if DMA usage is disabled by user
667  if (!port->status.tx_use_dma)
668  return(0);
669 
670 
671  // Capture UART and DMA object addresses
672  uart = p33c_MpUartPort_GetHandle(port->uart);
673  dma = p33c_DmaInstance_GetHandle(port->TxD.dma);
674 
675  if ((uart != NULL) && (dma != NULL)) {
676 
677  // Set BUSY bit
678  port->status.busy = true;
679 
680  // Wait while previous process is ongoing
681  while((dma->DMACNTx.value > 0) && (timeout++<50000)); // Wait until DMA has transferred all data to destination
682  if (timeout >= 50000) return(0); // Timeout Error
683  timeout = 0; // Clear timeout counter
684  while((!uart->UxSTA.bits.TRMT) && (timeout++<50000)); // Wait until last TX buffer shifter activity has seized
685  if (timeout >= 50000) return(0); // Timeout Error
686  timeout = 0; // Clear timeout counter
687  while((!uart->UxSTAH.bits.UTXBE) && (timeout++<50000)); // Wait until UART signals empty buffer
688  if (timeout >= 50000) return(0); // Timeout Error
689  timeout = 0; // Clear timeout counter
690 
691  // Reset TX FIFO
692  uart->UxMODE.bits.UTXEN = 0; // Disable Transmitter
693  uart->UxSTAH.bits.UTXBE = 1; // Reset TX FIFO
694  uart->UxMODE.bits.UTXEN = 1; // Enable Transmitter
695 
696  // Clear pending TX error flags
697  uart->UxSTA.bits.TXCIF = 0; // Clear Transmit Collision Interrupt Flag bit
698  uart->UxSTAH.bits.TXWRE = 0; // Clear TX Write Transmit Error Status bit
699 
700  dma->DMASRCx.value = start_addr; // Set DMA start address
701  dma->DMACNTx.value = length; // Set DMA data set length
702  dma->DMACHx.bits.CHREQ = 1; // Trigger DMA request by software
703  dma->DMACHx.bits.CHEN = true; // DMA Channel Enable: enabled
704 
705  port->status.busy = false; // clear BUSY bit
706 
707  }
708  else { retval = 0; }
709 
710  return(retval);
711 }
712 
713 /*********************************************************************************
714  * @fn uint16_t p33c_UartPort_SendBufferDma(volatile struct UART_INTERFACE_s* port)
715  * @ingroup lib-layer-pral-functions-public-uart
716  * @brief Sends complete user buffer via an opened UART interface using MDA
717  * @param port Pointer to UART interface object of type struct UART_INTERFACE_s
718  * @param start_addr Data buffer start address of type unsigned integer
719  * @param length Length of data buffer to be sent
720  * @return DMA buffer write to the specified UART interface
721  *
722  * @details
723  * This function reads a single byte from the specified UART interface.
724  *
725  **********************************************************************************/
726 
727 volatile uint16_t p33c_UartPort_RecieveBufferDma(
728  volatile struct UART_INTERFACE_s* port,
729  volatile uint16_t start_addr,
730  volatile uint16_t length
731  )
732 {
733 
734  volatile uint16_t retval=1;
735  volatile struct P33C_UART_INSTANCE_s* uart;
736  volatile struct P33C_DMA_INSTANCE_s* dma;
737 
738  // Exit if DMA usage is disabled by user
739  if (!port->status.rx_use_dma)
740  return(0);
741 
742  // Capture UART and DMA object addresses
743  uart = p33c_MpUartPort_GetHandle(port->uart);
744  dma = p33c_DmaInstance_GetHandle(port->RxD.dma);
745 
746  if ((uart != NULL) && (dma != NULL)) {
747 
748  // Set BUSY bit
749  port->status.busy = true;
750 
751  // Clear pending RX error flags
752  uart->UxSTA.bits.OERR = 0; // Clear Receive Buffer Overflow Interrupt Flag bit
753 
754  dma->DMADSTx.value = start_addr; // Set DMA start address
755  dma->DMACNTx.value = length; // Set DMA data set length
756  dma->DMACHx.bits.CHREQ = 0; // Trigger DMA
757  dma->DMACHx.bits.CHEN = true; // DMA Channel Enable: enabled
758 
759  port->status.busy = false; // clear BUSY bit
760 
761  }
762  else { retval = 0; }
763 
764 
765  return(retval);
766 }
767 
768 /***********************************************************************************
769  * PRIVATE DMA CONFIGURATION TEMPLATES
770  **********************************************************************************/
771 
772 volatile struct P33C_DMA_INSTANCE_s dmaInstanceConfigRxD = {
773 
774  .DMACHx.bits.NULLW = 0, // No dummy write is initiated
775  .DMACHx.bits.TRMODE = 0b00, // Transfer Mode Selection: One-Shot
776  .DMACHx.bits.SIZE = 1, // Data Size Selection: Byte (8-bit)
777  .DMACHx.bits.SAMODE = 0b00, // Source Address Mode Selection: DMASRCn remains unchanged after a transfer completion
778  .DMACHx.bits.DAMODE = 0b01, // Destination Address Mode Selection: DMADSTn is incremented based on the SIZE bit after a transfer completion
779  .DMACHx.bits.RELOAD = 0, // Registers are not reloaded to their previous values upon the start of the next operation
780  .DMACHx.bits.CHREQ = 0, // No DMA request is pending
781 
782 // .DMACHx.value = 0x0000, // Clear DMA Channel n Control Register
783  .DMAINTx.value = 0x0000, // Clear DMA Channel n Interrupt Control Register
784  .DMASRCx.value = 0x0000, // Clear DMA Channel n Source Address Register
785  .DMADSTx.value = 0x0000, // DMA Channel n Destination Address Register
786  .DMACNTx.value = 0x0000 // DMA Channel n Count Register
787  };
788 
789 volatile struct P33C_DMA_INSTANCE_s dmaInstanceConfigTxD = {
790 
791  .DMACHx.bits.NULLW = 0, // No dummy write is initiated
792  .DMACHx.bits.TRMODE = 0b00, // Transfer Mode Selection: One-Shot
793  .DMACHx.bits.SIZE = 1, // Data Size Selection: Byte (8-bit)
794  .DMACHx.bits.SAMODE = 0b01, // Source Address Mode Selection: DMASRCn is incremented based on the SIZE bit after a transfer completion
795  .DMACHx.bits.DAMODE = 0b00, // Destination Address Mode Selection: DMADSTn remains unchanged after a transfer completion
796  .DMACHx.bits.RELOAD = 0, // Registers are not reloaded to their previous values upon the start of the next operation
797  .DMACHx.bits.CHREQ = 0, // No DMA request is pending
798 
799  .DMAINTx.value = 0x0000, // Clear DMA Channel n Interrupt Control Register
800  .DMASRCx.value = 0x0000, // Clear DMA Channel n Source Address Register
801  .DMADSTx.value = 0x0000, // DMA Channel n Destination Address Register
802  .DMACNTx.value = 0x0000 // DMA Channel n Count Register
803  };
804 
805 
806 // end of file
union P33C_UART_INSTANCE_s::@150 UxSTAH
volatile uint16_t value
Definition: p33c_gpio.h:274
union P33C_GPIO_INSTANCE_s::@124 TRISx
volatile uint16_t port
Definition: p33c_gpio.h:367
union P33C_DMA_INSTANCE_s::@106 DMACHx
volatile struct UART_INSTANCE_STATUS_s status
Status word of the UART object.
Definition: p33c_uart.h:125
union P33C_UART_INSTANCE_s::@148 UxMODEH
volatile uint16_t pin
Definition: p33c_gpio.h:368
volatile uint32_t fosc
Definition: p33c_osc.h:128
union P33C_DMA_INSTANCE_s::@107 DMAINTx
union P33C_UART_INSTANCE_s::@151 UxBRG
volatile uint16_t rpid
Definition: p33c_gpio.h:369
union P33C_UART_INSTANCE_s::@147 UxMODE
volatile uint16_t uart
UART instance of interface.
Definition: p33c_uart.h:128
union P33C_UART_INSTANCE_s::@149 UxSTA
volatile struct UART_INTERFACE_CHANNEL_s TxD
UART Receive Channel Configuration.
Definition: p33c_uart.h:127
union P33C_DMA_MODULE_s::@92 DmaCon
volatile uint16_t dma
DMA instance of interface.
Definition: p33c_uart.h:119
volatile struct tagDMACH0BITS bits
Definition: p33c_dma.h:120
union P33C_UART_INSTANCE_s::@153 UxRXREG
union P33C_DMA_INSTANCE_s::@108 DMASRCx
union P33C_DMA_INSTANCE_s::@110 DMACNTx
volatile struct tagDMACONBITS bits
Definition: p33c_dma.h:62
volatile struct GPIO_PORT_PIN_s io
GPIO configuration.
Definition: p33c_uart.h:120
union P33C_UART_INSTANCE_s::@152 UxBRGH
volatile uint16_t value
Definition: p33c_dma.h:121
union P33C_DMA_INSTANCE_s::@109 DMADSTx
struct tagU1MODEBITS bits
Definition: p33c_mpuart.h:87
volatile struct UART_INTERFACE_CHANNEL_s RxD
UART Receive Channel Configuration.
Definition: p33c_uart.h:126
union P33C_UART_INSTANCE_s::@154 UxTXREG