main.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. #define SHELL_WA_SIZE THD_WORKING_AREA_SIZE(1024)
  18. static const ShellCommand commands[] = {
  19. {NULL, NULL}
  20. };
  21. static const ShellConfig shell_cfg1 = {
  22. (BaseSequentialStream *)&SD1,
  23. commands
  24. };
  25. /*
  26. * LEDs blinker thread, times are in milliseconds.
  27. */
  28. static THD_WORKING_AREA(waThread1, 128);
  29. static THD_FUNCTION(Thread1, arg) {
  30. (void)arg;
  31. chRegSetThreadName("blinker");
  32. while (true) {
  33. unsigned i;
  34. for (i = 0; i < 4; i++) {
  35. palClearPad(PORT_E, PE_LED1);
  36. chThdSleepMilliseconds(100);
  37. palClearPad(PORT_E, PE_LED2);
  38. chThdSleepMilliseconds(100);
  39. palClearPad(PORT_E, PE_LED3);
  40. chThdSleepMilliseconds(100);
  41. palClearPad(PORT_E, PE_LED4);
  42. chThdSleepMilliseconds(100);
  43. palSetPad(PORT_E, PE_LED1);
  44. chThdSleepMilliseconds(100);
  45. palSetPad(PORT_E, PE_LED2);
  46. chThdSleepMilliseconds(100);
  47. palSetPad(PORT_E, PE_LED3);
  48. chThdSleepMilliseconds(100);
  49. palSetPad(PORT_E, PE_LED4);
  50. chThdSleepMilliseconds(300);
  51. }
  52. for (i = 0; i < 4; i++) {
  53. palTogglePort(PORT_E, PAL_PORT_BIT(PE_LED1) | PAL_PORT_BIT(PE_LED2) |
  54. PAL_PORT_BIT(PE_LED3) | PAL_PORT_BIT(PE_LED4));
  55. chThdSleepMilliseconds(500);
  56. palTogglePort(PORT_E, PAL_PORT_BIT(PE_LED1) | PAL_PORT_BIT(PE_LED2) |
  57. PAL_PORT_BIT(PE_LED3) | PAL_PORT_BIT(PE_LED4));
  58. chThdSleepMilliseconds(500);
  59. }
  60. for (i = 0; i < 4; i++) {
  61. palTogglePad(PORT_E, PE_LED1);
  62. chThdSleepMilliseconds(250);
  63. palTogglePad(PORT_E, PE_LED1);
  64. palTogglePad(PORT_E, PE_LED2);
  65. chThdSleepMilliseconds(250);
  66. palTogglePad(PORT_E, PE_LED2);
  67. palTogglePad(PORT_E, PE_LED3);
  68. chThdSleepMilliseconds(250);
  69. palTogglePad(PORT_E, PE_LED3);
  70. palTogglePad(PORT_E, PE_LED4);
  71. chThdSleepMilliseconds(250);
  72. palTogglePad(PORT_E, PE_LED4);
  73. }
  74. for (i = 0; i < 4; i++) {
  75. palClearPort(PORT_E, PAL_PORT_BIT(PE_LED1) | PAL_PORT_BIT(PE_LED3));
  76. palSetPort(PORT_E, PAL_PORT_BIT(PE_LED2) | PAL_PORT_BIT(PE_LED4));
  77. chThdSleepMilliseconds(500);
  78. palClearPort(PORT_E, PAL_PORT_BIT(PE_LED2) | PAL_PORT_BIT(PE_LED4));
  79. palSetPort(PORT_E, PAL_PORT_BIT(PE_LED1) | PAL_PORT_BIT(PE_LED3));
  80. chThdSleepMilliseconds(500);
  81. }
  82. palSetPort(PORT_E, PAL_PORT_BIT(PE_LED1) | PAL_PORT_BIT(PE_LED2) |
  83. PAL_PORT_BIT(PE_LED3) | PAL_PORT_BIT(PE_LED4));
  84. }
  85. }
  86. /*
  87. * Application entry point.
  88. */
  89. int main(void) {
  90. /*
  91. * System initializations.
  92. * - HAL initialization, this also initializes the configured device drivers
  93. * and performs the board-specific initializations.
  94. * - Kernel initialization, the main() function becomes a thread and the
  95. * RTOS is active.
  96. */
  97. halInit();
  98. chSysInit();
  99. /*
  100. * Shell manager initialization.
  101. */
  102. shellInit();
  103. /*
  104. * Activates the serial driver 1 using the driver default configuration.
  105. */
  106. sdStart(&SD1, NULL);
  107. /*
  108. * Creates the blinker thread.
  109. */
  110. chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
  111. /*
  112. * Normal main() thread activity, spawning shells.
  113. */
  114. while (true) {
  115. thread_t *shelltp = chThdCreateFromHeap(NULL, SHELL_WA_SIZE,
  116. "shell", NORMALPRIO + 1,
  117. shellThread, (void *)&shell_cfg1);
  118. chThdWait(shelltp); /* Waiting termination. */
  119. chThdSleepMilliseconds(1000);
  120. }
  121. }