RCM — ESP32 Room Controller
WT32-ETH01 firmware
Loading...
Searching...
No Matches
pcf8574.c File Reference

I2C-обмен с PCF8574T (8-битный расширитель портов). More...

#include "pcf8574.h"
#include "driver/i2c_master.h"
#include "esp_check.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

Go to the source code of this file.

Macros

#define I2C_PORT   I2C_NUM_0
 Порт I2C.
 
#define I2C_SCL_GPIO   14
 GPIO шины I2C.
 
#define I2C_SDA_GPIO   15
 
#define I2C_FREQ_HZ   100000
 Частота I2C, Гц
 
#define I2C_TIMEOUT_MS   1000
 Таймаут транзакции, мс
 
#define PCF8574_I2C_ADDR   0x20
 7-битный адрес PCF8574 на шине (A0=A1=A2=0 → 0x20)
 
#define LED_CHASE_PIN_COUNT   3
 Светодиоды на P0..P2 (остальные пины не трогаем)
 
#define LED_CHASE_PIN_MASK   0x07
 
#define LED_CHASE_PORT_IDLE   0xF8
 
#define LED_CHASE_STEP_MS   300
 

Functions

static esp_err_t pcf8574_write_raw (uint8_t value)
 
static esp_err_t pcf8574_read_raw (uint8_t *value)
 
esp_err_t pcf8574_init (void)
 Инициализирует шину I2C и устройство PCF8574T.
 
esp_err_t pcf8574_write (uint8_t value)
 Записывает байт в порт расширителя (состояние P0..P7).
 
esp_err_t pcf8574_read (uint8_t *value)
 Читает текущее состояние порта P0..P7.
 
esp_err_t pcf8574_set_pin (uint8_t pin, bool level)
 Устанавливает уровень одного пина (0..7).
 
esp_err_t pcf8574_get_pin (uint8_t pin, bool *level)
 Читает уровень одного пина (0..7).
 
static void led_chase_task (void *arg)
 Циклически включает по одному светодиоду на P0, P1, P2.
 
esp_err_t pcf8574_led_chase_start (void)
 Запускает задачу «бегущий огонь» на светодиодах P0..P2.
 

Variables

static const char * TAG = "pcf8574"
 
static i2c_master_bus_handle_t s_i2c_bus
 
static i2c_master_dev_handle_t s_pcf8574_dev
 
static uint8_t s_port_shadow
 
static bool s_initialized
 

Detailed Description

I2C-обмен с PCF8574T (8-битный расширитель портов).

SCL — GPIO2, SDA — GPIO4. Адрес по умолчанию 0x20 (A0=A1=A2=GND).

Definition in file pcf8574.c.

Macro Definition Documentation

◆ I2C_FREQ_HZ

#define I2C_FREQ_HZ   100000

Частота I2C, Гц

Definition at line 27 of file pcf8574.c.

Referenced by pcf8574_init().

◆ I2C_PORT

#define I2C_PORT   I2C_NUM_0

Порт I2C.

Definition at line 20 of file pcf8574.c.

Referenced by pcf8574_init().

◆ I2C_SCL_GPIO

#define I2C_SCL_GPIO   14

GPIO шины I2C.

Definition at line 23 of file pcf8574.c.

Referenced by pcf8574_init().

◆ I2C_SDA_GPIO

#define I2C_SDA_GPIO   15

Definition at line 24 of file pcf8574.c.

Referenced by pcf8574_init().

◆ I2C_TIMEOUT_MS

#define I2C_TIMEOUT_MS   1000

Таймаут транзакции, мс

Definition at line 30 of file pcf8574.c.

Referenced by pcf8574_read_raw(), and pcf8574_write_raw().

◆ LED_CHASE_PIN_COUNT

#define LED_CHASE_PIN_COUNT   3

Светодиоды на P0..P2 (остальные пины не трогаем)

Definition at line 36 of file pcf8574.c.

Referenced by led_chase_task(), and pcf8574_led_chase_start().

◆ LED_CHASE_PIN_MASK

#define LED_CHASE_PIN_MASK   0x07

Definition at line 37 of file pcf8574.c.

◆ LED_CHASE_PORT_IDLE

#define LED_CHASE_PORT_IDLE   0xF8

Definition at line 38 of file pcf8574.c.

Referenced by led_chase_task().

◆ LED_CHASE_STEP_MS

#define LED_CHASE_STEP_MS   300

Definition at line 39 of file pcf8574.c.

Referenced by led_chase_task().

◆ PCF8574_I2C_ADDR

#define PCF8574_I2C_ADDR   0x20

7-битный адрес PCF8574 на шине (A0=A1=A2=0 → 0x20)

Definition at line 33 of file pcf8574.c.

Referenced by pcf8574_init().

Function Documentation

◆ led_chase_task()

static void led_chase_task ( void * arg)
static

Циклически включает по одному светодиоду на P0, P1, P2.

Definition at line 143 of file pcf8574.c.

References LED_CHASE_PIN_COUNT, LED_CHASE_PORT_IDLE, LED_CHASE_STEP_MS, pcf8574_write(), and TAG.

Referenced by pcf8574_led_chase_start().

◆ pcf8574_get_pin()

esp_err_t pcf8574_get_pin ( uint8_t pin,
bool * level )

Читает уровень одного пина (0..7).

Definition at line 128 of file pcf8574.c.

References pcf8574_read(), and TAG.

◆ pcf8574_init()

esp_err_t pcf8574_init ( void )

Инициализирует шину I2C и устройство PCF8574T.

Returns
ESP_OK при успешной инициализации и ответе на шине.

Definition at line 56 of file pcf8574.c.

References I2C_FREQ_HZ, I2C_PORT, I2C_SCL_GPIO, I2C_SDA_GPIO, PCF8574_I2C_ADDR, pcf8574_read_raw(), pcf8574_write_raw(), s_i2c_bus, s_initialized, s_pcf8574_dev, s_port_shadow, and TAG.

◆ pcf8574_led_chase_start()

esp_err_t pcf8574_led_chase_start ( void )

Запускает задачу «бегущий огонь» на светодиодах P0..P2.

Светодиоды подключены к GND: бит 1 — включён, бит 0 — выключен.

Returns
ESP_OK при успешном создании задачи.

Definition at line 158 of file pcf8574.c.

References LED_CHASE_PIN_COUNT, led_chase_task(), s_initialized, and TAG.

◆ pcf8574_read()

esp_err_t pcf8574_read ( uint8_t * value)

Читает текущее состояние порта P0..P7.

Definition at line 104 of file pcf8574.c.

References pcf8574_read_raw(), s_initialized, and TAG.

Referenced by pcf8574_get_pin().

◆ pcf8574_read_raw()

static esp_err_t pcf8574_read_raw ( uint8_t * value)
static

Definition at line 51 of file pcf8574.c.

References I2C_TIMEOUT_MS, and s_pcf8574_dev.

Referenced by pcf8574_init(), and pcf8574_read().

◆ pcf8574_set_pin()

esp_err_t pcf8574_set_pin ( uint8_t pin,
bool level )

Устанавливает уровень одного пина (0..7).

Для PCF8574: 0 — выход в низком уровне, 1 — вход (линия через pull-up).

Definition at line 112 of file pcf8574.c.

References pcf8574_write(), s_port_shadow, and TAG.

◆ pcf8574_write()

esp_err_t pcf8574_write ( uint8_t value)

Записывает байт в порт расширителя (состояние P0..P7).

Definition at line 93 of file pcf8574.c.

References pcf8574_write_raw(), s_initialized, s_port_shadow, and TAG.

Referenced by led_chase_task(), and pcf8574_set_pin().

◆ pcf8574_write_raw()

static esp_err_t pcf8574_write_raw ( uint8_t value)
static

Definition at line 46 of file pcf8574.c.

References I2C_TIMEOUT_MS, and s_pcf8574_dev.

Referenced by pcf8574_init(), and pcf8574_write().

Variable Documentation

◆ s_i2c_bus

i2c_master_bus_handle_t s_i2c_bus
static

Definition at line 41 of file pcf8574.c.

Referenced by pcf8574_init().

◆ s_initialized

bool s_initialized
static

Definition at line 44 of file pcf8574.c.

Referenced by pcf8574_init(), pcf8574_led_chase_start(), pcf8574_read(), and pcf8574_write().

◆ s_pcf8574_dev

i2c_master_dev_handle_t s_pcf8574_dev
static

Definition at line 42 of file pcf8574.c.

Referenced by pcf8574_init(), pcf8574_read_raw(), and pcf8574_write_raw().

◆ s_port_shadow

uint8_t s_port_shadow
static

Definition at line 43 of file pcf8574.c.

Referenced by pcf8574_init(), pcf8574_set_pin(), and pcf8574_write().

◆ TAG

const char* TAG = "pcf8574"
static