serial.c 40.4 KB
Newer Older
1
/*
dongly's avatar
dongly 已提交
2
 * Copyright (c) 2006-2022, RT-Thread Development Team
3
 *
4
 * SPDX-License-Identifier: Apache-2.0
5 6 7 8 9 10
 *
 * Change Logs:
 * Date           Author       Notes
 * 2006-03-13     bernard      first version
 * 2012-05-15     lgnq         modified according bernard's implementation.
 * 2012-05-28     bernard      code cleanup
11
 * 2012-11-23     bernard      fix compiler warning.
12 13
 * 2013-02-20     bernard      use RT_SERIAL_RB_BUFSZ to define
 *                             the size of ring buffer.
B
bernard 已提交
14
 * 2014-07-10     bernard      rewrite serial framework
B
bernard 已提交
15
 * 2014-12-31     bernard      use open_flag for poll_tx stream mode.
16
 * 2015-05-19     Quintin      fix DMA tx mod tx_dma->activated flag !=RT_FALSE BUG
17
 *                             in open function.
18
 * 2015-11-10     bernard      fix the poll rx issue when there is no data.
19
 * 2016-05-10     armink       add fifo mode to DMA rx when serial->config.bufsz != 0.
20
 * 2017-01-19     aubr.cool    prevent change serial rx bufsz when serial is opened.
21
 * 2017-11-07     JasonJia     fix data bits error issue when using tcsetattr.
J
JasonJiaJie 已提交
22 23
 * 2017-11-15     JasonJia     fix poll rx issue when data is full.
 *                             add TCFLSH and FIONREAD support.
24
 * 2018-12-08     Ernest Chen  add DMA choice
25
 * 2020-09-14     WillianChan  add a line feed to the carriage return character
W
WillianChan 已提交
26
 *                             when using interrupt tx
27 28
 * 2020-12-14     Meco Man     implement function of setting window's size(TIOCSWINSZ)
 * 2021-08-22     Meco Man     implement function of getting window's size(TIOCGWINSZ)
29 30 31 32 33 34
 */

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

35 36
#define DBG_TAG    "UART"
#define DBG_LVL    DBG_INFO
B
bernard 已提交
37 38
#include <rtdbg.h>

mysterywolf's avatar
mysterywolf 已提交
39
#ifdef RT_USING_POSIX_STDIO
40
#include <dfs_file.h>
mysterywolf's avatar
update  
mysterywolf 已提交
41
#include <fcntl.h>
42
#include <unistd.h>
mysterywolf's avatar
mysterywolf 已提交
43
#include <poll.h>
mysterywolf's avatar
mysterywolf 已提交
44
#include <sys/ioctl.h>
B
bernard 已提交
45

B
bernard 已提交
46
#ifdef RT_USING_POSIX_TERMIOS
47
#include <termios.h>
B
bernard 已提交
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
#endif

/* it's possible the 'getc/putc' is defined by stdio.h in gcc/newlib. */
#ifdef getc
#undef getc
#endif

#ifdef putc
#undef putc
#endif

static rt_err_t serial_fops_rx_ind(rt_device_t dev, rt_size_t size)
{
    rt_wqueue_wakeup(&(dev->wait_queue), (void*)POLLIN);

    return RT_EOK;
}

/* fops for serial */
static int serial_fops_open(struct dfs_fd *fd)
{
    rt_err_t ret = 0;
    rt_uint16_t flags = 0;
    rt_device_t device;

    device = (rt_device_t)fd->data;
    RT_ASSERT(device != RT_NULL);

    switch (fd->flags & O_ACCMODE)
    {
mysterywolf's avatar
mysterywolf 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
        case O_RDONLY:
            LOG_D("fops open: O_RDONLY!");
            flags = RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_RDONLY;
            break;
        case O_WRONLY:
            LOG_D("fops open: O_WRONLY!");
            flags = RT_DEVICE_FLAG_WRONLY;
            break;
        case O_RDWR:
            LOG_D("fops open: O_RDWR!");
            flags = RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_RDWR;
            break;
        default:
            LOG_E("fops open: unknown mode - %d!", fd->flags & O_ACCMODE);
            break;
B
bernard 已提交
93 94
    }

B
bernard 已提交
95 96
    if ((fd->flags & O_ACCMODE) != O_WRONLY)
        rt_device_set_rx_indicate(device, serial_fops_rx_ind);
B
bernard 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
    ret = rt_device_open(device, flags);
    if (ret == RT_EOK) return 0;

    return ret;
}

static int serial_fops_close(struct dfs_fd *fd)
{
    rt_device_t device;

    device = (rt_device_t)fd->data;

    rt_device_set_rx_indicate(device, RT_NULL);
    rt_device_close(device);

    return 0;
}

static int serial_fops_ioctl(struct dfs_fd *fd, int cmd, void *args)
{
    rt_device_t device;
118 119
    int flags = (int)(rt_base_t)args;
    int mask  = O_NONBLOCK | O_APPEND;
B
bernard 已提交
120 121 122 123 124 125 126 127

    device = (rt_device_t)fd->data;
    switch (cmd)
    {
    case FIONREAD:
        break;
    case FIONWRITE:
        break;
128 129 130 131 132
    case F_SETFL:
        flags &= mask;
        fd->flags &= ~mask;
        fd->flags |= flags;
        break;
B
bernard 已提交
133 134 135 136 137 138 139 140 141 142 143 144
    }

    return rt_device_control(device, cmd, args);
}

static int serial_fops_read(struct dfs_fd *fd, void *buf, size_t count)
{
    int size = 0;
    rt_device_t device;

    device = (rt_device_t)fd->data;

J
JasonJiaJie 已提交
145
    do
B
bernard 已提交
146
    {
mysterywolf's avatar
mysterywolf 已提交
147
        size = rt_device_read(device, -1, buf, count);
B
bernard 已提交
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
        if (size <= 0)
        {
            if (fd->flags & O_NONBLOCK)
            {
                size = -EAGAIN;
                break;
            }

            rt_wqueue_wait(&(device->wait_queue), 0, RT_WAITING_FOREVER);
        }
    }while (size <= 0);

    return size;
}

static int serial_fops_write(struct dfs_fd *fd, const void *buf, size_t count)
{
    rt_device_t device;

    device = (rt_device_t)fd->data;
    return rt_device_write(device, -1, buf, count);
}

static int serial_fops_poll(struct dfs_fd *fd, struct rt_pollreq *req)
{
    int mask = 0;
    int flags = 0;
    rt_device_t device;
    struct rt_serial_device *serial;
J
JasonJiaJie 已提交
177

B
bernard 已提交
178 179 180 181 182 183 184 185 186 187 188 189 190
    device = (rt_device_t)fd->data;
    RT_ASSERT(device != RT_NULL);

    serial = (struct rt_serial_device *)device;

    /* only support POLLIN */
    flags = fd->flags & O_ACCMODE;
    if (flags == O_RDONLY || flags == O_RDWR)
    {
        rt_base_t level;
        struct rt_serial_rx_fifo* rx_fifo;

        rt_poll_add(&(device->wait_queue), req);
J
JasonJiaJie 已提交
191

B
bernard 已提交
192 193 194
        rx_fifo = (struct rt_serial_rx_fifo*) serial->serial_rx;

        level = rt_hw_interrupt_disable();
J
JasonJiaJie 已提交
195
        if ((rx_fifo->get_index != rx_fifo->put_index) || (rx_fifo->get_index == rx_fifo->put_index && rx_fifo->is_full == RT_TRUE))
B
bernard 已提交
196 197 198 199 200 201 202
            mask |= POLLIN;
        rt_hw_interrupt_enable(level);
    }

    return mask;
}

dongly's avatar
dongly 已提交
203
static const struct dfs_file_ops _serial_fops =
B
bernard 已提交
204 205 206 207 208 209 210 211 212 213 214
{
    serial_fops_open,
    serial_fops_close,
    serial_fops_ioctl,
    serial_fops_read,
    serial_fops_write,
    RT_NULL, /* flush */
    RT_NULL, /* lseek */
    RT_NULL, /* getdents */
    serial_fops_poll,
};
mysterywolf's avatar
mysterywolf 已提交
215
#endif /* RT_USING_POSIX_STDIO */
B
bernard 已提交
216

B
bernard 已提交
217
/*
218
 * Serial poll routines
B
bernard 已提交
219 220
 */
rt_inline int _serial_poll_rx(struct rt_serial_device *serial, rt_uint8_t *data, int length)
221
{
222 223
    int ch;
    int size;
224

225 226 227 228 229 230
    RT_ASSERT(serial != RT_NULL);
    size = length;

    while (length)
    {
        ch = serial->ops->getc(serial);
231 232
        if (ch == -1) break;

233
        *data = ch;
234 235
        data ++; length --;

236 237 238 239
        if(serial->parent.open_flag & RT_DEVICE_FLAG_STREAM)
        {
            if (ch == '\n') break;
        }
240 241 242
    }

    return size - length;
243 244
}

B
bernard 已提交
245
rt_inline int _serial_poll_tx(struct rt_serial_device *serial, const rt_uint8_t *data, int length)
246
{
247 248 249 250 251 252 253 254 255 256
    int size;
    RT_ASSERT(serial != RT_NULL);

    size = length;
    while (length)
    {
        /*
         * to be polite with serial console add a line feed
         * to the carriage return character
         */
B
bernard 已提交
257
        if (*data == '\n' && (serial->parent.open_flag & RT_DEVICE_FLAG_STREAM))
258 259 260
        {
            serial->ops->putc(serial, '\r');
        }
261

262
        serial->ops->putc(serial, *data);
263

264 265 266 267 268
        ++ data;
        -- length;
    }

    return size - length;
269 270
}

B
bernard 已提交
271 272 273 274
/*
 * Serial interrupt routines
 */
rt_inline int _serial_int_rx(struct rt_serial_device *serial, rt_uint8_t *data, int length)
275
{
276 277 278 279
    int size;
    struct rt_serial_rx_fifo* rx_fifo;

    RT_ASSERT(serial != RT_NULL);
280 281
    size = length;

282 283 284 285 286 287 288 289 290 291 292
    rx_fifo = (struct rt_serial_rx_fifo*) serial->serial_rx;
    RT_ASSERT(rx_fifo != RT_NULL);

    /* read from software FIFO */
    while (length)
    {
        int ch;
        rt_base_t level;

        /* disable interrupt */
        level = rt_hw_interrupt_disable();
J
JasonJiaJie 已提交
293 294 295

        /* there's no data: */
        if ((rx_fifo->get_index == rx_fifo->put_index) && (rx_fifo->is_full == RT_FALSE))
296 297 298 299 300 301
        {
            /* no data, enable interrupt and break out */
            rt_hw_interrupt_enable(level);
            break;
        }

J
JasonJiaJie 已提交
302 303 304 305 306 307 308 309 310 311
        /* otherwise there's the data: */
        ch = rx_fifo->buffer[rx_fifo->get_index];
        rx_fifo->get_index += 1;
        if (rx_fifo->get_index >= serial->config.bufsz) rx_fifo->get_index = 0;

        if (rx_fifo->is_full == RT_TRUE)
        {
            rx_fifo->is_full = RT_FALSE;
        }

312 313 314 315 316 317 318 319
        /* enable interrupt */
        rt_hw_interrupt_enable(level);

        *data = ch & 0xff;
        data ++; length --;
    }

    return size - length;
320 321
}

B
bernard 已提交
322
rt_inline int _serial_int_tx(struct rt_serial_device *serial, const rt_uint8_t *data, int length)
323
{
324 325
    int size;
    struct rt_serial_tx_fifo *tx;
326

327 328 329 330 331 332 333 334
    RT_ASSERT(serial != RT_NULL);

    size = length;
    tx = (struct rt_serial_tx_fifo*) serial->serial_tx;
    RT_ASSERT(tx != RT_NULL);

    while (length)
    {
335 336 337 338 339 340 341 342 343 344 345
        /*
         * to be polite with serial console add a line feed
         * to the carriage return character
         */
        if (*data == '\n' && (serial->parent.open_flag & RT_DEVICE_FLAG_STREAM))
        {
            if (serial->ops->putc(serial, '\r') == -1)
            {
                rt_completion_wait(&(tx->completion), RT_WAITING_FOREVER);
                continue;
            }
346
        }
347

348 349 350 351 352 353 354 355 356 357
        if (serial->ops->putc(serial, *(char*)data) == -1)
        {
            rt_completion_wait(&(tx->completion), RT_WAITING_FOREVER);
            continue;
        }

        data ++; length --;
    }

    return size - length;
358 359
}

360 361 362
static void _serial_check_buffer_size(void)
{
    static rt_bool_t already_output = RT_FALSE;
363

364 365
    if (already_output == RT_FALSE)
    {
366
#if !defined(RT_USING_ULOG) || defined(ULOG_USING_ISR_LOG)
367 368
        LOG_W("Warning: There is no enough buffer for saving data,"
              " please increase the RT_SERIAL_RB_BUFSZ option.");
369
#endif
370 371
        already_output = RT_TRUE;
    }
372
}
373

mysterywolf's avatar
mysterywolf 已提交
374
#if defined(RT_USING_POSIX_STDIO) || defined(RT_SERIAL_USING_DMA)
J
JasonJiaJie 已提交
375
static rt_size_t _serial_fifo_calc_recved_len(struct rt_serial_device *serial)
376 377
{
    struct rt_serial_rx_fifo *rx_fifo = (struct rt_serial_rx_fifo *) serial->serial_rx;
378 379 380

    RT_ASSERT(rx_fifo != RT_NULL);

J
JasonJiaJie 已提交
381 382 383 384
    if (rx_fifo->put_index == rx_fifo->get_index)
    {
        return (rx_fifo->is_full == RT_FALSE ? 0 : serial->config.bufsz);
    }
385 386
    else
    {
J
JasonJiaJie 已提交
387 388 389 390
        if (rx_fifo->put_index > rx_fifo->get_index)
        {
            return rx_fifo->put_index - rx_fifo->get_index;
        }
391
        else
J
JasonJiaJie 已提交
392 393 394 395 396
        {
            return serial->config.bufsz - (rx_fifo->get_index - rx_fifo->put_index);
        }
    }
}
mysterywolf's avatar
mysterywolf 已提交
397
#endif /* RT_USING_POSIX_STDIO || RT_SERIAL_USING_DMA */
J
JasonJiaJie 已提交
398

399
#ifdef RT_SERIAL_USING_DMA
J
JasonJiaJie 已提交
400 401 402 403 404 405 406 407 408 409
/**
 * Calculate DMA received data length.
 *
 * @param serial serial device
 *
 * @return length
 */
static rt_size_t rt_dma_calc_recved_len(struct rt_serial_device *serial)
{
    return _serial_fifo_calc_recved_len(serial);
410 411 412
}

/**
J
JasonJiaJie 已提交
413
 * Read data finish by DMA mode then update the get index for receive fifo.
414 415 416 417
 *
 * @param serial serial device
 * @param len get data length for this operate
 */
418 419 420
static void rt_dma_recv_update_get_index(struct rt_serial_device *serial, rt_size_t len)
{
    struct rt_serial_rx_fifo *rx_fifo = (struct rt_serial_rx_fifo *) serial->serial_rx;
421 422 423 424

    RT_ASSERT(rx_fifo != RT_NULL);
    RT_ASSERT(len <= rt_dma_calc_recved_len(serial));

425 426
    if (rx_fifo->is_full && len != 0) rx_fifo->is_full = RT_FALSE;

427
    rx_fifo->get_index += (rt_uint16_t)len;
428
    if (rx_fifo->get_index >= serial->config.bufsz)
429
    {
430
        rx_fifo->get_index %= serial->config.bufsz;
431 432 433 434 435 436 437 438 439
    }
}

/**
 * DMA received finish then update put index for receive fifo.
 *
 * @param serial serial device
 * @param len received length for this transmit
 */
440 441
static void rt_dma_recv_update_put_index(struct rt_serial_device *serial, rt_size_t len)
{
442 443 444 445
    struct rt_serial_rx_fifo *rx_fifo = (struct rt_serial_rx_fifo *)serial->serial_rx;

    RT_ASSERT(rx_fifo != RT_NULL);

446 447
    if (rx_fifo->get_index <= rx_fifo->put_index)
    {
448
        rx_fifo->put_index += (rt_uint16_t)len;
449
        /* beyond the fifo end */
450 451 452
        if (rx_fifo->put_index >= serial->config.bufsz)
        {
            rx_fifo->put_index %= serial->config.bufsz;
453
            /* force overwrite get index */
454 455
            if (rx_fifo->put_index >= rx_fifo->get_index)
            {
456
                rx_fifo->is_full = RT_TRUE;
457 458
            }
        }
459 460 461
    }
    else
    {
462
        rx_fifo->put_index += (rt_uint16_t)len;
463 464
        if (rx_fifo->put_index >= rx_fifo->get_index)
        {
465
            /* beyond the fifo end */
466 467 468
            if (rx_fifo->put_index >= serial->config.bufsz)
            {
                rx_fifo->put_index %= serial->config.bufsz;
469 470
            }
            /* force overwrite get index */
471
            rx_fifo->is_full = RT_TRUE;
472 473
        }
    }
474

475
    if(rx_fifo->is_full == RT_TRUE)
B
Bluebear233 已提交
476
    {
477
        _serial_check_buffer_size();
478 479
        rx_fifo->get_index = rx_fifo->put_index;
    }
480 481
}

B
bernard 已提交
482 483 484 485
/*
 * Serial DMA routines
 */
rt_inline int _serial_dma_rx(struct rt_serial_device *serial, rt_uint8_t *data, int length)
486
{
487 488 489 490 491 492
    rt_base_t level;

    RT_ASSERT((serial != RT_NULL) && (data != RT_NULL));

    level = rt_hw_interrupt_disable();

493 494
    if (serial->config.bufsz == 0)
    {
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
        int result = RT_EOK;
        struct rt_serial_rx_dma *rx_dma;

        rx_dma = (struct rt_serial_rx_dma*)serial->serial_rx;
        RT_ASSERT(rx_dma != RT_NULL);

        if (rx_dma->activated != RT_TRUE)
        {
            rx_dma->activated = RT_TRUE;
            RT_ASSERT(serial->ops->dma_transmit != RT_NULL);
            serial->ops->dma_transmit(serial, data, length, RT_SERIAL_DMA_RX);
        }
        else result = -RT_EBUSY;
        rt_hw_interrupt_enable(level);

        if (result == RT_EOK) return length;

        rt_set_errno(result);
        return 0;
514 515 516
    }
    else
    {
517 518 519 520
        struct rt_serial_rx_fifo *rx_fifo = (struct rt_serial_rx_fifo *) serial->serial_rx;
        rt_size_t recv_len = 0, fifo_recved_len = rt_dma_calc_recved_len(serial);

        RT_ASSERT(rx_fifo != RT_NULL);
521

522
        if (length < (int)fifo_recved_len)
523
            recv_len = length;
524
        else
525 526
            recv_len = fifo_recved_len;

527
        if (rx_fifo->get_index + recv_len < serial->config.bufsz)
528
            rt_memcpy(data, rx_fifo->buffer + rx_fifo->get_index, recv_len);
529 530
        else
        {
531 532 533 534 535 536 537 538 539
            rt_memcpy(data, rx_fifo->buffer + rx_fifo->get_index,
                    serial->config.bufsz - rx_fifo->get_index);
            rt_memcpy(data + serial->config.bufsz - rx_fifo->get_index, rx_fifo->buffer,
                    recv_len + rx_fifo->get_index - serial->config.bufsz);
        }
        rt_dma_recv_update_get_index(serial, recv_len);
        rt_hw_interrupt_enable(level);
        return recv_len;
    }
B
bernard 已提交
540
}
541

B
bernard 已提交
542 543
rt_inline int _serial_dma_tx(struct rt_serial_device *serial, const rt_uint8_t *data, int length)
{
544 545 546 547 548
    rt_base_t level;
    rt_err_t result;
    struct rt_serial_tx_dma *tx_dma;

    tx_dma = (struct rt_serial_tx_dma*)(serial->serial_tx);
549 550

    result = rt_data_queue_push(&(tx_dma->data_queue), data, length, RT_WAITING_FOREVER);
551 552 553 554 555 556 557 558 559
    if (result == RT_EOK)
    {
        level = rt_hw_interrupt_disable();
        if (tx_dma->activated != RT_TRUE)
        {
            tx_dma->activated = RT_TRUE;
            rt_hw_interrupt_enable(level);

            /* make a DMA transfer */
560
            serial->ops->dma_transmit(serial, (rt_uint8_t *)data, length, RT_SERIAL_DMA_TX);
561 562 563 564 565 566 567 568 569 570 571 572 573
        }
        else
        {
            rt_hw_interrupt_enable(level);
        }

        return length;
    }
    else
    {
        rt_set_errno(result);
        return 0;
    }
574
}
575
#endif /* RT_SERIAL_USING_DMA */
576 577 578

/* RT-Thread Device Interface */
/*
B
bernard 已提交
579
 * This function initializes serial device.
580 581 582 583 584 585 586 587 588
 */
static rt_err_t rt_serial_init(struct rt_device *dev)
{
    rt_err_t result = RT_EOK;
    struct rt_serial_device *serial;

    RT_ASSERT(dev != RT_NULL);
    serial = (struct rt_serial_device *)dev;

589 590 591
    /* initialize rx/tx */
    serial->serial_rx = RT_NULL;
    serial->serial_tx = RT_NULL;
592

593 594 595
    /* apply configuration */
    if (serial->ops->configure)
        result = serial->ops->configure(serial, &serial->config);
B
bernard 已提交
596

597
    return result;
598 599 600 601
}

static rt_err_t rt_serial_open(struct rt_device *dev, rt_uint16_t oflag)
{
B
bernard 已提交
602
    rt_uint16_t stream_flag = 0;
603 604 605 606 607
    struct rt_serial_device *serial;

    RT_ASSERT(dev != RT_NULL);
    serial = (struct rt_serial_device *)dev;

608
    LOG_D("open serial device: 0x%08x with open flag: 0x%04x",
B
bernard 已提交
609
        dev, oflag);
610
    /* check device flag with the open flag */
611
    if ((oflag & RT_DEVICE_FLAG_DMA_RX) && !(dev->flag & RT_DEVICE_FLAG_DMA_RX))
612 613 614 615 616 617 618 619
        return -RT_EIO;
    if ((oflag & RT_DEVICE_FLAG_DMA_TX) && !(dev->flag & RT_DEVICE_FLAG_DMA_TX))
        return -RT_EIO;
    if ((oflag & RT_DEVICE_FLAG_INT_RX) && !(dev->flag & RT_DEVICE_FLAG_INT_RX))
        return -RT_EIO;
    if ((oflag & RT_DEVICE_FLAG_INT_TX) && !(dev->flag & RT_DEVICE_FLAG_INT_TX))
        return -RT_EIO;

B
bernard 已提交
620 621 622 623
    /* keep steam flag */
    if ((oflag & RT_DEVICE_FLAG_STREAM) || (dev->open_flag & RT_DEVICE_FLAG_STREAM))
        stream_flag = RT_DEVICE_FLAG_STREAM;

624 625
    /* get open flags */
    dev->open_flag = oflag & 0xff;
626

627 628
    /* initialize the Rx/Tx structure according to open flag */
    if (serial->serial_rx == RT_NULL)
629
    {
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
        if (oflag & RT_DEVICE_FLAG_INT_RX)
        {
            struct rt_serial_rx_fifo* rx_fifo;

            rx_fifo = (struct rt_serial_rx_fifo*) rt_malloc (sizeof(struct rt_serial_rx_fifo) +
                serial->config.bufsz);
            RT_ASSERT(rx_fifo != RT_NULL);
            rx_fifo->buffer = (rt_uint8_t*) (rx_fifo + 1);
            rt_memset(rx_fifo->buffer, 0, serial->config.bufsz);
            rx_fifo->put_index = 0;
            rx_fifo->get_index = 0;
            rx_fifo->is_full = RT_FALSE;

            serial->serial_rx = rx_fifo;
            dev->open_flag |= RT_DEVICE_FLAG_INT_RX;
            /* configure low level device */
            serial->ops->control(serial, RT_DEVICE_CTRL_SET_INT, (void *)RT_DEVICE_FLAG_INT_RX);
        }
648
#ifdef RT_SERIAL_USING_DMA
649
        else if (oflag & RT_DEVICE_FLAG_DMA_RX)
650
        {
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
            if (serial->config.bufsz == 0) {
                struct rt_serial_rx_dma* rx_dma;

                rx_dma = (struct rt_serial_rx_dma*) rt_malloc (sizeof(struct rt_serial_rx_dma));
                RT_ASSERT(rx_dma != RT_NULL);
                rx_dma->activated = RT_FALSE;

                serial->serial_rx = rx_dma;
            } else {
                struct rt_serial_rx_fifo* rx_fifo;

                rx_fifo = (struct rt_serial_rx_fifo*) rt_malloc (sizeof(struct rt_serial_rx_fifo) +
                    serial->config.bufsz);
                RT_ASSERT(rx_fifo != RT_NULL);
                rx_fifo->buffer = (rt_uint8_t*) (rx_fifo + 1);
                rt_memset(rx_fifo->buffer, 0, serial->config.bufsz);
                rx_fifo->put_index = 0;
                rx_fifo->get_index = 0;
669
                rx_fifo->is_full = RT_FALSE;
670 671 672 673
                serial->serial_rx = rx_fifo;
                /* configure fifo address and length to low level device */
                serial->ops->control(serial, RT_DEVICE_CTRL_CONFIG, (void *) RT_DEVICE_FLAG_DMA_RX);
            }
674 675
            dev->open_flag |= RT_DEVICE_FLAG_DMA_RX;
        }
676
#endif /* RT_SERIAL_USING_DMA */
677 678 679 680 681
        else
        {
            serial->serial_rx = RT_NULL;
        }
    }
682 683
    else
    {
684
        if (oflag & RT_DEVICE_FLAG_INT_RX)
685
            dev->open_flag |= RT_DEVICE_FLAG_INT_RX;
686 687 688
#ifdef RT_SERIAL_USING_DMA
        else if (oflag & RT_DEVICE_FLAG_DMA_RX)
            dev->open_flag |= RT_DEVICE_FLAG_DMA_RX;
689
#endif /* RT_SERIAL_USING_DMA */
690
    }
691 692 693

    if (serial->serial_tx == RT_NULL)
    {
694
        if (oflag & RT_DEVICE_FLAG_INT_TX)
695 696 697 698 699 700 701 702 703 704 705 706 707
        {
            struct rt_serial_tx_fifo *tx_fifo;

            tx_fifo = (struct rt_serial_tx_fifo*) rt_malloc(sizeof(struct rt_serial_tx_fifo));
            RT_ASSERT(tx_fifo != RT_NULL);

            rt_completion_init(&(tx_fifo->completion));
            serial->serial_tx = tx_fifo;

            dev->open_flag |= RT_DEVICE_FLAG_INT_TX;
            /* configure low level device */
            serial->ops->control(serial, RT_DEVICE_CTRL_SET_INT, (void *)RT_DEVICE_FLAG_INT_TX);
        }
708 709 710 711 712 713 714 715 716 717 718 719 720
#ifdef RT_SERIAL_USING_DMA
        else if (oflag & RT_DEVICE_FLAG_DMA_TX)
        {
            struct rt_serial_tx_dma* tx_dma;

            tx_dma = (struct rt_serial_tx_dma*) rt_malloc (sizeof(struct rt_serial_tx_dma));
            RT_ASSERT(tx_dma != RT_NULL);
            tx_dma->activated = RT_FALSE;

            rt_data_queue_init(&(tx_dma->data_queue), 8, 4, RT_NULL);
            serial->serial_tx = tx_dma;

            dev->open_flag |= RT_DEVICE_FLAG_DMA_TX;
721 722
            /* configure low level device */
            serial->ops->control(serial, RT_DEVICE_CTRL_CONFIG, (void *)RT_DEVICE_FLAG_DMA_TX);
723 724
        }
#endif /* RT_SERIAL_USING_DMA */
725 726 727 728 729
        else
        {
            serial->serial_tx = RT_NULL;
        }
    }
730 731
    else
    {
732
        if (oflag & RT_DEVICE_FLAG_INT_TX)
733
            dev->open_flag |= RT_DEVICE_FLAG_INT_TX;
734 735 736
#ifdef RT_SERIAL_USING_DMA
        else if (oflag & RT_DEVICE_FLAG_DMA_TX)
            dev->open_flag |= RT_DEVICE_FLAG_DMA_TX;
737
#endif /* RT_SERIAL_USING_DMA */
738
    }
739

B
bernard 已提交
740 741 742
    /* set stream flag */
    dev->open_flag |= stream_flag;

743 744 745 746 747 748 749 750 751 752
    return RT_EOK;
}

static rt_err_t rt_serial_close(struct rt_device *dev)
{
    struct rt_serial_device *serial;

    RT_ASSERT(dev != RT_NULL);
    serial = (struct rt_serial_device *)dev;

753 754
    /* this device has more reference count */
    if (dev->ref_count > 1) return RT_EOK;
755

756 757 758 759
    if (dev->open_flag & RT_DEVICE_FLAG_INT_RX)
    {
        struct rt_serial_rx_fifo* rx_fifo;

A
Aubr.Cool 已提交
760 761 762 763
        /* configure low level device */
        serial->ops->control(serial, RT_DEVICE_CTRL_CLR_INT, (void*)RT_DEVICE_FLAG_INT_RX);
        dev->open_flag &= ~RT_DEVICE_FLAG_INT_RX;

764 765 766 767 768
        rx_fifo = (struct rt_serial_rx_fifo*)serial->serial_rx;
        RT_ASSERT(rx_fifo != RT_NULL);

        rt_free(rx_fifo);
        serial->serial_rx = RT_NULL;
S
SummerGift 已提交
769

770
    }
771
#ifdef RT_SERIAL_USING_DMA
772 773
    else if (dev->open_flag & RT_DEVICE_FLAG_DMA_RX)
    {
A
Aubr.Cool 已提交
774 775 776 777
        /* configure low level device */
        serial->ops->control(serial, RT_DEVICE_CTRL_CLR_INT, (void *) RT_DEVICE_FLAG_DMA_RX);
        dev->open_flag &= ~RT_DEVICE_FLAG_DMA_RX;

mysterywolf's avatar
mysterywolf 已提交
778 779
        if (serial->config.bufsz == 0)
        {
780
            struct rt_serial_rx_dma* rx_dma;
781

782 783 784 785
            rx_dma = (struct rt_serial_rx_dma*)serial->serial_rx;
            RT_ASSERT(rx_dma != RT_NULL);

            rt_free(rx_dma);
mysterywolf's avatar
mysterywolf 已提交
786 787 788
        }
        else
        {
789
            struct rt_serial_rx_fifo* rx_fifo;
790

791 792 793 794 795
            rx_fifo = (struct rt_serial_rx_fifo*)serial->serial_rx;
            RT_ASSERT(rx_fifo != RT_NULL);

            rt_free(rx_fifo);
        }
796
        serial->serial_rx = RT_NULL;
S
SummerGift 已提交
797

798
    }
799
#endif /* RT_SERIAL_USING_DMA */
800

801 802 803 804
    if (dev->open_flag & RT_DEVICE_FLAG_INT_TX)
    {
        struct rt_serial_tx_fifo* tx_fifo;

A
Aubr.Cool 已提交
805 806 807
        serial->ops->control(serial, RT_DEVICE_CTRL_CLR_INT, (void*)RT_DEVICE_FLAG_INT_TX);
        dev->open_flag &= ~RT_DEVICE_FLAG_INT_TX;

808
        tx_fifo = (struct rt_serial_tx_fifo*)serial->serial_tx;
809 810 811 812
        RT_ASSERT(tx_fifo != RT_NULL);

        rt_free(tx_fifo);
        serial->serial_tx = RT_NULL;
S
SummerGift 已提交
813

814 815
        /* configure low level device */
    }
816
#ifdef RT_SERIAL_USING_DMA
817 818 819 820
    else if (dev->open_flag & RT_DEVICE_FLAG_DMA_TX)
    {
        struct rt_serial_tx_dma* tx_dma;

A
Aubr.Cool 已提交
821 822 823 824
        /* configure low level device */
        serial->ops->control(serial, RT_DEVICE_CTRL_CLR_INT, (void *) RT_DEVICE_FLAG_DMA_TX);
        dev->open_flag &= ~RT_DEVICE_FLAG_DMA_TX;

825 826 827
        tx_dma = (struct rt_serial_tx_dma*)serial->serial_tx;
        RT_ASSERT(tx_dma != RT_NULL);

828 829
        rt_data_queue_deinit(&(tx_dma->data_queue));

830 831
        rt_free(tx_dma);
        serial->serial_tx = RT_NULL;
S
SummerGift 已提交
832

833
    }
834
#endif /* RT_SERIAL_USING_DMA */
S
SummerGift 已提交
835 836 837 838

    serial->ops->control(serial, RT_DEVICE_CTRL_CLOSE, RT_NULL);
    dev->flag &= ~RT_DEVICE_FLAG_ACTIVATED;

839 840 841 842 843 844 845 846 847 848 849
    return RT_EOK;
}

static rt_size_t rt_serial_read(struct rt_device *dev,
                                rt_off_t          pos,
                                void             *buffer,
                                rt_size_t         size)
{
    struct rt_serial_device *serial;

    RT_ASSERT(dev != RT_NULL);
B
bernard 已提交
850
    if (size == 0) return 0;
851

852 853
    serial = (struct rt_serial_device *)dev;

854 855
    if (dev->open_flag & RT_DEVICE_FLAG_INT_RX)
    {
856
        return _serial_int_rx(serial, (rt_uint8_t *)buffer, size);
857
    }
858
#ifdef RT_SERIAL_USING_DMA
859 860
    else if (dev->open_flag & RT_DEVICE_FLAG_DMA_RX)
    {
861
        return _serial_dma_rx(serial, (rt_uint8_t *)buffer, size);
862
    }
863
#endif /* RT_SERIAL_USING_DMA */
864

865
    return _serial_poll_rx(serial, (rt_uint8_t *)buffer, size);
866 867 868 869 870 871 872 873 874 875
}

static rt_size_t rt_serial_write(struct rt_device *dev,
                                 rt_off_t          pos,
                                 const void       *buffer,
                                 rt_size_t         size)
{
    struct rt_serial_device *serial;

    RT_ASSERT(dev != RT_NULL);
B
bernard 已提交
876
    if (size == 0) return 0;
877

878 879
    serial = (struct rt_serial_device *)dev;

880 881
    if (dev->open_flag & RT_DEVICE_FLAG_INT_TX)
    {
882
        return _serial_int_tx(serial, (const rt_uint8_t *)buffer, size);
883
    }
884
#ifdef RT_SERIAL_USING_DMA
885 886
    else if (dev->open_flag & RT_DEVICE_FLAG_DMA_TX)
    {
887
        return _serial_dma_tx(serial, (const rt_uint8_t *)buffer, size);
888
    }
889
#endif /* RT_SERIAL_USING_DMA */
890 891
    else
    {
892
        return _serial_poll_tx(serial, (const rt_uint8_t *)buffer, size);
893
    }
894 895
}

B
bernard 已提交
896
#ifdef RT_USING_POSIX_TERMIOS
J
JasonJiaJie 已提交
897
struct speed_baudrate_item
B
bernard 已提交
898 899 900 901 902
{
    speed_t speed;
    int baudrate;
};

dongly's avatar
dongly 已提交
903
static const struct speed_baudrate_item _tbl[] =
B
bernard 已提交
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
{
    {B2400, BAUD_RATE_2400},
    {B4800, BAUD_RATE_4800},
    {B9600, BAUD_RATE_9600},
    {B19200, BAUD_RATE_19200},
    {B38400, BAUD_RATE_38400},
    {B57600, BAUD_RATE_57600},
    {B115200, BAUD_RATE_115200},
    {B230400, BAUD_RATE_230400},
    {B460800, BAUD_RATE_460800},
    {B921600, BAUD_RATE_921600},
    {B2000000, BAUD_RATE_2000000},
    {B3000000, BAUD_RATE_3000000},
};

static speed_t _get_speed(int baudrate)
{
dongly's avatar
dongly 已提交
921
    size_t index;
B
bernard 已提交
922 923 924 925 926 927 928 929 930 931 932 933

    for (index = 0; index < sizeof(_tbl)/sizeof(_tbl[0]); index ++)
    {
        if (_tbl[index].baudrate == baudrate)
            return _tbl[index].speed;
    }

    return B0;
}

static int _get_baudrate(speed_t speed)
{
dongly's avatar
dongly 已提交
934
    size_t index;
B
bernard 已提交
935 936 937 938 939 940 941 942 943

    for (index = 0; index < sizeof(_tbl)/sizeof(_tbl[0]); index ++)
    {
        if (_tbl[index].speed == speed)
            return _tbl[index].baudrate;
    }

    return 0;
}
944 945 946

static void _tc_flush(struct rt_serial_device *serial, int queue)
{
947
    rt_base_t level;
948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966
    int ch = -1;
    struct rt_serial_rx_fifo *rx_fifo = RT_NULL;
    struct rt_device *device = RT_NULL;

    RT_ASSERT(serial != RT_NULL);

    device = &(serial->parent);
    rx_fifo = (struct rt_serial_rx_fifo *) serial->serial_rx;

    switch(queue)
    {
        case TCIFLUSH:
        case TCIOFLUSH:

            RT_ASSERT(rx_fifo != RT_NULL);

            if((device->open_flag & RT_DEVICE_FLAG_INT_RX) || (device->open_flag & RT_DEVICE_FLAG_DMA_RX))
            {
                RT_ASSERT(RT_NULL != rx_fifo);
967
                level = rt_hw_interrupt_disable();
H
HubretXie 已提交
968
                rx_fifo->get_index = rx_fifo->put_index;
969
                rx_fifo->is_full = RT_FALSE;
970
                rt_hw_interrupt_enable(level);
971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987
            }
            else
            {
                while (1)
                {
                    ch = serial->ops->getc(serial);
                    if (ch == -1) break;
                }
            }

            break;

         case TCOFLUSH:
            break;
    }

}
988
#endif /* RT_USING_POSIX_TERMIOS */
B
bernard 已提交
989

990
static rt_err_t rt_serial_control(struct rt_device *dev,
B
bernard 已提交
991
                                  int              cmd,
992 993
                                  void             *args)
{
B
bernard 已提交
994
    rt_err_t ret = RT_EOK;
995 996 997 998 999 1000 1001
    struct rt_serial_device *serial;

    RT_ASSERT(dev != RT_NULL);
    serial = (struct rt_serial_device *)dev;

    switch (cmd)
    {
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
        case RT_DEVICE_CTRL_SUSPEND:
            /* suspend device */
            dev->flag |= RT_DEVICE_FLAG_SUSPENDED;
            break;

        case RT_DEVICE_CTRL_RESUME:
            /* resume device */
            dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED;
            break;

        case RT_DEVICE_CTRL_CONFIG:
1013 1014 1015
            if (args)
            {
                struct serial_configure *pconfig = (struct serial_configure *) args;
1016
                if (pconfig->bufsz != serial->config.bufsz && serial->parent.ref_count)
1017 1018 1019 1020 1021 1022
                {
                    /*can not change buffer size*/
                    return RT_EBUSY;
                }
                /* set serial configure */
                serial->config = *pconfig;
1023
                if (serial->parent.ref_count)
1024 1025
                {
                    /* serial device has been opened, to configure it */
1026
                    serial->ops->configure(serial, (struct serial_configure *) args);
1027 1028
                }
            }
B
bernard 已提交
1029 1030

            break;
mysterywolf's avatar
mysterywolf 已提交
1031
#ifdef RT_USING_POSIX_STDIO
B
bernard 已提交
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
#ifdef RT_USING_POSIX_TERMIOS
        case TCGETA:
            {
                struct termios *tio = (struct termios*)args;
                if (tio == RT_NULL) return -RT_EINVAL;

                tio->c_iflag = 0;
                tio->c_oflag = 0;
                tio->c_lflag = 0;

                /* update oflag for console device */
                if (rt_console_get_device() == dev)
                    tio->c_oflag = OPOST | ONLCR;

                /* set cflag */
                tio->c_cflag = 0;
                if (serial->config.data_bits == DATA_BITS_5)
                    tio->c_cflag = CS5;
                else if (serial->config.data_bits == DATA_BITS_6)
                    tio->c_cflag = CS6;
                else if (serial->config.data_bits == DATA_BITS_7)
                    tio->c_cflag = CS7;
                else if (serial->config.data_bits == DATA_BITS_8)
                    tio->c_cflag = CS8;

                if (serial->config.stop_bits == STOP_BITS_2)
                    tio->c_cflag |= CSTOPB;

                if (serial->config.parity == PARITY_EVEN)
                    tio->c_cflag |= PARENB;
                else if (serial->config.parity == PARITY_ODD)
                    tio->c_cflag |= (PARODD | PARENB);

                cfsetospeed(tio, _get_speed(serial->config.baud_rate));
            }
1067 1068
            break;

B
bernard 已提交
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
        case TCSETAW:
        case TCSETAF:
        case TCSETA:
            {
                int baudrate;
                struct serial_configure config;

                struct termios *tio = (struct termios*)args;
                if (tio == RT_NULL) return -RT_EINVAL;

                config = serial->config;

                baudrate = _get_baudrate(cfgetospeed(tio));
                config.baud_rate = baudrate;

1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
                switch (tio->c_cflag & CSIZE)
                {
                case CS5:
                    config.data_bits = DATA_BITS_5;
                    break;
                case CS6:
                    config.data_bits = DATA_BITS_6;
                    break;
                case CS7:
                    config.data_bits = DATA_BITS_7;
                    break;
                default:
                    config.data_bits = DATA_BITS_8;
                    break;
                }
B
bernard 已提交
1099

1100 1101
                if (tio->c_cflag & CSTOPB) config.stop_bits = STOP_BITS_2;
                else config.stop_bits = STOP_BITS_1;
B
bernard 已提交
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113

                if (tio->c_cflag & PARENB)
                {
                    if (tio->c_cflag & PARODD) config.parity = PARITY_ODD;
                    else config.parity = PARITY_EVEN;
                }
                else config.parity = PARITY_NONE;

                serial->ops->configure(serial, &config);
            }
            break;
        case TCFLSH:
J
JasonJiaJie 已提交
1114
            {
J
JasonJiaJie 已提交
1115
                int queue = (int)args;
J
JasonJiaJie 已提交
1116

1117
                _tc_flush(serial, queue);
J
JasonJiaJie 已提交
1118 1119
            }

B
bernard 已提交
1120 1121 1122
            break;
        case TCXONC:
            break;
1123
#endif /*RT_USING_POSIX_TERMIOS*/
mysterywolf's avatar
mysterywolf 已提交
1124 1125 1126 1127 1128 1129
        case TIOCSWINSZ:
            {
                struct winsize* p_winsize;

                p_winsize = (struct winsize*)args;
                rt_kprintf("\x1b[8;%d;%dt", p_winsize->ws_col, p_winsize->ws_row);
mysterywolf's avatar
mysterywolf 已提交
1130 1131 1132 1133 1134 1135
            }
            break;
        case TIOCGWINSZ:
            {
                struct winsize* p_winsize;
                p_winsize = (struct winsize*)args;
1136 1137 1138 1139 1140 1141 1142 1143 1144

                if(rt_thread_self() != rt_thread_find("tshell"))
                {
                    /* only can be used in tshell thread; otherwise, return default size */
                    p_winsize->ws_col = 80;
                    p_winsize->ws_row = 24;
                }
                else
                {
mysterywolf's avatar
mysterywolf 已提交
1145
                    #include <shell.h>
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160
                    #define _TIO_BUFLEN 20
                    char _tio_buf[_TIO_BUFLEN];
                    unsigned char cnt1, cnt2, cnt3, i;
                    char row_s[4], col_s[4];
                    char *p;

                    rt_memset(_tio_buf, 0, _TIO_BUFLEN);

                    /* send the command to terminal for getting the window size of the terminal */
                    rt_kprintf("\033[18t");

                    /* waiting for the response from the terminal */
                    i = 0;
                    while(i < _TIO_BUFLEN)
                    {
mysterywolf's avatar
mysterywolf 已提交
1161
                        _tio_buf[i] = finsh_getchar();
1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
                        if(_tio_buf[i] != 't')
                        {
                            i ++;
                        }
                        else
                        {
                            break;
                        }
                    }
                    if(i == _TIO_BUFLEN)
                    {
                        /* buffer overloaded, and return default size */
                        p_winsize->ws_col = 80;
                        p_winsize->ws_row = 24;
                        break;
                    }

                    /* interpreting data eg: "\033[8;1;15t" which means row is 1 and col is 15 (unit: size of ONE character) */
                    rt_memset(row_s,0,4);
                    rt_memset(col_s,0,4);
                    cnt1 = 0;
                    while(_tio_buf[cnt1] != ';' && cnt1 < _TIO_BUFLEN)
                    {
                        cnt1++;
                    }
                    cnt2 = ++cnt1;
                    while(_tio_buf[cnt2] != ';' && cnt2 < _TIO_BUFLEN)
                    {
                        cnt2++;
                    }
                    p = row_s;
                    while(cnt1 < cnt2)
                    {
                        *p++ = _tio_buf[cnt1++];
                    }
                    p = col_s;
                    cnt2++;
                    cnt3 = rt_strlen(_tio_buf) - 1;
                    while(cnt2 < cnt3)
                    {
                        *p++ = _tio_buf[cnt2++];
                    }

                    /* load the window size date */
                    p_winsize->ws_col = atoi(col_s);
                    p_winsize->ws_row = atoi(row_s);
                #undef _TIO_BUFLEN
                }

                p_winsize->ws_xpixel = 0;/* unused */
                p_winsize->ws_ypixel = 0;/* unused */
mysterywolf's avatar
mysterywolf 已提交
1213 1214
            }
            break;
J
JasonJiaJie 已提交
1215 1216 1217 1218
        case FIONREAD:
            {
                rt_size_t recved = 0;
                rt_base_t level;
B
bernard 已提交
1219

J
JasonJiaJie 已提交
1220 1221 1222 1223 1224 1225 1226
                level = rt_hw_interrupt_disable();
                recved = _serial_fifo_calc_recved_len(serial);
                rt_hw_interrupt_enable(level);

                *(rt_size_t *)args = recved;
            }
            break;
mysterywolf's avatar
mysterywolf 已提交
1227
#endif /* RT_USING_POSIX_STDIO */
1228 1229
        default :
            /* control device */
B
bernard 已提交
1230
            ret = serial->ops->control(serial, cmd, args);
1231
            break;
1232 1233
    }

B
bernard 已提交
1234
    return ret;
1235 1236
}

B
Bernard Xiong 已提交
1237
#ifdef RT_USING_DEVICE_OPS
1238
const static struct rt_device_ops serial_ops =
B
Bernard Xiong 已提交
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
{
    rt_serial_init,
    rt_serial_open,
    rt_serial_close,
    rt_serial_read,
    rt_serial_write,
    rt_serial_control
};
#endif

1249 1250 1251 1252 1253 1254 1255 1256
/*
 * serial register
 */
rt_err_t rt_hw_serial_register(struct rt_serial_device *serial,
                               const char              *name,
                               rt_uint32_t              flag,
                               void                    *data)
{
B
bernard 已提交
1257
    rt_err_t ret;
1258 1259 1260 1261 1262 1263 1264 1265 1266
    struct rt_device *device;
    RT_ASSERT(serial != RT_NULL);

    device = &(serial->parent);

    device->type        = RT_Device_Class_Char;
    device->rx_indicate = RT_NULL;
    device->tx_complete = RT_NULL;

B
Bernard Xiong 已提交
1267 1268 1269
#ifdef RT_USING_DEVICE_OPS
    device->ops         = &serial_ops;
#else
1270 1271 1272 1273 1274 1275
    device->init        = rt_serial_init;
    device->open        = rt_serial_open;
    device->close       = rt_serial_close;
    device->read        = rt_serial_read;
    device->write       = rt_serial_write;
    device->control     = rt_serial_control;
B
Bernard Xiong 已提交
1276
#endif
1277 1278 1279
    device->user_data   = data;

    /* register a character device */
B
bernard 已提交
1280 1281
    ret = rt_device_register(device, name, flag);

mysterywolf's avatar
mysterywolf 已提交
1282
#ifdef RT_USING_POSIX_STDIO
B
bernard 已提交
1283 1284 1285 1286 1287
    /* set fops */
    device->fops        = &_serial_fops;
#endif

    return ret;
1288 1289 1290
}

/* ISR for serial interrupt */
B
bernard 已提交
1291
void rt_hw_serial_isr(struct rt_serial_device *serial, int event)
1292
{
1293 1294 1295 1296 1297 1298 1299 1300
    switch (event & 0xff)
    {
        case RT_SERIAL_EVENT_RX_IND:
        {
            int ch = -1;
            rt_base_t level;
            struct rt_serial_rx_fifo* rx_fifo;

1301
            /* interrupt mode receive */
1302 1303
            rx_fifo = (struct rt_serial_rx_fifo*)serial->serial_rx;
            RT_ASSERT(rx_fifo != RT_NULL);
1304

1305 1306 1307 1308 1309
            while (1)
            {
                ch = serial->ops->getc(serial);
                if (ch == -1) break;

1310

1311 1312
                /* disable interrupt */
                level = rt_hw_interrupt_disable();
1313

1314 1315 1316
                rx_fifo->buffer[rx_fifo->put_index] = ch;
                rx_fifo->put_index += 1;
                if (rx_fifo->put_index >= serial->config.bufsz) rx_fifo->put_index = 0;
1317

1318 1319 1320 1321
                /* if the next position is read index, discard this 'read char' */
                if (rx_fifo->put_index == rx_fifo->get_index)
                {
                    rx_fifo->get_index += 1;
J
JasonJiaJie 已提交
1322
                    rx_fifo->is_full = RT_TRUE;
1323
                    if (rx_fifo->get_index >= serial->config.bufsz) rx_fifo->get_index = 0;
1324 1325

                    _serial_check_buffer_size();
1326
                }
1327

1328 1329 1330
                /* enable interrupt */
                rt_hw_interrupt_enable(level);
            }
1331

1332 1333 1334 1335
            /* invoke callback */
            if (serial->parent.rx_indicate != RT_NULL)
            {
                rt_size_t rx_length;
1336

1337 1338 1339 1340 1341 1342
                /* get rx length */
                level = rt_hw_interrupt_disable();
                rx_length = (rx_fifo->put_index >= rx_fifo->get_index)? (rx_fifo->put_index - rx_fifo->get_index):
                    (serial->config.bufsz - (rx_fifo->get_index - rx_fifo->put_index));
                rt_hw_interrupt_enable(level);

1343 1344 1345 1346
                if (rx_length)
                {
                    serial->parent.rx_indicate(&serial->parent, rx_length);
                }
1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357
            }
            break;
        }
        case RT_SERIAL_EVENT_TX_DONE:
        {
            struct rt_serial_tx_fifo* tx_fifo;

            tx_fifo = (struct rt_serial_tx_fifo*)serial->serial_tx;
            rt_completion_done(&(tx_fifo->completion));
            break;
        }
1358
#ifdef RT_SERIAL_USING_DMA
1359 1360 1361 1362 1363
        case RT_SERIAL_EVENT_TX_DMADONE:
        {
            const void *data_ptr;
            rt_size_t data_size;
            const void *last_data_ptr;
1364
            struct rt_serial_tx_dma *tx_dma;
1365 1366

            tx_dma = (struct rt_serial_tx_dma*) serial->serial_tx;
1367

1368
            rt_data_queue_pop(&(tx_dma->data_queue), &last_data_ptr, &data_size, 0);
1369
            if (rt_data_queue_peek(&(tx_dma->data_queue), &data_ptr, &data_size) == RT_EOK)
1370 1371 1372
            {
                /* transmit next data node */
                tx_dma->activated = RT_TRUE;
1373
                serial->ops->dma_transmit(serial, (rt_uint8_t *)data_ptr, data_size, RT_SERIAL_DMA_TX);
1374 1375 1376 1377 1378
            }
            else
            {
                tx_dma->activated = RT_FALSE;
            }
1379

1380 1381 1382 1383 1384 1385 1386 1387 1388 1389
            /* invoke callback */
            if (serial->parent.tx_complete != RT_NULL)
            {
                serial->parent.tx_complete(&serial->parent, (void*)last_data_ptr);
            }
            break;
        }
        case RT_SERIAL_EVENT_RX_DMADONE:
        {
            int length;
1390
            rt_base_t level;
1391 1392 1393

            /* get DMA rx length */
            length = (event & (~0xff)) >> 8;
1394

1395 1396
            if (serial->config.bufsz == 0)
            {
1397 1398
                struct rt_serial_rx_dma* rx_dma;

1399
                rx_dma = (struct rt_serial_rx_dma*) serial->serial_rx;
1400 1401 1402 1403 1404
                RT_ASSERT(rx_dma != RT_NULL);

                RT_ASSERT(serial->parent.rx_indicate != RT_NULL);
                serial->parent.rx_indicate(&(serial->parent), length);
                rx_dma->activated = RT_FALSE;
1405 1406 1407 1408 1409
            }
            else
            {
                /* disable interrupt */
                level = rt_hw_interrupt_disable();
1410 1411
                /* update fifo put index */
                rt_dma_recv_update_put_index(serial, length);
1412 1413 1414 1415
                /* calculate received total length */
                length = rt_dma_calc_recved_len(serial);
                /* enable interrupt */
                rt_hw_interrupt_enable(level);
1416
                /* invoke callback */
1417 1418 1419
                if (serial->parent.rx_indicate != RT_NULL)
                {
                    serial->parent.rx_indicate(&(serial->parent), length);
1420 1421
                }
            }
1422 1423
            break;
        }
1424
#endif /* RT_SERIAL_USING_DMA */
1425
    }
1426
}