From b4ca71fc214ba3c58cec25661ba1f81cf7b1b871 Mon Sep 17 00:00:00 2001 From: hackbard Date: Fri, 7 Sep 2007 21:48:25 +0200 Subject: [PATCH] code clean up! --- betty/Makefile | 2 +- betty/betty.c | 254 +++--------------------------------------------- betty/betty.h | 17 ++-- betty/buttons.h | 3 +- betty/display.c | 37 +++++++ betty/display.h | 20 ++++ betty/spi.c | 35 +++++++ betty/spi.h | 22 +++++ betty/system.c | 39 ++++++++ betty/system.h | 19 ++++ betty/uart.c | 105 ++++++++++++++++++++ betty/uart.h | 22 +++++ 12 files changed, 322 insertions(+), 253 deletions(-) create mode 100644 betty/display.c create mode 100644 betty/display.h create mode 100644 betty/spi.c create mode 100644 betty/spi.h create mode 100644 betty/system.c create mode 100644 betty/system.h create mode 100644 betty/uart.c create mode 100644 betty/uart.h diff --git a/betty/Makefile b/betty/Makefile index 54f1d34..f740401 100644 --- a/betty/Makefile +++ b/betty/Makefile @@ -19,7 +19,7 @@ HOST_TARGET = lpcload fwdump CROSS_TARGET = fwbc.hex fwflash.hex betty.hex # betty deps -BETTY_DEPS = buttons.o +BETTY_DEPS = system.o uart.o buttons.o spi.o display.o # all projects all: $(HOST_TARGET) $(CROSS_TARGET) diff --git a/betty/betty.c b/betty/betty.c index 3b19542..31bc5a2 100644 --- a/betty/betty.c +++ b/betty/betty.c @@ -5,249 +5,14 @@ * */ +/* includes */ #include "betty.h" /* * functions */ -void mmap_init(u8 memtype) { - - MEMMAP=memtype; -} - -void pll_init(void) { - - /* configuration */ - PLLCFG=0x42; // multiplier = 3 (for cclk), dividor = 4 (for f_cco) - PLLCON=0x03; // enable and set as clk source for the lpc - /* feed sequence */ - PLLFEED=0xaa; - PLLFEED=0x55; - /* wait for lock */ - while(!(PLLSTAT&(1<<10))) - continue; -} - -void ext_mem_bank_init(void) { - - BCFG0=0x10000420; // flash 1 - BCFG1=0x00000c42; // lcd - BCFG2=0x10000420; // flash 2 -} - - -void pin_select_init() { - - /* - * a[19:2] -> address lines - */ - - PINSEL2=0x0d6041d4; -} - -void uart0_init(void) { - - /* select pins 0.0 and 0.1 as tx and rx */ - PINSEL0=(PINSEL0&~(0xf))|0x05; // pin select -> tx, rx - - /* configure uart0 */ - UART0_FCR=0x07; // enable fifo - UART0_LCR=0x83; // set dlab + word length - UART0_DLL=0x04; // br: 38400 @ 10/4 mhz - UART0_DLM=0x00; - UART0_LCR=0x03; // unset dlab -} - -void uart0_send_string(char *txbuf) { - - int i; - - i=0; - - while(txbuf[i]) { - UART0_THR=txbuf[i++]; - /* flush if tx buffer maximum reached */ - if(!(i%16)) - while(!(UART0_LSR&(1<<6))) - continue; - } - - /* flush if \n and \r do not fit in the tx buffer */ - if(i>14) - while(!(UART0_LSR&(1<<6))) - continue; - - UART0_THR='\n'; - UART0_THR='\r'; - - /* flush uart0 anyways */ - while(!(UART0_LSR&(1<<6))) - continue; -} - -void uart0_send_buf16(u16 *buf,int len) { - - int i; - - i=0; - - for(i=0;i>8)&0xff; - } -} - -void uart0_send_buf32(u32 *buf,int len) { - - int i; - - i=0; - - for(i=0;i>8)&0xff; - UART0_THR=(buf[i]>>16)&0xff; - UART0_THR=(buf[i]>>24)&0xff; - } -} - -void uart0_send_byte(u8 send) { - - while(!(UART0_LSR&(1<<5))) - continue; - - UART0_THR=send; -} - -u8 uart0_get_byte(void) { - - u8 rx; - - while(!(UART0_LSR&(1<<0))) - continue; - - rx=UART0_RBR; - - return rx; -} - -void pause(int cnt) { - - while(cnt--) - asm volatile ("nop"); -} - -void init_lcd(u8 s) { - - LCD_CMD=0xe1; // ? - LCD_CMD=0xe2; - pause(48000); - LCD_CMD=0xab; - LCD_CMD=0x27; - LCD_CMD=0x81; - LCD_CMD=0x3f; - LCD_CMD=0x65; - LCD_CMD=0x60; - LCD_CMD=0x1c; - LCD_CMD=0x61; - LCD_CMD=0x0a; - LCD_CMD=0x62; - LCD_CMD=0x75; - LCD_CMD=0x63; - LCD_CMD=0x81; - LCD_CMD=0x90; - LCD_CMD=0x88; - LCD_CMD=0x00; - LCD_CMD=0x89; - LCD_CMD=0x00; - LCD_CMD=0x8a; - LCD_CMD=0x33; - LCD_CMD=0x8b; - LCD_CMD=0x33; - LCD_CMD=0x8c; - LCD_CMD=0x66; - LCD_CMD=0x8d; - LCD_CMD=0x66; - LCD_CMD=0x8e; - LCD_CMD=0x99; - LCD_CMD=0x8f; - LCD_CMD=0x99; - if(s) { - LCD_CMD=0xa1; - LCD_CMD=0xc0; - } - else { - LCD_CMD=0xa0; - LCD_CMD=0xc8; - } - LCD_CMD=0x2e; - pause(48000); - LCD_CMD=0x2f; - LCD_CMD=0xa4; - LCD_CMD=0xa6; -} - -/* - * spi0 stuff (+ cc1100) - */ - -#define SLAVE 0 -#define MASTER 1 - -void spi1_init(u8 width,u8 type,u8 div) { - - if((width<8)|(width>16)) - width=8; - if(width==16) - width=0; - - div&=0xfe; - if(div<8) - div=8; - - S1SPCR=(1<<2)|(width<<8)|(type<<5); - S1SPCCR=div; -} - -#define cc1100_init spi1_init(8,MASTER,8) - -void spi1_send(u16 data) { - - S1SPDR=data; - - while(!(S1SPSR&(1<<7))) - continue; -} - -void bl_init(void) { - - IODIR0|=(1<<4); -} - -void bl_toggle(void) { - - if(IOPIN0&(1<<4)) - IOCLR0=(1<<4); - else - IOSET0=(1<<4); -} - -void bl_on(void) { - - IOCLR0=(1<<4); -} - -void bl_off(void) { - - IOSET0=(1<<4); -} +#define cc1100_init spi1_init(8,SPI_MASTER,8) /* * main function @@ -258,17 +23,24 @@ int main() { char buf[]="betty - live from flash at 0x80000000! ;)\r\n"; t_button button; + /* system init */ pll_init(); + + /* uart init */ uart0_init(); - ext_mem_bank_init(); - pin_select_init(); - init_lcd(0); + + /* display init */ bl_init(); + + /* button init */ button_init(&button); button_set_retries(&button,100); - pause(0xffffff); + /* + * start it ... + */ + pause(0xffffff); bl_toggle(); while(1) { diff --git a/betty/betty.h b/betty/betty.h index dae3f0a..41b3687 100644 --- a/betty/betty.h +++ b/betty/betty.h @@ -15,9 +15,15 @@ // processor #include "lpc2xxx.h" +// types +#include "types.h" + // api +#include "system.h" +#include "uart.h" #include "buttons.h" - +#include "spi.h" +#include "display.h" /* * defines @@ -43,15 +49,6 @@ #define LCD_CMD (*((volatile unsigned char *)(BANK1))) #define LCD_DATA (*((volatile unsigned char *)(BANK1+1))) -/* - * type definitions - */ - -typedef unsigned char u8; -typedef unsigned short u16; -typedef unsigned int u32; -typedef unsigned long long int u64; - /* * function prototypes */ diff --git a/betty/buttons.h b/betty/buttons.h index 3c69ad0..04ed9fa 100644 --- a/betty/buttons.h +++ b/betty/buttons.h @@ -9,7 +9,8 @@ #define BUTTONS_H /* includes */ -#include "betty.h" +#include "lpc2xxx.h" +#include "types.h" /* defines */ #define BUTTON_MAX 8 diff --git a/betty/display.c b/betty/display.c new file mode 100644 index 0000000..a5db338 --- /dev/null +++ b/betty/display.c @@ -0,0 +1,37 @@ +/* + * display.c - handling the display + * + * author: hackbard@hackdaworld.org + * + */ + +#include "display.h" + +/* + * functions + */ + +void bl_init(void) { + + PINSEL0&=~(1<<9|(1<<8)); + IODIR0|=(1<<4); +} + +void bl_toggle(void) { + + if(IOPIN0&(1<<4)) + IOCLR0=(1<<4); + else + IOSET0=(1<<4); +} + +void bl_on(void) { + + IOCLR0=(1<<4); +} + +void bl_off(void) { + + IOSET0=(1<<4); +} + diff --git a/betty/display.h b/betty/display.h new file mode 100644 index 0000000..cad2e76 --- /dev/null +++ b/betty/display.h @@ -0,0 +1,20 @@ +/* + * display.h - header file for the display handling + * + * author: hackbard@hackdaworld.org + * + */ + +#ifndef DISPLAY_H +#define DISPLAY_H + +#include "lpc2xxx.h" +#include "types.h" + +/* function prototypes */ +void bl_init(void); +void bl_toggle(void); +void bl_on(void); +void bl_off(void); + +#endif diff --git a/betty/spi.c b/betty/spi.c new file mode 100644 index 0000000..198213d --- /dev/null +++ b/betty/spi.c @@ -0,0 +1,35 @@ +/* + * spi.c - serial peripheral interface 0/1 + * + * author: hackbard@hackdaworld.org + * + */ + +#include "spi.h" + +/* + * functions + */ + +void spi1_init(u8 width,u8 type,u8 div) { + + if((width<8)|(width>16)) + width=8; + if(width==16) + width=0; + + div&=0xfe; + if(div<8) + div=8; + + S1SPCR=(1<<2)|(width<<8)|(type<<5); + S1SPCCR=div; +} + +void spi1_send(u16 data) { + + S1SPDR=data; + + while(!(S1SPSR&(1<<7))) + continue; +} diff --git a/betty/spi.h b/betty/spi.h new file mode 100644 index 0000000..c84a8cd --- /dev/null +++ b/betty/spi.h @@ -0,0 +1,22 @@ +/* + * spi-h - spi header file + * + * author: hackbard@hackdaworld.org + * + */ + +#ifndef SPI_H +#define SPI_H + +#include "lpc2xxx.h" +#include "types.h" + +/* defines */ +#define SPI_MASTER (1<<0) +#define SPI_SLAVE (1<<1) + +/* function prototypes */ +void spi1_init(u8 width,u8 type,u8 div); +void spi1_send(u16 data); + +#endif diff --git a/betty/system.c b/betty/system.c new file mode 100644 index 0000000..4876802 --- /dev/null +++ b/betty/system.c @@ -0,0 +1,39 @@ +/* + * system.c - misc system specific stuff + * + * author: hackbard@hackdaworld.org + * + */ + +#include "system.h" + +/* + * functions + */ + +void mmap_init(u8 memtype) { + + MEMMAP=memtype; +} + +void pll_init(void) { + + /* configuration */ + PLLCFG=0x42; // multiplier = 3 (for cclk), dividor = 4 (for f_cco) + PLLCON=0x03; // enable and set as clk source for the lpc + + /* feed sequence */ + PLLFEED=0xaa; + PLLFEED=0x55; + + /* wait for lock */ + while(!(PLLSTAT&(1<<10))) + continue; +} + +void pause(int cnt) { + + while(cnt--) + asm volatile ("nop"); +} + diff --git a/betty/system.h b/betty/system.h new file mode 100644 index 0000000..37dcaa9 --- /dev/null +++ b/betty/system.h @@ -0,0 +1,19 @@ +/* + * system.h - header file for misc system specific stuff + * + * author: hackbard@hackdaworld.org + * + */ + +#ifndef SYSTEM_H +#define SYSTEM_H + +#include "lpc2xxx.h" +#include "types.h" + +/* function prototypes */ +void mmap_init(u8 memtype); +void pll_init(void); +void pause(int cnt); + +#endif diff --git a/betty/uart.c b/betty/uart.c new file mode 100644 index 0000000..5760e3b --- /dev/null +++ b/betty/uart.c @@ -0,0 +1,105 @@ +/* + * uart.c - uart0/1 api + * + * author: hackbard@hackdaworld.org + * + */ + +#include "uart.h" + +/* + * functions + */ + +void uart0_init(void) { + + /* select pins 0.0 and 0.1 as tx and rx */ + PINSEL0=(PINSEL0&~(0xf))|0x05; + + /* configure uart 0 */ + UART0_FCR=0x07; // enable fifo + UART0_LCR=0x83; // set dlab + word length + UART0_DLL=0x04; // br: 115200 + UART0_DLM=0x00; + UART0_LCR=0x03; // unset dlab +} + +void uart0_send_string(char *txbuf) { + + int i; + + i=0; + + while(txbuf[i]) { + UART0_THR=txbuf[i++]; + /* flush if tx buffer maximum reached */ + if(!(i%16)) + while(!(UART0_LSR&(1<<6))) + continue; + } + + /* flush if \n and \r do not fit in the tx buffer */ + if(i>14) + while(!(UART0_LSR&(1<<6))) + continue; + + UART0_THR='\n'; + UART0_THR='\r'; + + /* flush uart0 anyways */ + while(!(UART0_LSR&(1<<6))) + continue; +} + +void uart0_send_buf16(u16 *buf,int len) { + + int i; + + i=0; + + for(i=0;i>8)&0xff; + } +} + +void uart0_send_buf32(u32 *buf,int len) { + + int i; + + i=0; + + for(i=0;i>8)&0xff; + UART0_THR=(buf[i]>>16)&0xff; + UART0_THR=(buf[i]>>24)&0xff; + } +} + +void uart0_send_byte(u8 send) { + + while(!(UART0_LSR&(1<<5))) + continue; + + UART0_THR=send; +} + +u8 uart0_get_byte(void) { + + u8 rx; + + while(!(UART0_LSR&(1<<0))) + continue; + + rx=UART0_RBR; + + return rx; +} + diff --git a/betty/uart.h b/betty/uart.h new file mode 100644 index 0000000..518cd4c --- /dev/null +++ b/betty/uart.h @@ -0,0 +1,22 @@ +/* + * uart.h - uart0/1 api header file + * + * author: hackbard@hackdaworld.org + * + */ + +#ifndef UART_H +#define UART_H + +#include "lpc2xxx.h" +#include "types.h" + +/* function prototypes */ +void uart0_init(void); +void uart0_send_string(char *txbuf); +void uart0_send_buf16(u16 *buf,int len); +void uart0_send_buf32(u32 *buf,int len); +void uart0_send_byte(u8 send); +u8 uart0_get_byte(void); + +#endif -- 2.20.1