From 63c6126af33480f526086dd17b0ddefab48db52c Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Mon, 13 Jun 2011 15:22:37 +0200 Subject: [PATCH 01/11] Fix dependencies so make MODULE=foo works without make clean :-) --- modules/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/Makefile b/modules/Makefile index 1cc23f0..e4e19db 100644 --- a/modules/Makefile +++ b/modules/Makefile @@ -59,6 +59,6 @@ clean: $(WRAPSRC): ./mkwrapper $(OBJS) > $@ -.PHONY: $(LIBFILE) +.PHONY: $(LIBFILE) $(WRAPSRC) .SUFFIXES: From f0801647c5c1b8667f151c8844a9f19966474a5a Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Mon, 13 Jun 2011 19:56:51 +0200 Subject: [PATCH 02/11] Increase decoder buffer as not to crash on "g" --- lcd/decoder.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lcd/decoder.c b/lcd/decoder.c index 10b54f0..d4ff7b8 100644 --- a/lcd/decoder.c +++ b/lcd/decoder.c @@ -1,7 +1,7 @@ #include #include -#define MAXCHR (20*10) +#define MAXCHR (30*20) static uint8_t buf[MAXCHR]; // Local function: Get next nibble. From 31634eaafc330e13bbd4312902b7d98bc144db87 Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Mon, 13 Jun 2011 19:58:12 +0200 Subject: [PATCH 03/11] Backdoor-ISP is now up+left+enter --- modules/menutest.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/menutest.c b/modules/menutest.c index 5219886..d4545f9 100644 --- a/modules/menutest.c +++ b/modules/menutest.c @@ -94,7 +94,7 @@ uint8_t getInput(void) { result += BTN_RIGHT; } - if (result == (BTN_LEFT+BTN_RIGHT)){ /* Development hack */ + if (result == (BTN_LEFT+BTN_TOP+BTN_ENTER)){ /* Development hack */ gotoISP(); } From 572bf9439b8fb263977dd0dd31aa33d00641eb89 Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Mon, 13 Jun 2011 21:06:09 +0200 Subject: [PATCH 04/11] Move some functions into basic/ and add prototypes for them --- Makefile | 3 +-- basic/basic.h | 8 ++++++++ basic/delayms.c | 24 ++++++++++++++++++++++++ reinvoke_isp.c => basic/reinvoke_isp.c | 6 ++++++ lcd/display.c | 23 +---------------------- 5 files changed, 40 insertions(+), 24 deletions(-) create mode 100644 basic/delayms.c rename reinvoke_isp.c => basic/reinvoke_isp.c (96%) diff --git a/Makefile b/Makefile index 87c8faf..46000d0 100644 --- a/Makefile +++ b/Makefile @@ -7,9 +7,8 @@ OBJS = main.o VPATH += OBJS += -OBJS += basic/basic.o +OBJS += basic/basic.o basic/reinvoke_isp.o basic/delayms.o OBJS += eeprom/eeprom.o -OBJS += reinvoke_isp.o LIBS += core/libcore.a lcd/liblcd.a modules/libmodules.a ########################################################################## diff --git a/basic/basic.h b/basic/basic.h index 2aee0d6..5c5c29a 100644 --- a/basic/basic.h +++ b/basic/basic.h @@ -116,4 +116,12 @@ void rbInit(void); +// reinvoke_isp.c +void ReinvokeISP(void); +void EnableWatchdog(uint32_t ms); +void EnterISP(void); + +// delayms.c +void delayms(uint32_t ms); + #endif diff --git a/basic/delayms.c b/basic/delayms.c new file mode 100644 index 0000000..17857cd --- /dev/null +++ b/basic/delayms.c @@ -0,0 +1,24 @@ +#include +#include "lpc134x.h" + +/**************************************************************************/ +/*! + Approximates a 1 millisecond delay using "nop". This is less + accurate than a dedicated timer, but is useful in certain situations. + + The number of ticks to delay depends on the optimisation level set + when compiling (-O). Depending on the compiler settings, one of the + two defined values for 'delay' should be used. +*/ +/**************************************************************************/ +void delayms(uint32_t ms) +{ + uint32_t delay = ms * ((CFG_CPU_CCLK / 100) / 45); // Release Mode (-Os) + // uint32_t delay = ms * ((CFG_CPU_CCLK / 100) / 120); // Debug Mode (No optimisations) + + while (delay > 0) + { + __asm volatile ("nop"); + delay--; + } +} diff --git a/reinvoke_isp.c b/basic/reinvoke_isp.c similarity index 96% rename from reinvoke_isp.c rename to basic/reinvoke_isp.c index cc3e6de..5e5afe5 100644 --- a/reinvoke_isp.c +++ b/basic/reinvoke_isp.c @@ -71,3 +71,9 @@ void EnableWatchdog(uint32_t ms){ WDT_WDFEED = WDT_WDFEED_FEED1; WDT_WDFEED = WDT_WDFEED_FEED2; }; + +void ISPandReset(int delay){ + EnableWatchdog(1000*delay); + ReinvokeISP(); +}; + diff --git a/lcd/display.c b/lcd/display.c index 80cdde3..9b77ca8 100644 --- a/lcd/display.c +++ b/lcd/display.c @@ -2,28 +2,7 @@ #include #include "lpc134x.h" #include "gpio/gpio.h" - -/**************************************************************************/ -/*! - Approximates a 1 millisecond delay using "nop". This is less - accurate than a dedicated timer, but is useful in certain situations. - - The number of ticks to delay depends on the optimisation level set - when compiling (-O). Depending on the compiler settings, one of the - two defined values for 'delay' should be used. -*/ -/**************************************************************************/ -void delayms(uint32_t ms) -{ - uint32_t delay = ms * ((CFG_CPU_CCLK / 100) / 45); // Release Mode (-Os) - // uint32_t delay = ms * ((CFG_CPU_CCLK / 100) / 120); // Debug Mode (No optimisations) - - while (delay > 0) - { - __asm volatile ("nop"); - delay--; - } -} +#include "basic/basic.h" /**************************************************************************/ /* Utility routines to manage nokia display */ From 94003362a49d50e11402e526aac8fbd24dd1890f Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Mon, 13 Jun 2011 23:09:37 +0200 Subject: [PATCH 05/11] set USB_CONNECT high on bootup to not confuse usb host. Also remove adcInit and pmuInit from main.c because systemInit already calls them. --- basic/basic.c | 3 +++ basic/basic.h | 2 ++ main.c | 6 ------ 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/basic/basic.c b/basic/basic.c index 51d8e69..1c7e43d 100644 --- a/basic/basic.c +++ b/basic/basic.c @@ -10,6 +10,9 @@ void rbInit() { gpioSetDir(RB_PWR_GOOD, gpioDirection_Output); gpioSetValue (RB_PWR_GOOD, 0); + // Disable USB Connect (we don't want USB by default) + gpioSetDir(USB_CONNECT, gpioDirection_Output); + gpioSetValue(USB_CONNECT, 1); // prepare buttons gpioSetDir(RB_BTN0, gpioDirection_Input); diff --git a/basic/basic.h b/basic/basic.h index 5c5c29a..c6ba32e 100644 --- a/basic/basic.h +++ b/basic/basic.h @@ -114,6 +114,8 @@ #define RB_EEPROM_ADDR 0xA0 +#define USB_CONNECT 0,6 + void rbInit(void); // reinvoke_isp.c diff --git a/main.c b/main.c index 1aa0d35..3ec1e8e 100644 --- a/main.c +++ b/main.c @@ -17,16 +17,10 @@ int main(void) { // Configure cpu and mandatory peripherals systemInit(); - //enable clocks to adc and watchdog - pmuInit(); - - // initialise basic badge functions rbInit(); lcdInit(); // display - - adcInit(); lcdFill(0); lcdDisplay(0); From 2dfe80221a672bc524d8d24d3dbbdbd5e7261d4e Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Mon, 13 Jun 2011 23:17:09 +0200 Subject: [PATCH 06/11] Add VoltageCheck to shutdown on low power --- Makefile | 2 +- basic/basic.h | 3 +++ basic/voltage.c | 20 ++++++++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 basic/voltage.c diff --git a/Makefile b/Makefile index 46000d0..54652e2 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ OBJS = main.o VPATH += OBJS += -OBJS += basic/basic.o basic/reinvoke_isp.o basic/delayms.o +OBJS += basic/basic.o basic/reinvoke_isp.o basic/delayms.o basic/voltage.o OBJS += eeprom/eeprom.o LIBS += core/libcore.a lcd/liblcd.a modules/libmodules.a diff --git a/basic/basic.h b/basic/basic.h index c6ba32e..73cf337 100644 --- a/basic/basic.h +++ b/basic/basic.h @@ -126,4 +126,7 @@ void EnterISP(void); // delayms.c void delayms(uint32_t ms); +// voltage.c +void VoltageCheck(void); + #endif diff --git a/basic/voltage.c b/basic/voltage.c new file mode 100644 index 0000000..0732e26 --- /dev/null +++ b/basic/voltage.c @@ -0,0 +1,20 @@ +#include + +#include "basic/basic.h" + + +void VoltageCheck(void){ + uint32_t results; + + results = adcRead(1); + results *= 10560; + results /= 1024; + + if( results < 3500 ){ + gpioSetValue (RB_PWR_GOOD, 0); + gpioSetValue (RB_LCD_BL, 0); + SCB_SCR |= SCB_SCR_SLEEPDEEP; + PMU_PMUCTRL = PMU_PMUCTRL_DPDEN_DEEPPOWERDOWN; + __asm volatile ("WFI"); + }; +}; From 93a5e2696beecae5b13f85e0534585246314c8f3 Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Mon, 13 Jun 2011 23:19:18 +0200 Subject: [PATCH 07/11] Enable systick (10ms) by default and allow modules to be called from systick Also add VoltageCheck once per sec by default --- core/systick/systick.c | 16 +++------------- modules/default.c | 15 ++++++++++++++- modules/mkwrapper | 10 ++++++++++ 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/core/systick/systick.c b/core/systick/systick.c index ebf50b8..bbec5e5 100644 --- a/core/systick/systick.c +++ b/core/systick/systick.c @@ -63,14 +63,11 @@ #include "systick.h" -#ifdef CFG_SDCARD -#include "drivers/fatfs/diskio.h" -volatile uint32_t fatTicks = 0; -#endif - volatile uint32_t systickTicks = 0; // 1ms tick counter volatile uint32_t systickRollovers = 0; +void tick_wrapper(void); + /**************************************************************************/ /*! @brief Systick interrupt handler @@ -83,14 +80,7 @@ void SysTick_Handler (void) // Increment rollover counter if (systickTicks == 0xFFFFFFFF) systickRollovers++; - #ifdef CFG_SDCARD - fatTicks++; - if (fatTicks == 10) - { - fatTicks = 0; - disk_timerproc(); - } - #endif + tick_wrapper(); } /**************************************************************************/ diff --git a/modules/default.c b/modules/default.c index 33bf3de..3d2f8d2 100644 --- a/modules/default.c +++ b/modules/default.c @@ -3,5 +3,18 @@ /**************************************************************************/ void module_default(void) { + systickInit(10); return; -} +}; + +// every 10 ms +void tick_default(void) { + static int ctr; + ctr++; + if(ctr>100){ + VoltageCheck(); + ctr=0; + }; + + return; +}; diff --git a/modules/mkwrapper b/modules/mkwrapper index 2b6ef98..8a3f1e2 100755 --- a/modules/mkwrapper +++ b/modules/mkwrapper @@ -3,6 +3,7 @@ for a in $* ; do base=${a%.o} echo "void module_$base(void);" + echo "void tick_$base(void);" done echo @@ -14,3 +15,12 @@ for a in $* ; do done echo "}" + +echo "void tick_wrapper(void){" + +for a in $* ; do + base=${a%.o} + grep -q \ tick_$base ${base}.c && echo "tick_$base();" +done + +echo "}" From 2b73b151575385c92e5652e390e80d837a457234 Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Mon, 13 Jun 2011 23:39:21 +0200 Subject: [PATCH 08/11] Add low-voltage warning blink --- basic/basic.h | 1 + basic/voltage.c | 6 +++++- modules/default.c | 11 +++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/basic/basic.h b/basic/basic.h index 73cf337..c2eb6ac 100644 --- a/basic/basic.h +++ b/basic/basic.h @@ -128,5 +128,6 @@ void delayms(uint32_t ms); // voltage.c void VoltageCheck(void); +uint32_t GetVoltage(void); #endif diff --git a/basic/voltage.c b/basic/voltage.c index 0732e26..1ed2d69 100644 --- a/basic/voltage.c +++ b/basic/voltage.c @@ -2,9 +2,9 @@ #include "basic/basic.h" +uint32_t results=5000; void VoltageCheck(void){ - uint32_t results; results = adcRead(1); results *= 10560; @@ -18,3 +18,7 @@ void VoltageCheck(void){ __asm volatile ("WFI"); }; }; + +uint32_t GetVoltage(void){ + return results; +}; diff --git a/modules/default.c b/modules/default.c index 3d2f8d2..86581f2 100644 --- a/modules/default.c +++ b/modules/default.c @@ -1,4 +1,5 @@ #include +#include "basic/basic.h" /**************************************************************************/ @@ -15,6 +16,16 @@ void tick_default(void) { VoltageCheck(); ctr=0; }; + if(ctr%5==0){ + if(GetVoltage()<3600){ + IOCON_PIO1_11 = 0x0; + gpioSetDir(RB_LED3, gpioDirection_Output); + if( (ctr/5)%10 == 1 ) + gpioSetValue (RB_LED3, 1); + else + gpioSetValue (RB_LED3, 0); + }; + }; return; }; From 4cd3460138c9fd0a5b66a3fa8d8364f56a3aceb2 Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Mon, 13 Jun 2011 23:53:02 +0200 Subject: [PATCH 09/11] Add keyin.c, generic joystick input function --- Makefile | 1 + basic/basic.h | 11 ++++++++++- basic/keyin.c | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 basic/keyin.c diff --git a/Makefile b/Makefile index 54652e2..7d01469 100644 --- a/Makefile +++ b/Makefile @@ -8,6 +8,7 @@ OBJS = main.o VPATH += OBJS += OBJS += basic/basic.o basic/reinvoke_isp.o basic/delayms.o basic/voltage.o +OBJS += basic/keyin.o OBJS += eeprom/eeprom.o LIBS += core/libcore.a lcd/liblcd.a modules/libmodules.a diff --git a/basic/basic.h b/basic/basic.h index c2eb6ac..020399c 100644 --- a/basic/basic.h +++ b/basic/basic.h @@ -121,7 +121,7 @@ void rbInit(void); // reinvoke_isp.c void ReinvokeISP(void); void EnableWatchdog(uint32_t ms); -void EnterISP(void); +void ISPandReset(int delay); // delayms.c void delayms(uint32_t ms); @@ -130,4 +130,13 @@ void delayms(uint32_t ms); void VoltageCheck(void); uint32_t GetVoltage(void); +// keyin.c +#define BTN_NONE 0 +#define BTN_UP (1<<0) +#define BTN_DOWN (1<<1) +#define BTN_LEFT (1<<2) +#define BTN_RIGHT (1<<3) +#define BTN_ENTER (1<<4) +uint8_t getInput(void); + #endif diff --git a/basic/keyin.c b/basic/keyin.c new file mode 100644 index 0000000..b0d6964 --- /dev/null +++ b/basic/keyin.c @@ -0,0 +1,38 @@ +#include +#include "basic/basic.h" + +uint8_t getInput(void) { + uint8_t result = BTN_NONE; + + if (gpioGetValue(RB_BTN3)==0) { + while(gpioGetValue(RB_BTN3)==0); + result += BTN_UP; + } + + if (gpioGetValue(RB_BTN2)==0) { + while(gpioGetValue(RB_BTN2)==0); + result += BTN_DOWN; + } + + if (gpioGetValue(RB_BTN4)==0) { + while(gpioGetValue(RB_BTN4)==0); + result += BTN_ENTER; + } + + if (gpioGetValue(RB_BTN0)==0) { + while(gpioGetValue(RB_BTN0)==0); + result += BTN_LEFT; + } + + if (gpioGetValue(RB_BTN1)==0) { + while(gpioGetValue(RB_BTN1)==0); + result += BTN_RIGHT; + } + + if (result == (BTN_LEFT+BTN_UP+BTN_ENTER)){ /* Development hack */ + ISPandReset(5); + } + + return result; +} + From b861fecbc4cbd910a97ed3b0d79d13f4dd521054 Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Mon, 13 Jun 2011 23:59:02 +0200 Subject: [PATCH 10/11] Clean up my experiment module --- modules/sec.c | 140 ++++++++++++++++++-------------------------------- 1 file changed, 49 insertions(+), 91 deletions(-) diff --git a/modules/sec.c b/modules/sec.c index 79b6182..8b0f6e2 100644 --- a/modules/sec.c +++ b/modules/sec.c @@ -5,21 +5,14 @@ #include "lcd/render.h" #include "lcd/allfonts.h" -void ReinvokeISP(void); -void EnableWatchdog(uint32_t ms); -void delayms(uint32_t ms); +void backlightInit(void); /**************************************************************************/ void module_sec(void) { - //Make PIO1_11 an analog input - gpioSetDir(RB_LED3, gpioDirection_Input); - IOCON_PIO1_11 = 0x41; backlightInit(); - uint32_t j=0; - //disable the JTAG on PIO3_3 IOCON_PIO3_3 = 0x10; @@ -27,105 +20,70 @@ void module_sec(void) { int dx=0; font_direction = FONT_DIR_LTR; // LeftToRight is the default - font = &Font_8x8; - static FONT fonts[] = { - &Font_7x8, - &Font_Ubuntu18pt, // 3 byte-font - &Font_8x8, - }; - - int fontctr=0; yctr=18; uint8_t trigger; -#define SEND -#ifdef SEND - trigger=200; - gpioSetDir(RB_LED0, gpioDirection_Output); - IOCON_JTAG_TDI_PIO0_11 = 0x11; -#else - trigger=380; - gpioSetDir(RB_LED0, gpioDirection_Input); - IOCON_JTAG_TDI_PIO0_11 = 0x42; -#endif - + trigger=20; uint32_t ctr=0; + char key; while (1) { - ctr++; - uint32_t results; - lcdDisplay(j); - delayms(10); + ctr++; - font=fonts[fontctr]; + lcdDisplay(0); + delayms(10); - if(gpioGetValue(RB_BTN3)==0){ - while(gpioGetValue(RB_BTN3)==0); - trigger +=10; - }; - if(gpioGetValue(RB_BTN2)==0){ - while(gpioGetValue(RB_BTN2)==0); - trigger -=10; - }; - dx=DoString(0,0,"Trig:"); - dx=DoInt(dx,0,trigger); - DoString(dx,0," "); + key= getInput(); + if(key==BTN_UP){ + trigger +=1; + }else if (key ==BTN_DOWN){ + trigger -=1; + }; - if(gpioGetValue(RB_BTN0)==0){ - while(gpioGetValue(RB_BTN0)==0); - DoString(0,8,"Enter ISP!"); - lcdDisplay(0); - EnableWatchdog(1000*5); - ReinvokeISP(); - }; + font=&Font_7x8; + dx=DoString(0,0,"Trig:"); + dx=DoInt(dx,0,trigger); + DoString(dx,0," "); - font = &Font_Ubuntu36pt; - dx=DoString(0,0,"Sec"); -#ifdef SEND - if(ctr++>trigger/10){ - ctr=0; - if (gpioGetValue(RB_LED0) == CFG_LED_OFF){ - gpioSetValue (RB_LED0, CFG_LED_ON); -// DoString(dx,14,"ON!"); - } else { - gpioSetValue (RB_LED0, CFG_LED_OFF); -// DoString(dx,14,"off"); - }; - }; -#else - results = adcRead(0); - DoInt(dx,20,results); + // Easy flashing + if(key==BTN_LEFT){ + DoString(0,8,"Enter ISP!"); + lcdDisplay(0); + ISPandReset(5); + }; - if(results>trigger){ - DoString(dx,30,"YES!"); - }else{ - DoString(dx,30," no "); - }; + // Display nickname + font = &Font_Ubuntu36pt; + dx=DoString(0,0,"Sec"); -#endif - font = &Font_7x8; + // Blink LED + if(ctr++>trigger){ + ctr=0; + if (gpioGetValue(RB_LED2) == CFG_LED_OFF){ + gpioSetValue (RB_LED2, CFG_LED_ON); + } else { + gpioSetValue (RB_LED2, CFG_LED_OFF); + }; + }; - results = adcRead(1); - dx=DoString(0,yctr+28,"Voltage:"); - results *= 10560; - results /= 1024; - DoInt(dx,yctr+28,results); - - if( results < 3500 ){ - DoString(0,yctr+30,"Shutdown"); - gpioSetValue (RB_PWR_GOOD, 0); - gpioSetValue (RB_LCD_BL, 0); - SCB_SCR |= SCB_SCR_SLEEPDEEP; - PMU_PMUCTRL = PMU_PMUCTRL_DPDEN_DEEPPOWERDOWN; - __asm volatile ("WFI"); - }else{ - //DoString(0,yctr+30,"OK "); - ; - } + // Print Voltage + font = &Font_7x8; + dx=DoString(0,yctr+28,"Voltage:"); + DoInt(dx,yctr+28,GetVoltage()); } - return; } + +void tick_sec(void){ + static int foo=0; + static int toggle=0; + if(foo++>50){ + toggle=1-toggle; + foo=0; + gpioSetValue (RB_LED0, toggle); + }; + return; +}; From 890b63991c9f34094f40d716d89a3c5bb4f41bc7 Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Tue, 14 Jun 2011 00:16:14 +0200 Subject: [PATCH 11/11] it did work %) --- lcd/display.c | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/lcd/display.c b/lcd/display.c index 9b77ca8..ac536c1 100644 --- a/lcd/display.c +++ b/lcd/display.c @@ -11,20 +11,10 @@ uint8_t lcdBuffer[RESX*RESY_B]; int inverted = 0; -/* -//TODO FIXME why doenst that work ? -#define CS RB_LCD_CS -#define SCK RB_SPI_SCK -#define SDA RB_SPI_MOSI -#define RST RB_LCD_RST -*/ - -#define CS 2,1 -#define SCK 2,11 -//#define SCK 2,8 -#define SDA 0,9 -//#define SDA 2,8 -#define RST 2,2 +#define CS RB_LCD_CS +#define SCK RB_SPI_SCK +#define SDA RB_SPI_MOSI +#define RST RB_LCD_RST void lcdWrite(uint8_t cd, uint8_t data) {