uart_ow: Initial support for Dallas 1-wire over UART support for MRAA
This commit introduces support for Dallas Semiconductor (DS) 1-wire compliant device support using an available UART device. The principle of operation is described in the following Application note by Maxim Electronics: https://www.maximintegrated.com/en/app-notes/index.mvp/id/214 with help (1-wire search) from: https://www.maximintegrated.com/en/app-notes/index.mvp/id/187 It has been tested on Galileo 2 and Edison, with 2 DS 1-wire devices, the DS18B20 and DS2413 connected to the bus. A UPM driver for the DS2413 is already complete and a PR will be submitted after this one. It is important that 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 order for this to work, a simple interface circuit, using a single diode must be constructed: (forgive my "Asciihematic" :) -| 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 1-wire data line. The 1-wire data line requires a pull-up resistor, as the DS 1-wire spec requires. 4.7-5K is typical for DS 1-wire buses. NOTE: DHT-type (temp/humidity sensor) 1-wire devices ARE NOT DS 1-wire compliant, and will not work with this code/circuit unfortunately. Also note, this will use up one of your UARTs, which cannot be used for any other purpose (ie: to access true UART-type serial devices). You can however, connect as many DS 1-wire devices as feasible to this UART, as it will function as a DS 1-wire bus master. Signed-off-by: Jon Trulson <jtrulson@ics.com> Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
This commit is contained in:
committed by
Brendan Le Foll
parent
69d9c26e6e
commit
a379eb7bf6
@@ -33,6 +33,7 @@ extern "C" {
|
||||
#include "mraa/spi.h"
|
||||
#include "mraa/i2c.h"
|
||||
#include "mraa/uart.h"
|
||||
#include "mraa/uart_ow.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
@@ -207,6 +207,9 @@ typedef enum {
|
||||
MRAA_ERROR_INVALID_PLATFORM = 10, /**< Platform not recognised */
|
||||
MRAA_ERROR_PLATFORM_NOT_INITIALISED = 11, /**< Board information not initialised */
|
||||
MRAA_ERROR_PLATFORM_ALREADY_INITIALISED = 0, /**< Board is already initialised, same as MRAA_SUCESS */
|
||||
MRAA_ERROR_UART_OW_SHORTED = 12, /**< UART OW Short Circuit Detected*/
|
||||
MRAA_ERROR_UART_OW_NO_DEVICES = 13, /**< UART OW No devices detected */
|
||||
MRAA_ERROR_UART_OW_DATA_ERROR = 14, /**< UART OW Data/Bus error detected */
|
||||
|
||||
MRAA_ERROR_UNSPECIFIED = 99 /**< Unknown Error */
|
||||
} mraa_result_t;
|
||||
|
||||
@@ -205,6 +205,9 @@ typedef enum {
|
||||
ERROR_INVALID_PLATFORM = 10, /**< Platform not recognised */
|
||||
ERROR_PLATFORM_NOT_INITIALISED = 11, /**< Board information not initialised */
|
||||
ERROR_PLATFORM_ALREADY_INITIALISED = 0, /**< Board is already initialised, same as SUCCESS */
|
||||
ERROR_UART_OW_SHORTED = 12, /**< UART OW Short Circuit Detected*/
|
||||
ERROR_UART_OW_NO_DEVICES = 13, /**< UART OW No devices detected */
|
||||
ERROR_UART_OW_DATA_ERROR = 14, /**< UART OW Data/Bus error detected */
|
||||
|
||||
ERROR_UNSPECIFIED = 99 /**< Unknown Error */
|
||||
} Result;
|
||||
|
||||
189
api/mraa/uart_ow.h
Normal file
189
api/mraa/uart_ow.h
Normal file
@@ -0,0 +1,189 @@
|
||||
/*
|
||||
* Author: Jon Trulson <jtrulson@ics.com>
|
||||
* Copyright (c) 2016 Intel Corporation
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
/**
|
||||
* @file
|
||||
* @brief UART OW module
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* @snippet uart_ow.c Interesting
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "common.h"
|
||||
|
||||
/* for now, we simply use the normal MRAA UART context */
|
||||
typedef struct _uart* mraa_uart_ow_context;
|
||||
|
||||
/* 8 bytes (64 bits) for a device rom code */
|
||||
static const int MRAA_UART_OW_ROMCODE_SIZE = 8;
|
||||
|
||||
/**
|
||||
* UART One Wire ROM related Command bytes
|
||||
*/
|
||||
typedef enum {
|
||||
MRAA_UART_OW_CMD_READ_ROM = 0x33, /**< read rom, when only one device on bus */
|
||||
MRAA_UART_OW_CMD_MATCH_ROM = 0x55, /**< match a specific rom code */
|
||||
MRAA_UART_OW_CMD_SKIP_ROM = 0xcc, /**< skip match/search rom */
|
||||
MRAA_UART_OW_CMD_SEARCH_ROM_ALARM = 0xec, /**< search all roms in alarm state */
|
||||
MRAA_UART_OW_CMD_SEARCH_ROM = 0xf0 /**< search all rom codes */
|
||||
} mraa_uart_ow_rom_cmd_t;
|
||||
|
||||
/**
|
||||
* Initialise uart_ow_context, uses UART board mapping
|
||||
*
|
||||
* @param uart the index of the uart set to use
|
||||
* @return uart_ow context or NULL
|
||||
*/
|
||||
mraa_uart_ow_context mraa_uart_ow_init(int uart);
|
||||
|
||||
/**
|
||||
* Initialise a raw uart_ow_context. No board setup.
|
||||
*
|
||||
* @param path for example "/dev/ttyS0"
|
||||
* @return uart_ow context or NULL
|
||||
*/
|
||||
mraa_uart_ow_context mraa_uart_ow_init_raw(const char* path);
|
||||
|
||||
/**
|
||||
* Get char pointer with tty device path within Linux
|
||||
* For example. Could point to "/dev/ttyS0"
|
||||
*
|
||||
* @param dev uart_ow context
|
||||
* @return char pointer of device path
|
||||
*/
|
||||
const char* mraa_uart_ow_get_dev_path(mraa_uart_ow_context dev);
|
||||
|
||||
/**
|
||||
* Destroy a mraa_uart_ow_context
|
||||
*
|
||||
* @param dev uart_ow context
|
||||
* @return mraa_result_t
|
||||
*/
|
||||
mraa_result_t mraa_uart_ow_stop(mraa_uart_ow_context dev);
|
||||
|
||||
/**
|
||||
* Read a byte from the 1-wire bus
|
||||
*
|
||||
* @param dev uart_ow context
|
||||
* @return the byte read
|
||||
*/
|
||||
uint8_t mraa_uart_ow_read_byte(mraa_uart_ow_context dev);
|
||||
|
||||
/**
|
||||
* Write a byte to a 1-wire bus
|
||||
*
|
||||
* @param dev uart_ow context
|
||||
* @param byte the byte to write to the bus
|
||||
* @return the byte read back during the time slot
|
||||
*/
|
||||
uint8_t mraa_uart_ow_write_byte(mraa_uart_ow_context dev, uint8_t byte);
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @param dev uart_ow context
|
||||
* @param bit the bit to write to the bus
|
||||
* @return the bit read back during the time slot
|
||||
*/
|
||||
uint8_t mraa_uart_ow_bit(mraa_uart_ow_context dev, uint8_t bit);
|
||||
|
||||
/**
|
||||
* Send a reset pulse to the 1-wire bus and test for device presence
|
||||
*
|
||||
* @param dev uart_ow context
|
||||
* @return one of the mraa_result_t values
|
||||
*/
|
||||
mraa_result_t mraa_uart_ow_reset(mraa_uart_ow_context dev);
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @param dev uart_ow context
|
||||
* @param start true to start a new search from scratch, false to
|
||||
* continue an existing search
|
||||
* @param id the 8-byte rom code id of the current matched device when
|
||||
* a device is found
|
||||
* @return one of the mraa_result_t values
|
||||
*/
|
||||
mraa_result_t mraa_uart_ow_rom_search(mraa_uart_ow_context dev, mraa_boolean_t start, uint8_t* id);
|
||||
|
||||
/**
|
||||
* Send a command byte to a device on the 1-wire bus
|
||||
*
|
||||
* @param dev uart_ow context
|
||||
* @param command the command byte to send
|
||||
* @param id the rom code id of the device to receive the command,
|
||||
* NULL for all devices on the bus
|
||||
* @return one of the mraa_result_t values
|
||||
*/
|
||||
mraa_result_t mraa_uart_ow_command(mraa_uart_ow_context dev, uint8_t command, uint8_t* id);
|
||||
|
||||
/**
|
||||
* Perform a Dallas 1-wire compliant CRC8 computation on a buffer
|
||||
*
|
||||
* @param buffer the buffer containing the data
|
||||
* @param length the length of the buffer
|
||||
* @return the computed CRC
|
||||
*/
|
||||
uint8_t mraa_uart_ow_crc8(uint8_t* buffer, uint16_t length);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
260
api/mraa/uart_ow.hpp
Normal file
260
api/mraa/uart_ow.hpp
Normal file
@@ -0,0 +1,260 @@
|
||||
/*
|
||||
* Author: Jon Trulson <jtrulson@ics.com>
|
||||
* Copyright (c) 2016 Intel Corporation
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "uart_ow.h"
|
||||
#include "types.hpp"
|
||||
#include <stdexcept>
|
||||
#include <cstring>
|
||||
|
||||
namespace mraa
|
||||
{
|
||||
|
||||
/**
|
||||
* @brief API for UART One Wire
|
||||
*
|
||||
* This file defines the UartOW (UART to Dallas 1-wire) interface for libmraa
|
||||
*
|
||||
* @snippet UartOW.cpp Interesting
|
||||
*/
|
||||
class UartOW
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* UartOW Constructor, takes a pin number which will map directly to the
|
||||
* linux uart number, this 'enables' the uart, nothing more
|
||||
*
|
||||
* @param uart the index of the uart to use
|
||||
*/
|
||||
UartOW(int uart)
|
||||
{
|
||||
m_uart = mraa_uart_ow_init(uart);
|
||||
|
||||
if (m_uart == NULL) {
|
||||
throw std::invalid_argument("Error initialising UART_OW");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* UartOW Constructor, takes a string to the path of the serial
|
||||
* interface that is needed.
|
||||
*
|
||||
* @param path the file path for the UART to use
|
||||
*/
|
||||
UartOW(std::string path)
|
||||
{
|
||||
m_uart = mraa_uart_ow_init_raw(path.c_str());
|
||||
|
||||
if (m_uart == NULL) {
|
||||
throw std::invalid_argument("Error initialising UART");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Uart destructor
|
||||
*/
|
||||
~UartOW()
|
||||
{
|
||||
mraa_uart_ow_stop(m_uart);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get string with tty device path within Linux
|
||||
* For example. Could point to "/dev/ttyS0"
|
||||
*
|
||||
* @return char pointer of device path
|
||||
*/
|
||||
std::string
|
||||
getDevicePath()
|
||||
{
|
||||
std::string ret_val(mraa_uart_ow_get_dev_path(m_uart));
|
||||
return ret_val;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read a byte from the 1-wire bus
|
||||
*
|
||||
* @return the byte read
|
||||
*/
|
||||
uint8_t
|
||||
readByte()
|
||||
{
|
||||
return mraa_uart_ow_read_byte(m_uart);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a byte to a 1-wire bus
|
||||
*
|
||||
* @param byte the byte to write to the bus
|
||||
* @return the byte read back during the time slot
|
||||
*/
|
||||
uint8_t
|
||||
writeByte(uint8_t byte)
|
||||
{
|
||||
return mraa_uart_ow_write_byte(m_uart, byte);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @param bit the bit to write to the bus
|
||||
* @return the bit read back during the time slot
|
||||
*/
|
||||
bool
|
||||
writeBit(bool bit)
|
||||
{
|
||||
uint8_t rv = mraa_uart_ow_bit(m_uart, (bit) ? 1 : 0);
|
||||
return ((rv) ? true : false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a reset pulse to the 1-wire bus and test for device presence
|
||||
*
|
||||
* @return one of the mraa::Result values
|
||||
*/
|
||||
mraa::Result
|
||||
reset()
|
||||
{
|
||||
return (mraa::Result) mraa_uart_ow_reset(m_uart);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @param start true to start a search from scratch, false to
|
||||
* continue a previously started search
|
||||
* @param id the 8-byte rom code id of the current matched device when a
|
||||
* device is found
|
||||
* @return one of the mraa::Result values
|
||||
*/
|
||||
mraa::Result
|
||||
search(bool start, uint8_t* id)
|
||||
{
|
||||
return (mraa::Result) mraa_uart_ow_rom_search(m_uart, (start) ? 1 : 0, id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Begin a rom code search of the 1-wire bus. This function
|
||||
* implements the 1-wire search algorithm. See the UartOW.cpp
|
||||
* example for an idea on how to use this function to identify all
|
||||
* devices present on the bus.
|
||||
*
|
||||
* @param start true to start a search from scratch, false to
|
||||
* continue a previously started search
|
||||
* @return an empty string if no [more] devices are found, or a
|
||||
* string containing the 8-byte romcode of a detected device.
|
||||
*/
|
||||
std::string
|
||||
search(bool start)
|
||||
{
|
||||
uint8_t id[MRAA_UART_OW_ROMCODE_SIZE];
|
||||
mraa_result_t rv;
|
||||
|
||||
rv = mraa_uart_ow_rom_search(m_uart, (start) ? 1 : 0, id);
|
||||
|
||||
if (rv == MRAA_SUCCESS) {
|
||||
// we found one
|
||||
std::string idStr((char*) id, MRAA_UART_OW_ROMCODE_SIZE);
|
||||
return idStr;
|
||||
} else {
|
||||
// failure, or end of search
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a command byte to a device on the 1-wire bus
|
||||
*
|
||||
* @param command the command byte to send
|
||||
* @param id the rom code id of the device to receive the command,
|
||||
* NULL for all devices on the bus
|
||||
* @return one of the mraa::Result values
|
||||
*/
|
||||
mraa::Result
|
||||
command(uint8_t command, uint8_t* id)
|
||||
{
|
||||
return (mraa::Result) mraa_uart_ow_command(m_uart, command, id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a command byte to a device on the 1-wire bus, supplying
|
||||
* the id as a std::string
|
||||
*
|
||||
* @param command the command byte to send
|
||||
* @param id std::string representing the code id of the device to
|
||||
* receive the command, or an empty string for all devices on the
|
||||
* bus. This string should be 8 bytes in size.
|
||||
* @return one of the mraa::Result values
|
||||
*/
|
||||
mraa::Result
|
||||
command(uint8_t command, std::string id)
|
||||
{
|
||||
if (id.empty() == 0)
|
||||
return (mraa::Result) mraa_uart_ow_command(m_uart, command, NULL);
|
||||
else {
|
||||
if (id.size() != 8) {
|
||||
// Only 8 byte romcodes are legal.
|
||||
throw std::invalid_argument(std::string(__FUNCTION__) +
|
||||
": id must be 8 bytes only");
|
||||
}
|
||||
return (mraa::Result) mraa_uart_ow_command(m_uart, command, (uint8_t*) id.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform a Dallas 1-wire compliant CRC8 computation on a buffer
|
||||
*
|
||||
* @param buffer the buffer containing the data
|
||||
* @param length the length of the buffer
|
||||
* @return the computed CRC
|
||||
*/
|
||||
uint8_t
|
||||
crc8(uint8_t* buffer, uint16_t length)
|
||||
{
|
||||
return mraa_uart_ow_crc8(buffer, length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform a Dallas 1-wire compliant CRC8 computation on a
|
||||
* std::string based buffer
|
||||
*
|
||||
* @param buffer std::string buffer containing the data
|
||||
* @return the computed CRC
|
||||
*/
|
||||
uint8_t
|
||||
crc8(std::string buffer)
|
||||
{
|
||||
return mraa_uart_ow_crc8((uint8_t*) buffer.c_str(), buffer.size());
|
||||
}
|
||||
|
||||
private:
|
||||
mraa_uart_ow_context m_uart;
|
||||
};
|
||||
}
|
||||
@@ -9,6 +9,7 @@ add_executable (spi_mcp4261 spi_mcp4261.c)
|
||||
add_executable (mmap-io2 mmap-io2.c)
|
||||
add_executable (blink_onboard blink_onboard.c)
|
||||
add_executable (uart uart.c)
|
||||
add_executable (uart_ow uart_ow.c)
|
||||
add_executable (mraa-gpio mraa-gpio.c)
|
||||
add_executable (mraa-i2c mraa-i2c.c)
|
||||
add_executable (spi_max7219 spi_max7219.c)
|
||||
@@ -30,6 +31,7 @@ target_link_libraries (spi_mcp4261 mraa)
|
||||
target_link_libraries (mmap-io2 mraa)
|
||||
target_link_libraries (blink_onboard mraa)
|
||||
target_link_libraries (uart mraa)
|
||||
target_link_libraries (uart_ow mraa)
|
||||
target_link_libraries (mraa-gpio mraa)
|
||||
target_link_libraries (mraa-i2c mraa)
|
||||
target_link_libraries (spi_max7219 mraa)
|
||||
|
||||
@@ -8,6 +8,7 @@ add_executable (Spi-pot Spi-pot.cpp)
|
||||
add_executable (Uart Uart-example.cpp)
|
||||
add_executable (Isr-pin6 Isr-pin6.cpp)
|
||||
add_executable (Iio-dummy Iio-dummy.cpp)
|
||||
add_executable (UartOW UartOW.cpp)
|
||||
|
||||
include_directories(${PROJECT_SOURCE_DIR}/api)
|
||||
|
||||
@@ -19,3 +20,4 @@ target_link_libraries (Spi-pot mraa stdc++)
|
||||
target_link_libraries (Uart mraa stdc++)
|
||||
target_link_libraries (Isr-pin6 mraa stdc++)
|
||||
target_link_libraries (Iio-dummy mraa stdc++)
|
||||
target_link_libraries (UartOW mraa stdc++)
|
||||
|
||||
82
examples/c++/UartOW.cpp
Normal file
82
examples/c++/UartOW.cpp
Normal file
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* Author: Jon Trulson <jtrulson@ics.com>
|
||||
* Copyright (c) 2016 Intel Corporation
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "stdio.h"
|
||||
#include "iostream"
|
||||
//! [Interesting]
|
||||
#include "uart_ow.hpp"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int
|
||||
main(int argc, char** argv)
|
||||
{
|
||||
mraa::UartOW* uart = new mraa::UartOW(0);
|
||||
|
||||
// Reset the ow bus and see if anything is present
|
||||
mraa::Result rv;
|
||||
|
||||
if ((rv = uart->reset()) == mraa::SUCCESS) {
|
||||
cout << "Reset succeeded, device(s) detected!" << endl;
|
||||
} else {
|
||||
cout << "Reset failed, returned " << int(rv) << ". No devices on bus?" << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
cout << "Looking for devices..." << endl;
|
||||
;
|
||||
|
||||
uint8_t count = 0;
|
||||
// start the search from scratch
|
||||
string id = uart->search(true);
|
||||
|
||||
if (id.empty()) {
|
||||
cout << "No devices detected." << endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (!id.empty()) {
|
||||
// hack so we don't need to cast each element of the romcode
|
||||
// for printf purposes
|
||||
uint8_t* ptr = (uint8_t*) id.c_str();
|
||||
|
||||
// The first byte (0) is the device type (family) code.
|
||||
// The last byte (7) is the rom code CRC value. The
|
||||
// intervening bytes are the unique 48 bit device ID.
|
||||
|
||||
printf("Device %02d Type 0x%02x ID %02x%02x%02x%02x%02x%02x CRC 0x%02x\n", count, ptr[0],
|
||||
ptr[6], ptr[5], ptr[4], ptr[3], ptr[2], ptr[1], ptr[7]);
|
||||
count++;
|
||||
|
||||
// continue the search with start argument set to false
|
||||
id = uart->search(false);
|
||||
}
|
||||
|
||||
cout << "Exiting..." << endl;
|
||||
|
||||
delete uart;
|
||||
|
||||
return 0;
|
||||
}
|
||||
//! [Interesting]
|
||||
93
examples/uart_ow.c
Normal file
93
examples/uart_ow.c
Normal file
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
* Author: Jon Trulson <jtrulson@ics.com>
|
||||
* Copyright (c) 2016 Intel Corporation
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "stdio.h"
|
||||
//! [Interesting]
|
||||
#include "mraa.h"
|
||||
|
||||
int
|
||||
main(int argc, char** argv)
|
||||
{
|
||||
mraa_uart_ow_context uart;
|
||||
uart = mraa_uart_ow_init(0);
|
||||
|
||||
if (!uart) {
|
||||
printf("mraa_uart_ow_init() failed\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Reset the ow bus and see if anything is present
|
||||
mraa_result_t rv;
|
||||
|
||||
if ((rv = mraa_uart_ow_reset(uart)) == MRAA_SUCCESS) {
|
||||
printf("Reset succeeded, device(s) detected!\n");
|
||||
} else {
|
||||
printf("Reset failed, returned %d. No devices on bus?\n", rv);
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("Looking for devices...\n");
|
||||
uint8_t id[MRAA_UART_OW_ROMCODE_SIZE];
|
||||
|
||||
/* 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.
|
||||
*/
|
||||
|
||||
uint8_t count = 0;
|
||||
|
||||
// start the search from scratch
|
||||
uint8_t result = mraa_uart_ow_rom_search(uart, 1, id);
|
||||
|
||||
if (result == MRAA_ERROR_UART_OW_NO_DEVICES) {
|
||||
printf("No devices detected.\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (result == MRAA_ERROR_UART_OW_DATA_ERROR) {
|
||||
printf("Bus/Data error.\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
while (result == 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.
|
||||
*/
|
||||
printf("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
|
||||
result = mraa_uart_ow_rom_search(uart, 0, id);
|
||||
}
|
||||
|
||||
printf("Exiting...\n");
|
||||
|
||||
mraa_uart_ow_stop(uart);
|
||||
|
||||
return 0;
|
||||
}
|
||||
//! [Interesting]
|
||||
@@ -23,6 +23,7 @@ set (mraa_LIB_SRCS_NOAUTO
|
||||
${PROJECT_SOURCE_DIR}/src/uart/uart.c
|
||||
${PROJECT_SOURCE_DIR}/src/iio/iio.c
|
||||
${mraa_LIB_SRCS_NOAUTO}
|
||||
${PROJECT_SOURCE_DIR}/src/uart_ow/uart_ow.c
|
||||
)
|
||||
|
||||
set (mraa_LIB_X86_SRCS_NOAUTO
|
||||
|
||||
@@ -486,6 +486,15 @@ mraa_result_print(mraa_result_t result)
|
||||
case MRAA_ERROR_PLATFORM_NOT_INITIALISED:
|
||||
fprintf(stdout, "MRAA: Platform not initialised.\n");
|
||||
break;
|
||||
case MRAA_ERROR_UART_OW_SHORTED:
|
||||
fprintf(stdout, "MRAA: UART OW: Bus short detected.\n");
|
||||
break;
|
||||
case MRAA_ERROR_UART_OW_NO_DEVICES:
|
||||
fprintf(stdout, "MRAA: UART OW: No devices detected on bus.\n");
|
||||
break;
|
||||
case MRAA_ERROR_UART_OW_DATA_ERROR:
|
||||
fprintf(stdout, "MRAA: UART OW: Data or Bus error detected.\n");
|
||||
break;
|
||||
case MRAA_ERROR_UNSPECIFIED:
|
||||
fprintf(stdout, "MRAA: Unspecified Error.\n");
|
||||
break;
|
||||
|
||||
498
src/uart_ow/uart_ow.c
Normal file
498
src/uart_ow/uart_ow.c
Normal file
@@ -0,0 +1,498 @@
|
||||
/*
|
||||
* Author: Jon Trulson <jtrulson@ics.com>
|
||||
* Copyright (c) 2016 Intel Corporation
|
||||
*
|
||||
* Portions (search) copyright:
|
||||
* Copyright (C) 2004 Dallas Semiconductor Corporation, All Rights Reserved.
|
||||
*
|
||||
* For the crc8 algorithm:
|
||||
* Copyright (c) 2002 Colin O'Flynn
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <termios.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "uart.h"
|
||||
#include "uart_ow.h"
|
||||
#include "mraa_internal.h"
|
||||
|
||||
// global search state
|
||||
static unsigned char ROM_NO[8]; /* 8 byte (64b) rom code */
|
||||
static int LastDiscrepancy;
|
||||
static int LastFamilyDiscrepancy;
|
||||
static mraa_boolean_t LastDeviceFlag;
|
||||
|
||||
// low-level read byte
|
||||
static unsigned char
|
||||
read_byte(mraa_uart_ow_context dev)
|
||||
{
|
||||
unsigned char ch = 0;
|
||||
|
||||
while (!mraa_uart_read(dev, &ch, 1))
|
||||
;
|
||||
|
||||
return ch;
|
||||
}
|
||||
|
||||
// low-level write byte
|
||||
static void
|
||||
write_byte(mraa_uart_ow_context dev, const unsigned char ch)
|
||||
{
|
||||
mraa_uart_write(dev, &ch, 1);
|
||||
}
|
||||
|
||||
// Here we setup a very simple termios with the minimum required
|
||||
// settings. We use this to also change speed from high to low. We
|
||||
// use the low speed (9600 bd) for emitting the reset pulse, and
|
||||
// high speed (115200 bd) for actual data communications.
|
||||
//
|
||||
static void
|
||||
set_speed(mraa_uart_context dev, mraa_boolean_t speed)
|
||||
{
|
||||
|
||||
if (!dev) {
|
||||
syslog(LOG_ERR, "uart_ow: set_speed: context is NULL");
|
||||
return;
|
||||
}
|
||||
|
||||
static speed_t baud;
|
||||
if (speed)
|
||||
baud = B115200;
|
||||
else
|
||||
baud = B9600;
|
||||
|
||||
struct termios termio = {
|
||||
.c_cflag = baud | CS8 | CLOCAL | CREAD, .c_iflag = 0, .c_oflag = 0, .c_lflag = NOFLSH, .c_cc = { 0 },
|
||||
};
|
||||
|
||||
tcflush(dev->fd, TCIFLUSH);
|
||||
|
||||
// TCSANOW is required
|
||||
if (tcsetattr(dev->fd, TCSANOW, &termio) < 0) {
|
||||
syslog(LOG_ERR, "uart_ow: tcsetattr() failed");
|
||||
return;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// Perform the 1-Wire Search Algorithm on the 1-Wire bus using the existing
|
||||
// search state.
|
||||
// Return 1 : device found, ROM number in ROM_NO buffer
|
||||
// 0 : device not found, end of search
|
||||
//
|
||||
static mraa_boolean_t
|
||||
_ow_search(mraa_uart_ow_context dev)
|
||||
{
|
||||
int id_bit_number;
|
||||
int last_zero, rom_byte_number, search_result;
|
||||
int id_bit, cmp_id_bit;
|
||||
unsigned char rom_byte_mask, search_direction;
|
||||
|
||||
// initialize for search
|
||||
id_bit_number = 1;
|
||||
last_zero = 0;
|
||||
rom_byte_number = 0;
|
||||
rom_byte_mask = 1;
|
||||
search_result = 0;
|
||||
|
||||
// if the last call was not the last device
|
||||
if (!LastDeviceFlag) {
|
||||
// 1-Wire reset
|
||||
if (mraa_uart_ow_reset(dev) != MRAA_SUCCESS) {
|
||||
// reset the search
|
||||
LastDiscrepancy = 0;
|
||||
LastDeviceFlag = 0;
|
||||
LastFamilyDiscrepancy = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// issue the search command
|
||||
mraa_uart_ow_write_byte(dev, MRAA_UART_OW_CMD_SEARCH_ROM);
|
||||
|
||||
// loop to do the search
|
||||
do {
|
||||
// read a bit and its complement
|
||||
id_bit = mraa_uart_ow_bit(dev, 1);
|
||||
cmp_id_bit = mraa_uart_ow_bit(dev, 1);
|
||||
|
||||
// check for no devices on 1-wire
|
||||
if ((id_bit == 1) && (cmp_id_bit == 1))
|
||||
break;
|
||||
else {
|
||||
// all devices coupled have 0 or 1
|
||||
if (id_bit != cmp_id_bit)
|
||||
search_direction = id_bit; // bit write value for search
|
||||
else {
|
||||
// if this discrepancy if before the Last Discrepancy
|
||||
// on a previous next then pick the same as last time
|
||||
if (id_bit_number < LastDiscrepancy)
|
||||
search_direction = ((ROM_NO[rom_byte_number] & rom_byte_mask) > 0);
|
||||
else
|
||||
// if equal to last pick 1, if not then pick 0
|
||||
search_direction = (id_bit_number == LastDiscrepancy);
|
||||
// if 0 was picked then record its position in LastZero
|
||||
if (search_direction == 0) {
|
||||
last_zero = id_bit_number;
|
||||
// check for Last discrepancy in family
|
||||
if (last_zero < 9)
|
||||
LastFamilyDiscrepancy = last_zero;
|
||||
}
|
||||
}
|
||||
|
||||
// set or clear the bit in the ROM byte rom_byte_number
|
||||
// with mask rom_byte_mask
|
||||
if (search_direction == 1)
|
||||
ROM_NO[rom_byte_number] |= rom_byte_mask;
|
||||
else
|
||||
ROM_NO[rom_byte_number] &= ~rom_byte_mask;
|
||||
|
||||
// serial number search direction write bit
|
||||
mraa_uart_ow_bit(dev, search_direction);
|
||||
|
||||
// increment the byte counter id_bit_number
|
||||
// and shift the mask rom_byte_mask
|
||||
id_bit_number++;
|
||||
rom_byte_mask <<= 1;
|
||||
// if the mask is 0 then go to new SerialNum byte
|
||||
// rom_byte_number and reset
|
||||
if (rom_byte_mask == 0) {
|
||||
rom_byte_number++;
|
||||
rom_byte_mask = 1;
|
||||
}
|
||||
}
|
||||
} while (rom_byte_number < 8);
|
||||
|
||||
// loop until through all ROM bytes 0-7
|
||||
// if the search was successful then
|
||||
if (id_bit_number >= 65) {
|
||||
// search successful so set
|
||||
// LastDiscrepancy,LastDeviceFlag,search_result
|
||||
LastDiscrepancy = last_zero;
|
||||
|
||||
// check for last device
|
||||
if (LastDiscrepancy == 0)
|
||||
LastDeviceFlag = 1;
|
||||
}
|
||||
search_result = 1;
|
||||
}
|
||||
|
||||
// if no device found then reset counters so next 'search' will be
|
||||
// like a first
|
||||
if (!search_result || !ROM_NO[0]) {
|
||||
LastDiscrepancy = 0;
|
||||
LastDeviceFlag = 0;
|
||||
LastFamilyDiscrepancy = 0;
|
||||
search_result = 0;
|
||||
}
|
||||
|
||||
return search_result;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
// Find the 'first' devices on the 1-Wire bus
|
||||
// Return 1 : device found, ROM number in ROM_NO buffer
|
||||
// 0 : no device present
|
||||
//
|
||||
static mraa_boolean_t
|
||||
_ow_first(mraa_uart_ow_context dev)
|
||||
{
|
||||
// reset the search state
|
||||
LastDiscrepancy = 0;
|
||||
LastDeviceFlag = 0;
|
||||
LastFamilyDiscrepancy = 0;
|
||||
|
||||
return _ow_search(dev);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
// Find the 'next' devices on the 1-Wire bus
|
||||
// Return 1 : device found, ROM number in ROM_NO buffer
|
||||
// 0 : device not found, end of search
|
||||
//
|
||||
static mraa_boolean_t
|
||||
_ow_next(mraa_uart_ow_context dev)
|
||||
{
|
||||
// leave the search state alone
|
||||
return _ow_search(dev);
|
||||
}
|
||||
|
||||
// Start of exported mraa functionality
|
||||
|
||||
mraa_uart_ow_context
|
||||
mraa_uart_ow_init(int index)
|
||||
{
|
||||
mraa_uart_ow_context dev = mraa_uart_init(index);
|
||||
|
||||
if (!dev)
|
||||
return NULL;
|
||||
|
||||
// now get the fd, and set it up for non-blocking operation
|
||||
if (fcntl(dev->fd, F_SETFL, O_NONBLOCK) == -1) {
|
||||
syslog(LOG_ERR, "uart_ow: failed to set non-blocking on fd");
|
||||
mraa_uart_ow_stop(dev);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return dev;
|
||||
}
|
||||
|
||||
mraa_uart_ow_context
|
||||
mraa_uart_ow_init_raw(const char* path)
|
||||
{
|
||||
mraa_uart_ow_context dev = mraa_uart_init_raw(path);
|
||||
|
||||
if (!dev)
|
||||
return NULL;
|
||||
|
||||
// now get the fd, and set it up for non-blocking operation
|
||||
if (fcntl(dev->fd, F_SETFL, O_NONBLOCK) == -1) {
|
||||
syslog(LOG_ERR, "uart_ow: failed to set non-blocking on fd");
|
||||
mraa_uart_ow_stop(dev);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return dev;
|
||||
}
|
||||
|
||||
mraa_result_t
|
||||
mraa_uart_ow_stop(mraa_uart_ow_context dev)
|
||||
{
|
||||
return mraa_uart_stop(dev);
|
||||
}
|
||||
|
||||
const char*
|
||||
mraa_uart_ow_get_dev_path(mraa_uart_ow_context dev)
|
||||
{
|
||||
return mraa_uart_get_dev_path(dev);
|
||||
}
|
||||
|
||||
uint8_t
|
||||
mraa_uart_ow_bit(mraa_uart_ow_context dev, uint8_t bit)
|
||||
{
|
||||
if (!dev) {
|
||||
syslog(LOG_ERR, "uart_ow: ow_bit: context is NULL");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (bit)
|
||||
write_byte(dev, 0xff); /* write a 1 bit */
|
||||
else
|
||||
write_byte(dev, 0x00); /* write a 0 bit */
|
||||
|
||||
/* return the bit present on the bus (0xff is a '1', anything else
|
||||
* (typically 0xfc or 0x00) is a 0
|
||||
*/
|
||||
return (read_byte(dev) == 0xff);
|
||||
}
|
||||
|
||||
uint8_t
|
||||
mraa_uart_ow_write_byte(mraa_uart_ow_context dev, uint8_t byte)
|
||||
{
|
||||
if (!dev) {
|
||||
syslog(LOG_ERR, "uart_ow: write_byte: context is NULL");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* writing bytes - each bit on the byte to send corresponds to a
|
||||
* byte on the uart. At the same time, we read bits (uart bytes)
|
||||
* from the bus and build a byte to return. This is possible due to
|
||||
* the way we wire the UART TX/RX pins together, similar to a
|
||||
* loopback connection, except the devices on the 1-wire bus have
|
||||
* the ability to modify the returning bitstream.
|
||||
*/
|
||||
|
||||
uint8_t bit;
|
||||
int i;
|
||||
for (i = 0; i < 8; i++) {
|
||||
bit = mraa_uart_ow_bit(dev, byte & 0x01);
|
||||
/* prep for next bit to send, and clear space for bit read */
|
||||
byte >>= 1;
|
||||
/* store read bit in the msb */
|
||||
if (bit)
|
||||
byte |= 0x80;
|
||||
}
|
||||
|
||||
/* return the new byte read */
|
||||
return byte;
|
||||
}
|
||||
|
||||
uint8_t
|
||||
mraa_uart_ow_read_byte(mraa_uart_ow_context dev)
|
||||
{
|
||||
if (!dev) {
|
||||
syslog(LOG_ERR, "uart_ow: read_byte: context is NULL");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* we read by sending 0xff, so the bus is released on the initial
|
||||
* low pulse (uart start bit) for every timeslot, when the device
|
||||
* will then send it's bits
|
||||
*/
|
||||
return mraa_uart_ow_write_byte(dev, 0xff);
|
||||
}
|
||||
|
||||
mraa_result_t
|
||||
mraa_uart_ow_reset(mraa_uart_ow_context dev)
|
||||
{
|
||||
if (!dev) {
|
||||
syslog(LOG_ERR, "uart_ow: reset: context is NULL");
|
||||
return MRAA_ERROR_INVALID_HANDLE;
|
||||
}
|
||||
|
||||
uint8_t rv;
|
||||
|
||||
/* To emit a proper reset pulse, we set low speed (9600 baud) for
|
||||
* the reset pulse and send 0xf0 to pull the line down for the
|
||||
* minimum amount of time.
|
||||
*
|
||||
* From the Maxim whitepaper:
|
||||
*
|
||||
* Transmitting an 0xF0 from the UART forms a proper Reset
|
||||
* pulse. The receive value depends on whether one or more 1-Wire
|
||||
* slave devices are present, their internal timing of each slave
|
||||
* device present, and the UART's detection timing within each bit
|
||||
* window. If no device is present, the receive value will equal the
|
||||
* transmit value. Otherwise the receive value can vary.
|
||||
*/
|
||||
set_speed(dev, 0);
|
||||
/* pull the data line low */
|
||||
write_byte(dev, 0xf0);
|
||||
|
||||
rv = read_byte(dev);
|
||||
|
||||
/* back up to high speed for normal data transmissions */
|
||||
set_speed(dev, 1);
|
||||
|
||||
/* shorted data line */
|
||||
if (rv == 0x00)
|
||||
return MRAA_ERROR_UART_OW_SHORTED;
|
||||
|
||||
/* no devices detected (no presence pulse) */
|
||||
if (rv == 0xf0)
|
||||
return MRAA_ERROR_UART_OW_NO_DEVICES;
|
||||
|
||||
/* otherwise, at least one device is present */
|
||||
return MRAA_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
mraa_result_t
|
||||
mraa_uart_ow_rom_search(mraa_uart_ow_context dev, mraa_boolean_t start, uint8_t* id)
|
||||
{
|
||||
if (!dev) {
|
||||
syslog(LOG_ERR, "uart_ow: rom_search: context is NULL");
|
||||
return MRAA_ERROR_INVALID_HANDLE;
|
||||
}
|
||||
|
||||
// bail if there aren't any devices, or some other error occurs
|
||||
mraa_result_t rv;
|
||||
if ((rv = mraa_uart_ow_reset(dev)) != MRAA_SUCCESS)
|
||||
return rv;
|
||||
|
||||
mraa_boolean_t result;
|
||||
|
||||
// see if we are starting from scratch
|
||||
if (start)
|
||||
result = _ow_first(dev);
|
||||
else
|
||||
result = _ow_next(dev);
|
||||
|
||||
if (result) {
|
||||
// found one. Copy into id and return 1
|
||||
int i;
|
||||
for (i = 0; i < MRAA_UART_OW_ROMCODE_SIZE; i++)
|
||||
id[i] = ROM_NO[i];
|
||||
|
||||
return MRAA_SUCCESS;
|
||||
} else
|
||||
return MRAA_ERROR_UART_OW_NO_DEVICES;
|
||||
}
|
||||
|
||||
mraa_result_t
|
||||
mraa_uart_ow_command(mraa_uart_ow_context dev, uint8_t command, uint8_t* id)
|
||||
{
|
||||
if (!dev) {
|
||||
syslog(LOG_ERR, "uart_ow: ow_command: context is NULL");
|
||||
return MRAA_ERROR_INVALID_HANDLE;
|
||||
}
|
||||
|
||||
/* send reset pulse first */
|
||||
mraa_result_t rv = mraa_uart_ow_reset(dev);
|
||||
|
||||
if (rv != MRAA_SUCCESS)
|
||||
return rv;
|
||||
|
||||
if (id) {
|
||||
/* send the match rom command */
|
||||
mraa_uart_ow_write_byte(dev, MRAA_UART_OW_CMD_MATCH_ROM);
|
||||
|
||||
/* sending to a specific device, so send out the full romcode */
|
||||
int i;
|
||||
for (i = 0; i < MRAA_UART_OW_ROMCODE_SIZE; i++)
|
||||
mraa_uart_ow_write_byte(dev, id[i]);
|
||||
} else {
|
||||
/* send to all devices (or a single device if it's the only one
|
||||
* on the bus)
|
||||
*/
|
||||
mraa_uart_ow_write_byte(dev, MRAA_UART_OW_CMD_SKIP_ROM);
|
||||
}
|
||||
|
||||
mraa_uart_ow_write_byte(dev, command);
|
||||
|
||||
return MRAA_SUCCESS;
|
||||
}
|
||||
|
||||
uint8_t
|
||||
mraa_uart_ow_crc8(uint8_t* buffer, uint16_t length)
|
||||
{
|
||||
// 0x18 = X ^ 8 + X ^ 5 + X ^ 4 + X ^ 0
|
||||
static const uint8_t CRC8POLY = 0x18;
|
||||
|
||||
uint8_t crc = 0x00;
|
||||
uint16_t loop_count;
|
||||
uint8_t bit_counter;
|
||||
uint8_t data;
|
||||
uint8_t feedback_bit;
|
||||
|
||||
for (loop_count = 0; loop_count != length; loop_count++) {
|
||||
data = buffer[loop_count];
|
||||
bit_counter = 8;
|
||||
do {
|
||||
feedback_bit = (crc ^ data) & 0x01;
|
||||
if (feedback_bit == 0x01)
|
||||
crc = crc ^ CRC8POLY;
|
||||
crc = (crc >> 1) & 0x7F;
|
||||
if (feedback_bit == 0x01)
|
||||
crc = crc | 0x80;
|
||||
data = data >> 1;
|
||||
bit_counter--;
|
||||
} while (bit_counter > 0);
|
||||
}
|
||||
|
||||
return crc;
|
||||
}
|
||||
Reference in New Issue
Block a user