An I2C Scanner, to test that your I2C can be detected. Works with the NRF51 Development Kit.

Dependencies:   mbed-src-nrf51822

Committer:
Cannonball2134
Date:
Thu Jun 30 07:43:22 2016 +0000
Revision:
1:117c1bb3ce43
Parent:
0:839b7264c387
Working I2C Scanner on the NRF51-DK

Who changed what in which revision?

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