|
| 1 | +/* |
| 2 | + * TWI/I2C library for Arduino Zero |
| 3 | + * based on Copyright (c) 2011 Cristian Maglie <[email protected]>. |
| 4 | + * Copyright (c) 2014 Arduino. |
| 5 | + * All rights reserved. |
| 6 | + * |
| 7 | + * Copyright (c) 2015 Amel-Tech (a division of Amel Srl). All right reserved. |
| 8 | + * |
| 9 | + * |
| 10 | + * This library is free software; you can redistribute it and/or |
| 11 | + * modify it under the terms of the GNU Lesser General Public |
| 12 | + * License as published by the Free Software Foundation; either |
| 13 | + * version 2.1 of the License, or (at your option) any later version. |
| 14 | + * |
| 15 | + * This library is distributed in the hope that it will be useful, |
| 16 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 | + * Lesser General Public License for more details. |
| 19 | + * |
| 20 | + * You should have received a copy of the GNU Lesser General Public |
| 21 | + * License along with this library; if not, write to the Free Software |
| 22 | + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 23 | + */ |
| 24 | + |
| 25 | +extern "C" { |
| 26 | +#include <string.h> |
| 27 | +} |
| 28 | + |
| 29 | +#include "WireIoExt.h" |
| 30 | +#include "variant.h" |
| 31 | +#include "wiring_private.h" |
| 32 | + |
| 33 | +I2CIoExt::I2CIoExt(SERCOM * s) |
| 34 | +{ |
| 35 | + this->sercom = s; |
| 36 | + transmissionBegun = false; |
| 37 | +} |
| 38 | + |
| 39 | +void I2CIoExt::begin(void) { |
| 40 | + //Master Mode |
| 41 | + sercom->initMasterWIRE(TWI_CLOCK); |
| 42 | + sercom->enableWIRE(); |
| 43 | + |
| 44 | + pinPeripheral(PIN_WIRE_SDA, g_APinDescription[PIN_WIRE_SDA].ulPinType); |
| 45 | + pinPeripheral(PIN_WIRE_SCL, g_APinDescription[PIN_WIRE_SCL].ulPinType); |
| 46 | +} |
| 47 | + |
| 48 | +uint8_t I2CIoExt::requestFrom(uint8_t address, size_t quantity, bool stopBit) |
| 49 | +{ |
| 50 | + if(quantity == 0) |
| 51 | + { |
| 52 | + return 0; |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | + size_t byteRead = 0; |
| 57 | + |
| 58 | + if(sercom->startTransmissionWIRE(address, WIRE_READ_FLAG)) |
| 59 | + { |
| 60 | + |
| 61 | + // Read first data |
| 62 | + rxBuffer.store_char(sercom->readDataWIRE()); |
| 63 | + |
| 64 | + // Connected to slave |
| 65 | + //while(toRead--) |
| 66 | + for(byteRead = 0; byteRead < quantity; ++byteRead) |
| 67 | + { |
| 68 | + if( byteRead == quantity - 1) // Stop transmission |
| 69 | + { |
| 70 | + sercom->prepareNackBitWIRE(); // Prepare NACK to stop slave transmission |
| 71 | + //sercom->readDataWIRE(); // Clear data register to send NACK |
| 72 | + sercom->prepareCommandBitsWire(WIRE_MASTER_ACT_STOP); // Send Stop |
| 73 | + } |
| 74 | + else // Continue transmission |
| 75 | + { |
| 76 | + sercom->prepareAckBitWIRE(); // Prepare Acknowledge |
| 77 | + sercom->prepareCommandBitsWire(WIRE_MASTER_ACT_READ); // Prepare the ACK command for the slave |
| 78 | + rxBuffer.store_char( sercom->readDataWIRE() ); // Read data and send the ACK |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return byteRead; |
| 84 | +} |
| 85 | + |
| 86 | +uint8_t I2CIoExt::requestFrom(uint8_t address, size_t quantity) |
| 87 | +{ |
| 88 | + return requestFrom(address, quantity, true); |
| 89 | +} |
| 90 | + |
| 91 | +void I2CIoExt::beginTransmission(uint8_t address) { |
| 92 | + // save address of target and clear buffer |
| 93 | + txAddress = address; |
| 94 | + txBuffer.clear(); |
| 95 | + |
| 96 | + transmissionBegun = true; |
| 97 | +} |
| 98 | + |
| 99 | +// Errors: |
| 100 | +// 0 : Success |
| 101 | +// 1 : Data too long |
| 102 | +// 2 : NACK on transmit of address |
| 103 | +// 3 : NACK on transmit of data |
| 104 | +// 4 : Other error |
| 105 | +uint8_t I2CIoExt::endTransmission(bool stopBit) |
| 106 | +{ |
| 107 | + transmissionBegun = false ; |
| 108 | + |
| 109 | + // Check if there are data to send |
| 110 | + if ( txBuffer.available() == 0) |
| 111 | + { |
| 112 | + return 4 ; |
| 113 | + } |
| 114 | + |
| 115 | + // Start I2C transmission |
| 116 | + if ( !sercom->startTransmissionWIRE( txAddress, WIRE_WRITE_FLAG ) ) |
| 117 | + { |
| 118 | + sercom->prepareCommandBitsWire(WIRE_MASTER_ACT_STOP); |
| 119 | + return 2 ; // Address error |
| 120 | + } |
| 121 | + |
| 122 | + // Send all buffer |
| 123 | + while( txBuffer.available() ) |
| 124 | + { |
| 125 | + |
| 126 | + // Trying to send data |
| 127 | + if ( !sercom->sendDataMasterWIRE( txBuffer.read_char() ) ) |
| 128 | + { |
| 129 | + sercom->prepareCommandBitsWire(WIRE_MASTER_ACT_STOP); |
| 130 | + return 3 ; // Nack or error |
| 131 | + } |
| 132 | + |
| 133 | + |
| 134 | + if(txBuffer.available() == 0) |
| 135 | + { |
| 136 | + sercom->prepareCommandBitsWire(WIRE_MASTER_ACT_STOP); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + return 0; |
| 141 | +} |
| 142 | + |
| 143 | +uint8_t I2CIoExt::endTransmission() |
| 144 | +{ |
| 145 | + return endTransmission(true); |
| 146 | +} |
| 147 | + |
| 148 | +size_t I2CIoExt::write(uint8_t ucData) |
| 149 | +{ |
| 150 | + if(sercom->isMasterWIRE()) |
| 151 | + { |
| 152 | + // No writing, without begun transmission or a full buffer |
| 153 | + if ( !transmissionBegun || txBuffer.isFull() ) |
| 154 | + { |
| 155 | + return 0 ; |
| 156 | + } |
| 157 | + |
| 158 | + txBuffer.store_char( ucData ) ; |
| 159 | + |
| 160 | + return 1 ; |
| 161 | + } |
| 162 | + else |
| 163 | + { |
| 164 | + if(sercom->sendDataSlaveWIRE( ucData )) |
| 165 | + { |
| 166 | + return 1; |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + return 0; |
| 171 | +} |
| 172 | + |
| 173 | +size_t I2CIoExt::write(const uint8_t *data, size_t quantity) |
| 174 | +{ |
| 175 | + //Try to store all data |
| 176 | + for(size_t i = 0; i < quantity; ++i) |
| 177 | + { |
| 178 | + //Return the number of data stored, when the buffer is full (if write return 0) |
| 179 | + if(!write(data[i])) |
| 180 | + return i; |
| 181 | + } |
| 182 | + |
| 183 | + //All data stored |
| 184 | + return quantity; |
| 185 | +} |
| 186 | + |
| 187 | +int I2CIoExt::available(void) |
| 188 | +{ |
| 189 | + return rxBuffer.available(); |
| 190 | +} |
| 191 | + |
| 192 | +int I2CIoExt::read(void) |
| 193 | +{ |
| 194 | + return rxBuffer.read_char(); |
| 195 | +} |
| 196 | + |
| 197 | +int I2CIoExt::peek(void) |
| 198 | +{ |
| 199 | + return rxBuffer.peek(); |
| 200 | +} |
| 201 | + |
| 202 | +void I2CIoExt::flush(void) |
| 203 | +{ |
| 204 | + // Do nothing, use endTransmission(..) to force |
| 205 | + // data transfer. |
| 206 | +} |
| 207 | + |
| 208 | +#if WIRE_INTERFACES_COUNT > 0 |
| 209 | + |
| 210 | +I2CIoExt WireTemp(&sercom3); |
| 211 | + |
| 212 | +#endif |
0 commit comments