main.c 6.1 KB

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