⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 i2cbus.cpp

📁 并行口仿真i2c总线
💻 CPP
📖 第 1 页 / 共 5 页
字号:
/* Name : StartI2C                                                            */
/* Role : Create a START condition on the I2C bus                             */
/* Interface : None                                                           */
/* Pre-condition : MUST be in STOP mode                                       */
/* Constraints : None                                                         */
/* Behavior :                                                                 */
/*   DO                                                                       */
/*     [Put the I2C bus in START mode]                                        */
/*   OD                                                                       */
/*----------------------------------------------------------------------------*/
/* PROC StartI2C                                                              */
/* (                                                                          */
/* )                                                                          */
/* DATA                                                                       */
/* ATAD                                                                       */
/*                                                                            */
/* DO                                                                         */
/*   [Put the I2C bus in START mode] =                                        */
/*   DO                                                                       */
/*     [Put SDA to '0' and SCL to '1', falling edge of SDA first]             */
/*     [Put SDA to '0' and SCL to '0', falling edge of SCL then]              */
/*   OD                                                                       */
/* OD                                                                         */
/*--- END FUNCTION HEADER ----------------------------------------------------*/
void FluxI2C::StartI2C(void)
{
  // On PENSE que l'on se trouve APRES un 'InitI2C' ou
  // un 'StopI2C', donc que SDA et SCL sont a '1' !

  // On passe D'ABORD SDA a '0'
  // bit 0 a '1' : AlimON
  // bit 1 a '1' : SDA a '0' <<
  // bit 2 a '0' : SCL a '1'
  SetBitI2C(CNST_BITN_LPTP_WSDA);
  delay(CNST_DATA_TIME_TEMPO);

  // On passe ENSUITE SCL a '0'
  // bit 0 a '1' : AlimON
  // bit 1 a '1' : SDA a '0'
  // bit 2 a '1' : SCL a '0' <<
  SetBitI2C(CNST_BITN_LPTP_WSCL);
  delay(CNST_DATA_TIME_TEMPO);
}

/*--- START FUNCTION HEADER --------------------------------------------------*/
/* Name : StopI2C                                                             */
/* Role : Create a STOP condition on the I2C bus                              */
/* Interface : None                                                           */
/* Pre-condition : None                                                       */
/* Constraints : None                                                         */
/* Behavior :                                                                 */
/*   DO                                                                       */
/*     [Put the I2C bus in STOP mode]                                         */
/*   OD                                                                       */
/*----------------------------------------------------------------------------*/
/* PROC StopI2C                                                               */
/* (                                                                          */
/* )                                                                          */
/* DATA                                                                       */
/* ATAD                                                                       */
/*                                                                            */
/* DO                                                                         */
/*   [Put the I2C bus in STOP mode] =                                         */
/*   DO                                                                       */
/*     [Put SDA to '0' and SCL to '1', rising edge of SCL first]              */
/*     [Put SDA to '1' and SCL to '1', rising edge of SDA then]               */
/*   OD                                                                       */
/* OD                                                                         */
/*--- END FUNCTION HEADER ----------------------------------------------------*/
void FluxI2C::StopI2C(void)
{
  // Il s'agit de retrouver un etat SIMILAIRE a un
  // 'InitI2C', donc SDA et SCL a '1'
  // On SAIT qu'a ce niveau, SDA et SCL sont a '0'

  // on passe D'ABORD SCL a '1'
  // bit 0 a '1' : AlimON
  // bit 1 a '1' : SDA a '0'
  // bit 2 a '0' : SCL a '1' <<
  ClearBitI2C(CNST_BITN_LPTP_WSCL);
  delay(CNST_DATA_TIME_TEMPO);

  // Puis ENSUITE SDA a '1' <<
  // La, en fait, y'a 'JUSTE' a reinitialiser...
  ClearBitI2C(CNST_BITN_LPTP_WSDA);
  delay(CNST_DATA_TIME_TEMPO);
}

/*--- START FUNCTION HEADER --------------------------------------------------*/
/* Name : SAcknowledgeI2C                                                     */
/* Role : 'Acknowledge' condition returned by the Slave on emitting           */
/* Interface : OUT SAcknowledgeI2C                                            */
/*                 0 - Error                                                  */
/*                 1 - OK                                                     */
/* Pre-condition : A byte MUST have been sent to a slave                      */
/* Constraints : None                                                         */
/* Behavior :                                                                 */
/*   DO                                                                       */
/*     [Return Slave's 'Acknowledge']                                         */
/*   OD                                                                       */
/*----------------------------------------------------------------------------*/
/* PROC SAcknowledgeI2C                                                       */
/* (                                                                          */
/* )                                                                          */
/* DATA                                                                       */
/* ATAD                                                                       */
/*                                                                            */
/* DO                                                                         */
/*   [Return Slave's 'Acknowledge'] =                                         */
/*   DO                                                                       */
/*     [Set SDA to '1', and keep SCL to '0']                                  */
/*     [Set SCL to '1', rising edge of SCL]                                   */
/*     [The Slave change the state of SDA according to the its receive state] */
/*     [Set SDA and SCL to '0']                                               */
/*     [Return the Slave's receive state]                                     */
/*   OD                                                                       */
/* OD                                                                         */
/*--- END FUNCTION HEADER ----------------------------------------------------*/
int FluxI2C::SAcknowledgeI2C(void)
{
  int ReturnStatut;

  // Methode :
  // 1. On passe SDA a '1'
  // 2. On passe un cycle d'horloge sur SCL
  // 3. On retourne l'etat de SDA

  // On passe SDA a '1' (SCL a '0')
  ClearBitI2C(CNST_BITN_LPTP_WSDA);
  delay(CNST_DATA_TIME_TEMPO);

  // On passe un cycle d'horloge sur SCL (front Montant)
  ClearBitI2C(CNST_BITN_LPTP_WSCL);
  delay(CNST_DATA_TIME_TEMPO);

  // A ce moment, l'Esclave change l'etat de SDA suivant l'etat de la reception
  // On GARDE l'etat de l'Esclave indique sur SDA
  // Si SDA est a '1', c'est qu'il y a eu une ERREUR -> '0'
  // Sinon, c'est Toubon -> '1'
  ReturnStatut=(~TestBitI2C(CNST_BITN_LPTP_RSDA))&1;

  // On termine le cycle d'horloge sur SCL (front Descendant)
  // On passe TOUT a '0' !!!
  SetBitI2C(CNST_BITN_LPTP_WSDA);
  SetBitI2C(CNST_BITN_LPTP_WSCL);
  delay(CNST_DATA_TIME_TEMPO);

  // On retourne l'etat de SDA
  return ReturnStatut;
}

/*--- START FUNCTION HEADER --------------------------------------------------*/
/* Name : RAcknowledgeI2C                                                     */
/* Role : 'Acknowledge' condition returned TO the Slave on receiving from     */
/* Interface : None                                                           */
/* Pre-condition : None                                                       */
/* Constraints : ALWAYS return OK !                                           */
/* Behavior :                                                                 */
/*   DO                                                                       */
/*     [Return OK to the Slave]                                               */
/*   OD                                                                       */
/*----------------------------------------------------------------------------*/
/* PROC RAcknowledgeI2C                                                       */
/* (                                                                          */
/* )                                                                          */
/* DATA                                                                       */
/* ATAD                                                                       */
/*                                                                            */
/* DO                                                                         */
/*   [Return OK to the Slave] =                                               */
/*   DO                                                                       */
/*     [Set SDA to '0', reception OK]                                         */
/*     [Validate with a complete clock cycle, SCL to '1', then '0']           */
/*   OD                                                                       */
/* OD                                                                         */
/*--- END FUNCTION HEADER ----------------------------------------------------*/

// WARNING, I NEVER TRIED THIS FUNCTION, BUT IT SHOULD WORKS FINE !!!

void FluxI2C::RAcknowledgeI2C(void)
{
  // Methode :
  // 1. On passe SDA a '0'
  // 2. On passe un cycle d'horloge sur SCL

  // On passe SDA a '0' (SCL a '0')
  SetBitI2C(CNST_BITN_LPTP_WSDA);
  delay(CNST_DATA_TIME_TEMPO);

  // On passe un cycle d'horloge sur SCL (front Montant)
  ClearBitI2C(CNST_BITN_LPTP_WSCL);
  delay(CNST_DATA_TIME_TEMPO);

  // On termine le cycle d'horloge sur SCL (front Descendant)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -