drv_uart.c 6.2 KB
Newer Older
G
Grissiom 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
/*
 * File      : uart.c
 * This file is part of RT-Thread RTOS
 * COPYRIGHT (C) 2013, RT-Thread Develop Team
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://openlab.rt-thread.com/license/LICENSE
 *
 * Change Logs:
 * Date           Author       Notes
 * 2013-05-27     Grissiom     port to RM48x50
 */

/* welcome, if you open this file, you may want to see uart driver code.
 * However, TI call it Serial Communication Interface(SCI) and all the low
 * level API is prefixed by "sci". To avoid messive renaming, I want to keep
 * with TI and call all the things SCI. You could safely substitude the word
 * "sci" with "uart". Enjoy. */

#include <rthw.h>
#include <rtthread.h>
#include <rtdevice.h>

#include <reg_sci.h>

/* bring from sci.h */
enum sciIntFlags
{
    SCI_FE_INT    = 0x04000000U,  /* framing error */
    SCI_OE_INT    = 0x02000000U,  /* overrun error */
    SCI_PE_INT    = 0x01000000U,  /* parity error */
    SCI_RX_INT    = 0x00000200U,  /* receive buffer ready */
    SCI_TX_INT    = 0x00000100U,  /* transmit buffer ready */
    SCI_WAKE_INT  = 0x00000002U,  /* wakeup */
    SCI_BREAK_INT = 0x00000001U   /* break detect */
};

/* LIN1 High level interrupt. Change this if you set a different channel in
 * HALCoGen. */
G
Grissiom 已提交
41
#define SCI_INT_VEC 14
G
Grissiom 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178

#define VCLK_HZ 100000000L

static rt_err_t _configure(struct rt_serial_device *serial, struct serial_configure *cfg)
{
    /** - global control 1 */
    rt_uint32_t gcr1 = (1U << 25U)  /* enable transmit */
                       | (1U << 24U)  /* enable receive */
                       | (1U << 5U)   /* internal clock (device has no clock pin) */
                       | (1U << 1U);  /* asynchronous timing mode */
    if (cfg->stop_bits == STOP_BITS_2)
        gcr1 |= (1U << 4U);  /* number of stop bits */
    else if (cfg->stop_bits != STOP_BITS_1)
        return -RT_ERROR;

    if (cfg->parity == PARITY_EVEN)
    {
        gcr1 |= (1U << 3U) | (1U << 2U);
    }
    else if (cfg->parity == PARITY_ODD)
    {
        gcr1 |= (0U << 3U) | (1U << 2U);
    }

    /** - bring SCI out of reset */
    scilinREG->GCR0 = 1U;

    /** - Disable all interrupts */
    scilinREG->CLRINT    = 0xFFFFFFFFU;
    scilinREG->CLRINTLVL = 0xFFFFFFFFU;

    scilinREG->GCR1 = gcr1;

    /** - set baudrate */
    scilinREG->BRS = VCLK_HZ/16/cfg->baud_rate - 1;  /* baudrate */

    /** - transmission length */
    scilinREG->FORMAT = cfg->data_bits - 1;  /* length */

    /** - set SCI pins functional mode */
    scilinREG->FUN = (1U << 2U)  /* tx pin */
                 | (1U << 1U)  /* rx pin */
                 | (0U);  /* clk pin */

    /** - set SCI pins default output value */
    scilinREG->DOUT = (0U << 2U)  /* tx pin */
                  | (0U << 1U)  /* rx pin */
                  | (0U);  /* clk pin */

    /** - set SCI pins output direction */
    scilinREG->DIR = (0U << 2U)  /* tx pin */
                 | (0U << 1U)  /* rx pin */
                 | (0U);  /* clk pin */

    /** - set SCI pins open drain enable */
    scilinREG->ODR = (0U << 2U)  /* tx pin */
                 | (0U << 1U)  /* rx pin */
                 | (0U);  /* clk pin */

    /** - set SCI pins pullup/pulldown enable */
    scilinREG->PD = (0U << 2U)  /* tx pin */
                | (0U << 1U)  /* rx pin */
                | (0U);  /* clk pin */

    /** - set SCI pins pullup/pulldown select */
    scilinREG->PSL = (1U << 2U)  /* tx pin */
                 | (1U << 1U)  /* rx pin */
                 | (1U);  /* clk pin */

    /** - set interrupt level */
    scilinREG->SETINTLVL = (0U << 26U)  /* Framing error */
                       | (0U << 25U)  /* Overrun error */
                       | (0U << 24U)  /* Parity error */
                       | (0U << 9U)  /* Receive */
                       | (0U << 8U)  /* Transmit */
                       | (0U << 1U)  /* Wakeup */
                       | (0U);  /* Break detect */

    /** - set interrupt enable */
    scilinREG->SETINT = (0U << 26U)  /* Framing error */
                    | (0U << 25U)  /* Overrun error */
                    | (0U << 24U)  /* Parity error */
                    | (1U << 9U)  /* Receive */
                    | (0U << 1U)  /* Wakeup */
                    | (0U);  /* Break detect */

    /** - Finaly start SCILIN */
    scilinREG->GCR1 |= (1U << 7U);

    return RT_EOK;
}

static rt_err_t _control(struct rt_serial_device *serial, int cmd, void *arg)
{
    sciBASE_t *sci = (sciBASE_t*)serial->parent.user_data;

    switch (cmd)
    {
    case RT_DEVICE_CTRL_CLR_INT:
        /* disable rx irq */
        sci->CLRINT = SCI_RX_INT;
        break;
    case RT_DEVICE_CTRL_SET_INT:
        /* enable rx irq */
        sci->SETINT = SCI_RX_INT;
        break;
    }

    return RT_EOK;
}

static int _putc(struct rt_serial_device *serial, char c)
{
    sciBASE_t *sci = (sciBASE_t*)serial->parent.user_data;
    while ((sci->FLR & SCI_TX_INT) == 0U)
        ;
    sci->TD = c;
    return 1;
}

static int _getc(struct rt_serial_device *serial)
{
    sciBASE_t *sci = (sciBASE_t*)serial->parent.user_data;
    if (sci->FLR & (1<<9))
        return (sci->RD & 0x000000FFU);
    else
        return -1;
}

static const struct rt_uart_ops _sci_ops =
{
    _configure,
    _control,
    _putc,
    _getc,
};

G
Grissiom 已提交
179 180 181 182 183
static void _irq_wrapper(int vector, void *param)
{
    rt_hw_serial_isr((struct rt_serial_device*)param);
}

G
Grissiom 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
static struct rt_serial_device _sci2_serial;
static struct serial_ringbuffer _sci2_int_rx;

void rt_hw_uart_init(void)
{
    struct serial_configure config;

    /* fake configuration */
    config.baud_rate = BAUD_RATE_115200;
    config.bit_order = BIT_ORDER_LSB;
    config.data_bits = DATA_BITS_8;
    config.parity    = PARITY_NONE;
    config.stop_bits = STOP_BITS_1;
    config.invert    = NRZ_NORMAL;

    _sci2_serial.ops    = &_sci_ops;
    _sci2_serial.int_rx = &_sci2_int_rx;
    _sci2_serial.config = config;

    rt_hw_serial_register(&_sci2_serial, "sci2",
                          RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
                          (void*)scilinREG);

G
Grissiom 已提交
207 208
    rt_device_control(&_sci2_serial.parent, RT_DEVICE_CTRL_SET_INT, 0);
    rt_hw_interrupt_install(SCI_INT_VEC, _irq_wrapper, &_sci2_serial, "sci2");
G
Grissiom 已提交
209 210
    rt_hw_interrupt_umask(SCI_INT_VEC);
}