adc_transport.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. #include "at32f403a_407.h"
  2. #include "ms5192t.h"
  3. #include "FreeRTOS.h"
  4. #include "task.h"
  5. #include "settings_api.h"
  6. #include "io_utils.h"
  7. #include "adc_transport.h"
  8. #include <stdio.h>
  9. //
  10. void adc_gpio_init(void)
  11. {
  12. #if 0
  13. gpio_init_type gpio_initstructure;
  14. crm_periph_clock_enable(CRM_GPIOE_PERIPH_CLOCK, TRUE);
  15. gpio_initstructure.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
  16. gpio_initstructure.gpio_pull = GPIO_PULL_NONE;
  17. gpio_initstructure.gpio_mode = GPIO_MODE_OUTPUT;
  18. gpio_initstructure.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  19. gpio_initstructure.gpio_pins = GPIO_PINS_11 | GPIO_PINS_12 |
  20. GPIO_PINS_13 | GPIO_PINS_14;
  21. gpio_init(GPIOE, &gpio_initstructure);
  22. #endif
  23. gpio_init_type gpio_initstructure;
  24. spi_init_type spi_init_struct;
  25. crm_periph_clock_enable(CRM_IOMUX_PERIPH_CLOCK, TRUE);
  26. crm_periph_clock_enable(CRM_GPIOE_PERIPH_CLOCK, TRUE);
  27. gpio_pin_remap_config(SPI4_GMUX_0001, TRUE);
  28. // SCK
  29. gpio_initstructure.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
  30. gpio_initstructure.gpio_pull = GPIO_PULL_DOWN;
  31. gpio_initstructure.gpio_mode = GPIO_MODE_MUX;
  32. gpio_initstructure.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  33. gpio_initstructure.gpio_pins = GPIO_PINS_11;
  34. gpio_init(GPIOE, &gpio_initstructure);
  35. // MISO
  36. gpio_initstructure.gpio_pull = GPIO_PULL_UP;
  37. gpio_initstructure.gpio_mode = GPIO_MODE_INPUT;
  38. gpio_initstructure.gpio_pins = GPIO_PINS_13;
  39. gpio_init(GPIOE, &gpio_initstructure);
  40. // MOSI
  41. gpio_initstructure.gpio_pull = GPIO_PULL_UP;
  42. gpio_initstructure.gpio_mode = GPIO_MODE_MUX;
  43. gpio_initstructure.gpio_pins = GPIO_PINS_14;
  44. gpio_init(GPIOE, &gpio_initstructure);
  45. // CS
  46. gpio_initstructure.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
  47. gpio_initstructure.gpio_pull = GPIO_PULL_UP;
  48. gpio_initstructure.gpio_mode = GPIO_MODE_OUTPUT;
  49. gpio_initstructure.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  50. gpio_initstructure.gpio_pins = GPIO_PINS_12;
  51. gpio_init(GPIOE, &gpio_initstructure);
  52. MS5192T_CS_HIGH;
  53. crm_periph_clock_enable(CRM_SPI4_PERIPH_CLOCK, TRUE);
  54. spi_default_para_init(&spi_init_struct);
  55. spi_init_struct.transmission_mode = SPI_TRANSMIT_FULL_DUPLEX;
  56. spi_init_struct.master_slave_mode = SPI_MODE_MASTER;
  57. spi_init_struct.mclk_freq_division = SPI_MCLK_DIV_32; //SPI_MCLK_DIV_2;
  58. spi_init_struct.first_bit_transmission = SPI_FIRST_BIT_MSB;
  59. spi_init_struct.frame_bit_num = SPI_FRAME_8BIT;
  60. spi_init_struct.clock_polarity = SPI_CLOCK_POLARITY_HIGH;
  61. spi_init_struct.clock_phase = SPI_CLOCK_PHASE_2EDGE;
  62. spi_init_struct.cs_mode_selection = SPI_CS_SOFTWARE_MODE;
  63. spi_init(MS5192T_SPI, &spi_init_struct);
  64. //spi_hardware_cs_output_enable(SPI3, TRUE);
  65. //adc_mosi_to_spi();
  66. spi_enable(MS5192T_SPI, TRUE);
  67. }
  68. //
  69. void adc_mosi_high(void)
  70. {
  71. gpio_init_type gpio_initstructure;
  72. gpio_initstructure.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
  73. gpio_initstructure.gpio_pull = GPIO_PULL_UP;
  74. gpio_initstructure.gpio_mode = GPIO_MODE_OUTPUT;
  75. gpio_initstructure.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  76. gpio_initstructure.gpio_pins = GPIO_PINS_14;
  77. gpio_init(GPIOE, &gpio_initstructure);
  78. gpio_bits_set(GPIOE, GPIO_PINS_14);
  79. }
  80. //
  81. void adc_mosi_to_spi(void)
  82. {
  83. gpio_init_type gpio_initstructure;
  84. gpio_initstructure.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
  85. gpio_initstructure.gpio_pull = GPIO_PULL_UP;
  86. gpio_initstructure.gpio_mode = GPIO_MODE_MUX;
  87. gpio_initstructure.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  88. gpio_initstructure.gpio_pins = GPIO_PINS_14;
  89. gpio_init(GPIOE, &gpio_initstructure);
  90. }
  91. /***************************************************************************//**
  92. * @brief Initializes the SPI communication peripheral.
  93. *
  94. * @param lsbFirst - Transfer format (0 or 1).
  95. * Example: 0x0 - MSB first.
  96. * 0x1 - LSB first.
  97. * @param clockFreq - SPI clock frequency (Hz).
  98. * Example: 1000 - SPI clock frequency is 1 kHz.
  99. * @param clockPol - SPI clock polarity (0 or 1).
  100. * Example: 0x0 - idle state for SPI clock is low.
  101. * 0x1 - idle state for SPI clock is high.
  102. * @param clockPha - SPI clock phase (0 or 1).
  103. * Example: 0x0 - data is latched on the leading edge of SPI
  104. * clock and data changes on trailing edge.
  105. * 0x1 - data is latched on the trailing edge of SPI
  106. * clock and data changes on the leading edge.
  107. *
  108. * @return 0 - Initialization failed, 1 - Initialization succeeded.
  109. *******************************************************************************/
  110. unsigned char SPI_Init(unsigned char lsbFirst,
  111. unsigned long clockFreq,
  112. unsigned char clockPol,
  113. unsigned char clockPha)
  114. {
  115. /*
  116. ST7579_CS_PIN_OUT;
  117. ST7579_CS_HIGH;
  118. ADI_PART_CS_PIN_OUT;
  119. ADI_PART_CS_HIGH;
  120. R_CSI10_Start();
  121. */
  122. return(1);
  123. }
  124. /***************************************************************************//**
  125. * @brief Writes data to SPI.
  126. *
  127. * @param data - Write data buffer:
  128. * - first byte is the chip select number;
  129. * - from the second byte onwards are located data bytes to write.
  130. * @param bytesNumber - Number of bytes to write.
  131. *
  132. * @return Number of written bytes.
  133. *******************************************************************************/
  134. unsigned char SPI_Write(unsigned char* data,
  135. unsigned char bytesNumber)
  136. {
  137. unsigned char readData[4] = {0, 0, 0, 0};
  138. for (int i = 0; i < bytesNumber; i++)
  139. {
  140. while (spi_i2s_flag_get(MS5192T_SPI, SPI_I2S_TDBE_FLAG) == RESET);
  141. MS5192T_SPI->dt = data[i];
  142. while (spi_i2s_flag_get(MS5192T_SPI, SPI_I2S_RDBF_FLAG) == RESET);
  143. readData[i] = MS5192T_SPI->dt;
  144. }
  145. #if 0
  146. unsigned char chipSelect = data[0];
  147. unsigned char writeData[4] = {0, 0, 0, 0};
  148. unsigned char readData[4] = {0, 0, 0, 0};
  149. unsigned char byte = 0;
  150. for(byte = 0;byte < bytesNumber;byte ++)
  151. {
  152. writeData[byte] = data[byte + 1];
  153. }
  154. if(chipSelect == 1)
  155. {
  156. ADI_PART_CS_LOW;
  157. }
  158. if(chipSelect == 2)
  159. {
  160. ST7579_CS_LOW;
  161. }
  162. for(byte = 0;byte < bytesNumber;byte ++)
  163. {
  164. R_CSI10_Send_Receive((uint8_t *)&writeData[byte],
  165. 1,
  166. (uint8_t *)readData);
  167. while(CSIIF10 == 0);
  168. }
  169. if(chipSelect == 1)
  170. {
  171. ADI_PART_CS_HIGH;
  172. }
  173. if(chipSelect == 2)
  174. {
  175. ST7579_CS_HIGH;
  176. }
  177. return(bytesNumber);
  178. #endif
  179. }
  180. /***************************************************************************//**
  181. * @brief Reads data from SPI.
  182. *
  183. * @param data - As an input parameter, data represents the write buffer:
  184. * - first byte is the chip select number;
  185. * - from the second byte onwards are located data bytes to write.
  186. * As an output parameter, data represents the read buffer:
  187. * - from the first byte onwards are located the read data bytes.
  188. * @param bytesNumber - Number of bytes to write.
  189. *
  190. * @return Number of written bytes.
  191. *******************************************************************************/
  192. unsigned char SPI_Read(unsigned char* data,
  193. unsigned char bytesNumber)
  194. {
  195. unsigned char writeData[4] = {0, 0, 0, 0};
  196. unsigned char readData[4] = {0, 0, 0, 0};
  197. unsigned char byte = 0;
  198. for(byte = 0;byte < bytesNumber;byte ++)
  199. {
  200. writeData[byte] = data[byte + 1];
  201. data[byte + 1] = 0;
  202. }
  203. MS5192T_CS_LOW;
  204. for(byte = 0;byte < bytesNumber;byte ++)
  205. {
  206. while (spi_i2s_flag_get(MS5192T_SPI, SPI_I2S_TDBE_FLAG) == RESET);
  207. MS5192T_SPI->dt = writeData[byte];
  208. while (spi_i2s_flag_get(MS5192T_SPI, SPI_I2S_RDBF_FLAG) == RESET);
  209. readData[byte] = MS5192T_SPI->dt;
  210. }
  211. MS5192T_CS_HIGH;
  212. for(byte = 0;byte < bytesNumber;byte ++)
  213. {
  214. data[byte] = readData[byte];
  215. }
  216. return(bytesNumber);
  217. #if 0
  218. unsigned char chipSelect = data[0];
  219. unsigned char writeData[4] = {0, 0, 0, 0};
  220. unsigned char readData[4] = {0, 0, 0, 0};
  221. unsigned char byte = 0;
  222. for(byte = 0;byte < bytesNumber;byte ++)
  223. {
  224. writeData[byte] = data[byte + 1];
  225. data[byte + 1] = 0;
  226. }
  227. if(chipSelect == 1)
  228. {
  229. ADI_PART_CS_LOW;
  230. }
  231. if(chipSelect == 2)
  232. {
  233. ST7579_CS_LOW;
  234. }
  235. for(byte = 0;byte < bytesNumber;byte ++)
  236. {
  237. R_CSI10_Send_Receive((uint8_t *)&writeData[byte],
  238. 1,
  239. (uint8_t *)&readData[byte]);
  240. while(CSIIF10 == 0);
  241. }
  242. if(chipSelect == 1)
  243. {
  244. ADI_PART_CS_HIGH;
  245. }
  246. if(chipSelect == 2)
  247. {
  248. ST7579_CS_HIGH;
  249. }
  250. for(byte = 0;byte < bytesNumber;byte ++)
  251. {
  252. data[byte] = readData[byte];
  253. }
  254. return(bytesNumber);
  255. #endif
  256. }
  257. //
  258. void adc_test(void)
  259. {
  260. vTaskDelay(100);
  261. }