CMSIS2000  0.0.7
 Указатель Структуры данных Файлы Функции Переменные Определения типов Перечисления Элементы перечислений Макросы Группы Страницы
main.c
См. документацию.
1 /**
2  ******************************************************************************
3  * @file USART/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 #ifdef USE_FULL_ASSERT
26 void assert_failed(uint8_t* file, uint32_t line);
27 /* !!!! to remove this just put stm32f10x_conf.h in some pheripheral library files*/
28 void assert_param(int some_param) {
29  if (some_param == 0)
30  assert_failed("NO stm32f10x_conf in some file",0xffffffff);
31 }
32 #endif
33 #include "stm32f10x_conf.h"
34 
35 /** @addtogroup STM32F10x_StdPeriph_Examples
36  * @{
37  */
38 
39 /** @addtogroup USART_Interrupt
40  * @{
41  */
42 
43 /* Private typedef -----------------------------------------------------------*/
44 typedef enum { FAILED = 0, PASSED = !FAILED} TestStatus;
45 
46 /* Private define ------------------------------------------------------------*/
47 #define TxBufferSize1 (countof(TxBuffer1) - 1)
48 #define TxBufferSize2 (countof(TxBuffer2) - 1)
49 #define RxBufferSize1 TxBufferSize2
50 #define RxBufferSize2 TxBufferSize1
51 
52 /* Private macro -------------------------------------------------------------*/
53 #define countof(a) (sizeof(a) / sizeof(*(a)))
54 
55 /* Private variables ---------------------------------------------------------*/
56 USART_InitTypeDef USART_InitStructure;
57 uint8_t TxBuffer1[] = "USART Interrupt Example: USARTy -> USARTz using Interrupt";
58 uint8_t TxBuffer2[] = "USART Interrupt Example: USARTz -> USARTy using Interrupt";
71 
72 /* Private function prototypes -----------------------------------------------*/
73 void RCC_Configuration(void);
74 void GPIO_Configuration(void);
75 void NVIC_Configuration(void);
76 TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);
77 
78 /* Private functions ---------------------------------------------------------*/
79 
80 /**
81  * @brief Main program
82  * @param None
83  * @retval None
84  */
85 int main(void)
86 {
87  /*!< At this stage the microcontroller clock setting is already configured,
88  this is done through SystemInit() function which is called from startup
89  file (startup_stm32f10x_xx.s) before to branch to application main.
90  To reconfigure the default setting of SystemInit() function, refer to
91  system_stm32f10x.c file
92  */
93 
94  /* System Clocks Configuration */
96 
97  /* NVIC configuration */
99 
100  /* Configure the GPIO ports */
102 
103 /* USARTy and USARTz configuration ------------------------------------------------------*/
104  /* USARTy and USARTz configured as follow:
105  - BaudRate = 9600 baud
106  - Word Length = 8 Bits
107  - One Stop Bit
108  - No parity
109  - Hardware flow control disabled (RTS and CTS signals)
110  - Receive and transmit enabled
111  */
112  USART_InitStructure.USART_BaudRate = 9600;
113  USART_InitStructure.USART_WordLength = USART_WordLength_8b;
114  USART_InitStructure.USART_StopBits = USART_StopBits_1;
115  USART_InitStructure.USART_Parity = USART_Parity_No;
116  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
117  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
118 
119  /* Configure USARTy */
120  USART_Init(USARTy, &USART_InitStructure);
121  /* Configure USARTz */
122  USART_Init(USARTz, &USART_InitStructure);
123 
124  /* Enable USARTy Receive and Transmit interrupts */
125  USART_ITConfig(USARTy, USART_IT_RXNE, ENABLE);
126  USART_ITConfig(USARTy, USART_IT_TXE, ENABLE);
127 
128  /* Enable USARTz Receive and Transmit interrupts */
129  USART_ITConfig(USARTz, USART_IT_RXNE, ENABLE);
130  USART_ITConfig(USARTz, USART_IT_TXE, ENABLE);
131 
132  /* Enable the USARTy */
133  USART_Cmd(USARTy, ENABLE);
134  /* Enable the USARTz */
135  USART_Cmd(USARTz, ENABLE);
136 
137  /* Wait until end of transmission from USARTy to USARTz */
138  while(RxCounter2 < RxBufferSize2)
139  {
140  }
141 
142  /* Wait until end of transmission from USARTz to USARTy */
143  while(RxCounter1 < RxBufferSize1)
144  {
145  }
146 
147  /* Check the received data with the send ones */
148  TransferStatus1 = Buffercmp(TxBuffer2, RxBuffer1, RxBufferSize1);
149  /* TransferStatus1 = PASSED, if the data transmitted from USARTz and
150  received by USARTy are the same */
151  /* TransferStatus1 = FAILED, if the data transmitted from USARTz and
152  received by USARTy are different */
153  TransferStatus2 = Buffercmp(TxBuffer1, RxBuffer2, RxBufferSize2);
154  /* TransferStatus2 = PASSED, if the data transmitted from USARTy and
155  received by USARTz are the same */
156  /* TransferStatus2 = FAILED, if the data transmitted from USARTy and
157  received by USARTz are different */
158 
159  while (1)
160  {
161  }
162 }
163 
164 /**
165  * @brief Configures the different system clocks.
166  * @param None
167  * @retval None
168  */
170 {
171  /* Enable GPIO clock */
172  RCC_APB2PeriphClockCmd(USARTy_GPIO_CLK | USARTz_GPIO_CLK | RCC_APB2Periph_AFIO, ENABLE);
173 
174 #ifndef USE_STM3210C_EVAL
175  /* Enable USARTy Clock */
176  RCC_APB2PeriphClockCmd(USARTy_CLK, ENABLE);
177 #else
178  /* Enable USARTy Clock */
179  RCC_APB1PeriphClockCmd(USARTy_CLK, ENABLE);
180 #endif
181  /* Enable USARTz Clock */
182  RCC_APB1PeriphClockCmd(USARTz_CLK, ENABLE);
183 }
184 
185 /**
186  * @brief Configures the different GPIO ports.
187  * @param None
188  * @retval None
189  */
191 {
192  GPIO_InitTypeDef GPIO_InitStructure;
193 
194 #ifdef USE_STM3210C_EVAL
195  /* Enable the USART3 Pins Software Remapping */
196  GPIO_PinRemapConfig(GPIO_PartialRemap_USART3, ENABLE);
197 
198  /* Enable the USART2 Pins Software Remapping */
199  GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
200 #elif defined USE_STM3210B_EVAL || defined USE_STM32100B_EVAL
201  /* Enable the USART2 Pins Software Remapping */
202  GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
203 #endif
204 
205  /* Configure USARTy Rx as input floating */
206  GPIO_InitStructure.GPIO_Pin = USARTy_RxPin;
207  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
208  GPIO_Init(USARTy_GPIO, &GPIO_InitStructure);
209 
210  /* Configure USARTz Rx as input floating */
211  GPIO_InitStructure.GPIO_Pin = USARTz_RxPin;
212  GPIO_Init(USARTz_GPIO, &GPIO_InitStructure);
213 
214  /* Configure USARTy Tx as alternate function push-pull */
215  GPIO_InitStructure.GPIO_Pin = USARTy_TxPin;
216  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
217  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
218  GPIO_Init(USARTy_GPIO, &GPIO_InitStructure);
219 
220  /* Configure USARTz Tx as alternate function push-pull */
221  GPIO_InitStructure.GPIO_Pin = USARTz_TxPin;
222  GPIO_Init(USARTz_GPIO, &GPIO_InitStructure);
223 }
224 
225 /**
226  * @brief Configures the nested vectored interrupt controller.
227  * @param None
228  * @retval None
229  */
231 {
232  NVIC_InitTypeDef NVIC_InitStructure;
233 
234  /* Configure the NVIC Preemption Priority Bits */
235  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
236 
237  /* Enable the USARTy Interrupt */
238  NVIC_InitStructure.NVIC_IRQChannel = USARTy_IRQn;
239  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
240  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
241  NVIC_Init(&NVIC_InitStructure);
242 
243  /* Enable the USARTz Interrupt */
244  NVIC_InitStructure.NVIC_IRQChannel = USARTz_IRQn;
245  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
246  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
247  NVIC_Init(&NVIC_InitStructure);
248 }
249 
250 /**
251  * @brief Compares two buffers.
252  * @param pBuffer1, pBuffer2: buffers to be compared.
253  * @param BufferLength: buffer's length
254  * @retval PASSED: pBuffer1 identical to pBuffer2
255  * FAILED: pBuffer1 differs from pBuffer2
256  */
257 TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength)
258 {
259  while(BufferLength--)
260  {
261  if(*pBuffer1 != *pBuffer2)
262  {
263  return FAILED;
264  }
265 
266  pBuffer1++;
267  pBuffer2++;
268  }
269 
270  return PASSED;
271 }
272 
273 #ifdef USE_FULL_ASSERT
274 
275 /**
276  * @brief Reports the name of the source file and the source line number
277  * where the assert_param error has occurred.
278  * @param file: pointer to the source file name
279  * @param line: assert_param error line source number
280  * @retval None
281  */
282 void assert_failed(uint8_t* file, uint32_t line)
283 {
284  /* User can add his own implementation to report the file name and line number,
285  ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
286 
287  /* Infinite loop */
288  while (1)
289  {
290  }
291 }
292 #endif
293 
294 /**
295  * @}
296  */
297 
298 /**
299  * @}
300  */
301 
302 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/