Menu
An Arduino Library that facilitates packet-based serial communication using COBS or SLIP encoding.
Features
Dec 10, 2014 - Serial communication is a major backbone in embedded electronics and it is exceptionally common to have two embedded devices to talk to.
PacketSerial is an small, efficient, library that allows Arduinos to send and receive serial data packets (with COBS, SLIP or a user-defined encoding) that include bytes of any value (0 - 255).A packet is simply an array of bytes.
Documentation
If you're asking Why do I need this?, read the background introduction page. If you're ready to get started, check out the getting started page. You can also learn a lot by reading through the examples. Finally, if you're interested in learning more about how the code works, read the comments in the source code and check out the API documentation.
Support
If you're looking for help, read the support page.
Compatibility
Requrements
- Arduino IDE (version 1.5+)
Other Platforms
This project has been used successfully with openFrameworks using the ofxSerial addon. In particular, see the
ofx::IO::PacketSerial
object. Additionally this project has been used with Python using the PySerial package. In particular, check out the COBS (see this discussion) and SLIP packages.Ultimately, any library that correctly implements a COBS or SLIP encoding scheme should be compatible with this project.
Continuous Integration
Continuous integration tests are carried out on a variety of common Arduino platforms. See this script for a list.
Licensing
This project is licensed under the MIT License.
Project Management
Repository
Contributing
If you'd like to contribute to this project, please check out the Code of Conduct and the contributing guide.
Versioning
This project uses Semantic Versioning. You can check out recent changes in the changelog.
- Arduino Tutorial
- Arduino Function Libraries
- Arduino Advanced
- Arduino Projects
- Arduino Sensors
- Motor Control
- Arduino And Sound
- Arduino Useful Resources
- Selected Reading
A Serial Peripheral Interface (SPI) bus is a system for serial communication, which uses up to four conductors, commonly three. One conductor is used for data receiving, one for data sending, one for synchronization and one alternatively for selecting a device to communicate with. It is a full duplex connection, which means that the data is sent and received simultaneously. The maximum baud rate is higher than that in the I2C communication system.
Board SPI Pins
SPI uses the following four wires −
- SCK − This is the serial clock driven by the master.
- MOSI − This is the master output / slave input driven by the master.
- MISO − This is the master input / slave output driven by the master.
- SS − This is the slave-selection wire.
The following functions are used. You have to include the SPI.h.
- SPI.begin() − Initializes the SPI bus by setting SCK, MOSI, and SS to outputs, pulling SCK and MOSI low, and SS high.
- SPI.setClockDivider(divider) − To set the SPI clock divider relative to the system clock. On AVR based boards, the dividers available are 2, 4, 8, 16, 32, 64 or 128. The default setting is SPI_CLOCK_DIV4, which sets the SPI clock to one-quarter of the frequency of the system clock (5 Mhz for the boards at 20 MHz).
- Divider − It could be (SPI_CLOCK_DIV2, SPI_CLOCK_DIV4, SPI_CLOCK_DIV8, SPI_CLOCK_DIV16, SPI_CLOCK_DIV32, SPI_CLOCK_DIV64, SPI_CLOCK_DIV128).
- SPI.transfer(val) − SPI transfer is based on a simultaneous send and receive: the received data is returned in receivedVal.
- SPI.beginTransaction(SPISettings(speedMaximum, dataOrder, dataMode)) − speedMaximum is the clock, dataOrder(MSBFIRST or LSBFIRST), dataMode(SPI_MODE0, SPI_MODE1, SPI_MODE2, or SPI_MODE3).
We have four modes of operation in SPI as follows −
- Mode 0 (the default) − Clock is normally low (CPOL = 0), and the data is sampled on the transition from low to high (leading edge) (CPHA = 0).
- Mode 1 − Clock is normally low (CPOL = 0), and the data is sampled on the transition from high to low (trailing edge) (CPHA = 1).
- Mode 2 − Clock is normally high (CPOL = 1), and the data is sampled on the transition from high to low (leading edge) (CPHA = 0).
- Mode 3 − Clock is normally high (CPOL = 1), and the data is sampled on the transition from low to high (trailing edge) (CPHA = 1).
- SPI.attachInterrupt(handler) − Function to be called when a slave device receives data from the master.
Now, we will connect two Arduino UNO boards together; one as a master and the other as a slave.
- (SS) : pin 10
- (MOSI) : pin 11
- (MISO) : pin 12
- (SCK) : pin 13
The ground is common. Following is the diagrammatic representation of the connection between both the boards −
Let us see examples of SPI as Master and SPI as Slave.