diff --git a/Makefile b/Makefile index 40904bf..5820e74 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,7 @@ PROGRAMMER_ARGS = MAIN = avrrfm.c -SRC = rfm69.c spi.c usart.c +SRC = rfm69.c i2c.c spi.c usart.c CC = avr-gcc OBJCOPY = avr-objcopy @@ -33,7 +33,7 @@ OBJ = $(SRC:.c=.o) OBJ = $(SRC:.S=.o) -$(TARGET).elf: pins.h rfm69.h spi.h usart.h utils.h Makefile +$(TARGET).elf: pins.h rfm69.h i2c.h spi.h usart.h utils.h Makefile all: $(TARGET).hex diff --git a/avrrfm.c b/avrrfm.c index 0716777..5ba9d4f 100644 --- a/avrrfm.c +++ b/avrrfm.c @@ -39,8 +39,8 @@ PORT_SPI |= (1 << PIN_MISO); // set SDA and SCL as output pin - // DDR_I2C |= (1 << PIN_SCL); - // DDR_I2C |= (1 << PIN_SDA); + DDR_I2C |= (1 << PIN_SCL); + DDR_I2C |= (1 << PIN_SDA); // set radio CS and RST pin as output pin DDR_RFM |= (1 << PIN_RCS); diff --git a/i2c.c b/i2c.c new file mode 100644 index 0000000..f6d52c1 --- /dev/null +++ b/i2c.c @@ -0,0 +1,37 @@ +/* + * File: i2c.c + * Author: torsten.roemer@luniks.net + * + * Created on 15. Dezember 2023, 19:01 + */ + +#include "i2c.h" + +void i2cStart(void) { + TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTA); + loop_until_bit_is_set(TWCR, TWINT); +} + +void i2cStop(void) { + TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTO); +} + +void i2cSend(uint8_t data) { + TWDR = data; + TWCR = (1 << TWINT) | (1 << TWEN); + loop_until_bit_is_set(TWCR, TWINT); +} + +uint8_t i2cReadAck(void) { + TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWEA); + loop_until_bit_is_set(TWCR, TWINT); + + return TWDR; +} + +uint8_t i2cReadNack(void) { + TWCR = (1 << TWINT) | (1 << TWEN); + loop_until_bit_is_set(TWCR, TWINT); + + return TWDR; +} \ No newline at end of file diff --git a/i2c.h b/i2c.h new file mode 100644 index 0000000..d62f3af --- /dev/null +++ b/i2c.h @@ -0,0 +1,48 @@ +/* + * File: i2c.h + * Author: torsten.roemer@luniks.net + * + * Created on 15. Dezember 2023, 19:01 + */ + +#ifndef I2C_H +#define I2C_H + +#include +#include // TODO check status register + +#include "pins.h" + +/** + * Begins a message. + */ +void i2cStart(void); + +/** + * Ends a message. + */ +void i2cStop(void); + +/** + * Transmits the given byte of data. + * + * @param data + */ +void i2cSend(uint8_t data); + +/** + * Reads one byte with ack. + * + * @return data + */ +uint8_t i2cReadAck(void); + +/** + * Reads one byte without ack. + * + * @return + */ +uint8_t i2cReadNack(void); + +#endif /* I2C_H */ + diff --git a/rfm69.c b/rfm69.c index 70b66f4..669f6e5 100644 --- a/rfm69.c +++ b/rfm69.c @@ -98,9 +98,9 @@ do { } while (!(regRead(OSC1) & 0x40)); // LNA 200 Ohm, gain AGC (default) - // regWrite(0x18, 0x88); + // regWrite(LNA, 0x88); // reduce gain for transmitter and receiver are so close to each other - regWrite(LNA, 0x8c); + regWrite(LNA, 0x86); // freq of DC offset canceller and channel filter bandwith (default) regWrite(RX_BW, 0x55); @@ -166,7 +166,7 @@ spiSel(); transmit(FIFO); for (size_t i = 0; i < len; i++) { - payload[i] = (transmit(FIFO)); + payload[i] = transmit(FIFO); } spiDes(); diff --git a/utils.h b/utils.h index 3d9a00b..a924c21 100644 --- a/utils.h +++ b/utils.h @@ -35,6 +35,5 @@ _a < _b ? _a : _b; \ }) - #endif /* UTILS_H */