mraa  2.0.0
Low Level Skeleton Library for Communication on GNU/Linux platforms
Data Structures | Functions | Macros | Typedefs | Enumerations
uart_ow.h File Reference

API Description

This module allows one to use MRAA's UART support in order to interact with Dallas 1-wire compliant devices on a 1-wire bus. It makes use of the UART for timing purposes. The principle of operation is described here: https://www.maximintegrated.com/en/app-notes/index.mvp/id/214

It is important the you use a UART with CMOS/TTL level voltages (3.3v/5v) RX and TX lines. DO NOT use standard RS232 level voltages, or you are going to have a bad day.

In addition, a diode should be placed across the RX and TX lines like so:

-| U| A| TX—|<–+ R| | T| RX----—o-----—o 1-wire data bus -|

The diode on TX is a 1N4148 (cheap and common), with the cathode connected to TX, and the anode connected to RX and the rest of the 1-wire data line.

uart_ow = mraa_uart_ow_init(0);
if (uart_ow == NULL) {
fprintf(stderr, "Failed to initialize UART OW\n");
return EXIT_FAILURE;
}
// Reset the ow bus and see if anything is present
status = mraa_uart_ow_reset(uart_ow);
if (status == MRAA_SUCCESS) {
fprintf(stdout, "Reset succeeded, device(s) detected!\n");
} else {
goto err_exit;
}
fprintf(stdout, "Looking for devices...\n");
/*
* we are essentially doing a binary tree search through the 64
* bit address space. id is modified during this search, and will
* be set to the valid rom code for each device found.
*
* start the search from scratch
*/
status = mraa_uart_ow_rom_search(uart_ow, 1, id);
if (status != MRAA_SUCCESS) {
goto err_exit;
}
while (status == MRAA_SUCCESS) {
/* The first byte (id[0]]) is the device type (family) code.
* The last byte (id[7]) is the rom code CRC value. The
* intervening bytes (id[1]-id[6]) are the unique 48 bit
* device ID.
*/
fprintf(stdout, "Device %02d Type 0x%02x ID %02x%02x%02x%02x%02x%02x CRC 0x%02x\n", count,
id[0], id[6], id[5], id[4], id[3], id[2], id[1], id[7]);
count++;
// continue the search with start argument set to 0
status = mraa_uart_ow_rom_search(uart_ow, 0, id);
}
/* stop uart_ow */

Go to the source code of this file.

Data Structures

struct  _mraa_uart_ow
 

Functions

mraa_uart_ow_context mraa_uart_ow_init (int uart)
 
mraa_uart_ow_context mraa_uart_ow_init_raw (const char *path)
 
const char * mraa_uart_ow_get_dev_path (mraa_uart_ow_context dev)
 
mraa_result_t mraa_uart_ow_stop (mraa_uart_ow_context dev)
 
int mraa_uart_ow_read_byte (mraa_uart_ow_context dev)
 
int mraa_uart_ow_write_byte (mraa_uart_ow_context dev, uint8_t byte)
 
int mraa_uart_ow_bit (mraa_uart_ow_context dev, uint8_t bit)
 
mraa_result_t mraa_uart_ow_reset (mraa_uart_ow_context dev)
 
mraa_result_t mraa_uart_ow_rom_search (mraa_uart_ow_context dev, mraa_boolean_t start, uint8_t *id)
 
mraa_result_t mraa_uart_ow_command (mraa_uart_ow_context dev, uint8_t command, uint8_t *id)
 
uint8_t mraa_uart_ow_crc8 (uint8_t *buffer, uint16_t length)
 

Macros

#define MRAA_UART_OW_ROMCODE_SIZE   8
 

Typedefs

typedef struct _mraa_uart_owmraa_uart_ow_context
 

Enumerations

enum  mraa_uart_ow_rom_cmd_t {
  MRAA_UART_OW_CMD_READ_ROM = 0x33, MRAA_UART_OW_CMD_MATCH_ROM = 0x55, MRAA_UART_OW_CMD_SKIP_ROM = 0xcc, MRAA_UART_OW_CMD_SEARCH_ROM_ALARM = 0xec,
  MRAA_UART_OW_CMD_SEARCH_ROM = 0xf0
}
 

Function Documentation

mraa_uart_ow_context mraa_uart_ow_init ( int  uart)

Initialise uart_ow_context, uses UART board mapping

Parameters
uartthe index of the uart set to use
Returns
uart_ow context or NULL

Here is the caller graph for this function:

mraa_uart_ow_context mraa_uart_ow_init_raw ( const char *  path)

Initialise a raw uart_ow_context. No board setup.

Parameters
pathfor example "/dev/ttyS0"
Returns
uart_ow context or NULL

Here is the caller graph for this function:

const char* mraa_uart_ow_get_dev_path ( mraa_uart_ow_context  dev)

Get char pointer with tty device path within Linux For example. Could point to "/dev/ttyS0"

Parameters
devuart_ow context
Returns
char pointer of device path

Here is the caller graph for this function:

mraa_result_t mraa_uart_ow_stop ( mraa_uart_ow_context  dev)

Destroy a mraa_uart_ow_context

Parameters
devuart_ow context
Returns
mraa_result_t

Here is the caller graph for this function:

int mraa_uart_ow_read_byte ( mraa_uart_ow_context  dev)

Read a byte from the 1-wire bus

Parameters
devuart_ow context
Returns
the byte read or -1 for error

Here is the caller graph for this function:

int mraa_uart_ow_write_byte ( mraa_uart_ow_context  dev,
uint8_t  byte 
)

Write a byte to a 1-wire bus

Parameters
devuart_ow context
bytethe byte to write to the bus
Returns
the byte read back during the time slot or -1 for error

Here is the caller graph for this function:

int mraa_uart_ow_bit ( mraa_uart_ow_context  dev,
uint8_t  bit 
)

Write a bit to a 1-wire bus and read a bit corresponding to the time slot back. This is possible due to the way we wired the TX and RX together with a diode, forming a loopback.

Parameters
devuart_ow context
bitthe bit to write to the bus
Returns
the bit read back during the time slot or -1 for error

Here is the caller graph for this function:

mraa_result_t mraa_uart_ow_reset ( mraa_uart_ow_context  dev)

Send a reset pulse to the 1-wire bus and test for device presence

Parameters
devuart_ow context
Returns
one of the mraa_result_t values

Here is the caller graph for this function:

mraa_result_t mraa_uart_ow_rom_search ( mraa_uart_ow_context  dev,
mraa_boolean_t  start,
uint8_t *  id 
)

Begin a rom code search of the 1-wire bus. This function implements the 1-wire search algorithm. See the uart_ow.c example for an idea on how to use this function to identify all devices present on the bus.

Parameters
devuart_ow context
starttrue to start a new search from scratch, false to continue an existing search
idthe 8-byte rom code id of the current matched device when a device is found
Returns
one of the mraa_result_t values

Here is the caller graph for this function:

mraa_result_t mraa_uart_ow_command ( mraa_uart_ow_context  dev,
uint8_t  command,
uint8_t *  id 
)

Send a command byte to a device on the 1-wire bus

Parameters
devuart_ow context
commandthe command byte to send
idthe rom code id of the device to receive the command, NULL for all devices on the bus
Returns
one of the mraa_result_t values

Here is the caller graph for this function:

uint8_t mraa_uart_ow_crc8 ( uint8_t *  buffer,
uint16_t  length 
)

Perform a Dallas 1-wire compliant CRC8 computation on a buffer

Parameters
bufferthe buffer containing the data
lengththe length of the buffer
Returns
the computed CRC

Here is the caller graph for this function:

Macro Definition Documentation

#define MRAA_UART_OW_ROMCODE_SIZE   8

8 bytes (64 bits) for a device rom code

Typedef Documentation

for now, we simply use the normal MRAA UART context

Enumeration Type Documentation

UART One Wire ROM related Command bytes

Enumerator
MRAA_UART_OW_CMD_READ_ROM 

read rom, when only one device on bus

MRAA_UART_OW_CMD_MATCH_ROM 

match a specific rom code

MRAA_UART_OW_CMD_SKIP_ROM 

skip match/search rom

MRAA_UART_OW_CMD_SEARCH_ROM_ALARM 

search all roms in alarm state

MRAA_UART_OW_CMD_SEARCH_ROM 

search all rom codes

Include dependency graph for uart_ow.h: