Forum: Mikrocontroller und Digitale Elektronik CrossWorks AVR 2.0 - ATmega2560 - Timer0 läuft im Simulator nicht


von Tom R. (t0m)


Lesenswert?

Hallo liebe µC Freunde,
ich habe mich heute mal rangemacht um meinen Code vom Atmega2560 
(geschrieben im AVR Studio) in Crossworks AVR 2.0 zu portieren. 
Allerdings bin ich hier schon sehr früh gescheitert.

Ich wollte den Timer0 für die CTL als Timer einbinden, doch leider 
funktioniert der Timer0 im Simulator garnicht.

Diese Zeilen Code funktionieren im AVRStudio mit 1ms Ticks:
1
TCNT0 = 0;
2
OCR0A = 249;
3
TIMSK0 = 0x02;
4
TCCR0A = 0x02;
5
TCCR0B= 0x01;
6
SREG |= 0x01;

Wenn ich die Zeilen allerdings in den Standard CTL (Code nach dem 
Erstellen des CTL Projektes) einfüge, werden die Werte richtig in die 
Register geschrieben, aber TCNT0 bewegt sich kein Stück.
Hier der gesamte CTL Code:
1
#include <string.h>
2
#include <ctl_api.h>
3
#include <ATmega2560.h>
4
5
CTL_TASK_t main_task, new_task;
6
7
#define CALLSTACKSIZE 16 // this is only required for AVR builds
8
#define STACKSIZE 64          
9
unsigned new_task_stack[1+STACKSIZE+1];
10
11
void 
12
new_task_code(void *p)
13
{  
14
  unsigned int v=0;
15
16
  
17
18
  while (1)
19
    {      
20
      // task logic goes here      
21
      v++;
22
    }  
23
}
24
25
void
26
ctl_handle_error(CTL_ERROR_CODE_t e)
27
{
28
  while (1);
29
}
30
31
int main(void)
32
{
33
  unsigned int v=0;
34
35
  TCNT0 = 0;
36
  OCR0A = 249;
37
  TIMSK0 = 0x02;
38
  TCCR0A = 0x02;
39
  TCCR0B= 0x01;
40
  SREG |= 0x01;
41
42
  ctl_task_init(&main_task, 255, "main"); // create subsequent tasks whilst running at the highest priority.
43
  ctl_start_timer(ctl_increment_tick_from_isr); // start the timer 
44
  memset(new_task_stack, 0xcd, sizeof(new_task_stack));  // write known values into the stack
45
  new_task_stack[0]=new_task_stack[1+STACKSIZE]=0xfacefeed; // put marker values at the words before/after the stack
46
  ctl_task_run(&new_task, 1, new_task_code, 0, "new_task", STACKSIZE, new_task_stack+1, CALLSTACKSIZE);
47
  //ctl_task_set_priority(&main_task, 0); // drop to lowest priority to start created tasks running.
48
  
49
  while (1)
50
    {    
51
      // power down can go here if supported      
52
      v++;
53
    }
54
  return 0;
55
}

Würde mich sehr über einige Hilfestellungen freuen.

Gruß Tom

von Zipp (Gast)


Lesenswert?

Naja. Eigentlich musst du dem Hersteller des Tools einheizen. Die haben 
sicher ein forum.

von Tom R. (t0m)


Lesenswert?

Habe es nun selbst rausbekommen. Der AVR Simulator simmuliert nur den 
CPU Core. Für die Peripherie muss ein JS-Skript geschrieben werden 
(siehe 
http://www.crossstudio.co.uk/documentation/avr_1_4/index.htm?http://www.crossstudio.co.uk/documentation/avr_1_4/avr_simulator_target_interface.htm).

Für meinen speziellen Fall des Timer0 sieht das Skript wie folgt aus:
1
// ATmega2560
2
3
peripherals = new Array(0x200);
4
var totalCycleCount;
5
6
function reset()
7
{  
8
  totalCycleCount = 0;
9
  for (i=0; i<peripherals.length;i++)
10
    peripherals[i] = 0xcd;
11
  
12
  peripherals[0x44] = 0; // TCCR0A
13
  peripherals[0x45] = 0; // TCCR0B
14
  peripherals[0x46] = 0; // TCNT0
15
  peripherals[0x47] = 0; // OCR0A
16
  peripherals[0x48] = 0; // OCR0B
17
  peripherals[0x6E] = 0; // TIMSK0
18
  peripherals[0x35] = 0; // TIFR0
19
}
20
21
function pollForInterrupts(cycleCount)
22
{  
23
  switch (peripherals[0x45] & 0x7)
24
    {
25
      case 0: // not enabled
26
        return 0;
27
      case 1:
28
        ls = 0; // div 1
29
        break;
30
      case 2:        
31
        ls = 3; // div 8
32
        break;
33
      case 3:       
34
        ls = 6; // div 64
35
        break;
36
      case 4:        
37
        ls = 8; // div 256
38
        break;
39
      case 5:       
40
        ls = 10; // div 1024
41
        break;
42
    }
43
  var newCycleCount = totalCycleCount + cycleCount;  // calc new cpu cycle counter
44
  var increment = (newCycleCount>>ls) - (totalCycleCount>>ls);  //calc counter tick increment
45
  peripherals[0x46] = (peripherals[0x46] + increment) & 0xFF;  // increment counter
46
  totalCycleCount = newCycleCount;   // increment cpou cycle counter
47
  if (peripherals[0x46] >= peripherals[0x47])
48
    {
49
      peripherals[0x46] = 0;     
50
      if (peripherals[0x6E] & 2)
51
    {
52
    
53
        return 22;
54
    }
55
    }
56
  return 0;
57
}
58
59
function loadPeripheral(address)
60
{ 
61
  if (address >= 0x20 && address < 0x200)
62
    return peripherals[address];
63
  return 0xcd;
64
}
65
66
function storePeripheral(address, value)
67
{
68
  if (address >= 0x20 && address < 0x200)
69
  {
70
  return peripherals[address]=value;   
71
  }
72
}

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.