static void my_TIM2_initInputCaptureTimer(void) {
// enable clock source for timer
RCC->APB1LENR |= (0x1 << 0);
// set prescaler to 200
TIM2->PSC = 200 - 1;
// choose TIM2_CH1 input
TIM2->TISEL |= (0x0 << 0);
// set channel 1 as input mapped on TI1
TIM2->CCMR1 |= (0x1 << 0);
// digital filter length (0)
TIM2->CCMR1 |= (0x0 << 4);
// rising & falling edge
TIM2->CCER |= (0x1 << 1);
TIM2->CCER |= (0x1 << 3);
// prescaler to (0)
TIM2->CCMR1 |= (0x0 << 2);
// enable DMA interrupt & Capture/Compare interrupt
TIM2->DIER |= (0x1 << 9) | (0x1 << 1);
// capture enabled
TIM2->CCER |= (0x1 << 0);
// reset registers WARNING: need for preloading PSC
TIM2->EGR |= (0x1 << 0);
// enable TIM3 timer
TIM2->CR1 |= TIM_CR1_CEN;
// enable interrupt request
NVIC_EnableIRQ(TIM2_IRQn);
// set priority
NVIC_SetPriority(TIM2_IRQn, 1);
}
static void my_DMA_init(void) {
// enable DMA1 clocking
RCC->AHB1ENR |= (0x1 << 0);
// clear EN bit to 0
DMA1_Stream0->CR &= ~(0x1 << 0);
// safeguard EN bit reset
while (DMA1_Stream0->CR & 0x1);
// check LISR HISR registers
if ((DMA1->HISR == 0) && (DMA1->LISR == 0))
printf("status registers is clearrn");
else
printf("status register is not clear -- DMA wont startrn");
// set peripheral addres
DMA1_Stream0->PAR = TIM2_CCR1_Address;
// set memory addres
DMA1_Stream0->M0AR = (unsigned int)buffer;
// set total number of data items
DMA1_Stream0->NDTR = 10;
// NOTE: configurate TIM2_CH1 interrupt route
// set DMAMUX to route request (TIM2_CH1)
DMAMUX1_Channel0->CCR |= 18U;
// set DMA priority (very high)
DMA1_Stream0->CR |= (0x3 << 16);
// set memory data size (32)
DMA1_Stream0->CR |= (0x2 << 13);
// set peripheral data size (32)
DMA1_Stream0->CR |= (0x2 << 11);
// set memory addres increment (enable)
DMA1_Stream0->CR |= (0x1 << 10);
// set peripheral addres increment (disable)
DMA1_Stream0->CR |= (0x0 << 9);
// set circular buffer mode (enable)
DMA1_Stream0->CR |= (0x1 << 8);
// set data transfer direction (peripheral to memory)
DMA1_Stream0->CR |= (0x0 << 6);
// set transfer complete interrupt
DMA1_Stream0->CR |= (0x1 << 4);
// set transfer error interrupt
DMA1_Stream0->CR |= (0x1 << 2);
// enable DMA1
DMA1_Stream0->CR |= (0x1 << 0);
// enable IRQ
NVIC_EnableIRQ(DMA1_Stream0_IRQn);
printf("DMA1_Stream0 %u rn", (DMA1_Stream0->CR & 0x1));}
更多回帖