Add files via upload

This commit is contained in:
agsNeto20
2020-12-14 23:04:32 +00:00
committed by GitHub
parent b804d9acf4
commit 132a34e895
22 changed files with 1939 additions and 0 deletions
+154
View File
@@ -0,0 +1,154 @@
void DS3231::begin()
{
_use_hw = false;
if ((_sda_pin == SDA) and (_scl_pin == SCL))
{
_use_hw = true;
twi = TWI1;
pmc_enable_periph_clk(WIRE_INTERFACE_ID);
PIO_Configure(g_APinDescription[PIN_WIRE_SDA].pPort, g_APinDescription[PIN_WIRE_SDA].ulPinType, g_APinDescription[PIN_WIRE_SDA].ulPin, g_APinDescription[PIN_WIRE_SDA].ulPinConfiguration);
PIO_Configure(g_APinDescription[PIN_WIRE_SCL].pPort, g_APinDescription[PIN_WIRE_SCL].ulPinType, g_APinDescription[PIN_WIRE_SCL].ulPin, g_APinDescription[PIN_WIRE_SCL].ulPinConfiguration);
NVIC_DisableIRQ(TWI1_IRQn);
NVIC_ClearPendingIRQ(TWI1_IRQn);
NVIC_SetPriority(TWI1_IRQn, 0);
NVIC_EnableIRQ(TWI1_IRQn);
}
else if ((_sda_pin == SDA1) and (_scl_pin == SCL1))
{
_use_hw = true;
twi = TWI0;
pmc_enable_periph_clk(WIRE1_INTERFACE_ID);
PIO_Configure(g_APinDescription[PIN_WIRE1_SDA].pPort, g_APinDescription[PIN_WIRE1_SDA].ulPinType, g_APinDescription[PIN_WIRE1_SDA].ulPin, g_APinDescription[PIN_WIRE1_SDA].ulPinConfiguration);
PIO_Configure(g_APinDescription[PIN_WIRE1_SCL].pPort, g_APinDescription[PIN_WIRE1_SCL].ulPinType, g_APinDescription[PIN_WIRE1_SCL].ulPin, g_APinDescription[PIN_WIRE1_SCL].ulPinConfiguration);
NVIC_DisableIRQ(TWI0_IRQn);
NVIC_ClearPendingIRQ(TWI0_IRQn);
NVIC_SetPriority(TWI0_IRQn, 0);
NVIC_EnableIRQ(TWI0_IRQn);
}
if (_use_hw)
{
// activate internal pullups for twi.
digitalWrite(SDA, 1);
digitalWrite(SCL, 1);
// Reset the TWI
twi->TWI_CR = TWI_CR_SWRST;
// TWI Slave Mode Disabled, TWI Master Mode Disabled.
twi->TWI_CR = TWI_CR_SVDIS;
twi->TWI_CR = TWI_CR_MSDIS;
// Set TWI Speed
twi->TWI_CWGR = (TWI_DIV << 16) | (TWI_SPEED << 8) | TWI_SPEED;
// Set master mode
twi->TWI_CR = TWI_CR_MSEN;
}
else
{
pinMode(_scl_pin, OUTPUT);
}
}
void DS3231::_burstRead()
{
if (_use_hw)
{
// Set slave address and number of internal address bytes.
twi->TWI_MMR = (1 << 8) | TWI_MMR_MREAD | (DS3231_ADDR << 16);
// Set internal address bytes
twi->TWI_IADR = 0;
// Send START condition
twi->TWI_CR = TWI_CR_START;
for (int i=0; i<6; i++)
{
while ((twi->TWI_SR & TWI_SR_RXRDY) != TWI_SR_RXRDY)
{
};
_burstArray[i] = twi->TWI_RHR;
}
twi->TWI_CR = TWI_CR_STOP;
while ((twi->TWI_SR & TWI_SR_RXRDY) != TWI_SR_RXRDY) {};
_burstArray[6] = twi->TWI_RHR;
while ((twi->TWI_SR & TWI_SR_TXCOMP) != TWI_SR_TXCOMP) {};
}
else
{
_sendStart(DS3231_ADDR_W);
_waitForAck();
_writeByte(0);
_waitForAck();
_sendStart(DS3231_ADDR_R);
_waitForAck();
for (int i=0; i<7; i++)
{
_burstArray[i] = _readByte();
if (i<6)
_sendAck();
else
_sendNack();
}
_sendStop();
}
}
uint8_t DS3231::_readRegister(uint8_t reg)
{
uint8_t readValue=0;
if (_use_hw)
{
// Set slave address and number of internal address bytes.
twi->TWI_MMR = (1 << 8) | TWI_MMR_MREAD | (DS3231_ADDR << 16);
// Set internal address bytes
twi->TWI_IADR = reg;
// Send START and STOP condition to read a single byte
twi->TWI_CR = TWI_CR_START | TWI_CR_STOP;
while ((twi->TWI_SR & TWI_SR_RXRDY) != TWI_SR_RXRDY) {};
readValue = twi->TWI_RHR;
while ((twi->TWI_SR & TWI_SR_TXCOMP) != TWI_SR_TXCOMP) {};
}
else
{
_sendStart(DS3231_ADDR_W);
_waitForAck();
_writeByte(reg);
_waitForAck();
_sendStart(DS3231_ADDR_R);
_waitForAck();
readValue = _readByte();
_sendNack();
_sendStop();
}
return readValue;
}
void DS3231::_writeRegister(uint8_t reg, uint8_t value)
{
if (_use_hw)
{
// Set slave address and number of internal address bytes.
twi->TWI_MMR = (1 << 8) | (DS3231_ADDR << 16);
// Set internal address bytes
twi->TWI_IADR = reg;
// Send a single byte to start transfer
twi->TWI_THR = value;
while ((twi->TWI_SR & TWI_SR_TXRDY) != TWI_SR_TXRDY) {};
// Send STOP condition
twi->TWI_CR = TWI_CR_STOP;
while ((twi->TWI_SR & TWI_SR_TXCOMP) != TWI_SR_TXCOMP) {};
}
else
{
_sendStart(DS3231_ADDR_W);
_waitForAck();
_writeByte(reg);
_waitForAck();
_writeByte(value);
_waitForAck();
_sendStop();
}
}
+13
View File
@@ -0,0 +1,13 @@
// *** Hardwarespecific defines ***
#define SDA 20
#define SCL 21
#define SDA1 70
#define SCL1 71
#define TWI_SPEED TWI_SPEED_400k // Set default TWI Speed
#define TWI_SPEED_100k 208
#define TWI_SPEED_400k 101
#define TWI_DIV TWI_DIV_400k // Set divider for TWI Speed (must match TWI_SPEED setting)
#define TWI_DIV_100k 1
#define TWI_DIV_400k 0
+153
View File
@@ -0,0 +1,153 @@
void DS3231::begin()
{
if ((_sda_pin == SDA) and (_scl_pin == SCL))
{
_use_hw = true;
// activate internal pullups for twi.
digitalWrite(SDA, HIGH);
digitalWrite(SCL, HIGH);
//delay(1); // Workaround for a linker bug
// initialize twi prescaler and bit rate
cbi(TWSR, TWPS0);
cbi(TWSR, TWPS1);
TWBR = ((F_CPU / TWI_FREQ) - 16) / 2;
// enable twi module, acks, and twi interrupt
TWCR = _BV(TWEN) | _BV(TWIE)/* | _BV(TWEA)*/;
}
else
{
_use_hw = false;
pinMode(_scl_pin, OUTPUT);
}
}
void DS3231::_burstRead()
{
if (_use_hw)
{
// Send start address
TWCR = _BV(TWEN) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTA); // Send START
while ((TWCR & _BV(TWINT)) == 0) {}; // Wait for TWI to be ready
TWDR = DS3231_ADDR_W;
TWCR = _BV(TWEN) | _BV(TWINT) | _BV(TWEA); // Clear TWINT to proceed
while ((TWCR & _BV(TWINT)) == 0) {}; // Wait for TWI to be ready
TWDR = 0;
TWCR = _BV(TWEN) | _BV(TWINT) | _BV(TWEA); // Clear TWINT to proceed
while ((TWCR & _BV(TWINT)) == 0) {}; // Wait for TWI to be ready
// Read data starting from start address
TWCR = _BV(TWEN) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTA); // Send rep. START
while ((TWCR & _BV(TWINT)) == 0) {}; // Wait for TWI to be ready
TWDR = DS3231_ADDR_R;
TWCR = _BV(TWEN) | _BV(TWINT) | _BV(TWEA); // Clear TWINT to proceed
while ((TWCR & _BV(TWINT)) == 0) {}; // Wait for TWI to be ready
for (int i=0; i<7; i++)
{
TWCR = _BV(TWEN) | _BV(TWINT) | _BV(TWEA); // Send ACK and clear TWINT to proceed
while ((TWCR & _BV(TWINT)) == 0) {}; // Wait for TWI to be ready
_burstArray[i] = TWDR;
}
TWCR = _BV(TWEN) | _BV(TWINT); // Send NACK and clear TWINT to proceed
while ((TWCR & _BV(TWINT)) == 0) {}; // Wait for TWI to be ready
TWCR = _BV(TWEN)| _BV(TWINT) | _BV(TWSTO); // Send STOP
}
else
{
_sendStart(DS3231_ADDR_W);
_waitForAck();
_writeByte(0);
_waitForAck();
_sendStart(DS3231_ADDR_R);
_waitForAck();
for (int i=0; i<7; i++)
{
_burstArray[i] = _readByte();
if (i<6)
_sendAck();
else
_sendNack();
}
_sendStop();
}
}
uint8_t DS3231::_readRegister(uint8_t reg)
{
uint8_t readValue=0;
if (_use_hw)
{
// Send start address
TWCR = _BV(TWEN) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTA); // Send START
while ((TWCR & _BV(TWINT)) == 0) {}; // Wait for TWI to be ready
TWDR = DS3231_ADDR_W;
TWCR = _BV(TWEN) | _BV(TWINT) | _BV(TWEA); // Clear TWINT to proceed
while ((TWCR & _BV(TWINT)) == 0) {}; // Wait for TWI to be ready
TWDR = reg;
TWCR = _BV(TWEN) | _BV(TWINT) | _BV(TWEA); // Clear TWINT to proceed
while ((TWCR & _BV(TWINT)) == 0) {}; // Wait for TWI to be ready
// Read data starting from start address
TWCR = _BV(TWEN) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTA); // Send rep. START
while ((TWCR & _BV(TWINT)) == 0) {}; // Wait for TWI to be ready
TWDR = DS3231_ADDR_R;
TWCR = _BV(TWEN) | _BV(TWINT) | _BV(TWEA); // Clear TWINT to proceed
while ((TWCR & _BV(TWINT)) == 0) {}; // Wait for TWI to be ready
TWCR = _BV(TWEN) | _BV(TWINT) | _BV(TWEA); // Send ACK and clear TWINT to proceed
while ((TWCR & _BV(TWINT)) == 0) {}; // Wait for TWI to be ready
readValue = TWDR;
TWCR = _BV(TWEN) | _BV(TWINT); // Send NACK and clear TWINT to proceed
while ((TWCR & _BV(TWINT)) == 0) {}; // Wait for TWI to be ready
TWCR = _BV(TWEN)| _BV(TWINT) | _BV(TWSTO); // Send STOP
}
else
{
_sendStart(DS3231_ADDR_W);
_waitForAck();
_writeByte(reg);
_waitForAck();
_sendStart(DS3231_ADDR_R);
_waitForAck();
readValue = _readByte();
_sendNack();
_sendStop();
}
return readValue;
}
void DS3231::_writeRegister(uint8_t reg, uint8_t value)
{
if (_use_hw)
{
// Send start address
TWCR = _BV(TWEN) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTA); // Send START
while ((TWCR & _BV(TWINT)) == 0) {}; // Wait for TWI to be ready
TWDR = DS3231_ADDR_W;
TWCR = _BV(TWEN) | _BV(TWINT) | _BV(TWEA); // Clear TWINT to proceed
while ((TWCR & _BV(TWINT)) == 0) {}; // Wait for TWI to be ready
TWDR = reg;
TWCR = _BV(TWEN) | _BV(TWINT) | _BV(TWEA); // Clear TWINT to proceed
while ((TWCR & _BV(TWINT)) == 0) {}; // Wait for TWI to be ready
TWDR = value;
TWCR = _BV(TWEN) | _BV(TWINT) | _BV(TWEA); // Clear TWINT to proceed
while ((TWCR & _BV(TWINT)) == 0) {}; // Wait for TWI to be ready
TWCR = _BV(TWEN)| _BV(TWINT) | _BV(TWSTO); // Send STOP
}
else
{
_sendStart(DS3231_ADDR_W);
_waitForAck();
_writeByte(reg);
_waitForAck();
_writeByte(value);
_waitForAck();
_sendStop();
}
}
@@ -0,0 +1,6 @@
// *** Hardwarespecific defines ***
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#ifndef TWI_FREQ
#define TWI_FREQ 400000L
#endif
+192
View File
@@ -0,0 +1,192 @@
inline void _waitForIdleBus() { while (I2C1CON & 0x1f) {} }
void DS3231::begin()
{
if ((_sda_pin == SDA) and (_scl_pin == SCL))
{
uint32_t tpgd;
_use_hw = true;
pinMode(SDA, OUTPUT);
digitalWrite(SDA, HIGH);
IFS0CLR = 0xE0000000; // Clear Interrupt Flag
IEC0CLR = 0xE0000000; // Disable Interrupt
I2C1CONCLR = (1 << _I2CCON_ON); // Disable I2C interface
tpgd = ((F_CPU / 8) * 104) / 125000000;
I2C1BRG = (F_CPU / (2 * TWI_FREQ) - tpgd) - 2; // Set I2C Speed
I2C1ADD = DS3231_ADDR; // Set I2C device address
I2C1CONSET = (1 << _I2CCON_ON) | (1 << _I2CCON_STREN); // Enable I2C Interface
}
else
{
_use_hw = false;
pinMode(_scl_pin, OUTPUT);
}
}
void DS3231::_burstRead()
{
if (_use_hw)
{
_waitForIdleBus(); // Wait for I2C bus to be Idle before starting
I2C1CONSET = (1 << _I2CCON_SEN); // Send start condition
if (I2C1STAT & (1 << _I2CSTAT_BCL)) { return; } // Check if there is a bus collision
while (I2C1CON & (1 << _I2CCON_SEN)) {} // Wait for start condition to finish
I2C1TRN = (DS3231_ADDR<<1); // Send device Write address
while (I2C1STAT & (1 << _I2CSTAT_IWCOL)) // Check if there is a Write collision
{
I2C1STATCLR = (1 << _I2CSTAT_IWCOL); // Clear Write collision flag
I2C1TRN = (DS3231_ADDR<<1); // Retry send device Write address
}
while (I2C1STAT & (1 << _I2CSTAT_TRSTAT)) {} // Wait for transmit to finish
while (I2C1STAT & (1 << _I2CSTAT_ACKSTAT)) {} // Wait for ACK
I2C1TRN = 0; // Send the register address
while (I2C1STAT & (1 << _I2CSTAT_TRSTAT)) {} // Wait for transmit to finish
while (I2C1STAT & (1 << _I2CSTAT_ACKSTAT)) {} // Wait for ACK
_waitForIdleBus(); // Wait for I2C bus to be Idle before starting
I2C1CONSET = (1 << _I2CCON_RSEN); // Send start condition
if (I2C1STAT & (1 << _I2CSTAT_BCL)) { return; } // Check if there is a bus collision
while (I2C1CON & (1 << _I2CCON_RSEN)) {} // Wait for start condition to finish
I2C1TRN = (DS3231_ADDR<<1) | 1; // Send device Read address
while (I2C1STAT & (1 << _I2CSTAT_IWCOL)) // Check if there is a Write collision
{
I2C1STATCLR = (1 << _I2CSTAT_IWCOL); // Clear Write collision flag
I2C1TRN = (DS3231_ADDR<<1) | 1; // Retry send device Read address
}
while (I2C1STAT & (1 << _I2CSTAT_TRSTAT)) {} // Wait for transmit to finish
while (I2C1STAT & (1 << _I2CSTAT_ACKSTAT)) {} // Wait for ACK
byte dummy = I2C1RCV; // Clear _I2CSTAT_RBF (Receive Buffer Full)
for (int i=0; i<7; i++)
{
_waitForIdleBus(); // Wait for I2C bus to be Idle before continuing
I2C1CONSET = (1 << _I2CCON_RCEN); // Set RCEN to start receive
while (I2C1CON & (1 << _I2CCON_RCEN)) {} // Wait for Receive operation to finish
while (!(I2C1STAT & (1 << _I2CSTAT_RBF))) {} // Wait for Receive Buffer Full
_burstArray[i] = I2C1RCV; // Read data
if (i == 6)
I2C1CONSET = (1 << _I2CCON_ACKDT); // Prepare to send NACK
else
I2C1CONCLR = (1 << _I2CCON_ACKDT); // Prepare to send ACK
I2C1CONSET = (1 << _I2CCON_ACKEN); // Send ACK/NACK
while (I2C1CON & (1 << _I2CCON_ACKEN)) {} // Wait for ACK/NACK send to finish
}
I2C1CONSET = (1 << _I2CCON_PEN); // Send stop condition
while (I2C1CON & (1 << _I2CCON_PEN)) {} // Wait for stop condition to finish
}
else
{
_sendStart(DS3231_ADDR_W);
_waitForAck();
_writeByte(0);
_waitForAck();
_sendStart(DS3231_ADDR_R);
_waitForAck();
for (int i=0; i<7; i++)
{
_burstArray[i] = _readByte();
if (i<6)
_sendAck();
else
_sendNack();
}
_sendStop();
}
}
uint8_t DS3231::_readRegister(uint8_t reg)
{
uint8_t readValue=0;
if (_use_hw)
{
_waitForIdleBus(); // Wait for I2C bus to be Idle before starting
I2C1CONSET = (1 << _I2CCON_SEN); // Send start condition
if (I2C1STAT & (1 << _I2CSTAT_BCL)) { return 0xff; }// Check if there is a bus collision
while (I2C1CON & (1 << _I2CCON_SEN)) {} // Wait for start condition to finish
I2C1TRN = (DS3231_ADDR<<1); // Send device Write address
while (I2C1STAT & (1 << _I2CSTAT_IWCOL)) // Check if there is a Write collision
{
I2C1STATCLR = (1 << _I2CSTAT_IWCOL); // Clear Write collision flag
I2C1TRN = (DS3231_ADDR<<1); // Retry send device Write address
}
while (I2C1STAT & (1 << _I2CSTAT_TRSTAT)) {} // Wait for transmit to finish
while (I2C1STAT & (1 << _I2CSTAT_ACKSTAT)) {} // Wait for ACK
I2C1TRN = reg; // Send the register address
while (I2C1STAT & (1 << _I2CSTAT_TRSTAT)) {} // Wait for transmit to finish
while (I2C1STAT & (1 << _I2CSTAT_ACKSTAT)) {} // Wait for ACK
_waitForIdleBus(); // Wait for I2C bus to be Idle before starting
I2C1CONSET = (1 << _I2CCON_RSEN); // Send start condition
if (I2C1STAT & (1 << _I2CSTAT_BCL)) { return 0xff; }// Check if there is a bus collision
while (I2C1CON & (1 << _I2CCON_RSEN)) {} // Wait for start condition to finish
I2C1TRN = (DS3231_ADDR<<1) | 1; // Send device Read address
while (I2C1STAT & (1 << _I2CSTAT_IWCOL)) // Check if there is a Write collision
{
I2C1STATCLR = (1 << _I2CSTAT_IWCOL); // Clear Write collision flag
I2C1TRN = (DS3231_ADDR<<1) | 1; // Retry send device Read address
}
while (I2C1STAT & (1 << _I2CSTAT_TRSTAT)) {} // Wait for transmit to finish
while (I2C1STAT & (1 << _I2CSTAT_ACKSTAT)) {} // Wait for ACK
byte dummy = I2C1RCV; // Clear _I2CSTAT_RBF (Receive Buffer Full)
_waitForIdleBus(); // Wait for I2C bus to be Idle before continuing
I2C1CONSET = (1 << _I2CCON_RCEN); // Set RCEN to start receive
while (I2C1CON & (1 << _I2CCON_RCEN)) {} // Wait for Receive operation to finish
while (!(I2C1STAT & (1 << _I2CSTAT_RBF))) {} // Wait for Receive Buffer Full
readValue = I2C1RCV; // Read data
I2C1CONSET = (1 << _I2CCON_ACKDT); // Prepare to send NACK
I2C1CONSET = (1 << _I2CCON_ACKEN); // Send NACK
while (I2C1CON & (1 << _I2CCON_ACKEN)) {} // Wait for NACK send to finish
I2C1CONSET = (1 << _I2CCON_PEN); // Send stop condition
while (I2C1CON & (1 << _I2CCON_PEN)) {} // Wait for stop condition to finish
}
else
{
_sendStart(DS3231_ADDR_W);
_waitForAck();
_writeByte(reg);
_waitForAck();
_sendStart(DS3231_ADDR_R);
_waitForAck();
readValue = _readByte();
_sendNack();
_sendStop();
}
return readValue;
}
void DS3231::_writeRegister(uint8_t reg, uint8_t value)
{
if (_use_hw)
{
_waitForIdleBus(); // Wait for I2C bus to be Idle before starting
I2C1CONSET = (1 << _I2CCON_SEN); // Send start condition
if (I2C1STAT & (1 << _I2CSTAT_BCL)) { return; } // Check if there is a bus collision
while (I2C1CON & (1 << _I2CCON_SEN)) {} // Wait for start condition to finish
I2C1TRN = (DS3231_ADDR<<1); // Send device Write address
while (I2C1STAT & (1 << _I2CSTAT_IWCOL)) // Check if there is a Write collision
{
I2C1STATCLR = (1 << _I2CSTAT_IWCOL); // Clear Write collision flag
I2C1TRN = (DS3231_ADDR<<1); // Retry send device Write address
}
while (I2C1STAT & (1 << _I2CSTAT_TRSTAT)) {} // Wait for transmit to finish
while (I2C1STAT & (1 << _I2CSTAT_ACKSTAT)) {} // Wait for ACK
I2C1TRN = reg; // Send the 1st data byte
while (I2C1STAT & (1 << _I2CSTAT_TRSTAT)) {} // Wait for transmit to finish
while (I2C1STAT & (1 << _I2CSTAT_ACKSTAT)) {} // Wait for ACK
I2C1TRN = value; // Send the 2nd data byte
while (I2C1STAT & (1 << _I2CSTAT_TRSTAT)) {} // Wait for transmit to finish
while (I2C1STAT & (1 << _I2CSTAT_ACKSTAT)) {} // Wait for ACK
I2C1CONSET = (1 << _I2CCON_PEN); // Send stop condition
while (I2C1CON & (1 << _I2CCON_PEN)) {} // Wait for stop condition to finish
}
else
{
_sendStart(DS3231_ADDR_W);
_waitForAck();
_writeByte(reg);
_waitForAck();
_writeByte(value);
_waitForAck();
_sendStop();
}
}
@@ -0,0 +1,21 @@
// *** Hardwarespecific defines ***
#if !defined(_UP_MCU_)
#if defined(__32MX320F128H__)
#define SDA 18 // A4 (Remeber to set the jumper correctly)
#define SCL 19 // A5 (Remeber to set the jumper correctly)
#elif defined(__32MX340F512H__)
#define SDA 18 // A4 (Remeber to set the jumper correctly)
#define SCL 19 // A5 (Remeber to set the jumper correctly)
#elif defined(__32MX795F512L__)
#define SDA 20 // Digital 20
#define SCL 21 // Digital 21
#else
#error "Unsupported PIC32 MCU!"
#endif
#endif
#ifndef TWI_FREQ
#define TWI_FREQ 400000L
#endif