alt_driver.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #ifndef __ALT_DRIVER_H__
  2. #define __ALT_DRIVER_H__
  3. /******************************************************************************
  4. * *
  5. * License Agreement *
  6. * *
  7. * Copyright (c) 2006 Altera Corporation, San Jose, California, USA. *
  8. * All rights reserved. *
  9. * *
  10. * Permission is hereby granted, free of charge, to any person obtaining a *
  11. * copy of this software and associated documentation files (the "Software"), *
  12. * to deal in the Software without restriction, including without limitation *
  13. * the rights to use, copy, modify, merge, publish, distribute, sublicense, *
  14. * and/or sell copies of the Software, and to permit persons to whom the *
  15. * Software is furnished to do so, subject to the following conditions: *
  16. * *
  17. * The above copyright notice and this permission notice shall be included in *
  18. * all copies or substantial portions of the Software. *
  19. * *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *
  21. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *
  22. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
  23. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
  24. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *
  25. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *
  26. * DEALINGS IN THE SOFTWARE. *
  27. * *
  28. * This agreement shall be governed in all respects by the laws of the State *
  29. * of California and by the laws of the United States of America. *
  30. * *
  31. * Altera does not recommend, suggest or require that this reference design *
  32. * file be used in conjunction or combination with any other product. *
  33. ******************************************************************************/
  34. /*
  35. * Macros used to access a driver without HAL file descriptors.
  36. */
  37. /*
  38. * ALT_MODULE_CLASS
  39. *
  40. * This macro returns the module class name for the specified module instance.
  41. * It uses information in the system.h file.
  42. * Neither the instance name or class name are quoted (so that they can
  43. * be used with other pre-processor macros).
  44. *
  45. * Example:
  46. * Assume the design has an instance of an altera_avalon_uart called uart1.
  47. * Calling ALT_MODULE_CLASS(uart1) returns altera_avalon_uart.
  48. */
  49. #define ALT_MODULE_CLASS(instance) ALT_MODULE_CLASS_ ## instance
  50. /*
  51. * ALT_DRIVER_FUNC_NAME
  52. *
  53. * --> instance Instance name.
  54. * --> func Function name.
  55. *
  56. * This macro returns the device driver function name of the specified
  57. * module instance for the specified function name.
  58. *
  59. * Example:
  60. * Assume the design has an instance of an altera_avalon_uart called uart1.
  61. * Calling ALT_DRIVER_FUNC_NAME(uart1, write) returns
  62. * altera_avalon_uart_write.
  63. */
  64. #define ALT_DRIVER_FUNC_NAME(instance, func) \
  65. ALT_DRIVER_FUNC_NAME1(ALT_MODULE_CLASS(instance), func)
  66. #define ALT_DRIVER_FUNC_NAME1(module_class, func) \
  67. ALT_DRIVER_FUNC_NAME2(module_class, func)
  68. #define ALT_DRIVER_FUNC_NAME2(module_class, func) \
  69. module_class ## _ ## func
  70. /*
  71. * ALT_DRIVER_STATE_STRUCT
  72. *
  73. * --> instance Instance name.
  74. *
  75. * This macro returns the device driver state type name of the specified
  76. * module instance.
  77. *
  78. * Example:
  79. * Assume the design has an instance of an altera_avalon_uart called uart1.
  80. * Calling ALT_DRIVER_STATE_STRUCT(uart1) returns:
  81. * struct altera_avalon_uart_state_s
  82. *
  83. * Note that the ALT_DRIVER_FUNC_NAME macro is used even though "state" isn't
  84. * really a function but it does match the required naming convention.
  85. */
  86. #define ALT_DRIVER_STATE_STRUCT(instance) \
  87. struct ALT_DRIVER_FUNC_NAME(instance, state_s)
  88. /*
  89. * ALT_DRIVER_STATE
  90. *
  91. * --> instance Instance name.
  92. *
  93. * This macro returns the device driver state name of the specified
  94. * module instance.
  95. *
  96. * Example:
  97. * Assume the design has an instance of an altera_avalon_uart called uart1.
  98. * Calling ALT_DRIVER_STATE(uart1) returns uart1.
  99. */
  100. #define ALT_DRIVER_STATE(instance) instance
  101. /*
  102. * ALT_DRIVER_WRITE
  103. *
  104. * --> instance Instance name.
  105. * --> buffer Write buffer.
  106. * --> len Length of write buffer data.
  107. * --> flags Control flags (e.g. O_NONBLOCK)
  108. *
  109. * This macro calls the "write" function of the specified driver instance.
  110. */
  111. #define ALT_DRIVER_WRITE_EXTERNS(instance) \
  112. extern ALT_DRIVER_STATE_STRUCT(instance) ALT_DRIVER_STATE(instance); \
  113. extern int ALT_DRIVER_FUNC_NAME(instance, write) \
  114. (ALT_DRIVER_STATE_STRUCT(instance) *, const char *, int, int);
  115. #define ALT_DRIVER_WRITE(instance, buffer, len, flags) \
  116. ALT_DRIVER_FUNC_NAME(instance, write)(&ALT_DRIVER_STATE(instance), buffer, len, flags)
  117. /*
  118. * ALT_DRIVER_READ
  119. *
  120. * --> instance Instance name.
  121. * <-- buffer Read buffer.
  122. * --> len Length of read buffer.
  123. * --> flags Control flags (e.g. O_NONBLOCK)
  124. *
  125. * This macro calls the "read" function of the specified driver instance.
  126. */
  127. #define ALT_DRIVER_READ_EXTERNS(instance) \
  128. extern ALT_DRIVER_STATE_STRUCT(instance) ALT_DRIVER_STATE(instance); \
  129. extern int ALT_DRIVER_FUNC_NAME(instance, read) \
  130. (ALT_DRIVER_STATE_STRUCT(instance) *, const char *, int, int);
  131. #define ALT_DRIVER_READ(instance, buffer, len, flags) \
  132. ALT_DRIVER_FUNC_NAME(instance, read)(&ALT_DRIVER_STATE(instance), buffer, len, flags)
  133. /*
  134. * ALT_DRIVER_IOCTL
  135. *
  136. * --> instance Instance name.
  137. * --> req ioctl request (e.g. TIOCSTIMEOUT)
  138. * --> arg Optional argument (void*)
  139. *
  140. * This macro calls the "ioctl" function of the specified driver instance
  141. */
  142. #define ALT_DRIVER_IOCTL_EXTERNS(instance) \
  143. extern ALT_DRIVER_STATE_STRUCT(instance) ALT_DRIVER_STATE(instance); \
  144. extern int ALT_DRIVER_FUNC_NAME(instance, ioctl) \
  145. (ALT_DRIVER_STATE_STRUCT(instance) *, int, void*);
  146. #define ALT_DRIVER_IOCTL(instance, req, arg) \
  147. ALT_DRIVER_FUNC_NAME(instance, ioctl)(&ALT_DRIVER_STATE(instance), req, arg)
  148. #endif /* __ALT_DRIVER_H__ */