|
@@ -1,123 +1,131 @@
|
|
|
-#include "at32f403a_407.h"
|
|
|
-#include "shift_reg.h"
|
|
|
-#include "FreeRTOS.h"
|
|
|
-#include "task.h"
|
|
|
-#include <stdio.h>
|
|
|
-#include <stdbool.h>
|
|
|
-
|
|
|
-
|
|
|
-#define SH_SPI SPI3
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-void sh_init(void)
|
|
|
-{
|
|
|
- spi_init_type spi_init_struct;
|
|
|
- gpio_init_type gpio_initstructure;
|
|
|
-
|
|
|
-
|
|
|
- crm_periph_clock_enable(CRM_IOMUX_PERIPH_CLOCK, TRUE);
|
|
|
- crm_periph_clock_enable(CRM_GPIOC_PERIPH_CLOCK, TRUE);
|
|
|
- crm_periph_clock_enable(CRM_GPIOD_PERIPH_CLOCK, TRUE);
|
|
|
-
|
|
|
- gpio_pin_remap_config(SPI3_GMUX_0011, TRUE);
|
|
|
-
|
|
|
-
|
|
|
- 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_10;
|
|
|
- gpio_init(GPIOC, &gpio_initstructure);
|
|
|
-
|
|
|
-
|
|
|
- gpio_initstructure.gpio_pull = GPIO_PULL_UP;
|
|
|
- gpio_initstructure.gpio_mode = GPIO_MODE_INPUT;
|
|
|
- gpio_initstructure.gpio_pins = GPIO_PINS_11;
|
|
|
- gpio_init(GPIOC, &gpio_initstructure);
|
|
|
-
|
|
|
-
|
|
|
- gpio_initstructure.gpio_pull = GPIO_PULL_UP;
|
|
|
- gpio_initstructure.gpio_mode = GPIO_MODE_MUX;
|
|
|
- gpio_initstructure.gpio_pins = GPIO_PINS_12;
|
|
|
- gpio_init(GPIOC, &gpio_initstructure);
|
|
|
-
|
|
|
-
|
|
|
- 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_1;
|
|
|
- gpio_init(GPIOD, &gpio_initstructure);
|
|
|
-
|
|
|
-
|
|
|
- crm_periph_clock_enable(CRM_SPI3_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_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(SPI3, &spi_init_struct);
|
|
|
-
|
|
|
- spi_hardware_cs_output_enable(SPI3, FALSE);
|
|
|
-
|
|
|
- spi_enable(SPI3, TRUE);
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-void sh_test(uint8_t val)
|
|
|
-{
|
|
|
- uint8_t ret;
|
|
|
-
|
|
|
-#if 0
|
|
|
- gpio_bits_set(GPIOD, GPIO_PINS_1);
|
|
|
- gpio_bits_reset(GPIOD, GPIO_PINS_1);
|
|
|
-
|
|
|
- while (spi_i2s_flag_get(SH_SPI, SPI_I2S_TDBE_FLAG) == RESET);
|
|
|
- SH_SPI->dt = val;
|
|
|
-
|
|
|
- while (spi_i2s_flag_get(SH_SPI, SPI_I2S_RDBF_FLAG) == RESET);
|
|
|
- ret = SH_SPI->dt;
|
|
|
-
|
|
|
-
|
|
|
- gpio_bits_set(GPIOD, GPIO_PINS_1);
|
|
|
-
|
|
|
- vTaskDelay(1);
|
|
|
-
|
|
|
- gpio_bits_reset(GPIOD, GPIO_PINS_1);
|
|
|
-*/
|
|
|
- printf("SH return: %X\r\n", ret);
|
|
|
-#endif
|
|
|
-
|
|
|
- while (spi_i2s_flag_get(SH_SPI, SPI_I2S_TDBE_FLAG) == RESET);
|
|
|
- SH_SPI->dt = val;
|
|
|
-
|
|
|
- while (spi_i2s_flag_get(SH_SPI, SPI_I2S_RDBF_FLAG) == RESET);
|
|
|
- ret = SH_SPI->dt;
|
|
|
-
|
|
|
- gpio_bits_set(GPIOD, GPIO_PINS_1);
|
|
|
- gpio_bits_reset(GPIOD, GPIO_PINS_1);
|
|
|
-
|
|
|
- printf("SH return: %X\r\n", ret & 0x3F);
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-void sh_delay(void)
|
|
|
-{
|
|
|
- for (unsigned int i = 0; i < 1; i++) {}
|
|
|
+#include "at32f403a_407.h"
|
|
|
+#include "shift_reg.h"
|
|
|
+#include "FreeRTOS.h"
|
|
|
+#include "task.h"
|
|
|
+#include <stdio.h>
|
|
|
+#include <stdbool.h>
|
|
|
+
|
|
|
+
|
|
|
+#define SH_SPI SPI2
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+void sh_init(void)
|
|
|
+{
|
|
|
+ spi_init_type spi_init_struct;
|
|
|
+ gpio_init_type gpio_initstructure;
|
|
|
+
|
|
|
+
|
|
|
+ crm_periph_clock_enable(CRM_IOMUX_PERIPH_CLOCK, TRUE);
|
|
|
+ crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
|
|
|
+ crm_periph_clock_enable(CRM_GPIOC_PERIPH_CLOCK, TRUE);
|
|
|
+ crm_periph_clock_enable(CRM_GPIOD_PERIPH_CLOCK, TRUE);
|
|
|
+
|
|
|
+
|
|
|
+ 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_13;
|
|
|
+ gpio_init(GPIOB, &gpio_initstructure);
|
|
|
+
|
|
|
+
|
|
|
+ gpio_initstructure.gpio_pull = GPIO_PULL_UP;
|
|
|
+ gpio_initstructure.gpio_mode = GPIO_MODE_INPUT;
|
|
|
+ gpio_initstructure.gpio_pins = GPIO_PINS_14;
|
|
|
+ gpio_init(GPIOB, &gpio_initstructure);
|
|
|
+
|
|
|
+
|
|
|
+ gpio_initstructure.gpio_pull = GPIO_PULL_UP;
|
|
|
+ gpio_initstructure.gpio_mode = GPIO_MODE_MUX;
|
|
|
+ gpio_initstructure.gpio_pins = GPIO_PINS_15;
|
|
|
+ gpio_init(GPIOB, &gpio_initstructure);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ 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_0 | GPIO_PINS_1;
|
|
|
+ gpio_init(GPIOD, &gpio_initstructure);
|
|
|
+
|
|
|
+
|
|
|
+ gpio_initstructure.gpio_pins = GPIO_PINS_3;
|
|
|
+ gpio_init(GPIOD, &gpio_initstructure);
|
|
|
+
|
|
|
+ crm_periph_clock_enable(CRM_SPI2_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_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(SH_SPI, &spi_init_struct);
|
|
|
+
|
|
|
+ spi_hardware_cs_output_enable(SH_SPI, FALSE);
|
|
|
+
|
|
|
+ spi_enable(SH_SPI, TRUE);
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+void en_crnt_alrm_in(uint16_t val)
|
|
|
+{
|
|
|
+ uint16_t ret = 0;
|
|
|
+
|
|
|
+
|
|
|
+ while (spi_i2s_flag_get(SH_SPI, SPI_I2S_TDBE_FLAG) == RESET);
|
|
|
+ SH_SPI->dt = 0x02;
|
|
|
+
|
|
|
+
|
|
|
+ while (spi_i2s_flag_get(SH_SPI, SPI_I2S_RDBF_FLAG) == RESET);
|
|
|
+ ret = SH_SPI->dt & 0x3F;
|
|
|
+
|
|
|
+
|
|
|
+ while (spi_i2s_flag_get(SH_SPI, SPI_I2S_TDBE_FLAG) == RESET);
|
|
|
+ SH_SPI->dt = 0x01;
|
|
|
+
|
|
|
+
|
|
|
+ while (spi_i2s_flag_get(SH_SPI, SPI_I2S_RDBF_FLAG) == RESET);
|
|
|
+ ret |= (SH_SPI->dt & 0x3F) << 8;
|
|
|
+
|
|
|
+ gpio_bits_set(GPIOD, GPIO_PINS_0 | GPIO_PINS_1);
|
|
|
+ gpio_bits_reset(GPIOD, GPIO_PINS_0 | GPIO_PINS_1);
|
|
|
+
|
|
|
+ printf("SH return: %X\r\n", ret);
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+void an_inp(uint16_t val)
|
|
|
+{
|
|
|
+ uint16_t ret;
|
|
|
+
|
|
|
+ while (spi_i2s_flag_get(SH_SPI, SPI_I2S_TDBE_FLAG) == RESET);
|
|
|
+ SH_SPI->dt = val;
|
|
|
+
|
|
|
+ while (spi_i2s_flag_get(SH_SPI, SPI_I2S_RDBF_FLAG) == RESET);
|
|
|
+ ret = SH_SPI->dt;
|
|
|
+
|
|
|
+ gpio_bits_set(GPIOD, GPIO_PINS_3);
|
|
|
+ gpio_bits_reset(GPIOD, GPIO_PINS_3);
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+void sh_delay(void)
|
|
|
+{
|
|
|
+ for (unsigned int i = 0; i < 1; i++) {}
|
|
|
}
|