Linduino  1.3.0
Linear Technology Arduino-Compatible Demonstration Board
LT_SMBusGroup.cpp
Go to the documentation of this file.
1 /*!
2 LTC SMBus Support: API for a shared SMBus layere
3 
4 @verbatim
5 
6 This API is shared with Linduino and RTOS code. End users should code to this
7 API to enable use of the PMBus code without modifications.
8 
9 @endverbatim
10 
11 
12 Copyright 2018(c) Analog Devices, Inc.
13 
14 All rights reserved.
15 
16 Redistribution and use in source and binary forms, with or without
17 modification, are permitted provided that the following conditions are met:
18  - Redistributions of source code must retain the above copyright
19  notice, this list of conditions and the following disclaimer.
20  - Redistributions in binary form must reproduce the above copyright
21  notice, this list of conditions and the following disclaimer in
22  the documentation and/or other materials provided with the
23  distribution.
24  - Neither the name of Analog Devices, Inc. nor the names of its
25  contributors may be used to endorse or promote products derived
26  from this software without specific prior written permission.
27  - The use of this software may or may not infringe the patent rights
28  of one or more patent holders. This license does not release you
29  from the requirement that you obtain separate licenses from these
30  patent holders to use this software.
31  - Use of the software either in source or binary form, must be run
32  on or directly connected to an Analog Devices Inc. component.
33 
34 THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
35 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
36 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
37 IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
38 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39 LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
40 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
41 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
42 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
43 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 */
45 
46 //! @ingroup PMBus_SMBus
47 //! @{
48 //! @defgroup LT_SMBusGroup LT_SMBusGroup: SMBus Group Protocol
49 //! @}
50 
51 /*! @file
52  @ingroup LT_SMBusGroup
53  Library Header File for LT_SMBusGroup
54 */
55 
56 #include <Arduino.h>
57 #include "LT_SMBusGroup.h"
58 
60 {
61  executor = smbus;
62  queueing = false;
63  head = NULL;
64  tail = NULL;
65 }
66 
68 {
69  executor = smbus;
70  queueing = false;
71  head = NULL;
72  tail = NULL;
73 }
74 
75 void LT_SMBusGroup::writeByte(uint8_t address, uint8_t command, uint8_t data)
76 {
77  if (queueing)
78  addToQueue(new WriteByte(executor, address, command, data));
79  else
80  executor->writeByte(address, command, data);
81 }
82 
83 void LT_SMBusGroup::writeBytes(uint8_t *addresses, uint8_t *commands, uint8_t *data, uint8_t no_addresses)
84 {
85  if (queueing)
86  addToQueue(new WriteBytes(executor, addresses, commands, data, no_addresses));
87  else
88  executor->writeBytes(addresses, commands, data, no_addresses);
89 }
90 
91 uint8_t LT_SMBusGroup::readByte(uint8_t address, uint8_t command)
92 {
93  if (queueing)
94  return executor->readByte(address, command);
95  else
96  return executor->readByte(address, command);
97 }
98 
99 void LT_SMBusGroup::writeWord(uint8_t address, uint8_t command, uint16_t data)
100 {
101  if (queueing)
102  addToQueue(new WriteWord(executor, address, command, data));
103  else
104  executor->writeWord(address, command, data);
105 }
106 
107 uint16_t LT_SMBusGroup::readWord(uint8_t address, uint8_t command)
108 {
109  if (queueing)
110  return executor->readWord(address, command);
111  else
112  return executor->readWord(address, command);
113 }
114 
115 void LT_SMBusGroup::writeBlock(uint8_t address, uint8_t command,
116  uint8_t *block, uint16_t block_size)
117 {
118  if (queueing)
119  addToQueue(new WriteBlock(executor, address, command, block, block_size));
120  else
121  executor->writeBlock(address, command, block, block_size);
122 }
123 
124 uint8_t LT_SMBusGroup::writeReadBlock(uint8_t address, uint8_t command,
125  uint8_t *block_out, uint16_t block_out_size, uint8_t *block_in, uint16_t block_in_size)
126 {
127  if (queueing)
128  return 0;
129  else
130  return executor->writeReadBlock(address, command, block_out, block_out_size, block_in, block_in_size);
131 }
132 
133 uint8_t LT_SMBusGroup::readBlock(uint8_t address, uint8_t command,
134  uint8_t *block, uint16_t block_size)
135 {
136  if (queueing)
137  return 0;
138  else
139  return executor->readBlock(address, command, block, block_size);
140 }
141 
142 void LT_SMBusGroup::sendByte(uint8_t address, uint8_t command)
143 {
144  if (queueing)
145  addToQueue(new SendByte(executor, address, command));
146  else
147  executor->sendByte(address, command);
148 }
149 
150 
152 {
153  queueing = true;
154  head = new Node();
155  tail = head;
156 }
157 
158 bool LT_SMBusGroup::addToQueue(Executable *ex)
159 {
160  if (!ex)
161  return false;
162 
163  Node *newNode = new Node();
164  if (!newNode)
165  {
166  delete ex;
167  return false;
168  }
169  newNode->executable = ex;
170  tail->next = newNode;
171  tail = newNode;
172  return true;
173 }
174 
176 {
177  executor->i2cbus()->startGroupProtocol();
178  tail = head;
179  while ((tail = head->next) != NULL)
180  {
181  delete head;
182  head = tail;
183  if (!head->next)
184  executor->i2cbus()->endGroupProtocol();
185  head->executable->execute();
186  delete head->executable;
187  }
188  delete head;
189  head = 0;
190  tail = 0;
191  queueing = false;
192 }
193 
194 
195 //private class methods
196 
197 LT_SMBusGroup::Executable::Executable(LT_SMBus *e)
198 {
199  executor = e;
200 }
201 
202 LT_SMBusGroup::WriteByte::WriteByte(LT_SMBus *e, uint8_t a, uint8_t c, uint8_t d) : Executable(e)
203 {
204  address = a;
205  command = c;
206  data = d;
207 }
208 void LT_SMBusGroup::WriteByte::execute()
209 {
210  executor->writeByte(address, command, data);
211 }
212 
213 LT_SMBusGroup::WriteBytes::WriteBytes(LT_SMBus *e, uint8_t *a, uint8_t *c, uint8_t *d, uint8_t n) : Executable(e)
214 {
215  addresses = a;
216  commands = c;
217  data = d;
218  no_addresses = n;
219 }
220 void LT_SMBusGroup::WriteBytes::execute()
221 {
222  executor->writeBytes(addresses, commands, data, no_addresses);
223 };
224 
225 LT_SMBusGroup::WriteWord::WriteWord(LT_SMBus *e, uint8_t a, uint8_t c, uint16_t d) : Executable(e)
226 {
227  address = a;
228  command = c;
229  data = d;
230 }
231 void LT_SMBusGroup::WriteWord::execute()
232 {
233  executor->writeWord(address, command, data);
234 };
235 
236 LT_SMBusGroup::WriteBlock::WriteBlock(LT_SMBus *e, uint8_t a, uint8_t c, uint8_t *b, uint16_t bl) : Executable(e)
237 {
238  address = a;
239  command = c;
240  block = b;
241  block_size = bl;
242 }
243 void LT_SMBusGroup::WriteBlock::execute()
244 {
245  executor->writeBlock(address, command, block, block_size);
246 };
247 
248 LT_SMBusGroup::SendByte::SendByte(LT_SMBus *e, uint8_t a, uint8_t c) : Executable(e)
249 {
250  address = a;
251  command = c;
252 };
253 void LT_SMBusGroup::SendByte::execute()
254 {
255  executor->sendByte(address, command);
256 };
uint8_t readByte(uint8_t address, uint8_t command)
SMBus read byte command.
void endGroupProtocol(void)
ends group protocol so I2CBus knows to send STOPs again.
Definition: LT_I2CBus.cpp:244
LTC SMBus Support: Implementation for a shared SMBus layer.
uint8_t writeReadBlock(uint8_t address, uint8_t command, uint8_t *block_out, uint16_t block_out_size, uint8_t *block_in, uint16_t block_in_size)
SMBus write then read block command.
virtual void sendByte(uint8_t address, uint8_t command)=0
SMBus send byte command.
void beginStoring()
Group Protocol Begin.
void execute()
Group Protocol Execute queued commands.
void writeByte(uint8_t address, uint8_t command, uint8_t data)
SMBus write byte command.
void startGroupProtocol(void)
starts group protocol so I2CBus knows to repeat START instead of STOP.
Definition: LT_I2CBus.cpp:240
LT_SMBusGroup(LT_SMBus *)
virtual void writeWord(uint8_t address, uint8_t command, uint16_t data)=0
SMBus write word command.
static uint8_t address
Definition: DC2091A.ino:83
LT_SMBusGroup * smbus
Definition: retention.ino:139
union LT_union_int32_4bytes data
Definition: DC2094A.ino:138
void writeBlock(uint8_t address, uint8_t command, uint8_t *block, uint16_t block_size)
SMBus write block command.
virtual void writeBytes(uint8_t *addresses, uint8_t *commands, uint8_t *data, uint8_t no_addresses)=0
SMBus write byte command for a list of addresses.
void writeWord(uint8_t address, uint8_t command, uint16_t data)
SMBus write word command.
void writeBytes(uint8_t *addresses, uint8_t *commands, uint8_t *data, uint8_t no_addresses)
SMBus write byte command for a list of addresses.
virtual void writeByte(uint8_t address, uint8_t command, uint8_t data)=0
SMBus write byte command.
virtual uint8_t readByte(uint8_t address, uint8_t command)=0
SMBus read byte command.
uint16_t readWord(uint8_t address, uint8_t command)
SMBus read word command.
void sendByte(uint8_t address, uint8_t command)
SMBus send byte command.
virtual LT_I2CBus * i2cbus(void)=0
virtual uint8_t readBlock(uint8_t address, uint8_t command, uint8_t *block, uint16_t block_size)=0
SMBus read block command.
virtual void writeBlock(uint8_t address, uint8_t command, uint8_t *block, uint16_t block_size)=0
SMBus write block command.
virtual uint8_t writeReadBlock(uint8_t address, uint8_t command, uint8_t *block_out, uint16_t block_out_size, uint8_t *block_in, uint16_t block_in_size)=0
SMBus write then read block command.
uint8_t readBlock(uint8_t address, uint8_t command, uint8_t *block, uint16_t block_size)
SMBus read block command.
virtual uint16_t readWord(uint8_t address, uint8_t command)=0
SMBus read word command.