diff --git a/examples/arm-none-eabi-example-cpp/main.cpp b/examples/arm-none-eabi-example-cpp/main.cpp index 5f498a8..d3b0351 100644 --- a/examples/arm-none-eabi-example-cpp/main.cpp +++ b/examples/arm-none-eabi-example-cpp/main.cpp @@ -1,10 +1,11 @@ -/* a simple blink example */ +/* a simple blink example and serial out */ #include extern "C" { #include "stm32f10x.h" #include "system.h" +#include "uart.h" } #include //#include @@ -94,7 +95,7 @@ int main(void) { static std::vector vec; - + uartInit(115200); RCC->APB2ENR |= RCC_APB2ENR_IOPCEN; // enable port c @@ -102,36 +103,26 @@ int main(void) 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 "); } } diff --git a/examples/arm-none-eabi-example-cpp/uart.c b/examples/arm-none-eabi-example-cpp/uart.c index 363784f..8978299 100644 --- a/examples/arm-none-eabi-example-cpp/uart.c +++ b/examples/arm-none-eabi-example-cpp/uart.c @@ -1,18 +1,36 @@ +#include "stm32f10x.h" #include -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); + } } diff --git a/examples/arm-none-eabi-example-cpp/uart.h b/examples/arm-none-eabi-example-cpp/uart.h index 42e9255..124528d 100644 --- a/examples/arm-none-eabi-example-cpp/uart.h +++ b/examples/arm-none-eabi-example-cpp/uart.h @@ -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