From 1c58250425a1fec238f1bbc60b16a8d0b1b554c8 Mon Sep 17 00:00:00 2001 From: jaseg Date: Sat, 9 Dec 2017 12:08:32 +0100 Subject: Cycle timing is fixed again --- fw/main.c | 130 ++++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 76 insertions(+), 54 deletions(-) diff --git a/fw/main.c b/fw/main.c index d40dbff..0aa7c32 100644 --- a/fw/main.c +++ b/fw/main.c @@ -99,7 +99,7 @@ volatile unsigned int sys_time_seconds = 0; volatile struct framebuf fb[2] = {0}; volatile struct framebuf *read_fb=fb+0, *write_fb=fb+1; volatile int led_state = 0; -volatile enum { FB_WRITE, FB_UPDATE } fb_op = FB_WRITE; +volatile enum { FB_WRITE, FB_FORMAT, FB_UPDATE } fb_op; volatile union { struct __attribute__((packed)) { struct framebuf fb; uint8_t end[0]; } set_fb_rq; struct __attribute__((packed)) { uint8_t nbits; uint8_t end[0]; } set_nbits_rq; @@ -174,6 +174,7 @@ void cfg_spi1() { } void SPI1_IRQHandler() { + GPIOA->BSRR = GPIO_BSRR_BS_0; // Debug switch (spi_state) { case SPI_AUX: strobe_aux(); @@ -190,6 +191,7 @@ void SPI1_IRQHandler() { break; } spi_state ++; + GPIOA->BSRR = GPIO_BSRR_BR_0; // Debug } uint8_t segment_map[8] = {5, 7, 6, 4, 1, 3, 0, 2}; @@ -234,19 +236,62 @@ int shift_data() { } SPI1->CR2 |= SPI_CR2_TXEIE; - return 1<CCMR1 = 6<CCER = TIM_CCER_CC1E | TIM_CCER_CC1P | TIM_CCER_CC2E | TIM_CCER_CC2P; /* Inverting output */ - TIM3->DIER = TIM_DIER_CC2IE; - TIM3->CCR2 = 1000; /* Schedule first interrupt */ - TIM3->PSC = SystemCoreClock/5000000 * 2; /* 0.40us/tick */ + TIM3->CCMR1 = (6<CCER = TIM_CCER_CC1E; /* Inverting output */ + TIM3->DIER = TIM_DIER_UIE; + TIM3->PSC = SystemCoreClock/5000000 * 2 - 1; /* 0.20us/tick */ TIM3->ARR = 0xffff; TIM3->EGR |= TIM_EGR_UG; TIM3->CR1 = TIM_CR1_ARPE; @@ -256,18 +301,16 @@ void cfg_timer3() { } void TIM3_IRQHandler() { + GPIOA->BSRR = GPIO_BSRR_BS_4; // Debug //TIM3->CR1 &= ~TIM_CR1_CEN_Msk; FIXME - int period = shift_data(); - TIM3->CCR1 = period; - if (period < 32) /* FIXME this constant */ - TIM3->CCR2 = 32; - else - TIM3->CCR2 = period; - TIM3->CNT = 0xffff; /* To not enable OC1 riglt away */ + int idx = shift_data(); + TIM3->CCR1 = TIMER_CYCLES_FOR_SPI_TRANSMISSIONS; + TIM3->ARR = timer_period_lookup[idx]; - TIM3->SR &= ~TIM_SR_CC2IF_Msk; + TIM3->SR &= ~TIM_SR_UIF_Msk; //TIM3->CR1 |= TIM_CR1_CEN; + GPIOA->BSRR = GPIO_BSRR_BR_4; // Debug } enum Command { @@ -300,14 +343,13 @@ void uart_config(void) { | USART_CR1_MME /* WAKE clear */ /* PCE, PS clear */ - //| USART_CR1_RXNEIE + | USART_CR1_RXNEIE /* other interrupts clear */ | USART_CR1_TE | USART_CR1_RE; //USART1->CR2 = USART_CR2_RTOEN; /* Timeout enable */ - USART1->CR3 = USART_CR3_DEM; /* RS485 DE enable (output on RTS) */ - //| USART_CR3_DMAT; - /* Baud rate 2MBd */ + USART1->CR3 = USART_CR3_DEM /* RS485 DE enable (output on RTS) */ + | USART_CR3_DMAT; int usartdiv = 25; USART1->BRR = usartdiv; USART1->CR1 |= USART_CR1_UE; @@ -380,9 +422,11 @@ int main(void) { RCC->APB1ENR |= RCC_APB1ENR_TIM3EN; GPIOA->MODER |= - (2<OSPEEDR |= - (2<ISR & USART_ISR_ORE) { - USART1->ICR |= USART_ICR_ORECF; - num_overrun_errors ++; - } else if (USART1->ISR & USART_ISR_FE) { - USART1->ICR |= USART_ICR_FECF; - } else if (USART1->ISR & USART_ISR_RXNE) { - data = USART1->RDR; - /* - if (expect_framing) { - if (data == 0x42) { - expect_framing = 0; - } else { - rx_idx = 0; - } - } else { - rx_buf.byte_data[rx_idx] = data; - rx_idx++; - if (rx_idx >= sizeof(rx_buf.set_fb_rq)) { - rx_idx = 0; - if (fb_op == FB_WRITE) { - transpose_data(rx_buf.byte_data, write_fb); - fb_op = FB_UPDATE; - } - } - if ((rx_idx&0x1F) == 0) { - expect_framing = 1; + if (USART1->ISR & USART_ISR_RXNE) { + *rxd++ = USART1->RDR; + if (rxd >= rx_buf.set_fb_rq.end) { + rxd = rx_buf.byte_data; + if (fb_op == FB_FORMAT) { + transpose_data(rx_buf.byte_data, write_fb); + fb_op = FB_UPDATE; } } - */ } } } -- cgit