main.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. /*===========================================================================*/
  70. /* Generic code. */
  71. /*===========================================================================*/
  72. /*
  73. * Red LED blinker thread, times are in milliseconds.
  74. */
  75. static THD_WORKING_AREA(waThread1, 128);
  76. static THD_FUNCTION(Thread1, arg) {
  77. (void)arg;
  78. chRegSetThreadName("blinker");
  79. while (true) {
  80. systime_t time = serusbcfg.usbp->state == USB_ACTIVE ? 250 : 500;
  81. palClearPad(IOPORT3, GPIOC_LED);
  82. chThdSleepMilliseconds(time);
  83. palSetPad(IOPORT3, GPIOC_LED);
  84. chThdSleepMilliseconds(time);
  85. }
  86. }
  87. /*
  88. * Application entry point.
  89. */
  90. int main(void) {
  91. /*
  92. * System initializations.
  93. * - HAL initialization, this also initializes the configured device drivers
  94. * and performs the board-specific initializations.
  95. * - Kernel initialization, the main() function becomes a thread and the
  96. * RTOS is active.
  97. */
  98. halInit();
  99. chSysInit();
  100. /*
  101. * Initializes a serial-over-USB CDC driver.
  102. */
  103. sduObjectInit(&SDU1);
  104. sduStart(&SDU1, &serusbcfg);
  105. /*
  106. * Activates the USB driver and then the USB bus pull-up on D+.
  107. * Note, a delay is inserted in order to not have to disconnect the cable
  108. * after a reset.
  109. */
  110. usbDisconnectBus(serusbcfg.usbp);
  111. chThdSleepMilliseconds(1500);
  112. usbStart(serusbcfg.usbp, &usbcfg);
  113. usbConnectBus(serusbcfg.usbp);
  114. /*
  115. * Shell manager initialization.
  116. */
  117. shellInit();
  118. /*
  119. * Creates the blinker thread.
  120. */
  121. chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
  122. /*
  123. * Normal main() thread activity, spawning shells.
  124. */
  125. while (true) {
  126. if (SDU1.config->usbp->state == USB_ACTIVE) {
  127. thread_t *shelltp = chThdCreateFromHeap(NULL, SHELL_WA_SIZE,
  128. "shell", NORMALPRIO + 1,
  129. shellThread, (void *)&shell_cfg1);
  130. chThdWait(shelltp); /* Waiting termination. */
  131. }
  132. chThdSleepMilliseconds(1000);
  133. }
  134. }