Statistics
| Branch: | Tag: | Revision:

amiro-lld / source / alld_tlc5947.c @ 8c47f14b

History | View | Annotate | Download (5.59 KB)

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

5
This program is free software: you can redistribute it and/or modify
6 f0ca400f Thomas Schöpping
it under the terms of the GNU Lesser General Public License as published by
7 d6728c5b Thomas Schöpping
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 f0ca400f Thomas Schöpping
GNU Lesser General Public License for more details.
14 d6728c5b Thomas Schöpping

15 f0ca400f Thomas Schöpping
You should have received a copy of the GNU Lesser General Public License
16 d6728c5b Thomas Schöpping
along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
*/
18
19 5e2f673b Marc Rothmann
/**
20
 * @file    alld_tlc5947.c
21
 * @brief   LED Driver function implementations.
22
 *
23
 * @addtogroup lld_leddriver
24
 * @{
25
 */
26
27 d6728c5b Thomas Schöpping
#include <alld_tlc5947.h>
28
29
#if defined(AMIROLLD_CFG_USE_TLC5947) || defined(__DOXYGEN__)
30
31
/**
32
 * @brief   Set the blank state of the TLC5947.
33
 * @param[in] tlc5947   The TLC5947 driver object.
34
 * @param[in] blank     The state to set the TLC5947 driver to.
35
 * @return              An indicator whether the call was successful.
36
 */
37
inline apalExitStatus_t
38
tlc5947_lld_setBlank(const TLC5947Driver* const tlc5947, const tlc5947_lld_blank_t blank)
39
{
40
  apalDbgAssert(tlc5947 != NULL);
41
42
  // set the output value of the GPIO pin depending on the activation property
43 cf1f756b Thomas Schöpping
  return apalControlGpioSet(tlc5947->blank_gpio, blank == TLC5947_LLD_BLANK_ENABLE ? APAL_GPIO_ON : APAL_GPIO_OFF);
44 d6728c5b Thomas Schöpping
}
45
46
/**
47
 * @brief   Get the current status of the TLC5947s blank pin.
48
 * @param[in]  tlc5947  The TLC5947 driver object.
49
 * @param[out] blank    The state object to fill.
50
 * @return              An indicator whether the call was successful.
51
 */
52
inline apalExitStatus_t
53
tlc5947_lld_getBlank(const TLC5947Driver* const tlc5947, tlc5947_lld_blank_t* const blank)
54
{
55
  apalDbgAssert(tlc5947 != NULL);
56
  apalDbgAssert(blank != NULL);
57
58
  apalControlGpioState_t gpio_state;
59 cf1f756b Thomas Schöpping
  apalExitStatus_t status = apalControlGpioGet(tlc5947->blank_gpio, &gpio_state);
60 d6728c5b Thomas Schöpping
  *blank = gpio_state == APAL_GPIO_ON ? TLC5947_LLD_BLANK_ENABLE : TLC5947_LLD_BLANK_DISABLE;
61
  return status;
62
}
63
64
/**
65
 * @brief   Writes a pulse on XLAT signal.
66
 * @details A pulse makes the TLC to move the grayscale register data to the internal latch and visualize it.
67
 * @param[in] tlc5947   The TLC5947 driver object.
68
 * @return              An indicator whether the call was successful.
69
 */
70
apalExitStatus_t tlc5947_lld_update(const TLC5947Driver* const tlc5947)
71
{
72
  apalDbgAssert(tlc5947 != NULL);
73
74 cf1f756b Thomas Schöpping
  apalExitStatus_t status = apalControlGpioSet(tlc5947->xlat_gpio, APAL_GPIO_ON);
75 d6728c5b Thomas Schöpping
  // The XLAT signal has to be active for at least 30 ns.
76
  // It is assumed that that these function calls satisfy this requirement even without explicit delay.
77 cf1f756b Thomas Schöpping
  if (apalControlGpioSet(tlc5947->xlat_gpio, APAL_GPIO_OFF) == APAL_STATUS_OK && status == APAL_STATUS_OK) {
78 d6728c5b Thomas Schöpping
    return APAL_STATUS_OK;
79
  } else {
80
    return APAL_STATUS_ERROR;
81
  }
82
}
83
84
/**
85
 * @brief Write buffer via SPI to the TLC5947.
86
 * @return                  An indicator whether the call was successful.
87
 */
88
inline apalExitStatus_t
89
tlc5947_lld_write(const TLC5947Driver* const tlc5947, const tlc5947_lld_buffer_t* const buffer)
90
{
91
  apalDbgAssert(tlc5947 != NULL);
92
  apalDbgAssert(buffer != NULL);
93
94
  // send buffer via SPI
95
  return apalSPITransmit(tlc5947->spi_driver, buffer->data, TLC5947_LLD_BUFFER_SIZE);
96
}
97
98
/**
99
 * @brief   Set a specific channel of a given buffer.
100
 * @param[in] buffer    The buffer to modify.
101
 * @param[in] channel   The channel to set.
102
 * @param[in] value     The new value to set.
103
 *                      Must be a 12bit value.
104
 * @return              An indicator whether the call was successful.
105
 */
106
inline void
107
tlc5947_lld_setBuffer(tlc5947_lld_buffer_t* const buffer, const uint8_t channel, const uint16_t value)
108
{
109
  apalDbgAssert(buffer != NULL);
110
  apalDbgAssert(channel < TLC5947_LLD_NUM_CHANNELS);
111
  apalDbgAssert(value <= (1 << TLC5947_LLD_PWM_RESOLUTION_BITS) - 1);
112
113
  // reverse the channel number, since the data is shifted within the TLC5947 and thus the order is inverted
114
  const uint8_t idx = (uint16_t)(TLC5947_LLD_NUM_CHANNELS-1 - channel) * TLC5947_LLD_PWM_RESOLUTION_BITS / 8;
115
  // channel is odd / inverse channel is even
116
  if (channel % 2) {
117
    buffer->data[idx] = (value >> 4) & 0xFFu;
118
    buffer->data[idx+1] = ((value << 4) & 0xF0u) | (buffer->data[idx+1] & 0x0Fu);
119
  }
120
  // channel is even / inverse channel is odd
121
  else {
122
    buffer->data[idx] = (buffer->data[idx] & 0xF0u) | ((value >> 8) & 0x0Fu);
123
    buffer->data[idx+1] = value & 0xFFu;
124
  }
125
  return;
126
}
127
128
/**
129
 * @brief   Read a specific state from a given buffer.
130
 * @param[in] buffer    The buffer to read from.
131
 * @param[in] channel   The channel to read
132
 * @return              An indicator whether the call was successful.
133
 */
134
inline uint16_t
135
tlc5947_lld_getBuffer(const tlc5947_lld_buffer_t* const buffer, const uint8_t channel)
136
{
137
  apalDbgAssert(buffer != NULL);
138
  apalDbgAssert(channel < TLC5947_LLD_NUM_CHANNELS);
139
140
  // reverse the channel number, since the data is shifted within the TLC5947 and thus the order is inverted
141
  const uint8_t idx = (uint16_t)(TLC5947_LLD_NUM_CHANNELS-1- channel) * TLC5947_LLD_PWM_RESOLUTION_BITS / 8;
142
  // channel is odd / inverse channel is even
143
  if (channel % 2) {
144
    return (((uint16_t)(buffer->data[idx])) << 4) | (uint16_t)((buffer->data[idx+1] & 0xF0u) >> 4);
145
  }
146
  // channel is even / inverse channel is odd
147
  else {
148
    return (((uint16_t)(buffer->data[idx] & 0x0Fu)) << 8) | (uint16_t)(buffer->data[idx+1]);
149
  }
150
}
151
152
#endif /* defined(AMIROLLD_CFG_USE_TLC5947) */
153 5e2f673b Marc Rothmann
154
/** @} */