I2C libraries for the implementation of I2C communication in nRF51 based on I2C redbearlabs libraries

Fork of I2C_Driver by CESAR CAZAL

Committer:
cesarcazal
Date:
Tue Aug 23 16:23:15 2016 +0000
Revision:
0:2514f1c72462
Driver para la utiilizaci?n del I2C basado en librer?as de Redbearlab

Who changed what in which revision?

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