Review line ending handling (#4820)

* New line end handling rules

* Normalize all the line endings
This commit is contained in:
3djc
2017-04-19 10:54:43 +02:00
committed by Bertrand Songis
parent 189b65b724
commit fa951acf40
397 changed files with 252824 additions and 252713 deletions
+171 -171
View File
@@ -1,171 +1,171 @@
/*
* 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 _BOARD_GRUVIN9X_H_
#define _BOARD_GRUVIN9X_H_
#include "../common/avr/board_avr.h"
// Board driver
void boardInit(void);
#define boardOff() pwrOff()
// Keys
#define KEYS_GPIO_REG_MENU pinl
#define KEYS_GPIO_PIN_MENU (1<<4)
#define KEYS_GPIO_REG_EXIT pinl
#define KEYS_GPIO_PIN_EXIT (1<<5)
#define KEYS_GPIO_REG_RIGHT pinl
#define KEYS_GPIO_PIN_RIGHT (1<<2)
#define KEYS_GPIO_REG_LEFT pinl
#define KEYS_GPIO_PIN_LEFT (1<<3)
#define KEYS_GPIO_REG_UP pinl
#define KEYS_GPIO_PIN_UP (1<<1)
#define KEYS_GPIO_REG_DOWN pinl
#define KEYS_GPIO_PIN_DOWN (1<<0)
// Trims
#define TRIMS_GPIO_REG_LHL pinj
#define TRIMS_GPIO_REG_LVD pinj
#define TRIMS_GPIO_REG_RVU pinj
#define TRIMS_GPIO_REG_RHL pinj
#define TRIMS_GPIO_REG_LHR pinj
#define TRIMS_GPIO_REG_LVU pinj
#define TRIMS_GPIO_REG_RVD pinj
#define TRIMS_GPIO_REG_RHR pinj
#define TRIMS_GPIO_PIN_LHL (1<<0)
#define TRIMS_GPIO_PIN_LVD (1<<2)
#define TRIMS_GPIO_PIN_RVU (1<<5)
#define TRIMS_GPIO_PIN_RHL (1<<6)
#define TRIMS_GPIO_PIN_LHR (1<<1)
#define TRIMS_GPIO_PIN_LVU (1<<3)
#define TRIMS_GPIO_PIN_RVD (1<<4)
#define TRIMS_GPIO_PIN_RHR (1<<7)
#define TIMER_16KHZ_VECT TIMER2_OVF_vect
#define COUNTER_16KHZ TCNT2
#define TIMER_10MS_VECT TIMER2_COMPA_vect
#define TIMER_10MS_COMPVAL OCR2A
#define PAUSE_10MS_INTERRUPT() TIMSK2 &= ~(1<<OCIE2A)
#define RESUME_10MS_INTERRUPT() TIMSK2 |= (1<<OCIE2A)
#define PAUSE_PPMIN_INTERRUPT() TIMSK3 &= ~(1<<ICIE3)
#define RESUME_PPMIN_INTERRUPT() TIMSK3 |= (1<<ICIE3)
#define SLAVE_MODE() (PING & (1<<INP_G_RF_POW))
#define JACK_PPM_OUT() PORTG |= (1<<OUT_G_SIM_CTL)
#define JACK_PPM_IN() PORTG &= ~(1<<OUT_G_SIM_CTL)
// SD driver
#define BLOCK_SIZE 512 /* Block Size in Bytes */
#define sdDone()
#define SD_IS_HC() (0)
#define SD_GET_SPEED() (0)
#if !defined(SIMU)
bool sdMounted();
void sdMountPoll();
void sdPoll10ms();
#endif
#define speakerOn() TCCR0A |= (1 << COM0A0)
#define speakerOff() TCCR0A &= ~(1 << COM0A0)
// Backlight driver
#define OUT_C_LIGHT 0
#define backlightEnable() PORTC |= (1<<OUT_C_LIGHT)
#define backlightDisable() PORTC &= ~(1<<OUT_C_LIGHT)
#define isBacklightEnabled() (PORTC & (1<<OUT_C_LIGHT))
#define BACKLIGHT_ENABLE() backlightEnable()
#define BACKLIGHT_DISABLE() backlightDisable()
#define INP_E_PPM_IN 7
#define INP_E_ROT_ENC_1_B 6
#define INP_E_ROT_ENC_1_A 5
#define INP_E_USB_D_PLS 4
#define OUT_E_BUZZER 3
#define INP_E_USB_D_NEG 2
#define INP_E_TELEM_RX 1
#define OUT_E_TELEM_TX 0
#define OUT_D_HAPTIC 7
#define INP_D_SPARE4 6
#define INP_D_ROT_ENC_2_PUSH 5
#define INP_D_ROT_ENC_1_PUSH 4
#define OUT_D_ROT_ENC_2_B 3
#define INP_D_ROT_ENC_2_A 2
#define INP_D_I2C_SCL 1
#define INP_D_I2C_SDA 0
#define INP_G_Gear 5
#define INP_G_ThrCt 2
#define OUT_G_SIM_CTL 4 //1 : phone-jack=ppm_in
#define INP_G_ID1 3
#define INP_G_RF_POW 1
#define INP_G_RuddDR 0
#define INP_C_AileDR 7
#define INP_C_ElevDR 6
#define OUT_B_Speaker 7
#define OUT_B_PPM 6 // will be switched by TCNT1==OCR1B in hardware
#define INP_B_Trainer 5
#define INP_B_ID2 4
#if defined(VOICE)
#define OUT_H_14DRESET 3
#define OUT_H_14DCLK 4
#define OUT_H_14DDATA 5
#define INP_H_14DBUSY 6
#endif
// Keys driver
#define TRIMS_PRESSED() (~PINJ)
#define KEYS_PRESSED() (~PINL) // used only for DBLKEYS code.
// LCD driver
#define PORTA_LCD_DAT PORTA
#define PORTC_LCD_CTRL PORTC
#define OUT_C_LCD_E 5
#define OUT_C_LCD_RnW 4
#define OUT_C_LCD_A0 3
#define OUT_C_LCD_RES 2
#define OUT_C_LCD_CS1 1
// Power driver
uint8_t pwrCheck();
void pwrOff();
#define UNEXPECTED_SHUTDOWN() ((mcusr & (1 << WDRF)) || g_eeGeneral.unexpectedShutdown)
// USB fake driver
#define usbPlugged() false
// Haptic driver
#define hapticOn() PORTD &= ~(1 << OUT_D_HAPTIC)
#define hapticOff() PORTD |= (1 << OUT_D_HAPTIC)
// Rotary encoder driver
#define REA_DOWN() (~PIND & 0x20)
#define REB_DOWN() (~PIND & 0x10)
#define ROTENC_DOWN() (REA_DOWN() || REB_DOWN())
// Buzzer driver
#define buzzerOn() PORTE |= (1 << OUT_E_BUZZER)
#define buzzerOff() PORTE &= ~(1 << OUT_E_BUZZER)
#endif // _BOARD_GRUVIN9X_H_
/*
* 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 _BOARD_GRUVIN9X_H_
#define _BOARD_GRUVIN9X_H_
#include "../common/avr/board_avr.h"
// Board driver
void boardInit(void);
#define boardOff() pwrOff()
// Keys
#define KEYS_GPIO_REG_MENU pinl
#define KEYS_GPIO_PIN_MENU (1<<4)
#define KEYS_GPIO_REG_EXIT pinl
#define KEYS_GPIO_PIN_EXIT (1<<5)
#define KEYS_GPIO_REG_RIGHT pinl
#define KEYS_GPIO_PIN_RIGHT (1<<2)
#define KEYS_GPIO_REG_LEFT pinl
#define KEYS_GPIO_PIN_LEFT (1<<3)
#define KEYS_GPIO_REG_UP pinl
#define KEYS_GPIO_PIN_UP (1<<1)
#define KEYS_GPIO_REG_DOWN pinl
#define KEYS_GPIO_PIN_DOWN (1<<0)
// Trims
#define TRIMS_GPIO_REG_LHL pinj
#define TRIMS_GPIO_REG_LVD pinj
#define TRIMS_GPIO_REG_RVU pinj
#define TRIMS_GPIO_REG_RHL pinj
#define TRIMS_GPIO_REG_LHR pinj
#define TRIMS_GPIO_REG_LVU pinj
#define TRIMS_GPIO_REG_RVD pinj
#define TRIMS_GPIO_REG_RHR pinj
#define TRIMS_GPIO_PIN_LHL (1<<0)
#define TRIMS_GPIO_PIN_LVD (1<<2)
#define TRIMS_GPIO_PIN_RVU (1<<5)
#define TRIMS_GPIO_PIN_RHL (1<<6)
#define TRIMS_GPIO_PIN_LHR (1<<1)
#define TRIMS_GPIO_PIN_LVU (1<<3)
#define TRIMS_GPIO_PIN_RVD (1<<4)
#define TRIMS_GPIO_PIN_RHR (1<<7)
#define TIMER_16KHZ_VECT TIMER2_OVF_vect
#define COUNTER_16KHZ TCNT2
#define TIMER_10MS_VECT TIMER2_COMPA_vect
#define TIMER_10MS_COMPVAL OCR2A
#define PAUSE_10MS_INTERRUPT() TIMSK2 &= ~(1<<OCIE2A)
#define RESUME_10MS_INTERRUPT() TIMSK2 |= (1<<OCIE2A)
#define PAUSE_PPMIN_INTERRUPT() TIMSK3 &= ~(1<<ICIE3)
#define RESUME_PPMIN_INTERRUPT() TIMSK3 |= (1<<ICIE3)
#define SLAVE_MODE() (PING & (1<<INP_G_RF_POW))
#define JACK_PPM_OUT() PORTG |= (1<<OUT_G_SIM_CTL)
#define JACK_PPM_IN() PORTG &= ~(1<<OUT_G_SIM_CTL)
// SD driver
#define BLOCK_SIZE 512 /* Block Size in Bytes */
#define sdDone()
#define SD_IS_HC() (0)
#define SD_GET_SPEED() (0)
#if !defined(SIMU)
bool sdMounted();
void sdMountPoll();
void sdPoll10ms();
#endif
#define speakerOn() TCCR0A |= (1 << COM0A0)
#define speakerOff() TCCR0A &= ~(1 << COM0A0)
// Backlight driver
#define OUT_C_LIGHT 0
#define backlightEnable() PORTC |= (1<<OUT_C_LIGHT)
#define backlightDisable() PORTC &= ~(1<<OUT_C_LIGHT)
#define isBacklightEnabled() (PORTC & (1<<OUT_C_LIGHT))
#define BACKLIGHT_ENABLE() backlightEnable()
#define BACKLIGHT_DISABLE() backlightDisable()
#define INP_E_PPM_IN 7
#define INP_E_ROT_ENC_1_B 6
#define INP_E_ROT_ENC_1_A 5
#define INP_E_USB_D_PLS 4
#define OUT_E_BUZZER 3
#define INP_E_USB_D_NEG 2
#define INP_E_TELEM_RX 1
#define OUT_E_TELEM_TX 0
#define OUT_D_HAPTIC 7
#define INP_D_SPARE4 6
#define INP_D_ROT_ENC_2_PUSH 5
#define INP_D_ROT_ENC_1_PUSH 4
#define OUT_D_ROT_ENC_2_B 3
#define INP_D_ROT_ENC_2_A 2
#define INP_D_I2C_SCL 1
#define INP_D_I2C_SDA 0
#define INP_G_Gear 5
#define INP_G_ThrCt 2
#define OUT_G_SIM_CTL 4 //1 : phone-jack=ppm_in
#define INP_G_ID1 3
#define INP_G_RF_POW 1
#define INP_G_RuddDR 0
#define INP_C_AileDR 7
#define INP_C_ElevDR 6
#define OUT_B_Speaker 7
#define OUT_B_PPM 6 // will be switched by TCNT1==OCR1B in hardware
#define INP_B_Trainer 5
#define INP_B_ID2 4
#if defined(VOICE)
#define OUT_H_14DRESET 3
#define OUT_H_14DCLK 4
#define OUT_H_14DDATA 5
#define INP_H_14DBUSY 6
#endif
// Keys driver
#define TRIMS_PRESSED() (~PINJ)
#define KEYS_PRESSED() (~PINL) // used only for DBLKEYS code.
// LCD driver
#define PORTA_LCD_DAT PORTA
#define PORTC_LCD_CTRL PORTC
#define OUT_C_LCD_E 5
#define OUT_C_LCD_RnW 4
#define OUT_C_LCD_A0 3
#define OUT_C_LCD_RES 2
#define OUT_C_LCD_CS1 1
// Power driver
uint8_t pwrCheck();
void pwrOff();
#define UNEXPECTED_SHUTDOWN() ((mcusr & (1 << WDRF)) || g_eeGeneral.unexpectedShutdown)
// USB fake driver
#define usbPlugged() false
// Haptic driver
#define hapticOn() PORTD &= ~(1 << OUT_D_HAPTIC)
#define hapticOff() PORTD |= (1 << OUT_D_HAPTIC)
// Rotary encoder driver
#define REA_DOWN() (~PIND & 0x20)
#define REB_DOWN() (~PIND & 0x10)
#define ROTENC_DOWN() (REA_DOWN() || REB_DOWN())
// Buzzer driver
#define buzzerOn() PORTE |= (1 << OUT_E_BUZZER)
#define buzzerOff() PORTE &= ~(1 << OUT_E_BUZZER)
#endif // _BOARD_GRUVIN9X_H_
File diff suppressed because it is too large Load Diff
+304 -304
View File
@@ -1,304 +1,304 @@
/*
* 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.
*/
/*--------------------------------------------------------------------------*/
/* RTC controls */
#include "opentx.h"
#include "FatFs/integer.h"
typedef struct {
WORD year; /* 2000..2099 */
BYTE month; /* 1..12 */
BYTE mday; /* 1.. 31 */
BYTE wday; /* 1..7 */
BYTE hour; /* 0..23 */
BYTE min; /* 0..59 */
BYTE sec; /* 0..59 */
} RTC;
#define SCL_LOW() DDRD |= 0x01 /* SCL = LOW */
#define SCL_HIGH() DDRD &= ~0x01 /* SCL = High-Z */
#define SCL_VAL ((PIND & 0x01) ? 1 : 0) /* SCL input level */
#define SDA_LOW() DDRD |= 0x02 /* SDA = LOW */
#define SDA_HIGH() DDRD &= ~0x02 /* SDA = High-Z */
#define SDA_VAL ((PIND & 0x02) ? 1 : 0) /* SDA input level */
/*-------------------------------------------------*/
/* I2C bus protocol */
static
void iic_delay (void)
{
#ifndef SIMU
for (int n = 4; n; n--) PINB;
#endif
}
/* Generate start condition on the IIC bus */
static
void iic_start (void)
{
SDA_HIGH();
iic_delay();
SCL_HIGH();
iic_delay();
SDA_LOW();
iic_delay();
SCL_LOW();
iic_delay();
}
/* Generate stop condition on the IIC bus */
static
void iic_stop (void)
{
SDA_LOW();
iic_delay();
SCL_HIGH();
iic_delay();
SDA_HIGH();
iic_delay();
}
/* Send a byte to the IIC bus */
static
int iic_send (BYTE dat)
{
BYTE b = 0x80;
int ack;
do {
if (dat & b) { /* SDA = Z/L */
SDA_HIGH();
} else {
SDA_LOW();
}
iic_delay();
SCL_HIGH();
iic_delay();
SCL_LOW();
iic_delay();
} while (b >>= 1);
SDA_HIGH();
iic_delay();
SCL_HIGH();
ack = SDA_VAL ? 0 : 1; /* Sample ACK */
iic_delay();
SCL_LOW();
iic_delay();
return ack;
}
/* Receive a byte from the IIC bus */
static
BYTE iic_rcvr (int ack)
{
UINT d = 1;
do {
d <<= 1;
SCL_HIGH();
if (SDA_VAL) d++;
iic_delay();
SCL_LOW();
iic_delay();
} while (d < 0x100);
if (ack) { /* SDA = ACK */
SDA_LOW();
} else {
SDA_HIGH();
}
iic_delay();
SCL_HIGH();
iic_delay();
SCL_LOW();
SDA_HIGH();
iic_delay();
return (BYTE)d;
}
/*-------------------------------------------------*/
/* I2C block read/write controls */
int iic_read (
BYTE dev, /* Device address */
UINT adr, /* Read start address */
UINT cnt, /* Read byte count */
BYTE *buff /* Read data buffer */
)
{
BYTE *rbuff = buff;
int n;
if (!cnt) return 0;
n = 10;
do { /* Select device */
iic_start();
} while (!iic_send(dev) && --n);
if (n) {
if (iic_send((BYTE)adr)) { /* Set start address */
iic_start(); /* Reselect device in read mode */
if (iic_send(dev | 1)) {
do { /* Receive data */
cnt--;
*rbuff++ = iic_rcvr(cnt ? 1 : 0);
} while (cnt);
}
}
}
iic_stop(); /* Deselect device */
return cnt ? 0 : 1;
}
int iic_write (
BYTE dev, /* Device address */
UINT adr, /* Write start address */
UINT cnt, /* Write byte count */
const BYTE *buff /* Data to be written */
)
{
const BYTE *wbuff = buff;
int n;
if (!cnt) return 0;
n = 10;
do { /* Select device */
iic_start();
} while (!iic_send(dev) && --n);
if (n) {
if (iic_send((BYTE)adr)) { /* Set start address */
do { /* Send data */
if (!iic_send(*wbuff++)) break;
} while (--cnt);
}
}
iic_stop(); /* Deselect device */
return cnt ? 0 : 1;
}
/*-------------------------------------------------*/
/* RTC functions */
int g9x_rtcGetTime (RTC *rtc)
{
BYTE buf[8];
if (!iic_read(0xD0, 0, 7, buf)) return 0;
rtc->sec = (buf[0] & 0x0F) + ((buf[0] >> 4) & 7) * 10;
rtc->min = (buf[1] & 0x0F) + (buf[1] >> 4) * 10;
rtc->hour = (buf[2] & 0x0F) + ((buf[2] >> 4) & 3) * 10;
rtc->wday = (buf[2] & 0x07);
rtc->mday = (buf[4] & 0x0F) + ((buf[4] >> 4) & 3) * 10;
rtc->month = (buf[5] & 0x0F) + ((buf[5] >> 4) & 1) * 10;
rtc->year = 2000 + (buf[6] & 0x0F) + (buf[6] >> 4) * 10;
return 1;
}
int g9x_rtcSetTime (const RTC *rtc)
{
BYTE buf[8];
buf[0] = rtc->sec / 10 * 16 + rtc->sec % 10;
buf[1] = rtc->min / 10 * 16 + rtc->min % 10;
buf[2] = rtc->hour / 10 * 16 + rtc->hour % 10;
buf[3] = rtc->wday & 7;
buf[4] = rtc->mday / 10 * 16 + rtc->mday % 10;
buf[5] = rtc->month / 10 * 16 + rtc->month % 10;
buf[6] = (rtc->year - 2000) / 10 * 16 + (rtc->year - 2000) % 10;
return iic_write(0xD0, 0, 7, buf);
}
void rtcGetTime(struct gtm * utm)
{
RTC rtc = {0,0,0,0,0,0,0};
g9x_rtcGetTime(&rtc);
utm->tm_year = rtc.year - TM_YEAR_BASE;
utm->tm_mon = rtc.month - 1;
utm->tm_mday = rtc.mday;
utm->tm_hour = rtc.hour;
utm->tm_min = rtc.min;
utm->tm_sec = rtc.sec;
utm->tm_wday = rtc.wday - 1;
}
void rtcSetTime(const struct gtm * t)
{
g_ms100 = 0; // start of next second begins now
RTC rtc;
rtc.year = t->tm_year + TM_YEAR_BASE;
rtc.month = t->tm_mon + 1;
rtc.mday = t->tm_mday;
rtc.hour = t->tm_hour;
rtc.min = t->tm_min;
rtc.sec = t->tm_sec;
rtc.wday = t->tm_wday + 1;
g9x_rtcSetTime(&rtc);
}
void rtcInit (void)
{
BYTE buf[8]; /* RTC R/W buffer */
UINT adr;
/* Read RTC registers */
if (!iic_read(0xD0, 0, 8, buf)) return; /* IIC error */
if (buf[7] & 0x20) { /* When data has been volatiled, set default time */
/* Clear nv-ram. Reg[8..63] */
memset(buf, 0, 8);
for (adr = 8; adr < 64; adr += 8)
iic_write(0x0D, adr, 8, buf);
/* Reset time to Jan 1, '08. Reg[0..7] */
buf[4] = 1; buf[5] = 1; buf[6] = 8;
iic_write(0x0D, 0, 8, buf);
}
struct gtm utm;
rtcGetTime(&utm);
g_rtcTime = gmktime(&utm);
}
/*
* 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.
*/
/*--------------------------------------------------------------------------*/
/* RTC controls */
#include "opentx.h"
#include "FatFs/integer.h"
typedef struct {
WORD year; /* 2000..2099 */
BYTE month; /* 1..12 */
BYTE mday; /* 1.. 31 */
BYTE wday; /* 1..7 */
BYTE hour; /* 0..23 */
BYTE min; /* 0..59 */
BYTE sec; /* 0..59 */
} RTC;
#define SCL_LOW() DDRD |= 0x01 /* SCL = LOW */
#define SCL_HIGH() DDRD &= ~0x01 /* SCL = High-Z */
#define SCL_VAL ((PIND & 0x01) ? 1 : 0) /* SCL input level */
#define SDA_LOW() DDRD |= 0x02 /* SDA = LOW */
#define SDA_HIGH() DDRD &= ~0x02 /* SDA = High-Z */
#define SDA_VAL ((PIND & 0x02) ? 1 : 0) /* SDA input level */
/*-------------------------------------------------*/
/* I2C bus protocol */
static
void iic_delay (void)
{
#ifndef SIMU
for (int n = 4; n; n--) PINB;
#endif
}
/* Generate start condition on the IIC bus */
static
void iic_start (void)
{
SDA_HIGH();
iic_delay();
SCL_HIGH();
iic_delay();
SDA_LOW();
iic_delay();
SCL_LOW();
iic_delay();
}
/* Generate stop condition on the IIC bus */
static
void iic_stop (void)
{
SDA_LOW();
iic_delay();
SCL_HIGH();
iic_delay();
SDA_HIGH();
iic_delay();
}
/* Send a byte to the IIC bus */
static
int iic_send (BYTE dat)
{
BYTE b = 0x80;
int ack;
do {
if (dat & b) { /* SDA = Z/L */
SDA_HIGH();
} else {
SDA_LOW();
}
iic_delay();
SCL_HIGH();
iic_delay();
SCL_LOW();
iic_delay();
} while (b >>= 1);
SDA_HIGH();
iic_delay();
SCL_HIGH();
ack = SDA_VAL ? 0 : 1; /* Sample ACK */
iic_delay();
SCL_LOW();
iic_delay();
return ack;
}
/* Receive a byte from the IIC bus */
static
BYTE iic_rcvr (int ack)
{
UINT d = 1;
do {
d <<= 1;
SCL_HIGH();
if (SDA_VAL) d++;
iic_delay();
SCL_LOW();
iic_delay();
} while (d < 0x100);
if (ack) { /* SDA = ACK */
SDA_LOW();
} else {
SDA_HIGH();
}
iic_delay();
SCL_HIGH();
iic_delay();
SCL_LOW();
SDA_HIGH();
iic_delay();
return (BYTE)d;
}
/*-------------------------------------------------*/
/* I2C block read/write controls */
int iic_read (
BYTE dev, /* Device address */
UINT adr, /* Read start address */
UINT cnt, /* Read byte count */
BYTE *buff /* Read data buffer */
)
{
BYTE *rbuff = buff;
int n;
if (!cnt) return 0;
n = 10;
do { /* Select device */
iic_start();
} while (!iic_send(dev) && --n);
if (n) {
if (iic_send((BYTE)adr)) { /* Set start address */
iic_start(); /* Reselect device in read mode */
if (iic_send(dev | 1)) {
do { /* Receive data */
cnt--;
*rbuff++ = iic_rcvr(cnt ? 1 : 0);
} while (cnt);
}
}
}
iic_stop(); /* Deselect device */
return cnt ? 0 : 1;
}
int iic_write (
BYTE dev, /* Device address */
UINT adr, /* Write start address */
UINT cnt, /* Write byte count */
const BYTE *buff /* Data to be written */
)
{
const BYTE *wbuff = buff;
int n;
if (!cnt) return 0;
n = 10;
do { /* Select device */
iic_start();
} while (!iic_send(dev) && --n);
if (n) {
if (iic_send((BYTE)adr)) { /* Set start address */
do { /* Send data */
if (!iic_send(*wbuff++)) break;
} while (--cnt);
}
}
iic_stop(); /* Deselect device */
return cnt ? 0 : 1;
}
/*-------------------------------------------------*/
/* RTC functions */
int g9x_rtcGetTime (RTC *rtc)
{
BYTE buf[8];
if (!iic_read(0xD0, 0, 7, buf)) return 0;
rtc->sec = (buf[0] & 0x0F) + ((buf[0] >> 4) & 7) * 10;
rtc->min = (buf[1] & 0x0F) + (buf[1] >> 4) * 10;
rtc->hour = (buf[2] & 0x0F) + ((buf[2] >> 4) & 3) * 10;
rtc->wday = (buf[2] & 0x07);
rtc->mday = (buf[4] & 0x0F) + ((buf[4] >> 4) & 3) * 10;
rtc->month = (buf[5] & 0x0F) + ((buf[5] >> 4) & 1) * 10;
rtc->year = 2000 + (buf[6] & 0x0F) + (buf[6] >> 4) * 10;
return 1;
}
int g9x_rtcSetTime (const RTC *rtc)
{
BYTE buf[8];
buf[0] = rtc->sec / 10 * 16 + rtc->sec % 10;
buf[1] = rtc->min / 10 * 16 + rtc->min % 10;
buf[2] = rtc->hour / 10 * 16 + rtc->hour % 10;
buf[3] = rtc->wday & 7;
buf[4] = rtc->mday / 10 * 16 + rtc->mday % 10;
buf[5] = rtc->month / 10 * 16 + rtc->month % 10;
buf[6] = (rtc->year - 2000) / 10 * 16 + (rtc->year - 2000) % 10;
return iic_write(0xD0, 0, 7, buf);
}
void rtcGetTime(struct gtm * utm)
{
RTC rtc = {0,0,0,0,0,0,0};
g9x_rtcGetTime(&rtc);
utm->tm_year = rtc.year - TM_YEAR_BASE;
utm->tm_mon = rtc.month - 1;
utm->tm_mday = rtc.mday;
utm->tm_hour = rtc.hour;
utm->tm_min = rtc.min;
utm->tm_sec = rtc.sec;
utm->tm_wday = rtc.wday - 1;
}
void rtcSetTime(const struct gtm * t)
{
g_ms100 = 0; // start of next second begins now
RTC rtc;
rtc.year = t->tm_year + TM_YEAR_BASE;
rtc.month = t->tm_mon + 1;
rtc.mday = t->tm_mday;
rtc.hour = t->tm_hour;
rtc.min = t->tm_min;
rtc.sec = t->tm_sec;
rtc.wday = t->tm_wday + 1;
g9x_rtcSetTime(&rtc);
}
void rtcInit (void)
{
BYTE buf[8]; /* RTC R/W buffer */
UINT adr;
/* Read RTC registers */
if (!iic_read(0xD0, 0, 8, buf)) return; /* IIC error */
if (buf[7] & 0x20) { /* When data has been volatiled, set default time */
/* Clear nv-ram. Reg[8..63] */
memset(buf, 0, 8);
for (adr = 8; adr < 64; adr += 8)
iic_write(0x0D, adr, 8, buf);
/* Reset time to Jan 1, '08. Reg[0..7] */
buf[4] = 1; buf[5] = 1; buf[6] = 8;
iic_write(0x0D, 0, 8, buf);
}
struct gtm utm;
rtcGetTime(&utm);
g_rtcTime = gmktime(&utm);
}