usb_test.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*!
  2. * Copyright (C) 2001-2003 by egnite Software GmbH
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. */
  34. #include <string.h>
  35. #include <stdio.h>
  36. #include <io.h>
  37. #include <dev/board.h>
  38. #include <sys/timer.h>
  39. #include <sys/thread.h>
  40. #include <sys/event.h>
  41. #include <dev/gpio.h>
  42. #include <dev/usb_stm32/stm32_otg.h>
  43. static char *banner = "\nNut/OS USB Sample " __DATE__ " " __TIME__ "\n";
  44. static char *pgm_ptr = "\nHello stranger and our/my friends!\n";
  45. static char inbuf[128];
  46. extern NUTDEVICE devStm32Otg;
  47. FILE *usb;
  48. #if defined(LED2_PORT) && defined( LED2_PIN)
  49. THREAD(LedBlink, arg)
  50. {
  51. GpioPinConfigSet( LED2_PORT, LED2_PIN, GPIO_CFG_OUTPUT);
  52. for(;;)
  53. {
  54. NutSleep(100);
  55. GpioPinSetHigh(LED2_PORT, LED2_PIN);
  56. NutSleep(100);
  57. GpioPinSetLow(LED2_PORT, LED2_PIN);
  58. }
  59. }
  60. #endif
  61. /*
  62. * USB sample.
  63. *
  64. */
  65. int main(void)
  66. {
  67. int res;
  68. char *cp;
  69. uint32_t baud = 9600;
  70. res = NutRegisterDevice(&DEV_UART, 0, 0);
  71. freopen(DEV_UART_NAME, "w", stdout);
  72. freopen(DEV_UART_NAME, "r", stdin);
  73. _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
  74. printf(banner);
  75. #if defined(LED1_PORT) && defined( LED1_PIN)
  76. GpioPinConfigSet( LED1_PORT, LED1_PIN, GPIO_CFG_OUTPUT);
  77. #endif
  78. if (NutThreadCreate("t2", LedBlink, 0, 512)== 0)
  79. printf("Can't create LED thread\n");
  80. else
  81. printf("LED thread started\n");
  82. printf("Trying register OTG");
  83. res = NutRegisterDevice(&devStm32Otg, 0,0);
  84. if(res)
  85. printf(" failure\n");
  86. else
  87. printf(" success\n");
  88. usb = fopen("usb_otg", "r+");
  89. if(usb == 0)
  90. printf("Can't open OTG\n");
  91. else
  92. printf("OTH open Success\n");
  93. NutSleep(1000); /* Sleep sufficent or next print will stall */
  94. fprintf(usb, banner);
  95. /*
  96. * Nut/OS never expects a thread to return. So we enter an
  97. * endless loop here.
  98. */
  99. for (;;) {
  100. #if defined(LED1_PORT) && defined( LED1_PIN)
  101. GpioPinSet(LED1_PORT, LED1_PIN,
  102. !(GpioPinGet(LED1_PORT, LED1_PIN)));
  103. #endif
  104. /*
  105. * A bit more advanced input routine is able to read a string
  106. * up to and including the first newline character or until a
  107. * specified maximum number of characters, whichever comes first.
  108. */
  109. puts("\nEnter your name: ");
  110. fflush(stdout);
  111. fgets(inbuf, sizeof(inbuf), stdin);
  112. /*
  113. * Chop off trailing linefeed.
  114. */
  115. cp = strchr(inbuf, '\n');
  116. if (cp)
  117. *cp = 0;
  118. /*
  119. * Streams support formatted output as well as printing strings
  120. * from program space.
  121. */
  122. if (inbuf[0])
  123. printf("\nHello %s!\n", inbuf);
  124. else {
  125. puts(pgm_ptr);
  126. }
  127. fprintf(usb, "Bla\n");
  128. }
  129. return 0;
  130. }