Skip to content

Commit

Permalink
Improv WiFi works.
Browse files Browse the repository at this point in the history
  • Loading branch information
BrucePerens committed Jun 21, 2022
1 parent 00cc0ef commit 8a9c8cd
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 24 deletions.
14 changes: 14 additions & 0 deletions components/generic_main/command.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,17 @@ gm_command_add_registered_to_console(void)
}
gm_array_destroy(array);
}

void
gm_command_interpreter_start(void)
{
esp_console_dev_uart_config_t uart_config = ESP_CONSOLE_DEV_UART_CONFIG_DEFAULT();

// Configure the console command system.
esp_console_repl_config_t repl_config = ESP_CONSOLE_REPL_CONFIG_DEFAULT();
repl_config.task_stack_size = 20 * 1024;
repl_config.prompt = ">";
ESP_ERROR_CHECK(esp_console_new_repl_uart(&uart_config, &repl_config, &GM.repl));
gm_command_add_registered_to_console();
ESP_ERROR_CHECK(esp_console_start_repl(GM.repl));
}
17 changes: 4 additions & 13 deletions components/generic_main/generic_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ static const char TASK_NAME[] = "main";
void app_main(void)
{
GM.log_file_pointer = stderr;
gm_user_initialize_early();
initialize();
}

Expand All @@ -40,6 +39,10 @@ static void initialize(void)
// This can't be used for non-tasks.
pthread_mutex_init(&GM.console_print_mutex, 0);

gm_improv_wifi(0);

gm_user_initialize_early();

// Initialize the TCP/IP stack. gm_select_task uses sockets.
esp_netif_init();

Expand Down Expand Up @@ -80,16 +83,4 @@ static void initialize(void)

// Start WiFi, if it's already configured.
gm_wifi_start();

esp_console_dev_uart_config_t uart_config = ESP_CONSOLE_DEV_UART_CONFIG_DEFAULT();

// Configure the console command system.
esp_console_repl_config_t repl_config = ESP_CONSOLE_REPL_CONFIG_DEFAULT();
repl_config.task_stack_size = 20 * 1024;
repl_config.prompt = ">";
ESP_ERROR_CHECK(esp_console_new_repl_uart(&uart_config, &repl_config, &GM.repl));
gm_command_add_registered_to_console();
gm_user_initialize_late();
gm_improv_wifi(0);
ESP_ERROR_CHECK(esp_console_start_repl(GM.repl));
}
72 changes: 63 additions & 9 deletions components/generic_main/improv_wifi.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,20 @@
#include <stdint.h>
#include <errno.h>
#include <stdbool.h>
#include "sdkconfig.h"
#include <esp_event.h>
#include <esp_wifi.h>
#include <esp_vfs.h>
#include <esp_vfs_dev.h>
#include <driver/uart.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "generic_main.h"

const char name[] = "Improv WiFi ";
const uint8_t magic[6] = "IMPROV"; // Explicit size eliminates trailing '\0'.
const uint8_t ImprovVersion = 1;
// static const char name[] = "Improv WiFi ";
static const uint8_t magic[6] = "IMPROV"; // Explicit size eliminates trailing '\0'.
static const uint8_t ImprovVersion = 1;
static const esp_console_dev_uart_config_t dev_uart_config = ESP_CONSOLE_DEV_UART_CONFIG_DEFAULT();

typedef enum improv_type
{
Expand Down Expand Up @@ -81,6 +88,7 @@ typedef enum improv_error_state {
UnknownError = 0xff
} improv_error_state_t;

static TaskHandle_t improv_task_id = NULL;
static improv_state_t improv_state = Ready;
static int enter_count = 0;
struct sockaddr_in address = {};
Expand Down Expand Up @@ -417,23 +425,69 @@ improv_process(int fd, const uint8_t * data, improv_type_t type, uint8_t length)
return NoError;
}

void
gm_improv_wifi(int fd)
static void
initialize_uart(void)
{
const uart_config_t uart_config = {
.baud_rate = dev_uart_config.baud_rate,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
.source_clk = UART_SCLK_REF_TICK,
};

#if CONFIG_UART_ISR_IN_IRAM
const int intr_alloc_flags = ESP_INTR_FLAG_IRAM;
#else
const int intr_alloc_flags = 0;
#endif

// Apparently this works to set the UART driver to be interrupt-driven rather than
// to work in an event queue.
ESP_ERROR_CHECK(uart_driver_install(dev_uart_config.channel, 512, 512, 0, NULL, intr_alloc_flags));

ESP_ERROR_CHECK(uart_param_config(dev_uart_config.channel, &uart_config));
ESP_ERROR_CHECK(uart_set_pin(
dev_uart_config.channel,
dev_uart_config.tx_gpio_num,
dev_uart_config.rx_gpio_num,
-1,
-1));

esp_vfs_dev_uart_use_driver(dev_uart_config.channel);
}

static void
improv_task(void * p)
{
// Start the WiFi scan as soon as possible, so that the data is ready when the user
// wishes to configure WiFi.
uint8_t data[256];
improv_error_state_t error;
improv_type_t type;
uint8_t length;

gm_printf("\n\n*** To start the command line, press ENTER three times. ***\n\n");
int fd = 0;



gm_printf("*** To start the command line, press ENTER three times. ***\n");
for ( ; ; ) {
error = improv_read(fd, data, &type, &length);
if ( error == EndImprov )
return;
break;
else if ( error == NoError )
improv_process(fd, data, type, length);
}
uart_driver_delete(CONFIG_ESP_CONSOLE_UART_NUM);
gm_command_interpreter_start();
vTaskDelete(improv_task_id);
}

void
gm_improv_wifi(int fd)
{
// Don't initialize the uart in the Improv task, it flushes the outgoing queue.
// Do it in main() before tasks that are likely to print start.
initialize_uart();
xTaskCreate(improv_task, "generic main: Improv WiFi protocol", 10240, NULL, 3, &improv_task_id);
}
1 change: 1 addition & 0 deletions components/generic_main/include/generic_main.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ extern size_t gm_array_size(GM_Array * array);

extern size_t gm_choose_one(size_t number_of_entries);
extern void gm_command_add_registered_to_console(void);
extern void gm_command_interpreter_start(void);
extern void gm_command_register(const esp_console_cmd_t * command);

extern int gm_ddns(void);
Expand Down
4 changes: 2 additions & 2 deletions scripts/make_firmware_archive.sh
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ cat > firmware/index.html <<-END
<LI>If the program says
<i>Serial port is not ready. Close any other application using it and try again:</i>
Unplug and reconnect its USB interface.
Sometimes, it's necessary to remove and restore
If that doesn't work, remove and restore
power to the USB hub the card is connected to.</LI>
<LI>If the browser opens an alert box that says
<i>Failed to open serial port:</i>
The first time you run this, you may have to grant yourself
permission to open serial ports on your computer. You'll only
need to do this once per user and computer.<BR>
need to do this once per user and computer:<BR>
<P>On <B>Linux</B> use the command</P>
<BLOCKQUOTE>
<SPAN style="font-family: monospace">sudo adduser</SPAN>
Expand Down

0 comments on commit 8a9c8cd

Please sign in to comment.