Rohm / rohm-sensor-hal

Dependents:   rohm-bm1383-hello-mbedclassic rohm-bh1726-hello rohm-rpr0521-hello rohm-bh1745-hello ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers I2CCommonMbedHardwareLib.cpp Source File

I2CCommonMbedHardwareLib.cpp

00001 /*   Copyright 2016 Rohm Semiconductor
00002 
00003    Licensed under the Apache License, Version 2.0 (the "License");
00004    you may not use this file except in compliance with the License.
00005    You may obtain a copy of the License at
00006 
00007        http://www.apache.org/licenses/LICENSE-2.0
00008 
00009    Unless required by applicable law or agreed to in writing, software
00010    distributed under the License is distributed on an "AS IS" BASIS,
00011    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012    See the License for the specific language governing permissions and
00013    limitations under the License.
00014 */
00015 #include "../rohm-sensor-hal/rohm_hal.h"           //types, USE_MBED_HARDWARE_I2C, DEBUG_print*, I2C.h, I2C_SDA, I2C_SCL
00016 #ifdef USE_MBED_HARDWARE_I2C
00017 #include "../rohm-sensor-hal/I2CCommon.h"          //prototypes
00018 #define I2C_WRITE 0
00019 #define I2C_READ  1
00020 I2C i2c(I2C_SDA, I2C_SCL);
00021 
00022 
00023 //Note that I2CCommonBegin() must be called before using read/write functions.
00024 bool I2CCommonBegin(){
00025     return( true );        //always succeeds
00026 }
00027 
00028 /* i2c common functions */
00029 uint8_t read_register(uint8_t sad, uint8_t reg, uint8_t* buf, uint8_t buf_len) {
00030     uint8_t received_bytes;
00031     int read_ok;
00032 
00033     i2c.write( (int)((sad << 1) | I2C_WRITE), (char*)&reg, (int)1, true );
00034     read_ok = i2c.read( (int)((sad << 1) | I2C_READ), (char*)buf, (int)buf_len);
00035 
00036     if( read_ok == 0 ){     //0 == success(ack)
00037         received_bytes = buf_len;
00038         }
00039     else{                   //non0 == fail (nack)
00040         received_bytes = 0;
00041         }
00042     return( received_bytes );
00043 }
00044 
00045 void write_registers(uint8_t sad, uint8_t reg, uint8_t* data, uint8_t data_len) {
00046     i2c.write( (int)((sad << 1) | I2C_WRITE ), (char*)&reg, (int)1, true);
00047     i2c.write( (int)((sad << 1) | I2C_WRITE ), (char*)data, (int)data_len, false);
00048 }
00049 
00050 void write_register(uint8_t sad, uint8_t reg, uint8_t data) {
00051     char data_to_send[2];
00052 
00053     data_to_send[0] = reg;
00054     data_to_send[1] = data;
00055     i2c.write( (int)((sad << 1) | I2C_WRITE ), &data_to_send[0], 2);
00056 }
00057 
00058 bool change_bits(uint8_t sad, uint8_t reg, uint8_t mask, uint8_t bits){
00059     uint8_t value, read_bytes;
00060     read_bytes = read_register(sad, reg, &value, 1);
00061     if( read_bytes != 0 ){
00062         value = value & ~mask;
00063         value = value | (bits & mask);
00064         write_register(sad, reg, value);
00065         return true;
00066         }
00067     else{
00068         //DEBUG_printf("Read before change_bits() failed.");
00069         return false;
00070         }
00071 }
00072 
00073 #endif
00074 
00075