Skip to content

Commit c14cdd8

Browse files
fontamiHwfbraghiroli
authored andcommitted
Added missing IO_Extended initialization file
1 parent 7be30bd commit c14cdd8

File tree

2 files changed

+291
-0
lines changed

2 files changed

+291
-0
lines changed
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
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
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* TwoWire.h - TWI/I2C library for Arduino Due
3+
* Copyright (c) 2011 Cristian Maglie <[email protected]>.
4+
* All rights reserved.
5+
*
6+
* Copyright (c) 2015 Amel-Tech (a division of Amel Srl). All right reserved.
7+
*
8+
* This library is free software; you can redistribute it and/or
9+
* modify it under the terms of the GNU Lesser General Public
10+
* License as published by the Free Software Foundation; either
11+
* version 2.1 of the License, or (at your option) any later version.
12+
*
13+
* This library is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16+
* Lesser General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public
19+
* License along with this library; if not, write to the Free Software
20+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21+
*/
22+
23+
#ifndef WIRE_IO_EXT_h
24+
#define WIRE_IO_EXT_h
25+
26+
27+
#include "Stream.h"
28+
#include "variant.h"
29+
30+
#include "SERCOM.h"
31+
#include "RingBuffer.h"
32+
33+
#define BUFFER_LENGTH 32
34+
35+
36+
class I2CIoExt : public Stream
37+
{
38+
public:
39+
I2CIoExt(SERCOM *s);
40+
void begin();
41+
42+
void beginTransmission(uint8_t);
43+
uint8_t endTransmission(bool stopBit);
44+
uint8_t endTransmission(void);
45+
46+
uint8_t requestFrom(uint8_t address, size_t quantity, bool stopBit);
47+
uint8_t requestFrom(uint8_t address, size_t quantity);
48+
49+
size_t write(uint8_t data);
50+
size_t write(const uint8_t * data, size_t quantity);
51+
52+
virtual int available(void);
53+
virtual int read(void);
54+
virtual int peek(void);
55+
virtual void flush(void);
56+
57+
using Print::write;
58+
59+
private:
60+
SERCOM * sercom;
61+
bool transmissionBegun;
62+
63+
// RX Buffer
64+
RingBuffer rxBuffer;
65+
66+
//TX buffer
67+
RingBuffer txBuffer;
68+
uint8_t txAddress;
69+
70+
71+
// TWI clock frequency
72+
static const uint32_t TWI_CLOCK = 100000;
73+
74+
};
75+
76+
77+
extern I2CIoExt WireTemp;
78+
79+
#endif

0 commit comments

Comments
 (0)