From 52ab68c71d424fdd89a42586d19ee0bfea29255b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Antonio=20Bagur=20N=C3=A1jera?= Date: Tue, 3 Aug 2021 20:04:10 -0600 Subject: [PATCH 1/2] Add docs folder --- libraries/Wire/docs/api.md | 370 ++++++++++++++++++++++++++++++++++ libraries/Wire/docs/readme.md | 32 +++ 2 files changed, 402 insertions(+) create mode 100644 libraries/Wire/docs/api.md create mode 100644 libraries/Wire/docs/readme.md diff --git a/libraries/Wire/docs/api.md b/libraries/Wire/docs/api.md new file mode 100644 index 000000000..81a806003 --- /dev/null +++ b/libraries/Wire/docs/api.md @@ -0,0 +1,370 @@ +# Wire library + +## Methods + +### `begin()` + +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. + +#### Syntax + +``` +Wire.begin() +Wire.begin(address) +``` + +#### Parameters + +* _address_: the 7-bit slave address (optional); if not specified, join the bus as a controller device. + +#### Returns + +None. + +#### See also + +* [requestFrom()](#requestfrom) +* [beginTransmission()](#begintransmission) +* [endTransmission()](#endtransmission) +* [write()](#write) +* [available()](#available) +* [read()](#read) +* [setClock()](#setclock) +* [onReceive()](#onreceive) +* [onRequest()](#onrequest) + +### `requestFrom()` + +This function is used by the controller device to request bytes from a peripheral device. The bytes may then be retrieved with the available() and read() functions. As of Arduino 1.0.1, requestFrom() accepts a boolean argument changing its behavior for compatibility with certain I2C devices. If true, requestFrom() sends a stop message after the request, releasing the I2C bus. If false, requestFrom() sends a restart message after the request. The bus will not be released, which prevents another master device from requesting between messages. This allows one master device to send multiple requests while in control. The default value is true. + +#### Syntax + +``` +Wire.requestFrom(address, quantity) +Wire.requestFrom(address, quantity, stop) +``` + +#### Parameters + +* _address_: the 7-bit slave address of the device to request bytes from. +* _quantity_: the number of bytes to request. +* _stop_: true or false. true will send a stop message after the request, releasing the bus. False will continually send a restart after the request, keeping the connection active. + +#### Returns + +* _byte_ : the number of bytes returned from the peripheral device. + +#### See also + +* [begin()](#begin) +* [beginTransmission()](#begintransmission) +* [endTransmission()](#endtransmission) +* [write()](#write) +* [available()](#available) +* [read()](#read) +* [setClock()](#setclock) +* [onReceive()](#onreceive) +* [onRequest()](#onrequest) + +### `beginTransmission()` + +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(). + +#### Syntax + +``` +Wire.beginTransmission(address) +``` + +#### Parameters + +* _address_: the 7-bit address of the device to transmit to. + +#### Returns + +None. + +#### See also + +* [begin()](#begin) +* [requestFrom()](#requestfrom) +* [endTransmission()](#endtransmission) +* [write()](#write) +* [available()](#available) +* [read()](#read) +* [setClock()](#setclock) +* [onReceive()](#onreceive) +* [onRequest()](#onrequest) + +### `endTransmission()` + +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. + +#### Syntax + +``` +Wire.endTransmission() +Wire.endTransmission(stop) +``` + +#### 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. + +#### 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. + +#### See also + +* [begin()](#begin) +* [requestFrom()](#requestfrom) +* [beginTransmission()](#begintransmission) +* [write()](#write) +* [available()](#available) +* [read()](#read) +* [setClock()](#setclock) +* [onReceive()](#onreceive) +* [onRequest()](#onrequest) + +### `write()` + +This function writes data from a peripheral device in response to a request from a controller device, or queues bytes for transmission from a controller to peripheral device (in-between calls to beginTransmission() and endTransmission()). + +#### Syntax + +``` +Wire.write(value) +Wire.write(string) +Wire.write(data, length) +``` + +#### Parameters + +* _value_: a value to send as a single byte. +* _string_: a string to send as a series of bytes. +* _data_: an array of data to send as bytes. +* _length_: the number of bytes to transmit. + +#### Returns + +The number of bytes written (reading this number is optional). + +#### Example + +``` +#include + +byte val = 0; + +void setup() { + Wire.begin(); // Join I2C bus +} + +void loop() { + Wire.beginTransmission(44); // Transmit to device number 44 (0x2C) + + Wire.write(val); // Sends value byte + Wire.endTransmission(); // Stop transmitting + + val++; // Increment value + + // if reached 64th position (max) + if(val == 64) { + val = 0; // Start over from lowest value + } + + delay(500); +} +``` + +#### See also + +* [begin()](#begin) +* [requestFrom()](#requestfrom) +* [beginTransmission()](#begintransmission) +* [endTransmission()](#endtransmission) +* [write()](#write) +* [available()](#available) +* [read()](#read) +* [setClock()](#setclock) +* [onReceive()](#onreceive) +* [onRequest()](#onrequest) + +### `available()` + +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. + +#### Syntax + +``` +Wire.available() +``` + +#### Parameters + +None. + +#### Returns + +The number of bytes available for reading. + +#### See also + +* [begin()](#begin) +* [requestFrom()](#requestFrom) +* [beginTransmission()](#begintransmission) +* [endTransmission()](#endtransmission) +* [write()](#write) +* [available()](#available) +* [read()](#read) +* [setClock()](#setclock) +* [onReceive()](#onreceive) +* [onRequest()](#onrequest) + +### `read()` + +This function reads a byte that was transmitted from a peripheral device to a controller device after a call to requestFrom() or was transmitted from a controller device to a peripheral device. read() inherits from the Stream utility class. + +#### Syntax + +``` +Wire.read() +``` + +#### Parameters + +None. + +#### Returns + +The next byte received. + +#### Example + +``` +#include + +void setup() { + Wire.begin(); // Join I2C bus (address is optional for controller device) + Serial.begin(9600); // Start serial for output +} + +void loop() { + Wire.requestFrom(2, 6); // Request 6 bytes from slave device number two + + // Slave may send less than requested + while(Wire.available()) { + char c = Wire.read(); // Receive a byte as character + Serial.print(c); // Print the character + } + + delay(500); +} +``` + +#### See also + +* [begin()](#begin) +* [requestFrom()](#requestfrom) +* [beginTransmission()](#begintransmission) +* [endTransmission()](#endtransmission) +* [write()](#write) +* [available()](#available) +* [setClock()](#setclock) +* [onReceive()](#onreceive) +* [onRequest()](#onrequest) + +### `setClock()` + +This function modifies the clock frequency for I2C communication. I2C peripheral devices have no minimum working clock frequency, however 100KHz is usually the baseline. + +#### Syntax + +``` +Wire.setClock(clockFrequency) +``` + +#### Parameters + +* _clockFrequency_: the value (in Hertz) of the desired communication clock. Accepted values are 100000 (standard mode) and 400000 (fast mode). Some processors also support 10000 (low speed mode), 1000000 (fast mode plus) and 3400000 (high speed mode). Please refer to the specific processor documentation to make sure the desired mode is supported. + +#### Returns + +None. + +#### See also + +* [begin()](#begin) +* [requestFrom()](#requestfrom) +* [beginTransmission()](#begintransmission) +* [endTransmission()](#endtransmission) +* [write()](#write) +* [available()](#available) +* [read()](#read) +* [onReceive()](#onreceive) +* [onRequest()](#onrequest) + +### `onReceive()` + +This function registers a function to be called when a peripheral device receives a transmission from a controller device. + +#### Syntax + +``` +Wire.onReceive(handler) +``` + +#### 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. + +#### Returns + +None. + +#### See also + +* [begin()](#begin) +* [requestFrom()](#requestfrom) +* [beginTransmission()](#begintransmission) +* [endTransmission()](#endtransmission) +* [write()](#write) +* [available()](#available) +* [read()](#read) +* [setClock()](#setclock) +* [onRequest()](#onrequest) + +### `onRequest()` + +This function registers a function to be called when a controller device requests data from a peripheral device. + +#### Syntax + +``` +Wire.onRequest(handler) +``` + +#### Parameters + +* _handler_: the function to be called, takes no parameters and returns nothing. + +#### Returns + +None. + +#### See also + +* [begin()](#begin) +* [requestFrom()](#requestfrom) +* [beginTransmission()](#begintransmission) +* [endTransmission()](#endtransmission) +* [write()](#write) +* [available()](#available) +* [read()](#read) +* [setClock()](#setclock) +* [onReceive()](#onreceive) \ No newline at end of file diff --git a/libraries/Wire/docs/readme.md b/libraries/Wire/docs/readme.md new file mode 100644 index 000000000..997a11940 --- /dev/null +++ b/libraries/Wire/docs/readme.md @@ -0,0 +1,32 @@ +# Wire library + +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. + +| Board | I2C/TWI pins | +|-------------|------------------------------| +|UNO, Ethernet|A4 (SDA), A5 (SCL) | +|Mega2560 |20 (SDA), 21 (SCL) | +|Leonardo |2 (SDA), 3 (SCL) | +|Due |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(). + +**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 informations. 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 +``` + +## Examples + +* [Digital Potentiometer](https://www.arduino.cc/en/Tutorial/LibraryExamples/DigitalPotentiometer): Control an Analog Devices AD5171 digital potentiometer. +* [Master Reader/Slave Writer](https://www.arduino.cc/en/Tutorial/LibraryExamples/MasterReader): Program two Arduino boards to communicate with one another in a Master Reader/Slave Sender configuration via the I2C. +* [Master Writer/Slave receive](https://www.arduino.cc/en/Tutorial/LibraryExamples/MasterWriter)r:Program two Arduino boards to communicate with one another in a Master Writer/Slave Receiver configuration via the I2C. +* [SFR Ranger Reader](https://www.arduino.cc/en/Tutorial/LibraryExamples/SFRRangerReader): Read an ultra-sonic range finder interfaced via the I2C. +* [Add SERCOM](https://www.arduino.cc/en/Tutorial/SamdSercom): Adding mores Serial interfaces to SAMD microcontrollers. \ No newline at end of file From 31ec1f42443a0f5b6a24706e217f3b2009a077fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Antonio=20Bagur=20N=C3=A1jera?= Date: Tue, 3 Aug 2021 22:28:21 -0600 Subject: [PATCH 2/2] Update api.md --- libraries/Wire/docs/readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/Wire/docs/readme.md b/libraries/Wire/docs/readme.md index 997a11940..7a69c5795 100644 --- a/libraries/Wire/docs/readme.md +++ b/libraries/Wire/docs/readme.md @@ -13,7 +13,7 @@ As a reference the table below shows where TWI pins are located on various Ardui 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(). -**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 informations. MEGA 2560 board has pull-up resistors on pins 20 and 21 onboard. +**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.**