Contiki-Inga 3.x
tlc59116.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013, Jelmer Tiete.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote
14  * products derived from this software without specific prior
15  * written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
18  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  */
32 
33 /**
34  * \file
35  * Device drivers for tlc59116 i2c led driver on Zolertia Z1.
36  * See http://www.ti.com/product/tlc59116 for datasheet.
37  * \author
38  * Jelmer Tiete, VUB <jelmer@tiete.be>
39  */
40 
41 
42 #include <stdio.h>
43 #include "contiki.h"
44 #include "tlc59116.h"
45 #include "i2cmaster.h"
46 
47 
48 /*---------------------------------------------------------------------------*/
49 /* Write to a register.
50  * args:
51  * reg register to write to
52  * val value to write
53  */
54 
55 void
56 tlc59116_write_reg(uint8_t reg, uint8_t val)
57 {
58  uint8_t tx_buf[] = { reg, val };
59 
60  i2c_transmitinit(TLC59116_ADDR);
61  while(i2c_busy());
62  PRINTFDEBUG("I2C Ready to TX\n");
63 
64  i2c_transmit_n(2, tx_buf);
65  while(i2c_busy());
66  PRINTFDEBUG("WRITE_REG 0x%02X @ reg 0x%02X\n", val, reg);
67 }
68 /*---------------------------------------------------------------------------*/
69 /* Write several registers from a stream.
70  * args:
71  * len number of bytes to write
72  * data pointer to where the data is written from
73  *
74  * First byte in stream must be the register address to begin writing to.
75  * The data is then written from second byte and increasing.
76  */
77 
78 void
79 tlc59116_write_stream(uint8_t len, uint8_t * data)
80 {
81  i2c_transmitinit(TLC59116_ADDR);
82  while(i2c_busy());
83  PRINTFDEBUG("I2C Ready to TX(stream)\n");
84 
85  i2c_transmit_n(len, data); // start tx and send conf reg
86  while(i2c_busy());
87  PRINTFDEBUG("WRITE_STR %u B to 0x%02X\n", len, data[0]);
88 }
89 
90 /*---------------------------------------------------------------------------*/
91 /* Read one register.
92  * args:
93  * reg what register to read
94  * returns the value of the read register
95  */
96 
97 uint8_t
98 tlc59116_read_reg(uint8_t reg)
99 {
100  uint8_t retVal = 0;
101  uint8_t rtx = reg;
102 
103  PRINTFDEBUG("READ_REG 0x%02X\n", reg);
104 
105  /* transmit the register to read */
106  i2c_transmitinit(TLC59116_ADDR);
107  while(i2c_busy());
108  i2c_transmit_n(1, &rtx);
109  while(i2c_busy());
110 
111  /* receive the data */
112  i2c_receiveinit(TLC59116_ADDR);
113  while(i2c_busy());
114  i2c_receive_n(1, &retVal);
115  while(i2c_busy());
116 
117  return retVal;
118 }
119 
120 /*---------------------------------------------------------------------------*/
121 /* Read several registers in a stream.
122  * args:
123  * reg what register to start reading from
124  * len number of bytes to read
125  * whereto pointer to where the data is saved
126  */
127 
128 void
129 tlc59116_read_stream(uint8_t reg, uint8_t len, uint8_t * whereto)
130 {
131  uint8_t rtx = reg;
132 
133  PRINTFDEBUG("READ_STR %u B from 0x%02X\n", len, reg);
134 
135  /* transmit the register to start reading from */
136  i2c_transmitinit(TLC59116_ADDR);
137  while(i2c_busy());
138  i2c_transmit_n(1, &rtx);
139  while(i2c_busy());
140 
141  /* receive the data */
142  i2c_receiveinit(TLC59116_ADDR);
143  while(i2c_busy());
144  i2c_receive_n(len, whereto);
145  while(i2c_busy());
146 }
147 
148 /*---------------------------------------------------------------------------*/
149 /* Set pwm value for individual led. Make sure PWM mode is enabled.
150  * args:
151  * led led output -> 0 till 15
152  * pwm led pwm value
153  */
154 
155 void
156 tlc59116_led(uint8_t led, uint8_t pwm)
157 {
158  if(led < 0 | led > 15) {
159  PRINTFDEBUG("TLC59116: wrong led value.");
160  } else {
161  tlc59116_write_reg(led + TLC59116_PWM0, pwm);
162  }
163 }
164 
165 /*---------------------------------------------------------------------------*/
166 /* Init the led driver: ports, pins, registers, interrupts (none enabled), I2C,
167  * default threshold values etc.
168  */
169 
170 void
171 tlc59116_init(void)
172 {
173  /* Set up ports and pins for I2C communication */
174  i2c_enable();
175 
176  /* set default register values. */
177  tlc59116_write_reg(TLC59116_MODE1, TLC59116_MODE1_DEFAULT);
178  tlc59116_write_reg(TLC59116_MODE2, TLC59116_MODE2_DEFAULT);
179 
180  /*Set all PWM values to 0x00 (off) */
181  /*This would maybe be better with a SWRST */
182  uint8_t tx_buf[] =
183  { TLC59116_PWM0_AUTOINCR, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
184  tlc59116_write_stream(17, &tx_buf);
185 
186  /* set all leds to PWM control */
187  tlc59116_write_reg(TLC59116_LEDOUT0, TLC59116_LEDOUT_PWM);
188  tlc59116_write_reg(TLC59116_LEDOUT1, TLC59116_LEDOUT_PWM);
189  tlc59116_write_reg(TLC59116_LEDOUT2, TLC59116_LEDOUT_PWM);
190  tlc59116_write_reg(TLC59116_LEDOUT3, TLC59116_LEDOUT_PWM);
191 }