Skip to content

Commit

Permalink
Testing compiler time issue..
Browse files Browse the repository at this point in the history
  • Loading branch information
dattasaurabh82 committed Jul 25, 2021
1 parent b359194 commit 60fa2ad
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 42 deletions.
67 changes: 43 additions & 24 deletions src/TinyMegaI2C.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,58 +10,77 @@

#include "TinyMegaI2C.h"

TinyMegaI2CMaster::TinyMegaI2CMaster(void) {
TinyMegaI2CMaster::TinyMegaI2CMaster(void)
{
}


void TinyMegaI2CMaster::init () {
void TinyMegaI2CMaster::init()
{
pinMode(PIN_WIRE_SDA, INPUT_PULLUP);
pinMode(PIN_WIRE_SCL, INPUT_PULLUP);
uint32_t baud = ((F_CPU / FREQUENCY) - (((F_CPU * T_RISE) / 1000) / 1000) / 1000 - 10) / 2;
TWI0.MBAUD = (uint8_t)baud;
TWI0.MCTRLA = TWI_ENABLE_bm; // Enable as master, no interrupts
TWI0.MCTRLA = TWI_ENABLE_bm; // Enable as master, no interrupts
TWI0.MSTATUS = TWI_BUSSTATE_IDLE_gc;
}

uint8_t TinyMegaI2CMaster::read (void) {
if (I2Ccount != 0) I2Ccount--;
while (!(TWI0.MSTATUS & TWI_RIF_bm)); // Wait for read interrupt flag
uint8_t TinyMegaI2CMaster::read(void)
{
if (I2Ccount != 0)
I2Ccount--;
while (!(TWI0.MSTATUS & TWI_RIF_bm))
; // Wait for read interrupt flag
uint8_t data = TWI0.MDATA;
// Check slave sent ACK?
if (I2Ccount != 0) TWI0.MCTRLB = TWI_MCMD_RECVTRANS_gc; // if ACK = more bytes to read
else TWI0.MCTRLB = TWI_ACKACT_bm | TWI_MCMD_RECVTRANS_gc; // else Send NAK
if (I2Ccount != 0)
TWI0.MCTRLB = TWI_MCMD_RECVTRANS_gc; // if ACK = more bytes to read
else
TWI0.MCTRLB = TWI_ACKACT_bm | TWI_MCMD_RECVTRANS_gc; // else Send NAK
return data;
}

uint8_t TinyMegaI2CMaster::readLast (void) {
uint8_t TinyMegaI2CMaster::readLast(void)
{
I2Ccount = 0;
return TinyMegaI2CMaster::read();
}

bool TinyMegaI2CMaster::write (uint8_t data) {
while (!(TWI0.MSTATUS & TWI_WIF_bm)); // Wait for write interrupt flag
bool TinyMegaI2CMaster::write(uint8_t data)
{
while (!(TWI0.MSTATUS & TWI_WIF_bm))
; // Wait for write interrupt flag
TWI0.MDATA = data;
TWI0.MCTRLB = TWI_MCMD_RECVTRANS_gc; // Do nothing
return !(TWI0.MSTATUS & TWI_RXACK_bm); // Returns true if slave gave an ACK
TWI0.MCTRLB = TWI_MCMD_RECVTRANS_gc; // Do nothing
return !(TWI0.MSTATUS & TWI_RXACK_bm); // Returns true if slave gave an ACK
}

// Start transmission by sending address
bool TinyMegaI2CMaster::start (uint8_t address, int readcount) {
bool TinyMegaI2CMaster::start(uint8_t address, int readcount)
{
bool read;
if (readcount == 0) read = 0; // Write
else { I2Ccount = readcount; read = 1; } // Read
TWI0.MADDR = address<<1 | read; // Send START condition
while (!(TWI0.MSTATUS & (TWI_WIF_bm | TWI_RIF_bm))); // Wait for write or read interrupt flag
if ((TWI0.MSTATUS & TWI_ARBLOST_bm)) return false; // Return false if arbitration lost or bus error
return !(TWI0.MSTATUS & TWI_RXACK_bm); // Return true if slave gave an ACK
if (readcount == 0)
read = 0; // Write
else
{
I2Ccount = readcount;
read = 1;
} // Read
TWI0.MADDR = address << 1 | read; // Send START condition
while (!(TWI0.MSTATUS & (TWI_WIF_bm | TWI_RIF_bm)))
; // Wait for write or read interrupt flag
if ((TWI0.MSTATUS & TWI_ARBLOST_bm))
return false; // Return false if arbitration lost or bus error
return !(TWI0.MSTATUS & TWI_RXACK_bm); // Return true if slave gave an ACK
}

bool TinyMegaI2CMaster::restart(uint8_t address, int readcount) {
bool TinyMegaI2CMaster::restart(uint8_t address, int readcount)
{
return TinyMegaI2CMaster::start(address, readcount);
}

void TinyMegaI2CMaster::stop (void) {
TWI0.MCTRLB = TWI_ACKACT_bm | TWI_MCMD_STOP_gc; // Send STOP
void TinyMegaI2CMaster::stop(void)
{
TWI0.MCTRLB = TWI_ACKACT_bm | TWI_MCMD_STOP_gc; // Send STOP
}

TinyMegaI2CMaster TinyMegaI2C = TinyMegaI2CMaster();
48 changes: 30 additions & 18 deletions src/TinyMegaI2C.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,42 @@
uC: ATTINY-1607
******************************************************************************/

#pragma once
// #pragma once

// #ifndef TinyMegaI2CMaster_h
// #define TinyMegaI2CMaster_h

#ifndef TinyMegaI2C_h
#define TinyMegaI2C_h

#include <stdint.h>
#include <Arduino.h>
#include <avr/io.h>

// 400kHz clock
uint32_t const FREQUENCY = 400000L; // Hardware I2C clock in Hz
uint32_t const T_RISE = 300L; // Rise time

class TinyMegaI2CMaster {
public:
TinyMegaI2CMaster( void );

void init(void);

uint8_t read(void);
uint8_t readLast(void);
bool write(uint8_t data);
bool start(uint8_t address, int readcount);
bool restart(uint8_t address, int readcount);
void stop(void);
private:
int I2Ccount;
uint32_t const FREQUENCY = 400000L; // Hardware I2C clock in Hz
uint32_t const T_RISE = 300L; // Rise time

class TinyMegaI2CMaster
{
public:
// TinyMegaI2CMaster(void);
TinyMegaI2CMaster(void);

void init(void);

uint8_t read(void);
uint8_t readLast(void);

bool write(uint8_t data);
bool start(uint8_t address, int readcount);
bool restart(uint8_t address, int readcount);
void stop(void);

private:
int I2Ccount;
};

extern TinyMegaI2CMaster TinyMegaI2C;

#endif

0 comments on commit 60fa2ad

Please sign in to comment.