连接eeprom与微控制器时遇到的错误

时间:2016-05-13 08:10:18

标签: c pointers structure microcontroller

我不是专家c程序员,在c代码中我遇到了这些错误。我有很多并试图解决它们但无法解决这些问题。代码如下:

/*
 * EEPROM.c
 * interfacing microchip 24aa64f IC with atmel sam4e
 */ 

#include <asf.h>
#include "EEPROM_I2C.h"

#define DEVICE_ADDRESS       0x50   // 7-bit device identifier 0101000, (refer datasheet)
//#define EEPROM_NAME          24AA6F
#define I2C_FAST_MODE_SPEED  400000//TWI_BUS_CLOCK 400KHz
#define TWI_CLK_DIVIDER      2
#define TWI_CLK_DIV_MIN      7
#define TWI_CLK_CALC_ARGU    4
#define TWI_CLK_DIV_MAX      0xFF


/*************************** Main function ******************************/
int eeprom_main( void )
{

      struct micro24 ptMicro24 ;
      typedef struct twi_options twi_options_t;
      typedef struct Twi_registers Twi;

    char TxBuffer[128] ;
    char RxBuffer[128] ;
    int BufferIndex;
    unsigned int PageCount;
    unsigned int error = 0 ;
    unsigned int i;

   ptMicro24.PageSize = 32;
   ptMicro24.NumOfPage = 128;
   ptMicro24.EepromSize = 128*32;
   ptMicro24.SlaveAddress = DEVICE_ADDRESS;
   ptMicro24.EepromName = 64;

 /*****************************  CLOCK SETTINGS TO GET 400KHz **********************
 * Set the I2C bus speed in conjunction with the clock frequency.
 * param p_twi Pointer to a TWI instance.
 * return value PASS\Fail New speed setting is accepted\rejected
 **********************************************************************************/

uint32_t twi_set_speed(struct Twi_registers *Twi, uint32_t ul_speed, uint32_t ul_mck)
//uint32_t twi_set_speed(Twi *p_twi, uint32_t ul_speed, uint32_t ul_mck)
{
    uint32_t ckdiv = 0; //clock divider is used to increase both TWCK high and low periods (16-18)
    uint32_t c_lh_div; //CHDIV (0-7) and CLDIV (8-15)

    if (ul_speed > I2C_FAST_MODE_SPEED) {   //ul_speed is the desired I2C bus speed
        return FAIL;
    }

    c_lh_div = ul_mck / (ul_speed * TWI_CLK_DIVIDER) - TWI_CLK_CALC_ARGU; //ul_mck main clock of the device

    /* cldiv must fit in 8 bits, ckdiv must fit in 3 bits */
    while ((c_lh_div > TWI_CLK_DIV_MAX) && (ckdiv < TWI_CLK_DIV_MIN)) 
    {
        ckdiv++; // Increase clock divider
        c_lh_div /= TWI_CLK_DIVIDER; //Divide cldiv value
    }

    /* set clock waveform generator register */
    Twi->TWI_CWGR =
            TWI_CWGR_CLDIV(c_lh_div) | TWI_CWGR_CHDIV(c_lh_div) |
            TWI_CWGR_CKDIV(ckdiv);

    return PASS;
}

/************************************ Initialize TWI master mode ************************
 * Set the control register TWI_CR by MSEN and SVDIS
 * param p_opt Options for initializing the TWI module
 * return TWI_SUCCESS if initialization is complete
 * twi_options... structure contains clock speed, master clock, chip and smbus
 *****************************************************************************************/
uint32_t twi_master_start(struct Twi_registers *Twi, struct twi_options_t *twi_options_t)
//uint32_t twi_master_start(Twi *p_twi, const twi_options_t *p_opt)
{
    uint32_t status = TWI_SUCCESS; // status success return code is 0

    // Enable master mode and disable slave mode in TWI_CR 
        Twi -> TWI_CR_START = TWI_CR_START;
        Twi->TWI_CR_MSEN = TWI_CR_MSEN; // Set Master Enable bit
        Twi->TWI_CR_SVDIS = TWI_CR_SVDIS; // Set Slave Disable bit 

    /* Select the speed */
    //new//if (twi_set_speed(Twi->TWI_SR, twi_options_t->speed, twi_options_t->master_clk) == FAIL) 
    //if (twi_set_speed(Twi, twi_options_t->speed, twi_options_t->master_clk) == FAIL) 
    //{
        //status = TWI_INVALID_ARGUMENT; /* The desired speed setting is rejected */
    //}

    if (twi_options_t->smbus == 0)
    {
    Twi->TWI_CR_QUICK == 0;
    status = TWI_INVALID_ARGUMENT;
    }

    else

    if (twi_options_t->smbus == 1) 
    {
        Twi->TWI_CR_QUICK == 1;
        status = TWI_SUCCESS;
    }
    return status;
}


/***************************** WriteByte Function ********************************
This function uses a two bytes internal address (IADR) along with
Internal word address of eeprom.
Return Value: None
***********************************************************************************/

void WriteByte (struct micro24 *ptMicro24, char Data2Write,
                            unsigned int Address)

//Data2Write is the data to be written n the eeprom
//struct <micro24 *ptMicro24> : Structure of Microchip 24AA Two-wire Eeprom
//unsigned int Address>: Address where to write

{
  unsigned int WordAddress;
  unsigned int SlaveAddress;
  unsigned char p0=0;

  TWI_CR_START ==1;

   if (ptMicro24->EepromName == 64 )
  {
    if ( Address > 0xFFFF) 
    {
        p0 = 1;
        /* Mask the 17th bit to get the 16th LSB */
        WordAddress = Address & 0xFFFF ;
        SlaveAddress = ptMicro24->SlaveAddress + (p0<<16) ;
    }

   else {
    SlaveAddress = ptMicro24->SlaveAddress ;
    WordAddress = Address ;
    }
  }
  TWI_CR_STOP ==1;

  //TWI_WriteSingleIadr(TWI_IADR_IADR,SlaveAddress, WordAddress,  
    //                  TWI_MMR_IADRSZ_2_BYTE, &Data2Write); // declared as extern
                      // to write to internal address, utilizing internal address and master mode register    

//}
 /******************** Increase Speed Function *****************************
 * TWI is accessed without calling TWI functions           
 /***************************************************************************/

 int NumOfBytes, Count;
 int status;
 uint32_t Buffer;

  /* Enable Master Mode of the TWI */
    TWI_CR_MSEN == 1;
 // Twi.TWI_CR_MSEN ==1;
  //TWI_CR->TWI_CR_MSEN = TWI_CR_MSEN ;

  /* Set the TWI Master Mode Register */

  Twi->TWI_MMR =  (SlaveAddress & (~TWI_MMR_MREAD) | (TWI_MMR_IADRSZ_2_BYTE));

  /* Set the internal address to access the wanted page */
  Twi -> TWI_IADR =   WordAddress ;

  /* Wait until TXRDY is high to transmit the next data */
  status = TWI_SR_TXRDY; 
  while (!(status & TWI_SR_TXRDY))
    status = TWI_SR_TXRDY;

  /* Send the buffer to the page */
  for (Count=0; Count < NumOfBytes ;Count++ )
  {
    Twi ->TWI_THR_TXDATA = Buffer++;

    /* Wait until TXRDY is high to transmit the next data */
     status = TWI_SR_TXRDY;
     while (!(status & TWI_SR_TXRDY))
     status = TWI_SR_TXRDY;
  }

  /* Wait for the Transmit complete is set */
  status = TWI_SR_TXCOMP;
  while (!(status & TWI_SR_TXCOMP))
    status = TWI_SR_TXCOMP;

// add some wait function according to datasheet before sending the next data
// e.g: 10ms
// e.g: WaitMiliSecond (10);
}

/****************************** ReadByte Function **************************
This function uses a two bytes internal address (IADR) along with
Internal word address of eeprom.
Return Value: None
****************************************************************************/
char ReadByte (struct micro24 *ptMicro24,
unsigned int Address)  //int Address to read
{
    unsigned int WordAddress;
    unsigned int SlaveAddress;
    char Data2Read ;

    unsigned char p0=0;

    TWI_CR_START == 1;
    //p_twi -> TWI_CR_START = TWI_CR_START;


    if (ptMicro24->EepromName == 64)
    {
        if ( Address > 0xFFFF) {
            p0 = 1;
            // Mask the 17th bit to get the 16th LSB 
            WordAddress = Address & 0xFFFF ;
            SlaveAddress = ptMicro24->SlaveAddress + (p0<<16) ;
        }

        else {
            SlaveAddress = ptMicro24->SlaveAddress ;
            WordAddress = Address ;
        }
    }

//TWI_ReadSingleIadr(TWI_IADR_IADR,SlaveAddress,WordAddress,
    //               TWI_MMR_IADRSZ_2_BYTE,&Data2Read);  
    // declared as extern
    // to write to internal address, utilizing internal address and master mode register
return (Data2Read);
}
}

错误是:

(24,19): error: storage size of 'ptMicro24' isn't known
67,5): error: dereferencing pointer to incomplete type
      Twi->TWI_CWGR =
error: expected identifier before '(' token
     #define TWI_CR_START (0x1u << 0) /**< \brief (TWI_CR) Send a START Condition */
error: expected identifier before '(' token
     #define TWI_CR_MSEN (0x1u << 2) /**< \brief (TWI_CR) TWI Master Mode Enabled */
error: expected identifier before '(' token
     #define TWI_CR_SVDIS (0x1u << 5) /**< \brief (TWI_CR) TWI Slave Mode Disabled */
error: dereferencing pointer to incomplete type
         if (twi_options_t->smbus == 0)

1 个答案:

答案 0 :(得分:0)

似乎缺少struct micro24的声明,这可能是第一次出错的原因:error: storage size of 'ptMicro24' isn't known.

Twi_registers的声明相同,导致其他错误。

要么您忘记声明这些结构,要么包含声明它们的头文件。