main.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 "chprintf.h"
  16. #include "shell.h"
  17. #include "usbcfg.h"
  18. /*
  19. * Red LED blinker thread, times are in milliseconds.
  20. */
  21. static THD_WORKING_AREA(waThread1, 128);
  22. static THD_FUNCTION(Thread1, arg) {
  23. (void)arg;
  24. chRegSetThreadName("blinker1");
  25. while (true) {
  26. palClearPad(GPIOG, GPIOG_LED4_RED);
  27. chThdSleepMilliseconds(500);
  28. palSetPad(GPIOG, GPIOG_LED4_RED);
  29. chThdSleepMilliseconds(500);
  30. }
  31. }
  32. /*
  33. * Green LED blinker thread, times are in milliseconds.
  34. */
  35. static THD_WORKING_AREA(waThread2, 128);
  36. static THD_FUNCTION(Thread2, arg) {
  37. (void)arg;
  38. chRegSetThreadName("blinker2");
  39. while (true) {
  40. palClearPad(GPIOG, GPIOG_LED3_GREEN);
  41. chThdSleepMilliseconds(250);
  42. palSetPad(GPIOG, GPIOG_LED3_GREEN);
  43. chThdSleepMilliseconds(250);
  44. }
  45. }
  46. /*===========================================================================*/
  47. /* Command line related. */
  48. /*===========================================================================*/
  49. #define SHELL_WA_SIZE THD_WORKING_AREA_SIZE(2048)
  50. static const ShellCommand commands[] = {
  51. {NULL, NULL}
  52. };
  53. static const ShellConfig shell_cfg1 = {
  54. (BaseSequentialStream *)&SDU1,
  55. commands
  56. };
  57. /*===========================================================================*/
  58. /* Initialization and main thread. */
  59. /*===========================================================================*/
  60. /*
  61. * Application entry point.
  62. */
  63. int main(void) {
  64. /*
  65. * System initializations.
  66. * - HAL initialization, this also initializes the configured device drivers
  67. * and performs the board-specific initializations.
  68. * - Kernel initialization, the main() function becomes a thread and the
  69. * RTOS is active.
  70. */
  71. halInit();
  72. chSysInit();
  73. /*
  74. * Shell manager initialization.
  75. */
  76. shellInit();
  77. /*
  78. * Initializes a serial-over-USB CDC driver.
  79. */
  80. sduObjectInit(&SDU1);
  81. sduStart(&SDU1, &serusbcfg);
  82. /*
  83. * Activates the USB driver and then the USB bus pull-up on D+.
  84. * Note, a delay is inserted in order to not have to disconnect the cable
  85. * after a reset.
  86. */
  87. usbDisconnectBus(serusbcfg.usbp);
  88. chThdSleepMilliseconds(1000);
  89. usbStart(serusbcfg.usbp, &usbcfg);
  90. usbConnectBus(serusbcfg.usbp);
  91. /*
  92. * Creating the blinker threads.
  93. */
  94. chThdCreateStatic(waThread1, sizeof(waThread1),
  95. NORMALPRIO + 10, Thread1, NULL);
  96. chThdCreateStatic(waThread2, sizeof(waThread2),
  97. NORMALPRIO + 10, Thread2, NULL);
  98. /*
  99. * Normal main() thread activity, spawning shells.
  100. */
  101. while (true) {
  102. if (SDU1.config->usbp->state == USB_ACTIVE) {
  103. thread_t *shelltp = chThdCreateFromHeap(NULL, SHELL_WA_SIZE,
  104. "shell", NORMALPRIO + 1,
  105. shellThread, (void *)&shell_cfg1);
  106. chThdWait(shelltp); /* Waiting termination. */
  107. }
  108. chThdSleepMilliseconds(1000);
  109. }
  110. }