CUED IIA Project
Dependencies: BLE_API mbed nRF51822
Fork of BLE_GATT_Example by
wire.cpp@23:708cc5ef2604, 2017-05-31 (annotated)
- Committer:
- AidasL
- Date:
- Wed May 31 22:13:36 2017 +0000
- Revision:
- 23:708cc5ef2604
- Child:
- 24:baa43caa2f3d
Initial commit;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AidasL | 23:708cc5ef2604 | 1 | |
AidasL | 23:708cc5ef2604 | 2 | /* |
AidasL | 23:708cc5ef2604 | 3 | |
AidasL | 23:708cc5ef2604 | 4 | Copyright (c) 2014 RedBearLab, All rights reserved. |
AidasL | 23:708cc5ef2604 | 5 | |
AidasL | 23:708cc5ef2604 | 6 | This library is free software; you can redistribute it and/or |
AidasL | 23:708cc5ef2604 | 7 | modify it under the terms of the GNU Lesser General Public |
AidasL | 23:708cc5ef2604 | 8 | License as published by the Free Software Foundation; either |
AidasL | 23:708cc5ef2604 | 9 | version 2.1 of the License, or (at your option) any later version. |
AidasL | 23:708cc5ef2604 | 10 | |
AidasL | 23:708cc5ef2604 | 11 | This library is distributed in the hope that it will be useful, |
AidasL | 23:708cc5ef2604 | 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
AidasL | 23:708cc5ef2604 | 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
AidasL | 23:708cc5ef2604 | 14 | See the GNU Lesser General Public License for more details. |
AidasL | 23:708cc5ef2604 | 15 | |
AidasL | 23:708cc5ef2604 | 16 | You should have received a copy of the GNU Lesser General Public |
AidasL | 23:708cc5ef2604 | 17 | License along with this library; if not, write to the Free Software |
AidasL | 23:708cc5ef2604 | 18 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
AidasL | 23:708cc5ef2604 | 19 | |
AidasL | 23:708cc5ef2604 | 20 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
AidasL | 23:708cc5ef2604 | 21 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
AidasL | 23:708cc5ef2604 | 22 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
AidasL | 23:708cc5ef2604 | 23 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
AidasL | 23:708cc5ef2604 | 24 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
AidasL | 23:708cc5ef2604 | 25 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
AidasL | 23:708cc5ef2604 | 26 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
AidasL | 23:708cc5ef2604 | 27 | |
AidasL | 23:708cc5ef2604 | 28 | */ |
AidasL | 23:708cc5ef2604 | 29 | |
AidasL | 23:708cc5ef2604 | 30 | #include "wire.h" |
AidasL | 23:708cc5ef2604 | 31 | #include "nrf_soc.h" |
AidasL | 23:708cc5ef2604 | 32 | #include "nrf_sdm.h" |
AidasL | 23:708cc5ef2604 | 33 | |
AidasL | 23:708cc5ef2604 | 34 | //extern Serial pc; |
AidasL | 23:708cc5ef2604 | 35 | /********************************************************************** |
AidasL | 23:708cc5ef2604 | 36 | name : |
AidasL | 23:708cc5ef2604 | 37 | function : return: 0--SUCCESS, 1--FAIL |
AidasL | 23:708cc5ef2604 | 38 | **********************************************************************/ |
AidasL | 23:708cc5ef2604 | 39 | bool TwoWire::twi_master_clear_bus(void) |
AidasL | 23:708cc5ef2604 | 40 | { |
AidasL | 23:708cc5ef2604 | 41 | bool bus_clear; |
AidasL | 23:708cc5ef2604 | 42 | uint32_t twi_state; |
AidasL | 23:708cc5ef2604 | 43 | uint32_t sck_pin_config; |
AidasL | 23:708cc5ef2604 | 44 | uint32_t sda_pin_config; |
AidasL | 23:708cc5ef2604 | 45 | |
AidasL | 23:708cc5ef2604 | 46 | twi_state = twi->ENABLE; |
AidasL | 23:708cc5ef2604 | 47 | twi->ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos; |
AidasL | 23:708cc5ef2604 | 48 | |
AidasL | 23:708cc5ef2604 | 49 | sck_pin_config = NRF_GPIO->PIN_CNF[SCL_Pin]; |
AidasL | 23:708cc5ef2604 | 50 | NRF_GPIO->PIN_CNF[SCL_Pin] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) |
AidasL | 23:708cc5ef2604 | 51 | | (GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos) |
AidasL | 23:708cc5ef2604 | 52 | | (GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) |
AidasL | 23:708cc5ef2604 | 53 | | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) |
AidasL | 23:708cc5ef2604 | 54 | | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos); |
AidasL | 23:708cc5ef2604 | 55 | |
AidasL | 23:708cc5ef2604 | 56 | sda_pin_config = NRF_GPIO->PIN_CNF[SDA_Pin]; |
AidasL | 23:708cc5ef2604 | 57 | NRF_GPIO->PIN_CNF[SDA_Pin] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) |
AidasL | 23:708cc5ef2604 | 58 | | (GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos) |
AidasL | 23:708cc5ef2604 | 59 | | (GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) |
AidasL | 23:708cc5ef2604 | 60 | | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) |
AidasL | 23:708cc5ef2604 | 61 | | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos); |
AidasL | 23:708cc5ef2604 | 62 | |
AidasL | 23:708cc5ef2604 | 63 | NRF_GPIO->OUTSET = ( 1 << SCL_Pin ); |
AidasL | 23:708cc5ef2604 | 64 | NRF_GPIO->OUTSET = ( 1 << SDA_Pin ); |
AidasL | 23:708cc5ef2604 | 65 | TWI_DELAY(4); |
AidasL | 23:708cc5ef2604 | 66 | |
AidasL | 23:708cc5ef2604 | 67 | if( ( (NRF_GPIO->IN >> SCL_Pin) & 0X1UL ) && ( (NRF_GPIO->IN >> SDA_Pin) & 0x1UL ) ) |
AidasL | 23:708cc5ef2604 | 68 | { |
AidasL | 23:708cc5ef2604 | 69 | bus_clear = 0; |
AidasL | 23:708cc5ef2604 | 70 | } |
AidasL | 23:708cc5ef2604 | 71 | else |
AidasL | 23:708cc5ef2604 | 72 | { |
AidasL | 23:708cc5ef2604 | 73 | uint_fast8_t index; |
AidasL | 23:708cc5ef2604 | 74 | bus_clear = 1; |
AidasL | 23:708cc5ef2604 | 75 | for( index=18; index--;) |
AidasL | 23:708cc5ef2604 | 76 | { |
AidasL | 23:708cc5ef2604 | 77 | NRF_GPIO->OUTCLR = ( 1 << SCL_Pin ); |
AidasL | 23:708cc5ef2604 | 78 | TWI_DELAY(4); |
AidasL | 23:708cc5ef2604 | 79 | NRF_GPIO->OUTSET = ( 1 << SDA_Pin ); |
AidasL | 23:708cc5ef2604 | 80 | TWI_DELAY(4); |
AidasL | 23:708cc5ef2604 | 81 | |
AidasL | 23:708cc5ef2604 | 82 | if( (NRF_GPIO->IN >> SDA_Pin) & 0x1UL == 1 ) |
AidasL | 23:708cc5ef2604 | 83 | { |
AidasL | 23:708cc5ef2604 | 84 | bus_clear = 0; |
AidasL | 23:708cc5ef2604 | 85 | break; |
AidasL | 23:708cc5ef2604 | 86 | } |
AidasL | 23:708cc5ef2604 | 87 | } |
AidasL | 23:708cc5ef2604 | 88 | } |
AidasL | 23:708cc5ef2604 | 89 | |
AidasL | 23:708cc5ef2604 | 90 | NRF_GPIO->PIN_CNF[SCL_Pin] = sck_pin_config; |
AidasL | 23:708cc5ef2604 | 91 | NRF_GPIO->PIN_CNF[SDA_Pin] = sda_pin_config; |
AidasL | 23:708cc5ef2604 | 92 | |
AidasL | 23:708cc5ef2604 | 93 | twi->ENABLE = twi_state; |
AidasL | 23:708cc5ef2604 | 94 | |
AidasL | 23:708cc5ef2604 | 95 | return bus_clear; |
AidasL | 23:708cc5ef2604 | 96 | } |
AidasL | 23:708cc5ef2604 | 97 | |
AidasL | 23:708cc5ef2604 | 98 | /********************************************************************** |
AidasL | 23:708cc5ef2604 | 99 | name : |
AidasL | 23:708cc5ef2604 | 100 | function : return: 0--SUCCESS, 1--FAIL |
AidasL | 23:708cc5ef2604 | 101 | **********************************************************************/ |
AidasL | 23:708cc5ef2604 | 102 | bool TwoWire::twi_master_init(void) |
AidasL | 23:708cc5ef2604 | 103 | { |
AidasL | 23:708cc5ef2604 | 104 | uint8_t softdevice_enabled; |
AidasL | 23:708cc5ef2604 | 105 | //uint32_t err_code = NRF_SUCCESS; |
AidasL | 23:708cc5ef2604 | 106 | |
AidasL | 23:708cc5ef2604 | 107 | NRF_GPIO->PIN_CNF[SCL_Pin] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) |
AidasL | 23:708cc5ef2604 | 108 | | (GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos) |
AidasL | 23:708cc5ef2604 | 109 | | (GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) |
AidasL | 23:708cc5ef2604 | 110 | | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) |
AidasL | 23:708cc5ef2604 | 111 | | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos); |
AidasL | 23:708cc5ef2604 | 112 | |
AidasL | 23:708cc5ef2604 | 113 | NRF_GPIO->PIN_CNF[SDA_Pin] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) |
AidasL | 23:708cc5ef2604 | 114 | | (GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos) |
AidasL | 23:708cc5ef2604 | 115 | | (GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) |
AidasL | 23:708cc5ef2604 | 116 | | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) |
AidasL | 23:708cc5ef2604 | 117 | | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos); |
AidasL | 23:708cc5ef2604 | 118 | |
AidasL | 23:708cc5ef2604 | 119 | twi->EVENTS_RXDREADY = 0; |
AidasL | 23:708cc5ef2604 | 120 | twi->EVENTS_TXDSENT = 0; |
AidasL | 23:708cc5ef2604 | 121 | twi->PSELSCL = SCL_Pin; |
AidasL | 23:708cc5ef2604 | 122 | twi->PSELSDA = SDA_Pin; |
AidasL | 23:708cc5ef2604 | 123 | twi->FREQUENCY = twi_frequency; //TWI_FREQUENCY_FREQUENCY_K100 << TWI_FREQUENCY_FREQUENCY_Pos; |
AidasL | 23:708cc5ef2604 | 124 | |
AidasL | 23:708cc5ef2604 | 125 | sd_softdevice_is_enabled(&softdevice_enabled); |
AidasL | 23:708cc5ef2604 | 126 | //APP_ERROR_CHECK(err_code); |
AidasL | 23:708cc5ef2604 | 127 | if (softdevice_enabled == 0) |
AidasL | 23:708cc5ef2604 | 128 | { |
AidasL | 23:708cc5ef2604 | 129 | NRF_PPI->CH[7].EEP = (uint32_t)&twi->EVENTS_BB; |
AidasL | 23:708cc5ef2604 | 130 | NRF_PPI->CH[7].TEP = (uint32_t)&twi->TASKS_SUSPEND; |
AidasL | 23:708cc5ef2604 | 131 | NRF_PPI->CHEN &= ~(1 << 7); |
AidasL | 23:708cc5ef2604 | 132 | } |
AidasL | 23:708cc5ef2604 | 133 | else |
AidasL | 23:708cc5ef2604 | 134 | { |
AidasL | 23:708cc5ef2604 | 135 | sd_ppi_channel_assign(7, &twi->EVENTS_BB, &twi->TASKS_SUSPEND); |
AidasL | 23:708cc5ef2604 | 136 | //APP_ERROR_CHECK(err_code); |
AidasL | 23:708cc5ef2604 | 137 | sd_ppi_channel_enable_clr(1 << 7); |
AidasL | 23:708cc5ef2604 | 138 | //APP_ERROR_CHECK(err_code); |
AidasL | 23:708cc5ef2604 | 139 | } |
AidasL | 23:708cc5ef2604 | 140 | |
AidasL | 23:708cc5ef2604 | 141 | twi->ENABLE = TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos; |
AidasL | 23:708cc5ef2604 | 142 | |
AidasL | 23:708cc5ef2604 | 143 | return twi_master_clear_bus(); |
AidasL | 23:708cc5ef2604 | 144 | } |
AidasL | 23:708cc5ef2604 | 145 | /********************************************************************** |
AidasL | 23:708cc5ef2604 | 146 | name : |
AidasL | 23:708cc5ef2604 | 147 | function : return: 0--SUCCESS, 1--FAIL |
AidasL | 23:708cc5ef2604 | 148 | **********************************************************************/ |
AidasL | 23:708cc5ef2604 | 149 | uint8_t TwoWire::twi_master_write(uint8_t *data, uint8_t data_length, uint8_t issue_stop_condition) |
AidasL | 23:708cc5ef2604 | 150 | { |
AidasL | 23:708cc5ef2604 | 151 | uint32_t timeout = MAX_TIMEOUT_LOOPS; |
AidasL | 23:708cc5ef2604 | 152 | |
AidasL | 23:708cc5ef2604 | 153 | if(data_length == 0) |
AidasL | 23:708cc5ef2604 | 154 | { |
AidasL | 23:708cc5ef2604 | 155 | return 1; |
AidasL | 23:708cc5ef2604 | 156 | } |
AidasL | 23:708cc5ef2604 | 157 | twi->TXD = *data++; |
AidasL | 23:708cc5ef2604 | 158 | twi->TASKS_STARTTX = 1; |
AidasL | 23:708cc5ef2604 | 159 | while(1) |
AidasL | 23:708cc5ef2604 | 160 | { |
AidasL | 23:708cc5ef2604 | 161 | while( (twi->EVENTS_TXDSENT == 0) && (--timeout) );//&& (twi->EVENTS_ERROR == 0) ); |
AidasL | 23:708cc5ef2604 | 162 | |
AidasL | 23:708cc5ef2604 | 163 | if( 0 == timeout )//|| twi->EVENTS_ERROR != 0) |
AidasL | 23:708cc5ef2604 | 164 | { |
AidasL | 23:708cc5ef2604 | 165 | twi->EVENTS_ERROR = 0; |
AidasL | 23:708cc5ef2604 | 166 | twi->ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos; |
AidasL | 23:708cc5ef2604 | 167 | twi->POWER = 0; |
AidasL | 23:708cc5ef2604 | 168 | TWI_DELAY(5); |
AidasL | 23:708cc5ef2604 | 169 | twi->POWER = 1; |
AidasL | 23:708cc5ef2604 | 170 | twi->ENABLE = TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos; |
AidasL | 23:708cc5ef2604 | 171 | |
AidasL | 23:708cc5ef2604 | 172 | twi_master_init(); |
AidasL | 23:708cc5ef2604 | 173 | return 1; |
AidasL | 23:708cc5ef2604 | 174 | } |
AidasL | 23:708cc5ef2604 | 175 | twi->EVENTS_TXDSENT = 0; |
AidasL | 23:708cc5ef2604 | 176 | if( --data_length == 0) |
AidasL | 23:708cc5ef2604 | 177 | { |
AidasL | 23:708cc5ef2604 | 178 | break; |
AidasL | 23:708cc5ef2604 | 179 | } |
AidasL | 23:708cc5ef2604 | 180 | |
AidasL | 23:708cc5ef2604 | 181 | twi->TXD = *data++; |
AidasL | 23:708cc5ef2604 | 182 | } |
AidasL | 23:708cc5ef2604 | 183 | if(issue_stop_condition) |
AidasL | 23:708cc5ef2604 | 184 | { |
AidasL | 23:708cc5ef2604 | 185 | twi->EVENTS_STOPPED = 0; |
AidasL | 23:708cc5ef2604 | 186 | twi->TASKS_STOP = 1; |
AidasL | 23:708cc5ef2604 | 187 | while(twi->EVENTS_STOPPED == 0) |
AidasL | 23:708cc5ef2604 | 188 | { |
AidasL | 23:708cc5ef2604 | 189 | //do nothing, wait for stop sequence is sent |
AidasL | 23:708cc5ef2604 | 190 | } |
AidasL | 23:708cc5ef2604 | 191 | } |
AidasL | 23:708cc5ef2604 | 192 | return 0; |
AidasL | 23:708cc5ef2604 | 193 | } |
AidasL | 23:708cc5ef2604 | 194 | /********************************************************************** |
AidasL | 23:708cc5ef2604 | 195 | name : |
AidasL | 23:708cc5ef2604 | 196 | function : return: 0--SUCCESS, 1--FAIL |
AidasL | 23:708cc5ef2604 | 197 | **********************************************************************/ |
AidasL | 23:708cc5ef2604 | 198 | uint8_t TwoWire::twi_master_read(uint8_t *data, uint8_t data_length, uint8_t issue_stop_condition) |
AidasL | 23:708cc5ef2604 | 199 | { |
AidasL | 23:708cc5ef2604 | 200 | uint8_t softdevice_enabled; |
AidasL | 23:708cc5ef2604 | 201 | uint32_t timeout = MAX_TIMEOUT_LOOPS;// err_code = NRF_SUCCESS; |
AidasL | 23:708cc5ef2604 | 202 | |
AidasL | 23:708cc5ef2604 | 203 | sd_softdevice_is_enabled(&softdevice_enabled); |
AidasL | 23:708cc5ef2604 | 204 | //APP_ERROR_CHECK(err_code); |
AidasL | 23:708cc5ef2604 | 205 | if( 0 == data_length ) |
AidasL | 23:708cc5ef2604 | 206 | { |
AidasL | 23:708cc5ef2604 | 207 | return 1; |
AidasL | 23:708cc5ef2604 | 208 | } |
AidasL | 23:708cc5ef2604 | 209 | else if( 1== data_length )//&& issue_stop_condition == 1) |
AidasL | 23:708cc5ef2604 | 210 | { |
AidasL | 23:708cc5ef2604 | 211 | if (softdevice_enabled == 0) |
AidasL | 23:708cc5ef2604 | 212 | { |
AidasL | 23:708cc5ef2604 | 213 | NRF_PPI->CH[7].EEP = (uint32_t)&twi->EVENTS_BB; |
AidasL | 23:708cc5ef2604 | 214 | NRF_PPI->CH[7].TEP = (uint32_t)&twi->TASKS_STOP; |
AidasL | 23:708cc5ef2604 | 215 | NRF_PPI->CHEN |= (1 << 7); |
AidasL | 23:708cc5ef2604 | 216 | } |
AidasL | 23:708cc5ef2604 | 217 | else |
AidasL | 23:708cc5ef2604 | 218 | { |
AidasL | 23:708cc5ef2604 | 219 | sd_ppi_channel_assign(7, &twi->EVENTS_BB, &twi->TASKS_STOP); |
AidasL | 23:708cc5ef2604 | 220 | //APP_ERROR_CHECK(err_code); |
AidasL | 23:708cc5ef2604 | 221 | sd_ppi_channel_enable_set(1 << 7); |
AidasL | 23:708cc5ef2604 | 222 | //APP_ERROR_CHECK(err_code); |
AidasL | 23:708cc5ef2604 | 223 | } |
AidasL | 23:708cc5ef2604 | 224 | } |
AidasL | 23:708cc5ef2604 | 225 | else |
AidasL | 23:708cc5ef2604 | 226 | { |
AidasL | 23:708cc5ef2604 | 227 | if (softdevice_enabled == 0) |
AidasL | 23:708cc5ef2604 | 228 | { |
AidasL | 23:708cc5ef2604 | 229 | NRF_PPI->CH[7].EEP = (uint32_t)&twi->EVENTS_BB; |
AidasL | 23:708cc5ef2604 | 230 | NRF_PPI->CH[7].TEP = (uint32_t)&twi->TASKS_SUSPEND; |
AidasL | 23:708cc5ef2604 | 231 | NRF_PPI->CHEN |= (1 << 7); |
AidasL | 23:708cc5ef2604 | 232 | } |
AidasL | 23:708cc5ef2604 | 233 | else |
AidasL | 23:708cc5ef2604 | 234 | { |
AidasL | 23:708cc5ef2604 | 235 | sd_ppi_channel_assign(7, &twi->EVENTS_BB, &twi->TASKS_SUSPEND); |
AidasL | 23:708cc5ef2604 | 236 | //APP_ERROR_CHECK(err_code); |
AidasL | 23:708cc5ef2604 | 237 | sd_ppi_channel_enable_set(1 << 7); |
AidasL | 23:708cc5ef2604 | 238 | //APP_ERROR_CHECK(err_code); |
AidasL | 23:708cc5ef2604 | 239 | } |
AidasL | 23:708cc5ef2604 | 240 | } |
AidasL | 23:708cc5ef2604 | 241 | |
AidasL | 23:708cc5ef2604 | 242 | twi->EVENTS_RXDREADY = 0; |
AidasL | 23:708cc5ef2604 | 243 | twi->TASKS_STARTRX = 1; |
AidasL | 23:708cc5ef2604 | 244 | |
AidasL | 23:708cc5ef2604 | 245 | while(1) |
AidasL | 23:708cc5ef2604 | 246 | { |
AidasL | 23:708cc5ef2604 | 247 | while( twi->EVENTS_RXDREADY == 0 && (--timeout) ) |
AidasL | 23:708cc5ef2604 | 248 | { |
AidasL | 23:708cc5ef2604 | 249 | //do nothing, just wait |
AidasL | 23:708cc5ef2604 | 250 | } |
AidasL | 23:708cc5ef2604 | 251 | |
AidasL | 23:708cc5ef2604 | 252 | if( timeout == 0 ) |
AidasL | 23:708cc5ef2604 | 253 | { |
AidasL | 23:708cc5ef2604 | 254 | twi->EVENTS_ERROR = 0; |
AidasL | 23:708cc5ef2604 | 255 | twi->ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos; |
AidasL | 23:708cc5ef2604 | 256 | twi->POWER = 0; |
AidasL | 23:708cc5ef2604 | 257 | TWI_DELAY(5); |
AidasL | 23:708cc5ef2604 | 258 | twi->POWER = 1; |
AidasL | 23:708cc5ef2604 | 259 | twi->ENABLE = TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos; |
AidasL | 23:708cc5ef2604 | 260 | |
AidasL | 23:708cc5ef2604 | 261 | twi_master_init(); |
AidasL | 23:708cc5ef2604 | 262 | |
AidasL | 23:708cc5ef2604 | 263 | return 1; |
AidasL | 23:708cc5ef2604 | 264 | } |
AidasL | 23:708cc5ef2604 | 265 | |
AidasL | 23:708cc5ef2604 | 266 | twi->EVENTS_RXDREADY = 0; |
AidasL | 23:708cc5ef2604 | 267 | *data++ = twi->RXD; |
AidasL | 23:708cc5ef2604 | 268 | |
AidasL | 23:708cc5ef2604 | 269 | if( --data_length == 1 ) |
AidasL | 23:708cc5ef2604 | 270 | { |
AidasL | 23:708cc5ef2604 | 271 | if (softdevice_enabled == 0) |
AidasL | 23:708cc5ef2604 | 272 | { |
AidasL | 23:708cc5ef2604 | 273 | //NRF_PPI->CH[7].EEP = (uint32_t)&twi->EVENTS_BB; |
AidasL | 23:708cc5ef2604 | 274 | NRF_PPI->CH[7].TEP = (uint32_t)&twi->TASKS_STOP; |
AidasL | 23:708cc5ef2604 | 275 | } |
AidasL | 23:708cc5ef2604 | 276 | else |
AidasL | 23:708cc5ef2604 | 277 | { |
AidasL | 23:708cc5ef2604 | 278 | sd_ppi_channel_assign(7, &twi->EVENTS_BB, &twi->TASKS_STOP); |
AidasL | 23:708cc5ef2604 | 279 | //APP_ERROR_CHECK(err_code); |
AidasL | 23:708cc5ef2604 | 280 | } |
AidasL | 23:708cc5ef2604 | 281 | } |
AidasL | 23:708cc5ef2604 | 282 | |
AidasL | 23:708cc5ef2604 | 283 | if( data_length == 0 ) |
AidasL | 23:708cc5ef2604 | 284 | { |
AidasL | 23:708cc5ef2604 | 285 | twi->TASKS_STOP = 1; |
AidasL | 23:708cc5ef2604 | 286 | break; |
AidasL | 23:708cc5ef2604 | 287 | } |
AidasL | 23:708cc5ef2604 | 288 | TWI_DELAY(20); |
AidasL | 23:708cc5ef2604 | 289 | twi->TASKS_RESUME = 1; |
AidasL | 23:708cc5ef2604 | 290 | } |
AidasL | 23:708cc5ef2604 | 291 | while( twi->EVENTS_STOPPED == 0 ) |
AidasL | 23:708cc5ef2604 | 292 | { |
AidasL | 23:708cc5ef2604 | 293 | //do nothing |
AidasL | 23:708cc5ef2604 | 294 | } |
AidasL | 23:708cc5ef2604 | 295 | |
AidasL | 23:708cc5ef2604 | 296 | twi->EVENTS_STOPPED = 0; |
AidasL | 23:708cc5ef2604 | 297 | |
AidasL | 23:708cc5ef2604 | 298 | if (softdevice_enabled == 0) |
AidasL | 23:708cc5ef2604 | 299 | { |
AidasL | 23:708cc5ef2604 | 300 | NRF_PPI->CHEN &= ~(1 << 7); |
AidasL | 23:708cc5ef2604 | 301 | } |
AidasL | 23:708cc5ef2604 | 302 | else |
AidasL | 23:708cc5ef2604 | 303 | { |
AidasL | 23:708cc5ef2604 | 304 | sd_ppi_channel_enable_clr( 1 << 7 ); |
AidasL | 23:708cc5ef2604 | 305 | //APP_ERROR_CHECK(err_code); |
AidasL | 23:708cc5ef2604 | 306 | } |
AidasL | 23:708cc5ef2604 | 307 | return 0; |
AidasL | 23:708cc5ef2604 | 308 | } |
AidasL | 23:708cc5ef2604 | 309 | |
AidasL | 23:708cc5ef2604 | 310 | /********************************************************************** |
AidasL | 23:708cc5ef2604 | 311 | name : |
AidasL | 23:708cc5ef2604 | 312 | function : |
AidasL | 23:708cc5ef2604 | 313 | **********************************************************************/ |
AidasL | 23:708cc5ef2604 | 314 | TwoWire::TwoWire(NRF_TWI_Type *twi_use) |
AidasL | 23:708cc5ef2604 | 315 | { |
AidasL | 23:708cc5ef2604 | 316 | twi = twi_use; |
AidasL | 23:708cc5ef2604 | 317 | |
AidasL | 23:708cc5ef2604 | 318 | RX_BufferIndex = 0; |
AidasL | 23:708cc5ef2604 | 319 | RX_BufferLength = 0; |
AidasL | 23:708cc5ef2604 | 320 | TX_BufferIndex = 0; |
AidasL | 23:708cc5ef2604 | 321 | TX_BufferLength = 0; |
AidasL | 23:708cc5ef2604 | 322 | |
AidasL | 23:708cc5ef2604 | 323 | Transform_Addr = 0; |
AidasL | 23:708cc5ef2604 | 324 | |
AidasL | 23:708cc5ef2604 | 325 | twi_status = UNINITIALIZED; |
AidasL | 23:708cc5ef2604 | 326 | } |
AidasL | 23:708cc5ef2604 | 327 | /********************************************************************** |
AidasL | 23:708cc5ef2604 | 328 | name : |
AidasL | 23:708cc5ef2604 | 329 | function : |
AidasL | 23:708cc5ef2604 | 330 | **********************************************************************/ |
AidasL | 23:708cc5ef2604 | 331 | void TwoWire::begin(uint32_t scl, uint32_t sda, uint8_t speed) |
AidasL | 23:708cc5ef2604 | 332 | { |
AidasL | 23:708cc5ef2604 | 333 | if( speed == 2 ) |
AidasL | 23:708cc5ef2604 | 334 | { |
AidasL | 23:708cc5ef2604 | 335 | twi_frequency = TWI_FREQUENCY_FREQUENCY_K400; |
AidasL | 23:708cc5ef2604 | 336 | } |
AidasL | 23:708cc5ef2604 | 337 | else if( speed == 1 ) |
AidasL | 23:708cc5ef2604 | 338 | { |
AidasL | 23:708cc5ef2604 | 339 | twi_frequency = TWI_FREQUENCY_FREQUENCY_K250; |
AidasL | 23:708cc5ef2604 | 340 | } |
AidasL | 23:708cc5ef2604 | 341 | else |
AidasL | 23:708cc5ef2604 | 342 | { |
AidasL | 23:708cc5ef2604 | 343 | twi_frequency = TWI_FREQUENCY_FREQUENCY_K100; |
AidasL | 23:708cc5ef2604 | 344 | } |
AidasL | 23:708cc5ef2604 | 345 | |
AidasL | 23:708cc5ef2604 | 346 | SCL_Pin = scl; |
AidasL | 23:708cc5ef2604 | 347 | SDA_Pin = sda; |
AidasL | 23:708cc5ef2604 | 348 | twi_master_init(); |
AidasL | 23:708cc5ef2604 | 349 | |
AidasL | 23:708cc5ef2604 | 350 | twi_status = MASTER_IDLE; |
AidasL | 23:708cc5ef2604 | 351 | } |
AidasL | 23:708cc5ef2604 | 352 | /********************************************************************** |
AidasL | 23:708cc5ef2604 | 353 | name : |
AidasL | 23:708cc5ef2604 | 354 | function : |
AidasL | 23:708cc5ef2604 | 355 | **********************************************************************/ |
AidasL | 23:708cc5ef2604 | 356 | void TwoWire::begin() |
AidasL | 23:708cc5ef2604 | 357 | { |
AidasL | 23:708cc5ef2604 | 358 | begin(TWI_SCL, TWI_SDA, 0); |
AidasL | 23:708cc5ef2604 | 359 | } |
AidasL | 23:708cc5ef2604 | 360 | /********************************************************************** |
AidasL | 23:708cc5ef2604 | 361 | name : |
AidasL | 23:708cc5ef2604 | 362 | function : |
AidasL | 23:708cc5ef2604 | 363 | **********************************************************************/ |
AidasL | 23:708cc5ef2604 | 364 | void TwoWire::beginTransmission( uint8_t address ) |
AidasL | 23:708cc5ef2604 | 365 | { |
AidasL | 23:708cc5ef2604 | 366 | Transform_Addr = address; |
AidasL | 23:708cc5ef2604 | 367 | TX_BufferIndex = 0; |
AidasL | 23:708cc5ef2604 | 368 | twi_status = MASTER_SEND; |
AidasL | 23:708cc5ef2604 | 369 | } |
AidasL | 23:708cc5ef2604 | 370 | /********************************************************************** |
AidasL | 23:708cc5ef2604 | 371 | name : |
AidasL | 23:708cc5ef2604 | 372 | function : |
AidasL | 23:708cc5ef2604 | 373 | **********************************************************************/ |
AidasL | 23:708cc5ef2604 | 374 | void TwoWire::beginTransmission( int address ) |
AidasL | 23:708cc5ef2604 | 375 | { |
AidasL | 23:708cc5ef2604 | 376 | beginTransmission( (uint8_t)address ); |
AidasL | 23:708cc5ef2604 | 377 | } |
AidasL | 23:708cc5ef2604 | 378 | /********************************************************************** |
AidasL | 23:708cc5ef2604 | 379 | name : |
AidasL | 23:708cc5ef2604 | 380 | function : return: 0--SUCCESS, 1--FAIL |
AidasL | 23:708cc5ef2604 | 381 | **********************************************************************/ |
AidasL | 23:708cc5ef2604 | 382 | uint8_t TwoWire::endTransmission( uint8_t stop) |
AidasL | 23:708cc5ef2604 | 383 | { |
AidasL | 23:708cc5ef2604 | 384 | uint8_t twi_flag = 1; |
AidasL | 23:708cc5ef2604 | 385 | |
AidasL | 23:708cc5ef2604 | 386 | if(TX_BufferLength > 0 && !(twi_master_clear_bus()) ) |
AidasL | 23:708cc5ef2604 | 387 | { |
AidasL | 23:708cc5ef2604 | 388 | twi->ADDRESS = ( Transform_Addr >> 1); |
AidasL | 23:708cc5ef2604 | 389 | twi_flag = twi_master_write(TX_Buffer, TX_BufferLength, stop); |
AidasL | 23:708cc5ef2604 | 390 | } |
AidasL | 23:708cc5ef2604 | 391 | |
AidasL | 23:708cc5ef2604 | 392 | TX_BufferLength = 0; |
AidasL | 23:708cc5ef2604 | 393 | twi_status = MASTER_IDLE; |
AidasL | 23:708cc5ef2604 | 394 | |
AidasL | 23:708cc5ef2604 | 395 | return twi_flag; |
AidasL | 23:708cc5ef2604 | 396 | } |
AidasL | 23:708cc5ef2604 | 397 | /********************************************************************** |
AidasL | 23:708cc5ef2604 | 398 | name : |
AidasL | 23:708cc5ef2604 | 399 | function : return: 0--SUCCESS, 1--FAIL |
AidasL | 23:708cc5ef2604 | 400 | **********************************************************************/ |
AidasL | 23:708cc5ef2604 | 401 | uint8_t TwoWire::endTransmission(void) |
AidasL | 23:708cc5ef2604 | 402 | { |
AidasL | 23:708cc5ef2604 | 403 | uint8_t twi_flag; |
AidasL | 23:708cc5ef2604 | 404 | |
AidasL | 23:708cc5ef2604 | 405 | twi_flag = endTransmission(1); |
AidasL | 23:708cc5ef2604 | 406 | |
AidasL | 23:708cc5ef2604 | 407 | return twi_flag; |
AidasL | 23:708cc5ef2604 | 408 | } |
AidasL | 23:708cc5ef2604 | 409 | /********************************************************************** |
AidasL | 23:708cc5ef2604 | 410 | name : |
AidasL | 23:708cc5ef2604 | 411 | function : return: 0--SUCCESS, -1--FAIL, |
AidasL | 23:708cc5ef2604 | 412 | **********************************************************************/ |
AidasL | 23:708cc5ef2604 | 413 | int TwoWire::write(uint8_t data) |
AidasL | 23:708cc5ef2604 | 414 | { |
AidasL | 23:708cc5ef2604 | 415 | if(twi_status == MASTER_SEND) |
AidasL | 23:708cc5ef2604 | 416 | { |
AidasL | 23:708cc5ef2604 | 417 | if(TX_BufferLength >= BUFF_MAX_LENGTH) |
AidasL | 23:708cc5ef2604 | 418 | { |
AidasL | 23:708cc5ef2604 | 419 | return -1; |
AidasL | 23:708cc5ef2604 | 420 | } |
AidasL | 23:708cc5ef2604 | 421 | TX_Buffer[TX_BufferLength++] = data; |
AidasL | 23:708cc5ef2604 | 422 | return 0; |
AidasL | 23:708cc5ef2604 | 423 | } |
AidasL | 23:708cc5ef2604 | 424 | else |
AidasL | 23:708cc5ef2604 | 425 | { |
AidasL | 23:708cc5ef2604 | 426 | return -1; |
AidasL | 23:708cc5ef2604 | 427 | } |
AidasL | 23:708cc5ef2604 | 428 | } |
AidasL | 23:708cc5ef2604 | 429 | /********************************************************************** |
AidasL | 23:708cc5ef2604 | 430 | name : |
AidasL | 23:708cc5ef2604 | 431 | function : return: -1--FAIL, else--the length of data stored |
AidasL | 23:708cc5ef2604 | 432 | **********************************************************************/ |
AidasL | 23:708cc5ef2604 | 433 | int TwoWire::write(const uint8_t *data, size_t quantity ) |
AidasL | 23:708cc5ef2604 | 434 | { |
AidasL | 23:708cc5ef2604 | 435 | if( twi_status == MASTER_SEND ) |
AidasL | 23:708cc5ef2604 | 436 | { |
AidasL | 23:708cc5ef2604 | 437 | for( size_t i=0; i<quantity; ++i ) |
AidasL | 23:708cc5ef2604 | 438 | { |
AidasL | 23:708cc5ef2604 | 439 | if(TX_BufferLength >= BUFF_MAX_LENGTH) |
AidasL | 23:708cc5ef2604 | 440 | { |
AidasL | 23:708cc5ef2604 | 441 | return i; |
AidasL | 23:708cc5ef2604 | 442 | } |
AidasL | 23:708cc5ef2604 | 443 | TX_Buffer[TX_BufferLength++] = data[i]; |
AidasL | 23:708cc5ef2604 | 444 | } |
AidasL | 23:708cc5ef2604 | 445 | } |
AidasL | 23:708cc5ef2604 | 446 | else |
AidasL | 23:708cc5ef2604 | 447 | { |
AidasL | 23:708cc5ef2604 | 448 | return -1; |
AidasL | 23:708cc5ef2604 | 449 | } |
AidasL | 23:708cc5ef2604 | 450 | |
AidasL | 23:708cc5ef2604 | 451 | return quantity; |
AidasL | 23:708cc5ef2604 | 452 | } |
AidasL | 23:708cc5ef2604 | 453 | /********************************************************************** |
AidasL | 23:708cc5ef2604 | 454 | name : |
AidasL | 23:708cc5ef2604 | 455 | function : return: 0--SUCCESS, 1--FAIL |
AidasL | 23:708cc5ef2604 | 456 | **********************************************************************/ |
AidasL | 23:708cc5ef2604 | 457 | uint8_t TwoWire::requestFrom(uint8_t addr, uint8_t quantity, uint8_t stop) |
AidasL | 23:708cc5ef2604 | 458 | { |
AidasL | 23:708cc5ef2604 | 459 | uint8_t twi_flag = 1; |
AidasL | 23:708cc5ef2604 | 460 | |
AidasL | 23:708cc5ef2604 | 461 | if(quantity > BUFF_MAX_LENGTH) |
AidasL | 23:708cc5ef2604 | 462 | { |
AidasL | 23:708cc5ef2604 | 463 | quantity = BUFF_MAX_LENGTH; |
AidasL | 23:708cc5ef2604 | 464 | } |
AidasL | 23:708cc5ef2604 | 465 | if(quantity > 0 && !(twi_master_clear_bus()) ) |
AidasL | 23:708cc5ef2604 | 466 | { |
AidasL | 23:708cc5ef2604 | 467 | twi->ADDRESS = ( addr >> 1 ); |
AidasL | 23:708cc5ef2604 | 468 | twi_flag = twi_master_read(RX_Buffer, quantity, stop); |
AidasL | 23:708cc5ef2604 | 469 | } |
AidasL | 23:708cc5ef2604 | 470 | RX_BufferIndex = 0; |
AidasL | 23:708cc5ef2604 | 471 | RX_BufferLength = quantity; |
AidasL | 23:708cc5ef2604 | 472 | |
AidasL | 23:708cc5ef2604 | 473 | return twi_flag; |
AidasL | 23:708cc5ef2604 | 474 | } |
AidasL | 23:708cc5ef2604 | 475 | /********************************************************************** |
AidasL | 23:708cc5ef2604 | 476 | name : |
AidasL | 23:708cc5ef2604 | 477 | function : |
AidasL | 23:708cc5ef2604 | 478 | **********************************************************************/ |
AidasL | 23:708cc5ef2604 | 479 | uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity) |
AidasL | 23:708cc5ef2604 | 480 | { |
AidasL | 23:708cc5ef2604 | 481 | return requestFrom((uint8_t) address, (uint8_t) quantity, (uint8_t) true); |
AidasL | 23:708cc5ef2604 | 482 | } |
AidasL | 23:708cc5ef2604 | 483 | /********************************************************************** |
AidasL | 23:708cc5ef2604 | 484 | name : |
AidasL | 23:708cc5ef2604 | 485 | function : |
AidasL | 23:708cc5ef2604 | 486 | **********************************************************************/ |
AidasL | 23:708cc5ef2604 | 487 | uint8_t TwoWire::requestFrom(int address, int quantity) |
AidasL | 23:708cc5ef2604 | 488 | { |
AidasL | 23:708cc5ef2604 | 489 | return requestFrom((uint8_t) address, (uint8_t) quantity, (uint8_t) true); |
AidasL | 23:708cc5ef2604 | 490 | } |
AidasL | 23:708cc5ef2604 | 491 | /********************************************************************** |
AidasL | 23:708cc5ef2604 | 492 | name : |
AidasL | 23:708cc5ef2604 | 493 | function : |
AidasL | 23:708cc5ef2604 | 494 | **********************************************************************/ |
AidasL | 23:708cc5ef2604 | 495 | uint8_t TwoWire::requestFrom(int address, int quantity, int sendStop) |
AidasL | 23:708cc5ef2604 | 496 | { |
AidasL | 23:708cc5ef2604 | 497 | return requestFrom((uint8_t) address, (uint8_t) quantity, (uint8_t) sendStop); |
AidasL | 23:708cc5ef2604 | 498 | } |
AidasL | 23:708cc5ef2604 | 499 | /********************************************************************** |
AidasL | 23:708cc5ef2604 | 500 | name : |
AidasL | 23:708cc5ef2604 | 501 | function : return:-1--FAIL, else:the length of data that could be read |
AidasL | 23:708cc5ef2604 | 502 | **********************************************************************/ |
AidasL | 23:708cc5ef2604 | 503 | int TwoWire::available(void) |
AidasL | 23:708cc5ef2604 | 504 | { |
AidasL | 23:708cc5ef2604 | 505 | if(RX_BufferIndex <= RX_BufferLength) |
AidasL | 23:708cc5ef2604 | 506 | { |
AidasL | 23:708cc5ef2604 | 507 | return (RX_BufferLength - RX_BufferIndex); |
AidasL | 23:708cc5ef2604 | 508 | } |
AidasL | 23:708cc5ef2604 | 509 | else |
AidasL | 23:708cc5ef2604 | 510 | { |
AidasL | 23:708cc5ef2604 | 511 | return -1; |
AidasL | 23:708cc5ef2604 | 512 | } |
AidasL | 23:708cc5ef2604 | 513 | } |
AidasL | 23:708cc5ef2604 | 514 | /********************************************************************** |
AidasL | 23:708cc5ef2604 | 515 | name : |
AidasL | 23:708cc5ef2604 | 516 | function : |
AidasL | 23:708cc5ef2604 | 517 | **********************************************************************/ |
AidasL | 23:708cc5ef2604 | 518 | int TwoWire::read(void) |
AidasL | 23:708cc5ef2604 | 519 | { |
AidasL | 23:708cc5ef2604 | 520 | if(RX_BufferIndex < RX_BufferLength) |
AidasL | 23:708cc5ef2604 | 521 | { |
AidasL | 23:708cc5ef2604 | 522 | return RX_Buffer[RX_BufferIndex++]; |
AidasL | 23:708cc5ef2604 | 523 | } |
AidasL | 23:708cc5ef2604 | 524 | else |
AidasL | 23:708cc5ef2604 | 525 | { |
AidasL | 23:708cc5ef2604 | 526 | return -1; |
AidasL | 23:708cc5ef2604 | 527 | } |
AidasL | 23:708cc5ef2604 | 528 | } |
AidasL | 23:708cc5ef2604 | 529 | |
AidasL | 23:708cc5ef2604 | 530 | |
AidasL | 23:708cc5ef2604 | 531 | |
AidasL | 23:708cc5ef2604 | 532 | |
AidasL | 23:708cc5ef2604 | 533 | |
AidasL | 23:708cc5ef2604 | 534 | |
AidasL | 23:708cc5ef2604 | 535 |