diff --git a/Makefile b/Makefile index 87c8faf..7d01469 100644 --- a/Makefile +++ b/Makefile @@ -7,9 +7,9 @@ OBJS = main.o VPATH += OBJS += -OBJS += basic/basic.o +OBJS += basic/basic.o basic/reinvoke_isp.o basic/delayms.o basic/voltage.o +OBJS += basic/keyin.o OBJS += eeprom/eeprom.o -OBJS += reinvoke_isp.o LIBS += core/libcore.a lcd/liblcd.a modules/libmodules.a ########################################################################## 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 2aee0d6..020399c 100644 --- a/basic/basic.h +++ b/basic/basic.h @@ -114,6 +114,29 @@ #define RB_EEPROM_ADDR 0xA0 +#define USB_CONNECT 0,6 + void rbInit(void); +// reinvoke_isp.c +void ReinvokeISP(void); +void EnableWatchdog(uint32_t ms); +void ISPandReset(int delay); + +// delayms.c +void delayms(uint32_t ms); + +// voltage.c +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/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/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; +} + 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/basic/voltage.c b/basic/voltage.c new file mode 100644 index 0000000..1ed2d69 --- /dev/null +++ b/basic/voltage.c @@ -0,0 +1,24 @@ +#include + +#include "basic/basic.h" + +uint32_t results=5000; + +void VoltageCheck(void){ + + 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"); + }; +}; + +uint32_t GetVoltage(void){ + return results; +}; 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/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. diff --git a/lcd/display.c b/lcd/display.c index 14edd7c..e40ed08 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 */ @@ -32,20 +11,10 @@ void delayms(uint32_t ms) 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) { 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); 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: diff --git a/modules/default.c b/modules/default.c index 33bf3de..86581f2 100644 --- a/modules/default.c +++ b/modules/default.c @@ -1,7 +1,31 @@ #include +#include "basic/basic.h" /**************************************************************************/ void module_default(void) { + systickInit(10); return; -} +}; + +// every 10 ms +void tick_default(void) { + static int ctr; + ctr++; + if(ctr>100){ + 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; +}; 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(); } 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 "}" 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; +};