2018-07-22 23:13:39 +02:00
|
|
|
/****************************************************************************
|
2021-03-08 22:39:04 +01:00
|
|
|
* drivers/leds/max7219.c
|
2018-07-22 23:13:39 +02:00
|
|
|
*
|
2021-03-13 10:55:07 +01:00
|
|
|
* 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
|
2018-07-22 23:13:39 +02:00
|
|
|
*
|
2021-03-13 10:55:07 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2018-07-22 23:13:39 +02:00
|
|
|
*
|
2021-03-13 10:55:07 +01:00
|
|
|
* 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.
|
2018-07-22 23:13:39 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-03-13 10:55:07 +01:00
|
|
|
/* Maxim MAX7219 driver used to control 7-segment display. */
|
|
|
|
|
2018-07-22 23:13:39 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <nuttx/kmalloc.h>
|
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
#include <nuttx/spi/spi.h>
|
|
|
|
#include <nuttx/leds/max7219.h>
|
|
|
|
|
|
|
|
#if defined(CONFIG_SPI) && defined(CONFIG_LEDS_MAX7219)
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* SPI frequency */
|
|
|
|
|
|
|
|
#ifndef CONFIG_MAX7219_FREQUENCY
|
|
|
|
# define CONFIG_MAX7219_FREQUENCY 5000000
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef CONFIG_MAX7219_INTENSITY
|
|
|
|
# define CONFIG_MAX7219_INTENSITY 8
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct max7219_dev_s
|
|
|
|
{
|
|
|
|
FAR struct spi_dev_s *spi; /* SPI interface */
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline void max7219_configspi(FAR struct spi_dev_s *spi);
|
|
|
|
static inline void max7219_write16(FAR struct max7219_dev_s *priv,
|
|
|
|
uint16_t value);
|
|
|
|
|
|
|
|
/* Character driver methods */
|
|
|
|
|
|
|
|
static ssize_t max7219_read(FAR struct file *filep, FAR char *buffer,
|
|
|
|
size_t buflen);
|
|
|
|
static ssize_t max7219_write(FAR struct file *filep, FAR const char *buffer,
|
2022-04-12 12:16:13 +02:00
|
|
|
size_t buflen);
|
2018-07-22 23:13:39 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static const struct file_operations g_max7219fops =
|
|
|
|
{
|
2022-04-12 12:16:13 +02:00
|
|
|
NULL, /* open */
|
|
|
|
NULL, /* close */
|
2018-07-22 23:13:39 +02:00
|
|
|
max7219_read, /* read */
|
|
|
|
max7219_write, /* write */
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: max7219_configspi
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Set the SPI bus configuration
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline void max7219_configspi(FAR struct spi_dev_s *spi)
|
|
|
|
{
|
|
|
|
/* Configure SPI for the MAX7219 */
|
|
|
|
|
|
|
|
SPI_SETMODE(spi, SPIDEV_MODE0);
|
|
|
|
SPI_SETBITS(spi, 8);
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_HWFEATURES(spi, 0);
|
|
|
|
SPI_SETFREQUENCY(spi, CONFIG_MAX7219_FREQUENCY);
|
2018-07-22 23:13:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: max7219_write16
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Write 16-bit to MAX7219
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static inline void max7219_write16(FAR struct max7219_dev_s *priv,
|
|
|
|
uint16_t value)
|
|
|
|
{
|
|
|
|
/* If SPI bus is shared then lock and configure it */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_LOCK(priv->spi, true);
|
2018-07-22 23:13:39 +02:00
|
|
|
|
|
|
|
/* Configure the SPI */
|
|
|
|
|
|
|
|
max7219_configspi(priv->spi);
|
|
|
|
|
|
|
|
/* Select */
|
|
|
|
|
|
|
|
SPI_SELECT(priv->spi, SPIDEV_DISPLAY(0), true);
|
|
|
|
|
|
|
|
/* Send 16 bits (2 bytes) */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_SNDBLOCK(priv->spi, &value, 2);
|
2018-07-22 23:13:39 +02:00
|
|
|
|
2018-09-29 19:16:45 +02:00
|
|
|
/* De-select */
|
2018-07-22 23:13:39 +02:00
|
|
|
|
|
|
|
SPI_SELECT(priv->spi, SPIDEV_DISPLAY(0), false);
|
|
|
|
|
|
|
|
/* Unlock bus */
|
|
|
|
|
2020-01-02 17:49:34 +01:00
|
|
|
SPI_LOCK(priv->spi, false);
|
2018-07-22 23:13:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: max7219_read
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-03-13 11:55:37 +01:00
|
|
|
static ssize_t max7219_read(FAR struct file *filep,
|
|
|
|
FAR char *buffer, size_t buflen)
|
2018-07-22 23:13:39 +02:00
|
|
|
{
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: max7219_write
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static ssize_t max7219_write(FAR struct file *filep, FAR const char *buffer,
|
2022-04-12 12:16:13 +02:00
|
|
|
size_t buflen)
|
2018-07-22 23:13:39 +02:00
|
|
|
{
|
|
|
|
FAR struct inode *inode = filep->f_inode;
|
|
|
|
FAR struct max7219_dev_s *priv = inode->i_private;
|
|
|
|
uint16_t data;
|
2018-09-29 19:16:45 +02:00
|
|
|
uint8_t digits[8];
|
|
|
|
uint8_t digcnt = 0;
|
2018-07-22 23:13:39 +02:00
|
|
|
uint8_t i;
|
|
|
|
|
2018-09-29 19:16:45 +02:00
|
|
|
if (buffer == NULL)
|
2018-07-22 23:13:39 +02:00
|
|
|
{
|
2018-09-29 19:16:45 +02:00
|
|
|
lederr("ERROR: Buffer is null\n");
|
2018-07-22 23:13:39 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-09-29 19:16:45 +02:00
|
|
|
/* We need at least one display, so 1 byte */
|
2018-07-22 23:13:39 +02:00
|
|
|
|
|
|
|
if (buflen < 1)
|
|
|
|
{
|
2018-09-29 19:16:45 +02:00
|
|
|
lederr("ERROR: You need to control at least 1 digit!\n");
|
2018-07-22 23:13:39 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-09-29 19:16:45 +02:00
|
|
|
/* Get and count the valid digits */
|
2018-07-22 23:13:39 +02:00
|
|
|
|
2018-09-29 19:16:45 +02:00
|
|
|
for (i = 0; i < buflen; i++)
|
2018-07-22 23:13:39 +02:00
|
|
|
{
|
2018-09-29 19:16:45 +02:00
|
|
|
if (buffer[i] >= '0' && buffer[i] <= '9')
|
|
|
|
{
|
|
|
|
digits[digcnt] = buffer[i] - '0';
|
|
|
|
digcnt++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
switch (buffer[i])
|
|
|
|
{
|
|
|
|
case '-':
|
|
|
|
digits[digcnt] = 0x0a;
|
|
|
|
digcnt++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'E':
|
|
|
|
digits[digcnt] = 0x0b;
|
|
|
|
digcnt++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'H':
|
|
|
|
digits[digcnt] = 0x0c;
|
|
|
|
digcnt++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'L':
|
|
|
|
digits[digcnt] = 0x0d;
|
|
|
|
digcnt++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'P':
|
|
|
|
digits[digcnt] = 0x0e;
|
|
|
|
digcnt++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ' ':
|
|
|
|
digits[digcnt] = 0x0f;
|
|
|
|
digcnt++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '.':
|
|
|
|
if (i == 0)
|
|
|
|
{
|
|
|
|
digits[0] = 0x0f | 0x80;
|
|
|
|
digcnt++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
digits[digcnt - 1] |= 0x80;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
lederr("Digit code unsupported\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we reach 8 digits then stop */
|
|
|
|
|
|
|
|
if (digcnt >= 8 && buffer[i + 1] != '.')
|
|
|
|
{
|
|
|
|
ledwarn("ERROR: The limit is 8 digits!\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Blank the display */
|
|
|
|
|
|
|
|
for (i = 0; i < 8; i++)
|
|
|
|
{
|
|
|
|
data = (MAX7219_DIGIT0 + i) | (0x0f << 8);
|
|
|
|
max7219_write16(priv, data);
|
2018-07-22 23:13:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Write each digit */
|
|
|
|
|
2018-09-29 19:16:45 +02:00
|
|
|
for (i = 0; i < digcnt; i++)
|
2018-07-22 23:13:39 +02:00
|
|
|
{
|
2018-09-29 19:16:45 +02:00
|
|
|
data = (MAX7219_DIGIT0 + i) | (digits[digcnt - i - 1]) << 8;
|
2018-07-22 23:13:39 +02:00
|
|
|
max7219_write16(priv, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* All bytes were written */
|
|
|
|
|
|
|
|
return buflen;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2018-09-29 19:16:45 +02:00
|
|
|
* Name: max7219_leds_register
|
2018-07-22 23:13:39 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Register the MAX7219 character device as 'devpath'
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* devpath - The full path to the driver to register. E.g., "/dev/temp0"
|
|
|
|
* i2c - An instance of the I2C interface to use to communicate with
|
|
|
|
* MAX7219
|
|
|
|
* addr - The I2C address of the LM-75. The base I2C address of the
|
|
|
|
* MAX7219 is 0x48. Bits 0-3 can be controlled to get 8 unique
|
|
|
|
* addresses from 0x48 through 0x4f.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) on success; a negated errno value on failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int max7219_leds_register(FAR const char *devpath, FAR struct spi_dev_s *spi)
|
|
|
|
{
|
|
|
|
FAR struct max7219_dev_s *priv;
|
|
|
|
uint16_t data;
|
|
|
|
int ret;
|
2018-09-29 19:16:45 +02:00
|
|
|
int i;
|
2018-07-22 23:13:39 +02:00
|
|
|
|
|
|
|
/* Initialize the MAX7219 device structure */
|
|
|
|
|
2021-03-13 11:55:37 +01:00
|
|
|
priv = (FAR struct max7219_dev_s *)
|
|
|
|
kmm_malloc(sizeof(struct max7219_dev_s));
|
2018-07-22 23:13:39 +02:00
|
|
|
if (!priv)
|
|
|
|
{
|
2018-09-29 19:16:45 +02:00
|
|
|
lederr("ERROR: Failed to allocate instance\n");
|
2018-07-22 23:13:39 +02:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
priv->spi = spi;
|
|
|
|
|
|
|
|
/* Setup defined intensity */
|
|
|
|
|
|
|
|
data = (MAX7219_INTENSITY) |
|
|
|
|
(DISPLAY_INTENSITY(CONFIG_MAX7219_INTENSITY) << 8);
|
|
|
|
max7219_write16(priv, data);
|
|
|
|
|
|
|
|
/* Enable decoding for all digits */
|
|
|
|
|
|
|
|
data = (MAX7219_DECODE_MODE) | (ENABLE_DECODE << 8);
|
|
|
|
max7219_write16(priv, data);
|
|
|
|
|
2018-09-29 19:16:45 +02:00
|
|
|
/* Display all digits */
|
|
|
|
|
|
|
|
data = (MAX7219_SCAN_LIMIT) | (0x07 << 8);
|
|
|
|
max7219_write16(priv, data);
|
|
|
|
|
|
|
|
/* Leave the shutdown mode */
|
|
|
|
|
|
|
|
data = (MAX7219_SHUTDOWN) | (MAX7219_POWER_ON << 8);
|
|
|
|
max7219_write16(priv, data);
|
|
|
|
|
|
|
|
/* Blank the display */
|
|
|
|
|
|
|
|
for (i = 0; i < 8; i++)
|
|
|
|
{
|
|
|
|
data = (MAX7219_DIGIT0 + i) | (0x0f << 8);
|
|
|
|
max7219_write16(priv, data);
|
|
|
|
}
|
|
|
|
|
2018-07-22 23:13:39 +02:00
|
|
|
/* Register the character driver */
|
|
|
|
|
|
|
|
ret = register_driver(devpath, &g_max7219fops, 0666, priv);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2018-09-29 19:16:45 +02:00
|
|
|
lederr("ERROR: Failed to register driver: %d\n", ret);
|
2018-07-22 23:13:39 +02:00
|
|
|
kmm_free(priv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_SPI && CONFIG_MAX7219 */
|