|
| 1 | +/* |
| 2 | + *************************************************************************** |
| 3 | + * |
| 4 | + * bno055.c - part of sample SW for using BNO055 with Arduino |
| 5 | + * |
| 6 | + * Usage: BNO055 Sensor Driver Support Source File |
| 7 | + * |
| 8 | + * (C) All rights reserved by ROBERT BOSCH GMBH |
| 9 | + * |
| 10 | + * Copyright (C) 2014 Bosch Sensortec GmbH |
| 11 | + * |
| 12 | + * This program is free software: you can redistribute it and/or modify |
| 13 | + * it under the terms of the GNU General Public License as published by |
| 14 | + * the Free Software Foundation, either version 3 of the License, or |
| 15 | + * (at your option) any later version. |
| 16 | + * |
| 17 | + * This program is distributed in the hope that it will be useful, |
| 18 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | + * GNU General Public License for more details. |
| 21 | + * |
| 22 | + * You should have received a copy of the GNU General Public License |
| 23 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 24 | + * |
| 25 | + **************************************************************************/ |
| 26 | +/* Date: 2014/01/07 |
| 27 | + * Revision: 1.2 |
| 28 | + * |
| 29 | + */ |
| 30 | + |
| 31 | +#include "BNO055_support.h" |
| 32 | + |
| 33 | +/***************************************************************************** |
| 34 | + * Description: *//**\brief |
| 35 | + * This function initialises the structure pointer, receives |
| 36 | + * and assigns the I2C address. |
| 37 | + * |
| 38 | + * |
| 39 | + * |
| 40 | + * |
| 41 | + * |
| 42 | + * \param bno055_t *bno055 structure pointer. |
| 43 | + * |
| 44 | + * |
| 45 | + * |
| 46 | + * \return communication results. |
| 47 | + * |
| 48 | + * |
| 49 | + ****************************************************************************/ |
| 50 | +/* Scheduling: |
| 51 | + * |
| 52 | + * |
| 53 | + * |
| 54 | + * Usage guide: |
| 55 | + * |
| 56 | + * |
| 57 | + * Remarks: |
| 58 | + * |
| 59 | + ****************************************************************************/ |
| 60 | +BNO055_RETURN_FUNCTION_TYPE BNO_Init(struct bno055_t *bno055) |
| 61 | +{ |
| 62 | + |
| 63 | + BNO055_RETURN_FUNCTION_TYPE comres = BNO055_Zero_U8X; |
| 64 | + //Link the function pointers for communication (late-binding) |
| 65 | + bno055->bus_read = BNO055_I2C_bus_read; |
| 66 | + bno055->bus_write = BNO055_I2C_bus_write; |
| 67 | + bno055->delay_msec = _delay; |
| 68 | + //Initialization from the BNO055 API |
| 69 | + comres = bno055_init(bno055); |
| 70 | + return comres; |
| 71 | + |
| 72 | +} |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | +/***************************************************************************** |
| 78 | + * Description: *//**\brief |
| 79 | + * This function is called when data has to be read over the I2C bus |
| 80 | + * |
| 81 | + * |
| 82 | + * |
| 83 | + * |
| 84 | + * |
| 85 | + * \param unsigned char dev_addr holds the device address |
| 86 | + * unsigned char reg_addr holds the register address |
| 87 | + * unsigned char *reg_data holds the pointer for the start of the |
| 88 | + * data structure |
| 89 | + * unsigned char cnt holds the count of the number of bytes to be |
| 90 | + * read |
| 91 | + * |
| 92 | + * |
| 93 | + * \return communication results. |
| 94 | + * |
| 95 | + * |
| 96 | + ****************************************************************************/ |
| 97 | +/* Scheduling: |
| 98 | + * |
| 99 | + * |
| 100 | + * |
| 101 | + * Usage guide: |
| 102 | + * |
| 103 | + * |
| 104 | + * Remarks: |
| 105 | + * |
| 106 | + ****************************************************************************/ |
| 107 | +BNO055_RETURN_FUNCTION_TYPE BNO055_I2C_bus_read(unsigned char dev_addr,unsigned char reg_addr, unsigned char *reg_data, unsigned char cnt) |
| 108 | +{ |
| 109 | + BNO055_RETURN_FUNCTION_TYPE comres = BNO055_Zero_U8X; |
| 110 | + Wire.beginTransmission(dev_addr); //Start of transmission |
| 111 | + Wire.write(reg_addr); //Desired start register |
| 112 | + Wire.endTransmission(); //Stop of transmission |
| 113 | + delayMicroseconds(150); |
| 114 | + Wire.requestFrom(dev_addr, cnt); //Request data |
| 115 | + while(Wire.available()) //The slave device may send less than requested |
| 116 | + { |
| 117 | + *reg_data = Wire.read(); //Receive a byte |
| 118 | + reg_data++; //Increment pointer |
| 119 | + } |
| 120 | + return comres; |
| 121 | +} |
| 122 | + |
| 123 | + |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | +/***************************************************************************** |
| 128 | + * Description: *//**\brief |
| 129 | + * This function is called when data has to be written over |
| 130 | + * I2C bus |
| 131 | + * |
| 132 | + * |
| 133 | + * |
| 134 | + * |
| 135 | + * |
| 136 | + * \param unsigned char dev_addr holds the device address |
| 137 | + * unsigned char reg_addr holds the register address |
| 138 | + * unsigned char *reg_data holds the pointer for the start of the |
| 139 | + * data structure |
| 140 | + * unsigned char cnt holds the count of the number of bytes to be |
| 141 | + * written |
| 142 | + * |
| 143 | + * |
| 144 | + * \return communication results. |
| 145 | + * |
| 146 | + * |
| 147 | + ****************************************************************************/ |
| 148 | +/* Scheduling: |
| 149 | + * |
| 150 | + * |
| 151 | + * |
| 152 | + * Usage guide: |
| 153 | + * |
| 154 | + * |
| 155 | + * Remarks: |
| 156 | + * |
| 157 | + ****************************************************************************/ |
| 158 | +BNO055_RETURN_FUNCTION_TYPE BNO055_I2C_bus_write(unsigned char dev_addr,unsigned char reg_addr, unsigned char *reg_data, unsigned char cnt) |
| 159 | +{ |
| 160 | + BNO055_RETURN_FUNCTION_TYPE comres = BNO055_Zero_U8X; |
| 161 | + Wire.beginTransmission(dev_addr); //Start of transmission |
| 162 | + Wire.write(reg_addr); //Desired start register |
| 163 | + for(unsigned char index = 0; index < cnt; index++) |
| 164 | + { |
| 165 | + Wire.write(*reg_data); //Write the data |
| 166 | + reg_data++; //Increment pointer |
| 167 | + } |
| 168 | + Wire.endTransmission(); //Stop of transmission |
| 169 | + delayMicroseconds(150); |
| 170 | + return comres; |
| 171 | +} |
| 172 | + |
| 173 | +/***************************************************************************** |
| 174 | + * Description: *//**\brief |
| 175 | + * This function is a mirror for the delay function for type casting |
| 176 | + * |
| 177 | + * |
| 178 | + * |
| 179 | + * |
| 180 | + * |
| 181 | + * \param unsigned int |
| 182 | + * |
| 183 | + * |
| 184 | + * \return none |
| 185 | + * |
| 186 | + * |
| 187 | + ****************************************************************************/ |
| 188 | +/* Scheduling: |
| 189 | + * |
| 190 | + * |
| 191 | + * |
| 192 | + * Usage guide: |
| 193 | + * |
| 194 | + * |
| 195 | + * Remarks: |
| 196 | + * |
| 197 | + ****************************************************************************/ |
| 198 | +void _delay(unsigned int period) |
| 199 | +{ |
| 200 | + delay(long(period)); |
| 201 | +} |
| 202 | + |
| 203 | + |
| 204 | + |
| 205 | + |
0 commit comments