comparison libs/STM32F10x_StdPeriph_Lib_V3.5.0/Project/STM32F10x_StdPeriph_Examples/USART/Synchronous/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 USART/Synchronous/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 USART_Synchronous
31 * @{
32 */
33
34 /* Private typedef -----------------------------------------------------------*/
35 typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
36
37 /* Private define ------------------------------------------------------------*/
38 #define TxBufferSize1 (countof(TxBuffer1) - 1)
39 #define TxBufferSize2 (countof(TxBuffer2) - 1)
40 #define DYMMY_BYTE 0x00
41
42 /* Private macro -------------------------------------------------------------*/
43 #define countof(a) (sizeof(a) / sizeof(*(a)))
44
45 /* Private variables ---------------------------------------------------------*/
46 USART_InitTypeDef USART_InitStructure;
47 USART_ClockInitTypeDef USART_ClockInitStructure;
48
49 uint8_t TxBuffer1[] = "USART Synchronous Example: USARTy -> SPIy using TXE and RXNE Flags";
50 uint8_t TxBuffer2[] = "USART Synchronous Example: SPIy -> USARTy using TXE and RXNE Flags";
51 uint8_t RxBuffer1[TxBufferSize2];
52 uint8_t RxBuffer2[TxBufferSize1];
53 __IO uint8_t NbrOfDataToRead1 = TxBufferSize2;
54 __IO uint8_t NbrOfDataToRead2 = TxBufferSize1;
55 __IO uint8_t TxCounter1 = 0, RxCounter1 = 0;
56 __IO uint8_t TxCounter2 = 0, RxCounter2 = 0;
57 volatile TestStatus TransferStatus1 = FAILED, TransferStatus2 = FAILED;
58
59 /* Private function prototypes -----------------------------------------------*/
60 void RCC_Configuration(void);
61 void GPIO_Configuration(void);
62 void SPI_Configuration(void);
63 TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);
64
65 /* Private functions ---------------------------------------------------------*/
66
67 /**
68 * @brief Main program
69 * @param None
70 * @retval None
71 */
72 int main(void)
73 {
74 /*!< At this stage the microcontroller clock setting is already configured,
75 this is done through SystemInit() function which is called from startup
76 file (startup_stm32f10x_xx.s) before to branch to application main.
77 To reconfigure the default setting of SystemInit() function, refer to
78 system_stm32f10x.c file
79 */
80
81 /* System Clocks Configuration */
82 RCC_Configuration();
83
84 /* Configure the GPIO ports */
85 GPIO_Configuration();
86
87 /* Configure the SPI */
88 SPI_Configuration();
89
90 /* USARTy configuration ------------------------------------------------------*/
91 /* USARTy configured as follow:
92 - BaudRate = 115200 baud
93 - Word Length = 8 Bits
94 - One Stop Bit
95 - No parity
96 - Hardware flow control disabled (RTS and CTS signals)
97 - Receive and transmit enabled
98 - USART Clock Enabled
99 - USART CPOL: Clock is active High
100 - USART CPHA: Data is captured on the second edge
101 - USART LastBit: The clock pulse of the last data bit is output to
102 the SCLK pin
103 */
104 USART_ClockInitStructure.USART_Clock = USART_Clock_Enable;
105 USART_ClockInitStructure.USART_CPOL = USART_CPOL_High;
106 USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
107 USART_ClockInitStructure.USART_LastBit = USART_LastBit_Enable;
108 USART_ClockInit(USARTy, &USART_ClockInitStructure);
109
110 USART_InitStructure.USART_BaudRate = 115200;
111 USART_InitStructure.USART_WordLength = USART_WordLength_8b;
112 USART_InitStructure.USART_StopBits = USART_StopBits_1;
113 USART_InitStructure.USART_Parity = USART_Parity_No ;
114 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
115 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
116 USART_Init(USARTy, &USART_InitStructure);
117
118 /* Configure the USARTy */
119 USART_Init(USARTy, &USART_InitStructure);
120
121 /* Enable the USARTy */
122 USART_Cmd(USARTy, ENABLE);
123
124 while(NbrOfDataToRead2--)
125 {
126 /* Write one byte in the USARTy Transmit Data Register */
127 USART_SendData(USARTy, TxBuffer1[TxCounter1++]);
128 /* Wait until end of transmit */
129 while(USART_GetFlagStatus(USARTy, USART_FLAG_TC) == RESET)
130 {
131 }
132 /* Wait the byte is entirely received by SPIy */
133 while(SPI_I2S_GetFlagStatus(SPIy, SPI_I2S_FLAG_RXNE) == RESET)
134 {
135 }
136 /* Store the received byte in the RxBuffer2 */
137 RxBuffer2[RxCounter2++] = SPI_I2S_ReceiveData(SPIy);
138 }
139
140 /* Clear the USARTy Data Register */
141 USART_ReceiveData(USARTy);
142
143 while(NbrOfDataToRead1--)
144 {
145 /* Wait until end of transmit */
146 while(SPI_I2S_GetFlagStatus(SPIy, SPI_I2S_FLAG_TXE)== RESET)
147 {
148 }
149 /* Write one byte in the SPIy Transmit Data Register */
150 SPI_I2S_SendData(SPIy, TxBuffer2[TxCounter2++]);
151
152 /* Send a Dummy byte to generate clock to slave */
153 USART_SendData(USARTy, DYMMY_BYTE);
154 /* Wait until end of transmit */
155 while(USART_GetFlagStatus(USARTy, USART_FLAG_TC) == RESET)
156 {
157 }
158 /* Wait the byte is entirely received by USARTy */
159 while(USART_GetFlagStatus(USARTy, USART_FLAG_RXNE) == RESET)
160 {
161 }
162 /* Store the received byte in the RxBuffer1 */
163 RxBuffer1[RxCounter1++] = USART_ReceiveData(USARTy);
164 }
165
166 /* Check the received data with the send ones */
167 TransferStatus1 = Buffercmp(TxBuffer1, RxBuffer2, TxBufferSize1);
168 /* TransferStatus = PASSED, if the data transmitted from USARTy and
169 received by SPIy are the same */
170 /* TransferStatus = FAILED, if the data transmitted from USARTy and
171 received by SPIy are different */
172 TransferStatus2 = Buffercmp(TxBuffer2, RxBuffer1, TxBufferSize2);
173 /* TransferStatus = PASSED, if the data transmitted from SPIy and
174 received by USARTy are the same */
175 /* TransferStatus = FAILED, if the data transmitted from SPIy and
176 received by USARTy are different */
177
178 while (1)
179 {
180 }
181 }
182
183 /**
184 * @brief Configures the different system clocks.
185 * @param None
186 * @retval None
187 */
188 void RCC_Configuration(void)
189 {
190 /* Enable GPIO clock */
191 RCC_APB2PeriphClockCmd(USARTy_GPIO_CLK | SPIy_GPIO_CLK | RCC_APB2Periph_AFIO, ENABLE);
192
193 /* Enable USARTy Clock */
194 RCC_APB2PeriphClockCmd(USARTy_CLK, ENABLE);
195 /* Enable SPIy Clock */
196 RCC_APB2PeriphClockCmd(SPIy_CLK, ENABLE);
197 }
198
199 /**
200 * @brief Configures the different GPIO ports.
201 * @param None
202 * @retval None
203 */
204 void GPIO_Configuration(void)
205 {
206 GPIO_InitTypeDef GPIO_InitStructure;
207
208 /* Configure USARTy TX and USARTy CK pins as alternate function push-pull */
209 GPIO_InitStructure.GPIO_Pin = USARTy_TxPin | USARTy_ClkPin;
210 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
211 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
212 GPIO_Init(USARTy_GPIO, &GPIO_InitStructure);
213
214 /* Configure SPI1 pins: SCK, MISO and MOSI */
215 GPIO_InitStructure.GPIO_Pin = SPIy_SCKPin | SPIy_MISOPin | SPIy_MOSIPin;
216 GPIO_Init(SPIy_GPIO, &GPIO_InitStructure);
217
218 /* Configure USARTy RX as input floating */
219 GPIO_InitStructure.GPIO_Pin = USARTy_RxPin;
220 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
221 GPIO_Init(USARTy_GPIO, &GPIO_InitStructure);
222 }
223
224 /**
225 * @brief Configures the SPI.
226 * @param None
227 * @retval None
228 */
229 void SPI_Configuration(void)
230 {
231 SPI_InitTypeDef SPI_InitStructure;
232
233 SPI_StructInit(&SPI_InitStructure);
234
235 SPI_I2S_DeInit(SPIy);
236
237 /* SPIy Config */
238 SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
239 SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
240 SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
241 SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
242 SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
243 SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
244 SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB;
245
246 /* Configure SPIy */
247 SPI_Init(SPIy, &SPI_InitStructure);
248
249 /* SPIy enable */
250 SPI_Cmd(SPIy, ENABLE);
251 }
252
253 /**
254 * @brief Compares two buffers.
255 * @param pBuffer1, pBuffer2: buffers to be compared.
256 * @param BufferLength: buffer's length
257 * @retval PASSED: pBuffer1 identical to pBuffer2
258 * FAILED: pBuffer1 differs from pBuffer2
259 */
260 TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength)
261 {
262 while(BufferLength--)
263 {
264 if(*pBuffer1 != *pBuffer2)
265 {
266 return FAILED;
267 }
268
269 pBuffer1++;
270 pBuffer2++;
271 }
272
273 return PASSED;
274 }
275
276 #ifdef USE_FULL_ASSERT
277
278 /**
279 * @brief Reports the name of the source file and the source line number
280 * where the assert_param error has occurred.
281 * @param file: pointer to the source file name
282 * @param line: assert_param error line source number
283 * @retval None
284 */
285 void assert_failed(uint8_t* file, uint32_t line)
286 {
287 /* User can add his own implementation to report the file name and line number,
288 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
289
290 /* Infinite loop */
291 while (1)
292 {
293 }
294 }
295
296 #endif
297
298 /**
299 * @}
300 */
301
302 /**
303 * @}
304 */
305
306 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/