I got it working:
module NodeC {
uses interface Boot;
uses interface Leds;
uses interface Resource;
uses interface I2CPacket<TI2CBasicAddr> as I2C;
..........
uint8_t buffer[17]; // RTC i2c w
uint8_t rbuffer[8]; // RTC i2c r
typedef enum{
RTC_INIT_STATE = 0,
RTC_WRITE_CLOCK,
RTC_READ_CLOCK,
}rtc_state_t;
rtc_state_t rtc_state;
task void readClock();
event void Boot.booted() {
rtc_state = RTC_INIT_STATE;
call Resource.request(); // RTC i2c
}
task void readClock(){
rtc_state = RTC_READ_CLOCK;
call Resource.request();
}
event void Resource.granted() {
switch (rtc_state){
case RTC_INIT_STATE:
buffer[0] = 0x00; // word address 0, next
bytes are data
buffer[1] = 0x00; // 00 control/status1,
buffer[2] = 0x00; // 01 control/status2, no
alarm/timer flags and
interrupts
buffer[3] = 0x00; // 02 setting seconds clear the
voltage low
detector
buffer[4] = 0x00; // 03 setting minutes
buffer[5] = 0x13; // 04 setting hours
buffer[6] = 0x04; // 05 setting date
buffer[7] = 0x06; // 06 setting day of
week
buffer[8] = 0x08; // 07 setting century
and months
buffer[9] = 0x09; // 08 setting years
buffer[10] = 0x80; // 09 setting minute alarm
buffer[11] = 0x80; // 0A setting hour alarm
buffer[12] = 0x80; // 0B setting day alarm
buffer[13] = 0x80; // 0C setting weekday alarm
buffer[14] = 0x00; // 0D setting out freq
buffer[15] = 0x00; // 0E timer control
buffer[16] = 0x00; // 0F timer
if (call I2C.write(I2C_START | I2C_STOP, 0x51, 17,
buffer) == FAIL) {
call Leds.led2On();
}
break;
case RTC_READ_CLOCK:
buffer[0] = 0x02; // 02 seconds register
if (call I2C.write(I2C_START, 0x51, 1, &buffer[0]) ==
FAIL) {
call Leds.led2On();
}
break;
case RTC_WRITE_CLOCK:
break;
}
}
async event void I2C.writeDone(error_t error, uint16_t addr, uint8_t
length, uint8_t* data) {
uint8_t i;
switch(rtc_state){
case RTC_INIT_STATE:
call Resource.release();
break;
case RTC_READ_CLOCK:
for (i=0;i<sizeof(rbuffer);i++) rbuffer[i] = 0;
if (call I2C.read(I2C_START | I2C_STOP, 0x51,
8, rbuffer) == FAIL) {
call Leds.led0On();
}
break;
case RTC_WRITE_CLOCK:
break;
}
}
async event void I2C.readDone(error_t error, uint16_t addr, uint8_t
length, uint8_t* data) {
uint32_t Time;
uint8_t seconds, minutes, hours, days, months, years;
if (rtc_state == RTC_READ_CLOCK){
call Resource.release();
rtcDone = TRUE;
call Leds.led2Toggle();
seconds = ((data[1] & 0x7F) >> 4) * 10 + (data[1] &
0xF); //
seconds
minutes = ((data[2] & 0x7F) >> 4) * 10 + (data[2] &
0xF); // minutes
hours = ((data[3] & 0x3F) >> 4) * 10 + (data[3] & 0xF);
// hours
days = ((data[4] & 0x3F) >> 4) * 10 + (data[4] & 0xF);
// days
months = ((data[6] & 0x1F) >> 4) * 10 + (data[6] &
0xF); // months
years = ((data[7] & 0xFF) >> 4) * 10 + (data[7] & 0xF);
// years
//shift RTC time to uint32_t
Time = years; // NB! upto year 63 ;)
Time = (Time << 6) | months;
Time = (Time << 4) | days;
Time = (Time << 5) | hours;
Time = (Time << 5) | minutes;
Time = (Time << 6) | seconds;
RTC = Time; // RTC is global uint32_t variable
}
}
//Post task readClock() somewhere to start reading.
I tried to also make driver component, but it seems too complicated;)
I need to glue together at leas t 6 files to get it working. Maybe
later.
Andres Vahter
_______________________________________________
Tinyos-help mailing list
[email protected]
https://www.millennium.berkeley.edu/cgi-bin/mailman/listinfo/tinyos-help