Author Topic: systick timer on SAMC  (Read 5711 times)

0 Members and 1 Guest are viewing this topic.

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 18220
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
systick timer on SAMC
« on: January 04, 2023, 10:09:03 am »
I'm trying to implement the systick timer on a samc for general program timing. I don't see a specific interrupt line for it in the ARM interrupt controller, is it always active anyway? I see there is a dummy handler for it in the header files and it has a bit in the register to enable an "exception request".
 

Offline Rudolph Riedel

  • Regular Contributor
  • *
  • Posts: 70
  • Country: de
Re: systick timer on SAMC
« Reply #1 on: January 04, 2023, 10:31:30 am »
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 18220
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: systick timer on SAMC
« Reply #2 on: January 04, 2023, 12:28:10 pm »
right, in there "somewhere". I see some code that sets the tick interval but that is all.
 

Offline Rudolph Riedel

  • Regular Contributor
  • *
  • Posts: 70
  • Country: de
Re: systick timer on SAMC
« Reply #3 on: January 04, 2023, 12:39:16 pm »
 

Offline wek

  • Frequent Contributor
  • **
  • Posts: 561
  • Country: sk
Re: systick timer on SAMC
« Reply #4 on: January 04, 2023, 01:15:15 pm »
SysTick interrupt is permanently enabled, see ARMv7-M Architecture Reference Manual, chapter B1.5 Armv7-M exception model. [EDIT] See ataradov's post below [/EDIT]

You can enable/disable SysTick counter, though, effectively enabling/disabling the related interrupt.

[EDIT] The same applies to ARMv6-M (Cortex-M0/M0+)[/EDIT]


JW
« Last Edit: January 05, 2023, 11:48:02 pm by wek »
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 12017
  • Country: us
    • Personal site
Re: systick timer on SAMC
« Reply #5 on: January 04, 2023, 08:04:23 pm »
SysTick interrupt can be enabled independently from the timer. CSR.TICKINT enables the interrupt and CSR.ENABLE  enables the timer itself.

The SysTick interrupt handler is in the first "core" group of the interrupts. You don't need to define it (have it be dummy) if CSR.TICKINT=0. And I assume your "exception request" is TICKINT.
« Last Edit: January 04, 2023, 08:06:00 pm by ataradov »
Alex
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4413
  • Country: us
Re: systick timer on SAMC
« Reply #6 on: January 04, 2023, 09:29:36 pm »
Quote
I don't see a specific interrupt line for [SysTick] in the ARM interrupt controller
Systick is part of the ARM "core", so it doesn't go through the NVIC.  Also it frequently won't show up in vendor documentation that only documents what the "core" considers "external interrupts."  SysTick on an CM0 is always exception number 15 (and #16 is the first external interrupt.)
I guess that makes it "permanently enabled" in the sense that you don't have to turn it on (and you can't change it's priority) in the NVIC.  You just have to turn on the interrupt bit in SysTick itself.
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 12017
  • Country: us
    • Personal site
Re: systick timer on SAMC
« Reply #7 on: January 04, 2023, 09:35:15 pm »
You can change SysTick priority though SCB.SHPR3 register.
Alex
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 18220
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: systick timer on SAMC
« Reply #8 on: January 05, 2023, 03:39:49 pm »
Uh, yes I'm not done yet, the interrupt fires but then never again, naturally ARM documentation does not point you around, found this collection of registers: https://developer.arm.com/documentation/dui0497/a/cortex-m0-peripherals/system-control-block?lang=en with a Interrupt Control and State Register. I'm still confused though as this is the old manual clearing of the interrupt to stop it constantly firing logic that I am going down but I actually have the opposite, it fire once and never again.

Is there a place where I can see some of the code that the automatic code generator uses? I don't want to have to actually dable with the horrible mess that the configurator in mplabx is and I am using studio as it's less distracting but it feels like looking at examples may be more informative than trying to read a self generated manual.
 

Offline voltsandjolts

  • Supporter
  • ****
  • Posts: 2754
  • Country: gb
Re: systick timer on SAMC
« Reply #9 on: January 05, 2023, 04:48:10 pm »
Are you sure you are writing the reload register SysTick->LOAD?
I've never used SAMC but setup can't be too different from STM32...

Code: [Select]
volatile uint32_t systicks10ms;

void SysTick_Handler(void) {
systicks10ms++;
}

static inline void SuspendTick(void) {
SysTick->CTRL &= ~SysTick_CTRL_TICKINT_Msk;
}

static inline void ResumeTick(void) {
SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk;
}

void main(void) {

NVIC_SetPriority(SysTick_IRQn, TICK_INT_PRIORITY);
SysTick->LOAD = (SystemCoreClock / 100) - 1; /* set auto reload register for 10ms systick rate */
SysTick->VAL = 0UL;
SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | /* clock is HCLK, remove this for HCLK/8 */
SysTick_CTRL_TICKINT_Msk | SysTick_CTRL_ENABLE_Msk; /* Enable SysTick IRQ and SysTick Timer */

}
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 12017
  • Country: us
    • Personal site
Re: systick timer on SAMC
« Reply #10 on: January 05, 2023, 05:22:34 pm »
naturally ARM documentation does not point you around
First of all, download the PDF version so you have everything at once and a meaningful search. Arm documentation for the core only briefly mentions the registers that are implemented in this core, the actual description of the operation is in the Architecture Reference Manual (DDI0419C_arm_architecture_v6m_reference_manual.pdf), which is referenced from the main core document.

The code above is correct. You don't need to do anything special, just configure LOAD value and set TICKINT and ENABLE. Interrupt handler would be called automatically and you just run the code you need there, no need for any other maintenance, interrupt clearing or anything like that.
Alex
 

Offline wek

  • Frequent Contributor
  • **
  • Posts: 561
  • Country: sk
Re: systick timer on SAMC
« Reply #11 on: January 05, 2023, 11:45:25 pm »
SysTick interrupt can be enabled independently from the timer. CSR.TICKINT enables the interrupt and CSR.ENABLE  enables the timer itself.
I stand corrected, thanks.

Wading through ARMv6M ARM (btw.this is telling, too), I stumbled upon an interesting fact: while in ARMv7M SysTick is integral part of processor, in ARMv6M it is optional.

JW
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 18220
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: systick timer on SAMC
« Reply #12 on: January 06, 2023, 03:35:21 pm »
This is what I have, it transmits once rather than once every 350ms

Code: [Select]

#define systick_reset_value 0x00FFFFFF // 350ms value between 0x0 and 0x00FFFFFF (24 bits)


// https://developer.arm.com/documentation/dui0497/a/cortex-m0-peripherals/optional-system-timer--systick?lang=en

#define SYST_CSR ( *(uint32_t *) 0xE000E010 ) // SysTick Control and Status Register
#define SYST_RVR ( *(uint32_t *) 0xE000E014 ) // SysTick Reload Value Register
#define SYST_CVR ( *(uint32_t *) 0xE000E018 ) // SysTick Current Value Register
// #define SYST_CALIB ( *(uint32_t *) 0xE000E01C ) // SysTick Calibration Value Register (not implemented)

// https://developer.arm.com/documentation/dui0497/a/cortex-m0-peripherals/system-control-block?lang=en
#define ICSR ( *(uint32_t *) 0xE000ED04 ) // Interrupt Control and State Register

void systick_setup()
{
SYST_RVR = systick_reset_value ;
SYST_CVR = 0x0 ;
SYST_CSR = 0x1 << 2 | 0x1 << 1 | 0x1 << 0 ;
}

void SysTick_Handler()
{

sys_com_tx( 0 , 0) ;
}

 

Offline eutectique

  • Frequent Contributor
  • **
  • Posts: 489
  • Country: be
Re: systick timer on SAMC
« Reply #13 on: January 06, 2023, 03:51:12 pm »
You are transmitting from the interrupt handler. Not seeing the implementation of sys_com_tx(), I would not say that it's wrong. But it's suspicious.
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 12017
  • Country: us
    • Personal site
Re: systick timer on SAMC
« Reply #14 on: January 06, 2023, 05:00:17 pm »
The code is fine assuming vector table has SysTick_Handler in the corresponding entry.  Also, what is your core frequency? 350 ms is a strange value.

So yes, test it some other way. Toggle a pin or something.
Alex
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4413
  • Country: us
Re: systick timer on SAMC
« Reply #15 on: January 07, 2023, 08:21:04 am »
Quote
#define SYST_CSR ( *(uint32_t *) 0xE000E010 ) // SysTick Control and Status Register
Why are you doing this instead of just #including the appropriate CMSIS include file?  (Not that setting up all the -I include paths to cover both vendor and ARM definitions isn't a pain, but if an IDE doesn't do it for you, you should only have to do it once, somewhere.)


Quote
350 ms is a strange value.
350ms is about what you get when you divide a 48MHz clock (SAMC max) by the ~16e6 maximum systick divisor.  It ought to show nicely on a LED, which I guess is nice.
I've never actually used sysTick with the maximum divisor, but I can't imagine that it would have a bug there.


Quote
in ARMv6M [sysTick] is optional.
I haven't ever seen an implementation without it...

 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 12017
  • Country: us
    • Personal site
Re: systick timer on SAMC
« Reply #16 on: January 07, 2023, 08:28:05 am »
SysTick is clocked directly by the core clock, there is no configurable divisor. Misinterpreted your sentence about the divider.

But yes, I originally miscalculated. (1<<24) / 48e6 is 350 ms, so this makes sense.

It would work with the maximum value, I use that all the time to implement a full system time. Just let the timer overflow and in the interrupt increment remaining bits. When you need to take the time, combine the current value of the counter with the incrementing variable. This way you get very granular system time for cheap.
« Last Edit: January 07, 2023, 08:33:03 am by ataradov »
Alex
 

Offline wek

  • Frequent Contributor
  • **
  • Posts: 561
  • Country: sk
Re: systick timer on SAMC
« Reply #17 on: January 07, 2023, 08:40:40 am »
Quote
Quote
in ARMv6M [sysTick] is optional.
I haven't ever seen an implementation without it...
FPU in CM4 is optional, too. I believe all initial implementations did include it, and only later appeared implementations which chose to omit it (I may be wrong and it may be only me not being aware of the early FPU-less implementations). At any case, it raised enough confusion so that ARM and some (most?) implementers rebadged the FPU-containing CM4 as CM4F.

OTOH, FPU is probably quite a chunk of silicon while SysTick is probably miniscule in size, so it probably doesn't make any sense to omit it. I don't see any reason for it to be optional, but then I am not a decision maker in ARM.

JW
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 12017
  • Country: us
    • Personal site
Re: systick timer on SAMC
« Reply #18 on: January 07, 2023, 08:48:07 am »
It is probably optional because the first core using ARMv6-M was Cortex-M1 targeting FPGAs. And there SysTick may be costly and not necessary. Plus it is far easier to have options in the FPGAs.
« Last Edit: January 07, 2023, 08:50:05 am by ataradov »
Alex
 
The following users thanked this post: wek

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 18220
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: systick timer on SAMC
« Reply #19 on: January 08, 2023, 08:31:39 am »
I'll set the pin up as a digital output tomorrow and toggle it.
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 18220
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: systick timer on SAMC
« Reply #20 on: January 09, 2023, 09:36:27 am »
OK that works, i have problems somewhere else.
 

Offline sslupsky

  • Contributor
  • Posts: 18
  • Country: ca
Re: systick timer on SAMC
« Reply #21 on: January 11, 2023, 10:11:11 pm »
There is some tricky stuff you need to take care of when the systick wraps.  There are some drivers in the zephyr project repo that might be helpful reference:
https://github.com/zephyrproject-rtos/zephyr/blob/main/drivers/timer/cortex_m_systick.c
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4413
  • Country: us
Re: systick timer on SAMC
« Reply #22 on: January 12, 2023, 09:43:41 am »
Quote
There is some tricky stuff you need to take care of when the systick wraps.  There are some drivers in the zephyr project repo that might be helpful reference
Well, that's the most complex systick code I've ever seen.  And it wasn't helpful at all, because It doesn't explain why it's doing things the way it IS.

I *think* their idea is to run sysTick with varying counts, so as to enable sleeping between "ticks" or something.  That's not the USUAL way to use sysTick.  (although to be fair, having the system wake up every millisecond or so just to increment a tick counter is probably not a great way to achieve low-power devices.)
 

Offline sslupsky

  • Contributor
  • Posts: 18
  • Country: ca
Re: systick timer on SAMC
« Reply #23 on: January 13, 2023, 04:43:53 am »
Well, that's the most complex systick code I've ever seen.  And it wasn't helpful at all, because It doesn't explain why it's doing things the way it IS.

I *think* their idea is to run sysTick with varying counts, so as to enable sleeping between "ticks" or something.  That's not the USUAL way to use sysTick.  (although to be fair, having the system wake up every millisecond or so just to increment a tick counter is probably not a great way to achieve low-power devices.)

Yes, Zephyr can be configured to use a tick or to be "tickless".

The clock API's are documented here:  https://docs.zephyrproject.org/latest/doxygen/html/group__clock__apis.html

Which provides a bit more explanation about what this driver is supposed to do.

Unfortunately, the Systick in the Cortex M0 is not very power efficient.  For SAMD you can achieve quite low power operation using the RTC to generate the system timer.
« Last Edit: January 13, 2023, 04:48:12 am by sslupsky »
 

Offline cv007

  • Frequent Contributor
  • **
  • Posts: 892
Re: systick timer on SAMC
« Reply #24 on: January 13, 2023, 12:49:22 pm »
Quote
For SAMD you can achieve quite low power operation using the RTC to generate the system timer.
A low power timer (32k clock source) is a good alternative in any case as you can keep time moving through low power modes, plus you get a single clock speed running the time that doesn't change or depend on a 'ticks per second' define (as is used in the zephyr project which means your time is based on one cpu speed that cannot change).

For a sam with a rtc in 32bit count mode, you get ~36 hours to work with at highest resolution (~30us, plenty good for general use), and by also keeping track of overflows you get a system time that never rolls over.


//using a time stamp, manually checking time
led.on(); //on for 10 seconds
auto t = now() + 10_sec;
while( now() < t ){}
led.off();

//blocking wait in low power mode, default low power mode is standby, uses rtc compare to wake
while( true ){ wait(1_sec); led.toggle(); }

//wait random time between 100ms/2sec
while( true ){ wait(100_ms, 2_sec); led.toggle(); }

The above code works the same when using an avr0/1 series for example with its rtc peripheral (16bit). The details are a little different like available low power modes and the timer width, but the app code will look the same. There is usually a low power timer in the modern mcu that is typically unused.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf