From 729f2a2c3ebfb2612d873caf453a1d7ca02180d9 Mon Sep 17 00:00:00 2001 From: dmlunar Date: Wed, 22 Jan 2025 16:47:21 +0200 Subject: varpa: initial public commit --- firmware/src/peripheral/twi.c | 114 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 firmware/src/peripheral/twi.c (limited to 'firmware/src/peripheral/twi.c') diff --git a/firmware/src/peripheral/twi.c b/firmware/src/peripheral/twi.c new file mode 100644 index 0000000..671490b --- /dev/null +++ b/firmware/src/peripheral/twi.c @@ -0,0 +1,114 @@ +/** + * + * Author: Dylan Muller + * Copyright (c) 2025 + * All rights reserved. + * + * - Commercial/IP use prohibited. + * - Attribution required. + * See License.txt + * + */ + +#include "setup.h" + +#include "peripheral/twi.h" + +#include +#include +#include +#include + +#define TWBR_VALUE ((F_CPU/TWI_FREQUENCY - 16) / (2 * TWI_PRESCALER)) + +static uint8_t twi_address; +static uint8_t* twi_data; +static size_t twi_index; +static size_t twi_len; + +void twi_init(void) +{ + TWBR = TWBR_VALUE; + TWSR = (TWPS1_VALUE << TWPS1) | (TWPS0_VALUE << TWPS0); + + TWCR = (1 << TWINT) | (1 << TWEN); +} + +bool twi_busy(void) +{ + return TWCR & (1<