1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
#include "at32f403a_407_board.h"
#include "at32f403a_407_clock.h"
#define SPI_MASTER_CS_HIGH gpio_bits_set(GPIOA, GPIO_PINS_15)
#define SPI_MASTER_CS_LOW gpio_bits_reset(GPIOA, GPIO_PINS_15)
#define BUFFER_SIZE 32
static void dma_config(void)
{
dma_init_type dma_init_struct;
crm_periph_clock_enable(CRM_DMA1_PERIPH_CLOCK, TRUE);
/* use dma1_channel4 as spi2 receive channel */
dma_reset(DMA1_CHANNEL4);
dma_default_para_init(&dma_init_struct);
dma_init_struct.buffer_size = BUFFER_SIZE;
dma_init_struct.direction = DMA_DIR_PERIPHERAL_TO_MEMORY;
dma_init_struct.memory_base_addr = (uint32_t)spi2_rx_buffer;
dma_init_struct.memory_data_width = DMA_MEMORY_DATA_WIDTH_BYTE;
dma_init_struct.memory_inc_enable = TRUE;
dma_init_struct.peripheral_base_addr = (uint32_t)&(SPI2->dt);
dma_init_struct.peripheral_data_width = DMA_PERIPHERAL_DATA_WIDTH_BYTE;
dma_init_struct.peripheral_inc_enable = FALSE;
dma_init_struct.priority = DMA_PRIORITY_MEDIUM;
dma_init_struct.loop_mode_enable = FALSE;
dma_init(DMA1_CHANNEL4, &dma_init_struct);
crm_periph_clock_enable(CRM_DMA2_PERIPH_CLOCK, TRUE);
/* use dma2_channel2 as spi3 transmit channel */
dma_reset(DMA2_CHANNEL2);
dma_default_para_init(&dma_init_struct);
dma_init_struct.buffer_size = BUFFER_SIZE;
dma_init_struct.direction = DMA_DIR_MEMORY_TO_PERIPHERAL;
dma_init_struct.memory_base_addr = (uint32_t)spi3_tx_buffer;
dma_init_struct.memory_data_width = DMA_MEMORY_DATA_WIDTH_BYTE;
dma_init_struct.memory_inc_enable = TRUE;
dma_init_struct.peripheral_base_addr = (uint32_t)&(SPI3->dt);
dma_init_struct.peripheral_data_width = DMA_PERIPHERAL_DATA_WIDTH_BYTE;
dma_init_struct.peripheral_inc_enable = FALSE;
dma_init_struct.priority = DMA_PRIORITY_MEDIUM;
dma_init_struct.loop_mode_enable = FALSE;
dma_init(DMA2_CHANNEL2, &dma_init_struct);
}
static void spi_config(void)
{
spi_init_type spi_init_struct;
/* spi master initialization */
crm_periph_clock_enable(CRM_SPI3_PERIPH_CLOCK, TRUE);
spi_default_para_init(&spi_init_struct);
/* single line bidirectional half duplex mode-transmitting */
spi_init_struct.transmission_mode = SPI_TRANSMIT_HALF_DUPLEX_TX;
spi_init_struct.master_slave_mode = SPI_MODE_MASTER;
spi_init_struct.mclk_freq_division = SPI_MCLK_DIV_8;
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_LOW;
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);
/* use dma transmit */
spi_i2s_dma_transmitter_enable(SPI3, TRUE);
spi_enable(SPI3, TRUE);
/* spi slave initialization */
crm_periph_clock_enable(CRM_SPI2_PERIPH_CLOCK, TRUE);
/* single line bidirectional half duplex mode-receiving */
spi_init_struct.transmission_mode = SPI_TRANSMIT_HALF_DUPLEX_RX;
spi_init_struct.master_slave_mode = SPI_MODE_SLAVE;
spi_init_struct.mclk_freq_division = SPI_MCLK_DIV_8;
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_LOW;
spi_init_struct.clock_phase = SPI_CLOCK_PHASE_2EDGE;
spi_init_struct.cs_mode_selection = SPI_CS_HARDWARE_MODE;
spi_init(SPI2, &spi_init_struct);
/* use dma receive */
spi_i2s_dma_receiver_enable(SPI2, TRUE);
spi_enable(SPI2, TRUE);
}
static void gpio_config(void)
{
gpio_init_type gpio_initstructure;
crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
crm_periph_clock_enable(CRM_IOMUX_PERIPH_CLOCK, TRUE);
gpio_pin_remap_config(SWJTAG_GMUX_010, TRUE);
gpio_pin_remap_config(SPI3_GMUX_0010, TRUE);
/* master cs pin */
gpio_initstructure.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
gpio_initstructure.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
gpio_initstructure.gpio_pull = GPIO_PULL_UP;
gpio_initstructure.gpio_mode = GPIO_MODE_OUTPUT;
gpio_initstructure.gpio_pins = GPIO_PINS_15;
gpio_init(GPIOA, &gpio_initstructure);
/* non communication time: master pull up CS pin release slave */
SPI_MASTER_CS_HIGH;
/* master sck pin */
gpio_initstructure.gpio_pull = GPIO_PULL_DOWN;
gpio_initstructure.gpio_mode = GPIO_MODE_MUX;
gpio_initstructure.gpio_pins = GPIO_PINS_3;
gpio_init(GPIOB, &gpio_initstructure);
/* master mosi pin */
gpio_initstructure.gpio_pull = GPIO_PULL_UP;
gpio_initstructure.gpio_mode = GPIO_MODE_MUX;
gpio_initstructure.gpio_pins = GPIO_PINS_5;
gpio_init(GPIOB, &gpio_initstructure);
/* slave cs pin */
gpio_initstructure.gpio_pull = GPIO_PULL_UP;
gpio_initstructure.gpio_mode = GPIO_MODE_INPUT;
gpio_initstructure.gpio_pins = GPIO_PINS_12;
gpio_init(GPIOB, &gpio_initstructure);
/* slave sck pin */
gpio_initstructure.gpio_pull = GPIO_PULL_DOWN;
gpio_initstructure.gpio_mode = GPIO_MODE_INPUT;
gpio_initstructure.gpio_pins = GPIO_PINS_13;
gpio_init(GPIOB, &gpio_initstructure);
/* slave miso pin */
gpio_initstructure.gpio_pull = GPIO_PULL_UP;
gpio_initstructure.gpio_mode = GPIO_MODE_MUX;
gpio_initstructure.gpio_pins = GPIO_PINS_14;
gpio_init(GPIOB, &gpio_initstructure);
}
int main(void)
{
system_clock_config();
at32_board_init();
at32_led_on(LED4);
gpio_config();
dma_config();
spi_config();
/* start communication: master pull down CS pin select slave */
SPI_MASTER_CS_LOW;
/* enable spi slave dma to get data */
dma_channel_enable(DMA1_CHANNEL4, TRUE);
/* enable spi master dma to fill data */
dma_channel_enable(DMA2_CHANNEL2, TRUE);
/* wait master spi data fill end */
while(dma_flag_get(DMA2_FDT2_FLAG) == RESET)
{
}
/* wait slave spi data get end */
while(dma_flag_get(DMA1_FDT4_FLAG) == RESET)
{
}
/* wait master and slave idle when communication end */
while(spi_i2s_flag_get(SPI3, SPI_I2S_BF_FLAG) != RESET);
while(spi_i2s_flag_get(SPI2, SPI_I2S_BF_FLAG) != RESET);
/* end communication: master pull up CS pin release slave */
SPI_MASTER_CS_HIGH;
/* test result:the data check */
transfer_status = buffer_compare(spi2_rx_buffer, spi3_tx_buffer, BUFFER_SIZE);
/* test result indicate:if SUCCESS ,led2 lights */
if(transfer_status == SUCCESS)
{
at32_led_on(LED2);
}
else
{
at32_led_on(LED3);
}
while(1)
{
}
}
|