problem with RTC

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

problem with RTC

725 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by ldima55 on Mon Apr 25 12:04:29 MST 2011
hi i get the board  with  LPC1343 from microbuilder and i am trying to connect a  DS1337 (RTC) to  the board thru i2c and i do not know how to get him to work i wrote a  function to him .when i connected a LED to port 2.5 and a the ds1337 to  i2c.
it's just don't want to work please help me.
here what i worte:

H-file
/**************************************************************************/

#ifndef _DS1337_H_
#define _DS1337_H_

#include "projectconfig.h"

#define DS1337_ADDR    0xD0          // 11010000
#define DS1337_RW      0x01
#define DS1337_READBIT 0x01
#define DS1337_MAXADDR 0x07        

typedef enum
{
  DS1337_ERROR_OK = 0,               // Everything executed normally
  DS1337_ERROR_I2CINIT,              // Unable to initialise I2C
  DS1337_ERROR_I2CBUSY,              // I2C already in use
  DS1337_ERROR_ADDRERR,              // Address out of range
  DS1337_ERROR_BUFFEROVERFLOW,       // Max 8 bytes can be read/written in one operation
  DS1337_ERROR_LAST
}
DS1337_Error_e;

DS1337_Error_e DS1337_Init (void);
DS1337_Error_e DS1337_ReadBuffer (uint16_t address, uint8_t *buffer, uint32_t bufferLength);
DS1337_Error_e DS1337_WriteBuffer (uint16_t address, uint8_t *buffer, uint32_t bufferLength);
DS1337_Error_e DS1337_ReadByte (uint16_t address, uint8_t *buffer);
DS1337_Error_e DS1337_WriteByte (uint16_t address, uint8_t value);


#endif
/******************************************************************/
C-file:
*************************************************************************/

#include "DS1337.h"
#include "core/systick/systick.h"
#include "core/i2c/i2c.h"

extern volatile uint8_t   I2CMasterBuffer[I2C_BUFSIZE];
extern volatile uint8_t   I2CSlaveBuffer[I2C_BUFSIZE];
extern volatile uint32_t  I2CReadLength, I2CWriteLength;

uint32_t i, timeout;

static bool DS1337_Initialised = false;

/**************************************************************************/
/*!
    @brief  Initialises the I2C block
*/
/**************************************************************************/
DS1337_Error_e DS1337_Init()
{
  // Initialise I2C
  if (i2cInit(I2CMODE_MASTER) == false)
  {
    return DS1337_ERROR_I2CINIT;    /* Fatal error */
  }

  // Set initialisation flag
  DS1337_Initialised = true;

  return DS1337_ERROR_OK;
}

/**************************************************************************/
/*!
    @brief Reads the specified number of bytes from the supplied address.

    This function will read one or more bytes starting at the supplied
    address.  A maximum of 8 bytes can be read in one operation.

    @param[in]  address
                The 16-bit address where the read will start.  The maximum
                value for the address depends on the size of the EEPROM
    @param[in]  *buffer
                Pointer to the buffer that will store the read results
    @param[in]  bufferLength
                Length of the buffer
*/
/**************************************************************************/
DS1337_Error_e DS1337_ReadBuffer (uint16_t address, uint8_t *buffer, uint32_t bufferLength)
{
  if (!DS1337_Initialised) DS1337_Init();

  if (address >= DS1337_MAXADDR)
  {
    return DS1337_ERROR_ADDRERR;
  }


  // ToDo: Check if I2C is ready

  // Clear buffers
  for ( i = 0; i < I2C_BUFSIZE; i++ )  I2CMasterBuffer = 0x00;
 
 

  // Write address bits to enable random read
  I2CWriteLength = 8;
  I2CReadLength = 7;
  I2CMasterBuffer[0] = DS1337_ADDR;                    // I2C device address
  I2CMasterBuffer[1] = 0x00;         // start Address
 
  // If you wish to read, you need to append the address w/read bit, though this
  // needs to be placed one bit higher than the size of I2CWriteLength which
  // may be unexpected
  I2CMasterBuffer[8] = DS1337_ADDR | DS1337_READBIT; 

  // Transmit command
  i2cEngine();

  // Fill response buffer
  for (i = 0; i < bufferLength; i++)
  {
    buffer = I2CSlaveBuffer;
  }

  return DS1337_ERROR_OK;
}

/**************************************************************************/
/*!
    @brief Writes the supplied bytes at a specified address.

    This function will write one or more bytes starting at the supplied
    address.  A maximum of 8 bytes can be written in one operation.

    @param[in]  address
                The 16-bit address where the write will start.  The
                maximum value for the address depends on the size of the
                EEPROM
    @param[in]  *buffer
                Pointer to the buffer that contains the values to write.
    @param[in]  bufferLength
                Length of the buffer
*/
/**************************************************************************/
DS1337_Error_e DS1337_WriteBuffer (uint16_t address, uint8_t *buffer, uint32_t bufferLength)
{
  if (!DS1337_Initialised) DS1337_Init();

  if (address >= DS1337_MAXADDR)
  {
    return DS1337_ERROR_ADDRERR;
  }

  if (bufferLength > 8)
  {
    return DS1337_ERROR_BUFFEROVERFLOW;
  }

  // ToDo: Check if I2C is ready

  // Clear write buffer
  for ( i = 0; i < I2C_BUFSIZE; i++ )
  {
    I2CMasterBuffer = 0x00;
  }

  // Write address bits and data to the master buffer
  I2CWriteLength = 1 + bufferLength;
  I2CReadLength = 0;
  I2CMasterBuffer[0] = DS1337_ADDR;                // I2C device address
  I2CMasterBuffer[1] = 0x00;   // Address
 
  for (i = 0; i < bufferLength; i++)
  {
    I2CMasterBuffer[i+2] = buffer;
  }

  // Transmit command
  i2cEngine();

  // Wait at least 10ms
  systickDelay(10 / CFG_SYSTICK_DELAY_IN_MS);
 
  return DS1337_ERROR_OK;
}

/*******************************************************************
the main:
/*************************************************************/
#include <stdio.h>

#include "projectconfig.h"
#include "sysinit.h"
#include "core/timer32/timer32.h"
#include "core/cpu/cpu.h"
#include "drivers/DS1337/DS1337.h"

int main (void)
{
uint8_t time_date[]={0x37,0x25,0x11,0x05,0x30,0x03,0x11},i;
 
  // Configure cpu and mandatory peripherals
  systemInit();
  cpuInit();
  timer32Init(0,TIMER32_CCLK_100MS); 
  gpioInit();
  DS1337_Init();

gpioSetDir (2,5,1);

DS1337_WriteBuffer (0x00, time_date,7);

for(i=0;i<7;i++)time_date=0x00;

while(time_date[0]!=0x47){//he get stuck here for ever :(
                                   DS1337_ReadBuffer (0x00, time_date, 7);
                                   gpioSetValue(2,5,1);
                                   systickDelay(500);
                                   gpioSetValue(2,5,0);
                                   systickDelay(500);
                                  }
while(1){   //he never get to this line this is my problem :(
                gpioSetValue(2,5,1);
                systickDelay(50);
                gpioSetValue(2,5,0);
                systickDelay(50);
                }
           
       
return(0);
}
[B]thanks for advance[/B]
0 Kudos
Reply
8 Replies

689 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by ldima55 on Thu May 05 02:45:14 MST 2011
ok i make a new board for the RTC and it working now here is the final code:
/************************************************************************* 
    @file     DS1337.h
    @author   ldima55
    @date     30 March 2011
    @version  0.10

   

 
*************************************************************************/

#ifndef _DS1337_H_
#define _DS1337_H_

#include "projectconfig.h"

#define DS1337_ADDR_WRITE    0xD0          // 11010000
#define DS1337_ADDR_READ   0xD1          // 11010001
#define DS1337_RW      0x01
#define DS1337_READBIT 0x01
#define DS1337_MAXADDR 0x07         

typedef enum
{
  DS1337_ERROR_OK = 0,               // Everything executed normally
  DS1337_ERROR_I2CINIT,              // Unable to initialise I2C
  DS1337_ERROR_I2CBUSY,              // I2C already in use
  DS1337_ERROR_ADDRERR,              // Address out of range
  DS1337_ERROR_BUFFEROVERFLOW,       // Max 8 bytes can be read/written in one operation
  DS1337_ERROR_LAST
}
DS1337_Error_e;

DS1337_Error_e DS1337_Init (void);
DS1337_Error_e DS1337_ReadBuffer (uint8_t address, uint8_t *buffer, uint32_t bufferLength);
DS1337_Error_e DS1337_WriteBuffer (uint8_t address, uint8_t *buffer, uint32_t bufferLength);

#endif
/*************************************************************************
 
    @file     DS1337.c
    @author   ldima55
    @date     30 March 2011
    @version  0.1

    @section DESCRIPTION

    Driver for DS1337 I2C Serial Real-Time-Clock-->(RTC).
    
*************************************************************************/

#include "rsa.h"
#include "core/systick/systick.h"
#include "core/i2c/i2c.h"

extern volatile uint8_t   I2CMasterBuffer[I2C_BUFSIZE];
extern volatile uint8_t   I2CSlaveBuffer[I2C_BUFSIZE];
extern volatile uint32_t  I2CReadLength, I2CWriteLength;

uint32_t i, timeout;

static bool DS1337_Initialised = false;

/**************************************************************************/
/*! 
    @brief  Initialises the I2C block
*/
/**************************************************************************/
DS1337_Error_e DS1337_Init()
{
  // Initialise I2C
  if (i2cInit(I2CMODE_MASTER) == false)
  {
    return DS1337_ERROR_I2CINIT;    /* Fatal error */
  }

  // Set initialisation flag
  DS1337_Initialised = true;

  return DS1337_ERROR_OK;
}

/*************************************************************************

    @brief Reads the specified number of bytes from the supplied address.

    This function will read one or more bytes starting at the supplied
    address.  A maximum of 7 bytes can be read in one operation.

    @param[in]  address
                The 8-bit address where the read will start.
    @param[in]  *buffer
                Pointer to the buffer that will store the read results
    @param[in]  bufferLength
                Length of the buffer

*************************************************************************/
DS1337_Error_e DS1337_ReadBuffer (uint8_t address, uint8_t *buffer, uint32_t bufferLength)
{
  if (!DS1337_Initialised) DS1337_Init();

  if (address >= DS1337_MAXADDR)
  {
    return DS1337_ERROR_ADDRERR;
  }

 
  // ToDo: Check if I2C is ready

  // Clear buffers
  for ( i = 0; i < I2C_BUFSIZE; i++ )
  {
    I2CMasterBuffer = 0x00;
    I2CSlaveBuffer = 0x00;
  }
  
  // Write address bits to enable random read
  I2CWriteLength = 2;
  I2CReadLength = bufferLength;
  I2CMasterBuffer[0] = DS1337_ADDR_WRITE;  // I2C device address
  I2CMasterBuffer[1] = address;         // start Address 
  I2CMasterBuffer[2] = DS1337_ADDR_READ; 

  // Transmit command
  i2cEngine();

  // Fill response buffer
  for (i = 0; i < bufferLength; i++)
  {
    buffer = I2CSlaveBuffer;
  }

  return DS1337_ERROR_OK;
}

/*************************************************************************
 
    @brief Writes the supplied bytes at a specified address.

    This function will write one or more bytes starting at the supplied
    address.  A maximum of 7 bytes can be written in one operation.

    @param[in]  address
                The 8-bit address where the write will start.
    @param[in]  *buffer
                Pointer to the buffer that contains the values to write.
    @param[in]  bufferLength
                Length of the buffer

*************************************************************************/
DS1337_Error_e DS1337_WriteBuffer (uint8_t address, uint8_t *buffer, uint32_t bufferLength)
{
  if (!DS1337_Initialised) DS1337_Init();

  if (address >= DS1337_MAXADDR)
  {
    return DS1337_ERROR_ADDRERR;
  }

  if (bufferLength > 7)
  {
    return DS1337_ERROR_BUFFEROVERFLOW;
  }

  // ToDo: Check if I2C is ready

  // Clear write buffer
  for ( i = 0; i < I2C_BUFSIZE; i++ )
  {
    I2CMasterBuffer = 0x00;
  }

  // Write address bits and data to the master buffer
  I2CWriteLength =2+bufferLength;
  I2CReadLength = 0;
  I2CMasterBuffer[0] = DS1337_ADDR_WRITE; // I2C device address
  I2CMasterBuffer[1] = address;   // Address 
  
  for (i = 0; i < bufferLength; i++)
  {
    I2CMasterBuffer[i+2] = buffer;
  }

  // Transmit command
  i2cEngine();

  // Wait at least 10ms
  systickDelay(10 / CFG_SYSTICK_DELAY_IN_MS);
  
  return DS1337_ERROR_OK;
}





 



0 Kudos
Reply

689 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by ldima55 on Sun May 01 12:02:41 MST 2011
Thank you again for your help I really appreciate it ,Because it is the only forum which really helped me.

-I'm using the i2c driver from Code-Base of microbuilder http://www.microbuilder.eu/Projects/LPC1343ReferenceDesign/LPC1343CodeBase.aspx
which is 100% working.

-I'm using a ds1337 but ds1307 is the same except the alarm that i not going to use at this moment , i just need to write and read the time from him.
 
-and i am still not try to debug but i will try In the coming days.
0 Kudos
Reply

689 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Rob65 on Sat Apr 30 11:37:29 MST 2011
Have you tried using a debugger?
That might be a good thing to do to figure out where the error is.

You could of course also send me a DS1337, then I can debug the problem for you :D

Which I2C driver are you using? The one provided byNXp or the one I enhanced?
The NXP one has known errors.

Rob
0 Kudos
Reply

689 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by ldima55 on Sat Apr 30 08:10:16 MST 2011
sorry for my delay ,but i think that i fix the driver but it still not working please help me.
H-file:
/************************************************************************* 
    @file     DS1337.h
    @author   Dima Teller
    @date     30 March 2011
    @version  0.10

   

 
*************************************************************************/

#ifndef _DS1337_H_
#define _DS1337_H_

#include "projectconfig.h"

#define DS1337_ADDR_WRITE    0xD0          // 11010000
#define DS1337_ADDR_READ   0xD1          // 11010001
#define DS1337_RW      0x01
#define DS1337_READBIT 0x01
#define DS1337_MAXADDR 0x07         

typedef enum
{
  DS1337_ERROR_OK = 0,               // Everything executed normally
  DS1337_ERROR_I2CINIT,              // Unable to initialise I2C
  DS1337_ERROR_I2CBUSY,              // I2C already in use
  DS1337_ERROR_ADDRERR,              // Address out of range
  DS1337_ERROR_BUFFEROVERFLOW,       // Max 8 bytes can be read/written in one operation
  DS1337_ERROR_LAST
}
DS1337_Error_e;

DS1337_Error_e DS1337_Init (void);
DS1337_Error_e DS1337_ReadBuffer (uint8_t address, uint8_t *buffer, uint32_t bufferLength);
DS1337_Error_e DS1337_WriteBuffer (uint8_t address, uint8_t *buffer, uint32_t bufferLength);
DS1337_Error_e DS1337_ReadByte (uint16_t address, uint8_t *buffer);
DS1337_Error_e DS1337_WriteByte (uint16_t address, uint8_t value);


#endif
C-file:
/*************************************************************************
 
    @file     DS1337.c
    @author   Dima Teller
    @date     30 March 2011
    @version  0.1

    @section DESCRIPTION

    Driver for DS1337 I2C Serial Real-Time-Clock-->(RTC).
    
*************************************************************************/

#include "rsa.h"
#include "core/systick/systick.h"
#include "core/i2c/i2c.h"

extern volatile uint8_t   I2CMasterBuffer[I2C_BUFSIZE];
extern volatile uint8_t   I2CSlaveBuffer[I2C_BUFSIZE];
extern volatile uint32_t  I2CReadLength, I2CWriteLength;

uint32_t i, timeout;

static bool DS1337_Initialised = false;

/**************************************************************************/
/*! 
    @brief  Initialises the I2C block
*/
/**************************************************************************/
DS1337_Error_e DS1337_Init()
{
  // Initialise I2C
  if (i2cInit(I2CMODE_MASTER) == false)
  {
    return DS1337_ERROR_I2CINIT;    /* Fatal error */
  }

  // Set initialisation flag
  DS1337_Initialised = true;

  return DS1337_ERROR_OK;
}

/*************************************************************************

    @brief Reads the specified number of bytes from the supplied address.

    This function will read one or more bytes starting at the supplied
    address.  A maximum of 7 bytes can be read in one operation.

    @param[in]  address
                The 8-bit address where the read will start.
    @param[in]  *buffer
                Pointer to the buffer that will store the read results
    @param[in]  bufferLength
                Length of the buffer

*************************************************************************/
DS1337_Error_e DS1337_ReadBuffer (uint8_t address, uint8_t *buffer, uint32_t bufferLength)
{
  if (!DS1337_Initialised) DS1337_Init();

  if (address >= DS1337_MAXADDR)
  {
    return DS1337_ERROR_ADDRERR;
  }

 
  // ToDo: Check if I2C is ready

  // Clear buffers
  for ( i = 0; i < I2C_BUFSIZE; i++ )
  {
    I2CMasterBuffer = 0x00;
    I2CSlaveBuffer = 0x00;
  }
  
  // Write address bits to enable random read
  I2CWriteLength = 2;
  I2CReadLength = bufferLength;
  I2CMasterBuffer[0] = DS1337_ADDR_WRITE;  // I2C device address
  I2CMasterBuffer[1] = address;         // start Address 
  I2CMasterBuffer[2] = DS1337_ADDR_READ; 

  // Transmit command
  i2cEngine();

  // Fill response buffer
  for (i = 0; i < bufferLength; i++)
  {
    buffer = I2CSlaveBuffer;
  }

  return DS1337_ERROR_OK;
}

/*************************************************************************
 
    @brief Writes the supplied bytes at a specified address.

    This function will write one or more bytes starting at the supplied
    address.  A maximum of 7 bytes can be written in one operation.

    @param[in]  address
                The 8-bit address where the write will start.
    @param[in]  *buffer
                Pointer to the buffer that contains the values to write.
    @param[in]  bufferLength
                Length of the buffer

*************************************************************************/
DS1337_Error_e DS1337_WriteBuffer (uint8_t address, uint8_t *buffer, uint32_t bufferLength)
{
  if (!DS1337_Initialised) DS1337_Init();

  if (address >= DS1337_MAXADDR)
  {
    return DS1337_ERROR_ADDRERR;
  }

  if (bufferLength > 7)
  {
    return DS1337_ERROR_BUFFEROVERFLOW;
  }

  // ToDo: Check if I2C is ready

  // Clear write buffer
  for ( i = 0; i < I2C_BUFSIZE; i++ )
  {
    I2CMasterBuffer = 0x00;
  }

  // Write address bits and data to the master buffer
  I2CWriteLength =2+bufferLength;
  I2CReadLength = 0;
  I2CMasterBuffer[0] = DS1337_ADDR_WRITE; // I2C device address
  I2CMasterBuffer[1] = address;   // Address 
  
  for (i = 0; i < bufferLength; i++)
  {
    I2CMasterBuffer[i+2] = buffer;
  }

  // Transmit command
  i2cEngine();

  // Wait at least 10ms
  systickDelay(10 / CFG_SYSTICK_DELAY_IN_MS);
  
  return DS1337_ERROR_OK;
}

/**************************************************************************/
the main:
int main (void)
{
uint8_t time_date[]={0x01,0x01,0x01,0x01,0x01,0x01,0x01},i;
systemInit();
  cpuInit();
  timer32Init(0,TIMER32_CCLK_100MS);  
  gpioInit();
  DS1337_Init();
gpioSetDir (2,5,1);
for(i=0;i<3;i++){//blink 3 times
                gpioSetValue(2,5,1);
                systickDelay(500);
                gpioSetValue(2,5,0);
                systickDelay(500);
                        }
DS1337_WriteBuffer (0x00, time_date,7);
while(time_date[0]!=0x02){
                           DS1337_ReadBuffer (0x00, time_date, 7);               
                                 }
for(i=0;i<3;i++){//blink 3 times
                gpioSetValue(2,5,1);
                systickDelay(500);
                gpioSetValue(2,5,0);
                systickDelay(500);
                        }
return(0);
}       
0 Kudos
Reply

689 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by larryvc on Tue Apr 26 01:43:18 MST 2011
Idima55,

I didn't get a chance to look at this today, so I'm really glad that Rob65 replied back to you.
0 Kudos
Reply

689 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by larryvc on Tue Apr 26 01:18:28 MST 2011
It's very late here now I will have to look at it in the morning.

I think someone else is replying now as well.

Hag Sameach .. if appropriate.

Larry
0 Kudos
Reply

689 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by ldima55 on Tue Apr 26 00:15:07 MST 2011
first thank for viewing my post
this returns a state that symbols an error (its a comment)
0 Kudos
Reply

689 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by larryvc on Mon Apr 25 12:22:34 MST 2011
Idima55,


Quote:
return DS1337_ERROR_I2CINIT;    /* Fatal error */



Is this just a comment or do you receive a fatal error here?

I'm looking at your code to see if I can find the error.

Larry
0 Kudos
Reply