diff --git a/hardware/firmware/lib/mfrc522_driver/mfrc522.c b/hardware/firmware/lib/mfrc522_driver/mfrc522.c new file mode 100644 index 0000000..4c45995 --- /dev/null +++ b/hardware/firmware/lib/mfrc522_driver/mfrc522.c @@ -0,0 +1,142 @@ +#include "mfrc522.h" +#include "driver/gpio.h" +#include "esp_log.h" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include +#include + +static const char *TAG = "MFRC522"; + +#define SPI_MASTER_FREQ_HZ 1000000 // 1MHz + +mfrc522_handle_t* mfrc522_init(int sck_pin, int mosi_pin, int miso_pin, int cs_pin, int rst_pin) +{ + mfrc522_handle_t *handle = (mfrc522_handle_t *)malloc(sizeof(mfrc522_handle_t)); + if (!handle) { + ESP_LOGE(TAG, "Failed to allocate memory"); + return NULL; + } + + // 配置 RST 引脚 + gpio_config_t io_conf = { + .pin_bit_mask = (1ULL << rst_pin), + .mode = GPIO_MODE_OUTPUT, + .pull_up_en = GPIO_PULLUP_DISABLE, + .pull_down_en = GPIO_PULLDOWN_DISABLE, + .intr_type = GPIO_INTR_DISABLE + }; + gpio_config(&io_conf); + gpio_set_level(rst_pin, 1); + + // 配置 SPI + spi_bus_config_t buscfg = { + .miso_io_num = miso_pin, + .mosi_io_num = mosi_pin, + .sclk_io_num = sck_pin, + .quadwp_io_num = -1, + .quadhd_io_num = -1, + .max_transfer_sz = 4096, + }; + + spi_device_interface_config_t devcfg = { + .mode = 0, + .clock_speed_hz = SPI_MASTER_FREQ_HZ, + .spics_io_num = cs_pin, + .queue_size = 7, + }; + + esp_err_t ret = spi_bus_initialize(HSPI_HOST, &buscfg, SPI_DMA_CH_AUTO); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "Failed to initialize SPI bus"); + free(handle); + return NULL; + } + + ret = spi_bus_add_device(HSPI_HOST, &devcfg, &handle->spi); + if (ret != ESP_OK) { + ESP_LOGE(TAG, "Failed to add SPI device"); + free(handle); + return NULL; + } + + handle->uid_len = 0; + memset(handle->uid, 0, sizeof(handle->uid)); + + // 复位 MFRC522 + mfrc522_reset(handle); + + ESP_LOGI(TAG, "MFRC522 initialized successfully"); + return handle; +} + +void mfrc522_reset(mfrc522_handle_t *handle) +{ + mfrc522_write_reg(handle, MFRC522_REG_COMMAND, MFRC522_CMD_RESETPHASE); + vTaskDelay(pdMS_TO_TICKS(50)); + + // 初始化寄存器 + mfrc522_write_reg(handle, MFRC522_REG_MODE, 0x3D); // CRC初始值 + mfrc522_write_reg(handle, MFRC522_REG_TX_CONTROL, 0x83); // 打开TX1和TX2输出 + mfrc522_write_reg(handle, MFRC522_REG_TX_AUTO, 0x40); // 自动 RF 开启 + + mfrc522_write_reg(handle, MFRC522_REG_COMMAND, MFRC522_CMD_IDLE); +} + +void mfrc522_write_reg(mfrc522_handle_t *handle, uint8_t reg, uint8_t val) +{ + uint8_t tx_data[2]; + tx_data[0] = ((reg << 1) & 0x7E) | 0x00; // 写操作 + tx_data[1] = val; + + spi_transaction_t t = { + .length = 16, + .tx_buffer = tx_data, + }; + spi_device_transmit(handle->spi, &t); +} + +uint8_t mfrc522_read_reg(mfrc522_handle_t *handle, uint8_t reg) +{ + uint8_t tx_data[2] = {0}; + uint8_t rx_data[2] = {0}; + + tx_data[0] = ((reg << 1) & 0x7E) | 0x80; // 读操作 + + spi_transaction_t t = { + .length = 16, + .tx_buffer = tx_data, + .rx_buffer = rx_data, + }; + spi_device_transmit(handle->spi, &t); + + return rx_data[1]; +} + +uint8_t mfrc522_picc_is_new(mfrc522_handle_t *handle) +{ + uint8_t status = mfrc522_read_reg(handle, MFRC522_REG_STATUS1); + return (status & 0x01); // 检查标签检测位 +} + +uint8_t mfrc522_picc_read_serial(mfrc522_handle_t *handle) +{ + // 这是一个简化版本,实际实现需要完整的 Mifare 协议 + // 这里仅作示例 + handle->uid_len = 4; + return handle->uid_len; +} + +void mfrc522_picc_halt_a(mfrc522_handle_t *handle) +{ + mfrc522_write_reg(handle, MFRC522_REG_COMMAND, MFRC522_CMD_IDLE); +} + +void mfrc522_deinit(mfrc522_handle_t *handle) +{ + if (handle) { + spi_bus_remove_device(handle->spi); + spi_bus_free(HSPI_HOST); + free(handle); + } +} diff --git a/hardware/firmware/lib/mfrc522_driver/mfrc522.h b/hardware/firmware/lib/mfrc522_driver/mfrc522.h new file mode 100644 index 0000000..27bc785 --- /dev/null +++ b/hardware/firmware/lib/mfrc522_driver/mfrc522.h @@ -0,0 +1,120 @@ +#ifndef MFRC522_H +#define MFRC522_H + +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +// MFRC522 SPI 命令 +#define MFRC522_CMD_IDLE 0x00 +#define MFRC522_CMD_AUTHENT 0x0E +#define MFRC522_CMD_RECEIVE 0x08 +#define MFRC522_CMD_TRANSMIT 0x04 +#define MFRC522_CMD_TRANSCEIVE 0x0C +#define MFRC522_CMD_RESETPHASE 0x0F +#define MFRC522_CMD_CALCCRC 0x03 + +// MFRC522 寄存器 +#define MFRC522_REG_COMMAND 0x01 +#define MFRC522_REG_COMM_IE_N 0x04 +#define MFRC522_REG_DIV_IE_N 0x05 +#define MFRC522_REG_COMM_IRQ 0x04 +#define MFRC522_REG_DIV_IRQ 0x05 +#define MFRC522_REG_ERROR 0x06 +#define MFRC522_REG_STATUS1 0x07 +#define MFRC522_REG_STATUS2 0x08 +#define MFRC522_REG_FIFO_DATA 0x09 +#define MFRC522_REG_FIFO_LEVEL 0x0A +#define MFRC522_REG_WATER_LEVEL 0x0B +#define MFRC522_REG_CONTROL 0x0C +#define MFRC522_REG_BITFRAMING 0x0D +#define MFRC522_REG_COLL 0x0E +#define MFRC522_REG_MODE 0x11 +#define MFRC522_REG_TX_CONTROL 0x14 +#define MFRC522_REG_TX_AUTO 0x15 +#define MFRC522_REG_TX_SELREQ 0x16 +#define MFRC522_REG_RX_SELREQ 0x17 +#define MFRC522_REG_RX_THRESHOLD 0x18 +#define MFRC522_REG_DEMOD 0x19 +#define MFRC522_REG_MIFARE_TX 0x1C +#define MFRC522_REG_MIFARE_RX 0x1D +#define MFRC522_REG_SERIAL_SPEED 0x1F +#define MFRC522_REG_TIMER_MODE 0x2A +#define MFRC522_REG_TIMER_PRESCALER 0x2B +#define MFRC522_REG_TIMER_RELOAD_HI 0x2C +#define MFRC522_REG_TIMER_RELOAD_LO 0x2D +#define MFRC522_REG_TIMER_COUNTER_HI 0x2E +#define MFRC522_REG_TIMER_COUNTER_LO 0x2F +#define MFRC522_REG_TEST_SEN_SEL 0x38 +#define MFRC522_REG_TEST_BUS 0x39 +#define MFRC522_REG_AUTO_TEST 0x3A +#define MFRC522_REG_VERSION 0x37 + +// MFRC522 Mifare 命令 +#define MFRC522_MIFARE_CMD_AUTH_KEY_A 0x60 +#define MFRC522_MIFARE_CMD_AUTH_KEY_B 0x61 +#define MFRC522_MIFARE_CMD_READ 0x30 +#define MFRC522_MIFARE_CMD_WRITE 0xA0 +#define MFRC522_PICC_CMD_SELECT_CL 0x93 +#define MFRC522_PICC_CMD_HALT_A 0x50 + +typedef struct { + spi_device_handle_t spi; + uint8_t uid[10]; + uint8_t uid_len; +} mfrc522_handle_t; + +/** + * 初始化 MFRC522 + * @param sck_pin SCK 引脚 + * @param mosi_pin MOSI 引脚 + * @param miso_pin MISO 引脚 + * @param cs_pin CS 引脚 + * @param rst_pin RST 引脚 + * @return mfrc522_handle_t 指针 + */ +mfrc522_handle_t* mfrc522_init(int sck_pin, int mosi_pin, int miso_pin, int cs_pin, int rst_pin); + +/** + * 复位 MFRC522 + */ +void mfrc522_reset(mfrc522_handle_t *handle); + +/** + * 写寄存器 + */ +void mfrc522_write_reg(mfrc522_handle_t *handle, uint8_t reg, uint8_t val); + +/** + * 读寄存器 + */ +uint8_t mfrc522_read_reg(mfrc522_handle_t *handle, uint8_t reg); + +/** + * 检测卡片 + */ +uint8_t mfrc522_picc_is_new(mfrc522_handle_t *handle); + +/** + * 读卡 UID + */ +uint8_t mfrc522_picc_read_serial(mfrc522_handle_t *handle); + +/** + * 停止与卡通信 + */ +void mfrc522_picc_halt_a(mfrc522_handle_t *handle); + +/** + * 关闭 MFRC522 + */ +void mfrc522_deinit(mfrc522_handle_t *handle); + +#ifdef __cplusplus +} +#endif + +#endif // MFRC522_H diff --git a/hardware/firmware/platformio.ini b/hardware/firmware/platformio.ini index 67efa01..385e75c 100644 --- a/hardware/firmware/platformio.ini +++ b/hardware/firmware/platformio.ini @@ -15,5 +15,6 @@ framework = espidf lib_deps = matth-x/MicroOcpp@^1.2.0 matth-x/MicroOcppMongoose@^1.2.0 -build_flags = -DMO_PLATFORM=MO_PLATFORM_ESPIDF -DMO_MG_USE_VERSION=MO_MG_V715 + roboticsbrno/SmartLeds@^3.1.5 +build_flags = -DMO_PLATFORM=MO_PLATFORM_ESPIDF -DMO_MG_USE_VERSION=MO_MG_V715 -DCONFIG_RMT_ISR_IRAM_SAFE=1 board_build.partitions = partitions.csv diff --git a/hardware/firmware/sdkconfig.rymcu-esp32-devkitc b/hardware/firmware/sdkconfig.rymcu-esp32-devkitc index c482b32..2b354d2 100644 --- a/hardware/firmware/sdkconfig.rymcu-esp32-devkitc +++ b/hardware/firmware/sdkconfig.rymcu-esp32-devkitc @@ -407,7 +407,7 @@ CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y # CONFIG_ESPTOOLPY_FLASHSIZE_64MB is not set # CONFIG_ESPTOOLPY_FLASHSIZE_128MB is not set CONFIG_ESPTOOLPY_FLASHSIZE="4MB" -CONFIG_ESPTOOLPY_HEADER_FLASHSIZE_UPDATE=y +# CONFIG_ESPTOOLPY_HEADER_FLASHSIZE_UPDATE is not set CONFIG_ESPTOOLPY_BEFORE_RESET=y # CONFIG_ESPTOOLPY_BEFORE_NORESET is not set CONFIG_ESPTOOLPY_BEFORE="default_reset" @@ -1445,7 +1445,7 @@ CONFIG_LWIP_MAX_SOCKETS=10 # CONFIG_LWIP_SO_LINGER is not set CONFIG_LWIP_SO_REUSE=y CONFIG_LWIP_SO_REUSE_RXTOALL=y -# CONFIG_LWIP_SO_RCVBUF is not set +CONFIG_LWIP_SO_RCVBUF=y # CONFIG_LWIP_NETBUF_RECVINFO is not set CONFIG_LWIP_IP_DEFAULT_TTL=64 CONFIG_LWIP_IP4_FRAG=y diff --git a/hardware/firmware/src/config.h b/hardware/firmware/src/config.h new file mode 100644 index 0000000..fe9c63a --- /dev/null +++ b/hardware/firmware/src/config.h @@ -0,0 +1,8 @@ +#define CFG_WIFI_SSID "MisakaNetwork" +#define CFG_WIFI_PASS "20211028" +#define CFG_WIFI_MAXIMUM_RETRY 5 +#define CFG_OCPP_BACKEND "ws://192.168.1.100:8180/steve/websocket/CentralSystemService" +#define CFG_CP_IDENTIFIER "CP0001" +#define CFG_CP_MODAL "Helios DA One" +#define CFG_CP_VENDOR "RayineElec" +#define CFG_AUTHORIZATIONKEY "my_secret_key" diff --git a/hardware/firmware/src/main.c b/hardware/firmware/src/main.cpp similarity index 54% rename from hardware/firmware/src/main.c rename to hardware/firmware/src/main.cpp index de92606..0b268d5 100644 --- a/hardware/firmware/src/main.c +++ b/hardware/firmware/src/main.cpp @@ -1,13 +1,3 @@ -// #include "esp_wifi.h" -// #include - -// void app_main() {} - -/* Based on the ESP-IDF WiFi station Example (see https://github.com/espressif/esp-idf/tree/release/v4.4/examples/wifi/getting_started/station/main) - - This example code extends the WiFi example with the necessary calls to establish an - OCPP connection on the ESP-IDF. -*/ #include #include "freertos/FreeRTOS.h" #include "freertos/task.h" @@ -21,22 +11,15 @@ #include "lwip/err.h" #include "lwip/sys.h" -/* MicroOcpp includes */ #include -#include //C-facade of MicroOcpp -#include //WebSocket integration for ESP-IDF +#include +#include -/* The examples use WiFi configuration that you can set via project configuration menu +#include - If you'd rather not, just change the below entries to strings with - the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid" -*/ -#define EXAMPLE_ESP_WIFI_SSID "MisakaNetwork" -#define EXAMPLE_ESP_WIFI_PASS "20211028" -#define EXAMPLE_ESP_MAXIMUM_RETRY 5 -#define EXAMPLE_MO_OCPP_BACKEND "ws://192.168.1.100:8180/steve/websocket/CentralSystemService" -#define EXAMPLE_MO_CHARGEBOXID "CP0001" -#define EXAMPLE_MO_AUTHORIZATIONKEY "authkey123" +#include "mfrc522.h" + +#include "config.h" /* FreeRTOS event group to signal when we are connected*/ static EventGroupHandle_t s_wifi_event_group; @@ -47,12 +30,16 @@ static EventGroupHandle_t s_wifi_event_group; #define WIFI_CONNECTED_BIT BIT0 #define WIFI_FAIL_BIT BIT1 +#define LED_PIN 17 +#define NUM_LEDS 1 + +SmartLed leds(LED_WS2812B, NUM_LEDS, LED_PIN, 0, DoubleBuffer); + static const char *TAG = "wifi station"; static int s_retry_num = 0; -static void event_handler(void *arg, esp_event_base_t event_base, - int32_t event_id, void *event_data) +static void event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) { if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) { @@ -60,7 +47,7 @@ static void event_handler(void *arg, esp_event_base_t event_base, } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { - if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY) + if (s_retry_num < CFG_WIFI_MAXIMUM_RETRY) { esp_wifi_connect(); s_retry_num++; @@ -95,27 +82,18 @@ void wifi_init_sta(void) esp_event_handler_instance_t instance_any_id; esp_event_handler_instance_t instance_got_ip; - ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, - ESP_EVENT_ANY_ID, - &event_handler, - NULL, - &instance_any_id)); - ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, - IP_EVENT_STA_GOT_IP, - &event_handler, - NULL, - &instance_got_ip)); + ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL, &instance_any_id)); + ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL, &instance_got_ip)); - wifi_config_t wifi_config = { - .sta = { - .ssid = EXAMPLE_ESP_WIFI_SSID, - .password = EXAMPLE_ESP_WIFI_PASS, - /* Setting a password implies station will connect to all security modes including WEP/WPA. - * However these modes are deprecated and not advisable to be used. Incase your Access point - * doesn't support WPA2, these mode can be enabled by commenting below line */ - .threshold.authmode = WIFI_AUTH_WPA2_PSK, - }, - }; + /* Initialize wifi_config in a C++-friendly way (designated initializers are C-only) */ + wifi_config_t wifi_config; + memset(&wifi_config, 0, sizeof(wifi_config)); + strncpy((char *)wifi_config.sta.ssid, CFG_WIFI_SSID, sizeof(wifi_config.sta.ssid) - 1); + strncpy((char *)wifi_config.sta.password, CFG_WIFI_PASS, sizeof(wifi_config.sta.password) - 1); + /* Setting a password implies station will connect to all security modes including WEP/WPA. + * However these modes are deprecated and not advisable to be used. Incase your Access point + * doesn't support WPA2, these mode can be enabled by changing the authmode below. */ + wifi_config.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK; ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config)); ESP_ERROR_CHECK(esp_wifi_start()); @@ -124,23 +102,17 @@ void wifi_init_sta(void) /* Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum * number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) */ - EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group, - WIFI_CONNECTED_BIT | WIFI_FAIL_BIT, - pdFALSE, - pdFALSE, - portMAX_DELAY); + EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group, WIFI_CONNECTED_BIT | WIFI_FAIL_BIT, pdFALSE, pdFALSE, portMAX_DELAY); /* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually * happened. */ if (bits & WIFI_CONNECTED_BIT) { - ESP_LOGI(TAG, "connected to ap SSID:%s password:%s", - EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS); + ESP_LOGI(TAG, "connected to ap SSID:%s password:%s", CFG_WIFI_SSID, CFG_WIFI_PASS); } else if (bits & WIFI_FAIL_BIT) { - ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s", - EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS); + ESP_LOGI(TAG, "Failed to connect to SSID:%s, password:%s", CFG_WIFI_SSID, CFG_WIFI_PASS); } else { @@ -153,7 +125,16 @@ void wifi_init_sta(void) vEventGroupDelete(s_wifi_event_group); } -void app_main(void) +uint8_t hue; +void showGradient() +{ + hue++; + for (int i = 0; i != NUM_LEDS; i++) + leds[i] = Hsv{static_cast(hue + 30 * i), 255, 255}; + leds.show(); +} + +extern "C" void app_main(void) { // Initialize NVS esp_err_t ret = nvs_flash_init(); @@ -175,17 +156,15 @@ void app_main(void) /* Initialize MicroOcpp */ struct OCPP_FilesystemOpt fsopt = {.use = true, .mount = true, .formatFsOnFail = true}; - OCPP_Connection *osock = ocpp_makeConnection(&mgr, - EXAMPLE_MO_OCPP_BACKEND, - EXAMPLE_MO_CHARGEBOXID, - EXAMPLE_MO_AUTHORIZATIONKEY, "", fsopt); - ocpp_initialize(osock, "HeliosDA One", "RayineElec", fsopt, false); + OCPP_Connection *osock = ocpp_makeConnection(&mgr, CFG_OCPP_BACKEND, CFG_CP_IDENTIFIER, CFG_AUTHORIZATIONKEY, "", fsopt); + ocpp_initialize(osock, CFG_CP_MODAL, CFG_CP_VENDOR, fsopt, false); /* Enter infinite loop */ while (1) { mg_mgr_poll(&mgr, 10); ocpp_loop(); + showGradient(); } /* Deallocate ressources */ @@ -193,4 +172,4 @@ void app_main(void) ocpp_deinitConnection(osock); mg_mgr_free(&mgr); return; -} \ No newline at end of file +}