How to find the execution time in stm32f3xx? - c

#include "main.h"
#include "stm32f3xx_hal.h"
#include<time.h>
TIM_HandleTypeDef htim2
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_TIM2_Init(void);
long int count1,count2,count,i=5;
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_TIM2_Init();
HAL_TIM_Base_Init(&htim2);
HAL_TIM_Base_Start(&htim2);
count1= __HAL_TIM_GET_COUNTER(&htim2);
while (i)
{
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_3,GPIO_PIN_SET);
HAL_Delay(50000);
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_3,GPIO_PIN_RESET);
HAL_Delay(10000);
i--;
}
count=count2-count1;
count2= __HAL_TIM_GET_COUNTER(&htim2);
}
The code output is always 0. I am unable to obtain the count values. can anyone tell me why it is not executing? I am using STM32F303k8 microcontroller. The count values are always zero, even though it takes few minutes to execute completely!!
Thanks in advance!

This:
count=count2-count1;
count2= __HAL_TIM_GET_COUNTER(&htim2);
makes no sense at all, you're subtracting from count2 before refreshing it from the timer?
It should perhaps be:
const uint32_t now = __HAL_TIM_GET_COUNTER(&htim2);
count += now - last;
last = now;
With uint32_t last = __HAL_TIM_GET_COUNTER(&htim2); before the loop.

Newer ARM Cortex-M3/4/7 devices provide a register called CYCLECOUNTER, which can be often inspected in a debugger even without using any additional timer/counter and without adding any instrumentation to the code. The technique is described for example in the IAR AppNote "How to measure execution time with CYCLECOUNTER" at:
https://www.iar.com/support/resources/articles/how-to-measure-execution-time-with-cyclecounter/

Related

How can you get a random number on GBDK?

I'm new to C and to GBDK and I want to code a random number generator that decides between 0 and 1.
Like a 'hacker' simulator.
I have tried a lot of examples from the Internet. But none worked.
Screenshot from the output of the last attempt I made: https://i.ibb.co/f8G39vX/bgberrors.png
Last attempt code:
#include <gb/gb.h>
#include <stdio.h>
#include <rand.h>
void init();
void main()
{
init();
while(1)
{
UINT8 r = ((UINT8)rand()) % (UINT8)4;
printf(r);
}
}
void init()
{
DISPLAY_ON;
}
How can I accomplish it?
#include <gb/gb.h>
#include <stdint.h>
#include <stdio.h>
#include <rand.h>
void init();
void main()
{
init();
printf(" \n\n\n\n\n\n\n\n PRESS START!\n");
// abuse user input for seed generation
waitpad(J_START);
uint16_t seed = LY_REG;
seed |= (uint16_t)DIV_REG << 8;
initrand(seed);
while(1)
{
UINT8 r = ((UINT8)rand()) % (UINT8)2;
printf("%d", r);
}
}
void init()
{
DISPLAY_ON;
}
Tested with GBDK-2020 4.0.3
Also check the "rand" example in GBDK-2020.
Regarding the comments:
Yes, GBDK has it's own lib (including stdlib). It's probably a fork of SDCC's lib 20 years ago. Current SDCC has rand() in stdlib.h, but GBDK-2020 doesn't. Max is 0xFF, I don't know of a define for that.
Float should be avoided as much as possible, it's completely done in software, there is no hardware support for this. Double isn't really supported by the compiler and falls back to float.
There are no man pages, documentation is available here: https://gbdk-2020.github.io/gbdk-2020/docs/api/rand_8h.html or read the gbdk_manual.pdf comming with gbdk-2020

uC/OS hardware register read

I am trying to use the low power timer on the Freescale FRDM K64F, with IAR as design framework and uC/OS as operating system. I read the manual and I found that the timer is controlled by means of a bunch of registers, but I am getting in trouble with the register reading and writing.
At first I am just trying to read a register with the code attached below. The code stucks as soon as the read line is reached: the line “after read” is not printed on terminal. Searching through the debugger, I found out that the hardfault_handler exception is raised. I am not a software expert, so: does anyone have an idea about what is the problem here? It should be something related with the OS, but I can’t understand what. I said I am not a software expert, so, should I have forgot to tell something important, please let me know. Thanks in advance.
#include "fsl_interrupt_manager.h"
#include <math.h>
#include <lib_math.h>
#include <cpu_core.h>
#include <app_cfg.h>
#include <os.h>
#include <fsl_os_abstraction.h>
#include <system_MK64F12.h>
#include <board.h>
#include <bsp_ser.h>
/*
*********************************************************************************************************
* LOCAL DEFINES
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL GLOBAL VARIABLES
*********************************************************************************************************
*/
static OS_TCB AppTaskStartTCB;
static CPU_STK AppTaskStartStk[APP_CFG_TASK_START_STK_SIZE];
/*
*********************************************************************************************************
* LOCAL FUNCTION PROTOTYPES
*********************************************************************************************************
*/
static void AppTaskStart (void *p_arg);
int main (void)
{
OS_ERR err;
#if (CPU_CFG_NAME_EN == DEF_ENABLED)
CPU_ERR cpu_err;
#endif
hardware_init();
GPIO_DRV_Init(switchPins, ledPins);
#if (CPU_CFG_NAME_EN == DEF_ENABLED)
CPU_NameSet((CPU_CHAR *)"MK64FN1M0VMD12",
(CPU_ERR *)&cpu_err);
#endif
OSA_Init(); /* Init uC/OS-III. */
OSTaskCreate(&AppTaskStartTCB, /* Create the start task */
"App Task Start",
AppTaskStart,
0u,
APP_CFG_TASK_START_PRIO,
&AppTaskStartStk[0u],
(APP_CFG_TASK_START_STK_SIZE / 10u),
APP_CFG_TASK_START_STK_SIZE,
0u,
0u,
0u,
(OS_OPT_TASK_STK_CHK | OS_OPT_TASK_STK_CLR | OS_OPT_TASK_SAVE_FP),
&err);
OSA_Start(); /* Start multitasking (i.e. give control to uC/OS-III). */
while (DEF_ON) { /* Should Never Get Here */
;
}
}
static void AppTaskStart (void *p_arg)
{
OS_ERR os_err;
(void)p_arg; /* See Note #1 */
char string[800];
CPU_Init(); /* Initialize the uC/CPU Services. */
Mem_Init(); /* Initialize the Memory Management Module */
Math_Init(); /* Initialize the Mathematical Module */
int * PSR = (int *) 0x42040004;
BSP_Ser_Init(115200u);
APP_TRACE_DBG(("Blinking RGB LED...\n\r"));
int value;
APP_TRACE_DBG(("Before read\n"));
value=*PSR;
APP_TRACE_DBG(("After read\n"));
APP_TRACE_DBG(("Before sprintf\n"));
sprintf(string,"Value is %d\n",value);
APP_TRACE_DBG(("After sprintf\n"));
}

Waiting in DOS using djgpp -- alternatives to busy wait?

I recently wrote a little curses game and as all it needs to work is some timer mechanism and a curses implementation, the idea to try building it for DOS comes kind of naturally. Curses is provided by pdcurses for DOS.
Timing is already different between POSIX and Win32, so I have defined this interface:
#ifndef CSNAKE_TICKER_H
#define CSNAKE_TICKER_H
void ticker_init(void);
void ticker_done(void);
void ticker_start(int msec);
void ticker_stop(void);
void ticker_wait(void);
#endif
The game calls ticker_init() and ticker_done() once, ticker_start() with a millisecond interval as soon as it needs ticks and ticker_wait() in its main loop to wait for the next tick.
Using the same implementation on DOS as the one for POSIX platforms, using setitimer(), didn't work. One reason was that the C lib coming with djgpp doesn't implement waitsig(). So I created a new implementation of my interface for DOS:
#undef __STRICT_ANSI__
#include <time.h>
uclock_t tick;
uclock_t nextTick;
uclock_t tickTime;
void
ticker_init(void)
{
}
void
ticker_done(void)
{
}
void
ticker_start(int msec)
{
tickTime = msec * UCLOCKS_PER_SEC / 1000;
tick = uclock();
nextTick = tick + tickTime;
}
void
ticker_stop()
{
}
void
ticker_wait(void)
{
while ((tick = uclock()) < nextTick);
nextTick = tick + tickTime;
}
This works like a charm in dosbox (I don't have a real DOS system right now). But my concern is: Is busy waiting really the best I can do on this platform? I'd like to have a solution allowing the CPU to at least save some energy.
For reference, here's the whole source.
Ok, I think I can finally answer my own question (thanks Wyzard for the helpful comment!)
The obvious solution, as there doesn't seem any library call doing this, is putting a hlt in inline assembly. Unfortunately, this crashed my program. Looking for the reason, it is because the default dpmi server used runs the program in ring 3 ... hlt is reserved to ring 0. So to use it, you have to modify the loader stub to load a dpmi server running your program in ring 0. See later.
Browsing through the docs, I came across __dpmi_yield(). If we are running in a multitasking environment (Win 3.x or 9x ...), there will already be a dpmi server provided by the operating system, and of course, in that case we want to give up our time slice while waiting instead of trying the privileged hlt.
So, putting it all together, the source for DOS now looks like this:
#undef __STRICT_ANSI__
#include <time.h>
#include <dpmi.h>
#include <errno.h>
static uclock_t nextTick;
static uclock_t tickTime;
static int haveYield;
void
ticker_init(void)
{
errno = 0;
__dpmi_yield();
haveYield = errno ? 0 : 1;
}
void
ticker_done(void)
{
}
void
ticker_start(int msec)
{
tickTime = msec * UCLOCKS_PER_SEC / 1000;
nextTick = uclock() + tickTime;
}
void
ticker_stop()
{
}
void
ticker_wait(void)
{
if (haveYield)
{
while (uclock() < nextTick) __dpmi_yield();
}
else
{
while (uclock() < nextTick) __asm__ volatile ("hlt");
}
nextTick += tickTime;
}
In order for this to work on plain DOS, the loader stub in the compiled executable must be modified like this:
<path to>/stubedit bin/csnake.exe dpmi=CWSDPR0.EXE
CWSDPR0.EXE is a dpmi server running all code in ring 0.
Still to test is whether yielding will mess with the timing when running under win 3.x / 9x. Maybe the time slices are too long, will have to check that. Update: It works great in Windows 95 with this code above.
The usage of the hlt instruction breaks compatibility with dosbox 0.74 in a weird way .. the program seems to hang forever when trying to do a blocking getch() through PDcurses. This doesn't happen however on a real MS-DOS 6.22 in virtualbox. Update: This is a bug in dosbox 0.74 that is fixed in the current SVN tree.
Given those findings, I assume this is the best way to wait "nicely" in a DOS program.
Update: It's possible to do even better by checking all available methods and picking the best one. I found a DOS idle call that should be considered as well. The strategy:
If yield is supported, use this (we are running in a multitasking environment)
If idle is supported, use this. Optionally, if we're in ring-0, do a hlt each time before calling idle, because idle is documented to return immediately when no other program is ready to run.
Otherwise, in ring-0 just use plain hlt instructions.
Busy-waiting as a last resort.
Here's a little example program (DJGPP) that tests for all possibilities:
#include <stdio.h>
#include <dpmi.h>
#include <errno.h>
static unsigned int ring;
static int
haveDosidle(void)
{
__dpmi_regs regs;
regs.x.ax = 0x1680;
__dpmi_int(0x28, &regs);
return regs.h.al ? 0 : 1;
}
int main()
{
puts("checking idle methods:");
fputs("yield (int 0x2f 0x1680): ", stdout);
errno = 0;
__dpmi_yield();
if (errno)
{
puts("not supported.");
}
else
{
puts("supported.");
}
fputs("idle (int 0x28 0x1680): ", stdout);
if (!haveDosidle())
{
puts("not supported.");
}
else
{
puts("supported.");
}
fputs("ring-0 HLT instruction: ", stdout);
__asm__ ("mov %%cs, %0\n\t"
"and $3, %0" : "=r" (ring));
if (ring)
{
printf("not supported. (running in ring-%u)\n", ring);
}
else
{
puts("supported. (running in ring-0)");
}
}
The code in my github repo reflects the changes.

Making a delay in C with 20MHz crystal(beginner level)

I've recently started with C and Im trying to figure out how to make a 10ms delay for
PIC16F884. From the formulas in the datasheet I've managed to create the following:
fosc = 20MHz and Toscx4 = 200ns
If I put a preset to 100 => t=100x200ns=20us and to get a 10 ms delay
10ms/20us = 500
Will the following code in C give me what Im looking for (10ms delay)? Assume I have all the initiating code and variables.
void interrupt ISR(void){
if(TMR0IF){
TMR0IF=0;
counter++;
}
if(counter==100){
delay++;
counter=0;
}
}
int main(void){
TMR0=155;
if(delay>4){
//any code
delay=0;
}
}
Might be a bad example but hopefully you understand
No, since the code in main() never actually waits, it won't implement a delay.
Assuming the interrupt and timer logic is properly set up, you're still going to need to loop:
delay = 0;
TMR0 = 155; /* Start timer. */
while(delay < 4)
; /* Do nothing */
/* More code here, delay has expired */
Also, remember to make delay a volatile variable since you're accessing it from multiple parallel threads of execution.
If you're a really beginner and want a tool that can help you calculate timer settings I advise you to look at this link :
http://www.mikroe.com/timer-calculator/
It generate the ccode for settiing properly a lot of different microcontroller

hrtimer repeating task in the Linux kernel

My goal is to create a recurring task in the linux kernel using the hrtimer struct. I would like it to recur every 500 ms.
However, I'm a little confused about how hrtimer works in the linux kernel (see linux/hrtimer.h). I know that the time is specified, and the callback should return either HRTIMER_RESTART or HRTIMER_NORESTART. I've found some sources online that state that the timer needs to be reset in the callback using the hrtimer_forward method. However, the sources I've seen are a little unclear on how adding the time works. Here's the code I have so far:
static struct hrtimer timer;
static enum hrtimer_restart timer_callback(struct hrtimer *timer)
{
printk(KERN_ERR "Callback\n");
//I know something needs to go here to reset the timer
return HRTIMER_RESTART;
}
static int init_timer(void)
{
ktime_t ktime;
unsigned long delay_in_ms = 500L;
printk(KERN_ERR "Timer being set up\n");
ktime = ktime_set(0,delay_in_ms*1E6L);
hrtimer_init(&timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
timer.function = &timer_callback;
printk(KERN_ERR "Timer starting to fire\n");
printk(KERN_ERR "in %ldms %ld\n", delay_in_ms, jiffies);
hrtimer_start(&timer, ktime, HRTIMER_MODE_REL);
return 0;
}
static void clean_load_balancing_timer(void)
{
int cancelled = hrtimer_cancel(&timer);
if (cancelled)
printk(KERN_ERR "Timer still running\n");
else
printk(KERN_ERR "Timer cancelled\n");
}
Can someone explain exactly how resetting the timer would work in the callback function? Thanks!
If you look in kernel/sched.c around line 170 in the function sched_rt_period_timer, you will see an example usage. The essential lines are
now = hrtimer_cb_get_time(timer);
overrun = hrtimer_forward(timer, now, rt_b->rt_period);
Now get's the timer's current time as a ktime_t and rt_b->rt_period is another ktime_t specifying the period at which to advance timer. The expiration time of the hrtimer will be continuously incremented by the period until it is greater than the current time. If it took more than one addition of the period to get the expiration time greater than the current time, the return value will greater than 1 (indicating more overrruns). It can be zero, if the timer expire didn't get advanced at all.
Reference: http://lwn.net/Articles/167897/
The API it uses is from a different version of the kernel so some of the arguments have changed. The basic idea is still the same.
Below is the simple solution,
#include <linux/slab.h>
#include <linux/time.h>
#include <asm/string.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/hrtimer.h>
#include <linux/ktime.h>
#define NSEC_PER_MSEC 1000000L
static struct hrtimer hr_timer;
enum hrtimer_restart enHRTimer=HRTIMER_NORESTART;
s64 i64TimeInNsec = 500 * NSEC_PER_MSEC;
enum hrtimer_restart my_hrtimer_callback( struct hrtimer *timer )
{
hrtimer_forward(timer,hrtimer_cb_get_time(timer),ktime_set(0,i64TimeInNsec));
return enHRTimer;
}
void hrtimer_event_init_module(void)
{
ktime_t kt;
enHRTimer = HRTIMER_RESTART;
//HRT init
kt = ktime_set(0, i64TimeInNsec);
hrtimer_init( &hr_timer, CLOCK_REALTIME, HRTIMER_MODE_ABS);
hrtimer_set_expires(&hr_timer, kt);
hr_timer.function = &my_hrtimer_callback;
hrtimer_start( &hr_timer, kt, HRTIMER_MODE_ABS);
}
void hrtimer_event_cleanup_module( void )
{
//Reset
hrtimer_cancel(&hr_timer);
enHRTimer = HRTIMER_NORESTART;
printk("HR-Timer module uninstalized\n");
}

Resources