Platform HAL for rohm sensor drivers. Used for abstracting Arduino/mbed os/mbed classic.

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

Committer:
MACRUM
Date:
Wed Feb 27 04:43:34 2019 +0000
Revision:
9:aecd1e03720a
Parent:
8:4d5812bed3fb
Fix debug print

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Mikko Koivunen 3:309530db41ec 1 /* Copyright 2016 Rohm Semiconductor
Mikko Koivunen 3:309530db41ec 2
Mikko Koivunen 3:309530db41ec 3 Licensed under the Apache License, Version 2.0 (the "License");
Mikko Koivunen 3:309530db41ec 4 you may not use this file except in compliance with the License.
Mikko Koivunen 3:309530db41ec 5 You may obtain a copy of the License at
Mikko Koivunen 3:309530db41ec 6
Mikko Koivunen 3:309530db41ec 7 http://www.apache.org/licenses/LICENSE-2.0
Mikko Koivunen 3:309530db41ec 8
Mikko Koivunen 3:309530db41ec 9 Unless required by applicable law or agreed to in writing, software
Mikko Koivunen 3:309530db41ec 10 distributed under the License is distributed on an "AS IS" BASIS,
Mikko Koivunen 3:309530db41ec 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Mikko Koivunen 3:309530db41ec 12 See the License for the specific language governing permissions and
Mikko Koivunen 3:309530db41ec 13 limitations under the License.
Mikko Koivunen 3:309530db41ec 14 */
Mikko Koivunen 3:309530db41ec 15 #include "../rohm-sensor-hal/rohm_hal.h" //types, USE_MBED_HARDWARE_I2C, DEBUG_print*, I2C.h, I2C_SDA, I2C_SCL
Mikko Koivunen 3:309530db41ec 16 #ifdef USE_MBED_HARDWARE_I2C
Mikko Koivunen 3:309530db41ec 17 #include "../rohm-sensor-hal/I2CCommon.h" //prototypes
Mikko Koivunen 3:309530db41ec 18 #define I2C_WRITE 0
Mikko Koivunen 3:309530db41ec 19 #define I2C_READ 1
Mikko Koivunen 3:309530db41ec 20 I2C i2c(I2C_SDA, I2C_SCL);
Mikko Koivunen 3:309530db41ec 21
Mikko Koivunen 3:309530db41ec 22
Mikko Koivunen 3:309530db41ec 23 //Note that I2CCommonBegin() must be called before using read/write functions.
Mikko Koivunen 3:309530db41ec 24 bool I2CCommonBegin(){
Mikko Koivunen 3:309530db41ec 25 return( true ); //always succeeds
Mikko Koivunen 3:309530db41ec 26 }
Mikko Koivunen 3:309530db41ec 27
Mikko Koivunen 3:309530db41ec 28 /* i2c common functions */
Mikko Koivunen 3:309530db41ec 29 uint8_t read_register(uint8_t sad, uint8_t reg, uint8_t* buf, uint8_t buf_len) {
Mikko Koivunen 3:309530db41ec 30 uint8_t received_bytes;
Mikko Koivunen 3:309530db41ec 31 int read_ok;
Mikko Koivunen 3:309530db41ec 32
Ren Boting 8:4d5812bed3fb 33 i2c.write( (int)((sad << 1) | I2C_WRITE), (char*)&reg, (int)1, true );
Mikko Koivunen 3:309530db41ec 34 read_ok = i2c.read( (int)((sad << 1) | I2C_READ), (char*)buf, (int)buf_len);
Mikko Koivunen 3:309530db41ec 35
Mikko Koivunen 3:309530db41ec 36 if( read_ok == 0 ){ //0 == success(ack)
Mikko Koivunen 3:309530db41ec 37 received_bytes = buf_len;
Mikko Koivunen 3:309530db41ec 38 }
Mikko Koivunen 3:309530db41ec 39 else{ //non0 == fail (nack)
Mikko Koivunen 3:309530db41ec 40 received_bytes = 0;
Mikko Koivunen 3:309530db41ec 41 }
Mikko Koivunen 3:309530db41ec 42 return( received_bytes );
Mikko Koivunen 3:309530db41ec 43 }
Mikko Koivunen 3:309530db41ec 44
Mikko Koivunen 3:309530db41ec 45 void write_registers(uint8_t sad, uint8_t reg, uint8_t* data, uint8_t data_len) {
Mikko Koivunen 3:309530db41ec 46 i2c.write( (int)((sad << 1) | I2C_WRITE ), (char*)&reg, (int)1, true);
Mikko Koivunen 3:309530db41ec 47 i2c.write( (int)((sad << 1) | I2C_WRITE ), (char*)data, (int)data_len, false);
Mikko Koivunen 3:309530db41ec 48 }
Mikko Koivunen 3:309530db41ec 49
Mikko Koivunen 3:309530db41ec 50 void write_register(uint8_t sad, uint8_t reg, uint8_t data) {
Mikko Koivunen 3:309530db41ec 51 char data_to_send[2];
Mikko Koivunen 3:309530db41ec 52
Mikko Koivunen 3:309530db41ec 53 data_to_send[0] = reg;
Mikko Koivunen 3:309530db41ec 54 data_to_send[1] = data;
Mikko Koivunen 3:309530db41ec 55 i2c.write( (int)((sad << 1) | I2C_WRITE ), &data_to_send[0], 2);
Mikko Koivunen 3:309530db41ec 56 }
Mikko Koivunen 3:309530db41ec 57
Mikko Koivunen 3:309530db41ec 58 bool change_bits(uint8_t sad, uint8_t reg, uint8_t mask, uint8_t bits){
Mikko Koivunen 3:309530db41ec 59 uint8_t value, read_bytes;
Mikko Koivunen 3:309530db41ec 60 read_bytes = read_register(sad, reg, &value, 1);
Mikko Koivunen 3:309530db41ec 61 if( read_bytes != 0 ){
Mikko Koivunen 3:309530db41ec 62 value = value & ~mask;
Mikko Koivunen 3:309530db41ec 63 value = value | (bits & mask);
Mikko Koivunen 3:309530db41ec 64 write_register(sad, reg, value);
Mikko Koivunen 3:309530db41ec 65 return true;
Mikko Koivunen 3:309530db41ec 66 }
Mikko Koivunen 3:309530db41ec 67 else{
Mikko Koivunen 3:309530db41ec 68 //DEBUG_printf("Read before change_bits() failed.");
Mikko Koivunen 3:309530db41ec 69 return false;
Mikko Koivunen 3:309530db41ec 70 }
Mikko Koivunen 3:309530db41ec 71 }
Mikko Koivunen 3:309530db41ec 72
Mikko Koivunen 3:309530db41ec 73 #endif
Mikko Koivunen 3:309530db41ec 74
Mikko Koivunen 3:309530db41ec 75