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/default.c b/firmware/applications/default.c index b7331e4..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); @@ -116,9 +119,13 @@ void tick_default(void) { ctr++; incTimer(); if(ctr>100){ - VoltageCheck(); - LightCheck(); - ctr=0; + if(!adcMutex){ + VoltageCheck(); + LightCheck(); + ctr=0; + }else{ + ctr--; + }; }; if(isNight()) 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; diff --git a/firmware/applications/initial.c b/firmware/applications/initial.c new file mode 100644 index 0000000..8ed9c19 --- /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/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(); }; 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); }; 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/idle.c b/firmware/basic/idle.c index a4154a7..f59bd73 100644 --- a/firmware/basic/idle.c +++ b/firmware/basic/idle.c @@ -46,12 +46,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)){ 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 */ +}; + 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);