mbed library sources

Dependents:   Freedman_v2 Nucleo_i2c_OLED_BME280_copy

Fork of mbed-src by mbed official

Committer:
mbed_official
Date:
Wed Jul 29 09:45:09 2015 +0100
Revision:
598:2d5fc5624619
Synchronized with git revision e87fec7b35d45d8663318a40a4a9fb58f91d0237

Full URL: https://github.com/mbedmicro/mbed/commit/e87fec7b35d45d8663318a40a4a9fb58f91d0237/

Microbit addition

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 598:2d5fc5624619 1 /* Copyright (c) 2009 Nordic Semiconductor. All Rights Reserved.
mbed_official 598:2d5fc5624619 2 *
mbed_official 598:2d5fc5624619 3 * The information contained herein is property of Nordic Semiconductor ASA.
mbed_official 598:2d5fc5624619 4 * Terms and conditions of usage are described in detail in NORDIC
mbed_official 598:2d5fc5624619 5 * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
mbed_official 598:2d5fc5624619 6 *
mbed_official 598:2d5fc5624619 7 * Licensees are granted free, non-transferable use of the information. NO
mbed_official 598:2d5fc5624619 8 * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
mbed_official 598:2d5fc5624619 9 * the file.
mbed_official 598:2d5fc5624619 10 *
mbed_official 598:2d5fc5624619 11 */
mbed_official 598:2d5fc5624619 12
mbed_official 598:2d5fc5624619 13 #include "twi_master.h"
mbed_official 598:2d5fc5624619 14 #include "twi_config.h"
mbed_official 598:2d5fc5624619 15 #include <stdbool.h>
mbed_official 598:2d5fc5624619 16 #include <stdint.h>
mbed_official 598:2d5fc5624619 17 #include "nrf.h"
mbed_official 598:2d5fc5624619 18 #include "nrf_delay.h"
mbed_official 598:2d5fc5624619 19
mbed_official 598:2d5fc5624619 20 /* Max cycles approximately to wait on RXDREADY and TXDREADY event,
mbed_official 598:2d5fc5624619 21 * This is optimized way instead of using timers, this is not power aware. */
mbed_official 598:2d5fc5624619 22 #define MAX_TIMEOUT_LOOPS (20000UL) /**< MAX while loops to wait for RXD/TXD event */
mbed_official 598:2d5fc5624619 23
mbed_official 598:2d5fc5624619 24 static bool twi_master_write(uint8_t * data, uint8_t data_length, bool issue_stop_condition)
mbed_official 598:2d5fc5624619 25 {
mbed_official 598:2d5fc5624619 26 uint32_t timeout = MAX_TIMEOUT_LOOPS; /* max loops to wait for EVENTS_TXDSENT event*/
mbed_official 598:2d5fc5624619 27
mbed_official 598:2d5fc5624619 28 if (data_length == 0)
mbed_official 598:2d5fc5624619 29 {
mbed_official 598:2d5fc5624619 30 /* Return false for requesting data of size 0 */
mbed_official 598:2d5fc5624619 31 return false;
mbed_official 598:2d5fc5624619 32 }
mbed_official 598:2d5fc5624619 33
mbed_official 598:2d5fc5624619 34 NRF_TWI1->TXD = *data++;
mbed_official 598:2d5fc5624619 35 NRF_TWI1->TASKS_STARTTX = 1;
mbed_official 598:2d5fc5624619 36
mbed_official 598:2d5fc5624619 37 /** @snippet [TWI HW master write] */
mbed_official 598:2d5fc5624619 38 while (true)
mbed_official 598:2d5fc5624619 39 {
mbed_official 598:2d5fc5624619 40 while (NRF_TWI1->EVENTS_TXDSENT == 0 && NRF_TWI1->EVENTS_ERROR == 0 && (--timeout))
mbed_official 598:2d5fc5624619 41 {
mbed_official 598:2d5fc5624619 42 // Do nothing.
mbed_official 598:2d5fc5624619 43 }
mbed_official 598:2d5fc5624619 44
mbed_official 598:2d5fc5624619 45 if (timeout == 0 || NRF_TWI1->EVENTS_ERROR != 0)
mbed_official 598:2d5fc5624619 46 {
mbed_official 598:2d5fc5624619 47 // Recover the peripheral as indicated by PAN 56: "TWI: TWI module lock-up." found at
mbed_official 598:2d5fc5624619 48 // Product Anomaly Notification document found at
mbed_official 598:2d5fc5624619 49 // https://www.nordicsemi.com/eng/Products/Bluetooth-R-low-energy/nRF51822/#Downloads
mbed_official 598:2d5fc5624619 50 NRF_TWI1->EVENTS_ERROR = 0;
mbed_official 598:2d5fc5624619 51 NRF_TWI1->ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos;
mbed_official 598:2d5fc5624619 52 NRF_TWI1->POWER = 0;
mbed_official 598:2d5fc5624619 53 nrf_delay_us(5);
mbed_official 598:2d5fc5624619 54 NRF_TWI1->POWER = 1;
mbed_official 598:2d5fc5624619 55 NRF_TWI1->ENABLE = TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos;
mbed_official 598:2d5fc5624619 56
mbed_official 598:2d5fc5624619 57 (void)twi_master_init_and_clear();
mbed_official 598:2d5fc5624619 58
mbed_official 598:2d5fc5624619 59 return false;
mbed_official 598:2d5fc5624619 60 }
mbed_official 598:2d5fc5624619 61 NRF_TWI1->EVENTS_TXDSENT = 0;
mbed_official 598:2d5fc5624619 62 if (--data_length == 0)
mbed_official 598:2d5fc5624619 63 {
mbed_official 598:2d5fc5624619 64 break;
mbed_official 598:2d5fc5624619 65 }
mbed_official 598:2d5fc5624619 66
mbed_official 598:2d5fc5624619 67 NRF_TWI1->TXD = *data++;
mbed_official 598:2d5fc5624619 68 }
mbed_official 598:2d5fc5624619 69 /** @snippet [TWI HW master write] */
mbed_official 598:2d5fc5624619 70
mbed_official 598:2d5fc5624619 71 if (issue_stop_condition)
mbed_official 598:2d5fc5624619 72 {
mbed_official 598:2d5fc5624619 73 NRF_TWI1->EVENTS_STOPPED = 0;
mbed_official 598:2d5fc5624619 74 NRF_TWI1->TASKS_STOP = 1;
mbed_official 598:2d5fc5624619 75 /* Wait until stop sequence is sent */
mbed_official 598:2d5fc5624619 76 while(NRF_TWI1->EVENTS_STOPPED == 0)
mbed_official 598:2d5fc5624619 77 {
mbed_official 598:2d5fc5624619 78 // Do nothing.
mbed_official 598:2d5fc5624619 79 }
mbed_official 598:2d5fc5624619 80 }
mbed_official 598:2d5fc5624619 81 return true;
mbed_official 598:2d5fc5624619 82 }
mbed_official 598:2d5fc5624619 83
mbed_official 598:2d5fc5624619 84
mbed_official 598:2d5fc5624619 85 /** @brief Function for read by twi_master.
mbed_official 598:2d5fc5624619 86 */
mbed_official 598:2d5fc5624619 87 static bool twi_master_read(uint8_t * data, uint8_t data_length, bool issue_stop_condition)
mbed_official 598:2d5fc5624619 88 {
mbed_official 598:2d5fc5624619 89 uint32_t timeout = MAX_TIMEOUT_LOOPS; /* max loops to wait for RXDREADY event*/
mbed_official 598:2d5fc5624619 90
mbed_official 598:2d5fc5624619 91 if (data_length == 0)
mbed_official 598:2d5fc5624619 92 {
mbed_official 598:2d5fc5624619 93 /* Return false for requesting data of size 0 */
mbed_official 598:2d5fc5624619 94 return false;
mbed_official 598:2d5fc5624619 95 }
mbed_official 598:2d5fc5624619 96 else if (data_length == 1)
mbed_official 598:2d5fc5624619 97 {
mbed_official 598:2d5fc5624619 98 NRF_PPI->CH[0].TEP = (uint32_t)&NRF_TWI1->TASKS_STOP;
mbed_official 598:2d5fc5624619 99 }
mbed_official 598:2d5fc5624619 100 else
mbed_official 598:2d5fc5624619 101 {
mbed_official 598:2d5fc5624619 102 NRF_PPI->CH[0].TEP = (uint32_t)&NRF_TWI1->TASKS_SUSPEND;
mbed_official 598:2d5fc5624619 103 }
mbed_official 598:2d5fc5624619 104
mbed_official 598:2d5fc5624619 105 NRF_PPI->CHENSET = PPI_CHENSET_CH0_Msk;
mbed_official 598:2d5fc5624619 106 NRF_TWI1->EVENTS_RXDREADY = 0;
mbed_official 598:2d5fc5624619 107 NRF_TWI1->TASKS_STARTRX = 1;
mbed_official 598:2d5fc5624619 108
mbed_official 598:2d5fc5624619 109 /** @snippet [TWI HW master read] */
mbed_official 598:2d5fc5624619 110 while (true)
mbed_official 598:2d5fc5624619 111 {
mbed_official 598:2d5fc5624619 112 while (NRF_TWI1->EVENTS_RXDREADY == 0 && NRF_TWI1->EVENTS_ERROR == 0 && (--timeout))
mbed_official 598:2d5fc5624619 113 {
mbed_official 598:2d5fc5624619 114 // Do nothing.
mbed_official 598:2d5fc5624619 115 }
mbed_official 598:2d5fc5624619 116 NRF_TWI1->EVENTS_RXDREADY = 0;
mbed_official 598:2d5fc5624619 117
mbed_official 598:2d5fc5624619 118 if (timeout == 0 || NRF_TWI1->EVENTS_ERROR != 0)
mbed_official 598:2d5fc5624619 119 {
mbed_official 598:2d5fc5624619 120 // Recover the peripheral as indicated by PAN 56: "TWI: TWI module lock-up." found at
mbed_official 598:2d5fc5624619 121 // Product Anomaly Notification document found at
mbed_official 598:2d5fc5624619 122 // https://www.nordicsemi.com/eng/Products/Bluetooth-R-low-energy/nRF51822/#Downloads
mbed_official 598:2d5fc5624619 123 NRF_TWI1->EVENTS_ERROR = 0;
mbed_official 598:2d5fc5624619 124 NRF_TWI1->ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos;
mbed_official 598:2d5fc5624619 125 NRF_TWI1->POWER = 0;
mbed_official 598:2d5fc5624619 126 nrf_delay_us(5);
mbed_official 598:2d5fc5624619 127 NRF_TWI1->POWER = 1;
mbed_official 598:2d5fc5624619 128 NRF_TWI1->ENABLE = TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos;
mbed_official 598:2d5fc5624619 129
mbed_official 598:2d5fc5624619 130 (void)twi_master_init_and_clear();
mbed_official 598:2d5fc5624619 131
mbed_official 598:2d5fc5624619 132 return false;
mbed_official 598:2d5fc5624619 133 }
mbed_official 598:2d5fc5624619 134
mbed_official 598:2d5fc5624619 135 *data++ = NRF_TWI1->RXD;
mbed_official 598:2d5fc5624619 136
mbed_official 598:2d5fc5624619 137 /* Configure PPI to stop TWI master before we get last BB event */
mbed_official 598:2d5fc5624619 138 if (--data_length == 1)
mbed_official 598:2d5fc5624619 139 {
mbed_official 598:2d5fc5624619 140 NRF_PPI->CH[0].TEP = (uint32_t)&NRF_TWI1->TASKS_STOP;
mbed_official 598:2d5fc5624619 141 }
mbed_official 598:2d5fc5624619 142
mbed_official 598:2d5fc5624619 143 if (data_length == 0)
mbed_official 598:2d5fc5624619 144 {
mbed_official 598:2d5fc5624619 145 break;
mbed_official 598:2d5fc5624619 146 }
mbed_official 598:2d5fc5624619 147
mbed_official 598:2d5fc5624619 148 // Recover the peripheral as indicated by PAN 56: "TWI: TWI module lock-up." found at
mbed_official 598:2d5fc5624619 149 // Product Anomaly Notification document found at
mbed_official 598:2d5fc5624619 150 // https://www.nordicsemi.com/eng/Products/Bluetooth-R-low-energy/nRF51822/#Downloads
mbed_official 598:2d5fc5624619 151 nrf_delay_us(20);
mbed_official 598:2d5fc5624619 152 NRF_TWI1->TASKS_RESUME = 1;
mbed_official 598:2d5fc5624619 153 }
mbed_official 598:2d5fc5624619 154 /** @snippet [TWI HW master read] */
mbed_official 598:2d5fc5624619 155
mbed_official 598:2d5fc5624619 156 /* Wait until stop sequence is sent */
mbed_official 598:2d5fc5624619 157 while(NRF_TWI1->EVENTS_STOPPED == 0)
mbed_official 598:2d5fc5624619 158 {
mbed_official 598:2d5fc5624619 159 // Do nothing.
mbed_official 598:2d5fc5624619 160 }
mbed_official 598:2d5fc5624619 161 NRF_TWI1->EVENTS_STOPPED = 0;
mbed_official 598:2d5fc5624619 162
mbed_official 598:2d5fc5624619 163 NRF_PPI->CHENCLR = PPI_CHENCLR_CH0_Msk;
mbed_official 598:2d5fc5624619 164 return true;
mbed_official 598:2d5fc5624619 165 }
mbed_official 598:2d5fc5624619 166
mbed_official 598:2d5fc5624619 167
mbed_official 598:2d5fc5624619 168 /**
mbed_official 598:2d5fc5624619 169 * @brief Function for detecting stuck slaves (SDA = 0 and SCL = 1) and tries to clear the bus.
mbed_official 598:2d5fc5624619 170 *
mbed_official 598:2d5fc5624619 171 * @return
mbed_official 598:2d5fc5624619 172 * @retval false Bus is stuck.
mbed_official 598:2d5fc5624619 173 * @retval true Bus is clear.
mbed_official 598:2d5fc5624619 174 */
mbed_official 598:2d5fc5624619 175 static bool twi_master_clear_bus(void)
mbed_official 598:2d5fc5624619 176 {
mbed_official 598:2d5fc5624619 177 uint32_t twi_state;
mbed_official 598:2d5fc5624619 178 bool bus_clear;
mbed_official 598:2d5fc5624619 179 uint32_t clk_pin_config;
mbed_official 598:2d5fc5624619 180 uint32_t data_pin_config;
mbed_official 598:2d5fc5624619 181
mbed_official 598:2d5fc5624619 182 // Save and disable TWI hardware so software can take control over the pins.
mbed_official 598:2d5fc5624619 183 twi_state = NRF_TWI1->ENABLE;
mbed_official 598:2d5fc5624619 184 NRF_TWI1->ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos;
mbed_official 598:2d5fc5624619 185
mbed_official 598:2d5fc5624619 186 clk_pin_config = \
mbed_official 598:2d5fc5624619 187 NRF_GPIO->PIN_CNF[TWI_MASTER_CONFIG_CLOCK_PIN_NUMBER];
mbed_official 598:2d5fc5624619 188 NRF_GPIO->PIN_CNF[TWI_MASTER_CONFIG_CLOCK_PIN_NUMBER] = \
mbed_official 598:2d5fc5624619 189 (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) \
mbed_official 598:2d5fc5624619 190 | (GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos) \
mbed_official 598:2d5fc5624619 191 | (GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) \
mbed_official 598:2d5fc5624619 192 | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) \
mbed_official 598:2d5fc5624619 193 | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos);
mbed_official 598:2d5fc5624619 194
mbed_official 598:2d5fc5624619 195 data_pin_config = \
mbed_official 598:2d5fc5624619 196 NRF_GPIO->PIN_CNF[TWI_MASTER_CONFIG_DATA_PIN_NUMBER];
mbed_official 598:2d5fc5624619 197 NRF_GPIO->PIN_CNF[TWI_MASTER_CONFIG_DATA_PIN_NUMBER] = \
mbed_official 598:2d5fc5624619 198 (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) \
mbed_official 598:2d5fc5624619 199 | (GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos) \
mbed_official 598:2d5fc5624619 200 | (GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) \
mbed_official 598:2d5fc5624619 201 | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) \
mbed_official 598:2d5fc5624619 202 | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos);
mbed_official 598:2d5fc5624619 203
mbed_official 598:2d5fc5624619 204 TWI_SDA_HIGH();
mbed_official 598:2d5fc5624619 205 TWI_SCL_HIGH();
mbed_official 598:2d5fc5624619 206 TWI_DELAY();
mbed_official 598:2d5fc5624619 207
mbed_official 598:2d5fc5624619 208 if ((TWI_SDA_READ() == 1) && (TWI_SCL_READ() == 1))
mbed_official 598:2d5fc5624619 209 {
mbed_official 598:2d5fc5624619 210 bus_clear = true;
mbed_official 598:2d5fc5624619 211 }
mbed_official 598:2d5fc5624619 212 else
mbed_official 598:2d5fc5624619 213 {
mbed_official 598:2d5fc5624619 214 uint_fast8_t i;
mbed_official 598:2d5fc5624619 215 bus_clear = false;
mbed_official 598:2d5fc5624619 216
mbed_official 598:2d5fc5624619 217 // Clock max 18 pulses worst case scenario(9 for master to send the rest of command and 9
mbed_official 598:2d5fc5624619 218 // for slave to respond) to SCL line and wait for SDA come high.
mbed_official 598:2d5fc5624619 219 for (i=18; i--;)
mbed_official 598:2d5fc5624619 220 {
mbed_official 598:2d5fc5624619 221 TWI_SCL_LOW();
mbed_official 598:2d5fc5624619 222 TWI_DELAY();
mbed_official 598:2d5fc5624619 223 TWI_SCL_HIGH();
mbed_official 598:2d5fc5624619 224 TWI_DELAY();
mbed_official 598:2d5fc5624619 225
mbed_official 598:2d5fc5624619 226 if (TWI_SDA_READ() == 1)
mbed_official 598:2d5fc5624619 227 {
mbed_official 598:2d5fc5624619 228 bus_clear = true;
mbed_official 598:2d5fc5624619 229 break;
mbed_official 598:2d5fc5624619 230 }
mbed_official 598:2d5fc5624619 231 }
mbed_official 598:2d5fc5624619 232 }
mbed_official 598:2d5fc5624619 233
mbed_official 598:2d5fc5624619 234 NRF_GPIO->PIN_CNF[TWI_MASTER_CONFIG_CLOCK_PIN_NUMBER] = clk_pin_config;
mbed_official 598:2d5fc5624619 235 NRF_GPIO->PIN_CNF[TWI_MASTER_CONFIG_DATA_PIN_NUMBER] = data_pin_config;
mbed_official 598:2d5fc5624619 236
mbed_official 598:2d5fc5624619 237 NRF_TWI1->ENABLE = twi_state;
mbed_official 598:2d5fc5624619 238
mbed_official 598:2d5fc5624619 239 return bus_clear;
mbed_official 598:2d5fc5624619 240 }
mbed_official 598:2d5fc5624619 241
mbed_official 598:2d5fc5624619 242
mbed_official 598:2d5fc5624619 243 /** @brief Function for initializing the twi_master.
mbed_official 598:2d5fc5624619 244 */
mbed_official 598:2d5fc5624619 245 bool twi_master_init_and_clear(void)
mbed_official 598:2d5fc5624619 246 {
mbed_official 598:2d5fc5624619 247 /* To secure correct signal levels on the pins used by the TWI
mbed_official 598:2d5fc5624619 248 master when the system is in OFF mode, and when the TWI master is
mbed_official 598:2d5fc5624619 249 disabled, these pins must be configured in the GPIO peripheral.
mbed_official 598:2d5fc5624619 250 */
mbed_official 598:2d5fc5624619 251 NRF_GPIO->PIN_CNF[TWI_MASTER_CONFIG_CLOCK_PIN_NUMBER] = \
mbed_official 598:2d5fc5624619 252 (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) \
mbed_official 598:2d5fc5624619 253 | (GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos) \
mbed_official 598:2d5fc5624619 254 | (GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) \
mbed_official 598:2d5fc5624619 255 | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) \
mbed_official 598:2d5fc5624619 256 | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos);
mbed_official 598:2d5fc5624619 257
mbed_official 598:2d5fc5624619 258 NRF_GPIO->PIN_CNF[TWI_MASTER_CONFIG_DATA_PIN_NUMBER] = \
mbed_official 598:2d5fc5624619 259 (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) \
mbed_official 598:2d5fc5624619 260 | (GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos) \
mbed_official 598:2d5fc5624619 261 | (GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) \
mbed_official 598:2d5fc5624619 262 | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) \
mbed_official 598:2d5fc5624619 263 | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos);
mbed_official 598:2d5fc5624619 264
mbed_official 598:2d5fc5624619 265 NRF_TWI1->EVENTS_RXDREADY = 0;
mbed_official 598:2d5fc5624619 266 NRF_TWI1->EVENTS_TXDSENT = 0;
mbed_official 598:2d5fc5624619 267 NRF_TWI1->PSELSCL = TWI_MASTER_CONFIG_CLOCK_PIN_NUMBER;
mbed_official 598:2d5fc5624619 268 NRF_TWI1->PSELSDA = TWI_MASTER_CONFIG_DATA_PIN_NUMBER;
mbed_official 598:2d5fc5624619 269 NRF_TWI1->FREQUENCY = TWI_FREQUENCY_FREQUENCY_K100 << TWI_FREQUENCY_FREQUENCY_Pos;
mbed_official 598:2d5fc5624619 270 NRF_PPI->CH[0].EEP = (uint32_t)&NRF_TWI1->EVENTS_BB;
mbed_official 598:2d5fc5624619 271 NRF_PPI->CH[0].TEP = (uint32_t)&NRF_TWI1->TASKS_SUSPEND;
mbed_official 598:2d5fc5624619 272 NRF_PPI->CHENCLR = PPI_CHENCLR_CH0_Msk;
mbed_official 598:2d5fc5624619 273 NRF_TWI1->ENABLE = TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos;
mbed_official 598:2d5fc5624619 274
mbed_official 598:2d5fc5624619 275 return twi_master_clear_bus();
mbed_official 598:2d5fc5624619 276 }
mbed_official 598:2d5fc5624619 277
mbed_official 598:2d5fc5624619 278
mbed_official 598:2d5fc5624619 279 /** @brief Function for transfer by twi_master.
mbed_official 598:2d5fc5624619 280 */
mbed_official 598:2d5fc5624619 281 bool twi_master_transfer(uint8_t address,
mbed_official 598:2d5fc5624619 282 uint8_t * data,
mbed_official 598:2d5fc5624619 283 uint8_t data_length,
mbed_official 598:2d5fc5624619 284 bool issue_stop_condition)
mbed_official 598:2d5fc5624619 285 {
mbed_official 598:2d5fc5624619 286 bool transfer_succeeded = false;
mbed_official 598:2d5fc5624619 287 if (data_length > 0 && twi_master_clear_bus())
mbed_official 598:2d5fc5624619 288 {
mbed_official 598:2d5fc5624619 289 NRF_TWI1->ADDRESS = (address >> 1);
mbed_official 598:2d5fc5624619 290
mbed_official 598:2d5fc5624619 291 if ((address & TWI_READ_BIT))
mbed_official 598:2d5fc5624619 292 {
mbed_official 598:2d5fc5624619 293 transfer_succeeded = twi_master_read(data, data_length, issue_stop_condition);
mbed_official 598:2d5fc5624619 294 }
mbed_official 598:2d5fc5624619 295 else
mbed_official 598:2d5fc5624619 296 {
mbed_official 598:2d5fc5624619 297 transfer_succeeded = twi_master_write(data, data_length, issue_stop_condition);
mbed_official 598:2d5fc5624619 298 }
mbed_official 598:2d5fc5624619 299 }
mbed_official 598:2d5fc5624619 300 return transfer_succeeded;
mbed_official 598:2d5fc5624619 301 }
mbed_official 598:2d5fc5624619 302
mbed_official 598:2d5fc5624619 303 /*lint --flb "Leave library region" */