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