@@ -22,7 +22,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
22
SOFTWARE.
23
23
*/
24
24
25
- extern " C" {
25
+ extern " C"
26
+ {
26
27
#include < string.h>
27
28
}
28
29
@@ -31,113 +32,128 @@ extern "C" {
31
32
32
33
#include " Wire.h"
33
34
34
- typedef enum {
35
+ typedef enum
36
+ {
35
37
AP3_WIRE_SCL = 0x00 ,
36
38
AP3_WIRE_SDA,
37
- }ap3_iom_i2c_pad_type_e;
39
+ } ap3_iom_i2c_pad_type_e;
38
40
39
41
TwoWire::TwoWire (uint8_t iom_instance) : IOMaster(iom_instance)
40
42
{
41
43
_transmissionBegun = false ;
42
44
}
43
45
44
- void TwoWire::begin (void ) {
46
+ void TwoWire::begin (void )
47
+ {
45
48
// Master Mode
46
49
47
50
am_hal_gpio_pincfg_t pincfg = AP3_GPIO_DEFAULT_PINCFG;
48
51
uint8_t funcsel = 0 ;
49
52
50
53
ap3_err_t retval = AP3_OK;
51
54
52
- retval = ap3_iom_pad_funcsel ( _instance, AP3_IOM_I2C_SCL, &_padSCL, &funcsel);
53
- if ( retval != AP3_OK ){ return /* retval*/ ; }
55
+ retval = ap3_iom_pad_funcsel (_instance, AP3_IOM_I2C_SCL, &_padSCL, &funcsel);
56
+ if (retval != AP3_OK)
57
+ {
58
+ return /* retval*/ ;
59
+ }
54
60
pincfg.uFuncSel = funcsel; // set the proper function select option for this instance/pin/type combination
55
61
pincfg.ePullup = AM_HAL_GPIO_PIN_PULLUP_1_5K;
56
62
pincfg.eDriveStrength = AM_HAL_GPIO_PIN_DRIVESTRENGTH_12MA;
57
63
pincfg.eGPOutcfg = AM_HAL_GPIO_PIN_OUTCFG_OPENDRAIN;
58
64
pincfg.uIOMnum = _instance;
59
- padMode ( _padSCL, pincfg, &retval ); if ( retval != AP3_OK){ return /* ap3_return(retval)*/ ; }
65
+ padMode (_padSCL, pincfg, &retval);
66
+ if (retval != AP3_OK)
67
+ {
68
+ return /* ap3_return(retval)*/ ;
69
+ }
60
70
pincfg = AP3_GPIO_DEFAULT_PINCFG; // set back to default for use with next pin
61
71
62
- retval = ap3_iom_pad_funcsel ( _instance, AP3_IOM_I2C_SDA, &_padSDA, &funcsel);
63
- if ( retval != AP3_OK ){ return /* retval*/ ; }
72
+ retval = ap3_iom_pad_funcsel (_instance, AP3_IOM_I2C_SDA, &_padSDA, &funcsel);
73
+ if (retval != AP3_OK)
74
+ {
75
+ return /* retval*/ ;
76
+ }
64
77
pincfg.uFuncSel = funcsel;
65
78
pincfg.ePullup = AM_HAL_GPIO_PIN_PULLUP_1_5K;
66
79
pincfg.eDriveStrength = AM_HAL_GPIO_PIN_DRIVESTRENGTH_12MA;
67
80
pincfg.eGPOutcfg = AM_HAL_GPIO_PIN_OUTCFG_OPENDRAIN;
68
81
pincfg.uIOMnum = _instance;
69
- padMode ( _padSDA, pincfg, &retval ); if ( retval != AP3_OK){ return /* ap3_return(retval)*/ ; }
82
+ padMode (_padSDA, pincfg, &retval);
83
+ if (retval != AP3_OK)
84
+ {
85
+ return /* ap3_return(retval)*/ ;
86
+ }
70
87
pincfg = AP3_GPIO_DEFAULT_PINCFG; // set back to default for use with next pin
71
88
72
-
73
- memset ( (void *)&_config, 0x00 , sizeof (am_hal_iom_config_t ) ); // Set the IOM configuration
89
+ memset ((void *)&_config, 0x00 , sizeof (am_hal_iom_config_t )); // Set the IOM configuration
74
90
_config.eInterfaceMode = AM_HAL_IOM_I2C_MODE;
75
91
_config.ui32ClockFreq = AM_HAL_IOM_100KHZ;
76
92
77
93
initialize (); // Initialize the IOM
78
94
}
79
95
80
- void TwoWire::begin (uint8_t address, bool enableGeneralCall) {
96
+ void TwoWire::begin (uint8_t address, bool enableGeneralCall)
97
+ {
81
98
// Slave mode
82
99
83
100
// ToDo:
84
101
}
85
102
86
- void TwoWire::setClock (uint32_t baudrate) {
87
- // sercom->disableWIRE();
88
- // sercom->initMasterWIRE(baudrate);
89
- // sercom->enableWIRE();
90
-
103
+ void TwoWire::setClock (uint32_t baudrate)
104
+ {
91
105
// ToDo: disable I2C while switching, if necessary
92
106
93
107
_config.ui32ClockFreq = baudrate;
94
108
initialize (); // Initialize the IOM
95
109
}
96
110
97
- void TwoWire::end () {
111
+ void TwoWire::end ()
112
+ {
98
113
// sercom->disableWIRE();
99
114
}
100
115
101
116
uint8_t TwoWire::requestFrom (uint8_t address, size_t quantity, bool stopBit)
102
117
{
103
- if (quantity == 0 )
118
+ if (quantity == 0 )
104
119
{
105
120
return 0 ;
106
121
}
107
122
size_t byteRead = 0 ;
108
123
_rxBuffer.clear ();
109
124
110
125
am_hal_iom_transfer_t iomTransfer = {0 };
111
- iomTransfer.uPeerInfo .ui32I2CDevAddr = address;
112
- iomTransfer.ui32InstrLen = 0 ; // 8-bit transfers
113
- iomTransfer.ui32Instr = 0 ; // Offset;
114
- iomTransfer.ui32NumBytes = quantity; // How many bytes to receive
115
- iomTransfer.eDirection = AM_HAL_IOM_RX;
116
- iomTransfer.pui32TxBuffer = NULL ;
117
- iomTransfer.pui32RxBuffer = (uint32_t *)_linearBugger; // Link in the RX buffer
118
- iomTransfer.bContinue = stopBit ? false : true ;
119
- iomTransfer.ui8RepeatCount = 0 ; // ?
120
- iomTransfer.ui8Priority = 1 ; // ?
121
- iomTransfer.ui32PauseCondition = 0 ; // ?
122
- iomTransfer.ui32StatusSetClr = 0 ; // ?
123
-
124
- uint32_t retVal32 = am_hal_iom_blocking_transfer (_handle, &iomTransfer);
125
- if (retVal32 != 0 )
126
- {
127
- // Serial.println("got an error on requestFrom");
128
- return retVal32;
129
- }
126
+ iomTransfer.uPeerInfo .ui32I2CDevAddr = address;
127
+ iomTransfer.ui32InstrLen = 0 ; // 8-bit transfers
128
+ iomTransfer.ui32Instr = 0 ; // Offset;
129
+ iomTransfer.ui32NumBytes = quantity; // How many bytes to receive
130
+ iomTransfer.eDirection = AM_HAL_IOM_RX;
131
+ iomTransfer.pui32TxBuffer = NULL ;
132
+ iomTransfer.pui32RxBuffer = (uint32_t *)_linearBugger; // Link in the RX buffer
133
+ iomTransfer.bContinue = stopBit ? false : true ;
134
+ iomTransfer.ui8RepeatCount = 0 ; // ?
135
+ iomTransfer.ui8Priority = 1 ; // ?
136
+ iomTransfer.ui32PauseCondition = 0 ; // ?
137
+ iomTransfer.ui32StatusSetClr = 0 ; // ?
138
+
139
+ uint32_t retVal32 = am_hal_iom_blocking_transfer (_handle, &iomTransfer);
140
+ if (retVal32 != 0 )
141
+ {
142
+ // Serial.println("got an error on requestFrom");
143
+ return retVal32;
144
+ }
130
145
131
146
// Copy the bytes into the rx buffer
132
- for (byteRead = 0 ; byteRead < quantity; byteRead++)
147
+ for (byteRead = 0 ; byteRead < quantity; byteRead++)
133
148
{
134
- _rxBuffer.store_char ( _linearBugger[byteRead] ); // Read data and send the ACK
149
+ _rxBuffer.store_char (_linearBugger[byteRead]); // Read data and send the ACK
135
150
}
136
151
137
152
return byteRead;
138
153
}
139
154
140
- void TwoWire::beginTransmission (uint8_t address) {
155
+ void TwoWire::beginTransmission (uint8_t address)
156
+ {
141
157
// save address of target and clear buffer
142
158
_transmissionAddress = address;
143
159
_txBuffer.clear ();
@@ -153,71 +169,77 @@ void TwoWire::beginTransmission(uint8_t address) {
153
169
// 4 : Other error
154
170
uint8_t TwoWire::endTransmission (bool stopBit)
155
171
{
156
- _transmissionBegun = false ;
172
+ _transmissionBegun = false ;
157
173
158
174
am_hal_iom_transfer_t iomTransfer = {0 };
159
- iomTransfer.uPeerInfo .ui32I2CDevAddr = _transmissionAddress;
160
- iomTransfer.ui32InstrLen = 0 ; // Use only data phase
161
- iomTransfer.ui32Instr = 0 ; //
162
- // iomTransfer.ui32NumBytes = ; //
163
- iomTransfer.eDirection = AM_HAL_IOM_TX;
164
- iomTransfer.pui32TxBuffer = (uint32_t *)_linearBugger;
165
- iomTransfer.pui32RxBuffer = NULL ;
166
- iomTransfer.bContinue = (stopBit ? false : true ); // whether or not to hold onto the bus after this transfer
167
- iomTransfer.ui8RepeatCount = 0 ; // ?
168
- iomTransfer.ui8Priority = 1 ; // ?
169
- iomTransfer.ui32PauseCondition = 0 ; // ?
170
- iomTransfer.ui32StatusSetClr = 0 ; // ?
175
+ iomTransfer.uPeerInfo .ui32I2CDevAddr = _transmissionAddress;
176
+ iomTransfer.ui32InstrLen = 0 ; // Use only data phase
177
+ iomTransfer.ui32Instr = 0 ; //
178
+ // iomTransfer.ui32NumBytes = ; //
179
+ iomTransfer.eDirection = AM_HAL_IOM_TX;
180
+ iomTransfer.pui32TxBuffer = (uint32_t *)_linearBugger;
181
+ iomTransfer.pui32RxBuffer = NULL ;
182
+ iomTransfer.bContinue = (stopBit ? false : true ); // whether or not to hold onto the bus after this transfer
183
+ iomTransfer.ui8RepeatCount = 0 ; // ?
184
+ iomTransfer.ui8Priority = 1 ; // ?
185
+ iomTransfer.ui32PauseCondition = 0 ; // ?
186
+ iomTransfer.ui32StatusSetClr = 0 ; // ?
171
187
172
188
// Copy the bytes from the TX Buffer into the linear buffer
173
189
size_t count = 0 ;
174
- if ( _txBuffer.available () > AP3_WIRE_LINEAR_BUFFER_LEN ){
190
+ if (_txBuffer.available () > AP3_WIRE_LINEAR_BUFFER_LEN)
191
+ {
175
192
return 1 ; // data too long
176
193
}
177
- while ( _txBuffer.available () )
194
+ while ( _txBuffer.available ())
178
195
{
179
196
*(_linearBugger + count++) = _txBuffer.read_char ();
180
197
}
181
198
182
199
iomTransfer.ui32NumBytes = count; // Set the length
183
200
184
- // ToDo: better error reporting
201
+ // ToDo: better error reporting
185
202
uint32_t retVal32 = am_hal_iom_blocking_transfer (_handle, &iomTransfer);
186
- switch ( retVal32 ){
187
- case AM_HAL_STATUS_SUCCESS :
188
- return 0 ;
189
- break ;
203
+ switch (retVal32)
204
+ {
205
+ case AM_HAL_STATUS_SUCCESS:
206
+ return 0 ;
207
+ break ;
190
208
191
209
// return 1; // data too long
192
210
// return 2; // NAK on address
193
211
// return 3; // NAK on data
194
212
195
- case AM_HAL_STATUS_INVALID_OPERATION :
196
- case AM_HAL_STATUS_INVALID_ARG :
197
- case AM_HAL_STATUS_INVALID_HANDLE :
198
- default :
199
- return 4 ; break ; // other error
213
+ case AM_HAL_STATUS_INVALID_OPERATION:
214
+ case AM_HAL_STATUS_INVALID_ARG:
215
+ case AM_HAL_STATUS_INVALID_HANDLE:
216
+ default :
217
+ return 4 ;
218
+ break ; // other error
200
219
}
201
220
}
202
221
203
222
size_t TwoWire::write (uint8_t ucData)
204
223
{
205
224
// No writing, without begun transmission or a full buffer
206
- if ( !_transmissionBegun || _txBuffer.isFull () ){
207
- return 0 ;
225
+ if (!_transmissionBegun || _txBuffer.isFull ())
226
+ {
227
+ return 0 ;
208
228
}
209
- _txBuffer.store_char ( ucData ) ;
210
- return 1 ;
229
+ _txBuffer.store_char (ucData) ;
230
+ return 1 ;
211
231
}
212
232
213
233
size_t TwoWire::write (const uint8_t *data, size_t quantity)
214
234
{
215
- for (size_t i = 0 ; i < quantity; ++i){ // Try to store all data
216
- if (!write (data[i])){ // Return the number of data stored, when the buffer is full (if write return 0)
235
+ for (size_t i = 0 ; i < quantity; ++i)
236
+ { // Try to store all data
237
+ if (!write (data[i]))
238
+ { // Return the number of data stored, when the buffer is full (if write return 0)
217
239
return i;
218
240
}
219
241
}
220
- return quantity;// All data stored
242
+ return quantity; // All data stored
221
243
}
222
244
223
245
int TwoWire::available (void )
@@ -241,12 +263,12 @@ void TwoWire::flush(void)
241
263
// data transfer.
242
264
}
243
265
244
- void TwoWire::onReceive (void (*function)(int ))
266
+ void TwoWire::onReceive (void (*function)(int ))
245
267
{
246
268
_onReceiveCallback = function;
247
269
}
248
270
249
- void TwoWire::onRequest (void (*function)(void ))
271
+ void TwoWire::onRequest (void (*function)(void ))
250
272
{
251
273
_onRequestCallback = function;
252
274
}
@@ -257,7 +279,7 @@ void TwoWire::onService(void)
257
279
258
280
// if ( sercom->isSlaveWIRE() )
259
281
// {
260
- // if(sercom->isStopDetectedWIRE() ||
282
+ // if(sercom->isStopDetectedWIRE() ||
261
283
// (sercom->isAddressMatch() && sercom->isRestartDetectedWIRE() && !sercom->isMasterReadOperationWIRE())) //Stop or Restart detected
262
284
// {
263
285
// sercom->prepareAckBitWIRE();
@@ -268,7 +290,7 @@ void TwoWire::onService(void)
268
290
// {
269
291
// onReceiveCallback(available());
270
292
// }
271
-
293
+
272
294
// rxBuffer.clear();
273
295
// }
274
296
// else if(sercom->isAddressMatch()) //Address Match
@@ -302,12 +324,12 @@ void TwoWire::onService(void)
302
324
// transmissionBegun = sercom->sendDataSlaveWIRE(c);
303
325
// } else { //Received data
304
326
// if (rxBuffer.isFull()) {
305
- // sercom->prepareNackBitWIRE();
327
+ // sercom->prepareNackBitWIRE();
306
328
// } else {
307
329
// //Store data
308
330
// rxBuffer.store_char(sercom->readDataWIRE());
309
331
310
- // sercom->prepareAckBitWIRE();
332
+ // sercom->prepareAckBitWIRE();
311
333
// }
312
334
313
335
// sercom->prepareCommandBitsWire(0x03);
@@ -316,34 +338,32 @@ void TwoWire::onService(void)
316
338
// }
317
339
}
318
340
319
-
320
-
321
341
// In variant.h define WIRE_INTERFACES_COUNT as well as the io master instance to use for each AP3_Wire_IOM and AP3_WireN_IOM [1 <= N <= 5]
322
342
323
343
#if WIRE_INTERFACES_COUNT > 0
324
- TwoWire Wire (AP3_Wire_IOM);
344
+ TwoWire Wire (AP3_Wire_IOM);
325
345
326
346
// void WIRE_IT_HANDLER(void) { // example of how you might implement directable callbacks. #define WIRE_IT_HANDLER your_handler in variant.h
327
347
// Wire.onService();
328
348
// }
329
349
#endif
330
350
331
351
#if WIRE_INTERFACES_COUNT > 1
332
- TwoWire Wire1 (AP3_Wire1_IOM);
352
+ TwoWire Wire1 (AP3_Wire1_IOM);
333
353
#endif
334
354
335
355
#if WIRE_INTERFACES_COUNT > 2
336
- TwoWire Wire2 (AP3_Wire1_IOM);
356
+ TwoWire Wire2 (AP3_Wire1_IOM);
337
357
#endif
338
358
339
359
#if WIRE_INTERFACES_COUNT > 3
340
- TwoWire Wire3 (AP3_Wire1_IOM);
360
+ TwoWire Wire3 (AP3_Wire1_IOM);
341
361
#endif
342
362
343
363
#if WIRE_INTERFACES_COUNT > 4
344
- TwoWire Wire4 (AP3_Wire1_IOM);
364
+ TwoWire Wire4 (AP3_Wire1_IOM);
345
365
#endif
346
366
347
367
#if WIRE_INTERFACES_COUNT > 5
348
- TwoWire Wire5 (AP3_Wire1_IOM);
368
+ TwoWire Wire5 (AP3_Wire1_IOM);
349
369
#endif
0 commit comments