Linduino  1.3.0
Linear Technology Arduino-Compatible Demonstration Board
LTC4155.c
Go to the documentation of this file.
1 /*!
2 LTC4155: Dual-Input Power Manager / 3.5A Li-Ion Battery Charger with I²C Control and USB OTG
3 
4 @verbatim
5 The LTC®4155 is a 15 watt I²C controlled power manager with PowerPath™ instant-
6 on operation, high efficiency switching battery charging and USB compatibility.
7 The LTC4155 seamlessly manages power distribution from two 5V sources, such as a
8 USB port and a wall adapter, to a single-cell rechargeable Lithium-Ion/Polymer
9 battery and a system load.
10 @endverbatim
11 
12 http://www.linear.com/product/LTC4155
13 
14 http://www.linear.com/product/LTC4155#demoboards
15 
16 
17 Copyright 2018(c) Analog Devices, Inc.
18 
19 All rights reserved.
20 
21 Redistribution and use in source and binary forms, with or without
22 modification, are permitted provided that the following conditions are met:
23  - Redistributions of source code must retain the above copyright
24  notice, this list of conditions and the following disclaimer.
25  - Redistributions in binary form must reproduce the above copyright
26  notice, this list of conditions and the following disclaimer in
27  the documentation and/or other materials provided with the
28  distribution.
29  - Neither the name of Analog Devices, Inc. nor the names of its
30  contributors may be used to endorse or promote products derived
31  from this software without specific prior written permission.
32  - The use of this software may or may not infringe the patent rights
33  of one or more patent holders. This license does not release you
34  from the requirement that you obtain separate licenses from these
35  patent holders to use this software.
36  - Use of the software either in source or binary form, must be run
37  on or directly connected to an Analog Devices Inc. component.
38 
39 THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
40 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
41 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
42 IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
43 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
44 LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
45 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
46 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
47 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
48 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
49 
50 Generated on: 2016-01-19
51 */
52 
53 //! @ingroup Switching_Regulators
54 //! @{
55 //! @defgroup LTC4155 LTC4155: Dual-Input Power Manager / 3.5A Li-Ion Battery Charger with I²C Control and USB OTG
56 //! @}
57 
58 /*! @file
59  * @ingroup LTC4155
60  * @brief LTC4155 lightweight, hardware ambiguous, embeddable C Communication
61  * Library.
62  *
63  * Communication is bit-field based as well as whole-register based. This library
64  * automatically masks and right-justifies bit fields to ease programmer workload.
65  *
66  * This C library provides a header file with the complete definitions of registers
67  * and bit fields within those registers, and routines to read and write those
68  * registers and individual bit-fields.
69  *
70  * Outside of the Linduino environment, this library can be built with any
71  * standard C compiler and can be used with different I2C/SMBus communication
72  * hardware simply by swapping out the pointers to appropriate user-supplied
73  * functions *@ref smbus_write_register and *@ref smbus_read_register. An
74  * example is provided using the i2c-tools Linux kernel driver which has been
75  * tested on the BeagleBone Black Linux single board computer. It can also be
76  * readily adapted to common microcontrollers with minimal memory impact on
77  * embedded systems.
78  *
79  * A higher level hardware ambiguous Python communication library is also available.
80  *
81  * Please visit http://www.linear.com/product/LTC4155#code or contact the
82  * factory at 408-432-1900 or www.linear.com for further information.
83  */
84 
85 #include "LTC4155.h"
86 
87 #ifndef LTC4155_USE_MALLOC
90 #endif
91 
92 
93 //private function
95 {
96  //! this function "allocates" a LTC4155_chip structure.
97  //! It may or may not use malloc.
98 #ifdef LTC4155_USE_MALLOC
99  return malloc(sizeof(LTC4155_chip_cfg_t));
100 #else
102  {
103  return &LTC4155_chip_array[LTC4155_instances++];
104  }
105  else
106  {
107  return 0;
108  }
109 #endif
110 }
111 
113 {
115  if (chip == NULL) return NULL;
116  chip->addr = cfg->addr;
117  chip->write_register = cfg->write_register;
118  chip->read_register = cfg->read_register;
120  return (LTC4155) chip;
121 }
122 
123 static inline uint8_t get_size(uint16_t registerinfo)
124 {
125  return ((registerinfo >> 8) & 0x0F) + 1;
126 }
127 static inline uint8_t get_subaddr(uint16_t registerinfo)
128 {
129  return (registerinfo) & 0xFF;
130 }
131 static inline uint8_t get_offset(uint16_t registerinfo)
132 {
133  return (registerinfo >> 12) & 0x0F;
134 }
135 static inline uint8_t get_mask(uint16_t registerinfo)
136 {
137  uint8_t mask = 1 << get_offset(registerinfo);
138  uint8_t size = get_size(registerinfo);
139  uint8_t i;
140  for (i=0; i<size-1; i++)
141  {
142  mask |= mask << 1;
143  }
144  return mask;
145 }
146 
147 int LTC4155_write_register(LTC4155 chip_handle, uint16_t registerinfo, uint8_t data)
148 {
149  LTC4155_chip_cfg_t *chip = (LTC4155_chip_cfg_t *) chip_handle;
150  int failure;
151  uint8_t command_code = get_subaddr(registerinfo);
152  if (get_size(registerinfo) != 8)
153  {
154  uint8_t offset = get_offset(registerinfo);
155  uint16_t mask = get_mask(registerinfo);
156  uint8_t read_data;
157  failure = chip->read_register(chip->addr,command_code,&read_data,chip->port_configuration);
158  if (failure) return failure;
159  data = (read_data & ~mask) | (data << offset);
160  }
161  return chip->write_register(chip->addr,command_code,data,chip->port_configuration);
162 }
163 
164 int LTC4155_read_register(LTC4155 chip_handle, uint16_t registerinfo, uint8_t *data)
165 {
166  LTC4155_chip_cfg_t *chip = (LTC4155_chip_cfg_t *) chip_handle;
167  int result;
168  uint8_t command_code = get_subaddr(registerinfo);
169  uint8_t offset = get_offset(registerinfo);
170  uint16_t mask = get_mask(registerinfo);
171  result = chip->read_register(chip->addr,command_code,data,chip->port_configuration);
172  *data &= mask;
173  *data = *data >> offset;
174  return result;
175 }
static uint8_t get_offset(uint16_t registerinfo)
Definition: LTC4155.c:131
int LTC4155_read_register(LTC4155 chip_handle, uint16_t registerinfo, uint8_t *data)
Retrieves a bit field data into *data.
Definition: LTC4155.c:164
uint8_t read_data()
uint8_t addr
Target IC&#39;s SMBus address.
Definition: LTC4155.h:106
void * LTC4155
Definition: LTC4155.h:102
LTC4155 LTC4155_alloc(void)
Definition: LTC4155.c:94
smbus_read_register read_register
Pointer to a user supplied smbus_read_register function.
Definition: LTC4155.h:107
LTC4155 LTC4155_init(LTC4155_chip_cfg_t *cfg)
Returns a pointer to a LTC4155 structure used by LTC4155_write_register and LTC4155_read_register.
Definition: LTC4155.c:112
#define MAX_NUM_LTC4155_INSTANCES
Multiple LTC4155 use.
Definition: LTC4155.h:135
Information required to access hardware SMBus port.
Definition: LTC4155.h:104
static uint8_t get_mask(uint16_t registerinfo)
Definition: LTC4155.c:135
union LT_union_int32_4bytes data
Definition: DC2094A.ino:138
LTC4155_chip_cfg_t LTC4155_chip_array[MAX_NUM_LTC4155_INSTANCES]
Definition: LTC4155.c:89
int LTC4155_instances
Definition: LTC4155.c:88
LTC4155_chip_cfg_t cfg
Definition: DC1674A.ino:79
port_configuration_t * port_configuration
Pointer to a user supplied port_configuration struct.
Definition: LTC4155.h:109
LTC4155 chip
Definition: DC1674A.ino:88
static uint8_t get_subaddr(uint16_t registerinfo)
Definition: LTC4155.c:127
int LTC4155_write_register(LTC4155 chip_handle, uint16_t registerinfo, uint8_t data)
Function to modify a bit field within a register while preserving the unaddressed bit fields...
Definition: LTC4155.c:147
LTC4155: Dual-Input Power Manager / 3.5A Li-Ion Battery Charger with I²C Control and USB OTG...
smbus_write_register write_register
Pointer to a user supplied smbus_write_register function.
Definition: LTC4155.h:108
static uint8_t get_size(uint16_t registerinfo)
Definition: LTC4155.c:123
static int i
Definition: DC2430A.ino:184