Created
May 27, 2016 16:05
-
-
Save sreichholf/9b3a2a9f311ed0fe56853a76eff34a79 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "i2c_driver.h" | |
#include "twi.h" | |
#include "board.h" | |
#include "stdlib.h" | |
#define I2C_CLK_FAST 400000 | |
uint32_t fromBCD(uint8_t bcd_value) { | |
return (((bcd_value & 0xF0) * 10) >> 4) + (bcd_value & 0x0F); | |
} | |
uint8_t toBCD(uint32_t value) { | |
div_t qr = div(value, 10); | |
return (qr.quot << 4) + qr.rem; | |
} | |
twi_packet getPacket(uint8_t chip, uint8_t* addr, uint8_t addr_len, uint8_t *buffer, uint32_t length) | |
{ | |
twi_packet packet; | |
packet.chip = chip; | |
for(uint8_t i=0; i < addr_len; ++i) | |
packet.addr[i] = addr[i]; | |
packet.addr_length = addr_len; | |
packet.buffer = buffer; | |
packet.length = length; | |
return packet; | |
} | |
bool i2cInit() | |
{ | |
//Enable Peripheral Clock | |
PMC->PMC_PCER0 |= 0x00080000L ; | |
//Enable TWI PIOs | |
register Pio *pioptr; | |
pioptr = PIOA ; | |
pioptr->PIO_ABCDSR[0] &= ~0x00000018 ; // Peripheral A | |
pioptr->PIO_ABCDSR[1] &= ~0x00000018 ; // Peripheral A | |
pioptr->PIO_PDR = 0x00000018 ; // Assign to peripheral | |
twi_enable_master_mode(TWI0); | |
twi_options_t opt; | |
opt.master_clk = Master_frequency; | |
opt.speed = I2C_CLK_FAST; | |
return twi_master_init(TWI0, &opt) == TWI_SUCCESS; | |
} | |
void i2cCheck(){}; | |
uint32_t i2cReadBuffer(uint8_t chip, uint8_t* addr, uint8_t addr_len, uint8_t *buffer, uint32_t length) | |
{ | |
twi_packet packet( | |
getPacket(chip, addr, addr_len, buffer, length) | |
); | |
return twi_master_read(TWI0, &packet); | |
} | |
bool i2cWriteBuffer(uint8_t chip, uint8_t* addr, uint8_t addr_len, uint8_t *buffer, uint32_t length) | |
{ | |
twi_packet packet( | |
getPacket(chip, addr, addr_len, buffer, length) | |
); | |
return twi_master_write(TWI0, &packet) == TWI_SUCCESS; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "opentx.h" | |
#include "i2c_driver.h" | |
#include "twi.h" | |
#define DS3231_I2C_ADDR 0x68 | |
int8_t volumeRequired; //HACK ALERT! | |
void readExtRtc() { | |
uint8_t buffer[7]; | |
gtm utm; | |
int res = i2cReadBuffer(DS3231_I2C_ADDR, 0, 2, buffer, 7); | |
if(res != TWI_SUCCESS) { | |
gtm utm; | |
utm.tm_sec = 0; | |
utm.tm_min = 0; | |
utm.tm_hour = 0; | |
utm.tm_mday = 0; | |
utm.tm_mon = 1; | |
utm.tm_year = res + 100; | |
g_rtcTime = gmktime(&utm); | |
} else { | |
// Set the date and time | |
utm.tm_sec = fromBCD(buffer[0] & 0x7F); | |
utm.tm_min = fromBCD(buffer[1] & 0x7F); | |
utm.tm_hour = fromBCD(buffer[2] & 0x3F); | |
utm.tm_mday = fromBCD(buffer[4] & 0x3F); | |
utm.tm_mon = fromBCD(buffer[5] & 0x1F) - 1; | |
utm.tm_year = fromBCD(buffer[6]) + 100; | |
} | |
g_rtcTime = gmktime(&utm); | |
} | |
void writeExtRtc(gtm* ptr) { | |
g_rtcTime = gmktime(ptr); | |
uint8_t buffer[7]; | |
buffer[0] = toBCD(ptr->tm_sec); | |
buffer[1] = toBCD(ptr->tm_min); | |
buffer[2] = toBCD(ptr->tm_hour); | |
buffer[3] = toBCD(ptr->tm_wday); | |
buffer[4] = toBCD(ptr->tm_mday); | |
buffer[5] = toBCD(ptr->tm_mon + 1); | |
uint8_t offset = ptr->tm_year >= 100 ? 100 : 0; | |
buffer[6] = toBCD(ptr->tm_year - offset); | |
i2cWriteBuffer(DS3231_I2C_ADDR, 0, 1, buffer, 7); | |
} | |
void rtcSetTime(struct gtm * t) { | |
writeExtRtc(t); | |
} | |
void rtcInit() { | |
readExtRtc(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment