1 /* vim: set sts=4:sw=4:ts=4:noexpandtab
4 Copyright 2012 Torbjorn Tyridal <ttyridal@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/>.
34 #include "avr_ioport.h"
39 #include "sim_vcd_file.h"
41 struct vhci_usb_t vhci_usb;
47 char avr_flash_path[1024];
50 // avr special flash initalization
51 // here: open and map a file to enable a persistent storage for the flash memory
52 void avr_special_init( avr_t* avr)
54 //puts(" --=== INIT CALLED ===--");
55 // release flash memory if allocated
56 if(avr->flash) free(avr->flash);
58 avr_flash_fd = open(avr_flash_path, O_RDWR|O_CREAT, 0644);
59 if (avr_flash_fd < 0) {
60 perror(avr_flash_path);
63 // resize and map the file the file
64 (void)ftruncate(avr_flash_fd, avr->flashend + 1);
65 avr->flash = (uint8_t*)mmap(NULL, avr->flashend + 1, // 32k is multiple of 4096
66 PROT_READ|PROT_WRITE, MAP_SHARED, avr_flash_fd, 0);
68 fprintf(stderr, "unable to map memory\n");
69 perror(avr_flash_path);
74 // avr special flash deinitalization
75 // here: cleanup the persistent storage
76 void avr_special_deinit( avr_t* avr)
78 //puts(" --=== DEINIT CALLED ===--");
79 // unmap and close the file
80 munmap( avr->flash, avr->flashend + 1);
82 // signal that cleanup is done
86 int main(int argc, char *argv[])
89 const char * pwd = dirname(argv[0]);
91 avr = avr_make_mcu_by_name("at90usb162");
93 fprintf(stderr, "%s: Error creating the AVR core\n", argv[0]);
96 strcpy(avr_flash_path, "simusb_flash.bin");
97 // register our own functions
98 avr->special_init = avr_special_init;
99 avr->special_deinit = avr_special_deinit;
102 avr->frequency = 8000000;
104 // this trick creates a file that contains /and keep/ the flash
105 // in the same state as it was before. This allow the bootloader
106 // app to be kept, and re-run if the bootloader doesn't get a
111 snprintf(path, sizeof(path), "%s/../%s", pwd, "at90usb162_cdc_loopback.hex");
113 uint8_t * boot = read_ihex_file(path, &size, &base);
115 fprintf(stderr, "%s: Unable to load %s\n", argv[0], path);
118 printf("Booloader %04x: %d\n", base, size);
119 memcpy(avr->flash + base, boot, size);
122 avr->codeend = avr->flashend;
125 // even if not setup at startup, activate gdb if crashing
126 avr->gdb_port = 1234;
128 //avr->state = cpu_Stopped;
132 vhci_usb_init(avr, &vhci_usb);
133 vhci_usb_connect(&vhci_usb, '0');