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:
13:3d4508874121
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 #ifndef i2c_common_pp_h
MikkoZ 8:dea50f607a45 26 #define i2c_common_pp_h
MikkoZ 8:dea50f607a45 27
MikkoZ 8:dea50f607a45 28 #include "rohm_hal2.h" //types, DEBUG_print*, USE_*_HARDWARE_I2C
MikkoZ 8:dea50f607a45 29
MikkoZ 9:51c7b97b6a7d 30 /**
MikkoZ 9:51c7b97b6a7d 31 * RegisterWriter class for writing sensor registers via I2C object
MikkoZ 9:51c7b97b6a7d 32 */
MikkoZ 8:dea50f607a45 33 class RegisterWriter
MikkoZ 8:dea50f607a45 34 {
MikkoZ 8:dea50f607a45 35 public:
MikkoZ 8:dea50f607a45 36 /**
MikkoZ 9:51c7b97b6a7d 37 * Use pre-instantiated I2C instance for HAL.
MikkoZ 8:dea50f607a45 38 *
MikkoZ 8:dea50f607a45 39 * @param i2c_obj pre-instantiated i2c object.
MikkoZ 8:dea50f607a45 40 */
MikkoZ 8:dea50f607a45 41 RegisterWriter(I2C &i2c_obj);
MikkoZ 8:dea50f607a45 42
MikkoZ 8:dea50f607a45 43 /**
MikkoZ 8:dea50f607a45 44 * Create a i2c instance which is connected to specified I2C pins.
MikkoZ 8:dea50f607a45 45 *
MikkoZ 8:dea50f607a45 46 * @param sda SDA pin
MikkoZ 8:dea50f607a45 47 * @param sdl SCL pin
MikkoZ 8:dea50f607a45 48 */
MikkoZ 8:dea50f607a45 49 RegisterWriter(PinName sda = I2C_SDA, PinName scl = I2C_SCL);
MikkoZ 8:dea50f607a45 50
MikkoZ 8:dea50f607a45 51 ~RegisterWriter();
MikkoZ 8:dea50f607a45 52
MikkoZ 8:dea50f607a45 53 /**
MikkoZ 9:51c7b97b6a7d 54 * General read @buf_len value(s) to @*buf from sensor @reg in address @sad.
MikkoZ 9:51c7b97b6a7d 55 * @param sad Slave address of sensor
MikkoZ 9:51c7b97b6a7d 56 * @param reg Register of sensor
MikkoZ 9:51c7b97b6a7d 57 * @param *buf uint8_t[@buf_len] for read data
MikkoZ 9:51c7b97b6a7d 58 * @param buf_len amount of data to read from @reg
MikkoZ 8:dea50f607a45 59 */
MikkoZ 13:3d4508874121 60 uint8_t read_register(uint8_t sad, uint8_t reg, uint8_t* buf, uint8_t buf_len = 1);
MikkoZ 9:51c7b97b6a7d 61
MikkoZ 9:51c7b97b6a7d 62 /**
MikkoZ 9:51c7b97b6a7d 63 * FIFO Read @buf_len value(s) to @*buf from sensor @reg in address @sad.
MikkoZ 9:51c7b97b6a7d 64 * Difference is the usage of stop-bit between commands.
MikkoZ 9:51c7b97b6a7d 65 * @param sad Slave address of sensor
MikkoZ 9:51c7b97b6a7d 66 * @param reg Register of sensor
MikkoZ 9:51c7b97b6a7d 67 * @param *buf uint8_t[@buf_len] for read data
MikkoZ 9:51c7b97b6a7d 68 * @param buf_len amount of data to read from @reg
MikkoZ 9:51c7b97b6a7d 69 */
MikkoZ 8:dea50f607a45 70 uint8_t read_fifo_register(uint8_t sad, uint8_t reg, uint8_t* buf, uint8_t buf_len);
MikkoZ 9:51c7b97b6a7d 71
MikkoZ 9:51c7b97b6a7d 72 /**
MikkoZ 12:bc2446aabbfe 73 * Read @buf_len value(s) in high speed to @*buf from sensor @reg in address @sad.
MikkoZ 12:bc2446aabbfe 74 * Reference to kx123 specification page 24, hs 3.4mhZ mode.
MikkoZ 12:bc2446aabbfe 75 * @param sad Slave address of sensor
MikkoZ 12:bc2446aabbfe 76 * @param reg Register of sensor
MikkoZ 12:bc2446aabbfe 77 * @param *buf uint8_t[@buf_len] for read data
MikkoZ 12:bc2446aabbfe 78 * @param buf_len amount of data to read from @reg
MikkoZ 12:bc2446aabbfe 79 */
MikkoZ 12:bc2446aabbfe 80 uint8_t hs_read_register(uint8_t sad, uint8_t reg, uint8_t* buf, uint8_t buf_len);
MikkoZ 12:bc2446aabbfe 81
MikkoZ 12:bc2446aabbfe 82 /**
MikkoZ 9:51c7b97b6a7d 83 * Write @data_len value(s) from @*data to sensor @reg in address @sad.
MikkoZ 9:51c7b97b6a7d 84 * @param sad Slave address of sensor
MikkoZ 9:51c7b97b6a7d 85 * @param reg Register of sensor
MikkoZ 9:51c7b97b6a7d 86 * @param *data uint8_t[@data_len] for written data
MikkoZ 9:51c7b97b6a7d 87 * @param data_len amount of data to written to @reg
MikkoZ 9:51c7b97b6a7d 88 */
MikkoZ 12:bc2446aabbfe 89 bool write_register(uint8_t sad, uint8_t reg, uint8_t* data, uint8_t data_len);
MikkoZ 13:3d4508874121 90 bool write_register_single(uint8_t sad, uint8_t reg, uint8_t* data, uint8_t data_len);
MikkoZ 13:3d4508874121 91 bool write_register_separate(uint8_t sad, uint8_t reg, uint8_t* data, uint8_t data_len);
MikkoZ 9:51c7b97b6a7d 92
MikkoZ 9:51c7b97b6a7d 93 /**
MikkoZ 9:51c7b97b6a7d 94 * Write 1 value from @data to sensor @reg in address @sad.
MikkoZ 9:51c7b97b6a7d 95 * @param sad Slave address of sensor
MikkoZ 9:51c7b97b6a7d 96 * @param reg Register of sensor
MikkoZ 9:51c7b97b6a7d 97 * @param data to be written
MikkoZ 9:51c7b97b6a7d 98 */
MikkoZ 12:bc2446aabbfe 99 bool write_register(uint8_t sad, uint8_t reg, uint8_t data);
MikkoZ 12:bc2446aabbfe 100
MikkoZ 12:bc2446aabbfe 101 /**
MikkoZ 12:bc2446aabbfe 102 * Write @data_len value(s) in high speed from @*data to sensor @reg in address @sad.
MikkoZ 12:bc2446aabbfe 103 * @param sad Slave address of sensor
MikkoZ 12:bc2446aabbfe 104 * @param reg Register of sensor
MikkoZ 12:bc2446aabbfe 105 * @param *data uint8_t[@data_len] for written data
MikkoZ 12:bc2446aabbfe 106 * @param data_len amount of data to written to @reg
MikkoZ 12:bc2446aabbfe 107 */
MikkoZ 12:bc2446aabbfe 108 bool hs_write_register(uint8_t sad, uint8_t reg, uint8_t* data, uint8_t data_len);
MikkoZ 9:51c7b97b6a7d 109
MikkoZ 9:51c7b97b6a7d 110 /**
MikkoZ 9:51c7b97b6a7d 111 * Read-change-write register (@sad/@reg)
MikkoZ 9:51c7b97b6a7d 112 * @param sad Slave address of sensor
MikkoZ 9:51c7b97b6a7d 113 * @param reg Register of sensor
MikkoZ 9:51c7b97b6a7d 114 * @param mask bits to clear before applying new @bits
MikkoZ 9:51c7b97b6a7d 115 * @param bits value to write
MikkoZ 12:bc2446aabbfe 116 * @return true on error, false on ok
MikkoZ 9:51c7b97b6a7d 117 */
MikkoZ 8:dea50f607a45 118 bool change_bits(uint8_t sad, uint8_t reg, uint8_t mask, uint8_t bits);
MikkoZ 8:dea50f607a45 119
MikkoZ 8:dea50f607a45 120 private:
MikkoZ 8:dea50f607a45 121 I2C i2c_bus;
MikkoZ 8:dea50f607a45 122 bool self_created_i2c;
MikkoZ 13:3d4508874121 123 bool write_single; //Single command write or two command write
MikkoZ 12:bc2446aabbfe 124
MikkoZ 12:bc2446aabbfe 125 void set_hs_mode_for_one_command();
MikkoZ 12:bc2446aabbfe 126
MikkoZ 8:dea50f607a45 127 };
MikkoZ 8:dea50f607a45 128
MikkoZ 8:dea50f607a45 129
MikkoZ 8:dea50f607a45 130
MikkoZ 8:dea50f607a45 131
MikkoZ 8:dea50f607a45 132 #endif
MikkoZ 8:dea50f607a45 133
MikkoZ 8:dea50f607a45 134