123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- #include "at32f403a_407.h"
- #include "ms5192t.h"
- #include "FreeRTOS.h"
- #include "task.h"
- #include "settings_api.h"
- #include "io_utils.h"
- #include "adc_transport.h"
- #include <stdio.h>
- //
- void adc_gpio_init(void)
- {
- #if 0
- gpio_init_type gpio_initstructure;
-
- crm_periph_clock_enable(CRM_GPIOE_PERIPH_CLOCK, TRUE);
-
- gpio_initstructure.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
- gpio_initstructure.gpio_pull = GPIO_PULL_NONE;
- gpio_initstructure.gpio_mode = GPIO_MODE_OUTPUT;
- gpio_initstructure.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
- gpio_initstructure.gpio_pins = GPIO_PINS_11 | GPIO_PINS_12 |
- GPIO_PINS_13 | GPIO_PINS_14;
-
- gpio_init(GPIOE, &gpio_initstructure);
- #endif
-
- gpio_init_type gpio_initstructure;
- spi_init_type spi_init_struct;
-
- crm_periph_clock_enable(CRM_IOMUX_PERIPH_CLOCK, TRUE);
- crm_periph_clock_enable(CRM_GPIOE_PERIPH_CLOCK, TRUE);
-
- gpio_pin_remap_config(SPI4_GMUX_0001, TRUE);
-
- // SCK
- gpio_initstructure.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
- gpio_initstructure.gpio_pull = GPIO_PULL_DOWN;
- gpio_initstructure.gpio_mode = GPIO_MODE_MUX;
- gpio_initstructure.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
- gpio_initstructure.gpio_pins = GPIO_PINS_11;
- gpio_init(GPIOE, &gpio_initstructure);
-
- // MISO
- gpio_initstructure.gpio_pull = GPIO_PULL_UP;
- gpio_initstructure.gpio_mode = GPIO_MODE_INPUT;
- gpio_initstructure.gpio_pins = GPIO_PINS_13;
- gpio_init(GPIOE, &gpio_initstructure);
-
- // MOSI
- gpio_initstructure.gpio_pull = GPIO_PULL_UP;
- gpio_initstructure.gpio_mode = GPIO_MODE_MUX;
- gpio_initstructure.gpio_pins = GPIO_PINS_14;
- gpio_init(GPIOE, &gpio_initstructure);
-
- // CS
- gpio_initstructure.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
- gpio_initstructure.gpio_pull = GPIO_PULL_UP;
- gpio_initstructure.gpio_mode = GPIO_MODE_OUTPUT;
- gpio_initstructure.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
- gpio_initstructure.gpio_pins = GPIO_PINS_12;
- gpio_init(GPIOE, &gpio_initstructure);
-
-
- MS5192T_CS_HIGH;
- crm_periph_clock_enable(CRM_SPI4_PERIPH_CLOCK, TRUE);
-
- spi_default_para_init(&spi_init_struct);
- spi_init_struct.transmission_mode = SPI_TRANSMIT_FULL_DUPLEX;
- spi_init_struct.master_slave_mode = SPI_MODE_MASTER;
- spi_init_struct.mclk_freq_division = SPI_MCLK_DIV_32; //SPI_MCLK_DIV_2;
- spi_init_struct.first_bit_transmission = SPI_FIRST_BIT_MSB;
- spi_init_struct.frame_bit_num = SPI_FRAME_8BIT;
- spi_init_struct.clock_polarity = SPI_CLOCK_POLARITY_HIGH;
- spi_init_struct.clock_phase = SPI_CLOCK_PHASE_2EDGE;
- spi_init_struct.cs_mode_selection = SPI_CS_SOFTWARE_MODE;
-
- spi_init(MS5192T_SPI, &spi_init_struct);
-
- //spi_hardware_cs_output_enable(SPI3, TRUE);
-
- //adc_mosi_to_spi();
-
- spi_enable(MS5192T_SPI, TRUE);
-
-
- }
- //
- void adc_mosi_high(void)
- {
- gpio_init_type gpio_initstructure;
-
- gpio_initstructure.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
- gpio_initstructure.gpio_pull = GPIO_PULL_UP;
- gpio_initstructure.gpio_mode = GPIO_MODE_OUTPUT;
- gpio_initstructure.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
- gpio_initstructure.gpio_pins = GPIO_PINS_14;
- gpio_init(GPIOE, &gpio_initstructure);
-
- gpio_bits_set(GPIOE, GPIO_PINS_14);
- }
- //
- void adc_mosi_to_spi(void)
- {
- gpio_init_type gpio_initstructure;
-
- gpio_initstructure.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
- gpio_initstructure.gpio_pull = GPIO_PULL_UP;
- gpio_initstructure.gpio_mode = GPIO_MODE_MUX;
- gpio_initstructure.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
- gpio_initstructure.gpio_pins = GPIO_PINS_14;
- gpio_init(GPIOE, &gpio_initstructure);
- }
- /***************************************************************************//**
- * @brief Initializes the SPI communication peripheral.
- *
- * @param lsbFirst - Transfer format (0 or 1).
- * Example: 0x0 - MSB first.
- * 0x1 - LSB first.
- * @param clockFreq - SPI clock frequency (Hz).
- * Example: 1000 - SPI clock frequency is 1 kHz.
- * @param clockPol - SPI clock polarity (0 or 1).
- * Example: 0x0 - idle state for SPI clock is low.
- * 0x1 - idle state for SPI clock is high.
- * @param clockPha - SPI clock phase (0 or 1).
- * Example: 0x0 - data is latched on the leading edge of SPI
- * clock and data changes on trailing edge.
- * 0x1 - data is latched on the trailing edge of SPI
- * clock and data changes on the leading edge.
- *
- * @return 0 - Initialization failed, 1 - Initialization succeeded.
- *******************************************************************************/
- unsigned char SPI_Init(unsigned char lsbFirst,
- unsigned long clockFreq,
- unsigned char clockPol,
- unsigned char clockPha)
- {
- /*
- ST7579_CS_PIN_OUT;
- ST7579_CS_HIGH;
- ADI_PART_CS_PIN_OUT;
- ADI_PART_CS_HIGH;
- R_CSI10_Start();
- */
- return(1);
- }
- /***************************************************************************//**
- * @brief Writes data to SPI.
- *
- * @param data - Write data buffer:
- * - first byte is the chip select number;
- * - from the second byte onwards are located data bytes to write.
- * @param bytesNumber - Number of bytes to write.
- *
- * @return Number of written bytes.
- *******************************************************************************/
- unsigned char SPI_Write(unsigned char* data,
- unsigned char bytesNumber)
- {
- unsigned char readData[4] = {0, 0, 0, 0};
-
- for (int i = 0; i < bytesNumber; i++)
- {
- while (spi_i2s_flag_get(MS5192T_SPI, SPI_I2S_TDBE_FLAG) == RESET);
- MS5192T_SPI->dt = data[i];
-
- while (spi_i2s_flag_get(MS5192T_SPI, SPI_I2S_RDBF_FLAG) == RESET);
- readData[i] = MS5192T_SPI->dt;
- }
- #if 0
- unsigned char chipSelect = data[0];
- unsigned char writeData[4] = {0, 0, 0, 0};
- unsigned char readData[4] = {0, 0, 0, 0};
- unsigned char byte = 0;
-
- for(byte = 0;byte < bytesNumber;byte ++)
- {
- writeData[byte] = data[byte + 1];
- }
- if(chipSelect == 1)
- {
- ADI_PART_CS_LOW;
- }
- if(chipSelect == 2)
- {
- ST7579_CS_LOW;
- }
- for(byte = 0;byte < bytesNumber;byte ++)
- {
- R_CSI10_Send_Receive((uint8_t *)&writeData[byte],
- 1,
- (uint8_t *)readData);
- while(CSIIF10 == 0);
- }
- if(chipSelect == 1)
- {
- ADI_PART_CS_HIGH;
- }
- if(chipSelect == 2)
- {
- ST7579_CS_HIGH;
- }
- return(bytesNumber);
- #endif
- }
- /***************************************************************************//**
- * @brief Reads data from SPI.
- *
- * @param data - As an input parameter, data represents the write buffer:
- * - first byte is the chip select number;
- * - from the second byte onwards are located data bytes to write.
- * As an output parameter, data represents the read buffer:
- * - from the first byte onwards are located the read data bytes.
- * @param bytesNumber - Number of bytes to write.
- *
- * @return Number of written bytes.
- *******************************************************************************/
- unsigned char SPI_Read(unsigned char* data,
- unsigned char bytesNumber)
- {
- unsigned char writeData[4] = {0, 0, 0, 0};
- unsigned char readData[4] = {0, 0, 0, 0};
- unsigned char byte = 0;
-
- for(byte = 0;byte < bytesNumber;byte ++)
- {
- writeData[byte] = data[byte + 1];
- data[byte + 1] = 0;
- }
-
- MS5192T_CS_LOW;
-
- for(byte = 0;byte < bytesNumber;byte ++)
- {
- while (spi_i2s_flag_get(MS5192T_SPI, SPI_I2S_TDBE_FLAG) == RESET);
- MS5192T_SPI->dt = writeData[byte];
-
- while (spi_i2s_flag_get(MS5192T_SPI, SPI_I2S_RDBF_FLAG) == RESET);
- readData[byte] = MS5192T_SPI->dt;
- }
-
- MS5192T_CS_HIGH;
-
- for(byte = 0;byte < bytesNumber;byte ++)
- {
- data[byte] = readData[byte];
- }
-
- return(bytesNumber);
-
- #if 0
- unsigned char chipSelect = data[0];
- unsigned char writeData[4] = {0, 0, 0, 0};
- unsigned char readData[4] = {0, 0, 0, 0};
- unsigned char byte = 0;
-
- for(byte = 0;byte < bytesNumber;byte ++)
- {
- writeData[byte] = data[byte + 1];
- data[byte + 1] = 0;
- }
- if(chipSelect == 1)
- {
- ADI_PART_CS_LOW;
- }
- if(chipSelect == 2)
- {
- ST7579_CS_LOW;
- }
- for(byte = 0;byte < bytesNumber;byte ++)
- {
- R_CSI10_Send_Receive((uint8_t *)&writeData[byte],
- 1,
- (uint8_t *)&readData[byte]);
- while(CSIIF10 == 0);
- }
- if(chipSelect == 1)
- {
- ADI_PART_CS_HIGH;
- }
- if(chipSelect == 2)
- {
- ST7579_CS_HIGH;
- }
- for(byte = 0;byte < bytesNumber;byte ++)
- {
- data[byte] = readData[byte];
- }
-
- return(bytesNumber);
- #endif
- }
- //
- void adc_test(void)
- {
- vTaskDelay(100);
- }
|