comparison libs/STM32F10x_StdPeriph_Lib_V3.5.0/Project/STM32F10x_StdPeriph_Examples/SPI/Simplex_Interrupt/main.c @ 0:c59513fd84fb

Initial commit of STM32 test code.
author Daniel O'Connor <darius@dons.net.au>
date Mon, 03 Oct 2011 21:19:15 +1030
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:c59513fd84fb
1 /**
2 ******************************************************************************
3 * @file SPI/Simplex_Interrupt/main.c
4 * @author MCD Application Team
5 * @version V3.5.0
6 * @date 08-April-2011
7 * @brief Main program body
8 ******************************************************************************
9 * @attention
10 *
11 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
12 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
13 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
14 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
15 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
16 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
17 *
18 * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
19 ******************************************************************************
20 */
21
22 /* Includes ------------------------------------------------------------------*/
23 #include "stm32f10x.h"
24 #include "platform_config.h"
25
26 /** @addtogroup STM32F10x_StdPeriph_Examples
27 * @{
28 */
29
30 /** @addtogroup SPI_Simplex_Interrupt
31 * @{
32 */
33
34 /* Private typedef -----------------------------------------------------------*/
35 typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
36
37 /* Private define ------------------------------------------------------------*/
38 #define BufferSize 32
39
40 /* Private macro -------------------------------------------------------------*/
41 /* Private variables ---------------------------------------------------------*/
42 SPI_InitTypeDef SPI_InitStructure;
43 uint8_t SPI_MASTER_Buffer_Tx[BufferSize] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
44 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C,
45 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12,
46 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
47 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E,
48 0x1F, 0x20};
49 uint8_t SPI_SLAVE_Buffer_Rx[BufferSize];
50 __IO uint8_t TxIdx = 0, RxIdx = 0;
51 volatile TestStatus TransferStatus = FAILED;
52
53 /* Private functions ---------------------------------------------------------*/
54 void RCC_Configuration(void);
55 void GPIO_Configuration(void);
56 void NVIC_Configuration(void);
57 TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);
58
59 /**
60 * @brief Main program
61 * @param None
62 * @retval None
63 */
64 int main(void)
65 {
66 /*!< At this stage the microcontroller clock setting is already configured,
67 this is done through SystemInit() function which is called from startup
68 file (startup_stm32f10x_xx.s) before to branch to application main.
69 To reconfigure the default setting of SystemInit() function, refer to
70 system_stm32f10x.c file
71 */
72
73 /* System clocks configuration ---------------------------------------------*/
74 RCC_Configuration();
75
76 /* NVIC configuration ------------------------------------------------------*/
77 NVIC_Configuration();
78
79 /* GPIO configuration ------------------------------------------------------*/
80 GPIO_Configuration();
81
82 /* SPI_MASTER configuration ------------------------------------------------*/
83 SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx;
84 SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
85 SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
86 SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
87 SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
88 SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
89 SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
90 SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
91 SPI_InitStructure.SPI_CRCPolynomial = 7;
92 SPI_Init(SPI_MASTER, &SPI_InitStructure);
93
94 /* SPI_SLAVE configuration -------------------------------------------------*/
95 SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Rx;
96 SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
97 SPI_Init(SPI_SLAVE, &SPI_InitStructure);
98
99 /* Enable SPI_MASTER TXE interrupt */
100 SPI_I2S_ITConfig(SPI_MASTER, SPI_I2S_IT_TXE, ENABLE);
101 /* Enable SPI_SLAVE RXNE interrupt */
102 SPI_I2S_ITConfig(SPI_SLAVE, SPI_I2S_IT_RXNE, ENABLE);
103
104 /* Enable SPI_SLAVE */
105 SPI_Cmd(SPI_SLAVE, ENABLE);
106 /* Enable SPI_MASTER */
107 SPI_Cmd(SPI_MASTER, ENABLE);
108
109 /* Transfer procedure */
110 while (RxIdx < BufferSize)
111 {}
112
113 /* Check the correctness of written dada */
114 TransferStatus = Buffercmp(SPI_SLAVE_Buffer_Rx, SPI_MASTER_Buffer_Tx, BufferSize);
115 /* TransferStatus = PASSED, if the transmitted and received data
116 are equal */
117 /* TransferStatus = FAILED, if the transmitted and received data
118 are different */
119
120 while (1)
121 {}
122 }
123
124 /**
125 * @brief Configures the different system clocks.
126 * @param None
127 * @retval None
128 */
129 void RCC_Configuration(void)
130 {
131 /* PCLK2 = HCLK/2 */
132 RCC_PCLK2Config(RCC_HCLK_Div2);
133
134 /* Enable peripheral clocks --------------------------------------------------*/
135 #ifdef USE_STM3210C_EVAL
136 /* Enable GPIO clock for SPI_MASTER and SPI_SLAVE */
137 RCC_APB2PeriphClockCmd(SPI_MASTER_GPIO_CLK | SPI_SLAVE_GPIO_CLK |
138 RCC_APB2Periph_AFIO, ENABLE);
139
140 /* Enable SPI_MASTER Periph clock */
141 RCC_APB1PeriphClockCmd(SPI_MASTER_CLK, ENABLE);
142 #else
143 /* Enable SPI_MASTER clock and GPIO clock for SPI_MASTER and SPI_SLAVE */
144 RCC_APB2PeriphClockCmd(SPI_MASTER_GPIO_CLK | SPI_SLAVE_GPIO_CLK |
145 SPI_MASTER_CLK, ENABLE);
146 #endif
147 /* Enable SPI_SLAVE Periph clock */
148 RCC_APB1PeriphClockCmd(SPI_SLAVE_CLK, ENABLE);
149 }
150
151 /**
152 * @brief Configures the different GPIO ports.
153 * @param None
154 * @retval None
155 */
156 void GPIO_Configuration(void)
157 {
158 GPIO_InitTypeDef GPIO_InitStructure;
159
160 #ifdef USE_STM3210C_EVAL
161 /* Enable SPI3 Pins Software Remapping */
162 GPIO_PinRemapConfig(GPIO_Remap_SPI3, ENABLE);
163 #endif
164
165 /* Configure SPI_MASTER pins: SCK and MOSI ---------------------------------*/
166 /* Configure SCK and MOSI pins as Alternate Function Push Pull */
167 GPIO_InitStructure.GPIO_Pin = SPI_MASTER_PIN_SCK | SPI_MASTER_PIN_MOSI;
168 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
169 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
170 GPIO_Init(SPI_MASTER_GPIO, &GPIO_InitStructure);
171
172 /* Configure SPI_SLAVE pins: SCK and MISO ---------------------------------*/
173 /* Configure SCK and MOSI pins as Input Floating */
174 GPIO_InitStructure.GPIO_Pin = SPI_SLAVE_PIN_SCK ;
175 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
176 GPIO_Init(SPI_SLAVE_GPIO, &GPIO_InitStructure);
177 /* Configure MISO pin as Alternate Function Push Pull */
178 GPIO_InitStructure.GPIO_Pin = SPI_SLAVE_PIN_MISO;
179 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
180 GPIO_Init(SPI_SLAVE_GPIO, &GPIO_InitStructure);
181 }
182
183 /**
184 * @brief Configure the nested vectored interrupt controller.
185 * @param None
186 * @retval None
187 */
188 void NVIC_Configuration(void)
189 {
190 NVIC_InitTypeDef NVIC_InitStructure;
191
192 /* 1 bit for pre-emption priority, 3 bits for subpriority */
193 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
194
195 /* Configure and enable SPI_MASTER interrupt -------------------------------*/
196 NVIC_InitStructure.NVIC_IRQChannel = SPI_MASTER_IRQn;
197 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
198 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
199 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
200 NVIC_Init(&NVIC_InitStructure);
201
202 /* Configure and enable SPI_SLAVE interrupt --------------------------------*/
203 NVIC_InitStructure.NVIC_IRQChannel = SPI_SLAVE_IRQn;
204 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
205 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
206 NVIC_Init(&NVIC_InitStructure);
207 }
208
209 /**
210 * @brief Compares two buffers.
211 * @param pBuffer1, pBuffer2: buffers to be compared.
212 * @param BufferLength: buffer's length
213 * @retval PASSED: pBuffer1 identical to pBuffer2
214 * FAILED: pBuffer1 differs from pBuffer2
215 */
216 TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength)
217 {
218 while (BufferLength--)
219 {
220 if (*pBuffer1 != *pBuffer2)
221 {
222 return FAILED;
223 }
224
225 pBuffer1++;
226 pBuffer2++;
227 }
228
229 return PASSED;
230 }
231
232 #ifdef USE_FULL_ASSERT
233
234 /**
235 * @brief Reports the name of the source file and the source line number
236 * where the assert_param error has occurred.
237 * @param file: pointer to the source file name
238 * @param line: assert_param error line source number
239 * @retval None
240 */
241 void assert_failed(uint8_t* file, uint32_t line)
242 {
243 /* User can add his own implementation to report the file name and line number,
244 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
245
246 /* Infinite loop */
247 while (1)
248 {}
249 }
250
251 #endif
252
253 /**
254 * @}
255 */
256
257 /**
258 * @}
259 */
260
261 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/