Skip to content

Commit c26ee62

Browse files
committed
main: code refactoring
1 parent 5860f8d commit c26ee62

File tree

7 files changed

+51
-49
lines changed

7 files changed

+51
-49
lines changed

main/inc/chip/i2c.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
#ifndef INC_CHIP_I2C_H_
99
#define INC_CHIP_I2C_H_
1010

11-
extern void i2c0_init(void);
11+
#include "driver/i2c.h"
12+
13+
#define I2C_HOST_TAG "i2c-0"
14+
#define I2C_HOST_NUM I2C_NUM_0
15+
16+
extern void i2c_host_init(void);
1217

1318
#endif /* INC_CHIP_I2C_H_ */

main/inc/chip/spi.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@
1010

1111
#include "driver/spi_master.h"
1212

13-
extern spi_device_handle_t hspi;
13+
#define SPI_HOST_TAG "spi-2"
14+
#define SPI_HOST_NUM SPI2_HOST
1415

15-
extern void hspi_init(void);
16+
extern spi_device_handle_t spi_host;
17+
18+
extern void spi_host_init(void);
1619

1720
#endif /* INC_CHIP_SPI_H_ */

main/src/app_main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ static void chip_init(void)
3737
bt_init();
3838

3939
#ifdef CONFIG_ENABLE_GUI
40-
hspi_init();
40+
spi_host_init();
4141
#endif
4242

4343
#ifdef CONFIG_ENABLE_POWER_MONITOR
44-
i2c0_init();
44+
i2c_host_init();
4545
#endif
4646
}
4747

main/src/board/ina219.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
#include "esp_log.h"
1111

12-
#include "driver/i2c.h"
12+
#include "chip/i2c.h"
1313

1414
#define TAG "ina219"
1515

@@ -91,7 +91,7 @@ static void ina219_reg_write(uint8_t reg, const uint8_t *buff)
9191

9292
i2c_master_stop(cmd);
9393

94-
i2c_master_cmd_begin(I2C_NUM_0, cmd, portMAX_DELAY);
94+
i2c_master_cmd_begin(I2C_HOST_NUM, cmd, portMAX_DELAY);
9595

9696
i2c_cmd_link_delete(cmd);
9797
}
@@ -114,7 +114,7 @@ static void ina219_reg_read(uint8_t reg, uint8_t *buff)
114114

115115
i2c_master_stop(cmd);
116116

117-
i2c_master_cmd_begin(I2C_NUM_0, cmd, portMAX_DELAY);
117+
i2c_master_cmd_begin(I2C_HOST_NUM, cmd, portMAX_DELAY);
118118

119119
i2c_cmd_link_delete(cmd);
120120
}

main/src/board/st7789.c

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
#include "driver/gpio.h"
1313
#include "driver/ledc.h"
14-
#include "driver/spi_master.h"
1514

1615
#include "drivers/gdisp/ST7789/ST7789.h"
1716

@@ -22,11 +21,11 @@
2221

2322
#define TAG "st7789"
2423

25-
static spi_transaction_t hspi_trans[2];
24+
static spi_transaction_t spi_trans[2];
2625

2726
void st7789_init_board(void)
2827
{
29-
memset(hspi_trans, 0x00, sizeof(hspi_trans));
28+
memset(spi_trans, 0x00, sizeof(spi_trans));
3029

3130
gpio_config_t io_conf = {
3231
.pin_bit_mask = BIT64(CONFIG_LCD_DC_PIN) | BIT64(CONFIG_LCD_RST_PIN),
@@ -78,44 +77,44 @@ void st7789_setpin_reset(uint8_t val)
7877

7978
void st7789_write_cmd(uint8_t cmd)
8079
{
81-
hspi_trans[0].length = 8;
82-
hspi_trans[0].tx_buffer = &cmd;
83-
hspi_trans[0].user = (void *)0;
80+
spi_trans[0].length = 8;
81+
spi_trans[0].tx_buffer = &cmd;
82+
spi_trans[0].user = (void *)0;
8483

85-
spi_device_transmit(hspi, &hspi_trans[0]);
84+
spi_device_transmit(spi_host, &spi_trans[0]);
8685
}
8786

8887
void st7789_write_data(uint8_t data)
8988
{
90-
hspi_trans[0].length = 8;
91-
hspi_trans[0].tx_buffer = &data;
92-
hspi_trans[0].user = (void *)1;
89+
spi_trans[0].length = 8;
90+
spi_trans[0].tx_buffer = &data;
91+
spi_trans[0].user = (void *)1;
9392

94-
spi_device_transmit(hspi, &hspi_trans[0]);
93+
spi_device_transmit(spi_host, &spi_trans[0]);
9594
}
9695

9796
void st7789_write_buff(uint8_t *buff, uint32_t n)
9897
{
99-
hspi_trans[0].length = n * 8;
100-
hspi_trans[0].tx_buffer = buff;
101-
hspi_trans[0].user = (void *)1;
98+
spi_trans[0].length = n * 8;
99+
spi_trans[0].tx_buffer = buff;
100+
spi_trans[0].user = (void *)1;
102101

103-
spi_device_transmit(hspi, &hspi_trans[0]);
102+
spi_device_transmit(spi_host, &spi_trans[0]);
104103
}
105104

106105
void st7789_refresh_gram(uint8_t *gram)
107106
{
108-
hspi_trans[0].length = 8,
109-
hspi_trans[0].tx_data[0] = ST7789_RAMWR;
110-
hspi_trans[0].user = (void *)0;
111-
hspi_trans[0].flags = SPI_TRANS_USE_TXDATA;
107+
spi_trans[0].length = 8,
108+
spi_trans[0].tx_data[0] = ST7789_RAMWR;
109+
spi_trans[0].user = (void *)0;
110+
spi_trans[0].flags = SPI_TRANS_USE_TXDATA;
112111

113-
spi_device_queue_trans(hspi, &hspi_trans[0], portMAX_DELAY);
112+
spi_device_queue_trans(spi_host, &spi_trans[0], portMAX_DELAY);
114113

115-
hspi_trans[1].length = ST7789_SCREEN_WIDTH * ST7789_SCREEN_HEIGHT * 2 * 8;
116-
hspi_trans[1].tx_buffer = gram;
117-
hspi_trans[1].user = (void *)1;
114+
spi_trans[1].length = ST7789_SCREEN_WIDTH * ST7789_SCREEN_HEIGHT * 2 * 8;
115+
spi_trans[1].tx_buffer = gram;
116+
spi_trans[1].user = (void *)1;
118117

119-
spi_device_queue_trans(hspi, &hspi_trans[1], portMAX_DELAY);
118+
spi_device_queue_trans(spi_host, &spi_trans[1], portMAX_DELAY);
120119
}
121120
#endif

main/src/chip/i2c.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,10 @@
77

88
#include "esp_log.h"
99

10-
#include "driver/i2c.h"
11-
12-
#define I2C0_TAG "i2c-0"
10+
#include "chip/i2c.h"
1311

1412
#ifdef CONFIG_ENABLE_POWER_MONITOR
15-
void i2c0_init(void)
13+
void i2c_host_init(void)
1614
{
1715
i2c_config_t i2c_conf = {
1816
.mode = I2C_MODE_MASTER,
@@ -22,10 +20,10 @@ void i2c0_init(void)
2220
.scl_pullup_en = GPIO_PULLUP_ENABLE,
2321
.master.clk_speed = 400000
2422
};
25-
ESP_ERROR_CHECK(i2c_param_config(I2C_NUM_0, &i2c_conf));
26-
ESP_ERROR_CHECK(i2c_driver_install(I2C_NUM_0, i2c_conf.mode, 0, 0, 0));
27-
ESP_ERROR_CHECK(i2c_set_timeout(I2C_NUM_0, 80 * (I2C_APB_CLK_FREQ / i2c_conf.master.clk_speed)));
23+
ESP_ERROR_CHECK(i2c_param_config(I2C_HOST_NUM, &i2c_conf));
24+
ESP_ERROR_CHECK(i2c_driver_install(I2C_HOST_NUM, i2c_conf.mode, 0, 0, 0));
25+
ESP_ERROR_CHECK(i2c_set_timeout(I2C_HOST_NUM, 80 * (I2C_APB_CLK_FREQ / i2c_conf.master.clk_speed)));
2826

29-
ESP_LOGI(I2C0_TAG, "initialized, sda: %d, scl: %d", i2c_conf.sda_io_num, i2c_conf.scl_io_num);
27+
ESP_LOGI(I2C_HOST_TAG, "initialized, sda: %d, scl: %d", i2c_conf.sda_io_num, i2c_conf.scl_io_num);
3028
}
3129
#endif

main/src/chip/spi.c

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,13 @@
77

88
#include "esp_log.h"
99

10-
#include "driver/spi_master.h"
11-
10+
#include "chip/spi.h"
1211
#include "board/st7789.h"
1312

14-
#define HSPI_TAG "hspi"
15-
1613
#ifdef CONFIG_ENABLE_GUI
17-
spi_device_handle_t hspi;
14+
spi_device_handle_t spi_host;
1815

19-
void hspi_init(void)
16+
void spi_host_init(void)
2017
{
2118
spi_bus_config_t bus_conf = {
2219
.miso_io_num = -1,
@@ -26,7 +23,7 @@ void hspi_init(void)
2623
.quadhd_io_num = -1,
2724
.max_transfer_sz = ST7789_SCREEN_WIDTH * ST7789_SCREEN_HEIGHT * 2
2825
};
29-
ESP_ERROR_CHECK(spi_bus_initialize(HSPI_HOST, &bus_conf, 1));
26+
ESP_ERROR_CHECK(spi_bus_initialize(SPI_HOST_NUM, &bus_conf, 1));
3027

3128
spi_device_interface_config_t dev_conf = {
3229
.mode = 0, // SPI mode 0
@@ -36,9 +33,9 @@ void hspi_init(void)
3633
.queue_size = 2, // we want to be able to queue 2 transactions at a time
3734
.flags = SPI_DEVICE_3WIRE | SPI_DEVICE_HALFDUPLEX
3835
};
39-
ESP_ERROR_CHECK(spi_bus_add_device(HSPI_HOST, &dev_conf, &hspi));
36+
ESP_ERROR_CHECK(spi_bus_add_device(SPI_HOST_NUM, &dev_conf, &spi_host));
4037

41-
ESP_LOGI(HSPI_TAG, "initialized, sclk: %d, mosi: %d, miso: %d, cs: %d",
38+
ESP_LOGI(SPI_HOST_TAG, "initialized, sclk: %d, mosi: %d, miso: %d, cs: %d",
4239
bus_conf.sclk_io_num,
4340
bus_conf.mosi_io_num,
4441
bus_conf.miso_io_num,

0 commit comments

Comments
 (0)