/* Megumin LED display firmware * Copyright (C) 2018 Sebastian Götte * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include "led.h" #include "display.h" volatile int frame_duration_us = 0; volatile int nbits = MAX_BITS; /* Modulation data */ volatile enum FB_OPERATION fb_op; static volatile struct framebuf fb[2] = {0}; volatile struct framebuf *read_fb=fb+0, *write_fb=fb+1; /* Auxiliary shift register values */ #define LED_COMM 0x0001 #define LED_ERROR 0x0002 #define LED_ID 0x0004 #define SR_ILED_HIGH 0x0080 #define SR_ILED_LOW 0x0040 /* This is a lookup table mapping segments to present a standard segment order on the UART interface. This is converted * into an internal representation once on startup in main(). The data type must be at least uint16. */ static uint32_t segment_map[8] = {5, 7, 6, 4, 1, 3, 0, 2}; static unsigned int active_bit = 0; static int active_segment = 0; /* Bit timing base value. This is the lowes bit interval used in TIM1/TIM3 timer counts. */ #define PERIOD_BASE 4 /* This value is a constant offset added to every bit period to allow for the timer IRQ handler to execute. This is set * empirically using a debugger and a logic analyzer. * * This value is in TIM1/TIM3 timer counts. */ #define TIMER_CYCLES_FOR_SPI_TRANSMISSIONS 9 /* This value sets the point when the LED strobe is asserted after the begin of the current bit cycle and IRQ * processing. This must be less than TIMER_CYCLES_FOR_SPI_TRANSMISSIONS but must be large enough to allow for the SPI * transmission to reliably finish. * * This value is in TIM1/TIM3 timer counts. */ #define TIMER_CYCLES_BEFORE_LED_STROBE 8 /* This value sets how long the TIM1 CC IRQ used for AUX register setting etc. is triggered before the end of the * longest cycle. This value should not be larger than PERIOD_BASE<brightness = 1; for (int i=0; idata)/sizeof(uint32_t); i++) { read_fb->data[i] = 0xffffffff; /* FIXME this is a debug value. Should be 0x00000000; */ } display_cfg_timers(); } void display_cfg_spi() { /* Configure SPI controller */ SPI1->I2SCFGR = 0; SPI1->CR2 &= ~SPI_CR2_DS_Msk; SPI1->CR2 &= ~SPI_CR2_DS_Msk; SPI1->CR2 |= LL_SPI_DATAWIDTH_16BIT; /* Baud rate PCLK/4 -> 12.5MHz */ SPI1->CR1 = SPI_CR1_BIDIMODE | SPI_CR1_BIDIOE | SPI_CR1_SSM | SPI_CR1_SSI | SPI_CR1_SPE | (1<CR2 = (2<CCMR1 = (6<CCER = TIM_CCER_CC1E; TIM3->CCR1 = TIMER_CYCLES_FOR_SPI_TRANSMISSIONS; 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; TIM3->CR1 |= TIM_CR1_CEN; /* Slave TIM1 to TIM3. */ TIM1->PSC = TIM3->PSC; TIM1->SMCR = (2< TIM3; slave mode: reset */ /* Setup CC1 and CC2. CC2 generates the LED drivers' STROBE, CC1 triggers the IRQ handler */ TIM1->BDTR = TIM_BDTR_MOE; TIM1->CCMR1 = (6<CCMR2 = (6<CCER = TIM_CCER_CC1E | TIM_CCER_CC2E | TIM_CCER_CC4E; TIM1->CCR2 = TIMER_CYCLES_BEFORE_LED_STROBE; /* Trigger at the end of the longest bit cycle. This means this does not trigger in shorter bit cycles. */ TIM1->CCR1 = timer_period_lookup[nbits-1] - AUX_SPI_PRETRIGGER; TIM1->CCR4 = timer_period_lookup[nbits-1] - ADC_PRETRIGGER; TIM1->DIER = TIM_DIER_CC1IE; TIM1->ARR = 0xffff; /* This is as large as possible since TIM1 is reset by TIM3. */ /* Preload all values */ TIM1->EGR |= TIM_EGR_UG; TIM1->CR1 = TIM_CR1_ARPE; /* And... go! */ TIM1->CR1 |= TIM_CR1_CEN; /* Sends aux data and swaps frame buffers if necessary */ NVIC_EnableIRQ(TIM1_CC_IRQn); NVIC_SetPriority(TIM1_CC_IRQn, 0); /* Sends LED data and sets up the next bit cycle's timings */ NVIC_EnableIRQ(TIM3_IRQn); NVIC_SetPriority(TIM3_IRQn, 0); } void TIM1_CC_IRQHandler() { //static int last_frame_time = 0; /* This handler takes about 1.5us */ GPIOA->BSRR = GPIO_BSRR_BS_0; // Debug /* Set SPI baudrate to 12.5MBd for slow-ish 74HC(T)595. This is reset again in TIM3's IRQ handler.*/ SPI1->CR1 |= (2<BSRR = GPIO_BSRR_BR_10; /* Send AUX register data */ uint32_t aux_reg = (read_fb->brightness ? SR_ILED_HIGH : SR_ILED_LOW) | (led_state<<1); SPI1->DR = aux_reg | segment_map[active_segment]; /* TODO: Measure frame rate for status report */ /* Clear interrupt flag */ TIM1->SR &= ~TIM_SR_CC1IF_Msk; GPIOA->BSRR = GPIO_BSRR_BR_0; // Debug } void TIM3_IRQHandler() { /* This handler takes about 2.1us */ GPIOA->BSRR = GPIO_BSRR_BS_0; // Debug /* Reset SPI baudrate to 25MBd for fast MBI5026. Every couple of cycles, TIM1's ISR will set this to a slower value * for the slower AUX registers.*/ SPI1->CR1 &= ~SPI_CR1_BR_Msk; /* Assert aux strobe reset by TIM1's IRQ handler */ GPIOA->BSRR = GPIO_BSRR_BS_10; /* Queue LED driver data into SPI peripheral */ uint32_t spi_word = read_fb->data[active_bit*FRAME_SIZE_WORDS + active_segment]; SPI1->DR = spi_word>>16; spi_word &= 0xFFFF; /* Note that this only waits until the internal FIFO is ready, not until all data has been sent. */ while (!(SPI1->SR & SPI_SR_TXE)); SPI1->DR = spi_word; /* Advance bit. This will overflow, but that is OK since before the next invocation of this ISR, the other ISR will * reset it. */ active_bit++; /* Schedule next bit cycle */ TIM3->ARR = timer_period_lookup[active_bit]; /* Clear interrupt flag */ TIM3->SR &= ~TIM_SR_UIF_Msk; GPIOA->BSRR = GPIO_BSRR_BR_0; // Debug }