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