comparison libs/STM32F10x_StdPeriph_Lib_V3.5.0/Project/STM32F10x_StdPeriph_Examples/USART/Polling/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/Polling/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_Polling
31 * @{
32 */
33
34 /* Private typedef -----------------------------------------------------------*/
35 typedef enum { FAILED = 0, PASSED = !FAILED} TestStatus;
36
37 /* Private define ------------------------------------------------------------*/
38 #define TxBufferSize (countof(TxBuffer))
39
40 /* Private macro -------------------------------------------------------------*/
41 #define countof(a) (sizeof(a) / sizeof(*(a)))
42
43 /* Private variables ---------------------------------------------------------*/
44 USART_InitTypeDef USART_InitStructure;
45 uint8_t TxBuffer[] = "Buffer Send from USARTy to USARTz using Flags";
46 uint8_t RxBuffer[TxBufferSize];
47 __IO uint8_t TxCounter = 0, RxCounter = 0;
48 volatile TestStatus TransferStatus = FAILED;
49
50 /* Private function prototypes -----------------------------------------------*/
51 void RCC_Configuration(void);
52 void GPIO_Configuration(void);
53 TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);
54 __IO uint8_t index = 0;
55
56 /* Private functions ---------------------------------------------------------*/
57
58 /**
59 * @brief Main program
60 * @param None
61 * @retval None
62 */
63 int main(void)
64 {
65 /*!< At this stage the microcontroller clock setting is already configured,
66 this is done through SystemInit() function which is called from startup
67 file (startup_stm32f10x_xx.s) before to branch to application main.
68 To reconfigure the default setting of SystemInit() function, refer to
69 system_stm32f10x.c file
70 */
71
72 /* System Clocks Configuration */
73 RCC_Configuration();
74
75 /* Configure the GPIO ports */
76 GPIO_Configuration();
77
78 /* USARTy and USARTz configuration ------------------------------------------------------*/
79 /* USARTy and USARTz configured as follow:
80 - BaudRate = 230400 baud
81 - Word Length = 8 Bits
82 - One Stop Bit
83 - Even parity
84 - Hardware flow control disabled (RTS and CTS signals)
85 - Receive and transmit enabled
86 */
87 USART_InitStructure.USART_BaudRate = 230400;
88 USART_InitStructure.USART_WordLength = USART_WordLength_8b;
89 USART_InitStructure.USART_StopBits = USART_StopBits_1;
90 USART_InitStructure.USART_Parity = USART_Parity_Even;
91 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
92 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
93
94 /* Configure USARTy */
95 USART_Init(USARTy, &USART_InitStructure);
96 /* Configure USARTz */
97 USART_Init(USARTz, &USART_InitStructure);
98
99 /* Enable the USARTy */
100 USART_Cmd(USARTy, ENABLE);
101
102 /* Enable the USARTz */
103 USART_Cmd(USARTz, ENABLE);
104
105 while(TxCounter < TxBufferSize)
106 {
107 /* Send one byte from USARTy to USARTz */
108 USART_SendData(USARTy, TxBuffer[TxCounter++]);
109
110 /* Loop until USARTy DR register is empty */
111 while(USART_GetFlagStatus(USARTy, USART_FLAG_TXE) == RESET)
112 {
113 }
114
115 /* Loop until the USARTz Receive Data Register is not empty */
116 while(USART_GetFlagStatus(USARTz, USART_FLAG_RXNE) == RESET)
117 {
118 }
119
120 /* Store the received byte in RxBuffer */
121 RxBuffer[RxCounter++] = (USART_ReceiveData(USARTz) & 0x7F);
122
123 }
124 /* Check the received data with the send ones */
125 TransferStatus = Buffercmp(TxBuffer, RxBuffer, TxBufferSize);
126 /* TransferStatus = PASSED, if the data transmitted from USARTy and
127 received by USARTz are the same */
128 /* TransferStatus = FAILED, if the data transmitted from USARTy and
129 received by USARTz are different */
130
131 while (1)
132 {
133 }
134 }
135
136 /**
137 * @brief Configures the different system clocks.
138 * @param None
139 * @retval None
140 */
141 void RCC_Configuration(void)
142 {
143 /* Enable GPIO clock */
144 RCC_APB2PeriphClockCmd(USARTy_GPIO_CLK | USARTz_GPIO_CLK | RCC_APB2Periph_AFIO, ENABLE);
145
146 #ifndef USE_STM3210C_EVAL
147 /* Enable USARTy Clock */
148 RCC_APB2PeriphClockCmd(USARTy_CLK, ENABLE);
149 #else
150 /* Enable USARTy Clock */
151 RCC_APB1PeriphClockCmd(USARTy_CLK, ENABLE);
152 #endif
153 /* Enable USARTz Clock */
154 RCC_APB1PeriphClockCmd(USARTz_CLK, ENABLE);
155 }
156
157 /**
158 * @brief Configures the different GPIO ports.
159 * @param None
160 * @retval None
161 */
162 void GPIO_Configuration(void)
163 {
164 GPIO_InitTypeDef GPIO_InitStructure;
165
166 #ifdef USE_STM3210C_EVAL
167 /* Enable the USART3 Pins Software Remapping */
168 GPIO_PinRemapConfig(GPIO_PartialRemap_USART3, ENABLE);
169
170 /* Enable the USART2 Pins Software Remapping */
171 GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
172 #elif defined(USE_STM3210B_EVAL) || defined(USE_STM32100B_EVAL)
173 /* Enable the USART2 Pins Software Remapping */
174 GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
175 #endif
176
177 /* Configure USARTy Rx as input floating */
178 GPIO_InitStructure.GPIO_Pin = USARTy_RxPin;
179 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
180 GPIO_Init(USARTy_GPIO, &GPIO_InitStructure);
181
182 /* Configure USARTz Rx as input floating */
183 GPIO_InitStructure.GPIO_Pin = USARTz_RxPin;
184 GPIO_Init(USARTz_GPIO, &GPIO_InitStructure);
185
186 /* Configure USARTy Tx as alternate function push-pull */
187 GPIO_InitStructure.GPIO_Pin = USARTy_TxPin;
188 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
189 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
190 GPIO_Init(USARTy_GPIO, &GPIO_InitStructure);
191
192 /* Configure USARTz Tx as alternate function push-pull */
193 GPIO_InitStructure.GPIO_Pin = USARTz_TxPin;
194 GPIO_Init(USARTz_GPIO, &GPIO_InitStructure);
195 }
196
197 /**
198 * @brief Compares two buffers.
199 * @param pBuffer1, pBuffer2: buffers to be compared.
200 * @param BufferLength: buffer's length
201 * @retval PASSED: pBuffer1 identical to pBuffer2
202 * FAILED: pBuffer1 differs from pBuffer2
203 */
204 TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength)
205 {
206 while(BufferLength--)
207 {
208 if(*pBuffer1 != *pBuffer2)
209 {
210 return FAILED;
211 }
212
213 pBuffer1++;
214 pBuffer2++;
215 }
216
217 return PASSED;
218 }
219
220 #ifdef USE_FULL_ASSERT
221
222 /**
223 * @brief Reports the name of the source file and the source line number
224 * where the assert_param error has occurred.
225 * @param file: pointer to the source file name
226 * @param line: assert_param error line source number
227 * @retval None
228 */
229 void assert_failed(uint8_t* file, uint32_t line)
230 {
231 /* User can add his own implementation to report the file name and line number,
232 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
233
234 /* Infinite loop */
235 while (1)
236 {
237 }
238 }
239
240 #endif
241
242 /**
243 * @}
244 */
245
246 /**
247 * @}
248 */
249
250 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/