Statistics
| Branch: | Tag: | Revision:

amiro-lld / drivers / TLC5947 / v1 / alld_TLC5947.c @ 4dba9195

History | View | Annotate | Download (7.296 KB)

1
/*
2
AMiRo-LLD is a compilation of low-level hardware drivers for the Autonomous Mini Robot (AMiRo) platform.
3
Copyright (C) 2016..2019  Thomas Schöpping et al.
4

5
This program is free software: you can redistribute it and/or modify
6
it under the terms of the GNU Lesser General Public License as published by
7
the Free Software Foundation, either version 3 of the License, or
8
(at your option) any later version.
9

10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
GNU Lesser General Public License for more details.
14

15
You should have received a copy of the GNU Lesser General Public License
16
along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
*/
18

    
19
/**
20
 * @file    alld_TLC5947.c
21
 * @brief   LED Driver function implementations.
22
 *
23
 * @addtogroup lld_leddriver
24
 * @{
25
 */
26

    
27
#include <alld_TLC5947.h>
28

    
29
/******************************************************************************/
30
/* LOCAL DEFINITIONS                                                          */
31
/******************************************************************************/
32

    
33
/******************************************************************************/
34
/* EXPORTED VARIABLES                                                         */
35
/******************************************************************************/
36

    
37
/******************************************************************************/
38
/* LOCAL TYPES                                                                */
39
/******************************************************************************/
40

    
41
/******************************************************************************/
42
/* LOCAL VARIABLES                                                            */
43
/******************************************************************************/
44

    
45
/******************************************************************************/
46
/* LOCAL FUNCTIONS                                                            */
47
/******************************************************************************/
48

    
49
/******************************************************************************/
50
/* EXPORTED FUNCTIONS                                                         */
51
/******************************************************************************/
52

    
53
/**
54
 * @brief   Set the blank state of the TLC5947.
55
 * @param[in] tlc5947   The TLC5947 driver object.
56
 * @param[in] blank     The state to set the TLC5947 driver to.
57
 * @return              An indicator whether the call was successful.
58
 */
59
apalExitStatus_t tlc5947_lld_setBlank(const TLC5947Driver* const tlc5947, const tlc5947_lld_blank_t blank)
60
{
61
  apalDbgAssert(tlc5947 != NULL);
62

    
63
  // BLANK signal is optional
64
  if (tlc5947->blank_gpio == NULL) {
65
    return APAL_STATUS_WARNING;
66
  } else {
67
    // set the output value of the GPIO pin depending on the activation property
68
    return apalControlGpioSet(tlc5947->blank_gpio, blank == TLC5947_LLD_BLANK_ENABLE ? APAL_GPIO_ON : APAL_GPIO_OFF);
69
  }
70
}
71

    
72
/**
73
 * @brief   Get the current status of the TLC5947s blank pin.
74
 * @param[in]  tlc5947  The TLC5947 driver object.
75
 * @param[out] blank    The state object to fill.
76
 * @return              An indicator whether the call was successful.
77
 */
78
apalExitStatus_t tlc5947_lld_getBlank(const TLC5947Driver* const tlc5947, tlc5947_lld_blank_t* const blank)
79
{
80
  apalDbgAssert(tlc5947 != NULL);
81
  apalDbgAssert(blank != NULL);
82

    
83
  // BLANK signal is optional
84
  if (tlc5947->blank_gpio == NULL) {
85
    return APAL_STATUS_WARNING;
86
  } else {
87
    apalControlGpioState_t gpio_state;
88
    apalExitStatus_t status = apalControlGpioGet(tlc5947->blank_gpio, &gpio_state);
89
    *blank = gpio_state == APAL_GPIO_ON ? TLC5947_LLD_BLANK_ENABLE : TLC5947_LLD_BLANK_DISABLE;
90
    return status;
91
  }
92
}
93

    
94
/**
95
 * @brief   Writes a pulse on XLAT signal.
96
 * @details A pulse makes the TLC to move the grayscale register data to the internal latch and visualize it.
97
 * @param[in] tlc5947   The TLC5947 driver object.
98
 * @return              An indicator whether the call was successful.
99
 */
100
apalExitStatus_t tlc5947_lld_update(const TLC5947Driver* const tlc5947)
101
{
102
  apalDbgAssert(tlc5947 != NULL);
103

    
104
  // XLAT signal GPIO is optional
105
  if (tlc5947->xlat_gpio == NULL) {
106
    return APAL_STATUS_WARNING;
107
  } else {
108
    apalExitStatus_t status = apalGpioWrite(tlc5947->xlat_gpio->gpio, (tlc5947->xlat_gpio->meta.edge == APAL_GPIO_EDGE_RISING) ? APAL_GPIO_LOW : APAL_GPIO_HIGH);
109
    // The XLAT signal has to be active for at least 30 ns.
110
    // It is assumed that that these function calls satisfy this requirement even without explicit delay.
111
    status |= apalGpioWrite(tlc5947->xlat_gpio->gpio, (tlc5947->xlat_gpio->meta.edge == APAL_GPIO_EDGE_RISING) ? APAL_GPIO_HIGH : APAL_GPIO_LOW);
112
    return status;
113
  }
114
}
115

    
116
/**
117
 * @brief Write buffer via SPI to the TLC5947.
118
 * @return                  An indicator whether the call was successful.
119
 */
120
apalExitStatus_t tlc5947_lld_write(const TLC5947Driver* const tlc5947, const tlc5947_lld_buffer_t* const buffer)
121
{
122
  apalDbgAssert(tlc5947 != NULL);
123
  apalDbgAssert(buffer != NULL);
124

    
125
  // send buffer via SPI
126
  return apalSPITransmit(tlc5947->spi_driver, buffer->data, TLC5947_LLD_BUFFER_SIZE);
127
}
128

    
129
/**
130
 * @brief   Set a specific channel of a given buffer.
131
 * @param[in] buffer    The buffer to modify.
132
 * @param[in] channel   The channel to set.
133
 * @param[in] value     The new value to set.
134
 *                      Must be a 12bit value.
135
 * @return              An indicator whether the call was successful.
136
 */
137
void tlc5947_lld_setBuffer(tlc5947_lld_buffer_t* const buffer, const uint8_t channel, const uint16_t value)
138
{
139
  apalDbgAssert(buffer != NULL);
140
  apalDbgAssert(channel < TLC5947_LLD_NUM_CHANNELS);
141
  apalDbgAssert(value <= (1 << TLC5947_LLD_PWM_RESOLUTION_BITS) - 1);
142

    
143
  // reverse the channel number, since the data is shifted within the TLC5947 and thus the order is inverted
144
  const uint8_t idx = (uint16_t)(TLC5947_LLD_NUM_CHANNELS-1 - channel) * TLC5947_LLD_PWM_RESOLUTION_BITS / 8;
145
  // channel is odd / inverse channel is even
146
  if (channel % 2) {
147
    buffer->data[idx] = (value >> 4) & 0xFFu;
148
    buffer->data[idx+1] = ((value << 4) & 0xF0u) | (buffer->data[idx+1] & 0x0Fu);
149
  }
150
  // channel is even / inverse channel is odd
151
  else {
152
    buffer->data[idx] = (buffer->data[idx] & 0xF0u) | ((value >> 8) & 0x0Fu);
153
    buffer->data[idx+1] = value & 0xFFu;
154
  }
155
  return;
156
}
157

    
158
/**
159
 * @brief   Read a specific state from a given buffer.
160
 * @param[in] buffer    The buffer to read from.
161
 * @param[in] channel   The channel to read
162
 * @return              An indicator whether the call was successful.
163
 */
164
uint16_t tlc5947_lld_getBuffer(const tlc5947_lld_buffer_t* const buffer, const uint8_t channel)
165
{
166
  apalDbgAssert(buffer != NULL);
167
  apalDbgAssert(channel < TLC5947_LLD_NUM_CHANNELS);
168

    
169
  // reverse the channel number, since the data is shifted within the TLC5947 and thus the order is inverted
170
  const uint8_t idx = (uint16_t)(TLC5947_LLD_NUM_CHANNELS-1- channel) * TLC5947_LLD_PWM_RESOLUTION_BITS / 8;
171
  // channel is odd / inverse channel is even
172
  if (channel % 2) {
173
    return (((uint16_t)(buffer->data[idx])) << 4) | (uint16_t)((buffer->data[idx+1] & 0xF0u) >> 4);
174
  }
175
  // channel is even / inverse channel is odd
176
  else {
177
    return (((uint16_t)(buffer->data[idx] & 0x0Fu)) << 8) | (uint16_t)(buffer->data[idx+1]);
178
  }
179
}
180

    
181
/** @} */