Functions to access the MC68HC68T1 Real Time Clock on the 68HC11 EVBU connected to the 68HC11 through the SPI


#include <hc11.h>
#include "rtc.h"
#include "spi.h"

/***************************************************************************
 * Routines to access the MC68HC68T1 Real Time Clock on the 68HC11 EVBU.  
 * 
 * These routines assume the SPI has been properly initialized.
 *
 * RTC Slave Select is active high on 68HC11 Port D bit 5
 ***************************************************************************/

int read_rtc_reg(unsigned char reg)
/*
 * Routine to read a single RTC register at location reg.
 * Returns -1 if address is out of range, value of register on success.
 */
{
    unsigned char c;
    if ((reg < RTC_SEC_READ) || (reg > RTC_INT_CONTROL_READ))
        return -1;
    PORTD |= 0x20;                  /* Select RTC */
    spi_putchar(reg);               /* Send address of register to read */
    c = spi_getchar();              /* Read register */
    PORTD &= ~0x20;                 /* De-select RTC */
    return c & 0x00ff;              /* Return byte read -- positive integer */
}

int write_rtc_reg(unsigned char byte, unsigned char reg)
/*
 * Routine to write a single byte to RTC register reg.
 * Returns -1 if address out of range; return 0 on success.
 */
{
    if ((reg < RTC_SEC_WRITE) || (reg > RTC_INT_CONTROL_WRITE))
        return -1;
    PORTD |= 0x20;                  /* Select RTC */
    spi_putchar(reg);               /* Send address of register to write */
    spi_putchar(byte);              /* Write register */
    PORTD &= ~0x20;                 /* De-select RTC */
    return 0;
}

void read_rtc(struct rtc_regs *r)
/*
 * Routine to read the time and date from the RTC.
 * Since read_rtc() reads the RTC status reg, it clears pending RTC interrupts
 *
 */
{
    PORTD |= 0x20;                  /* Select RTC */
    spi_putchar(RTC_SEC_READ);      /* Send address of first byte to read */
    spi_get_bytes((unsigned char *) r,sizeof(struct rtc_regs)); 
                                    /* Read all clock registers */
    PORTD &= ~0x20;                 /* De-select RTC */
}

void write_rtc(struct rtc_regs *r)
/*
 * Routine to write the RTC registers.
 *
 */
{
    PORTD |= 0x20;                  /* Select RTC */
    spi_putchar(RTC_SEC_WRITE); /* Send address of first byte */
    spi_put_bytes((unsigned char *) r,sizeof(struct rtc_regs)); 
                                    /* Write all clock registers */
    PORTD &= ~0x20;                 /* De-select RTC */
}