uart_ow: Better error handling in corner cases
Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
This commit is contained in:
@@ -117,18 +117,18 @@ 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
|
||||
* @return the byte read or -1 for error
|
||||
*/
|
||||
uint8_t mraa_uart_ow_read_byte(mraa_uart_ow_context dev);
|
||||
int 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
|
||||
* @return the byte read back during the time slot or -1 for error
|
||||
*/
|
||||
uint8_t mraa_uart_ow_write_byte(mraa_uart_ow_context dev, uint8_t byte);
|
||||
int 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
|
||||
@@ -137,9 +137,9 @@ uint8_t mraa_uart_ow_write_byte(mraa_uart_ow_context dev, uint8_t byte);
|
||||
*
|
||||
* @param dev uart_ow context
|
||||
* @param bit the bit to write to the bus
|
||||
* @return the bit read back during the time slot
|
||||
* @return the bit read back during the time slot or -1 for error
|
||||
*/
|
||||
uint8_t mraa_uart_ow_bit(mraa_uart_ow_context dev, uint8_t bit);
|
||||
int 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
|
||||
|
||||
@@ -46,6 +46,7 @@ class UartOW
|
||||
* UartOW Constructor, takes a pin number which will map directly to the
|
||||
* linux uart number, this 'enables' the uart, nothing more
|
||||
*
|
||||
* @throws std::invalid_argument in case of error
|
||||
* @param uart the index of the uart to use
|
||||
*/
|
||||
UartOW(int uart)
|
||||
@@ -61,6 +62,7 @@ class UartOW
|
||||
* UartOW Constructor, takes a string to the path of the serial
|
||||
* interface that is needed.
|
||||
*
|
||||
* @throws std::invalid_argument in case of error
|
||||
* @param path the file path for the UART to use
|
||||
*/
|
||||
UartOW(std::string path)
|
||||
@@ -96,24 +98,35 @@ class UartOW
|
||||
/**
|
||||
* Read a byte from the 1-wire bus
|
||||
*
|
||||
* @throws std::invalid_argument in case of error
|
||||
* @return the byte read
|
||||
*/
|
||||
uint8_t
|
||||
readByte()
|
||||
{
|
||||
return mraa_uart_ow_read_byte(m_uart);
|
||||
int res = mraa_uart_ow_read_byte(m_uart);
|
||||
if (res == -1) {
|
||||
throw std::invalid_argument("Unknown UART_OW error");
|
||||
}
|
||||
return (uint8_t) res;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a byte to a 1-wire bus
|
||||
*
|
||||
* @param byte the byte to write to the bus
|
||||
*
|
||||
* @throws std::invalid_argument in case of error
|
||||
* @return the byte read back during the time slot
|
||||
*/
|
||||
uint8_t
|
||||
writeByte(uint8_t byte)
|
||||
{
|
||||
return mraa_uart_ow_write_byte(m_uart, byte);
|
||||
int res = mraa_uart_ow_write_byte(m_uart, byte);
|
||||
if (res == -1) {
|
||||
throw std::invalid_argument("Unknown UART_OW error");
|
||||
}
|
||||
return (uint8_t) res;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -122,13 +135,17 @@ class UartOW
|
||||
* and RX together with a diode, forming a loopback.
|
||||
*
|
||||
* @param bit the bit to write to the bus
|
||||
* @throws std::invalid_argument in case of error
|
||||
* @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);
|
||||
int res = mraa_uart_ow_bit(m_uart, (bit) ? 1 : 0);
|
||||
if (res == -1) {
|
||||
throw std::invalid_argument("Unknown UART_OW error");
|
||||
}
|
||||
return ((res) ? true : false);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user