Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I2c slave implementation #452

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
52 changes: 52 additions & 0 deletions examples/board_i2cslave/Makefile
@@ -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
15 changes: 15 additions & 0 deletions examples/board_i2cslave/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 "slave" 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.
150 changes: 150 additions & 0 deletions examples/board_i2cslave/atmega328p_i2cslave.c
@@ -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();
}