sdsads

Committer:
MikkoZ
Date:
Mon Apr 11 08:26:39 2016 +0000
Revision:
0:38c206f19bb7
Child:
1:4d6dc19a2347
Child:
10:37b8f497b698
Added initial version of i2C HAL

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 0:38c206f19bb7 15 #include "../rohm-sensor-hal/rohm_hal.h" //types, USE_MBED_HARDWARE_I2C, DEBUG_print*, 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 #include "I2C.h" //I2C
MikkoZ 0:38c206f19bb7 19 #define I2C_WRITE 0
MikkoZ 0:38c206f19bb7 20 #define I2C_READ 1
MikkoZ 0:38c206f19bb7 21 I2C i2c(I2C_SDA, I2C_SCL);
MikkoZ 0:38c206f19bb7 22
MikkoZ 0:38c206f19bb7 23
MikkoZ 0:38c206f19bb7 24 //Note that I2CCommonBegin() must be called before using read/write functions.
MikkoZ 0:38c206f19bb7 25 bool I2CCommonBegin(){
MikkoZ 0:38c206f19bb7 26 return( true ); //always succeeds
MikkoZ 0:38c206f19bb7 27 }
MikkoZ 0:38c206f19bb7 28
MikkoZ 0:38c206f19bb7 29 /* i2c common functions */
MikkoZ 0:38c206f19bb7 30 uint8_t read_register(uint8_t sad, uint8_t reg, uint8_t* buf, uint8_t buf_len) {
MikkoZ 0:38c206f19bb7 31 uint8_t received_bytes;
MikkoZ 0:38c206f19bb7 32 int read_ok;
MikkoZ 0:38c206f19bb7 33
MikkoZ 0:38c206f19bb7 34 i2c.write( (int)((sad << 1) | I2C_WRITE), (char*)&reg, (int)1 );
MikkoZ 0:38c206f19bb7 35 read_ok = i2c.read( (int)((sad << 1) | I2C_READ), (char*)buf, (int)buf_len);
MikkoZ 0:38c206f19bb7 36
MikkoZ 0:38c206f19bb7 37 if( read_ok == 0 ){ //0 == success(ack)
MikkoZ 0:38c206f19bb7 38 received_bytes = buf_len;
MikkoZ 0:38c206f19bb7 39 }
MikkoZ 0:38c206f19bb7 40 else{ //non0 == fail (nack)
MikkoZ 0:38c206f19bb7 41 received_bytes = 0;
MikkoZ 0:38c206f19bb7 42 }
MikkoZ 0:38c206f19bb7 43 return( received_bytes );
MikkoZ 0:38c206f19bb7 44 }
MikkoZ 0:38c206f19bb7 45
MikkoZ 0:38c206f19bb7 46 void write_registers(uint8_t sad, uint8_t reg, uint8_t* data, uint8_t data_len) {
MikkoZ 0:38c206f19bb7 47 i2c.write( (int)((sad << 1) | I2C_WRITE ), (char*)&reg, (int)1, true);
MikkoZ 0:38c206f19bb7 48 i2c.write( (int)((sad << 1) | I2C_WRITE ), (char*)data, (int)data_len, false);
MikkoZ 0:38c206f19bb7 49 }
MikkoZ 0:38c206f19bb7 50
MikkoZ 0:38c206f19bb7 51 void write_register(uint8_t sad, uint8_t reg, uint8_t data) {
MikkoZ 0:38c206f19bb7 52 char data_to_send[2];
MikkoZ 0:38c206f19bb7 53
MikkoZ 0:38c206f19bb7 54 data_to_send[0] = reg;
MikkoZ 0:38c206f19bb7 55 data_to_send[1] = data;
MikkoZ 0:38c206f19bb7 56 i2c.write( (int)((sad << 1) | I2C_WRITE ), &data_to_send[0], 2);
MikkoZ 0:38c206f19bb7 57 }
MikkoZ 0:38c206f19bb7 58
MikkoZ 0:38c206f19bb7 59 bool change_bits(uint8_t sad, uint8_t reg, uint8_t mask, uint8_t bits){
MikkoZ 0:38c206f19bb7 60 uint8_t value, read_bytes;
MikkoZ 0:38c206f19bb7 61 read_bytes = read_register(sad, reg, &value, 1);
MikkoZ 0:38c206f19bb7 62 if( read_bytes != 0 ){
MikkoZ 0:38c206f19bb7 63 value = value & ~mask;
MikkoZ 0:38c206f19bb7 64 value = value | (bits & mask);
MikkoZ 0:38c206f19bb7 65 write_register(sad, reg, value);
MikkoZ 0:38c206f19bb7 66 return true;
MikkoZ 0:38c206f19bb7 67 }
MikkoZ 0:38c206f19bb7 68 else{
MikkoZ 0:38c206f19bb7 69 //DEBUG_printf("Read before change_bits() failed.");
MikkoZ 0:38c206f19bb7 70 return false;
MikkoZ 0:38c206f19bb7 71 }
MikkoZ 0:38c206f19bb7 72 }
MikkoZ 0:38c206f19bb7 73
MikkoZ 0:38c206f19bb7 74 #endif
MikkoZ 0:38c206f19bb7 75
MikkoZ 0:38c206f19bb7 76