-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New driver for OnSemi CAT24AAxx I2C Eeproms.
- Loading branch information
Mike Wolfram
committed
Jan 20, 2020
1 parent
34bb804
commit b721551
Showing
5 changed files
with
281 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() ); | ||
} |