testing button interrupts, hw-change required
This commit is contained in:
67
main/main.c
67
main/main.c
@ -30,6 +30,10 @@ static const char *TAG = "eth_tester";
|
|||||||
#define IP_FSTRING "%3d.%3d.%3d.%3d"
|
#define IP_FSTRING "%3d.%3d.%3d.%3d"
|
||||||
|
|
||||||
#define SIGNAL_SHUTDOWN 1
|
#define SIGNAL_SHUTDOWN 1
|
||||||
|
#define SIGNAL_BUTTON 2
|
||||||
|
|
||||||
|
#define SIGNAL_PAR_OFFSET 24
|
||||||
|
#define SIGNAL_MASK 0x00FFFFFF
|
||||||
|
|
||||||
#define LINE_STATUS_MAIN 0
|
#define LINE_STATUS_MAIN 0
|
||||||
|
|
||||||
@ -53,6 +57,9 @@ int dns_resolv = -1;
|
|||||||
|
|
||||||
bool blink_mod = false;
|
bool blink_mod = false;
|
||||||
|
|
||||||
|
#define ex_button_count 4
|
||||||
|
uint32_t ex_buttons[ex_button_count] = {15, 14, 2, 5};
|
||||||
|
|
||||||
static uint8_t get_byte(uint32_t addr, int n)
|
static uint8_t get_byte(uint32_t addr, int n)
|
||||||
{
|
{
|
||||||
return (addr >> (8 * n)) & 0xFF;
|
return (addr >> (8 * n)) & 0xFF;
|
||||||
@ -225,18 +232,41 @@ static void IRAM_ATTR isr_shutdown(void* arg)
|
|||||||
xQueueSendFromISR(signal_queue, &sig, NULL);
|
xQueueSendFromISR(signal_queue, &sig, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void IRAM_ATTR isr_button(void* arg)
|
||||||
|
{
|
||||||
|
uint32_t sig = SIGNAL_BUTTON;
|
||||||
|
uint32_t mod = (uint32_t)arg;
|
||||||
|
|
||||||
|
sig = sig | (mod << SIGNAL_PAR_OFFSET);
|
||||||
|
|
||||||
|
xQueueSendFromISR(signal_queue, &sig, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
static void task_signals(void* arg)
|
static void task_signals(void* arg)
|
||||||
{
|
{
|
||||||
|
uint32_t dat;
|
||||||
uint32_t sig;
|
uint32_t sig;
|
||||||
|
uint32_t par;
|
||||||
|
|
||||||
for(;;) {
|
for(;;) {
|
||||||
if(xQueueReceive(signal_queue, &sig, portMAX_DELAY)) {
|
if(xQueueReceive(signal_queue, &dat, portMAX_DELAY)) {
|
||||||
|
|
||||||
|
//ESP_LOGI(TAG, "Signal: %x", dat);
|
||||||
|
|
||||||
|
sig = dat & SIGNAL_MASK;
|
||||||
|
par = sig >> SIGNAL_PAR_OFFSET;
|
||||||
|
|
||||||
if (sig == SIGNAL_SHUTDOWN) {
|
if (sig == SIGNAL_SHUTDOWN) {
|
||||||
ESP_LOGI(TAG, "Got shutdown trigger, going to sleep...");
|
ESP_LOGI(TAG, "Got shutdown trigger, going to sleep...");
|
||||||
ssd1306_clear_screen(&display, false);
|
ssd1306_clear_screen(&display, false);
|
||||||
esp_sleep_enable_ext0_wakeup(34, 0);
|
esp_sleep_enable_ext0_wakeup(34, 0);
|
||||||
esp_deep_sleep_start();
|
esp_deep_sleep_start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (sig == SIGNAL_BUTTON) {
|
||||||
|
//ESP_LOGI(TAG, "Button pressed: %d", par);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -314,6 +344,7 @@ void app_main(void)
|
|||||||
|
|
||||||
ssd1306_clear_screen(&dev, false);
|
ssd1306_clear_screen(&dev, false);
|
||||||
ssd1306_contrast(&dev, 0xff);
|
ssd1306_contrast(&dev, 0xff);
|
||||||
|
ssd1306_display_text(&display, 0, "Waiting for eth", 15, false, 0);
|
||||||
|
|
||||||
display = dev;
|
display = dev;
|
||||||
|
|
||||||
@ -398,22 +429,48 @@ void app_main(void)
|
|||||||
adc1_config_channel_atten(ADC1_CHANNEL_3, ADC_ATTEN_DB_11);
|
adc1_config_channel_atten(ADC1_CHANNEL_3, ADC_ATTEN_DB_11);
|
||||||
|
|
||||||
// gpio setup
|
// gpio setup
|
||||||
|
|
||||||
|
// front buttons
|
||||||
|
|
||||||
//zero-initialize the config structure.
|
//zero-initialize the config structure.
|
||||||
gpio_config_t io_conf = {};
|
gpio_config_t io_conf = {};
|
||||||
|
|
||||||
//interrupt of rising edge
|
//interrupt of rising edge
|
||||||
io_conf.intr_type = GPIO_INTR_POSEDGE;
|
io_conf.intr_type = GPIO_INTR_NEGEDGE;
|
||||||
//bit mask of the pins, here 34
|
//bit mask of the pins, here 34 + front buttons
|
||||||
io_conf.pin_bit_mask = 1ULL<<34;
|
io_conf.pin_bit_mask = 1ULL<<34;
|
||||||
|
|
||||||
|
for (int i = 0; i < ex_button_count; i++) {
|
||||||
|
io_conf.pin_bit_mask = io_conf.pin_bit_mask | (1ULL<<ex_buttons[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t *bit_mask;
|
||||||
|
bit_mask = (uint8_t*)&io_conf.pin_bit_mask;
|
||||||
|
ESP_LOGI(TAG, "pin_bit_mask: %02x %02x %02x %02x %02x %02x %02x %02x", bit_mask[7], bit_mask[6], bit_mask[5], bit_mask[4], bit_mask[3], bit_mask[2], bit_mask[1], bit_mask[0]);
|
||||||
|
|
||||||
|
// pull down
|
||||||
|
io_conf.pull_down_en = 1;
|
||||||
//set as input mode
|
//set as input mode
|
||||||
io_conf.mode = GPIO_MODE_INPUT;
|
io_conf.mode = GPIO_MODE_INPUT;
|
||||||
|
|
||||||
ESP_ERROR_CHECK(gpio_config(&io_conf));
|
ESP_ERROR_CHECK(gpio_config(&io_conf));
|
||||||
|
|
||||||
|
// setup interrupts for buttons
|
||||||
ESP_ERROR_CHECK(gpio_set_intr_type(34, GPIO_INTR_POSEDGE));
|
ESP_ERROR_CHECK(gpio_set_intr_type(34, GPIO_INTR_POSEDGE));
|
||||||
|
|
||||||
|
for (int i = 0; i < ex_button_count; i++) {
|
||||||
|
ESP_ERROR_CHECK(gpio_set_intr_type(ex_buttons[i], GPIO_INTR_NEGEDGE));
|
||||||
|
}
|
||||||
|
|
||||||
// interrut for shutdown/deep sleep
|
// interrut for shutdown/deep sleep
|
||||||
ESP_ERROR_CHECK(gpio_install_isr_service(ESP_INTR_FLAG_LOWMED));
|
ESP_ERROR_CHECK(gpio_install_isr_service(ESP_INTR_FLAG_LOWMED));
|
||||||
ESP_ERROR_CHECK(gpio_isr_handler_add(34, isr_shutdown, NULL));
|
ESP_ERROR_CHECK(gpio_isr_handler_add(34, isr_shutdown, NULL));
|
||||||
|
|
||||||
|
for (int i = 0; i < ex_button_count; i++) {
|
||||||
|
uint32_t btn = ex_buttons + i;
|
||||||
|
ESP_ERROR_CHECK(gpio_isr_handler_add(ex_buttons[i], isr_button, (void*)btn));
|
||||||
|
ESP_LOGI(TAG, "Installed ISR on pin %d", ex_buttons[i]);
|
||||||
|
}
|
||||||
|
|
||||||
// signal task, to catch shutdown signal
|
// signal task, to catch shutdown signal
|
||||||
signal_queue = xQueueCreate(10, sizeof(uint32_t));
|
signal_queue = xQueueCreate(10, sizeof(uint32_t));
|
||||||
@ -429,5 +486,9 @@ void app_main(void)
|
|||||||
blink_mod = !blink_mod;
|
blink_mod = !blink_mod;
|
||||||
refresh_display();
|
refresh_display();
|
||||||
vTaskDelay(350 / portTICK_RATE_MS);
|
vTaskDelay(350 / portTICK_RATE_MS);
|
||||||
|
|
||||||
|
for (int i = 0; i < ex_button_count; i++) {
|
||||||
|
//ESP_LOGI(TAG, "Button status (%d): %d", ex_buttons[i], gpio_get_level(ex_buttons[i]));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user