Flemmard | 0604a8e | 2013-05-23 16:15:48 -0700 | [diff] [blame] | 1 | /* |
| 2 | $License: |
| 3 | Copyright (C) 2010 InvenSense Corporation, All Rights Reserved. |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation; either version 2 of the License, or |
| 8 | (at your option) any later version. |
| 9 | |
| 10 | This program is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | $ |
| 18 | */ |
| 19 | |
| 20 | #include "mlsl.h" |
| 21 | #include "mpu-i2c.h" |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | tMLError MLSLSerialOpen(char const *port, void **sl_handle) |
| 27 | { |
| 28 | return ML_SUCCESS; |
| 29 | } |
| 30 | |
| 31 | tMLError MLSLSerialReset(void *sl_handle) |
| 32 | { |
| 33 | return ML_SUCCESS; |
| 34 | } |
| 35 | |
| 36 | tMLError MLSLSerialClose(void *sl_handle) |
| 37 | { |
| 38 | return ML_SUCCESS; |
| 39 | } |
| 40 | |
| 41 | tMLError MLSLSerialWriteSingle(void *sl_handle, |
| 42 | unsigned char slaveAddr, |
| 43 | unsigned char registerAddr, |
| 44 | unsigned char data) |
| 45 | { |
| 46 | return sensor_i2c_write_register((struct i2c_adapter *) sl_handle, |
| 47 | slaveAddr, registerAddr, data); |
| 48 | } |
| 49 | |
| 50 | |
| 51 | tMLError MLSLSerialWrite(void *sl_handle, |
| 52 | unsigned char slaveAddr, |
| 53 | unsigned short length, unsigned char const *data) |
| 54 | { |
| 55 | tMLError result; |
| 56 | const unsigned short dataLength = length - 1; |
| 57 | const unsigned char startRegAddr = data[0]; |
| 58 | unsigned char i2cWrite[SERIAL_MAX_TRANSFER_SIZE + 1]; |
| 59 | unsigned short bytesWritten = 0; |
| 60 | |
| 61 | while (bytesWritten < dataLength) { |
| 62 | unsigned short thisLen = min(SERIAL_MAX_TRANSFER_SIZE, |
| 63 | dataLength - bytesWritten); |
| 64 | if (bytesWritten == 0) { |
| 65 | result = sensor_i2c_write((struct i2c_adapter *) |
| 66 | sl_handle, slaveAddr, |
| 67 | 1 + thisLen, data); |
| 68 | } else { |
| 69 | |
| 70 | i2cWrite[0] = startRegAddr + bytesWritten; |
| 71 | memcpy(&i2cWrite[1], &data[1 + bytesWritten], |
| 72 | thisLen); |
| 73 | result = sensor_i2c_write((struct i2c_adapter *) |
| 74 | sl_handle, slaveAddr, |
| 75 | 1 + thisLen, i2cWrite); |
| 76 | } |
| 77 | if (ML_SUCCESS != result) |
| 78 | return result; |
| 79 | bytesWritten += thisLen; |
| 80 | } |
| 81 | return ML_SUCCESS; |
| 82 | } |
| 83 | |
| 84 | |
| 85 | tMLError MLSLSerialRead(void *sl_handle, |
| 86 | unsigned char slaveAddr, |
| 87 | unsigned char registerAddr, |
| 88 | unsigned short length, unsigned char *data) |
| 89 | { |
| 90 | tMLError result; |
| 91 | unsigned short bytesRead = 0; |
| 92 | |
| 93 | if (registerAddr == MPUREG_FIFO_R_W |
| 94 | || registerAddr == MPUREG_MEM_R_W) { |
| 95 | return ML_ERROR_INVALID_PARAMETER; |
| 96 | } |
| 97 | while (bytesRead < length) { |
| 98 | unsigned short thisLen = |
| 99 | min(SERIAL_MAX_TRANSFER_SIZE, length - bytesRead); |
| 100 | result = |
| 101 | sensor_i2c_read((struct i2c_adapter *) sl_handle, |
| 102 | slaveAddr, registerAddr + bytesRead, |
| 103 | thisLen, &data[bytesRead]); |
| 104 | if (ML_SUCCESS != result) |
| 105 | return result; |
| 106 | bytesRead += thisLen; |
| 107 | } |
| 108 | return ML_SUCCESS; |
| 109 | } |
| 110 | |
| 111 | |
| 112 | tMLError MLSLSerialWriteMem(void *sl_handle, |
| 113 | unsigned char slaveAddr, |
| 114 | unsigned short memAddr, |
| 115 | unsigned short length, |
| 116 | unsigned char const *data) |
| 117 | { |
| 118 | tMLError result; |
| 119 | unsigned short bytesWritten = 0; |
| 120 | |
| 121 | if ((memAddr & 0xFF) + length > MPU_MEM_BANK_SIZE) { |
| 122 | printk |
| 123 | ("memory read length (%d B) extends beyond its limits (%d) " |
| 124 | "if started at location %d\n", length, |
| 125 | MPU_MEM_BANK_SIZE, memAddr & 0xFF); |
| 126 | return ML_ERROR_INVALID_PARAMETER; |
| 127 | } |
| 128 | while (bytesWritten < length) { |
| 129 | unsigned short thisLen = |
| 130 | min(SERIAL_MAX_TRANSFER_SIZE, length - bytesWritten); |
| 131 | result = |
| 132 | mpu_memory_write((struct i2c_adapter *) sl_handle, |
| 133 | slaveAddr, memAddr + bytesWritten, |
| 134 | thisLen, &data[bytesWritten]); |
| 135 | if (ML_SUCCESS != result) |
| 136 | return result; |
| 137 | bytesWritten += thisLen; |
| 138 | } |
| 139 | return ML_SUCCESS; |
| 140 | } |
| 141 | |
| 142 | |
| 143 | tMLError MLSLSerialReadMem(void *sl_handle, |
| 144 | unsigned char slaveAddr, |
| 145 | unsigned short memAddr, |
| 146 | unsigned short length, unsigned char *data) |
| 147 | { |
| 148 | tMLError result; |
| 149 | unsigned short bytesRead = 0; |
| 150 | |
| 151 | if ((memAddr & 0xFF) + length > MPU_MEM_BANK_SIZE) { |
| 152 | printk |
| 153 | ("memory read length (%d B) extends beyond its limits (%d) " |
| 154 | "if started at location %d\n", length, |
| 155 | MPU_MEM_BANK_SIZE, memAddr & 0xFF); |
| 156 | return ML_ERROR_INVALID_PARAMETER; |
| 157 | } |
| 158 | while (bytesRead < length) { |
| 159 | unsigned short thisLen = |
| 160 | min(SERIAL_MAX_TRANSFER_SIZE, length - bytesRead); |
| 161 | result = |
| 162 | mpu_memory_read((struct i2c_adapter *) sl_handle, |
| 163 | slaveAddr, memAddr + bytesRead, |
| 164 | thisLen, &data[bytesRead]); |
| 165 | if (ML_SUCCESS != result) |
| 166 | return result; |
| 167 | bytesRead += thisLen; |
| 168 | } |
| 169 | return ML_SUCCESS; |
| 170 | } |
| 171 | |
| 172 | |
| 173 | tMLError MLSLSerialWriteFifo(void *sl_handle, |
| 174 | unsigned char slaveAddr, |
| 175 | unsigned short length, |
| 176 | unsigned char const *data) |
| 177 | { |
| 178 | tMLError result; |
| 179 | unsigned char i2cWrite[SERIAL_MAX_TRANSFER_SIZE + 1]; |
| 180 | unsigned short bytesWritten = 0; |
| 181 | |
| 182 | if (length > FIFO_HW_SIZE) { |
| 183 | printk(KERN_ERR |
| 184 | "maximum fifo write length is %d\n", FIFO_HW_SIZE); |
| 185 | return ML_ERROR_INVALID_PARAMETER; |
| 186 | } |
| 187 | while (bytesWritten < length) { |
| 188 | unsigned short thisLen = |
| 189 | min(SERIAL_MAX_TRANSFER_SIZE, length - bytesWritten); |
| 190 | i2cWrite[0] = MPUREG_FIFO_R_W; |
| 191 | memcpy(&i2cWrite[1], &data[bytesWritten], thisLen); |
| 192 | result = sensor_i2c_write((struct i2c_adapter *) sl_handle, |
| 193 | slaveAddr, thisLen + 1, |
| 194 | i2cWrite); |
| 195 | if (ML_SUCCESS != result) |
| 196 | return result; |
| 197 | bytesWritten += thisLen; |
| 198 | } |
| 199 | return ML_SUCCESS; |
| 200 | } |
| 201 | |
| 202 | |
| 203 | tMLError MLSLSerialReadFifo(void *sl_handle, |
| 204 | unsigned char slaveAddr, |
| 205 | unsigned short length, unsigned char *data) |
| 206 | { |
| 207 | tMLError result; |
| 208 | unsigned short bytesRead = 0; |
| 209 | |
| 210 | if (length > FIFO_HW_SIZE) { |
| 211 | printk(KERN_ERR |
| 212 | "maximum fifo read length is %d\n", FIFO_HW_SIZE); |
| 213 | return ML_ERROR_INVALID_PARAMETER; |
| 214 | } |
| 215 | while (bytesRead < length) { |
| 216 | unsigned short thisLen = |
| 217 | min(SERIAL_MAX_TRANSFER_SIZE, length - bytesRead); |
| 218 | result = |
| 219 | sensor_i2c_read((struct i2c_adapter *) sl_handle, |
| 220 | slaveAddr, MPUREG_FIFO_R_W, thisLen, |
| 221 | &data[bytesRead]); |
| 222 | if (ML_SUCCESS != result) |
| 223 | return result; |
| 224 | bytesRead += thisLen; |
| 225 | } |
| 226 | |
| 227 | return ML_SUCCESS; |
| 228 | } |
| 229 | |