I2C libraries for the implementation of I2C communication in nRF51 based on I2C redbearlabs libraries

Fork of I2C_Driver by CESAR CAZAL

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?

UserRevisionLine numberNew contents of line
cesarcazal 0:2514f1c72462 1
cesarcazal 0:2514f1c72462 2 /*
cesarcazal 0:2514f1c72462 3
cesarcazal 0:2514f1c72462 4 Copyright (c) 2014 RedBearLab, All rights reserved.
cesarcazal 0:2514f1c72462 5
cesarcazal 0:2514f1c72462 6 This library is free software; you can redistribute it and/or
cesarcazal 0:2514f1c72462 7 modify it under the terms of the GNU Lesser General Public
cesarcazal 0:2514f1c72462 8 License as published by the Free Software Foundation; either
cesarcazal 0:2514f1c72462 9 version 2.1 of the License, or (at your option) any later version.
cesarcazal 0:2514f1c72462 10
cesarcazal 0:2514f1c72462 11 This library is distributed in the hope that it will be useful,
cesarcazal 0:2514f1c72462 12 but WITHOUT ANY WARRANTY; without even the implied warranty of
cesarcazal 0:2514f1c72462 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
cesarcazal 0:2514f1c72462 14 See the GNU Lesser General Public License for more details.
cesarcazal 0:2514f1c72462 15
cesarcazal 0:2514f1c72462 16 You should have received a copy of the GNU Lesser General Public
cesarcazal 0:2514f1c72462 17 License along with this library; if not, write to the Free Software
cesarcazal 0:2514f1c72462 18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
cesarcazal 0:2514f1c72462 19
cesarcazal 0:2514f1c72462 20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
cesarcazal 0:2514f1c72462 21 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
cesarcazal 0:2514f1c72462 22 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
cesarcazal 0:2514f1c72462 23 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
cesarcazal 0:2514f1c72462 24 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
cesarcazal 0:2514f1c72462 25 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
cesarcazal 0:2514f1c72462 26 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
cesarcazal 0:2514f1c72462 27
cesarcazal 0:2514f1c72462 28 */
cesarcazal 0:2514f1c72462 29
cesarcazal 0:2514f1c72462 30 #ifndef _WIRE_H_
cesarcazal 0:2514f1c72462 31 #define _WIRE_H_
cesarcazal 0:2514f1c72462 32
cesarcazal 0:2514f1c72462 33 #include "mbed.h"
cesarcazal 0:2514f1c72462 34
cesarcazal 0:2514f1c72462 35 #define TWI_DELAY(x) wait_us(x);
cesarcazal 0:2514f1c72462 36
cesarcazal 0:2514f1c72462 37 #define BUFF_MAX_LENGTH 128
cesarcazal 0:2514f1c72462 38
cesarcazal 0:2514f1c72462 39 #define MAX_TIMEOUT_LOOPS (20000UL)
cesarcazal 0:2514f1c72462 40
cesarcazal 0:2514f1c72462 41 #define TWI_FREQUENCY_100K 0
cesarcazal 0:2514f1c72462 42 #define TWI_FREQUENCY_250K 1
cesarcazal 0:2514f1c72462 43 #define TWI_FREQUENCY_400K 2
cesarcazal 0:2514f1c72462 44
cesarcazal 0:2514f1c72462 45 #define TWI_SCL 28
cesarcazal 0:2514f1c72462 46 #define TWI_SDA 29
cesarcazal 0:2514f1c72462 47
cesarcazal 0:2514f1c72462 48
cesarcazal 0:2514f1c72462 49 class TwoWire
cesarcazal 0:2514f1c72462 50 {
cesarcazal 0:2514f1c72462 51 public :
cesarcazal 0:2514f1c72462 52 TwoWire(NRF_TWI_Type *twi_use);
cesarcazal 0:2514f1c72462 53 void begin();
cesarcazal 0:2514f1c72462 54 void begin(uint32_t scl_pin, uint32_t sda_pin, uint8_t speed);
cesarcazal 0:2514f1c72462 55 void beginTransmission(uint8_t);
cesarcazal 0:2514f1c72462 56 void beginTransmission(int);
cesarcazal 0:2514f1c72462 57 uint8_t endTransmission(void);
cesarcazal 0:2514f1c72462 58 uint8_t endTransmission(uint8_t);
cesarcazal 0:2514f1c72462 59 uint8_t requestFrom(uint8_t, uint8_t);
cesarcazal 0:2514f1c72462 60 uint8_t requestFrom(uint8_t, uint8_t, uint8_t);
cesarcazal 0:2514f1c72462 61 uint8_t requestFrom(int, int);
cesarcazal 0:2514f1c72462 62 uint8_t requestFrom(int, int, int);
cesarcazal 0:2514f1c72462 63 int write(uint8_t);
cesarcazal 0:2514f1c72462 64 int write(const uint8_t *, size_t);
cesarcazal 0:2514f1c72462 65 int available(void);
cesarcazal 0:2514f1c72462 66 int read(void);
cesarcazal 0:2514f1c72462 67
cesarcazal 0:2514f1c72462 68 private :
cesarcazal 0:2514f1c72462 69 uint8_t RX_Buffer[BUFF_MAX_LENGTH];
cesarcazal 0:2514f1c72462 70 uint8_t RX_BufferIndex;
cesarcazal 0:2514f1c72462 71 uint8_t RX_BufferLength;
cesarcazal 0:2514f1c72462 72
cesarcazal 0:2514f1c72462 73 uint8_t TX_Buffer[BUFF_MAX_LENGTH];
cesarcazal 0:2514f1c72462 74 uint8_t TX_BufferIndex;
cesarcazal 0:2514f1c72462 75 uint8_t TX_BufferLength;
cesarcazal 0:2514f1c72462 76
cesarcazal 0:2514f1c72462 77 NRF_TWI_Type *twi;
cesarcazal 0:2514f1c72462 78
cesarcazal 0:2514f1c72462 79 uint8_t PPI_channel;
cesarcazal 0:2514f1c72462 80 uint8_t Transform_Addr;
cesarcazal 0:2514f1c72462 81
cesarcazal 0:2514f1c72462 82 uint32_t SDA_Pin;
cesarcazal 0:2514f1c72462 83 uint32_t SCL_Pin;
cesarcazal 0:2514f1c72462 84
cesarcazal 0:2514f1c72462 85 uint32_t twi_frequency;
cesarcazal 0:2514f1c72462 86
cesarcazal 0:2514f1c72462 87 enum TwoWireStatus {
cesarcazal 0:2514f1c72462 88 UNINITIALIZED,
cesarcazal 0:2514f1c72462 89 MASTER_IDLE,
cesarcazal 0:2514f1c72462 90 MASTER_SEND,
cesarcazal 0:2514f1c72462 91 MASTER_RECV,
cesarcazal 0:2514f1c72462 92 SLAVE_IDLE,
cesarcazal 0:2514f1c72462 93 SLAVE_RECV,
cesarcazal 0:2514f1c72462 94 SLAVE_SEND
cesarcazal 0:2514f1c72462 95 };
cesarcazal 0:2514f1c72462 96 TwoWireStatus twi_status;
cesarcazal 0:2514f1c72462 97
cesarcazal 0:2514f1c72462 98 bool twi_master_clear_bus(void);
cesarcazal 0:2514f1c72462 99 bool twi_master_init(void);
cesarcazal 0:2514f1c72462 100 uint8_t twi_master_read(uint8_t *data, uint8_t data_length, uint8_t issue_stop_condition);
cesarcazal 0:2514f1c72462 101 uint8_t twi_master_write(uint8_t *data, uint8_t data_length, uint8_t issue_stop_condition);
cesarcazal 0:2514f1c72462 102 };
cesarcazal 0:2514f1c72462 103
cesarcazal 0:2514f1c72462 104 #endif