Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 15a00dc

Browse files
committedMay 26, 2021
make version 2 board work
1 parent a10cfb2 commit 15a00dc

File tree

10 files changed

+169
-33
lines changed

10 files changed

+169
-33
lines changed
 

‎main/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
idf_component_register(SRCS "main.c" "wifi.c" "server.c" "switch.c"
1+
idf_component_register(SRCS "main.c" "wifi.c" "server.c" "switch.c" "host.c"
22
INCLUDE_DIRS ".")

‎main/config.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#pragma once
22

3-
#define WIFI_SSID "my wifi name"
4-
#define WIFI_PASSWORD "my wifi password"
3+
#define WIFI_SSID "ZagtoNet"
4+
#define WIFI_PASSWORD "catlike relocate camper"

‎main/host.c

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include "driver/gpio.h"
2+
#include "host.h"
3+
#include "switch.h"
4+
#include "pins.h"
5+
#include "freertos/task.h"
6+
#include "esp_log.h"
7+
8+
9+
void host_task(void) {
10+
while (true) {
11+
vTaskDelay(25);
12+
13+
while (gpio_get_level(HOST_VOLT_DETECT) == 0) {
14+
vTaskDelay(25);
15+
}
16+
ESP_LOGI("host", "Detected host power up");
17+
if (!enter_host_mode()) {
18+
ESP_LOGE("host", "Could not enter host mode - Please disconnect NBD client!");
19+
vTaskDelay(1000);
20+
continue;
21+
}
22+
23+
while (gpio_get_level(HOST_VOLT_DETECT) == 1) {
24+
vTaskDelay(25);
25+
}
26+
ESP_LOGI("host", "Detected host power down");
27+
leave_host_mode();
28+
}
29+
}

‎main/host.h

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#pragma once
2+
3+
__attribute__((noreturn)) void host_task(void);

‎main/main.c

+14
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
11
#include "driver/sdmmc_host.h"
22
#include "wifi.h"
33
#include "server.h"
4+
#include "host.h"
5+
#include "switch.h"
6+
#include <pthread.h>
47

8+
static void *server_thread_main(void *unused) {
9+
server_task();
10+
}
11+
12+
static void *host_thread_main(void *ununsed) {
13+
host_task();
14+
}
515

616
void app_main(void) {
717
sdmmc_host_init();
18+
initialize_switch();
819
initialize_wifi();
20+
pthread_t host_thread;
21+
pthread_create(&host_thread, NULL, host_thread_main, NULL);
922
server_task();
23+
//host_task();
1024
}

‎main/pins.h

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#pragma once
2+
3+
#define LED_GN 25
4+
#define LED_RT 26
5+
6+
#define BUS_SW_HOST 16
7+
8+
#define BUS_SW_ESP 17
9+
10+
// for Power over MOSFET
11+
12+
#define POWER_OVER_MOSFET 0
13+
14+
#define SD_MOSFET_POWER_SWITCH 32
15+
#define SD_POWER_HOST 33
16+
#define SD_POWER_ESP 27
17+
18+
19+
// SD Interface
20+
#define ESP_CLK 14
21+
#define ESP_CMD 15
22+
#define ESP_D0 2
23+
#define ESP_D1 4
24+
#define ESP_D2 12
25+
#define ESP_D3 13
26+
27+
// SPI
28+
#define ESP_MOSI 23
29+
#define ESP_MISO 19
30+
#define ESP_SCLK 18
31+
#define ESP_CS 5
32+
#define SD_CS ESP_CS
33+
34+
// I2C
35+
#define ESP_SDA 21
36+
#define ESP_SCL 22
37+
38+
#define SD_DETECT_PIN 35
39+
40+
#define HOST_VOLT_DETECT 39

‎main/server.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -226,15 +226,15 @@ static bool transfer_card_data(int fd, sdmmc_card_t *card, uint32_t action, uint
226226
esp_err_t status;
227227
assert(sizeof(buffer) >= sector_size);
228228

229-
ESP_LOGE("server", "Access action %u, global_offset %llu, length %llu", action, global_offset, length);
229+
//ESP_LOGE("server", "Access action %u, global_offset %llu, length %llu", action, global_offset, length);
230230

231231
while (num_sectors > 0) {
232232
size_t max_sectors_in_buffer = sizeof(buffer) / sector_size;
233233
size_t sectors_in_buffer = num_sectors > max_sectors_in_buffer ? max_sectors_in_buffer : num_sectors;
234-
ESP_LOGE("server", "iteration sectors_in_buffer %u", sectors_in_buffer);
234+
//ESP_LOGE("server", "iteration sectors_in_buffer %u", sectors_in_buffer);
235235

236236
if (start_offset != 0 && action == NBD_CMD_WRITE) {
237-
ESP_LOGE("server", "reading first sector");
237+
//ESP_LOGE("server", "reading first sector");
238238
/* For writes which don't align with sector boarders, we need to read the first sector
239239
* to know what should be written into the area that should not be changed but is part
240240
* of the same sector */
@@ -246,7 +246,7 @@ static bool transfer_card_data(int fd, sdmmc_card_t *card, uint32_t action, uint
246246
}
247247

248248
if (num_sectors == sectors_in_buffer && end_offset != 0 && (num_sectors > 1 || start_offset == 0)) {
249-
ESP_LOGE("server", "reading last sector");
249+
//ESP_LOGE("server", "reading last sector");
250250
/* same for the last block, unless there is only one sector that has just been written
251251
* as the first */
252252
status = sdmmc_read_sectors(card,
@@ -263,7 +263,7 @@ static bool transfer_card_data(int fd, sdmmc_card_t *card, uint32_t action, uint
263263
if (num_sectors == sectors_in_buffer) {
264264
transfer_length -= end_offset;
265265
}
266-
ESP_LOGE("server", "iteration transfer_length %u", transfer_length);
266+
//ESP_LOGE("server", "iteration transfer_length %u", transfer_length);
267267

268268
if (action == NBD_CMD_READ) {
269269
status = sdmmc_read_sectors(card, buffer, first_sector, sectors_in_buffer);
@@ -296,7 +296,7 @@ static bool transfer_card_data(int fd, sdmmc_card_t *card, uint32_t action, uint
296296
start_offset = 0;
297297
}
298298

299-
ESP_LOGE("server", "Access successful");
299+
//ESP_LOGE("server", "Access successful");
300300
return true;
301301
}
302302

‎main/server.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
#pragma once
22

3-
void server_task(void);
3+
__attribute__((noreturn)) void server_task(void);

‎main/switch.c

+69-22
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,35 @@
11
#include <stdlib.h>
2+
#include <pthread.h>
23
#include "esp_log.h"
34
#include "sdmmc_cmd.h"
45
#include "driver/sdmmc_host.h"
56
#include "driver/gpio.h"
67
#include "switch.h"
8+
#include "pins.h"
79
#include "freertos/task.h"
810

911

1012
static const uint64_t ALL_SD_PINS = GPIO_SEL_14|GPIO_SEL_15|GPIO_SEL_2|GPIO_SEL_4|GPIO_SEL_12|GPIO_SEL_13;
11-
static const uint64_t POWER_PINS = GPIO_SEL_0|GPIO_SEL_16|GPIO_SEL_19|GPIO_SEL_21|GPIO_SEL_32|GPIO_SEL_33;
1213

1314
enum Mode {
1415
IDLE, FLASH, DEVICE
1516
};
1617
static enum Mode active_mode = IDLE;
1718

1819

20+
static pthread_mutex_t access_mutex = PTHREAD_MUTEX_INITIALIZER;
21+
22+
1923
bool enter_flash_mode(sdmmc_card_t *card) {
24+
if (pthread_mutex_trylock(&access_mutex) != 0) {
25+
return false;
26+
}
2027
ESP_LOGI("flash-mode", "Entering...");
2128

22-
gpio_config_t power_config = {
23-
POWER_PINS,
24-
GPIO_MODE_OUTPUT,
25-
GPIO_PULLUP_ENABLE,
26-
GPIO_PULLDOWN_DISABLE,
27-
GPIO_INTR_DISABLE
28-
};
29-
gpio_config(&power_config);
30-
for (uint64_t pin = 0; pin < 64; pin++) {
31-
if (POWER_PINS & (1ull << pin)) {
32-
gpio_set_drive_capability(pin, GPIO_DRIVE_CAP_3);
33-
}
34-
}
35-
for (uint64_t pin = 0; pin < 64; pin++) {
36-
if (POWER_PINS & (1ull << pin)) {
37-
gpio_set_level(pin, 1);
38-
}
39-
}
29+
gpio_set_level(SD_POWER_ESP, 0);
30+
gpio_set_level(BUS_SW_ESP, 1);
4031

41-
vTaskDelay(50);
32+
vTaskDelay(25);
4233

4334
sdmmc_host_t host = SDMMC_HOST_DEFAULT();
4435
sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
@@ -66,11 +57,67 @@ bool enter_flash_mode(sdmmc_card_t *card) {
6657
return true;
6758
}
6859

69-
70-
void leave_flash_mode() {
60+
void leave_flash_mode(void) {
61+
ESP_LOGI("flash-mode", "Leaving...");
62+
gpio_set_level(BUS_SW_ESP, 0);
7163
for (uint64_t pin = 0; pin < 64; pin++) {
7264
if (ALL_SD_PINS & (1ull << pin)) {
7365
gpio_reset_pin(pin);
7466
}
7567
}
68+
vTaskDelay(25);
69+
gpio_set_level(SD_POWER_ESP, 1);
70+
71+
vTaskDelay(25);
72+
pthread_mutex_unlock(&access_mutex);
73+
}
74+
75+
bool enter_host_mode() {
76+
if (pthread_mutex_trylock(&access_mutex) != 0) {
77+
return false;
78+
}
79+
ESP_LOGI("host-mode", "Entering...");
80+
81+
gpio_set_level(SD_POWER_HOST, 0);
82+
gpio_set_level(BUS_SW_HOST, 1);
83+
return true;
84+
}
85+
86+
void leave_host_mode() {
87+
ESP_LOGI("host-mode", "Leaving...");
88+
gpio_set_level(BUS_SW_HOST, 0);
89+
vTaskDelay(25);
90+
gpio_set_level(SD_POWER_HOST, 1);
91+
vTaskDelay(25);
92+
pthread_mutex_unlock(&access_mutex);
93+
}
94+
95+
void initialize_switch(void) {
96+
gpio_config_t power_config = {
97+
(1ull<<SD_POWER_HOST)|(1ull<<SD_POWER_ESP)|(1ull<<BUS_SW_ESP)|(1ull<<BUS_SW_HOST),
98+
GPIO_MODE_OUTPUT,
99+
GPIO_PULLUP_DISABLE,
100+
GPIO_PULLDOWN_DISABLE,
101+
GPIO_INTR_DISABLE
102+
};
103+
104+
gpio_config(&power_config);
105+
106+
gpio_config_t input_config = {
107+
(1ull<<HOST_VOLT_DETECT),
108+
GPIO_MODE_INPUT,
109+
GPIO_PULLUP_DISABLE,
110+
GPIO_PULLDOWN_ENABLE,
111+
GPIO_INTR_DISABLE
112+
};
113+
gpio_config(&input_config);
114+
115+
// leave_flash_mode();
116+
117+
gpio_set_level(SD_POWER_HOST, 1);
118+
gpio_set_level(BUS_SW_HOST, 0);
119+
gpio_set_level(SD_POWER_ESP, 1);
120+
gpio_set_level(BUS_SW_ESP, 0);
121+
122+
vTaskDelay(25);
76123
}

‎main/switch.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@
44
#include "driver/sdmmc_host.h"
55

66
bool enter_flash_mode(sdmmc_card_t *card);
7-
void leave_flash_mode();
7+
void leave_flash_mode(void);
8+
bool enter_host_mode(void);
9+
void leave_host_mode(void);
10+
void initialize_switch(void);

0 commit comments

Comments
 (0)
Failed to load comments.