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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers RegisterWriter.h Source File

RegisterWriter.h

00001 /*
00002 The MIT License (MIT)
00003 Copyright (c) 2017 Rohm Semiconductor
00004 
00005 Permission is hereby granted, free of charge, to any person obtaining a
00006 copy of this software and associated documentation files (the
00007 "Software"), to deal in the Software without restriction, including
00008 without limitation the rights to use, copy, modify, merge, publish,
00009 distribute, sublicense, and/or sell copies of the Software, and to
00010 permit persons to whom the Software is furnished to do so, subject to
00011 the following conditions:
00012 
00013 The above copyright notice and this permission notice shall be included
00014 in all copies or substantial portions of the Software.
00015 
00016 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00017 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00018 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00019 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
00020 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
00021 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
00022 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00023 */
00024 
00025 #ifndef i2c_common_pp_h
00026 #define i2c_common_pp_h
00027 
00028 #include "rohm_hal2.h"       //types, DEBUG_print*, USE_*_HARDWARE_I2C
00029 
00030 /**
00031 * RegisterWriter class for writing sensor registers via I2C object
00032 */
00033 class RegisterWriter
00034 {
00035 public:
00036     /**
00037     * Use pre-instantiated I2C instance for HAL.
00038     *
00039     * @param i2c_obj pre-instantiated i2c object.
00040     */
00041     RegisterWriter(I2C &i2c_obj);
00042 
00043     /**
00044     * Create a i2c instance which is connected to specified I2C pins.
00045     *
00046     * @param sda SDA pin
00047     * @param sdl SCL pin
00048     */
00049     RegisterWriter(PinName sda = I2C_SDA, PinName scl = I2C_SCL);
00050 
00051     ~RegisterWriter();
00052 
00053     /**
00054     * General read @buf_len value(s) to @*buf from sensor @reg in address @sad.
00055     * @param sad Slave address of sensor
00056     * @param reg Register of sensor
00057     * @param *buf uint8_t[@buf_len] for read data
00058     * @param buf_len amount of data to read from @reg
00059     */
00060     uint8_t read_register(uint8_t sad, uint8_t reg, uint8_t* buf, uint8_t buf_len = 1);
00061 
00062     /**
00063     * FIFO Read @buf_len value(s) to @*buf from sensor @reg in address @sad.
00064     * Difference is the usage of stop-bit between commands.
00065     * @param sad Slave address of sensor
00066     * @param reg Register of sensor
00067     * @param *buf uint8_t[@buf_len] for read data
00068     * @param buf_len amount of data to read from @reg
00069     */
00070     uint8_t read_fifo_register(uint8_t sad, uint8_t reg, uint8_t* buf, uint8_t buf_len);
00071 
00072     /**
00073     * Read @buf_len value(s) in high speed to @*buf from sensor @reg in address @sad.
00074     * Reference to kx123 specification page 24, hs 3.4mhZ mode.
00075     * @param sad Slave address of sensor
00076     * @param reg Register of sensor
00077     * @param *buf uint8_t[@buf_len] for read data
00078     * @param buf_len amount of data to read from @reg
00079     */
00080     uint8_t hs_read_register(uint8_t sad, uint8_t reg, uint8_t* buf, uint8_t buf_len);
00081     
00082     /**
00083     * Write @data_len value(s) from @*data to sensor @reg in address @sad.
00084     * @param sad Slave address of sensor
00085     * @param reg Register of sensor
00086     * @param *data uint8_t[@data_len] for written data
00087     * @param data_len amount of data to written to @reg
00088     */
00089     bool write_register(uint8_t sad, uint8_t reg, uint8_t* data, uint8_t data_len);
00090     bool write_register_single(uint8_t sad, uint8_t reg, uint8_t* data, uint8_t data_len);
00091     bool write_register_separate(uint8_t sad, uint8_t reg, uint8_t* data, uint8_t data_len);
00092 
00093     /**
00094     * Write 1 value from @data to sensor @reg in address @sad.
00095     * @param sad Slave address of sensor
00096     * @param reg Register of sensor
00097     * @param data to be written
00098     */
00099     bool write_register(uint8_t sad, uint8_t reg, uint8_t data);
00100 
00101     /**
00102     * Write @data_len value(s) in high speed from @*data to sensor @reg in address @sad.
00103     * @param sad Slave address of sensor
00104     * @param reg Register of sensor
00105     * @param *data uint8_t[@data_len] for written data
00106     * @param data_len amount of data to written to @reg
00107     */
00108     bool hs_write_register(uint8_t sad, uint8_t reg, uint8_t* data, uint8_t data_len);
00109 
00110     /**
00111     * Read-change-write register (@sad/@reg)
00112     * @param sad Slave address of sensor
00113     * @param reg Register of sensor
00114     * @param mask bits to clear before applying new @bits
00115     * @param bits value to write
00116     * @return true on error, false on ok
00117     */
00118     bool change_bits(uint8_t sad, uint8_t reg, uint8_t mask, uint8_t bits);
00119 
00120 private:
00121     I2C i2c_bus;
00122     bool self_created_i2c;
00123     bool write_single;   //Single command write or two command write
00124 
00125     void set_hs_mode_for_one_command();
00126 
00127 };
00128 
00129 
00130 
00131 
00132 #endif
00133 
00134