Skip to content

Import of Wire library reference pages #448

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions Language/Functions/Communication/Wire.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
---
title: Wire
categories: [ "Functions" ]
subCategories: [ "Communication" ]
---


= Wire


//OVERVIEW SECTION STARTS
[#overview]
--

[float]
=== Description


This library allows you to communicate with I2C/TWI devices. On the Arduino boards with the R3 layout (1.0 pinout), the SDA (data line) and SCL (clock line) are on the pin headers close to the AREF pin. The Arduino Due has two I2C/TWI interfaces SDA1 and SCL1 are near to the AREF pin and the additional one is on pins 20 and 21.

As a reference the table below shows where TWI pins are located on various Arduino boards.

[cols="1,1"]
|===
|Board
|I2C/TWI pins

|UNO, Ethernet
|A4 (SDA), A5 (SCL)

|Mega2560
|20 (SDA), 21 (SCL)

|Leonardo
|20 (SDA), 21 (SCL), SDA1, SCL1
|===


As of Arduino 1.0, the library inherits from the Stream functions, making it consistent with other read/write libraries. Because of this, `send()` and `receive()` have been replaced with `read()` and `write()`.

Recent versions of the Wire library can use timeouts to prevent a lockup in the face of certain problems on the bus, but this is not enabled by default (yet) in current versions. It is recommended to always enable these timeouts when using the Wire library. See the Wire.setWireTimeout function for more details.

*Note:* There are both 7 and 8-bit versions of I2C addresses. 7 bits identify the device, and the eighth bit determines if it's being written to or read from. The Wire library uses 7 bit addresses throughout. If you have a datasheet or sample code that uses 8-bit address, you'll want to drop the low bit (i.e. shift the value one bit to the right), yielding an address between 0 and 127. However the addresses from 0 to 7 are not used because are reserved so the first address that can be used is 8. Please note that a pull-up resistor is needed when connecting SDA/SCL pins. Please refer to the examples for more information. MEGA 2560 board has pull-up resistors on pins 20 and 21 onboard.

*The Wire library implementation uses a 32 byte buffer, therefore any communication should be within this limit. Exceeding bytes in a single transmission will just be dropped.*

To use this library:

`#include <Wire.h>`

--
// OVERVIEW SECTION ENDS

//FUNCTION SECTION STARTS
[#functions]
--

'''
[float]
=== Functions
link:../wire/begin[begin()] +
link:../wire/end[end()] +
link:../wire/requestfrom[requestFrom()] +
link:../wire/begintransmission[beginTransmission()] +
link:../wire/write[write()] +
link:../wire/available[available()] +
link:../wire/read[read()] +
link:../wire/setclock[setClock()] +
link:../wire/onreceive[onReceive()] +
link:../wire/onrequest[onRequest()] +
link:../wire/setwiretimeout[setWireTimeout()] +
link:../wire/clearwiretimeoutflag[clearWireTimeoutFlag()] +
link:../wire/getwiretimeoutflag[getWireTimeoutFlag()]

'''

--
// FUNCTION SECTION ENDS
30 changes: 30 additions & 0 deletions Language/Functions/Communication/Wire/available.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
title: available()
---

= available

//OVERVIEW SECTION STARTS
[#overview]
--

[float]
=== Description
This function returns the number of bytes available for retrieval with `read()`. This function should be called on a controller device after a call to `requestFrom()` or on a peripheral inside the `onReceive()` handler. `available()` inherits from the Stream utility class.

[float]
=== Syntax
`Wire.available()`

[float]
=== Parameters

None.

[float]
=== Returns

The number of bytes available for reading.

--
//OVERVIEW SECTION STARTS
31 changes: 31 additions & 0 deletions Language/Functions/Communication/Wire/begin.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
title: begin()
---
= begin

//OVERVIEW SECTION STARTS
[#overview]
--

[float]
=== Description

This function initializes the Wire library and join the I2C bus as a controller or a peripheral. This function should normally be called only once.

[float]
=== Syntax

`Wire.begin()`

`Wire.begin(address)`

[float]
=== Parameters
* _address_: the 7-bit slave address (optional); if not specified, join the bus as a controller device.

[float]
=== Returns
None.
--

//OVERVIEW SECTION ENDS
28 changes: 28 additions & 0 deletions Language/Functions/Communication/Wire/beginTransmission.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
title: beginTransmission()
---

= beginTransmission

//OVERVIEW SECTION STARTS
[#overview]
--

[float]
=== Description
This function begins a transmission to the I2C peripheral device with the given address. Subsequently, queue bytes for transmission with the `write()` function and transmit them by calling `endTransmission()`.

[float]
=== Syntax

`Wire.beginTransmission(address)`

[float]
=== Parameters
* _address_: the 7-bit address of the device to transmit to.

[float]
=== Returns
None.
--
//OVERVIEW SECTION ENDS
37 changes: 37 additions & 0 deletions Language/Functions/Communication/Wire/clearWireTimeoutFlag.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
title: clearWireTimeoutFlag()
---
= clearWireTimeoutFlag

//OVERVIEW SECTION STARTS
[#overview]
--

[float]
=== Description

Clears the timeout flag.

Timeouts might not be enabled by default. See the documentation for `Wire.setWireTimeout()` for more information on how to configure timeouts and how they work.


[float]
=== Syntax

`Wire.clearTimeout()`

[float]
=== Parameters
None.

[float]
=== Returns
* bool: The current value of the flag

[float]
=== Portability Notes
This function was not available in the original version of the Wire library and might still not be available on all platforms. Code that needs to be portable across platforms and versions can use the `WIRE_HAS_TIMEOUT` macro, which is only defined when `Wire.setWireTimeout()`, `Wire.getWireTimeoutFlag()` and `Wire.clearWireTimeout()` are all available.

--

//OVERVIEW SECTION ENDS
31 changes: 31 additions & 0 deletions Language/Functions/Communication/Wire/end.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
title: end()
---
= end

//OVERVIEW SECTION STARTS
[#overview]
--

[float]
=== Description

Disable the Wire library, reversing the effect of `Wire.begin()`. To use the Wire library again after this, call `Wire.begin()` again.

*Note:* This function was not available in the original version of the Wire library and might still not be available on all platforms. Code that needs to be portable across platforms and versions can use the `WIRE_HAS_END` macro, which is only defined when `Wire.end()` is available.

[float]
=== Syntax

`Wire.end()`

[float]
=== Parameters
None.

[float]
=== Returns
None.
--

//OVERVIEW SECTION ENDS
34 changes: 34 additions & 0 deletions Language/Functions/Communication/Wire/endTransmission.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
title: endTransmission()
---

= endTransmission

//OVERVIEW SECTION STARTS
[#overview]
--

[float]
=== Description
This function ends a transmission to a peripheral device that was begun by `beginTransmission()` and transmits the bytes that were queued by `write()`. As of Arduino 1.0.1, `endTransmission()` accepts a boolean argument changing its behavior for compatibility with certain I2C devices. If true, `endTransmission()` sends a stop message after transmission, releasing the I2C bus. If false, `endTransmission()` sends a restart message after transmission. The bus will not be released, which prevents another controller device from transmitting between messages. This allows one controller device to send multiple transmissions while in control. The default value is true.

[float]
=== Syntax
`Wire.endTransmission()`
`Wire.endTransmission(stop)`

[float]
=== Parameters

* _stop_: true or false. True will send a stop message, releasing the bus after transmission. False will send a restart, keeping the connection active.
[float]
=== Returns

* _0_: success.
* _1_: data too long to fit in transmit buffer.
* _2_: received NACK on transmit of address.
* _3_: received NACK on transmit of data.
* _4_: other error.
* _5_: timeout
--
//OVERVIEW SECTION ENDS
37 changes: 37 additions & 0 deletions Language/Functions/Communication/Wire/getWireTimeoutFlag.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
title: getWireTimeoutFlag()
---
= getWireTimeoutFlag

//OVERVIEW SECTION STARTS
[#overview]
--

[float]
=== Description

Checks whether a timeout has occured since the last time the flag was cleared.

This flag is set is set whenever a timeout occurs and cleared when `Wire.clearWireTimeoutFlag()` is called, or when the timeout is changed using `Wire.setWireTimeout()`.


[float]
=== Syntax

`Wire.getWireTimeoutFlag()`

[float]
=== Parameters
None.

[float]
=== Returns
* bool: The current value of the flag

[float]
=== Portability Notes
This function was not available in the original version of the Wire library and might still not be available on all platforms. Code that needs to be portable across platforms and versions can use the `WIRE_HAS_TIMEOUT` macro, which is only defined when `Wire.setWireTimeout()`, `Wire.getWireTimeoutFlag()` and `Wire.clearWireTimeout()` are all available.

--

//OVERVIEW SECTION ENDS
30 changes: 30 additions & 0 deletions Language/Functions/Communication/Wire/onReceive.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
title: onReceieve()
---

= onReceive

//OVERVIEW SECTION STARTS
[#overview]
--

[float]
=== Description

This function registers a function to be called when a peripheral device receives a transmission from a controller device.

[float]
=== Syntax
`Wire.onReceive(handler)`

[float]
=== Parameters

* _handler_: the function to be called when the peripheral device receives data; this should take a single int parameter (the number of bytes read from the controller device) and return nothing.

[float]
=== Returns

None.
--
//OVERVIEW SECTION ENDS
30 changes: 30 additions & 0 deletions Language/Functions/Communication/Wire/onRequest.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
title: onRequest()
---

= onRequest

//OVERVIEW SECTION STARTS
[#overview]
--

[float]
=== Description
This function registers a function to be called when a controller device requests data from a peripheral device.

[float]
=== Syntax
`Wire.onRequest(handler)`

[float]
=== Parameters

* _handler_: the function to be called, takes no parameters and returns nothing.

[float]
=== Returns

None.

--
//OVERVIEW SECTION ENDS
Loading