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