stmisp适配ESP32串口设置延时问题

This commit is contained in:
OPTOC
2025-10-14 10:21:56 +08:00
parent e7989c7444
commit b59b115a3f
2 changed files with 43 additions and 13 deletions

View File

@@ -1,7 +1,6 @@
#include "stmisp.h"
#include <termios.h>
#include <sys/time.h>
extern int uart_fd;
// Utility: XOR checksum of bytes
uint8_t xor_checksum(const uint8_t *buf, size_t len) {
uint8_t cs = 0;
@@ -58,7 +57,7 @@ int wait_ack(stmisp_device_t *stmisp_device, uint32_t timeout)
// struct timeval t1;
// gettimeofday(&t1, NULL);
// elapsed = (t1.tv_sec - t0.tv_sec) + (t1.tv_usec - t0.tv_usec) / 1e6;
// }
// }
// return 0; // timeout
// }
@@ -66,8 +65,7 @@ int send_cmd_wait(stmisp_device_t *stmisp_device, uint8_t cmd, uint32_t timeout)
{
uint8_t pkt[2];
pkt[0] = cmd; pkt[1] = cmd ^ 0xFF;
tcflush(uart_fd, TCIOFLUSH);
stmisp_device->send(pkt, 2, 1000);
stmisp_device->send(pkt, 2, 0);
uint8_t ack = wait_ack(stmisp_device, timeout);
switch(ack)
@@ -81,7 +79,7 @@ int send_cmd_wait(stmisp_device_t *stmisp_device, uint8_t cmd, uint32_t timeout)
default:
break;
}
printf("stmisp: No ACK to cmd 0x%02x (timeout)\n", cmd);
printf("stmisp: No ACK to cmd 0x%02x (timeout)\n", cmd);
return 0;
}
@@ -90,7 +88,7 @@ int send_sync(stmisp_device_t *stmisp_device, uint32_t retries)
for(int i = 0; i < retries; i++)
{
uint8_t sync = SYNC_BYTE;
stmisp_device->send(&sync, 1, 1000);
stmisp_device->send(&sync, 1, 0);
if(wait_ack(stmisp_device, 1000) == 1)
{
return 1;
@@ -149,6 +147,24 @@ int cmd_getid(stmisp_device_t *stmisp_device)
return 0;
}
int cmd_go(stmisp_device_t *stmisp_device, uint32_t adder)
{
uint8_t ack = send_cmd_wait(stmisp_device, GO_CMD, 1000);
if(ack == 1)
{
uint8_t addrb[4] = { (uint8_t)(adder >> 24), (uint8_t)(adder >> 16), (uint8_t)(adder >> 8), (uint8_t)(adder >> 0) };
uint8_t cs_addr = xor_checksum(addrb, 4);
uint8_t pkt[5]; memcpy(pkt, addrb, 4); pkt[4] = cs_addr;
stmisp_device->send(pkt, 5, 0);
if(wait_ack(stmisp_device, 1000) == 1)
{
return 1;
}
}
return 0;
}
int cmd_extended_erase_mass(stmisp_device_t *stmisp_device)
{
uint8_t ack = send_cmd_wait(stmisp_device, ERASE_CMD, 1000);
@@ -156,7 +172,7 @@ int cmd_extended_erase_mass(stmisp_device_t *stmisp_device)
{
uint8_t payload[3];
payload[0] = 0xFF; payload[1] = 0xFF; payload[2] = xor_checksum(payload, 2);
stmisp_device->send(&payload, 3, 1000);
stmisp_device->send(&payload, 3, 0);
if(wait_ack(stmisp_device, 60000) == 1)
{
return 1;
@@ -178,7 +194,7 @@ int cmd_write_unprotect(stmisp_device_t *stmisp_device)
return 0;
}
int cmd_read_protect(stmisp_device_t *stmisp_device)
int cmd_read_unprotect(stmisp_device_t *stmisp_device)
{
uint8_t ack = send_cmd_wait(stmisp_device, READ_UN_CMD, 1000);
if(ack == 1)
@@ -206,7 +222,7 @@ int cmd_write_memory(stmisp_device_t *stmisp_device, uint32_t addr, const uint8_
uint8_t cs_addr = xor_checksum(addrb, 4);
uint8_t pkt[5]; memcpy(pkt, addrb, 4); pkt[4] = cs_addr;
stmisp_device->send(pkt, 5, 1000);
stmisp_device->send(pkt, 5, 0);
if(wait_ack(stmisp_device, 1000) != 1) return 0;
@@ -215,9 +231,9 @@ int cmd_write_memory(stmisp_device_t *stmisp_device, uint32_t addr, const uint8_
uint8_t cs = nfield;
for (size_t i = 0; i < chunk; ++i) cs ^= data[idx + i];
stmisp_device->send(&nfield, 1, 1000);
stmisp_device->send((void *)&data[idx], chunk, 1000);
stmisp_device->send(&cs, 1, 1000);
stmisp_device->send(&nfield, 1, 0);
stmisp_device->send((void *)&data[idx], chunk, 0);
stmisp_device->send(&cs, 1, 0);
if(wait_ack(stmisp_device, 5000) != 1) return 0;
//是否需要校验写入的数据块
@@ -229,4 +245,4 @@ int cmd_write_memory(stmisp_device_t *stmisp_device, uint32_t addr, const uint8_
}
}
return 1;
}
}

View File

@@ -19,6 +19,8 @@
#define READ_UN_CMD 0x92
#define WRITE_CMD 0x31
#define GO_CMD 0x21
typedef struct
{
uint8_t version; //目标buildroot版本
@@ -32,6 +34,8 @@ typedef struct
int (*get_length )(void); // 获取数据长度
uint8_t flag; //启动标志位
uint32_t adder_offset; //地址偏移
}stmisp_device_t;
/**
@@ -69,6 +73,11 @@ int cmd_get(stmisp_device_t *stmisp_device);
*/
int cmd_getid(stmisp_device_t *stmisp_device);
/**
* @brief isp实现go命令
*/
int cmd_go(stmisp_device_t *stmisp_device, uint32_t adder);
/**
* @brief isp设置写保护
*/
@@ -111,6 +120,11 @@ int cmd_read_memory(stmisp_device_t *stmisp_device, uint32_t addr, uint32_t leng
int cmd_write_memory(stmisp_device_t *stmisp_device, uint32_t addr, const uint8_t *data, size_t len, bool verify);
/**
* @brief 运行示例
*/
void Examples_run(void);
int stmisp_send(void* data, uint16_t len, int timeout);
int stmisp_recv(void* data, uint16_t len, int timeout);
int stmisp_get_length(void);