Skip to content

Commit

Permalink
New driver for OnSemi CAT24AAxx I2C Eeproms.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Wolfram committed Jan 20, 2020
1 parent 34bb804 commit b721551
Show file tree
Hide file tree
Showing 5 changed files with 281 additions and 8 deletions.
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,58 +179,59 @@ can easily configure them for you specific needs.
<td align="center">BME280</td>
<td align="center">BMP085</td>
<td align="center">BNO055</td>
<td align="center">CAT24AA</td>
<td align="center">DRV832X</td>
<td align="center">DS1302</td>
</tr><tr>
<td align="center">DS1302</td>
<td align="center">DS1631</td>
<td align="center">DS18B20</td>
<td align="center">EA-DOG</td>
<td align="center">FT245</td>
<td align="center">FT6X06</td>
<td align="center">HCLAx</td>
</tr><tr>
<td align="center">HCLAx</td>
<td align="center">HD44780</td>
<td align="center">HMC58x</td>
<td align="center">HMC6343</td>
<td align="center">I2C-EEPROM</td>
<td align="center">ITG3200</td>
<td align="center">L3GD20</td>
</tr><tr>
<td align="center">L3GD20</td>
<td align="center">LAWICEL</td>
<td align="center">LIS302DL</td>
<td align="center">LIS3DSH</td>
<td align="center">LM75</td>
<td align="center">LSM303A</td>
<td align="center">LTC2984</td>
</tr><tr>
<td align="center">LTC2984</td>
<td align="center">MAX6966</td>
<td align="center">MAX7219</td>
<td align="center">MCP23X17</td>
<td align="center">MCP2515</td>
<td align="center">NOKIA5110</td>
<td align="center">NRF24</td>
</tr><tr>
<td align="center">NRF24</td>
<td align="center">TFT-DISPLAY</td>
<td align="center">PAT9125EL</td>
<td align="center">PCA8574</td>
<td align="center">PCA9535</td>
<td align="center">PCA9548A</td>
<td align="center">PCA9685</td>
</tr><tr>
<td align="center">PCA9685</td>
<td align="center">SIEMENS-S65</td>
<td align="center">SIEMENS-S75</td>
<td align="center">SK6812</td>
<td align="center">SK9822</td>
<td align="center">SSD1306</td>
<td align="center">TCS3414</td>
</tr><tr>
<td align="center">TCS3414</td>
<td align="center">TCS3472</td>
<td align="center">TLC594X</td>
<td align="center">TMP102</td>
<td align="center">TMP175</td>
<td align="center">VL53L0</td>
<td align="center">VL6180</td>
</tr><tr>
<td align="center">VL6180</td>
<td align="center">WS2812</td>
</tr>
</table>
Expand Down
49 changes: 49 additions & 0 deletions src/modm/driver/storage/cat24aa.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2020, Mike Wolfram
*
* This file is part of the modm project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
// ----------------------------------------------------------------------------

#include "cat24aa.hpp"

modm::cat24Aa::DataTransmissionAdapter::DataTransmissionAdapter(uint8_t address) :
I2cWriteReadTransaction(address), writeAddress(false)
{}

bool
modm::cat24Aa::DataTransmissionAdapter::configureWrite(uint16_t address,
const uint8_t *buffer, std::size_t size)
{
if (I2cWriteReadTransaction::configureWrite(buffer, size))
{
addressBuffer[0] = uint8_t(address & 0xff);
writeAddress = true;
return true;
}
return false;
}

bool
modm::cat24Aa::DataTransmissionAdapter::configureRead(uint16_t address,
uint8_t *buffer, std::size_t size)
{
addressBuffer[0] = uint8_t(address & 0xff);
writeAddress = false;
return I2cWriteReadTransaction::configureWriteRead(addressBuffer, 1, buffer, size);
}

modm::I2cTransaction::Writing
modm::cat24Aa::DataTransmissionAdapter::writing()
{
if (writeAddress)
{
writeAddress = false;
return Writing(addressBuffer, 1, OperationAfterWrite::Write);
}
return I2cWriteReadTransaction::writing();
}
139 changes: 139 additions & 0 deletions src/modm/driver/storage/cat24aa.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
* Copyright (c) 2020, Mike Wolfram
*
* This file is part of the modm project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
// ----------------------------------------------------------------------------

#ifndef MODM_CAT24AA_HPP
#define MODM_CAT24AA_HPP

#include <modm/processing/resumable.hpp>
#include <modm/architecture/interface/i2c_device.hpp>

namespace modm
{

/// @cond
struct cat24Aa {
class DataTransmissionAdapter : public modm::I2cWriteReadTransaction
{
public:
DataTransmissionAdapter(uint8_t address = 0x50);

bool
configureWrite(uint16_t address, const uint8_t *buffer, std::size_t size);

bool
configureRead(uint16_t address, uint8_t *buffer, std::size_t size);

inline static constexpr uint8_t getAddress() {
return 0x50;
}

protected:
virtual Writing
writing() override;

uint8_t addressBuffer[1];
bool writeAddress;
};
};
/// @endcond

/**
* I2C Eeprom CAT24AA
*
* Driver for OnSemi CAT24AA Eeprom.
* I2C eeprom with an 11-bit address pointer, encoded in 3 bits of the device
* slave address plus the address byte.
* Base address is fixed to 0x50.
*
* @ingroup modm_driver_i2c_eeprom
* @author Mike Wolfram
*/
template <typename I2cMaster>
class Cat24Aa : public modm::I2cDevice<I2cMaster, 1, cat24Aa::DataTransmissionAdapter>
{
public:
Cat24Aa();

/**
* Write byte
*
* @param address Address
* @param data Data byte
*
* @return `true` if the data could be written,
* `false` otherwise
*/
inline modm::ResumableResult<bool>
writeByte(uint32_t address, uint8_t data)
{
return write(address, &data, 1);
}

/**
* Write block
*
* @param address Address
* @param data Data block
* @param length Number of bytes to be written
*
* @return `true` if the data could be written,
* `false` otherwise
*/
modm::ResumableResult<bool>
write(uint32_t address, const uint8_t *data, std::size_t length);

/**
* Convenience function
*
* Shortcut for:
* @code
* return write(address, static_cast<const uint8_t *>(&data), sizeof(T));
* @endcode
*/
template <typename T>
inline modm::ResumableResult<bool>
write(uint32_t address, const T& data)
{
return write(address, reinterpret_cast<const uint8_t *>(&data), sizeof(T));
}

/// Read byte
inline modm::ResumableResult<bool>
readByte(uint32_t address, uint8_t &data)
{
return read(address, &data, 1);
}

/// Read block
modm::ResumableResult<bool>
read(uint32_t address, uint8_t *data, std::size_t length);

/**
* Convenience function
*
* Shortcut for:
* @code
* return read(address, static_cast<uint8_t *>(&data), sizeof(T));
* @endcode
*/
template <typename T>
inline modm::ResumableResult<bool>
read(uint16_t address, T& data)
{
return read(address, reinterpret_cast<uint8_t *>(&data), sizeof(T));
}
};

} // namespace modm

#include "cat24aa_impl.hpp"

#endif // MODM_CAT24AA_HPP
32 changes: 32 additions & 0 deletions src/modm/driver/storage/cat24aa.lb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (c) 2020, Mike Wolfram
#
# This file is part of the modm project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# -----------------------------------------------------------------------------


def init(module):
module.name = ":driver:cat24aa"
module.description = """\
# CAT24AAxx I2C Eeprom
Compatible with the OnSemi CAT24AA I2C eeprom (1 kBit to 16 kBit).
It uses a 8-bit address pointer, the top 3 bits of the address pointer
are added to the the slave address, which is fixed to 0x50.
"""

def prepare(module, options):
module.depends(":architecture:i2c.device")
return True

def build(env):
env.outbasepath = "modm/src/modm/driver/storage"
env.copy("cat24aa.hpp")
env.copy("cat24aa.cpp")
env.copy("cat24aa_impl.hpp")
52 changes: 52 additions & 0 deletions src/modm/driver/storage/cat24aa_impl.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2020, Mike Wolfram
*
* This file is part of the modm project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
// ----------------------------------------------------------------------------

#ifndef MODM_CAT24AA_HPP
# error "Don't include this file directly, use 'cat24aa.hpp' instead!"
#endif

// ----------------------------------------------------------------------------
template <typename I2cMaster>
modm::Cat24Aa<I2cMaster>::Cat24Aa() :
I2cDevice<I2cMaster, 1, cat24Aa::DataTransmissionAdapter>(0x50)
{
}

template <typename I2cMaster>
modm::ResumableResult<bool>
modm::Cat24Aa<I2cMaster>::write(uint32_t address, const uint8_t *data,
std::size_t length)
{
RF_BEGIN();

this->setAddress(this->transaction.getAddress() | ((address >> 8) & 0x07));

RF_WAIT_UNTIL( this->transaction.configureWrite(address, data, length) and this->startTransaction() );

RF_WAIT_WHILE( this->isTransactionRunning() );

RF_END_RETURN( this->wasTransactionSuccessful() );
}

template <typename I2cMaster>
modm::ResumableResult<bool>
modm::Cat24Aa<I2cMaster>::read(uint32_t address, uint8_t *data, std::size_t length)
{
RF_BEGIN();

this->setAddress(this->transaction.getAddress() | ((address >> 8) & 0x07));

RF_WAIT_UNTIL( this->transaction.configureRead(address, data, length) and this->startTransaction() );

RF_WAIT_WHILE( this->isTransactionRunning() );

RF_END_RETURN( this->wasTransactionSuccessful() );
}

0 comments on commit b721551

Please sign in to comment.