From f55975fe9bda8982e4b51c1c41b0cbee63e810ae Mon Sep 17 00:00:00 2001 From: Pavel Zakopaylo Date: Thu, 30 Nov 2023 18:43:57 +1100 Subject: [PATCH] Implemented setting clock parameters for BL808 --- data/bl808_clock_para.h | 16 ++++++++++++ lib/blisp.c | 56 ++++++++++++++++++++++++++++++---------- tools/blisp/src/common.c | 9 +++++++ 3 files changed, 68 insertions(+), 13 deletions(-) create mode 100644 data/bl808_clock_para.h diff --git a/data/bl808_clock_para.h b/data/bl808_clock_para.h new file mode 100644 index 0000000..70d741a --- /dev/null +++ b/data/bl808_clock_para.h @@ -0,0 +1,16 @@ +#ifndef BLISP_BL808_CLOCK_PARA_H +#define BLISP_BL808_CLOCK_PARA_H + +#include + +/** + * Extracted from the bflb-iot-tool PIP package: + * original location: chips/bl808/efuse_bootheader/clock_para.bin + * SHA256 SUM: 14e57dfae793a41cf8a61c31a2c93b73b09126d8315230ede29898634a8dad3c + * Copyright Bouffalo Lab, License: MIT + */ +static uint8_t bl808_clock_para_bin[] = { 0x50, 0x43, 0x46, 0x47, 0x07, 0x04, 0x00, 0x00, 0x03, 0x01, 0x03, 0x00, + 0x01, 0x02, 0x00, 0x02, 0x01, 0x01, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, + 0x0a, 0x89, 0x4b, 0x86 }; + +#endif // BLISP_BL808_CLOCK_PARA_H diff --git a/lib/blisp.c b/lib/blisp.c index 6dbdfbd..1603417 100644 --- a/lib/blisp.c +++ b/lib/blisp.c @@ -11,6 +11,8 @@ #include #endif +#include "../data/bl808_clock_para.h" + #define DEBUG static void drain(struct sp_port* port) { @@ -177,6 +179,7 @@ blisp_return_t blisp_receive_response(struct blisp_device* device, blisp_return_t blisp_device_handshake(struct blisp_device* device, bool in_ef_loader) { int ret; + bool ok = false; uint8_t handshake_buffer[600]; struct sp_port* serial_port = device->serial_port; @@ -217,26 +220,30 @@ blisp_return_t blisp_device_handshake(struct blisp_device* device, } 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 }; - ret = sp_blocking_write(serial_port, second_handshake, sizeof(second_handshake), 300); - if (ret < 0) { - blisp_dlog("Second handshake write failed, ret %d", ret); - return BLISP_ERR_API_ERROR; - } + sleep_ms(300); + const uint8_t second_handshake[] = { 0x50, 0x00, 0x08, 0x00, 0x38, 0xF0, 0x00, 0x20, 0x00, 0x00, 0x00, 0x18 }; + ret = sp_blocking_write(serial_port, second_handshake, sizeof(second_handshake), 300); + if (ret < 0) { + blisp_dlog("Second handshake write failed, ret %d", ret); + return BLISP_ERR_API_ERROR; + } } 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; - } + for (uint8_t j = 0; j < (ret - 1); j++) { + if (device->rx_buffer[j] == 'O' && device->rx_buffer[j + 1] == 'K') { + ok = true; } + } } + if (!ok) { + blisp_dlog("Received no response from chip."); + return BLISP_ERR_NO_RESPONSE; + } + + return BLISP_OK; } - blisp_dlog("Received no response from chip."); - return BLISP_ERR_NO_RESPONSE; } blisp_return_t blisp_device_get_boot_info(struct blisp_device* device, @@ -426,3 +433,26 @@ void blisp_device_close(struct blisp_device* device) { struct sp_port* serial_port = device->serial_port; sp_close(serial_port); } + +blisp_return_t bl808_load_clock_para(struct blisp_device* device, + bool irq_en, uint32_t baudrate) { + // XXX: this may be a good place to increase the baudrate for subsequent comms + // XXX: for the write command, we may wish to use this data to update the boot header + const uint32_t clock_para_size = sizeof(bl808_clock_para_bin); + const uint32_t payload_size = 8 + clock_para_size; + uint8_t payload[payload_size] = {}; + + uint32_t irq_enable = irq_en ? 1 : 0; + memcpy(&payload[0], &irq_enable, 4); + memcpy(&payload[4], &baudrate, 4); + memcpy(&payload[8], bl808_clock_para_bin, clock_para_size); + + blisp_return_t ret = blisp_send_command(device, 0x22, payload, payload_size, true); + if (ret < 0) + return ret; + ret = blisp_receive_response(device, false); + if (ret < 0) + return ret; + + return BLISP_OK; +} diff --git a/tools/blisp/src/common.c b/tools/blisp/src/common.c index f1e1c4a..1595c56 100644 --- a/tools/blisp/src/common.c +++ b/tools/blisp/src/common.c @@ -86,6 +86,15 @@ blisp_return_t blisp_common_prepare_flash(struct blisp_device* device) { boot_info.chip_id[3], boot_info.chip_id[4], boot_info.chip_id[5], boot_info.chip_id[6], boot_info.chip_id[7]); + if (device->chip->type == BLISP_CHIP_BL808) { + printf("Setting clock parameters ...\n"); + ret = bl808_load_clock_para(device, true, device->current_baud_rate); + if (ret != BLISP_OK) { + fprintf(stderr, "Failed to set clock parameters.\n"); + return ret; + } + } + if (device->chip->load_eflash_loader == NULL) { return BLISP_OK; }