1 | /* SPIx Communication boards Interface */
|
2 | #define SPIx SPI2
|
3 | #define SPIx_CLK RCC_APB1Periph_SPI2
|
4 | #define SPIx_CLK_INIT RCC_APB1PeriphClockCmd
|
5 | #define SPIx_IRQn SPI2_IRQn
|
6 | #define SPIx_IRQHANDLER SPI2_IRQHandler
|
7 |
|
8 | #define SPIx_SCK_PIN GPIO_Pin_1
|
9 | #define SPIx_SCK_GPIO_PORT GPIOI
|
10 | #define SPIx_SCK_GPIO_CLK RCC_AHB1Periph_GPIOI
|
11 | #define SPIx_SCK_SOURCE GPIO_PinSource1
|
12 | #define SPIx_SCK_AF GPIO_AF_SPI2
|
13 |
|
14 | #define SPIx_MISO_PIN GPIO_Pin_2
|
15 | #define SPIx_MISO_GPIO_PORT GPIOI
|
16 | #define SPIx_MISO_GPIO_CLK RCC_AHB1Periph_GPIOI
|
17 | #define SPIx_MISO_SOURCE GPIO_PinSource2
|
18 | #define SPIx_MISO_AF GPIO_AF_SPI2
|
19 |
|
20 | #define SPIx_MOSI_PIN GPIO_Pin_3
|
21 | #define SPIx_MOSI_GPIO_PORT GPIOI
|
22 | #define SPIx_MOSI_GPIO_CLK RCC_AHB1Periph_GPIOI
|
23 | #define SPIx_MOSI_SOURCE GPIO_PinSource3
|
24 | #define SPIx_MOSI_AF GPIO_AF_SPI2
|
25 |
|
26 | #define TXBUFFERSIZE (countof(TxBuffer) - 1)
|
27 | #define RXBUFFERSIZE TXBUFFERSIZE
|
28 |
|
29 |
|
30 | GPIO_InitTypeDef GPIO_InitStructure;
|
31 | SPI_InitTypeDef SPI_InitStructure;
|
32 |
|
33 | static void SPI_Config(void);
|
34 | void SPIx_IRQHANDLER(void);
|
35 | uint8_t GetVar_NbrOfData(void);
|
36 |
|
37 | uint8_t TxBuffer[];
|
38 | __IO uint8_t Counter = 0x00;
|
39 | __IO uint32_t TimeOut;
|
40 |
|
41 | __IO uint8_t Tx_Idx = 0x00;
|
42 | __IO uint8_t CmdStatus = 0x00;
|
43 | __IO uint8_t NumberOfByte = 0x01;
|
44 |
|
45 | int TimeStep100ms = 100;
|
46 | int TimeStep200ms = 200;
|
47 | int TimeStep500ms = 500;
|
48 | int NextTime = 0;
|
49 |
|
50 | void main (void)
|
51 | {
|
52 | /* SPI configuration ------------------------------------------------------*/
|
53 | SPI_Config();
|
54 |
|
55 | /* Initializes the SPI communication */
|
56 | SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
|
57 | SPI_Init(SPIx, &SPI_InitStructure);
|
58 |
|
59 | /* The Data transfer is performed in the SPI interrupt routine */
|
60 | /* Enable the SPI peripheral */
|
61 | SPI_Cmd(SPIx, ENABLE);
|
62 |
|
63 | while (1)
|
64 | {
|
65 | STM_EVAL_LEDToggle(LED2);
|
66 |
|
67 | /* Enable the Tx buffer empty interrupt */
|
68 | SPI_I2S_ITConfig(SPIx, SPI_I2S_IT_TXE, ENABLE);
|
69 |
|
70 | /* Waiting the end of Data transfer */
|
71 | while (Tx_Idx < GetVar_NbrOfData())
|
72 | {
|
73 | }
|
74 | delay(1000);
|
75 | }
|
76 | }
|
77 |
|
78 | void SPIx_IRQHANDLER(void)
|
79 | {
|
80 | /* SPI in Master Tramitter mode--------------------------------------- */
|
81 | if (SPI_I2S_GetITStatus(SPIx, SPI_I2S_IT_TXE) == SET)
|
82 | {
|
83 | if (Tx_Idx < GetVar_NbrOfData())
|
84 | {
|
85 | /* Send Transaction data */
|
86 | SPI_I2S_SendData(SPIx, TxBuffer[Tx_Idx++]);
|
87 | }
|
88 | else
|
89 | {
|
90 | /* Disable the Tx buffer empty interrupt */
|
91 | SPI_I2S_ITConfig(SPIx, SPI_I2S_IT_TXE, DISABLE);
|
92 | }
|
93 | }
|
94 | }
|
95 |
|
96 | uint8_t GetVar_NbrOfData(void)
|
97 | {
|
98 | return NumberOfByte;
|
99 | }
|
100 |
|
101 | static void SPI_Config(void)
|
102 | {
|
103 | GPIO_InitTypeDef GPIO_InitStructure;
|
104 | NVIC_InitTypeDef NVIC_InitStructure;
|
105 |
|
106 | /* Enable the SPI clock */
|
107 | SPIx_CLK_INIT(SPIx_CLK, ENABLE);
|
108 |
|
109 | /* Enable GPIO clocks */
|
110 | RCC_AHB1PeriphClockCmd(SPIx_SCK_GPIO_CLK | SPIx_MISO_GPIO_CLK | SPIx_MOSI_GPIO_CLK, ENABLE);
|
111 |
|
112 | /* Connect SPI pins to AF5 */
|
113 | GPIO_PinAFConfig(SPIx_SCK_GPIO_PORT, SPIx_SCK_SOURCE, SPIx_SCK_AF);
|
114 | GPIO_PinAFConfig(SPIx_MOSI_GPIO_PORT, SPIx_MOSI_SOURCE, SPIx_MOSI_AF);
|
115 |
|
116 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
|
117 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
118 | GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
|
119 | GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
|
120 |
|
121 | /* SPI SCK pin configuration */
|
122 | GPIO_InitStructure.GPIO_Pin = SPIx_SCK_PIN;
|
123 | GPIO_Init(SPIx_SCK_GPIO_PORT, &GPIO_InitStructure);
|
124 |
|
125 | /* SPI MOSI pin configuration */
|
126 | GPIO_InitStructure.GPIO_Pin = SPIx_MOSI_PIN;
|
127 | GPIO_Init(SPIx_MOSI_GPIO_PORT, &GPIO_InitStructure);
|
128 |
|
129 | /* SPI configuration -------------------------------------------------------*/
|
130 | SPI_I2S_DeInit(SPIx);
|
131 | SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
|
132 | SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
|
133 | SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
|
134 | SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
|
135 | SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
|
136 | SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
|
137 | SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
|
138 | SPI_InitStructure.SPI_CRCPolynomial = 7;
|
139 |
|
140 | /* Configure the Priority Group to 1 bit */
|
141 | NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
|
142 |
|
143 | /* Configure the SPI interrupt priority */
|
144 | NVIC_InitStructure.NVIC_IRQChannel = SPIx_IRQn;
|
145 | NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
|
146 | NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
|
147 | NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
148 | NVIC_Init(&NVIC_InitStructure);
|
149 | }
|