Simplified access to Ramtron (Cypress) FM24Vxx F-RAM devices
Revision 0:fa858f79d48d, committed 2013-03-23
- Comitter:
- Yann
- Date:
- Sat Mar 23 15:54:01 2013 +0000
- Child:
- 1:6a16bddd7222
- Commit message:
- Create library interface for FM24V10 1Mb Serial 3V F-RAM Memory (Requested by M. Reed Kimble)
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/FM24Vxx_I2C.cpp Sat Mar 23 15:54:01 2013 +0000 @@ -0,0 +1,490 @@ +/* mbed simplified access to RAMTRON FV24xx Serial 3V F-RAM Memory (I2C) + * Copyright (c) 20103 ygarcia, MIT License + * + * 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. + */ +#include <iostream> +#include <sstream> + +#include "FM24Vxx_I2C.h" + +namespace _FM24VXX_I2C { + + unsigned char CFM24VXX_I2C::I2CModuleRefCounter = 0; + + CFM24VXX_I2C::CFM24VXX_I2C(const PinName p_sda, const PinName p_scl, const unsigned char p_address, const PinName p_wp, const unsigned int p_frequency) : _internalId("") { + DEBUG_ENTER("CFM24VXX_I2C") + + if (CFM24VXX_I2C::I2CModuleRefCounter != 0) { + error("CFM24VXX_I2C: Wrong params"); + } +#ifdef __DEBUG + std::ostringstream out(std::ostringstream::out); + out << "CFM24VXX_I2C #" << CFM24VXX_I2C::I2CModuleRefCounter; + _internalId.assign(out.str()); + DEBUG("CFM24VXX_I2C: _internalId='%s'", _internalId.c_str()) +#endif // __DEBUG + _i2cInstance = new I2C(p_sda, p_scl); + CFM24VXX_I2C::I2CModuleRefCounter += 1; + DEBUG_ENTER("CFM24VXX_I2C: refCounter=%d", CFM24VXX_I2C::I2CModuleRefCounter) + // Memory page select is set to 0 + _slaveAddress = (p_address << 2) | 0xa0; // Slave address format is: 1 0 1 0 A2 A1 PS R/W, PS set to 0 + DEBUG("CFM24VXX_I2C: I2C slave adress: 0x%02x", _slaveAddress) + _i2cInstance->frequency(p_frequency); // Set the frequency of the I2C interface + + if (p_wp != NC) { + DEBUG("CFM24VXX_I2C: WP managed"); + _wp = new DigitalOut(p_wp); + _wp->write(0); // Disable write protect + } else { + DEBUG("CFM24VXX_I2C: WP not managed"); + _wp = NULL; // Not used + } + + // Retrieve device identifiers + _deviceID = NULL; + GetDevideIDs(); + + DEBUG_LEAVE("CFM24VXX_I2C") + } + + CFM24VXX_I2C::~CFM24VXX_I2C() { + DEBUG_ENTER("~CFM24VXX_I2C") + + // Release I2C instance + DEBUG_ENTER("~CFM24VXX_I2C: refCounter=%d", CFM24VXX_I2C::I2CModuleRefCounter) + CFM24VXX_I2C::I2CModuleRefCounter -= 1; + if (CFM24VXX_I2C::I2CModuleRefCounter == 0) { + delete _i2cInstance; + _i2cInstance = NULL; + if (_deviceID != NULL) { + delete _deviceID; + _deviceID = NULL; + } + } + // Release _wp if required + if (_wp != NULL) { + _wp->write(0); + delete _wp; + } + + DEBUG_LEAVE("~CFM24VXX_I2C") + } + + bool CFM24VXX_I2C::WriteProtect(const bool p_writeProtect) { + if (_wp != NULL) { + DEBUG("WP set to: %x", (int)p_writeProtect) + _wp->write((int)(p_writeProtect)); + return true; + } + + return false; + } + + void CFM24VXX_I2C::GetDevideIDs() { + _deviceID = new CFM24VXX_IDs(0x00, 0x00, 0x00); + } + + bool CFM24VXX_I2C::EraseMemoryArea(const short p_startAddress, const int p_count, const unsigned char p_pattern) { + DEBUG_ENTER("CFM24VXX_I2C::EraseMemoryArea: 0x%02x - %d - 0x%02x", p_startAddress, p_count, p_pattern) + + std::vector<unsigned char> eraseBuffer(p_count, p_pattern); + return Write(p_startAddress, eraseBuffer, false); + } + + bool CFM24VXX_I2C::Write(const short p_address, const unsigned char p_byte) { + DEBUG_ENTER("CFM24VXX_I2C::Write (byte): Memory address: 0x%02x - 0x%02x", p_address, p_byte) + + // 1.Prepare buffer + char i2cBuffer[3]; // Memory address + one byte of data + // 1.1. Memory address + short address = p_address + 1; // Index start to 1 + i2cBuffer[0] = (unsigned char)(address >> 8); + DEBUG("CFM24VXX_I2C::Write (byte): pI2CBuffer[0]: 0x%02x", i2cBuffer[0]) + i2cBuffer[1] = (unsigned char)((unsigned char)address & 0xff); + DEBUG("CFM24VXX_I2C::Write (byte): pI2CBuffer[1]: 0x%02x", i2cBuffer[1]) + // 1.2. Datas + i2cBuffer[2] = p_byte; + DEBUG("CFM24VXX_I2C::Write (byte): value=0x%02x", i2cBuffer[2]) + + // 2. Send I2C start + I2C address + Memory Address + Datas + I2C stop + int result = _i2cInstance->write(_slaveAddress, i2cBuffer, 3); + wait(0.02); + + DEBUG_LEAVE("CFM24VXX_I2C::Write (byte) %x", (bool)(result == 0)) + return (bool)(result == 0); + } + + bool CFM24VXX_I2C::Write(const short p_address, const short p_short, const CFM24VXX_I2C::Mode p_mode) { + DEBUG_ENTER("CFM24VXX_I2C::Write (short): Memory address:0x%02x, Mode:%d", p_address, p_mode) + + // 1.Prepare buffer + char i2cBuffer[4]; // Memory address + one short (2 bytes) + // 1.1. Memory address + short address = p_address + 1; // Index start to 1 + i2cBuffer[0] = (unsigned char)(address >> 8); + DEBUG("CFM24VXX_I2C::Write (short): pI2CBuffer[0]: 0x%02x", i2cBuffer[0]) + i2cBuffer[1] = (unsigned char)((unsigned char)address & 0xff); + DEBUG("CFM24VXX_I2C::Write (short): pI2CBuffer[1]: 0x%02x", i2cBuffer[1]) + // 1.2. Datas + if (p_mode == BigEndian) { + i2cBuffer[2] = (unsigned char)(p_short >> 8); + i2cBuffer[3] = (unsigned char)((unsigned char)p_short & 0xff); + } else { + i2cBuffer[2] = (unsigned char)((unsigned char)p_short & 0xff); + i2cBuffer[3] = (unsigned char)(p_short >> 8); + } + DEBUG("CFM24VXX_I2C::Write (short): value=0x%02x%02x", i2cBuffer[2], i2cBuffer[3]) + + // 2. Send I2C start + I2C address + Memory Address + Datas + I2C stop + int result = _i2cInstance->write(_slaveAddress, i2cBuffer, 4); + wait(0.02); + + DEBUG_LEAVE("CFM24VXX_I2C::Write (short) %x", (bool)(result == 0)) + return (bool)(result == 0); + } + + bool CFM24VXX_I2C::Write(const short p_address, const int p_int, const CFM24VXX_I2C::Mode p_mode) { + DEBUG_ENTER("CFM24VXX_I2C::Write (int): Memory address:0x%02x, Mode:%d", p_address, p_mode) + + // 1.Prepare buffer + char i2cBuffer[6]; // Memory address + one integer (4 bytes) + // 1.1. Memory address + short address = p_address + 1; // Index start to 1 + i2cBuffer[0] = (unsigned char)(address >> 8); + DEBUG("CFM24VXX_I2C::Write (int): pI2CBuffer[0]: 0x%02x", i2cBuffer[0]) + i2cBuffer[1] = (unsigned char)((unsigned char)address & 0xff); + DEBUG("CFM24VXX_I2C::Write (int): pI2CBuffer[1]: 0x%02x", i2cBuffer[1]) + // 1.2. Datas + if (p_mode == BigEndian) { + i2cBuffer[2] = (unsigned char)(p_int >> 24); + i2cBuffer[3] = (unsigned char)(p_int >> 16); + i2cBuffer[4] = (unsigned char)(p_int >> 8); + i2cBuffer[5] = (unsigned char)((unsigned char)p_int & 0xff); + } else { + i2cBuffer[2] = (unsigned char)((unsigned char)p_int & 0xff); + i2cBuffer[3] = (unsigned char)(p_int >> 8); + i2cBuffer[4] = (unsigned char)(p_int >> 16); + i2cBuffer[5] = (unsigned char)(p_int >> 24); + } + DEBUG("CFM24VXX_I2C::Write (int): value=0x%02x%02x%02x%02x", i2cBuffer[2], i2cBuffer[3], i2cBuffer[4], i2cBuffer[5]) + + // 2. Send I2C start + I2C address + Memory Address + Datas + I2C stop + int result = _i2cInstance->write(_slaveAddress, i2cBuffer, 6); + wait(0.02); + + DEBUG_LEAVE("CFM24VXX_I2C::Write (int) %x", (bool)(result == 0)) + return (bool)(result == 0); + } + + bool CFM24VXX_I2C::Write(const short p_address, const std::string & p_string, const bool p_storeLength, const int p_length2write) { + DEBUG_ENTER("CFM24VXX_I2C::Write (std::string)") + return Write(p_address, p_string.c_str(), p_storeLength, p_length2write); + } + + bool CFM24VXX_I2C::Write(const short p_address, const std::vector<unsigned char> & p_datas, const bool p_storeLength, const int p_length2write) { + DEBUG_ENTER("CFM24VXX_I2C::Write (std::vector)") + + int length = (p_length2write == -1) ? p_datas.size() : p_length2write; + unsigned char array[length]; + std::copy(p_datas.begin(), p_datas.end(), array); + bool result = Write(p_address, array, p_storeLength, length); + wait(0.02); + + DEBUG_LEAVE("CFM24VXX_I2C::Write (std::vector): %d", result) + return result; + } + + bool CFM24VXX_I2C::Write(const short p_address, const char *p_datas, const bool p_storeLength, const int p_length2write) { + DEBUG_ENTER("CFM24VXX_I2C::Write (char *): Memory address: 0x%02x - %x - %d", p_address, p_storeLength, p_length2write) + + // 1.Prepare buffer + int length = (p_length2write == -1) ? strlen(p_datas) : p_length2write; + if (p_storeLength) { + length += 4; // Add four bytes for the length as integer + } + DEBUG("CFM24VXX_I2C::Write (char *): length:%d", length) + + char i2cBuffer[2 + length]; + // 1.1. Memory address + short address = p_address + 1; + i2cBuffer[0] = (unsigned char)(address >> 8); + DEBUG("CFM24VXX_I2C::Write (char *): pI2CBuffer[0]: 0x%02x", i2cBuffer[0]) + i2cBuffer[1] = (unsigned char)((unsigned char)address & 0xff); + DEBUG("CFM24VXX_I2C::Write (char *): pI2CBuffer[1]: 0x%02x", i2cBuffer[1]) + // 1.2. Datas + if (p_storeLength) { + // Fill the length + i2cBuffer[2] = (unsigned char)(length >> 24); + i2cBuffer[3] = (unsigned char)(length >> 16); + i2cBuffer[4] = (unsigned char)(length >> 8); + i2cBuffer[5] = (unsigned char)((unsigned char)length & 0xff); + for (int i = 0; i < length - 4; i++) { + i2cBuffer[6 + i] = *(p_datas + i); + } + } else { // The length was not stored + for (int i = 0; i < length; i++) { + i2cBuffer[2 + i] = *(p_datas + i); + } + } + + // 2. Send I2C start + I2C address + Memory Address + Datas + I2C stop + int result = _i2cInstance->write(_slaveAddress, i2cBuffer, 2 + length); + wait(0.02); + + DEBUG_LEAVE("CFM24VXX_I2C::Write (char *) %x", (bool)(result == 0)) + return (bool)(result == 0); + } + + bool CFM24VXX_I2C::Write(const short p_address, const unsigned char *p_datas, const bool p_storeLength, const int p_length2write) { + DEBUG_ENTER("CFM24VXX_I2C::Write (byte *): Memory address: 0x%02x - %x - %d", p_address, p_storeLength, p_length2write) + return Write(p_address, (const char *)p_datas, p_storeLength, p_length2write); + } + + bool CFM24VXX_I2C::Read(const short p_address, unsigned char * p_byte) { + DEBUG_ENTER("CFM24VXX_I2C::Read (byte): Memory address:0x%02x", p_address) + + // 1.Prepare buffer + char i2cBuffer[2]; + // 1.1. Memory address + i2cBuffer[0] = (unsigned char)(p_address >> 8); + DEBUG("CFM24VXX_I2C::Read (byte): pI2CBuffer[0]: 0x%02x", i2cBuffer[0]) + i2cBuffer[1] = (unsigned char)((unsigned char)p_address & 0xff); + DEBUG("CFM24VXX_I2C::Read (byte): pI2CBuffer[1]: 0x%02x", i2cBuffer[1]) + + // 2. Send I2C start + memory address + if (_i2cInstance->write(_slaveAddress, i2cBuffer, 2, true) == 0) { + wait(0.02); + DEBUG("CFM24VXX_I2C::Read (byte): Write memory done") + // 2. Read data + I2C stop + int result = _i2cInstance->read(_slaveAddress, (char *)p_byte, 1); + wait(0.02); + + DEBUG_LEAVE("CFM24VXX_I2C::Read (byte): %x", (bool)(result == 0)) + return (bool)(result == 0); + } + + DEBUG_LEAVE("CFM24VXX_I2C::Read (byte) (false)") + return false; + } + + bool CFM24VXX_I2C::Read(const short p_address, short *p_short, const CFM24VXX_I2C::Mode p_mode) { + DEBUG_ENTER("CFM24VXX_I2C::Read (short): Memory address:0x%02x, Mode:%d", p_address, p_mode) + + // 1.Prepare buffer + char i2cBuffer[2]; + // 1.1. Memory address + i2cBuffer[0] = (unsigned char)(p_address >> 8); + DEBUG("CFM24VXX_I2C::Read (short): pI2CBuffer[0]: 0x%02x", i2cBuffer[0]) + i2cBuffer[1] = (unsigned char)((unsigned char)p_address & 0xff); + DEBUG("CFM24VXX_I2C::Read (short): pI2CBuffer[1]: 0x%02x", i2cBuffer[1]) + + // 2. Send I2C start + memory address + if (_i2cInstance->write(_slaveAddress, i2cBuffer, 2, true) == 0) { + wait(0.02); + DEBUG("CFM24VXX_I2C::Read (short): Write memory done") + // 2. Read data + I2C stop + int result = _i2cInstance->read(_slaveAddress, i2cBuffer, 2); + if (result == 0) { +DEBUG("CFM24VXX_I2C::Read (short): value: 0x%02x - 0x%02x", i2cBuffer[0], i2cBuffer[1]) + if (p_mode == BigEndian) { + *p_short = (short)(i2cBuffer[0] << 8 | i2cBuffer[1]); + } else { + *p_short = (short)(i2cBuffer[1] << 8 | i2cBuffer[0]); + } + + DEBUG_LEAVE("CFM24VXX_I2C::Read (short): 0x%04x", *p_short) + return true; + } + } + + DEBUG_LEAVE("CFM24VXX_I2C::Read (short) (false)") + return false; + } + + bool CFM24VXX_I2C::Read(const short p_address, int *p_int, const CFM24VXX_I2C::Mode p_mode) { + DEBUG_ENTER("CFM24VXX_I2C::Read (int): Memory address:0x%02x, Mode:%d", p_address, p_mode) + + // 1.Prepare buffer + char i2cBuffer[4]; + // 1.1. Memory address + i2cBuffer[0] = (unsigned char)(p_address >> 8); + DEBUG("CFM24VXX_I2C::Read (int): pI2CBuffer[0]: 0x%02x", i2cBuffer[0]) + i2cBuffer[1] = (unsigned char)((unsigned char)p_address & 0xff); + DEBUG("CFM24VXX_I2C::Read (int): pI2CBuffer[1]: 0x%02x", i2cBuffer[1]) + + // 2. Send I2C start + memory address + if (_i2cInstance->write(_slaveAddress, i2cBuffer, 2, true) == 0) { + wait(0.02); + DEBUG("CFM24VXX_I2C::Read (int): Write memory done") + // 2. Read data + I2C stop + int result = _i2cInstance->read(_slaveAddress, i2cBuffer, 4); + if (result == 0) { +DEBUG("CFM24VXX_I2C::Read (int): value: 0x%02x - 0x%02x - 0x%02x - 0x%02x", i2cBuffer[0], i2cBuffer[1], i2cBuffer[2], i2cBuffer[3]) + wait(0.02); + if (p_mode == BigEndian) { + *p_int = (int)(i2cBuffer[0] << 24 | i2cBuffer[1] << 16 | i2cBuffer[2] << 8 | i2cBuffer[3]); + } else { + *p_int = (int)(i2cBuffer[3] << 24 | i2cBuffer[2] << 16 | i2cBuffer[1] << 8 | i2cBuffer[0]); + } + + DEBUG_LEAVE("CFM24VXX_I2C::Read (int): %d", *p_int) + return true; + } + + DEBUG_LEAVE("CFM24VXX_I2C::Read (int):false") + return false; + } + + DEBUG_LEAVE("CFM24VXX_I2C::Read (int) (false)") + return false; + } + + bool CFM24VXX_I2C::Read(const short p_address, std::vector<unsigned char> & p_datas, const bool p_readLengthFirst, const int p_length2write) { + DEBUG_ENTER("CFM24VXX_I2C::Read (vector): Memory address:0x%02x, readLength:%01x, Length:%d", p_address, p_readLengthFirst, p_length2write) + + // 1.Prepare buffer + short address = p_address; + int length = 0; + if (p_readLengthFirst) { + if (!Read(address, &length)) { // Read the length in big endian mode + DEBUG_LEAVE("CFM24VXX_I2C::Read (vector) Failed to read length") + return false; + } + DEBUG("CFM24VXX_I2C::Read (vector): length= %d", length) + if (length == 0) { + return true; + } + address += 4; // Skip the length value + length -= 4; // length is the size of (string length + string) + } else { + if (p_length2write == -1) { + length = p_datas.size(); + } else { + length = p_length2write; + } + } + DEBUG("CFM24VXX_I2C::Read (vector): length= %d", length) + + // 2. Memory address + char i2cBuffer[2]; + i2cBuffer[0] = (unsigned char)(address >> 8); + DEBUG("CFM24VXX_I2C::Read (vector): pI2CBuffer[0]: 0x%02x", i2cBuffer[0]) + i2cBuffer[1] = (unsigned char)((unsigned char)address & 0xff); + DEBUG("CFM24VXX_I2C::Read (vector): pI2CBuffer[1]: 0x%02x", i2cBuffer[1]) + + // 3. Send I2C start + memory address + if (_i2cInstance->write(_slaveAddress, i2cBuffer, 2, true) == 0) { + wait(0.02); + DEBUG("CFM24VXX_I2C::Read (vector): Write memory done") + // 4. read data + I2C stop + unsigned char buffer[length]; + int result = _i2cInstance->read(_slaveAddress, (char *)buffer, length); + wait(0.02); + if (result == 0) { + p_datas.assign(buffer, buffer + length); + + DEBUG_LEAVE("CFM24VXX_I2C::Read (vector): %x", (bool)(result == 0)) + return (bool)(result == 0); + } + } + + DEBUG_LEAVE("CFM24VXX_I2C::Read (vector) (false)") + return false; + } + + bool CFM24VXX_I2C::Read(const short p_address, std::string & p_string, const bool p_readLengthFirst, const int p_length2write) { + DEBUG_ENTER("CFM24VXX_I2C::Read (string): Memory address:0x%02x, readLength:%01x, Length:%d", p_address, p_readLengthFirst, p_length2write) + +/* std::vector<unsigned char> datas; + if (Read(p_address, datas, p_readLengthFirst, p_length2write) == true) { + p_string.assign((char *)datas.begin(), datas.size()); + + return true; + } + + DEBUG_LEAVE("CFM24VXX_I2C::Read (string) (false)") + return false; +*/ + + // 1.Prepare buffer + short address = p_address; + int length = -1; + if (p_readLengthFirst) { // The string was stored with its length + if (!Read(address, &length)) { // Read the length as integer in big endian mode + DEBUG_ERROR("CFM24VXX_I2C::Read (string): Failed to read length") + return false; + } + wait(0.02); + DEBUG("CFM24VXX_I2C::Read (string): length=%d", length) + if (length == 0) { + DEBUG_ERROR("CFM24VXX_I2C::Read (string): empty") + return true; + } + address += 4; // Skip the length value size + length -= 4; // length is the size of (string length + string) + } else { // The string length is provided by p_length2write parameter + if (p_length2write == -1) { + length = p_string.size(); + } else { + length = p_length2write; + p_string.resize(p_length2write); + } + } + DEBUG("CFM24VXX_I2C::Read (string): Address=0x%02x - Length=%d", address, length) + + // 2. Memory address + char i2cBuffer[2]; + i2cBuffer[0] = (unsigned char)(address >> 8); + DEBUG("CFM24VXX_I2C::Read (string): pI2CBuffer[0]: 0x%02x", i2cBuffer[0]) + i2cBuffer[1] = (unsigned char)((unsigned char)address & 0xff); + DEBUG("CFM24VXX_I2C::Read (string): pI2CBuffer[1]: 0x%02x", i2cBuffer[1]) + + // 3. Send I2C start + memory address with repeat start + if (_i2cInstance->write(_slaveAddress, i2cBuffer, 2, true) == 0) { + wait(0.02); + DEBUG("CFM24VXX_I2C::Read (string): Write memory done") + // 4. Read data + I2C stop + char buffer[length]; + int result = _i2cInstance->read(_slaveAddress, (char *)buffer, length); + if (result == 0) { + p_string.assign(buffer, length); + + return true; + } + } + + DEBUG_LEAVE("CFM24VXX_I2C::Read (string) (false)") + return false; + } + +#if defined(__DEBUG) + void CFM24VXX_I2C::DumpMemoryArea(const int p_address, const int p_count) { + DEBUG_ENTER("CFM24VXX_I2C::DumpMemoryArea: %d - %d", p_address, p_count) + + DEBUG("CFM24VXX_I2C::DumpMemoryArea: Reading datas..."); + std::vector<unsigned char> datas(p_count); + if (!Read(p_address, datas, false)) { // Read bytes, including the lenght indication, buffer size is not set before the call + std::cout << "CFM24VXX_I2C::DumpMemoryArea: read failed\r" << std::endl; + } else { + std::cout << "CFM24VXX_I2C::DumpMemoryArea: Read bytes:\r" << std::endl; + HEXADUMP(&datas[0], p_count); + std::cout << "\r" << std::endl; + } + } +#endif // _DEBUG + +} // End of namespace _FM24VXX_I2C
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/FM24Vxx_I2C.h Sat Mar 23 15:54:01 2013 +0000 @@ -0,0 +1,353 @@ +/* mbed simplified access to RAMTRON FV24xx Serial 3V F-RAM Memory (I2C) + * Copyright (c) 2013 ygarcia, MIT License + * + * 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. + */ +#if !defined(__FM24VXX_I2C_H__) +#define __FM24VXX_I2C_H__ + +#include <string> +#include <vector> + +#include "FM24Vxx_IDs.h" +#include "Debug.h" // Include mbed header + debug primitives. See DebugLibrary + +namespace _FM24VXX_I2C { + /** This class provides simplified I2C access to a RAMTRON FV24xx Serial 3V F-RAM Memory device. V0.0.0.1 + * + * Note that RAMTRON FV24xx Serial 3V F-RAM Memory device could be powered at 3.3V or less only. + * Note that this header file include following headers: + * - <string> + * - <vector> + * - <mbed.h> + * + * @author Yann Garcia (Don't hesitate to contact me: garcia.yann@gmail.com) + */ + class CFM24VXX_I2C { + /** Reference counter used to guarentee unicity of the instance of I2C class + */ + static unsigned char I2CModuleRefCounter; + + /** Device address input: A1, A2 (Pins <2,3>). See FM24V10_ds.pdf - Clause Pin Configuration + */ + unsigned char _slaveAddress; + /** WP state indicator (pin 7); true is write protected, false otherwise + */ + DigitalOut *_wp; + /** An unique instance of I2C class + */ + I2C *_i2cInstance; + /** Device ID. Used for Sleep mode + */ + CFM24VXX_IDs *_deviceID; + public: + /** Memory storage mode + */ + enum Mode { + LittleEndian, //<! Little Endian mode: 0xA0B70708 is stored as 08: MSB and A0 LSB + BigEndian //<! Little Endian mode: 0xA0B70708 is stored as AO: MSB and 08 LSB + }; + public: + /** Constructor with Write Protect command pin wired. Use it to manage the first I2C module + * + * @param p_sda: MBed pin for SDA + * @param p_scl: MBed pin for SCL + * @param p_address: Device address input: A1, A2 (Pins <2,3>) + * @param p_wp: MBed pin to manage Write Protect input. If NC, WP is not managed, default value is NC, not connected + * @param p_frequency: Frequency of the I2C interface (SCL), default value is 400KHz + * Example: + * - If A1 pin is tired to Vdd and A2 is tired to Vss, address shall '00000001'B + */ + CFM24VXX_I2C(const PinName p_sda, const PinName p_scl, const unsigned char p_address, const PinName p_wp = NC, const unsigned int p_frequency = 400000); + + /** Destructor + */ + virtual ~CFM24VXX_I2C(); + + /** Used to return the unique instance of I2C instance + */ + inline const I2C * operator * () { return (const I2C *)_i2cInstance; }; + + /** Used to return the unique device identifier + */ + inline const CFM24VXX_IDs * GetDevideID() { return (const CFM24VXX_IDs *)_deviceID; }; + + /** + * Used to swith high speed mode + * @param highSpeedMode Set to true to switch to high speed mode + * @remark See FM24V10_ds.pdf Page 4/16 Clause High Speed Mode (HS-mode) + */ + inline void SwitchSpeedMode(const bool highSpeedMode) { /* FIXME To be done */ }; + + /** + * Used to enter in sleep mode + * @remark See FM24V10_ds.pdf Page 8/16 Clause Sleep Mode + */ + inline void EnterSleepMode() { /* FIXME To be done */ }; + + /** + * Used to enter in sleep mode + * @remark See FM24V10_ds.pdf Page 8/16 Clause Sleep Mode + */ + inline void LeaveSpeedMode() { /* FIXME To be done */ }; + + /** + * Used to select memory page + * @param memoryPage The selected memory page (0 or 1) + * @remark See FM24V10_ds.pdf Page 3/16 Clause Memory Architecture + */ + inline void SelectMemoryPage(const unsigned char memoryPage) { _slaveAddress |= ((memoryPage & 0x01) << 1) | 0xa0; }; + + /** Erase of memory area starting at the specified address, using the specified pattern to fill the memory area + * + * @param p_startAddress The address of the memory area (from 0 to N - 1, N is the number of cells of the memory) + * @param p_count The size of the memory area to erase + * @param p_pattern The pattern value to use to fill the memory area. Default vqlue: 0x00 + * @return true on success, false otherwise + * Exemple: + * @code + * ... + * myEEPROM.EraseMemoryArea(0, 1024); // Set to 0x00 the first 1Kb memory + * ... + * @endcode + */ + bool EraseMemoryArea(const short p_startAddress, const int p_count, const unsigned char p_pattern = 0x00); + + /** Write a byte at the specified memory address + * + * @param p_address The memory address (from 0 to N - 1, N is the number of cells of the memory) + * @param p_byte The byte value to save + * @return true on success, false otherwise + * Exemple: + * @code + * unsigned char value = 0xaa; + * ... + * myEEPROM.Write(memoryAddress, value); + * ... + * @endcode + */ + bool Write(const short p_address, const unsigned char p_byte); + + /** Write a short at the specified memory address according to the specified mode + * + * @param p_address The memory address (from 0 to N - 1, N is the number of cells of the memory) + * @param p_short The short value to save + * @param p_mode The storage mode. Default value: BigEndian + * @return true on success, false otherwise + * Exemple: + * @code + * short value = 0xcafe; + * ... + * myEEPROM.Write(memoryAddress, value, LittleEndian); + * ... + * @endcode + */ + bool Write(const short p_address, const short p_short, const CFM24VXX_I2C::Mode p_mode = BigEndian); + + /** Write an integer at the specified memory address according to the specified mode + * + * @param p_address The memory address (from 0 to N - 1, N is the number of cells of the memory) + * @param p_int The integer value to save + * @param p_mode The storage mode. Default value: BigEndian + * @return true on success, false otherwise + * Exemple: + * @code + * int value = 0xcafedeca; + * ... + * myEEPROM.Write(memoryAddress, value, LittleEndian); + * ... + * @endcode + */ + bool Write(const short p_address, const int p_int, const CFM24VXX_I2C::Mode p_mode = BigEndian); + + /** Write a buffer of bytes at the specified memory address + * + * @param p_address The memory address (from 0 to N - 1, N is the number of cells of the memory) + * @param p_datas The string to save + * @param p_storeLength If true, store also the length of the buffer in Big Endian mode, otherwise the length will be provided by p_length2write parameter. Default value: true. + * @param p_length2write The number of bytes to write, -1 for all characters. Default value: -1 + * @return true on success, false otherwise + */ + bool Write(const short p_address, const std::vector<unsigned char> & p_datas, bool p_storeLength = true, const int p_length2write = -1); + + /** Write a buffer of bytes at the specified memory address + * + * @param p_address The memory address (from 0 to N - 1, N is the number of cells of the memory) + * @param p_datas The buffer of bytes to save + * @param p_storeLength If true, store also the length of the buffer in Big Endian mode, otherwise the length will be provided by p_length2write parameter. Default value: true. + * @param p_length2write The number of bytes to write, -1 for all bytes. Default value: -1 + * @return true on success, false otherwise + */ + bool Write(const short p_address, const unsigned char *p_datas, bool p_storeLength = true, const int p_length2write = -1); + + /** Write a string at the specified memory address + * + * @param p_address The memory address (from 0 to N - 1, N is the number of cells of the memory) + * @param p_string The string to save + * @param p_storeLength If true, store also the length of the string in Big Endian mode, otherwise the length will be provided by p_length2write parameter. Default value: true. + * @param p_length2write The number of character to write, -1 for all characters + * @return true on success, false otherwise + * Exemple: + * @code + * std::string text2save("CafeDeca"); + * ... + * myEEPROM.Write(memoryAddress, text2save); + * ... + * @endcode + */ + bool Write(const short p_address, const std::string & p_string, const bool p_storeLength = true, const int p_length2write = -1); + + /** Write a buffer of characters at the specified memory address (from 0 to N - 1, N is the number of cells of the memory) + * + * Note that the length of the buffer is not saved and the string is saved in Big Endian mode + * @param p_address The memory address (from 0 to N - 1, N is the number of cells of the memory) + * @param p_datas The string to save + * @param p_storeLength If true, store also the length of the string in Big Endian mode, otherwise the length will be provided by p_length2write parameter. Default value: true. + * @param length2write The number of character to write, -1 for all characters + * @return true on success, false otherwise + */ + bool Write(const short p_address, const char *p_datas, const bool p_storeLength = true, const int p_length2write = -1); + + /** Read a byte from the specified memory address + * + * @param p_address The memory address (from 0 to N - 1, N is the number of cells of the memory) + * @param p_byte The byte value to read + * @return true on success, false otherwise + * Exemple: + * @code + * unsigned char value; + * ... + * myEEPROM.Read(memoryAddress, (unsigned char *)&value); + * ... + * @endcode + */ + bool Read(const short p_address, unsigned char *p_value); + + /** Read a short from the specified memory address + * + * @param p_address The memory address (from 0 to N - 1, N is the number of cells of the memory) + * @param p_short The short value to read + * @return true on success, false otherwise + * Exemple: + * @code + * short value; + * ... + * myEEPROM.Read(memoryAddress, (short *)&value); + * ... + * @endcode + */ + bool Read(const short p_address, short *p_short, CFM24VXX_I2C::Mode p_mode = BigEndian); + + /** Read an integer from the specified memory address + * + * @param p_address The memory address (from 0 to N - 1, N is the number of cells of the memory) + * @param p_int The integer value to read + * @return true on success, false otherwise + * Exemple: + * @code + * int value; + * ... + * myEEPROM.Read(memoryAddress, (int *)&value); + * ... + * @endcode + */ + bool Read(const short p_address, int *p_int, CFM24VXX_I2C::Mode p_mode = BigEndian); + + /** Read a buffer of bytes from the specified memory address and store it into a std::vector<unsigned char> object + * + * Note that the size of the buffer object is used for the number of bytes to read + * @param p_address The memory address (from 0 to N - 1, N is the number of cells of the memory) + * @param p_datas The buffer to fill + * @param p_readLengthFirst If true, read the length first and p_length2write parameter is ignored, otherwise the length is provided by p_length2write parameter. Default value: true + * @param p_length2read The number of character to write, -1 to use the size of the string buffer + * @return true on success, false otherwise + * Exemple: + * @code + * std::vector<unsigned char> datas(bufferLength); + * ... + * myEEPROM.Read(memoryAddress, datas); + * ... + * @endcode + */ + bool Read(const short p_address, std::vector<unsigned char> & p_datas, bool p_readLengthFirst = true, int p_length2read = -1); + + /** Read a buffer of characters from the specified memory address and store it into a string object + * + * Note that the size of the string object is used for the number of characters to read + * @param p_address The memory address (from 0 to N - 1, N is the number of cells of the memory) + * @param p_string The string buffer to fill + * @param p_readLengthFirst If true, read the length first and p_length2write parameter is ignored, otherwise the length is provided by p_length2write parameter. Default value: true + * @param p_length2write The number of character to write, -1 to use the size of the string buffer + * @return true on success, false otherwise + * Exemple: + * @code + * std::string readtext; + * ... + * myEEPROM.Read(memoryAddress, readtext); + * ... + * @endcode + */ + bool Read(const short p_address, std::string & p_string, bool p_readLengthFirst = true, int p_length2write = -1); + + /** Activate or deactivate write protect (pin 7) + * + * @param p_writeProtect: Set to true to activate write protection, false otherwise + * @return true on success, false otherwise + */ + bool WriteProtect(const bool p_writeProtect); + + /** Indicate the current WP state indicator (pin 7) + * @return true is write protected, false otherwise + */ + inline bool IsWriteProtected() { + return (_wp != NULL) ? (bool)(_wp->read() == 1) : false; + } + +#if defined(__DEBUG) + /** Dump a memory area + * + * Note that this method is available only on debug mode + * @param p_address The memory address (from 0 to N - 1, N is the number of cells of the memory) + * @param p_count The number of bytes toi dump + * @return true on success, false otherwise + */ + void DumpMemoryArea(const int p_address, const int p_count); + /** For debug purpose only + */ + inline std::string & ToString() { return _internalId; }; +#else // __DEBUG + inline void DumpMemoryArea(const int p_address, const int p_count) {}; +#endif // _DEBUG + + private: + /** Internal reference identifier + */ + std::string _internalId; + + private: + /** + * Retrieve device identifiers + * @remark See FM24V10_ds.pdf Page 9/16 Clause Device ID + */ + void GetDevideIDs(); + + }; // End of class CFM24VXX_I2C + +}; // End of namespace _FM24VXX_I2C + +using namespace _FM24VXX_I2C; + +#endif // __FM24VXX_I2C_H__
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/FM24Vxx_IDs.h Sat Mar 23 15:54:01 2013 +0000 @@ -0,0 +1,67 @@ +/* mbed simplified access to RAMTRON FV24xx Serial 3V F-RAM Memory (I2C) + * Copyright (c) 2013 ygarcia, MIT License + * + * 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. + */ +#if !defined(__FM24VXX_IDS_H__) +#define __FM24VXX_IDS_H__ + +#include <mbed.h> + +namespace _FM24VXX_I2C { + +/** + * Device ID description + * @remark See Figure 14. Manufacturer and Product ID + */ +class CFM24VXX_IDs { + + /** Device ID description + */ + struct { + unsigned char manufacturerID; + union { + unsigned char id; + struct { + unsigned char variation:4; + unsigned char density:4; + } vd; + } productID; + unsigned char dieRevision; + } _deviceID; + + public: + inline CFM24VXX_IDs(const unsigned char manufacturerID, const unsigned char productID, const unsigned char dieRevision) { + _deviceID.manufacturerID = manufacturerID; + _deviceID.productID.id = productID; + _deviceID.dieRevision = dieRevision; + } // End of Constructor + + inline unsigned char GetManufacturerID() const { return _deviceID.manufacturerID; }; + + inline unsigned char GetProductID() const { return _deviceID.productID.id; }; + + inline unsigned char GetRevisionID() const { return _deviceID.dieRevision; }; + + inline unsigned char GetDensity() const { return _deviceID.productID.vd.density >> 4; }; + + inline unsigned char GetVariation() const { return _deviceID.productID.vd.variation; }; + +}; // End of class CFM24VXX_IDs + +} // End of namespace _FM24VXX_I2C + +#endif // __FM24VXX_IDS_H__