Fixed ISerialBus interface and added device and register address fields so that future i2c and SPI devices can be addressed, to. Added i2c HAL. Tested, working.
The update on the interface required FunctionStatus and the logger to be updated, too
This commit is contained in:
@@ -45,6 +45,8 @@
|
||||
// Constant and macro definitions
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
#define NO_DEVICE_ADDRESS (255)
|
||||
#define NO_REGISTER_ADDRESS (255)
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// Type definitions.
|
||||
@@ -70,9 +72,9 @@ class ISerialBus
|
||||
|
||||
FunctionStatus close() {status = CLOSED; return FUNCTION_STATUS_OK;}
|
||||
|
||||
virtual FunctionStatus read(T* buffer, uint32_t length, uint32_t* actualLength) = 0;
|
||||
virtual FunctionStatus read(T deviceAddress, T registerAddress, T* buffer, uint32_t length, uint32_t* actualLength) = 0;
|
||||
|
||||
virtual FunctionStatus write(T* buffer, uint32_t length) = 0;
|
||||
virtual FunctionStatus write(T deviceAddress, T registerAddress, T* buffer, uint32_t length) = 0;
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
/// \file i2c.h
|
||||
/// \brief File description
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
//
|
||||
// vbchaos software design
|
||||
//
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
/// $Revision: $
|
||||
/// $Author: $
|
||||
/// $Date: $
|
||||
// (c) 2023 vbchaos
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
#ifndef MAIN_HAL_INC_I2C_H_
|
||||
#define MAIN_HAL_INC_I2C_H_
|
||||
|
||||
/**
|
||||
* i2c implementation
|
||||
* \defgroup i2c
|
||||
* \brief {group_description}
|
||||
* \addtogroup {Layer}
|
||||
*
|
||||
* Detailed description
|
||||
* @{
|
||||
*/
|
||||
|
||||
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// Include files
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
// CompilerIncludes
|
||||
// All include files that are provided by the compiler directly
|
||||
|
||||
|
||||
|
||||
// ProjectIncludes
|
||||
// All include files that are provided by the project
|
||||
#include "ISerialBus.h"
|
||||
|
||||
#include "driver/i2c.h"
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// Constant and macro definitions
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// Type definitions.
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// Function declarations
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
class i2c : public ISerialBus<uint8_t>
|
||||
{
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
// Public Section
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
public:
|
||||
// Class Constructor
|
||||
i2c(i2c_port_t* port);
|
||||
~i2c();
|
||||
|
||||
FunctionStatus read(uint8_t deviceAddress, uint8_t registerAddress, uint8_t* buffer, uint32_t length, uint32_t* actualLength);
|
||||
|
||||
FunctionStatus write(uint8_t deviceAddress, uint8_t registerAddress, uint8_t* buffer, uint32_t length);
|
||||
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
// Protected Section
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
protected:
|
||||
|
||||
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
// Private Section
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
private:
|
||||
|
||||
void intWrite(uint8_t slaveAddress, uint8_t registerAddress, uint8_t* data, uint8_t length);
|
||||
void intRead(uint8_t slaveAddress, uint8_t registerAddress, uint8_t* data, uint8_t length);
|
||||
|
||||
i2c_port_t* port;
|
||||
|
||||
};
|
||||
|
||||
/** @} */
|
||||
|
||||
|
||||
#endif /* MAIN_HAL_INC_I2C_H_ */
|
||||
@@ -67,9 +67,9 @@ class uart : public ISerialBus<uint8_t>
|
||||
uart(uart_port_t* uartPort);
|
||||
~uart();
|
||||
|
||||
FunctionStatus read(uint8_t* buffer, uint32_t length, uint32_t* actualLength);
|
||||
FunctionStatus read(uint8_t deviceAddress, uint8_t registerAddress, uint8_t* buffer, uint32_t length, uint32_t* actualLength);
|
||||
|
||||
FunctionStatus write(uint8_t* buffer, uint32_t length);
|
||||
FunctionStatus write(uint8_t deviceAddress, uint8_t registerAddress, uint8_t* buffer, uint32_t length);
|
||||
|
||||
// -----------------------------------------------------------------------------------------------------------------
|
||||
// Protected Section
|
||||
|
||||
@@ -0,0 +1,113 @@
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
/// \file i2c.cpp
|
||||
/// \brief Description
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
//
|
||||
// vbchaos software design
|
||||
//
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
/// $Revision: $
|
||||
/// $Author: $
|
||||
/// $Date: $
|
||||
// (c) 2023 vbchaos
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// Include files
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
#include <i2c.h>
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// Constant and macro definitions
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
#define I2C_MASTER_TIMEOUT_MS 1000
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// Type definitions
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// File-scope variables
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// Function declarations
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
// Function definitions
|
||||
// --------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
i2c::i2c(i2c_port_t* port) : port {port}
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
i2c::~i2c()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
FunctionStatus i2c::write(uint8_t deviceAddress, uint8_t registerAddress, uint8_t* buffer, uint32_t length)
|
||||
{
|
||||
FunctionStatus returnValue = FUNCTION_STATUS_OK;
|
||||
|
||||
if (status == OPEN)
|
||||
{
|
||||
intWrite(deviceAddress, registerAddress, buffer, length);
|
||||
}
|
||||
else
|
||||
{
|
||||
returnValue = FUNCTION_STATUS_NOT_OPEN;
|
||||
}
|
||||
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
|
||||
FunctionStatus i2c::read(uint8_t deviceAddress, uint8_t registerAddress, uint8_t* buffer, uint32_t length, uint32_t* actualLength)
|
||||
{
|
||||
FunctionStatus returnValue = FUNCTION_STATUS_OK;
|
||||
|
||||
if (status == OPEN)
|
||||
{
|
||||
intRead(deviceAddress, registerAddress, buffer, length);
|
||||
*actualLength = length;
|
||||
}
|
||||
else
|
||||
{
|
||||
returnValue = FUNCTION_STATUS_NOT_OPEN;
|
||||
}
|
||||
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
|
||||
void i2c::intWrite(uint8_t slaveAddress, uint8_t registerAddress, uint8_t* data, uint8_t length)
|
||||
{
|
||||
uint8_t buffer[length + 1];
|
||||
|
||||
buffer[0] = registerAddress;
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
buffer[i+1] = data[i];
|
||||
}
|
||||
|
||||
ESP_ERROR_CHECK(i2c_master_write_to_device(*port, slaveAddress, buffer, sizeof(buffer), I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS));
|
||||
}
|
||||
|
||||
|
||||
void i2c::intRead(uint8_t slaveAddress, uint8_t registerAddress, uint8_t* data, uint8_t length)
|
||||
{
|
||||
ESP_ERROR_CHECK(i2c_master_write_read_device(*port, slaveAddress, ®isterAddress, 1, data, length, I2C_MASTER_TIMEOUT_MS / portTICK_PERIOD_MS));
|
||||
}
|
||||
@@ -57,13 +57,23 @@ uart::~uart()
|
||||
|
||||
}
|
||||
|
||||
FunctionStatus uart::write(uint8_t* buffer, uint32_t length)
|
||||
FunctionStatus uart::write(uint8_t deviceAddress, uint8_t registerAddress, uint8_t* buffer, uint32_t length)
|
||||
{
|
||||
FunctionStatus returnValue = FUNCTION_STATUS_OK;
|
||||
|
||||
if (status == OPEN)
|
||||
{
|
||||
uart_write_bytes(*this->port, (const void*)buffer, length);
|
||||
// For UART devices it is allowed to ignore the device or register address if it is requested as such
|
||||
// The common case is that both addresses are ignorable since UARTs are mostly not used this way, so this
|
||||
// implementation contains a shortcut and will immediatly write the buffer to the bus
|
||||
if ((deviceAddress == NO_DEVICE_ADDRESS) && (registerAddress == NO_REGISTER_ADDRESS))
|
||||
{
|
||||
uart_write_bytes(*this->port, (const void*)buffer, length);
|
||||
}
|
||||
else
|
||||
{
|
||||
returnValue = FUNCTION_STATUS_NOT_IMPLEMENTED;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -73,7 +83,7 @@ FunctionStatus uart::write(uint8_t* buffer, uint32_t length)
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
FunctionStatus uart::read(uint8_t* buffer, uint32_t length, uint32_t* actualLength)
|
||||
FunctionStatus uart::read(uint8_t deviceAddress, uint8_t registerAddress, uint8_t* buffer, uint32_t length, uint32_t* actualLength)
|
||||
{
|
||||
FunctionStatus returnValue = FUNCTION_STATUS_OK;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user