Skip to content

Commit

Permalink
uart
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Haarer committed May 21, 2024
1 parent 85c30c6 commit 21d4c65
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 22 deletions.
21 changes: 6 additions & 15 deletions examples/arm-none-eabi-example-cpp/main.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@

/* a simple blink example */
/* a simple blink example and serial out */
#include <stdint.h>
extern "C"
{
#include "stm32f10x.h"
#include "system.h"
#include "uart.h"
}
#include <stdio.h>
//#include <iostream>
Expand Down Expand Up @@ -94,44 +95,34 @@ int main(void)
{

static std::vector<int> vec;

uartInit(115200);

RCC->APB2ENR |= RCC_APB2ENR_IOPCEN; // enable port c

pinMode(GPIOC,13,OUTPUT_2MHZ);
pinConfg(GPIOC,13,OUTPUT_PUSH_PULL);




SysTick_Config(SystemCoreClock/1000);



while (1)
{


highPin(GPIOC,13);

myDelay(100);


printf("enough is enough\n ");


lowPin(GPIOC,13);
myDelay(100);



//does not fit into flash
//std::cout << "hello" << std::endl;
vec.push_back(0);
printf("hello vector contains %d elements \n",vec.size());
if (vec.size() > 1000)
if (vec.size() > 100)
{
vec.clear();
printf("enough is enough, resetting\n ");
}

}
Expand Down
28 changes: 23 additions & 5 deletions examples/arm-none-eabi-example-cpp/uart.c
Original file line number Diff line number Diff line change
@@ -1,18 +1,36 @@

#include "stm32f10x.h"

#include <uart.h>

static void __attribute__((constructor)) uartInit(void)
char uartGetch(void)
{

return 0;
}

int uartGetch(void)
void uartPutch(char c)
{
return 0;
// wait for tx data register to be empty
while (!(USART1->SR & USART_SR_TXE))
;
USART1->DR = 0x000000ff & c;
}

void uartPutch(unsigned int ch)
void uartPuts(const char *ch)
{
while (*ch)
{
uartPutch(*ch);
ch++;
}
}


void USART1_IRQHandler(void)
{
if (USART1->SR & USART_SR_RXNE)
{
// this clears RXNE flag
uartPutch(USART1->DR & 0xff);
}
}
39 changes: 37 additions & 2 deletions examples/arm-none-eabi-example-cpp/uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,43 @@
#define UART_H


extern int uartGetch(void);
extern void uartPutch(unsigned int ch);
#include "stm32f10x.h"
#include "system.h"
extern char uartGetch(void);
extern void uartPutch(char ch);
extern void USART1_IRQHandler(void);



/**
* @brief Initialize USART1 peripheral
*
* @param baudrate baudrate to be configurate
*/
static inline void uartInit(uint32_t baudrate)
{
// enable clock for GPIOA and USART1
RCC->APB2ENR |= RCC_APB2ENR_USART1EN | RCC_APB2ENR_IOPAEN;

// reset pin configurations for PA9 and PA10
GPIOA->CRH &= ~(GPIO_CRH_MODE10 | GPIO_CRH_MODE9 | GPIO_CRH_CNF10 | GPIO_CRH_CNF9);

// PA9 as Output@50Hz Alternate function
GPIOA->CRH |= GPIO_CRH_MODE9_0 | GPIO_CRH_MODE9_1 | GPIO_CRH_CNF9_1;

// PA10 as floating input
GPIOA->CRH |= GPIO_CRH_CNF10_0;

uint32_t baud = (uint32_t)(SystemCoreClock / baudrate);

USART1->BRR = baud;

// transmitter enable, receiver enable, receiver interrupt enable and USART enable
USART1->CR1 = USART_CR1_TE | USART_CR1_RE | USART_CR1_RXNEIE | USART_CR1_UE;

// enable USART1 interrupt
NVIC_EnableIRQ(USART1_IRQn);
}


#endif

0 comments on commit 21d4c65

Please sign in to comment.