From 68b5afbe473eced438bfb64dfe03e74268a94b57 Mon Sep 17 00:00:00 2001 From: kiu Date: Mon, 25 Jul 2011 20:33:20 +0200 Subject: [PATCH 01/10] handle i2c through queue, probe i2c for flame --- firmware/applications/flame.c | 46 ++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/firmware/applications/flame.c b/firmware/applications/flame.c index a9a5343..0b6ed48 100644 --- a/firmware/applications/flame.c +++ b/firmware/applications/flame.c @@ -37,26 +37,29 @@ void ReinvokeISP(void); /**************************************************************************/ -void flameSetI2C(uint8_t cr, uint8_t value) { +uint8_t flameEnabled = 0; +uint8_t flameMode = FLAME_OFF; +uint8_t flameI2Cpwm = 0; +uint16_t flameTicks = 0; + +uint32_t flameSetI2C(uint8_t cr, uint8_t value) { I2CMasterBuffer[0] = FLAME_I2C_WRITE; I2CMasterBuffer[1] = cr; I2CMasterBuffer[2] = value; I2CWriteLength = 3; I2CReadLength = 0; - i2cEngine(); + return i2cEngine(); } - -uint8_t flameMode = FLAME_OFF; -uint8_t flameI2Csend = 0; -uint8_t flameI2Cpwm = 0; -uint16_t flameTicks = 0; +void setFlamePWM() { + flameSetI2C(FLAME_I2C_CR_PWM0, flameI2Cpwm); // set pwm +} void tick_flame(void) { // every 10ms flameTicks++; if (flameMode == FLAME_OFF) { - if (isNight()) { + if (isNight() && flameEnabled) { flameTicks = 0; flameMode = FLAME_UP; } @@ -64,7 +67,7 @@ void tick_flame(void) { // every 10ms if (flameMode == FLAME_UP) { flameI2Cpwm++; - flameI2Csend = 1; + push_queue(&setFlamePWM); if (flameI2Cpwm == 0xFF) { flameMode = FLAME_UP_WAIT; flameTicks = 0; @@ -79,7 +82,7 @@ void tick_flame(void) { // every 10ms if (flameMode == FLAME_DOWN) { flameI2Cpwm--; - flameI2Csend = 1; + push_queue(&setFlamePWM); if (flameI2Cpwm == 0x00) { flameMode = FLAME_DOWN_WAIT; flameTicks = 0; @@ -97,18 +100,20 @@ void main_flame(void) { i2cInit(I2CMASTER); // Init I2C - flameSetI2C(FLAME_I2C_CR_PSC0, 0x00); // set prescaler - flameSetI2C(FLAME_I2C_CR_PWM0, 0x00); // set pwm - flameSetI2C(FLAME_I2C_CR_LS0, FLAME_I2C_LS0_PWM0 << FLAME_I2C_LS0_LED0); // set led0 to pwm + flameEnabled = (flameSetI2C(FLAME_I2C_CR_LS0, FLAME_I2C_LS0_OFF << FLAME_I2C_LS0_LED0) == I2CSTATE_ACK); // probe i2c + + if (flameEnabled) { + flameSetI2C(FLAME_I2C_CR_LS0, FLAME_I2C_LS0_OFF << FLAME_I2C_LS0_LED0); // set led0 off + flameSetI2C(FLAME_I2C_CR_LS0, FLAME_I2C_LS0_OFF << FLAME_I2C_LS0_LED1); // set led1 off + flameSetI2C(FLAME_I2C_CR_LS0, FLAME_I2C_LS0_OFF << FLAME_I2C_LS0_LED2); // set led2 off + flameSetI2C(FLAME_I2C_CR_LS0, FLAME_I2C_LS0_OFF << FLAME_I2C_LS0_LED3); // set led3 off + + flameSetI2C(FLAME_I2C_CR_PSC0, 0x00); // set prescaler + flameSetI2C(FLAME_I2C_CR_PWM0, 0x00); // set pwm + flameSetI2C(FLAME_I2C_CR_LS0, FLAME_I2C_LS0_PWM0 << FLAME_I2C_LS0_LED0); // set led0 to pwm + } while (1) { - delayms(20); - - if (flameI2Csend == 1) { - flameI2Csend = 0; - flameSetI2C(FLAME_I2C_CR_PWM0, flameI2Cpwm); // set pwm - } - char key = getInput(); if (key == BTN_ENTER) { DoString(0,50,"ISP!"); @@ -116,6 +121,7 @@ void main_flame(void) { ISPandReset(); } + work_queue(); } return; From 0fbdd071ab0d3647bb8972084572f4f08af2af15 Mon Sep 17 00:00:00 2001 From: schneider Date: Mon, 25 Jul 2011 23:25:00 +0200 Subject: [PATCH 02/10] added adc mutex --- firmware/core/adc/adc.c | 4 ++++ firmware/core/adc/adc.h | 1 + 2 files changed, 5 insertions(+) diff --git a/firmware/core/adc/adc.c b/firmware/core/adc/adc.c index 37613c2..71fd7be 100644 --- a/firmware/core/adc/adc.c +++ b/firmware/core/adc/adc.c @@ -67,6 +67,7 @@ static bool _adcInitialised = false; static uint8_t _adcLastChannel = 0; +uint8_t adcMutex = 0; /**************************************************************************/ /*! @@ -89,6 +90,7 @@ static uint8_t _adcLastChannel = 0; /**************************************************************************/ uint32_t adcRead (uint8_t channelNum) { + adcMutex = 1; if (!_adcInitialised) adcInit(); uint32_t regVal, adcData; @@ -154,11 +156,13 @@ uint32_t adcRead (uint8_t channelNum) /* return 0 if an overrun occurred */ if ( regVal & ADC_DR_OVERRUN ) { + adcMutex = 0; return (1); } /* return conversion results */ adcData = (regVal >> 6) & 0x3FF; + adcMutex = 0; return (adcData); } diff --git a/firmware/core/adc/adc.h b/firmware/core/adc/adc.h index 29e168d..72aa317 100644 --- a/firmware/core/adc/adc.h +++ b/firmware/core/adc/adc.h @@ -41,6 +41,7 @@ #include "projectconfig.h" +extern uint8_t adcMutex; uint32_t adcRead (uint8_t channelNum); void adcInit (void); From 0b930aef48d91610dae0dbedea0d1094e33b72fb Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Mon, 25 Jul 2011 23:26:18 +0200 Subject: [PATCH 03/10] Use new mutex :) --- firmware/applications/default.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/firmware/applications/default.c b/firmware/applications/default.c index b7331e4..51d306d 100644 --- a/firmware/applications/default.c +++ b/firmware/applications/default.c @@ -116,9 +116,11 @@ void tick_default(void) { ctr++; incTimer(); if(ctr>100){ - VoltageCheck(); - LightCheck(); - ctr=0; + if(!adcMutex){ + VoltageCheck(); + LightCheck(); + }; + ctr--; }; if(isNight()) From bb178629a21961c92ac00c4d9534ba8b4701150a Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Mon, 25 Jul 2011 23:41:26 +0200 Subject: [PATCH 04/10] oops --- firmware/applications/default.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/firmware/applications/default.c b/firmware/applications/default.c index 51d306d..831f6f4 100644 --- a/firmware/applications/default.c +++ b/firmware/applications/default.c @@ -119,8 +119,10 @@ void tick_default(void) { if(!adcMutex){ VoltageCheck(); LightCheck(); + ctr=0; + }else{ + ctr--; }; - ctr--; }; if(isNight()) From 7eba1c52404002002523093d34296491854d8fc9 Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Tue, 26 Jul 2011 01:04:34 +0200 Subject: [PATCH 05/10] Fix delayms_power to do proper timecounting --- firmware/basic/idle.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/firmware/basic/idle.c b/firmware/basic/idle.c index 7625abb..c640f13 100644 --- a/firmware/basic/idle.c +++ b/firmware/basic/idle.c @@ -41,12 +41,12 @@ void delayms_queue(uint32_t ms){ }; void delayms_power(uint32_t ms){ + ms+=_timectr; do { - ms-=10; #ifdef ARM - __asm volatile ("WFI"); + __asm volatile ("WFI"); #endif - } while(ms>10); + } while (ms >_timectr); }; int push_queue(void (*new)(void)){ From 292d1ba36f01faec413c2d2cae2676ea59807d62 Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Tue, 26 Jul 2011 01:09:31 +0200 Subject: [PATCH 06/10] Rename timer.c into more correct queue.c --- .../applications/tester/{timer.c => queue.c} | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) rename firmware/applications/tester/{timer.c => queue.c} (83%) diff --git a/firmware/applications/tester/timer.c b/firmware/applications/tester/queue.c similarity index 83% rename from firmware/applications/tester/timer.c rename to firmware/applications/tester/queue.c index e93bd2c..280b0f7 100644 --- a/firmware/applications/tester/timer.c +++ b/firmware/applications/tester/queue.c @@ -33,21 +33,18 @@ void s_ticks(void) { void b_one(void){ gpioSetValue (RB_LED2, 0); - delayms_power(100); + delayms(100); gpioSetValue (RB_LED2, 1); - delayms_power(1000); + delayms(1000); gpioSetValue (RB_LED2, 0); }; -void do_qone(void) { - work_queue(); -}; - -void do_q(void) { - delayms_queue(500); -}; - -void push_qone(void) { +void push_q(void) { + push_queue(&b_one); + push_queue(&b_one); + push_queue(&b_one); + push_queue(&b_one); + push_queue(&b_one); push_queue(&b_one); }; From e182a2c1798c1d6fde6aeb97fefb5ab457e0d829 Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Tue, 26 Jul 2011 01:18:31 +0200 Subject: [PATCH 07/10] APP=tester now supports systick-functions, too --- firmware/applications/Makefile | 2 +- firmware/applications/mktester | 9 +++++++++ firmware/applications/tester.c | 11 ++--------- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/firmware/applications/Makefile b/firmware/applications/Makefile index 140f53a..b8b0246 100644 --- a/firmware/applications/Makefile +++ b/firmware/applications/Makefile @@ -81,7 +81,7 @@ $(LIBFILE): $(OBJS) $(WRAPOBJ) $(CC) $(CFLAGS) -o $@ $< clean: - rm -f $(OBJS) $(WRAPOBJ) $(WRAPSRC) $(LIBFILE) *.o + rm -f $(OBJS) $(WRAPOBJ) $(WRAPSRC) $(LIBFILE) *.o tester/*.o %.c: @echo diff --git a/firmware/applications/mktester b/firmware/applications/mktester index f584fa1..661351d 100755 --- a/firmware/applications/mktester +++ b/firmware/applications/mktester @@ -58,4 +58,13 @@ done echo "NULL" echo "};" +echo "inline void generated_tick(void){" +for f in $* ; do + grep -h '^void tick_' $f|sed 's/^void //;s/(.*//'|while read a ; do + echo "$a();" + done +done +echo "return;" +echo "};" + diff --git a/firmware/applications/tester.c b/firmware/applications/tester.c index 26f56bd..50a3542 100644 --- a/firmware/applications/tester.c +++ b/firmware/applications/tester.c @@ -17,15 +17,8 @@ void main_tester(void) { gotoISP(); }; -void no_tick_tester(void){ - static int foo=0; - static int toggle=0; - if(foo++>80){ - toggle=1-toggle; - foo=0; - gpioSetValue (RB_LED0, toggle); - }; - return; +void tick_tester(void){ + generated_tick(); }; From cb5ebc57d8cc7188110222ecc1aea19466fc90a4 Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Tue, 26 Jul 2011 01:55:33 +0200 Subject: [PATCH 08/10] add APP=initial, template for initial firmware to be flashed. --- firmware/applications/initial.c | 97 +++++++++++++++++++++++++++++++++ firmware/basic/basic.h | 2 + firmware/basic/keyin.c | 8 ++- 3 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 firmware/applications/initial.c diff --git a/firmware/applications/initial.c b/firmware/applications/initial.c new file mode 100644 index 0000000..396467a --- /dev/null +++ b/firmware/applications/initial.c @@ -0,0 +1,97 @@ +#include + +#include "basic/basic.h" + +#include "lcd/print.h" +#include "filesystem/ff.h" +#include "usb/usbmsc.h" + + +/**************************************************************************/ + +void main_initial(void) { + char key=BTN_NONE; + gpioSetValue (RB_LED0, 0); + gpioSetValue (RB_LED1, 0); + gpioSetValue (RB_LED2, 0); + gpioSetValue (RB_LED3, 0); + IOCON_PIO1_11 = 0x0; + gpioSetDir(RB_LED3, gpioDirection_Output); + + while(1){ + lcdClear(); + lcdPrintln("Init v.42"); + lcdNl(); + lcdPrintln("Left: ISP()"); + lcdPrintln("Right: MSC()"); + lcdPrintln("Up: FormatDF()"); + lcdPrintln("Down: ???"); + lcdPrintln("Enter: LEDs()"); + lcdRefresh(); + + key=getInputWait(); + + if(key&BTN_ENTER){ + gpioSetValue (RB_LED0, 1); + gpioSetValue (RB_LED1, 1); + gpioSetValue (RB_LED2, 1); + gpioSetValue (RB_LED3, 1); + delayms_power(100); + getInputWaitRelease(); + + gpioSetValue (RB_LED0, 0); + gpioSetValue (RB_LED1, 0); + gpioSetValue (RB_LED2, 0); + gpioSetValue (RB_LED3, 0); + delayms_power(50); + }; + if(key&BTN_RIGHT){ + lcdClear(); + lcdPrintln("MSC Enabled."); + lcdRefresh(); + delayms_power(300); + usbMSCInit(); + getInputWait(); + lcdPrintln("MSC Disabled."); + usbMSCOff(); + lcdRefresh(); + } + if(key&BTN_LEFT){ + lcdClear(); + lcdPrintln("Enter ISP!"); + lcdRefresh(); + ISPandReset(); + } + if(key&BTN_UP){ + FATFS FatFs; + int res; + + lcdClear(); + + lcdPrintln("Mount DF:"); + res=f_mount(0, &FatFs); + lcdPrintln(f_get_rc_string(res)); + lcdRefresh(); + + lcdPrintln("Formatting DF..."); +// res=f_mkfs(0,1,0); + lcdPrintln(f_get_rc_string(res)); + lcdRefresh(); + } + if(key&BTN_DOWN){ + ; + } + + getInputWaitRelease(); + }; +} + +void tick_initial(void){ + static int foo=0; + static int toggle=0; + if(foo++>80){ + toggle=1-toggle; + foo=0; + gpioSetValue (RB_LED0, toggle); + }; +}; diff --git a/firmware/basic/basic.h b/firmware/basic/basic.h index eff70fb..94b9222 100644 --- a/firmware/basic/basic.h +++ b/firmware/basic/basic.h @@ -150,6 +150,7 @@ char isNight(void); uint8_t getInput(void); uint8_t getInputRaw(void); uint8_t getInputWait(void); +void getInputWaitRelease(void); //uuid.c #include "basic/uuid.h" @@ -182,3 +183,4 @@ void handleMenu(const struct MENU *the_menu); #include "basic/idle.h" #endif + diff --git a/firmware/basic/keyin.c b/firmware/basic/keyin.c index ad44423..d4983c5 100644 --- a/firmware/basic/keyin.c +++ b/firmware/basic/keyin.c @@ -44,10 +44,16 @@ uint8_t getInput(void) { uint8_t getInputWait(void) { uint8_t key; - while ((key=getInput())==BTN_NONE) + while ((key=getInputRaw())==BTN_NONE) work_queue(); delayms_queue(10); /* Delay a little more to debounce */ return key; }; +void getInputWaitRelease(void) { + while (getInputRaw()!=BTN_NONE) + work_queue(); + delayms_queue(10); /* Delay a little more to debounce */ +}; + From 3d701fda1b7c6ef9fb76723de56e5cf78d417844 Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Tue, 26 Jul 2011 02:04:41 +0200 Subject: [PATCH 09/10] If no filesystem, fail configstuff quickly --- firmware/applications/default.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/firmware/applications/default.c b/firmware/applications/default.c index 831f6f4..fc6e145 100644 --- a/firmware/applications/default.c +++ b/firmware/applications/default.c @@ -30,6 +30,9 @@ int lcdInitConfig(){ lcdPrint("open:"); lcdPrintln(f_get_rc_string(res)); if(res){ + if(res==FR_NO_FILESYSTEM) + return 1; + lcdPrintln("new r0ket.cfg..."); res=f_open(&file, "r0ket.cfg", FA_OPEN_ALWAYS|FA_WRITE); From 3e30e03ba9efb0ee5f495112338df4648a2dcff8 Mon Sep 17 00:00:00 2001 From: Stefan `Sec` Zehl Date: Tue, 26 Jul 2011 02:09:52 +0200 Subject: [PATCH 10/10] Actually enable formatting the DF --- firmware/applications/initial.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/firmware/applications/initial.c b/firmware/applications/initial.c index 396467a..8ed9c19 100644 --- a/firmware/applications/initial.c +++ b/firmware/applications/initial.c @@ -74,7 +74,7 @@ void main_initial(void) { lcdRefresh(); lcdPrintln("Formatting DF..."); -// res=f_mkfs(0,1,0); + res=f_mkfs(0,1,0); lcdPrintln(f_get_rc_string(res)); lcdRefresh(); }