mirror of
https://github.com/eclipse/upm.git
synced 2025-03-15 04:57:30 +03:00
ds1307: throw exception(s) on fatal errors
Signed-off-by: Jon Trulson <jtrulson@ics.com> Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
This commit is contained in:
parent
3d837284fd
commit
78d940aaf5
@ -27,6 +27,7 @@
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <stdexcept>
|
||||
|
||||
#include "ds1307.h"
|
||||
|
||||
@ -34,20 +35,15 @@ using namespace upm;
|
||||
using namespace std;
|
||||
|
||||
|
||||
DS1307::DS1307(int bus)
|
||||
DS1307::DS1307(int bus) : m_i2c(bus)
|
||||
{
|
||||
// setup our i2c link
|
||||
m_i2c = mraa_i2c_init(bus);
|
||||
|
||||
mraa_result_t ret = mraa_i2c_address(m_i2c, DS1307_I2C_ADDR);
|
||||
|
||||
if (ret != MRAA_SUCCESS)
|
||||
cerr << "DS1307: Could not initialize i2c bus. " << endl;
|
||||
}
|
||||
|
||||
DS1307::~DS1307()
|
||||
{
|
||||
mraa_i2c_stop(m_i2c);
|
||||
mraa::Result ret = m_i2c.address(DS1307_I2C_ADDR);
|
||||
if (ret != mraa::SUCCESS){
|
||||
throw std::invalid_argument(std::string(__FUNCTION__) +
|
||||
": i2c.address() failed");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
mraa::Result DS1307::writeBytes(uint8_t reg, uint8_t *buffer, int len)
|
||||
@ -65,9 +61,14 @@ mraa::Result DS1307::writeBytes(uint8_t reg, uint8_t *buffer, int len)
|
||||
for (int i=1; i<(len + 1); i++)
|
||||
buf2[i] = buffer[i-1];
|
||||
|
||||
mraa_i2c_address(m_i2c, DS1307_I2C_ADDR);
|
||||
mraa::Result ret = m_i2c.address(DS1307_I2C_ADDR);
|
||||
if (ret != mraa::SUCCESS){
|
||||
throw std::invalid_argument(std::string(__FUNCTION__) +
|
||||
": i2c.address() failed");
|
||||
return ret;
|
||||
}
|
||||
|
||||
return (mraa::Result) mraa_i2c_write(m_i2c, buf2, len + 1);
|
||||
return m_i2c.write(buf2, len + 1);
|
||||
}
|
||||
|
||||
int DS1307::readBytes(uint8_t reg, uint8_t *buffer, int len)
|
||||
@ -75,10 +76,15 @@ int DS1307::readBytes(uint8_t reg, uint8_t *buffer, int len)
|
||||
if (!len || !buffer)
|
||||
return 0;
|
||||
|
||||
mraa_i2c_address(m_i2c, DS1307_I2C_ADDR);
|
||||
mraa_i2c_write_byte(m_i2c, reg);
|
||||
mraa::Result ret = m_i2c.address(DS1307_I2C_ADDR);
|
||||
if (ret != mraa::SUCCESS){
|
||||
throw std::invalid_argument(std::string(__FUNCTION__) +
|
||||
": i2c.address() failed");
|
||||
return 0;
|
||||
}
|
||||
m_i2c.writeByte(reg);
|
||||
|
||||
return mraa_i2c_read(m_i2c, buffer, len);
|
||||
return m_i2c.read(buffer, len);
|
||||
}
|
||||
|
||||
bool DS1307::loadTime()
|
||||
@ -90,8 +96,8 @@ bool DS1307::loadTime()
|
||||
if (bytesRead != 7)
|
||||
{
|
||||
// problem
|
||||
cerr << __FUNCTION__ << ": read " << bytesRead <<
|
||||
" bytes, expected 7." << endl;
|
||||
throw std::runtime_error(std::string(__FUNCTION__) +
|
||||
": failed to read expected 7 bytes from device");
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -77,11 +77,6 @@ namespace upm {
|
||||
*/
|
||||
DS1307(int bus);
|
||||
|
||||
/**
|
||||
* DS1307 destructor
|
||||
*/
|
||||
~DS1307();
|
||||
|
||||
/**
|
||||
* Loads all the time values
|
||||
*
|
||||
@ -100,7 +95,7 @@ namespace upm {
|
||||
/**
|
||||
* Enables an oscillator on the clock.
|
||||
*
|
||||
* @return 0 (MRAA_SUCCESS) if successful; non-zero otherwise
|
||||
* @return 0 (mraa::SUCCESS) if successful; non-zero otherwise
|
||||
*/
|
||||
mraa::Result enableClock();
|
||||
|
||||
@ -108,7 +103,7 @@ namespace upm {
|
||||
* Disables the oscillator on the clock. This prevents the clock
|
||||
* from updating any time/date values
|
||||
*
|
||||
* @return 0 (MRAA_SUCCESS) if successful; non-zero otherwise
|
||||
* @return 0 (mraa::SUCCESS) if successful; non-zero otherwise
|
||||
*/
|
||||
mraa::Result disableClock();
|
||||
|
||||
@ -118,7 +113,7 @@ namespace upm {
|
||||
* @param reg Register location to start writing into
|
||||
* @param buffer Buffer for data storage
|
||||
* @param len Number of bytes to write
|
||||
* @return 0 (MRAA_SUCCESS) if successful; non-zero otherwise
|
||||
* @return 0 (mraa::SUCCESS) if successful; non-zero otherwise
|
||||
*/
|
||||
mraa::Result writeBytes(uint8_t reg, uint8_t *buffer, int len);
|
||||
|
||||
@ -191,7 +186,7 @@ namespace upm {
|
||||
bool pm;
|
||||
|
||||
private:
|
||||
mraa_i2c_context m_i2c;
|
||||
mraa::I2c m_i2c;
|
||||
};
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user