Minimal working example to demonstrate I2C with SI7021 sensor

Dependencies:   BLE_API mbed nRF51822

Committer:
ghost22
Date:
Sat Dec 10 16:17:12 2016 +0000
Revision:
2:fd94a7e87ac5
Parent:
0:d843c5528596
Fix of I2C

Who changed what in which revision?

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