From ee4cce25870e99c5e39588259df9cf7cdda92bb7 Mon Sep 17 00:00:00 2001 From: Michel Pollet Date: Fri, 6 Apr 2012 16:06:46 +0100 Subject: [PATCH 1/1] i2ctest: Added a mega1280 version Probably will lose either this one, or the older version as they are exactly the same Signed-off-by: Michel Pollet --- examples/board_i2ctest_atmega1280/Makefile | 55 ++++++++++++ examples/board_i2ctest_atmega1280/README | 15 ++++ .../atmega1280_i2ctest.c | 86 ++++++++++++++++++ examples/board_i2ctest_atmega1280/i2ctest.c | 90 +++++++++++++++++++ 4 files changed, 246 insertions(+) create mode 100644 examples/board_i2ctest_atmega1280/Makefile create mode 100644 examples/board_i2ctest_atmega1280/README create mode 100644 examples/board_i2ctest_atmega1280/atmega1280_i2ctest.c create mode 100644 examples/board_i2ctest_atmega1280/i2ctest.c diff --git a/examples/board_i2ctest_atmega1280/Makefile b/examples/board_i2ctest_atmega1280/Makefile new file mode 100644 index 0000000..0cbef69 --- /dev/null +++ b/examples/board_i2ctest_atmega1280/Makefile @@ -0,0 +1,55 @@ +# +# Copyright 2008-2012 Michel Pollet +# +# This file is part of simavr. +# +# simavr is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# simavr is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with simavr. If not, see . + +target= i2ctest +firm_src = ${wildcard at*${board}.c} +firmware = ${firm_src:.c=.axf} +simavr = ../.. +#simavr = /usr/local/lib/simavr + +SHELL = /bin/bash + +IPATH = . +#IPATH += ../parts +IPATH += ${simavr}/examples/parts +IPATH += ${simavr}/include +IPATH += ${simavr}/simavr/sim + +VPATH = . +VPATH += ../parts +VPATH += ../shared + + +all: obj atmega1280_${target}.axf ${target} + +include ${simavr}/Makefile.common + +atmega1280_${target}.axf: atmega1280_${target}.c +atmega1280_${target}.axf: avr_twi_master.c avr_twi_master.h + +board = ${OBJ}/${target}.elf + +${board} : ${OBJ}/${target}.o +${board} : ${OBJ}/i2c_eeprom.o +${board} : ${simavr}/simavr/${OBJ}/libsimavr.a + +${target}: ${board} + @echo $@ done + +clean: clean-${OBJ} + rm -rf *.hex *.a *.axf ${target} *.vcd .*.swo .*.swp .*.swm .*.swn diff --git a/examples/board_i2ctest_atmega1280/README b/examples/board_i2ctest_atmega1280/README new file mode 100644 index 0000000..f65bc4e --- /dev/null +++ b/examples/board_i2ctest_atmega1280/README @@ -0,0 +1,15 @@ + +This contains a sample program to demonstrate the use of simavr +using 'custom' code, and own "peripherals". It shows how it is +possible to "hook" code to the AVR pins, and also how to make +"peripherals" and also hook them up to AVR pins. + +This demo demonstrate how to write a i2c/twi "peripheral" and hook it to +an AVR, and then run a firmware that behaves as a TWI "master" to talk to it. + +The code uses a generic i2c "eeprom" were the AVR writes some bytes, +then read them again. The AVR code is based on the Atmel reference +implementation, with quite a few changes to make it more functional. + +Thid "board" doesn't use opengl, the eeprom will display what the +transactions are. \ No newline at end of file diff --git a/examples/board_i2ctest_atmega1280/atmega1280_i2ctest.c b/examples/board_i2ctest_atmega1280/atmega1280_i2ctest.c new file mode 100644 index 0000000..168bda7 --- /dev/null +++ b/examples/board_i2ctest_atmega1280/atmega1280_i2ctest.c @@ -0,0 +1,86 @@ +/* + atmega48_i2ctest.c + + Copyright 2008-2011 Michel Pollet + + This file is part of simavr. + + simavr is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + simavr is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with simavr. If not, see . + */ + +#include +#include +#include + +// for linker, emulator, and programmer's sake +#include "avr_mcu_section.h" +AVR_MCU(F_CPU, "atmega1280"); + +#include "avr_twi_master.h" + +#include + +static int uart_putchar(char c, FILE *stream) { + if (c == '\n') + uart_putchar('\r', stream); + loop_until_bit_is_set(UCSR0A, UDRE0); + UDR0 = c; + return 0; +} + +static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL, + _FDEV_SETUP_WRITE); + +int main() +{ + stdout = &mystdout; + + sei(); + + TWI_Master_Initialise(); + + { // write 2 bytes at some random address + uint8_t msg[8] = { + 0xa0, // TWI address, + 0xaa, 0x01, // eeprom address, in little endian + 0xde, 0xad, // data bytes + }; + TWI_Start_Transceiver_With_Data(msg, 5, 1); + + while (TWI_Transceiver_Busy()) + sleep_mode(); + } + { + uint8_t msg[8] = { + 0xa0, // TWI address, + 0xa8, 0x01, // eeprom address, in little endian + }; + TWI_Start_Transceiver_With_Data(msg, 3, 0); // dont send stop! + + while (TWI_Transceiver_Busy()) + sleep_mode(); + } + { + uint8_t msg[9] = { + 0xa0 + 1, // TWI address, + }; + TWI_Start_Transceiver_With_Data(msg, 9, 1); // write 1 byte, read 8, send stop + + while (TWI_Transceiver_Busy()) + sleep_mode(); + } + cli(); + sleep_mode(); +} + diff --git a/examples/board_i2ctest_atmega1280/i2ctest.c b/examples/board_i2ctest_atmega1280/i2ctest.c new file mode 100644 index 0000000..7d191de --- /dev/null +++ b/examples/board_i2ctest_atmega1280/i2ctest.c @@ -0,0 +1,90 @@ +/* + i2ctest.c + + Copyright 2008-2011 Michel Pollet + + This file is part of simavr. + + simavr is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + simavr is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with simavr. If not, see . + */ + +#include +#include +#include +#include + +#include "sim_avr.h" +#include "avr_twi.h" +#include "sim_elf.h" +#include "sim_gdb.h" +#include "sim_vcd_file.h" +#include "i2c_eeprom.h" + +avr_t * avr = NULL; +avr_vcd_t vcd_file; + +i2c_eeprom_t ee; + +int main(int argc, char *argv[]) +{ + elf_firmware_t f; + const char * fname = "atmega1280_i2ctest.axf"; + + printf("Firmware pathname is %s\n", fname); + elf_read_firmware(fname, &f); + + printf("firmware %s f=%d mmcu=%s\n", fname, (int)f.frequency, f.mmcu); + + avr = avr_make_mcu_by_name(f.mmcu); + if (!avr) { + fprintf(stderr, "%s: AVR '%s' now known\n", argv[0], f.mmcu); + exit(1); + } + avr_init(avr); + avr_load_firmware(avr, &f); + + // initialize our 'peripheral' + i2c_eeprom_init(avr, &ee, 0xa0, 0xfe, NULL, 1024); + + i2c_eeprom_attach(avr, &ee, AVR_IOCTL_TWI_GETIRQ(0)); + ee.verbose = 1; + + // even if not setup at startup, activate gdb if crashing + avr->gdb_port = 1234; + if (0) { + //avr->state = cpu_Stopped; + avr_gdb_init(avr); + } + + /* + * VCD file initialization + * + * This will allow you to create a "wave" file and display it in gtkwave + * Pressing "r" and "s" during the demo will start and stop recording + * the pin changes + */ +// avr_vcd_init(avr, "gtkwave_output.vcd", &vcd_file, 100000 /* usec */); +// avr_vcd_add_signal(&vcd_file, +// avr_io_getirq(avr, AVR_IOCTL_TWI_GETIRQ(0), TWI_IRQ_STATUS), 8 /* bits */ , +// "TWSR" ); + + printf( "\nDemo launching:\n"); + + int state = cpu_Running; + while((state!= cpu_Done)&&(state != cpu_Crashed )) + state = avr_run(avr); + + printf("\n\nPress enter to terminate the program."); + getchar(); +} -- 2.20.1