sdsads

Committer:
MikkoZ
Date:
Tue Apr 12 04:57:59 2016 +0000
Revision:
1:4d6dc19a2347
Parent:
0:38c206f19bb7
Child:
3:309530db41ec
Added support for mbed OS

Added support for mbed OS.
Successfully compiled and tested program for frdm-k64f-gcc/bm1383.

Who changed what in which revision?

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