137 lines
4.1 KiB
C
137 lines
4.1 KiB
C
/*****************************************************************************
|
|
* Copyright (C) 2008
|
|
* ProbeStar Telematics, LLC
|
|
* All Rights Reserved. Proprietary and Confidential.
|
|
*============================================================================
|
|
* PIC Serial I/O Interface
|
|
*----------------------------------------------------------------------------
|
|
*****************************************************************************/
|
|
|
|
/* prevent multiple inclusions */
|
|
#ifndef _SERIAL_H_
|
|
#define _SERIAL_H_
|
|
|
|
/*****************************************************************************
|
|
* includes
|
|
*****************************************************************************/
|
|
|
|
#include "system.h"
|
|
#include "types.h"
|
|
|
|
/*****************************************************************************
|
|
* defines
|
|
*****************************************************************************/
|
|
|
|
#ifndef BAUD
|
|
#define BAUD 57600
|
|
#endif
|
|
|
|
// some of these may not apply w/ -DSERIAL_LOOP
|
|
#define SER_ERR_DEVICE (-1)
|
|
#define SER_ERR_TIMEOUT (-2)
|
|
#define SER_ERR_OVERRUN (-3)
|
|
#define SER_ERR_FRAME (-4)
|
|
#define SER_NO_DATA (-5)
|
|
|
|
/*****************************************************************************
|
|
* macros
|
|
*****************************************************************************/
|
|
|
|
// rate generation
|
|
#define USART_GEN(B) ((((byXtal*1000000L)/(B))/16L)-1)
|
|
#define USART_RATE USART_GEN(BAUD)
|
|
|
|
#define hexchar(c) ((c >= (int)'0' && c <= (int)'9') || \
|
|
(c >= (int)'A' && c <= (int)'F') || (c >= (int)'a' && c <= (int)'f'))
|
|
|
|
#define putnl() puts("\n")
|
|
#define putlf() putch('\f')
|
|
#define putbl() putch(' ')
|
|
#define putline(s) puts(s);putnl()
|
|
|
|
// memory i/o style semantics
|
|
#define SerialReadByte() getch()
|
|
#define SerialWriteByte(B) putch(B)
|
|
#define SerialReadBlock(B,N) serial_get(B,N)
|
|
#define SerialWriteBlock(B,N) serial_put(B,N)
|
|
|
|
/*****************************************************************************
|
|
* structs & typedefs
|
|
*****************************************************************************/
|
|
|
|
/*****************************************************************************
|
|
* global constants
|
|
*****************************************************************************/
|
|
|
|
/*****************************************************************************
|
|
* global variables
|
|
*****************************************************************************/
|
|
|
|
/*****************************************************************************
|
|
* C function prototypes
|
|
*****************************************************************************/
|
|
/* export C functions to C++ */
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
extern void SerialIdle( void ); // user supplied serial idle routine
|
|
extern void SerialInit( void );
|
|
extern void SerialBaud( DWORD dwBaud );
|
|
extern void SerialClear( void );
|
|
extern void SerialAlloc( BYTE *pbyTx, BYTE nTx, BYTE *pbyRx, BYTE nRx );
|
|
|
|
// interrupt
|
|
extern void SerialHandler( void );
|
|
|
|
extern BYTE getchavail( void );
|
|
extern void getchflush( void );
|
|
extern BYTE putchempty( void );
|
|
extern void putchflush( void );
|
|
|
|
extern void putch( char c );
|
|
|
|
extern BYTE gethook( void ); // nonzero if cable plugged in
|
|
|
|
#ifndef _STDIO_H_
|
|
extern int getch( void ); // <0 error
|
|
#endif
|
|
extern int getchtimeout( UINT * t ); // <0 error
|
|
|
|
// primitive data i/o
|
|
#define putint(I) putword(I)
|
|
extern void putword( WORD i );
|
|
|
|
#define putlint(L) putdword(L)
|
|
extern void putdword( DWORD i );
|
|
|
|
#define getint() getword()
|
|
extern WORD getword( void );
|
|
|
|
#define getlint() getdword()
|
|
extern DWORD getdword( void );
|
|
|
|
// string i/o
|
|
extern int puts( const char *s );
|
|
extern int serial_put( const BYTE *s, WORD n );
|
|
extern void puthex( DWORD l, BYTE s );
|
|
extern void putbx( BYTE n );
|
|
extern void putwx( WORD b );
|
|
extern void putdwx( DWORD n );
|
|
extern void putun( DWORD n );
|
|
extern void putn( long n );
|
|
|
|
extern int serial_get( const BYTE *s, WORD n );
|
|
extern DWORD getx( void );
|
|
extern DWORD getun( void );
|
|
extern DWORD getuntimeout( UINT * t );
|
|
extern DWORD getunn( void ); // no echo
|
|
extern DWORD getunntimeout( UINT * t ); // no echo
|
|
|
|
extern void hexdump( DWORD dwAddr, const BYTE * pbyData, WORD nBytes );
|
|
|
|
#ifdef __cpluscplus
|
|
}
|
|
#endif
|
|
#endif
|