@@ -22,7 +22,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222SOFTWARE.
2323*/
2424
25- extern " C" {
25+ extern " C"
26+ {
2627#include < string.h>
2728}
2829
@@ -31,113 +32,128 @@ extern "C" {
3132
3233#include " Wire.h"
3334
34- typedef enum {
35+ typedef enum
36+ {
3537 AP3_WIRE_SCL = 0x00 ,
3638 AP3_WIRE_SDA,
37- }ap3_iom_i2c_pad_type_e;
39+ } ap3_iom_i2c_pad_type_e;
3840
3941TwoWire::TwoWire (uint8_t iom_instance) : IOMaster(iom_instance)
4042{
4143 _transmissionBegun = false ;
4244}
4345
44- void TwoWire::begin (void ) {
46+ void TwoWire::begin (void )
47+ {
4548 // Master Mode
4649
4750 am_hal_gpio_pincfg_t pincfg = AP3_GPIO_DEFAULT_PINCFG;
4851 uint8_t funcsel = 0 ;
4952
5053 ap3_err_t retval = AP3_OK;
5154
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+ }
5460 pincfg.uFuncSel = funcsel; // set the proper function select option for this instance/pin/type combination
5561 pincfg.ePullup = AM_HAL_GPIO_PIN_PULLUP_1_5K;
5662 pincfg.eDriveStrength = AM_HAL_GPIO_PIN_DRIVESTRENGTH_12MA;
5763 pincfg.eGPOutcfg = AM_HAL_GPIO_PIN_OUTCFG_OPENDRAIN;
5864 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+ }
6070 pincfg = AP3_GPIO_DEFAULT_PINCFG; // set back to default for use with next pin
6171
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+ }
6477 pincfg.uFuncSel = funcsel;
6578 pincfg.ePullup = AM_HAL_GPIO_PIN_PULLUP_1_5K;
6679 pincfg.eDriveStrength = AM_HAL_GPIO_PIN_DRIVESTRENGTH_12MA;
6780 pincfg.eGPOutcfg = AM_HAL_GPIO_PIN_OUTCFG_OPENDRAIN;
6881 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+ }
7087 pincfg = AP3_GPIO_DEFAULT_PINCFG; // set back to default for use with next pin
7188
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
7490 _config.eInterfaceMode = AM_HAL_IOM_I2C_MODE;
7591 _config.ui32ClockFreq = AM_HAL_IOM_100KHZ;
7692
7793 initialize (); // Initialize the IOM
7894}
7995
80- void TwoWire::begin (uint8_t address, bool enableGeneralCall) {
96+ void TwoWire::begin (uint8_t address, bool enableGeneralCall)
97+ {
8198 // Slave mode
8299
83100 // ToDo:
84101}
85102
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+ {
91105 // ToDo: disable I2C while switching, if necessary
92106
93107 _config.ui32ClockFreq = baudrate;
94108 initialize (); // Initialize the IOM
95109}
96110
97- void TwoWire::end () {
111+ void TwoWire::end ()
112+ {
98113 // sercom->disableWIRE();
99114}
100115
101116uint8_t TwoWire::requestFrom (uint8_t address, size_t quantity, bool stopBit)
102117{
103- if (quantity == 0 )
118+ if (quantity == 0 )
104119 {
105120 return 0 ;
106121 }
107122 size_t byteRead = 0 ;
108123 _rxBuffer.clear ();
109124
110125 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+ }
130145
131146 // Copy the bytes into the rx buffer
132- for (byteRead = 0 ; byteRead < quantity; byteRead++)
147+ for (byteRead = 0 ; byteRead < quantity; byteRead++)
133148 {
134- _rxBuffer.store_char ( _linearBugger[byteRead] ); // Read data and send the ACK
149+ _rxBuffer.store_char (_linearBugger[byteRead]); // Read data and send the ACK
135150 }
136151
137152 return byteRead;
138153}
139154
140- void TwoWire::beginTransmission (uint8_t address) {
155+ void TwoWire::beginTransmission (uint8_t address)
156+ {
141157 // save address of target and clear buffer
142158 _transmissionAddress = address;
143159 _txBuffer.clear ();
@@ -153,71 +169,77 @@ void TwoWire::beginTransmission(uint8_t address) {
153169// 4 : Other error
154170uint8_t TwoWire::endTransmission (bool stopBit)
155171{
156- _transmissionBegun = false ;
172+ _transmissionBegun = false ;
157173
158174 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 ; // ?
171187
172188 // Copy the bytes from the TX Buffer into the linear buffer
173189 size_t count = 0 ;
174- if ( _txBuffer.available () > AP3_WIRE_LINEAR_BUFFER_LEN ){
190+ if (_txBuffer.available () > AP3_WIRE_LINEAR_BUFFER_LEN)
191+ {
175192 return 1 ; // data too long
176193 }
177- while ( _txBuffer.available () )
194+ while ( _txBuffer.available ())
178195 {
179196 *(_linearBugger + count++) = _txBuffer.read_char ();
180197 }
181198
182199 iomTransfer.ui32NumBytes = count; // Set the length
183200
184- // ToDo: better error reporting
201+ // ToDo: better error reporting
185202 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 ;
190208
191209 // return 1; // data too long
192210 // return 2; // NAK on address
193211 // return 3; // NAK on data
194212
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
200219 }
201220}
202221
203222size_t TwoWire::write (uint8_t ucData)
204223{
205224 // 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 ;
208228 }
209- _txBuffer.store_char ( ucData ) ;
210- return 1 ;
229+ _txBuffer.store_char (ucData) ;
230+ return 1 ;
211231}
212232
213233size_t TwoWire::write (const uint8_t *data, size_t quantity)
214234{
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)
217239 return i;
218240 }
219241 }
220- return quantity;// All data stored
242+ return quantity; // All data stored
221243}
222244
223245int TwoWire::available (void )
@@ -241,12 +263,12 @@ void TwoWire::flush(void)
241263 // data transfer.
242264}
243265
244- void TwoWire::onReceive (void (*function)(int ))
266+ void TwoWire::onReceive (void (*function)(int ))
245267{
246268 _onReceiveCallback = function;
247269}
248270
249- void TwoWire::onRequest (void (*function)(void ))
271+ void TwoWire::onRequest (void (*function)(void ))
250272{
251273 _onRequestCallback = function;
252274}
@@ -257,7 +279,7 @@ void TwoWire::onService(void)
257279
258280 // if ( sercom->isSlaveWIRE() )
259281 // {
260- // if(sercom->isStopDetectedWIRE() ||
282+ // if(sercom->isStopDetectedWIRE() ||
261283 // (sercom->isAddressMatch() && sercom->isRestartDetectedWIRE() && !sercom->isMasterReadOperationWIRE())) //Stop or Restart detected
262284 // {
263285 // sercom->prepareAckBitWIRE();
@@ -268,7 +290,7 @@ void TwoWire::onService(void)
268290 // {
269291 // onReceiveCallback(available());
270292 // }
271-
293+
272294 // rxBuffer.clear();
273295 // }
274296 // else if(sercom->isAddressMatch()) //Address Match
@@ -302,12 +324,12 @@ void TwoWire::onService(void)
302324 // transmissionBegun = sercom->sendDataSlaveWIRE(c);
303325 // } else { //Received data
304326 // if (rxBuffer.isFull()) {
305- // sercom->prepareNackBitWIRE();
327+ // sercom->prepareNackBitWIRE();
306328 // } else {
307329 // //Store data
308330 // rxBuffer.store_char(sercom->readDataWIRE());
309331
310- // sercom->prepareAckBitWIRE();
332+ // sercom->prepareAckBitWIRE();
311333 // }
312334
313335 // sercom->prepareCommandBitsWire(0x03);
@@ -316,34 +338,32 @@ void TwoWire::onService(void)
316338 // }
317339}
318340
319-
320-
321341// 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]
322342
323343#if WIRE_INTERFACES_COUNT > 0
324- TwoWire Wire (AP3_Wire_IOM);
344+ TwoWire Wire (AP3_Wire_IOM);
325345
326346// void WIRE_IT_HANDLER(void) { // example of how you might implement directable callbacks. #define WIRE_IT_HANDLER your_handler in variant.h
327347// Wire.onService();
328348// }
329349#endif
330350
331351#if WIRE_INTERFACES_COUNT > 1
332- TwoWire Wire1 (AP3_Wire1_IOM);
352+ TwoWire Wire1 (AP3_Wire1_IOM);
333353#endif
334354
335355#if WIRE_INTERFACES_COUNT > 2
336- TwoWire Wire2 (AP3_Wire1_IOM);
356+ TwoWire Wire2 (AP3_Wire1_IOM);
337357#endif
338358
339359#if WIRE_INTERFACES_COUNT > 3
340- TwoWire Wire3 (AP3_Wire1_IOM);
360+ TwoWire Wire3 (AP3_Wire1_IOM);
341361#endif
342362
343363#if WIRE_INTERFACES_COUNT > 4
344- TwoWire Wire4 (AP3_Wire1_IOM);
364+ TwoWire Wire4 (AP3_Wire1_IOM);
345365#endif
346366
347367#if WIRE_INTERFACES_COUNT > 5
348- TwoWire Wire5 (AP3_Wire1_IOM);
368+ TwoWire Wire5 (AP3_Wire1_IOM);
349369#endif
0 commit comments