pls

Dependencies:   BLE_API mbed nRF51822

Fork of zach_thresholding by Zachary Newman

Committer:
nkosarek
Date:
Tue May 02 04:16:38 2017 +0000
Revision:
15:d117591084ff
Working i2c to ble to pi without serial

Who changed what in which revision?

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