예제 #1
0
/**
 * Get the next byte in the RX buffer. Before calling this function, the
 * i2c1IsGetReady() function should be called to check if there is any
 * data available.
 *
 * @return Returns next byte in receive buffer.
 */
BYTE i2c1BusGetByte(void) {
    BYTE c;

    if (busRxBufHasData(BUSID1)) {
        c = busPeekByteRxBuf(BUSID1);
        busRemoveByteRxBuf(BUSID1);
    }
    
    return c;
}
예제 #2
0
/**
 * Get the next byte in the RX buffer. Before calling this function, the
 * serIsGetReady() function should be called to check if there is any
 * data available on the serial port.
 *
 * @preCondition serIsGetReady() must be called before calling this function to
 * confirm the receive buffer has data! 
 *
 * @return Returns byte received via the serial port.
 */
BYTE serGetByte(void) {
    BYTE c;

    if (busRxBufHasData(BUSID)) {
        c = busPeekByteRxBuf(BUSID);
        busRemoveByteRxBuf(BUSID);
    }
    
    return c;
}
예제 #3
0
/**
 * Must be called every couple of ms
 *
 */
void busTask(void) {
    BYTE c;
    BYTE bytesRead;
    BYTE busId;
    BYTE linkedMask;

    /////////////////////////////////////////////////
    //UDP 1
    
    //Get bus to link to UDP 1
    busId = appcfgGetc(BUSCFG_UDP1_LINK);

    #if defined(BRD_SBC65EC)    
    //Currently UDP 1 can only be linked to Serial 1, Serial 2 and I2C for SBC65EC
    if ((busId >= BUSID_SER1) && (busId <= BUSID_I2C1) && (udpBus1 != INVALID_UDP_SOCKET)) {
    #else
    //Currently UDP 1 can only be linked to Serial 1 and I2C for SBC68EC
    if ( ((busId == BUSID_SER1) || (busId == BUSID_I2C1)) && (udpBus1 != INVALID_UDP_SOCKET)) {
    #endif
    
        //Is there any data waiting for us on this UDP port?
        if (UDPIsGetReady(udpBus1)) {

            if (busIsEnabled(busId)) {

                //Read bytes
                while (UDPGet(&c)) {

                    //Wait until a byte is transmitted by the interrupt routine and buffer has place again.
                    while (busIsTxBufFull(busId)) {

                        busService(busId);
                        FAST_USER_PROCESS();
                    }
                    //Add byte to TX buffer, and update buffer pointers
                    busPutByteTxBuf(busId, c);
                }

                //Initiate transmitting if not already transmitting
                if (!busIsTxing(busId)) {

                    busService(busId);
                }
            }

            //Discard the socket buffer.
            UDPDiscard();
        }

        //Is there any data to send on UDP port
        if(busRxBufHasData(busId)) {

            if (UDPIsPutReady(udpBus1)) {

                while(busRxBufHasData(busId)) {
                
                    //Read and remove byte from buffer
                    c = busPeekByteRxBuf(busId);
                    busRemoveByteRxBuf(busId);
                    
                    UDPPut(c);
                }
            
                // Now transmit it.
                UDPFlush();
            }
        }
    }


    /////////////////////////////////////////////////
    //UDP 2
    
    //Get bus to link to UDP 2
    busId = appcfgGetc(BUSCFG_UDP2_LINK);

    #if defined(BRD_SBC65EC)    
    //Currently UDP 1 can only be linked to Serial 1, Serial 2 and I2C for SBC65EC
    if ((busId >= BUSID_SER1) && (busId <= BUSID_I2C1) && (udpBus1 != INVALID_UDP_SOCKET)) {
    #else
    //Currently UDP 1 can only be linked to Serial 1 and I2C for SBC68EC
    if ( ((busId == BUSID_SER1) || (busId == BUSID_I2C1)) && (udpBus1 != INVALID_UDP_SOCKET)) {
    #endif
        //Is there any data waiting for us on this UDP port?
        if (UDPIsGetReady(udpBus2)) {

            if (busIsEnabled(busId)) {
                //Read bytes
                while (UDPGet(&c)) {
                    //Wait until a byte is transmitted by the interrupt routine and buffer has place again.
                    while (busIsTxBufFull(busId)) {
                        busService(busId);
                        FAST_USER_PROCESS();
                    }
                    //Add byte to TX buffer, and update buffer pointers
                    busPutByteTxBuf(busId, c);
                }

                //Initiate transmitting if not already transmitting
                if (!busIsTxing(busId)) {
                    busService(busId);
                }
            }

            //Discard the socket buffer.
            UDPDiscard();
        }

        //Is there any data to send on UDP port
        if(busRxBufHasData(busId)) {
            if (UDPIsPutReady(udpBus2)) {

                while(busRxBufHasData(busId)) {
                    //Read and remove byte from buffer
                    c = busPeekByteRxBuf(busId);
                    busRemoveByteRxBuf(busId);
            
                    UDPPut(c);
                }
            
                // Now transmit it.
                UDPFlush();
            }
        }
    }


    /////////////////////////////////////////////////
    //Service all serial buses
    for (busId=0; busId<BUS_COUNT; busId++) {
        busService(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.
 *
 * @param busId The bus ID of the requested bus. Is a BUSID_XXX variable
 *
 */
void busService(BYTE busId) {
    switch(busId) {
        case BUSID_SER1:
            serService();
        break;
        
        #if defined(BRD_SBC65EC)
        case BUSID_SER2:
            ser2Service();
        break;
        #endif
        
        case BUSID_I2C1:
            i2cBusService();
        break;
        
        case BUSID_SPI1:
            NOP();
        break;
    }
}


/**
 * Gets a byte at the given offset from the Transmit Buffer, without removing it.
 * The byte is NOT removed from the buffer, and the buffer pointers are NOT updated! 
 * The byte at the given offset it returned. The offset is how deep the byte is in
 * the buffer. For example, 0 will return first byte in buffer, 5 will return the 6th
 * byte in the buffer.
 *
 * @preCondition Ensure offset parameter is not larger than current number of bytes
 * contained in buffer. Call busGetTxBufCount() to get current number of bytes in buffer.
 *
 * @param busId The bus ID of the requested bus. Is a BUSID_XXX variable
 * @param offset Offset of byte to return. Is a value from 0-n, where n = (busGetTxBufCount() - 1)
 *
 * @return Returns the byte at the given offset in the given bus's Transmit buffer.
 */
BYTE busPeekByteTxBufAt(BYTE busId, BYTE offset) {
	BYTE ofst;

	ofst = busInfo.buf[busId].getTx + offset;

	//Check if offset wrap around
	if (ofst >= busInfo.buf[busId].txBufSize) {
		ofst = ofst - busInfo.buf[busId].txBufSize;
	}

	return *(busInfo.buf[busId].txBuf + ofst);

}


/**
 * Gets a byte at the given offset from the Receive Buffer, without removing it.
 * The byte is NOT removed from the buffer, and the buffer pointers are NOT updated! 
 * The byte at the given offset it returned. The offset is how deep the byte is in
 * the buffer. For example, 0 will return first byte in buffer, 5 will return the 6th
 * byte in the buffer.
 *
 * @preCondition Ensure offset parameter is not larger than current number of bytes
 * contained in buffer. Call busGetRxBufCount() to get current number of bytes in buffer.
 *
 * @param busId The bus ID of the requested bus. Is a BUSID_XXX variable
 * @param offset Offset of byte to return. Is a value from 0-n, where n = (busGetRxBufCount() - 1)
 *
 * @return Returns the byte at the given offset in the given bus's Receive buffer.
 */
BYTE busPeekByteRxBufAt(BYTE busId, BYTE offset) {
	BYTE ofst;

	ofst = busInfo.buf[busId].getRx + offset;

	//Check if offset wrap around
	if (ofst >= busInfo.buf[busId].rxBufSize) {
		ofst = ofst - busInfo.buf[busId].rxBufSize;
	}

	return *(busInfo.buf[busId].rxBuf + ofst);
}