Skip to content

Commit

Permalink
修改IO口定义方式,采用宏定义方式提高可移植性
Browse files Browse the repository at this point in the history
  • Loading branch information
elitah committed Jan 26, 2021
1 parent 800b701 commit 8c9c698
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 29 deletions.
114 changes: 114 additions & 0 deletions smart_config/include/user_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,119 @@
#ifndef __USER_CONFIG_H__
#define __USER_CONFIG_H__

#include "gpio.h"

#define SERVICE_ADDRESS "example.com"
#define SERVICE_PORT 8888

#define IO_MODE_INPUT 0
#define IO_MODE_OUTPUT 1

#define IO_PULLUP_EN 1
#define IO_PULLUP_DIS 0

inline void io_init_at(int id, int mode, int up, int def)
{
int mux = 0, func = 0;

switch (id) {
case 0:
mux = PERIPHS_IO_MUX_GPIO0_U;
func = FUNC_GPIO0;
break;
case 1:
mux = PERIPHS_IO_MUX_U0TXD_U;
func = FUNC_GPIO1;
break;
case 2:
mux = PERIPHS_IO_MUX_GPIO2_U;
func = FUNC_GPIO2;
break;
case 3:
mux = PERIPHS_IO_MUX_U0RXD_U;
func = FUNC_GPIO3;
break;
case 4:
mux = PERIPHS_IO_MUX_GPIO4_U;
func = FUNC_GPIO4;
break;
case 5:
mux = PERIPHS_IO_MUX_GPIO5_U;
func = FUNC_GPIO5;
break;
case 6:
mux = PERIPHS_IO_MUX_SD_CLK_U;
func = FUNC_GPIO6;
break;
case 7:
mux = PERIPHS_IO_MUX_SD_DATA0_U;
func = FUNC_GPIO7;
break;
case 8:
mux = PERIPHS_IO_MUX_SD_DATA1_U;
func = FUNC_GPIO8;
break;
case 9:
mux = PERIPHS_IO_MUX_SD_DATA2_U;
func = FUNC_GPIO9;
break;
case 10:
mux = PERIPHS_IO_MUX_SD_DATA3_U;
func = FUNC_GPIO10;
break;
case 11:
mux = PERIPHS_IO_MUX_SD_CMD_U;
func = FUNC_GPIO11;
break;
case 12:
mux = PERIPHS_IO_MUX_MTDI_U;
func = FUNC_GPIO12;
break;
case 13:
mux = PERIPHS_IO_MUX_MTCK_U;
func = FUNC_GPIO13;
break;
case 14:
mux = PERIPHS_IO_MUX_MTMS_U;
func = FUNC_GPIO14;
break;
case 15:
mux = PERIPHS_IO_MUX_MTDO_U;
func = FUNC_GPIO15;
break;
default:
return;
}

// 输入模式
if (0 == mode)
{
if (0 == up)
{
PIN_PULLUP_DIS(mux);
}
else
{
PIN_PULLUP_EN(mux);
}

GPIO_DIS_OUTPUT(GPIO_ID_PIN(id));
}
else
{
PIN_PULLUP_DIS(mux);

GPIO_OUTPUT_SET(GPIO_ID_PIN(id), 0 == def ? 0 : 1);
}

PIN_FUNC_SELECT(mux, func);
}

inline void io_init_all()
{
};

#define IO_INIT_FUNC() io_init_all()

#endif

72 changes: 43 additions & 29 deletions smart_config/user/user_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

#include "esp_common.h"

#include "user_config.h"

#include "gpio.h"

#include "freertos/FreeRTOS.h"
Expand Down Expand Up @@ -340,7 +342,9 @@ wol_client_task(void *pvParameters)

unsigned int retry_count = 0;

unsigned int gpio5_count = 0;
#if defined(IO_WARN_TO_PINID)
unsigned int io_warn_count = 0;
#endif

char buffer[256] = {0};

Expand All @@ -358,7 +362,7 @@ wol_client_task(void *pvParameters)
struct ip_info info;
struct station_config sta_con;

char ip[16] = {0};
char ip[32] = {0};

cJSON *pRoot = NULL;

Expand All @@ -374,9 +378,9 @@ wol_client_task(void *pvParameters)
{
if(0 == setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)))
{
if(true != get_server_ip("example.com", ip, sizeof(ip)))
if(true != get_server_ip(SERVICE_ADDRESS, ip, sizeof(ip)))
{
os_printf("%s: unable get server ip address by: example.com\n", __func__);
os_printf("%s: unable get server ip address by: %s\n", __func__, SERVICE_ADDRESS);

vTaskDelay(1000 / portTICK_RATE_MS);

Expand All @@ -387,7 +391,7 @@ wol_client_task(void *pvParameters)

sock_addr.sin_family = AF_INET;
sock_addr.sin_addr.s_addr = inet_addr(ip);
sock_addr.sin_port = htons(8888);
sock_addr.sin_port = htons(SERVICE_PORT);

timestamp_send_bind = 0;
timestamp_send_beat = 0;
Expand All @@ -400,9 +404,10 @@ wol_client_task(void *pvParameters)

if(0 == connect(sockfd, (struct sockaddr *)&sock_addr, sizeof(sock_addr)))
{
// 设置GPIO5为输入
GPIO_DIS_OUTPUT(GPIO_ID_PIN(5));

#if defined(IO_WARN_TO_PINID)
// 设置报警引脚为输入
GPIO_DIS_OUTPUT(IO_WARN_TO_PINID);
#endif
os_printf("connect to %s:%u ok...\n", inet_ntoa(sock_addr.sin_addr), ntohs(sock_addr.sin_port));

retry_count = 0;
Expand Down Expand Up @@ -575,14 +580,14 @@ wol_client_task(void *pvParameters)

break;
}

if(0 == (GPIO_INPUT_GET(GPIO_ID_PIN(5))))
#if defined(IO_WARN_TO_PINID)
if(0 == (GPIO_INPUT_GET(IO_WARN_TO_PINID)))
{
gpio5_count++;
io_warn_count++;

os_printf("gpio5: warn %u\n", gpio5_count);
os_printf("io_warn: warn %u\n", io_warn_count);

if(3 <= gpio5_count)
if(3 <= io_warn_count)
{
ret = snprintf(buffer, sizeof(buffer), "{\"cmd\":\"warn\"}");

Expand All @@ -592,19 +597,20 @@ wol_client_task(void *pvParameters)

if(0 < ret)
{
os_printf("gpio5: warn send ok\n");
os_printf("io_warn: warn send ok\n");
}
else
{
os_printf("gpio5: warn send failed\n");
os_printf("io_warn: warn send failed\n");
}
}

gpio5_count = 0;
io_warn_count = 0;
}
} else {
gpio5_count = 0;
io_warn_count = 0;
}
#endif
}
}

Expand Down Expand Up @@ -696,12 +702,13 @@ send_key_task(void *pvParameters)
void ICACHE_FLASH_ATTR
led_task(void *pvParameters)
{
#if defined(IO_LED_TO_PINID)
uint32 gpio_mask = 0x0;

uint32 count_led = 6;

// 常亮
GPIO_OUTPUT_SET(GPIO_ID_PIN(2), 0);
GPIO_OUTPUT_SET(IO_LED_TO_PINID, 0);

while(1)
{
Expand All @@ -714,17 +721,17 @@ led_task(void *pvParameters)
if(0 == count_led % 8)
{
// 当前是否处于亮状态
if(0 == GPIO_INPUT_GET(GPIO_ID_PIN(2)))
if(0 == GPIO_INPUT_GET(IO_LED_TO_PINID))
{
// 是,灭灯
GPIO_OUTPUT_SET(GPIO_ID_PIN(2), 1);
GPIO_OUTPUT_SET(IO_LED_TO_PINID, 1);

count_led = 0;
}
else
{
// 否,点亮
GPIO_OUTPUT_SET(GPIO_ID_PIN(2), 0);
GPIO_OUTPUT_SET(IO_LED_TO_PINID, 0);

count_led = 6;
}
Expand All @@ -735,24 +742,24 @@ led_task(void *pvParameters)
else
{
// 常亮
GPIO_OUTPUT_SET(GPIO_ID_PIN(2), 0);
GPIO_OUTPUT_SET(IO_LED_TO_PINID, 0);
}
}
else
{
if(0 == GPIO_INPUT_GET(GPIO_ID_PIN(2)))
if(0 == GPIO_INPUT_GET(IO_LED_TO_PINID))
{
GPIO_OUTPUT_SET(GPIO_ID_PIN(2), 1);
GPIO_OUTPUT_SET(IO_LED_TO_PINID, 1);
}
else
{
GPIO_OUTPUT_SET(GPIO_ID_PIN(2), 0);
GPIO_OUTPUT_SET(IO_LED_TO_PINID, 0);
}

vTaskDelay(50 / portTICK_RATE_MS);
}
}

#endif
vTaskDelete(NULL);
}

Expand Down Expand Up @@ -812,13 +819,14 @@ smartconfig_task(void *pvParameters)
void ICACHE_FLASH_ATTR
button_task(void *pvParameters)
{
#if defined(IO_RESET_TO_PINID)
uint32 count_btn = 0;

GPIO_DIS_OUTPUT(GPIO_ID_PIN(0));
GPIO_DIS_OUTPUT(IO_RESET_TO_PINID);

while(1)
{
if(0 == GPIO_INPUT_GET(GPIO_ID_PIN(0)))
if(0 == GPIO_INPUT_GET(IO_RESET_TO_PINID))
{
if(0 == count_btn)
{
Expand Down Expand Up @@ -851,7 +859,7 @@ button_task(void *pvParameters)
}

os_printf("Reset Config, Reboot...\n");

#endif
vTaskDelete(NULL);
}

Expand Down Expand Up @@ -992,6 +1000,10 @@ user_init(void)
{
unsigned int flash_size = user_get_flash_size();

#if defined(IO_INIT_FUNC)
IO_INIT_FUNC();
#endif

os_printf("SDK version:%s\n", system_get_sdk_version());

if(0x100000 > flash_size)
Expand Down Expand Up @@ -1023,5 +1035,7 @@ user_init(void)
}
}

#if defined(IO_LED_TO_PINID)
xTaskCreate(led_task, "led_task", 512, NULL, 2, NULL);
#endif
}

0 comments on commit 8c9c698

Please sign in to comment.