adc_transport.c 10 KB

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