DS1337 - 2.6.12.2 kernel

This forum is for users of Microchip MPUs and who are interested in using Linux OS.

Moderator: nferre

bruce vicknair
Contact:
Location: tomball, tx
Posts: 60
Joined: Wed Jan 26, 2005 3:24 am

DS1337 - 2.6.12.2 kernel

Mon Aug 01, 2005 6:48 pm

Hello I need some newbie help, How can i read a ds1337 from user space? Can't seem to find my way through the code. The part is registered etc but the kernel does not use it? The driver is designed for user space access only?

thanks in advance for the snipit.
bruce vicknair
Contact:
Location: tomball, tx
Posts: 60
Joined: Wed Jan 26, 2005 3:24 am

Mon Aug 08, 2005 3:34 pm

answering my question again.. run some code like this and use 'system' to send string to 'date', (using 2.6.12 kernel). I stole the code from u-boot ds1338.c
its so simple, embarassed.
snipit:

fd_i2c=open("/dev/i2c/0", O_RDWR);
if (fd_i2c == -1)
{
printf("can't open i2c adapter.\n");
exit(0);
}
/* Connect to the first i2c device, set address to read*/
if (ioctl(fd_i2c, I2C_SLAVE, CFG_I2C_RTC_ADDR) < 0)
{ //CFG_I2C_RTC_ADDR
printf("Can't set address on ds1338.\n");
exit (1);
}
//rtc_set(&tm); will change clock setting !!!
rtc_get( &tm );




/*
* Set the RTC
*/
void rtc_set (struct rtc_time *tmp)
{
//unsigned char century;
// test
tmp->tm_sec=0; // 00-59 seconds
tmp->tm_min=55; // 00-59 minutes
tmp->tm_hour=15; // 00-23 hour, also bit 6 forced low,00-23 hr fmt
tmp->tm_day=5; // 1-7 day
tmp->tm_date=4; // 01-31 date
tmp->tm_month=8; // 01-12 month
tmp->tm_year=05; // 00-99 year

printf ("Set DATE: %04d-%02d-%02d (day=%d) TIME: %2d:%02d:%02d\n",
tmp->tm_year, tmp->tm_month, tmp->tm_date, tmp->tm_day,
tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
rtc_write (RTC_YR_REG_ADDR, bin2bcd (tmp->tm_year));
rtc_write (RTC_MON_REG_ADDR, bin2bcd (tmp->tm_month));
rtc_write (RTC_DAY_REG_ADDR, bin2bcd (tmp->tm_day));
rtc_write (RTC_DATE_REG_ADDR, bin2bcd (tmp->tm_date));
rtc_write (RTC_HR_REG_ADDR, (bin2bcd (tmp->tm_hour) & 0xbf) ); // force 24 hr mode
rtc_write (RTC_MIN_REG_ADDR, bin2bcd (tmp->tm_min));
rtc_write (RTC_SEC_REG_ADDR, bin2bcd (tmp->tm_sec));
printf(" Set hour read hour reg= %02x hex \n",rtc_read (RTC_HR_REG_ADDR));
}


/*
* Reset the RTC. We also enable the oscillator output on the
* SQW/INTB* pin and program it for 32,768 Hz output. Note that
* according to the datasheet, turning on the square wave output
* increases the current drain on the backup battery from about
* 600 nA to 2uA.
*/

void rtc_reset (void)
{
rtc_write (RTC_CTL_REG_ADDR, RTC_CTL_BIT_RS0 | RTC_CTL_BIT_RS1 | RTC_CTL_BIT_SQWE);
// 32khz output on, osf off
}


/*
* Helper functions
*/

static
unsigned char rtc_read (unsigned char reg)
{
int res;
unsigned char i2c_buf[4];
i2c_buf[0]=reg;
res=write(fd_i2c,i2c_buf,1);
if (res < 0) printf("i2c read set address error \n");
res=read(fd_i2c,i2c_buf,1);
if (res < 0) printf("i2c read data error \n");
return(i2c_buf[0]);
}


static void rtc_write (unsigned char reg, unsigned char val)
{
int res;
unsigned char i2c_buf[4];
i2c_buf[0]=reg;
i2c_buf[1]=val;
res=write(fd_i2c,i2c_buf,2);
if (res < 0) printf("i2c write data error \n");
}

static unsigned bcd2bin (unsigned char n)
{
return((((n >> 4) & 0x0F) * 10) + (n & 0x0F));
}

static unsigned char bin2bcd (unsigned int n)
{
return(((n / 10) << 4) | (n % 10));
}

/*
* Get the current time from the RTC
*/
void rtc_get (struct rtc_time *tmp)
{
unsigned char sec, min, hour, day, date, month, year,control;

sec = rtc_read (RTC_SEC_REG_ADDR);
min = rtc_read (RTC_MIN_REG_ADDR);
hour = rtc_read (RTC_HR_REG_ADDR);
day = rtc_read (RTC_DAY_REG_ADDR);
date = rtc_read (RTC_DATE_REG_ADDR);
month = rtc_read (RTC_MON_REG_ADDR);
year = rtc_read (RTC_YR_REG_ADDR);
control = rtc_read (RTC_CTL_REG_ADDR);

// printf ("Get RTC year=%02x Month=%02x Date=%02x Day=%02x "
// "hr=%02x min=%02x sec=%02x Control=%02x \n",
// year, month, date, day, hour, min, sec, control);
if (control & RTC_CTL_BIT_OSF)
{
printf ("### Warning: RTC oscillator has stopped\n");
/* clear the OSF flag */
rtc_write (RTC_CTL_REG_ADDR,
rtc_read (RTC_CTL_REG_ADDR) & ~RTC_CTL_BIT_OSF);
}
tmp->tm_sec = bcd2bin (sec & 0x7F);
tmp->tm_min = bcd2bin (min & 0x7F);
tmp->tm_hour = bcd2bin (hour & 0x3F);
tmp->tm_day = bcd2bin (day & 0x7);
tmp->tm_date = bcd2bin (date & 0x3F);
tmp->tm_month = bcd2bin (month & 0x1f);
tmp->tm_year = bcd2bin (year);
// printf ("Debug DATE:= 20%02d-%02d-%02d (Day=%d) TIME: %2d:%02d:%02d\n",
// tmp->tm_year, tmp->tm_month, tmp->tm_date, tmp->tm_day,
// tmp->tm_hour, tmp->tm_min, tmp->tm_sec); // no Y3K bit
printf("%02d%02d%02d%02d20%02d\n",tmp->tm_month,tmp->tm_date,tmp->tm_hour,tmp->tm_min,tmp->tm_year);
// printf ("Debug DATE:= 20%02d-%02d-%02d (Day=%d) TIME: %2d:%02d:%02d\n",
// tmp->tm_year, tmp->tm_month, tmp->tm_date, tmp->tm_day,
// tmp->tm_hour, tmp->tm_min, tmp->tm_sec); // no Y
}

Return to “LINUX”

Who is online

Users browsing this forum: Google [Bot] and 0 guests