From 369c0909555371866df5fcc3d267927aee5aafa6 Mon Sep 17 00:00:00 2001 From: jaseg Date: Mon, 14 Aug 2017 12:51:49 +0200 Subject: Multiplexing is working --- fw/main.c | 213 ++++++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 152 insertions(+), 61 deletions(-) (limited to 'fw/main.c') diff --git a/fw/main.c b/fw/main.c index 73e7280..7153d59 100644 --- a/fw/main.c +++ b/fw/main.c @@ -1,18 +1,26 @@ + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wstrict-aliasing" #include -#include -#include #include #include +#pragma GCC diagnostic pop + +#include + +#include #include #include /* * Part number: STM32F030F4C6 */ -void tick(void) { - for(int i=0; i<50; i++) - __asm__("nop"); +/* Wait for about 0.2us */ +static void tick(void) { + /* 1 */ /* 2 */ /* 3 */ /* 4 */ /* 5 */ + /* 5 */ __asm__("nop"); __asm__("nop"); __asm__("nop"); __asm__("nop"); __asm__("nop"); + /* 10 */ __asm__("nop"); __asm__("nop"); __asm__("nop"); __asm__("nop"); __asm__("nop"); } void spi_send(int data) { @@ -32,18 +40,117 @@ void strobe_leds(void) { GPIOA->BSRR = GPIO_BSRR_BR_9; } -#define SR_COMM 0x0002 -#define SR_ERROR 0x0004 -#define SR_ID 0x0008 +static volatile unsigned int sys_time = 0; + +enum Segment { SegA, SegB, SegC, SegD, SegE, SegF, SegG, SegDP, nsegments }; +enum { + nrows = 4, + ncols = 8, + nbits = 10, +}; +enum { + frame_size_words = nrows*ncols*nsegments/32, +}; +struct framebuf { + /* Multiplexing order: first Digits, then Time/bits, last Segments */ + uint32_t data[nbits*frame_size_words]; + int brightness; /* 0 or 1; controls global brighntess control */ +}; + +struct framebuf fb[2] = {0}; +struct framebuf *read_fb=fb+0, *write_fb=fb+1; +volatile int led_state = 0; +volatile enum { FB_WRITE, FB_UPDATE } fb_op; + +#define LED_COMM 0x0001 +#define LED_ERROR 0x0002 +#define LED_ID 0x0004 #define SR_ILED_HIGH 0x0080 #define SR_ILED_LOW 0x0040 + +void shift_aux(int global_current, int leds, int active_segment) { + spi_send( + (global_current ? SR_ILED_HIGH : SR_ILED_LOW) + | (leds<<1) + | (0xff00 ^ (0x100<brightness, led_state, active_segment); + } + + uint32_t current_word = read_fb->data[active_bit*frame_size_words + active_segment]; + spi_send(current_word&0xffff); + spi_send(current_word>>16); + strobe_leds(); + + return 1<CCMR1 = 6<CCER = TIM_CCER_CC1E | TIM_CCER_CC1P; /* Inverting output */ + TIM3->DIER = TIM_DIER_CC1IE; + TIM3->CCR1 = 1000; /* Schedule first interrupt */ + TIM3->PSC = SystemCoreClock/5000000 * 2; /* 0.40us/tick */ + TIM3->ARR = 0xffff; + TIM3->EGR |= TIM_EGR_UG; + TIM3->CR1 = TIM_CR1_ARPE; + TIM3->CR1 |= TIM_CR1_CEN; +} + +TIM_TypeDef *tim3 = TIM3; + +void TIM3_IRQHandler() { + //TIM3->CR1 &= ~TIM_CR1_CEN_Msk; + + static int last_ivl; + last_ivl = TIM3->CNT; + /* This takes about 10us */ + int period = shift_data(); + static int ivl; + ivl = TIM3->CNT - last_ivl; + TIM3->CCR1 = period; + TIM3->CNT = 0xffff; /* To not enable OC1 right away */ + + TIM3->SR &= ~TIM_SR_CC1IF_Msk; + //TIM3->CR1 |= TIM_CR1_CEN; +} + int main(void) { RCC->CR |= RCC_CR_HSEON; while (!(RCC->CR&RCC_CR_HSERDY)); - RCC->CFGR &= ~RCC_CFGR_PLLMUL_Msk & ~RCC_CFGR_SW_Msk; - RCC->CFGR |= (2<CFGR &= ~RCC_CFGR_PLLMUL_Msk & ~RCC_CFGR_SW_Msk & ~RCC_CFGR_PPRE_Msk & ~RCC_CFGR_HPRE_Msk; + RCC->CFGR |= (1< 50.0MHz */ RCC->CFGR2 &= ~RCC_CFGR2_PREDIV_Msk; - RCC->CFGR2 |= RCC_CFGR2_PREDIV_DIV2; + RCC->CFGR2 |= RCC_CFGR2_PREDIV_DIV2; /* prediv :2 -> 12.5MHz */ RCC->CR |= RCC_CR_PLLON; while (!(RCC->CR&RCC_CR_PLLRDY)); RCC->CFGR |= (2<AHBENR |= RCC_AHBENR_GPIOAEN; RCC->APB2ENR |= RCC_APB2ENR_SPI1EN; + RCC->APB1ENR |= RCC_APB1ENR_TIM3EN; GPIOA->MODER |= (2<AFR[0] |= (0<CR2 &= ~SPI_CR2_DS_Msk; SPI1->CR2 |= LL_SPI_DATAWIDTH_16BIT; /* FIXME maybe try w/o BIDI */ + /* Baud rate PCLK/2 -> 25MHz */ SPI1->CR1 = SPI_CR1_BIDIMODE | SPI_CR1_BIDIOE | SPI_CR1_SSM | SPI_CR1_SSI | SPI_CR1_SPE | (0<BSRR = GPIO_BSRR_BR_6; /* OE */ - int j = 0; - int ctr = 0; - int q = 0; - int bval = 0x400; + read_fb->brightness = 1; + for (int i=0; idata)/sizeof(uint32_t); i++) { + read_fb->data[i] = 0xffffffff; + } + cfg_timer3(); + + NVIC_EnableIRQ(TIM3_IRQn); + NVIC_SetPriority(TIM3_IRQn, 2); + + SysTick_Config(SystemCoreClock/1000); /* 1ms interval */ + while (42) { - for (int i=0; i<8; i++) { - spi_send(1<<(ctr&3)); - spi_send(1<<((ctr>>1)&3)); - strobe_leds(); - //spi_send(0x0200 | bval | (0xff^(1<>8)&7; + + int ctr = sys_time>>2; + for (int bit=0, bmask=1; bitdata[bit*frame_size_words + seg] = data; } + fb_op = FB_UPDATE; + while (fb_op == FB_UPDATE) + ; } } @@ -151,5 +241,6 @@ void PendSV_Handler(void) { } void SysTick_Handler(void) { + sys_time++; } -- cgit