Skip to content

Instantly share code, notes, and snippets.

@RyanSchw
Created December 1, 2019 21:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RyanSchw/95a155c38f73b6f5b7d25ca209bc1583 to your computer and use it in GitHub Desktop.
Save RyanSchw/95a155c38f73b6f5b7d25ca209bc1583 to your computer and use it in GitHub Desktop.
Starting code
void setup_led() {
// Configure GPIO
RCC->AHBENR |= RCC_AHBENR_GPIOAEN;
// Configure PA8 (TIM1_CH1) for LED data output
GPIOA->MODER |= (2 << (2 * 8));
GPIOA->OSPEEDR |= (3 << (2 * 8));
GPIOA->AFR[1] |= 0x02; // Configure channel mode for PWM mode 1 (Table 14, TIM1_CH1)
// Configure TIM1
RCC->APB2ENR |= RCC_APB2ENR_TIM1EN;
TIM1->CR1 &= ~TIM_CR1_CEN;
// Enable capture compare PWM mode
TIM1->CCMR1 |= TIM_CCMR1_OC1M_1 | TIM_CCMR1_OC1M_2 | TIM_CCMR1_OC1PE;
TIM1->CCER |= TIM_CCER_CC1E; // use capture compare 1 as output
TIM1->CR1 &= ~TIM_CR1_CMS;
TIM1->CR1 &= ~TIM_CR1_DIR;
TIM1->PSC = 8 - 1;
TIM1->ARR = 7 - 1;
// Configure the timer so that an update event triggers a DMA transfer.
TIM1->DIER |= TIM_DIER_CC1DE;
// TIM1->DIER |= TIM_DIER_UDE;
// Don't use timer interrupt
// TIM1->DIER |= TIM_DIER_UIE;
// NVIC->ISER[0] |= 1<<TIM1_BRK_UP_TRG_COM_IRQn;
// Configure DMA
// Use DMA channel 2 because Table 30 (TIM1_Ch1 => DMA1_Ch2)
RCC->AHBENR |= RCC_AHBENR_DMA1EN;
DMA1_Channel2->CCR &= ~DMA_CCR_EN;
DMA1_Channel2->CMAR = (uint32_t) playercolor; // Copy from the led table
DMA1_Channel2->CPAR = (uint32_t)(&(TIM1->CCR1)); // Copy to the CCR for the PWM
DMA1_Channel2->CNDTR = sizeof playercolor; // 24 LEDs in ring, same size as ledtable
DMA1_Channel2->CCR &= ~(DMA_CCR_MEM2MEM | DMA_CCR_MSIZE_1 | DMA_CCR_PSIZE_1);
DMA1_Channel2->CCR |= DMA_CCR_MSIZE_0; // MSIZE 16-bit
DMA1_Channel2->CCR |= DMA_CCR_PSIZE_0; // PSIZE 16-bit
DMA1_Channel2->CCR |= DMA_CCR_DIR | DMA_CCR_MINC | DMA_CCR_CIRC; // DMA_CCR_CIRC
// Enable these two to view IRQHandler
// DMA1_Channel2->CCR |= DMA_CCR_TCIE | DMA_CCR_TEIE;
// NVIC->ISER[0] |= 1<<DMA1_Channel2_3_IRQn;
// Enable everything
DMA1_Channel2->CCR |= DMA_CCR_EN;
TIM1->CR1 |= TIM_CR1_CEN;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment