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@13:3d4508874121, 2017-06-16 (annotated)
- Committer:
- MikkoZ
- Date:
- Fri Jun 16 11:54:33 2017 +0000
- Revision:
- 13:3d4508874121
- Parent:
- 12:bc2446aabbfe
- Child:
- 14:f33e0914ea36
Changes to make bh1790 working.; ; Changed return values and added two separate write commands to choose from. Not tested on other drivers (kx123), so don't update yet if you are not planning to make fixes.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MikkoZ | 13:3d4508874121 | 1 | /* |
MikkoZ | 13:3d4508874121 | 2 | The MIT License (MIT) |
MikkoZ | 13:3d4508874121 | 3 | Copyright (c) 2017 Rohm Semiconductor |
MikkoZ | 8:dea50f607a45 | 4 | |
MikkoZ | 13:3d4508874121 | 5 | Permission is hereby granted, free of charge, to any person obtaining a |
MikkoZ | 13:3d4508874121 | 6 | copy of this software and associated documentation files (the |
MikkoZ | 13:3d4508874121 | 7 | "Software"), to deal in the Software without restriction, including |
MikkoZ | 13:3d4508874121 | 8 | without limitation the rights to use, copy, modify, merge, publish, |
MikkoZ | 13:3d4508874121 | 9 | distribute, sublicense, and/or sell copies of the Software, and to |
MikkoZ | 13:3d4508874121 | 10 | permit persons to whom the Software is furnished to do so, subject to |
MikkoZ | 13:3d4508874121 | 11 | the following conditions: |
MikkoZ | 8:dea50f607a45 | 12 | |
MikkoZ | 13:3d4508874121 | 13 | The above copyright notice and this permission notice shall be included |
MikkoZ | 13:3d4508874121 | 14 | in all copies or substantial portions of the Software. |
MikkoZ | 8:dea50f607a45 | 15 | |
MikkoZ | 13:3d4508874121 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
MikkoZ | 13:3d4508874121 | 17 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
MikkoZ | 13:3d4508874121 | 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
MikkoZ | 13:3d4508874121 | 19 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
MikkoZ | 13:3d4508874121 | 20 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
MikkoZ | 13:3d4508874121 | 21 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
MikkoZ | 13:3d4508874121 | 22 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
MikkoZ | 8:dea50f607a45 | 23 | */ |
MikkoZ | 13:3d4508874121 | 24 | |
MikkoZ | 8:dea50f607a45 | 25 | #include "../RegisterWriter/rohm_hal2.h" //types, USE_MBED_HARDWARE_I2C, DEBUG_print*, I2C.h, I2C_SDA, I2C_SCL |
MikkoZ | 8:dea50f607a45 | 26 | |
MikkoZ | 8:dea50f607a45 | 27 | #ifdef USE_MBED_HARDWARE_I2C |
MikkoZ | 8:dea50f607a45 | 28 | #include "../RegisterWriter/RegisterWriter.h" //prototypes |
MikkoZ | 8:dea50f607a45 | 29 | #define I2C_WRITE 0 |
MikkoZ | 8:dea50f607a45 | 30 | #define I2C_READ 1 |
MikkoZ | 8:dea50f607a45 | 31 | |
MikkoZ | 8:dea50f607a45 | 32 | |
MikkoZ | 8:dea50f607a45 | 33 | RegisterWriter::RegisterWriter(I2C &i2c_obj) : i2c_bus(i2c_obj) { |
MikkoZ | 8:dea50f607a45 | 34 | self_created_i2c = false; |
MikkoZ | 13:3d4508874121 | 35 | write_single = true; |
MikkoZ | 8:dea50f607a45 | 36 | } |
MikkoZ | 8:dea50f607a45 | 37 | |
MikkoZ | 8:dea50f607a45 | 38 | RegisterWriter::RegisterWriter(PinName sda, PinName scl) : i2c_bus(sda, scl) { |
MikkoZ | 8:dea50f607a45 | 39 | self_created_i2c = true; |
MikkoZ | 13:3d4508874121 | 40 | write_single = true; |
MikkoZ | 8:dea50f607a45 | 41 | } |
MikkoZ | 8:dea50f607a45 | 42 | |
MikkoZ | 8:dea50f607a45 | 43 | RegisterWriter::~RegisterWriter(){ |
MikkoZ | 8:dea50f607a45 | 44 | if (self_created_i2c == true){ |
MikkoZ | 8:dea50f607a45 | 45 | delete &i2c_bus; |
MikkoZ | 8:dea50f607a45 | 46 | } |
MikkoZ | 8:dea50f607a45 | 47 | } |
MikkoZ | 8:dea50f607a45 | 48 | |
MikkoZ | 8:dea50f607a45 | 49 | /* i2c common functions */ |
MikkoZ | 8:dea50f607a45 | 50 | uint8_t RegisterWriter::read_register(uint8_t sad, uint8_t reg, uint8_t* buf, uint8_t buf_len) { |
MikkoZ | 13:3d4508874121 | 51 | int error; |
MikkoZ | 8:dea50f607a45 | 52 | |
MikkoZ | 8:dea50f607a45 | 53 | i2c_bus.write( (int)((sad << 1) | I2C_WRITE), (char*)®, (int)1 ); |
MikkoZ | 13:3d4508874121 | 54 | error = i2c_bus.read( (int)((sad << 1) | I2C_READ), (char*)buf, (int)buf_len); |
MikkoZ | 13:3d4508874121 | 55 | return( error ); |
MikkoZ | 8:dea50f607a45 | 56 | } |
MikkoZ | 8:dea50f607a45 | 57 | |
MikkoZ | 8:dea50f607a45 | 58 | uint8_t RegisterWriter::read_fifo_register(uint8_t sad, uint8_t reg, uint8_t* buf, uint8_t buf_len) { |
MikkoZ | 8:dea50f607a45 | 59 | uint8_t received_bytes; |
MikkoZ | 8:dea50f607a45 | 60 | int read_ok; |
MikkoZ | 8:dea50f607a45 | 61 | |
MikkoZ | 11:272713c9e118 | 62 | i2c_bus.write( (int)((sad << 1) | I2C_WRITE), (char*)®, (int)1, true ); |
MikkoZ | 8:dea50f607a45 | 63 | read_ok = i2c_bus.read( (int)((sad << 1) | I2C_READ), (char*)buf, (int)buf_len); |
MikkoZ | 8:dea50f607a45 | 64 | |
MikkoZ | 8:dea50f607a45 | 65 | if( read_ok == 0 ){ //0 == success(ack) |
MikkoZ | 8:dea50f607a45 | 66 | received_bytes = buf_len; |
MikkoZ | 8:dea50f607a45 | 67 | } |
MikkoZ | 8:dea50f607a45 | 68 | else{ //non0 == fail (nack) |
MikkoZ | 8:dea50f607a45 | 69 | received_bytes = 0; |
MikkoZ | 8:dea50f607a45 | 70 | } |
MikkoZ | 8:dea50f607a45 | 71 | return( received_bytes ); |
MikkoZ | 8:dea50f607a45 | 72 | } |
MikkoZ | 8:dea50f607a45 | 73 | |
MikkoZ | 12:bc2446aabbfe | 74 | uint8_t RegisterWriter::hs_read_register(uint8_t sad, uint8_t reg, uint8_t* buf, uint8_t buf_len) { |
MikkoZ | 12:bc2446aabbfe | 75 | set_hs_mode_for_one_command(); |
MikkoZ | 12:bc2446aabbfe | 76 | //Next read command as usual, but in highspeed |
MikkoZ | 12:bc2446aabbfe | 77 | return read_fifo_register(sad, reg, buf, buf_len); |
MikkoZ | 8:dea50f607a45 | 78 | } |
MikkoZ | 8:dea50f607a45 | 79 | |
MikkoZ | 13:3d4508874121 | 80 | /** Write data to register. */ |
MikkoZ | 12:bc2446aabbfe | 81 | bool RegisterWriter::write_register(uint8_t sad, uint8_t reg, uint8_t* data, uint8_t data_len) { |
MikkoZ | 13:3d4508874121 | 82 | if (write_single) |
MikkoZ | 13:3d4508874121 | 83 | return write_register_single(sad, reg, data, data_len); |
MikkoZ | 13:3d4508874121 | 84 | return write_register_separate(sad, reg, data, data_len); |
MikkoZ | 13:3d4508874121 | 85 | } |
MikkoZ | 13:3d4508874121 | 86 | |
MikkoZ | 13:3d4508874121 | 87 | /** Write register with single write. */ |
MikkoZ | 13:3d4508874121 | 88 | bool RegisterWriter::write_register_single(uint8_t sad, uint8_t reg, uint8_t* data, uint8_t data_len) { |
MikkoZ | 12:bc2446aabbfe | 89 | bool error; |
MikkoZ | 13:3d4508874121 | 90 | |
MikkoZ | 13:3d4508874121 | 91 | char cmd[data_len+1]; |
MikkoZ | 13:3d4508874121 | 92 | |
MikkoZ | 13:3d4508874121 | 93 | cmd[0] = reg; |
MikkoZ | 13:3d4508874121 | 94 | for (int i = 0; i < data_len; i++) { |
MikkoZ | 13:3d4508874121 | 95 | cmd[i+1] = data[i]; |
MikkoZ | 13:3d4508874121 | 96 | } |
MikkoZ | 13:3d4508874121 | 97 | error = i2c_bus.write( (int)((sad << 1) | I2C_WRITE ), cmd, data_len+1, true); |
MikkoZ | 13:3d4508874121 | 98 | return error; |
MikkoZ | 13:3d4508874121 | 99 | } |
MikkoZ | 13:3d4508874121 | 100 | |
MikkoZ | 13:3d4508874121 | 101 | /** Write register with two separate writes. |
MikkoZ | 13:3d4508874121 | 102 | * First write register address and then continue with data. Send stop only after second write. |
MikkoZ | 13:3d4508874121 | 103 | */ |
MikkoZ | 13:3d4508874121 | 104 | bool RegisterWriter::write_register_separate(uint8_t sad, uint8_t reg, uint8_t* data, uint8_t data_len) { |
MikkoZ | 13:3d4508874121 | 105 | bool error; |
MikkoZ | 13:3d4508874121 | 106 | |
MikkoZ | 12:bc2446aabbfe | 107 | error = i2c_bus.write( (int)((sad << 1) | I2C_WRITE ), (char*)®, (int)1, true); |
MikkoZ | 12:bc2446aabbfe | 108 | error = error || i2c_bus.write( (int)((sad << 1) | I2C_WRITE ), (char*)data, (int)data_len, false); |
MikkoZ | 13:3d4508874121 | 109 | |
MikkoZ | 12:bc2446aabbfe | 110 | return error; |
MikkoZ | 12:bc2446aabbfe | 111 | } |
MikkoZ | 12:bc2446aabbfe | 112 | |
MikkoZ | 12:bc2446aabbfe | 113 | bool RegisterWriter::write_register(uint8_t sad, uint8_t reg, uint8_t data) { |
MikkoZ | 8:dea50f607a45 | 114 | char data_to_send[2]; |
MikkoZ | 12:bc2446aabbfe | 115 | bool error; |
MikkoZ | 8:dea50f607a45 | 116 | |
MikkoZ | 8:dea50f607a45 | 117 | data_to_send[0] = reg; |
MikkoZ | 8:dea50f607a45 | 118 | data_to_send[1] = data; |
MikkoZ | 12:bc2446aabbfe | 119 | error = i2c_bus.write( (int)((sad << 1) | I2C_WRITE ), &data_to_send[0], 2); |
MikkoZ | 12:bc2446aabbfe | 120 | |
MikkoZ | 12:bc2446aabbfe | 121 | return error; |
MikkoZ | 8:dea50f607a45 | 122 | } |
MikkoZ | 8:dea50f607a45 | 123 | |
MikkoZ | 13:3d4508874121 | 124 | /* @return error true/false */ |
MikkoZ | 8:dea50f607a45 | 125 | bool RegisterWriter::change_bits(uint8_t sad, uint8_t reg, uint8_t mask, uint8_t bits){ |
MikkoZ | 13:3d4508874121 | 126 | uint8_t value, error; |
MikkoZ | 13:3d4508874121 | 127 | |
MikkoZ | 13:3d4508874121 | 128 | error = read_register(sad, reg, &value, 1); |
MikkoZ | 13:3d4508874121 | 129 | if( !error ){ |
MikkoZ | 8:dea50f607a45 | 130 | value = value & ~mask; |
MikkoZ | 8:dea50f607a45 | 131 | value = value | (bits & mask); |
MikkoZ | 12:bc2446aabbfe | 132 | return write_register(sad, reg, value); |
MikkoZ | 8:dea50f607a45 | 133 | } |
MikkoZ | 8:dea50f607a45 | 134 | else{ |
MikkoZ | 8:dea50f607a45 | 135 | //DEBUG_printf("Read before change_bits() failed."); |
MikkoZ | 12:bc2446aabbfe | 136 | return true; |
MikkoZ | 8:dea50f607a45 | 137 | } |
MikkoZ | 8:dea50f607a45 | 138 | } |
MikkoZ | 8:dea50f607a45 | 139 | |
MikkoZ | 12:bc2446aabbfe | 140 | void RegisterWriter::set_hs_mode_for_one_command(){ |
MikkoZ | 12:bc2446aabbfe | 141 | #define MCODE (1<<3) |
MikkoZ | 12:bc2446aabbfe | 142 | char temp; |
MikkoZ | 12:bc2446aabbfe | 143 | //Fullspeed mode -> highspeed mode for one command. |
MikkoZ | 12:bc2446aabbfe | 144 | i2c_bus.write( (int)(MCODE), (char*)&temp, (int)0, true ); //Trick to write just mcode+make nack. |
MikkoZ | 12:bc2446aabbfe | 145 | } |
MikkoZ | 12:bc2446aabbfe | 146 | |
MikkoZ | 8:dea50f607a45 | 147 | |
MikkoZ | 8:dea50f607a45 | 148 | #endif |
MikkoZ | 8:dea50f607a45 | 149 | |
MikkoZ | 8:dea50f607a45 | 150 |