KM3NeT CLB  2.0
KM3NeT CLB v2 Embedded Software
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
suart.c
1 /*
2  * KM3NeT CLB v2 Firmware
3  *
4  * Copyright 2013 KM3NeT Collaboration
5  *
6  * All Rights Reserved.
7  *
8  *
9  * File : suart.c
10  * Created : 25 jan 2013
11  * Author : Vincent van Beveren
12  */
13 #include <assert.h>
14 
15 #include "kernel/tm.h"
16 #include "kernel/err.h"
17 #include "drv/wb/suart.h"
18 
19 static void queueChar(SUART_Descriptor * desc) ;
20 
21 #define UART_TIMEOUT_MS 100
22 
23 #define _UART_CFG( IDX, UART, IRQ ) \
24  BF_INIT(SuartFifo ## IDX, SUART_FIFORX_CAP) \
25  static SUART_Descriptor SUARTD ## IDX = { \
26  .dev = UART, \
27  .rxfifo = &SuartFifo ## IDX, \
28  .interrupt = IRQ \
29  }; \
30  SUART_Descriptor* UART_DESCR_PTR(IDX) = &SUARTD ## IDX; \
31  void IRQ_HANDLER(IRQ) { \
32  queueChar(UART_DESCR_PTR(IDX)); \
33  }
34 
35 #define UART_CFG( IDX, UART, IRQ ) _UART_CFG( IDX, UART, IRQ )
36 
37 UART_CFGS
38 
39 #undef _UART_CFG
40 #undef UART_CFG
41 
42 
43 
44 void suartInit(SUART_Descriptor * desc, unsigned int baudrate)
45 {
46  assert((baudrate & 0xFFF00000 ) == 0); // baud rate should not exceed 20 bits.
47  desc->dev->RATE = ( ( baudrate << 12 ) +
48  ( WISHBONE_FREQ >> 8 ) ) /
49  ( WISHBONE_FREQ >> 7 );
50  __irqDisable();
51  irqMaskSet(desc->interrupt, true);
52  __irqEnable();
53 }
54 
55 
56 
57 
58 bool suartTx(SUART_Descriptor * desc, char c)
59 {
60  uint32_t to = timeOutInit(UART_TIMEOUT_MS);
61  while(!suartTxReady(desc))
62  {
63  if (timeOut(to)) {
65  return false;
66  }
67  }
68 
69  desc->dev->TXDATA = c;
70 
71  return true;
72 }
73 bool suartRx(SUART_Descriptor * desc,char * c)
74 {
75 /* uint32_t to = timeOutInit(UART_TIMEOUT_MS);
76  while (!suartRxReady(desc)) {
77  if (timeOut(to)) {
78  errSet(ERROR(E_SUART_TIMEOUT));
79  return false;
80  }
81  }
82 
83  *c = desc->dev->RXDATA & 0xFF;
84  return true;*/
85  uint32_t to = timeOutInit(UART_TIMEOUT_MS);
86  while (bfEmpty(desc->rxfifo)) {
87  if (timeOut(to)) {
89  return false;
90  }
91  }
92  __irqDisable();
93  bfRead(desc->rxfifo,(uint8_t*) c);
94  __irqEnable();
95  return true;
96 
97 }
98 
99 static void queueChar(SUART_Descriptor * desc) {
100  // code to queue byte using the byte fifo
101  char c;
102  if( desc->dev->STATUS & SUART_STATUS_RX_RDY ) {
103  c = desc->dev->RXDATA & 0xFF;
104  bfWrite(desc->rxfifo, c);
105 
106  }
107 
108 }
109 
110 
111 
112 
static bool bfEmpty(ByteFifo *const bf)
Returns whether or not the byte-fifo is empty.
Definition: bytefifo.h:72
const volatile unsigned int RXDATA
Receive register.
Definition: dev_suart.h:44
const volatile unsigned int STATUS
Status Register.
Definition: dev_suart.h:41
static void __irqEnable()
Enabled IRQ&#39;s on a global level.
Definition: lm32.h:75
bool bfWrite(ByteFifo *const bf, uint8_t b)
Writes a byte to the byte-fifo.
Definition: bytefifo.c:17
#define E_SUART_TIMEOUT
Receive / Transmission timeout.
Definition: suart.h:33
static void __irqDisable()
Disables IRQ&#39;s on a global level.
Definition: lm32.h:62
static bool suartTxReady(SUART_Descriptor *desc)
Returns whether or not the TX buffer is empty.
Definition: suart.h:80
White Rabbit Simple UART Driver.
#define SUART_STATUS_RX_RDY
Uart has rx data ready.
Definition: dev_suart.h:29
volatile unsigned int RATE
Data rate register.
Definition: dev_suart.h:42
void suartInit(SUART_Descriptor *desc, unsigned int baudrate)
Initializes the simple UART.
Definition: suart.c:44
void irqMaskSet(int irq, bool set)
Set/clear the interrupt mask for the specified IRQ.
Definition: lm32.c:77
static uint32_t timeOutInit(uint32_t msec)
Initializes a timeout with the specified no of msecs.
Definition: tm.h:53
static bool timeOut(uint32_t to)
Checks whether or not the timeout has expired.
Definition: tm.h:77
Manages the global system error.
Simple timer functions.
bool suartRx(SUART_Descriptor *desc, char *c)
Receives a character.
Definition: suart.c:73
volatile unsigned int TXDATA
Transmit register.
Definition: dev_suart.h:43
bool bfRead(ByteFifo *const bf, uint8_t *const b)
Reads a byte from the byte-fifo.
Definition: bytefifo.c:28
bool errSet(uint32_t code, const char *error, const char *name)
Sets an error.
#define ERROR(CODE,...)
Expands an error code to an error code with a description (if ERROR_W_DESCR is declared).
bool suartTx(SUART_Descriptor *desc, char c)
Transmits a character.
Definition: suart.c:58