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