Class for making communication easier from code to i2c connected Rohm/Kionix sensors. Maybe could be used later also for abstracting Arduino/mbed os. Code ported from 'C'-library rohm-sensor-hal.
Dependents: kionix-kx123-hello rohm-bh1790glc-hello simple-sensor-client rohm-SensorShield-example
Fork of rohm-sensor-hal by
source/RegisterWriter.cpp@8:dea50f607a45, 2016-09-29 (annotated)
- Committer:
- MikkoZ
- Date:
- Thu Sep 29 15:10:08 2016 +0000
- Revision:
- 8:dea50f607a45
- Child:
- 10:7ac47540f580
Initial version of C++ RegisterWriter. Code ported from 'C'-library rohm-sensor-hal.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MikkoZ | 8:dea50f607a45 | 1 | /* Copyright 2016 Rohm Semiconductor |
MikkoZ | 8:dea50f607a45 | 2 | |
MikkoZ | 8:dea50f607a45 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); |
MikkoZ | 8:dea50f607a45 | 4 | you may not use this file except in compliance with the License. |
MikkoZ | 8:dea50f607a45 | 5 | You may obtain a copy of the License at |
MikkoZ | 8:dea50f607a45 | 6 | |
MikkoZ | 8:dea50f607a45 | 7 | http://www.apache.org/licenses/LICENSE-2.0 |
MikkoZ | 8:dea50f607a45 | 8 | |
MikkoZ | 8:dea50f607a45 | 9 | Unless required by applicable law or agreed to in writing, software |
MikkoZ | 8:dea50f607a45 | 10 | distributed under the License is distributed on an "AS IS" BASIS, |
MikkoZ | 8:dea50f607a45 | 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
MikkoZ | 8:dea50f607a45 | 12 | See the License for the specific language governing permissions and |
MikkoZ | 8:dea50f607a45 | 13 | limitations under the License. |
MikkoZ | 8:dea50f607a45 | 14 | */ |
MikkoZ | 8:dea50f607a45 | 15 | #include "../RegisterWriter/rohm_hal2.h" //types, USE_MBED_HARDWARE_I2C, DEBUG_print*, I2C.h, I2C_SDA, I2C_SCL |
MikkoZ | 8:dea50f607a45 | 16 | |
MikkoZ | 8:dea50f607a45 | 17 | #ifdef USE_MBED_HARDWARE_I2C |
MikkoZ | 8:dea50f607a45 | 18 | #include "../RegisterWriter/RegisterWriter.h" //prototypes |
MikkoZ | 8:dea50f607a45 | 19 | #define I2C_WRITE 0 |
MikkoZ | 8:dea50f607a45 | 20 | #define I2C_READ 1 |
MikkoZ | 8:dea50f607a45 | 21 | |
MikkoZ | 8:dea50f607a45 | 22 | |
MikkoZ | 8:dea50f607a45 | 23 | RegisterWriter::RegisterWriter(I2C &i2c_obj) : i2c_bus(i2c_obj) { |
MikkoZ | 8:dea50f607a45 | 24 | self_created_i2c = false; |
MikkoZ | 8:dea50f607a45 | 25 | } |
MikkoZ | 8:dea50f607a45 | 26 | |
MikkoZ | 8:dea50f607a45 | 27 | RegisterWriter::RegisterWriter(PinName sda, PinName scl) : i2c_bus(sda, scl) { |
MikkoZ | 8:dea50f607a45 | 28 | self_created_i2c = true; |
MikkoZ | 8:dea50f607a45 | 29 | } |
MikkoZ | 8:dea50f607a45 | 30 | |
MikkoZ | 8:dea50f607a45 | 31 | RegisterWriter::~RegisterWriter(){ |
MikkoZ | 8:dea50f607a45 | 32 | if (self_created_i2c == true){ |
MikkoZ | 8:dea50f607a45 | 33 | delete &i2c_bus; |
MikkoZ | 8:dea50f607a45 | 34 | } |
MikkoZ | 8:dea50f607a45 | 35 | } |
MikkoZ | 8:dea50f607a45 | 36 | |
MikkoZ | 8:dea50f607a45 | 37 | /* i2c common functions */ |
MikkoZ | 8:dea50f607a45 | 38 | uint8_t RegisterWriter::read_register(uint8_t sad, uint8_t reg, uint8_t* buf, uint8_t buf_len) { |
MikkoZ | 8:dea50f607a45 | 39 | uint8_t received_bytes; |
MikkoZ | 8:dea50f607a45 | 40 | int read_ok; |
MikkoZ | 8:dea50f607a45 | 41 | |
MikkoZ | 8:dea50f607a45 | 42 | i2c_bus.write( (int)((sad << 1) | I2C_WRITE), (char*)®, (int)1 ); |
MikkoZ | 8:dea50f607a45 | 43 | read_ok = i2c_bus.read( (int)((sad << 1) | I2C_READ), (char*)buf, (int)buf_len); |
MikkoZ | 8:dea50f607a45 | 44 | |
MikkoZ | 8:dea50f607a45 | 45 | if( read_ok == 0 ){ //0 == success(ack) |
MikkoZ | 8:dea50f607a45 | 46 | received_bytes = buf_len; |
MikkoZ | 8:dea50f607a45 | 47 | } |
MikkoZ | 8:dea50f607a45 | 48 | else{ //non0 == fail (nack) |
MikkoZ | 8:dea50f607a45 | 49 | received_bytes = 0; |
MikkoZ | 8:dea50f607a45 | 50 | } |
MikkoZ | 8:dea50f607a45 | 51 | return( received_bytes ); |
MikkoZ | 8:dea50f607a45 | 52 | } |
MikkoZ | 8:dea50f607a45 | 53 | |
MikkoZ | 8:dea50f607a45 | 54 | uint8_t RegisterWriter::read_fifo_register(uint8_t sad, uint8_t reg, uint8_t* buf, uint8_t buf_len) { |
MikkoZ | 8:dea50f607a45 | 55 | uint8_t received_bytes; |
MikkoZ | 8:dea50f607a45 | 56 | int read_ok; |
MikkoZ | 8:dea50f607a45 | 57 | |
MikkoZ | 8:dea50f607a45 | 58 | i2c_bus.write( (int)((sad << 1) | I2C_WRITE), (char*)®, (int)0 ); |
MikkoZ | 8:dea50f607a45 | 59 | read_ok = i2c_bus.read( (int)((sad << 1) | I2C_READ), (char*)buf, (int)buf_len); |
MikkoZ | 8:dea50f607a45 | 60 | |
MikkoZ | 8:dea50f607a45 | 61 | if( read_ok == 0 ){ //0 == success(ack) |
MikkoZ | 8:dea50f607a45 | 62 | received_bytes = buf_len; |
MikkoZ | 8:dea50f607a45 | 63 | } |
MikkoZ | 8:dea50f607a45 | 64 | else{ //non0 == fail (nack) |
MikkoZ | 8:dea50f607a45 | 65 | received_bytes = 0; |
MikkoZ | 8:dea50f607a45 | 66 | } |
MikkoZ | 8:dea50f607a45 | 67 | return( received_bytes ); |
MikkoZ | 8:dea50f607a45 | 68 | } |
MikkoZ | 8:dea50f607a45 | 69 | |
MikkoZ | 8:dea50f607a45 | 70 | void RegisterWriter::write_register(uint8_t sad, uint8_t reg, uint8_t* data, uint8_t data_len) { |
MikkoZ | 8:dea50f607a45 | 71 | i2c_bus.write( (int)((sad << 1) | I2C_WRITE ), (char*)®, (int)1, true); |
MikkoZ | 8:dea50f607a45 | 72 | i2c_bus.write( (int)((sad << 1) | I2C_WRITE ), (char*)data, (int)data_len, false); |
MikkoZ | 8:dea50f607a45 | 73 | } |
MikkoZ | 8:dea50f607a45 | 74 | |
MikkoZ | 8:dea50f607a45 | 75 | void RegisterWriter::write_register(uint8_t sad, uint8_t reg, uint8_t data) { |
MikkoZ | 8:dea50f607a45 | 76 | char data_to_send[2]; |
MikkoZ | 8:dea50f607a45 | 77 | |
MikkoZ | 8:dea50f607a45 | 78 | data_to_send[0] = reg; |
MikkoZ | 8:dea50f607a45 | 79 | data_to_send[1] = data; |
MikkoZ | 8:dea50f607a45 | 80 | i2c_bus.write( (int)((sad << 1) | I2C_WRITE ), &data_to_send[0], 2); |
MikkoZ | 8:dea50f607a45 | 81 | } |
MikkoZ | 8:dea50f607a45 | 82 | |
MikkoZ | 8:dea50f607a45 | 83 | bool RegisterWriter::change_bits(uint8_t sad, uint8_t reg, uint8_t mask, uint8_t bits){ |
MikkoZ | 8:dea50f607a45 | 84 | uint8_t value, read_bytes; |
MikkoZ | 8:dea50f607a45 | 85 | read_bytes = read_register(sad, reg, &value, 1); |
MikkoZ | 8:dea50f607a45 | 86 | if( read_bytes != 0 ){ |
MikkoZ | 8:dea50f607a45 | 87 | value = value & ~mask; |
MikkoZ | 8:dea50f607a45 | 88 | value = value | (bits & mask); |
MikkoZ | 8:dea50f607a45 | 89 | write_register(sad, reg, value); |
MikkoZ | 8:dea50f607a45 | 90 | return true; |
MikkoZ | 8:dea50f607a45 | 91 | } |
MikkoZ | 8:dea50f607a45 | 92 | else{ |
MikkoZ | 8:dea50f607a45 | 93 | //DEBUG_printf("Read before change_bits() failed."); |
MikkoZ | 8:dea50f607a45 | 94 | return false; |
MikkoZ | 8:dea50f607a45 | 95 | } |
MikkoZ | 8:dea50f607a45 | 96 | } |
MikkoZ | 8:dea50f607a45 | 97 | |
MikkoZ | 8:dea50f607a45 | 98 | |
MikkoZ | 8:dea50f607a45 | 99 | #endif |
MikkoZ | 8:dea50f607a45 | 100 | |
MikkoZ | 8:dea50f607a45 | 101 |