I2C example for BLE Nano and RedBearLab nRF51822. It demonstrates how to use I2C to communicate with AT24C512 EEPROM device.

Dependencies:   mbed BLE_API nRF51822

Committer:
RedBearLab
Date:
Thu Jan 07 02:55:12 2016 +0000
Revision:
2:0298ffee8eba
Parent:
0:34c256ab7309
Update libraries.

Who changed what in which revision?

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