-
Notifications
You must be signed in to change notification settings - Fork 1
/
usart_stm32_console.c
66 lines (53 loc) · 2.24 KB
/
usart_stm32_console.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "stm32f0xx.h"
#include "usart_stm32_console.h"
#include "string.h"
#include <stdlib.h>
void USART_STM32_configureInterface(void) {
// Enabling Clocks
RCC_AHBPeriphClockCmd(USART_STM32_GPIO_PERIPH, ENABLE);
RCC_APB2PeriphClockCmd(USART_STM32_USART_PERIPH, ENABLE);
// Configuring GPIO
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = USART_STM32_GPIO_TX_PIN | USART_STM32_GPIO_RX_PIN;
GPIO_PinAFConfig(USART_STM32_GPIO_PORT, USART_STM32_GPIO_TX_PINSRC, USART_STM32_USART_AF);
GPIO_PinAFConfig(USART_STM32_GPIO_PORT, USART_STM32_GPIO_RX_PINSRC, USART_STM32_USART_AF);
GPIO_Init(USART_STM32_GPIO_PORT, &GPIO_InitStructure);
// Configuring USARTx
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = USART_STM32_BAUDRATE;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_Init(USART_STM32_USART_PORT, &USART_InitStructure);
USART_Cmd(USART_STM32_USART_PORT, ENABLE);
}
void USART_STM32_sendStringToUSART(char const *givenString) {
for (int i = 0; i < strlen(givenString); i++) {
USART_SendData(USART_STM32_USART_PORT, givenString[i]);
while(USART_GetFlagStatus(USART_STM32_USART_PORT, USART_FLAG_TC) == RESET) {
// wait for buffer to clear
}
}
USART_SendData(USART_STM32_USART_PORT, 0x0A);
USART_SendData(USART_STM32_USART_PORT, 0x0D);
while(USART_GetFlagStatus(USART_STM32_USART_PORT, USART_FLAG_TC) == RESET) {
// wait for buffer to clear
}
}
void USART_STM32_sendIntegerToUSART(char const *givenString, uint16_t myInteger) {
for (int i = 0; i < strlen(givenString); i++) {
USART_SendData(USART_STM32_USART_PORT, givenString[i]);
while(USART_GetFlagStatus(USART_STM32_USART_PORT, USART_FLAG_TC) == RESET) {
// wait for buffer to clear
}
}
char buffer[8];
itoa(myInteger,buffer,10);
USART_STM32_sendStringToUSART(buffer);
}