summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/lm32/shared/milkymist_console/uart.c
blob: 229ee40985ceeb2e9ab5bfd4ffab937353748705 (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
67
68
69
/*
 *  Uart driver for Lattice Mico32 (lm32) 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) Yann Sionneau <yann.sionneau@telecom-sudparis.eu> (GSoC 2010)
 *  Telecom SudParis
 */

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

static inline int uartread(unsigned int reg)
{
  return *((int*)(reg));
}

static inline void uartwrite(unsigned int reg, int value)
{
  *((int*)(reg)) = value;
}

void BSP_uart_init(int baud)
{

  /* Set baud rate */
  uartwrite(MM_UART_DIV, CPU_FREQUENCY/baud/16);
}

void BSP_uart_polled_write(char ch)
{
  int ip;
  /* Wait until THR is empty. */
  uartwrite(MM_UART_RXTX, ch);
  do {
    lm32_read_interrupts(ip);
  } while (! (ip & MM_IRQ_UARTTX) );
  lm32_interrupt_ack(MM_IRQ_UARTTX);
}

char BSP_uart_polled_read( void )
{
  int ip;
  /* Wait until there is a byte in RBR */
  do {
    lm32_read_interrupts(ip);
  } while(! (ip & MM_IRQ_UARTRX) );
  lm32_interrupt_ack(MM_IRQ_UARTRX);
  return (char) uartread(MM_UART_RXTX);
}

char BSP_uart_is_character_ready(char *ch)
{
  int ip;
  lm32_read_interrupts(ip);
  if (ip & MM_IRQ_UARTRX)
    {
      *ch = (char) uartread(MM_UART_RXTX);
      lm32_interrupt_ack(MM_IRQ_UARTRX);
      return true;
    }
  *ch = '0';
  return false;
}