summaryrefslogtreecommitdiff
path: root/peripheral/libupm/src/ds2413/ds2413.cxx
blob: 24f2795793ec9984b1b2b7d51c3e00a06782026c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 * 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 <iostream>
#include <time.h>
#include <stdexcept>

#include "ds2413.hpp"

using namespace upm;
using namespace std;


DS2413::DS2413(int uart) :
  m_uart(uart)
{
  m_devicesFound = 0;

  // check basic access to the 1-wire bus (presence detect)
  mraa::Result rv;

  if ((rv = m_uart.reset()) != mraa::SUCCESS)
    {
      throw std::runtime_error(std::string(__FUNCTION__) +
                               ": reset() failed, no devices on bus?");
      return;
    }
}

DS2413::~DS2413()
{
}

void DS2413::init()
{
  // iterate through the bus and build up a list of detected DS2413
  // devices (only)

  // empty the map, in case this method has already been run once
  // before
  m_devicesFound = 0;
  m_deviceMap.clear();

  // start the search from scratch
  string id = m_uart.search(true);

  if (id.empty())
    {
      throw std::runtime_error(std::string(__FUNCTION__) +
                               ": no devices detected on bus");
      return;
    }

  while (!id.empty())
    {
      // The first byte (id[0]]) is the device type (family) code.  We
      // are only interested in the family code for this device (0x3a).

      if ((uint8_t)id[0] == DS2413_FAMILY_CODE)
        {
          // we have a winner, add it to our map and continue searching

          m_deviceMap[m_devicesFound] = id;
          m_devicesFound++;
        }

      // continue search
      id = m_uart.search(false);
    }

  if (!m_devicesFound)
    {
      throw std::runtime_error(std::string(__FUNCTION__) +
                               ": no DS2413 devices found on bus");
      return;
    }
}

int DS2413::readGpios(int index)
{
  if (index < 0 || index >= m_devicesFound)
    {
      throw std::out_of_range(std::string(__FUNCTION__) +
                              ": device index out of range");
      return 0;
    }

  m_uart.command(ACCESS_READ, m_deviceMap[index]);

  uint8_t value = m_uart.readByte();

  // Validity is performed by taking the high nibble, inverting it, and
  // copmpating it to the low nibble.  If they are equal, then the
  // data is good.

  if ( (value & 0x0f) != ((~value >> 4) & 0x0f) )
    {
      throw std::runtime_error(std::string(__FUNCTION__) +
                              ": returned value failed integrity check");
      return 0;
    }

  m_uart.reset();

  // Only the 4 lsb's are relevant
  return (value & 0x0f);
}

void DS2413::writeGpios(int index, int value)
{
  if (index < 0 || index >= m_devicesFound)
    {
      throw std::out_of_range(std::string(__FUNCTION__) +
                              ": device index out of range");
      return;
    }

  //  mask out everything but the first 2 bits
  uint8_t val = (uint8_t)value & 0x03;

  // the value must have the upper 8 bits written set to 1's
  val |= 0xfc;

  m_uart.command(ACCESS_WRITE, m_deviceMap[index]);

  // first we write the new value, then the inverted value
  m_uart.writeByte(val);
  m_uart.writeByte(~val);

  // now we read back a response indicating success or failure
  uint8_t resp = m_uart.readByte();

  if (resp != ACK_SUCCESS)
    {
      throw std::runtime_error(std::string(__FUNCTION__) +
                               ": failed response validation");
      return;
    }

  m_uart.reset();

  return;

}