buses.h File Reference

Serial Buses. More...


Data Structures

struct  _BUS_BUFFER
struct  _BUSES_INFO

Defines

#define BUFTYPE   BYTE
#define BUS_COUNT   (BUSID_MAX)
#define BUSBUFFER_SIZE   512ul
#define busDisabled(busId)   (busInfo.stat[busId] &= ~BUSSTAT_ENABLED)
#define busEmptyRxBuf(busId)   {busInfo.buf[busId].getRx = busInfo.buf[busId].putRx = busInfo.buf[busId].rxCount = 0;}
#define busEmptyTxBuf(busId)   {busInfo.buf[busId].getTx = busInfo.buf[busId].putTx = busInfo.buf[busId].txCount = 0;}
#define busEnable(busId)   (busInfo.stat[busId] |= BUSSTAT_ENABLED)
#define busGetStat(busId)   (busInfo.stat[busId])
#define busIsEnabled(busId)   (busInfo.stat[busId] & BUSSTAT_ENABLED)
#define busIsRxBufEmpty(busId)   (busInfo.buf[busId].rxCount == 0)
#define busIsRxBufFull(busId)   (busInfo.buf[busId].rxCount == busInfo.buf[busId].rxBufSize)
#define busIsTxBufEmpty(busId)   (busInfo.buf[busId].txCount == 0)
#define busIsTxBufFull(busId)   (busInfo.buf[busId].txCount == busInfo.buf[busId].txBufSize)
#define busIsTxing(busId)   (busInfo.stat[busId] & BUSSTAT_TXING)
#define busPeekByteRxBuf(busId)   (*(busInfo.buf[busId].rxBuf + busInfo.buf[busId].getRx))
#define busPeekByteTxBuf(busId)   (*(busInfo.buf[busId].txBuf + busInfo.buf[busId].getTx))
#define busPutByteRxBuf(busId, b)   {*(busInfo.buf[busId].rxBuf + busInfo.buf[busId].putRx) = b; /* Write byte to buf */ if((++busInfo.buf[busId].putRx)==busInfo.buf[busId].rxBufSize) {busInfo.buf[busId].putRx = 0;} busInfo.buf[busId].rxCount++; /*One more byte available in buffer*/}
#define busPutByteTxBuf(busId, b)   {*(busInfo.buf[busId].txBuf + busInfo.buf[busId].putTx) = b; /* Write byte to buf */ if((++busInfo.buf[busId].putTx)==busInfo.buf[busId].txBufSize) {busInfo.buf[busId].putTx = 0;} busInfo.buf[busId].txCount++; /*One more byte available in buffer*/}
#define busRemoveByteRxBuf(busId)   { if((++busInfo.buf[busId].getRx)==busInfo.buf[busId].rxBufSize) {busInfo.buf[busId].getRx = 0;} busInfo.buf[busId].rxCount--; /*Update available data left in rx buffer*/}
#define busRemoveByteTxBuf(busId)   { if((++busInfo.buf[busId].getTx)==busInfo.buf[busId].txBufSize) {busInfo.buf[busId].getTx = 0;} busInfo.buf[busId].txCount--; /*Update available data left in tx buffer*/}
#define busRxBufHasData(busId)   (busInfo.buf[busId].rxCount != 0)
#define busRxBufNotFull(busId)   (busInfo.buf[busId].rxCount != busInfo.buf[busId].rxBufSize)
#define BUSSTAT_ENABLED   0x80
#define BUSSTAT_RX_OVERRUN   0x10
#define BUSSTAT_TX_OVERRUN   0x01
#define BUSSTAT_TXING   0x02
#define BUSSTAT_UDP_ENABLE   0x01
#define busStatClearOverrun(busId)   (busInfo.stat[busId] &= (~(BUSSTAT_TX_OVERRUN | BUSSTAT_RX_OVERRUN)) )
#define busTxBufHasData(busId)   (busInfo.buf[busId].txCount != 0)

Typedefs

typedef struct _BUS_BUFFER BUS_BUFFER
typedef struct _BUSES_INFO BUS_INFO

Enumerations

enum  BUSES { BUSID_SER1 = 0, BUSID_I2C1, BUSID_SPI1, BUSID_MAX }

Functions

void busInfoInit (void)
void busInit (void)
void busNetInit (void)
void busService (BYTE busId)
void busTask (void)

Variables

BUS_INFO busInfo


Detailed Description

Serial Buses.

Author:
Modtronix Engineering
Dependencies:
Compiler:
MPLAB C18 v2.10 or higher
HITECH PICC-18 V8.35PL3 or higher

Description

This module contains code for implementing different serial buses.

Configuration

The following defines are used to configure this module, and should be placed in the projdefs.h (or similar) file. For details, see Project Configuration. To configure the module, the required defines should be uncommended, and the rest commented out.
 //*********************************************************************
 //--------------------  Buses Configuration --------------------
 //*********************************************************************
 //The default UDP port to use for the UDP 1 Port.
 //#define DEFAULT_BUS_UDP1PORT  (54125ul)
 //
 //The default UDP port to use for the UDP 2 Port.
 //#define DEFAULT_BUS_UDP2PORT  (54126ul)
 //
 //UDP1 Port
 //#define BUS_UDP1PORT DEFAULT_BUS_UDP1PORT
 //
 //UDP2 Port
 //#define BUS_UDP2PORT DEFAULT_BUS_UDP2PORT
 //
 //Serial Port 1 buffer sizes
 //#define BUS_SER1_TXBUFSIZE   50
 //#define BUS_SER1_RXBUFSIZE   100
 //
 //Serial Port 2 buffer sizes
 //#define BUS_SER2_TXBUFSIZE   20
 //#define BUS_SER2_RXBUFSIZE   20
 //
 //I2C Port 1 buffer sizes
 //#define BUS_I2C1_TXBUFSIZE   100
 //#define BUS_I2C1_RXBUFSIZE   20
 //
 //SPI Port 1 buffer sizes
 //#define BUS_SPI1_TXBUFSIZE   20
 //#define BUS_SPI1_RXBUFSIZE   20
 //

Author:
Modtronix Engineering
Dependencies:
Compiler:
MPLAB C18 v2.10 or higher
HITECH PICC-18 V8.35PL3 or higher

Description

This module contains code for implementing different serial buses.

Configuration

The following defines are used to configure this module, and should be placed in the projdefs.h (or similar) file. For details, see Project Configuration. To configure the module, the required defines should be uncommended, and the rest commented out.
 //*********************************************************************
 //--------------------  Buses Configuration --------------------
 //*********************************************************************
 //Default "UDP Command Port"
 //#define DEFAULT_CMD_UDPPORT (54123)

Define Documentation

#define BUFTYPE   BYTE

#define BUS_COUNT   (BUSID_MAX)

The number of buses

#define BUSBUFFER_SIZE   512ul

#define busDisabled ( busId   )     (busInfo.stat[busId] &= ~BUSSTAT_ENABLED)

Disaable the given bus.

Parameters:
busId The bus ID of the requested bus. Is a BUSID_XXX variable

#define busEmptyRxBuf ( busId   )     {busInfo.buf[busId].getRx = busInfo.buf[busId].putRx = busInfo.buf[busId].rxCount = 0;}

Empty the given buses receive buffer. Any data contained in the receive buffer is lost.

Parameters:
busId The bus whos receive buffer has to be cleared. Is a BUSID_XXX variable

#define busEmptyTxBuf ( busId   )     {busInfo.buf[busId].getTx = busInfo.buf[busId].putTx = busInfo.buf[busId].txCount = 0;}

Empty the given buses transmit buffer. Any data contained in the transmit buffer is lost.

Parameters:
busId The bus whos transmit buffer has to be cleared. Is a BUSID_XXX variable

#define busEnable ( busId   )     (busInfo.stat[busId] |= BUSSTAT_ENABLED)

Enable the given bus.

Parameters:
busId The bus ID of the requested bus. Is a BUSID_XXX variable

#define busGetStat ( busId   )     (busInfo.stat[busId])

Reads a byte from the given bus's receive buffer. Ensure that the bus has data before calling this function! The read byte is removed from the buses receive buffer.

Parameters:
busId The bus ID of the requested bus. Is a BUSID_XXX variable
Returns:
Returns the next byte from the given bus's RX buffer. Get the current status of this module. Use the busStatClearOverrun() function to clear the overrun flags if required. This byte contains the following flags:
  • BUSSTAT_TX_OVERRUN
  • BUSSTAT_RX_OVERRUN
Parameters:
busId The bus ID of the requested bus. Is a BUSID_XXX variable
Returns:
The status of this module. Consists out of SER_STAT_XXX flags

#define busIsEnabled ( busId   )     (busInfo.stat[busId] & BUSSTAT_ENABLED)

Indicates if this bus is enabled.

Parameters:
busId The bus ID of the requested bus. Is a BUSID_XXX variable
Returns:
True if the given bus is enabled, else false.

#define busIsRxBufEmpty ( busId   )     (busInfo.buf[busId].rxCount == 0)

Checks if the given bus's receive buffer is empty - it has no pending data in it's receive buffer.

Parameters:
busId The bus ID of the requested bus. Is a BUSID_XXX variable
Returns:
Returns true if the given bus's RX buffer is empty. Else, returns false.

#define busIsRxBufFull ( busId   )     (busInfo.buf[busId].rxCount == busInfo.buf[busId].rxBufSize)

Checks if the given bus's receive buffer is full - no more space for adding data.

Parameters:
busId The bus ID of the requested bus. Is a BUSID_XXX variable
Returns:
Returns true if the given bus's RX buffer is full. Else, returns false.

#define busIsTxBufEmpty ( busId   )     (busInfo.buf[busId].txCount == 0)

Checks if the given bus's transmit buffer is empty - it has nothing to transmit.

Parameters:
busId The bus ID of the requested bus. Is a BUSID_XXX variable
Returns:
Returns true if the given bus's TX buffer is empty. Else, returns false.

#define busIsTxBufFull ( busId   )     (busInfo.buf[busId].txCount == busInfo.buf[busId].txBufSize)

Checks if the given bus's transmit buffer is full - no more space for adding data.

Parameters:
busId The bus ID of the requested bus. Is a BUSID_XXX variable
Returns:
Returns true if the given bus's TX buffer is full. Else, returns false.

#define busIsTxing ( busId   )     (busInfo.stat[busId] & BUSSTAT_TXING)

Indicates if this bus is currently transmitting - returns the status of the BUSSTAT_TX flag. For interrupt driven buses, this means that a transmit interrupt can occur at any time, and clear this flag.

When checking if the bus is transmitting, it should be remembered that a transmit interrupt can occur at any time, and clear the trasmitting flag. This means that if this function returns true, it could change to false (transmit interrupt occurs) by the time we receive the true reply. To prevent this, this function should be called in a critical section, so that no interrupt can occur while calling it.

When checking if the bus is NOT transmitting, it does not have to be performed in a critical section. Not transmitting, means no transmit interrupt can occur and change this flag.

Parameters:
busId The bus ID of the requested bus. Is a BUSID_XXX variable
Returns:
True if the given bus is currently transmitting, else false.

#define busPeekByteRxBuf ( busId   )     (*(busInfo.buf[busId].rxBuf + busInfo.buf[busId].getRx))

Gets a byte from the RX Buffer, without removing it. The byte is NOT removed from the buffer, and the buffer pointers are NOT updated! To remove the byte, and update the pointers, call the busRemoveByteRxBuf() function.

Pre-Condition:
busIsRxBufEmpty() must have been called to confirm the RX Buffer has data!
Parameters:
busId The bus ID of the requested bus. Is a BUSID_XXX variable
Returns:
Returns the next byte in the given bus's RX buffer.

#define busPeekByteTxBuf ( busId   )     (*(busInfo.buf[busId].txBuf + busInfo.buf[busId].getTx))

Gets a byte from the TX Buffer, without removing it. The byte is NOT removed from the buffer, and the buffer pointers are NOT updated! To remove the byte, and update the pointers, call the busRemoveByteTxBuf() function.

Pre-Condition:
busIsTxBufEmpty() must have been called to confirm the TX Buffer has data!
Parameters:
busId The bus ID of the requested bus. Is a BUSID_XXX variable
Returns:
Returns the next byte in the given bus's TX buffer.

#define busPutByteRxBuf ( busId,
 )     {*(busInfo.buf[busId].rxBuf + busInfo.buf[busId].putRx) = b; /* Write byte to buf */ if((++busInfo.buf[busId].putRx)==busInfo.buf[busId].rxBufSize) {busInfo.buf[busId].putRx = 0;} busInfo.buf[busId].rxCount++; /*One more byte available in buffer*/}

Adds a byte to the RX Buffer, and updates the buffer pointers.

Pre-Condition:
busIsRxBufFull() must have been called to confirm the RX Buffer is not full!
Parameters:
busId The bus ID of the requested bus. Is a BUSID_XXX variable
b Byte to add to the buffer

#define busPutByteTxBuf ( busId,
 )     {*(busInfo.buf[busId].txBuf + busInfo.buf[busId].putTx) = b; /* Write byte to buf */ if((++busInfo.buf[busId].putTx)==busInfo.buf[busId].txBufSize) {busInfo.buf[busId].putTx = 0;} busInfo.buf[busId].txCount++; /*One more byte available in buffer*/}

Adds a byte to the TX Buffer, and updates the buffer pointers.

Pre-Condition:
busIsTxBufFull() must have been called to confirm the TX Buffer is not full!
Parameters:
busId The bus ID of the requested bus. Is a BUSID_XXX variable
b Byte to add to the buffer

#define busRemoveByteRxBuf ( busId   )     { if((++busInfo.buf[busId].getRx)==busInfo.buf[busId].rxBufSize) {busInfo.buf[busId].getRx = 0;} busInfo.buf[busId].rxCount--; /*Update available data left in rx buffer*/}

Gets a byte from the RX Buffer. The buffer pointers are NOT updated! After calling this function, the

Pre-Condition:
busIsRxBufEmpty() must have been called to confirm the RX Buffer has data!
Parameters:
busId The bus ID of the requested bus. Is a BUSID_XXX variable
Returns:
Returns true if the given bus's RX buffer is full. Else, returns false.

#define busRemoveByteTxBuf ( busId   )     { if((++busInfo.buf[busId].getTx)==busInfo.buf[busId].txBufSize) {busInfo.buf[busId].getTx = 0;} busInfo.buf[busId].txCount--; /*Update available data left in tx buffer*/}

Gets a byte from the TX Buffer. The buffer pointers are NOT updated! After calling this function, the

Pre-Condition:
busIsTxBufEmpty() must have been called to confirm the TX Buffer has data!
Parameters:
busId The bus ID of the requested bus. Is a BUSID_XXX variable
Returns:
Returns true if the given bus's TX buffer is full. Else, returns false.

#define busRxBufHasData ( busId   )     (busInfo.buf[busId].rxCount != 0)

Checks if the given bus's receive buffer contains data.

Parameters:
busId The bus ID of the requested bus. Is a BUSID_XXX variable
Returns:
Returns true if the given bus's RX buffer has data.

#define busRxBufNotFull ( busId   )     (busInfo.buf[busId].rxCount != busInfo.buf[busId].rxBufSize)

Checks if the given bus's receive buffer still has space - more space for adding data.

Parameters:
busId The bus ID of the requested bus. Is a BUSID_XXX variable
Returns:
Returns true if the given bus's RX buffer is not full. Else, returns false.

#define BUSSTAT_ENABLED   0x80

The buffer is enabled

#define BUSSTAT_RX_OVERRUN   0x10

The transmit buffer has overrun. Must be cleared by the user with busStatClearOverrun() function.

#define BUSSTAT_TX_OVERRUN   0x01

The transmit buffer has overrun. Must be cleared by the user with busStatClearOverrun() function.

#define BUSSTAT_TXING   0x02

Indicates if this bus is currently transmitting. For interrupt driven buses, this means that a transmit interrupt can occur at any time. Checking this function should be performed in a critical section, so that no interrupt can occur while checking!

#define BUSSTAT_UDP_ENABLE   0x01

#define busStatClearOverrun ( busId   )     (busInfo.stat[busId] &= (~(BUSSTAT_TX_OVERRUN | BUSSTAT_RX_OVERRUN)) )

Get the current status of this module. The following flags are cleared during this function:

  • BUSSTAT_TX_OVERRUN
  • BUSSTAT_RX_OVERRUN

Parameters:
busId The bus ID of the requested bus. Is a BUSID_XXX variable
Returns:
The status of this module. Consists out of SER_STAT_XXX flags

#define busTxBufHasData ( busId   )     (busInfo.buf[busId].txCount != 0)

Checks if the given bus's transmit buffer contains data.

Parameters:
busId The bus ID of the requested bus. Is a BUSID_XXX variable
Returns:
Returns true if the given bus's TX buffer has data.


Typedef Documentation

typedef struct _BUS_BUFFER BUS_BUFFER

Structure containing buffer data for a single bus.

The "txCount" member gives the number of entries currently contained in the buffer. It is also used to indicate when the buffer is emptey: txCount == 0.

To test if the buffer is full, check if txCount == txBufSize

DO NOT use putTx == putTx to test if buffer is empty! This will be true when the buffer is empty AND when it is full!

typedef struct _BUSES_INFO BUS_INFO

Structure containing bus data.


Enumeration Type Documentation

enum BUSES

Enumerator:
BUSID_SER1 
BUSID_I2C1 
BUSID_SPI1 
BUSID_MAX 


Function Documentation

void busInfoInit ( void   ) 

Initializes the busInfo structure with pointers to all buffers.

void busInit ( void   ) 

Initialization code.

void busNetInit ( void   ) 

Initialize bus network components

Pre-Condition:
Do NOT call this function before stackInit() has been called!

void busService ( BYTE  busId  ) 

Service the given bus. If our code has a section where it has to wait for the transmit buffer of a bus to be empties, it should call this function while waiting.

Parameters:
busId The bus ID of the requested bus. Is a BUSID_XXX variable

void busTask ( void   ) 

Must be called every couple of ms


Variable Documentation


Generated on Wed Feb 3 12:45:34 2010 for SBC65EC Web Server by  doxygen 1.5.8