Skip to content

Commit

Permalink
added flag to use internal/external pullup resistors
Browse files Browse the repository at this point in the history
  • Loading branch information
jmatth11 committed Nov 24, 2024
1 parent d8a9269 commit 298462f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 36 deletions.
43 changes: 9 additions & 34 deletions src/driver/i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@
_BV(USIDC) | \
(0xE << USICNT0))// we set the clock to 1110 so it overflows after 1 exchange

/**
* Initialize I2C registers and ports.
*/
void i2c_init() {
void i2c_init(bool internal_pullups) {
// preload data register with default HIGH
i2c_data = 0xff;

Expand All @@ -41,20 +38,20 @@ void i2c_init() {
// reset overflow counter
(0x0 << USICNT0)
);
// flip the ports to input mode so we can enable pullup resistors on the next line
i2c_ddr &= ~_BV(i2c_sda);
i2c_ddr &= ~_BV(i2c_scl);

// set both pins to HIGH to enable pullup.
i2c_port |= (_BV(i2c_sda) | _BV(i2c_scl));
if (internal_pullups) {
// flip the ports to input mode so we can enable pullup resistors on the next line
i2c_ddr &= ~_BV(i2c_sda);
i2c_ddr &= ~_BV(i2c_scl);

// set both pins to HIGH to enable pullup.
i2c_port |= (_BV(i2c_sda) | _BV(i2c_scl));
}

// flip the ports to output mode
i2c_ddr |= (_BV(i2c_sda) | _BV(i2c_scl));
}

/**
* Send start command.
*/
bool i2c_start() {
// ensure both lines are high
i2c_port |= (_BV(i2c_sda) | _BV(i2c_scl));
Expand All @@ -76,9 +73,6 @@ bool i2c_start() {
return (i2c_status & _BV(USISIF));
}

/**
* Send the stop command.
*/
void i2c_stop() {

// ensure data line is low
Expand Down Expand Up @@ -123,12 +117,6 @@ unsigned char transfer(unsigned char mask) {
return data;
}

/**
* Write the given byte.
*
* @param[in] data The byte to send.
* @return The N/ACK byte.
*/
unsigned char i2c_write_byte(unsigned char data) {
i2c_data = data;
transfer(STATUS_CLOCK_8_BITS);
Expand All @@ -141,12 +129,6 @@ unsigned char i2c_write_byte(unsigned char data) {
return nack;
}

/**
* Read the next byte.
*
* @param[in] ack True for reading more, false otherwise.
* @return The read byte.
*/
unsigned char i2c_read_byte(bool ack) {
// HIGH value means stop sending
unsigned char response = I2C_NACK;
Expand All @@ -165,13 +147,6 @@ unsigned char i2c_read_byte(bool ack) {
return data;
}

/**
* Write the target's address out.
*
* @param[in] address The target address.
* @param[in] write Flag for Write or Read bit.
* @return The N/ACK byte.
*/
unsigned char i2c_write_address(unsigned char address, bool write) {
unsigned char rw_flag = 1;
if (write) rw_flag = 0;
Expand Down
7 changes: 6 additions & 1 deletion src/driver/i2c.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,15 @@

/**
* Initialize I2C registers and ports.
*
* @param[in] internal_pullups Flag for using internal pullup resistors,
* False for external.
*/
void i2c_init();
void i2c_init(bool internal_pullups);

/**
* Send start command.
*
* @return True if the start successfully initiated, False otherwise.
*/
bool i2c_start();
Expand All @@ -64,6 +68,7 @@ void i2c_stop();

/**
* Write the given byte.
*
* @param[in] data The byte to send.
* @return The N/ACK byte.
*/
Expand Down
4 changes: 3 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ static void setup_led() {

int main (void) {
setup_led();
i2c_init();
i2c_init(
true // using internal pullup resistors
);
while(1) {
if(!i2c_start()) error_loop();
if (i2c_write_address(TARGET_ADDR, true) & 0x01) {
Expand Down

0 comments on commit 298462f

Please sign in to comment.