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]