Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

163 lines
5.4KB

  1. /* USB EHCI Host for Teensy 3.6
  2. * Copyright 2017 Paul Stoffregen (paul@pjrc.com)
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sublicense, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included
  13. * in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  16. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <Arduino.h>
  24. #include "USBHost_t36.h" // Read this header first for key info
  25. // Memory allocation for Device_t, Pipe_t and Transfer_t structures.
  26. //
  27. // To provide an Arduino-friendly experience, the memory allocation of
  28. // these item is primarily done by the instances of device driver objects,
  29. // which are typically created as static objects near the beginning of
  30. // the Arduino sketch. Static allocation allows Arduino's memory usage
  31. // summary to accurately show the amount of RAM this library is using.
  32. // Users can choose which devices they wish to support and how many of
  33. // each by creating more object instances.
  34. //
  35. // Device driver objects "contribute" their copies of these structures.
  36. // When ehci.cpp allocates Pipe_t and Transfer_t, or enumeration.cpp
  37. // allocates Device_t, the memory actually comes from these structures
  38. // physically located within the device driver instances. The usage
  39. // model looks like traditional malloc/free dynamic memory on the heap,
  40. // but in fact it's a simple memory pool from the drivers.
  41. //
  42. // Timing is deterministic and fast, because each pool allocates only
  43. // a single fixed size object. In theory, each driver should contribute
  44. // the number of items it will use, so we should not ever end up with
  45. // a situation where an item can't be allocated when it's needed. Well,
  46. // unless there's a bug or oversight...
  47. // Lists of "free" memory
  48. static Device_t * free_Device_list = NULL;
  49. static Pipe_t * free_Pipe_list = NULL;
  50. static Transfer_t * free_Transfer_list = NULL;
  51. static strbuf_t * free_strbuf_list = NULL;
  52. // A small amount of non-driver memory, just to get things started
  53. // TODO: is this really necessary? Can these be eliminated, so we
  54. // use only memory from the drivers?
  55. static Device_t memory_Device[1];
  56. static Pipe_t memory_Pipe[1] __attribute__ ((aligned(32)));
  57. static Transfer_t memory_Transfer[4] __attribute__ ((aligned(32)));
  58. void USBHost::init_Device_Pipe_Transfer_memory(void)
  59. {
  60. contribute_Devices(memory_Device, sizeof(memory_Device)/sizeof(Device_t));
  61. contribute_Pipes(memory_Pipe, sizeof(memory_Pipe)/sizeof(Pipe_t));
  62. contribute_Transfers(memory_Transfer, sizeof(memory_Transfer)/sizeof(Transfer_t));
  63. }
  64. Device_t * USBHost::allocate_Device(void)
  65. {
  66. Device_t *device = free_Device_list;
  67. if (device) free_Device_list = *(Device_t **)device;
  68. return device;
  69. }
  70. void USBHost::free_Device(Device_t *device)
  71. {
  72. *(Device_t **)device = free_Device_list;
  73. free_Device_list = device;
  74. }
  75. Pipe_t * USBHost::allocate_Pipe(void)
  76. {
  77. Pipe_t *pipe = free_Pipe_list;
  78. if (pipe) free_Pipe_list = *(Pipe_t **)pipe;
  79. return pipe;
  80. }
  81. void USBHost::free_Pipe(Pipe_t *pipe)
  82. {
  83. *(Pipe_t **)pipe = free_Pipe_list;
  84. free_Pipe_list = pipe;
  85. }
  86. Transfer_t * USBHost::allocate_Transfer(void)
  87. {
  88. Transfer_t *transfer = free_Transfer_list;
  89. if (transfer) free_Transfer_list = *(Transfer_t **)transfer;
  90. return transfer;
  91. }
  92. void USBHost::free_Transfer(Transfer_t *transfer)
  93. {
  94. *(Transfer_t **)transfer = free_Transfer_list;
  95. free_Transfer_list = transfer;
  96. }
  97. strbuf_t * USBHost::allocate_string_buffer(void)
  98. {
  99. strbuf_t *strbuf = free_strbuf_list;
  100. if (strbuf) {
  101. free_strbuf_list = *(strbuf_t **)strbuf;
  102. strbuf->iStrings[strbuf_t::STR_ID_MAN] = 0; // Set indexes into string buffer to say not there...
  103. strbuf->iStrings[strbuf_t::STR_ID_PROD] = 0;
  104. strbuf->iStrings[strbuf_t::STR_ID_SERIAL] = 0;
  105. strbuf->buffer[0] = 0; // have trailing NULL..
  106. }
  107. return strbuf;
  108. }
  109. void USBHost::free_string_buffer(strbuf_t *strbuf)
  110. {
  111. *(strbuf_t **)strbuf = free_strbuf_list;
  112. free_strbuf_list = strbuf;
  113. }
  114. void USBHost::contribute_Devices(Device_t *devices, uint32_t num)
  115. {
  116. Device_t *end = devices + num;
  117. for (Device_t *device = devices ; device < end; device++) {
  118. free_Device(device);
  119. }
  120. }
  121. void USBHost::contribute_Pipes(Pipe_t *pipes, uint32_t num)
  122. {
  123. Pipe_t *end = pipes + num;
  124. for (Pipe_t *pipe = pipes; pipe < end; pipe++) {
  125. free_Pipe(pipe);
  126. }
  127. }
  128. void USBHost::contribute_Transfers(Transfer_t *transfers, uint32_t num)
  129. {
  130. Transfer_t *end = transfers + num;
  131. for (Transfer_t *transfer = transfers ; transfer < end; transfer++) {
  132. free_Transfer(transfer);
  133. }
  134. }
  135. void USBHost::contribute_String_Buffers(strbuf_t *strbufs, uint32_t num)
  136. {
  137. strbuf_t *end = strbufs + num;
  138. for (strbuf_t *str = strbufs ; str < end; str++) {
  139. free_string_buffer(str);
  140. }
  141. }