main.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 <stdio.h>
  14. #include <string.h>
  15. #include "ch.h"
  16. #include "hal.h"
  17. #include "shell.h"
  18. #include "chprintf.h"
  19. #include "usbcfg.h"
  20. /*===========================================================================*/
  21. /* Command line related. */
  22. /*===========================================================================*/
  23. #define SHELL_WA_SIZE THD_WORKING_AREA_SIZE(2048)
  24. /* Can be measured using dd if=/dev/xxxx of=/dev/null bs=512 count=10000.*/
  25. static void cmd_write(BaseSequentialStream *chp, int argc, char *argv[]) {
  26. static uint8_t buf[] =
  27. "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
  28. "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
  29. "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
  30. "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
  31. "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
  32. "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
  33. "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
  34. "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
  35. "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
  36. "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
  37. "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
  38. "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
  39. "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
  40. "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
  41. "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
  42. "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
  43. (void)argv;
  44. if (argc > 0) {
  45. chprintf(chp, "Usage: write\r\n");
  46. return;
  47. }
  48. while (chnGetTimeout((BaseChannel *)chp, TIME_IMMEDIATE) == Q_TIMEOUT) {
  49. #if 1
  50. /* Writing in channel mode.*/
  51. chnWrite(&SDU1, buf, sizeof buf - 1);
  52. #else
  53. /* Writing in buffer mode.*/
  54. (void) obqGetEmptyBufferTimeout(&SDU1.obqueue, TIME_INFINITE);
  55. memcpy(SDU1.obqueue.ptr, buf, SERIAL_USB_BUFFERS_SIZE);
  56. obqPostFullBuffer(&SDU1.obqueue, SERIAL_USB_BUFFERS_SIZE);
  57. #endif
  58. }
  59. chprintf(chp, "\r\n\nstopped\r\n");
  60. }
  61. static const ShellCommand commands[] = {
  62. {"write", cmd_write},
  63. {NULL, NULL}
  64. };
  65. static const ShellConfig shell_cfg1 = {
  66. (BaseSequentialStream *)&SDU1,
  67. commands
  68. };
  69. static const ShellConfig shell_cfg2 = {
  70. (BaseSequentialStream *)&SDU2,
  71. commands
  72. };
  73. /*===========================================================================*/
  74. /* Generic code. */
  75. /*===========================================================================*/
  76. /*
  77. * Red LED blinker thread, times are in milliseconds.
  78. */
  79. static THD_WORKING_AREA(waThread1, 128);
  80. static THD_FUNCTION(Thread1, arg) {
  81. (void)arg;
  82. chRegSetThreadName("blinker");
  83. while (true) {
  84. systime_t time = serusbcfg1.usbp->state == USB_ACTIVE ? 250 : 500;
  85. palClearLine(LINE_LED);
  86. chThdSleepMilliseconds(time);
  87. palSetLine(LINE_LED);
  88. chThdSleepMilliseconds(time);
  89. }
  90. }
  91. /*
  92. * Application entry point.
  93. */
  94. int main(void) {
  95. thread_t *shelltp1 = NULL;
  96. thread_t *shelltp2 = NULL;
  97. event_listener_t shell_el;
  98. /*
  99. * System initializations.
  100. * - HAL initialization, this also initializes the configured device drivers
  101. * and performs the board-specific initializations.
  102. * - Kernel initialization, the main() function becomes a thread and the
  103. * RTOS is active.
  104. */
  105. halInit();
  106. chSysInit();
  107. /*
  108. * Initializes two serial-over-USB CDC drivers.
  109. */
  110. sduObjectInit(&SDU1);
  111. sduStart(&SDU1, &serusbcfg1);
  112. sduObjectInit(&SDU2);
  113. sduStart(&SDU2, &serusbcfg2);
  114. /*
  115. * Activates the USB driver and then the USB bus pull-up on D+.
  116. * Note, a delay is inserted in order to not have to disconnect the cable
  117. * after a reset.
  118. */
  119. usbDisconnectBus(serusbcfg1.usbp);
  120. chThdSleepMilliseconds(1500);
  121. usbStart(serusbcfg1.usbp, &usbcfg);
  122. usbConnectBus(serusbcfg1.usbp);
  123. /*
  124. * Shell manager initialization.
  125. * Event zero is shell exit.
  126. */
  127. shellInit();
  128. chEvtRegister(&shell_terminated, &shell_el, 0);
  129. /*
  130. * Creates the blinker thread.
  131. */
  132. chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
  133. /*
  134. * Normal main() thread activity, managing two shells.
  135. */
  136. while (true) {
  137. if (SDU1.config->usbp->state == USB_ACTIVE) {
  138. /* Starting shells.*/
  139. if (shelltp1 == NULL) {
  140. shelltp1 = chThdCreateFromHeap(NULL, SHELL_WA_SIZE,
  141. "shell1", NORMALPRIO + 1,
  142. shellThread, (void *)&shell_cfg1);
  143. }
  144. if (shelltp2 == NULL) {
  145. shelltp2 = chThdCreateFromHeap(NULL, SHELL_WA_SIZE,
  146. "shell2", NORMALPRIO + 1,
  147. shellThread, (void *)&shell_cfg2);
  148. }
  149. /* Waiting for an exit event then freeing terminated shells.*/
  150. chEvtWaitAny(EVENT_MASK(0));
  151. if (chThdTerminatedX(shelltp1)) {
  152. chThdRelease(shelltp1);
  153. shelltp1 = NULL;
  154. }
  155. if (chThdTerminatedX(shelltp2)) {
  156. chThdRelease(shelltp2);
  157. shelltp2 = NULL;
  158. }
  159. }
  160. else {
  161. chThdSleepMilliseconds(1000);
  162. }
  163. }
  164. }