misc: Made (most) of the headers c++ friendly
[simavr] / include / avr_mcu_section.h
1 /*
2         avr_mcu_section.h
3
4         Copyright 2008, 2009 Michel Pollet <buserror@gmail.com>
5
6         This file is part of simavr.
7
8         simavr is free software: you can redistribute it and/or modify
9         it under the terms of the GNU General Public License as published by
10         the Free Software Foundation, either version 3 of the License, or
11         (at your option) any later version.
12
13         simavr is distributed in the hope that it will be useful,
14         but WITHOUT ANY WARRANTY; without even the implied warranty of
15         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16         GNU General Public License for more details.
17
18         You should have received a copy of the GNU General Public License
19         along with simavr.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22 #ifndef __AVR_MCU_SECTION_H__
23 #define __AVR_MCU_SECTION_H__
24
25 /*
26  * This header is used to pass "parameters" to the programmer or the simulator,
27  * it tags the ELF file with a section that contains parameters about the physical
28  * AVR this was compiled for, including the speed, model, and signature bytes.
29  *
30  * A programmer software can read this and verify fuses values for example, and a
31  * simulator can instanciate the proper "model" of AVR, the speed and so on without
32  * command line parameters.
33  *
34  * Exemple of use:
35  *
36  * #include "avr_mcu_section.h"
37  * AVR_MCU(F_CPU, "atmega88");
38  *
39  */
40
41 #include <stdint.h>
42
43 #ifdef __cplusplus
44 extern "C" {
45 #endif
46
47 enum {
48         AVR_MMCU_TAG = 0,
49         AVR_MMCU_TAG_NAME,
50         AVR_MMCU_TAG_FREQUENCY,
51         AVR_MMCU_TAG_VCC,
52         AVR_MMCU_TAG_AVCC,
53         AVR_MMCU_TAG_AREF,
54         AVR_MMCU_TAG_LFUSE,
55         AVR_MMCU_TAG_HFUSE,
56         AVR_MMCU_TAG_EFUSE,
57         AVR_MMCU_TAG_SIGNATURE,
58         AVR_MMCU_TAG_SIMAVR_COMMAND,
59         AVR_MMCU_TAG_VCD_FILENAME,
60         AVR_MMCU_TAG_VCD_PERIOD,        
61         AVR_MMCU_TAG_VCD_TRACE,
62 };
63
64 enum {
65         SIMAVR_CMD_NONE = 0,
66         SIMAVR_CMD_VCD_START_TRACE,
67         SIMAVR_CMD_VCD_STOP_TRACE,
68         SIMAVR_CMD_UART_LOOPBACK,
69 };
70
71 #if __AVR__
72
73 #define _MMCU_ __attribute__((section(".mmcu")))
74 struct avr_mmcu_long_t {
75         uint8_t tag;
76         uint8_t len;
77         uint32_t val; 
78 } __attribute__((__packed__));
79
80 struct avr_mmcu_string_t {
81         uint8_t tag;
82         uint8_t len;
83         char string[]; 
84 } __attribute__((__packed__));
85
86 struct avr_mmcu_addr_t {
87         uint8_t tag;
88         uint8_t len;
89         void * what;
90 } __attribute__((__packed__));
91
92 struct avr_mmcu_vcd_trace_t {
93         uint8_t tag;
94         uint8_t len;
95         uint8_t mask;
96         void * what;
97         char name[]; 
98 } __attribute__((__packed__));
99
100 #define AVR_MCU_STRING(_tag, _str) \
101 const struct avr_mmcu_string_t _##_tag _MMCU_ = {\
102         .tag = _tag,\
103         .len = sizeof(_str),\
104         .string = _str,\
105 }
106
107 #define AVR_MCU_LONG(_tag, _val) \
108 const struct avr_mmcu_long_t _##_tag _MMCU_ = {\
109         .tag = _tag,\
110         .len = sizeof(uint32_t),\
111         .val = _val,\
112 }
113
114 #define AVR_MCU_BYTE(_tag, _val) \
115 const uint8_t _##_tag _MMCU_ = { _tag, 1, _val }
116
117 #define AVR_MCU_VCD_SYMBOL(_name) \
118         .tag = AVR_MMCU_TAG_VCD_TRACE, \
119         .len = sizeof(struct avr_mmcu_vcd_trace_t) - 2 + sizeof(_name),\
120         .name = _name
121
122 // specified the name and wanted period (usec) for a VCD file
123 // thid is not mandatory, a default one will be created if
124 // symbols are declared themselves
125 #define AVR_MCU_VCD_FILE(_name, _period) \
126         AVR_MCU_STRING(AVR_MMCU_TAG_VCD_FILENAME, _name);\
127         AVR_MCU_LONG(AVR_MMCU_TAG_VCD_PERIOD, _period)
128
129 // It is possible to send "commands" to simavr from the
130 // firmware itself. For this to work you need to specify
131 // an IO register that is to be used for a write-only
132 // bridge. A favourite is one of the usual "GPIO register"
133 // that most (all ?) AVR have
134 #define AVR_MCU_SIMAVR_COMMAND(_register) \
135         const struct avr_mmcu_addr_t _simavr_command_register _MMCU_ = {\
136                 .tag = AVR_MMCU_TAG_SIMAVR_COMMAND,\
137                 .len = sizeof(void *),\
138                 .what = (void*)_register, \
139         }
140
141 /*
142  * This the has to be used if you want to add other tags to the .mmcu section
143  * the _mmcu symbol is used as an anchor to make sure it stays linked in.
144  */
145 #define AVR_MCU(_speed, _name) \
146         const uint8_t _mmcu[2] _MMCU_ = { AVR_MMCU_TAG, 0 }; \
147         AVR_MCU_STRING(AVR_MMCU_TAG_NAME, _name);\
148         AVR_MCU_LONG(AVR_MMCU_TAG_FREQUENCY, _speed)
149
150 #define AVR_MCU_VOLTAGES(_vcc, _avcc, _aref) \
151         AVR_MCU_LONG(AVR_MMCU_TAG_VCC, (_vcc));\
152         AVR_MCU_LONG(AVR_MMCU_TAG_AVCC, (_avcc));\
153         AVR_MCU_LONG(AVR_MMCU_TAG_AREF, (_aref));
154
155 #endif /* __AVR__ */
156
157 #ifdef __cplusplus
158 };
159 #endif
160
161 #endif