Is there an code example for using a timer in count mode with compare?

Hi i am trying to configure a timer to work in count mode and when it hits a certain number ( for example 100) to activeta an interrupt, clear count and start again.

Best regards.

Parents Reply Children
  • ISR_DIRECT_DECLARE(trig_cnt_irq_handler)
    {
    	// Half way point
    	if(SPI_TRIG_COUNTER->EVENTS_COMPARE[0]) {
    		SPI_TRIG_COUNTER->EVENTS_COMPARE[0] = 0;
    		
    		m_item_num = LIST_ITEMS / 2;
    		m_item_index = 0;
    		k_sem_give(&m_sem_samples_available);
    	}	
    
    	// End of buffer (not including margin)
    	if(SPI_TRIG_COUNTER->EVENTS_COMPARE[1]) {
    		SPI_TRIG_COUNTER->EVENTS_COMPARE[1] = 0;
    
    		// If there is a risk that this interrupt gets delayed we need to handle the case where more items have been sampled than expected
    		// trigger a capture on the counter to check how far the count has reached
    		// TODO: Possibly we would also need to check the state of the timer to check if is just about to trigger another sample, which could lead to a race condition
    		SPI_TRIG_COUNTER->TASKS_CAPTURE[3] = 1;
    		m_item_num = SPI_TRIG_COUNTER->CC[3] - (LIST_ITEMS / 2);
    		m_item_index = LIST_ITEMS / 2;
    		SPI_MASTER->TXD.PTR = (uint32_t)m_tx_buffer[0];
    		SPI_MASTER->RXD.PTR = (uint32_t)m_rx_buffer[0];
    		SPI_TRIG_COUNTER->TASKS_CLEAR = 1;
    		k_sem_give(&m_sem_samples_available);
    	}
    
    	ISR_DIRECT_PM();
    	return 1;
    }
    
    
    static void trig_counter_init(void)
    {
    	SPI_TRIG_COUNTER->MODE = TIMER_MODE_MODE_Counter << TIMER_MODE_MODE_Pos;
    	SPI_TRIG_COUNTER->CC[0] = LIST_ITEMS / 2;
    	SPI_TRIG_COUNTER->CC[1] = LIST_ITEMS;
    	SPI_TRIG_COUNTER->INTENSET = TIMER_INTENSET_COMPARE0_Msk | TIMER_INTENSET_COMPARE1_Msk;
    	SPI_TRIG_COUNTER->TASKS_CLEAR = 1;
    	SPI_TRIG_COUNTER->TASKS_START = 1;
    
        IRQ_DIRECT_CONNECT(TIMER2_IRQn, IRQ_PRIO_LOWEST, trig_cnt_irq_handler, 0);
    	irq_enable(TIMER2_IRQn);
    
    	LOG_INF("timer konfigurisan");
    }
    Here it is.

Related