c3gl: Implemented buffer objects and vertex array objects
[simavr] / examples / board_i2ctest / atmega1280_i2ctest.c
1 /*
2         atmega48_i2ctest.c
3
4         Copyright 2008-2011 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
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.
12
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.
17
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/>.
20  */
21
22 #include <avr/io.h>
23 #include <avr/interrupt.h>
24 #include <avr/sleep.h>
25
26 // for linker, emulator, and programmer's sake
27 #include "avr_mcu_section.h"
28 AVR_MCU(F_CPU, "atmega1280");
29
30 #include "../shared/avr_twi_master.h"
31
32 #include <stdio.h>
33
34 static int uart_putchar(char c, FILE *stream) {
35   if (c == '\n')
36     uart_putchar('\r', stream);
37   loop_until_bit_is_set(UCSR0A, UDRE0);
38   UDR0 = c;
39   return 0;
40 }
41
42 static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL,
43                                          _FDEV_SETUP_WRITE);
44
45 int main()
46 {
47         stdout = &mystdout;
48
49         sei();
50
51         TWI_Master_Initialise();
52
53         {       // write 2 bytes at some random address
54                 uint8_t msg[8] = {
55                                 0xa0, // TWI address,
56                                 0xaa, 0x01, // eeprom address, in little endian
57                                 0xde, 0xad,     // data bytes
58                 };
59                 TWI_Start_Transceiver_With_Data(msg, 5, 1);
60
61                 while (TWI_Transceiver_Busy())
62                         sleep_mode();
63         }
64         {
65                 uint8_t msg[8] = {
66                                 0xa0, // TWI address,
67                                 0xa8, 0x01, // eeprom address, in little endian
68                 };
69                 TWI_Start_Transceiver_With_Data(msg, 3, 0); // dont send stop!
70
71                 while (TWI_Transceiver_Busy())
72                         sleep_mode();
73         }
74         {
75                 uint8_t msg[9] = {
76                                 0xa0 + 1, // TWI address,
77                 };
78                 TWI_Start_Transceiver_With_Data(msg, 9, 1); // write 1 byte, read 8, send stop
79
80                 while (TWI_Transceiver_Busy())
81                         sleep_mode();
82         }
83         cli();
84         sleep_mode();
85 }
86