x
Embed:
(wiki syntax)
Show/hide line numbers
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 <mbed.h> 00020 #include "BlockDevice.h" 00021 00022 00023 /** BlockDevice for I2C based flash device such as 00024 * Microchip's 24LC or ATMEL's AT24C ranges 00025 * 00026 * @code 00027 * // Here's an example using a 24LC256 on a GR PEACH 00028 * #include "mbed.h" 00029 * #include "I2CEEBlockDevice.h" 00030 * 00031 * // Create EEPROM device on I2C bus with 32kbytes of memory 00032 * I2CEEBlockDevice i2cee(D14, D15, 0xa0, 32*1024); 00033 * 00034 * int main() { 00035 * printf("i2cee test\n"); 00036 * 00037 * // Initialize the device and print the memory layout 00038 * i2cee.init(); 00039 * printf("i2cee size: %llu\n", i2cee.size()); 00040 * printf("i2cee read size: %llu\n", i2cee.get_read_size()); 00041 * printf("i2cee program size: %llu\n", i2cee.get_program_size()); 00042 * printf("i2cee erase size: %llu\n", i2cee.get_erase_size()); 00043 * 00044 * // Write "Hello World!" to the first block 00045 * char *buffer = (char*)malloc(i2cee.get_erase_size()); 00046 * sprintf(buffer, "Hello World!\n"); 00047 * i2cee.erase(0, i2cee.get_erase_size()); 00048 * i2cee.program(buffer, 0, i2cee.get_erase_size()); 00049 * 00050 * // Read back what was stored 00051 * i2cee.read(buffer, 0, i2cee.get_erase_size()); 00052 * printf("%s", buffer); 00053 * 00054 * // Deinitialize the device 00055 * i2cee.deinit(); 00056 * } 00057 * @endcode 00058 */ 00059 class I2CEEBlockDevice : public BlockDevice { 00060 public: 00061 /** Constructor to create an I2CEEBlockDevice on I2C pins 00062 * 00063 * @param sda The pin name for the sda line of the I2C bus. 00064 * @param scl The pin name for the scl line of the I2C bus. 00065 * @param addr The 8bit I2C address of the chip, common range 0xa0 - 0xae. 00066 * @param size The size of the device in bytes 00067 * @param block The page size of the device in bytes, defaults to 32bytes 00068 * @param freq The frequency of the I2C bus, defaults to 400K. 00069 */ 00070 I2CEEBlockDevice( 00071 PinName sda, PinName scl, uint8_t address, 00072 bd_size_t size, bd_size_t block=32, 00073 int bus_speed=400000); 00074 00075 /** Initialize a block device 00076 * 00077 * @return 0 on success or a negative error code on failure 00078 */ 00079 virtual int init(); 00080 00081 /** Deinitialize a block device 00082 * 00083 * @return 0 on success or a negative error code on failure 00084 */ 00085 virtual int deinit(); 00086 00087 /** Read blocks from a block device 00088 * 00089 * @param buffer Buffer to write blocks to 00090 * @param addr Address of block to begin reading from 00091 * @param size Size to read in bytes, must be a multiple of read block size 00092 * @return 0 on success, negative error code on failure 00093 */ 00094 virtual int read(void *buffer, bd_addr_t addr, bd_size_t size); 00095 00096 /** Program blocks to a block device 00097 * 00098 * The blocks must have been erased prior to being programmed 00099 * 00100 * @param buffer Buffer of data to write to blocks 00101 * @param addr Address of block to begin writing to 00102 * @param size Size to write in bytes, must be a multiple of program block size 00103 * @return 0 on success, negative error code on failure 00104 */ 00105 virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size); 00106 00107 /** Erase blocks on a block device 00108 * 00109 * The state of an erased block is undefined until it has been programmed 00110 * 00111 * @param addr Address of block to begin erasing 00112 * @param size Size to erase in bytes, must be a multiple of erase block size 00113 * @return 0 on success, negative error code on failure 00114 */ 00115 virtual int erase(bd_addr_t addr, bd_size_t size); 00116 00117 /** Get the size of a readable block 00118 * 00119 * @return Size of a readable block in bytes 00120 */ 00121 virtual bd_size_t get_read_size() const; 00122 00123 /** Get the size of a programable block 00124 * 00125 * @return Size of a programable block in bytes 00126 * @note Must be a multiple of the read size 00127 */ 00128 virtual bd_size_t get_program_size() const; 00129 00130 /** Get the size of a eraseable block 00131 * 00132 * @return Size of a eraseable block in bytes 00133 * @note Must be a multiple of the program size 00134 */ 00135 virtual bd_size_t get_erase_size() const; 00136 00137 /** Get the total size of the underlying device 00138 * 00139 * @return Size of the underlying device in bytes 00140 */ 00141 virtual bd_size_t size() const; 00142 00143 private: 00144 I2C _i2c; 00145 uint8_t _i2c_addr; 00146 uint32_t _size; 00147 uint32_t _block; 00148 00149 int _sync(); 00150 }; 00151 00152 00153 #endif /* MBED_I2CEEPROM_BLOCK_DEVICE_H */
Generated on Tue Jul 12 2022 20:17:22 by
