Created
April 6, 2020 11:47
-
-
Save saffetblt/05af22d88539f293398712abb961ab9f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "stm32f4xx.h" // Device header | |
#include "stdio.h" | |
void USART2_Init(void); | |
int main() { | |
int n; | |
char str[100]; | |
USART2_Init(); | |
printf("Hello from the other side\r\n"); | |
fprintf(stdout, "test for stdout\r\n"); | |
fprintf(stderr, "test for stderr\r\n"); | |
while(1) { | |
printf("How old are you? "); | |
scanf("%d", &n); | |
printf("Your age is : %d\r\n",n); | |
printf("Enter your first name : "); | |
gets(str); | |
printf("I like a your style :"); | |
puts(str); | |
printf("\r\n"); | |
} | |
} | |
void USART2_Init(void){ | |
RCC->AHB1ENR |= 0x0001; // Enable GPIOA | |
RCC->APB1ENR |= (1<<17); // Enable USART2: PA2=RX, PA3=TX | |
GPIOA->AFR[0] |= 0x7700; // Alternate function for PA2=AF7 and PA3=AF7 | |
GPIOA->MODER |= 0x00A0; // Enable Alternate function for PA2,PA3 | |
USART2->BRR |= 0x0683; // 9600 Baudrate: @16Mhz | |
USART2->CR1 |= 0x000C; // Enable RX and TX | |
USART2->CR1 |= 0x2000; // Enable USART2 | |
} | |
int USART2_Write(int ch) { | |
while(!(USART2->SR & 0x0080)); // Wait for Tx buffer empty | |
USART2->DR = (ch & 0xFF); | |
return ch; | |
} | |
int USART2_Read() { | |
while(!(USART2->SR & 0x0020)); // Wait for Rx buffer empty | |
return USART2->DR; | |
} | |
struct __FILE{int handle;}; | |
FILE __stdin = {0}; | |
FILE __stdout = {1}; | |
FILE __stderr = {2}; | |
int fgetc(FILE *f){ | |
int c; | |
c = USART2_Read(); | |
if (c == '\r') { | |
USART2_Write(c); | |
c = '\n'; | |
} | |
USART2_Write(c); | |
return c; | |
} | |
int fputc(int c, FILE *f) { | |
return USART2_Write(c); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment