2014-02-03

Coocox I2C EEPROM example code reading notes 1

// *************************************************************************** 
   I2C_M_SETUP_Type transferMCfg; // I2C xmit/recv control/data structure
// *************************************************************************** 

// I2C_M_SETUP_Type;
typedef struct
{
  uint32_t sl_addr7bit; /**< Slave address in 7bit mode */
  uint8_t* tx_data; /**< Pointer to Transmit data - NULL if data transmit
                    is not used */
  uint32_t tx_length; /**< Transmit data length - 0 if data transmit
                                         is not used*/
  uint32_t tx_count; /**< Current Transmit data counter */
  uint8_t* rx_data; /**< Pointer to Receive data - NULL if data receive
 is not used */
  uint32_t rx_length; /**< Receive data length - 0 if data receive is
    not used */
  uint32_t rx_count; /**< Current Receive data counter */
  uint32_t retransmissions_max; /**< Max Re-Transmission value */
  uint32_t retransmissions_count; /**< Current Re-Transmission counter */
  uint32_t status; /**< Current status of I2C activity */
  void   (*callback)(void); /**< Pointer to Call back function when transmission complete
    used in interrupt transfer mode */
} I2C_M_SETUP_Type;


// *************************************************************************** 
   SYSCON_PeriphResetCmd(SYSCON_RSTPeriph_I2C, ENABLE);  // Enable then disable
   SYSCON_PeriphResetCmd(SYSCON_RSTPeriph_I2C, DISABLE); //   why this order?
// *************************************************************************** 

/*********************************************************************//**
 * @brief   Reset the SPI or I2C peripherals
 * @param[in]  Periph it can be
 *              - SYSCON_RSTPeriph_SSP0 : Reset SSP0
 *              - SYSCON_RSTPeriph_I2C  : Reset I2C                           
 *              - SYSCON_RSTPeriph_SSP1 : Reset SSP1
 *              NewState new state of the specified peripheral, it can be:
 *              - ENABLE  : Resets the peripheral
 *              - DISABLE : Peripheral reset de-asserted
 * @return none
 **********************************************************************/
void SYSCON_PeriphResetCmd(uint32_t Periph, FunctionalState NewState)
{
    if(NewState == ENABLE) {
        LPC_SYSCON->PRESETCTRL &= (~Periph) & 0x7;  
    } else if (NewState == DISABLE) {
        LPC_SYSCON->PRESETCTRL |= Periph & 0x7;          
    }       
}

// *************************************************************************** 
   I2C_PinsInit(I2CMODE_SF);  // Set SCL/SDA(P04/P05) mode = Standard Fast
   I2C_Init(LPC_I2C, 100000); // Set I2C peripheral clock rate = 100kHz
   I2C_Cmd(LPC_I2C, ENABLE);  // Enable I2C operation
// *************************************************************************** 

/********************************************************************//**
 * @brief Init I2C SDA and SCL pins, pins assign:
 *                I2C SCL : PIO0_4
 *                I2C SDA : PIO0_5
 * @param[in] mod, i2c mode, it can be
 *               -I2CMODE_SF  : Standard mode/ Fast-mode I2C
 *               -I2CMODE_SIO : Standard I/O functionality
 *               -I2CMODE_FP  : Fast-mode Plus I2C
 * @return None
 *********************************************************************/
void I2C_PinsInit(I2CMODE_Typedef mod)
{
    IOCON_SetPinFunc(IOCON_PIO0_4, PIO0_4_FUN_SCL);   /* I2C SCL - PIO0_4 */
    IOCON_SetPinFunc(IOCON_PIO0_5, PIO0_5_FUN_SDA);   /* I2C SDA - PIO0_5 */

    IOCON_SetI2CMode(IOCON_PIO0_4, mod);
    IOCON_SetI2CMode(IOCON_PIO0_5, mod);
}

/********************************************************************//**
 * @brief Initializes the I2Cx peripheral with specified parameter.                
 *                
 * @param[in] I2Cx I2C peripheral selected, should be LPC_I2C
 * @param[in] clockrate Target clock rate value to initialized I2C
 * peripheral
 * @return None
 *********************************************************************/
void I2C_Init(I2C_TypeDef *I2Cx, uint32_t clockrate)
{
CHECK_PARAM(PARAM_I2Cx(I2Cx));

    /* Enable I2C clock */
SYSCON_AHBPeriphClockCmd (SYSCON_AHBPeriph_I2C, ENABLE);

    /* Set clock rate */
    I2C_SetClock(I2Cx, clockrate);
    /* Set I2C operation to default */
    I2Cx->CONCLR = (I2C_I2CONCLR_AAC | I2C_I2CONCLR_STAC | I2C_I2CONCLR_I2ENC);
}


/*********************************************************************//**
 * @brief Enable or disable I2C peripheral's operation
 * @param[in] I2Cx I2C peripheral selected, LPC_I2C
 * @param[in] NewState New State of I2Cx peripheral's operation
 * @return none
 **********************************************************************/
void I2C_Cmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
{
CHECK_PARAM(PARAM_FUNCTIONALSTATE(NewState));
CHECK_PARAM(PARAM_I2Cx(I2Cx));

if (NewState == ENABLE)
{
I2Cx->CONSET = I2C_I2CONSET_I2EN;
}
else
{
I2Cx->CONCLR = I2C_I2CONCLR_I2ENC;
}
}

// *************************************************************************** 
   unsigned char send_buf[10] = {0,0, 1,2,3,4,5,6,7};
   unsigned char rece_buf[10] = {0};
// *************************************************************************** 

// *************************************************************************** 
   transferMCfg.sl_addr7bit = 0xA0>>1; // Standard EEPROM 7 bit address = 1010000
   transferMCfg.tx_data = send_buf;    // Send data in send buffer 
   transferMCfg.tx_length = 8;         // Send 8 bits   
   transferMCfg.rx_data = NULL;        // Receive buffer not used 
   transferMCfg.rx_length = 0;         // Receive buffer length = 0
   transferMCfg.retransmissions_max = 3; // maximum try 3 times
   I2C_MasterTransferData(LPC_I2C, &transferMCfg, I2C_TRANSFER_POLLING); 
     // send polling mode
// *************************************************************************** 

// *************************************************************************** 
   for(i=0;i<100;i++) // Delay for EEPROM operation
   {
        for(j=0;j<1000;j++);
    }
// *************************************************************************** 

// *************************************************************************** 
   transferMCfg.tx_data = send_buf;      // Send 
   transferMCfg.tx_length = 2;           //   2 zero bits to EEPROM
   transferMCfg.rx_data = rece_buf;      // Receive
   transferMCfg.rx_length = 6;           //   6 data bits from EEPROM    
   transferMCfg.retransmissions_max = 3; // Maximum try 3 times
   I2C_MasterTransferData(LPC_I2C, &transferMCfg, I2C_TRANSFER_POLLING); 
     // receive in polling mode
// *************************************************************************** 


No comments:

Post a Comment