• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
3  *
4  * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
5  * You may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *        http://www.st.com/software_license_agreement_liberty_v2
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "usart.h"
18 
Fputc(int ch,FILE * f)19 int Fputc(int ch, FILE *f)
20 {
21 	while ((USART1->SR & 0X40) == 0); /* 0, register status */
22 	USART1->DR = (u8)ch;
23 	return ch;
24 }
25 
26 #if EN_USART1_RX
27 u8 USART_RX_BUF[USART_REC_LEN];
28 u16 USART_RX_STA = 0;
29 
UartInit(u32 bound)30 void UartInit(u32 bound) {
31 	USART_InitTypeDef usartInitStruct;
32 	NVIC_InitTypeDef nvicInitStruct;
33 	GPIO_InitTypeDef gpioInitStruct;
34 
35 	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
36 	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
37 
38 	GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
39 	GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
40 
41   	gpioInitStruct.GPIO_PuPd = GPIO_PuPd_UP;
42 	gpioInitStruct.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
43 	gpioInitStruct.GPIO_OType = GPIO_OType_PP;
44 	gpioInitStruct.GPIO_Mode = GPIO_Mode_AF;
45 	gpioInitStruct.GPIO_Speed = GPIO_Speed_50MHz;
46 	GPIO_Init(GPIOA, &gpioInitStruct);
47 
48 	usartInitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
49 	usartInitStruct.USART_BaudRate = bound;
50 	usartInitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
51 	usartInitStruct.USART_WordLength = USART_WordLength_8b;
52 	usartInitStruct.USART_Parity = USART_Parity_No;
53 	usartInitStruct.USART_StopBits = USART_StopBits_1;
54 	USART_Init(USART1, &usartInitStruct);
55 
56 	USART_Cmd(USART1, ENABLE);
57 
58 #if EN_USART1_RX
59 	USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
60   	nvicInitStruct.NVIC_IRQChannel = USART1_IRQn;
61 	nvicInitStruct.NVIC_IRQChannelSubPriority = 3; /* 3, priority */
62 	nvicInitStruct.NVIC_IRQChannelCmd = ENABLE;
63 	nvicInitStruct.NVIC_IRQChannelPreemptionPriority = 3; /* 3, priority */
64 	NVIC_Init(&nvicInitStruct);
65 #endif
66 }
67 
68 #endif
69