main.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #include "ch.h"
  14. #include "hal.h"
  15. #include "shell.h"
  16. #include "chprintf.h"
  17. #include "portab.h"
  18. /*===========================================================================*/
  19. /* Command line related. */
  20. /*===========================================================================*/
  21. #define SHELL_WA_SIZE THD_WORKING_AREA_SIZE(2048)
  22. static void cmd_date(BaseSequentialStream *chp, int argc, char *argv[]) {
  23. RTCDateTime timespec;
  24. (void)argv;
  25. if (argc > 0) {
  26. chprintf(chp, "Usage: date\r\n");
  27. return;
  28. }
  29. rtcGetTime(&RTCD1, &timespec);
  30. chprintf(chp, "%02d:%02d:%02d:%03d - %02d-%02d-%04d\r\n",
  31. timespec.millisecond / 3600000U,
  32. (timespec.millisecond % 3600000U) / 60000U,
  33. (timespec.millisecond % 60000U) / 1000U,
  34. timespec.millisecond % 1000U,
  35. timespec.month,
  36. timespec.day,
  37. timespec.year + 1980U);
  38. }
  39. #if RTC_HAS_STORAGE
  40. static void cmd_storage(BaseSequentialStream *chp, int argc, char *argv[]) {
  41. size_t storage_size = psGetStorageSize(&RTCD1);
  42. ps_offset_t i;
  43. (void)argv;
  44. if (argc > 0) {
  45. chprintf(chp, "Usage: storage\r\n");
  46. return;
  47. }
  48. for (i = 0U; i < (ps_offset_t)storage_size; i++) {
  49. uint8_t val;
  50. psRead(&RTCD1, i, 1U, &val);
  51. chprintf(chp, "%02x ", val);
  52. if (((i + 1) & 15) == 0U) {
  53. chprintf(chp, "\r\n");
  54. }
  55. }
  56. }
  57. #endif
  58. static const ShellCommand commands[] = {
  59. {"date", cmd_date},
  60. #if RTC_HAS_STORAGE
  61. {"storage", cmd_storage},
  62. #endif
  63. {NULL, NULL}
  64. };
  65. static const ShellConfig shell_cfg1 = {
  66. (BaseSequentialStream *)&PORTAB_SD1,
  67. commands
  68. };
  69. /*===========================================================================*/
  70. /* Generic code. */
  71. /*===========================================================================*/
  72. static sysinterval_t interval = TIME_MS2I(500);
  73. /*
  74. * LED blinker thread, times are in milliseconds.
  75. */
  76. static THD_WORKING_AREA(waThread1, 128);
  77. static THD_FUNCTION(Thread1, arg) {
  78. (void)arg;
  79. chRegSetThreadName("blinker");
  80. while (true) {
  81. palToggleLine(PORTAB_LINE_LED1);
  82. chThdSleep(interval);
  83. palToggleLine(PORTAB_LINE_LED1);
  84. chThdSleep(interval);
  85. }
  86. }
  87. /*
  88. * RTC callback.
  89. */
  90. static void alarmcb(RTCDriver *rtcp, rtcevent_t event) {
  91. (void)rtcp;
  92. if (event == RTC_EVENT_ALARM_A) {
  93. interval = TIME_MS2I(500);
  94. }
  95. else if (event == RTC_EVENT_ALARM_B) {
  96. interval = TIME_MS2I(50);
  97. }
  98. }
  99. /*
  100. * Application entry point.
  101. */
  102. int main(void) {
  103. static const RTCAlarm alarm1 = {
  104. RTC_ALRM_MSK4 | /* No month/week day match. */
  105. RTC_ALRM_MSK3 | /* No hour match. */
  106. RTC_ALRM_MSK2 | /* No minutes match. */
  107. RTC_ALRM_ST(0) |
  108. RTC_ALRM_SU(0) /* Match minute start. */
  109. };
  110. static const RTCAlarm alarm2 = {
  111. RTC_ALRM_MSK4 | /* No month/week day match. */
  112. RTC_ALRM_MSK3 | /* No hour match. */
  113. RTC_ALRM_MSK2 | /* No minutes match. */
  114. RTC_ALRM_ST(3) |
  115. RTC_ALRM_SU(0) /* Match minute half. */
  116. };
  117. /*
  118. * System initializations.
  119. * - HAL initialization, this also initializes the configured device drivers
  120. * and performs the board-specific initializations.
  121. * - Kernel initialization, the main() function becomes a thread and the
  122. * RTOS is active.
  123. */
  124. halInit();
  125. chSysInit();
  126. /* Board-dependent GPIO setup code.*/
  127. portab_setup();
  128. /* Starting a serial port for shell.*/
  129. sdStart(&PORTAB_SD1, NULL);
  130. /* Shell manager initialization.*/
  131. shellInit();
  132. /* Creates the blinker thread.*/
  133. chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
  134. rtcSetAlarm(&RTCD1, 0, &alarm1);
  135. rtcSetAlarm(&RTCD1, 1, &alarm2);
  136. rtcSetCallback(&RTCD1, alarmcb);
  137. #if RTC_HAS_STORAGE
  138. psWrite(&RTCD1, 0U, 12U, (const uint8_t *)"Hello World!");
  139. #endif
  140. /* Normal main() thread activity, spawning shells.*/
  141. while (true) {
  142. thread_t *shelltp = chThdCreateFromHeap(NULL, SHELL_WA_SIZE,
  143. "shell", NORMALPRIO + 1,
  144. shellThread, (void *)&shell_cfg1);
  145. chThdWait(shelltp); /* Waiting termination. */
  146. chThdSleepMilliseconds(1000);
  147. }
  148. }