| 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
59 LT_SMBusGroup::LT_SMBusGroup(LT_SMBus *smbus) : LT_SMBusBase()
60 {
61 executor = smbus;
62 queueing = false;
63 head = NULL;
64 tail = NULL;
65 }
66
67 LT_SMBusGroup::LT_SMBusGroup(LT_SMBus *smbus, uint32_t speed) : LT_SMBusBase(speed)
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
151 void LT_SMBusGroup::beginStoring()
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
175 void LT_SMBusGroup::execute()
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 };
LT_SMBusGroup::readByte
uint8_t readByte(uint8_t address, uint8_t command)
SMBus read byte command.
Definition: LT_SMBusGroup.cpp:91
LT_I2CBus::endGroupProtocol
void endGroupProtocol(void)
ends group protocol so I2CBus knows to send STOPs again.
Definition: LT_I2CBus.cpp:244
LT_SMBusGroup.h
LTC SMBus Support: Implementation for a shared SMBus layer.
LT_SMBusGroup::writeReadBlock
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.
Definition: LT_SMBusGroup.cpp:124
LT_SMBus::sendByte
virtual void sendByte(uint8_t address, uint8_t command)=0
SMBus send byte command.
LT_SMBusGroup::beginStoring
void beginStoring()
Group Protocol Begin.
Definition: LT_SMBusGroup.cpp:151
LT_SMBusGroup::execute
void execute()
Group Protocol Execute queued commands.
Definition: LT_SMBusGroup.cpp:175
LT_SMBusGroup::writeByte
void writeByte(uint8_t address, uint8_t command, uint8_t data)
SMBus write byte command.
Definition: LT_SMBusGroup.cpp:75
LT_I2CBus::startGroupProtocol
void startGroupProtocol(void)
starts group protocol so I2CBus knows to repeat START instead of STOP.
Definition: LT_I2CBus.cpp:240
LT_SMBusGroup::LT_SMBusGroup
LT_SMBusGroup(LT_SMBus *)
Definition: LT_SMBusGroup.cpp:59
LT_SMBus::writeWord
virtual void writeWord(uint8_t address, uint8_t command, uint16_t data)=0
SMBus write word command.
address
static uint8_t address
Definition: DC2091A.ino:83
smbus
LT_SMBusGroup * smbus
Definition: retention.ino:139
data
union LT_union_int32_4bytes data
Definition: DC2094A.ino:138
LT_SMBusGroup::writeBlock
void writeBlock(uint8_t address, uint8_t command, uint8_t *block, uint16_t block_size)
SMBus write block command.
Definition: LT_SMBusGroup.cpp:115
LT_SMBus::writeBytes
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.
LT_SMBus
Definition: LT_SMBus.h:61
LT_SMBusGroup::writeWord
void writeWord(uint8_t address, uint8_t command, uint16_t data)
SMBus write word command.
Definition: LT_SMBusGroup.cpp:99
LT_SMBusGroup::writeBytes
void writeBytes(uint8_t *addresses, uint8_t *commands, uint8_t *data, uint8_t no_addresses)
SMBus write byte command for a list of addresses.
Definition: LT_SMBusGroup.cpp:83
LT_SMBus::writeByte
virtual void writeByte(uint8_t address, uint8_t command, uint8_t data)=0
SMBus write byte command.
LT_SMBus::readByte
virtual uint8_t readByte(uint8_t address, uint8_t command)=0
SMBus read byte command.
LT_SMBusBase
Definition: LT_SMBusBase.h:60
LT_SMBusGroup::readWord
uint16_t readWord(uint8_t address, uint8_t command)
SMBus read word command.
Definition: LT_SMBusGroup.cpp:107
LT_SMBusGroup::sendByte
void sendByte(uint8_t address, uint8_t command)
SMBus send byte command.
Definition: LT_SMBusGroup.cpp:142
LT_SMBus::i2cbus
virtual LT_I2CBus * i2cbus(void)=0
LT_SMBus::readBlock
virtual uint8_t readBlock(uint8_t address, uint8_t command, uint8_t *block, uint16_t block_size)=0
SMBus read block command.
LT_SMBus::writeBlock
virtual void writeBlock(uint8_t address, uint8_t command, uint8_t *block, uint16_t block_size)=0
SMBus write block command.
LT_SMBus::writeReadBlock
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.
LT_SMBusGroup::readBlock
uint8_t readBlock(uint8_t address, uint8_t command, uint8_t *block, uint16_t block_size)
SMBus read block command.
Definition: LT_SMBusGroup.cpp:133
LT_SMBus::readWord
virtual uint16_t readWord(uint8_t address, uint8_t command)=0
SMBus read word command.