4 Copyright 2008, 2009 Michel Pollet <buserror@gmail.com>
6 This file is part of simavr.
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.
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.
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/>.
32 #include "avr_ioport.h"
37 #include "reprap_gl.h"
42 #define __AVR_ATmega644__
43 #include "marlin/pins.h"
47 #include "marlin/Configuration.h"
50 * these are the sources of heat and cold to register to the heatpots
62 typedef struct ardupin_t {
63 uint32_t port : 7, pin : 3, analog : 1, adc : 3, pwm : 1, ardupin;
64 } ardupin_t, *ardupin_p;
66 ardupin_t arduidiot_644[32] = {
67 [ 0] = { .ardupin = 0, .port = 'B', .pin = 0 },
68 [ 1] = { .ardupin = 1, .port = 'B', .pin = 1 },
69 [ 2] = { .ardupin = 2, .port = 'B', .pin = 2 },
70 [ 3] = { .ardupin = 3, .port = 'B', .pin = 3 },
71 [ 4] = { .ardupin = 4, .port = 'B', .pin = 4 },
72 [ 5] = { .ardupin = 5, .port = 'B', .pin = 5 },
73 [ 6] = { .ardupin = 6, .port = 'B', .pin = 6 },
74 [ 7] = { .ardupin = 7, .port = 'B', .pin = 7 },
76 [ 8] = { .ardupin = 8, .port = 'D', .pin = 0 },
77 [ 9] = { .ardupin = 9, .port = 'D', .pin = 1 },
78 [10] = { .ardupin = 10, .port = 'D', .pin = 2 },
79 [11] = { .ardupin = 11, .port = 'D', .pin = 3 },
80 [12] = { .ardupin = 12, .port = 'D', .pin = 4 },
81 [13] = { .ardupin = 13, .port = 'D', .pin = 5 },
82 [14] = { .ardupin = 14, .port = 'D', .pin = 6 },
83 [15] = { .ardupin = 15, .port = 'D', .pin = 7 },
85 [16] = { .ardupin = 16, .port = 'C', .pin = 0 },
86 [17] = { .ardupin = 17, .port = 'C', .pin = 1 },
87 [18] = { .ardupin = 18, .port = 'C', .pin = 2 },
88 [19] = { .ardupin = 19, .port = 'C', .pin = 3 },
89 [20] = { .ardupin = 20, .port = 'C', .pin = 4 },
90 [21] = { .ardupin = 21, .port = 'C', .pin = 5 },
91 [22] = { .ardupin = 22, .port = 'C', .pin = 6 },
92 [23] = { .ardupin = 23, .port = 'C', .pin = 7 },
94 [24] = { .ardupin = 24, .port = 'A', .pin = 7, .analog = 1, .adc = 7 },
95 [25] = { .ardupin = 25, .port = 'A', .pin = 6, .analog = 1, .adc = 6 },
96 [26] = { .ardupin = 26, .port = 'A', .pin = 5, .analog = 1, .adc = 5 },
97 [27] = { .ardupin = 27, .port = 'A', .pin = 4, .analog = 1, .adc = 4 },
98 [28] = { .ardupin = 28, .port = 'A', .pin = 3, .analog = 1, .adc = 3 },
99 [29] = { .ardupin = 29, .port = 'A', .pin = 2, .analog = 1, .adc = 2 },
100 [30] = { .ardupin = 30, .port = 'A', .pin = 1, .analog = 1, .adc = 1 },
101 [31] = { .ardupin = 31, .port = 'A', .pin = 0, .analog = 1, .adc = 0 },
104 // gnu hackery to make sure the parameter is expanded
105 #define _TERMISTOR_TABLE(num) \
107 #define TERMISTOR_TABLE(num) \
108 _TERMISTOR_TABLE(num)
116 if (pins[ardupin].ardupin != ardupin) {
117 printf("%s pin %d isn't correct in table\n", __func__, ardupin);
120 struct avr_irq_t * irq = avr_io_getirq(avr,
121 AVR_IOCTL_IOPORT_GETIRQ(pins[ardupin].port), pins[ardupin].pin);
123 printf("%s pin %d PORT%C%d not found\n", __func__, ardupin, pins[ardupin].port, pins[ardupin].pin);
130 * called when the AVR change any of the pins on port B
131 * so lets update our buffer
135 struct avr_irq_t * irq,
139 printf("%s %d\n", __func__, value);
140 // pin_state = (pin_state & ~(1 << irq->irq)) | (value << irq->irq);
145 char avr_flash_path[1024];
146 int avr_flash_fd = 0;
148 // avr special flash initalization
149 // here: open and map a file to enable a persistent storage for the flash memory
150 void avr_special_init( avr_t * avr)
153 avr_flash_fd = open(avr_flash_path, O_RDWR|O_CREAT, 0644);
154 if (avr_flash_fd < 0) {
155 perror(avr_flash_path);
158 // resize and map the file the file
159 (void)ftruncate(avr_flash_fd, avr->flashend + 1);
160 ssize_t r = read(avr_flash_fd, avr->flash, avr->flashend + 1);
161 if (r != avr->flashend + 1) {
162 fprintf(stderr, "unable to load flash memory\n");
163 perror(avr_flash_path);
168 // avr special flash deinitalization
169 // here: cleanup the persistent storage
170 void avr_special_deinit( avr_t* avr)
173 lseek(avr_flash_fd, SEEK_SET, 0);
174 ssize_t r = write(avr_flash_fd, avr->flash, avr->flashend + 1);
175 if (r != avr->flashend + 1) {
176 fprintf(stderr, "unable to load flash memory\n");
177 perror(avr_flash_path);
180 uart_pty_stop(&reprap.uart_pty);
183 #define MEGA644_GPIOR0 0x3e
186 reprap_relief_callback(
192 // printf("%s write %x\n", __func__, addr);
193 static uint16_t tick = 0;
214 uart_pty_init(avr, &r->uart_pty);
215 uart_pty_connect(&r->uart_pty, '0');
217 thermistor_init(avr, &r->therm_hotend, 0,
218 (short*)TERMISTOR_TABLE(TEMP_SENSOR_0),
219 sizeof(TERMISTOR_TABLE(TEMP_SENSOR_0)) / sizeof(short) / 2,
220 OVERSAMPLENR, 25.0f);
221 thermistor_init(avr, &r->therm_hotbed, 2,
222 (short*)TERMISTOR_TABLE(TEMP_SENSOR_BED),
223 sizeof(TERMISTOR_TABLE(TEMP_SENSOR_BED)) / sizeof(short) / 2,
224 OVERSAMPLENR, 30.0f);
225 thermistor_init(avr, &r->therm_spare, 1,
226 (short*)temptable_5, sizeof(temptable_5) / sizeof(short) / 2,
227 OVERSAMPLENR, 10.0f);
229 heatpot_init(avr, &r->hotend, "hotend", 28.0f);
230 heatpot_init(avr, &r->hotbed, "hotbed", 25.0f);
232 /* connect heatpot temp output to thermistors */
233 avr_connect_irq(r->hotend.irq + IRQ_HEATPOT_TEMP_OUT,
234 r->therm_hotend.irq + IRQ_TERM_TEMP_VALUE_IN);
235 avr_connect_irq(r->hotbed.irq + IRQ_HEATPOT_TEMP_OUT,
236 r->therm_hotbed.irq + IRQ_TERM_TEMP_VALUE_IN);
238 float axis_pp_per_mm[4] = DEFAULT_AXIS_STEPS_PER_UNIT; // from Marlin!
240 avr_irq_t * e = get_ardu_irq(avr, X_ENABLE_PIN, arduidiot_644);
241 avr_irq_t * s = get_ardu_irq(avr, X_STEP_PIN, arduidiot_644);
242 avr_irq_t * d = get_ardu_irq(avr, X_DIR_PIN, arduidiot_644);
243 avr_irq_t * m = get_ardu_irq(avr, X_MIN_PIN, arduidiot_644);
245 stepper_init(avr, &r->step_x, "X", axis_pp_per_mm[0], 100, 220, 0);
246 stepper_connect(&r->step_x, s, d, e, m, stepper_endstop_inverted);
249 avr_irq_t * e = get_ardu_irq(avr, Y_ENABLE_PIN, arduidiot_644);
250 avr_irq_t * s = get_ardu_irq(avr, Y_STEP_PIN, arduidiot_644);
251 avr_irq_t * d = get_ardu_irq(avr, Y_DIR_PIN, arduidiot_644);
252 avr_irq_t * m = get_ardu_irq(avr, Y_MIN_PIN, arduidiot_644);
254 stepper_init(avr, &r->step_y, "Y", axis_pp_per_mm[1], 100, 220, 0);
255 stepper_connect(&r->step_y, s, d, e, m, stepper_endstop_inverted);
258 avr_irq_t * e = get_ardu_irq(avr, Z_ENABLE_PIN, arduidiot_644);
259 avr_irq_t * s = get_ardu_irq(avr, Z_STEP_PIN, arduidiot_644);
260 avr_irq_t * d = get_ardu_irq(avr, Z_DIR_PIN, arduidiot_644);
261 avr_irq_t * m = get_ardu_irq(avr, Z_MIN_PIN, arduidiot_644);
263 stepper_init(avr, &r->step_z, "Z", axis_pp_per_mm[2], 20, 110, 0);
264 stepper_connect(&r->step_z, s, d, e, m, stepper_endstop_inverted);
267 avr_irq_t * e = get_ardu_irq(avr, E0_ENABLE_PIN, arduidiot_644);
268 avr_irq_t * s = get_ardu_irq(avr, E0_STEP_PIN, arduidiot_644);
269 avr_irq_t * d = get_ardu_irq(avr, E0_DIR_PIN, arduidiot_644);
271 stepper_init(avr, &r->step_e, "E", axis_pp_per_mm[3], 0, 0, 0);
272 stepper_connect(&r->step_e, s, d, e, NULL, 0);
277 int main(int argc, char *argv[])
280 strcpy(path, argv[0]);
281 strcpy(path, dirname(path));
282 strcpy(path, dirname(path));
283 printf("Stripped base directory to '%s'\n", path);
288 for (int i = 1; i < argc; i++)
289 if (!strcmp(argv[i], "-d"))
291 avr = avr_make_mcu_by_name("atmega644");
293 fprintf(stderr, "%s: Error creating the AVR core\n", argv[0]);
296 // snprintf(avr_flash_path, sizeof(avr_flash_path), "%s/%s", pwd, "simduino_flash.bin");
297 strcpy(avr_flash_path, "reprap_flash.bin");
298 // register our own functions
299 avr->special_init = avr_special_init;
300 avr->special_deinit = avr_special_deinit;
302 avr->frequency = 20000000;
303 avr->aref = avr->avcc = avr->vcc = 5 * 1000; // needed for ADC
306 const char * fname = "/opt/reprap/tvrrug/Marlin/Marlin/applet/Marlin.elf";
307 // try to load an ELF file, before trying the .hex
308 if (elf_read_firmware(fname, &f) == 0) {
309 printf("firmware %s f=%d mmcu=%s\n", fname, (int)f.frequency, f.mmcu);
310 avr_load_firmware(avr, &f);
314 // snprintf(path, sizeof(path), "%s/%s", pwd, "ATmegaBOOT_168_atmega328.ihex");
315 strcpy(path, "marlin/Marlin.hex");
316 // strcpy(path, "marlin/bootloader-644-20MHz.hex");
317 uint8_t * boot = read_ihex_file(path, &size, &base);
319 fprintf(stderr, "%s: Unable to load %s\n", argv[0], path);
322 printf("Firmware %04x(%04x in AVR talk): %d bytes (%d words)\n", base, base/2, size, size/2);
323 memcpy(avr->flash + base, boot, size);
326 avr->codeend = avr->flashend;
330 // even if not setup at startup, activate gdb if crashing
331 avr->gdb_port = 1234;
333 printf("AVR is stopped, waiting on gdb on port %d. Use 'target remote :%d' in avr-gdb\n",
334 avr->gdb_port, avr->gdb_port);
335 avr->state = cpu_Stopped;
339 // Marlin doesn't loop, sleep, so we don't know when it's idle
340 // I changed Marlin to do a spurious write to the GPIOR0 register so we can trap it
341 avr_register_io_write(avr, MEGA644_GPIOR0, reprap_relief_callback, NULL);
343 reprap_init(avr, &reprap);
347 pthread_create(&run, NULL, avr_run_thread, NULL);