Add Support for the DS3231 RTC and enable it for AR9X, refs #3531 (#3555)

* Add Support for the DS3231 RTC and enable it for AR9X, refs #3531

* fix compile for PCB=sky9x

* rtc ds3231: Add missing OpenTX headers
This commit is contained in:
reichi
2016-06-09 17:58:42 +02:00
committed by Bertrand Songis
parent 0c66f520a2
commit 8606e75f45
9 changed files with 1425 additions and 11 deletions
+21 -7
View File
@@ -5324,17 +5324,31 @@ typedef struct {
#define TWI_MMR_IADRSZ_NO (0x0 << 8) /**< \brief (TWI_MMR) No internal device address */
#define TWI_MMR_IADRSZ_1_BYTE (0x1 << 8) /**< \brief (TWI_MMR) One-byte internal device address */
#define TWI_MMR_IADRSZ_2_BYTE (0x2 << 8) /**< \brief (TWI_MMR) Two-byte internal device address */
#define TWI_MMR_IADRSZ_3_BYTE (0x3 << 8) /**< \brief (TWI_MMR) Three-byte internal device address */
#define TWI_MMR_IADRSZ_3_BYTE (0x3 << 8) /**< \brief (TWI_MMR) Three-byte internal device address */
#define TWI_MMR_IADRSZ_Msk (0x3u << TWI_MMR_IADRSZ_Pos)
#define TWI_MMR_IADRSZ_Pos 8
#define TWI_MMR_MREAD (0x1 << 12) /**< \brief (TWI_MMR) Master Read Direction */
#define TWI_MMR_DADR (0x7f << 16) /**< \brief (TWI_MMR) Device Address */
#define TWI_MMR_DADR_Pos 16
#define TWI_MMR_DADR_Msk (0x7fu << TWI_MMR_DADR_Pos) /**< \brief (TWI_MMR) Device Address */
#define TWI_MMR_DADR(value) ((TWI_MMR_DADR_Msk & ((value) << TWI_MMR_DADR_Pos)))
/* -------- TWI_SMR : (TWI Offset: 0x08) Slave Mode Register -------- */
#define TWI_SMR_SADR (0x7f << 16) /**< \brief (TWI_SMR) Slave Address */
#define TWI_SMR_SADR_Pos 16
#define TWI_SMR_SADR_Msk (0x7fu << TWI_SMR_SADR_Pos) /**< \brief (TWI_SMR) Slave Address */
#define TWI_SMR_SADR(value) ((TWI_SMR_SADR_Msk & ((value) << TWI_SMR_SADR_Pos)))
/* -------- TWI_IADR : (TWI Offset: 0x0C) Internal Address Register -------- */
#define TWI_IADR_IADR (0xffffff << 0) /**< \brief (TWI_IADR) Internal Address */
#define TWI_IADR_IADR_Pos 0
#define TWI_IADR_IADR_Msk (0xffffffu << TWI_IADR_IADR_Pos) /**< \brief (TWI_IADR) Internal Address */
#define TWI_IADR_IADR(value) ((TWI_IADR_IADR_Msk & ((value) << TWI_IADR_IADR_Pos)))
/* -------- TWI_CWGR : (TWI Offset: 0x10) Clock Waveform Generator Register -------- */
#define TWI_CWGR_CLDIV (0xff << 0) /**< \brief (TWI_CWGR) Clock Low Divider */
#define TWI_CWGR_CHDIV (0xff << 8) /**< \brief (TWI_CWGR) Clock High Divider */
#define TWI_CWGR_CKDIV (0x7 << 16) /**< \brief (TWI_CWGR) Clock Divider */
#define TWI_CWGR_CLDIV_Pos 0
#define TWI_CWGR_CLDIV_Msk (0xffu << TWI_CWGR_CLDIV_Pos) /**< \brief (TWI_CWGR) Clock Low Divider */
#define TWI_CWGR_CLDIV(value) ((TWI_CWGR_CLDIV_Msk & ((value) << TWI_CWGR_CLDIV_Pos)))
#define TWI_CWGR_CHDIV_Pos 8
#define TWI_CWGR_CHDIV_Msk (0xffu << TWI_CWGR_CHDIV_Pos) /**< \brief (TWI_CWGR) Clock High Divider */
#define TWI_CWGR_CHDIV(value) ((TWI_CWGR_CHDIV_Msk & ((value) << TWI_CWGR_CHDIV_Pos)))
#define TWI_CWGR_CKDIV_Pos 16
#define TWI_CWGR_CKDIV_Msk (0x7u << TWI_CWGR_CKDIV_Pos) /**< \brief (TWI_CWGR) Clock Divider */
#define TWI_CWGR_CKDIV(value) ((TWI_CWGR_CKDIV_Msk & ((value) << TWI_CWGR_CKDIV_Pos)))
/* -------- TWI_SR : (TWI Offset: 0x20) Status Register -------- */
#define TWI_SR_TXCOMP (0x1 << 0) /**< \brief (TWI_SR) Transmission Completed (automatically set / reset) */
#define TWI_SR_RXRDY (0x1 << 1) /**< \brief (TWI_SR) Receive Holding Register Ready (automatically set / reset) */
+2
View File
@@ -165,10 +165,12 @@ void setScaledVolume(uint8_t volume)
{
#if !defined(NO_HARDWARE_VOLUME)
volumeRequired = volumeScale[min<uint8_t>(volume, VOLUME_LEVEL_MAX)];
#ifndef AR9X
__disable_irq() ;
i2cCheck() ;
__enable_irq() ;
#endif
#endif
}
#endif // #if !defined(SIMU)
+5
View File
@@ -19,6 +19,9 @@
*/
#include "../../opentx.h"
#ifdef AR9X
#include "i2c_driver.h"
#endif
uint16_t ResetReason;
uint32_t Master_frequency ;
@@ -367,6 +370,7 @@ void opentxBootloader();
// Set up for volume control (TWI0)
// Need PA3 and PA4 set to peripheral A
#ifndef AR9X
void i2cInit()
{
register Pio *pioptr;
@@ -392,6 +396,7 @@ void i2cInit()
TWI0->TWI_MMR = 0x002F0000 ; // Device 5E (>>1) and master is writing
NVIC_EnableIRQ(TWI0_IRQn) ;
}
#endif
void boardInit()
{
+83
View File
@@ -0,0 +1,83 @@
/*
* Copyright (C) OpenTX
*
* Based on code named
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#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);
}
uint32_t 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);
}
+33
View File
@@ -0,0 +1,33 @@
/*
* Copyright (C) OpenTX
*
* Based on code named
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#ifndef _I2C_DRIVER_H_
#define _I2C_DRIVER_H_
#include "stdint.h"
uint32_t fromBCD(uint8_t bcd_value);
uint8_t toBCD(uint32_t value);
bool i2cInit();
uint32_t i2cReadBuffer(uint8_t chip, uint8_t addr[3], uint8_t addr_len, uint8_t *buffer, uint32_t length);
uint32_t i2cWriteBuffer(uint8_t chip, uint8_t addr[3], uint8_t addr_len, uint8_t *buffer, uint32_t length);
#endif
@@ -0,0 +1,69 @@
/*
* Copyright (C) OpenTX
*
* Based on code named
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "rtc.h"
#include "i2c_driver.h"
#define DS3231_I2C_ADDR 0x68
void readRtc()
{
uint8_t buffer[7];
gtm utm;
int res = i2cReadBuffer(DS3231_I2C_ADDR, 0, 2, buffer, 7);
if (res != 0)
return;
// 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 writeRtc(gtm* ptr)
{
g_rtcTime = gmktime(ptr);
g_ms100 = 0; // start of next second begins now
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)
{
writeRtc(t);
}
void rtcInit()
{
readRtc();
}