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