2013-05-22 17:46:12 +02:00
|
|
|
/*
|
2013-06-24 16:13:35 +02:00
|
|
|
* Copyright (c) 2013, Jelmer Tiete.
|
2013-05-22 17:46:12 +02:00
|
|
|
* All rights reserved.
|
2014-10-16 17:59:52 +02:00
|
|
|
*
|
2013-05-22 17:46:12 +02:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2013-06-24 16:13:35 +02:00
|
|
|
* 3. The name of the author may not be used to endorse or promote
|
|
|
|
* products derived from this software without specific prior
|
2014-10-16 17:59:52 +02:00
|
|
|
* written permission.
|
|
|
|
*
|
2013-06-24 16:13:35 +02:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
|
|
|
|
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
|
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
|
|
|
|
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2014-10-16 17:59:52 +02:00
|
|
|
*
|
2013-05-22 17:46:12 +02:00
|
|
|
* This file is part of the Contiki operating system.
|
2014-10-16 17:59:52 +02:00
|
|
|
*
|
2013-05-22 17:46:12 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file
|
2013-05-29 00:41:06 +02:00
|
|
|
* Device drivers for tlc59116 i2c led driver on Zolertia Z1.
|
2013-05-24 00:07:57 +02:00
|
|
|
* See http://www.ti.com/product/tlc59116 for datasheet.
|
2013-05-22 17:46:12 +02:00
|
|
|
* \author
|
|
|
|
* Jelmer Tiete, VUB <jelmer@tiete.be>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "contiki.h"
|
|
|
|
#include "tlc59116.h"
|
|
|
|
#include "i2cmaster.h"
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/* Write to a register.
|
2013-05-29 00:41:06 +02:00
|
|
|
* args:
|
|
|
|
* reg register to write to
|
|
|
|
* val value to write
|
|
|
|
*/
|
2013-05-22 17:46:12 +02:00
|
|
|
|
|
|
|
void
|
2013-05-29 00:41:06 +02:00
|
|
|
tlc59116_write_reg(uint8_t reg, uint8_t val)
|
|
|
|
{
|
|
|
|
uint8_t tx_buf[] = { reg, val };
|
2013-05-22 17:46:12 +02:00
|
|
|
|
|
|
|
i2c_transmitinit(TLC59116_ADDR);
|
2013-05-29 00:41:06 +02:00
|
|
|
while(i2c_busy());
|
2013-05-22 17:46:12 +02:00
|
|
|
PRINTFDEBUG("I2C Ready to TX\n");
|
|
|
|
|
|
|
|
i2c_transmit_n(2, tx_buf);
|
2013-05-29 00:41:06 +02:00
|
|
|
while(i2c_busy());
|
2013-05-22 17:46:12 +02:00
|
|
|
PRINTFDEBUG("WRITE_REG 0x%02X @ reg 0x%02X\n", val, reg);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/* Write several registers from a stream.
|
2013-05-29 00:41:06 +02:00
|
|
|
* args:
|
|
|
|
* len number of bytes to write
|
|
|
|
* data pointer to where the data is written from
|
|
|
|
*
|
|
|
|
* First byte in stream must be the register address to begin writing to.
|
2014-10-16 17:59:52 +02:00
|
|
|
* The data is then written from second byte and increasing.
|
2013-05-29 00:41:06 +02:00
|
|
|
*/
|
2013-05-22 17:46:12 +02:00
|
|
|
|
|
|
|
void
|
2014-10-16 17:59:52 +02:00
|
|
|
tlc59116_write_stream(uint8_t len, uint8_t *data)
|
2013-05-29 00:41:06 +02:00
|
|
|
{
|
2013-05-22 17:46:12 +02:00
|
|
|
i2c_transmitinit(TLC59116_ADDR);
|
2013-05-29 00:41:06 +02:00
|
|
|
while(i2c_busy());
|
2013-05-22 17:46:12 +02:00
|
|
|
PRINTFDEBUG("I2C Ready to TX(stream)\n");
|
|
|
|
|
2014-10-16 17:59:52 +02:00
|
|
|
i2c_transmit_n(len, data); /* start tx and send conf reg */
|
2013-05-29 00:41:06 +02:00
|
|
|
while(i2c_busy());
|
2013-05-22 17:46:12 +02:00
|
|
|
PRINTFDEBUG("WRITE_STR %u B to 0x%02X\n", len, data[0]);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/* Read one register.
|
2013-05-29 00:41:06 +02:00
|
|
|
* args:
|
|
|
|
* reg what register to read
|
|
|
|
* returns the value of the read register
|
|
|
|
*/
|
2013-05-22 17:46:12 +02:00
|
|
|
|
|
|
|
uint8_t
|
2013-05-29 00:41:06 +02:00
|
|
|
tlc59116_read_reg(uint8_t reg)
|
|
|
|
{
|
2013-05-22 17:46:12 +02:00
|
|
|
uint8_t retVal = 0;
|
|
|
|
uint8_t rtx = reg;
|
2013-05-29 00:41:06 +02:00
|
|
|
|
2013-05-22 17:46:12 +02:00
|
|
|
PRINTFDEBUG("READ_REG 0x%02X\n", reg);
|
|
|
|
|
|
|
|
/* transmit the register to read */
|
|
|
|
i2c_transmitinit(TLC59116_ADDR);
|
2013-05-29 00:41:06 +02:00
|
|
|
while(i2c_busy());
|
2013-05-22 17:46:12 +02:00
|
|
|
i2c_transmit_n(1, &rtx);
|
2013-05-29 00:41:06 +02:00
|
|
|
while(i2c_busy());
|
2013-05-22 17:46:12 +02:00
|
|
|
|
|
|
|
/* receive the data */
|
|
|
|
i2c_receiveinit(TLC59116_ADDR);
|
2013-05-29 00:41:06 +02:00
|
|
|
while(i2c_busy());
|
2013-05-22 17:46:12 +02:00
|
|
|
i2c_receive_n(1, &retVal);
|
2013-05-29 00:41:06 +02:00
|
|
|
while(i2c_busy());
|
2013-05-22 17:46:12 +02:00
|
|
|
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/* Read several registers in a stream.
|
2013-05-29 00:41:06 +02:00
|
|
|
* args:
|
|
|
|
* reg what register to start reading from
|
|
|
|
* len number of bytes to read
|
|
|
|
* whereto pointer to where the data is saved
|
|
|
|
*/
|
2013-05-22 17:46:12 +02:00
|
|
|
|
|
|
|
void
|
2014-10-16 17:59:52 +02:00
|
|
|
tlc59116_read_stream(uint8_t reg, uint8_t len, uint8_t *whereto)
|
2013-05-29 00:41:06 +02:00
|
|
|
{
|
2013-05-22 17:46:12 +02:00
|
|
|
uint8_t rtx = reg;
|
2013-05-29 00:41:06 +02:00
|
|
|
|
2013-05-22 17:46:12 +02:00
|
|
|
PRINTFDEBUG("READ_STR %u B from 0x%02X\n", len, reg);
|
|
|
|
|
|
|
|
/* transmit the register to start reading from */
|
|
|
|
i2c_transmitinit(TLC59116_ADDR);
|
2013-05-29 00:41:06 +02:00
|
|
|
while(i2c_busy());
|
2013-05-22 17:46:12 +02:00
|
|
|
i2c_transmit_n(1, &rtx);
|
2013-05-29 00:41:06 +02:00
|
|
|
while(i2c_busy());
|
2013-05-22 17:46:12 +02:00
|
|
|
|
|
|
|
/* receive the data */
|
|
|
|
i2c_receiveinit(TLC59116_ADDR);
|
2013-05-29 00:41:06 +02:00
|
|
|
while(i2c_busy());
|
2013-05-22 17:46:12 +02:00
|
|
|
i2c_receive_n(len, whereto);
|
2013-05-29 00:41:06 +02:00
|
|
|
while(i2c_busy());
|
2013-05-22 17:46:12 +02:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2013-05-23 23:35:21 +02:00
|
|
|
/* Set pwm value for individual led. Make sure PWM mode is enabled.
|
2013-05-29 00:41:06 +02:00
|
|
|
* args:
|
|
|
|
* led led output -> 0 till 15
|
|
|
|
* pwm led pwm value
|
|
|
|
*/
|
2013-05-22 17:46:12 +02:00
|
|
|
|
|
|
|
void
|
2013-05-29 00:41:06 +02:00
|
|
|
tlc59116_led(uint8_t led, uint8_t pwm)
|
|
|
|
{
|
2014-10-16 17:59:52 +02:00
|
|
|
if((led < 0) || (led > 15)) {
|
2013-05-22 17:46:12 +02:00
|
|
|
PRINTFDEBUG("TLC59116: wrong led value.");
|
2013-05-29 00:41:06 +02:00
|
|
|
} else {
|
|
|
|
tlc59116_write_reg(led + TLC59116_PWM0, pwm);
|
2013-05-23 23:35:21 +02:00
|
|
|
}
|
2013-05-22 17:46:12 +02:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/* Init the led driver: ports, pins, registers, interrupts (none enabled), I2C,
|
2013-05-29 00:41:06 +02:00
|
|
|
* default threshold values etc.
|
|
|
|
*/
|
2013-05-22 17:46:12 +02:00
|
|
|
|
|
|
|
void
|
2013-05-29 00:41:06 +02:00
|
|
|
tlc59116_init(void)
|
|
|
|
{
|
2013-05-22 17:46:12 +02:00
|
|
|
/* Set up ports and pins for I2C communication */
|
|
|
|
i2c_enable();
|
|
|
|
|
|
|
|
/* set default register values. */
|
|
|
|
tlc59116_write_reg(TLC59116_MODE1, TLC59116_MODE1_DEFAULT);
|
|
|
|
tlc59116_write_reg(TLC59116_MODE2, TLC59116_MODE2_DEFAULT);
|
|
|
|
|
2013-05-29 00:41:06 +02:00
|
|
|
/*Set all PWM values to 0x00 (off) */
|
|
|
|
/*This would maybe be better with a SWRST */
|
|
|
|
uint8_t tx_buf[] =
|
2014-10-16 17:59:52 +02:00
|
|
|
{ TLC59116_PWM0_AUTOINCR, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
|
|
|
|
tlc59116_write_stream(17, tx_buf);
|
2013-05-23 23:35:21 +02:00
|
|
|
|
2013-05-22 17:46:12 +02:00
|
|
|
/* set all leds to PWM control */
|
2013-05-29 00:41:06 +02:00
|
|
|
tlc59116_write_reg(TLC59116_LEDOUT0, TLC59116_LEDOUT_PWM);
|
|
|
|
tlc59116_write_reg(TLC59116_LEDOUT1, TLC59116_LEDOUT_PWM);
|
|
|
|
tlc59116_write_reg(TLC59116_LEDOUT2, TLC59116_LEDOUT_PWM);
|
|
|
|
tlc59116_write_reg(TLC59116_LEDOUT3, TLC59116_LEDOUT_PWM);
|
2013-05-22 17:46:12 +02:00
|
|
|
}
|