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@12:bc2446aabbfe, 2016-10-06 (annotated)
- Committer:
- MikkoZ
- Date:
- Thu Oct 06 10:33:05 2016 +0000
- Revision:
- 12:bc2446aabbfe
- Parent:
- 11:272713c9e118
- Child:
- 13:3d4508874121
Change_bits bugfix and highspeed functions.; ; - change_bit returnvalue inverted; - write return values added; - New functions for setting up highspeed 3.4MHz mode; ; Compiles, but not tested yet beyond basic read/write/change.
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 | 11:272713c9e118 | 58 | i2c_bus.write( (int)((sad << 1) | I2C_WRITE), (char*)®, (int)1, true ); |
| 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 | 12:bc2446aabbfe | 70 | uint8_t RegisterWriter::hs_read_register(uint8_t sad, uint8_t reg, uint8_t* buf, uint8_t buf_len) { |
| MikkoZ | 12:bc2446aabbfe | 71 | set_hs_mode_for_one_command(); |
| MikkoZ | 12:bc2446aabbfe | 72 | //Next read command as usual, but in highspeed |
| MikkoZ | 12:bc2446aabbfe | 73 | return read_fifo_register(sad, reg, buf, buf_len); |
| MikkoZ | 8:dea50f607a45 | 74 | } |
| MikkoZ | 8:dea50f607a45 | 75 | |
| MikkoZ | 12:bc2446aabbfe | 76 | bool RegisterWriter::write_register(uint8_t sad, uint8_t reg, uint8_t* data, uint8_t data_len) { |
| MikkoZ | 12:bc2446aabbfe | 77 | bool error; |
| MikkoZ | 12:bc2446aabbfe | 78 | error = i2c_bus.write( (int)((sad << 1) | I2C_WRITE ), (char*)®, (int)1, true); |
| MikkoZ | 12:bc2446aabbfe | 79 | error = error || i2c_bus.write( (int)((sad << 1) | I2C_WRITE ), (char*)data, (int)data_len, false); |
| MikkoZ | 12:bc2446aabbfe | 80 | return error; |
| MikkoZ | 12:bc2446aabbfe | 81 | } |
| MikkoZ | 12:bc2446aabbfe | 82 | |
| MikkoZ | 12:bc2446aabbfe | 83 | bool RegisterWriter::write_register(uint8_t sad, uint8_t reg, uint8_t data) { |
| MikkoZ | 8:dea50f607a45 | 84 | char data_to_send[2]; |
| MikkoZ | 12:bc2446aabbfe | 85 | bool error; |
| MikkoZ | 8:dea50f607a45 | 86 | |
| MikkoZ | 8:dea50f607a45 | 87 | data_to_send[0] = reg; |
| MikkoZ | 8:dea50f607a45 | 88 | data_to_send[1] = data; |
| MikkoZ | 12:bc2446aabbfe | 89 | error = i2c_bus.write( (int)((sad << 1) | I2C_WRITE ), &data_to_send[0], 2); |
| MikkoZ | 12:bc2446aabbfe | 90 | |
| MikkoZ | 12:bc2446aabbfe | 91 | return error; |
| MikkoZ | 8:dea50f607a45 | 92 | } |
| MikkoZ | 8:dea50f607a45 | 93 | |
| MikkoZ | 8:dea50f607a45 | 94 | bool RegisterWriter::change_bits(uint8_t sad, uint8_t reg, uint8_t mask, uint8_t bits){ |
| MikkoZ | 8:dea50f607a45 | 95 | uint8_t value, read_bytes; |
| MikkoZ | 12:bc2446aabbfe | 96 | bool error; |
| MikkoZ | 8:dea50f607a45 | 97 | read_bytes = read_register(sad, reg, &value, 1); |
| MikkoZ | 8:dea50f607a45 | 98 | if( read_bytes != 0 ){ |
| MikkoZ | 8:dea50f607a45 | 99 | value = value & ~mask; |
| MikkoZ | 8:dea50f607a45 | 100 | value = value | (bits & mask); |
| MikkoZ | 12:bc2446aabbfe | 101 | return write_register(sad, reg, value); |
| MikkoZ | 8:dea50f607a45 | 102 | } |
| MikkoZ | 8:dea50f607a45 | 103 | else{ |
| MikkoZ | 8:dea50f607a45 | 104 | //DEBUG_printf("Read before change_bits() failed."); |
| MikkoZ | 12:bc2446aabbfe | 105 | return true; |
| MikkoZ | 8:dea50f607a45 | 106 | } |
| MikkoZ | 8:dea50f607a45 | 107 | } |
| MikkoZ | 8:dea50f607a45 | 108 | |
| MikkoZ | 12:bc2446aabbfe | 109 | void RegisterWriter::set_hs_mode_for_one_command(){ |
| MikkoZ | 12:bc2446aabbfe | 110 | #define MCODE (1<<3) |
| MikkoZ | 12:bc2446aabbfe | 111 | char temp; |
| MikkoZ | 12:bc2446aabbfe | 112 | //Fullspeed mode -> highspeed mode for one command. |
| MikkoZ | 12:bc2446aabbfe | 113 | i2c_bus.write( (int)(MCODE), (char*)&temp, (int)0, true ); //Trick to write just mcode+make nack. |
| MikkoZ | 12:bc2446aabbfe | 114 | } |
| MikkoZ | 12:bc2446aabbfe | 115 | |
| MikkoZ | 8:dea50f607a45 | 116 | |
| MikkoZ | 8:dea50f607a45 | 117 | #endif |
| MikkoZ | 8:dea50f607a45 | 118 | |
| MikkoZ | 8:dea50f607a45 | 119 |
