mirror of
https://github.com/eclipse/upm.git
synced 2025-03-24 01:10:22 +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 <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
#include "ds1307.h"
|
#include "ds1307.h"
|
||||||
|
|
||||||
@ -34,20 +35,15 @@ using namespace upm;
|
|||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
|
||||||
DS1307::DS1307(int bus)
|
DS1307::DS1307(int bus) : m_i2c(bus)
|
||||||
{
|
{
|
||||||
// setup our i2c link
|
// setup our i2c link
|
||||||
m_i2c = mraa_i2c_init(bus);
|
mraa::Result ret = m_i2c.address(DS1307_I2C_ADDR);
|
||||||
|
if (ret != mraa::SUCCESS){
|
||||||
mraa_result_t ret = mraa_i2c_address(m_i2c, DS1307_I2C_ADDR);
|
throw std::invalid_argument(std::string(__FUNCTION__) +
|
||||||
|
": i2c.address() failed");
|
||||||
if (ret != MRAA_SUCCESS)
|
return;
|
||||||
cerr << "DS1307: Could not initialize i2c bus. " << endl;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
DS1307::~DS1307()
|
|
||||||
{
|
|
||||||
mraa_i2c_stop(m_i2c);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
mraa::Result DS1307::writeBytes(uint8_t reg, uint8_t *buffer, int len)
|
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++)
|
for (int i=1; i<(len + 1); i++)
|
||||||
buf2[i] = buffer[i-1];
|
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)
|
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)
|
if (!len || !buffer)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
mraa_i2c_address(m_i2c, DS1307_I2C_ADDR);
|
mraa::Result ret = m_i2c.address(DS1307_I2C_ADDR);
|
||||||
mraa_i2c_write_byte(m_i2c, reg);
|
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()
|
bool DS1307::loadTime()
|
||||||
@ -90,8 +96,8 @@ bool DS1307::loadTime()
|
|||||||
if (bytesRead != 7)
|
if (bytesRead != 7)
|
||||||
{
|
{
|
||||||
// problem
|
// problem
|
||||||
cerr << __FUNCTION__ << ": read " << bytesRead <<
|
throw std::runtime_error(std::string(__FUNCTION__) +
|
||||||
" bytes, expected 7." << endl;
|
": failed to read expected 7 bytes from device");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -77,11 +77,6 @@ namespace upm {
|
|||||||
*/
|
*/
|
||||||
DS1307(int bus);
|
DS1307(int bus);
|
||||||
|
|
||||||
/**
|
|
||||||
* DS1307 destructor
|
|
||||||
*/
|
|
||||||
~DS1307();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Loads all the time values
|
* Loads all the time values
|
||||||
*
|
*
|
||||||
@ -100,7 +95,7 @@ namespace upm {
|
|||||||
/**
|
/**
|
||||||
* Enables an oscillator on the clock.
|
* 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();
|
mraa::Result enableClock();
|
||||||
|
|
||||||
@ -108,7 +103,7 @@ namespace upm {
|
|||||||
* Disables the oscillator on the clock. This prevents the clock
|
* Disables the oscillator on the clock. This prevents the clock
|
||||||
* from updating any time/date values
|
* 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();
|
mraa::Result disableClock();
|
||||||
|
|
||||||
@ -118,7 +113,7 @@ namespace upm {
|
|||||||
* @param reg Register location to start writing into
|
* @param reg Register location to start writing into
|
||||||
* @param buffer Buffer for data storage
|
* @param buffer Buffer for data storage
|
||||||
* @param len Number of bytes to write
|
* @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);
|
mraa::Result writeBytes(uint8_t reg, uint8_t *buffer, int len);
|
||||||
|
|
||||||
@ -191,7 +186,7 @@ namespace upm {
|
|||||||
bool pm;
|
bool pm;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
mraa_i2c_context m_i2c;
|
mraa::I2c m_i2c;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user