/** UART1 Generated Driver File @Company Microchip Technology Inc. @File Name uart1.c @Summary This is the generated source file for the UART1 driver using PIC32MX MCUs @Description This source file provides APIs for driver for UART1. Generation Information : Product Revision : PIC32MX MCUs - pic32mx : v1.35 Device : PIC32MX470F512H Driver Version : 0.5 The generated drivers are tested against the following: Compiler : XC32 1.42 MPLAB : MPLAB X 3.55 */ /* (c) 2016 Microchip Technology Inc. and its subsidiaries. You may use this software and any derivatives exclusively with Microchip products. THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A PARTICULAR PURPOSE, OR ITS INTERACTION WITH MICROCHIP PRODUCTS, COMBINATION WITH ANY OTHER PRODUCTS, OR USE IN ANY APPLICATION. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. MICROCHIP PROVIDES THIS SOFTWARE CONDITIONALLY UPON YOUR ACCEPTANCE OF THESE TERMS. */ /** Section: Included Files */ #include "uart1.h" #ifdef __cplusplus extern "C" { #endif /** Section: Data Type Definitions */ /** UART Driver Queue Status @Summary Defines the object required for the status of the queue. */ typedef union { struct { uint8_t full:1; uint8_t empty:1; uint8_t reserved:6; } s; uint8_t status; } UART_BYTEQ_STATUS; /** UART Driver Hardware Instance Object @Summary Defines the object required for the maintenance of the hardware instance. */ typedef struct { /* RX Byte Q */ uint8_t *rxTail; uint8_t *rxHead; /* TX Byte Q */ uint8_t *txTail; uint8_t *txHead; UART_BYTEQ_STATUS rxStatus; UART_BYTEQ_STATUS txStatus; } UART_OBJECT; static UART_OBJECT uart1_obj; /** UART Driver Queue Length @Summary Defines the length of the Transmit and Receive Buffers */ #define UART1_CONFIG_TX_BYTEQ_LENGTH 8 #define UART1_CONFIG_RX_BYTEQ_LENGTH 8 /** UART Driver Queue @Summary Defines the Transmit and Receive Buffers */ static uint8_t uart1_txByteQ[UART1_CONFIG_TX_BYTEQ_LENGTH]; static uint8_t uart1_rxByteQ[UART1_CONFIG_RX_BYTEQ_LENGTH]; /** UART Hardware FIFO Buffer Length @Summary Defines the length of the Transmit and Receive FIFOs */ #define UART1_TX_FIFO_LENGTH 1 #define UART1_RX_FIFO_LENGTH 1 /** Section: Driver Interface */ void UART1_Initialize (void) { // STSEL 1S; IREN disabled; PDSEL 8N; RTSMD disabled; RXINV disabled; SIDL disabled; WAKE disabled; ABAUD disabled; LPBACK disabled; BRGH enabled; UEN TX_RX; ON enabled; U1MODE = 0x8008; // UTXISEL TX_ONE_CHAR; UTXINV disabled; ADDR 0; URXEN disabled; OERR disabled; ADM_EN disabled; URXISEL RX_ONE_CHAR; UTXBRK disabled; UTXEN disabled; ADDEN disabled; U1STA = 0x0; // U1TXREG 0; U1TXREG = 0x0; // BaudRate = 9600; Frequency = 1000000 Hz; BRG 25; U1BRG = 0x19; IEC1bits.U1RXIE = 1; U1STAbits.UTXEN = 1; U1STAbits.URXEN = 1; //Enabling UART U1MODEbits.ON = 1; uart1_obj.txHead = uart1_txByteQ; uart1_obj.txTail = uart1_txByteQ; uart1_obj.rxHead = uart1_rxByteQ; uart1_obj.rxTail = uart1_rxByteQ; uart1_obj.rxStatus.s.empty = true; uart1_obj.txStatus.s.empty = true; uart1_obj.txStatus.s.full = false; uart1_obj.rxStatus.s.full = false; } /** Maintains the driver's transmitter/receiver/error state machine and implements its ISR */ void __ISR(_UART_1_VECTOR, IPL1AUTO) _UART_1 (void) { if(IFS1bits.U1RXIF) { int count = 0; while((count < UART1_RX_FIFO_LENGTH) && (U1STAbits.URXDA == 1)) { count++; *uart1_obj.rxTail = U1RXREG; uart1_obj.rxTail++; if(uart1_obj.rxTail == (uart1_rxByteQ + UART1_CONFIG_RX_BYTEQ_LENGTH)) { uart1_obj.rxTail = uart1_rxByteQ; } uart1_obj.rxStatus.s.empty = false; if(uart1_obj.rxTail == uart1_obj.rxHead) { //Sets the flag RX full uart1_obj.rxStatus.s.full = true; break; } } IFS1CLR= 1 << _IFS1_U1RXIF_POSITION; } else if (IFS1bits.U1TXIF) { if(uart1_obj.txStatus.s.empty) { IEC1bits.U1TXIE = false; return; } IFS1CLR= 1 << _IFS1_U1TXIF_POSITION; int count = 0; while((count < UART1_TX_FIFO_LENGTH)&& !(U1STAbits.UTXBF == 1)) { count++; U1TXREG = *uart1_obj.txHead; uart1_obj.txHead++; if(uart1_obj.txHead == (uart1_txByteQ + UART1_CONFIG_TX_BYTEQ_LENGTH)) { uart1_obj.txHead = uart1_txByteQ; } uart1_obj.txStatus.s.full = false; if(uart1_obj.txHead == uart1_obj.txTail) { uart1_obj.txStatus.s.empty = true; break; } } } else { if ((U1STAbits.OERR == 1)) { U1STAbits.OERR = 0; } IFS1CLR= 1 << _IFS1_U1EIF_POSITION; } } /** Section: UART Driver Client Routines */ uint8_t UART1_Read(void) { uint8_t data = 0; data = *uart1_obj.rxHead; uart1_obj.rxHead++; if (uart1_obj.rxHead == (uart1_rxByteQ + UART1_CONFIG_RX_BYTEQ_LENGTH)) { uart1_obj.rxHead = uart1_rxByteQ; } if (uart1_obj.rxHead == uart1_obj.rxTail) { uart1_obj.rxStatus.s.empty = true; } uart1_obj.rxStatus.s.full = false; return data; } unsigned int UART1_ReadBuffer(uint8_t *buffer, const unsigned int bufLen) { unsigned int numBytesRead = 0; while (numBytesRead < ( bufLen )) { if(uart1_obj.rxStatus.s.empty) { break; } else { buffer[numBytesRead++] = UART1_Read () ; } } return numBytesRead ; } void UART1_Write(const uint8_t byte) { IEC1bits.U1TXIE = false; *uart1_obj.txTail = byte; uart1_obj.txTail++; if (uart1_obj.txTail == (uart1_txByteQ + UART1_CONFIG_TX_BYTEQ_LENGTH)) { uart1_obj.txTail = uart1_txByteQ; } uart1_obj.txStatus.s.empty = false; if (uart1_obj.txHead == uart1_obj.txTail) { uart1_obj.txStatus.s.full = true; } IEC1bits.U1TXIE = true ; } unsigned int UART1_WriteBuffer(const uint8_t *buffer , const unsigned int bufLen ) { unsigned int numBytesWritten = 0 ; while ( numBytesWritten < ( bufLen )) { if((uart1_obj.txStatus.s.full)) { break; } else { UART1_Write (buffer[numBytesWritten++] ) ; } } return numBytesWritten ; } UART1_TRANSFER_STATUS UART1_TransferStatusGet (void) { uint8_t status = 0; if(uart1_obj.txStatus.s.full) { status |= UART1_TRANSFER_STATUS_TX_FULL; } if(uart1_obj.txStatus.s.empty) { status |= UART1_TRANSFER_STATUS_TX_EMPTY; } if(uart1_obj.rxStatus.s.full) { status |= UART1_TRANSFER_STATUS_RX_FULL; } if(uart1_obj.rxStatus.s.empty) { status |= UART1_TRANSFER_STATUS_RX_EMPTY; } else { status |= UART1_TRANSFER_STATUS_RX_DATA_PRESENT; } return (UART1_TRANSFER_STATUS) status; } uint8_t UART1_Peek(uint16_t offset) { if( (uart1_obj.rxHead + offset) > (uart1_rxByteQ + UART1_CONFIG_RX_BYTEQ_LENGTH)) { return uart1_rxByteQ[offset - (uart1_rxByteQ + UART1_CONFIG_RX_BYTEQ_LENGTH - uart1_obj.rxHead)]; } else { return *(uart1_obj.rxHead + offset); } } unsigned int UART1_ReceiveBufferSizeGet(void) { if(!uart1_obj.rxStatus.s.full) { if(uart1_obj.rxHead > uart1_obj.rxTail) { return(uart1_obj.rxHead - uart1_obj.rxTail); } else { return(UART1_CONFIG_RX_BYTEQ_LENGTH - (uart1_obj.rxTail - uart1_obj.rxHead)); } } return 0; } unsigned int UART1_TransmitBufferSizeGet(void) { if(!uart1_obj.txStatus.s.full) { if(uart1_obj.txHead > uart1_obj.txTail) { return (uart1_obj.txHead - uart1_obj.txTail); } else { return (UART1_CONFIG_TX_BYTEQ_LENGTH - (uart1_obj.txTail - uart1_obj.txHead)); } } return 0; } bool UART1_ReceiveBufferIsEmpty (void) { return(uart1_obj.rxStatus.s.empty); } bool UART1_TransmitBufferIsFull(void) { return(uart1_obj.txStatus.s.full); } UART1_STATUS UART1_StatusGet (void) { return (UART1_STATUS) U1STA; } /** End of File */ #ifdef __cplusplus } #endif