From e10f04f18c0561656dfb27a8cbf5a18ff989f7b6 Mon Sep 17 00:00:00 2001 From: Victor Tseng Date: Sun, 22 Jan 2017 14:48:42 +0800 Subject: [PATCH] fix whitespace... --- FRAM_MB85RC_I2C.cpp | 125 +++++++++++++++++++++----------------------- FRAM_MB85RC_I2C.h | 18 +++---- 2 files changed, 69 insertions(+), 74 deletions(-) diff --git a/FRAM_MB85RC_I2C.cpp b/FRAM_MB85RC_I2C.cpp index 4bce434..7a41b8e 100644 --- a/FRAM_MB85RC_I2C.cpp +++ b/FRAM_MB85RC_I2C.cpp @@ -32,7 +32,7 @@ Constructor */ /**************************************************************************/ -FRAM_MB85RC_I2C::FRAM_MB85RC_I2C(void) +FRAM_MB85RC_I2C::FRAM_MB85RC_I2C(void) { _framInitialised = false; _manualMode = false; @@ -42,7 +42,7 @@ FRAM_MB85RC_I2C::FRAM_MB85RC_I2C(void) } -FRAM_MB85RC_I2C::FRAM_MB85RC_I2C(uint8_t address, boolean wp) +FRAM_MB85RC_I2C::FRAM_MB85RC_I2C(uint8_t address, boolean wp) { _framInitialised = false; _manualMode = false; @@ -51,17 +51,17 @@ FRAM_MB85RC_I2C::FRAM_MB85RC_I2C(uint8_t address, boolean wp) byte result = FRAM_MB85RC_I2C::initWP(wp); } -FRAM_MB85RC_I2C::FRAM_MB85RC_I2C(uint8_t address, boolean wp, int pin) +FRAM_MB85RC_I2C::FRAM_MB85RC_I2C(uint8_t address, boolean wp, int pin) { _framInitialised = false; _manualMode = false; i2c_addr = address; wpPin = pin; byte result = FRAM_MB85RC_I2C::initWP(wp); - + } -FRAM_MB85RC_I2C::FRAM_MB85RC_I2C(uint8_t address, boolean wp, int pin, uint16_t chipDensity) +FRAM_MB85RC_I2C::FRAM_MB85RC_I2C(uint8_t address, boolean wp, int pin, uint16_t chipDensity) { //This constructor provides capability for chips without the device IDs implemented _framInitialised = false; @@ -71,8 +71,8 @@ FRAM_MB85RC_I2C::FRAM_MB85RC_I2C(uint8_t address, boolean wp, int pin, uint16_t density = chipDensity; byte result = FRAM_MB85RC_I2C::initWP(wp); - - + + } /*========================================================================*/ @@ -81,8 +81,8 @@ FRAM_MB85RC_I2C::FRAM_MB85RC_I2C(uint8_t address, boolean wp, int pin, uint16_t void FRAM_MB85RC_I2C::begin(void) { - - + + byte deviceFound = FRAM_MB85RC_I2C::checkDevice(); #ifdef SERIAL_DEBUG @@ -121,7 +121,7 @@ void FRAM_MB85RC_I2C::begin(void) { 7 = device not found */ /**************************************************************************/ -byte FRAM_MB85RC_I2C::checkDevice(void) +byte FRAM_MB85RC_I2C::checkDevice(void) { byte result; if (_manualMode) { @@ -130,8 +130,8 @@ byte FRAM_MB85RC_I2C::checkDevice(void) else { result = getDeviceIDs(); } - - // + + // if ((result == ERROR_0) && ((manufacturer == FUJITSU_MANUFACT_ID) || (manufacturer == CYPRESS_MANUFACT_ID) || (manufacturer == MANUALMODE_MANUFACT_ID)) && (maxaddress != 0)) { _framInitialised = true; } @@ -146,7 +146,7 @@ byte FRAM_MB85RC_I2C::checkDevice(void) /**************************************************************************/ /*! @brief Writes an array of bytes from a specific address - + @params[in] i2cAddr The I2C address of the FRAM memory chip (1010+A2+A1+A0) @params[in] framAddr @@ -162,8 +162,8 @@ byte FRAM_MB85RC_I2C::checkDevice(void) byte FRAM_MB85RC_I2C::writeArray (uint16_t framAddr, byte items, uint8_t values[]) { if ((framAddr >= maxaddress) || ((framAddr + (uint16_t) items - 1) >= maxaddress)) return ERROR_11; - - + + FRAM_MB85RC_I2C::I2CAddressAdapt(framAddr); for (byte i=0; i < items ; i++) { Wire.write(values[i]); @@ -174,7 +174,7 @@ byte FRAM_MB85RC_I2C::writeArray (uint16_t framAddr, byte items, uint8_t values[ /**************************************************************************/ /*! @brief Writes a single byte to a specific address - + @params[in] i2cAddr The I2C address of the FRAM memory chip (1010+A2+A1+A0) @params[in] framAddr @@ -188,7 +188,7 @@ byte FRAM_MB85RC_I2C::writeArray (uint16_t framAddr, byte items, uint8_t values[ byte FRAM_MB85RC_I2C::writeByte (uint16_t framAddr, uint8_t value) { - uint8_t buffer[] = {value}; + uint8_t buffer[] = {value}; return FRAM_MB85RC_I2C::writeArray(framAddr, 1, buffer); } @@ -206,14 +206,14 @@ byte FRAM_MB85RC_I2C::writeByte (uint16_t framAddr, uint8_t value) number of items to read from memory chip @params[out] values[] array to be filled in by the memory read - @returns + @returns return code of Wire.endTransmission() */ /**************************************************************************/ byte FRAM_MB85RC_I2C::readArray (uint16_t framAddr, byte items, uint8_t values[]) { if ((framAddr >= maxaddress) || ((framAddr + (uint16_t) items - 1) >= maxaddress)) return ERROR_11; - + byte result; if (items == 0) { result = ERROR_8; //number of bytes asked to read null @@ -221,7 +221,7 @@ byte FRAM_MB85RC_I2C::readArray (uint16_t framAddr, byte items, uint8_t values[] else { FRAM_MB85RC_I2C::I2CAddressAdapt(framAddr); result = Wire.endTransmission(); - + Wire.requestFrom(i2c_addr, (uint8_t)items); for (byte i=0; i < items; i++) { values[i] = Wire.read(); @@ -240,11 +240,11 @@ byte FRAM_MB85RC_I2C::readArray (uint16_t framAddr, byte items, uint8_t values[] The 16-bit address to read from in FRAM memory @params[out] *values data read from memory - @returns + @returns return code of Wire.endTransmission() */ /**************************************************************************/ -byte FRAM_MB85RC_I2C::readByte (uint16_t framAddr, uint8_t *value) +byte FRAM_MB85RC_I2C::readByte (uint16_t framAddr, uint8_t *value) { uint8_t buffer[1]; byte result = FRAM_MB85RC_I2C::readArray(framAddr, 1, buffer); @@ -261,11 +261,11 @@ byte FRAM_MB85RC_I2C::readByte (uint16_t framAddr, uint8_t *value) The 16-bit address to read from in FRAM memory @params[in] destAddr The 16-bit address to write in FRAM memory - @returns + @returns return code of Wire.endTransmission() */ /**************************************************************************/ -byte FRAM_MB85RC_I2C::copyByte (uint16_t origAddr, uint16_t destAddr) +byte FRAM_MB85RC_I2C::copyByte (uint16_t origAddr, uint16_t destAddr) { uint8_t buffer[1]; byte result = FRAM_MB85RC_I2C::readByte(origAddr, buffer); @@ -284,7 +284,7 @@ byte FRAM_MB85RC_I2C::copyByte (uint16_t origAddr, uint16_t destAddr) The bit position to read @params[out] *bit value of the bit: 0 | 1 - @returns + @returns return code of Wire.endTransmission() return code 9 if bit position is larger than 7 */ @@ -311,7 +311,7 @@ byte FRAM_MB85RC_I2C::readBit(uint16_t framAddr, uint8_t bitNb, byte *bit) The 16-bit address to read from in FRAM memory @params[in] bitNb The bit position to set - @returns + @returns return code of Wire.endTransmission() return code 9 if bit position is larger than 7 */ @@ -338,7 +338,7 @@ byte FRAM_MB85RC_I2C::setOneBit(uint16_t framAddr, uint8_t bitNb) The 16-bit address to read from in FRAM memory @params[in] bitNb The bit position to clear - @returns + @returns return code of Wire.endTransmission() return code 9 if bit position is larger than 7 */ @@ -365,7 +365,7 @@ byte FRAM_MB85RC_I2C::clearOneBit(uint16_t framAddr, uint8_t bitNb) The 16-bit address to read from in FRAM memory @params[in] bitNb The bit position to toggle - @returns + @returns return code of Wire.endTransmission() return code 9 if bit position is larger than 7 */ @@ -379,7 +379,7 @@ byte FRAM_MB85RC_I2C::toggleBit(uint16_t framAddr, uint8_t bitNb) else { uint8_t buffer[1]; result = FRAM_MB85RC_I2C::readArray(framAddr, 1, buffer); - + if ( (buffer[0] & (1 << bitNb)) == (1 << bitNb) ) { bitClear(buffer[0], bitNb); @@ -399,7 +399,7 @@ byte FRAM_MB85RC_I2C::toggleBit(uint16_t framAddr, uint8_t bitNb) The 16-bit address to read from in FRAM memory @params[out] value 16bits word - @returns + @returns return code of Wire.endTransmission() */ /**************************************************************************/ @@ -407,7 +407,7 @@ byte FRAM_MB85RC_I2C::readWord(uint16_t framAddr, uint16_t *value) { uint8_t buffer[2]; byte result = FRAM_MB85RC_I2C::readArray(framAddr, 2, buffer); - *value = ((uint16_t) buffer[0] << 8) + (uint16_t)buffer[1]; + *value = *reinterpret_cast(buffer); return result; } @@ -419,13 +419,13 @@ byte FRAM_MB85RC_I2C::readWord(uint16_t framAddr, uint16_t *value) The 16-bit address to read from in FRAM memory @params[in] value 16bits word - @returns + @returns return code of Wire.endTransmission() */ /**************************************************************************/ byte FRAM_MB85RC_I2C::writeWord(uint16_t framAddr, uint16_t value) { - uint8_t buffer[2] = {(value >> 8), (value & 0xFF)}; + uint8_t *buffer = reinterpret_cast(&value); return FRAM_MB85RC_I2C::writeArray(framAddr, 2, buffer); } /**************************************************************************/ @@ -436,7 +436,7 @@ byte FRAM_MB85RC_I2C::writeWord(uint16_t framAddr, uint16_t value) The 16-bit address to read from FRAM memory @params[in] value 32bits word - @returns + @returns return code of Wire.endTransmission() */ /**************************************************************************/ @@ -444,7 +444,7 @@ byte FRAM_MB85RC_I2C::readLong(uint16_t framAddr, uint32_t *value) { uint8_t buffer[4]; byte result = FRAM_MB85RC_I2C::readArray(framAddr, 4, buffer); - *value = ((uint32_t) buffer[0] << 24) + ((uint32_t)buffer[1] << 16) + ((uint32_t)buffer[2] << 8) + (uint32_t)buffer[3]; + *value = *reinterpret_cast(buffer); return result; } @@ -456,17 +456,13 @@ byte FRAM_MB85RC_I2C::readLong(uint16_t framAddr, uint32_t *value) The 16-bit address to write to FRAM memory @params[in] value 32bits word - @returns + @returns return code of Wire.endTransmission() */ /**************************************************************************/ byte FRAM_MB85RC_I2C::writeLong(uint16_t framAddr, uint32_t value) { - uint8_t buffer[4]; - buffer[0] = (uint8_t)(value >> 24); - buffer[1] = (uint8_t)((value & 0xFFFFFF) >> 16); - buffer[2] = (uint8_t)((value & 0xFFFF) >> 8); - buffer[3] = (uint8_t)(value & 0xFF); + uint8_t *buffer = reinterpret_cast(&value); return FRAM_MB85RC_I2C::writeArray(framAddr, 4, buffer); } /**************************************************************************/ @@ -482,14 +478,14 @@ byte FRAM_MB85RC_I2C::writeLong(uint16_t framAddr, uint32_t value) 1: error */ /**************************************************************************/ -byte FRAM_MB85RC_I2C::getOneDeviceID(uint8_t idType, uint16_t *id) +byte FRAM_MB85RC_I2C::getOneDeviceID(uint8_t idType, uint16_t *id) { byte result; const uint8_t manuf = 1; const uint8_t prod = 2; const uint8_t densc = 3; const uint8_t densi = 4; - + switch (idType) { case manuf: *id = manufacturer; @@ -612,19 +608,18 @@ byte FRAM_MB85RC_I2C::disableWP() { byte FRAM_MB85RC_I2C::eraseDevice(void) { byte result = 0; uint16_t i = 0; - + #ifdef SERIAL_DEBUG if (Serial){ Serial.println("Start erasing device"); } #endif - + while((i < maxaddress) && (result == 0)){ result = FRAM_MB85RC_I2C::writeByte(i, 0x00); i++; } - - + #ifdef SERIAL_DEBUG if (Serial){ if (result !=0) { @@ -660,7 +655,7 @@ byte FRAM_MB85RC_I2C::eraseDevice(void) { @param[out] The memory densitycode (bytes 11..8) from 0x03 (64K chip) to 0x07 (1M chip) @param[out] The memory density got from density code - from 64 to 1024K + from 64 to 1024K @param[out] The memory max address of storage slot @returns return code of Wire.endTransmission() or interpreted error. @@ -670,24 +665,24 @@ byte FRAM_MB85RC_I2C::getDeviceIDs(void) { uint8_t localbuffer[3] = { 0, 0, 0 }; uint8_t result; - + /* Get device IDs sequence */ /* 1/ Send 0xF8 to the I2C bus as a write instruction. bit 0: 0 => 0xF8 >> 1 */ /* Send 0xF8 to 12C bus. Bit shift to right as beginTransmission() requires a 7bit. beginTransmission() 0 for write => 0xF8 */ /* Send device address as 8 bits. Bit shift to left as we are using a simple write() */ /* Send 0xF9 to I2C bus. By requesting 3 bytes to read, requestFrom() add a 1 bit at the end of a 7 bits address => 0xF9 */ /* See p.10 of http://www.fujitsu.com/downloads/MICRO/fsa/pdf/products/memory/fram/MB85RC-DS501-00017-3v0-E.pdf */ - - + + Wire.beginTransmission(MASTER_CODE >> 1); Wire.write((byte)(i2c_addr << 1)); result = Wire.endTransmission(false); - + Wire.requestFrom(MASTER_CODE >> 1, 3); localbuffer[0] = (uint8_t) Wire.read(); localbuffer[1] = (uint8_t) Wire.read(); localbuffer[2] = (uint8_t) Wire.read(); - + /* Shift values to separate IDs */ manufacturer = (localbuffer[0] << 4) + (localbuffer[1] >> 4); densitycode = (uint16_t)(localbuffer[1] & 0x0F); @@ -751,7 +746,7 @@ byte FRAM_MB85RC_I2C::getDeviceIDs(void) density = 0; /* means error */ maxaddress = 0; /* means error */ if (result == 0) result = ERROR_7; /*device unidentified, comminication ok*/ - + } return result; @@ -762,9 +757,9 @@ byte FRAM_MB85RC_I2C::getDeviceIDs(void) @brief set devices IDs for chip that does not support the feature as this has not been implemented in every chips by manufacturers @params[in] none - + @params[out] manufacturerID set as "manual mode" - + @param[out] The memory max address of storage slot @returns return Error_0, Error_7, ERROR_10 codes @@ -802,7 +797,7 @@ byte FRAM_MB85RC_I2C::setDeviceIDs(void) densitycode = MANUALMODE_DENSITY_ID; productid = MANUALMODE_PRODUCT_ID; manufacturer = MANUALMODE_MANUFACT_ID; - if (maxaddress !=0) { + if (maxaddress !=0) { return ERROR_0; } else { @@ -815,7 +810,7 @@ byte FRAM_MB85RC_I2C::setDeviceIDs(void) } /**************************************************************************/ /*! - @brief Utility function to print out memory chip IDs to serial if Debug enabled + @brief Utility function to print out memory chip IDs to serial if Debug enabled @params[in] SERIAL_DEBUG @param[out] none @@ -882,7 +877,7 @@ byte FRAM_MB85RC_I2C::initWP(boolean wp) { 4K chips : 1 MSB of memory address as LSB of device address + 8 bits memory address 16K chips : 3 MSB of memory address as LSB of device address + 8 bits memory address 64K and more chips : full chipp address & 16 bits memory address - + @params[in] address : memory address @param[out] none @@ -890,9 +885,9 @@ byte FRAM_MB85RC_I2C::initWP(boolean wp) { */ /**************************************************************************/ void FRAM_MB85RC_I2C::I2CAddressAdapt(uint16_t framAddr) { - + uint8_t chipaddress; - + switch(density) { case 4: chipaddress = (i2c_addr | ((framAddr >> 8) & 0x1)); @@ -904,12 +899,12 @@ void FRAM_MB85RC_I2C::I2CAddressAdapt(uint16_t framAddr) { chipaddress = i2c_addr; break; } - + #ifdef SERIAL_DEBUG Serial.print("Calculated address 0x"); Serial.println(chipaddress, HEX); #endif - + if (density < 64) { Wire.beginTransmission(chipaddress); Wire.write(framAddr & 0xFF); @@ -917,7 +912,7 @@ void FRAM_MB85RC_I2C::I2CAddressAdapt(uint16_t framAddr) { else { Wire.beginTransmission(chipaddress); Wire.write(framAddr >> 8); - Wire.write(framAddr & 0xFF); + Wire.write(framAddr & 0xFF); } return; -} \ No newline at end of file +} diff --git a/FRAM_MB85RC_I2C.h b/FRAM_MB85RC_I2C.h index 34c25c9..5271aa6 100644 --- a/FRAM_MB85RC_I2C.h +++ b/FRAM_MB85RC_I2C.h @@ -1,8 +1,8 @@ /**************************************************************************/ -/*! +/*! @file FRAM_MB85RC_I2C.h @author SOSAndroid.fr (E. Ha.) - + @section HISTORY v1.0 - First release @@ -14,7 +14,7 @@ v1.1.0b - adding support for devices without device IDs + 4K & 16 K devices support Driver for the MB85RC I2C FRAM from Fujitsu. - + @section LICENSE Software License Agreement (BSD License) @@ -103,7 +103,7 @@ //Special commands #define MASTER_CODE 0xF8 -#define SLEEP_MODE 0x86 //Cypress codes, not used here +#define SLEEP_MODE 0x86 //Cypress codes, not used here #define HIGH_SPEED 0x08 //Cypress codes, not used here // Managing Write protect pin @@ -112,7 +112,7 @@ #define DEFAULT_WP_STATUS false //false means protection is off - write is enabled // Error management -#define ERROR_0 0 // Success +#define ERROR_0 0 // Success #define ERROR_1 1 // Data too long to fit the transmission buffer on Arduino #define ERROR_2 2 // received NACK on transmit of address #define ERROR_3 3 // received NACK on transmit of data @@ -132,7 +132,7 @@ class FRAM_MB85RC_I2C { FRAM_MB85RC_I2C(uint8_t address, boolean wp); FRAM_MB85RC_I2C(uint8_t address, boolean wp, int pin); FRAM_MB85RC_I2C(uint8_t address, boolean wp, int pin, uint16_t chipDensity); - + void begin(void); byte checkDevice(void); byte readBit(uint16_t framAddr, uint8_t bitNb, byte *bit); @@ -154,13 +154,13 @@ class FRAM_MB85RC_I2C { byte enableWP(void); byte disableWP(void); byte eraseDevice(void); - + private: uint8_t i2c_addr; boolean _framInitialised; boolean _manualMode; uint16_t manufacturer; - uint16_t productid; + uint16_t productid; uint16_t densitycode; uint16_t density; uint16_t maxaddress; @@ -168,7 +168,7 @@ class FRAM_MB85RC_I2C { int wpPin; boolean wpStatus; - byte getDeviceIDs(void); + byte getDeviceIDs(void); byte setDeviceIDs(void); byte initWP(boolean wp); byte deviceIDs2Serial(void);