Ref::Serial

Leo Selavo edited this page Jan 14, 2016 · 2 revisions

Serial port allows for exchange of digital data between devices, such as a microcontroller and a PC or another module.

Location

Functions

// Serial port default baudrate
#define SERIAL_BAUDRATE 38400

// Platform-specifig: the number of serial ports available
#define SERIAL_COUNT 1

// Callback for serial RX interrupt
typedef void (* SerialCallback_t)(uint8_t);


// Initialize serial interface with specific speed
// - id     serial interface ID
// - speed  serial interface baudrate.
//     The values supported are platform-specific.
//     On 1-series msp430 the following rates are supported:
//     2400, 4800, 9600 (the default), 38400, and 115200 bps
// - conf   advanced configuarion, unused at the moment.
uint_t serialInit(uint8_t id, uint32_t speed, uint8_t conf);

// Enable transmission on a specific serial interface
void serialEnableTX(uint8_t id);
// Disable transmission on a specific serial interface
void serialDisableTX(uint8_t id);
// Enable reception on a specific serial interface
void serialEnableRX(uint8_t id);
// Disable reception on a specific serial interface
void serialDisableRX(uint8_t id);

// Send a single byte to a specific serial interface
void serialSendByte(uint8_t id, uint8_t data);

// Send binary data to a specific serial interface
void serialSendData(uint8_t id, const uint8_t *data, uint16_t len);

// Send ASCII string to a specific serial interface
// Note: a single '\\n' symbol is converted to '\\r\\n'!
void serialSendString(uint8_t id, const char *string);

// Set callback function for per-byte data receive.
// The callback is called on every received byte
// Note: enables serial RX automatically if 'cb' is non-NULL.
// - id - ID of the UART used (See MCU datasheet to get IDs)
// - cb - callback function: void myCallback(uint8_t byte)
/uint_t serialSetReceiveHandle(uint8_t id, SerialCallback_t cb);

// Set callback for per-packet data receive.
// Stores the received bytes in  the buffer and the callback is called when:
//  - either a newline is received ('\\n', binary value 10),
//  - or at most len bytes are received.
// The newline is also stored in the buffer.
// After the callback returns buffer is reset and reception restarts.
// Note: enables serial RX automatically if 'cb' is non-NULL.
// Warning: Can use only one Serial at a time (single buffer, single handler)!
// - id - ID of the UART used (See MCU datasheet to get IDs)
// - cb - callback function: void myCallback(uint8_t bytes). Here the
//             bytes parameter contains not the last byte received but
//             total received byte count (i.e., bytes stored in the buffer)!
// - buf - the buffer where to store the packet
// - len - size of the buffer in bytes. Callback is called when len
//              bytes are received (or when '\\n' is received).
//              When len is zero, no packet size is checked, only on newline
//              reception the callback is called.
uint_t serialSetPacketReceiveHandle(uint8_t id, SerialCallback_t cb,
                                    void *buf, uint16_t len);

Example

The following example uses radio and serial port interfaces to forward data packets: apps/demo/PrintRadio

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.