usart.c 17.4 KB
Newer Older
M
Ming, Bai 已提交
1
/*
2
 * Copyright (c) 2006-2018, RT-Thread Development Team
M
Ming, Bai 已提交
3
 *
4
 * SPDX-License-Identifier: Apache-2.0
M
Ming, Bai 已提交
5 6 7 8 9
 *
 * Change Logs:
 * Date           Author       Notes
 * 2009-01-05     Bernard      the first version
 * 2010-03-29     Bernard      remove interrupt Tx and DMA Rx mode
10
 * 2013-05-13     aozima       update for kehong-lingtai.
11
 * 2015-01-31     armink       make sure the serial transmit complete in putc()
12
 * 2016-05-13     armink       add DMA Rx mode
13
 * 2017-01-19     aubr.cool    add interrupt Tx mode
14
 * 2017-04-13     aubr.cool    correct Rx parity err
M
Ming, Bai 已提交
15 16
 */

17
#include "stm32f10x.h"
M
Ming, Bai 已提交
18
#include "usart.h"
19 20
#include "board.h"
#include <rtdevice.h>
M
Ming, Bai 已提交
21

22
/* USART1 */
23 24 25
#define UART1_GPIO_TX        GPIO_Pin_9
#define UART1_GPIO_RX        GPIO_Pin_10
#define UART1_GPIO           GPIOA
26 27

/* USART2 */
28 29 30
#define UART2_GPIO_TX        GPIO_Pin_2
#define UART2_GPIO_RX        GPIO_Pin_3
#define UART2_GPIO           GPIOA
31 32

/* USART3_REMAP[1:0] = 00 */
33 34 35
#define UART3_GPIO_TX        GPIO_Pin_10
#define UART3_GPIO_RX        GPIO_Pin_11
#define UART3_GPIO           GPIOB
36

U
unknown 已提交
37 38 39 40 41 42
/* USART4 */
#define UART4_GPIO_TX        GPIO_Pin_10
#define UART4_GPIO_RX        GPIO_Pin_11
#define UART4_GPIO           GPIOC


43 44
/* STM32 uart driver */
struct stm32_uart
M
Ming, Bai 已提交
45
{
46
    USART_TypeDef *uart_device;
47
    IRQn_Type irq;
48 49
    struct stm32_uart_dma
    {
50 51 52 53 54 55
        /* dma channel */
        DMA_Channel_TypeDef *rx_ch;
        /* dma global flag */
        uint32_t rx_gl_flag;
        /* dma irq channel */
        uint8_t rx_irq_ch;
56 57
        /* setting receive len */
        rt_size_t setting_recv_len;
58
        /* last receive index */
59
        rt_size_t last_recv_index;
60
    } dma;
M
Ming, Bai 已提交
61 62
};

63 64
static void DMA_Configuration(struct rt_serial_device *serial);

65
static rt_err_t stm32_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
M
Ming, Bai 已提交
66
{
67 68
    struct stm32_uart* uart;
    USART_InitTypeDef USART_InitStructure;
M
Ming, Bai 已提交
69

70 71 72 73 74 75 76
    RT_ASSERT(serial != RT_NULL);
    RT_ASSERT(cfg != RT_NULL);

    uart = (struct stm32_uart *)serial->parent.user_data;

    USART_InitStructure.USART_BaudRate = cfg->baud_rate;

77
    if (cfg->data_bits == DATA_BITS_8){
78
        USART_InitStructure.USART_WordLength = USART_WordLength_8b;
79 80 81
    } else if (cfg->data_bits == DATA_BITS_9) {
        USART_InitStructure.USART_WordLength = USART_WordLength_9b;
    }
82

83
    if (cfg->stop_bits == STOP_BITS_1){
84
        USART_InitStructure.USART_StopBits = USART_StopBits_1;
85
    } else if (cfg->stop_bits == STOP_BITS_2){
86
        USART_InitStructure.USART_StopBits = USART_StopBits_2;
87 88 89 90 91 92 93 94 95
    }

    if (cfg->parity == PARITY_NONE){
        USART_InitStructure.USART_Parity = USART_Parity_No;
    } else if (cfg->parity == PARITY_ODD) {
        USART_InitStructure.USART_Parity = USART_Parity_Odd;
    } else if (cfg->parity == PARITY_EVEN) {
        USART_InitStructure.USART_Parity = USART_Parity_Even;
    }
96 97 98 99 100 101 102

    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
    USART_Init(uart->uart_device, &USART_InitStructure);

    /* Enable USART */
    USART_Cmd(uart->uart_device, ENABLE);
103 104
    
    USART_ClearFlag(uart->uart_device,USART_FLAG_TC);
105 106 107 108 109

    return RT_EOK;
}

static rt_err_t stm32_control(struct rt_serial_device *serial, int cmd, void *arg)
M
Ming, Bai 已提交
110
{
111
    struct stm32_uart* uart;
112
    rt_uint32_t ctrl_arg = (rt_uint32_t)(arg);
113 114 115 116 117 118

    RT_ASSERT(serial != RT_NULL);
    uart = (struct stm32_uart *)serial->parent.user_data;

    switch (cmd)
    {
armink_ztl's avatar
armink_ztl 已提交
119
        /* disable interrupt */
120 121 122
    case RT_DEVICE_CTRL_CLR_INT:
        /* disable rx irq */
        UART_DISABLE_IRQ(uart->irq);
armink_ztl's avatar
armink_ztl 已提交
123 124
        /* disable interrupt */
        USART_ITConfig(uart->uart_device, USART_IT_RXNE, DISABLE);
125
        break;
armink_ztl's avatar
armink_ztl 已提交
126
        /* enable interrupt */
127 128 129
    case RT_DEVICE_CTRL_SET_INT:
        /* enable rx irq */
        UART_ENABLE_IRQ(uart->irq);
armink_ztl's avatar
armink_ztl 已提交
130 131
        /* enable interrupt */
        USART_ITConfig(uart->uart_device, USART_IT_RXNE, ENABLE);
132
        break;
133 134 135 136 137 138
        /* USART config */
    case RT_DEVICE_CTRL_CONFIG :
        if (ctrl_arg == RT_DEVICE_FLAG_DMA_RX) {
            DMA_Configuration(serial);
        }
        break;
139 140 141
    }
    return RT_EOK;
}
M
Ming, Bai 已提交
142

143 144 145
static int stm32_putc(struct rt_serial_device *serial, char c)
{
    struct stm32_uart* uart;
M
Ming, Bai 已提交
146

147 148
    RT_ASSERT(serial != RT_NULL);
    uart = (struct stm32_uart *)serial->parent.user_data;
M
Ming, Bai 已提交
149

150
    if (serial->parent.open_flag & RT_DEVICE_FLAG_INT_TX)
151
    {
152 153 154 155 156 157 158
        if (!(uart->uart_device->SR & USART_FLAG_TXE))
        {
            USART_ITConfig(uart->uart_device, USART_IT_TC, ENABLE);
            return -1;
        }
        uart->uart_device->DR = c;
        USART_ITConfig(uart->uart_device, USART_IT_TC, ENABLE);
159 160 161 162 163 164
    }
    else
    {
        uart->uart_device->DR = c;
        while (!(uart->uart_device->SR & USART_FLAG_TC));
    }
M
Ming, Bai 已提交
165

166 167 168 169
    return 1;
}

static int stm32_getc(struct rt_serial_device *serial)
M
Ming, Bai 已提交
170
{
171 172
    int ch;
    struct stm32_uart* uart;
M
Ming, Bai 已提交
173

174 175
    RT_ASSERT(serial != RT_NULL);
    uart = (struct stm32_uart *)serial->parent.user_data;
M
Ming, Bai 已提交
176

177 178 179 180 181
    ch = -1;
    if (uart->uart_device->SR & USART_FLAG_RXNE)
    {
        ch = uart->uart_device->DR & 0xff;
    }
M
Ming, Bai 已提交
182

183 184
    return ch;
}
M
Ming, Bai 已提交
185

186 187 188 189 190 191 192
/**
 * Serial port receive idle process. This need add to uart idle ISR.
 *
 * @param serial serial device
 */
static void dma_uart_rx_idle_isr(struct rt_serial_device *serial) {
    struct stm32_uart *uart = (struct stm32_uart *) serial->parent.user_data;
193 194 195 196 197 198 199
    rt_size_t recv_total_index, recv_len;
    rt_base_t level;

    /* disable interrupt */
    level = rt_hw_interrupt_disable();

    recv_total_index = uart->dma.setting_recv_len - DMA_GetCurrDataCounter(uart->dma.rx_ch);
200
    recv_len = recv_total_index - uart->dma.last_recv_index;
201 202 203 204 205
    uart->dma.last_recv_index = recv_total_index;
    /* enable interrupt */
    rt_hw_interrupt_enable(level);

    if (recv_len) rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_DMADONE | (recv_len << 8));
206 207 208 209 210 211 212 213 214 215 216 217 218

    /* read a data for clear receive idle interrupt flag */
    USART_ReceiveData(uart->uart_device);
    DMA_ClearFlag(uart->dma.rx_gl_flag);
}

/**
 * DMA receive done process. This need add to DMA receive done ISR.
 *
 * @param serial serial device
 */
static void dma_rx_done_isr(struct rt_serial_device *serial) {
    struct stm32_uart *uart = (struct stm32_uart *) serial->parent.user_data;
219
    rt_size_t recv_len;
220 221 222 223 224
    rt_base_t level;

    /* disable interrupt */
    level = rt_hw_interrupt_disable();

225 226 227
    recv_len = uart->dma.setting_recv_len - uart->dma.last_recv_index;
    /* reset last recv index */
    uart->dma.last_recv_index = 0;
228 229 230 231
    /* enable interrupt */
    rt_hw_interrupt_enable(level);

    if (recv_len) rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_DMADONE | (recv_len << 8));
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247

    DMA_ClearFlag(uart->dma.rx_gl_flag);
}

/**
 * Uart common interrupt process. This need add to uart ISR.
 *
 * @param serial serial device
 */
static void uart_isr(struct rt_serial_device *serial) {
    struct stm32_uart *uart = (struct stm32_uart *) serial->parent.user_data;

    RT_ASSERT(uart != RT_NULL);

    if(USART_GetITStatus(uart->uart_device, USART_IT_RXNE) != RESET)
    {
248 249 250 251
        if(USART_GetFlagStatus(uart->uart_device, USART_FLAG_PE) == RESET)
        {
            rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
        }
252 253 254 255 256 257 258 259 260 261
        /* clear interrupt */
        USART_ClearITPendingBit(uart->uart_device, USART_IT_RXNE);
    }
    if(USART_GetITStatus(uart->uart_device, USART_IT_IDLE) != RESET)
    {
        dma_uart_rx_idle_isr(serial);
    }
    if (USART_GetITStatus(uart->uart_device, USART_IT_TC) != RESET)
    {
        /* clear interrupt */
262 263 264 265
        if(serial->parent.open_flag & RT_DEVICE_FLAG_INT_TX)
        {
            rt_hw_serial_isr(serial, RT_SERIAL_EVENT_TX_DONE);
        }
A
Aubr.Cool 已提交
266
        USART_ITConfig(uart->uart_device, USART_IT_TC, DISABLE);
267 268 269 270
        USART_ClearITPendingBit(uart->uart_device, USART_IT_TC);
    }
    if (USART_GetFlagStatus(uart->uart_device, USART_FLAG_ORE) == SET)
    {
271
        USART_ReceiveData(uart->uart_device);
272 273 274
    }
}

275 276 277 278 279 280 281
static const struct rt_uart_ops stm32_uart_ops =
{
    stm32_configure,
    stm32_control,
    stm32_putc,
    stm32_getc,
};
M
Ming, Bai 已提交
282

283 284 285 286 287 288
#if defined(RT_USING_UART1)
/* UART1 device driver structure */
struct stm32_uart uart1 =
{
    USART1,
    USART1_IRQn,
289 290 291 292 293 294
    {
        DMA1_Channel5,
        DMA1_FLAG_GL5,
        DMA1_Channel5_IRQn,
        0,
    },
295 296
};
struct rt_serial_device serial1;
M
Ming, Bai 已提交
297

298 299
void USART1_IRQHandler(void)
{
300 301
    /* enter interrupt */
    rt_interrupt_enter();
302

303 304 305 306 307
    uart_isr(&serial1);

    /* leave interrupt */
    rt_interrupt_leave();
}
308

309
void DMA1_Channel5_IRQHandler(void) {
310 311
    /* enter interrupt */
    rt_interrupt_enter();
312

313 314
    dma_rx_done_isr(&serial1);

315 316 317 318 319 320
    /* leave interrupt */
    rt_interrupt_leave();
}
#endif /* RT_USING_UART1 */

#if defined(RT_USING_UART2)
321
/* UART2 device driver structure */
322 323 324 325
struct stm32_uart uart2 =
{
    USART2,
    USART2_IRQn,
326 327 328 329 330 331
    {
        DMA1_Channel6,
        DMA1_FLAG_GL6,
        DMA1_Channel6_IRQn,
        0,
    },
332 333
};
struct rt_serial_device serial2;
M
Ming, Bai 已提交
334

335 336
void USART2_IRQHandler(void)
{
337 338
    /* enter interrupt */
    rt_interrupt_enter();
339

340
    uart_isr(&serial2);
341

342 343 344 345 346
    /* leave interrupt */
    rt_interrupt_leave();
}

void DMA1_Channel6_IRQHandler(void) {
347 348
    /* enter interrupt */
    rt_interrupt_enter();
349 350

    dma_rx_done_isr(&serial2);
351 352 353

    /* leave interrupt */
    rt_interrupt_leave();
M
Ming, Bai 已提交
354
}
355
#endif /* RT_USING_UART2 */
M
Ming, Bai 已提交
356

357
#if defined(RT_USING_UART3)
358
/* UART3 device driver structure */
359 360 361 362
struct stm32_uart uart3 =
{
    USART3,
    USART3_IRQn,
363 364 365 366 367 368
    {
        DMA1_Channel3,
        DMA1_FLAG_GL3,
        DMA1_Channel3_IRQn,
        0,
    },
369 370 371 372
};
struct rt_serial_device serial3;

void USART3_IRQHandler(void)
M
Ming, Bai 已提交
373
{
374 375
    /* enter interrupt */
    rt_interrupt_enter();
376

377
    uart_isr(&serial3);
378

379 380 381 382 383
    /* leave interrupt */
    rt_interrupt_leave();
}

void DMA1_Channel3_IRQHandler(void) {
384 385
    /* enter interrupt */
    rt_interrupt_enter();
386 387

    dma_rx_done_isr(&serial3);
388 389 390 391 392

    /* leave interrupt */
    rt_interrupt_leave();
}
#endif /* RT_USING_UART3 */
M
Ming, Bai 已提交
393

U
unknown 已提交
394 395 396 397 398 399
#if defined(RT_USING_UART4)
/* UART4 device driver structure */
struct stm32_uart uart4 =
{
    UART4,
    UART4_IRQn,
400 401 402 403 404 405
    {
        DMA2_Channel3,
        DMA2_FLAG_GL3,
        DMA2_Channel3_IRQn,
        0,
    },
U
unknown 已提交
406 407 408 409 410
};
struct rt_serial_device serial4;

void UART4_IRQHandler(void)
{
411 412
    /* enter interrupt */
    rt_interrupt_enter();
U
unknown 已提交
413

414
    uart_isr(&serial4);
U
unknown 已提交
415

416 417 418 419 420
    /* leave interrupt */
    rt_interrupt_leave();
}

void DMA2_Channel3_IRQHandler(void) {
U
unknown 已提交
421 422
    /* enter interrupt */
    rt_interrupt_enter();
423 424

    dma_rx_done_isr(&serial4);
U
unknown 已提交
425 426 427 428

    /* leave interrupt */
    rt_interrupt_leave();
}
429
#endif /* RT_USING_UART4 */
U
unknown 已提交
430

431 432
static void RCC_Configuration(void)
{
433
#if defined(RT_USING_UART1)
434
    /* Enable UART GPIO clocks */
435
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
436 437 438
    /* Enable UART clock */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
#endif /* RT_USING_UART1 */
M
Ming, Bai 已提交
439

440
#if defined(RT_USING_UART2)
441
    /* Enable UART GPIO clocks */
442
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
443 444 445
    /* Enable UART clock */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
#endif /* RT_USING_UART2 */
M
Ming, Bai 已提交
446

447
#if defined(RT_USING_UART3)
448
    /* Enable UART GPIO clocks */
449
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
450 451 452
    /* Enable UART clock */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
#endif /* RT_USING_UART3 */
U
unknown 已提交
453 454 455

#if defined(RT_USING_UART4)
    /* Enable UART GPIO clocks */
456
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);
U
unknown 已提交
457 458 459
    /* Enable UART clock */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE);
#endif /* RT_USING_UART4 */
M
Ming, Bai 已提交
460 461
}

462
static void GPIO_Configuration(void)
M
Ming, Bai 已提交
463
{
464 465 466
    GPIO_InitTypeDef GPIO_InitStructure;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
M
Ming, Bai 已提交
467

468
#if defined(RT_USING_UART1)
469 470 471 472 473 474 475 476 477
    /* Configure USART Rx/tx PIN */
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_InitStructure.GPIO_Pin = UART1_GPIO_RX;
    GPIO_Init(UART1_GPIO, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Pin = UART1_GPIO_TX;
    GPIO_Init(UART1_GPIO, &GPIO_InitStructure);
#endif /* RT_USING_UART1 */
M
Ming, Bai 已提交
478

479
#if defined(RT_USING_UART2)
480 481 482
    /* Configure USART Rx/tx PIN */
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_InitStructure.GPIO_Pin = UART2_GPIO_RX;
483
    GPIO_Init(UART2_GPIO, &GPIO_InitStructure);
484 485 486 487 488

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Pin = UART2_GPIO_TX;
    GPIO_Init(UART2_GPIO, &GPIO_InitStructure);
#endif /* RT_USING_UART2 */
M
Ming, Bai 已提交
489

490
#if defined(RT_USING_UART3)
491 492 493 494 495 496 497 498 499
    /* Configure USART Rx/tx PIN */
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_InitStructure.GPIO_Pin = UART3_GPIO_RX;
    GPIO_Init(UART3_GPIO, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Pin = UART3_GPIO_TX;
    GPIO_Init(UART3_GPIO, &GPIO_InitStructure);
#endif /* RT_USING_UART3 */
U
unknown 已提交
500 501 502 503 504 505 506 507 508 509 510

#if defined(RT_USING_UART4)
    /* Configure USART Rx/tx PIN */
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_InitStructure.GPIO_Pin = UART4_GPIO_RX;
    GPIO_Init(UART4_GPIO, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Pin = UART4_GPIO_TX;
    GPIO_Init(UART4_GPIO, &GPIO_InitStructure);
#endif /* RT_USING_UART4 */
M
Ming, Bai 已提交
511 512
}

513
static void NVIC_Configuration(struct stm32_uart* uart)
M
Ming, Bai 已提交
514
{
515 516 517 518 519
    NVIC_InitTypeDef NVIC_InitStructure;

    /* Enable the USART1 Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel = uart->irq;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
520
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
521 522
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
M
Ming, Bai 已提交
523 524
}

525 526 527 528 529 530
static void DMA_Configuration(struct rt_serial_device *serial) {
    struct stm32_uart *uart = (struct stm32_uart *) serial->parent.user_data;
    struct rt_serial_rx_fifo *rx_fifo = (struct rt_serial_rx_fifo *)serial->serial_rx;
    DMA_InitTypeDef DMA_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;

531 532
    uart->dma.setting_recv_len = serial->config.bufsz;
    
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
    /* enable transmit idle interrupt */
    USART_ITConfig(uart->uart_device, USART_IT_IDLE , ENABLE);

    /* DMA clock enable */
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE);

    /* rx dma config */
    DMA_DeInit(uart->dma.rx_ch);
    DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(uart->uart_device->DR);
    DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t) rx_fifo->buffer;
    DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
    DMA_InitStructure.DMA_BufferSize = serial->config.bufsz;
    DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
    DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
    DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
    DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
550
    DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
551 552 553 554 555 556 557 558 559 560 561
    DMA_InitStructure.DMA_Priority = DMA_Priority_High;
    DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
    DMA_Init(uart->dma.rx_ch, &DMA_InitStructure);
    DMA_ClearFlag(uart->dma.rx_gl_flag);
    DMA_ITConfig(uart->dma.rx_ch, DMA_IT_TC, ENABLE);
    USART_DMACmd(uart->uart_device, USART_DMAReq_Rx, ENABLE);
    DMA_Cmd(uart->dma.rx_ch, ENABLE);

    /* rx dma interrupt config */
    NVIC_InitStructure.NVIC_IRQChannel = uart->dma.rx_irq_ch;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
562
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
563 564 565 566
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
}

567
void rt_hw_usart_init(void)
M
Ming, Bai 已提交
568
{
569 570
    struct stm32_uart* uart;
    struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
M
Ming, Bai 已提交
571

572 573
    RCC_Configuration();
    GPIO_Configuration();
M
Ming, Bai 已提交
574

575
#if defined(RT_USING_UART1)
576 577
    uart = &uart1;
    config.baud_rate = BAUD_RATE_115200;
M
Ming, Bai 已提交
578

579 580
    serial1.ops    = &stm32_uart_ops;
    serial1.config = config;
M
Ming, Bai 已提交
581

582
    NVIC_Configuration(uart);
M
Ming, Bai 已提交
583

584 585
    /* register UART1 device */
    rt_hw_serial_register(&serial1, "uart1",
586 587
                          RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX |
                          RT_DEVICE_FLAG_INT_TX |   RT_DEVICE_FLAG_DMA_RX,
588 589
                          uart);
#endif /* RT_USING_UART1 */
M
Ming, Bai 已提交
590

591
#if defined(RT_USING_UART2)
592 593 594 595 596 597
    uart = &uart2;

    config.baud_rate = BAUD_RATE_115200;
    serial2.ops    = &stm32_uart_ops;
    serial2.config = config;

598
    NVIC_Configuration(uart);
599

600
    /* register UART2 device */
601
    rt_hw_serial_register(&serial2, "uart2",
602 603
                          RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX |
                          RT_DEVICE_FLAG_INT_TX |   RT_DEVICE_FLAG_DMA_RX,
604 605
                          uart);
#endif /* RT_USING_UART2 */
M
Ming, Bai 已提交
606

607
#if defined(RT_USING_UART3)
608 609 610 611 612 613 614
    uart = &uart3;

    config.baud_rate = BAUD_RATE_115200;

    serial3.ops    = &stm32_uart_ops;
    serial3.config = config;

615
    NVIC_Configuration(uart);
616

617
    /* register UART3 device */
618
    rt_hw_serial_register(&serial3, "uart3",
619 620
                          RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX |
                          RT_DEVICE_FLAG_INT_TX |   RT_DEVICE_FLAG_DMA_RX,
621 622
                          uart);
#endif /* RT_USING_UART3 */
U
unknown 已提交
623 624 625 626 627 628 629 630 631

#if defined(RT_USING_UART4)
    uart = &uart4;

    config.baud_rate = BAUD_RATE_115200;

    serial4.ops    = &stm32_uart_ops;
    serial4.config = config;

632
    NVIC_Configuration(uart);
U
unknown 已提交
633 634 635

    /* register UART4 device */
    rt_hw_serial_register(&serial4, "uart4",
636 637
                          RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX |
                          RT_DEVICE_FLAG_INT_TX |   RT_DEVICE_FLAG_DMA_RX,
U
unknown 已提交
638 639
                          uart);
#endif /* RT_USING_UART4 */
M
Ming, Bai 已提交
640
}