打印

分享个GD32F105R8操作FM24V05的例子

[复制链接]
1433|7
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
zhengbo7hui|  楼主 | 2015-11-30 14:54 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
采用模拟的方法:
/*******************************************************************************
* File Name          : i2c_fram.c
* Author             : Appcat
* Version            : V0.0.1
* Date               : 07/11/2009
* Description        : This file provides a set of functions needed to manage the
*                      communication between I2C peripheral and I2C FM24CL16 FRAM.
*******************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include "fm24v05m.h"
#include "stm32f10x_iwdg.h"

#define  WDT_REST                IWDG_ReloadCounter();
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define SCL_H         FM24V05_I2C_SCL_GPIO_PORT->BSRR = FM24V05_I2C_SCL_PIN
#define SCL_L         FM24V05_I2C_SCL_GPIO_PORT->BRR  = FM24V05_I2C_SCL_PIN
   
#define SDA_H         FM24V05_I2C_SDA_GPIO_PORT->BSRR = FM24V05_I2C_SDA_PIN
#define SDA_L         FM24V05_I2C_SDA_GPIO_PORT->BRR  = FM24V05_I2C_SDA_PIN

#define WP_H         FM24V05_I2C_WP_GPIO_PORT->BSRR = FM24V05_I2C_WP_PIN
#define WP_L         FM24V05_I2C_WP_GPIO_PORT->BRR  = FM24V05_I2C_WP_PIN

#define SCL_read      FM24V05_I2C_SCL_GPIO_PORT->IDR  & FM24V05_I2C_SCL_PIN
#define SDA_read      FM24V05_I2C_SDA_GPIO_PORT->IDR  & FM24V05_I2C_SDA_PIN

沙发
zhengbo7hui|  楼主 | 2015-11-30 14:55 | 只看该作者
/**
  * @brief  DeInitializes the FM24V05_I2C.
  * @param  None
  * @retval None
  */
void FM24V05_LowLevel_DeInit(void)
{
  GPIO_InitTypeDef  GPIO_InitStructure;
  NVIC_InitTypeDef NVIC_InitStructure;   
   
  /* FM24V05_I2C Peripheral Disable */
  I2C_Cmd(FM24V05_I2C, DISABLE);

  /* FM24V05_I2C DeInit */
  I2C_DeInit(FM24V05_I2C);

  /*!< FM24V05_I2C Periph clock disable */
  RCC_APB1PeriphClockCmd(FM24V05_I2C_CLK, DISABLE);
   
  /*!< GPIO configuration */  
  /*!< Configure FM24V05_I2C pins: SCL */
  GPIO_InitStructure.GPIO_Pin = FM24V05_I2C_SCL_PIN;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(FM24V05_I2C_SCL_GPIO_PORT, &GPIO_InitStructure);

  /*!< Configure FM24V05_I2C pins: SDA */
  GPIO_InitStructure.GPIO_Pin = FM24V05_I2C_SDA_PIN;
  GPIO_Init(FM24V05_I2C_SDA_GPIO_PORT, &GPIO_InitStructure);
  
  /* Configure and enable I2C DMA TX Channel interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = FM24V05_I2C_DMA_TX_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = FM24V05_I2C_DMA_PREPRIO;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = FM24V05_I2C_DMA_SUBPRIO;
  NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE;
  NVIC_Init(&NVIC_InitStructure);

  /* Configure and enable I2C DMA RX Channel interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = FM24V05_I2C_DMA_RX_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = FM24V05_I2C_DMA_PREPRIO;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = FM24V05_I2C_DMA_SUBPRIO;
  NVIC_Init(&NVIC_InitStructure);   
  
  /* Disable and Deinitialize the DMA channels */
  DMA_Cmd(FM24V05_I2C_DMA_CHANNEL_TX, DISABLE);
  DMA_Cmd(FM24V05_I2C_DMA_CHANNEL_RX, DISABLE);
  DMA_DeInit(FM24V05_I2C_DMA_CHANNEL_TX);
  DMA_DeInit(FM24V05_I2C_DMA_CHANNEL_RX);
}

使用特权

评论回复
板凳
zhengbo7hui|  楼主 | 2015-11-30 14:56 | 只看该作者
/**
  * @brief  Initializes the FM24V05_I2C.
  * @param  None
  * @retval None
  */
void FM24V05_LowLevel_Init(void)
{
  GPIO_InitTypeDef  GPIO_InitStructure;
  NVIC_InitTypeDef NVIC_InitStructure;  
   
  /*!< FM24V05_I2C_SCL_GPIO_CLK and FM24V05_I2C_SDA_GPIO_CLK Periph clock enable */
  RCC_APB2PeriphClockCmd(FM24V05_I2C_SCL_GPIO_CLK | FM24V05_I2C_SDA_GPIO_CLK | FM24V05_I2C_WP_GPIO_CLK, ENABLE);

  /*!< FM24V05_I2C Periph clock enable */
  RCC_APB1PeriphClockCmd(FM24V05_I2C_CLK, ENABLE);
  
  
  /*!< GPIO configuration */  
  /*!< Configure FM24V05_I2C pins: SCL */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);

  GPIO_PinRemapConfig(GPIO_Remap_I2C1 , ENABLE);

  GPIO_InitStructure.GPIO_Pin = FM24V05_I2C_SCL_PIN;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;  //
  GPIO_Init(FM24V05_I2C_SCL_GPIO_PORT, &GPIO_InitStructure);

  /*!< Configure FM24V05_I2C pins: SDA */
  GPIO_InitStructure.GPIO_Pin = FM24V05_I2C_SDA_PIN;
  GPIO_Init(FM24V05_I2C_SDA_GPIO_PORT, &GPIO_InitStructure);
  
  //写保护控制端

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

  GPIO_InitStructure.GPIO_Pin = FM24V05_I2C_WP_PIN;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(FM24V05_I2C_WP_GPIO_PORT, &GPIO_InitStructure);

  GPIOB->BRR = FM24V05_I2C_WP_PIN;


  /* Configure and enable I2C DMA TX Channel interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = FM24V05_I2C_DMA_TX_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = FM24V05_I2C_DMA_PREPRIO;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = FM24V05_I2C_DMA_SUBPRIO;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

  /* Configure and enable I2C DMA RX Channel interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = FM24V05_I2C_DMA_RX_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = FM24V05_I2C_DMA_PREPRIO;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = FM24V05_I2C_DMA_SUBPRIO;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);  
  
  /*!< I2C DMA TX and RX channels configuration */
  /* Enable the DMA clock */
  RCC_AHBPeriphClockCmd(FM24V05_I2C_DMA_CLK, ENABLE);

  /* I2C TX DMA Channel configuration */
  DMA_DeInit(FM24V05_I2C_DMA_CHANNEL_TX);
  FM24V05DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)FM24V05_I2C_DR_Address;
  FM24V05DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)0;   /* This parameter will be configured durig communication */
  FM24V05DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;    /* This parameter will be configured durig communication */
  FM24V05DMA_InitStructure.DMA_BufferSize = 0xFFFF;            /* This parameter will be configured durig communication */
  FM24V05DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  FM24V05DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  FM24V05DMA_InitStructure.DMA_PeripheralDataSize = DMA_MemoryDataSize_Byte;
  FM24V05DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
  FM24V05DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
  FM24V05DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
  FM24V05DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  DMA_Init(FM24V05_I2C_DMA_CHANNEL_TX, &FM24V05DMA_InitStructure);  
  
  /* I2C RX DMA Channel configuration */
  DMA_DeInit(FM24V05_I2C_DMA_CHANNEL_RX);



  DMA_Init(FM24V05_I2C_DMA_CHANNEL_RX, &FM24V05DMA_InitStructure);  
  
  /* Enable the DMA Channels Interrupts */
  DMA_ITConfig(FM24V05_I2C_DMA_CHANNEL_TX, DMA_IT_TC, ENABLE);
  DMA_ITConfig(FM24V05_I2C_DMA_CHANNEL_RX, DMA_IT_TC, ENABLE);   

}

使用特权

评论回复
地板
zhengbo7hui|  楼主 | 2015-11-30 14:57 | 只看该作者
/**
  * @}
  */


/** @defgroup STM32L152_EVAL_I2C_EE_Private_Function_Prototypes
  * @{
  */
/**
  * @}
  */


/** @defgroup STM32L152_EVAL_I2C_EE_Private_Functions
  * @{
  */

/**
  * @brief  DeInitializes peripherals used by the I2C EEPROM driver.
  * @param  None
  * @retval None
  */
void FM24V05_DeInit(void)
{
  FM24V05_LowLevel_DeInit();
}

/**
  * @brief  Initializes peripherals used by the I2C EEPROM driver.
  * @param  None
  * @retval None
  */
void FM24V05_Init(void)
{
  I2C_InitTypeDef  I2C_InitStructure;
  
  FM24V05_LowLevel_Init();
  
  /*!< I2C configuration */
  /* FM24V05_I2C configuration */
  I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
  I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
  I2C_InitStructure.I2C_OwnAddress1 = I2C_SLAVE_ADDRESS7;
  I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
  I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
  I2C_InitStructure.I2C_ClockSpeed = I2C_SPEED;
  
  /* FM24V05_I2C Peripheral Enable */
  I2C_Cmd(FM24V05_I2C, ENABLE);
  /* Apply FM24V05_I2C configuration after enabling it */
  I2C_Init(FM24V05_I2C, &I2C_InitStructure);

  /* Enable the FM24V05_I2C peripheral DMA requests */
  I2C_DMACmd(FM24V05_I2C, ENABLE);
  

/*!< FM24V05_ */   
}

使用特权

评论回复
5
zhengbo7hui|  楼主 | 2015-11-30 15:05 | 只看该作者
/**
  * @brief  Reads a block of data from the EEPROM.
  * @param  pBuffer : pointer to the buffer that receives the data read from
  *         the EEPROM.
  * @param  ReadAddr : EEPROM's internal address to start reading from.
  * @param  NumByteToRead : pointer to the variable holding number of bytes to
  *         be read from the EEPROM.
  *
  *        @NOTE The variable pointed by NumByteToRead is reset to 0 when all the
  *              data are read from the EEPROM. Application should monitor this
  *              variable in order know when the transfer is complete.
  *
  * @note When number of data to be read is higher than 1, this function just
  *       configures the communication and enable the DMA channel to transfer data.
  *       Meanwhile, the user application may perform other tasks.
  *       When number of data to be read is 1, then the DMA is not used. The byte
  *       is read in polling mode.
  *
  * @retval FM24V05_OK (0) if operation is correctly performed, else return value
  *         different from FM24V05_OK (0) or the timeout user callback.
  */
uint32_t FM24V05_ReadBuffer(uint8_t* pBuffer, uint16_t ReadAddr, uint16_t* NumByteToRead)
{  
  /* Set the pointer to the Number of data to be read. This pointer will be used
      by the DMA Transfer Completer interrupt Handler in order to reset the
      variable to 0. User should check on this variable in order to know if the
      DMA transfer has been complete or not. */
  FM24V05DataReadPointer = NumByteToRead;
  
  /*!< While the bus is busy */
  FM24V05Timeout = FM24V05_LONG_TIMEOUT;
  while(I2C_GetFlagStatus(FM24V05_I2C, I2C_FLAG_BUSY))
  {
    if((FM24V05Timeout--) == 0) return FM24V05_TIMEOUT_UserCallback();
  }
  
  /*!< Send START condition */
  I2C_GenerateSTART(FM24V05_I2C, ENABLE);
  
  /*!< Test on EV5 and clear it (cleared by reading SR1 then writing to DR) */
  FM24V05Timeout = FM24V05_FLAG_TIMEOUT;
  while(!I2C_CheckEvent(FM24V05_I2C, I2C_EVENT_MASTER_MODE_SELECT))
  {
    if((FM24V05Timeout--) == 0) return FM24V05_TIMEOUT_UserCallback();
  }
  
  /*!< Send EEPROM address for write */
  I2C_Send7bitAddress(FM24V05_I2C, I2C_SLAVE_ADDRESS7, I2C_Direction_Transmitter);

  /*!< Test on EV6 and clear it */
  FM24V05Timeout = FM24V05_FLAG_TIMEOUT;
  while(!I2C_CheckEvent(FM24V05_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
  {
    if((FM24V05Timeout--) == 0) return FM24V05_TIMEOUT_UserCallback();
  }

  /*!< Send the EEPROM's internal address to read from: MSB of the address first */
  I2C_SendData(FM24V05_I2C, (uint8_t)((ReadAddr & 0xFF00) >> 8));   

  /*!< Test on EV8 and clear it */
  FM24V05Timeout = FM24V05_FLAG_TIMEOUT;
  while(!I2C_CheckEvent(FM24V05_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTING))
  {
    if((FM24V05Timeout--) == 0) return FM24V05_TIMEOUT_UserCallback();
  }

  /*!< Send the EEPROM's internal address to read from: LSB of the address */
  I2C_SendData(FM24V05_I2C, (uint8_t)(ReadAddr & 0x00FF));   
  
  /*!< Test on EV8 and clear it */
  FM24V05Timeout = FM24V05_FLAG_TIMEOUT;
  while(I2C_GetFlagStatus(FM24V05_I2C, I2C_FLAG_BTF) == RESET)
  {
    if((FM24V05Timeout--) == 0) return FM24V05_TIMEOUT_UserCallback();
  }
  
  /*!< Send STRAT condition a second time */  
  I2C_GenerateSTART(FM24V05_I2C, ENABLE);
  
  /*!< Test on EV5 and clear it (cleared by reading SR1 then writing to DR) */
  FM24V05Timeout = FM24V05_FLAG_TIMEOUT;
  while(!I2C_CheckEvent(FM24V05_I2C, I2C_EVENT_MASTER_MODE_SELECT))
  {
    if((FM24V05Timeout--) == 0) return FM24V05_TIMEOUT_UserCallback();
  }
  
  /*!< Send EEPROM address for read */
  I2C_Send7bitAddress(FM24V05_I2C, I2C_SLAVE_ADDRESS7, I2C_Direction_Receiver);  
  
  /* If number of data to be read is 1, then DMA couldn't be used */
  /* One Byte Master Reception procedure (POLLING) ---------------------------*/
  if ((uint16_t)(*NumByteToRead) < 2)
  {
    /* Wait on ADDR flag to be set (ADDR is still not cleared at this level */
    FM24V05Timeout = FM24V05_FLAG_TIMEOUT;
    while(I2C_GetFlagStatus(FM24V05_I2C, I2C_FLAG_ADDR) == RESET)
    {
      if((FM24V05Timeout--) == 0) return FM24V05_TIMEOUT_UserCallback();
    }     
   
    /*!< Disable Acknowledgement */
    I2C_AcknowledgeConfig(FM24V05_I2C, DISABLE);   
   
    /* Clear ADDR register by reading SR1 then SR2 register (SR1 has already been read) */
    (void)FM24V05_I2C->SR2;
   
    /*!< Send STOP Condition */
    I2C_GenerateSTOP(FM24V05_I2C, ENABLE);
   
    /* Wait for the byte to be received */
    FM24V05Timeout = FM24V05_FLAG_TIMEOUT;
    while(I2C_GetFlagStatus(FM24V05_I2C, I2C_FLAG_RXNE) == RESET)
    {
      if((FM24V05Timeout--) == 0) return FM24V05_TIMEOUT_UserCallback();
    }
   
    /*!< Read the byte received from the EEPROM */
    *pBuffer = I2C_ReceiveData(FM24V05_I2C);
   
    /*!< Decrement the read bytes counter */
    (uint16_t)(*NumByteToRead)--;        
   
    /* Wait to make sure that STOP control bit has been cleared */
    FM24V05Timeout = FM24V05_FLAG_TIMEOUT;
    while(FM24V05_I2C->CR1 & I2C_CR1_STOP)
    {
      if((FM24V05Timeout--) == 0) return FM24V05_TIMEOUT_UserCallback();
    }  
   
    /*!< Re-Enable Acknowledgement to be ready for another reception */
    I2C_AcknowledgeConfig(FM24V05_I2C, ENABLE);   
  }
  else/* More than one Byte Master Reception procedure (DMA) -----------------*/
  {

          //************************************************************************DMA方式
    /*!< Test on EV6 and clear it */
    FM24V05Timeout = FM24V05_FLAG_TIMEOUT;
    while(!I2C_CheckEvent(FM24V05_I2C, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))
    {
      if((FM24V05Timeout--) == 0) return FM24V05_TIMEOUT_UserCallback();
    }  
      /* Enable the DMA clock */
    RCC_AHBPeriphClockCmd(FM24V05_I2C_DMA_CLK, ENABLE);
    /* Configure the DMA Rx Channel with the buffer address and the buffer size */
    FM24V05_LowLevel_DMAConfig((uint32_t)pBuffer, (uint16_t)(*NumByteToRead), FM24V05_DIRECTION_RX);
   
    /* Inform the DMA that the next End Of Transfer Signal will be the last one */
    I2C_DMALastTransferCmd(FM24V05_I2C, ENABLE);
   
    /* Enable the DMA Rx Channel */
    DMA_Cmd(FM24V05_I2C_DMA_CHANNEL_RX, ENABLE);  
         
  }
  
  /* If all operations OK, return FM24V05_OK (0) */
  return FM24V05_OK;
}

使用特权

评论回复
6
zhengbo7hui|  楼主 | 2015-11-30 15:07 | 只看该作者
/**
  * @brief  Writes more than one byte to the EEPROM with a single WRITE cycle.
  *
  * @note   The number of bytes (combined to write start address) must not
  *         cross the EEPROM page boundary. This function can only write into
  *         the boundaries of an EEPROM page.
  *         This function doesn't check on boundaries condition (in this driver
  *         the function FM24V05_WriteBuffer() which calls FM24V05_WritePage() is
  *         responsible of checking on Page boundaries).
  *
  * @param  pBuffer : pointer to the buffer containing the data to be written to
  *         the EEPROM.
  * @param  WriteAddr : EEPROM's internal address to write to.
  * @param  NumByteToWrite : pointer to the variable holding number of bytes to
  *         be written into the EEPROM.
  *
  *        @note The variable pointed by NumByteToWrite is reset to 0 when all the
  *              data are written to the EEPROM. Application should monitor this
  *              variable in order know when the transfer is complete.
  *
  * @note This function just configure the communication and enable the DMA
  *       channel to transfer data. Meanwhile, the user application may perform
  *       other tasks in parallel.
  *
  * @retval FM24V05_OK (0) if operation is correctly performed, else return value
  *         different from FM24V05_OK (0) or the timeout user callback.
  */
uint32_t FM24V05_WriteBuffer(uint8_t* pBuffer, uint16_t WriteAddr, uint8_t* NumByteToWrite)
{
   
  /* Set the pointer to the Number of data to be written. This pointer will be used
      by the DMA Transfer Completer interrupt Handler in order to reset the
      variable to 0. User should check on this variable in order to know if the
      DMA transfer has been complete or not. */
  
  FM24V05DataWritePointer = NumByteToWrite;  
  
  /*!< While the bus is busy  */
  FM24V05Timeout = FM24V05_LONG_TIMEOUT;
  while(I2C_GetFlagStatus(FM24V05_I2C, I2C_FLAG_BUSY))
  {
    if((FM24V05Timeout--) == 0) return FM24V05_TIMEOUT_UserCallback();
  }
  
  /*!< Send START condition */
  I2C_GenerateSTART(FM24V05_I2C, ENABLE);
  
  /*!< Test on EV5 and clear it */
  FM24V05Timeout = FM24V05_FLAG_TIMEOUT;
  while(!I2C_CheckEvent(FM24V05_I2C, I2C_EVENT_MASTER_MODE_SELECT))
  {
    if((FM24V05Timeout--) == 0) return FM24V05_TIMEOUT_UserCallback();
  }
  
  /*!< Send EEPROM address for write */
  FM24V05Timeout = FM24V05_FLAG_TIMEOUT;
  I2C_Send7bitAddress(FM24V05_I2C, I2C_SLAVE_ADDRESS7, I2C_Direction_Transmitter);

  /*!< Test on EV6 and clear it */
  FM24V05Timeout = FM24V05_FLAG_TIMEOUT;
  while(!I2C_CheckEvent(FM24V05_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
  {
    if((FM24V05Timeout--) == 0) return FM24V05_TIMEOUT_UserCallback();
  }

  
  /*!< Send the EEPROM's internal address to write to : MSB of the address first */
  I2C_SendData(FM24V05_I2C, (uint8_t)((WriteAddr & 0xFF00) >> 8));

  /*!< Test on EV8 and clear it */
  FM24V05Timeout = FM24V05_FLAG_TIMEOUT;  
  while(!I2C_CheckEvent(FM24V05_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
  {
    if((FM24V05Timeout--) == 0) return FM24V05_TIMEOUT_UserCallback();
  }  
  
  /*!< Send the EEPROM's internal address to write to : LSB of the address */
  I2C_SendData(FM24V05_I2C, (uint8_t)(WriteAddr & 0x00FF));
  
  
  /*!< Test on EV8 and clear it */
  FM24V05Timeout = FM24V05_FLAG_TIMEOUT;
  while(!I2C_CheckEvent(FM24V05_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
  {
    if((FM24V05Timeout--) == 0) return FM24V05_TIMEOUT_UserCallback();
  }  
  

  if ((uint8_t)(*NumByteToWrite) < 2)
  {
          I2C_SendData(FM24V05_I2C, (uint8_t)(pBuffer[0] & 0x00FF));
          while(!I2C_CheckEvent(FM24V05_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
          {
            if((FM24V05Timeout--) == 0) return FM24V05_TIMEOUT_UserCallback();
          }  
             /*!< Send STOP condition */
    I2C_GenerateSTOP(FM24V05_I2C, ENABLE);

  }
  else
  {

            /* Enable the DMA clock */
                  RCC_AHBPeriphClockCmd(FM24V05_I2C_DMA_CLK, ENABLE);
          /* Configure the DMA Tx Channel with the buffer address and the buffer size */
          FM24V05_LowLevel_DMAConfig((uint32_t)pBuffer, (uint8_t)(*NumByteToWrite), FM24V05_DIRECTION_TX);
            
          /* Enable the DMA Tx Channel */
          DMA_Cmd(FM24V05_I2C_DMA_CHANNEL_TX, ENABLE);

            /* DMA1 Channel6 transfer complete test */
                  //while(!DMA_GetFlagStatus(DMA1_FLAG_TC6));
  }
  /* If all operations OK, return FM24V05_OK (0) */
  return FM24V05_OK;

}

使用特权

评论回复
7
zhengbo7hui|  楼主 | 2015-11-30 15:14 | 只看该作者
/**
  * @brief  Initializes DMA channel used by the I2C EEPROM driver.
  * @param  None
  * @retval None
  */
void FM24V05_LowLevel_DMAConfig(uint32_t pBuffer, uint32_t BufferSize, uint32_t Direction)
{
  /* Initialize the DMA with the new parameters */
  if (Direction == FM24V05_DIRECTION_TX)
  {
    /* Configure the DMA Tx Channel with the buffer address and the buffer size */
    FM24V05DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)pBuffer;
    FM24V05DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;   
    FM24V05DMA_InitStructure.DMA_BufferSize = (uint32_t)BufferSize;  
    DMA_Init(FM24V05_I2C_DMA_CHANNEL_TX, &FM24V05DMA_InitStructure);  
  }
  else
  {
    /* Configure the DMA Rx Channel with the buffer address and the buffer size */
    FM24V05DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)pBuffer;
    FM24V05DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
    FM24V05DMA_InitStructure.DMA_BufferSize = (uint32_t)BufferSize;      
    DMA_Init(FM24V05_I2C_DMA_CHANNEL_RX, &FM24V05DMA_InitStructure);   
  }
}
/**
  * @brief  Wait for EEPROM Standby state.
  *
  * @note  This function allows to wait and check that EEPROM has finished the
  *        last operation. It is mostly used after Write operation: after receiving
  *        the buffer to be written, the EEPROM may need additional time to actually
  *        perform the write operation. During this time, it doesn't answer to
  *        I2C packets addressed to it. Once the write operation is complete
  *        the EEPROM responds to its address.
  *
  * @param  None
  * @retval FM24V05_OK (0) if operation is correctly performed, else return value
  *         different from FM24V05_OK (0) or the timeout user callback.
  */
uint32_t FM24V05_WaitEepromStandbyState(void)      
{
  __IO uint16_t tmpSR1 = 0;
  __IO uint32_t FM24V05Trials = 0;

  /*!< While the bus is busy */
  FM24V05Timeout = FM24V05_LONG_TIMEOUT;
  while(I2C_GetFlagStatus(FM24V05_I2C, I2C_FLAG_BUSY))
  {
    if((FM24V05Timeout--) == 0) return FM24V05_TIMEOUT_UserCallback();
  }

  /* Keep looping till the slave acknowledge his address or maximum number
     of trials is reached (this number is defined by FM24V05_MAX_TRIALS_NUMBER define
     in stm32_eval_i2c_ee.h file) */
  while (1)
  {
    /*!< Send START condition */
    I2C_GenerateSTART(FM24V05_I2C, ENABLE);

    /*!< Test on EV5 and clear it */
    FM24V05Timeout = FM24V05_FLAG_TIMEOUT;
    while(!I2C_CheckEvent(FM24V05_I2C, I2C_EVENT_MASTER_MODE_SELECT))
    {
      if((FM24V05Timeout--) == 0) return FM24V05_TIMEOUT_UserCallback();
    }   

    /*!< Send EEPROM address for write */
    I2C_Send7bitAddress(FM24V05_I2C, FM24V05Address, I2C_Direction_Transmitter);
   
    /* Wait for ADDR flag to be set (Slave acknowledged his address) */
    FM24V05Timeout = FM24V05_LONG_TIMEOUT;
    do
    {     
      /* Get the current value of the SR1 register */
      tmpSR1 = FM24V05_I2C->SR1;
      
      /* Update the timeout value and exit if it reach 0 */
      if((FM24V05Timeout--) == 0) return FM24V05_TIMEOUT_UserCallback();
    }
    /* Keep looping till the Address is acknowledged or the AF flag is
       set (address not acknowledged at time) */
    while((tmpSR1 & (I2C_SR1_ADDR | I2C_SR1_AF)) == 0);
     
    /* Check if the ADDR flag has been set */
    if (tmpSR1 & I2C_SR1_ADDR)
    {
      /* Clear ADDR Flag by reading SR1 then SR2 registers (SR1 have already
         been read) */
      (void)FM24V05_I2C->SR2;
      
      /*!< STOP condition */   
      I2C_GenerateSTOP(FM24V05_I2C, ENABLE);
        
      /* Exit the function */
      return FM24V05_OK;
    }
    else
    {
      /*!< Clear AF flag */
      I2C_ClearFlag(FM24V05_I2C, I2C_FLAG_AF);                  
    }
   
    /* Check if the maximum allowed numbe of trials has bee reached */
    if (FM24V05Trials++ == FM24V05_MAX_TRIALS_NUMBER)
    {
      /* If the maximum number of trials has been reached, exit the function */
      return FM24V05_TIMEOUT_UserCallback();
    }
  }
}

使用特权

评论回复
8
zb0830| | 2015-11-30 15:38 | 只看该作者
FM24V05好贵的啊,铁电存储器不知道什么时候能便宜下来

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

50

主题

502

帖子

1

粉丝