/***************************************************************************** * Copyright (C) 2008 * ProbeStar Telematics, LLC * All Rights Reserved. Proprietary and Confidential. *============================================================================ * Serial Initialization & Interrupt Handler *---------------------------------------------------------------------------- * Serial port driver loop using USART, interrupt driven. *****************************************************************************/ /***************************************************************************** * includes *****************************************************************************/ #include "system.h" #include "serial.h" #include "delay.h" #include "queue.h" /***************************************************************************** * defines *****************************************************************************/ /***************************************************************************** * macros *****************************************************************************/ /***************************************************************************** * structs & typedefs *****************************************************************************/ /***************************************************************************** * global constants *****************************************************************************/ /***************************************************************************** * global variables *****************************************************************************/ // interrupt driven serial i/o uses queues SERBANK Queue g_qSerialRx = { 0, 0, 0, 0 }; SERBANK Queue g_qSerialTx = { 0, 0, 0, 0 }; /***************************************************************************** * static constants *****************************************************************************/ /***************************************************************************** * static variables *****************************************************************************/ static LIBBANK BYTE byRx = 0; static LIBBANK BYTE byTx = 0; /***************************************************************************** * static prototypes *****************************************************************************/ /***************************************************************************** * C functions *****************************************************************************/ void SerialInit( void ) { // fix these RC6 = 1; RC7 = 1; TRISC6 = 0; TRISC7 = 1; // baud rate SerialBaud( BAUD ); // asynchronous serial // recv, 8 bits, continuous RCSTA = 0b10010000; // xmit, 8 bits, high speed TXSTA = 0b00100110; CREN = 0; CREN = 1; } void SerialAlloc( BYTE *pbyTx, BYTE nTx, BYTE *pbyRx, BYTE nRx ) { QueueAlloc( &g_qSerialTx, pbyTx, nTx ); QueueAlloc( &g_qSerialRx, pbyRx, nRx ); if ( nTx ) { RCIE = 1; #ifdef _PIC18 RCIP = 1; #endif } if ( nRx ) { TXIE = 0; // turned on by putch() #ifdef _PIC18 TXIP = 1; #endif } RCIF = TXIF = 0; } void SerialClear( void ) { CREN = 0; CREN = 1; QueueClear( &g_qSerialTx ); QueueClear( &g_qSerialRx ); getchflush(); } void SerialHandler( void ) { // RC7/RX if ( RCIF ) { // check queue, lose a byte if necessary while ( _QueueFull( g_qSerialRx ) ) _QueueGet( g_qSerialRx, byRx ); // read char byRx = RCREG; _QueuePut( g_qSerialRx, byRx ); // doesn't handle overflow, so app had better have big buffer if ( OERR ) { // overrun error (lost data) CREN = 0; CREN = 1; } RCIF = 0; } // RC6/TX if ( TXIF ) { // ready to xmit byTx = _QueueCount( g_qSerialTx ); if ( byTx ) { // output from queue _QueueGet( g_qSerialTx, TXREG ); byTx--; } if ( byTx == 0 ) { // nothing left, disable TXIE = 0; } TXIF = 0; } }