sleep example

main.cpp

Committer:
Nathan Yonkee
Date:
2017-09-25
Revision:
4:2afaac57dc82
Parent:
2:aa4afe9a928c

File content as of revision 4:2afaac57dc82:

#include "mbed.h"

bool sleepyTime = false;

void interrupter()
{
  sleepyTime = true;
};

int main()
{
  /* (#)Initialize the LPTIM low level resources by implementing the */
  DigitalOut led( PB_3 );
  InterruptIn inter( PA_12 );
  inter.fall( &interrupter );
  led = 0;
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  __HAL_RCC_MSI_RANGE_CONFIG( RCC_MSIRANGE_5 );
  wait_ms( 2000 );
  RCC_ClkInitStruct.ClockType      = ( RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2 );
  RCC_ClkInitStruct.SYSCLKSource   = RCC_SYSCLKSOURCE_MSI; // 80 MHz
  RCC_ClkInitStruct.AHBCLKDivider  = RCC_SYSCLK_DIV1;         // 80 MHz
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;           // 80 MHz
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;           // 80 MHz
  HAL_RCC_ClockConfig( &RCC_ClkInitStruct, FLASH_LATENCY_4 );
  HAL_PWREx_ControlVoltageScaling( PWR_REGULATOR_VOLTAGE_SCALE2 );
  HAL_PWREx_EnableLowPowerRunMode();
  SystemCoreClockUpdate();
  HAL_PWREx_EnterSTOP0Mode( PWR_STOPENTRY_WFI );

  if( HAL_RCC_GetSysClockFreq() < 3000000 ) { led = 1; }

  wait_ms( 1 );
  led = 0;
  wait_ms( 1000 );
  led = 1;
  wait_ms( 10 );
  led = 0;
  /* HAL_MPU_Disable(); */
  /* __HAL_RCC_MSI_STANDBY_RANGE_CONFIG(RCC_MSIRANGE_5); */
  /* __HAL_RCC_WAKEUPSTOP_CLK_CONFIG(RCC_STOP_WAKEUPCLOCK_MSI); */
  /* GPIO_InitTypeDef GPIO_InitStructure = {0}; */
  /* GPIO_InitStructure.Pin = GPIO_PIN_All; */
  /* GPIO_InitStructure.Mode = GPIO_MODE_ANALOG; */
  /* GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_LOW; */
  /* GPIO_InitStructure.Pull = GPIO_NOPULL; */
  /* HAL_GPIO_Init(GPIOC, &GPIO_InitStructure); */
  /* HAL_GPIO_Init(GPIOH, &GPIO_InitStructure); */
  /* __HAL_RCC_GPIOC_CLK_DISABLE(); */
  /* __HAL_RCC_GPIOH_CLK_DISABLE(); */
  /* __HAL_RCC_USB_CLK_DISABLE(); */
  /* __HAL_RCC_ADC_CLK_DISABLE(); */
  /* __HAL_RCC_USART1_CLK_DISABLE(); */
  /* __HAL_RCC_USART2_CLK_DISABLE(); */
  /* __HAL_RCC_LPUART1_CLK_DISABLE(); */
  wait_ms( 2000 );
  /* wait_ms(3000); */
  /* HAL_PWR_EnableWakeUpPin(PWR_WAKEUP_PIN5); */
  /* //pc.printf("1\r\n"); */

  /* /1* Enable the fast wake up from Ultra low power mode *1/ */
  /* /1* HAL_GPIO_Init(GPIOA, &GPIO_InitStructure); *1/ */
  /* /1* HAL_GPIO_Init(GPIOB, &GPIO_InitStructure); *1/ */
  /* /1* __HAL_RCC_GPIOA_CLK_DISABLE(); *1/ */
  /* /1* __HAL_RCC_GPIOB_CLK_DISABLE(); *1/ */
  /* for(int i = 0; i < 13; ++i){ */
  /*     Thread::wait(1); */
  /*     HAL_GPIO_DeInit(GPIOA,i); */
  /*     HAL_GPIO_DeInit(GPIOB,i); */
  /*     HAL_GPIO_DeInit(GPIOC,i); */
  /*     HAL_GPIO_DeInit(GPIOH,i); */
  /* } */
  /* int j = 0; */
  /* HAL_GPIO_Init(GPIOA, &GPIO_InitStructure); */
  /* HAL_GPIO_Init(GPIOB, &GPIO_InitStructure); */
  /* Thread::wait(5000); */
  /* GPIO_InitStructure.Pin = GPIO_PIN_All; */
  /* GPIO_InitStructure.Mode = GPIO_MODE_ANALOG; */
  /* GPIO_InitStructure.Pull = GPIO_NOPULL; */
  /* HAL_GPIO_Init(GPIOA, &GPIO_InitStructure); */
  /* HAL_GPIO_Init(GPIOB, &GPIO_InitStructure); */

  /* Timer t; */
  while( 1 ) {
    if( sleepyTime ) {
      led = 0;
      /* HAL_PWR_EnterSTANDBYMode(); */
      HAL_PWREx_EnterSTOP0Mode( PWR_STOPENTRY_WFI );
      HAL_RCC_GetSysClockFreq();
      wait_ms( 50 );
      sleepyTime = false;
    };

    wait_ms( 1000 );

    /* event.rise(&pressed); */
    /* if ((go_to_sleep == 0) || (go_to_sleep == 2)) { */
    /*     printf("%d: Running\n", i); */
    /*     myled = !myled; */
    /*     wait(1.0); */
    /* } */
    led = 1;

    wait_ms( 100 );

    led = 0;

    /* deepsleep(); */
    /* if (go_to_sleep == 1) { */
    /*     myled = 0; */
    /*     printf("%d: Entering sleep (press user button to resume)\n", i); */
    /*     sleep(); */
    /* } */
    /* if (go_to_sleep == 3) { */
    /*     myled = 0; */
    /*     printf("%d: Entering deepsleep (press user button to resume)\n", i); */
    /*     deepsleep(); */
    /* } */
    /* t.reset(); */
    /* t.start(); */
    /* led = !led; */
    /* j=0; */
    /* while(j < 100000) { */
    /*     j = j + 1; */
    /* } */
    /* t.stop(); */
    /* led = !led; */
    /* led = !led; */
    /* HAL_GPIO_Init(GPIOC, &GPIO_InitStructure); */
    /* HAL_GPIO_Init(GPIOD, &GPIO_InitStructure); */
    /* HAL_GPIO_Init(GPIOH, &GPIO_InitStructure); */
    /* Thread::wait(500); */
  }
}