wes3 commented on a change in pull request #1105: hw/drivers: Add TLC5971 driver
URL: https://github.com/apache/mynewt-core/pull/1105#discussion_r189133913
 
 

 ##########
 File path: hw/drivers/tlc5971/src/tlc5971.c
 ##########
 @@ -0,0 +1,270 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#include <string.h>
+#include "os/mynewt.h"
+#include "tlc5971/tlc5971.h"
+#include "hal/hal_gpio.h"
+#include "hal/hal_spi.h"
+
+/**
+ * tlc5972 open
+ *
+ * Device open funtion.
+ *
+ * @param odev Pointer to OS device structure
+ * @param wait The amount of time to wait to open (if needed).
+ * @param arg device open arg (not used)
+ *
+ * @return int
+ */
+static int
+tlc5971_open(struct os_dev *odev, uint32_t wait, void *arg)
+{
+    int rc;
+    int spi_num;
+    struct tlc5971_dev *dev;
+    struct hal_spi_settings spi_cfg;
+
+    dev = (struct tlc5971_dev *)odev;
+
+    /* Configure the spi and enable it */
+    spi_cfg.baudrate = dev->tlc_itf.tpi_spi_freq;
+    spi_cfg.data_mode = HAL_SPI_MODE0;
+    spi_cfg.data_order = HAL_SPI_MSB_FIRST;
+    spi_cfg.word_size = HAL_SPI_WORD_SIZE_8BIT;
+
+    spi_num = dev->tlc_itf.tpi_spi_num;
+    hal_spi_disable(spi_num);
+    rc = hal_spi_config(spi_num, &spi_cfg);
+    if (rc) {
+        return rc;
+    }
+    hal_spi_enable(spi_num);
+
+    dev->is_enabled = true;
+    return 0;
+}
+
+static int
+tlc5971_close(struct os_dev *odev)
+{
+    struct tlc5971_dev *dev;
+
+    dev = (struct tlc5971_dev *)odev;
+
+    /* Disable the SPI */
+    hal_spi_disable(dev->tlc_itf.tpi_spi_num);
+
+    /* Device is no longer enabled */
+    dev->is_enabled = false;
+
+    return 0;
+}
+
+/**
+ * tlc5971 is enabled
+ *
+ * Returns device enabled flag
+ *
+ *
+ * @param dev Pointer to tlc5971 device
+ *
+ * @return uint8_t 0:not enabled, 1:enabled
+ */
+int
+tlc5971_is_enabled(struct tlc5971_dev *dev)
+{
+    return dev->is_enabled;
+}
+
+/**
+ * tlc5971 construct packet
+ *
+ * This routine constructs the 224-bit buffer to send to the device. Bit 224
+ * should get sent first. Since packet buffer byte 0 gets sent first to the
+ * spi, we construct the data packet in reverse order. On other words, byte 27
+ * gets placed into packet location 0, byte in location 1, etc.
+ *
+ * @param dev
+ */
+static void
+tlc5971_construct_packet(struct tlc5971_dev *dev)
+{
+    int i;
+    uint8_t *dptr;
+
+    dptr = dev->data_packet;
+
+    /* Place command, control and global brightness control into buffer */
+    dptr[0] = TLC5971_DATA_BYTE27(dev->control_data);
+    dptr[1] = TLC5971_DATA_BYTE26(dev->control_data, dev->bc.bc_blue);
+    dptr[2] = TLC5971_DATA_BYTE25(dev->bc.bc_blue, dev->bc.bc_green);
+    dptr[3] = TLC5971_DATA_BYTE24(dev->bc.bc_green, dev->bc.bc_red);
+    dptr += 4;
+
+    /* Now place 16-bit values into buffer*/
+    i = 3;
+    while (i >= 0) {
+        dptr[0] = (uint8_t)(dev->gs[i].gs_blue >> 8);
+        dptr[1] = (uint8_t)(dev->gs[i].gs_blue);
+        dptr[2] = (uint8_t)(dev->gs[i].gs_green >> 8);
+        dptr[3] = (uint8_t)(dev->gs[i].gs_green);
+        dptr[4] = (uint8_t)(dev->gs[i].gs_red >> 8);
+        dptr[5] = (uint8_t)(dev->gs[i].gs_red);
+        dptr += 6;
+        --i;
+    }
+}
+
+/**
+ * tlc5971 write
+ *
+ * Send the 224 bits to the device. Note that the device must be opened
+ * prior to calling this function
+ *
+ * @param dev   Pointer to tlc5971 device
+ *
+ * @return int  0: success; -1 error
+ */
+int
+tlc5971_write(struct tlc5971_dev *dev)
+{
+    int rc;
+    os_sr_t sr;
+
+    if (!dev->is_enabled) {
+        return -1;
+    }
+
+    /*
+     * XXX: for now, disable interrupts around write as it is possible that
+     * too long a gap will cause mis-program of device.
+     */
+    tlc5971_construct_packet(dev);
+
+    OS_ENTER_CRITICAL(sr);
 
 Review comment:
   The reason the OS_ENTER_CRITICAL() is in the code is due to the chip 
specification. I might be reading this wrong but if the chip sees a gap of 8 
clocks (well, no clock activity for 8 clocks) it will think the transfer is 
done and latch the bits in the shift register. Someone familiar with the chip 
also mentioned that a gap in the spi transfer would cause undesirable behavior.
   
   Thus, even if we have exclusive access to the chip an interrupt could occur 
that would delay the spi transfer and create a gap in it. I think the only way 
around this is to use the non-blocking SPI interface (assuming the mcu being 
used has one).
   
   The current spi frequency is 8MHz, or 1MHz per byte. This means that the 
entire transfer will take 28 usecs (give or take). Seems ok to have interrupts 
disabled for that long.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to