diff --git a/Makefile b/Makefile index 1c403a2..60da885 100644 --- a/Makefile +++ b/Makefile @@ -2,15 +2,16 @@ NAME=quad -OBJS=crt0.o main.o +SSRCS=crt0.s +CSRCS=main.c i2c.c -CFLAGS=-march=armv4t -msoft-float +CFLAGS=-march=armv4t -msoft-float -O1 LDSCRIPT=lpc2103_flash.ld CC=arm-elf-gcc OBJCOPY=arm-elf-objcopy -CLEANOBJS=$(OBJS) $(NAME).hex $(NAME).elf $(NAME).bin $(NAME).map +CLEANOBJS=$(OBJS) $(NAME).hex $(NAME).elf $(NAME).bin $(NAME).map .depend all: $(NAME).bin @@ -18,6 +19,9 @@ all: $(NAME).bin # In theory, nothing below here needs touching for as long as all of the # sources are in a single directory. +COBJS=$(CSRCS:.c=.o) +SOBJS=$(SSRCS:.s=.o) +OBJS=$(SOBJS) $(COBJS) .SUFFIXES: .elf .hex .bin @@ -36,3 +40,7 @@ run: $(NAME).hex clean: rm -rf $(CLEANOBJS) +depend: + $(CC) -MM $(CFLAGS) -nostdlib -nostartfiles $(CSRCS) >.depend + +.sinclude ".depend" diff --git a/i2c.c b/i2c.c new file mode 100644 index 0000000..9620d2d --- /dev/null +++ b/i2c.c @@ -0,0 +1,104 @@ + +#include "i2c.h" + +#define I2CBASE 0xE001C000 + +#define I2CONSET 0x00 +#define I2STAT 0x04 +#define I2DAT 0x08 +#define I2ADR 0x0c +#define I2SCLH 0x10 +#define I2SCLL 0x14 +#define I2CONCLR 0x18 + +#define IREG(x) (((volatile unsigned char *)I2CBASE)[x]) +#define IWREG(x) (((volatile unsigned int *)I2CBASE)[(x)/sizeof(unsigned int)]) + +#define AAFLAG (1<<2) +#define SIFLAG (1<<3) +#define STOFLAG (1<<4) +#define STAFLAG (1<<5) + +#define SI (IREG(I2CONSET) & SIFLAG) + +void init_i2c(void) +{ + IREG(I2CONSET) = 0x40; /* Enable I2C ready for Master Tx */ + /* Set up for just under 400kHz */ +#if 0 + IWREG(I2SCLL) = (25 * 100); + IWREG(I2SCLH) = (12 * 100); +#else + IWREG(I2SCLL) = 1475; /* ~5kHz */ + IWREG(I2SCLH) = 1475; +#endif +} + +int i2cstat; + +int i2c_wait(void) +{ + int stat; + while (!SI); + stat = IREG(I2STAT); + i2cstat = stat; + return stat; +} + +void i2c_go(void) +{ + IREG(I2CONCLR) = SIFLAG; +} + +int i2c_conreg(void) +{ + return IREG(I2CONSET); +} + +int i2c_statreg(void) +{ + return IREG(I2STAT); +} + +bool i2c_send_start(void) +{ + IREG(I2CONCLR) = STOFLAG | STAFLAG; + IREG(I2CONSET) = STAFLAG; +/* i2c_go(); */ + switch (i2c_wait()) { + case 0x08: + case 0x10: +/* IREG(I2CONCLR) = STAFLAG; */ + return TRUE; + default: +/* IREG(I2CONCLR) = STAFLAG; */ + return FALSE; + } +} + +bool i2c_send_address(int addr, bool write) +{ + return i2c_send_data((addr<<1) + (write?0:1)); +} + +bool i2c_send_data(int data) +{ + IREG(I2DAT) = data; + IREG(I2CONCLR) = STAFLAG | SIFLAG; + switch (i2c_wait()) { + case 0x18: + case 0x28: + return TRUE; + default: + return FALSE; + } +} + +void i2c_send_stop(void) +{ + IREG(I2CONCLR) = STAFLAG | AAFLAG; + IREG(I2CONSET) = STOFLAG; + i2c_go(); + /* Don't think we need to wait here. Could be wrong. */ +} + diff --git a/i2c.h b/i2c.h new file mode 100644 index 0000000..45a7bc4 --- /dev/null +++ b/i2c.h @@ -0,0 +1,11 @@ +#include "types.h" +void init_i2c(void); +extern int i2cstat; +int i2c_wait(void); +void i2c_go(void); +int i2c_conreg(void); +int i2c_statreg(void); +bool i2c_send_start(void); +bool i2c_send_address(int addr, bool write); +bool i2c_send_data(int data); +void i2c_send_stop(void); diff --git a/main.c b/main.c index 497aa39..3384eb4 100644 --- a/main.c +++ b/main.c @@ -1,4 +1,6 @@ +#include "i2c.h" + #define UARTBASE 0xE000C000 #define RBR 0x00 @@ -16,29 +18,47 @@ #define FDR 0x28 #define TER 0x30 -#define REG(x) (((volatile unsigned char *)UARTBASE)[x]) +#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 I2CBASE 0xE001C000 + +#define I2CONSET 0x00 +#define I2STAT 0x04 +#define I2DAT 0x08 +#define I2ADR 0x0c +#define I2SCLH 0x10 +#define I2SCLL 0x14 +#define I2CONCLR 0x18 + +#define IREG(x) (((volatile unsigned char *)I2CBASE)[x]) -#define U0THRE ((REG(LSR) & (1<<5))) /* UART0 transmitter holding register is empty */ -#define U0DR ((REG(LSR) & (1<<0))) /* UART0 data ready */ #define PINSEL0 (*((volatile unsigned char *) 0xE002C000)) void init_uart(void) { - REG(FDR) = 0x10; /* DivAddVal = 0, MulVal = 1 */ + UREG(FDR) = 0x10; /* DivAddVal = 0, MulVal = 1 */ - REG(LCR) = 0x80; - REG(DLM) = 0x00; - REG(DLL) = 0x08; /* 14745600 / (16*115200) */ - REG(LCR) = 0x13; - REG(FCR) = 0x07; + UREG(LCR) = 0x80; + UREG(DLM) = 0x00; + UREG(DLL) = 0x08; /* 14745600 / (16*115200) */ + UREG(LCR) = 0x13; + UREG(FCR) = 0x07; +} - PINSEL0 = 0x00000005; /* P0.0 and P0.1 assigned to UART */ +void init_pins(void) +{ + PINSEL0 = 0x00000055; /* P0.0 and P0.1 assigned to UART */ + /* P0.2 and P0.3 assigned to I2C */ } void putch(char c) { while (!U0THRE); - REG(THR) = c; + UREG(THR) = c; } void putstr(char *s) { @@ -59,9 +79,26 @@ void putint(unsigned int n) { 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 REG(RBR); + return UREG(RBR); } void reply(char *str) @@ -74,6 +111,8 @@ unsigned int count = 0; int main(void) { init_uart(); + init_i2c(); + init_pins(); putstr("Your entire life has been a mathematical error... a mathematical error I'm about to correct!\r\n"); while (1) { @@ -101,6 +140,49 @@ int main(void) { case '?': reply("Help is not available. Try a psychiatrist."); break; + case 'R': + putstr("I2C register is: "); + puthex(i2c_conreg()); + reply("."); + break; + case 'T': + putstr("I2C status was: "); + puthex(i2cstat); + putstr(" I2C status is: "); + puthex(i2c_statreg()); + reply("."); + break; + case 'S': + putstr("Sending START... "); + if (i2c_send_start()) + reply("OK"); + else + reply("FAIL"); + break; + case 'D': + putstr("Sending address... "); + if (i2c_send_address(0x53, TRUE)) + reply("OK"); + else + reply("FAIL"); + break; + case 'B': + putstr("Sending bytes... "); + if (i2c_send_data(0xfe)) + reply("OK"); + else + reply("FAIL"); + + if (i2c_send_data(0x04)) + reply("OK"); + else + reply("FAIL"); + break; + case 'O': + putstr("Sending STOP... "); + i2c_send_stop(); + reply("sent"); + break; default: reply("Unrecognised command."); break; diff --git a/types.h b/types.h new file mode 100644 index 0000000..680c9a6 --- /dev/null +++ b/types.h @@ -0,0 +1,3 @@ +typedef int bool; +#define TRUE 1 +#define FALSE 0