diff --git a/CMakeLists.txt b/CMakeLists.txt index d9da57f..5756a55 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,9 +9,10 @@ option(BLISP_USE_SYSTEM_LIBRARIES "Use system-installed libraries" "${CMAKE_USE_ option(COMPILE_TESTS "Compile the tests" OFF) add_library(libblisp_obj OBJECT - lib/blisp.c + lib/blisp.c lib/blisp_easy.c lib/chip/blisp_chip_bl60x.c - lib/chip/blisp_chip_bl70x.c lib/blisp_easy.c) + lib/chip/blisp_chip_bl70x.c + lib/chip/blisp_chip_bl808.c) target_include_directories(libblisp_obj PRIVATE ${CMAKE_SOURCE_DIR}/include/) diff --git a/include/blisp_chip.h b/include/blisp_chip.h index 3ffac87..c19432e 100644 --- a/include/blisp_chip.h +++ b/include/blisp_chip.h @@ -28,4 +28,4 @@ extern struct blisp_chip blisp_chip_bl70x; extern struct blisp_chip blisp_chip_bl808; extern struct blisp_chip blisp_chip_bl61x; -#endif \ No newline at end of file +#endif diff --git a/lib/blisp.c b/lib/blisp.c index b3d8f42..37b2fc6 100644 --- a/lib/blisp.c +++ b/lib/blisp.c @@ -216,11 +216,38 @@ blisp_return_t blisp_device_handshake(struct blisp_device* device, sp_flush(serial_port, SP_BUF_INPUT); // Flush garbage out of RX } - ret = sp_blocking_read(serial_port, device->rx_buffer, 2, 50); - if (ret >= 2) { - if (device->rx_buffer[0] == 'O' && device->rx_buffer[1] == 'K') { - return BLISP_OK; - } + uint32_t bytes_count = device->chip->handshake_byte_multiplier * (float)device->current_baud_rate / 10.0f; + if (bytes_count > 600) bytes_count = 600; + memset(handshake_buffer, 'U', bytes_count); + +// sp_flush(serial_port, SP_BUF_BOTH); + + for (uint8_t i = 0; i < 5; i++) { + if (!in_ef_loader) { + if (device->is_usb) { + sp_blocking_write(serial_port, "BOUFFALOLAB5555RESET\0\0", 22, + 100); // TODO: Error handling + } + } + ret = sp_blocking_write(serial_port, handshake_buffer, bytes_count, + 500); + if (ret < 0) { + return -1; + } + + if (device->chip->type == BLISP_CHIP_BL808) { + sleep_ms(300); + const uint8_t second_handshake[] = { 0x50, 0x00, 0x08, 0x00, 0x38, 0xF0, 0x00, 0x20, 0x00, 0x00, 0x00, 0x18 }; + sp_blocking_write(serial_port, second_handshake, sizeof(second_handshake), 300); // TODO: Error handling + } + ret = sp_blocking_read(serial_port, device->rx_buffer, 20, 50); + if (ret >= 2) { + for (uint8_t j = 0; j < (ret - 1); j++) { + if (device->rx_buffer[j] == 'O' && device->rx_buffer[j + 1] == 'K') { + return BLISP_OK; + } + } + } } } blisp_dlog("Received no response from chip."); @@ -239,14 +266,13 @@ blisp_return_t blisp_device_get_boot_info(struct blisp_device* device, if (ret < 0) return ret; - memcpy(boot_info->boot_rom_version, &device->rx_buffer[0], + memcpy(boot_info->boot_rom_version, &device->rx_buffer[0], 4); // TODO: Endianess; this may break on big endian machines - - if (device->chip->type == BLISP_CHIP_BL70X) { - memcpy(boot_info->chip_id, &device->rx_buffer[16], 8); - } - // TODO: BL60X - return BLISP_OK; + if (device->chip->type == BLISP_CHIP_BL70X || device->chip->type == BLISP_CHIP_BL808) { // TODO: This is only 70X related + memcpy(boot_info->chip_id, &device->rx_buffer[16], 8); + } + // TODO: BL60X, BL808 + return BLISP_OK; } // TODO: Use struct instead of uint8_t* @@ -414,4 +440,4 @@ blisp_return_t blisp_device_reset(struct blisp_device* device) { void blisp_device_close(struct blisp_device* device) { struct sp_port* serial_port = device->serial_port; sp_close(serial_port); -} \ No newline at end of file +} diff --git a/lib/chip/blisp_chip_bl808.c b/lib/chip/blisp_chip_bl808.c index f2274c8..957507e 100644 --- a/lib/chip/blisp_chip_bl808.c +++ b/lib/chip/blisp_chip_bl808.c @@ -1,11 +1,12 @@ // SPDX-License-Identifier: MIT #include "blisp.h" +#include struct blisp_chip blisp_chip_bl808 = { .type = BLISP_CHIP_BL808, .type_str = "bl808", .usb_isp_available = true, // TODO: Only for BL808D :-( - .default_xtal = "-", // ? - .handshake_byte_multiplier = 0.003f, - .get_eflash_loader = NULL + .default_xtal = "-", // XXX: bfl software marks this as "Auto (0x07)" + .handshake_byte_multiplier = 0.006f, + .load_eflash_loader = NULL }; diff --git a/tools/blisp/src/common.c b/tools/blisp/src/common.c index b486daa..f1e1c4a 100644 --- a/tools/blisp/src/common.c +++ b/tools/blisp/src/common.c @@ -31,6 +31,8 @@ blisp_return_t blisp_common_init_device(struct blisp_device* device, chip = &blisp_chip_bl70x; } else if (strcmp(chip_type->sval[0], "bl60x") == 0) { chip = &blisp_chip_bl60x; + } else if (strcmp(chip_type->sval[0], "bl808") == 0) { + chip = &blisp_chip_bl808; } else { fprintf(stderr, "Chip type is invalid.\n"); return BLISP_ERR_INVALID_CHIP_TYPE; @@ -141,4 +143,4 @@ exit1: free(eflash_loader_buffer); return ret; -} \ No newline at end of file +}