Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 2 commits
  • 2 files changed
  • 0 comments
  • 1 contributor
54  milkymist/uart/__init__.py
@@ -9,7 +9,8 @@ def __init__(self, address, clk_freq, baud=115200):
9 9
 		self._divisor = RegisterField("divisor", 16, reset=int(clk_freq/baud/16))
10 10
 		
11 11
 		self._tx_event = EventSourceLevel()
12  
-		self.events = EventManager(self._tx_event)
  12
+		self._rx_event = EventSourcePulse()
  13
+		self.events = EventManager(self._tx_event, self._rx_event)
13 14
 		self.bank = csrgen.Bank([self._rxtx, self._divisor] + self.events.get_registers(),
14 15
 			address=address)
15 16
 
@@ -28,10 +29,11 @@ def get_fragment(self):
28 29
 				enable16_counter.eq(self._divisor.field.r - 1))
29 30
 		]
30 31
 		
  32
+		# TX
31 33
 		tx_reg = Signal(BV(8))
32 34
 		tx_bitcount = Signal(BV(4))
33 35
 		tx_count16 = Signal(BV(4))
34  
-		tx_busy = Signal()
  36
+		tx_busy = self._tx_event.trigger
35 37
 		sync += [
36 38
 			If(self._rxtx.re,
37 39
 				tx_reg.eq(self._rxtx.r),
@@ -55,7 +57,53 @@ def get_fragment(self):
55 57
 				)
56 58
 			)
57 59
 		]
58  
-		comb.append(self._tx_event.trigger.eq(tx_busy))
  60
+		
  61
+		# RX
  62
+		rx0 = Signal() # sychronize
  63
+		rx = Signal()
  64
+		sync += [
  65
+			rx0.eq(self.rx),
  66
+			rx.eq(rx0)
  67
+		]
  68
+		rx_r = Signal()
  69
+		rx_reg = Signal(BV(8))
  70
+		rx_bitcount = Signal(BV(4))
  71
+		rx_count16 = Signal(BV(4))
  72
+		rx_busy = Signal()
  73
+		rx_done = self._rx_event.trigger
  74
+		rx_data = self._rxtx.w
  75
+		sync += [
  76
+			rx_done.eq(0),
  77
+			If(enable16,
  78
+				rx_r.eq(rx),
  79
+				If(~rx_busy,
  80
+					If(~rx & rx_r, # look for start bit
  81
+						rx_busy.eq(1),
  82
+						rx_count16.eq(7),
  83
+						rx_bitcount.eq(0)
  84
+					)
  85
+				).Else(
  86
+					rx_count16.eq(rx_count16 + 1),
  87
+					If(rx_count16 == 0,
  88
+						rx_bitcount.eq(rx_bitcount + 1),
  89
+
  90
+						If(rx_bitcount == 0,
  91
+							If(rx, # verify start bit
  92
+								rx_busy.eq(0)
  93
+							)
  94
+						).Elif(rx_bitcount == 9,
  95
+							rx_busy.eq(0),
  96
+							If(rx, # verify stop bit
  97
+								rx_data.eq(rx_reg),
  98
+								rx_done.eq(1)
  99
+							)
  100
+						).Else(
  101
+							rx_reg.eq(Cat(rx_reg[1:], rx))
  102
+						)
  103
+					)
  104
+				)
  105
+			)
  106
+		]
59 107
 		
60 108
 		return self.bank.get_fragment() \
61 109
 			+ self.events.get_fragment() \
7  software/bios/main.c
@@ -4,11 +4,16 @@
4 4
 
5 5
 int main(void)
6 6
 {
  7
+	char c;
  8
+	
7 9
 	irq_setmask(0);
8 10
 	irq_setie(1);
9 11
 	uart_init();
10 12
 	
11 13
 	printf("Hello World with IRQs\n");
12 14
 	
13  
-	while(1);
  15
+	while(1) {
  16
+		c = uart_read();
  17
+		printf("You typed: %c\n", c);
  18
+	}
14 19
 }

No commit comments for this range

Something went wrong with that request. Please try again.