adc_transport.c 9.1 KB

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