Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
nschurando committed Jun 25, 2023
0 parents commit 5d228df
Show file tree
Hide file tree
Showing 9 changed files with 361 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Standard: Cpp11
BasedOnStyle: Google
IndentWidth: 4
ColumnLimit: 0
KeepEmptyLinesAtTheStartOfBlocks: true
AllowShortIfStatementsOnASingleLine: Always
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
patreon: sitronlabs
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Sitron Labs

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Binary file added doc/STMicroelectronics M24C64 Datasheet.pdf
Binary file not shown.
Empty file added keywords.txt
Empty file.
15 changes: 15 additions & 0 deletions library.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "Sitron_Labs_EEPROM_Arduino_Library",
"version": "0.1.0",
"description": "Arduino library supporting various EEPROMs (Electrically-Erasable Programmable Read-Only Memory).",
"keywords": "sitron labs,sitron,labs,arduino,library,storage,i2c,spi,eeprom,external,memory,stream",
"repository": {
"type": "git",
"url": "https:\/\/github.com\/sitronlabs\/SitronLabs_EEPROM_Arduino_Library.git"
},
"frameworks": "arduino",
"platforms": "*",
"build": {
"includeDir": "src"
}
}
9 changes: 9 additions & 0 deletions library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name=Sitron Labs EEPROM Arduino Library
version=0.1.0
author=Sitron Labs
maintainer=Sitron Labs <support@sitronlabs.com>
sentence=Arduino library supporting various EEPROMs (Electrically-Erasable Programmable Read-Only Memory).
paragraph=
category=Data Storage
url=https://github.com/sitronlabs/SitronLabs_EEPROM_Arduino_Library
architectures=*
262 changes: 262 additions & 0 deletions src/m24c64.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
/* Self header */
#include "m24c64.h"

/* Determine length of i2c transactions supported by the platform */
#if defined(WIRE_BUFFER_SIZE)
#define PAGE_WRITE_SUPPORTED 1
#define I2C_BUFFER_SIZE WIRE_BUFFER_SIZE
#else
#warning "Platform doesn't support page write"
#define PAGE_WRITE_SUPPORTED 0
#endif

/**
* Configures the driver with access over I2C.
* @note Call this from the Arduino setup function.
* @note Make sure the I2C library has been initialized with a call to its begin function for example.
* @param[in] i2c_library A reference to the i2c library to use.
* @param[in] i2c_address The i2c address of the device.
* @return 0 in case of success, or a negative error code otherwise.
*/
int m24c64::setup(TwoWire& i2c_library, const uint8_t i2c_address) {

/* Ensure i2c address is within valid range */
if ((i2c_address & 0xF8) != 0x50) {
return -EINVAL;
}

/* Enable i2c */
m_i2c_library = &i2c_library;
m_i2c_address = i2c_address;

/* Return success */
return 0;
}

/**
* Tries to detect the device.
* @return true if the device has been detected, or false otherwise.
*/
bool m24c64::detect(void) {
if (m_i2c_library != NULL) {
m_i2c_library->beginTransmission(m_i2c_address);
if (m_i2c_library->endTransmission() == 0) {
return true;
}
}
return false;
}

/**
*
* @param[in] address
* @param[out] data
* @param[in] length
* @return The number of bytes read in case of success, or a negative error code otherwise.
*/
int m24c64::read(const uint16_t address, uint8_t* const data, const size_t length) {
int res;

/* Ensure setup has been performed */
if (m_i2c_library == NULL) {
return -EINVAL;
}

/* Ensure start address is valid and prevent stop address from creatign a rollover */
if (address >= m_size_total) {
return -EINVAL;
}
size_t length_capped = (address + length > m_size_total) ? m_size_total - address : length;

/* Wait a little bit if a write has just been performed
* @todo Improve by implementing polling on ack */
while (millis() - m_timestamp_write <= 5) {
}

/* Read bytes */
for (size_t i = 0; i < length_capped;) {
m_i2c_library->beginTransmission(m_i2c_address);
m_i2c_library->write((uint8_t)((address + i) >> 8));
m_i2c_library->write((uint8_t)((address + i) >> 0));
if (m_i2c_library->endTransmission(false) != 0) return -EIO;
res = m_i2c_library->requestFrom(m_i2c_address, length_capped - i);
if (res <= 0) {
return i;
} else {
for (size_t j = 0; j < res; j++) {
data[i + j] = m_i2c_library->read();
}
i += res;
}
}

/* Return number of bytes read */
return length_capped;
}

/**
*
* @param[in] address
* @param[in] data
* @param[in] length
* @return The number of bytes written in case of success, or a negative error code otherwise.
*/
int m24c64::write(const uint16_t address, const uint8_t* const data, const size_t length) {

/* Ensure setup has been performed */
if (m_i2c_library == NULL) {
return -EINVAL;
}

/* Ensure start address is valid and prevent stop address from creating a rollover */
if (address >= m_size_total) {
return -EINVAL;
}
size_t length_capped = (address + length > m_size_total) ? m_size_total - address : length;

/* Write bytes */
for (size_t i = 0; i < length_capped;) {

/* Wait a little bit if a write has just been performed
* @todo Improve by implementing polling on ack */
while (millis() - m_timestamp_write <= 5) {
}

#if PAGE_WRITE_SUPPORTED
/* Page write when possible */
if ((address + i) % m_size_page == 0 && length_capped - i >= m_size_page && I2C_BUFFER_SIZE >= m_size_page + 2) {
m_i2c_library->beginTransmission(m_i2c_address);
m_i2c_library->write((uint8_t)((address + i) >> 8));
m_i2c_library->write((uint8_t)((address + i) >> 0));
i += m_i2c_library->write(&data[i], m_size_page);
if (m_i2c_library->endTransmission(true) != 0) return -EIO;
m_timestamp_write = millis();
}

/* Byte write otherwise */
else {
m_i2c_library->beginTransmission(m_i2c_address);
m_i2c_library->write((uint8_t)((address + i) >> 8));
m_i2c_library->write((uint8_t)((address + i) >> 0));
i += m_i2c_library->write(data[i]);
if (m_i2c_library->endTransmission(true) != 0) return -EIO;
m_timestamp_write = millis();
}
#else
/* Byte write */
m_i2c_library->beginTransmission(m_i2c_address);
m_i2c_library->write((uint8_t)((address + i) >> 8));
m_i2c_library->write((uint8_t)((address + i) >> 0));
i += m_i2c_library->write(data[i]);
if (m_i2c_library->endTransmission(true) != 0) return -EIO;
m_timestamp_write = millis();
#endif
}

/* Return number of bytes written */
return length_capped;
}

/**
* Gets the number of bytes available in the stream. This is only for bytes that have already arrived.
* @note Inherited from the stream interface
* @see https://www.arduino.cc/reference/en/language/functions/communication/stream/streamavailable/
*/
int m24c64::available() {
if (m_index_read <= m_size_total) {
return m_size_total - m_index_read;
} else {
return 0;
}
}

/**
* Reads characters from an incoming stream to the buffer.
* @note Inherited from the stream interface
* @see https://www.arduino.cc/reference/en/language/functions/communication/stream/streamread
*/

int m24c64::read() {
uint8_t data;
int res = read(m_index_read, &data, 1);
if (res < 0) {
return 0;
} else {
m_index_read += res;
return data;
}
}

/**
* Read a byte from the file without advancing to the next one. That is, successive calls to peek() will return the same value, as will the next call to read().
* @note Inherited from the stream interface
* @see https://www.arduino.cc/reference/en/language/functions/communication/stream/streampeek/
*/
int m24c64::peek() {
uint8_t data;
int res = read(m_index_read, &data, 1);
if (res < 0) {
return 0;
} else {
return data;
}
}

/**
*
* @param[in] data
* @note Inherited from the print interface
*/
size_t m24c64::write(uint8_t data) {
int res = write(m_index_write, &data, 1);
if (res < 0) {
return 0;
} else {
m_index_write += res;
return res;
}
}

/**
*
* @param[in] data
* @param[in] length
* @note Inherited from the print interface
*/
size_t m24c64::write(const uint8_t* data, size_t length) {
int res = write(m_index_write, data, length);
if (res < 0) {
return 0;
} else {
m_index_write += res;
return res;
}
}

/**
* @brief
* @param index
* @return
*/
size_t m24c64::seek_read(size_t index) {
if (index < m_size_total) {
m_index_read = index;
return index;
} else {
return ((size_t)-1);
}
}

/**
* @brief
* @param index
* @return
*/
size_t m24c64::seek_write(size_t index) {
if (index < m_size_total) {
m_index_write = index;
return index;
} else {
return ((size_t)-1);
}
}
47 changes: 47 additions & 0 deletions src/m24c64.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#ifndef M24C64_H
#define M24C64_H

/* Arduino libraries */
#include <Arduino.h>
#include <Stream.h>
#include <Wire.h>

/* C/C++ libraries */
#include <errno.h>
#include <stdint.h>

/**
*
*/
class m24c64 : public Stream {

public:
int setup(TwoWire& i2c_library, const uint8_t i2c_address);
bool detect(void);
int read(const uint16_t address, uint8_t* const data, const size_t length);
int write(const uint16_t address, const uint8_t* const data, const size_t length);

/* Inherited from the stream interface */
int available();
int read();
int peek();

/* Inherited from the print interface */
size_t write(uint8_t data);
size_t write(const uint8_t* data, size_t length);

/* Seek for stream and print interfaces */
size_t seek_read(size_t index);
size_t seek_write(size_t index);

protected:
TwoWire* m_i2c_library = NULL;
uint8_t m_i2c_address;
size_t m_index_write = 0;
size_t m_index_read = 0;
const size_t m_size_total = 8192; // 64 Kbit (8 Kbyte)
const size_t m_size_page = 32; // 32 byte
uint32_t m_timestamp_write = 0;
};

#endif

0 comments on commit 5d228df

Please sign in to comment.