collab/pb-hal

git clone https://git.julia.blue/collab/pb-hal

Joseph RyanAdd prep code for integrating working init routine64c6e28

tof
3.7 KiB72 linesraw
1// I2Cdev library collection - Main I2C device class
2// Abstracts bit and byte I2C R/W functions into a convenient class
3// RaspberryPi bcm2835 library port: bcm2835 library available at http://www.airspayce.com/mikem/bcm2835/index.html
4// Based on Arduino's I2Cdev by Jeff Rowberg <jeff@rowberg.net>
5//
6
7/* ============================================
8I2Cdev device library code is placed under the MIT license
9
10Permission is hereby granted, free of charge, to any person obtaining a copy
11of this software and associated documentation files (the "Software"), to deal
12in the Software without restriction, including without limitation the rights
13to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14copies of the Software, and to permit persons to whom the Software is
15furnished to do so, subject to the following conditions:
16
17The above copyright notice and this permission notice shall be included in
18all copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26THE SOFTWARE.
27===============================================
28*/
29
30#ifndef _I2CDEV_H_
31#define _I2CDEV_H_
32
33#include <bcm2835.h>
34#include <math.h> // required for BMP180
35#include <stdlib.h> // required for MPU6060
36#include <string.h> // required for MPU6060
37
38
39#define set_I2C_pins  false  
40/* used to boolean for setting RPi I2C pins P1-03 (SDA) and P1-05 (SCL) to alternate function ALT0, which enables those pins for I2C interface. 
41   setI2Cpin should be false, if the I2C are already configured in alt mode ... */
42
43#define i2c_baudrate 400000
44//uint32_t i2c_baudrate = 400000 ; //400 kHz, 
45
46class I2Cdev {
47 public:
48        I2Cdev();
49
50        static void initialize();
51        static void enable(bool isEnabled);
52
53        static int8_t readBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t *data);
54        //TODO static int8_t readBitW(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint16_t *data);
55        static int8_t readBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t *data);
56        //TODO static int8_t readBitsW(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint16_t *data);
57        static int8_t readByte(uint8_t devAddr, uint8_t regAddr, uint8_t *data);
58        static int8_t readWord(uint8_t devAddr, uint8_t regAddr, uint16_t *data);
59        static int8_t readBytes(uint8_t devAddr, uint16_t regAddr, uint8_t length, uint8_t *data);
60        static int8_t readWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint16_t *data);
61
62        static bool writeBit(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint8_t data);
63        //TODO static bool writeBitW(uint8_t devAddr, uint8_t regAddr, uint8_t bitNum, uint16_t data);
64        static bool writeBits(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint8_t data);
65        //TODO static bool writeBitsW(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart, uint8_t length, uint16_t data);
66        static bool writeByte(uint8_t devAddr, uint8_t regAddr, uint8_t data);
67        static bool writeWord(uint8_t devAddr, uint8_t regAddr, uint16_t data);
68        static bool writeBytes(uint8_t devAddr, uint16_t regAddr, uint8_t length, uint8_t *data);
69        static bool writeWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint16_t *data);
70};
71
72#endif /* _I2CDEV_H_ */