Exemplo n.º 1
0
/******************************************************************************
 * @brief  Main function
 *
 *****************************************************************************/
int main(void)
{
  /* Initialize chip - handle erratas */
  CHIP_Init( );

  /* Initialize clocks and oscillators */
  cmuSetup( );

  /* Initialize UART peripheral */
  uartSetup( );

  /* Initialize Development Kit in EBI mode */
  BSP_Init(BSP_INIT_DEFAULT);

  /* Enable RS-232 transceiver on Development Kit */
  BSP_PeripheralAccess(BSP_RS232_UART, true);

  /* When DVK is configured, and no more DVK access is needed, the interface can safely be disabled to save current */
  BSP_Disable();


  /* Write welcome message to UART */
  uartPutData((uint8_t*) welcomeString, welLen);

  /*  Eternal while loop
   *  CPU will sleep during Rx and Tx. When a byte is transmitted, an interrupt
   *  wakes the CPU which copies the next byte in the txBuf queue to the
   *  UART TXDATA register.
   *
   *  When the predefined termiation character is received, the all pending
   *  data in rxBuf is copied to txBuf and echoed back on the UART */
  while (1)
  {
    /* Wait in EM1 while UART transmits */
    EMU_EnterEM1();

    /* Check if RX buffer has overflowed */
    if (rxBuf.overflow)
    {
      rxBuf.overflow = false;
      uartPutData((uint8_t*) overflowString, ofsLen);
    }

    /* Check if termination character is received */
    if (rxBuf.data[(rxBuf.wrI - 1) % BUFFERSIZE] == TERMINATION_CHAR)
    {
      /* Copy received data to UART transmit queue */
      uint8_t tmpBuf[BUFFERSIZE];
      int     len = uartGetData(tmpBuf, 0);
      uartPutData(tmpBuf, len);
    }
  }
}
Exemplo n.º 2
0
/**************************************************************************//**
 * @brief  Main function
 *****************************************************************************/
int main(void)
{
    const int msDelay=100;
    char displayString[8];
    LCD_AnimInit_TypeDef anim = {
        true,
        0x00,
        lcdAnimShiftNone,
        0x03,
        lcdAnimShiftLeft,
        lcdAnimLogicOr
    };
    LCD_FrameCountInit_TypeDef fc = {
        true,
        2, /* Update each 2nd frame */
        lcdFCPrescDiv1,
    };

    /* Chip errata */
    CHIP_Init();

    /* Configure push button interrupts */
    BSP_Init(BSP_INIT_DK_SPI);

    /* If first word of user data page is non-zero, enable eA Profiler trace */
    BSP_TraceProfilerSetup();

    /* Initialize DK interrupt enable */
    DkIrqInit();

    /* Initialize GPIO interrupt */
    GpioIrqInit();

    /* Setup SysTick Timer for 1 msec interrupts  */
    if (SysTick_Config(SystemCoreClockGet() / 1000)) while (1) ;

    /* Initialize LCD controller */
    SegmentLCD_Init(false);

    /* Run countdown for user to select energy mode */
    msCountDown = 4000; /* milliseconds */
    eMode = 0;
    while(msCountDown > 0)
    {
        if ( eMode >=3 && eMode<=4) {
            sprintf(displayString, "EM%d", eMode);
            SegmentLCD_Write(displayString);
        }
        switch( eMode )
        {
        case 0:
            SegmentLCD_Write("EM0 32M");
            break;
        case 1:
            SegmentLCD_Write("EM1 32M");
            break;
        case 2:
            SegmentLCD_Write("EM2 32K");
            break;
        case 3:
            SegmentLCD_Write("EM3    ");
            break;
        case 4:
            SegmentLCD_Write("EM4    ");
            break;
        case 5:
            SegmentLCD_Write("EM2+RTC");
            break;
        case 6:
        default:
            SegmentLCD_Write("RTC+LCD");
            break;
        }
        SegmentLCD_Number(msCountDown);
        Delay(msDelay);
        msCountDown -= msDelay;
    }
    /* Disable components, reenable when needed */
    SegmentLCD_Disable();
    RTC_Enable(false);

    GPIO->IEN = 0x00000000;
    NVIC_DisableIRQ(GPIO_EVEN_IRQn);
    BSP_Disable();
    GpioDisablePins();

    /* Go to energy mode and wait for reset */
    switch(eMode)
    {
    case 0:
        /* Disable systick timer */
        SysTick->CTRL  = 0;

        /* 32Mhz primes demo - running off HFXO */
        CMU_ClockSelectSet(cmuClock_HF, cmuSelect_HFXO);
        /* Disable HFRCO, LFRCO and all unwanted clocks */
        CMU->OSCENCMD = CMU_OSCENCMD_HFRCODIS;
        CMU->OSCENCMD = CMU_OSCENCMD_LFRCODIS;
        CMU->HFPERCLKEN0  = 0x00000000;
        CMU->HFCORECLKEN0 = 0x00000000;
        CMU->LFACLKEN0    = 0x00000000;
        CMU->LFBCLKEN0    = 0x00000000;
        /* Supress Conditional Branch Target Prefetch */
        MSC->READCTRL = MSC_READCTRL_MODE_WS1SCBTP;
        {
#define PRIM_NUMS 64
            uint32_t i, d, n;
            uint32_t primes[PRIM_NUMS];

            /* Find prime numbers forever */
            while (1)
            {
                primes[0] = 1;
                for (i = 1; i < PRIM_NUMS;)
                {
                    for (n = primes[i - 1] + 1; ; n++)
                    {
                        for (d = 2; d <= n; d++)
                        {
                            if (n == d)
                            {
                                primes[i] = n;
                                goto nexti;
                            }
                            if (n%d == 0) break;
                        }
                    }
nexti:
                    i++;
                }
            }
        }
    case 1:
        /* Disable systick timer */
        SysTick->CTRL  = 0;

        CMU_ClockSelectSet(cmuClock_HF, cmuSelect_HFXO);
        /* Disable HFRCO, LFRCO and all unwanted clocks */
        CMU->OSCENCMD = CMU_OSCENCMD_HFRCODIS;
        CMU->OSCENCMD = CMU_OSCENCMD_LFRCODIS;
        CMU->HFPERCLKEN0  = 0x00000000;
        CMU->HFCORECLKEN0 = 0x00000000;
        CMU->LFACLKEN0    = 0x00000000;
        CMU->LFBCLKEN0    = 0x00000000;
        EMU_EnterEM1();
        break;
    case 2:
        /* Enable LFRCO */
        CMU->OSCENCMD = CMU_OSCENCMD_LFRCOEN;
        /* Disable everything else */
        CMU->OSCENCMD = CMU_OSCENCMD_LFXODIS;
        CMU->HFPERCLKEN0  = 0x00000000;
        CMU->HFCORECLKEN0 = 0x00000000;
        CMU->LFACLKEN0    = 0x00000000;
        CMU->LFBCLKEN0    = 0x00000000;
        EMU_EnterEM2(false);
        break;
    case 3:
        CMU->OSCENCMD = CMU_OSCENCMD_LFXODIS;
        CMU->OSCENCMD = CMU_OSCENCMD_LFRCODIS;
        CMU->HFPERCLKEN0  = 0x00000000;
        CMU->HFCORECLKEN0 = 0x00000000;
        CMU->LFACLKEN0    = 0x00000000;
        CMU->LFBCLKEN0    = 0x00000000;
        EMU_EnterEM3(false);
        break;
    case 4:
        EMU_EnterEM4();
        break;
    case 5:
        /* EM2 + RTC - only briefly wake up to reconfigure each second */
        while(1)
        {
            RTCDRV_Trigger(2000, NULL);
            EMU_EnterEM2(false);
        }
    case 6:
        /* EM2 + RTC + LCD */
        SegmentLCD_Init(false);
        /* Animate LCD */
        LCD_FrameCountInit(&fc);
        LCD_AnimInit(&anim);
        while(1)
        {
            SegmentLCD_Write("Energy");
            /* Sleep in EM2 */
            RTCDRV_Trigger(2000, NULL);
            EMU_EnterEM2(false);

            SegmentLCD_Write("Micro");
            /* Sleep in EM2 */
            RTCDRV_Trigger(2000, NULL);
            EMU_EnterEM2(false);
        }
    case 7:
        break;
    }

    return 0;
}