# HG changeset patch # User Daniel O'Connor # Date 1318068334 -37800 # Node ID 274e01fa5a4ca4819129c970bbd538fd187f6325 # Parent 7a08db98ae8b4294c81e67e4d93fecbee12a4b7f - Do console IO with RX IRQs. - Init tick counter (unused anyway). - Handle C-u and C-w. - Rejig NVIC setup for less duplication. diff -r 7a08db98ae8b -r 274e01fa5a4c main.c --- a/main.c Sat Oct 08 20:33:47 2011 +1030 +++ b/main.c Sat Oct 08 20:35:34 2011 +1030 @@ -5,7 +5,11 @@ #include "main.h" #include "comm.h" -USART_InitTypeDef USART_InitStructure; +typedef volatile struct { + char buf[40]; + uint8_t state; + uint8_t len; +} consbuf_t; void Setup_HW(void); void NVIC_Configuration(void); @@ -13,28 +17,70 @@ /* Called every millisecond */ RAMFUNC void SysTick_Handler(void) { - static uint32_t tick; + static uint32_t tick = 0; tick++; } +consbuf_t cmd; + RAMFUNC void USART1_IRQHandler(void) { + char c; + int i; + /* Recieved data */ + while (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) { + c = USART_ReceiveData(USART1); + + /* End of line? */ + if (c == '\n' || c == '\r') { + cmd.buf[cmd.state] = '\0'; + fputs("\r\n", stdout); + cmd.len = cmd.state; + cmd.state = 255; + continue; + } + + /* Ctrl-w / Ctrl-u */ + if (c == 0x17 || c == 0x15) { + for (i = 0; i < cmd.state; i++) + fputs("\010\040\010", stdout); + cmd.state = 0; + continue; + } + + /* Backspace/delete */ + if (c == 0x08 || c == 0x7f) { + if (cmd.state > 0) { + cmd.state--; + fputs("\010\040\010", stdout); + } + continue; + } + + /* Anything unprintable just ignore it */ + if (!isprint(c)) + continue; + + cmd.buf[cmd.state] = tolower(c); + + /* Echo back to the user */ + comm_put(cmd.buf[cmd.state]); + + cmd.state++; + /* Over flow? */ + if (cmd.state == ((sizeof(cmd.buf) / sizeof(cmd.buf[0])) - 1)) { + fputs("\r\nLine too long", stdout); + cmd.state = 0; + continue; + } + } } -typedef volatile struct { - char buf[40]; - uint8_t state; - uint8_t len; -} consbuf_t; int main(void) { - consbuf_t cmd; - char c; - int idx; - cmd.state = cmd.len = 0; /* Setup USART etc */ @@ -51,49 +97,17 @@ ; } + setvbuf(stdout, NULL, _IONBF, 0); + /* Say hello */ - comm_puts("\r\nHello world\r\n"); - idx = 0; + fputs("\r\nHello world\r\n", stdout); while (1) { - comm_puts("> "); - while (1) { - c = comm_get(); - - /* End of line? */ - if (c == '\n' || c == '\r') { - cmd.buf[cmd.state] = '\0'; - printf("\r\n"); - cmd.len = cmd.state; - cmd.state = 255; - break; - } + fputs("> ", stdout); - /* Backspace/delete */ - if (c == 0x08 || c == 0x7f) { - if (cmd.state > 0) { - cmd.state--; - printf("\177\040\177"); - } - continue; - } + while (cmd.state != 255) + ; - /* Anything unprintable just ignore it */ - if (!isprint(c)) - continue; - cmd.buf[cmd.state] = tolower(c); - - /* Echo back to the user */ - comm_put(cmd.buf[cmd.state]); - - cmd.state++; - /* Over flow? */ - if (cmd.state == ((sizeof(cmd.buf) / sizeof(cmd.buf[0])) - 1)) { - printf("\r\nLine too long"); - cmd.state = 0; - continue; - } - } if (cmd.len > 0) printf("Got command '%s'\r\n", cmd.buf); cmd.state = 0; @@ -104,6 +118,7 @@ void Setup_HW(void) { GPIO_InitTypeDef GPIO_InitStructure; + USART_InitTypeDef USART_InitStructure; /* Enable USART1, GPIOA, GPIOD and AFIO clocks */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA |RCC_APB2Periph_GPIOD @@ -136,6 +151,9 @@ /* Configure USART1 */ USART_Init(USART1, &USART_InitStructure); + /* Enable interrupts on receive data */ + USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); + /* Enable the USART1 */ USART_Cmd(USART1, ENABLE); } @@ -144,16 +162,26 @@ #ifdef VECT_TAB_RAM /* vector-offset (TBLOFF) from bottom of SRAM. defined in linker script */ extern uint32_t _isr_vectorsram_offs; -void -NVIC_Configuration(void) { - /* Set the Vector Table base location at 0x20000000+_isr_vectorsram_offs */ - NVIC_SetVectorTable(NVIC_VectTab_RAM, (uint32_t)&_isr_vectorsram_offs); -} #else extern uint32_t _isr_vectorsflash_offs; +#endif + void NVIC_Configuration(void) { + NVIC_InitTypeDef NVIC_InitStructure; + +#ifdef VECT_TAB_RAM + /* Set the Vector Table base location at 0x20000000+_isr_vectorsram_offs */ + NVIC_SetVectorTable(NVIC_VectTab_RAM, (uint32_t)&_isr_vectorsram_offs); +#else /* Set the Vector Table base location at 0x08000000+_isr_vectorsflash_offs */ NVIC_SetVectorTable(NVIC_VectTab_FLASH, (uint32_t)&_isr_vectorsflash_offs); +#endif + + /* Enable the USART1 Interrupt */ + NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; + NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; + NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; + NVIC_Init(&NVIC_InitStructure); } -#endif /* VECT_TAB_RAM */ +