Time handler (getting out of an loop) - Arduino Due

Hello,

So i’m trying to implement a time handler to call a function in every second. The code is attached bellow:

#define CS 10
#define SPI_RATE 2
#define SPI_CHIP_SEL 3

void setup()
{
  Serial.begin(9600);
  pinMode(CS, OUTPUT);
  spiBegin();
  spiInit(SPI_RATE);
  
  pmc_set_writeprotect(false);
  pmc_enable_periph_clk(ID_TC7);
  TC_Configure(/* clock */TC2, /* channel */1, TC_CMR_WAVE | TC_CMR_WAVSEL_UP_RC | TC_CMR_TCCLKS_TIMER_CLOCK4); 
  TC_SetRC(TC2, 1, /* time */ 656000);
  TC_Start(TC2, 1);
  TC2->TC_CHANNEL[1].TC_IER=TC_IER_CPCS;
  TC2->TC_CHANNEL[1].TC_IDR=~TC_IER_CPCS;
  NVIC_EnableIRQ(TC7_IRQn);
}

boolean time_count = true;
unsigned long lasttime = 0;
unsigned long now = 0;

void loop()
{
  uint16_t histb[4096] {};
  uint16_t valor = 0;
  unsigned long sum = 0;
  Spi* pSpi = SPI0;
  time_count = true;
  
  while(time_count)
  {
    g_APinDescription[CS].pPort -> PIO_SODR = g_APinDescription[CS].ulPin;
    g_APinDescription[CS].pPort -> PIO_CODR = g_APinDescription[CS].ulPin;

    pSpi->SPI_TDR = 0;
    __asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
    pSpi->SPI_SR & SPI_SR_RDRF;
    valor = (pSpi->SPI_RDR)*256;

    pSpi->SPI_TDR = 0;
    __asm__("nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t""nop\n\t");
    pSpi->SPI_SR & SPI_SR_RDRF;
    valor += (pSpi->SPI_RDR);
    
    histb[(valor << 2) >> 4]++;
  }
  now = millis(); Serial.print("Time: "); Serial.println(now - lasttime); lasttime = now;  
}

void TC7_Handler()
{
  TC_GetStatus(TC2, 1);
  time_count = false;
}

static void spiBegin() {
  PIO_Configure(
    g_APinDescription[PIN_SPI_MOSI].pPort,
    g_APinDescription[PIN_SPI_MOSI].ulPinType,
    g_APinDescription[PIN_SPI_MOSI].ulPin,
    g_APinDescription[PIN_SPI_MOSI].ulPinConfiguration);
  PIO_Configure(
    g_APinDescription[PIN_SPI_MISO].pPort,
    g_APinDescription[PIN_SPI_MISO].ulPinType,
    g_APinDescription[PIN_SPI_MISO].ulPin,
    g_APinDescription[PIN_SPI_MISO].ulPinConfiguration);
  PIO_Configure(
    g_APinDescription[PIN_SPI_SCK].pPort,
    g_APinDescription[PIN_SPI_SCK].ulPinType,
    g_APinDescription[PIN_SPI_SCK].ulPin,
    g_APinDescription[PIN_SPI_SCK].ulPinConfiguration);
  pmc_enable_periph_clk(ID_SPI0);
#if USE_SAM3X_DMAC
  pmc_enable_periph_clk(ID_DMAC);
  dmac_disable();
  DMAC->DMAC_GCFG = DMAC_GCFG_ARB_CFG_FIXED;
  dmac_enable();
#if USE_SAM3X_BUS_MATRIX_FIX
  MATRIX->MATRIX_WPMR = 0x4d415400;
  MATRIX->MATRIX_MCFG[1] = 1;
  MATRIX->MATRIX_MCFG[2] = 1;
  MATRIX->MATRIX_SCFG[0] = 0x01000010;
  MATRIX->MATRIX_SCFG[1] = 0x01000010;
  MATRIX->MATRIX_SCFG[7] = 0x01000010;
#endif
#endif
}

static void spiInit(uint8_t spiRate) {
  Spi* pSpi = SPI0;
  uint8_t scbr = 255;
  if (spiRate < 14)scbr = (2 | (spiRate & 1)) << (spiRate/2);
  scbr = spiRate;
  pSpi->SPI_CR = SPI_CR_SPIDIS;
  pSpi->SPI_CR = SPI_CR_SWRST;
  pSpi->SPI_MR = SPI_PCS(SPI_CHIP_SEL) | SPI_MR_MODFDIS | SPI_MR_MSTR;
  pSpi->SPI_CSR[SPI_CHIP_SEL] = SPI_CSR_SCBR(scbr) | SPI_CSR_NCPHA;
  pSpi->SPI_CR |= SPI_CR_SPIEN;
}

I’m trying to get out of while loop but without sucess. Any clue how to do it? Thanks!

Don't get into the loop in the first place!. See "Blink without delay".

Mark

I need the loop otherwise the code will be to slow for my situation. Anyway the handler 'TC7_Handler' its working (every clock its important) but i cant break the loop with time_count (thats the problem)!

P.s.: I'm new in time handlers.

Thanks.

hugof86:
P.s.: I'm new in time handlers.

Then maybe you should take advice.

You need to declare time_count to be volatile because it is being changed in an interrupt routine.

volatile boolean time_count = true;

Pete

volatile boolean time_count = true; Thats correct. Thank you!

There's another way to do it? The fact i use the variable consume 200ns (before i have a 90 ns delay).