SimAVR
AVR Simulator
avr_mcu_section.h
Go to the documentation of this file.
1 /*
2  avr_mcu_section.h
3 
4  Copyright 2008-2013 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 it under the terms of the GNU
9  General Public License as published by the Free Software Foundation, either version 3 of the
10  License, or (at your option) any later version.
11 
12  simavr is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
13  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
14  Public License for more details.
15 
16  You should have received a copy of the GNU General Public License along with simavr. If not, see
17  <http://www.gnu.org/licenses/>.
18 */
19 
20 #ifndef __AVR_MCU_SECTION_H__
21 #define __AVR_MCU_SECTION_H__
22 
23 /*
24  * This header is used to pass "parameters" to the programmer or the simulator, it tags the ELF file
25  * with a section that contains parameters about the physical AVR this was compiled for, including
26  * the speed, model, and signature bytes.
27  *
28  * A programmer software can read this and verify fuses values for example, and a simulator can
29  * instanciate the proper "model" of AVR, the speed and so on without command line parameters.
30  *
31  * Exemple of use:
32  *
33  * #include "avr_mcu_section.h"
34  * AVR_MCU(F_CPU, "atmega88");
35  *
36  */
37 
38 #include <stdint.h>
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
44  enum {
61  };
62 
63  enum {
68  };
69 
70 #if __AVR__
71 
72 #define _MMCU_ __attribute__((section(".mmcu")))
73 
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  /*
108  * This trick allows contatenation of tokens. We need a macro redirection for it to work.
109  * The goal is to make unique variable names (they don't matter anyway)
110  */
111 #define DO_CONCAT2(_a, _b) _a##_b
112 #define DO_CONCAT(_a, _b) DO_CONCAT2(_a,_b)
113 
114 #define AVR_MCU_LONG(_tag, _val) \
115  const struct avr_mmcu_long_t DO_CONCAT(DO_CONCAT(_, _tag), __LINE__) _MMCU_ = { \
116  .tag = _tag, \
117  .len = sizeof(uint32_t), \
118  .val = _val, \
119  }
120 
121 #define AVR_MCU_BYTE(_tag, _val) \
122  const uint8_t _##_tag _MMCU_ = { _tag, 1, _val }
123 
135 #define AVR_MCU_VCD_SYMBOL(_name) \
136  .tag = AVR_MMCU_TAG_VCD_TRACE, \
137  .len = sizeof(struct avr_mmcu_vcd_trace_t) - 2 + sizeof(_name), \
138  .name = _name
139 
144 #define AVR_MCU_VCD_FILE(_name, _period) \
145  AVR_MCU_STRING(AVR_MMCU_TAG_VCD_FILENAME, _name); \
146  AVR_MCU_LONG(AVR_MMCU_TAG_VCD_PERIOD, _period)
147 
154 #define AVR_MCU_SIMAVR_COMMAND(_register) \
155  const struct avr_mmcu_addr_t _simavr_command_register _MMCU_ = { \
156  .tag = AVR_MMCU_TAG_SIMAVR_COMMAND, \
157  .len = sizeof(void *), \
158  .what = (void*)_register, \
159  }
160 
166 #define AVR_MCU_SIMAVR_CONSOLE(_register) \
167  const struct avr_mmcu_addr_t _simavr_console_register _MMCU_ = { \
168  .tag = AVR_MMCU_TAG_SIMAVR_CONSOLE, \
169  .len = sizeof(void *), \
170  .what = (void*)_register, \
171  }
172 
179 #define AVR_MCU_EXTERNAL_PORT_PULL(_port, _mask, _val) \
180  AVR_MCU_LONG(AVR_MMCU_TAG_PORT_EXTERNAL_PULL, \
181  (((unsigned long)((_port)&0xff) << 16) | \
182  ((unsigned long)((_mask)&0xff) << 8) | \
183  ((_val)&0xff)));
184 
191 #define AVR_MCU_VOLTAGES(_vcc, _avcc, _aref) \
192  AVR_MCU_LONG(AVR_MMCU_TAG_VCC, (_vcc)); \
193  AVR_MCU_LONG(AVR_MMCU_TAG_AVCC, (_avcc)); \
194  AVR_MCU_LONG(AVR_MMCU_TAG_AREF, (_aref));
195 
200 #define AVR_MCU(_speed, _name) \
201  const uint8_t _mmcu[2] _MMCU_ = { AVR_MMCU_TAG, 0 }; \
202  AVR_MCU_STRING(AVR_MMCU_TAG_NAME, _name); \
203  AVR_MCU_LONG(AVR_MMCU_TAG_FREQUENCY, _speed)
204 
205 #endif /* __AVR__ */
206 
207 #ifdef __cplusplus
208 };
209 #endif
210 
211 #endif
Definition: avr_mcu_section.h:46
Definition: avr_mcu_section.h:57
Definition: avr_mcu_section.h:59
Definition: avr_mcu_section.h:50
Definition: avr_mcu_section.h:65
Definition: avr_mcu_section.h:47
Definition: avr_mcu_section.h:49
Definition: avr_mcu_section.h:58
Definition: avr_mcu_section.h:52
Definition: avr_mcu_section.h:60
Definition: avr_mcu_section.h:64
Definition: avr_mcu_section.h:51
Definition: avr_mcu_section.h:56
Definition: avr_mcu_section.h:45
Definition: avr_mcu_section.h:67
Definition: avr_mcu_section.h:55
static __attribute__((unused))
Definition: avr_twi.c:83
Definition: avr_mcu_section.h:66
Definition: avr_mcu_section.h:48
Definition: avr_mcu_section.h:53
Definition: avr_mcu_section.h:54