mirror of https://github.com/ARMmbed/mbed-os.git
Initial commit of the I2C EEPROM device
commit
ec88f7822f
|
|
@ -0,0 +1,138 @@
|
||||||
|
/* Simple access class for I2C EEPROM chips like Microchip 24LC
|
||||||
|
* Copyright (c) 2015 Robin Hourahane
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
#include "I2CEEBlockDevice.h"
|
||||||
|
|
||||||
|
#define I2CEE_TIMEOUT 10000
|
||||||
|
|
||||||
|
|
||||||
|
I2CEEBlockDevice::I2CEEBlockDevice(
|
||||||
|
PinName sda, PinName scl, uint8_t addr,
|
||||||
|
bd_size_t size, bd_size_t block, int freq)
|
||||||
|
: _i2c(sda, scl), _i2c_addr(addr), _size(size), _block(block)
|
||||||
|
{
|
||||||
|
_i2c.frequency(freq);
|
||||||
|
}
|
||||||
|
|
||||||
|
bd_error_t I2CEEBlockDevice::init()
|
||||||
|
{
|
||||||
|
return _sync();
|
||||||
|
}
|
||||||
|
|
||||||
|
bd_error_t I2CEEBlockDevice::deinit()
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bd_error_t I2CEEBlockDevice::read(void *buffer, bd_addr_t addr, bd_size_t size)
|
||||||
|
{
|
||||||
|
// Check the address and size fit onto the chip.
|
||||||
|
if (!is_valid_read(addr, size)) {
|
||||||
|
return BD_ERROR_PARAMETER;
|
||||||
|
}
|
||||||
|
|
||||||
|
_i2c.start();
|
||||||
|
if (!_i2c.write(_i2c_addr | 0) ||
|
||||||
|
!_i2c.write((char)(addr >> 8)) ||
|
||||||
|
!_i2c.write((char)(addr & 0xff))) {
|
||||||
|
return BD_ERROR_DEVICE_ERROR;
|
||||||
|
}
|
||||||
|
_i2c.stop();
|
||||||
|
|
||||||
|
if (_i2c.read(_i2c_addr, static_cast<char*>(buffer), size) < 0) {
|
||||||
|
return BD_ERROR_DEVICE_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bd_error_t I2CEEBlockDevice::program(const void *buffer, bd_addr_t addr, bd_size_t size)
|
||||||
|
{
|
||||||
|
// Check the addr and size fit onto the chip.
|
||||||
|
if (!is_valid_program(addr, size)) {
|
||||||
|
return BD_ERROR_PARAMETER;
|
||||||
|
}
|
||||||
|
|
||||||
|
// While we have some more data to write.
|
||||||
|
while (size > 0) {
|
||||||
|
uint32_t off = addr % _block;
|
||||||
|
uint32_t chunk = (off + size < _block) ? size : (_block - off);
|
||||||
|
|
||||||
|
_i2c.start();
|
||||||
|
if (!_i2c.write(_i2c_addr | 0) ||
|
||||||
|
!_i2c.write((char)(addr >> 8)) ||
|
||||||
|
!_i2c.write((char)(addr & 0xff))) {
|
||||||
|
return BD_ERROR_DEVICE_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (unsigned i = 0; i < chunk; i++) {
|
||||||
|
_i2c.write(static_cast<const char*>(buffer)[i]);
|
||||||
|
}
|
||||||
|
_i2c.stop();
|
||||||
|
|
||||||
|
bd_error_t err = _sync();
|
||||||
|
if (err) {
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
|
addr += chunk;
|
||||||
|
size -= chunk;
|
||||||
|
buffer = static_cast<const char*>(buffer) + chunk;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bd_error_t I2CEEBlockDevice::erase(bd_addr_t addr, bd_size_t size)
|
||||||
|
{
|
||||||
|
// No erase needed
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bd_error_t I2CEEBlockDevice::_sync()
|
||||||
|
{
|
||||||
|
// The chip doesn't ACK while writing to the actual EEPROM
|
||||||
|
// so loop trying to do a zero byte write until it is ACKed
|
||||||
|
// by the chip.
|
||||||
|
for (int i = 0; i < I2CEE_TIMEOUT; i++) {
|
||||||
|
if (_i2c.write(_i2c_addr | 0, 0, 0) < 1) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
wait_ms(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return BD_ERROR_DEVICE_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
|
bd_size_t I2CEEBlockDevice::get_read_size()
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
bd_size_t I2CEEBlockDevice::get_program_size()
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
bd_size_t I2CEEBlockDevice::get_erase_size()
|
||||||
|
{
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
bd_size_t I2CEEBlockDevice::size()
|
||||||
|
{
|
||||||
|
return _size;
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,149 @@
|
||||||
|
/* Simple access class for I2C EEPROM chips like Microchip 24LC
|
||||||
|
* Copyright (c) 2015 Robin Hourahane
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
#ifndef MBED_I2CEEPROM_BLOCK_DEVICE_H
|
||||||
|
#define MBED_I2CEEPROM_BLOCK_DEVICE_H
|
||||||
|
|
||||||
|
/* If the target has no I2C support then I2CEEPROM is not supported */
|
||||||
|
#ifdef DEVICE_I2C
|
||||||
|
|
||||||
|
#include <mbed.h>
|
||||||
|
#include "BlockDevice.h"
|
||||||
|
|
||||||
|
|
||||||
|
/** BlockDevice for I2C based flash device such as
|
||||||
|
* Microchip's 24LC or ATMEL's AT24C ranges
|
||||||
|
*
|
||||||
|
* @code
|
||||||
|
* #include "mbed.h"
|
||||||
|
* #include "I2CEEBlockDevice.h"
|
||||||
|
*
|
||||||
|
* // Create 24LC device with 32Kbytes of memory
|
||||||
|
* I2CEEBlockDevice flash(D14, D15, 0xa0, 32*1024);
|
||||||
|
*
|
||||||
|
* int main() {
|
||||||
|
* printf("flash test\n");
|
||||||
|
* mx52r.init();
|
||||||
|
* printf("flash size: %llu\n", flash.size());
|
||||||
|
* printf("flash read size: %llu\n", flash.get_read_size());
|
||||||
|
* printf("flash program size: %llu\n", flash.get_program_size());
|
||||||
|
* printf("flash erase size: %llu\n", flash.get_erase_size());
|
||||||
|
*
|
||||||
|
* uint8_t *buffer = malloc(flash.get_erase_size());
|
||||||
|
* sprintf(buffer, "Hello World!\n");
|
||||||
|
* flash.erase(0, flash.get_erase_size());
|
||||||
|
* flash.program(buffer, 0, flash.get_erase_size());
|
||||||
|
* flash.read(buffer, 0, flash.get_erase_size());
|
||||||
|
* printf("%s", buffer);
|
||||||
|
*
|
||||||
|
* flash.deinit();
|
||||||
|
* }
|
||||||
|
*/
|
||||||
|
class I2CEEBlockDevice : public BlockDevice {
|
||||||
|
public:
|
||||||
|
/** Constructor to create an I2CEEBlockDevice on I2C pins
|
||||||
|
*
|
||||||
|
* @param sda The pin name for the sda line of the I2C bus.
|
||||||
|
* @param scl The pin name for the scl line of the I2C bus.
|
||||||
|
* @param addr The 8bit I2C address of the chip, common range 0xa0 - 0xae.
|
||||||
|
* @param size The size of the device in bytes
|
||||||
|
* @param block The page size of the device in bytes, defaults to 32bytes
|
||||||
|
* @param freq The frequency of the I2C bus, defaults to 400K.
|
||||||
|
*/
|
||||||
|
I2CEEBlockDevice(
|
||||||
|
PinName sda, PinName scl, uint8_t address,
|
||||||
|
bd_size_t size, bd_size_t block=32,
|
||||||
|
int bus_speed=400000);
|
||||||
|
|
||||||
|
/** Initialize a block device
|
||||||
|
*
|
||||||
|
* @return 0 on success or a negative error code on failure
|
||||||
|
*/
|
||||||
|
virtual bd_error_t init();
|
||||||
|
|
||||||
|
/** Deinitialize a block device
|
||||||
|
*
|
||||||
|
* @return 0 on success or a negative error code on failure
|
||||||
|
*/
|
||||||
|
virtual bd_error_t deinit();
|
||||||
|
|
||||||
|
/** Read blocks from a block device
|
||||||
|
*
|
||||||
|
* @param buffer Buffer to write blocks to
|
||||||
|
* @param addr Address of block to begin reading from
|
||||||
|
* @param size Size to read in bytes, must be a multiple of read block size
|
||||||
|
* @return 0 on success, negative error code on failure
|
||||||
|
*/
|
||||||
|
virtual bd_error_t read(void *buffer, bd_addr_t addr, bd_size_t size);
|
||||||
|
|
||||||
|
/** Program blocks to a block device
|
||||||
|
*
|
||||||
|
* The blocks must have been erased prior to being programmed
|
||||||
|
*
|
||||||
|
* @param buffer Buffer of data to write to blocks
|
||||||
|
* @param addr Address of block to begin writing to
|
||||||
|
* @param size Size to write in bytes, must be a multiple of program block size
|
||||||
|
* @return 0 on success, negative error code on failure
|
||||||
|
*/
|
||||||
|
virtual bd_error_t program(const void *buffer, bd_addr_t addr, bd_size_t size);
|
||||||
|
|
||||||
|
/** Erase blocks on a block device
|
||||||
|
*
|
||||||
|
* The state of an erased block is undefined until it has been programmed
|
||||||
|
*
|
||||||
|
* @param addr Address of block to begin erasing
|
||||||
|
* @param size Size to erase in bytes, must be a multiple of erase block size
|
||||||
|
* @return 0 on success, negative error code on failure
|
||||||
|
*/
|
||||||
|
virtual bd_error_t erase(bd_addr_t addr, bd_size_t size);
|
||||||
|
|
||||||
|
/** Get the size of a readable block
|
||||||
|
*
|
||||||
|
* @return Size of a readable block in bytes
|
||||||
|
*/
|
||||||
|
virtual bd_size_t get_read_size();
|
||||||
|
|
||||||
|
/** Get the size of a programable block
|
||||||
|
*
|
||||||
|
* @return Size of a programable block in bytes
|
||||||
|
* @note Must be a multiple of the read size
|
||||||
|
*/
|
||||||
|
virtual bd_size_t get_program_size();
|
||||||
|
|
||||||
|
/** Get the size of a eraseable block
|
||||||
|
*
|
||||||
|
* @return Size of a eraseable block in bytes
|
||||||
|
* @note Must be a multiple of the program size
|
||||||
|
*/
|
||||||
|
virtual bd_size_t get_erase_size();
|
||||||
|
|
||||||
|
/** Get the total size of the underlying device
|
||||||
|
*
|
||||||
|
* @return Size of the underlying device in bytes
|
||||||
|
*/
|
||||||
|
virtual bd_size_t size();
|
||||||
|
|
||||||
|
private:
|
||||||
|
I2C _i2c;
|
||||||
|
uint8_t _i2c_addr;
|
||||||
|
uint32_t _size;
|
||||||
|
uint32_t _block;
|
||||||
|
|
||||||
|
bd_error_t _sync();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif /* DEVICE_SPI */
|
||||||
|
|
||||||
|
#endif /* MBED_SD_BLOCK_DEVICE_H */
|
||||||
|
|
@ -0,0 +1,165 @@
|
||||||
|
Apache License
|
||||||
|
Version 2.0, January 2004
|
||||||
|
http://www.apache.org/licenses/
|
||||||
|
|
||||||
|
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||||
|
|
||||||
|
1. Definitions.
|
||||||
|
|
||||||
|
"License" shall mean the terms and conditions for use, reproduction, and
|
||||||
|
distribution as defined by Sections 1 through 9 of this document.
|
||||||
|
|
||||||
|
"Licensor" shall mean the copyright owner or entity authorized by the copyright
|
||||||
|
owner that is granting the License.
|
||||||
|
|
||||||
|
"Legal Entity" shall mean the union of the acting entity and all other entities
|
||||||
|
that control, are controlled by, or are under common control with that entity.
|
||||||
|
For the purposes of this definition, "control" means (i) the power, direct or
|
||||||
|
indirect, to cause the direction or management of such entity, whether by
|
||||||
|
contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||||
|
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||||
|
|
||||||
|
"You" (or "Your") shall mean an individual or Legal Entity exercising
|
||||||
|
permissions granted by this License.
|
||||||
|
|
||||||
|
"Source" form shall mean the preferred form for making modifications, including
|
||||||
|
but not limited to software source code, documentation source, and configuration
|
||||||
|
files.
|
||||||
|
|
||||||
|
"Object" form shall mean any form resulting from mechanical transformation or
|
||||||
|
translation of a Source form, including but not limited to compiled object code,
|
||||||
|
generated documentation, and conversions to other media types.
|
||||||
|
|
||||||
|
"Work" shall mean the work of authorship, whether in Source or Object form, made
|
||||||
|
available under the License, as indicated by a copyright notice that is included
|
||||||
|
in or attached to the work (an example is provided in the Appendix below).
|
||||||
|
|
||||||
|
"Derivative Works" shall mean any work, whether in Source or Object form, that
|
||||||
|
is based on (or derived from) the Work and for which the editorial revisions,
|
||||||
|
annotations, elaborations, or other modifications represent, as a whole, an
|
||||||
|
original work of authorship. For the purposes of this License, Derivative Works
|
||||||
|
shall not include works that remain separable from, or merely link (or bind by
|
||||||
|
name) to the interfaces of, the Work and Derivative Works thereof.
|
||||||
|
|
||||||
|
"Contribution" shall mean any work of authorship, including the original version
|
||||||
|
of the Work and any modifications or additions to that Work or Derivative Works
|
||||||
|
thereof, that is intentionally submitted to Licensor for inclusion in the Work
|
||||||
|
by the copyright owner or by an individual or Legal Entity authorized to submit
|
||||||
|
on behalf of the copyright owner. For the purposes of this definition,
|
||||||
|
"submitted" means any form of electronic, verbal, or written communication sent
|
||||||
|
to the Licensor or its representatives, including but not limited to
|
||||||
|
communication on electronic mailing lists, source code control systems, and
|
||||||
|
issue tracking systems that are managed by, or on behalf of, the Licensor for
|
||||||
|
the purpose of discussing and improving the Work, but excluding communication
|
||||||
|
that is conspicuously marked or otherwise designated in writing by the copyright
|
||||||
|
owner as "Not a Contribution."
|
||||||
|
|
||||||
|
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf
|
||||||
|
of whom a Contribution has been received by Licensor and subsequently
|
||||||
|
incorporated within the Work.
|
||||||
|
|
||||||
|
2. Grant of Copyright License.
|
||||||
|
|
||||||
|
Subject to the terms and conditions of this License, each Contributor hereby
|
||||||
|
grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
|
||||||
|
irrevocable copyright license to reproduce, prepare Derivative Works of,
|
||||||
|
publicly display, publicly perform, sublicense, and distribute the Work and such
|
||||||
|
Derivative Works in Source or Object form.
|
||||||
|
|
||||||
|
3. Grant of Patent License.
|
||||||
|
|
||||||
|
Subject to the terms and conditions of this License, each Contributor hereby
|
||||||
|
grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free,
|
||||||
|
irrevocable (except as stated in this section) patent license to make, have
|
||||||
|
made, use, offer to sell, sell, import, and otherwise transfer the Work, where
|
||||||
|
such license applies only to those patent claims licensable by such Contributor
|
||||||
|
that are necessarily infringed by their Contribution(s) alone or by combination
|
||||||
|
of their Contribution(s) with the Work to which such Contribution(s) was
|
||||||
|
submitted. If You institute patent litigation against any entity (including a
|
||||||
|
cross-claim or counterclaim in a lawsuit) alleging that the Work or a
|
||||||
|
Contribution incorporated within the Work constitutes direct or contributory
|
||||||
|
patent infringement, then any patent licenses granted to You under this License
|
||||||
|
for that Work shall terminate as of the date such litigation is filed.
|
||||||
|
|
||||||
|
4. Redistribution.
|
||||||
|
|
||||||
|
You may reproduce and distribute copies of the Work or Derivative Works thereof
|
||||||
|
in any medium, with or without modifications, and in Source or Object form,
|
||||||
|
provided that You meet the following conditions:
|
||||||
|
|
||||||
|
You must give any other recipients of the Work or Derivative Works a copy of
|
||||||
|
this License; and
|
||||||
|
You must cause any modified files to carry prominent notices stating that You
|
||||||
|
changed the files; and
|
||||||
|
You must retain, in the Source form of any Derivative Works that You distribute,
|
||||||
|
all copyright, patent, trademark, and attribution notices from the Source form
|
||||||
|
of the Work, excluding those notices that do not pertain to any part of the
|
||||||
|
Derivative Works; and
|
||||||
|
If the Work includes a "NOTICE" text file as part of its distribution, then any
|
||||||
|
Derivative Works that You distribute must include a readable copy of the
|
||||||
|
attribution notices contained within such NOTICE file, excluding those notices
|
||||||
|
that do not pertain to any part of the Derivative Works, in at least one of the
|
||||||
|
following places: within a NOTICE text file distributed as part of the
|
||||||
|
Derivative Works; within the Source form or documentation, if provided along
|
||||||
|
with the Derivative Works; or, within a display generated by the Derivative
|
||||||
|
Works, if and wherever such third-party notices normally appear. The contents of
|
||||||
|
the NOTICE file are for informational purposes only and do not modify the
|
||||||
|
License. You may add Your own attribution notices within Derivative Works that
|
||||||
|
You distribute, alongside or as an addendum to the NOTICE text from the Work,
|
||||||
|
provided that such additional attribution notices cannot be construed as
|
||||||
|
modifying the License.
|
||||||
|
You may add Your own copyright statement to Your modifications and may provide
|
||||||
|
additional or different license terms and conditions for use, reproduction, or
|
||||||
|
distribution of Your modifications, or for any such Derivative Works as a whole,
|
||||||
|
provided Your use, reproduction, and distribution of the Work otherwise complies
|
||||||
|
with the conditions stated in this License.
|
||||||
|
|
||||||
|
5. Submission of Contributions.
|
||||||
|
|
||||||
|
Unless You explicitly state otherwise, any Contribution intentionally submitted
|
||||||
|
for inclusion in the Work by You to the Licensor shall be under the terms and
|
||||||
|
conditions of this License, without any additional terms or conditions.
|
||||||
|
Notwithstanding the above, nothing herein shall supersede or modify the terms of
|
||||||
|
any separate license agreement you may have executed with Licensor regarding
|
||||||
|
such Contributions.
|
||||||
|
|
||||||
|
6. Trademarks.
|
||||||
|
|
||||||
|
This License does not grant permission to use the trade names, trademarks,
|
||||||
|
service marks, or product names of the Licensor, except as required for
|
||||||
|
reasonable and customary use in describing the origin of the Work and
|
||||||
|
reproducing the content of the NOTICE file.
|
||||||
|
|
||||||
|
7. Disclaimer of Warranty.
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, Licensor provides the
|
||||||
|
Work (and each Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied,
|
||||||
|
including, without limitation, any warranties or conditions of TITLE,
|
||||||
|
NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are
|
||||||
|
solely responsible for determining the appropriateness of using or
|
||||||
|
redistributing the Work and assume any risks associated with Your exercise of
|
||||||
|
permissions under this License.
|
||||||
|
|
||||||
|
8. Limitation of Liability.
|
||||||
|
|
||||||
|
In no event and under no legal theory, whether in tort (including negligence),
|
||||||
|
contract, or otherwise, unless required by applicable law (such as deliberate
|
||||||
|
and grossly negligent acts) or agreed to in writing, shall any Contributor be
|
||||||
|
liable to You for damages, including any direct, indirect, special, incidental,
|
||||||
|
or consequential damages of any character arising as a result of this License or
|
||||||
|
out of the use or inability to use the Work (including but not limited to
|
||||||
|
damages for loss of goodwill, work stoppage, computer failure or malfunction, or
|
||||||
|
any and all other commercial damages or losses), even if such Contributor has
|
||||||
|
been advised of the possibility of such damages.
|
||||||
|
|
||||||
|
9. Accepting Warranty or Additional Liability.
|
||||||
|
|
||||||
|
While redistributing the Work or Derivative Works thereof, You may choose to
|
||||||
|
offer, and charge a fee for, acceptance of support, warranty, indemnity, or
|
||||||
|
other liability obligations and/or rights consistent with this License. However,
|
||||||
|
in accepting such obligations, You may act only on Your own behalf and on Your
|
||||||
|
sole responsibility, not on behalf of any other Contributor, and only if You
|
||||||
|
agree to indemnify, defend, and hold each Contributor harmless for any liability
|
||||||
|
incurred by, or claims asserted against, such Contributor by reason of your
|
||||||
|
accepting any such warranty or additional liability.
|
||||||
|
|
@ -0,0 +1,44 @@
|
||||||
|
# I2C EEPROM Driver
|
||||||
|
|
||||||
|
Block device driver for I2C based EEPROM devices such as the 24LC or AT24C series of devices.
|
||||||
|
|
||||||
|
EEPROM devices support much higher read/write cycles than flash based memory, at a cost in terms of size. Additionally, I2C EEPROM devices support byte-level read/writes with buffering of around 32bytes during writes.
|
||||||
|
|
||||||
|
**Note:** The I2C convention for EEPROM devices does not support inspection of the memory layout of the device, so the size must be specified during construction of the driver.
|
||||||
|
|
||||||
|
More info on EEPROM can be found on wikipedia:
|
||||||
|
https://en.wikipedia.org/wiki/EEPROM
|
||||||
|
|
||||||
|
``` cpp
|
||||||
|
// Here's an example using a 24LC256 on a GR PEACH
|
||||||
|
#include "mbed.h"
|
||||||
|
#include "I2CEEBlockDevice.h"
|
||||||
|
|
||||||
|
// Create EEPROM device on I2C bus with 32kbytes of memory
|
||||||
|
I2CEEBlockDevice i2cee(D14, D15, 0xa0, 32*1024);
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
printf("i2cee test\n");
|
||||||
|
|
||||||
|
// Initialize the device and print the memory layout
|
||||||
|
i2cee.init();
|
||||||
|
printf("i2cee size: %llu\n", i2cee.size());
|
||||||
|
printf("i2cee read size: %llu\n", i2cee.get_read_size());
|
||||||
|
printf("i2cee program size: %llu\n", i2cee.get_program_size());
|
||||||
|
printf("i2cee erase size: %llu\n", i2cee.get_erase_size());
|
||||||
|
|
||||||
|
// Write "Hello World!" to the first block
|
||||||
|
uint8_t *buffer = malloc(i2cee.get_erase_size());
|
||||||
|
sprintf(buffer, "Hello World!\n");
|
||||||
|
i2cee.erase(0, i2cee.get_erase_size());
|
||||||
|
i2cee.program(buffer, 0, i2cee.get_erase_size());
|
||||||
|
|
||||||
|
// Read back what was stored
|
||||||
|
i2cee.read(buffer, 0, i2cee.get_erase_size());
|
||||||
|
printf("%s", buffer);
|
||||||
|
|
||||||
|
// Deinitialize the device
|
||||||
|
i2cee.deinit();
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
@ -0,0 +1,145 @@
|
||||||
|
#include "mbed.h"
|
||||||
|
#include "greentea-client/test_env.h"
|
||||||
|
#include "unity.h"
|
||||||
|
#include "utest.h"
|
||||||
|
|
||||||
|
#include "I2CEEBlockDevice.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
using namespace utest::v1;
|
||||||
|
|
||||||
|
//#if !I2CEE_INSTALLED
|
||||||
|
//#error [NOT_SUPPORTED] I2CEE Required
|
||||||
|
//#endif
|
||||||
|
|
||||||
|
#define TEST_PINS D14, D15
|
||||||
|
#define TEST_ADDR 0xa0
|
||||||
|
#define TEST_SIZE 32*1024
|
||||||
|
#define TEST_BLOCK_SIZE 64
|
||||||
|
#define TEST_FREQ 400000
|
||||||
|
#define TEST_BLOCK_COUNT 10
|
||||||
|
#define TEST_ERROR_MASK 16
|
||||||
|
|
||||||
|
const struct {
|
||||||
|
const char *name;
|
||||||
|
bd_size_t (BlockDevice::*method)();
|
||||||
|
} ATTRS[] = {
|
||||||
|
{"read size", &BlockDevice::get_read_size},
|
||||||
|
{"program size", &BlockDevice::get_program_size},
|
||||||
|
{"erase size", &BlockDevice::get_erase_size},
|
||||||
|
{"total size", &BlockDevice::size},
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
void test_read_write() {
|
||||||
|
I2CEEBlockDevice bd(TEST_PINS, TEST_ADDR,
|
||||||
|
TEST_SIZE, TEST_BLOCK_SIZE, TEST_FREQ);
|
||||||
|
|
||||||
|
int err = bd.init();
|
||||||
|
TEST_ASSERT_EQUAL(0, err);
|
||||||
|
|
||||||
|
for (unsigned a = 0; a < sizeof(ATTRS)/sizeof(ATTRS[0]); a++) {
|
||||||
|
static const char *prefixes[] = {"", "k", "M", "G"};
|
||||||
|
for (int i = 3; i >= 0; i--) {
|
||||||
|
bd_size_t size = (bd.*ATTRS[a].method)();
|
||||||
|
if (size >= (1ULL << 10*i)) {
|
||||||
|
printf("%s: %llu%sbytes (%llubytes)\n",
|
||||||
|
ATTRS[a].name, size >> 10*i, prefixes[i], size);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bd_size_t block_size = bd.get_erase_size();
|
||||||
|
uint8_t *write_block = new uint8_t[block_size];
|
||||||
|
uint8_t *read_block = new uint8_t[block_size];
|
||||||
|
uint8_t *error_mask = new uint8_t[TEST_ERROR_MASK];
|
||||||
|
unsigned addrwidth = ceil(log(bd.size()-1) / log(16))+1;
|
||||||
|
|
||||||
|
for (int b = 0; b < TEST_BLOCK_COUNT; b++) {
|
||||||
|
// Find a random block
|
||||||
|
bd_addr_t block = (rand()*block_size) % bd.size();
|
||||||
|
|
||||||
|
// Use next random number as temporary seed to keep
|
||||||
|
// the address progressing in the pseudorandom sequence
|
||||||
|
unsigned seed = rand();
|
||||||
|
|
||||||
|
// Fill with random sequence
|
||||||
|
srand(seed);
|
||||||
|
for (bd_size_t i = 0; i < block_size; i++) {
|
||||||
|
write_block[i] = 0xff & rand();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Write, sync, and read the block
|
||||||
|
printf("test %0*llx:%llu...\n", addrwidth, block, block_size);
|
||||||
|
|
||||||
|
err = bd.write(write_block, block, block_size);
|
||||||
|
TEST_ASSERT_EQUAL(0, err);
|
||||||
|
|
||||||
|
printf("write %0*llx:%llu ", addrwidth, block, block_size);
|
||||||
|
for (int i = 0; i < block_size && i < 16; i++) {
|
||||||
|
printf("%02x", write_block[i]);
|
||||||
|
}
|
||||||
|
if (block_size > 16) {
|
||||||
|
printf("...\n");
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
err = bd.read(read_block, block, block_size);
|
||||||
|
TEST_ASSERT_EQUAL(0, err);
|
||||||
|
|
||||||
|
printf("read %0*llx:%llu ", addrwidth, block, block_size);
|
||||||
|
for (int i = 0; i < block_size && i < 16; i++) {
|
||||||
|
printf("%02x", read_block[i]);
|
||||||
|
}
|
||||||
|
if (block_size > 16) {
|
||||||
|
printf("...");
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
// Find error mask for debugging
|
||||||
|
memset(error_mask, 0, TEST_ERROR_MASK);
|
||||||
|
bd_size_t error_scale = block_size / (TEST_ERROR_MASK*8);
|
||||||
|
|
||||||
|
srand(seed);
|
||||||
|
for (bd_size_t i = 0; i < TEST_ERROR_MASK*8; i++) {
|
||||||
|
for (bd_size_t j = 0; j < error_scale; j++) {
|
||||||
|
if ((0xff & rand()) != read_block[i*error_scale + j]) {
|
||||||
|
error_mask[i/8] |= 1 << (i%8);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("error %0*llx:%llu ", addrwidth, block, block_size);
|
||||||
|
for (int i = 0; i < TEST_ERROR_MASK; i++) {
|
||||||
|
printf("%02x", error_mask[i]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
// Check that the data was unmodified
|
||||||
|
srand(seed);
|
||||||
|
for (bd_size_t i = 0; i < block_size; i++) {
|
||||||
|
TEST_ASSERT_EQUAL(0xff & rand(), read_block[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
err = bd.deinit();
|
||||||
|
TEST_ASSERT_EQUAL(0, err);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Test setup
|
||||||
|
utest::v1::status_t test_setup(const size_t number_of_cases) {
|
||||||
|
GREENTEA_SETUP(30, "default_auto");
|
||||||
|
return verbose_test_setup_handler(number_of_cases);
|
||||||
|
}
|
||||||
|
|
||||||
|
Case cases[] = {
|
||||||
|
Case("Testing read write random blocks", test_read_write),
|
||||||
|
};
|
||||||
|
|
||||||
|
Specification specification(test_setup, cases);
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
return !Harness::run(specification);
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue