Statistics
| Branch: | Tag: | Revision:

amiro-lld / source / alld_tlc5947.c @ ef078306

History | View | Annotate | Download (7.023 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
#if defined(AMIROLLD_CFG_USE_TLC5947) || defined(__DOXYGEN__)
30

    
31
/******************************************************************************/
32
/* LOCAL DEFINITIONS                                                          */
33
/******************************************************************************/
34

    
35
/******************************************************************************/
36
/* EXPORTED VARIABLES                                                         */
37
/******************************************************************************/
38

    
39
/******************************************************************************/
40
/* LOCAL TYPES                                                                */
41
/******************************************************************************/
42

    
43
/******************************************************************************/
44
/* LOCAL VARIABLES                                                            */
45
/******************************************************************************/
46

    
47
/******************************************************************************/
48
/* LOCAL FUNCTIONS                                                            */
49
/******************************************************************************/
50

    
51
/******************************************************************************/
52
/* EXPORTED FUNCTIONS                                                         */
53
/******************************************************************************/
54

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

    
66
  // set the output value of the GPIO pin depending on the activation property
67
  return apalControlGpioSet(tlc5947->blank_gpio, blank == TLC5947_LLD_BLANK_ENABLE ? APAL_GPIO_ON : APAL_GPIO_OFF);
68
}
69

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

    
82
  apalControlGpioState_t gpio_state;
83
  apalExitStatus_t status = apalControlGpioGet(tlc5947->blank_gpio, &gpio_state);
84
  *blank = gpio_state == APAL_GPIO_ON ? TLC5947_LLD_BLANK_ENABLE : TLC5947_LLD_BLANK_DISABLE;
85
  return status;
86
}
87

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

    
98
  apalExitStatus_t status = apalControlGpioSet(tlc5947->xlat_gpio, APAL_GPIO_ON);
99
  // The XLAT signal has to be active for at least 30 ns.
100
  // It is assumed that that these function calls satisfy this requirement even without explicit delay.
101
  if (apalControlGpioSet(tlc5947->xlat_gpio, APAL_GPIO_OFF) == APAL_STATUS_OK && status == APAL_STATUS_OK) {
102
    return APAL_STATUS_OK;
103
  } else {
104
    return APAL_STATUS_ERROR;
105
  }
106
}
107

    
108
/**
109
 * @brief Write buffer via SPI to the TLC5947.
110
 * @return                  An indicator whether the call was successful.
111
 */
112
inline apalExitStatus_t
113
tlc5947_lld_write(const TLC5947Driver* const tlc5947, const tlc5947_lld_buffer_t* const buffer)
114
{
115
  apalDbgAssert(tlc5947 != NULL);
116
  apalDbgAssert(buffer != NULL);
117

    
118
  // send buffer via SPI
119
  return apalSPITransmit(tlc5947->spi_driver, buffer->data, TLC5947_LLD_BUFFER_SIZE);
120
}
121

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

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

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

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

    
176
#endif /* defined(AMIROLLD_CFG_USE_TLC5947) */
177

    
178
/** @} */