i2ctest: Added a mega1280 version
authorMichel Pollet <buserror@gmail.com>
Fri, 6 Apr 2012 15:06:46 +0000 (16:06 +0100)
committerMichel Pollet <buserror@gmail.com>
Fri, 6 Apr 2012 15:06:46 +0000 (16:06 +0100)
Probably will lose either this one, or the older version as they
are exactly the same

Signed-off-by: Michel Pollet <buserror@gmail.com>
examples/board_i2ctest_atmega1280/Makefile [new file with mode: 0644]
examples/board_i2ctest_atmega1280/README [new file with mode: 0644]
examples/board_i2ctest_atmega1280/atmega1280_i2ctest.c [new file with mode: 0644]
examples/board_i2ctest_atmega1280/i2ctest.c [new file with mode: 0644]

diff --git a/examples/board_i2ctest_atmega1280/Makefile b/examples/board_i2ctest_atmega1280/Makefile
new file mode 100644 (file)
index 0000000..0cbef69
--- /dev/null
@@ -0,0 +1,55 @@
+# 
+#      Copyright 2008-2012 Michel Pollet <buserror@gmail.com>
+#
+#      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 <http://www.gnu.org/licenses/>.
+
+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 (file)
index 0000000..f65bc4e
--- /dev/null
@@ -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 (file)
index 0000000..168bda7
--- /dev/null
@@ -0,0 +1,86 @@
+/*
+       atmega48_i2ctest.c
+
+       Copyright 2008-2011 Michel Pollet <buserror@gmail.com>
+
+       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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <avr/io.h>
+#include <avr/interrupt.h>
+#include <avr/sleep.h>
+
+// for linker, emulator, and programmer's sake
+#include "avr_mcu_section.h"
+AVR_MCU(F_CPU, "atmega1280");
+
+#include "avr_twi_master.h"
+
+#include <stdio.h>
+
+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 (file)
index 0000000..7d191de
--- /dev/null
@@ -0,0 +1,90 @@
+/*
+       i2ctest.c
+
+       Copyright 2008-2011 Michel Pollet <buserror@gmail.com>
+
+       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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <libgen.h>
+#include <pthread.h>
+
+#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();
+}