Toyomasa Watarai / MB85RCxx_I2C

Dependents:   MB85RCxx_hello

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MB85RCxx_I2C.cpp Source File

MB85RCxx_I2C.cpp

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    MB85RCxx_I2C.cpp
00004  * @author  Toyomasa Watarai
00005  * @version V1.0.0
00006  * @date    22 April 2017
00007  * @brief   MB85RCxx_I2C class implementation
00008  ******************************************************************************
00009  * @attention
00010  *
00011  * Permission is hereby granted, free of charge, to any person obtaining a copy
00012  * of this software and associated documentation files (the "Software"), to deal
00013  * in the Software without restriction, including without limitation the rights
00014  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00015  * copies of the Software, and to permit persons to whom the Software is
00016  * furnished to do so, subject to the following conditions:
00017  *
00018  * The above copyright notice and this permission notice shall be included in
00019  * all copies or substantial portions of the Software.
00020  *
00021  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00022  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00023  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00024  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00025  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00026  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00027  * THE SOFTWARE.
00028  */
00029 #include "mbed.h"
00030 #include "MB85RCxx_I2C.h"
00031 
00032 MB85RCxx_I2C::MB85RCxx_I2C(PinName sda, PinName scl, char slave_adr)
00033     :
00034     _i2c_p(new I2C(sda, scl)), 
00035     _i2c(*_i2c_p),
00036     _address(slave_adr)
00037 {
00038     _i2c.frequency(400000);
00039 }
00040 
00041 MB85RCxx_I2C::~MB85RCxx_I2C()
00042 {
00043     if (NULL != _i2c_p)
00044         delete  _i2c_p;
00045 }
00046 
00047 int MB85RCxx_I2C::read_device_id(char* device_id)
00048 {
00049     _i2c.write(0xF8, &_address, 1, true);
00050     _i2c.read(0xF9, device_id, 3);
00051     
00052     return (device_id[1] & 0x0F);
00053 }
00054 
00055 void MB85RCxx_I2C::read(uint32_t address, char *data, uint32_t length)
00056 {
00057     char byte_address[2];
00058     char i2c_adrs = (_address | ((address >> 15) & 0x02));
00059     
00060     byte_address[0] = ((address >> 8) & 0xFF);
00061     byte_address[1] = ((address >> 0) & 0xFF);
00062     _i2c.write(i2c_adrs, byte_address, 2, true);
00063     _i2c.read(i2c_adrs, data, length);
00064 }
00065 
00066 void MB85RCxx_I2C::write(uint32_t address, char *data, uint32_t length)
00067 {
00068     char byte_address[2];
00069     char i2c_adrs = (_address | ((address >> 15) & 0x02));
00070     
00071     byte_address[0] = ((address >> 8) & 0xFF);
00072     byte_address[1] = ((address >> 0) & 0xFF);
00073     _i2c.write(i2c_adrs, byte_address, 2, true);
00074     for (uint32_t i = 0; i < length; i++) {
00075         _i2c.write(*data++);
00076     }
00077 }
00078 
00079 void MB85RCxx_I2C::write(uint32_t address, char data)
00080 {
00081     char byte_address[2];
00082     char i2c_adrs = (_address | ((address >> 15) & 0x02));
00083     
00084     byte_address[0] = ((address >> 8) & 0xFF);
00085     byte_address[1] = ((address >> 0) & 0xFF);
00086     _i2c.write(i2c_adrs, byte_address, 2, true);
00087     _i2c.write(data);
00088 }
00089 
00090 void MB85RCxx_I2C::fill(uint32_t address, uint8_t data, uint32_t length)
00091 {
00092     char byte_address[2];
00093     char i2c_adrs = (_address | ((address >> 15) & 0x02));
00094     
00095     byte_address[0] = ((address >> 8) & 0xFF);
00096     byte_address[1] = ((address >> 0) & 0xFF);
00097     _i2c.write(i2c_adrs, byte_address, 2, true);
00098     for (uint32_t i = 0; i < length; i++) {
00099         _i2c.write(data);
00100     }
00101 }