espressif / arduino-esp32

Arduino core for the ESP32
GNU Lesser General Public License v2.1
13.36k stars 7.37k forks source link

Esp32 to stm8 i2c communication #3867

Closed nikhil8333 closed 4 years ago

nikhil8333 commented 4 years ago

@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****/
stickbreaker commented 4 years ago

@nikhil8333 I don't have any experience with your STM micro.

this is how I would clean up your ESP code:

#include <Wire.h>

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);
  Wire.setClock(100000) ; // set i2c frequency to 100k, needs at least 3.3k pullups to 3.3v
}

uint8_t x = 0x00;

void loop()
{
   byte error;
   Serial.println("testing address 0x24");

   Wire.beginTransmission(24);
   Wire.write(x);
   error =Wire.endTransmission();

   if (error != 0) {
       Serial.printf("I2C error =%d(%s)\n",error, Wire.getErrorText(error));
   }
   else Serial.printf("Success!\n");

   delay(2000);

}

Here is a link to an I2C scanner that will display all slave I2C devices on the bus: i2c scanner

Chuck.

nikhil8333 commented 4 years ago

thanks for the help chuck, still no luck with the code, getting error 1 from your code same as mine

Here is a link to an I2C scanner that will display all slave I2C devices on the bus: i2c scanner

already adopted the same code for scanning, and my scanning has no problem. I was able to retrieve all the slave address. only my write and read commands are giving me errors. is there any other way around.

Thanks in advance.

stickbreaker commented 4 years ago

@nikhil8333 recompile with CORE_DEBUG_LEVEL set to INFO, the I2c subsection will log any problems.

I2C_ERROR_DEV is returned if the hardware was not successfully configured, or the Wire() object is corrupted.

Additionally you can enable the i2c Debug buffer by uncommenting line 45

//#define ENABLE_I2C_DEBUG_BUFFER

in cores\esp32\esp32_hal_i2c.c here https://github.com/espressif/arduino-esp32/blob/0f772270fbb32099cad72053755d6b4c7e8e1f82/cores/esp32/esp32-hal-i2c.c#L45

stale[bot] commented 4 years ago

[STALE_SET] This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs. Thank you for your contributions.

stale[bot] commented 4 years ago

[STALE_DEL] This stale issue has been automatically closed. Thank you for your contributions.