zephyr/drivers/serial/uart_msp432p4xx.c

359 lines
9.1 KiB
C
Raw Normal View History

/*
* Copyright (c) 2017, Linaro Ltd
*
* SPDX-License-Identifier: Apache-2.0
*/
#define DT_DRV_COMPAT ti_msp432p4xx_uart
/* See www.ti.com/lit/pdf/slau356f, Chapter 22, for MSP432P4XX UART info. */
/* include driverlib/gpio.h (from the msp432p4xx SDK) before Z's uart.h so
* that the definition of BIT is not overridden */
#include <driverlib/gpio.h>
#include <drivers/uart.h>
/* Driverlib includes */
#include <driverlib/rom.h>
#include <driverlib/rom_map.h>
#include <driverlib/uart.h>
struct uart_msp432p4xx_dev_data_t {
/* UART config structure */
eUSCI_UART_Config uartConfig;
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
uart_irq_callback_user_data_t cb; /**< Callback function pointer */
void *cb_data; /**< Callback function arg */
#endif /* CONFIG_UART_INTERRUPT_DRIVEN */
};
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
isr: Normalize usage of device instance through ISR The goal of this patch is to replace the 'void *' parameter by 'struct device *' if they use such variable or just 'const void *' on all relevant ISRs This will avoid not-so-nice const qualifier tweaks when device instances will be constant. Note that only the ISR passed to IRQ_CONNECT are of interest here. In order to do so, the script fix_isr.py below is necessary: from pathlib import Path import subprocess import pickle import mmap import sys import re import os cocci_template = """ @r_fix_isr_0 @ type ret_type; identifier P; identifier D; @@ -ret_type <!fn!>(void *P) +ret_type <!fn!>(const struct device *P) { ... ( const struct device *D = (const struct device *)P; | const struct device *D = P; ) ... } @r_fix_isr_1 @ type ret_type; identifier P; identifier D; @@ -ret_type <!fn!>(void *P) +ret_type <!fn!>(const struct device *P) { ... const struct device *D; ... ( D = (const struct device *)P; | D = P; ) ... } @r_fix_isr_2 @ type ret_type; identifier A; @@ -ret_type <!fn!>(void *A) +ret_type <!fn!>(const void *A) { ... } @r_fix_isr_3 @ const struct device *D; @@ -<!fn!>((void *)D); +<!fn!>(D); @r_fix_isr_4 @ type ret_type; identifier D; identifier P; @@ -ret_type <!fn!>(const struct device *P) +ret_type <!fn!>(const struct device *D) { ... ( -const struct device *D = (const struct device *)P; | -const struct device *D = P; ) ... } @r_fix_isr_5 @ type ret_type; identifier D; identifier P; @@ -ret_type <!fn!>(const struct device *P) +ret_type <!fn!>(const struct device *D) { ... -const struct device *D; ... ( -D = (const struct device *)P; | -D = P; ) ... } """ def find_isr(fn): db = [] data = None start = 0 try: with open(fn, 'r+') as f: data = str(mmap.mmap(f.fileno(), 0).read()) except Exception as e: return db while True: isr = "" irq = data.find('IRQ_CONNECT', start) while irq > -1: p = 1 arg = 1 p_o = data.find('(', irq) if p_o < 0: irq = -1 break; pos = p_o + 1 while p > 0: if data[pos] == ')': p -= 1 elif data[pos] == '(': p += 1 elif data[pos] == ',' and p == 1: arg += 1 if arg == 3: isr += data[pos] pos += 1 isr = isr.strip(',\\n\\t ') if isr not in db and len(isr) > 0: db.append(isr) start = pos break if irq < 0: break return db def patch_isr(fn, isr_list): if len(isr_list) <= 0: return for isr in isr_list: tmplt = cocci_template.replace('<!fn!>', isr) with open('/tmp/isr_fix.cocci', 'w') as f: f.write(tmplt) cmd = ['spatch', '--sp-file', '/tmp/isr_fix.cocci', '--in-place', fn] subprocess.run(cmd) def process_files(path): if path.is_file() and path.suffix in ['.h', '.c']: p = str(path.parent) + '/' + path.name isr_list = find_isr(p) patch_isr(p, isr_list) elif path.is_dir(): for p in path.iterdir(): process_files(p) if len(sys.argv) < 2: print("You need to provide a dir/file path") sys.exit(1) process_files(Path(sys.argv[1])) And is run: ./fix_isr.py <zephyr root directory> Finally, some files needed manual fixes such. Fixes #27399 Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
2020-06-17 14:58:56 +02:00
static void uart_msp432p4xx_isr(const struct device *dev);
#endif
static const struct uart_device_config uart_msp432p4xx_dev_cfg_0 = {
.base = (void *)DT_INST_REG_ADDR(0),
};
static struct uart_msp432p4xx_dev_data_t uart_msp432p4xx_dev_data_0 = {
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
.cb = NULL,
#endif
};
static int baudrate_set(eUSCI_UART_Config *config, uint32_t baudrate)
{
uint16_t prescalar;
uint8_t first_mod_reg, second_mod_reg;
switch (baudrate) {
case 1200:
prescalar = 2500U;
first_mod_reg = 0U;
second_mod_reg = 0U;
break;
case 2400:
prescalar = 1250U;
first_mod_reg = 0U;
second_mod_reg = 0U;
break;
case 4800:
prescalar = 625U;
first_mod_reg = 0U;
second_mod_reg = 0U;
break;
case 9600:
prescalar = 312U;
first_mod_reg = 8U;
second_mod_reg = 0U;
break;
case 19200:
prescalar = 156U;
first_mod_reg = 4U;
second_mod_reg = 0U;
break;
case 38400:
prescalar = 78U;
first_mod_reg = 2U;
second_mod_reg = 0U;
break;
case 57600:
prescalar = 52U;
first_mod_reg = 1U;
second_mod_reg = 37U;
break;
case 115200:
prescalar = 26U;
first_mod_reg = 0U;
second_mod_reg = 111U;
break;
case 230400:
prescalar = 13U;
first_mod_reg = 0U;
second_mod_reg = 37U;
break;
case 460800:
prescalar = 6U;
first_mod_reg = 8U;
second_mod_reg = 32U;
break;
default:
return -EINVAL;
}
config->clockPrescalar = prescalar;
config->firstModReg = first_mod_reg;
config->secondModReg = second_mod_reg;
return 0;
}
static int uart_msp432p4xx_init(const struct device *dev)
{
int err;
const struct uart_device_config *config = dev->config;
eUSCI_UART_Config UartConfig;
/* Select P1.2 and P1.3 in UART mode */
MAP_GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P1,
(GPIO_PIN2 | GPIO_PIN3), GPIO_PRIMARY_MODULE_FUNCTION);
UartConfig.selectClockSource = EUSCI_A_UART_CLOCKSOURCE_SMCLK;
UartConfig.parity = EUSCI_A_UART_NO_PARITY;
UartConfig.msborLsbFirst = EUSCI_A_UART_LSB_FIRST;
UartConfig.numberofStopBits = EUSCI_A_UART_ONE_STOP_BIT;
UartConfig.uartMode = EUSCI_A_UART_MODE;
UartConfig.overSampling = EUSCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION;
/* Baud rate settings calculated for 48MHz */
err = baudrate_set(&UartConfig, DT_INST_PROP(0, current_speed));
if (err) {
return err;
}
/* Configure UART Module */
MAP_UART_initModule((unsigned long)config->base, &UartConfig);
/* Enable UART module */
MAP_UART_enableModule((unsigned long)config->base);
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
IRQ_CONNECT(DT_INST_IRQN(0),
DT_INST_IRQ(0, priority),
uart_msp432p4xx_isr, DEVICE_DT_INST_GET(0),
0);
irq_enable(DT_INST_IRQN(0));
#endif
return 0;
}
static int uart_msp432p4xx_poll_in(const struct device *dev, unsigned char *c)
{
const struct uart_device_config *config = dev->config;
*c = MAP_UART_receiveData((unsigned long)config->base);
return 0;
}
static void uart_msp432p4xx_poll_out(const struct device *dev,
unsigned char c)
{
const struct uart_device_config *config = dev->config;
MAP_UART_transmitData((unsigned long)config->base, c);
}
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
static int uart_msp432p4xx_fifo_fill(const struct device *dev,
const uint8_t *tx_data, int size)
{
const struct uart_device_config *config = dev->config;
unsigned int num_tx = 0U;
while ((size - num_tx) > 0) {
MAP_UART_transmitData((unsigned long)config->base,
tx_data[num_tx]);
if (MAP_UART_getInterruptStatus((unsigned long)config->base,
EUSCI_A_UART_TRANSMIT_COMPLETE_INTERRUPT_FLAG)) {
num_tx++;
} else {
break;
}
}
return (int)num_tx;
}
static int uart_msp432p4xx_fifo_read(const struct device *dev,
uint8_t *rx_data,
const int size)
{
const struct uart_device_config *config = dev->config;
unsigned int num_rx = 0U;
while (((size - num_rx) > 0) &&
MAP_UART_getInterruptStatus((unsigned long)config->base,
EUSCI_A_UART_RECEIVE_INTERRUPT_FLAG)) {
rx_data[num_rx++] =
MAP_UART_receiveData((unsigned long)config->base);
}
return num_rx;
}
static void uart_msp432p4xx_irq_tx_enable(const struct device *dev)
{
const struct uart_device_config *config = dev->config;
MAP_UART_enableInterrupt((unsigned long)config->base,
EUSCI_A_UART_TRANSMIT_INTERRUPT);
}
static void uart_msp432p4xx_irq_tx_disable(const struct device *dev)
{
const struct uart_device_config *config = dev->config;
MAP_UART_disableInterrupt((unsigned long)config->base,
EUSCI_A_UART_TRANSMIT_INTERRUPT);
}
static int uart_msp432p4xx_irq_tx_ready(const struct device *dev)
{
const struct uart_device_config *config = dev->config;
unsigned int int_status;
int_status = MAP_UART_getInterruptStatus((unsigned long)config->base,
EUSCI_A_UART_TRANSMIT_INTERRUPT_FLAG);
return (int_status & EUSCI_A_IE_TXIE);
}
static void uart_msp432p4xx_irq_rx_enable(const struct device *dev)
{
const struct uart_device_config *config = dev->config;
MAP_UART_enableInterrupt((unsigned long)config->base,
EUSCI_A_UART_RECEIVE_INTERRUPT);
}
static void uart_msp432p4xx_irq_rx_disable(const struct device *dev)
{
const struct uart_device_config *config = dev->config;
MAP_UART_disableInterrupt((unsigned long)config->base,
EUSCI_A_UART_RECEIVE_INTERRUPT);
}
static int uart_msp432p4xx_irq_tx_complete(const struct device *dev)
{
const struct uart_device_config *config = dev->config;
return MAP_UART_getInterruptStatus((unsigned long)config->base,
EUSCI_A_UART_TRANSMIT_COMPLETE_INTERRUPT_FLAG);
}
static int uart_msp432p4xx_irq_rx_ready(const struct device *dev)
{
const struct uart_device_config *config = dev->config;
unsigned int int_status;
int_status = MAP_UART_getInterruptStatus((unsigned long)config->base,
EUSCI_A_UART_RECEIVE_INTERRUPT_FLAG);
return (int_status & EUSCI_A_IE_RXIE);
}
static void uart_msp432p4xx_irq_err_enable(const struct device *dev)
{
/* Not yet used in zephyr */
}
static void uart_msp432p4xx_irq_err_disable(const struct device *dev)
{
/* Not yet used in zephyr */
}
static int uart_msp432p4xx_irq_is_pending(const struct device *dev)
{
const struct uart_device_config *config = dev->config;
unsigned int int_status;
int_status = MAP_UART_getEnabledInterruptStatus(
(unsigned long)config->base);
return (int_status & (EUSCI_A_IE_TXIE | EUSCI_A_IE_RXIE));
}
static int uart_msp432p4xx_irq_update(const struct device *dev)
{
return 1;
}
static void uart_msp432p4xx_irq_callback_set(const struct device *dev,
uart_irq_callback_user_data_t cb,
void *cb_data)
{
struct uart_msp432p4xx_dev_data_t * const dev_data = dev->data;
dev_data->cb = cb;
dev_data->cb_data = cb_data;
}
/**
* @brief Interrupt service routine.
*
* This simply calls the callback function, if one exists.
*
* @param arg Argument to ISR.
*/
isr: Normalize usage of device instance through ISR The goal of this patch is to replace the 'void *' parameter by 'struct device *' if they use such variable or just 'const void *' on all relevant ISRs This will avoid not-so-nice const qualifier tweaks when device instances will be constant. Note that only the ISR passed to IRQ_CONNECT are of interest here. In order to do so, the script fix_isr.py below is necessary: from pathlib import Path import subprocess import pickle import mmap import sys import re import os cocci_template = """ @r_fix_isr_0 @ type ret_type; identifier P; identifier D; @@ -ret_type <!fn!>(void *P) +ret_type <!fn!>(const struct device *P) { ... ( const struct device *D = (const struct device *)P; | const struct device *D = P; ) ... } @r_fix_isr_1 @ type ret_type; identifier P; identifier D; @@ -ret_type <!fn!>(void *P) +ret_type <!fn!>(const struct device *P) { ... const struct device *D; ... ( D = (const struct device *)P; | D = P; ) ... } @r_fix_isr_2 @ type ret_type; identifier A; @@ -ret_type <!fn!>(void *A) +ret_type <!fn!>(const void *A) { ... } @r_fix_isr_3 @ const struct device *D; @@ -<!fn!>((void *)D); +<!fn!>(D); @r_fix_isr_4 @ type ret_type; identifier D; identifier P; @@ -ret_type <!fn!>(const struct device *P) +ret_type <!fn!>(const struct device *D) { ... ( -const struct device *D = (const struct device *)P; | -const struct device *D = P; ) ... } @r_fix_isr_5 @ type ret_type; identifier D; identifier P; @@ -ret_type <!fn!>(const struct device *P) +ret_type <!fn!>(const struct device *D) { ... -const struct device *D; ... ( -D = (const struct device *)P; | -D = P; ) ... } """ def find_isr(fn): db = [] data = None start = 0 try: with open(fn, 'r+') as f: data = str(mmap.mmap(f.fileno(), 0).read()) except Exception as e: return db while True: isr = "" irq = data.find('IRQ_CONNECT', start) while irq > -1: p = 1 arg = 1 p_o = data.find('(', irq) if p_o < 0: irq = -1 break; pos = p_o + 1 while p > 0: if data[pos] == ')': p -= 1 elif data[pos] == '(': p += 1 elif data[pos] == ',' and p == 1: arg += 1 if arg == 3: isr += data[pos] pos += 1 isr = isr.strip(',\\n\\t ') if isr not in db and len(isr) > 0: db.append(isr) start = pos break if irq < 0: break return db def patch_isr(fn, isr_list): if len(isr_list) <= 0: return for isr in isr_list: tmplt = cocci_template.replace('<!fn!>', isr) with open('/tmp/isr_fix.cocci', 'w') as f: f.write(tmplt) cmd = ['spatch', '--sp-file', '/tmp/isr_fix.cocci', '--in-place', fn] subprocess.run(cmd) def process_files(path): if path.is_file() and path.suffix in ['.h', '.c']: p = str(path.parent) + '/' + path.name isr_list = find_isr(p) patch_isr(p, isr_list) elif path.is_dir(): for p in path.iterdir(): process_files(p) if len(sys.argv) < 2: print("You need to provide a dir/file path") sys.exit(1) process_files(Path(sys.argv[1])) And is run: ./fix_isr.py <zephyr root directory> Finally, some files needed manual fixes such. Fixes #27399 Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
2020-06-17 14:58:56 +02:00
static void uart_msp432p4xx_isr(const struct device *dev)
{
const struct uart_device_config *config = dev->config;
struct uart_msp432p4xx_dev_data_t * const dev_data = dev->data;
unsigned int int_status;
int_status = MAP_UART_getEnabledInterruptStatus(
(unsigned long)config->base);
if (dev_data->cb) {
dev_data->cb(dev, dev_data->cb_data);
}
/*
* Clear interrupts only after cb called, as Zephyr UART clients expect
* to check interrupt status during the callback.
*/
MAP_UART_disableInterrupt((unsigned long)config->base, int_status);
}
#endif /* CONFIG_UART_INTERRUPT_DRIVEN */
static const struct uart_driver_api uart_msp432p4xx_driver_api = {
.poll_in = uart_msp432p4xx_poll_in,
.poll_out = uart_msp432p4xx_poll_out,
#ifdef CONFIG_UART_INTERRUPT_DRIVEN
.fifo_fill = uart_msp432p4xx_fifo_fill,
.fifo_read = uart_msp432p4xx_fifo_read,
.irq_tx_enable = uart_msp432p4xx_irq_tx_enable,
.irq_tx_disable = uart_msp432p4xx_irq_tx_disable,
.irq_tx_ready = uart_msp432p4xx_irq_tx_ready,
.irq_rx_enable = uart_msp432p4xx_irq_rx_enable,
.irq_rx_disable = uart_msp432p4xx_irq_rx_disable,
.irq_tx_complete = uart_msp432p4xx_irq_tx_complete,
.irq_rx_ready = uart_msp432p4xx_irq_rx_ready,
.irq_err_enable = uart_msp432p4xx_irq_err_enable,
.irq_err_disable = uart_msp432p4xx_irq_err_disable,
.irq_is_pending = uart_msp432p4xx_irq_is_pending,
.irq_update = uart_msp432p4xx_irq_update,
.irq_callback_set = uart_msp432p4xx_irq_callback_set,
#endif /* CONFIG_UART_INTERRUPT_DRIVEN */
};
DEVICE_DT_INST_DEFINE(0,
uart_msp432p4xx_init, NULL,
&uart_msp432p4xx_dev_data_0,
&uart_msp432p4xx_dev_cfg_0,
PRE_KERNEL_1, CONFIG_SERIAL_INIT_PRIORITY,
(void *)&uart_msp432p4xx_driver_api);