I2C libraries for the implementation of I2C communication in nRF51 based on I2C redbearlabs libraries
Dependents: I2C_Driver_Example I2C_Driver_Example nRF51822_SimpleControls LastCode ... more
I2C_Driver.cpp@0:2514f1c72462, 2016-08-23 (annotated)
- Committer:
- cesarcazal
- Date:
- Tue Aug 23 16:23:15 2016 +0000
- Revision:
- 0:2514f1c72462
Driver para la utiilizaci?n del I2C basado en librer?as de Redbearlab
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cesarcazal | 0:2514f1c72462 | 1 | /* |
cesarcazal | 0:2514f1c72462 | 2 | * Driver I2C para Proyecto - Pulsera Saturometrica. |
cesarcazal | 0:2514f1c72462 | 3 | * Util para implementar rapidamente comunicacion simple I2C. |
cesarcazal | 0:2514f1c72462 | 4 | * Basado en el driver I2C desarrollado por RedBearLab |
cesarcazal | 0:2514f1c72462 | 5 | |
cesarcazal | 0:2514f1c72462 | 6 | Copyright (c) 2012-2014 RedBearLab |
cesarcazal | 0:2514f1c72462 | 7 | |
cesarcazal | 0:2514f1c72462 | 8 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
cesarcazal | 0:2514f1c72462 | 9 | and associated documentation files (the "Software"), to deal in the Software without restriction, |
cesarcazal | 0:2514f1c72462 | 10 | including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, |
cesarcazal | 0:2514f1c72462 | 11 | and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, |
cesarcazal | 0:2514f1c72462 | 12 | subject to the following conditions: |
cesarcazal | 0:2514f1c72462 | 13 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
cesarcazal | 0:2514f1c72462 | 14 | |
cesarcazal | 0:2514f1c72462 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
cesarcazal | 0:2514f1c72462 | 16 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
cesarcazal | 0:2514f1c72462 | 17 | PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE |
cesarcazal | 0:2514f1c72462 | 18 | FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
cesarcazal | 0:2514f1c72462 | 19 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
cesarcazal | 0:2514f1c72462 | 20 | |
cesarcazal | 0:2514f1c72462 | 21 | */ |
cesarcazal | 0:2514f1c72462 | 22 | |
cesarcazal | 0:2514f1c72462 | 23 | #include "I2C_Driver.h" |
cesarcazal | 0:2514f1c72462 | 24 | #include "mbed.h" |
cesarcazal | 0:2514f1c72462 | 25 | #include "wire.h" |
cesarcazal | 0:2514f1c72462 | 26 | |
cesarcazal | 0:2514f1c72462 | 27 | |
cesarcazal | 0:2514f1c72462 | 28 | TwoWire Wire = TwoWire(NRF_TWI0); |
cesarcazal | 0:2514f1c72462 | 29 | |
cesarcazal | 0:2514f1c72462 | 30 | void ini_i2c( int scl, int sda){ |
cesarcazal | 0:2514f1c72462 | 31 | Wire.begin(scl, sda, TWI_FREQUENCY_100K); |
cesarcazal | 0:2514f1c72462 | 32 | } |
cesarcazal | 0:2514f1c72462 | 33 | |
cesarcazal | 0:2514f1c72462 | 34 | /* |
cesarcazal | 0:2514f1c72462 | 35 | WriteByte( addr, *pbuf, length, dev_addr) : escritura i2c en la dirección i2c definida al inicio del programa. |
cesarcazal | 0:2514f1c72462 | 36 | |
cesarcazal | 0:2514f1c72462 | 37 | addr: dirección del registro a ser escrito. |
cesarcazal | 0:2514f1c72462 | 38 | *pbuf: dirección (puntero) del array de datos a ser escritos. Dirección 0: LSB |
cesarcazal | 0:2514f1c72462 | 39 | length: cantidad de bytes a ser transmitidos. |
cesarcazal | 0:2514f1c72462 | 40 | dev_addr: Direección física del dispositivo esclavo i2c |
cesarcazal | 0:2514f1c72462 | 41 | |
cesarcazal | 0:2514f1c72462 | 42 | */ |
cesarcazal | 0:2514f1c72462 | 43 | |
cesarcazal | 0:2514f1c72462 | 44 | int write_i2c(uint8_t addr, uint8_t *pbuf, uint16_t length, uint8_t dev_addr){ //Parámetros: addr: dirección de memoria; *pbuf: puntero de byte 0 del buffer; length: cantidad de bytes a enviar; dev_addr: Direección física del dispositivo esclavo i2c |
cesarcazal | 0:2514f1c72462 | 45 | int aux=0; |
cesarcazal | 0:2514f1c72462 | 46 | uint8_t aux2[length]; |
cesarcazal | 0:2514f1c72462 | 47 | uint8_t *aux3; |
cesarcazal | 0:2514f1c72462 | 48 | for ( aux = 0; aux < length; aux++ ){ |
cesarcazal | 0:2514f1c72462 | 49 | aux3 = pbuf+length-(aux+1); |
cesarcazal | 0:2514f1c72462 | 50 | aux2[aux]= *aux3; |
cesarcazal | 0:2514f1c72462 | 51 | } |
cesarcazal | 0:2514f1c72462 | 52 | |
cesarcazal | 0:2514f1c72462 | 53 | Wire.beginTransmission(dev_addr); |
cesarcazal | 0:2514f1c72462 | 54 | Wire.write( (uint8_t)addr ); |
cesarcazal | 0:2514f1c72462 | 55 | Wire.write(aux2, length); |
cesarcazal | 0:2514f1c72462 | 56 | return (Wire.endTransmission()); |
cesarcazal | 0:2514f1c72462 | 57 | } |
cesarcazal | 0:2514f1c72462 | 58 | |
cesarcazal | 0:2514f1c72462 | 59 | /* |
cesarcazal | 0:2514f1c72462 | 60 | ReadByte( addr, *pbuf, length, dev_addr) : lectura i2c en la dirección i2c definida al inicio del programa. |
cesarcazal | 0:2514f1c72462 | 61 | |
cesarcazal | 0:2514f1c72462 | 62 | addr: dirección del registro a ser escrito. |
cesarcazal | 0:2514f1c72462 | 63 | *pbuf: dirección (puntero) del array de datos a ser escritos. Dirección 0: LSB |
cesarcazal | 0:2514f1c72462 | 64 | length: cantidad de bytes a ser transmitidos. |
cesarcazal | 0:2514f1c72462 | 65 | dev_addr: Direección física del dispositivo esclavo i2c |
cesarcazal | 0:2514f1c72462 | 66 | |
cesarcazal | 0:2514f1c72462 | 67 | */ |
cesarcazal | 0:2514f1c72462 | 68 | |
cesarcazal | 0:2514f1c72462 | 69 | int read_i2c(uint8_t addr, uint8_t *pbuf, uint16_t length, uint8_t dev_addr){ //Parámetros: addr: dirección de memoria; *pbuf: puntero de byte 0 del buffer; length: cantidad de bytes a enviar; dev_addr: Direección física del dispositivo esclavo i2c |
cesarcazal | 0:2514f1c72462 | 70 | int error=0; |
cesarcazal | 0:2514f1c72462 | 71 | |
cesarcazal | 0:2514f1c72462 | 72 | Wire.beginTransmission(dev_addr); |
cesarcazal | 0:2514f1c72462 | 73 | Wire.write( (uint8_t)addr ); |
cesarcazal | 0:2514f1c72462 | 74 | error = Wire.endTransmission(); |
cesarcazal | 0:2514f1c72462 | 75 | |
cesarcazal | 0:2514f1c72462 | 76 | Wire.requestFrom(dev_addr+1, length); |
cesarcazal | 0:2514f1c72462 | 77 | |
cesarcazal | 0:2514f1c72462 | 78 | pbuf=pbuf+length-1; |
cesarcazal | 0:2514f1c72462 | 79 | while( Wire.available() > 0 ) |
cesarcazal | 0:2514f1c72462 | 80 | { |
cesarcazal | 0:2514f1c72462 | 81 | *pbuf = Wire.read(); |
cesarcazal | 0:2514f1c72462 | 82 | pbuf--; |
cesarcazal | 0:2514f1c72462 | 83 | } |
cesarcazal | 0:2514f1c72462 | 84 | return(error); |
cesarcazal | 0:2514f1c72462 | 85 | } |