#include "Usart.h"
|
|
void Usart_Init(void)
|
{
|
USART_InitTypeDef USART_InitStructure;
|
GPIO_InitTypeDef GPIO_InitStructure;
|
|
/* Enable GPIO clock */
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
|
/* Enable USART clock */
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
|
|
/* Configure USART Tx as alternate function push-pull */
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
GPIO_InitStructure.GPIO_Pin = USART_TX_pin;
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
GPIO_Init(USART_GPIO, &GPIO_InitStructure);
|
|
/* Configure USART Rx as input floating */
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
|
GPIO_InitStructure.GPIO_Pin = USART_RX_pin;
|
GPIO_Init(USART_GPIO, &GPIO_InitStructure);
|
|
/* USARTx configured as follow:
|
- BaudRate = 115200 baud
|
- Word Length = 8 Bits
|
- One Stop Bit
|
- No parity
|
- Hardware flow control disabled (RTS and CTS signals)
|
- Receive and transmit enabled
|
*/
|
USART_InitStructure.USART_BaudRate = BAUD_RATE ;
|
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
|
USART_InitStructure.USART_StopBits = USART_StopBits_1;
|
USART_InitStructure.USART_Parity = USART_Parity_No;
|
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
|
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
|
|
/* USART configuration */
|
USART_Init(EXT_USART, &USART_InitStructure);
|
|
/* Enable USART */
|
USART_Cmd(EXT_USART, ENABLE);
|
|
}
|
|
uint16_t Checksum_u16(uint8_t *pdata, uint32_t len)
|
{
|
uint16_t sum = 0;
|
uint32_t i;
|
for(i = 0; i < len; i++)
|
sum += pdata[i];
|
sum = ~sum;
|
return sum;
|
}
|