Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Freq count t4 branch #11

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 23 additions & 4 deletions FreqCount.cpp
Expand Up @@ -26,14 +26,11 @@
#include "FreqCount.h"
#include "util/FreqCountTimers.h"

#if(!defined(__IMXRT1062__))
static uint16_t count_msw;
static uint32_t count_prev;
static volatile uint32_t count_output;
static volatile uint8_t count_ready;
static uint16_t gate_length;
static uint16_t gate_index;


void FreqCountClass::begin(uint16_t msec)
{
if (msec < 1) return;
Expand Down Expand Up @@ -113,6 +110,28 @@ ISR(TIMER_ISR_VECTOR)
}
}

#else
void FreqCountClass::begin(uint32_t msec)
{
counter_init();
timer_init(msec); // us
counter_start();
}

uint8_t FreqCountClass::available(void)
{
return count_ready;
}

uint32_t FreqCountClass::read(void)
{
if (count_ready) {
count_ready = 0;
}
return count_output;
}

#endif

FreqCountClass FreqCount;

Expand Down
6 changes: 5 additions & 1 deletion FreqCount.h
Expand Up @@ -5,7 +5,11 @@

class FreqCountClass {
public:
static void begin(uint16_t msec);
#if(!defined(__IMXRT1062__))
static void begin(uint16_t msec);
#else
static void begin(uint32_t msec);
#endif
static uint8_t available(void);
static uint32_t read(void);
static void end(void);
Expand Down
File renamed without changes.
28 changes: 28 additions & 0 deletions examples/Serial_Output_T4/Serial_Output_T4.ino
@@ -0,0 +1,28 @@
/* FreqCount - Example with serial output
* http://www.pjrc.com/teensy/td_libs_FreqCount.html
*
* This example code is in the public domain.
*
* For the Teensy 4.0 pin 14 is used to measure the freq
* FreqCount.begin time is in microseconds vs milliseconds
* for other boards.
*/
#include <FreqCount.h>

void setup() {
Serial.begin(57600);

delay(2000);
analogWriteFrequency(11, 123456); // test jumper 11 to 25
analogWrite(11, 128);

FreqCount.begin(1000000); //Time in microseconds
}

void loop() {
if (FreqCount.available()) {
unsigned long count = FreqCount.read();
Serial.println(count);
}
}

63 changes: 61 additions & 2 deletions util/FreqCountTimers.h
Expand Up @@ -26,6 +26,10 @@
#ifndef FreqCount_timers_h_
#define FreqCount_timers_h_

static uint32_t count_prev;
static volatile uint32_t count_output;
static volatile uint8_t count_ready;

// Arduino Mega
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
// #define COUNTER_USE_TIMER1 // T1 is not connected
Expand All @@ -34,6 +38,11 @@
#define COUNTER_USE_TIMER5 // T5 is pin 47
#define TIMER_USE_TIMER2

//Teensy 4.0
#elif defined(__IMXRT1062__)
#define COUNTER_USE_GPT
#define TIMER_USE_INTERVALTIMER_T4

// Teensy 3.0 & 3.1 & LC
#elif defined(__MK20DX128__) || defined(__MK20DX256__) || defined(__MKL26Z64__) || defined(__MK64FX512__) || defined(__MK66FX1M0__)
#define COUNTER_USE_LPTMR // LPTMR is pin 13 (has LED connected)
Expand Down Expand Up @@ -109,6 +118,42 @@ static inline void counter_overflow_reset(void)
}


#elif defined(COUNTER_USE_GPT)
static inline void counter_init(void)
{
CCM_CCGR0 |= CCM_CCGR0_GPT2_BUS(CCM_CCGR_ON) ; // enable GPT2 module
GPT2_CR = 0;
GPT2_SR = 0x3F; // clear all prior status
GPT2_CR = GPT_CR_CLKSRC(3);// | GPT_CR_FRR ;// 3 external clock
IOMUXC_SW_MUX_CTL_PAD_GPIO_AD_B1_02 = 0x08; //uses pin 14
IOMUXC_GPT2_IPP_IND_CLKIN_SELECT_INPUT = 0x01;
}

static inline void counter_start(void)
{
GPT2_CR |= GPT_CR_EN; // enable
}

static inline void counter_shutdown(void)
{
GPT2_CR = 0;
}

static inline uint32_t counter_read(void) // was uint16_t in FreqCount?
{
return GPT2_CNT;
}

static inline uint8_t counter_overflow(void)
{
return GPT2_SR & GPT_SR_ROV;
}

static inline void counter_overflow_reset(void)
{
GPT2_SR |= GPT_SR_ROV;
}



#elif defined(COUNTER_USE_TIMER1) // 16 bit Timer 1 on Atmel AVR
Expand Down Expand Up @@ -296,7 +341,7 @@ static inline void counter_overflow_reset(void)
#if defined(TIMER_USE_INTERVALTIMER) // Teensyduino IntervalTimer

static IntervalTimer itimer;
static void timer_interrupt(void);
static void timer_callback(void);

static inline uint16_t timer_init(uint16_t msec)
{
Expand All @@ -308,7 +353,7 @@ static inline void timer_start(void)
// IntervalTimer is capable of longer intervals, but
// LPTMR can overflow. Limiting to 1 ms allows counting
// up to 65.535 MHz... LPTMR on Teensy 3.1 can do 65 MHz!
itimer.begin(timer_interrupt, 1000);
itimer.begin(timer_callback, 1000000);
}

static inline void timer_shutdown(void)
Expand All @@ -322,7 +367,21 @@ static inline void timer_shutdown(void)
#endif
#define ISR(name) void name (void)

#elif defined(TIMER_USE_INTERVALTIMER_T4)
static IntervalTimer itimer;

void timer_callback() {
uint32_t count = counter_read();

//track rollover ?
count_output = count - count_prev;
count_prev = count;
count_ready = 1;
}

void timer_init(uint32_t msec){
itimer.begin(timer_callback, msec);
}


#elif defined(TIMER_USE_TIMER2) // 8 bit Timer 2 on Atmel AVR
Expand Down