main.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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.
  22. */
  23. #define usb_lld_connect_bus(usbp) palClearPad(GPIOC, GPIOC_USB_DISCONNECT)
  24. #define usb_lld_disconnect_bus(usbp) palSetPad(GPIOC, GPIOC_USB_DISCONNECT)
  25. /*===========================================================================*/
  26. /* Command line related. */
  27. /*===========================================================================*/
  28. #define SHELL_WA_SIZE THD_WORKING_AREA_SIZE(2048)
  29. /* Can be measured using dd if=/dev/xxxx of=/dev/null bs=512 count=10000.*/
  30. static void cmd_write(BaseSequentialStream *chp, int argc, char *argv[]) {
  31. static uint8_t buf[] =
  32. "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
  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. (void)argv;
  49. if (argc > 0) {
  50. chprintf(chp, "Usage: write\r\n");
  51. return;
  52. }
  53. while (chnGetTimeout((BaseChannel *)chp, TIME_IMMEDIATE) == Q_TIMEOUT) {
  54. #if 1
  55. /* Writing in channel mode.*/
  56. chnWrite(&SDU1, buf, sizeof buf - 1);
  57. #else
  58. /* Writing in buffer mode.*/
  59. (void) obqGetEmptyBufferTimeout(&SDU1.obqueue, TIME_INFINITE);
  60. memcpy(SDU1.obqueue.ptr, buf, SERIAL_USB_BUFFERS_SIZE);
  61. obqPostFullBuffer(&SDU1.obqueue, SERIAL_USB_BUFFERS_SIZE);
  62. #endif
  63. }
  64. chprintf(chp, "\r\n\nstopped\r\n");
  65. }
  66. static const ShellCommand commands[] = {
  67. {"write", cmd_write},
  68. {NULL, NULL}
  69. };
  70. static const ShellConfig shell_cfg1 = {
  71. (BaseSequentialStream *)&SDU1,
  72. commands
  73. };
  74. /*===========================================================================*/
  75. /* Generic code. */
  76. /*===========================================================================*/
  77. /*
  78. * Red LED blinker thread, times are in milliseconds.
  79. */
  80. static THD_WORKING_AREA(waThread1, 128);
  81. static THD_FUNCTION(Thread1, arg) {
  82. (void)arg;
  83. chRegSetThreadName("blinker");
  84. while (true) {
  85. systime_t time;
  86. time = serusbcfg.usbp->state == USB_ACTIVE ? 250 : 500;
  87. palClearLine(LINE_LED1);
  88. chThdSleepMilliseconds(time);
  89. palSetLine(LINE_LED1);
  90. chThdSleepMilliseconds(time);
  91. }
  92. }
  93. /*
  94. * Application entry point.
  95. */
  96. int main(void) {
  97. /*
  98. * System initializations.
  99. * - HAL initialization, this also initializes the configured device drivers
  100. * and performs the board-specific initializations.
  101. * - Kernel initialization, the main() function becomes a thread and the
  102. * RTOS is active.
  103. */
  104. halInit();
  105. chSysInit();
  106. /*
  107. * Initializes a serial-over-USB CDC driver.
  108. */
  109. sduObjectInit(&SDU1);
  110. sduStart(&SDU1, &serusbcfg);
  111. /*
  112. * Activates the USB driver and then the USB bus pull-up on D+.
  113. * Note, a delay is inserted in order to not have to disconnect the cable
  114. * after a reset.
  115. */
  116. usbDisconnectBus(serusbcfg.usbp);
  117. chThdSleepMilliseconds(1500);
  118. usbStart(serusbcfg.usbp, &usbcfg);
  119. usbConnectBus(serusbcfg.usbp);
  120. /*
  121. * Shell manager initialization.
  122. */
  123. shellInit();
  124. /*
  125. * Creates the blinker thread.
  126. */
  127. chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
  128. /*
  129. * Normal main() thread activity, spawning shells.
  130. */
  131. while (true) {
  132. if (SDU1.config->usbp->state == USB_ACTIVE) {
  133. thread_t *shelltp = chThdCreateFromHeap(NULL, SHELL_WA_SIZE,
  134. "shell", NORMALPRIO + 1,
  135. shellThread, (void *)&shell_cfg1);
  136. chThdWait(shelltp); /* Waiting termination. */
  137. }
  138. chThdSleepMilliseconds(1000);
  139. }
  140. }