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.

memory.cpp 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. // A small amount of non-driver memory, just to get things started
  52. // TODO: is this really necessary? Can these be eliminated, so we
  53. // use only memory from the drivers?
  54. static Device_t memory_Device[1];
  55. static Pipe_t memory_Pipe[1] __attribute__ ((aligned(32)));
  56. static Transfer_t memory_Transfer[4] __attribute__ ((aligned(32)));
  57. void USBHost::init_Device_Pipe_Transfer_memory(void)
  58. {
  59. contribute_Devices(memory_Device, sizeof(memory_Device)/sizeof(Device_t));
  60. contribute_Pipes(memory_Pipe, sizeof(memory_Pipe)/sizeof(Pipe_t));
  61. contribute_Transfers(memory_Transfer, sizeof(memory_Transfer)/sizeof(Transfer_t));
  62. }
  63. Device_t * USBHost::allocate_Device(void)
  64. {
  65. Device_t *device = free_Device_list;
  66. if (device) free_Device_list = *(Device_t **)device;
  67. return device;
  68. }
  69. void USBHost::free_Device(Device_t *device)
  70. {
  71. *(Device_t **)device = free_Device_list;
  72. free_Device_list = device;
  73. }
  74. Pipe_t * USBHost::allocate_Pipe(void)
  75. {
  76. Pipe_t *pipe = free_Pipe_list;
  77. if (pipe) free_Pipe_list = *(Pipe_t **)pipe;
  78. return pipe;
  79. }
  80. void USBHost::free_Pipe(Pipe_t *pipe)
  81. {
  82. *(Pipe_t **)pipe = free_Pipe_list;
  83. free_Pipe_list = pipe;
  84. }
  85. Transfer_t * USBHost::allocate_Transfer(void)
  86. {
  87. Transfer_t *transfer = free_Transfer_list;
  88. if (transfer) free_Transfer_list = *(Transfer_t **)transfer;
  89. return transfer;
  90. }
  91. void USBHost::free_Transfer(Transfer_t *transfer)
  92. {
  93. *(Transfer_t **)transfer = free_Transfer_list;
  94. free_Transfer_list = transfer;
  95. }
  96. void USBHost::contribute_Devices(Device_t *devices, uint32_t num)
  97. {
  98. Device_t *end = devices + num;
  99. for (Device_t *device = devices ; device < end; device++) {
  100. free_Device(device);
  101. }
  102. }
  103. void USBHost::contribute_Pipes(Pipe_t *pipes, uint32_t num)
  104. {
  105. Pipe_t *end = pipes + num;
  106. for (Pipe_t *pipe = pipes; pipe < end; pipe++) {
  107. free_Pipe(pipe);
  108. }
  109. }
  110. void USBHost::contribute_Transfers(Transfer_t *transfers, uint32_t num)
  111. {
  112. Transfer_t *end = transfers + num;
  113. for (Transfer_t *transfer = transfers ; transfer < end; transfer++) {
  114. free_Transfer(transfer);
  115. }
  116. }