CLKDIV-Durchlauf für Sync zum Messen.
[hackover2013-badge-firmware.git] / badge / main.c
index 81be76b..5f7fde3 100644 (file)
@@ -43,7 +43,9 @@
 #include "core/adc/adc.h"
 #include "core/cpu/cpu.h"
 #include "core/pmu/pmu.h"
+#include "core/wdt/wdt.h"
 #include "core/gpio/gpio.h"
+#include "core/ssp/ssp.h"
 #include "core/systick/systick.h"
 #include "core/usbhid-rom/usbmsc.h"
 
   #include "core/cmd/cmd.h"
 #endif
 
+#include "init.h"
 #include "ui/display.h"
 #include "ui/sprite.h"
+#include "ui/event.h"
+#include "util/util.h"
+#include "jumpnrun/jumpnrun.h"
 
-#include "r0ketports.h"
-#include "drivers/fatfs/ff.h"
-
-enum {
-  BADGE_BTN_UP    =  1,
-  BADGE_BTN_LEFT  =  2,
-  BADGE_BTN_DOWN  =  4,
-  BADGE_BTN_RIGHT =  8,
-  BADGE_BTN_A     = 16,
-  BADGE_BTN_B     = 32,
-  BADGE_BTN_MID   = 64
-};
-
-uint8_t getInputRaw(void) {
-    uint8_t result = BTN_NONE;
-
-    if (gpioGetValue(RB_BTN3)==0) {
-        result |= BADGE_BTN_UP;
-    }
+#include "dataflash/at45db041d.h"
 
-    if (gpioGetValue(RB_BTN2)==0) {
-        result |= BADGE_BTN_DOWN;
-    }
-
-    if (gpioGetValue(RB_BTN4)==0) {
-        result |= BADGE_BTN_MID;
-    }
-
-    if (gpioGetValue(RB_BTN0)==0) {
-        result |= BADGE_BTN_LEFT;
-    }
-
-    if (gpioGetValue(RB_BTN1)==0) {
-        result |= BADGE_BTN_RIGHT;
-    }
-
-    if(gpioGetValue(RB_HB3) == 0) {
-      result |= BADGE_BTN_A;
-    }
+#include "drivers/fatfs/ff.h"
 
-    if(gpioGetValue(RB_HB4) == 0 || gpioGetValue(RB_HB5) == 0) {
-      result |= BADGE_BTN_B;
-    }
+#include "badge/pinconfig.h"
 
-    return result;
-}
+#ifdef R0KET
 
-void backlightInit(void) {
+#include "r0ketports.h"
+void rbBacklightInit(void) {
   /* Enable the clock for CT16B1 */
   SCB_SYSAHBCLKCTRL |= (SCB_SYSAHBCLKCTRL_CT16B1);
 
   /* Configure PIO1.9 as Timer1_16 MAT0 Output */
   IOCON_PIO1_9 &= ~IOCON_PIO1_9_FUNC_MASK;
-  IOCON_PIO1_9 |= IOCON_PIO1_9_FUNC_CT16B1_MAT0;
+  IOCON_PIO1_9 |=  IOCON_PIO1_9_FUNC_CT16B1_MAT0;
 
   /* Set default duty cycle (MR1) */
   TMR_TMR16B1MR0 = 0; //(0xFFFF * (100 - brightness)) / 100;
@@ -126,82 +94,92 @@ void backlightInit(void) {
   gpioSetValue(RB_PWR_LCDBL, 0);
 }
 
-
-
 void rbInit() {
-    // TODO FIXME special port disable ? LEDs BTNs ?
-
-    // prepare power
-    // TODO FIXME more power init needed ? chrg + volt input ?
-    // enable external vcc
-    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);
-
-    static uint8_t ports[] = { RB_BTN0, RB_BTN1, RB_BTN2, RB_BTN3, RB_BTN4,
-                               RB_LED0, RB_LED1, RB_LED2,
-                               RB_HB3, RB_HB4, RB_SPI_SS2,
-                               RB_SPI_SS3, RB_SPI_SS4, RB_SPI_SS5,
-                               RB_HB0, RB_HB1, RB_HB2,
-                               RB_HB3, RB_HB4, RB_HB5};
-
-    volatile uint32_t * regs[] = {&RB_BTN0_IO, &RB_BTN1_IO, &RB_BTN2_IO,
-                                  &RB_BTN3_IO, &RB_BTN4_IO};
-
-    int i = 0;
-    while( i<10 ){
-        gpioSetDir(ports[i], ports[i+1], gpioDirection_Input);
-        gpioSetPullup(regs[i/2], gpioPullupMode_PullUp);
-        i+=2;
-    }
-
-    // prepate chrg_stat
-    gpioSetDir(RB_PWR_CHRG, gpioDirection_Input);
-    gpioSetPullup (&RB_PWR_CHRG_IO, gpioPullupMode_PullUp);
+  RB_HB0_IO &= ~IOCON_SWDIO_PIO1_3_FUNC_MASK;
+  RB_HB0_IO |=  IOCON_SWDIO_PIO1_3_FUNC_GPIO;
+  RB_HB1_IO &= ~IOCON_JTAG_TCK_PIO0_10_FUNC_MASK;
+  RB_HB1_IO |=  IOCON_JTAG_TCK_PIO0_10_FUNC_GPIO;
+
+  struct {
+    int port;
+    int pin;
+    uint32_t volatile *reg;
+    gpioPullupMode_t mode;
+  } const input_pins[] = {
+#ifdef HOB_REV2
+    { RB_BTN0    , &RB_BTN0_IO    , gpioPullupMode_PullDown },
+    { RB_BTN1    , &RB_BTN1_IO    , gpioPullupMode_PullDown },
+    { RB_BTN2    , &RB_BTN2_IO    , gpioPullupMode_PullDown },
+    { RB_BTN3    , &RB_BTN3_IO    , gpioPullupMode_PullDown },
+    { RB_BTN4    , &RB_BTN4_IO    , gpioPullupMode_PullDown },
+    { RB_HB0     , &RB_HB0_IO     , gpioPullupMode_PullDown },
+    { RB_HB1     , &RB_HB1_IO     , gpioPullupMode_PullDown },
+    { RB_PWR_CHRG, &RB_PWR_CHRG_IO, gpioPullupMode_PullDown }
+#else
+    { RB_BTN0    , &RB_BTN0_IO    , gpioPullupMode_PullUp },
+    { RB_BTN1    , &RB_BTN1_IO    , gpioPullupMode_PullUp },
+    { RB_BTN2    , &RB_BTN2_IO    , gpioPullupMode_PullUp },
+    { RB_BTN3    , &RB_BTN3_IO    , gpioPullupMode_PullUp },
+    { RB_BTN4    , &RB_BTN4_IO    , gpioPullupMode_PullUp },
+    { RB_HB0     , &RB_HB0_IO     , gpioPullupMode_PullUp },
+    { RB_HB1     , &RB_HB1_IO     , gpioPullupMode_PullUp },
+    { RB_PWR_CHRG, &RB_PWR_CHRG_IO, gpioPullupMode_PullUp }
+#endif
+  };
+    
+  for(int i = 0; i < ARRAY_SIZE(input_pins); ++i) {
+    gpioSetDir(input_pins[i].port, input_pins[i].pin, gpioDirection_Input);
+    gpioSetPullup(input_pins[i].reg, input_pins[i].mode);
+  }
 
-    gpioSetDir(RB_LED3, gpioDirection_Input);
-    IOCON_PIO1_11 = 0x41;
+  // LED3 zur Bestimmung der Umgebungshelligkeit.
+  gpioSetDir(RB_LED3, gpioDirection_Input);
+  RB_LED3_IO = (RB_LED3_IO & IOCON_PIO1_11_FUNC_MASK) | IOCON_PIO1_11_FUNC_AD7;
+
+  // prepare LEDs
+  IOCON_JTAG_TDI_PIO0_11 &= ~IOCON_JTAG_TDI_PIO0_11_FUNC_MASK;
+  IOCON_JTAG_TDI_PIO0_11 |=  IOCON_JTAG_TDI_PIO0_11_FUNC_GPIO;
+
+  struct {
+    int port;
+    int pin;
+    int value;
+  } const output_pins[] = {
+    { RB_PWR_GOOD , 0 },
+    { USB_CONNECT , 1 },
+    { RB_LCD_CS   , 1 },
+    { RB_SPI_CS_DF, 1 },
+    { RB_SPI_SS2 , 1 },
+    { RB_SPI_SS3 , 1 },
+    { RB_SPI_SS4 , 1 },
+    { RB_SPI_SS5 , 1 },
+    { RB_LED0    , 0 },
+    { RB_LED1    , 0 },
+    { RB_LED2    , 0 },
+    { RB_LCD_BL  , 0 },
+    { RB_HB2     , 1 },
+    { RB_HB3     , 1 },
+    { RB_HB4     , 1 },
+    { RB_HB5     , 1 }
+  };
 
-    // prepare LEDs
-    IOCON_JTAG_TDI_PIO0_11 &= ~IOCON_JTAG_TDI_PIO0_11_FUNC_MASK;
-    IOCON_JTAG_TDI_PIO0_11 |= IOCON_JTAG_TDI_PIO0_11_FUNC_GPIO;
+  for(int i = 0; i < ARRAY_SIZE(output_pins); ++i) {
+    gpioSetDir  (output_pins[i].port, output_pins[i].pin, gpioDirection_Output);
+    gpioSetValue(output_pins[i].port, output_pins[i].pin, output_pins[i].value);
+  }
 
-    while( i<16 ){
-        gpioSetDir(ports[i],ports[i+1], gpioDirection_Output);
-        gpioSetValue (ports[i], ports[i+1], 0);
-        i+=2;
-    }
+  // Set P0.0 to GPIO
+  RB_PWR_LCDBL_IO &= ~RB_PWR_LCDBL_IO_FUNC_MASK;
+  RB_PWR_LCDBL_IO |=  RB_PWR_LCDBL_IO_FUNC_GPIO;
 
-    // Set LED3 to ?
-    IOCON_PIO1_11 = 0x41;
-
-    // prepare lcd
-    // TODO FIXME more init needed ?
-    gpioSetDir(RB_LCD_BL, gpioDirection_Output);
-    gpioSetValue (RB_LCD_BL, 0);
-
-    // Set P0.0 to GPIO
-    RB_PWR_LCDBL_IO&= ~RB_PWR_LCDBL_IO_FUNC_MASK;
-    RB_PWR_LCDBL_IO|= RB_PWR_LCDBL_IO_FUNC_GPIO;
-    gpioSetDir(RB_PWR_LCDBL, gpioDirection_Input);
-    gpioSetPullup(&RB_PWR_LCDBL_IO, gpioPullupMode_Inactive);
-
-    // prepare SPI/SS
-    // TODO FIXME init miso/mosi/sck somehow ?
-    // prepare hackerbus
-    while(i<sizeof(ports)){
-        gpioSetDir(ports[i],ports[i+1], gpioDirection_Output);
-        gpioSetValue (ports[i], ports[i+1], 1);
-        i+=2;
-    }
+  gpioSetDir   ( RB_PWR_LCDBL   , gpioDirection_Input);
+  gpioSetPullup(&RB_PWR_LCDBL_IO, gpioPullupMode_Inactive);
 
-    backlightInit();
-    //font=&Font_7x8;
+  rbBacklightInit();
+  badge_display_init();
 }
 
+#endif
 
 /**************************************************************************/
 /*!
@@ -217,134 +195,90 @@ int main(void)
   cpuInit();
   systickInit(CFG_SYSTICK_DELAY_IN_MS);
 
-  //pmuInit();
-  //adcInit();
+  // pmuInit();
+  // adcInit();
+#ifdef R0KET
   rbInit();
+#else
+  badge_init();
+#endif
 
-  badge_display_init();
+  for(uint8_t i = 1; ; ++i) {
+    badge_framebuffer fb = { { { 0 } } };
 
-  gpioSetDir(0, 11, gpioDirection_Output);
-
-//  usbMSCInit();
-
-  RB_HB3_IO&= ~IOCON_PIO0_2_FUNC_MASK;
-  RB_HB3_IO|=  IOCON_PIO0_2_FUNC_GPIO;
-  gpioSetDir(RB_HB3, gpioDirection_Input);
-  gpioSetPullup(&RB_HB3_IO, gpioPullupMode_PullUp);
-
-  RB_HB4_IO&= ~IOCON_PIO1_4_FUNC_MASK;
-  RB_HB4_IO|=  IOCON_PIO1_4_FUNC_GPIO;
-  gpioSetDir(RB_HB4, gpioDirection_Input);
-  gpioSetPullup(&RB_HB4_IO, gpioPullupMode_PullUp);
-
-
-  badge_framebuffer fb = {
-    {
-      {
-        1,   2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
-        21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
-        41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
-        61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
-        81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96
-      }, {
-        1,   2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
-        21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
-        41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
-        61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
-        81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96
-      }, {
-        1,   2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
-        21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
-        41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
-        61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
-        81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96
-      }, {
-        1,   2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
-        21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
-        41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
-        61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
-        81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96
-      }, {
-        1,   2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
-        21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
-        41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
-        61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
-        81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96
-      }, {
-        1,   2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
-        21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
-        41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
-        61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
-        81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96
-      }, {
-        1,   2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
-        21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
-        41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
-        61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
-        81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96
-      }, {
-        1,   2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
-        21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
-        41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
-        61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
-        81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96
-      }, {
-        1,   2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
-        21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
-        41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60,
-        61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80,
-        81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96
-      }
+    fb.data[2][86] = 0xff;
+    fb.data[4][86] = 0xff;
+    for(uint8_t j = 0; j < i; j += 3) {
+      fb.data[3][j / 3] = 0xff;
     }
-  };
 
-  //  badge_framebuffer_flush(&fb);
+    SCB_CLKOUTCLKDIV = i;
+    badge_framebuffer_flush(&fb);
+    systickDelay(200);
+  }
+
+  {
+    //    f_mkfs(0, 1, 0);
+    badge_framebuffer fb;
+    int res = 0;
+    FATFS fatvol;
 
-  int res = 0;
-  FATFS fatvol;
+    while(FR_OK != f_mount(0, &fatvol)) {
+      f_mkfs(0, 1, 0);
+    }
 
-  if(FR_OK == f_mount(0, &fatvol)) {
     FIL fil;
     if(FR_OK == (res = f_open(&fil, "sshot.dat", FA_OPEN_EXISTING | FA_READ))) {
       UINT readbytes;
 
       if(FR_OK != f_read(&fil, &fb, sizeof(fb), &readbytes)) {
-        gpioSetDir(RB_LED3, gpioDirection_Output);
-        gpioSetValue(RB_LED3, 1);
       }
 
       f_close(&fil);
-    } else {
-      fb.data[0][0] = res;
-      gpioSetDir(RB_LED1, gpioDirection_Output);
-      gpioSetValue(RB_LED1, 1);
     }
-  } else {
-    gpioSetDir(RB_LED2, gpioDirection_Output);
-    gpioSetValue(RB_LED2, 1);
-  }
 
-  badge_framebuffer_flush(&fb);
+    badge_framebuffer_flush(&fb);
+  }
 
-  for(uint8_t i = 0; ; ++i) {
-    gpioSetValue(0, 11, i & 1);
+  if(badge_input_raw() & BADGE_EVENT_KEY_DOWN) {
+    usbMSCInit();
+    for(;;);
+  }
 
-    uint8_t buttons = getInputRaw();
-    memset(&fb, 0, sizeof(fb));
+  badge_event_start();
 
-    badge_sprite const sp = { 4, 4, (uint8_t const *) "\xff\xff" };
+  for(;;) {
+    if(JUMPNRUN_ERROR == jumpnrun_play("smb.lvl")) {
+      break;
+    }
+  }
 
-    if(buttons & BADGE_BTN_UP)    { badge_framebuffer_blt(&fb, 30, 10, &sp, 0); }
-    if(buttons & BADGE_BTN_DOWN)  { badge_framebuffer_blt(&fb, 30, 50, &sp, 0); }
-    if(buttons & BADGE_BTN_LEFT)  { badge_framebuffer_blt(&fb, 10, 30, &sp, 0); }
-    if(buttons & BADGE_BTN_RIGHT) { badge_framebuffer_blt(&fb, 50, 30, &sp, 0); }
-    if(buttons & BADGE_BTN_MID)   { badge_framebuffer_blt(&fb, 30, 30, &sp, 0); }
-    if(buttons & BADGE_BTN_A)     { badge_framebuffer_blt(&fb, 70, 10, &sp, 0); }
-    if(buttons & BADGE_BTN_B)     { badge_framebuffer_blt(&fb, 70, 50, &sp, 0); }
+  uint8_t buttons = 0;
 
-    badge_framebuffer_flush(&fb);
+  for(uint8_t i = 0; ; ++i) {
+    badge_event_t event = badge_event_wait();
 
-    systickDelay(500);
+    switch(badge_event_type(event)) {
+    case BADGE_EVENT_USER_INPUT: {
+      buttons = badge_event_current_input_state();
+      break;
+    }
+    case BADGE_EVENT_GAME_TICK: {
+      badge_sprite const sp = { 4, 4, (uint8_t const *) "\xff\xff" };
+      badge_framebuffer fb = { { { 0x80 } } };
+
+      if(buttons & BADGE_EVENT_KEY_UP)    { badge_framebuffer_blt(&fb, 30, 10, &sp, 0); }
+      if(buttons & BADGE_EVENT_KEY_DOWN)  { badge_framebuffer_blt(&fb, 30, 50, &sp, 0); }
+      if(buttons & BADGE_EVENT_KEY_LEFT)  { badge_framebuffer_blt(&fb, 10, 30, &sp, 0); }
+      if(buttons & BADGE_EVENT_KEY_RIGHT) { badge_framebuffer_blt(&fb, 50, 30, &sp, 0); }
+      if(buttons & BADGE_EVENT_KEY_CENTER){ badge_framebuffer_blt(&fb, 30, 30, &sp, 0); }
+      if(buttons & BADGE_EVENT_KEY_BTN_A) { badge_framebuffer_blt(&fb, 70, 10, &sp, 0); }
+      if(buttons & BADGE_EVENT_KEY_BTN_B) { badge_framebuffer_blt(&fb, 70, 50, &sp, 0); }
+
+      badge_framebuffer_flush(&fb);
+      break;
+    }
+    }
   }
 
   return 0;
This page took 0.031599 seconds and 4 git commands to generate.