From ac78f33c981ef9f947897f7a48cbba4b29c4901a Mon Sep 17 00:00:00 2001 From: "Sebastian H. Gabrielli" Date: Wed, 6 Mar 2024 14:58:49 +0100 Subject: [PATCH] Create basic i2c skeleton with interrupts I2C init function has been created. Interrupts have been made for each I2C interrupt. Which interrupt does what is currently unknown. See #1 --- prosjekt.X/i2c.c | 32 +++++++++++++++++++++++++ prosjekt.X/i2c.h | 26 ++++++++++++++++++++ prosjekt.X/nbproject/configurations.xml | 2 ++ 3 files changed, 60 insertions(+) create mode 100644 prosjekt.X/i2c.c create mode 100644 prosjekt.X/i2c.h diff --git a/prosjekt.X/i2c.c b/prosjekt.X/i2c.c new file mode 100644 index 0000000..6474273 --- /dev/null +++ b/prosjekt.X/i2c.c @@ -0,0 +1,32 @@ +#include "i2c.h" + +void init_i2c(void) { + // Initialize the control A register + TWI0.CTRLA = TWI_INPUTLVL_I2C_gc // I2C voltage transition level + | TWI_SDASETUP_4CYC_gc // Four clock cycles setup time + | TWI_SDAHOLD_50NS_gc // 50ns SDA hold time + | TWI_FMPEN_OFF_gc // Standard SPI timing + ; + + // The device's slave address + TWI0.SADDR = 0x42; + + // Enable acting as a slave + TWI0.SCTRLA = TWI_DIEN_bm // Enable data interrupt + | TWI_PIEN_bm // Enable stop flag interrupt + | TWI_SMEN_bm // Enable smart mode + | TWI_ENABLE_bm // Enable acting as a slave + ; +} + +// TODO: Figure out which interrupt does what + +// Interrupt vector +ISR(TWI0_TWIS_vect) { + asm('nop'); +} + +// Interrupt vector +ISR(TWI0_TWIM_vect) { + asm('nop'); +} \ No newline at end of file diff --git a/prosjekt.X/i2c.h b/prosjekt.X/i2c.h new file mode 100644 index 0000000..7330fd8 --- /dev/null +++ b/prosjekt.X/i2c.h @@ -0,0 +1,26 @@ +/* + * File: i2c.h + * Author: sebgab + * + * Created on March 6, 2024, 1:53 PM + */ + +#ifndef I2C_H +#define I2C_H + +#ifdef __cplusplus +extern "C" { +#endif + +// Include the IO for I2C +#include +#include + +// Initialize the I2C bus +void init_i2c(void); + +#ifdef __cplusplus +} +#endif + +#endif /* I2C_H */ diff --git a/prosjekt.X/nbproject/configurations.xml b/prosjekt.X/nbproject/configurations.xml index 64209ae..e78921c 100644 --- a/prosjekt.X/nbproject/configurations.xml +++ b/prosjekt.X/nbproject/configurations.xml @@ -5,6 +5,7 @@ displayName="Header Files" projectFiles="true"> command-handler.h + i2c.h main.c command-handler.c + i2c.c