Forum: Mikrocontroller und Digitale Elektronik CodeVision AVR Pointer auf fkt()


von Koopi (Gast)


Lesenswert?

Hallo und Guten Morgen,

folgende Zeile stellt eine Definition und Zuweisung eines Pointers auf 
eine Funktion dar.

void (*Init_ptr)( void ) = Init; // void init( void );

Laut CodeVisionHelp ist das immer ein Pointer auf flash. Leider nimmt 
sich CodeVision 4 Words Code und 2 Byte SRAM.
Die 2 Byte SRAM pro Pointer machen bei meinem Menue leider gleich 752 
Byte SRAM. Wie läßt sich das verhindern?

Koopi

von winne der philosoph ;-) (Gast)


Lesenswert?

mh?

Wahrscheinlich in dem man das Problem welches dich bewogen hat, diesen 
Weg zu gehen auf andere Weise löst. Lleider ist deine Anfrage vollkommen 
frei Evon Zusammenhängen. Das Einzige für mich erkennbare ist, dass du 
aus irgendeinem mir unbekannten Grund versuchst 376 Pointer im SRam 
anzuhäufen. Was sich sicher auch anders lösen läßt. Es sei denn  du 
versuchst sich selbst generierenden Code zu zu erzeugen. Das geht 
freilich nicht im flash und würde dank havard auch im ram nicht 
ausgeführt.   ;-)

von Koopi (Gast)


Lesenswert?

Die Anfrage ist bewusst auf den Punkt gebracht.

Ich möchte nichts im SRAM aufhängen, sondern im flash. Ein Menue läßt 
sich mit einer StateMachine, die entsprechend ihres Zustandes auf 
verschiedene Funktionen zugreift, leicht und komfortabel realisieren. 
Dazu muss ich aber mit Zeigern auf die in Frage kommenden Funktionen 
zugreifen können. Das ganze funktioniert mit uVision übrigens sehr gut.

Koopi

von winne (Gast)


Lesenswert?

so hier die lösung deines Rätsels eine eigen art von CVAR ist das du den 
speicherort von konstnten variablen und soit pointern definieren kannst

default ist sram alternativ flash und eeprom


siehe CVAVR help
stichwort: pointer
1
Due to the Harvard architecture of the AVR microcontroller, with separate address spaces for data (SRAM), program (FLASH) and EEPROM memory, the compiler implements three types of pointers.
2
The syntax for pointer declaration is:
3
4
[<type storage modifier>] type * [<pointer storage modifier>]
5
[* [<pointer storage modifier>] ...] pointer_name;
6
7
or
8
9
type [<type storage modifier>] * [<pointer storage modifier>]
10
[* [<pointer storage modifier>] ...] pointer_name;
11
12
where type can be any data type.
13
14
Variables placed in SRAM are accessed using normal pointers.
15
For accessing constants placed in FLASH memory, the flash type storage modifier is used.
16
For accessing variables placed in EEPROM, the eeprom type storage modifier is used.
17
Although the pointers may point to different memory areas, they are by default stored in SRAM.
18
Example:
19
20
/* Pointer to a char string placed in SRAM */
21
char *ptr_to_ram=”This string is placed in SRAM”;
22
23
/* Pointer to a char string placed in FLASH */
24
flash char *ptr_to_flash1=”This string is placed in FLASH”;
25
char flash *ptr_to_flash2=”This string is also placed in FLASH”;
26
27
/* Pointer to a char string placed in EEPROM */
28
eeprom char *ptr_to_eeprom1="This string is placed in EEPROM";
29
char eeprom *ptr_to_eeprom2="This string is also placed in EEPROM";
30
31
In order to store the pointer itself in other memory areas, like FLASH or EEPROM, the flash or eeprom pointer storage modifiers must be used as in the examples below:
32
33
/* Pointer stored in FLASH to a char string placed in SRAM */
34
char * flash flash_ptr_to_ram=”This string is placed in SRAM”;
35
36
/* Pointer stored in FLASH to a char string placed in FLASH */
37
flash char * flash flash_ptr_to_flash=”This string is placed in FLASH”;
38
39
/* Pointer stored in FLASH to a char string placed in EEPROM */
40
eeprom char * flash eeprom_ptr_to_eeprom="This string is placed in EEPROM";
41
42
/* Pointer stored in EEPROM to a char string placed in SRAM */
43
char * eeprom eeprom_ptr_to_ram=”This string is placed in SRAM”;
44
45
/* Pointer stored in EEPROM to a char string placed in FLASH */
46
flash char * eeprom eeprom_ptr_to_flash=”This string is placed in FLASH”;
47
48
/* Pointer stored in EEPROM to a char string placed in EEPROM */
49
eeprom char * eeprom eeprom_ptr_to_eeprom="This string is placed in EEPROM";
50
51
In order to improve the code efficiency several memory models are implemented.
52
53
The TINY memory model uses 8 bits for storing pointers to the variables placed in SRAM. In this memory model you can only have access to the first 256 bytes of SRAM.
54
55
The SMALL memory model uses 16 bits for storing pointers the variables placed in SRAM. In this memory model you can have access to 65536 bytes of SRAM.
56
57
In both TINY and SMALL memory models pointers to the FLASH memory area use 16 bits.
58
59
Because in these memory models pointers to the FLASH memory are 16 bits wide, the total size of the constant arrays and literal char strings is limited to 64K.
60
However the total size of the program can be the full amount of FLASH.
61
62
In order to remove the above mentioned limitation, there are available two additional memory models: MEDIUM and LARGE.
63
The MEDIUM memory model is similar to the SMALL memory model, except it uses pointers to constants in FLASH that are 32 bits wide. The pointers to functions are however 16 bit wide because they hold the word  address of the function, so 16 bits are enough to address a function located in all 128kbytes of FLASH.
64
65
The MEDIUM memory model can be used only for chips with 128kbytes of FLASH.
66
67
The LARGE memory model is similar to the SMALL memory model, except it uses pointers to the FLASH memory area that are 32 bits wide.
68
The LARGE memory model can be used for chips with 256kbytes or more of FLASH.
69
70
In all memory models pointers to the EEPROM memory area are 16 bit wide.
71
72
Pointers can be grouped in arrays, which can have up to 8 dimensions.
73
Example:
74
75
76
/* Declare and initialize a global array of pointers to strings  
77
78
   placed in SRAM */
79
80
char *strings[3]={"One","Two","Three"};
81
82
/* Declare and initialize a global array of pointers to strings  
83
84
   placed in FLASH
85
86
   The pointer array itself is also stored in FLASH */
87
88
flash char * flash messages[3]={"Message 1","Message 2","Message 3"};
89
90
/* Declare some strings in EEPROM */
91
92
eeprom char m1[]="aaaa";
93
94
eeprom char m2[]="bbbb";
95
96
void main(void) {
97
98
/* Declare a local array of pointers to the strings placed in EEPROM
99
100
   You must note that although the strings are located in EEPROM,
101
102
   the pointer array itself is located in SRAM */
103
104
char eeprom *pp[2];
105
106
/* and initialize the array */
107
108
pp[0]=m1;
109
110
pp[1]=m2;
111
112
}
113
114
Pointers to functions always access the FLASH memory area. There is no need to use the flash keyword for these types of pointers.
115
116
Example:
117
118
/* Declare a function */
119
120
int sum(int a, int b) {
121
122
return a+b;
123
124
}
125
126
/* Declare and initialize a global pointer to the function sum */
127
128
int (*sum_ptr) (int a, int b)=sum;
129
130
void main(void) {
131
132
int i;
133
134
/* Call the function sum using the pointer */
135
136
i=(*sum_ptr) (1,2);
137
138
}

von winne (Gast)


Lesenswert?

man kann auch nach storage modifier suchen ;-)

wenn deine funktionen fix sind sollte das mit flash als modifier gehen 
:-)

von winne (Gast)


Lesenswert?

etwa so
1
void (flash *Init_ptr)( void ) = Init; // void init( void );

oder so
1
void (eeprom *Init_ptr)( void ) = Init; // void init( void );

von winne (Gast)


Lesenswert?

Berichtigung der modifier muss hinter den * wen der pointer ins flash 
soll.
1
void (*flash Init_ptr)( void ) = Init; // void init( void );

Achtung wenn funktionen auf diesen pointer zugreifen sollen uss diesen 
auch mitgeteilt werden wo sie den pointer finden.

von winne (Gast)


Lesenswert?

das sieht dan etwa so aus
1
void LCD_Write_Strf (flash char str [] )
2
{   
3
    while ( *str )
4
        {
5
        LCD_Write_Data_to_RAM(*str++);
6
        }
7
   
8
}  
9
//------------------------------------------------- 
10
void LCD_Write_Stre (eeprom char str [] )
11
{   
12
    while ( *str )
13
        {
14
        LCD_Write_Data_to_RAM(*str++);
15
        }
16
   
17
}

von Koopi (Gast)


Lesenswert?

@winnie,

danke die 10:21 Antwort ist der Treffer.

Ich habe alle Varianten ( auch die 10:12 ) versucht, aber nicht *flash.

Kannst Du mir sagen warum es flash und nicht flash sein muss? Es ist 
zwar richtig, für mich aber nicht plausibel! ( Horizonterweiterungsfrage 
)

ein weiteres danke, dass Du nicht erst eine andere Lösung in den Raum 
geworfen hast. Davon gibt es hier leider reichlich.

von Koopi (Gast)


Lesenswert?

Berichtigung:
Das fette soll heißen: "(Stern)flash und nicht flash(Stern)"

Koopi

von winne (Gast)


Lesenswert?

>Kannst Du mir sagen warum es flash und nicht flash sein muss?

Das ist eine Syntaxfrage. Die Reihenfolge bei CVAVR lautet nunmal so.
Der storage modifier wird dem identifier vorangestellt, und dem Ganzen 
der Pointerstern wenn man sich auf den Pointer und nicht auf das 
funktionsergebnis bezieht.

Ich arbeite schon seit drei Jahren mit CVAVR und inzwischen 
überglücklich. Aber anfänglich bin ich fast verrückt geworden damit. 
Insbesondere damit, dass man für verschiedenen Speicherorte auch 
verschiedene Funktionen braucht. Ich komme von der 6502 mit einem 
itegriertem Adressraum. Die Umstellung auf Havard birgt Tücken, diese 
förden den Lernprozess, und deshalb habe ich sofort daran gedacht als 
ich dein Dilemma erkannte.

Leider scheinen viele das Geld und die Mühen des CVAVR zu scheuen. So 
bin ich bemüht diesem hier im forum zu einem Stand zu verhelfen, der 
diesem mächtigen Compiler nebst Helferlein (speziel sei der Codewizzard 
erwähnt) gerecht wird.

von Koopi (Gast)


Lesenswert?

ich arbeite auch schon einige Jahre mit diesem Compiler. Da ich aber 
sehr viel mit uVision und IAR mache, fallen mir doch immer wieder einige 
Unzulänglichkeiten auf. Wie Du schon bemerkt hast, sind die 
Funktionsaufrufe mit verschiedenen Speichertypen ein Problem. Das selbe 
Problem gibt es bei den structs und typedefs.
Der Editor hätte auch einige Erweiterungen nötig. Da wären insbesondere 
ein "Go to Definition" und fehlende Reiter für die gerade offenen 
Funktionen zu erwähnen.

Wenn ich aber den äußerst gut kalkulierten Preis dagegen halte ist es 
wieder in Ordnung ( uVision z.B. kostet 3000 Euro ).

Koopi

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.