dev: Add NXP SC16IS752 serial device driver

Update #2841.
This commit is contained in:
Alexander Krutwig authored and Sebastian Huber committed 2016-12-16 11:26:50 +01:00
1 parent e34fe384cb
commit 9edc73013b
6 files changed
+875

No files matched your search

+7
View File
@@ -15,6 +15,10 @@ include_dev_spidir = $(includedir)/dev/spi
include_dev_spi_HEADERS =
include_dev_spi_HEADERS += include/dev/spi/spi.h
include_dev_serialdir = $(includedir)/dev/serial
include_dev_serial_HEADERS =
include_dev_serial_HEADERS += include/dev/serial/sc16is752.h
include_linuxdir = $(includedir)/linux
include_linux_HEADERS =
include_linux_HEADERS += include/linux/i2c.h
@@ -33,6 +37,9 @@ libdev_a_SOURCES += i2c/i2c-bus.c
libdev_a_SOURCES += i2c/i2c-dev.c
libdev_a_SOURCES += i2c/switch-nxp-pca9548a.c
libdev_a_SOURCES += spi/spi-bus.c
libdev_a_SOURCES += serial/sc16is752.c
libdev_a_SOURCES += serial/sc16is752-spi.c
libdev_a_SOURCES += serial/sc16is752-regs.h
include $(srcdir)/preinstall.am
include $(top_srcdir)/automake/local.am
File diff suppressed because it is too large. Load diff
+9
View File
@@ -48,6 +48,15 @@ $(PROJECT_INCLUDE)/dev/spi/spi.h: include/dev/spi/spi.h $(PROJECT_INCLUDE)/dev/s
$(INSTALL_DATA) $< $(PROJECT_INCLUDE)/dev/spi/spi.h
PREINSTALL_FILES += $(PROJECT_INCLUDE)/dev/spi/spi.h
$(PROJECT_INCLUDE)/dev/serial/$(dirstamp):
@$(MKDIR_P) $(PROJECT_INCLUDE)/dev/serial
@: > $(PROJECT_INCLUDE)/dev/serial/$(dirstamp)
PREINSTALL_DIRS += $(PROJECT_INCLUDE)/dev/serial/$(dirstamp)
$(PROJECT_INCLUDE)/dev/serial/sc16is752.h: include/dev/serial/sc16is752.h $(PROJECT_INCLUDE)/dev/serial/$(dirstamp)
$(INSTALL_DATA) $< $(PROJECT_INCLUDE)/dev/serial/sc16is752.h
PREINSTALL_FILES += $(PROJECT_INCLUDE)/dev/serial/sc16is752.h
$(PROJECT_INCLUDE)/linux/$(dirstamp):
@$(MKDIR_P) $(PROJECT_INCLUDE)/linux
@: > $(PROJECT_INCLUDE)/linux/$(dirstamp)
+116
View File
@@ -0,0 +1,116 @@
/*
* Copyright (c) 2016 embedded brains GmbH. All rights reserved.
*
* embedded brains GmbH
* Dornierstr. 4
* 82178 Puchheim
* Germany
* <info@embedded-brains.de>
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rtems.org/license/LICENSE.
*/
#ifndef LIBBSP_ARM_ATSAM_SC16IS752_H
#define LIBBSP_ARM_ATSAM_SC16IS752_H
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
/* General register set */
#define SC16IS752_RHR 0x0
#define SC16IS752_THR 0x0
#define SC16IS752_IER 0x1
#define SC16IS752_FCR 0x2
#define SC16IS752_IIR 0x2
#define SC16IS752_LCR 0x3
#define SC16IS752_MCR 0x4
#define SC16IS752_LSR 0x5
#define SC16IS752_MSR 0x6
#define SC16IS752_TCR 0x6
#define SC16IS752_SPR 0x7
#define SC16IS752_TLR 0x7
#define SC16IS752_TXLVL 0x8
#define SC16IS752_RXLVL 0x9
#define SC16IS752_IODIR 0xA
#define SC16IS752_IOSTATE 0xB
#define SC16IS752_IOINTENA 0xC
#define SC16IS752_IOCONTROL 0xE
#define SC16IS752_EFCR 0xF
/* Special register set */
#define SC16IS752_DLL 0x0
#define SC16IS752_DLH 0x1
/* Enhanced register set */
#define SC16IS752_EFR 0x2
#define SC16IS752_XON1 0x4
#define SC16IS752_XON2 0x5
#define SC16IS752_XOFF1 0x6
#define SC16IS752_XOFF2 0x7
/* FCR */
#define FCR_FIFO_EN 0x01
#define FCR_RX_FIFO_RST 0x02
#define FCR_TX_FIFO_RST 0x04
#define FCR_TX_FIFO_TRG_8 0x00
#define FCR_TX_FIFO_TRG_16 0x10
#define FCR_TX_FIFO_TRG_32 0x20
#define FCR_TX_FIFO_TRG_56 0x30
#define FCR_RX_FIFO_TRG_8 0x00
#define FCR_RX_FIFO_TRG_16 0x40
#define FCR_RX_FIFO_TRG_56 0x80
#define FCR_RX_FIFO_TRG_60 0xc0
/* EFCR */
#define EFCR_RS485_ENABLE (1u << 0)
#define EFCR_RX_DISABLE (1u << 1)
#define EFCR_TX_DISABLE (1u << 2)
/* IER */
#define IER_RHR (1u << 0)
#define IER_THR (1u << 1)
#define IER_RECEIVE_LINE_STATUS (1u << 2)
#define IER_MODEM_STATUS (1u << 3)
#define IER_SLEEP_MODE (1u << 4)
#define IER_XOFF (1u << 5)
#define IER_RTS (1u << 6)
#define IER_CTS (1u << 7)
/* IIR */
#define IIR_TX_INTERRUPT (1u << 1)
#define IIR_RX_INTERRUPT (1u << 2)
/* LCR */
#define LCR_CHRL_5_BIT (0u << 1) | (0u << 0)
#define LCR_CHRL_6_BIT (0u << 1) | (1u << 0)
#define LCR_CHRL_7_BIT (1u << 1) | (0u << 0)
#define LCR_CHRL_8_BIT (1u << 1) | (1u << 0)
#define LCR_2_STOP_BIT (1u << 2)
#define LCR_SET_PARITY (1u << 3)
#define LCR_EVEN_PARITY (1u << 4)
#define LCR_ENABLE_DIVISOR (1u << 7)
/* LSR */
#define LSR_TXEMPTY (1u << 5)
#define LSR_RXRDY (1u << 0)
#define LSR_ERROR_BITS (7u << 2)
/* MCR */
#define MCR_PRESCALE_NEEDED (1u << 0)
/* EFR */
#define EFR_ENHANCED_FUNC_ENABLE (1u << 4)
#define EFR_SPECIAL_CHAR_DETECT (1u << 5)
#define EFR_RTS_FLOW_CTRL_EN (1u << 6)
#define EFR_CTS_FLOW_CTRL_EN (1u << 7)
#define SC16IS752_FIFO_DEPTH 64
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* LIBBSP_ARM_ATSAM_SC16IS752_H */
+151
View File
@@ -0,0 +1,151 @@
/*
* Copyright (c) 2016 embedded brains GmbH. All rights reserved.
*
* embedded brains GmbH
* Dornierstr. 4
* 82178 Puchheim
* Germany
* <info@embedded-brains.de>
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rtems.org/license/LICENSE.
*/
#include <dev/serial/sc16is752.h>
#include <assert.h>
#include <fcntl.h>
#include <inttypes.h>
#include <unistd.h>
#include <dev/spi/spi.h>
#include "sc16is752-regs.h"
static void msg_init(
struct spi_ioc_transfer *msg,
uint8_t cs,
uint32_t speed,
size_t len,
uint8_t *rx_buf,
const uint8_t *tx_buf
)
{
msg->rx_buf = rx_buf;
msg->tx_buf = tx_buf;
msg->len = len;
msg->speed_hz = speed;
msg->delay_usecs = 1;
msg->bits_per_word = 8;
msg->cs_change = 0;
msg->rx_nbits = 0;
msg->tx_nbits = 0;
msg->mode = 0;
msg->cs = cs;
}
static int sc16is752_spi_write_reg(
sc16is752_context *base,
uint8_t addr,
const uint8_t *data,
size_t len
)
{
sc16is752_spi_context *ctx = (sc16is752_spi_context *) base;
spi_ioc_transfer msg[2];
uint8_t unused[SC16IS752_FIFO_DEPTH];
uint8_t tx_cmd;
_Assert(len < RTEMS_ARRAY_SIZE(unused));
msg_init(&msg[0], ctx->cs, ctx->speed_hz, 1, &unused[0], &tx_cmd);
msg_init(&msg[1], ctx->cs, ctx->speed_hz, len, &unused[0], &data[0]);
tx_cmd = addr << 3;
msg[1].cs_change = 1;
return ioctl(ctx->fd, SPI_IOC_MESSAGE(2), &msg[0]);
}
static int sc16is752_spi_read_reg(
sc16is752_context *base,
uint8_t addr,
uint8_t *data,
size_t len
)
{
sc16is752_spi_context *ctx = (sc16is752_spi_context *) base;
spi_ioc_transfer msg[2];
uint8_t unused;
uint8_t tx_cmd;
uint8_t tx_buf[SC16IS752_FIFO_DEPTH];
_Assert(len < RTEMS_ARRAY_SIZE(tx_buf));
msg_init(&msg[0], ctx->cs, ctx->speed_hz, 1, &unused, &tx_cmd);
msg_init(&msg[1], ctx->cs, ctx->speed_hz, len, &data[0], &tx_buf[0]);
tx_cmd = 0x80 | (addr << 3);
msg[1].cs_change = 1;
return ioctl(ctx->fd, SPI_IOC_MESSAGE(2), &msg[0]);
}
static int sc16is752_spi_read_2_reg(
sc16is752_context *base,
uint8_t addr_0,
uint8_t addr_1,
uint8_t data[2]
)
{
sc16is752_spi_context *ctx = (sc16is752_spi_context *) base;
spi_ioc_transfer msg[4];
uint8_t unused;
uint8_t tx_cmd_0;
uint8_t tx_cmd_1;
msg_init(&msg[0], ctx->cs, ctx->speed_hz, 1, &unused, &tx_cmd_0);
msg_init(&msg[1], ctx->cs, ctx->speed_hz, 1, &data[0], &unused);
msg_init(&msg[2], ctx->cs, ctx->speed_hz, 1, &unused, &tx_cmd_1);
msg_init(&msg[3], ctx->cs, ctx->speed_hz, 1, &data[1], &unused);
tx_cmd_0 = 0x80 | (addr_0 << 3);
tx_cmd_1 = 0x80 | (addr_1 << 3);
msg[1].cs_change = 1;
msg[3].cs_change = 1;
return ioctl(ctx->fd, SPI_IOC_MESSAGE(4), &msg[0]);
}
static bool sc16is752_spi_first_open(sc16is752_context *base)
{
sc16is752_spi_context *ctx = (sc16is752_spi_context *) base;
ctx->fd = open(ctx->spi_path, O_RDWR);
return ctx->fd >= 0;
}
static void sc16is752_spi_last_close(sc16is752_context *base)
{
sc16is752_spi_context *ctx = (sc16is752_spi_context *) base;
close(ctx->fd);
}
rtems_status_code sc16is752_spi_create(
sc16is752_spi_context *ctx,
const char *device_path
)
{
ctx->base.write_reg = sc16is752_spi_write_reg;
ctx->base.read_reg = sc16is752_spi_read_reg;
ctx->base.read_2_reg = sc16is752_spi_read_2_reg;
ctx->base.first_open = sc16is752_spi_first_open;
ctx->base.last_close = sc16is752_spi_last_close;
return rtems_termios_device_install(
device_path,
&sc16is752_termios_handler,
NULL,
&ctx->base.base
);
}
File diff suppressed because it is too large. Load diff