Contiki-NG
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 #include <stdio.h>
42 #include "contiki.h"
43 #include "tlc59116.h"
44 #include "i2cmaster.h"
45 
46 /*---------------------------------------------------------------------------*/
47 /* Write to a register.
48  * args:
49  * reg register to write to
50  * val value to write
51  */
52 
53 void
54 tlc59116_write_reg(uint8_t reg, uint8_t val)
55 {
56  uint8_t tx_buf[] = { reg, val };
57 
58  i2c_transmitinit(TLC59116_ADDR);
59  while(i2c_busy());
60  PRINTFDEBUG("I2C Ready to TX\n");
61 
62  i2c_transmit_n(2, tx_buf);
63  while(i2c_busy());
64  PRINTFDEBUG("WRITE_REG 0x%02X @ reg 0x%02X\n", val, reg);
65 }
66 /*---------------------------------------------------------------------------*/
67 /* Write several registers from a stream.
68  * args:
69  * len number of bytes to write
70  * data pointer to where the data is written from
71  *
72  * First byte in stream must be the register address to begin writing to.
73  * The data is then written from second byte and increasing.
74  */
75 
76 void
77 tlc59116_write_stream(uint8_t len, uint8_t *data)
78 {
79  i2c_transmitinit(TLC59116_ADDR);
80  while(i2c_busy());
81  PRINTFDEBUG("I2C Ready to TX(stream)\n");
82 
83  i2c_transmit_n(len, data); /* start tx and send conf reg */
84  while(i2c_busy());
85  PRINTFDEBUG("WRITE_STR %u B to 0x%02X\n", len, data[0]);
86 }
87 /*---------------------------------------------------------------------------*/
88 /* Read one register.
89  * args:
90  * reg what register to read
91  * returns the value of the read register
92  */
93 
94 uint8_t
95 tlc59116_read_reg(uint8_t reg)
96 {
97  uint8_t retVal = 0;
98  uint8_t rtx = reg;
99 
100  PRINTFDEBUG("READ_REG 0x%02X\n", reg);
101 
102  /* transmit the register to read */
103  i2c_transmitinit(TLC59116_ADDR);
104  while(i2c_busy());
105  i2c_transmit_n(1, &rtx);
106  while(i2c_busy());
107 
108  /* receive the data */
109  i2c_receiveinit(TLC59116_ADDR);
110  while(i2c_busy());
111  i2c_receive_n(1, &retVal);
112  while(i2c_busy());
113 
114  return retVal;
115 }
116 /*---------------------------------------------------------------------------*/
117 /* Read several registers in a stream.
118  * args:
119  * reg what register to start reading from
120  * len number of bytes to read
121  * whereto pointer to where the data is saved
122  */
123 
124 void
125 tlc59116_read_stream(uint8_t reg, uint8_t len, uint8_t *whereto)
126 {
127  uint8_t rtx = reg;
128 
129  PRINTFDEBUG("READ_STR %u B from 0x%02X\n", len, reg);
130 
131  /* transmit the register to start reading from */
132  i2c_transmitinit(TLC59116_ADDR);
133  while(i2c_busy());
134  i2c_transmit_n(1, &rtx);
135  while(i2c_busy());
136 
137  /* receive the data */
138  i2c_receiveinit(TLC59116_ADDR);
139  while(i2c_busy());
140  i2c_receive_n(len, whereto);
141  while(i2c_busy());
142 }
143 /*---------------------------------------------------------------------------*/
144 /* Set pwm value for individual led. Make sure PWM mode is enabled.
145  * args:
146  * led led output -> 0 till 15
147  * pwm led pwm value
148  */
149 
150 void
151 tlc59116_led(uint8_t led, uint8_t pwm)
152 {
153  if((led < 0) || (led > 15)) {
154  PRINTFDEBUG("TLC59116: wrong led value.");
155  } else {
156  tlc59116_write_reg(led + TLC59116_PWM0, pwm);
157  }
158 }
159 /*---------------------------------------------------------------------------*/
160 /* Init the led driver: ports, pins, registers, interrupts (none enabled), I2C,
161  * default threshold values etc.
162  */
163 
164 void
165 tlc59116_init(void)
166 {
167  /* Set up ports and pins for I2C communication */
168  i2c_enable();
169 
170  /* set default register values. */
171  tlc59116_write_reg(TLC59116_MODE1, TLC59116_MODE1_DEFAULT);
172  tlc59116_write_reg(TLC59116_MODE2, TLC59116_MODE2_DEFAULT);
173 
174  /*Set all PWM values to 0x00 (off) */
175  /*This would maybe be better with a SWRST */
176  uint8_t tx_buf[] =
177  { TLC59116_PWM0_AUTOINCR, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
178  tlc59116_write_stream(17, tx_buf);
179 
180  /* set all leds to PWM control */
181  tlc59116_write_reg(TLC59116_LEDOUT0, TLC59116_LEDOUT_PWM);
182  tlc59116_write_reg(TLC59116_LEDOUT1, TLC59116_LEDOUT_PWM);
183  tlc59116_write_reg(TLC59116_LEDOUT2, TLC59116_LEDOUT_PWM);
184  tlc59116_write_reg(TLC59116_LEDOUT3, TLC59116_LEDOUT_PWM);
185 }
I2C communication device driver header file for Zolertia Z1 sensor node.
Device drivers header file for TLC59116 i2c led driver on Zolertia Z1.