mbed library sources
Dependents: Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more
Superseded
This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.
Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.
If you are looking for a stable and tested release, please import one of the official mbed library releases:
Import librarymbed
The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.
targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/i2c_api.c@621:9c82b0f79f3d, 2015-09-11 (annotated)
- Committer:
- mbed_official
- Date:
- Fri Sep 11 09:30:09 2015 +0100
- Revision:
- 621:9c82b0f79f3d
- Parent:
- 598:2d5fc5624619
Synchronized with git revision 6c1d63e069ab9bd86de92e8296ca783681257538
Full URL: https://github.com/mbedmicro/mbed/commit/6c1d63e069ab9bd86de92e8296ca783681257538/
ignore target files not supported by the yotta module
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 85:e1a8e879a6a9 | 1 | /* mbed Microcontroller Library |
mbed_official | 104:a6a92e2e5a92 | 2 | * Copyright (c) 2013 Nordic Semiconductor |
mbed_official | 85:e1a8e879a6a9 | 3 | * |
mbed_official | 85:e1a8e879a6a9 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
mbed_official | 85:e1a8e879a6a9 | 5 | * you may not use this file except in compliance with the License. |
mbed_official | 85:e1a8e879a6a9 | 6 | * You may obtain a copy of the License at |
mbed_official | 85:e1a8e879a6a9 | 7 | * |
mbed_official | 85:e1a8e879a6a9 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
mbed_official | 85:e1a8e879a6a9 | 9 | * |
mbed_official | 85:e1a8e879a6a9 | 10 | * Unless required by applicable law or agreed to in writing, software |
mbed_official | 85:e1a8e879a6a9 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
mbed_official | 85:e1a8e879a6a9 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
mbed_official | 85:e1a8e879a6a9 | 13 | * See the License for the specific language governing permissions and |
mbed_official | 85:e1a8e879a6a9 | 14 | * limitations under the License. |
mbed_official | 85:e1a8e879a6a9 | 15 | */ |
mbed_official | 227:7bd0639b8911 | 16 | #include "mbed_assert.h" |
mbed_official | 85:e1a8e879a6a9 | 17 | #include "i2c_api.h" |
mbed_official | 85:e1a8e879a6a9 | 18 | #include "cmsis.h" |
mbed_official | 85:e1a8e879a6a9 | 19 | #include "pinmap.h" |
mbed_official | 598:2d5fc5624619 | 20 | #include "twi_master.h" |
mbed_official | 395:bfce16e86ea4 | 21 | #include "mbed_error.h" |
mbed_official | 85:e1a8e879a6a9 | 22 | |
mbed_official | 395:bfce16e86ea4 | 23 | // nRF51822's I2C_0 and SPI_0 (I2C_1, SPI_1 and SPIS1) share the same address. |
mbed_official | 395:bfce16e86ea4 | 24 | // They can't be used at the same time. So we use two global variable to track the usage. |
mbed_official | 395:bfce16e86ea4 | 25 | // See nRF51822 address information at nRF51822_PS v2.0.pdf - Table 15 Peripheral instance reference |
mbed_official | 395:bfce16e86ea4 | 26 | volatile i2c_spi_peripheral_t i2c0_spi0_peripheral = {0, 0, 0, 0}; |
mbed_official | 395:bfce16e86ea4 | 27 | volatile i2c_spi_peripheral_t i2c1_spi1_peripheral = {0, 0, 0, 0}; |
mbed_official | 85:e1a8e879a6a9 | 28 | |
mbed_official | 300:55638feb26a4 | 29 | void i2c_interface_enable(i2c_t *obj) |
mbed_official | 300:55638feb26a4 | 30 | { |
mbed_official | 85:e1a8e879a6a9 | 31 | obj->i2c->ENABLE = (TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos); |
mbed_official | 85:e1a8e879a6a9 | 32 | } |
mbed_official | 85:e1a8e879a6a9 | 33 | |
mbed_official | 300:55638feb26a4 | 34 | void twi_master_init(i2c_t *obj, PinName sda, PinName scl, int frequency) |
mbed_official | 300:55638feb26a4 | 35 | { |
mbed_official | 300:55638feb26a4 | 36 | NRF_GPIO->PIN_CNF[scl] = ((GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) | |
mbed_official | 300:55638feb26a4 | 37 | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) | |
mbed_official | 300:55638feb26a4 | 38 | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) | |
mbed_official | 300:55638feb26a4 | 39 | (GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos) | |
mbed_official | 300:55638feb26a4 | 40 | (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos)); |
mbed_official | 85:e1a8e879a6a9 | 41 | |
mbed_official | 300:55638feb26a4 | 42 | NRF_GPIO->PIN_CNF[sda] = ((GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) | |
mbed_official | 300:55638feb26a4 | 43 | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) | |
mbed_official | 300:55638feb26a4 | 44 | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) | |
mbed_official | 300:55638feb26a4 | 45 | (GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos) | |
mbed_official | 300:55638feb26a4 | 46 | (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos)); |
mbed_official | 85:e1a8e879a6a9 | 47 | |
mbed_official | 300:55638feb26a4 | 48 | obj->i2c->PSELSCL = scl; |
mbed_official | 300:55638feb26a4 | 49 | obj->i2c->PSELSDA = sda; |
mbed_official | 85:e1a8e879a6a9 | 50 | // set default frequency at 100k |
mbed_official | 85:e1a8e879a6a9 | 51 | i2c_frequency(obj, frequency); |
mbed_official | 85:e1a8e879a6a9 | 52 | i2c_interface_enable(obj); |
mbed_official | 85:e1a8e879a6a9 | 53 | } |
mbed_official | 300:55638feb26a4 | 54 | |
mbed_official | 300:55638feb26a4 | 55 | void i2c_init(i2c_t *obj, PinName sda, PinName scl) |
mbed_official | 300:55638feb26a4 | 56 | { |
mbed_official | 598:2d5fc5624619 | 57 | twi_master_init_and_clear(); |
mbed_official | 598:2d5fc5624619 | 58 | NRF_TWI_Type *i2c = NULL; |
mbed_official | 395:bfce16e86ea4 | 59 | |
mbed_official | 395:bfce16e86ea4 | 60 | if (i2c0_spi0_peripheral.usage == I2C_SPI_PERIPHERAL_FOR_I2C && |
mbed_official | 395:bfce16e86ea4 | 61 | i2c0_spi0_peripheral.sda_mosi == (uint8_t)sda && |
mbed_official | 395:bfce16e86ea4 | 62 | i2c0_spi0_peripheral.scl_miso == (uint8_t)scl) { |
mbed_official | 395:bfce16e86ea4 | 63 | // The I2C with the same pins is already initialized |
mbed_official | 395:bfce16e86ea4 | 64 | i2c = (NRF_TWI_Type *)I2C_0; |
mbed_official | 395:bfce16e86ea4 | 65 | obj->peripheral = 0x1; |
mbed_official | 395:bfce16e86ea4 | 66 | } else if (i2c1_spi1_peripheral.usage == I2C_SPI_PERIPHERAL_FOR_I2C && |
mbed_official | 395:bfce16e86ea4 | 67 | i2c1_spi1_peripheral.sda_mosi == (uint8_t)sda && |
mbed_official | 395:bfce16e86ea4 | 68 | i2c1_spi1_peripheral.scl_miso == (uint8_t)scl) { |
mbed_official | 395:bfce16e86ea4 | 69 | // The I2C with the same pins is already initialized |
mbed_official | 395:bfce16e86ea4 | 70 | i2c = (NRF_TWI_Type *)I2C_1; |
mbed_official | 395:bfce16e86ea4 | 71 | obj->peripheral = 0x2; |
mbed_official | 395:bfce16e86ea4 | 72 | } else if (i2c0_spi0_peripheral.usage == 0) { |
mbed_official | 395:bfce16e86ea4 | 73 | i2c0_spi0_peripheral.usage = I2C_SPI_PERIPHERAL_FOR_I2C; |
mbed_official | 395:bfce16e86ea4 | 74 | i2c0_spi0_peripheral.sda_mosi = (uint8_t)sda; |
mbed_official | 395:bfce16e86ea4 | 75 | i2c0_spi0_peripheral.scl_miso = (uint8_t)scl; |
mbed_official | 395:bfce16e86ea4 | 76 | |
mbed_official | 395:bfce16e86ea4 | 77 | i2c = (NRF_TWI_Type *)I2C_0; |
mbed_official | 395:bfce16e86ea4 | 78 | obj->peripheral = 0x1; |
mbed_official | 395:bfce16e86ea4 | 79 | } else if (i2c1_spi1_peripheral.usage == 0) { |
mbed_official | 395:bfce16e86ea4 | 80 | i2c1_spi1_peripheral.usage = I2C_SPI_PERIPHERAL_FOR_I2C; |
mbed_official | 395:bfce16e86ea4 | 81 | i2c1_spi1_peripheral.sda_mosi = (uint8_t)sda; |
mbed_official | 395:bfce16e86ea4 | 82 | i2c1_spi1_peripheral.scl_miso = (uint8_t)scl; |
mbed_official | 395:bfce16e86ea4 | 83 | |
mbed_official | 395:bfce16e86ea4 | 84 | i2c = (NRF_TWI_Type *)I2C_1; |
mbed_official | 395:bfce16e86ea4 | 85 | obj->peripheral = 0x2; |
mbed_official | 395:bfce16e86ea4 | 86 | } else { |
mbed_official | 395:bfce16e86ea4 | 87 | // No available peripheral |
mbed_official | 395:bfce16e86ea4 | 88 | error("No available I2C"); |
mbed_official | 395:bfce16e86ea4 | 89 | } |
mbed_official | 300:55638feb26a4 | 90 | |
mbed_official | 395:bfce16e86ea4 | 91 | obj->i2c = i2c; |
mbed_official | 300:55638feb26a4 | 92 | obj->scl = scl; |
mbed_official | 300:55638feb26a4 | 93 | obj->sda = sda; |
mbed_official | 85:e1a8e879a6a9 | 94 | obj->i2c->EVENTS_ERROR = 0; |
mbed_official | 227:7bd0639b8911 | 95 | obj->i2c->ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos; |
mbed_official | 227:7bd0639b8911 | 96 | obj->i2c->POWER = 0; |
mbed_official | 300:55638feb26a4 | 97 | |
mbed_official | 300:55638feb26a4 | 98 | for (int i = 0; i<100; i++) { |
mbed_official | 85:e1a8e879a6a9 | 99 | } |
mbed_official | 300:55638feb26a4 | 100 | |
mbed_official | 300:55638feb26a4 | 101 | obj->i2c->POWER = 1; |
mbed_official | 300:55638feb26a4 | 102 | twi_master_init(obj, sda, scl, 100000); |
mbed_official | 85:e1a8e879a6a9 | 103 | } |
mbed_official | 300:55638feb26a4 | 104 | |
mbed_official | 300:55638feb26a4 | 105 | void i2c_reset(i2c_t *obj) |
mbed_official | 300:55638feb26a4 | 106 | { |
mbed_official | 85:e1a8e879a6a9 | 107 | obj->i2c->EVENTS_ERROR = 0; |
mbed_official | 227:7bd0639b8911 | 108 | obj->i2c->ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos; |
mbed_official | 227:7bd0639b8911 | 109 | obj->i2c->POWER = 0; |
mbed_official | 300:55638feb26a4 | 110 | for (int i = 0; i<100; i++) { |
mbed_official | 85:e1a8e879a6a9 | 111 | } |
mbed_official | 300:55638feb26a4 | 112 | |
mbed_official | 300:55638feb26a4 | 113 | obj->i2c->POWER = 1; |
mbed_official | 300:55638feb26a4 | 114 | twi_master_init(obj, obj->sda, obj->scl, obj->freq); |
mbed_official | 85:e1a8e879a6a9 | 115 | } |
mbed_official | 85:e1a8e879a6a9 | 116 | |
mbed_official | 300:55638feb26a4 | 117 | int i2c_start(i2c_t *obj) |
mbed_official | 300:55638feb26a4 | 118 | { |
mbed_official | 85:e1a8e879a6a9 | 119 | int status = 0; |
mbed_official | 85:e1a8e879a6a9 | 120 | i2c_reset(obj); |
mbed_official | 314:b682143dd337 | 121 | obj->address_set = 0; |
mbed_official | 85:e1a8e879a6a9 | 122 | return status; |
mbed_official | 85:e1a8e879a6a9 | 123 | } |
mbed_official | 85:e1a8e879a6a9 | 124 | |
mbed_official | 300:55638feb26a4 | 125 | int i2c_stop(i2c_t *obj) |
mbed_official | 300:55638feb26a4 | 126 | { |
mbed_official | 85:e1a8e879a6a9 | 127 | int timeOut = 100000; |
mbed_official | 85:e1a8e879a6a9 | 128 | obj->i2c->EVENTS_STOPPED = 0; |
mbed_official | 85:e1a8e879a6a9 | 129 | // write the stop bit |
mbed_official | 85:e1a8e879a6a9 | 130 | obj->i2c->TASKS_STOP = 1; |
mbed_official | 300:55638feb26a4 | 131 | while (!obj->i2c->EVENTS_STOPPED) { |
mbed_official | 85:e1a8e879a6a9 | 132 | timeOut--; |
mbed_official | 300:55638feb26a4 | 133 | if (timeOut<0) { |
mbed_official | 85:e1a8e879a6a9 | 134 | return 1; |
mbed_official | 300:55638feb26a4 | 135 | } |
mbed_official | 227:7bd0639b8911 | 136 | } |
mbed_official | 314:b682143dd337 | 137 | obj->address_set = 0; |
mbed_official | 85:e1a8e879a6a9 | 138 | i2c_reset(obj); |
mbed_official | 85:e1a8e879a6a9 | 139 | return 0; |
mbed_official | 85:e1a8e879a6a9 | 140 | } |
mbed_official | 85:e1a8e879a6a9 | 141 | |
mbed_official | 300:55638feb26a4 | 142 | int i2c_do_write(i2c_t *obj, int value) |
mbed_official | 300:55638feb26a4 | 143 | { |
mbed_official | 85:e1a8e879a6a9 | 144 | int timeOut = 100000; |
mbed_official | 85:e1a8e879a6a9 | 145 | obj->i2c->TXD = value; |
mbed_official | 300:55638feb26a4 | 146 | while (!obj->i2c->EVENTS_TXDSENT) { |
mbed_official | 85:e1a8e879a6a9 | 147 | timeOut--; |
mbed_official | 300:55638feb26a4 | 148 | if (timeOut<0) { |
mbed_official | 85:e1a8e879a6a9 | 149 | return 1; |
mbed_official | 300:55638feb26a4 | 150 | } |
mbed_official | 85:e1a8e879a6a9 | 151 | } |
mbed_official | 227:7bd0639b8911 | 152 | obj->i2c->EVENTS_TXDSENT = 0; |
mbed_official | 85:e1a8e879a6a9 | 153 | return 0; |
mbed_official | 85:e1a8e879a6a9 | 154 | } |
mbed_official | 85:e1a8e879a6a9 | 155 | |
mbed_official | 300:55638feb26a4 | 156 | int i2c_do_read(i2c_t *obj, char *data, int last) |
mbed_official | 300:55638feb26a4 | 157 | { |
mbed_official | 85:e1a8e879a6a9 | 158 | int timeOut = 100000; |
mbed_official | 177:d57c40a064c8 | 159 | |
mbed_official | 300:55638feb26a4 | 160 | if (last) { |
mbed_official | 314:b682143dd337 | 161 | // To trigger stop task when a byte is received, |
mbed_official | 314:b682143dd337 | 162 | // must be set before resume task. |
mbed_official | 314:b682143dd337 | 163 | obj->i2c->SHORTS = 2; |
mbed_official | 177:d57c40a064c8 | 164 | } |
mbed_official | 314:b682143dd337 | 165 | |
mbed_official | 314:b682143dd337 | 166 | obj->i2c->TASKS_RESUME = 1; |
mbed_official | 314:b682143dd337 | 167 | |
mbed_official | 300:55638feb26a4 | 168 | while (!obj->i2c->EVENTS_RXDREADY) { |
mbed_official | 85:e1a8e879a6a9 | 169 | timeOut--; |
mbed_official | 300:55638feb26a4 | 170 | if (timeOut<0) { |
mbed_official | 85:e1a8e879a6a9 | 171 | return 1; |
mbed_official | 300:55638feb26a4 | 172 | } |
mbed_official | 85:e1a8e879a6a9 | 173 | } |
mbed_official | 300:55638feb26a4 | 174 | obj->i2c->EVENTS_RXDREADY = 0; |
mbed_official | 85:e1a8e879a6a9 | 175 | *data = obj->i2c->RXD; |
mbed_official | 300:55638feb26a4 | 176 | |
mbed_official | 85:e1a8e879a6a9 | 177 | return 0; |
mbed_official | 85:e1a8e879a6a9 | 178 | } |
mbed_official | 85:e1a8e879a6a9 | 179 | |
mbed_official | 300:55638feb26a4 | 180 | void i2c_frequency(i2c_t *obj, int hz) |
mbed_official | 300:55638feb26a4 | 181 | { |
mbed_official | 300:55638feb26a4 | 182 | if (hz<250000) { |
mbed_official | 85:e1a8e879a6a9 | 183 | obj->freq = 100000; |
mbed_official | 85:e1a8e879a6a9 | 184 | obj->i2c->FREQUENCY = (TWI_FREQUENCY_FREQUENCY_K100 << TWI_FREQUENCY_FREQUENCY_Pos); |
mbed_official | 300:55638feb26a4 | 185 | } else if (hz<400000) { |
mbed_official | 85:e1a8e879a6a9 | 186 | obj->freq = 250000; |
mbed_official | 85:e1a8e879a6a9 | 187 | obj->i2c->FREQUENCY = (TWI_FREQUENCY_FREQUENCY_K250 << TWI_FREQUENCY_FREQUENCY_Pos); |
mbed_official | 300:55638feb26a4 | 188 | } else { |
mbed_official | 85:e1a8e879a6a9 | 189 | obj->freq = 400000; |
mbed_official | 85:e1a8e879a6a9 | 190 | obj->i2c->FREQUENCY = (TWI_FREQUENCY_FREQUENCY_K400 << TWI_FREQUENCY_FREQUENCY_Pos); |
mbed_official | 85:e1a8e879a6a9 | 191 | } |
mbed_official | 85:e1a8e879a6a9 | 192 | } |
mbed_official | 85:e1a8e879a6a9 | 193 | |
mbed_official | 300:55638feb26a4 | 194 | int checkError(i2c_t *obj) |
mbed_official | 300:55638feb26a4 | 195 | { |
mbed_official | 300:55638feb26a4 | 196 | if (obj->i2c->EVENTS_ERROR == 1) { |
mbed_official | 300:55638feb26a4 | 197 | if (obj->i2c->ERRORSRC & TWI_ERRORSRC_ANACK_Msk) { |
mbed_official | 300:55638feb26a4 | 198 | obj->i2c->EVENTS_ERROR = 0; |
mbed_official | 300:55638feb26a4 | 199 | obj->i2c->TASKS_STOP = 1; |
mbed_official | 85:e1a8e879a6a9 | 200 | return I2C_ERROR_BUS_BUSY; |
mbed_official | 85:e1a8e879a6a9 | 201 | } |
mbed_official | 300:55638feb26a4 | 202 | |
mbed_official | 300:55638feb26a4 | 203 | obj->i2c->EVENTS_ERROR = 0; |
mbed_official | 300:55638feb26a4 | 204 | obj->i2c->TASKS_STOP = 1; |
mbed_official | 227:7bd0639b8911 | 205 | return I2C_ERROR_NO_SLAVE; |
mbed_official | 227:7bd0639b8911 | 206 | } |
mbed_official | 85:e1a8e879a6a9 | 207 | return 0; |
mbed_official | 85:e1a8e879a6a9 | 208 | } |
mbed_official | 85:e1a8e879a6a9 | 209 | |
mbed_official | 300:55638feb26a4 | 210 | int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) |
mbed_official | 300:55638feb26a4 | 211 | { |
mbed_official | 300:55638feb26a4 | 212 | int status, count, errorResult; |
mbed_official | 300:55638feb26a4 | 213 | obj->i2c->ADDRESS = (address >> 1); |
mbed_official | 314:b682143dd337 | 214 | obj->i2c->SHORTS = 1; // to trigger suspend task when a byte is received |
mbed_official | 85:e1a8e879a6a9 | 215 | obj->i2c->EVENTS_RXDREADY = 0; |
mbed_official | 85:e1a8e879a6a9 | 216 | obj->i2c->TASKS_STARTRX = 1; |
mbed_official | 300:55638feb26a4 | 217 | |
mbed_official | 85:e1a8e879a6a9 | 218 | // Read in all except last byte |
mbed_official | 85:e1a8e879a6a9 | 219 | for (count = 0; count < (length - 1); count++) { |
mbed_official | 300:55638feb26a4 | 220 | status = i2c_do_read(obj, &data[count], 0); |
mbed_official | 227:7bd0639b8911 | 221 | if (status) { |
mbed_official | 85:e1a8e879a6a9 | 222 | errorResult = checkError(obj); |
mbed_official | 85:e1a8e879a6a9 | 223 | i2c_reset(obj); |
mbed_official | 300:55638feb26a4 | 224 | if (errorResult<0) { |
mbed_official | 85:e1a8e879a6a9 | 225 | return errorResult; |
mbed_official | 85:e1a8e879a6a9 | 226 | } |
mbed_official | 85:e1a8e879a6a9 | 227 | return count; |
mbed_official | 85:e1a8e879a6a9 | 228 | } |
mbed_official | 85:e1a8e879a6a9 | 229 | } |
mbed_official | 85:e1a8e879a6a9 | 230 | |
mbed_official | 85:e1a8e879a6a9 | 231 | // read in last byte |
mbed_official | 300:55638feb26a4 | 232 | status = i2c_do_read(obj, &data[length - 1], 1); |
mbed_official | 85:e1a8e879a6a9 | 233 | if (status) { |
mbed_official | 85:e1a8e879a6a9 | 234 | i2c_reset(obj); |
mbed_official | 85:e1a8e879a6a9 | 235 | return length - 1; |
mbed_official | 85:e1a8e879a6a9 | 236 | } |
mbed_official | 85:e1a8e879a6a9 | 237 | // If not repeated start, send stop. |
mbed_official | 85:e1a8e879a6a9 | 238 | if (stop) { |
mbed_official | 300:55638feb26a4 | 239 | while (!obj->i2c->EVENTS_STOPPED) { |
mbed_official | 85:e1a8e879a6a9 | 240 | } |
mbed_official | 85:e1a8e879a6a9 | 241 | obj->i2c->EVENTS_STOPPED = 0; |
mbed_official | 227:7bd0639b8911 | 242 | } |
mbed_official | 85:e1a8e879a6a9 | 243 | return length; |
mbed_official | 85:e1a8e879a6a9 | 244 | } |
mbed_official | 85:e1a8e879a6a9 | 245 | |
mbed_official | 300:55638feb26a4 | 246 | int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) |
mbed_official | 300:55638feb26a4 | 247 | { |
mbed_official | 85:e1a8e879a6a9 | 248 | int status, errorResult; |
mbed_official | 300:55638feb26a4 | 249 | obj->i2c->ADDRESS = (address >> 1); |
mbed_official | 85:e1a8e879a6a9 | 250 | obj->i2c->SHORTS = 0; |
mbed_official | 227:7bd0639b8911 | 251 | obj->i2c->TASKS_STARTTX = 1; |
mbed_official | 300:55638feb26a4 | 252 | |
mbed_official | 300:55638feb26a4 | 253 | for (int i = 0; i<length; i++) { |
mbed_official | 85:e1a8e879a6a9 | 254 | status = i2c_do_write(obj, data[i]); |
mbed_official | 300:55638feb26a4 | 255 | if (status) { |
mbed_official | 85:e1a8e879a6a9 | 256 | i2c_reset(obj); |
mbed_official | 85:e1a8e879a6a9 | 257 | errorResult = checkError(obj); |
mbed_official | 300:55638feb26a4 | 258 | if (errorResult<0) { |
mbed_official | 85:e1a8e879a6a9 | 259 | return errorResult; |
mbed_official | 85:e1a8e879a6a9 | 260 | } |
mbed_official | 85:e1a8e879a6a9 | 261 | return i; |
mbed_official | 85:e1a8e879a6a9 | 262 | } |
mbed_official | 85:e1a8e879a6a9 | 263 | } |
mbed_official | 300:55638feb26a4 | 264 | |
mbed_official | 85:e1a8e879a6a9 | 265 | // If not repeated start, send stop. |
mbed_official | 85:e1a8e879a6a9 | 266 | if (stop) { |
mbed_official | 300:55638feb26a4 | 267 | if (i2c_stop(obj)) { |
mbed_official | 85:e1a8e879a6a9 | 268 | return I2C_ERROR_NO_SLAVE; |
mbed_official | 85:e1a8e879a6a9 | 269 | } |
mbed_official | 85:e1a8e879a6a9 | 270 | } |
mbed_official | 85:e1a8e879a6a9 | 271 | return length; |
mbed_official | 85:e1a8e879a6a9 | 272 | } |
mbed_official | 85:e1a8e879a6a9 | 273 | |
mbed_official | 300:55638feb26a4 | 274 | int i2c_byte_read(i2c_t *obj, int last) |
mbed_official | 300:55638feb26a4 | 275 | { |
mbed_official | 85:e1a8e879a6a9 | 276 | char data; |
mbed_official | 85:e1a8e879a6a9 | 277 | int status; |
mbed_official | 300:55638feb26a4 | 278 | |
mbed_official | 300:55638feb26a4 | 279 | status = i2c_do_read(obj, &data, last); |
mbed_official | 85:e1a8e879a6a9 | 280 | if (status) { |
mbed_official | 85:e1a8e879a6a9 | 281 | i2c_reset(obj); |
mbed_official | 85:e1a8e879a6a9 | 282 | } |
mbed_official | 85:e1a8e879a6a9 | 283 | return data; |
mbed_official | 85:e1a8e879a6a9 | 284 | } |
mbed_official | 85:e1a8e879a6a9 | 285 | |
mbed_official | 300:55638feb26a4 | 286 | int i2c_byte_write(i2c_t *obj, int data) |
mbed_official | 300:55638feb26a4 | 287 | { |
mbed_official | 85:e1a8e879a6a9 | 288 | int status = 0; |
mbed_official | 314:b682143dd337 | 289 | if (!obj->address_set) { |
mbed_official | 314:b682143dd337 | 290 | obj->address_set = 1; |
mbed_official | 300:55638feb26a4 | 291 | obj->i2c->ADDRESS = (data >> 1); |
mbed_official | 300:55638feb26a4 | 292 | |
mbed_official | 300:55638feb26a4 | 293 | if (data & 1) { |
mbed_official | 85:e1a8e879a6a9 | 294 | obj->i2c->EVENTS_RXDREADY = 0; |
mbed_official | 314:b682143dd337 | 295 | obj->i2c->SHORTS = 1; |
mbed_official | 85:e1a8e879a6a9 | 296 | obj->i2c->TASKS_STARTRX = 1; |
mbed_official | 300:55638feb26a4 | 297 | } else { |
mbed_official | 314:b682143dd337 | 298 | obj->i2c->SHORTS = 0; |
mbed_official | 300:55638feb26a4 | 299 | obj->i2c->TASKS_STARTTX = 1; |
mbed_official | 85:e1a8e879a6a9 | 300 | } |
mbed_official | 300:55638feb26a4 | 301 | } else { |
mbed_official | 85:e1a8e879a6a9 | 302 | status = i2c_do_write(obj, data); |
mbed_official | 300:55638feb26a4 | 303 | if (status) { |
mbed_official | 85:e1a8e879a6a9 | 304 | i2c_reset(obj); |
mbed_official | 85:e1a8e879a6a9 | 305 | } |
mbed_official | 85:e1a8e879a6a9 | 306 | } |
mbed_official | 300:55638feb26a4 | 307 | return (1 - status); |
mbed_official | 85:e1a8e879a6a9 | 308 | } |