Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/tlc594x driver #242

Merged
merged 2 commits into from
Jul 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,10 @@ can easily configure them for you specific needs.
<td align="center">SSD1306</td>
<td align="center">TCS3414</td>
<td align="center">TCS3472</td>
<td align="center">TLC594X</td>
<td align="center">TMP102</td>
<td align="center">TMP175</td>
</tr><tr>
<td align="center">TMP175</td>
<td align="center">VL53L0</td>
<td align="center">VL6180</td>
<td align="center">WS2812</td>
Expand Down
2 changes: 1 addition & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ make gdb

## Interesting Examples

We have a lot of examples, <!--examplecount-->190<!--/examplecount--> to be
We have a lot of examples, <!--examplecount-->191<!--/examplecount--> to be
exact, but here are some of our favorite examples for our supported development
boards:

Expand Down
49 changes: 49 additions & 0 deletions examples/stm32f103c8t6_blue_pill/tlc594x/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2019, Linas Nikiperavicius
*
* This file is part of the modm project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
// ----------------------------------------------------------------------------

#include <modm/board.hpp>
#include <modm/driver/pwm/tlc594x.hpp>

using namespace Board;

/*
* Turns on each TLC594* output in ascending order.
* Each output is active for 500 milliseconds.
*/

#define CHANNELS 24

modm::TLC594X< CHANNELS, SpiMaster1, GpioA4, GpioA3 > tlc594x;

int
main()
{
Board::initialize();

SpiMaster1::connect< GpioA5::Sck, GpioA7::Mosi > ();
SpiMaster1::initialize< Board::SystemClock, 1125_kBd>();

tlc594x.initialize(0, -1, true, false, true);

while (true)
{
for (uint16_t i = 0; i < CHANNELS; ++i)
{
tlc594x.setAllChannels(0x000);
tlc594x.setChannel(i, 0xfff);
RF_CALL_BLOCKING(tlc594x.writeChannels());

modm::delayMilliseconds(500);
}
}

return 0;
}
2 changes: 2 additions & 0 deletions examples/stm32f103c8t6_blue_pill/tlc594x/openocd.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Replace this with your custom programmer
source [find interface/stlink-v2.cfg]
12 changes: 12 additions & 0 deletions examples/stm32f103c8t6_blue_pill/tlc594x/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<library>
<extends>modm:blue-pill</extends>
<options>
<option name="modm:build:build.path">../../../build/stm32f103c8t6_blue_pill/tlc594x</option>
<option name="modm:build:openocd.cfg">openocd.cfg</option>
</options>
<modules>
<module>modm:platform:spi:1</module>
linasdev marked this conversation as resolved.
Show resolved Hide resolved
<module>modm:driver:tlc594x</module>
<module>modm:build:scons</module>
</modules>
</library>
98 changes: 59 additions & 39 deletions src/modm/driver/pwm/tlc594x.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
* Copyright (c) 2012-2017, Niklas Hauser
* Copyright (c) 2015, Sascha Schade
* Copyright (c) 2019, Linas Nikiperavicius
*
* This file is part of the modm project.
*
Expand All @@ -13,10 +14,10 @@
#ifndef MODM_TLC594X_HPP
#define MODM_TLC594X_HPP

#include <stdint.h>
#include <modm/architecture/platform.hpp>
#include <modm/architecture/interface/gpio.hpp>
#include <cstdint>
#include <modm/architecture/interface/delay.hpp>
#include <modm/architecture/interface/gpio.hpp>
#include <modm/architecture/interface/spi.hpp>

namespace modm
{
Expand All @@ -29,10 +30,6 @@ namespace modm
* Use of any EEPROM functions is therefore not implemented here.
* Make sure that pin is connected to Vcc, otherwise Dot Correction does not work.
*
* This class also does not control the BLANK pin, as the setup of the timer
* to drive that pin is platform and configuration specific.
* Switching off all LEDs using one command is not implemented here.
*
* This driver can be used for the 16-channel TLC5940 and 24-channel TLC5947
* and probably similar TLC59s as well, simply by adjusting the number of
* CHANNELS.
Expand All @@ -53,6 +50,7 @@ namespace modm
* @tparam CHANNELS Number of channels must be multiples of 4, adjust for daisy-chained chips
* @tparam Spi Spi interface
* @tparam Xlat Level triggered latch pin
* @tparam Xblank Level triggered blank pin, use modm::platform::GpioUnused if not connected
* @tparam Vprog Vprog pin, use modm::platform::GpioUnused if not connected
* @tparam Xerr Error pin, use modm::platform::GpioUnused if not connected
*
Expand All @@ -63,9 +61,10 @@ template<
uint16_t CHANNELS,
typename Spi,
typename Xlat,
typename Xblank=modm::platform::GpioUnused,
typename Vprog=modm::platform::GpioUnused,
typename Xerr=modm::platform::GpioUnused >
class TLC594X
class TLC594X : private modm::NestedResumable< 1 >
{
public:
static_assert(CHANNELS % 4 == 0, "Template parameter CHANNELS must be a multiple of 4.");
Expand All @@ -76,86 +75,107 @@ class TLC594X
* @param dots initialize dot correction buffer with value, disable with -1
* @param writeCH write channels value to chip
* @param writeDC write dots value to chip
* @param enable set blank pin low after initialization
*/
static void
initialize(uint16_t channels=0, uint8_t dots=63, bool writeCH=true, bool writeDC=true);
void
initialize(uint16_t channels=0, uint8_t dots=63, bool writeCH=true, bool writeDC=true, bool enable=true);

/// set the 12bit value of a channel
/// call transfer() to update the chip
static void
/// call writeChannels() to update the chip
void
setChannel(uint16_t channel, uint16_t value);

/// @param value the 12bit value of all channels
/// @param update write data to chip
static void
setAllChannels(uint16_t value, bool update=false);
void
setAllChannels(uint16_t value);

/// get the stored 12bit value of a channel
/// this does reflect the actual value in the chip
static uint16_t
uint16_t
getChannel(uint16_t channel);

/// set the 6bit dot correction value of a channel
/// call transfer() to update the chip
static void
/// call writeChannels() to update the chip
void
setDotCorrection(uint16_t channel, uint8_t value);

/// @param value the 6bit dot correction value of all channels
/// @param update write data to chip
static void
setAllDotCorrection(uint8_t value, bool update=false);
void
setAllDotCorrection(uint8_t value);

/// get the stored 6bit dot correction value of a channel
/// this does reflect the actual value in the chip
static uint8_t
uint8_t
getDotCorrection(uint16_t channel);

/// transfer channel data to driver chip
static void
modm::ResumableResult<void>
writeChannels(bool flush=true);

/// transfer dot correction data to driver chip
static void
writeDotCorrection();
modm::ResumableResult<void>
writeDotCorrection(bool flush=true);

/// writes data from the input shift register to either GS or DC register.
inline static void
/// writes data from the input shift register to either GS or DC register.
void
latch();

/// sets the blank pin low
void
enable()
{
enabled=true;
Xblank::reset();
}

/// sets the blank pin high
void
disable()
{
enabled=false;
Xblank::set();
}

/// @return true if LOD or TEF is detected
modm_always_inline
static bool
bool
isError()
{
return !Xerr::read();
}

modm_always_inline
static uint8_t*
uint8_t*
getGrayscaleData()
{
return gs;
}
modm_always_inline
static uint8_t*

uint8_t*
getDotCorrectionData()
{
return dc;
}
modm_always_inline
static uint8_t*

uint8_t*
getStatusData()
{
return status;
}

private:
static uint8_t status[CHANNELS*3/2];
static uint8_t gs[CHANNELS*3/2];
static uint8_t dc[CHANNELS*3/4];
bool
isEnabled()
{
return enabled;
}

private:
uint8_t status[CHANNELS*3/2];
uint8_t gs[CHANNELS*3/2];
uint8_t dc[CHANNELS*3/4];
bool enabled = false;
};
}

} //namespace modm

#include "tlc594x_impl.hpp"

Expand Down
32 changes: 32 additions & 0 deletions src/modm/driver/pwm/tlc594x.lb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (c) 2019, Linas Nikiperavicius
#
# This file is part of the modm project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# -----------------------------------------------------------------------------


def init(module):
module.name = ":driver:tlc594x"
module.description = """\
# TLC594* 12-bit PWM Driver

TLC594* multi-channel, daisy-chainable, constant-current sink, 12bit PWM LED driver.
"""

def prepare(module, options):
module.depends(
":architecture:delay",
":architecture:gpio",
":architecture:spi")
return True

def build(env):
env.outbasepath = "modm/src/modm/driver/pwm"
env.copy("tlc594x.hpp")
env.copy("tlc594x_impl.hpp")
Loading