Browse over 10,000 Electronics Projects

How Frequency Counter Works? And Build a Nanocounter using an FPGA, STM32 and a Bluetooth Android App

How Frequency Counter Works? And Build a Nanocounter using an FPGA, STM32 and a Bluetooth Androi ...

The MCU Program

The MCU program uses my stm32plus library to communicate with the peripherals. There’s no direct support in the library yet for the 072 series but I’m only using peripherals that are also present on the 051 which is supported so I build against the 051 and use adjusted startup and linker scripts to cater for the larger memory sizes in the 072.

The program is mostly asynchronous but there is a main loop in Program.h that performs some work.

  for(;;) {

    // check the command processor for activity

    _commandProcessor.run();

    // if the lock is lost then stop

    if(!_pllLockState && _frequencyCounter.isStarted())
      _frequencyCounter.stop();

    // if not started, locked and counters have been processed then start

    if(_pllLockState && !_frequencyCounter.isStarted() && !_countersReady)
      _frequencyCounter.start();

    // new counters available?

    if(_countersReady) {

      MillisecondTimer::delay(10);

      // read from the FPGA

      _frequencyCounter.readCounters(counters);

      // stop (reset) the counters inside the FPGA

      _frequencyCounter.stop();

      // set the latest counters inside the command processor

      _commandProcessor.setCounters(counters);

      // reset for a new run

      _countersReady=false;
    }
  }

The command processor run() method simply keeps the ‘link’ LED lit if there has been any bluetooth activity in the last second. Bluetooth commands are received and processed completely asynchronously.



Advertisement1


A USART DMA channel is used to receive the command from the app and then I get an interrupt to tell me it’s ready. I then process the command in the IRQ context and initiate a USART DMA tranmission to send the response back to the app.

The device cannot function unless the PLL is locked so there’s an emergency stop feature if the lock is lost. The ‘lock’ LED is also lit up as long as the PLL is locked. I don’t need to poll for this because I’m using the STM32 EXTI feature to get an interrupt when the state of the PLL lock GPIO pin changes.

The rest of the loop just goes around in a loop starting a counting session on the FPGA, checking if it’s done (another EXTI interrupt) and if so then reading the final counters back and making them available to the app.

All the source code is of course free and available here on github.

 

NEXT:

Testing

Pages: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

 


Top