Skip to content

Commit

Permalink
Merge branch seplog:i2c-slave from github (PR buserror#452). Changed …
Browse files Browse the repository at this point in the history
…to fix

include paths in parts/i2c_master.[ch], initialisation of firmware
struct in i2ctest.c (needed after PR buserror#440) and spelling/syntax in README.
  • Loading branch information
ga committed May 20, 2022
1 parent a6077f1 commit 63dabd0
Show file tree
Hide file tree
Showing 7 changed files with 739 additions and 25 deletions.
52 changes: 52 additions & 0 deletions examples/board_i2cslave/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#
# 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 = ../..

IPATH = .
IPATH += ${simavr}/examples/shared
IPATH += ${simavr}/examples/parts
IPATH += ${simavr}/include
IPATH += ${simavr}/simavr/sim

VPATH = .
VPATH += ${simavr}/examples/shared
VPATH += ${simavr}/examples/parts


all: obj ${firmware} ${target}

include ${simavr}/Makefile.common

atmega1280_${target}.axf: atmega1280_${target}.c twimaster.c
atmega1280_${target}.axf: ${simavr}/examples/shared/avr_twi_master.c
atmega1280_${target}.axf: ${simavr}/examples/shared/avr_twi_master.h

board = ${OBJ}/${target}.elf

${board} : ${OBJ}/${target}.o
${board} : ${OBJ}/i2c_eeprom.o

${target}: ${board}
@echo $@ done

clean: clean-${OBJ}
rm -rf *.hex *.a *.axf ${target} *.vcd .*.swo .*.swp .*.swm .*.swn
14 changes: 14 additions & 0 deletions examples/board_i2cslave/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

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 "slave" to talk to it.

The code uses a generic i2c "eeprom" where the AVR writes some bytes,
then reads them again. The AVR code is based on the Atmel reference
implementation, with quite a few changes to make it more functional.

This "board" doesn't use opengl, the eeprom displays the transactions.
150 changes: 150 additions & 0 deletions examples/board_i2cslave/atmega328p_i2cslave.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*
atmega48_i2ctest.c
Copyright 2021 Sebastian Koschmieder <sep@seplog.org>
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 <string.h>

#include <avr/interrupt.h>
#include <avr/io.h>
#include <avr/sleep.h>
#include <util/twi.h>

// for linker, emulator, and programmer's sake
#include "avr_mcu_section.h"
AVR_MCU(F_CPU, "atmega328p");

#include <stdio.h>

void __attribute__((section(".init8"), naked)) initTWI(void) {
TWAR = (0x42 << 1);
TWAMR = 0x7F << 1;
TWCR = (1 << TWEA) | (1 << TWEN);
}

int twi_getState(void) {
while ((TWCR & (1 << TWINT)) == 0)
;
return TWSR & 0xF8;
}

static inline void twi_ack(void) { TWCR = (1 << TWEN) | (1 << TWINT) | (1 << TWEA); }
static inline void twi_nack(void) { TWCR = (1 << TWEN) | (1 << TWINT); }

static const __flash char msg[] = "Hello AVR!";
static char buffer[ 128 ];
static int index;

int twi_receive( char *buffer, size_t size ) {
if (twi_getState() != TW_SR_SLA_ACK) {
abort();
}

index = 0;
twi_ack();

if(twi_getState() != TW_SR_DATA_ACK) {
abort();
}
index = TWDR;
int start = index;
twi_ack();

uint8_t state;
while( ( state = twi_getState() ) == TW_SR_DATA_ACK ) {
buffer[index++] = TWDR;
if( ( index + 1 ) < size ) {
twi_ack();
}
else {
twi_nack();
}
}

if( state != TW_SR_STOP ) {
abort();
}

TWCR = (1 << TWEN) | (1 << TWINT) | (1 << TWEA) | (1 << TWSTO);

return start;
}

int twi_send( char *buffer, size_t size ) {
if (twi_getState() != TW_SR_SLA_ACK) {
abort();
}

index = 0;
twi_ack();

if(twi_getState() != TW_SR_DATA_ACK) {
abort();
}
index = TWDR;
int start = index;
twi_ack();

uint8_t state;

if( ( state = twi_getState() ) == TW_ST_SLA_ACK ) {
TWDR = buffer[index++];
if( ( index + 1 ) < size ) {
twi_ack();
}
else {
twi_nack();
}
}

while( ( state = twi_getState() ) == TW_ST_DATA_ACK ) {
TWDR = buffer[index++];
if( ( index + 1 ) < size ) {
twi_ack();
}
else {
twi_nack();
}
}

if( state != TW_ST_DATA_NACK ) {
abort();
}

TWCR = (1 << TWEN) | (1 << TWINT) | (1 << TWEA) | (1 << TWSTO);
return start;
}

int main() {

memset(&buffer[0], 0xFF, sizeof(buffer));

int start = twi_receive( &buffer[0], sizeof(buffer) );

for( int i = 0; i < sizeof( msg ); i++ ) {
if( buffer[start+i] != msg[i] ) {
abort();
}
}

start = twi_send( &buffer[0], sizeof(buffer) );

cli();
sleep_mode();
}
Loading

0 comments on commit 63dabd0

Please sign in to comment.