amiro-lld / source / TLC5947 / v1 / alld_TLC5947_v1.c @ b95be72b
History | View | Annotate | Download (7.047 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 | 1d5bcc82 | Thomas Schöpping | * @file alld_TLC5947_v1.c
|
| 21 | 5e2f673b | Marc Rothmann | * @brief LED Driver function implementations.
|
| 22 | *
|
||
| 23 | * @addtogroup lld_leddriver
|
||
| 24 | * @{
|
||
| 25 | */
|
||
| 26 | |||
| 27 | 1d5bcc82 | Thomas Schöpping | #include <alld_TLC5947.h> |
| 28 | d6728c5b | Thomas Schöpping | |
| 29 | 1d5bcc82 | Thomas Schöpping | #if (defined(AMIROLLD_CFG_TLC5947) && (AMIROLLD_CFG_TLC5947 == 1)) || defined(__DOXYGEN__) |
| 30 | d6728c5b | Thomas Schöpping | |
| 31 | ef078306 | Thomas Schöpping | /******************************************************************************/
|
| 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 | d6728c5b | Thomas Schöpping | /**
|
| 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 | 21076167 | Thomas Schöpping | apalExitStatus_t tlc5947_lld_setBlank(const TLC5947Driver* const tlc5947, const tlc5947_lld_blank_t blank) |
| 62 | d6728c5b | Thomas Schöpping | {
|
| 63 | apalDbgAssert(tlc5947 != NULL);
|
||
| 64 | |||
| 65 | // set the output value of the GPIO pin depending on the activation property
|
||
| 66 | cf1f756b | Thomas Schöpping | return apalControlGpioSet(tlc5947->blank_gpio, blank == TLC5947_LLD_BLANK_ENABLE ? APAL_GPIO_ON : APAL_GPIO_OFF);
|
| 67 | d6728c5b | Thomas Schöpping | } |
| 68 | |||
| 69 | /**
|
||
| 70 | * @brief Get the current status of the TLC5947s blank pin.
|
||
| 71 | * @param[in] tlc5947 The TLC5947 driver object.
|
||
| 72 | * @param[out] blank The state object to fill.
|
||
| 73 | * @return An indicator whether the call was successful.
|
||
| 74 | */
|
||
| 75 | 21076167 | Thomas Schöpping | apalExitStatus_t tlc5947_lld_getBlank(const TLC5947Driver* const tlc5947, tlc5947_lld_blank_t* const blank) |
| 76 | d6728c5b | Thomas Schöpping | {
|
| 77 | apalDbgAssert(tlc5947 != NULL);
|
||
| 78 | apalDbgAssert(blank != NULL);
|
||
| 79 | |||
| 80 | apalControlGpioState_t gpio_state; |
||
| 81 | cf1f756b | Thomas Schöpping | apalExitStatus_t status = apalControlGpioGet(tlc5947->blank_gpio, &gpio_state); |
| 82 | d6728c5b | Thomas Schöpping | *blank = gpio_state == APAL_GPIO_ON ? TLC5947_LLD_BLANK_ENABLE : TLC5947_LLD_BLANK_DISABLE; |
| 83 | return status;
|
||
| 84 | } |
||
| 85 | |||
| 86 | /**
|
||
| 87 | * @brief Writes a pulse on XLAT signal.
|
||
| 88 | * @details A pulse makes the TLC to move the grayscale register data to the internal latch and visualize it.
|
||
| 89 | * @param[in] tlc5947 The TLC5947 driver object.
|
||
| 90 | * @return An indicator whether the call was successful.
|
||
| 91 | */
|
||
| 92 | apalExitStatus_t tlc5947_lld_update(const TLC5947Driver* const tlc5947) |
||
| 93 | {
|
||
| 94 | apalDbgAssert(tlc5947 != NULL);
|
||
| 95 | |||
| 96 | cf1f756b | Thomas Schöpping | apalExitStatus_t status = apalControlGpioSet(tlc5947->xlat_gpio, APAL_GPIO_ON); |
| 97 | d6728c5b | Thomas Schöpping | // The XLAT signal has to be active for at least 30 ns.
|
| 98 | // It is assumed that that these function calls satisfy this requirement even without explicit delay.
|
||
| 99 | cf1f756b | Thomas Schöpping | if (apalControlGpioSet(tlc5947->xlat_gpio, APAL_GPIO_OFF) == APAL_STATUS_OK && status == APAL_STATUS_OK) {
|
| 100 | d6728c5b | Thomas Schöpping | return APAL_STATUS_OK;
|
| 101 | } else {
|
||
| 102 | return APAL_STATUS_ERROR;
|
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | /**
|
||
| 107 | * @brief Write buffer via SPI to the TLC5947.
|
||
| 108 | * @return An indicator whether the call was successful.
|
||
| 109 | */
|
||
| 110 | 21076167 | Thomas Schöpping | apalExitStatus_t tlc5947_lld_write(const TLC5947Driver* const tlc5947, const tlc5947_lld_buffer_t* const buffer) |
| 111 | d6728c5b | Thomas Schöpping | {
|
| 112 | apalDbgAssert(tlc5947 != NULL);
|
||
| 113 | apalDbgAssert(buffer != NULL);
|
||
| 114 | |||
| 115 | // send buffer via SPI
|
||
| 116 | return apalSPITransmit(tlc5947->spi_driver, buffer->data, TLC5947_LLD_BUFFER_SIZE);
|
||
| 117 | } |
||
| 118 | |||
| 119 | /**
|
||
| 120 | * @brief Set a specific channel of a given buffer.
|
||
| 121 | * @param[in] buffer The buffer to modify.
|
||
| 122 | * @param[in] channel The channel to set.
|
||
| 123 | * @param[in] value The new value to set.
|
||
| 124 | * Must be a 12bit value.
|
||
| 125 | * @return An indicator whether the call was successful.
|
||
| 126 | */
|
||
| 127 | 21076167 | Thomas Schöpping | void tlc5947_lld_setBuffer(tlc5947_lld_buffer_t* const buffer, const uint8_t channel, const uint16_t value) |
| 128 | d6728c5b | Thomas Schöpping | {
|
| 129 | apalDbgAssert(buffer != NULL);
|
||
| 130 | apalDbgAssert(channel < TLC5947_LLD_NUM_CHANNELS); |
||
| 131 | apalDbgAssert(value <= (1 << TLC5947_LLD_PWM_RESOLUTION_BITS) - 1); |
||
| 132 | |||
| 133 | // reverse the channel number, since the data is shifted within the TLC5947 and thus the order is inverted
|
||
| 134 | const uint8_t idx = (uint16_t)(TLC5947_LLD_NUM_CHANNELS-1 - channel) * TLC5947_LLD_PWM_RESOLUTION_BITS / 8; |
||
| 135 | // channel is odd / inverse channel is even
|
||
| 136 | if (channel % 2) { |
||
| 137 | buffer->data[idx] = (value >> 4) & 0xFFu; |
||
| 138 | buffer->data[idx+1] = ((value << 4) & 0xF0u) | (buffer->data[idx+1] & 0x0Fu); |
||
| 139 | } |
||
| 140 | // channel is even / inverse channel is odd
|
||
| 141 | else {
|
||
| 142 | buffer->data[idx] = (buffer->data[idx] & 0xF0u) | ((value >> 8) & 0x0Fu); |
||
| 143 | buffer->data[idx+1] = value & 0xFFu; |
||
| 144 | } |
||
| 145 | return;
|
||
| 146 | } |
||
| 147 | |||
| 148 | /**
|
||
| 149 | * @brief Read a specific state from a given buffer.
|
||
| 150 | * @param[in] buffer The buffer to read from.
|
||
| 151 | * @param[in] channel The channel to read
|
||
| 152 | * @return An indicator whether the call was successful.
|
||
| 153 | */
|
||
| 154 | 21076167 | Thomas Schöpping | uint16_t tlc5947_lld_getBuffer(const tlc5947_lld_buffer_t* const buffer, const uint8_t channel) |
| 155 | d6728c5b | Thomas Schöpping | {
|
| 156 | apalDbgAssert(buffer != NULL);
|
||
| 157 | apalDbgAssert(channel < TLC5947_LLD_NUM_CHANNELS); |
||
| 158 | |||
| 159 | // reverse the channel number, since the data is shifted within the TLC5947 and thus the order is inverted
|
||
| 160 | const uint8_t idx = (uint16_t)(TLC5947_LLD_NUM_CHANNELS-1- channel) * TLC5947_LLD_PWM_RESOLUTION_BITS / 8; |
||
| 161 | // channel is odd / inverse channel is even
|
||
| 162 | if (channel % 2) { |
||
| 163 | return (((uint16_t)(buffer->data[idx])) << 4) | (uint16_t)((buffer->data[idx+1] & 0xF0u) >> 4); |
||
| 164 | } |
||
| 165 | // channel is even / inverse channel is odd
|
||
| 166 | else {
|
||
| 167 | return (((uint16_t)(buffer->data[idx] & 0x0Fu)) << 8) | (uint16_t)(buffer->data[idx+1]); |
||
| 168 | } |
||
| 169 | } |
||
| 170 | |||
| 171 | 1d5bcc82 | Thomas Schöpping | #endif /* defined(AMIROLLD_CFG_TLC5947) && (AMIROLLD_CFG_TLC5947 == 1) */ |
| 172 | 5e2f673b | Marc Rothmann | |
| 173 | /** @} */ |