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