Fujitsu MB85RCxx I2C FRAM access library
MB85RCxx_I2C.cpp@0:f7119e0aa405, 2017-04-22 (annotated)
- Committer:
- MACRUM
- Date:
- Sat Apr 22 12:14:35 2017 +0000
- Revision:
- 0:f7119e0aa405
Initial commit;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MACRUM | 0:f7119e0aa405 | 1 | /** |
MACRUM | 0:f7119e0aa405 | 2 | ****************************************************************************** |
MACRUM | 0:f7119e0aa405 | 3 | * @file MB85RCxx_I2C.cpp |
MACRUM | 0:f7119e0aa405 | 4 | * @author Toyomasa Watarai |
MACRUM | 0:f7119e0aa405 | 5 | * @version V1.0.0 |
MACRUM | 0:f7119e0aa405 | 6 | * @date 22 April 2017 |
MACRUM | 0:f7119e0aa405 | 7 | * @brief MB85RCxx_I2C class implementation |
MACRUM | 0:f7119e0aa405 | 8 | ****************************************************************************** |
MACRUM | 0:f7119e0aa405 | 9 | * @attention |
MACRUM | 0:f7119e0aa405 | 10 | * |
MACRUM | 0:f7119e0aa405 | 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
MACRUM | 0:f7119e0aa405 | 12 | * of this software and associated documentation files (the "Software"), to deal |
MACRUM | 0:f7119e0aa405 | 13 | * in the Software without restriction, including without limitation the rights |
MACRUM | 0:f7119e0aa405 | 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
MACRUM | 0:f7119e0aa405 | 15 | * copies of the Software, and to permit persons to whom the Software is |
MACRUM | 0:f7119e0aa405 | 16 | * furnished to do so, subject to the following conditions: |
MACRUM | 0:f7119e0aa405 | 17 | * |
MACRUM | 0:f7119e0aa405 | 18 | * The above copyright notice and this permission notice shall be included in |
MACRUM | 0:f7119e0aa405 | 19 | * all copies or substantial portions of the Software. |
MACRUM | 0:f7119e0aa405 | 20 | * |
MACRUM | 0:f7119e0aa405 | 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
MACRUM | 0:f7119e0aa405 | 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
MACRUM | 0:f7119e0aa405 | 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
MACRUM | 0:f7119e0aa405 | 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
MACRUM | 0:f7119e0aa405 | 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
MACRUM | 0:f7119e0aa405 | 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
MACRUM | 0:f7119e0aa405 | 27 | * THE SOFTWARE. |
MACRUM | 0:f7119e0aa405 | 28 | */ |
MACRUM | 0:f7119e0aa405 | 29 | #include "mbed.h" |
MACRUM | 0:f7119e0aa405 | 30 | #include "MB85RCxx_I2C.h" |
MACRUM | 0:f7119e0aa405 | 31 | |
MACRUM | 0:f7119e0aa405 | 32 | MB85RCxx_I2C::MB85RCxx_I2C(PinName sda, PinName scl, char slave_adr) |
MACRUM | 0:f7119e0aa405 | 33 | : |
MACRUM | 0:f7119e0aa405 | 34 | _i2c_p(new I2C(sda, scl)), |
MACRUM | 0:f7119e0aa405 | 35 | _i2c(*_i2c_p), |
MACRUM | 0:f7119e0aa405 | 36 | _address(slave_adr) |
MACRUM | 0:f7119e0aa405 | 37 | { |
MACRUM | 0:f7119e0aa405 | 38 | _i2c.frequency(400000); |
MACRUM | 0:f7119e0aa405 | 39 | } |
MACRUM | 0:f7119e0aa405 | 40 | |
MACRUM | 0:f7119e0aa405 | 41 | MB85RCxx_I2C::~MB85RCxx_I2C() |
MACRUM | 0:f7119e0aa405 | 42 | { |
MACRUM | 0:f7119e0aa405 | 43 | if (NULL != _i2c_p) |
MACRUM | 0:f7119e0aa405 | 44 | delete _i2c_p; |
MACRUM | 0:f7119e0aa405 | 45 | } |
MACRUM | 0:f7119e0aa405 | 46 | |
MACRUM | 0:f7119e0aa405 | 47 | int MB85RCxx_I2C::read_device_id(char* device_id) |
MACRUM | 0:f7119e0aa405 | 48 | { |
MACRUM | 0:f7119e0aa405 | 49 | _i2c.write(0xF8, &_address, 1, true); |
MACRUM | 0:f7119e0aa405 | 50 | _i2c.read(0xF9, device_id, 3); |
MACRUM | 0:f7119e0aa405 | 51 | |
MACRUM | 0:f7119e0aa405 | 52 | return (device_id[1] & 0x0F); |
MACRUM | 0:f7119e0aa405 | 53 | } |
MACRUM | 0:f7119e0aa405 | 54 | |
MACRUM | 0:f7119e0aa405 | 55 | void MB85RCxx_I2C::read(uint32_t address, char *data, uint32_t length) |
MACRUM | 0:f7119e0aa405 | 56 | { |
MACRUM | 0:f7119e0aa405 | 57 | char byte_address[2]; |
MACRUM | 0:f7119e0aa405 | 58 | char i2c_adrs = (_address | ((address >> 15) & 0x02)); |
MACRUM | 0:f7119e0aa405 | 59 | |
MACRUM | 0:f7119e0aa405 | 60 | byte_address[0] = ((address >> 8) & 0xFF); |
MACRUM | 0:f7119e0aa405 | 61 | byte_address[1] = ((address >> 0) & 0xFF); |
MACRUM | 0:f7119e0aa405 | 62 | _i2c.write(i2c_adrs, byte_address, 2, true); |
MACRUM | 0:f7119e0aa405 | 63 | _i2c.read(i2c_adrs, data, length); |
MACRUM | 0:f7119e0aa405 | 64 | } |
MACRUM | 0:f7119e0aa405 | 65 | |
MACRUM | 0:f7119e0aa405 | 66 | void MB85RCxx_I2C::write(uint32_t address, char *data, uint32_t length) |
MACRUM | 0:f7119e0aa405 | 67 | { |
MACRUM | 0:f7119e0aa405 | 68 | char byte_address[2]; |
MACRUM | 0:f7119e0aa405 | 69 | char i2c_adrs = (_address | ((address >> 15) & 0x02)); |
MACRUM | 0:f7119e0aa405 | 70 | |
MACRUM | 0:f7119e0aa405 | 71 | byte_address[0] = ((address >> 8) & 0xFF); |
MACRUM | 0:f7119e0aa405 | 72 | byte_address[1] = ((address >> 0) & 0xFF); |
MACRUM | 0:f7119e0aa405 | 73 | _i2c.write(i2c_adrs, byte_address, 2, true); |
MACRUM | 0:f7119e0aa405 | 74 | for (uint32_t i = 0; i < length; i++) { |
MACRUM | 0:f7119e0aa405 | 75 | _i2c.write(*data++); |
MACRUM | 0:f7119e0aa405 | 76 | } |
MACRUM | 0:f7119e0aa405 | 77 | } |
MACRUM | 0:f7119e0aa405 | 78 | |
MACRUM | 0:f7119e0aa405 | 79 | void MB85RCxx_I2C::write(uint32_t address, char data) |
MACRUM | 0:f7119e0aa405 | 80 | { |
MACRUM | 0:f7119e0aa405 | 81 | char byte_address[2]; |
MACRUM | 0:f7119e0aa405 | 82 | char i2c_adrs = (_address | ((address >> 15) & 0x02)); |
MACRUM | 0:f7119e0aa405 | 83 | |
MACRUM | 0:f7119e0aa405 | 84 | byte_address[0] = ((address >> 8) & 0xFF); |
MACRUM | 0:f7119e0aa405 | 85 | byte_address[1] = ((address >> 0) & 0xFF); |
MACRUM | 0:f7119e0aa405 | 86 | _i2c.write(i2c_adrs, byte_address, 2, true); |
MACRUM | 0:f7119e0aa405 | 87 | _i2c.write(data); |
MACRUM | 0:f7119e0aa405 | 88 | } |
MACRUM | 0:f7119e0aa405 | 89 | |
MACRUM | 0:f7119e0aa405 | 90 | void MB85RCxx_I2C::fill(uint32_t address, uint8_t data, uint32_t length) |
MACRUM | 0:f7119e0aa405 | 91 | { |
MACRUM | 0:f7119e0aa405 | 92 | char byte_address[2]; |
MACRUM | 0:f7119e0aa405 | 93 | char i2c_adrs = (_address | ((address >> 15) & 0x02)); |
MACRUM | 0:f7119e0aa405 | 94 | |
MACRUM | 0:f7119e0aa405 | 95 | byte_address[0] = ((address >> 8) & 0xFF); |
MACRUM | 0:f7119e0aa405 | 96 | byte_address[1] = ((address >> 0) & 0xFF); |
MACRUM | 0:f7119e0aa405 | 97 | _i2c.write(i2c_adrs, byte_address, 2, true); |
MACRUM | 0:f7119e0aa405 | 98 | for (uint32_t i = 0; i < length; i++) { |
MACRUM | 0:f7119e0aa405 | 99 | _i2c.write(data); |
MACRUM | 0:f7119e0aa405 | 100 | } |
MACRUM | 0:f7119e0aa405 | 101 | } |