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