BusPal.h
/*
* Copyright (c) 2013-14, Freescale Semiconductor, Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* o Redistributions of source code must retain the above copyright notice, this list
* of conditions and the following disclaimer.
*
* o Redistributions in binary form must reproduce the above copyright notice, this
* list of conditions and the following disclaimer in the documentation and/or
* other materials provided with the distribution.
*
* o Neither the name of Freescale Semiconductor, Inc. nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _BusPal_h_
#define _BusPal_h_
#include "host_types.h"
namespace blfwk
{
class BusPal
{
public:
enum
{
kResetCount = 20,
kResponseOk = 0x01,
kBulkTransferMax = 4096,
kMaxResponse = kBulkTransferMax
};
enum bus_pal_function_t
{
kBusPalFunction_None,
kBusPalFunction_SPI,
kBusPalFunction_I2C,
kBusPalFunction_CAN,
kBusPalFunction_GPIO_CONFIG,
kBusPalFunction_GPIO_SET,
kBusPalFunction_FPGA_CLOCK_SET
};
enum spi_clock_polarity_t
{
kSpiClockPolarity_ActiveHigh = 0,
kSpiClockPolarity_ActiveLow = 1
};
enum spi_clock_phase_t
{
kSpiClockPhase_FirstEdge =
0,
kSpiClockPhase_SecondEdge =
1
};
enum spi_shift_direction_t
{
kSpiMsbFirst = 0,
kSpiLsbFirst = 1
};
static const uint8_t kBusPalDefaultI2cSlaveAddress = 0x10; // I2C Slave 7-bit address
public:
struct BusPalConfigData
{
bus_pal_function_t function;
uint32_t spiSpeedKHz;
spi_clock_polarity_t spiPolarity;
spi_clock_phase_t spiPhase;
spi_shift_direction_t spiDirection;
uint8_t i2cAddress;
uint32_t i2cSpeedKHz;
uint32_t canSpeed;
uint32_t canTxid;
uint32_t canRxid;
uint8_t gpioPort;
uint8_t gpioPinNum;
uint8_t gpioLevel;
uint8_t gpioMux;
uint32_t fpgaClockMhz;
BusPalConfigData(bus_pal_function_t bus = BusPal::kBusPalFunction_None)
: function(bus)
{
spiSpeedKHz = 100;
spiPolarity = BusPal::kSpiClockPolarity_ActiveLow;
spiPhase = BusPal::kSpiClockPhase_SecondEdge;
spiDirection = BusPal::kSpiMsbFirst;
i2cAddress = BusPal::kBusPalDefaultI2cSlaveAddress;
i2cSpeedKHz = 100;
canSpeed = 4; // 4: 1M
canTxid = 0x321;
canRxid = 0x123;
gpioPort = 0;
gpioPinNum = 0;
gpioLevel = 0;
gpioMux = 0;
fpgaClockMhz = 0;
}
};
BusPal(int fileDescriptor);
virtual ~BusPal(){};
static bool parse(const string_vector_t &params, BusPal::BusPalConfigData &config);
virtual bool reset();
virtual bool resetHardware();
virtual bool enterBitBangMode();
virtual bool enterSpiMode();
virtual bool enterCanMode();
virtual bool enterI2cMode();
virtual bool enterUartMode() { return false; }
virtual bool enter1WireMode() { return false; }
virtual bool enterRawWireMode() { return false; }
virtual bool rawConfigurePins(uint8_t port, uint8_t pin, uint8_t muxVal);
virtual bool rawSetPins(uint8_t port, uint8_t pin, uint8_t level);
virtual bool configurePins(uint8_t config = 0) { return true; }
virtual uint8_t readPins() { return 0; }
virtual bool setFPGAClock(uint32_t clock);
virtual bool setSpiSpeed(unsigned int speed);
virtual bool setSpiConfig(spi_clock_polarity_t polarity, spi_clock_phase_t phase, spi_shift_direction_t direction);
virtual bool setI2cAddress(uint8_t address);
virtual bool setI2cSpeed(uint32_t speed);
virtual bool setCanSpeed(unsigned int speed);
virtual bool setCanTxid(unsigned int txid);
virtual bool setCanRxid(unsigned int rxid);
virtual uint8_t *response(size_t byteCount = 1);
virtual bool write(const uint8_t *data, size_t byteCount = 1);
virtual int read(uint8_t *data, size_t byteCount);
protected:
virtual bool writeCommand(uint8_t commandByte, const char *expectedResponse);
virtual uint8_t writeCommand(uint8_t commandByte);
virtual uint8_t writeCommand(uint8_t *command, unsigned int length);
virtual bool writeSpi(const uint8_t *data, size_t byteCount = 1);
virtual bool writeI2c(const uint8_t *data, size_t byteCount = 1);
virtual bool writeCan(const uint8_t *data, size_t byteCount = 1);
virtual int readSpi(uint8_t *data, size_t byteCount);
virtual int readCan(uint8_t *data, size_t byteCount);
int readSpiActual(uint8_t *data, size_t byteCount);
virtual int readI2c(uint8_t *data, size_t byteCount);
int buspal_serial_read(uint8_t *buf, int size, bool isCommandData = false);
int buspal_serial_write(uint8_t *buf, int size, bool isCommandData = false);
void flushRX();
protected:
enum bus_pal_mode_t
{
kBusPalModeBitBang,
kBusPalModeSpi,
kBusPalModeI2c,
kBusPalModeCan
};
int m_fileDescriptor;
uint8_t m_responseBuffer[kMaxResponse];
bus_pal_mode_t m_mode;
unsigned int m_spiWriteByteCount;
unsigned int m_canWriteByteCount;
unsigned int m_canFirstReadDelay;
};
} // namespace blfwk
#endif // _BusPal_h_
// EOF
blfwk::BusPal::setCanTxid
virtual bool setCanTxid(unsigned int txid)
Set CAN txid.
Definition: BusPal.cpp:439
blfwk::BusPal::spi_shift_direction_t
spi_shift_direction_t
SPI data shifter direction options.
Definition: BusPal.h:90
blfwk::BusPal::enterBitBangMode
virtual bool enterBitBangMode()
Enter bit bang (binary scripting) mode.
Definition: BusPal.cpp:499
blfwk::BusPal
Interface with the BusPal.
Definition: BusPal.h:48
blfwk::BusPal::m_responseBuffer
uint8_t m_responseBuffer[kMaxResponse]
Command response buffer.
Definition: BusPal.h:269
blfwk
Definition: BlfwkErrors.h:16
blfwk::BusPal::writeCommand
virtual bool writeCommand(uint8_t commandByte, const char *expectedResponse)
Write command, check string response.
Definition: BusPal.cpp:522
blfwk::BusPal::enterRawWireMode
virtual bool enterRawWireMode()
Enter raw wire mode. Not currently supported for bus pal.
Definition: BusPal.h:173
blfwk::BusPal::kSpiClockPolarity_ActiveLow
Active-low SPI clock (idles high).
Definition: BusPal.h:77
blfwk::BusPal::kResponseOk
Successful command response.
Definition: BusPal.h:55
blfwk::BusPal::setI2cSpeed
virtual bool setI2cSpeed(uint32_t speed)
Set I2c speed.
Definition: BusPal.cpp:694
blfwk::BusPal::setSpiSpeed
virtual bool setSpiSpeed(unsigned int speed)
Set SPI speed.
Definition: BusPal.cpp:662
blfwk::BusPal::writeI2c
virtual bool writeI2c(const uint8_t *data, size_t byteCount=1)
write via I2c
Definition: BusPal.cpp:1014
blfwk::BusPal::buspal_serial_write
int buspal_serial_write(uint8_t *buf, int size, bool isCommandData=false)
Overriden serial_write for logging.
Definition: BusPal.cpp:1148
blfwk::BusPal::readI2c
virtual int readI2c(uint8_t *data, size_t byteCount)
read via I2c
Definition: BusPal.cpp:1053
blfwk::BusPal::write
virtual bool write(const uint8_t *data, size_t byteCount=1)
writes the data over the previously selected interface
Definition: BusPal.cpp:757
blfwk::BusPal::kSpiLsbFirst
Data transfers start with least significant bit.
Definition: BusPal.h:93
blfwk::BusPal::kMaxResponse
Definition: BusPal.h:57
blfwk::BusPal::kSpiClockPhase_FirstEdge
First edge on SPSCK occurs at the middle of the first cycle of a data transfer.
Definition: BusPal.h:83
blfwk::BusPal::m_fileDescriptor
int m_fileDescriptor
PC COM port file descriptor.
Definition: BusPal.h:268
blfwk::BusPal::BusPalConfigData
BusPal configuration data.
Definition: BusPal.h:101
blfwk::BusPal::rawConfigurePins
virtual bool rawConfigurePins(uint8_t port, uint8_t pin, uint8_t muxVal)
Raw configure pins.
Definition: BusPal.cpp:709
blfwk::BusPal::readSpiActual
int readSpiActual(uint8_t *data, size_t byteCount)
worker function to actually read SPI data
Definition: BusPal.cpp:856
blfwk::BusPal::enterCanMode
virtual bool enterCanMode()
Enter Can mode.
Definition: BusPal.cpp:426
blfwk::BusPal::setCanRxid
virtual bool setCanRxid(unsigned int rxid)
Set CAN rxid.
Definition: BusPal.cpp:455
blfwk::BusPal::bus_pal_function_t
bus_pal_function_t
BusPal Transports.
Definition: BusPal.h:62
blfwk::BusPal::setFPGAClock
virtual bool setFPGAClock(uint32_t clock)
Sets the FPGA clock, clock is in hertz.
Definition: BusPal.cpp:741
blfwk::BusPal::readSpi
virtual int readSpi(uint8_t *data, size_t byteCount)
read via Spi
Definition: BusPal.cpp:850
blfwk::BusPal::kResetCount
Max number of nulls to send to enter BBIO mode.
Definition: BusPal.h:54
blfwk::BusPal::readPins
virtual uint8_t readPins()
Read pins.
Definition: BusPal.h:183
blfwk::BusPal::enterI2cMode
virtual bool enterI2cMode()
Enter I2c mode.
Definition: BusPal.cpp:486
blfwk::BusPal::enter1WireMode
virtual bool enter1WireMode()
Enter 1wire mode. Not currently supported for bus pal.
Definition: BusPal.h:171
blfwk::BusPal::enterSpiMode
virtual bool enterSpiMode()
Enter Spi mode.
Definition: BusPal.cpp:414
blfwk::BusPal::~BusPal
virtual ~BusPal()
Destructor.
Definition: BusPal.h:143
blfwk::BusPal::flushRX
void flushRX()
Flushes the serial port of any RX data.
Definition: BusPal.cpp:258
blfwk::BusPal::buspal_serial_read
int buspal_serial_read(uint8_t *buf, int size, bool isCommandData=false)
Overriden serial_read for logging.
Definition: BusPal.cpp:1103
blfwk::BusPal::rawSetPins
virtual bool rawSetPins(uint8_t port, uint8_t pin, uint8_t level)
Set GPIO pin.
Definition: BusPal.cpp:725
blfwk::BusPal::parse
static bool parse(const string_vector_t &params, BusPal::BusPalConfigData &config)
parse the passed in options into the config structure.
Definition: BusPal.cpp:283
blfwk::BusPal::spi_clock_polarity_t
spi_clock_polarity_t
SPI clock polarity configuration.
Definition: BusPal.h:74
blfwk::BusPal::configurePins
virtual bool configurePins(uint8_t config=0)
Configure pins.
Definition: BusPal.h:181
blfwk::BusPal::spi_clock_phase_t
spi_clock_phase_t
SPI clock phase configuration.
Definition: BusPal.h:81
blfwk::BusPal::reset
virtual bool reset()
Reset to bit bang mode from another peripheral mode.
Definition: BusPal.cpp:616
blfwk::BusPal::readCan
virtual int readCan(uint8_t *data, size_t byteCount)
read via Can
Definition: BusPal.cpp:953
blfwk::BusPal::read
virtual int read(uint8_t *data, size_t byteCount)
Read data.
Definition: BusPal.cpp:773
blfwk::BusPal::setSpiConfig
virtual bool setSpiConfig(spi_clock_polarity_t polarity, spi_clock_phase_t phase, spi_shift_direction_t direction)
Sets the SPI configuration.
Definition: BusPal.cpp:644
blfwk::BusPal::kBulkTransferMax
Max number of bytes per bulk transfer.
Definition: BusPal.h:56
blfwk::BusPal::writeCan
virtual bool writeCan(const uint8_t *data, size_t byteCount=1)
write via Can
Definition: BusPal.cpp:907
blfwk::BusPal::writeSpi
virtual bool writeSpi(const uint8_t *data, size_t byteCount=1)
write via Spi
Definition: BusPal.cpp:808
blfwk::BusPal::kSpiMsbFirst
Data transfers start with most significant bit.
Definition: BusPal.h:92
blfwk::BusPal::kSpiClockPhase_SecondEdge
First edge on SPSCK occurs at the start of the first cycle of a data transfer.
Definition: BusPal.h:85
blfwk::BusPal::resetHardware
virtual bool resetHardware()
Reset the bus pal, comes up in terminal mode.
Definition: BusPal.cpp:604
blfwk::BusPal::response
virtual uint8_t * response(size_t byteCount=1)
Read response.
Definition: BusPal.cpp:581
blfwk::BusPal::enterUartMode
virtual bool enterUartMode()
Enter Uart mode. Not currently supported for bus pal.
Definition: BusPal.h:169
blfwk::BusPal::kSpiClockPolarity_ActiveHigh
Active-high SPI clock (idles low).
Definition: BusPal.h:76
blfwk::BusPal::setCanSpeed
virtual bool setCanSpeed(unsigned int speed)
Set CAN speed.
Definition: BusPal.cpp:471
blfwk::BusPal::setI2cAddress
virtual bool setI2cAddress(uint8_t address)
Set I2c address.
Definition: BusPal.cpp:678
blfwk::BusPal::BusPal
BusPal(int fileDescriptor)
Constructor.
Definition: BusPal.cpp:275
blfwk::BusPal::kBusPalDefaultI2cSlaveAddress
static const uint8_t kBusPalDefaultI2cSlaveAddress
I2C default address.
Definition: BusPal.h:97