summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/lm32/shared/milkymist_console/uart.c
blob: f32a1615fd46f70d9548a0caf637d2a1a40252da (plain) (blame)
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
41
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
/*
 *  Driver for Milkymist UART
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.com/license/LICENSE.
 *
 *  $Id$
 *
 *  COPYRIGHT (c) 2010 Sebastien Bourdeauducq
 *  COPYRIGHT (c) Yann Sionneau <yann.sionneau@telecom-sudparis.eu> (GSoC 2010)
 *  Telecom SudParis
 */

#include <rtems.h>
#include <rtems/libio.h>
#include "../include/system_conf.h"
#include "uart.h"

bool BSP_uart_txbusy;

void BSP_uart_init(int baud)
{
  MM_WRITE(MM_UART_DIV, CPU_FREQUENCY/baud/16);
}

void BSP_uart_polled_write(char ch)
{
  int ip;
  rtems_interrupt_level level;

  rtems_interrupt_disable(level);
  if (BSP_uart_txbusy) {
    /* wait for the end of the transmission by the IRQ-based driver */
    do {
      lm32_read_interrupts(ip);
    } while (!(ip & (1 << MM_IRQ_UARTTX)));
    lm32_interrupt_ack(1 << MM_IRQ_UARTTX);
  }
  MM_WRITE(MM_UART_RXTX, ch);
  do {
    lm32_read_interrupts(ip);
  } while (!(ip & (1 << MM_IRQ_UARTTX)));
  /* if TX was busy, do not ack the IRQ
   * so that the IRQ-based driver ISR is run */
  if (!BSP_uart_txbusy)
    lm32_interrupt_ack(1 << MM_IRQ_UARTTX);
  rtems_interrupt_enable(level);
}

int BSP_uart_polled_read(void)
{
  int ip;
  char r;
  rtems_interrupt_level level;

  rtems_interrupt_disable(level);
  do {
    lm32_read_interrupts(ip);
  } while (!(ip & (1 << MM_IRQ_UARTRX)));
  lm32_interrupt_ack(1 << MM_IRQ_UARTRX);
  r = MM_READ(MM_UART_RXTX);
  rtems_interrupt_enable(level);

  return r;
}