#include #include #include #include /* * Part number: STM32F030F4C6 */ int main(void) { LL_Init1msTick(SystemCoreClock); RCC->AHBENR |= RCC_AHBENR_GPIOAEN | RCC_AHBENR_GPIOBEN; RCC->APB2ENR |= RCC_APB2ENR_SPI1EN | RCC_APB2ENR_TIM1EN | RCC_APB2ENR_USART1EN | RCC_APB2ENR_ADCEN; GPIOA->MODER |= (3<MODER |= (1<OTYPER |= GPIO_OTYPER_OT_6 | GPIO_OTYPER_OT_4; /* LED outputs -> open drain */ /* Set shift register IO GPIO output speed */ GPIOA->OSPEEDR |= (1<AFR[0] |= (1<AFR[1] |= (2<BSRR = GPIO_BSRR_BR_1; /* clear output is active low */ /* Configure SPI controller */ /* CPOL=0, CPHA=0, prescaler=8 -> 1MBd */ // SPI1->CR1 = SPI_CR1_BIDIMODE | SPI_CR1_BIDIOE | SPI_CR1_SSM | SPI_CR1_SSI | SPI_CR1_SPE | (2<CR1 = SPI_CR1_BIDIMODE | SPI_CR1_BIDIOE | SPI_CR1_SSM | SPI_CR1_SSI | SPI_CR1_SPE | (7<CR2 = (7<CR1 = TIM_CR1_OPM | TIM_CR1_ARPE | TIM_CR1_URS; TIM1->PSC = 256; // debug TIM1->CCMR1 = (6<CCMR2 = (6<CCER = TIM_CCER_CC2E | TIM_CCER_CC3E; TIM1->BDTR = TIM_BDTR_MOE; TIM1->RCR = 2; TIM1->DIER = TIM_DIER_UIE; NVIC_EnableIRQ(TIM1_BRK_UP_TRG_COM_IRQn); NVIC_SetPriority(TIM1_BRK_UP_TRG_COM_IRQn, 2); TIM1->ARR = 1024; TIM1->EGR |= TIM_EGR_UG; TIM1->CR1 |= TIM_CR1_CEN; for (;;) { } } #define NBITS 4 uint8_t brightness_by_bit[NBITS] = { 0x11, 0x22, 0x44, 0x88 }; void TIM1_BRK_UP_TRG_COM_IRQHandler(void) { static uint32_t bitpos = 0; bitpos = (bitpos+1)&(NBITS-1); // SPI1->DR = ((uint32_t)brightness_by_bit[bitpos])<<8; SPI1->DR = (bitpos<<8) | (bitpos<<10) | (bitpos<<12) | (bitpos<<14); while (SPI1->SR & SPI_SR_BSY); const uint32_t cycles_strobe = 2; const uint32_t cycles_clear = 2; const uint32_t base_val = 16; uint32_t period = base_val<ARR = period; TIM1->ARR = 1024; TIM1->CCR3 = cycles_strobe; /* strobe */ TIM1->CCR2 = period-cycles_clear; /* clear */ TIM1->EGR |= TIM_EGR_UG; // TIM1->ARR = cycles_strobe+1; // LL_mDelay(1); // TIM1->CR1 |= TIM_CR1_CEN; // GPIOA->BSRR = GPIO_BSRR_BR_4 | GPIO_BSRR_BS_6; } void NMI_Handler(void) { } void HardFault_Handler(void) { for(;;); } void SVC_Handler(void) { } void PendSV_Handler(void) { } void SysTick_Handler(void) { }