Forum Menu




 


Log in Problems?
New User? Sign Up!
AVR Freaks Forum Index

Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Author Message
quadral
PostPosted: Dec 03, 2011 - 10:52 PM
Newbie


Joined: Nov 24, 2011
Posts: 14


Hello,

In those days, I started my first project on Xmega 128A1 in the simulation environment of AVR Studio 5.0.
My first project is a timer test - 1ms timer. Here I found my first problem Crying or Very sad - it's not working.
Code:
#include <avr/io.h>
#include <avr/interrupt.h>

ISR(TCC0_OVF_vect)
{
  static int g_breakpoint = 0;
  TCC0.CNT = 0;
  ++g_breakpoint;
  asm volatile("reti");
}

void init_timer()
{
   TCC0.PER = 125;
   TCC0.CTRLA = TC_CLKSEL_DIV256_gc;
   TCC0.INTCTRLA = TC_OVFINTLVL_LO_gc;
   
   /* Waveform Generation Mode */
   //TCC0.CTRLB = TC_WGMODE_SS_gc; // == single slope PWM
}

void init_32MhzClock(void)
{
  OSC.CTRL |= OSC_RC32MEN_bm;
  while( ! ( OSC.STATUS & OSC_RC32MRDY_bm ) );
  CCP = CCP_IOREG_gc;
  CLK.CTRL = ( CLK.CTRL & ~CLK_SCLKSEL_gm ) | CLK_SCLKSEL_RC32M_gc;
}

void init_pmic(void)
{
   PMIC.CTRL |= PMIC_LOLVLEN_bm;
}

int main(void)
{
    init_32MhzClock();
    init_pmic();
    init_timer();
     sei();
   
    while(1)
    {
        asm volatile("nop");
    }
      return 0;
}

If I uncomment the following line
Code:
TCC0.CTRLB = TC_WGMODE_SS_gc;

then it's working. But why? I don't want use PWM timer but normal mode timer.

And why I have to reset TCC0.CNT = 0 inside timer interrupt handler? All other examples I found never do it.

Thanks for responses
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Dec 04, 2011 - 11:24 AM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62209
Location: (using avr-gcc in) Finchingfield, Essex, England

Quote:


ISR(TCC0_OVF_vect)
{
static int g_breakpoint = 0;
TCC0.CNT = 0;
++g_breakpoint;
asm volatile("reti");
}

What on earth is that RETI doing in there?!? As the routine is not "naked" and you are doing nothing to rebalance that stack that is going to completely futz things!

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
quadral
PostPosted: Dec 04, 2011 - 04:52 PM
Newbie


Joined: Nov 24, 2011
Posts: 14


Ooops, you have fully right, my fault Sad
But even if I remove this line (with 'reti'), the timer still does not work properly!?
 
 View user's profile Send private message  
Reply with quote Back to top
DocJC
PostPosted: Dec 04, 2011 - 05:48 PM
Raving lunatic


Joined: Dec 11, 2007
Posts: 6841
Location: Cleveland, OH

Well, this little program Flashes 3 LEDs.

One LED is flashed using the (roughly timed) Waitms() instruction within the Main Loop.
One LED is flashed using a Timer/Counter Overflow interrupt.
One LED is flashed using a Timer/Counter in Capture/Compare Mode.

Its written in Basic, (Bascom), but the Registers and concepts are the same.

(It can also be used to evaluate the various Xmega interrupt priorities...)

Code:

'File Name: XPlainLEDISR V1.bas
'JEC Feb. 2, 2011
'Language: Bascom-AVR

'This program does a general initialization of the AVR XPlain Xmega Demo PCB.
'This program flashes three LEDs on the XPlain Demo board.
'It demonstrates two different ISR implementations, and setting up the Xmega
'for Timer/Counter Interrupts.

'The Main Loop uses a delay (Waitms) to flash one LED at roughly 1 Hz.
'The uC can not run other code, (except interrupts), while running a Waitms
'delay, and this instruction is only an approximate timer.

'This program flashes a second LED using the Overflow interrupt on a
'Timer/Counter.  Using the hardware to count clock pulses gives a consistent
'flash rate, but the Overflow interrupt has rather poor resolution for
'specifying the exact timing, (frequency), desired.

'This program flashes a third LED using the Compare Mode interrupt on a
'Timer/Counter.  This allows one to set the interrupt rate with much greater
'resolution in the timing measurements.
'Direct control of the Timer/Counter registers is used to implement this.

'...............................................................................
'Hardware Setup and Notes:
'Xplain uses an: ATXmega128A1-AU.
'There is also an AT90USB1287-16MU on the board.
'It is for programming the Xmega and providing a USB interface.
'Xmega always runs at 2 MHz on startup.
'The XPlain uses either a PDI programmer or a JTAG programmer.
'The Xplain has a 10-Pin header for the PDI programming interface.
'I have a custom 10-Pin to 6-Pin XPlain programming cable for the STK600 programmer.

'XPlain Hardware:

'Port A:
'Port A To Header for User I/O, (Analog Port)
'PortA.0
'PortA.1
'PortA.2
'PortA.3
'PortA.4
'PortA.5
'PortA.6
'PortA.7

'Port B:
'Port B, (Analog Port)
'PortB.0    NTC Sensor Input
'PortB.1    Pot Input, (~ 0.0 - 1.5V)
'PortB.2    Audio Output, (L), DACB0
'PortB.3    Audio Output, (R), DACB1 (? if really tied to amp)

'Port C:
'Port C, (8MB DataFlash Memory, SPI bus, USB interface)
'PortC.0    SDA
'PortC.1    SCL,  DF CS\
'PortC.2    RXD0, USB UART TxD
'PortC.3    TXD0, USB UART RxD
'PortC.4    USB SS
'PortC.5    MOSI, DF, USB
'PortC.6    MISO, DF, USB
'PortC.7    SCK,  DF, USB

'Port D:
'Port D To Header for User I/O, (Digital Port)
'PortD.0
'PortD.1
'PortD.2
'PortD.3
'PortD.4
'PortD.5
'PortD.6
'PortD.7

'Port E:
'Port E, 8 LEDs, Active Low, (i.e. 0=On, 1=Off)
'PortE.0    LED 0
'PortE.1    LED 1
'PortE.2    LED 2
'PortE.3    LED 3
'PortE.4    LED 4
'PortE.5    LED 5
'PortE.6    LED 6
'PortE.7    LED 7

'Port F:
'Port F, 8 Push Button Switches, Pushed = Pulled Low.  Use Int Pull-Up resistors
'PortF.0    PBSW 0
'PortF.1    PBSW 1
'PortF.2    PBSW 2
'PortF.3    PBSW 3
'PortF.4    PBSW 4
'PortF.5    PBSW 5
'PortF.6    PBSW 6
'PortF.7    PBSW 7

'PortQ.0    RTC 32.768 kHz External Xtal, TOSC1
'PortQ.1    RTC 32.768 kHz External Xtal, TOSC2
'PortQ.2    NTC Enable, (Active LOW)
'PortQ.3    Audio Amplifier chip Enable.  High = Enabled.

'External 8MB SDRAM using EBI Bus
'PortH
'PortJ
'PortK

'Port R:
'PortR.0    Ext Xtal, Not installed.
'PortR.1    Ext Xtal, Not installed.

'-----------------------------------------------------------------------------------------

$regfile = "xm128a1def.dat"                                 'Specify the microcontroller
$crystal = 32000000                                         'Xmega 2MHz default Start Up Clock Freq
                                                             'But will actually run prog at 32 MHz
$hwstack = 64                                               'Define the hardware stack
$swstack = 40                                               'Define the SW stack
$framesize = 40                                             'Define the frame space

'Add Bascom's Xmega specific definitions:
$lib "xmega.lib"
$external _xmegafix_clear
$external _xmegafix_rol_r1014

   Waitms 6                                                 'Startup Stabilization Delay
                                                             'Clk is 2 MHz, not 32 MHz, 6*16 = 96 mSec

   'Xplain LEDs on Port E:
   'Now Configure the Port's Pins for Input or Output mode.
   Porte_pin0ctrl = &B00000000                              'Control Register, Mode: Totem Pole Output
   Porte_pin1ctrl = &B00000000                              'Control Register, Mode: Totem Pole Output
   Porte_pin2ctrl = &B00000000                              'Control Register, Mode: Totem Pole Output
   Porte_pin3ctrl = &B00000000                              'Control Register, Mode: Totem Pole Output
   Porte_pin4ctrl = &B00000000                              'Control Register, Mode: Totem Pole Output
   Porte_pin5ctrl = &B00000000                              'Control Register, Mode: Totem Pole Output
   Porte_pin6ctrl = &B00000000                              'Control Register, Mode: Totem Pole Output
   Porte_pin7ctrl = &B00000000                              'Control Register, Mode: Totem Pole Output

   Porte_dir = 255                                          'Direction Register: All Outputs for LEDs

   Porte_out = 255                                          'Initialize LEDs Off, (High = Off)

   'Configure Xplain Push-Button Switches for Input, on PortF.
   'Do ENABLE the Internal Pull-Up Resistors.
   Portf_pin0ctrl = 24                                      '24 d = 0001 1000 Totum with Pull Ups Active
   Portf_pin1ctrl = 24                                      '24 d = 0001 1000 Totum with Pull Ups Active
   Portf_pin2ctrl = 24                                      '24 d = 0001 1000 Totum with Pull Ups Active
   Portf_pin3ctrl = 24                                      '24 d = 0001 1000 Totum with Pull Ups Active
   Portf_pin4ctrl = 24                                      '24 d = 0001 1000 Totum with Pull Ups Active
   Portf_pin5ctrl = 24                                      '24 d = 0001 1000 Totum with Pull Ups Active
   Portf_pin6ctrl = 24                                      '24 d = 0001 1000 Totum with Pull Ups Active
   Portf_pin7ctrl = 24                                      '24 d = 0001 1000 Totum with Pull Ups Active

   Portf_dir.0 = 0                                          'Direction Register, Mode: Input.
   Portf_dir.1 = 0                                          'Or just use a simple PortF_dir = 0
   Portf_dir.2 = 0                                          'for defining the entire Port in one line.
   Portf_dir.3 = 0
   Portf_dir.4 = 0
   Portf_dir.5 = 0
   Portf_dir.6 = 0
   Portf_dir.7 = 0


   'Config XMega PortD.0 as an Output for Scope Signal / Trigger:
   Portd_pin0ctrl = &B00000000                              'Control Register, Mode: Totem Pole Output
   Portd_dir = 1                                            'Direction Register, Output for Scope Signal
   Portd_out.0 = 0                                          'Init low


   'Define Global Variables:
   Dim Lpcnt As Byte                                        'Loop Counter
   Dim Regdata As Byte                                      'Register Value, Full Byte
   Dim Rvbit As Bit                                         'Register Value, Bit type
   Dim Ttcnt As Word                                        'Overflow ISR TickTock    Counter
   Dim Ttcnt2 As Word                                       'Compare  ISR TickTock #2 Counter

   Led0 Alias Porte.0                                       'Yellow LED, Low = On
   Led1 Alias Porte.1
   Led2 Alias Porte.2
   Led3 Alias Porte.3
   Led4 Alias Porte.4
   Led5 Alias Porte.5
   Led6 Alias Porte.6
   Led7 Alias Porte.7

   Pbsw0 Alias Portf_in.0                                   'Read Port PB Sw 1
   Pbsw1 Alias Portf_in.1                                   'Read Port PB Sw 2
   Pbsw2 Alias Portf_in.2                                   'Read Port PB Sw 3
   Pbsw3 Alias Portf_in.3                                   'Read Port PB Sw 4
   Pbsw4 Alias Portf_in.4                                   'Read Port PB Sw 1
   Pbsw5 Alias Portf_in.5                                   'Read Port PB Sw 2
   Pbsw6 Alias Portf_in.6                                   'Read Port PB Sw 3
   Pbsw7 Alias Portf_in.7                                   'Read Port PB Sw 4

   'Config Timer/Counter PortC1 for Overflow Interrupt.
   'Although this interrupt occurs at a fixed rate, it has poor resolution for
   'exact timing.  It is used to flash an LED at almost 1 / Sec.
   'This high level format isn't supported in Bascom: Config Tcc0_ovf , Prescale = 1024  Not Supported.
   'Therefore set the registers to configure this.
   'Clk = 32 MHz, Prescaler Div by 8, 16-Bit Timer/Counter = 65535 (65536)
   'Overflow gives 61.035 Intr / Sec, 1 Intr q 16.384 mSec
   'Manually set up T/CC1:
   'Subroutine TickTock runs on each interrupt.
   Tcc1_ctrla = 4                                           'PortC T/C 1, Source = Clock, Div by 8
   Tcc1_ctrlb = 0                                           'Normal Mode, Update on Top
   Tcc1_ctrlc = 0                                           'Waveform
   Tcc1_ctrld = 0                                           'Event System
   Tcc1_ctrle = 0                                           '8-Bit Mode
   Tcc1_intctrla = 2                                        'Overflow, Med Priority
   Tcc1_intctrlb = 0                                        'Compare
   Tcc1_intflags = 0                                        'Overflow Flag in bit 0
   On Tcc1_ovf Ticktock                                     'Heartbeat
   Enable Tcc1_ovf , Med                                    'PortC Timer/Counter #1

   'Config Timer/Counter PortC0 for Capture/Compare Mode, q 1 mSec Interrupt.
   'Use this for PB Switch debouncing.
   'Set this up as a Low Priority Interrupt.
   'Could redo this to Intr q 2, 4, or 5 mSec, using less uC time...
   'But I wanted to see this work at 1 / mSec...
   'Clk = 32 MHz, Prescaler Div by 8, 16-Bit Timer/Counter = 65535 (65536)
   'Using Overflow gives 61.035 Intr / Sec, 1 Intr q 16.384 mSec
   'Using Capture / Compare gives exactly 1 mSec per Interrupt, as desired.
   'Set the Capt/Comp A to 4000, 4000 Clk Cycles per Intr Trigger.
   'Note Well: Do NOT want to Override the PortC.0 pin, for waveform, which is
   'used by the DS Temp Chip.
   'Manually set up TCC0:
   'Subroutine TickTock2 runs on each interrupt.
   Tcc0_ctrla = 4                                           'PortC T/C 0, Source = Clock, Div by 8
   Tcc0_ctrlb = 1                                           'Enable Capture/Compare A, Top = CCA
   Tcc0_ctrlc = 0                                           'Waveform
   Tcc0_ctrld = 0                                           'Event System
   Tcc0_ctrle = 0                                           '8-Bit Mode
   Tcc0_intctrla = 0                                        'Overflow Intr OFF
   Tcc0_intctrlb = 1                                        'Capt/Compare A, Intr Enable, Low
   Tcc0_intflags = 0                                        'Capt/Comp A Intr Flag = Bit 4
   Tcc0_ccabuf = 4000                                       'Capt/Comp A
   On Tcc0_cca Ticktock2                                    'Capt/Comp Intr A used for Switch Debounce
   Enable Tcc0_cca , Lo                                     'PortC Timer/Counter #0

   'Now turn on Global Interrupts and let the fun begin:
   Config Priority = Roundrobin , Vector = Application , Med = Enabled , Lo = Enabled
   Enable Interrupts

Main:
   Gosub Clockopt1                                          'Set the Xmega Clock Frequency
   Do
      'Flash an Xplain LED On & Off using Waitms delay loop
      Reset Led0                                            'Low = On
      Waitms 500                                            'Delay 0.5 sec
      Set Led0                                              'High = Off
      Waitms 500                                            'Dealy 0.5 Sec
   Loop                                                     'Forever

'...............................................................................

Clockopt1:
   'Set up the Xmega clock using Bascom Instructions.
   'Run on Internal 32 MHz Osc at 32 MHz.
   'Leave 2 MHz Osc enabled, and enable the Int 32MHz Clock
   Config Osc = Enabled , 32mhzosc = Enabled
   'Wait a bit to allow clocks to start up and stabilize.
   'Note: Haven't yet switched to 32MHz, are at 2MHz, so Waitms 1 = 16 mSec
   Waitms 4                                                 'Wait 64mSec
   'Next configure the systemclock:
   Config Sysclock = 32mhz , Prescalea = 1 , Prescalebc = 1_1
   Waitms 100                                               'Startup Stabilization Delay
   Return

Ticktock:
   'Overflow Interrupt on Timer/Counter C #1
   'Medium Priority.
   'This flashes at About 1/Second, but using the Overflow Interrupt
   'does not give precise timing control.
   'This Interrupt runs 61.035 times / Second.  (Every 16.384 mSec)
   'The counter resets every 61 counts.
   '
   Ttcnt = Ttcnt + 1                                        'Incr Interrupt Counter
   If Ttcnt < 5 Then
      Reset Led1                                            'Turn On LED, Low = On
   Else
      Set Led1                                              'Turn Off LED, High = Off
   End If
   If Ttcnt > 61 Then
      Ttcnt = 0                                             'Rollover
   End If
   Return

Ticktock2:
   'Compare Mode Interrupt for Timer/Counter PortC #0
   'Low Priority.
   'Capture/Compare Mode, "A", Low Priority, 1 / mSec.
   'This interrupt demonstrates fine control of the interrupt rate.
   'It fires once every millisecond.
   'This allows much better timing control than using a Waitms loop, or using
   'an Overflow interrupt.
   'The Interrupt counter resets every 1000 interrupts.
   Ttcnt2 = Ttcnt2 + 1                                      'Incr Interrupt Counter
   If Ttcnt2 < 100 Then
      Reset Led2                                            'Turn On LED, Low = On
   Else
      Set Led2                                              'Turn Off LED, High = Off
   End If
   If Ttcnt2 > 1000 Then
      Ttcnt2 = 0                                            'Rollover
   End If
   Return
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
quadral
PostPosted: Dec 04, 2011 - 08:45 PM
Newbie


Joined: Nov 24, 2011
Posts: 14


I tried to reproduce the above example in 'C' and got the following result:

Code:
#include <avr/io.h>
#include <avr/interrupt.h>

ISR(TCC0_OVF_vect)
{
   static int g_breakpoint = 0;
  ++g_breakpoint;
}

void init_timer()
{
   TCC0.CTRLA = TC_CLKSEL_DIV8_gc; // PortC T/C 0, Source = Clock, Div by 8 
  TCC0.CTRLB = 0;                 // Normal Mode, Update on Top
  TCC0.CTRLC = 0;                 // Waveform
  TCC0.CTRLD = 0;                 // Event System
  TCC0.CTRLE = 0;                 // 8-Bit Mode
 
  TCC0.INTCTRLA = TC_OVFINTLVL_LO_gc; // Overflow, LOW Priority
 
  TCC0.INTCTRLB = 0;              // Compare
  TCC0.INTFLAGS = 0;              // Overflow Flag in bit 0
 
   TCC0.PER = 125;                 // Period register
}

void init_32MhzClock(void)
{
  OSC.CTRL |= OSC_RC32MEN_bm;
  while( ! ( OSC.STATUS & OSC_RC32MRDY_bm ) );
  CCP = CCP_IOREG_gc;
  CLK.CTRL = ( CLK.CTRL & ~CLK_SCLKSEL_gm ) | CLK_SCLKSEL_RC32M_gc;
}

void init_pmic(void)
{
   PMIC.CTRL |= PMIC_LOLVLEN_bm;
}

int main(void)
{
   cli();
  init_32MhzClock();
  init_timer();
  init_pmic();
  sei();
   
  while(1) { asm volatile("nop"); }
  return 0;
}


The timer is working only if I set TCC0.PER register but in your example above you don't set it?
The next question is, why it's not possible to change clock division register from dived by 8 ( TCC0.CTRLA = TC_CLKSEL_DIV8_gc ) to devided by 256 ( TCC0.CTRLA = TC_CLKSEL_DIV256_gc ). In this case the timer isn't working again.
My goal is to get a working 1 ms timer ( PER = ( 1ms * 32MHz ) / 256 = 125 ) thus I need a prescaler = 256
 
 View user's profile Send private message  
Reply with quote Back to top
indianajones11
PostPosted: Dec 05, 2011 - 05:15 AM
Raving lunatic


Joined: Nov 28, 2004
Posts: 3551
Location: San Diego, Ca

You don't need the " nop " , it's just a waste and just do :

Code:
void init_32MhzClock(void)
{
  OSC.CTRL = OSC_RC32MEN_bm;// No need for the '|' in init. code .

  while( ! ( OSC.STATUS & OSC_RC32MRDY_bm ) );
  CCP = CCP_IOREG_gc;
 
  CLK.CTRL = CLK_SCLKSEL_RC32M_gc; // WHY are you complicating this, you only have 4 cycles ?!
}


Edit: CLK.CTRL gets loaded with 1, NOT 2 using your code, according to the *.lss file !! The ISR didn't fire also . SO just do this for now :
Code:
CLK.CTRL = 0x02;
and then it fires . I used your latest posted code, of course init_timer() only needs the 3 lines, with div_256 and NO nop or return statement .

Lesson 1-- verify it's making it in 4 cycles from the *.lss file . It wasn't doing it your way .

Lesson 2-- Check critical values during debug using that same file . You must learn to use the *.lss file, esp. when debugging, bottom line .

Save some program bytes and use :
Code:
static inline void
in front of all init() code .

_________________
1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1


Last edited by indianajones11 on Mar 20, 2012 - 02:18 AM; edited 1 time in total
 
 View user's profile Send private message  
Reply with quote Back to top
quadral
PostPosted: Dec 05, 2011 - 11:43 AM
Newbie


Joined: Nov 24, 2011
Posts: 14


Unfortunately I can not test it now because I'm not at home. But
should not the CLOCK CTRL (CLK.CTRL) value be set to 1 ( 0x01 == 32 MHz Internal RC Oscillator; Table 7-1 System Clock Selection on page 84 in XMega Manual A );
The Oscillator description on page 87 covers only the Oscillator ( here especially OSC.CTRL) register, or not?

In my understanding, the register must be set as follows:
Code:

void init_32MhzClock(void)
{
  OSC.CTRL = 0x02;  // Bit 1- RC32MEN: 32 MHz Internal RC Oscillator Enable

  while( ! ( OSC.STATUS & OSC_RC32MRDY_bm ) );
  CCP = CCP_IOREG_gc;

  CLK.CTRL = 0x01; // System Clock Selection: 32 MHz Internal RC Oscillator
}


I do not understand why then the timer interrupt is not fired?
I will test today evening.

If I set CLK.CTRL = 0x02 then I set the internal Oscillator with 32kHz?
 
 View user's profile Send private message  
Reply with quote Back to top
indianajones11
PostPosted: Dec 05, 2011 - 09:19 PM
Raving lunatic


Joined: Nov 28, 2004
Posts: 3551
Location: San Diego, Ca

You're right, quadral it needs to be set to 1 ! I was confusing the clk_ctrl register with the osc_ctrl bit settings . Nothing wrong with the *.h files . So your problem was you were not meeting 4 cycle limit .

Sim. ver.2 gives 32,006 cycles until ISR fires for 32 MHz clk ( close enough to 1 mS ), after I reset its counter at sei() .
So do this :
Code:
void init_32MhzClock(void)
{
  OSC.CTRL = OSC_RC32MEN_bm;
  while( ! ( OSC.STATUS & OSC_RC32MRDY_bm ) );

  CCP = CCP_IOREG_gc;
  CLK.CTRL = CLK_SCLKSEL_RC32M_gc;
}

Quote:
If I set CLK.CTRL = 0x02 then I set the internal Oscillator with 32kHz?
Yep .

_________________
1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1


Last edited by indianajones11 on Jan 01, 2012 - 08:27 AM; edited 1 time in total
 
 View user's profile Send private message  
Reply with quote Back to top
quadral
PostPosted: Dec 05, 2011 - 10:24 PM
Newbie


Joined: Nov 24, 2011
Posts: 14


I slowly running out of ideas.
indiana jones 11, i do not know what you do differently? why it works on your envoronment Smile?
I built the following code without and with optimization (-Os) turned on. The timer should be fired every 1ms, but only comes every few seconds.

Environment: AvrStudio 5 simu
My test: breakpoint on line (++g_breakpoint;) inside interrupt.

I just want to see that the timer is fired 'Crying or Very sad'

Code:
#include <avr/io.h>
#include <avr/interrupt.h>

ISR(TCC0_OVF_vect)
{
   static int g_breakpoint = 0;
  ++g_breakpoint;
}

/*
  timer = 1ms
*/
static void init_timer()
{
  TCC0.CTRLA = TC_CLKSEL_DIV256_gc;     // Set: Prescaler
  TCC0.CTRLB = 0;                       // Set: Normal Mode, Update on Top
  TCC0.CTRLC = 0;                       // Disabled: Waveform
  TCC0.CTRLD = 0;                       // Disabled: Event System
  TCC0.CTRLE = 0;                       // 8-Bit Mode is off; word mode active;
 
  TCC0.INTCTRLA = TC_OVFINTLVL_LO_gc;   // Enabled: Overflow interrupt with LOW Priority on port C
  TCC0.INTCTRLB = 0;                    // Disabled: Disable ABCD Capture/Compare interrupts

  TCC0.INTFLAGS = 0;                    // Reset: Overflow Flag in bit 0; clear any initial interrupt flags!
 
  TCC0.CNT = 0;                         // Init: Counter register
  TCC0.PER = 125;                       // Set: Period register
}

static void init_32MhzClock(void)
{
  CCP = CCP_IOREG_gc; // instruction has to be done "within 4 clock cycles"
  OSC.CTRL = 0x02;  // 32 MHz Internal RC Oscillator Enable

  while( ! ( OSC.STATUS & OSC_RC32MRDY_bm ) );

  CCP = CCP_IOREG_gc; // instruction has to be done "within 4 clock cycles"
  CLK.CTRL = 0x01; // System Clock Selection: 32 MHz Internal RC Oscillator
}

static void init_pmic(void)
{
   PMIC.CTRL |= PMIC_LOLVLEN_bm;
}

void main(void)
{
  cli();
  init_32MhzClock();
  init_timer();
  init_pmic();
  sei();
   
  while(1)
  {
    //asm volatile("nop");
  }
}


listing from lss file ( with activated optimization -Os).Result: OK, because all I/O instructions within 4 clock cycles:

Code:
static void init_32MhzClock(void)
{
  CCP = CCP_IOREG_gc; // == 0xD8; next (following) instruction has to be done "within 4 clock cycles"
 262:   88 ed          ldi   r24, 0xD8   ; 216
 264:   84 bf          out   0x34, r24   ; 52
  OSC.CTRL = 0x02;  // Bit 1- RC32MEN: 32 MHz Internal RC Oscillator Enable
 266:   82 e0          ldi   r24, 0x02   ; 2
 268:   80 93 50 00    sts   0x0050, r24

  while( ! ( OSC.STATUS & OSC_RC32MRDY_bm ) );
 26c:   80 91 51 00    lds   r24, 0x0051
 270:   81 ff          sbrs   r24, 1
 272:   fc cf          rjmp   .-8         ; 0x26c <main+0xc>

  CCP = CCP_IOREG_gc; // == 0xD8; next (following) instruction has to be done "within 4 clock cycles"
 274:   88 ed          ldi   r24, 0xD8   ; 216
 276:   84 bf          out   0x34, r24   ; 52
  CLK.CTRL = 0x01; // System Clock Selection: 32 MHz Internal RC Oscillator
 278:   81 e0          ldi   r24, 0x01   ; 1
 27a:   80 93 40 00    sts   0x0040, r24
 
 View user's profile Send private message  
Reply with quote Back to top
indianajones11
PostPosted: Dec 05, 2011 - 11:36 PM
Raving lunatic


Joined: Nov 28, 2004
Posts: 3551
Location: San Diego, Ca

Don't use CCP for osc_ctrl, RTM ( could using it wrong like that cause it to fail ? I don't know ) . The *.lss is right except for that, but Studio 5 is more than buggy enough that it may be deceiving you . Try your code on Studio 4, MY environment ( AS4 & AS5 can co-exist ). Wink

Your init() functions should be, for example :
Code:
static inline void init_pmic(void)
{
   PMIC.CTRL |= PMIC_LOLVLEN_bm;
}
not just "static" in front .

_________________
1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1
 
 View user's profile Send private message  
Reply with quote Back to top
quadral
PostPosted: Dec 06, 2011 - 09:24 PM
Newbie


Joined: Nov 24, 2011
Posts: 14


Thank you indianajones11!
I have not tested the software in the AS4 environment but directly on hardware and it runs Razz
My conclusion is therefore, as you have already written, that the simulation environment of AS5 is very buggy. From now I check software directly on hardware.

One additional input: I think both CLK and OSC are I/O Protected Registers and thus they must been both protected from accidental changes with CCP.
And is inline not only a hint for compiler?
 
 View user's profile Send private message  
Reply with quote Back to top
indianajones11
PostPosted: Dec 07, 2011 - 04:07 AM
Raving lunatic


Joined: Nov 28, 2004
Posts: 3551
Location: San Diego, Ca

quadral I'm glad it works for you, YW ! I suggest installing/trying this code on AS4 if I were you, AS5 wasted alot of your time ! It's good to get comfortable with your ( working ) simulator .
Quote:
One additional input: I think both CLK and OSC are I/O Protected Registers and thus they must been both protected from accidental changes with CCP.

Don't think about it, KNOW it by checking pg 84 and 87... nothing debateable about that .

Quote:
And is inline not only a hint for compiler?
In this form it is, but I've never seen it not take the hint every time ( but under certain conditions, it won't do the inline ) . Wink There is an "attribute"/"always inline" combo that can be used to guarantee that it does it .

See the "minimizing functions" tutorial .

_________________
1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1
 
 View user's profile Send private message  
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT + 1 Hour
Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Powered by PNphpBB2 © 2003-2006 The PNphpBB Group
Credits