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 Rohm

Committer:
MACRUM
Date:
Wed Feb 27 04:43:21 2019 +0000
Revision:
15:726c671efcce
Parent:
14:f33e0914ea36
Fix debug print

Who changed what in which revision?

UserRevisionLine numberNew 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
Ren Boting 14:f33e0914ea36 31 #define MAX_DATA_LENGTH 6
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*)&reg, (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*)&reg, (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
Ren Boting 14:f33e0914ea36 91 char cmd[MAX_DATA_LENGTH+1]; // assume max value of data_len is 6
Ren Boting 14:f33e0914ea36 92 if (data_len>MAX_DATA_LENGTH) {
Ren Boting 14:f33e0914ea36 93 return false;
Ren Boting 14:f33e0914ea36 94 }
MikkoZ 13:3d4508874121 95
MikkoZ 13:3d4508874121 96 cmd[0] = reg;
MikkoZ 13:3d4508874121 97 for (int i = 0; i < data_len; i++) {
MikkoZ 13:3d4508874121 98 cmd[i+1] = data[i];
MikkoZ 13:3d4508874121 99 }
MikkoZ 13:3d4508874121 100 error = i2c_bus.write( (int)((sad << 1) | I2C_WRITE ), cmd, data_len+1, true);
MikkoZ 13:3d4508874121 101 return error;
MikkoZ 13:3d4508874121 102 }
MikkoZ 13:3d4508874121 103
MikkoZ 13:3d4508874121 104 /** Write register with two separate writes.
MikkoZ 13:3d4508874121 105 * First write register address and then continue with data. Send stop only after second write.
MikkoZ 13:3d4508874121 106 */
MikkoZ 13:3d4508874121 107 bool RegisterWriter::write_register_separate(uint8_t sad, uint8_t reg, uint8_t* data, uint8_t data_len) {
MikkoZ 13:3d4508874121 108 bool error;
MikkoZ 13:3d4508874121 109
MikkoZ 12:bc2446aabbfe 110 error = i2c_bus.write( (int)((sad << 1) | I2C_WRITE ), (char*)&reg, (int)1, true);
MikkoZ 12:bc2446aabbfe 111 error = error || i2c_bus.write( (int)((sad << 1) | I2C_WRITE ), (char*)data, (int)data_len, false);
MikkoZ 13:3d4508874121 112
MikkoZ 12:bc2446aabbfe 113 return error;
MikkoZ 12:bc2446aabbfe 114 }
MikkoZ 12:bc2446aabbfe 115
MikkoZ 12:bc2446aabbfe 116 bool RegisterWriter::write_register(uint8_t sad, uint8_t reg, uint8_t data) {
MikkoZ 8:dea50f607a45 117 char data_to_send[2];
MikkoZ 12:bc2446aabbfe 118 bool error;
MikkoZ 8:dea50f607a45 119
MikkoZ 8:dea50f607a45 120 data_to_send[0] = reg;
MikkoZ 8:dea50f607a45 121 data_to_send[1] = data;
MikkoZ 12:bc2446aabbfe 122 error = i2c_bus.write( (int)((sad << 1) | I2C_WRITE ), &data_to_send[0], 2);
MikkoZ 12:bc2446aabbfe 123
MikkoZ 12:bc2446aabbfe 124 return error;
MikkoZ 8:dea50f607a45 125 }
MikkoZ 8:dea50f607a45 126
MikkoZ 13:3d4508874121 127 /* @return error true/false */
MikkoZ 8:dea50f607a45 128 bool RegisterWriter::change_bits(uint8_t sad, uint8_t reg, uint8_t mask, uint8_t bits){
MikkoZ 13:3d4508874121 129 uint8_t value, error;
MikkoZ 13:3d4508874121 130
MikkoZ 13:3d4508874121 131 error = read_register(sad, reg, &value, 1);
MikkoZ 13:3d4508874121 132 if( !error ){
MikkoZ 8:dea50f607a45 133 value = value & ~mask;
MikkoZ 8:dea50f607a45 134 value = value | (bits & mask);
MikkoZ 12:bc2446aabbfe 135 return write_register(sad, reg, value);
MikkoZ 8:dea50f607a45 136 }
MikkoZ 8:dea50f607a45 137 else{
MikkoZ 8:dea50f607a45 138 //DEBUG_printf("Read before change_bits() failed.");
MikkoZ 12:bc2446aabbfe 139 return true;
MikkoZ 8:dea50f607a45 140 }
MikkoZ 8:dea50f607a45 141 }
MikkoZ 8:dea50f607a45 142
MikkoZ 12:bc2446aabbfe 143 void RegisterWriter::set_hs_mode_for_one_command(){
MikkoZ 12:bc2446aabbfe 144 #define MCODE (1<<3)
MikkoZ 12:bc2446aabbfe 145 char temp;
MikkoZ 12:bc2446aabbfe 146 //Fullspeed mode -> highspeed mode for one command.
MikkoZ 12:bc2446aabbfe 147 i2c_bus.write( (int)(MCODE), (char*)&temp, (int)0, true ); //Trick to write just mcode+make nack.
MikkoZ 12:bc2446aabbfe 148 }
MikkoZ 12:bc2446aabbfe 149
MikkoZ 8:dea50f607a45 150
MikkoZ 8:dea50f607a45 151 #endif
MikkoZ 8:dea50f607a45 152
MikkoZ 8:dea50f607a45 153