fft.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /* Copyright 2018 Canaan Inc.
  2. *
  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. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. #ifndef _DRIVER_FFT_H
  16. #define _DRIVER_FFT_H
  17. #include <stdint.h>
  18. #include "platform.h"
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. typedef struct _complex_hard
  23. {
  24. int16_t real;
  25. int16_t imag;
  26. } complex_hard_t;
  27. typedef struct _fft_data
  28. {
  29. int16_t I1;
  30. int16_t R1;
  31. int16_t I2;
  32. int16_t R2;
  33. } fft_data_t;
  34. typedef enum _fft_point
  35. {
  36. FFT_512,
  37. FFT_256,
  38. FFT_128,
  39. FFT_64,
  40. } fft_point_t;
  41. typedef enum _fft_direction
  42. {
  43. FFT_DIR_BACKWARD,
  44. FFT_DIR_FORWARD,
  45. FFT_DIR_MAX,
  46. } fft_direction_t;
  47. /**
  48. * @brief FFT algorithm accelerator register
  49. *
  50. * @note FFT algorithm accelerator register table
  51. *
  52. * | Offset | Name | Description |
  53. * |-----------|----------------|-------------------------------------|
  54. * | 0x00 | fft_input_fifo | input data fifo |
  55. * | 0x08 | fft_ctrl | fft ctrl reg |
  56. * | 0x10 | fifo_ctrl | fifo ctrl |
  57. * | 0x18 | intr_mask | interrupt mask |
  58. * | 0x20 | intr_clear | interrupt clear |
  59. * | 0x28 | fft_status | fft status reg |
  60. * | 0x30 | fft_status_raw | fft_status_raw |
  61. * | 0x38 | fft_output_fifo| fft_output_fifo |
  62. *
  63. */
  64. /**
  65. * @brief The calculation data is input through this register
  66. *
  67. * No. 0 Register (0x00)
  68. */
  69. typedef struct _fft_input_fifo
  70. {
  71. uint64_t fft_input_fifo : 64;
  72. } __attribute__((packed, aligned(8))) fft_input_fifo_t;
  73. /**
  74. * @brief fft ctrl reg
  75. *
  76. * No. 1 Register (0x08)
  77. */
  78. typedef struct _fft_fft_ctrl
  79. {
  80. /**
  81. *FFT calculation data length:
  82. *b'000:512 point; b'001:256 point; b'010:128 point; b'011:64 point;
  83. */
  84. uint64_t fft_point : 3;
  85. /* FFT mode: b'0:FFT b'1:IFFT */
  86. uint64_t fft_mode : 1;
  87. /* Corresponding to the nine layer butterfly shift operation, 0x0: does not shift; 0x1: shift 1st layer. ...*/
  88. uint64_t fft_shift : 9;
  89. /* FFT enable: b'0:disable b'1:enable */
  90. uint64_t fft_enable : 1;
  91. /* FFT DMA enable: b'0:disable b'1:enable */
  92. uint64_t dma_send : 1;
  93. /**
  94. *Input data arrangement: b'00:RIRI; b'01:only real part exist, RRRR;
  95. *b'10:First input the real part and then input the imaginary part.
  96. */
  97. uint64_t fft_input_mode : 2;
  98. /* Effective width of input data. b'0:64bit effective; b'1:32bit effective */
  99. uint64_t fft_data_mode : 1;
  100. uint64_t reserved : 46;
  101. } __attribute__((packed, aligned(8))) fft_fft_ctrl_t;
  102. /**
  103. * @brief fifo ctrl
  104. *
  105. * No. 2 Register (0x10)
  106. */
  107. typedef struct _fft_fifo_ctrl
  108. {
  109. /* Response memory initialization flag.b'1:initialization */
  110. uint64_t resp_fifo_flush_n : 1;
  111. /* Command memory initialization flag.b'1:initialization */
  112. uint64_t cmd_fifo_flush_n : 1;
  113. /* Output interface memory initialization flag.b'1:initialization */
  114. uint64_t gs_fifo_flush_n : 1;
  115. uint64_t reserved : 61;
  116. } __attribute__((packed, aligned(8))) fft_fifo_ctrl_t;
  117. /**
  118. * @brief interrupt mask
  119. *
  120. * No. 3 Register (0x18)
  121. */
  122. typedef struct _fft_intr_mask
  123. {
  124. /**
  125. *FFT return status set.
  126. *b'0:FFT returns to the state after completion.
  127. *b'1:FFT does not return to the state after completion
  128. */
  129. uint64_t fft_done_mask : 1;
  130. uint64_t reserved : 63;
  131. } __attribute__((packed, aligned(8))) fft_intr_mask_t;
  132. /**
  133. * @brief interrupt clear
  134. *
  135. * No. 4 Register (0x20)
  136. */
  137. typedef struct _fft_intr_clear
  138. {
  139. /* The interrupt state clears. b'1:clear current interrupt request */
  140. uint64_t fft_done_clear : 1;
  141. uint64_t reserved1 : 63;
  142. } __attribute__((packed, aligned(8))) fft_intr_clear_t;
  143. /**
  144. * @brief fft status reg
  145. *
  146. * No. 5 Register (0x28)
  147. */
  148. typedef struct _fft_status
  149. {
  150. /* FFT calculation state.b'0:not completed; b'1:completed */
  151. uint64_t fft_done_status : 1;
  152. uint64_t reserved1 : 63;
  153. } __attribute__((packed, aligned(8))) fft_status_t;
  154. /**
  155. * @brief fft status raw
  156. *
  157. * No. 6 Register (0x30)
  158. */
  159. typedef struct _fft_status_raw
  160. {
  161. /* FFT calculation state. b'1:done */
  162. uint64_t fft_done_status_raw : 1;
  163. /* FFT calculation state. b'1:working */
  164. uint64_t fft_work_status_raw : 1;
  165. uint64_t reserved : 62;
  166. } __attribute__((packed, aligned(8))) fft_status_raw_t;
  167. /**
  168. * @brief Output of FFT calculation data through this register
  169. *
  170. * No. 7 Register (0x38)
  171. */
  172. typedef struct _fft_output_fifo
  173. {
  174. uint64_t fft_output_fifo : 64;
  175. } __attribute__((packed, aligned(8))) fft_output_fifo_t;
  176. /**
  177. * @brief Fast Fourier transform (FFT) algorithm accelerator object
  178. *
  179. * A fast Fourier transform (FFT) algorithm computes the discrete
  180. * Fourier transform (DFT) of a sequence, or its inverse (IFFT).
  181. * Fourier analysis converts a signal from its original domain
  182. * (often time or space) to a representation in the frequency
  183. * domain and vice versa. An FFT rapidly computes such
  184. * transformations by factorizing the DFT matrix into a product of
  185. * sparse (mostly zero) factors.
  186. */
  187. typedef struct _fft
  188. {
  189. /* No. 0 (0x00): input data fifo */
  190. fft_input_fifo_t fft_input_fifo;
  191. /* No. 1 (0x08): fft ctrl reg */
  192. fft_fft_ctrl_t fft_ctrl;
  193. /* No. 2 (0x10): fifo ctrl */
  194. fft_fifo_ctrl_t fifo_ctrl;
  195. /* No. 3 (0x18): interrupt mask */
  196. fft_intr_mask_t intr_mask;
  197. /* No. 4 (0x20): interrupt clear */
  198. fft_intr_clear_t intr_clear;
  199. /* No. 5 (0x28): fft status reg */
  200. fft_status_t fft_status;
  201. /* No. 6 (0x30): fft_status_raw */
  202. fft_status_raw_t fft_status_raw;
  203. /* No. 7 (0x38): fft_output_fifo */
  204. fft_output_fifo_t fft_output_fifo;
  205. } __attribute__((packed, aligned(8))) fft_t;
  206. /**
  207. * @brief Do 16bit quantized complex FFT by DMA
  208. *
  209. * @param[in] dma_send_channel_num Dmac send channel number.
  210. * @param[in] dma_receive_channel_num Dmac receive channel number.
  211. * @param[in] shift The shifts selection in 9 stage
  212. * @param[in] direction The direction
  213. * @param[in] input The input data
  214. * @param[in] point The FFT points count
  215. * @param[out] output The output data
  216. */
  217. void fft_complex_uint16_dma(dmac_channel_number_t dma_send_channel_num,
  218. dmac_channel_number_t dma_receive_channel_num,
  219. uint16_t shift,
  220. fft_direction_t direction,
  221. const uint64_t *input,
  222. size_t point_num,
  223. uint64_t *output);
  224. #ifdef __cplusplus
  225. }
  226. #endif
  227. #endif /* _DRIVER_FFT_H */