How many CPU clocks long.. CpuGetTicks().. and nanosecs

Post your cool example code here.
Post Reply
User avatar
Pito
Posts: 1626
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

How many CPU clocks long.. CpuGetTicks().. and nanosecs

Post by Pito » Thu Apr 20, 2017 7:59 am

Code: Select all

// How long is an 1ms actually long ?? :)
// The enabling and use of DWT CPU Clock Counter
// for STM32F407 and for STM32F103 and friends
// Pito 4/2017

#define DWTEn()         (*((uint32_t*)0xE000EDFC)) |= (1<<24)
#define CpuTicksEn()    (*((uint32_t*)0xE0001000)) = 0x40000001
#define CpuTicksDis()   (*((uint32_t*)0xE0001000)) = 0x40000000
#define CpuGetTicks()   (*((uint32_t*)0xE0001004))

void setup() {
  Serial.begin(115200);
  DWTEn();
  CpuTicksEn();
}

void loop() {
  delay(100);
  uint32_t elapsed = CpuGetTicks();      // Measure the 1ms delay
  delay(1);
  elapsed = CpuGetTicks()- elapsed;      // How many CPUTicks?
  Serial.print("1 ms is ");
  // 5.9524ns for F407 and 13.8888ns for F103 at stock clock
  float nanos = 13.8888 * elapsed;        // Convert to nanoseconds
  Serial.print(nanos,3);
  Serial.println(" nanosecs long ");
}
// Maple Mini
//1 ms is 999993.625 nanosecs long
//1 ms is 999979.687 nanosecs long
//1 ms is 999910.250 nanosecs long
//1 ms is 999979.687 nanosecs long
//1 ms is 999993.625 nanosecs long
//1 ms is 999910.250 nanosecs long
//1 ms is 1000035.250 nanosecs long
Last edited by Pito on Sat Apr 29, 2017 6:03 pm, edited 6 times in total.
Pukao Hats Cleaning Services Ltd.

zmemw16
Posts: 1484
Joined: Wed Jul 08, 2015 2:09 pm
Location: St Annes, Lancs,UK

Re: How long is the delay(1) actually.. CpuGetTicks()

Post by zmemw16 » Thu Apr 20, 2017 11:57 am

how you compiling this ? uint32 is incorrect, try uint32_t.
i've tried general 407v and black f407v, unfortunately i can't seem to find any output at all on ttyusb0 uart1, ttyusb1 uart2 dumped out a load of binary 0x00.

i also had the st-link interface give errors, sometimes a reset was required execute it, sometimes it worked and a couple of times needed a reset, compile and program.

whilst it is nice, any chance of an easy disable on the st-link & serialusb/serialX fancy footwork?

unfortunately the board is a 407zgt, btw the leds are on pc0 and pd3, both working :-)

i've also worked my way through the tft connections and 34 way connector matches the 'standard'.
i've a few 3.2" touch tft ones labelled 3.2" LCD MODULE with a largish card socket and yellow 34w pin connector.
istr ili9341 chipset and never actually dupont wired them in :-)
how and if it maps onto the FMSC will be fun.
i'll put the 2 off 2x32 connector pinouts and the tft 2x17 in a csv file if anyone wants it.
then i'll have another crack at it with CubeMX, at least until the snooker starts :D

stephen

User avatar
Pito
Posts: 1626
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: How long is the delay(1) actually.. CpuGetTicks()

Post by Pito » Thu Apr 20, 2017 12:30 pm

I compiled that under Arduino IDE, STM32 Discovery F407 board.
Serial via PA9, PA10.
No USBserial in use.

Code: Select all

Sketch uses 28,376 bytes (2%) of program storage space. Maximum is 1,048,576 bytes.
Global variables use 13,544 bytes of dynamic memory.
Pukao Hats Cleaning Services Ltd.

stevestrong
Posts: 1813
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany

Re: How long is the delay(1) actually.. CpuGetTicks()

Post by stevestrong » Thu Apr 20, 2017 12:38 pm

The 0.1% error (1us) can be due to entering to and returning from the delay() function.

Oh, and the title should be "How many CPU clocks long..."

User avatar
Pito
Posts: 1626
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: How many CPU clocks long.. CpuGetTicks()

Post by Pito » Thu Apr 20, 2017 1:04 pm

Oh, and the title should be "How many CPU clocks long..."
Fixed.
Pukao Hats Cleaning Services Ltd.

stevestrong
Posts: 1813
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany

Re: How many CPU clocks long.. CpuGetTicks()

Post by stevestrong » Thu Apr 20, 2017 1:43 pm

Yes, but now you forgot the delay(1) from the title :mrgreen:

User avatar
Pito
Posts: 1626
Joined: Sat Mar 26, 2016 3:26 pm
Location: Rapa Nui

Re: How many CPU clocks long.. CpuGetTicks()

Post by Pito » Sat Apr 29, 2017 5:39 pm

Updated such it works on Maple Mini and BP as well..
Pukao Hats Cleaning Services Ltd.

Post Reply