adc_transport.c 8.8 KB

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