Linduino  1.3.0
Linear Technology Arduino-Compatible Demonstration Board
LT_PMBusDetect.cpp
Go to the documentation of this file.
1 /*!
2 LTC LT_PMBusDetect
3 
4 @verbatim
5 
6 Support for Detection
7 
8 @endverbatim
9 
10 
11 Copyright 2018(c) Analog Devices, Inc.
12 
13 All rights reserved.
14 
15 Redistribution and use in source and binary forms, with or without
16 modification, are permitted provided that the following conditions are met:
17  - Redistributions of source code must retain the above copyright
18  notice, this list of conditions and the following disclaimer.
19  - Redistributions in binary form must reproduce the above copyright
20  notice, this list of conditions and the following disclaimer in
21  the documentation and/or other materials provided with the
22  distribution.
23  - Neither the name of Analog Devices, Inc. nor the names of its
24  contributors may be used to endorse or promote products derived
25  from this software without specific prior written permission.
26  - The use of this software may or may not infringe the patent rights
27  of one or more patent holders. This license does not release you
28  from the requirement that you obtain separate licenses from these
29  patent holders to use this software.
30  - Use of the software either in source or binary form, must be run
31  on or directly connected to an Analog Devices Inc. component.
32 
33 THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
34 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
35 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
36 IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
37 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
38 LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
39 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
40 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
41 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
42 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43 */
44 
45 /*! @file
46  @ingroup LT_PMBusDevice
47  Library Header File for LT_PMBusDetect
48 */
49 
50 #include <string.h>
51 
52 #include "../LT_PMBUS/LT_PMBusDetect.h"
53 #include "LT_PMBusDeviceLTC3880.h"
54 #include "LT_PMBusDeviceLTC3882.h"
55 #include "LT_PMBusDeviceLTC3883.h"
56 #include "LT_PMBusDeviceLTC3886.h"
57 #include "LT_PMBusDeviceLTC3887.h"
58 #include "LT_PMBusDeviceLTM4675.h"
59 #include "LT_PMBusDeviceLTM4676.h"
60 #include "LT_PMBusDeviceLTM4677.h"
61 #include "LT_PMBusDeviceLTC2974.h"
62 #include "LT_PMBusDeviceLTC2975.h"
63 #include "LT_PMBusDeviceLTC2977.h"
64 #include "LT_PMBusDeviceLTC2978.h"
65 #include "LT_PMBusDeviceLTC2980.h"
66 #include "LT_PMBusDeviceLTM2987.h"
67 
69 {
70 }
71 
73 )
74 {
75  return devices_;
76 }
77 
79 )
80 {
81  return rails_;
82 }
83 
84 
86 {
87  devices_ = NULL;
88  rails_ = NULL;
89  deviceCnt_ = 0;
90  railCnt_ = 0;
91 
92  uint8_t *addresses;
94  unsigned int i, j;
95 
96  if (deviceCnt_ > 0)
97  {
98  while (deviceCnt_ > 0)
99  delete (*(devices_ + (--deviceCnt_)));
100  free(devices_);
101  }
102 
103  if (railCnt_ > 0)
104  {
105  while (railCnt_ > 0)
106  delete (*(rails_ + (--railCnt_)));
107  free(rails_);
108  }
109 
110  addresses = pmbus_->smbus()->probeUnique(0x00);
111 
112  // May be more than required. Can add code to trim based on deviceCnt_
113  // +1 and calloc so there is a terminating NULL
114  devices_ = (LT_PMBusDevice **) calloc(strlen((char *)addresses) + 1, sizeof(LT_PMBusDevice *));
115 
116  // Check each device type one by one. Trading generality and composability
117  // for performance. For better performance, cache IDs or write decoder here
118  // and then call detect on the correct device first time.
119  for (i = 0; i < strlen((char *)addresses); i++)
120  {
121  if ((device = LT_PMBusDeviceLTC3880::detect(pmbus_, addresses[i])) != NULL)
122  devices_[deviceCnt_++] = device;
123  else if ((device = LT_PMBusDeviceLTC3882::detect(pmbus_, addresses[i])) != NULL)
124  devices_[deviceCnt_++] = device;
125  else if ((device = LT_PMBusDeviceLTC3883::detect(pmbus_, addresses[i])) != NULL)
126  devices_[deviceCnt_++] = device;
127  else if ((device = LT_PMBusDeviceLTC3886::detect(pmbus_, addresses[i])) != NULL)
128  devices_[deviceCnt_++] = device;
129  else if ((device = LT_PMBusDeviceLTC3887::detect(pmbus_, addresses[i])) != NULL)
130  devices_[deviceCnt_++] = device;
131  else if ((device = LT_PMBusDeviceLTM4675::detect(pmbus_, addresses[i])) != NULL)
132  devices_[deviceCnt_++] = device;
133  else if ((device = LT_PMBusDeviceLTM4676::detect(pmbus_, addresses[i])) != NULL)
134  devices_[deviceCnt_++] = device;
135  else if ((device = LT_PMBusDeviceLTM4677::detect(pmbus_, addresses[i])) != NULL)
136  devices_[deviceCnt_++] = device;
137  else if ((device = LT_PMBusDeviceLTC2974::detect(pmbus_, addresses[i])) != NULL)
138  devices_[deviceCnt_++] = device;
139  else if ((device = LT_PMBusDeviceLTC2975::detect(pmbus_, addresses[i])) != NULL)
140  devices_[deviceCnt_++] = device;
141  else if ((device = LT_PMBusDeviceLTC2977::detect(pmbus_, addresses[i])) != NULL)
142  devices_[deviceCnt_++] = device;
143  else if ((device = LT_PMBusDeviceLTC2978::detect(pmbus_, addresses[i])) != NULL)
144  devices_[deviceCnt_++] = device;
145  else if ((device = LT_PMBusDeviceLTC2980::detect(pmbus_, addresses[i])) != NULL)
146  devices_[deviceCnt_++] = device;
147  else if ((device = LT_PMBusDeviceLTM2987::detect(pmbus_, addresses[i])) != NULL)
148  devices_[deviceCnt_++] = device;
149 
150  }
151 
152  // Get all the rails, while merging duplicates.
153  for (i = 0; i < deviceCnt_; i++)
154  {
155  LT_PMBusRail **new_rail;
156  void *m;
157  bool merged;
158 
159  new_rail = devices_[i]->getRails();
160 
161  // Loop if device has rail support, and if there are rails to process.
162  while (new_rail != NULL && *new_rail != NULL)
163  {
164  // See if we are already in the list.
165  merged = false;
166  for (j = 0; j < railCnt_; j++)
167  {
168  if (rails_[j]->isMultiphase() && rails_[j]->getAddress() == (*new_rail)->getAddress())
169  {
170  rails_[j]->merge(*new_rail);
171  delete (*new_rail);
172  merged = true;
173  break;
174  }
175  }
176  if (!merged)
177  {
178  if (rails_ == NULL)
179  rails_ = (LT_PMBusRail **) malloc(sizeof(LT_PMBusRail *));
180  else
181  {
182  m = realloc(rails_, (railCnt_ + 1) * sizeof(LT_PMBusRail *));
183  if (m == NULL)
184  free(m);
185  else
186  rails_ = (LT_PMBusRail **) m;
187  }
188  if (rails_ != NULL)
189  rails_[railCnt_++] = *new_rail;
190  }
191 
192  new_rail++;
193  }
194  }
195 
196  rails_ = (LT_PMBusRail **) realloc(rails_, (railCnt_ + 1) * sizeof(LT_PMBusRail *));
197  rails_[railCnt_] = NULL;
198 }
LTC PSM Device.
LT_PMBus * pmbus_
virtual LT_PMBusRail ** getRails()=0
Return a list of rails if any page is part of a rail.
static LT_PMBusDevice * detect(LT_PMBus *pmbus, uint8_t address)
LT_PMBusDetect(LT_PMBus *pmbus)
LT_PMBusRail ** getRails()
LTC PSM Device.
LTC PSM Device.
uint8_t getAddress()
Get ther rail address.
LTC PSM Device.
LTC PSM Device.
static LT_PMBusDevice * detect(LT_PMBus *pmbus, uint8_t address)
static LT_PMBusDevice * detect(LT_PMBus *pmbus, uint8_t address)
void merge(LT_PMBusRail *rail)
Merge a rail into this one. No delete of incomming rail.
static LT_PMBus * pmbus
Definition: DC2875A.ino:82
static LT_PMBusDevice * detect(LT_PMBus *pmbus, uint8_t address)
LTC PSM Device.
LT_PMBusDevice ** getDevices()
LTC PSM Device.
static LT_PMBusDevice * detect(LT_PMBus *pmbus, uint8_t address)
static LT_PMBusDevice * detect(LT_PMBus *pmbus, uint8_t address)
virtual uint8_t * probeUnique(uint8_t command)=0
SMBus bus probe.
LT_PMBusRail ** rails_
LT_SMBus * smbus()
Definition: LT_PMBus.h:401
static LT_PMBusDevice * detect(LT_PMBus *pmbus, uint8_t address)
static LT_PMBusDevice * detect(LT_PMBus *pmbus, uint8_t address)
unsigned int railCnt_
static LT_PMBusDevice * detect(LT_PMBus *pmbus, uint8_t address)
LTC PSM Device.
LTC PSM Device.
void detect()
Detect devices on bus.
static LT_PMBusDevice * detect(LT_PMBus *pmbus, uint8_t address)
static int i
Definition: DC2430A.ino:184
LTC PSM Device.
static LT_PMBusDevice * detect(LT_PMBus *pmbus, uint8_t address)
static LT_PMBusDevice * detect(LT_PMBus *pmbus, uint8_t address)
LT_PMBusDevice ** devices_
LTC PSM Device.
static LT_PMBusDevice * detect(LT_PMBus *pmbus, uint8_t address)
LTC PSM Device.
ad5933_dev * device
Definition: CN-0413.ino:67
PMBusRail communication. For Multiphase Rails.
Definition: LT_PMBusRail.h:72
LTC PSM Device.
static LT_PMBusDevice * detect(LT_PMBus *pmbus, uint8_t address)
unsigned int deviceCnt_
LTC PSM Device.
PMBus communication.
Definition: LT_PMBus.h:370