The unified diff between revisions [8760ae92..] and [23a3e9a5..] is displayed below. It can also be downloaded as a raw diff.

This diff has been restricted to the following files: 'main.c'

#
# old_revision [8760ae9232295422550b79f09e55122390704b3c]
# new_revision [23a3e9a50b4034343e3bd217d2c225dcaec064dd]
#
# patch "main.c"
#  from [ef5435dd23ece2876b0d557328e6c83a7ee04ab6]
#    to [e1a823b4962f3e8dc43b519a1f57745854ae6689]
#
============================================================
--- main.c	ef5435dd23ece2876b0d557328e6c83a7ee04ab6
+++ main.c	e1a823b4962f3e8dc43b519a1f57745854ae6689
@@ -2,93 +2,26 @@
 #include "wmp.h"
 #include "i2c.h"
 #include "timer.h"
+#include "uart.h"
+#include "interrupt.h"
+#include "event.h"
 
-#define UARTBASE 0xE000C000
-
-#define RBR 0x00
-#define THR 0x00
-#define DLL 0x00
-#define DLM 0x04
-#define IER 0x04
-#define IIR 0x08
-#define FCR 0x08
-
-#define LCR 0x0c
-#define LSR 0x14
-#define SCR 0x1c
-#define ACR 0x20
-#define FDR 0x28
-#define TER 0x30
-
-#define UREG(x) (((volatile unsigned char *)UARTBASE)[x])
-
-#define U0THRE ((UREG(LSR) & (1<<5))) /* UART0 transmitter holding register is empty */
-#define U0DR ((UREG(LSR) & (1<<0))) /* UART0 data ready */
-
 #define PINSEL0 (*((volatile unsigned char *) 0xE002C000))
+#define FP0XDIR (*((volatile unsigned int *) 0x3FFFC000))
+#define FP0XVAL (*((volatile unsigned int *) 0x3FFFC014))
 
-void init_uart(void)
-{
-	UREG(FDR) = 0x10; /* DivAddVal = 0, MulVal = 1 */
+#define SCS (*((volatile unsigned int *) 0xe01fc1a0))
 
-	UREG(LCR) = 0x80;
-	UREG(DLM) = 0x00;
-	UREG(DLL) = 0x08; /* 14745600 / (16*115200) */
-	UREG(LCR) = 0x13;
-	UREG(FCR) = 0x07;
-}
 
 void init_pins(void)
 {
 	PINSEL0 = 0x00000055; /* P0.0 and P0.1 assigned to UART */
 			      /* P0.2 and P0.3 assigned to I2C  */
+	SCS = 1;
+	FP0XDIR = 0x04000000; /* P0.26 is an output */
+	FP0XVAL = 0x0;
 }
 
-void putch(char c) {
-	while (!U0THRE);
-	UREG(THR) = c;
-}
-
-void putstr(char *s) {
-	while (*s) putch(*s++);
-}
-
-void putint(unsigned int n) {
-	char s[11];
-	int i;
-
-	i = 10;
-	s[i] = '\0';
-
-	do {
-		s[--i] = n % 10 + '0';
-	} while ((n /= 10) > 0);
-
-	putstr(s+i);
-}
-
-void puthex(unsigned int n) {
-	char s[9];
-	int i;
-
-	i = 8;
-	s[i] = '\0';
-
-	do {
-		int x = n % 16;
-		if (x > 9)
-			x += 'A' - '0' - 10;
-		s[--i] = x + '0';
-	} while ((n /= 16) > 0);
-
-	putstr(s+i);
-}
-
-char getch(void) {
-	while (!U0DR);
-	return UREG(RBR);
-}
-
 void reply(char *str)
 {
 	putstr(str);
@@ -226,18 +159,44 @@ void average_sample(void)
 	putstr(")\r\n");
 }
 
+void timer_event_handler(void)
+{
+	wmp_start_sample();
+}
+
+void menu_handler(void);
+
 int main(void) {
-	int i;
+	init_interrupt();
 	init_uart();
 	init_i2c();
 	init_pins();
 	init_timer();
+
+	event_register(EVENT_UART_INPUT, menu_handler);
+
+	event_register(EVENT_I2C_COMPLETE, wmp_event_handler);
+
+	event_register(EVENT_TIMER, timer_event_handler);
+
 	putstr("Your entire life has been a mathematical error... a mathematical error I'm about to correct!\r\n");
 
+	putstr("prompt> ");
+
 	while (1) {
-		char c;
-		putstr("prompt> ");
-		c = getch();
+		FP0XVAL ^= 0x04000000;
+		event_dispatch();
+	}
+
+	return 0;
+}
+
+void menu_handler(void)
+{
+	int i;
+	char c;
+
+	while (getch(&c)) {
 		if (c == 0x0a)
 			continue;
 		putch(c);
@@ -260,8 +219,6 @@ int main(void) {
 			reply("Help is not available. Try a psychiatrist.");
 			break;
 		case 'T':
-			putstr("I2C status was: ");
-			puthex(i2cstat);
 			putstr(" I2C status is: ");
 			puthex(i2c_statreg());
 			reply(".");
@@ -269,18 +226,6 @@ int main(void) {
 			puthex(i2c_conreg());
 			reply(".");
 			break;
-		case 'S':
-			putstr("Sending START... ");
-			if (i2c_send_start())
-				reply("OK");
-			else
-				reply("FAIL");
-			break;
-		case 'O':
-			putstr("Sending STOP... ");
-			i2c_send_stop();
-			reply("sent");
-			break;
 		case 'I':
 			putstr("Initialising WMP... ");
 			if (wmp_init())
@@ -330,11 +275,18 @@ int main(void) {
 			puthex(timer_read());
 			reply(".");
 			break;
+		case 'P':
+			putstr("Initialising timer... ");
+			/* We want a 100Hz loop but two samples per iteration.
+			 * So, we go for 200Hz. */
+			timer_set_period(5*TIMER_MS);
+			reply("done");
+			wmp_start_zero();
+			break;
 		default:
 			reply("Unrecognised command.");
 			break;
 		}
+		putstr("prompt> ");
 	}
-
-	return 0;
 }