Linduino  1.3.0
Linear Technology Arduino-Compatible Demonstration Board
LTC2874.cpp
Go to the documentation of this file.
1 /*!
2 LTC2874: Quad IO-Link Master Hot Swap Power Controller and PHY
3 
4 @verbatim
5 
6 The LTC2874 provides a rugged, 4-port IO-Link power and communications
7 interface to remote devices connected by cables up to 20m in length.
8 
9 Output supply voltage and inrush current are ramped up in a controlled
10 manner using external N-channel MOSFETs, providing improved robustness
11 compared to fully integrated solutions.
12 
13 Wake-up pulse generation, line noise suppression, connection sensing and
14 automatic restart after fault conditions are supported, along with
15 signaling at rates up to 4.8kb/s, 38.4kb/s, and 230.4kb/s.
16 
17 Configuration and fault reporting are exchanged using a SPI-compatible
18 4-wire interface that operates at clock rates up to 20MHz.
19 
20 @endverbatim
21 
22 http://www.linear.com/product/LTC2874
23 
24 http://www.linear.com/product/LTC2874#demoboards
25 
26 
27 
28 Copyright 2018(c) Analog Devices, Inc.
29 
30 All rights reserved.
31 
32 Redistribution and use in source and binary forms, with or without
33 modification, are permitted provided that the following conditions are met:
34  - Redistributions of source code must retain the above copyright
35  notice, this list of conditions and the following disclaimer.
36  - Redistributions in binary form must reproduce the above copyright
37  notice, this list of conditions and the following disclaimer in
38  the documentation and/or other materials provided with the
39  distribution.
40  - Neither the name of Analog Devices, Inc. nor the names of its
41  contributors may be used to endorse or promote products derived
42  from this software without specific prior written permission.
43  - The use of this software may or may not infringe the patent rights
44  of one or more patent holders. This license does not release you
45  from the requirement that you obtain separate licenses from these
46  patent holders to use this software.
47  - Use of the software either in source or binary form, must be run
48  on or directly connected to an Analog Devices Inc. component.
49 
50 THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
51 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
52 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
53 IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
54 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
55 LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
56 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
57 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
58 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
59 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
60 
61 IO-Link is a registered trademark of PROFIBUS User Organization (PNO).
62 */
63 
64 //! @ingroup Transceivers
65 //! @{
66 //! @defgroup LTC2874 LTC2874: Quad IO-Link Master Hot Swap Power Controller and PHY
67 //! @}
68 
69 /*! @file
70  @ingroup LTC2874
71  Library for LTC2874: Quad IO-Link Master Hot Swap Power Controller and PHY
72 */
73 
74 #include <stdint.h>
75 #include <math.h>
76 #include <Arduino.h>
77 #include "Linduino.h"
78 #include "LT_SPI.h"
79 #include "LTC2874.h"
80 
81 
82 
83 //************************************************************************************
84 // Enables or Disables CQ output for specified port, then Updates.
85 // parameters: port (1-4, or 5=ALL), value (0=Disable, 1=Enable)
86 // returns: nothing
87 //************************************************************************************
88 void LTC2874_cq_output(uint8_t port, uint8_t value)
89 {
90  uint8_t data_byte, command_byte;
91 
92  command_byte = LTC2874_READ | (LTC2874_CTRL1_REGD << 1);
93 
94  output_low(LTC2874_CS); //! 1) Pull CS low
95  spi_write(command_byte); //! 2) Write first byte and send command | address
96  data_byte = spi_read(0x00); //! 3) Read last byte (while sending null data)
98 
99  if (port == 5)
100  {
101  port = 0xF;
102  }
103  else
104  {
105  port = 0x01 << (port - 1); // make mask
106  }
107 
108  if (value)
109  {
110  data_byte |= port; // bitwise OR
111  }
112  else
113  {
114  data_byte &= ~port;
115  }
116 
117  command_byte = LTC2874_WRITE_UPDATE_ALL | (LTC2874_CTRL1_REGD << 1);
118 
119  output_low(LTC2874_CS); //! 1) Pull CS low to start transaction
120  spi_write(command_byte); //! 2) Write the command and address
121  spi_write(data_byte); //! 3) Write the data
122  output_high(LTC2874_CS); //! 4) Pull CS high to finish transaction
123 }
124 
125 
126 //************************************************************************************
127 // Enables or Disables L+ output for specified port, then Updates.
128 // parameters: port (1-4, or 5=ALL), value (0=Disable, 1=Enable)
129 // returns: nothing
130 //************************************************************************************
131 void LTC2874_lplus_output(uint8_t port, uint8_t value)
132 {
133  uint8_t data_byte, command_byte;
134 
135  command_byte = LTC2874_READ | (LTC2874_CTRL2_REGE << 1);
136 
137  output_low(LTC2874_CS); //! 1) Pull CS low
138  spi_write(command_byte); //! 2) Write first byte and send command | address
139  data_byte = spi_read(0x00); //! 3) Read last byte (while sending null data)
141 
142  if (port == 5)
143  {
144  port = 0xF0;
145  }
146  else
147  {
148  port = 0x01 << (port + 3); //make mask
149  }
150 
151  if (value)
152  {
153  data_byte |= port; //bitwise OR
154  }
155  else
156  {
157  data_byte &= ~port;
158  }
159 
160  command_byte = LTC2874_WRITE_UPDATE_ALL | (LTC2874_CTRL2_REGE << 1);
161 
162  output_low(LTC2874_CS); //! 1) Pull CS low to start transaction
163  spi_write(command_byte); //! 2) Write the command and address
164  spi_write(data_byte); //! 3) Write the data
165  output_high(LTC2874_CS); //! 4) Pull CS high to finish transaction
166 }
167 
168 
169 //************************************************************************************
170 // Sets SIO_MODE bit (and also Clears SLEW bit) for specified port, then Updates.
171 // parameter: port (1-4, or 5=ALL)
172 // returns: nothing
173 //************************************************************************************
174 void LTC2874_sio_mode(uint8_t port)
175 {
176  uint8_t data_byte_9, data_byte_E, command_byte_9, command_byte_E, position_9, position_E;
177 
178  if (port == 5) // If 5 set to 0x0F
179  {
180  position_E = 0x0f;
181  }
182  else
183  {
184  position_E = (0x01 << (port - 1)); // convert port number to position
185  }
186 
187  position_9 = position_E << 4; // move position for slew bits
188 
189  command_byte_9 = LTC2874_READ | (LTC2874_MODE2_REG9 << 1);
190  command_byte_E = LTC2874_READ | (LTC2874_CTRL2_REGE << 1);
191 
192  output_low(LTC2874_CS); //! 1) Pull CS low
193  spi_write(command_byte_9); //! 2) Write first byte and send command1 | address
194  data_byte_9 = spi_read(0x00); //! 3) Read last byte (while sending null data)
195  spi_write(command_byte_E); //! 4) Write first byte and send command2 | address
196  data_byte_E = spi_read(0x00); //! 5) Read last byte (while sending null data)
197  output_high(LTC2874_CS); //! 6) Pull CS high to finish transaction
198 
199  data_byte_9 &= ~position_9; // SLEW=0
200  data_byte_E |= position_E; // SIO_MODE=1
201 
202  command_byte_9 = LTC2874_WRITE_NO_UPDATE | (LTC2874_MODE2_REG9 << 1);
203  command_byte_E = LTC2874_WRITE_UPDATE_ALL | (LTC2874_CTRL2_REGE << 1);
204 
205  output_low(LTC2874_CS); //! 1) Pull CS low to start transaction
206  spi_write(command_byte_9); //! 2) Write the command and address
207  spi_write(data_byte_9); //! 3) Write the data
208  spi_write(command_byte_E); //! 4) Write the command and address
209  spi_write(data_byte_E); //! 5) Write the data
210  output_high(LTC2874_CS); //! 6) Pull CS high to finish transaction
211 }
212 
213 //************************************************************************************
214 // Changes value of the 24VMODE bit, then Updates.
215 // parameter: value (0=Disable, 1=Enable)
216 // returns: nothing
217 //************************************************************************************
218 void LTC2874_24v_mode(uint8_t value)
219 {
220  uint8_t command_byte, data_byte;
221  command_byte = LTC2874_READ | (LTC2874_MODE1_REG8 << 1);
222 
223  output_low(LTC2874_CS); //! 1) Pull CS low
224  spi_write(command_byte); //! 2) Write first byte and send command | address
225  data_byte = spi_read(0x00); //! 3) Read last byte (while sending null data)
226  output_high(LTC2874_CS); //! 4) Pull CS high to finish transaction
227 
228  if (value)
229  {
230  data_byte |= LTC2874_24VMODE; // set bit
231  }
232  else
233  {
234  data_byte &= ~LTC2874_24VMODE; // clr bit
235  }
236 
237  command_byte = LTC2874_WRITE_UPDATE_ALL | (LTC2874_MODE1_REG8 << 1);
238 
239  output_low(LTC2874_CS); //! 1) Pull CS low to start transaction
240  spi_write(command_byte); //! 2) Write the command and address
241  spi_write(data_byte); //! 3) Write the data
242  output_high(LTC2874_CS); //! 4) Pull CS high to finish transaction
243 }
244 
245 
246 //************************************************************************************
247 // Changes NSF (Noise Suppression Filter) setting for specified port.
248 // parameters: port#, value
249 // port#: [1,2,3, or 4]
250 // value: 0x0=disabled, 0x1=20.3us, 0x2=2.8us, 0x3=0.6us (default)
251 // Higher bits are ignored. No update. Returns: nothing.
252 //************************************************************************************
253 void LTC2874_write_NSF_value(uint8_t port, uint8_t value)
254 {
255  uint8_t command_byte , data_byte, position;
256  command_byte = LTC2874_READ | (LTC2874_NSF_REGA << 1); // Create command and address byte
257 
258  output_low(LTC2874_CS); //! 1) Pull CS low
259  spi_write(command_byte); //! 2) Write first byte and send command | address
260  data_byte = spi_read(0x00); //! 3) Read last byte (while sending null data)
261  output_high(LTC2874_CS); //! 4) Pull CS high to finish transaction
262 
263  position = ((port - 1) << 1); // convert port number to position,Equivalent to multiply by 2
264  data_byte &= ~(0x3<<position); // clear NSF bits with inverse mask
265  data_byte |= ((value) << position); // bitwise OR value into NSF port position
266 
267  command_byte = LTC2874_WRITE_NO_UPDATE | (LTC2874_NSF_REGA << 1);
268 
269  output_low(LTC2874_CS); //! 1) Pull CS low to start transaction
270  spi_write(command_byte); //! 2) Write the command and address
271  spi_write(data_byte); //! 3) Write the data
272  output_high(LTC2874_CS); //! 4) Pull CS high to finish transaction
273 }
274 
275 
276 //************************************************************************************
277 // Changes ILLM (Sinking current) setting for specified port.
278 // parameters: port#, value
279 // port#: (1-4, or 5=ALL)
280 // value: 0x0=500kohm, 0x1=2.5mA, 0x2=3.7mA, 0x3=6.2mA (default)
281 // Higher bits are ignored. No update. Returns: nothing.
282 //************************************************************************************
283 void LTC2874_write_ILLM_value(uint8_t port, uint8_t value)
284 {
285  uint8_t command_byte , data_byte, position;
286  // Create command and address byte
287  command_byte = LTC2874_READ | (LTC2874_ILLM_REGB << 1);
288 
289  output_low(LTC2874_CS); //! 1) Pull CS low
290  spi_write(command_byte); //! 2) Write first byte and send command | address
291  data_byte = spi_read(0x00); //! 3) Read last byte (while sending null data)
292  output_high(LTC2874_CS); //! 4) Pull CS high to finish transaction
293 
294  if (port == 5) // If 5 load each port same value
295  {
296  data_byte = value + (value << 2) + (value << 4) + (value << 6);
297  }
298  else
299  {
300  position = ((port - 1) << 1); // convert port number to position
301  data_byte &= ~(0x3<<position); // clear ILLM bits with inverse mask
302  data_byte |= ((value) << position); // bitwise OR value into ILLM port position
303  }
304 
305  command_byte = LTC2874_WRITE_NO_UPDATE | (LTC2874_ILLM_REGB << 1);
306 
307  output_low(LTC2874_CS); //! 1) Pull CS low to start transaction
308  spi_write(command_byte); //! 2) Write the command and address
309  spi_write(data_byte); //! 3) Write the data
310  output_high(LTC2874_CS); //! 4) Pull CS high to finish transaction
311 }
312 
313 
314 //************************************************************************************
315 // Change L+ Overcurrent Timer Control (LPTC) setting.
316 // parameter: value (4 bits, 0x0-0xF)
317 // 0x0=17us, 0x1=30us, 0x2=60us, 0x3=120us, 0x4=0.25ms, 0x5=0.5ms, 0x6=1ms, 0x7=2ms
318 // 0x8=3.9ms, 0x9=7.8ms, 0xA=16ms, 0xB=30ms, 0xC=0.60ms, 0xD=0.13s, 0xE/0xF=0.25s
319 // Higher bits are ignored. No update. Returns: nothing.
320 //************************************************************************************
321 void LTC2874_write_LPTC_value(uint8_t value)
322 {
323  uint8_t command_byte , data_byte, position;
324  // Create command and address byte
325  command_byte = LTC2874_READ | (LTC2874_TMRCTRL_REGC << 1);
326 
327  output_low(LTC2874_CS); //! 1) Pull CS low
328  spi_write(command_byte); //! 2) Write first byte and send command | address
329  data_byte = spi_read(0x00); //! 3) Read last byte (while sending null data)
330  output_high(LTC2874_CS); //! 4) Pull CS high to finish transaction
331 
332  data_byte &= ~(LTC2874_LPTC_msk); // clear LPTC bits with inverse mask
333  data_byte |= (LTC2874_LPTC(value)); // bitwise OR value into LPTC bits
334 
335  command_byte = LTC2874_WRITE_NO_UPDATE | (LTC2874_TMRCTRL_REGC << 1);
336 
337  output_low(LTC2874_CS); //! 1) Pull CS low to start transaction
338  spi_write(command_byte); //! 2) Write the command and address
339  spi_write(data_byte); //! 3) Write the data
340  output_high(LTC2874_CS); //! 4) Pull CS high to finish transaction
341 }
342 
343 
344 //************************************************************************************
345 // Changes Auto-Retry Timer Control (RETRYTC) setting.
346 // parameter: value (0x0=0.12s, 0x1=0.24s, 0x2=0.49s, 0x3=0.98s, 0x4=2.0s, 0x5=3.9s, 0x6=7.9s, 0x7=15.7s)
347 // Higher bits are ignored. No update. Returns: nothing.
348 //************************************************************************************
349 void LTC2874_write_RETRYTC_value(uint8_t value)
350 {
351  uint8_t command_byte , data_byte, position;
352  // Create command and address byte
353  command_byte = LTC2874_READ | (LTC2874_TMRCTRL_REGC << 1);
354 
355  output_low(LTC2874_CS); //! 1) Pull CS low
356  spi_write(command_byte); //! 2) Write first byte and send command | address
357  data_byte = spi_read(0x00); //! 3) Read last byte (while sending null data)
358  output_high(LTC2874_CS); //! 4) Pull CS high to finish transaction
359 
360  data_byte &= ~(LTC2874_RETRYTC_msk); // clear RETRYTC bits with inverse mask
361  data_byte |= (LTC2874_RETRYTC(value)); // bitwise OR value into RETRYTC bits
362 
363  command_byte = LTC2874_WRITE_NO_UPDATE | (LTC2874_TMRCTRL_REGC << 1);
364 
365  output_low(LTC2874_CS); //! 1) Pull CS low to start transaction
366  spi_write(command_byte); //! 2) Write the command and address
367  spi_write(data_byte); //! 3) Write the data
368  output_high(LTC2874_CS); //! 4) Pull CS high to finish transaction
369 }
370 
371 
372 //************************************************************************************
373 // Changes 2X Current Pulse Timer Control (2XPTC) setting.
374 // parameter: value (0x0=60ms (default), 0x1=disabled, 0x2=30ms, 0x3=120ms)
375 // Higher bits are ignored. No update. Returns: nothing.
376 //************************************************************************************
377 void LTC2874_write_2XPTC_value(uint8_t value)
378 {
379  uint8_t command_byte , data_byte, position;
380  // Create command and address byte
381  command_byte = LTC2874_READ | (LTC2874_MODE1_REG8 << 1);
382 
383  output_low(LTC2874_CS); //! 1) Pull CS low
384  spi_write(command_byte); //! 2) Write first byte and send command | address
385  data_byte = spi_read(0x00); //! 3) Read last byte (while sending null data)
386  output_high(LTC2874_CS); //! 4) Pull CS high to finish transaction
387 
388  data_byte &= ~(LTC2874_2XPTC_msk); // clear 2XPTC bits with inverse mask
389  data_byte |= (LTC2874_2XPTC(value)); // bitwise OR value into 2XPTC bits
390 
391  command_byte = LTC2874_WRITE_NO_UPDATE | (LTC2874_MODE1_REG8 << 1);
392 
393  output_low(LTC2874_CS); //! 1) Pull CS low to start transaction
394  spi_write(command_byte); //! 2) Write the command and address
395  spi_write(data_byte); //! 3) Write the data
396  output_high(LTC2874_CS); //! 4) Pull CS high to finish transaction
397 }
398 
399 
400 //************************************************************************************
401 // Changes VDD Overvoltage Threshold (OV_TH) setting.
402 // parameter: value (0x0=18V, 0x1=32V (default), 0x2=34V, 0x3=36V)
403 // Higher bits are ignored. No update. Returns: nothing.
404 //************************************************************************************
405 void LTC2874_write_OV_TH_value(uint8_t value)
406 {
407  uint8_t command_byte , data_byte, position;
408  // Create command and address byte
409  command_byte = LTC2874_READ | (LTC2874_MODE2_REG9 << 1);
410 
411  output_low(LTC2874_CS); //! 1) Pull CS low
412  spi_write(command_byte); //! 2) Write first byte and send command | address
413  data_byte = spi_read(0x00); //! 3) Read last byte (while sending null data)
414  output_high(LTC2874_CS); //! 4) Pull CS high to finish transaction
415 
416  data_byte &= ~(LTC2874_OV_TH_msk); // clear OV_TH bits with inverse mask
417  data_byte |= (LTC2874_OV_TH(value)); // bitwise OR value into OV_TH bits
418 
419  command_byte = LTC2874_WRITE_NO_UPDATE | (LTC2874_MODE2_REG9 << 1);
420 
421  output_low(LTC2874_CS); //! 1) Pull CS low to start transaction
422  spi_write(command_byte); //! 2) Write the command and address
423  spi_write(data_byte); //! 3) Write the data
424  output_high(LTC2874_CS); //! 4) Pull CS high to finish transaction
425 }
426 
427 
428 //************************************************************************************
429 // Sends WURQ (Wake-up Request) on specified port.
430 // parameter: port# [1,2,3, or 4; only one at a time]
431 // returns: nothing.
432 //************************************************************************************
433 void LTC2874_wakeup_request(uint8_t port)
434 {
435  uint8_t data_byte, command_byte;
436  port = 0x01 << (port + 3); //move to MS of byte, make mask
437 
438  command_byte = LTC2874_READ | (LTC2874_CTRL1_REGD << 1);
439 
440  output_low(LTC2874_CS); //! 1) Pull CS low
441  spi_write(command_byte); //! 2) Write first byte and send command | address
442  data_byte = spi_read(0x00); //! 3) Read last byte (while sending null data)
444 
445  data_byte |= port; // bitwise OR
446 
447  command_byte = LTC2874_WRITE_UPDATE_ALL | (LTC2874_CTRL1_REGD << 1);
448 
449  output_low(LTC2874_CS); //! 1) Pull CS low to start transaction
450  spi_write(command_byte); //! 2) Write the command and address
451  spi_write(data_byte); //! 3) Write the data
452  output_high(LTC2874_CS); //! 4) Pull CS high to finish transaction
453 }
454 
455 
456 //************************************************************************************
457 // Updates all registers.
458 // parameters: none
459 // returns: nothing
460 //************************************************************************************
462 {
463  uint8_t command_byte;
464  command_byte = LTC2874_UPDATE_ALL;
465 
466  output_low(LTC2874_CS); //! 1) Pull CS low to start transaction
467  spi_write(command_byte); //! 2) Write the command and address
468  spi_write(0x00); //! 3) Write null data
469  output_high(LTC2874_CS); //! 4) Pull CS high to finish transaction
470 }
471 
472 
473 //************************************************************************************
474 // Writes byte of data to a register, then Updates.
475 // parameters: register, data
476 // returns: nothing
477 //************************************************************************************
478 void LTC2874_write_register_update_all(uint8_t LTC2874_register, uint8_t LTC2874_data)
479 {
480  uint8_t command_byte;
481  command_byte = LTC2874_WRITE_UPDATE_ALL | (LTC2874_register << 1);
482 
483  output_low(LTC2874_CS); //! 1) Pull CS low to start transaction
484  spi_write(command_byte); //! 2) Write the command and address
485  spi_write(LTC2874_data); //! 3) Write the data
486  output_high(LTC2874_CS); //! 4) Pull CS high to finish transaction
487 }
488 
489 //************************************************************************************
490 // Changes value of a register bit, then Updates.
491 // NOTE: multi-bit handled by specific functions such as LTC2874_2XPTC_value()
492 // parameters: register, bit, value
493 // port#: [1,2,3, or 4]
494 // value: 0 or 1
495 // returns: nothing
496 //************************************************************************************
497 void LTC2874_write_bit_value_update_all(uint8_t LTC2874_register, uint8_t LTC2874_bit, uint8_t value)
498 {
499  uint8_t command_byte , data_byte;
500  command_byte = LTC2874_READ | (LTC2874_register << 1);
501 
502  output_low(LTC2874_CS); //! 1) Pull CS low
503  spi_write(command_byte); //! 2) Write first byte and send command | address
504  data_byte = spi_read(0x00); //! 3) Read last byte (while sending null data)
505  output_high(LTC2874_CS); //! 4) Pull CS high to finish transaction
506 
507  if (value)
508  {
509  data_byte |= LTC2874_bit; //bitwise or
510  }
511  else
512  {
513  data_byte &= ~LTC2874_bit;
514  }
515 
516  command_byte = LTC2874_WRITE_UPDATE_ALL | (LTC2874_register << 1);
517 
518  output_low(LTC2874_CS); //! 1) Pull CS low to start transaction
519  spi_write(command_byte); //! 2) Write the command and address
520  spi_write(data_byte); //! 3) Write the data
521  output_high(LTC2874_CS); //! 4) Pull CS high to finish transaction
522 
523 }
524 
525 //************************************************************************************
526 // Sets a register bit, then Updates.
527 // parameters: register, bit
528 // returns: nothing
529 //************************************************************************************
530 void LTC2874_write_bit_set_update_all(uint8_t LTC2874_register, uint8_t LTC2874_bit)
531 {
532  uint8_t command_byte , mask, data_byte;
533  command_byte = LTC2874_READ | (LTC2874_register << 1);
534 
535  output_low(LTC2874_CS); //! 1) Pull CS low
536  spi_write(command_byte); //! 2) Write first byte and send command | address
537  data_byte = spi_read(0x00); //! 3) Read last byte (while sending null data)
538  output_high(LTC2874_CS); //! 4) Pull CS high to finish transaction
539 
540  data_byte |= LTC2874_bit; // compound bitwise OR
541 
542  command_byte = LTC2874_WRITE_UPDATE_ALL | (LTC2874_register << 1);
543 
544  output_low(LTC2874_CS); //! 1) Pull CS low to start transaction
545  spi_write(command_byte); //! 2) Write the command and address
546  spi_write(data_byte); //! 3) Write the data
547  output_high(LTC2874_CS); //! 4) Pull CS high to finish transaction
548 }
549 
550 
551 //************************************************************************************
552 // Clears a register bit, then Updates.
553 // parameters: register, bit
554 // returns: nothing
555 //************************************************************************************
556 void LTC2874_write_bit_clr_update_all(uint8_t LTC2874_register, uint8_t LTC2874_bit)
557 {
558  uint8_t command_byte , mask, data_byte;
559  command_byte = LTC2874_READ | (LTC2874_register << 1);
560 
561  output_low(LTC2874_CS); //! 1) Pull CS low
562  spi_write(command_byte); //! 2) Write first byte and send command | address
563  data_byte = spi_read(0x00); //! 3) Read last byte (while sending null data)
564  output_high(LTC2874_CS); //! 4) Pull CS high to finish transaction
565 
566  mask = ~LTC2874_bit; // bitwise NOT
567  data_byte &= mask; // clears the bit, leaves the rest unchanged
568 
569  command_byte = LTC2874_WRITE_UPDATE_ALL | (LTC2874_register << 1);
570 
571  output_low(LTC2874_CS); //! 1) Pull CS low to start transaction
572  spi_write(command_byte); //! 2) Write the command and address
573  spi_write(data_byte); //! 3) Write the data
574  output_high(LTC2874_CS); //! 4) Pull CS high to finish transaction
575 }
576 
577 //************************************************************************************
578 // Resets LTC2874, returning default values to registers.
579 // parameter: none
580 // returns: nothing
581 //************************************************************************************
582 void LTC2874_reset(void)
583 {
584  uint8_t command_byte;
585  command_byte = LTC2874_RESET;
586 
587  output_low(LTC2874_CS); //! 1) Pull CS low to start transaction
588  spi_write(command_byte); //! 2) Write the command and address
589  spi_write(0x00); //! 3) Write null data
590  output_high(LTC2874_CS); //! 4) Pull CS high to finish transaction
591 }
592 
593 //************************************************************************************
594 // Reads data byte from specified register.
595 // parameter: register address
596 // returns: data byte
597 //************************************************************************************
598 uint8_t LTC2874_read_reg(uint8_t LTC2874_register)
599 {
600  uint8_t command_byte, data_byte;
601  //! Build the reg command byte
602  command_byte = LTC2874_READ | (LTC2874_register << 1);
603 
604  output_low(LTC2874_CS); //! 1) Pull CS low
605  spi_write(command_byte); //! 2) Write first byte and send command | address
606  data_byte = spi_read(0x00); //! 3) Read last byte (while sending null data)
607  output_high(LTC2874_CS); //! 4) Pull CS high to finish transaction
608 
609  return data_byte;
610 }
611 
612 
613 //************************************************************************************
614 // Reads a data BIT from specified register.
615 // parameters: register address, bit mask
616 // returns: bit value.
617 //************************************************************************************
618 uint8_t LTC2874_read_bit(uint8_t LTC2874_register, uint8_t LTC2874_bit)
619 {
620  uint8_t command_byte, data_byte;
621 
622  command_byte = LTC2874_READ | (LTC2874_register << 1);
623 
624  output_low(LTC2874_CS); //! 1) Pull CS low
625  spi_write(command_byte); //! 2) Write first byte and send command | address
626  data_byte = spi_read(0x00); //! 3) Read last byte (while sending null data)
627  output_high(LTC2874_CS); //! 4) Pull CS high to finish transaction
628 
629  data_byte &= LTC2874_bit;
630  data_byte = data_byte != 0; //! 5) If the bit is not zero set data_byte to 1
631 
632  return data_byte;
633 }
634 
635 
636 //************************************************************************************
637 // Writes byte of data to a register without Updating.
638 // parameters: register, data
639 // returns: nothing
640 //************************************************************************************
641 void LTC2874_write_register(uint8_t LTC2874_register, uint8_t LTC2874_data)
642 {
643  uint8_t command_byte;
644  command_byte = LTC2874_WRITE_NO_UPDATE | (LTC2874_register << 1);
645 
646  output_low(LTC2874_CS); //! 1) Pull CS low to start transaction
647  spi_write(command_byte); //! 2) Write the command and address
648  spi_write(LTC2874_data); //! 3) Write the data
649  output_high(LTC2874_CS); //! 4) Pull CS high to finish transaction
650 }
651 
652 
653 //************************************************************************************
654 // Sets a register bit without Updating.
655 // parameters: register, bit
656 // returns: nothing
657 //************************************************************************************
658 void LTC2874_write_bit_set(uint8_t LTC2874_register, uint8_t LTC2874_bit)
659 {
660  uint8_t command_byte , mask, data_byte;
661  command_byte = LTC2874_READ | (LTC2874_register << 1);
662 
663  output_low(LTC2874_CS); //! 1) Pull CS low
664  spi_write(command_byte); //! 2) Write first byte and send command | address
665  data_byte = spi_read(0x00); //! 3) Read last byte (while sending null data)
666  output_high(LTC2874_CS); //! 4) Pull CS high to finish transaction
667 
668  data_byte |= LTC2874_bit; // compound bitwise OR
669 
670  command_byte = LTC2874_WRITE_NO_UPDATE | (LTC2874_register << 1);
671 
672  output_low(LTC2874_CS); //! 1) Pull CS low to start transaction
673  spi_write(command_byte); //! 2) Write the command and address
674  spi_write(data_byte); //! 3) Write the data
675  output_high(LTC2874_CS); //! 4) Pull CS high to finish transaction
676 }
677 
678 //************************************************************************************
679 // Clears a register bit without Updating.
680 // parameters: register, bit
681 // returns: nothing.
682 //************************************************************************************
683 void LTC2874_write_bit_clr(uint8_t LTC2874_register, uint8_t LTC2874_bit)
684 {
685  uint8_t command_byte , mask, data_byte;
686  command_byte = LTC2874_READ | (LTC2874_register << 1);
687 
688  spi_write(command_byte); //! 2) Write first byte and send command | address
689  output_high(LTC2874_CS); //! 4) Pull CS high to finish transaction
690 
691  mask = ~LTC2874_bit; // bitwise NOT
692  data_byte &= mask; // clears the bit, leaves the rest unchanged
693 
694  command_byte = LTC2874_WRITE_NO_UPDATE | (LTC2874_register << 1);
695 
696  output_low(LTC2874_CS); //! 1) Pull CS low to start transaction
697  spi_write(command_byte); //! 2) Write the command and address
698  spi_write(data_byte); //! 3) Write the data
699  output_high(LTC2874_CS); //! 4) Pull CS high to finish transaction
700 }
701 
702 
703 //************************************************************************************
704 // Changes value of a register bit without Updating.
705 // NOTE: multi-bit handled by specific functions such as LTC2874_NSF_value()
706 // parameters: register, bit, value
707 // port#: [1,2,3, or 4]
708 // value: 0 or 1
709 // returns: nothing
710 //************************************************************************************
711 void LTC2874_write_bit_value(uint8_t LTC2874_register, uint8_t LTC2874_bit, uint8_t value)
712 {
713  uint8_t command_byte , data_byte;
714  command_byte = LTC2874_READ | (LTC2874_register << 1);
715 
716  output_low(LTC2874_CS); //! 1) Pull CS low
717  spi_write(command_byte); //! 2) Write first byte and send command | address
718  data_byte = spi_read(0x00); //! 3) Read last byte (while sending null data)
719  output_high(LTC2874_CS); //! 4) Pull CS high to finish transaction
720 
721  if (value)
722  {
723  data_byte |= LTC2874_bit;
724  }
725  else
726  {
727  data_byte &= ~LTC2874_bit;
728  }
729 
730  command_byte = LTC2874_WRITE_NO_UPDATE | (LTC2874_register << 1);
731 
732  output_low(LTC2874_CS); //! 1) Pull CS low to start transaction
733  spi_write(command_byte); //! 2) Write the command and address
734  spi_write(data_byte); //! 3) Write the data
735  output_high(LTC2874_CS); //! 4) Pull CS high to finish transaction
736 }
737 
#define LTC2874_NSF_REGA
Definition: LTC2874.h:142
#define LTC2874_RETRYTC(value)
Definition: LTC2874.h:278
#define LTC2874_2XPTC_msk
Definition: LTC2874.h:225
#define LTC2874_LPTC(value)
Definition: LTC2874.h:274
void LTC2874_lplus_output(uint8_t port, uint8_t value)
Enables or Disables L+ output for specified port, then Updates.
Definition: LTC2874.cpp:131
void LTC2874_sio_mode(uint8_t port)
Sets SIO_MODE bit (and also Clears SLEW bit) for specified port, then Updates.
Definition: LTC2874.cpp:174
#define output_high(pin)
Set "pin" high.
Definition: Linduino.h:75
#define LTC2874_CTRL2_REGE
Definition: LTC2874.h:146
Header File for Linduino Libraries and Demo Code.
void spi_write(int8_t data)
Write a data byte using the SPI hardware.
Definition: LT_SPI.cpp:176
LTC2874: Quad IO-Link Master Hot Swap Power Controller and PHY.
void LTC2874_write_RETRYTC_value(uint8_t value)
Changes Auto-Retry Timer Control (RETRYTC) setting without Updating.
Definition: LTC2874.cpp:349
void LTC2874_write_2XPTC_value(uint8_t value)
Changes 2X Current Pulse Timer Control (2XPTC) setting without Updating.
Definition: LTC2874.cpp:377
void LTC2874_write_bit_value_update_all(uint8_t LTC2874_register, uint8_t LTC2874_bit, uint8_t value)
Changes value of a register bit, then Updates.
Definition: LTC2874.cpp:497
void LTC2874_write_register(uint8_t LTC2874_register, uint8_t LTC2874_data)
Writes byte of data to a register without Updating.
Definition: LTC2874.cpp:641
#define LTC2874_TMRCTRL_REGC
Definition: LTC2874.h:144
#define LTC2874_2XPTC(value)
Definition: LTC2874.h:226
void LTC2874_cq_output(uint8_t port, uint8_t value)
Enables or Disables CQ output for specified port, then Updates.
Definition: LTC2874.cpp:88
#define output_low(pin)
Set "pin" low.
Definition: Linduino.h:72
void LTC2874_write_ILLM_value(uint8_t port, uint8_t value)
Changes ILLM (Sinking current) setting for specified port without Updating.
Definition: LTC2874.cpp:283
#define LTC2874_24VMODE
Definition: LTC2874.h:221
#define LTC2874_WRITE_UPDATE_ALL
Definition: LTC2874.h:104
void LTC2874_reset(void)
Resets LTC2874, returning default values to registers.
Definition: LTC2874.cpp:582
uint8_t LTC2874_read_bit(uint8_t LTC2874_register, uint8_t LTC2874_bit)
Reads a data BIT from specified register.
Definition: LTC2874.cpp:618
void LTC2874_write_bit_clr(uint8_t LTC2874_register, uint8_t LTC2874_bit)
Clears a register bit without Updating.
Definition: LTC2874.cpp:683
LT_SPI: Routines to communicate with ATmega328P&#39;s hardware SPI port.
void LTC2874_wakeup_request(uint8_t port)
Sends WURQ (Wake-up Request) on specified port.
Definition: LTC2874.cpp:433
void LTC2874_write_NSF_value(uint8_t port, uint8_t value)
Changes NSF (Noise Suppression Filter) setting for specified port without Updating.
Definition: LTC2874.cpp:253
#define LTC2874_UPDATE_ALL
Definition: LTC2874.h:103
void LTC2874_write_bit_value(uint8_t LTC2874_register, uint8_t LTC2874_bit, uint8_t value)
Changes value of a register bit without Updating.
Definition: LTC2874.cpp:711
int8_t spi_read(int8_t data)
The data byte to be written.
Definition: LT_SPI.cpp:189
#define LTC2874_MODE2_REG9
Definition: LTC2874.h:141
void LTC2874_write_bit_clr_update_all(uint8_t LTC2874_register, uint8_t LTC2874_bit)
Clears a register bit, then Updates.
Definition: LTC2874.cpp:556
#define LTC2874_LPTC_msk
Definition: LTC2874.h:273
#define LTC2874_OV_TH(value)
Definition: LTC2874.h:240
void LTC2874_update_all(void)
Updates all registers.
Definition: LTC2874.cpp:461
void LTC2874_write_register_update_all(uint8_t LTC2874_register, uint8_t LTC2874_data)
Writes byte of data to a register, then Updates.
Definition: LTC2874.cpp:478
void LTC2874_24v_mode(uint8_t value)
Changes value of the 24VMODE bit, then Updates.
Definition: LTC2874.cpp:218
void LTC2874_write_bit_set_update_all(uint8_t LTC2874_register, uint8_t LTC2874_bit)
Sets a register bit, then Updates.
Definition: LTC2874.cpp:530
#define LTC2874_OV_TH_msk
Definition: LTC2874.h:239
void LTC2874_write_LPTC_value(uint8_t value)
Change L+ Overcurrent Timer Control (LPTC) setting without Updating.
Definition: LTC2874.cpp:321
uint8_t LTC2874_read_reg(uint8_t LTC2874_register)
Reads data byte from specified register.
Definition: LTC2874.cpp:598
#define LTC2874_MODE1_REG8
Definition: LTC2874.h:140
#define LTC2874_RESET
Definition: LTC2874.h:105
#define LTC2874_CS
Definition: LTC2874.h:84
#define LTC2874_CTRL1_REGD
Definition: LTC2874.h:145
#define LTC2874_ILLM_REGB
Definition: LTC2874.h:143
#define LTC2874_RETRYTC_msk
Definition: LTC2874.h:277
void LTC2874_write_OV_TH_value(uint8_t value)
Changes VDD Overvoltage Threshold (OV_TH) setting without Updating.
Definition: LTC2874.cpp:405
#define LTC2874_READ
Definition: LTC2874.h:101
#define LTC2874_WRITE_NO_UPDATE
Definition: LTC2874.h:102
void LTC2874_write_bit_set(uint8_t LTC2874_register, uint8_t LTC2874_bit)
Sets a register bit without Updating.
Definition: LTC2874.cpp:658