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