main.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #if defined STM32F1
  2. # include <stm32f10x.h>
  3. #elif defined STM32F4
  4. # include <stm32f4xx.h>
  5. #endif
  6. #include "stm32f4xx_conf.h"
  7. #include <stdio.h>
  8. #include <time.h>
  9. #if defined STM32F4
  10. extern uint64_t virtualClock;
  11. void SysTick_Handler(void)
  12. {
  13. virtualClock++;
  14. }
  15. #endif
  16. void initGPIO()
  17. {
  18. GPIO_InitTypeDef GPIO_Config;
  19. #if defined STM32F1
  20. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  21. /* USART1 Tx */
  22. GPIO_Config.GPIO_Pin = GPIO_Pin_9;
  23. GPIO_Config.GPIO_Mode = GPIO_Mode_AF_PP;
  24. GPIO_Config.GPIO_Speed = GPIO_Speed_50MHz;
  25. GPIO_Init(GPIOA, &GPIO_Config);
  26. /* USART1 Rx */
  27. GPIO_Config.GPIO_Pin = GPIO_Pin_10;
  28. GPIO_Config.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  29. GPIO_Config.GPIO_Speed = GPIO_Speed_50MHz;
  30. GPIO_Init(GPIOA, &GPIO_Config);
  31. #elif defined STM32F4
  32. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
  33. /* USART3 Tx */
  34. GPIO_Config.GPIO_Mode = GPIO_Mode_AF;
  35. GPIO_Config.GPIO_OType = GPIO_OType_PP;
  36. GPIO_Config.GPIO_Pin = GPIO_Pin_10;
  37. GPIO_Config.GPIO_PuPd = GPIO_PuPd_UP;
  38. GPIO_Config.GPIO_Speed = GPIO_Speed_50MHz;
  39. GPIO_Init(GPIOC, &GPIO_Config);
  40. /* USART3 Rx */
  41. GPIO_Config.GPIO_Pin = GPIO_Pin_11;
  42. GPIO_Init(GPIOC, &GPIO_Config);
  43. GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_USART3);
  44. GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_USART3);
  45. #endif
  46. }
  47. void initRTC()
  48. {
  49. #if defined STM32F1
  50. RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
  51. if (BKP_ReadBackupRegister(BKP_DR1) != 0xA5A5)
  52. {
  53. PWR_BackupAccessCmd(ENABLE);
  54. BKP_DeInit();
  55. RCC_LSEConfig(RCC_LSE_ON);
  56. while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET);
  57. RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
  58. RCC_RTCCLKCmd(ENABLE);
  59. RTC_WaitForSynchro();
  60. RTC_WaitForLastTask();
  61. RTC_SetPrescaler(32767);
  62. RTC_WaitForLastTask();
  63. BKP_WriteBackupRegister(BKP_DR1, 0xA5A5);
  64. PWR_BackupAccessCmd(DISABLE);
  65. }
  66. RTC_WaitForSynchro();
  67. RTC_WaitForLastTask();
  68. #elif defined STM32F4
  69. SysTick_Config(SystemCoreClock / 8 / 1000);
  70. SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);
  71. #endif
  72. }
  73. void initUSART()
  74. {
  75. USART_InitTypeDef USART_InitStructure;
  76. USART_InitStructure.USART_BaudRate = 115200;
  77. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  78. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  79. USART_InitStructure.USART_Parity = USART_Parity_No;
  80. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  81. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  82. #if defined STM32F1
  83. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
  84. USART_Init(USART1, &USART_InitStructure);
  85. USART_Cmd(USART1, ENABLE);
  86. #elif defined STM32F4
  87. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
  88. USART_Init(USART3, &USART_InitStructure);
  89. USART_Cmd(USART3, ENABLE);
  90. #endif
  91. }
  92. void setTime(uint32_t time)
  93. {
  94. #if defined STM32F1
  95. PWR_BackupAccessCmd(ENABLE);
  96. RTC_WaitForLastTask();
  97. RTC_SetCounter(time);
  98. RTC_WaitForLastTask();
  99. PWR_BackupAccessCmd(DISABLE);
  100. #elif defined STM32F4
  101. virtualClock = (uint64_t)time * 1000;
  102. #endif
  103. }
  104. void initAll(void)
  105. {
  106. initRTC();
  107. initGPIO();
  108. initUSART();
  109. RCC_ClearFlag();
  110. }
  111. int main(void)
  112. {
  113. initAll();
  114. for (;;)
  115. {
  116. char c;
  117. time_t t;
  118. scanf("%c", &c);
  119. switch (c)
  120. {
  121. case 's':
  122. scanf("%d", &t);
  123. setTime(t);
  124. printf("Current time changed: %d - %s\r", t, ctime(&t));
  125. break;
  126. default:
  127. t = time(0);
  128. printf("Current time: %d - %s\r", t, ctime(&t));
  129. break;
  130. }
  131. }
  132. return 0;
  133. }