Skip to content

Commit

Permalink
I2C funcional
Browse files Browse the repository at this point in the history
  • Loading branch information
louisesampaio committed Mar 2, 2024
1 parent 0827559 commit 2216872
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
17 changes: 13 additions & 4 deletions lib/I2C/I2C.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "I2C.h"
#include "esp_log.h"
#include "stdio.h"
#include "string.h"

int i2c_init(int SDA_PIN, int SCL_PIN, int FREQ, i2c_mode MODE)
{
Expand Down Expand Up @@ -34,6 +36,9 @@ int i2c_init(int SDA_PIN, int SCL_PIN, int FREQ, i2c_mode MODE)
.master.clk_speed = FREQ,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.slave.addr_10bit_en = 0,
.slave.slave_addr = 0x55,
.clk_flags = 0
};
esp_err_t get_ret;

Expand All @@ -50,7 +55,7 @@ int i2c_send(const uint8_t *data, i2c_mode MODE)

i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, 0x0A<< I2C_MASTER_WRITE , true);
i2c_master_write_byte(cmd, (0x55<< 1)| I2C_MASTER_WRITE , true);
i2c_master_write(cmd, data, 5, true);
i2c_master_stop(cmd);
i2c_master_cmd_begin(I2C_PORT, cmd, 1000/ portTICK_PERIOD_MS);
Expand All @@ -60,8 +65,12 @@ int i2c_send(const uint8_t *data, i2c_mode MODE)

int i2c_read(uint8_t *buffer)
{
i2c_slave_read_buffer(I2C_PORT, buffer, 6, 20/ portTICK_PERIOD_MS);
i2c_reset_rx_fifo(I2C_PORT);
ESP_LOGI("Dado recebido", "%s\n", buffer);
if(i2c_slave_read_buffer(I2C_PORT, buffer, 6, 20/ portTICK_PERIOD_MS) > 0)
{
ESP_LOGI("Dado recebido", "%s\n", buffer);
i2c_reset_rx_fifo(I2C_PORT);
memset(buffer, 0, 6);
}

return 0;
}
11 changes: 5 additions & 6 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ const uint8_t command[] = "hello";
void app_main()
{
i2c_init(SDA_PIN_NUM, SCL_PIN_NUM, I2C_FREQ, I2C_MASTER_MODE);

i2c_send(command, I2C_MASTER_MODE);
vTaskDelay(1000/portTICK_PERIOD_MS);

i2c_send(command, I2C_MASTER_MODE);
vTaskDelay(1000/portTICK_PERIOD_MS);
while(1)
{
i2c_send(command, I2C_MASTER_MODE);
vTaskDelay(5000/portTICK_PERIOD_MS);
}
}

#endif
Expand Down

0 comments on commit 2216872

Please sign in to comment.