Skip to content

Esp32 to stm8 i2c communication #3867

@nikhil8333

Description

@nikhil8333

@me-no-dev
Hi ,
I was trying to implement a i2c protocol with esp 32 as master and multiple stm8's as slaves. Code was implimented using adruino ide and wire.h lib was used. Initial address scanning was successful but unable to communicate with slave. Can you plese help me
my esp code:

 * Wire - I2C Scanner
 *
 * for example the WeMos D1 Mini I2C bus uses pins:
 * D1 = SCL
 * D2 = SDA
 */

#include <Wire.h>
#define SERIAL_BUFFER_SIZE 255
const int SCLpin = 22;
const int SDApin = 21; // For ESP32 Lolin D32

void setup()
{
  Serial.begin(115200);
  Serial.println("I2C Scanner");
  Serial.println("SDA Pin = "+String(SDA));
  Serial.println("SCL Pin = "+String(SCL));
  
  Wire.begin(SDApin, SCLpin);
}

uint8_t x = 0x00;
char adress = 0;

void loop()
{
   byte error;
  //if (Serial.available() > 0) {
  
  //adress=Serial.read();
  Wire.setClock(24) ;
  Serial.println("Starting at address 0x30");
   Wire.beginTransmission(24);
   //delay(100);
    Wire.write(x);
   //Wire.write(24);
                // sends one byte  
                 //delay(2000);
   error =Wire.endTransmission();
   delay(2000);
  
 if (error == 0)
    {
      Serial.print("successful data transmission at address 0x30");
      Serial.println(" !");
    }
    else if (error == 1)
    {
      Serial.print("huge data error at address 0x30");
      Serial.println(" !");
    }
    
    else {
    Serial.println("error hahahaha!");
  }
  //x++;
  delay(2000);
  
}//}

my stm8 code

/* Includes ------------------------------------------------------------------*/
#include "stm8s.h"
//#include "stm8s_eval.h"
#include "main.h"

/** @addtogroup I2C_TwoBoards
  * @{
  */

/** @addtogroup I2C_DataExchange
  * @{
  */


/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
unsigned int com = 0;
//__IO uint16_t Slave_Buffer_Rx[1];
/* Private function prototypes -----------------------------------------------*/
static void GPIO_Config(void);
/* Private functions ---------------------------------------------------------*/
/* Public functions ----------------------------------------------------------*/

/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
void main(void)
{
/* GPIO Configuration  -----------------------------------------*/
  GPIO_Config();
  /* system_clock / 1 */
  //CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
  
  /* Initialize LEDs mounted on STM8/128-EVAL board */
  //STM_EVAL_LEDInit(LED2);
		//STM_EVAL_LEDOff(LED2);
	
  I2C_DeInit();
  /* Initialize I2C peripheral */

#ifdef I2C_slave_7Bits_Address
  I2C_Init(100000, SLAVE_ADDRESS, I2C_DUTYCYCLE_2, I2C_ACK_CURR, I2C_ADDMODE_7BIT, (CLK_GetClockFreq() / 1000000));           
#else
  I2C_Init(100000, SLAVE_ADDRESS, I2C_DUTYCYCLE_2, I2C_ACK_CURR,I2C_ADDMODE_10BIT, 16);
#endif
//I2C_Cmd(ENABLE);
  /* Enable Error Interrupt*/
  I2C_ITConfig((I2C_IT_TypeDef)(I2C_IT_ERR | I2C_IT_EVT | I2C_IT_BUF), ENABLE);

  /* Enable general interrupts */
  enableInterrupts();

  /*Main Loop */
  while (1)
  {
    if ( com == 2 ){
      GPIO_WriteHigh(LEDS_PORT, (GPIO_Pin_TypeDef)(LED1_PIN));
    }
  }
}
static void GPIO_Config(void)
{
  /* Initialize I/Os in Output Mode for LEDs */
  GPIO_Init(LEDS_PORT, (GPIO_Pin_TypeDef)(LED1_PIN),
            GPIO_MODE_OUT_PP_LOW_FAST);
     
  
}
#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *   where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
    
  }
}
#endif

/**
  * @}
  */

/**
  * @}
  */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

Metadata

Metadata

Assignees

No one assigned

    Labels

    Status: StaleIssue is stale stage (outdated/stuck)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions