cancel
Showing results for 
Search instead for 
Did you mean: 

Timer interrupts in the simulator

benryves
Associate II
Posted on January 07, 2010 at 09:33

Timer interrupts in the simulator

8 REPLIES 8
benryves
Associate II
Posted on May 17, 2011 at 15:06

Do timer interrupts work in the simulator (STM8S105C6)? I'm using TIM2, which has been configured like this:

Code:

// Initialise timer 2

TIM2_DeInit();

TIM2_TimeBaseInit(TIM2_PRESCALER_1, 1023);

TIM2_ITConfig(TIM2_IT_UPDATE, ENABLE);

TIM2_Cmd(ENABLE);

enableInterrupts();

and an interrupt handler that has been set up like this:

Code:

xdef f_TIM2_UPD_OVF_IRQHandler

f_TIM2_UPD_OVF_IRQHandler:

bres TIM2_SR1, ?UIF ; Assume ? is a hash sign

iret

I've set a debug on the ''bres'' line. This breakpoint is never hit if I run the code in the simulator, but when debugging over ST-LINK the breakpoint is repeatedly hit, as you'd expect.

If I pause the simulator and step through my code, the TIM2 counter value never increases - it's stuck at 0. One thing that may be telling is that every time I start the debugger, I get the following message:

Quote:

---------------------------

ST Visual Develop

---------------------------

Cannot open file [C:\Program Files\STMicroelectronics\st_toolset\stvd\Periph.ini] for reading.

---------------------------

OK

---------------------------

This file does not, as the error pointed out, exist. I made a copy of stm8_periph.ini (which did exist) and renamed that Periph.ini, but whilst that gets rid of the simulator error message, interrupts don't appear to fire and timers don't appear to count up.

Is there some configuration error I'm missing?

baroudi
Associate II
Posted on May 17, 2011 at 15:06

Hi benryves,

Take a look in the release note of STVD.

I have found that ST7 MCUs peripherals are supported during simulation and

that STM8 peripherals are'nt.

With regards,

benryves
Associate II
Posted on May 17, 2011 at 15:06

Oh, cheers. Is there any way to develop/test/debug time-sensitive code, then? I was hoping to use the simulator to check that the correct number of cycles had elapsed between interrupt responses.

I also note that the simulator ''does not take into account possible slowdowns in the pipeline execution'' - are the effects of the pipeline execution described anywhere? Careful cycle counting produced something that worked beautifully in the simulator, but was slightly off in reality in ''Debug'' mode and completely off in ''Release'' mode..!

jimb
Associate II
Posted on May 17, 2011 at 15:06

Simulation of timers is not supported - to the best of my knowledge. I just use a dummy PCB running and powered by my USB pod - you can develop and test but no simulation.

benryves
Associate II
Posted on May 17, 2011 at 15:06

I'm using the STM8S-Discovery evaluation board, which has an integrated ST-Link and has been excellent for general debugging, but impractical for cycle-accurate timing testing (and I lack a logic analyser or oscilloscope to verify that way) - you lose the cycle counter that the simulator provides. Could there be some clever way to use a timer peripheral to provide a cycle counter?

baroudi
Associate II
Posted on May 17, 2011 at 15:06

Hi benryves,

I have written a code that blinks a pin at 1 ms using STM8S208 and should be ported to STM8S105 because GPIOG doesn't exist in STM8S105.

The example is inspired from STM8S provided examples here:

http://www.st.com/products/support/micro/files/stm8sfwlib.zip

----------- In main.c ----------------

/* Configure HSI div 1 as clock source */

/* The MCU is clocked by 16 MHz */

CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);

/* Initialize I/Os in Output Mode */

/* GPIOG should be ported to GPIOA / GPIOB ... for STM8S105 */

GPIO_Init(GPIOG, GPIO_PIN_5, GPIO_MODE_OUT_PP_LOW_FAST);

/* Time base configuration */

/* prescler 128 -> the counter is clocked by 16 MHz /128 = 125 KHz */

/* The timer overflows each (124 + 1) / 125 000 = 1 ms */

TIM4_TimeBaseInit(TIM4_PRESCALER_128, 124);

TIM4_ITConfig(TIM4_IT_UPDATE, ENABLE);

enableInterrupts();

/* Enable TIM4 */

TIM4_Cmd(ENABLE);

/* Toggles PG.5 in ISR routine*/

while (1);

----------- In stm8s_it.c ----------------

#ifdef _COSMIC_

@far @interrupt void TIM4_UPD_OVF_IRQHandler(void)

#else /* _RAISONANCE_ */

void TIM4_UPD_OVF_IRQHandler(void) interrupt 23

#endif /* _COSMIC_ */

{

/* Toggle GPIO PIN5 */

/* GPIOG should be ported to GPIOA / GPIOB ... for STM8S105 */

GPIO_WriteReverse(GPIOG, GPIO_PIN_5);

TIM4_ClearITPendingBit(TIM4_IT_UPDATE);

}

#endif /*STM8S903*/

With regards,

mchnani
Associate II
Posted on May 17, 2011 at 15:06

Hi Benryves,

I tested just now the exemple given in frame library of STM8 and the result is bad (

the time variable is remained the initial value)

. see my code and tell me it's ok or not. thank you so much  

Regards

#include ''stm8s.h''

#include ''stm8s_tim2.h''

#include ''stm8s_it.h''

u16 GTimeStamp=0;

void Init_TIM2(void)

{

TIM2_TimeBaseInit(TIM2_PRESCALER_128,124);

//TIM2_UpdateRequestConfig(TIM2_UPDATESOURCE_GLOBAL);

//TIM2_UpdateRequestConfig(TIM2_UPDATESOURCE_GLOBAL);

TIM2_ClearFlag(TIM2_FLAG_UPDATE);

TIM2_ITConfig(TIM2_IT_UPDATE,ENABLE);

enableInterrupts();

TIM2_Cmd(ENABLE);

}

/** Wait a delay

* @param[in] Delay based on timer Tick

* @return None

**/

void WaitDelay(u16 Delay)

{

GTimeStamp = 0x00;

sim();

while (GTimeStamp <= Delay);

rim();

}

void TIM2_IRQHandler (void)

{

if (TIM2_GetITStatus(TIM2_IT_UPDATE)!= RESET)

{

GTimeStamp++;

TIM2_ClearITPendingBit(TIM2_IT_UPDATE);

}

}

void main (void)

{

//GPIO-------------------------------

/* Entrées et sorties du micro*/

// Configure PF.4 en entrée flotante PRESENCE SECTEUR

GPIOF->CR1&=~0x10;

GPIOF->DDR&=~0x10;

// Configure PB.5 en entrée flotante COURANT BAES

// Configure PB.4 en entrée flotante COURANT BAEH

// Configure PB.3 en entrée flotante TELECOMMANDE

// Configure PB.2 en entrée flotante TENSION Leds Vertes

// Configure PB.1 en entrée flotante Detection presence batterie BAES

// Configure PB.0 en entrée flotante Detection presence batterie BAEH

GPIOB->CR1&=~0x3F;

GPIOB->DDR&=~0x3F;

// Configure PE.5 en sortie PushPull Pilotage Alim driver BAEH

GPIOE->DDR|= 0x20;

GPIOE->CR1|= 0x20;

// Configure PC.4 en sortie PushPull Pilotage Leds Vertes

GPIOC->DDR|= 0x10;

GPIOC->CR1|= 0x10;

// Configure PD.0 en sortie PushPull Pilotage charge batterie BAEH

// Configure PD.2 en sortie PushPull Pilotage charge batterie BAES

// Configure PD.3 en sortie PushPull Pilotage SATI Vert

// Configure PD.4 en sortie PushPull Pilotage SATI Jaune

// Configure PD.7 en sortie PushPull Pilotage Alim driver BAES

GPIOD->DDR|= 0x9D;

GPIOD->CR1|= 0x9D;

while (1) {

if(GTimeStamp <= 200)

{

GPIOD->ODR &= 0x10;

GPIOD->ODR |= 0x10;

}

else

{

GPIOD->ODR &= 0x00;

GPIOD->ODR |= 0x00;

}

 

while (1);

}

peterpfandler
Associate
Posted on May 17, 2011 at 15:06

Hi,

I use STM8S demo board, and I also need timer in my application. I use the ST Toolset (STVD and STVP) and created an Assembly project to find out more about timers. I used Timer1 for tests.

Here is what I experienced:

Without the demo board the timer registers were not initialized, they were all set to $ff. Normally they are inited to 00, except the ones @ $5262, $5263. So when you are just debugging in the Simulator, you won't be able to use the timers.

After some reading in the docs and experimenting with the demo board I found, that it is not enough to enable the interrupts in the TIM1_IER register. It only sets the interrupt flag when the timer expires. Why is this good? Because your code can execute as long as it needs after setting the timer. It won't be interrupted. When your code has finished, you can either check the Timer interrupt flag, and act accordingly, or enter into Halt mode and let the MCU call your Timer Interrupt.

I only got the interrupt working, when I set the MCU in wait state either by invoking HALT or WFI.

I don't know what you can do in C, but it works in Assembly.

An other interesting experiment with timers is the following:

1. Start debug with any project, having the STM8S demo board as target.

2. Don't let the program go, just enter into debug mode.

3. Open the memory window, and set the address to any Timer's begin address

4. Set ''Read/Write On Fly'' mode by right clicking the memory window

5. The window's background changes to light yellow.

6. Start experimenting with the timer by hand: write the Timer's registers directly, and watch the results on screen. It is good to have the prescaler set to a relative high value, so that you can follow what's going on 😉

Regards, Peter