diff options
Diffstat (limited to 'serial2.cpp')
-rw-r--r-- | serial2.cpp | 98 |
1 files changed, 60 insertions, 38 deletions
diff --git a/serial2.cpp b/serial2.cpp index 58ea536..c01aa13 100644 --- a/serial2.cpp +++ b/serial2.cpp @@ -1,22 +1,30 @@ -#include "tinyprintf.h" #include <stdint.h> #include <stm32f10x.h> #include <stm32f10x_rcc.h> #include <stm32f10x_gpio.h> -#include <stddef.h> -#include <stdarg.h> -#include "tinyprintf.h" +#include <stm32f10x_usart.h> +#include <misc.h> + #include "debug.h" +#include "tinyprintf.h" int init_high(); extern "C" void halt(); -#include "stm32f10x_conf.h" - extern "C" __attribute__((naked)) void HardFault_Handler_C(uint32_t *hardfault_args) { + dbg_printf("r0 = 0x%08x (%d)\n", hardfault_args[0], hardfault_args[0]); + dbg_printf("r1 = 0x%08x (%d)\n", hardfault_args[1], hardfault_args[1]); + dbg_printf("r2 = 0x%08x (%d)\n", hardfault_args[2], hardfault_args[2]); + dbg_printf("r3 = 0x%08x (%d)\n", hardfault_args[3], hardfault_args[3]); + dbg_printf("r12 = 0x%08x (%d)\n", hardfault_args[4], hardfault_args[4]); + dbg_printf("lr = 0x%08x (%d)\n", hardfault_args[5], hardfault_args[5]); + dbg_printf("pc = 0x%08x (%d)\n", hardfault_args[6], hardfault_args[6]); + dbg_printf("psr = 0x%08x (%d)\n", hardfault_args[7], hardfault_args[7]); + dbg_printf("\n"); + halt(); } @@ -30,6 +38,8 @@ int run = 1; volatile USART_TypeDef *usart1 = (volatile USART_TypeDef *) USART1_BASE; +volatile uint8_t tx_ready = 0; + /* * When we get there the stack pointer is set */ @@ -79,53 +89,65 @@ int main() { init.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &init); + NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); + NVIC_InitTypeDef NVIC_InitStruct = { + /*.NVIC_IRQChannel =*/ USART1_IRQn, + /*.NVIC_IRQChannelPreemptionPriority =*/ 0, + /*.NVIC_IRQChannel =*/ 0, + /*.NVIC_IRQChannelCmd =*/ ENABLE, + }; + NVIC_Init(&NVIC_InitStruct); + NVIC_EnableIRQ(USART1_IRQn); + // 8 bit mode USART1->CR1 &= ~USART_CR1_M; USART1->CR2 &= ~USART_CR2_STOP_1; - USART1->CR1 |= USART_CR1_UE /* Set UART Enable */ - | USART_CR1_TE; /* Set Transmission Enable */ - // Set baud rate int mantissa = 39; int fraction = static_cast<int>(16 * 0.0625); // == 1 // 72M / (16*39.0625) = 115200 USART1->BRR = static_cast<uint16_t >(mantissa << 4 | fraction); + USART1->CR1 |= USART_CR1_UE /* Set UART Enable */ + | USART_CR1_TE /* Set Transmission Enable */ + | USART_CR1_TXEIE; /* Set TX buffer Empty Interrupt Enable */ + char c = 'A'; + tx_ready = 1; while (run) { - int txe = USART1->SR & USART_SR_TXE; - -// dbg_printf("1:%d?\n", x); - -// char mm[100]; -// tfp_sprintf(mm, "2:%d?\n", x); -// send_command(SYS_WRITE0, mm); - -// printf(" %u?\n", usart1->SR); -// printf(" %u?\n", 1); - - if (txe) { - GPIO_SetBits(GPIOB, GPIO_Pin_All); - GPIO_ResetBits(GPIOB, GPIO_Pin_All); - - USART1->DR = (uint16_t) c; -// USART1->DR = 0x55; - - if (c == 'Z') { - c = 'a'; - } else if (c == 'z') { - c = '0'; - } else if (c == '9') { - c = '\n'; - } else if (c == '\n') { - c = 'A'; - } else { - c++; - } + // wait for TX to be ready + while (!tx_ready); + tx_ready = 0; + + GPIO_SetBits(GPIOB, GPIO_Pin_All); + GPIO_ResetBits(GPIOB, GPIO_Pin_All); + + USART1->DR = (uint16_t) c; + USART_ITConfig(USART1, USART_IT_TXE, ENABLE); + + if (c == 'Z') { + c = 'a'; + } else if (c == 'z') { + c = '0'; + } else if (c == '9') { + c = '\n'; + } else if (c == '\n') { + c = 'A'; + } else { + c++; } } return 0; } +extern "C" +void USART1_IRQHandler() { + tx_ready = 1; + + if (USART_GetITStatus(USART1, USART_IT_TXE) == SET) { + // Disable the interrupt + USART_ITConfig(USART1, USART_IT_TXE, DISABLE); + } +} |