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