main.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 "usbcfg.h"
  18. static const uint8_t txbuf[] =
  19. "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
  20. "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
  21. "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
  22. "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
  23. "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
  24. "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
  25. "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
  26. "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
  27. "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
  28. "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
  29. "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
  30. "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
  31. "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
  32. "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
  33. "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
  34. "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
  35. static uint8_t rxbuf[1024 + 1];
  36. /*
  37. * USB writer. This thread writes data to the USB at maximum rate.
  38. * Can be measured using:
  39. * dd if=/dev/xxxx of=/dev/null bs=512 count=10000
  40. */
  41. static THD_WORKING_AREA(waWriter, 128);
  42. static THD_FUNCTION(Writer, arg) {
  43. (void)arg;
  44. chRegSetThreadName("writer");
  45. while (true) {
  46. msg_t msg = usbTransmit(&USBD2, USBD2_DATA_REQUEST_EP,
  47. txbuf, sizeof (txbuf));
  48. if (msg == MSG_RESET)
  49. chThdSleepMilliseconds(500);
  50. }
  51. }
  52. /*
  53. * USB reader. This thread reads data from the USB at maximum rate.
  54. * Can be measured using:
  55. * dd if=bigfile of=/dev/xxx bs=512 count=10000
  56. */
  57. static THD_WORKING_AREA(waReader, 128);
  58. static THD_FUNCTION(Reader, arg) {
  59. (void)arg;
  60. chRegSetThreadName("reader");
  61. while (true) {
  62. msg_t msg = usbReceive(&USBD2, USBD2_DATA_AVAILABLE_EP,
  63. rxbuf, sizeof (rxbuf));
  64. if (msg == MSG_RESET)
  65. chThdSleepMilliseconds(500);
  66. }
  67. }
  68. /*
  69. * Red LED blinker thread, times are in milliseconds.
  70. */
  71. static THD_WORKING_AREA(waThread1, 128);
  72. static THD_FUNCTION(Thread1, arg) {
  73. (void)arg;
  74. chRegSetThreadName("blinker");
  75. while (true) {
  76. systime_t time;
  77. time = USBD2.state == USB_ACTIVE ? 250 : 500;
  78. palClearLine(LINE_ARD_D13);
  79. chThdSleepMilliseconds(time);
  80. palSetLine(LINE_ARD_D13);
  81. chThdSleepMilliseconds(time);
  82. }
  83. }
  84. /*
  85. * Application entry point.
  86. */
  87. int main(void) {
  88. /*
  89. * System initializations.
  90. * - HAL initialization, this also initializes the configured device drivers
  91. * and performs the board-specific initializations.
  92. * - Kernel initialization, the main() function becomes a thread and the
  93. * RTOS is active.
  94. */
  95. halInit();
  96. chSysInit();
  97. /*
  98. * GPIOI1 is programmed as output (board LED).
  99. */
  100. palClearLine(LINE_ARD_D13);
  101. palSetLineMode(LINE_ARD_D13, PAL_MODE_OUTPUT_PUSHPULL);
  102. /*
  103. * Activates the USB driver and then the USB bus pull-up on D+.
  104. * Note, a delay is inserted in order to not have to disconnect the cable
  105. * after a reset.
  106. */
  107. usbDisconnectBus(&USBD2);
  108. chThdSleepMilliseconds(1500);
  109. usbStart(&USBD2, &usbcfg);
  110. usbConnectBus(&USBD2);
  111. /*
  112. * Starting threads.
  113. */
  114. chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
  115. chThdCreateStatic(waWriter, sizeof(waWriter), NORMALPRIO, Writer, NULL);
  116. chThdCreateStatic(waReader, sizeof(waReader), NORMALPRIO, Reader, NULL);
  117. /*
  118. * Normal main() thread activity, in this demo it does nothing except
  119. * sleeping in a loop and check the button state.
  120. */
  121. while (true) {
  122. chThdSleepMilliseconds(1000);
  123. }
  124. }