comparison libs/STM32F10x_StdPeriph_Lib_V3.5.0/Project/STM32F10x_StdPeriph_Examples/USART/DMA_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/DMA_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_DMA_Polling
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 DMA Polling: USARTy -> USARTz using DMA";
47 uint8_t TxBuffer2[] = "USART DMA Polling: USARTz -> USARTy using DMA";
48 uint8_t RxBuffer1[TxBufferSize2];
49 uint8_t RxBuffer2[TxBufferSize1];
50 volatile TestStatus TransferStatus1 = FAILED;
51 volatile TestStatus TransferStatus2 = FAILED;
52
53 /* Private function prototypes -----------------------------------------------*/
54 void RCC_Configuration(void);
55 void GPIO_Configuration(void);
56 void DMA_Configuration(void);
57 TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);
58
59 /* Private functions ---------------------------------------------------------*/
60
61 /**
62 * @brief Main program
63 * @param None
64 * @retval None
65 */
66 int main(void)
67 {
68 /*!< At this stage the microcontroller clock setting is already configured,
69 this is done through SystemInit() function which is called from startup
70 file (startup_stm32f10x_xx.s) before to branch to application main.
71 To reconfigure the default setting of SystemInit() function, refer to
72 system_stm32f10x.c file
73 */
74
75 /* System Clocks Configuration */
76 RCC_Configuration();
77
78 /* Configure the GPIO ports */
79 GPIO_Configuration();
80
81 /* Configure the DMA */
82 DMA_Configuration();
83
84 /* USARTy and USARTz configuration ------------------------------------------------------*/
85 /* USARTy and USARTz configured as follow:
86 - BaudRate = 230400 baud
87 - Word Length = 8 Bits
88 - One Stop Bit
89 - No parity
90 - Hardware flow control disabled (RTS and CTS signals)
91 - Receive and transmit enabled
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 USARTy DMA Rx and TX request */
106 USART_DMACmd(USARTy, USART_DMAReq_Rx | USART_DMAReq_Tx, ENABLE);
107 /* Enable USARTz DMA Rx and TX request */
108 USART_DMACmd(USARTz, USART_DMAReq_Rx | USART_DMAReq_Tx, ENABLE);
109
110 /* Enable USARTy TX DMA1 Channel */
111 DMA_Cmd(USARTy_Tx_DMA_Channel, ENABLE);
112 /* Enable USARTy RX DMA1 Channel */
113 DMA_Cmd(USARTy_Rx_DMA_Channel, ENABLE);
114
115 /* Enable USARTz TX DMA1 Channel */
116 DMA_Cmd(USARTz_Tx_DMA_Channel, ENABLE);
117 /* Enable USARTz RX DMA1 Channel */
118 DMA_Cmd(USARTz_Rx_DMA_Channel, ENABLE);
119
120 /* Enable the USARTy */
121 USART_Cmd(USARTy, ENABLE);
122 /* Enable the USARTz */
123 USART_Cmd(USARTz, ENABLE);
124
125 /* Wait until USARTy TX DMA1 Channel Transfer Complete */
126 while (DMA_GetFlagStatus(USARTy_Tx_DMA_FLAG) == RESET)
127 {
128 }
129 /* Wait until USARTy RX DMA1 Channel Transfer Complete */
130 while (DMA_GetFlagStatus(USARTy_Rx_DMA_FLAG) == RESET)
131 {
132 }
133
134 /* Wait until USARTz TX DMA1 Channel Transfer Complete */
135 while (DMA_GetFlagStatus(USARTz_Tx_DMA_FLAG) == RESET)
136 {
137 }
138 /* Wait until USARTz RX DMA1 Channel Transfer Complete */
139 while (DMA_GetFlagStatus(USARTz_Rx_DMA_FLAG) == RESET)
140 {
141 }
142
143 /* Check the received data with the send ones */
144 TransferStatus1 = Buffercmp(TxBuffer2, RxBuffer1, TxBufferSize2);
145 /* TransferStatus1 = PASSED, if the data transmitted from USARTz and
146 received by USARTy are the same */
147 /* TransferStatus1 = FAILED, if the data transmitted from USARTz and
148 received by USARTy are different */
149 TransferStatus2 = Buffercmp(TxBuffer1, RxBuffer2, TxBufferSize1);
150 /* TransferStatus2 = PASSED, if the data transmitted from USARTy and
151 received by USARTz are the same */
152 /* TransferStatus2 = FAILED, if the data transmitted from USARTy and
153 received by USARTz are different */
154
155 while (1)
156 {
157 }
158 }
159
160 /**
161 * @brief Configures the different system clocks.
162 * @param None
163 * @retval None
164 */
165 void RCC_Configuration(void)
166 {
167 /* DMA clock enable */
168 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
169
170 /* Enable GPIO clock */
171 RCC_APB2PeriphClockCmd(USARTy_GPIO_CLK | USARTz_GPIO_CLK | RCC_APB2Periph_AFIO, ENABLE);
172
173 #ifndef USE_STM3210C_EVAL
174 /* Enable USARTy Clock */
175 RCC_APB2PeriphClockCmd(USARTy_CLK, ENABLE);
176 #else
177 /* Enable USARTy Clock */
178 RCC_APB1PeriphClockCmd(USARTy_CLK, ENABLE);
179 #endif
180 /* Enable USARTz Clock */
181 RCC_APB1PeriphClockCmd(USARTz_CLK, ENABLE);
182 }
183
184 /**
185 * @brief Configures the different GPIO ports.
186 * @param None
187 * @retval None
188 */
189 void GPIO_Configuration(void)
190 {
191 GPIO_InitTypeDef GPIO_InitStructure;
192
193 #ifdef USE_STM3210C_EVAL
194 /* Enable the USART3 Pins Software Remapping */
195 GPIO_PinRemapConfig(GPIO_PartialRemap_USART3, ENABLE);
196
197 /* Enable the USART2 Pins Software Remapping */
198 GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
199 #elif defined(USE_STM3210B_EVAL) || defined(USE_STM32100B_EVAL)
200 /* Enable the USART2 Pins Software Remapping */
201 GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
202 #endif
203
204 /* Configure USARTy Rx as input floating */
205 GPIO_InitStructure.GPIO_Pin = USARTy_RxPin;
206 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
207 GPIO_Init(USARTy_GPIO, &GPIO_InitStructure);
208
209 /* Configure USARTz Rx as input floating */
210 GPIO_InitStructure.GPIO_Pin = USARTz_RxPin;
211 GPIO_Init(USARTz_GPIO, &GPIO_InitStructure);
212
213 /* Configure USARTy Tx as alternate function push-pull */
214 GPIO_InitStructure.GPIO_Pin = USARTy_TxPin;
215 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
216 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
217 GPIO_Init(USARTy_GPIO, &GPIO_InitStructure);
218
219 /* Configure USARTz Tx as alternate function push-pull */
220 GPIO_InitStructure.GPIO_Pin = USARTz_TxPin;
221 GPIO_Init(USARTz_GPIO, &GPIO_InitStructure);
222 }
223
224 /**
225 * @brief Configures the DMA.
226 * @param None
227 * @retval None
228 */
229 void DMA_Configuration(void)
230 {
231 DMA_InitTypeDef DMA_InitStructure;
232
233 /* USARTy TX DMA1 Channel (triggered by USARTy Tx event) Config */
234 DMA_DeInit(USARTy_Tx_DMA_Channel);
235 DMA_InitStructure.DMA_PeripheralBaseAddr = USARTy_DR_Base;
236 DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)TxBuffer1;
237 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
238 DMA_InitStructure.DMA_BufferSize = TxBufferSize1;
239 DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
240 DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
241 DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
242 DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
243 DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
244 DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
245 DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
246 DMA_Init(USARTy_Tx_DMA_Channel, &DMA_InitStructure);
247
248 /* USARTy RX DMA1 Channel (triggered by USARTy Rx event) Config */
249 DMA_DeInit(USARTy_Rx_DMA_Channel);
250 DMA_InitStructure.DMA_PeripheralBaseAddr = USARTy_DR_Base;
251 DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)RxBuffer1;
252 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
253 DMA_InitStructure.DMA_BufferSize = TxBufferSize2;
254 DMA_Init(USARTy_Rx_DMA_Channel, &DMA_InitStructure);
255
256 /* USARTz TX DMA1 Channel (triggered by USARTz Tx event) Config */
257 DMA_DeInit(USARTz_Tx_DMA_Channel);
258 DMA_InitStructure.DMA_PeripheralBaseAddr = USARTz_DR_Base;
259 DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)TxBuffer2;
260 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
261 DMA_InitStructure.DMA_BufferSize = TxBufferSize2;
262 DMA_Init(USARTz_Tx_DMA_Channel, &DMA_InitStructure);
263
264 /* USARTz RX DMA1 Channel (triggered by USARTz Rx event) Config */
265 DMA_DeInit(USARTz_Rx_DMA_Channel);
266 DMA_InitStructure.DMA_PeripheralBaseAddr = USARTz_DR_Base;
267 DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)RxBuffer2;
268 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
269 DMA_InitStructure.DMA_BufferSize = TxBufferSize1;
270 DMA_Init(USARTz_Rx_DMA_Channel, &DMA_InitStructure);
271 }
272
273 /**
274 * @brief Compares two buffers.
275 * @param pBuffer1, pBuffer2: buffers to be compared.
276 * @param BufferLength: buffer's length
277 * @retval PASSED: pBuffer1 identical to pBuffer2
278 * FAILED: pBuffer1 differs from pBuffer2
279 */
280 TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength)
281 {
282 while(BufferLength--)
283 {
284 if(*pBuffer1 != *pBuffer2)
285 {
286 return FAILED;
287 }
288
289 pBuffer1++;
290 pBuffer2++;
291 }
292
293 return PASSED;
294 }
295
296 #ifdef USE_FULL_ASSERT
297
298 /**
299 * @brief Reports the name of the source file and the source line number
300 * where the assert_param error has occurred.
301 * @param file: pointer to the source file name
302 * @param line: assert_param error line source number
303 * @retval None
304 */
305 void assert_failed(uint8_t* file, uint32_t line)
306 {
307 /* User can add his own implementation to report the file name and line number,
308 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
309
310 /* Infinite loop */
311 while (1)
312 {
313 }
314 }
315
316 #endif
317
318 /**
319 * @}
320 */
321
322 /**
323 * @}
324 */
325
326 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/