Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: TYBLE16_simple_data_logger TYBLE16_MP3_Air
I2CEEBlockDevice.h
00001 /* Simple access class for I2C EEPROM chips like Microchip 24LC 00002 * Copyright (c) 2015 Robin Hourahane 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 #ifndef MBED_I2CEEPROM_BLOCK_DEVICE_H 00017 #define MBED_I2CEEPROM_BLOCK_DEVICE_H 00018 00019 #include "features/storage/blockdevice/BlockDevice.h" 00020 #include "drivers/I2C.h" 00021 00022 /** BlockDevice for I2C based flash device such as 00023 * Microchip's 24LC or ATMEL's AT24C ranges 00024 * 00025 * @code 00026 * // Here's an example using a 24LC256 on a GR PEACH 00027 * #include "mbed.h" 00028 * #include "I2CEEBlockDevice.h" 00029 * 00030 * // Create EEPROM device on I2C bus with 32kbytes of memory 00031 * I2CEEBlockDevice i2cee(D14, D15, 0xa0, 32*1024); 00032 * 00033 * int main() { 00034 * printf("i2cee test\n"); 00035 * 00036 * // Initialize the device and print the memory layout 00037 * i2cee.init(); 00038 * printf("i2cee size: %llu\n", i2cee.size()); 00039 * printf("i2cee read size: %llu\n", i2cee.get_read_size()); 00040 * printf("i2cee program size: %llu\n", i2cee.get_program_size()); 00041 * printf("i2cee erase size: %llu\n", i2cee.get_erase_size()); 00042 * 00043 * // Write "Hello World!" to the first block 00044 * char *buffer = (char*)malloc(i2cee.get_erase_size()); 00045 * sprintf(buffer, "Hello World!\n"); 00046 * i2cee.erase(0, i2cee.get_erase_size()); 00047 * i2cee.program(buffer, 0, i2cee.get_erase_size()); 00048 * 00049 * // Read back what was stored 00050 * i2cee.read(buffer, 0, i2cee.get_erase_size()); 00051 * printf("%s", buffer); 00052 * 00053 * // Deinitialize the device 00054 * i2cee.deinit(); 00055 * } 00056 * @endcode 00057 */ 00058 class I2CEEBlockDevice : public BlockDevice { 00059 public: 00060 /** Constructor to create an I2CEEBlockDevice on I2C pins 00061 * 00062 * @param sda The pin name for the sda line of the I2C bus. 00063 * @param scl The pin name for the scl line of the I2C bus. 00064 * @param addr The 8bit I2C address of the chip, common range 0xa0 - 0xae. 00065 * @param size The size of the device in bytes 00066 * @param block The page size of the device in bytes, defaults to 32bytes 00067 * @param freq The frequency of the I2C bus, defaults to 400K. 00068 */ 00069 I2CEEBlockDevice( 00070 PinName sda, PinName scl, uint8_t address, 00071 bd_size_t size, bd_size_t block = 32, 00072 int bus_speed = 400000); 00073 00074 /** Constructor to create an I2CEEBlockDevice on I2C pins 00075 * 00076 * @param i2c The I2C instance pointer 00077 * @param addr The 8bit I2C address of the chip, common range 0xa0 - 0xae. 00078 * @param size The size of the device in bytes 00079 * @param block The page size of the device in bytes, defaults to 32bytes 00080 * @param freq The frequency of the I2C bus, defaults to 400K. 00081 */ 00082 I2CEEBlockDevice( 00083 mbed::I2C *i2c_obj, uint8_t address, 00084 bd_size_t size, bd_size_t block = 32); 00085 00086 /** Destructor of I2CEEBlockDevice 00087 */ 00088 00089 virtual ~I2CEEBlockDevice(); 00090 00091 /** Initialize a block device 00092 * 00093 * @return 0 on success or a negative error code on failure 00094 */ 00095 virtual int init(); 00096 00097 /** Deinitialize a block device 00098 * 00099 * @return 0 on success or a negative error code on failure 00100 */ 00101 virtual int deinit(); 00102 00103 /** Read blocks from a block device 00104 * 00105 * @param buffer Buffer to write blocks to 00106 * @param addr Address of block to begin reading from 00107 * @param size Size to read in bytes, must be a multiple of read block size 00108 * @return 0 on success, negative error code on failure 00109 */ 00110 virtual int read(void *buffer, bd_addr_t addr, bd_size_t size); 00111 00112 /** Program blocks to a block device 00113 * 00114 * The blocks must have been erased prior to being programmed 00115 * 00116 * @param buffer Buffer of data to write to blocks 00117 * @param addr Address of block to begin writing to 00118 * @param size Size to write in bytes, must be a multiple of program block size 00119 * @return 0 on success, negative error code on failure 00120 */ 00121 virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size); 00122 00123 /** Erase blocks on a block device 00124 * 00125 * The state of an erased block is undefined until it has been programmed 00126 * 00127 * @param addr Address of block to begin erasing 00128 * @param size Size to erase in bytes, must be a multiple of erase block size 00129 * @return 0 on success, negative error code on failure 00130 */ 00131 virtual int erase(bd_addr_t addr, bd_size_t size); 00132 00133 /** Get the size of a readable block 00134 * 00135 * @return Size of a readable block in bytes 00136 */ 00137 virtual bd_size_t get_read_size() const; 00138 00139 /** Get the size of a programable block 00140 * 00141 * @return Size of a programable block in bytes 00142 * @note Must be a multiple of the read size 00143 */ 00144 virtual bd_size_t get_program_size() const; 00145 00146 /** Get the size of a eraseable block 00147 * 00148 * @return Size of a eraseable block in bytes 00149 * @note Must be a multiple of the program size 00150 */ 00151 virtual bd_size_t get_erase_size() const; 00152 00153 /** Get the total size of the underlying device 00154 * 00155 * @return Size of the underlying device in bytes 00156 */ 00157 virtual bd_size_t size() const; 00158 00159 /** Get the BlockDevice class type. 00160 * 00161 * @return A string representation of the BlockDevice class type. 00162 */ 00163 virtual const char *get_type() const; 00164 00165 private: 00166 mbed::I2C *_i2c; 00167 uint32_t _i2c_buffer[sizeof(mbed::I2C) / sizeof(uint32_t)]; 00168 uint8_t _i2c_addr; 00169 uint32_t _size; 00170 uint32_t _block; 00171 00172 int _sync(); 00173 }; 00174 00175 00176 #endif /* MBED_SD_BLOCK_DEVICE_H */
Generated on Tue Jul 12 2022 13:54:24 by
1.7.2