main.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 "ch.h"
  14. #include "hal.h"
  15. #include "usbcfg.h"
  16. SerialUSBDriver SDU1;
  17. /*
  18. * LED blinker thread, times are in milliseconds.
  19. */
  20. static THD_WORKING_AREA(waThread1, 32);
  21. static THD_FUNCTION(Thread1, arg) {
  22. (void)arg;
  23. chRegSetThreadName("Blinker");
  24. while (true) {
  25. palTogglePad(IOPORT4, BOARD_LED1);
  26. chThdSleepMilliseconds(1000);
  27. }
  28. }
  29. /*
  30. * Application entry point.
  31. */
  32. int main(void) {
  33. /*
  34. * System initializations.
  35. * - HAL initialization, this also initializes the configured device drivers
  36. * and performs the board-specific initializations.
  37. * - Kernel initialization, the main() function becomes a thread and the
  38. * RTOS is active.
  39. */
  40. halInit();
  41. chSysInit();
  42. palSetPadMode(IOPORT4, BOARD_LED1, PAL_MODE_OUTPUT_PUSHPULL);
  43. palClearPad(IOPORT4, BOARD_LED1);
  44. /*
  45. * Initializes a serial-over-USB CDC driver.
  46. */
  47. sduObjectInit(&SDU1);
  48. sduStart(&SDU1, &serusbcfg);
  49. /*
  50. * Activates the USB driver and then the USB bus pull-up on D+.
  51. * Note, a delay is inserted in order to not have to disconnect the cable
  52. * after a reset.
  53. */
  54. usbDisconnectBus(serusbcfg.usbp);
  55. chThdSleepMilliseconds(1000);
  56. usbStart(serusbcfg.usbp, &usbcfg);
  57. usbConnectBus(serusbcfg.usbp);
  58. /*
  59. * Starts the LED blinker thread.
  60. */
  61. chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
  62. while (true) {
  63. if (SDU1.config->usbp->state == USB_ACTIVE) {
  64. chnWrite(&SDU1, (const uint8_t *)"Hello World!\r\n", 14);
  65. }
  66. chThdSleepMilliseconds(2000);
  67. }
  68. }