Skip to content

Commit

Permalink
Merge branch 'more_macros'
Browse files Browse the repository at this point in the history
  • Loading branch information
henols committed Feb 5, 2025
2 parents dc6d7c9 + 2e87abb commit b07e015
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 53 deletions.
22 changes: 21 additions & 1 deletion include/firestarter.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#define FW_VERSION VERSION ":" BOARD_NAME

#define DATA_BUFFER_SIZE 512
#define RESPONSE_MSG_SIZE 64

#define STATE_IDLE 0
#define STATE_READ 1
Expand Down Expand Up @@ -49,6 +50,25 @@
#define FLAG_OUTPUT_ENABLE 0x20
#define FLAG_CHIP_ENABLE 0x40

#define firestarter_warning_response(msg) \
firestarter_set_responce(RESPONSE_CODE_WARNING, msg)

#define firestarter_warning_response_format( msg, ...) \
firestarter_response_format(RESPONSE_CODE_WARNING, msg, __VA_ARGS__)

#define firestarter_error_response(msg) \
firestarter_set_responce(RESPONSE_CODE_ERROR,msg)

#define firestarter_error_response_format( msg, ...) \
firestarter_response_format(RESPONSE_CODE_ERROR, msg, __VA_ARGS__)

#define firestarter_set_responce( code, msg) \
copy_to_buffer(handle->response_msg, msg); \
handle->response_code = code;

#define firestarter_response_format(code, msg, ...) \
{format(handle->response_msg, msg, __VA_ARGS__);} \
handle->response_code = code;

#define is_flag_set(flag) \
((handle->ctrl_flags & flag) == flag)
Expand All @@ -67,7 +87,7 @@ typedef struct firestarter_handle {
uint8_t state;
uint8_t init;
uint8_t response_code;
char response_msg[64];
char response_msg[RESPONSE_MSG_SIZE];
uint8_t mem_type;
uint32_t protocol;
uint8_t pins;
Expand Down
6 changes: 2 additions & 4 deletions include/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@
log_data(handle->response_msg)



#define log_warn(msg) \
rurp_log(LOG_WARN_MSG, msg)

Expand Down Expand Up @@ -98,15 +97,14 @@
log_error(buf)


#define copy_to_buffer( buf, msg) \
#define copy_to_buffer(buf, msg) \
strcpy_P(buf, PSTR(msg)); \

#define format(buf, cformat, ...) \
char msg[80]; \
char msg[RESPONSE_MSG_SIZE]; \
copy_to_buffer(msg, cformat);\
sprintf(buf, msg, __VA_ARGS__)


#ifdef SERIAL_DEBUG
extern char* debug_msg_buffer;

Expand Down
1 change: 0 additions & 1 deletion src/firestarter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ void command_done(firestarter_handle_t* handle) {
debug("State done");
rurp_set_programmer_mode();
rurp_chip_disable();
// rurp_set_control_pin(OUTPUT_ENABLE, 1);
rurp_write_to_register(CONTROL_REGISTER, 0x00);
rurp_write_to_register(LEAST_SIGNIFICANT_BYTE, 0x00);
rurp_write_to_register(MOST_SIGNIFICANT_BYTE, 0x00);
Expand Down
21 changes: 10 additions & 11 deletions src/hardware_operations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ bool hw_read_voltage(firestarter_handle_t* handle) {
return false;
}

if (handle->init) {
if (handle->init) {
debug("Read voltage");
int res = op_execute_init(hw_init_read_voltage, handle);
if (res <= 0) {
Expand All @@ -37,7 +37,7 @@ bool hw_read_voltage(firestarter_handle_t* handle) {
}

bool fw_get_version(firestarter_handle_t* handle) {
// rurp_get_hardware_revision();
// rurp_get_hardware_revision();
debug("Get FW version");
log_ok_const(FW_VERSION);
return true;
Expand Down Expand Up @@ -67,27 +67,26 @@ bool hw_get_config(firestarter_handle_t* handle) {
return true;
}


void hw_init_read_voltage(firestarter_handle_t* handle) {
debug("Init read voltage");
if (rurp_get_hardware_revision() == REVISION_0) {
copy_to_buffer(handle->response_msg,"Rev0 dont support reading VPP/VPE");
handle->response_code = RESPONSE_CODE_ERROR;
return;
}
if (rurp_get_hardware_revision() == REVISION_0) {
firestarter_error_response("Rev0 dont support reading VPP/VPE");
return;
}

if (handle->state == STATE_READ_VPP) {
debug("Setting up VPP");
rurp_write_to_register(CONTROL_REGISTER, REGULATOR | VPE_TO_VPP); // Enable regulator and drop voltage to VPP
copy_to_buffer(handle->response_msg, "Setting up VPP");
return;
}
else if (handle->state == STATE_READ_VPE) {
debug("Setting up VPE");
rurp_write_to_register(CONTROL_REGISTER, REGULATOR); // Enable regulator
} else {
handle->response_code = RESPONSE_CODE_ERROR;
}
copy_to_buffer(handle->response_msg, "Error state");
else {
firestarter_error_response("Error state");
}
}

#ifdef HARDWARE_REVISION
Expand Down
3 changes: 1 addition & 2 deletions src/json_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,7 @@ int json_parse(char* json, jsmntok_t* tokens, int token_count, firestarter_handl
i += consumed_tokens + 1;
}
else {
handle->response_code = RESPONSE_CODE_ERROR;
format(handle->response_msg, "Unknown field: %s", json + tokens[i].start);
firestarter_error_response_format("Unknown field: %s", json + tokens[i].start);
return -1;
}
}
Expand Down
15 changes: 6 additions & 9 deletions src/proms/eprom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,7 @@ void eprom_write_execute(firestarter_handle_t* handle) {
debug("Mismatch, retrying");
}
handle->firestarter_set_control_register(handle, REGULATOR, 0);
format(handle->response_msg, "Failed to write memory, at 0x%06x, nr %d", handle->address, mismatch);
handle->response_code = RESPONSE_CODE_ERROR;
firestarter_error_response_format("Failed to write memory, at 0x%06x, nr %d", handle->address, mismatch);
}

// Use this function to set the control register and flip VPE_ENABLE bit to VPE_ENABLE or P1_VPP_ENABLE
Expand Down Expand Up @@ -187,8 +186,7 @@ void eprom_check_vpp(firestarter_handle_t* handle) {
debug("Check VPP");
#ifdef HARDWARE_REVISION
if (rurp_get_hardware_revision() == REVISION_0) {
handle->response_code = RESPONSE_CODE_WARNING;
copy_to_buffer(handle->response_msg, "Rev0 dont support reading VPP.");
firestarter_warning_response("Rev0 dont support reading VPP/VPE");
return;
}
#endif
Expand All @@ -213,16 +211,16 @@ void eprom_check_vpp(firestarter_handle_t* handle) {
dtostrf(vpp, 2, 2, vStr);
char rStr[6];
dtostrf(handle->vpp, 2, 2, rStr);
handle->response_code = is_flag_set(FLAG_FORCE) ? RESPONSE_CODE_WARNING : RESPONSE_CODE_ERROR;
int response_code = is_flag_set(FLAG_FORCE) ? RESPONSE_CODE_WARNING : RESPONSE_CODE_ERROR;
format(handle->response_msg, "VPP voltage is too high: %sv expected: %sv", vStr, rStr);
firestarter_response_format(response_code, "VPP voltage is too high: %sv expected: %sv", vStr, rStr);
}
else if (vpp < handle->vpp * .95) {
char vStr[6];
dtostrf(vpp, 2, 2, vStr);
char rStr[6];
dtostrf(handle->vpp, 2, 2, rStr);
handle->response_code = RESPONSE_CODE_WARNING;
format(handle->response_msg, "VPP voltage is low: %sv expected: %sv", vStr, rStr);
firestarter_warning_response_format("VPP voltage is low: %sv expected: %sv", vStr, rStr);
}
handle->firestarter_set_control_register(handle, REGULATOR | VPE_TO_VPP, 0);
}
Expand Down Expand Up @@ -259,7 +257,6 @@ void eprom_internal_check_chip_id(firestarter_handle_t* handle, uint8_t error_co
debug("Check chip ID");
uint16_t chip_id = eprom_get_chip_id(handle);
if (chip_id != handle->chip_id) {
handle->response_code = error_code;
format(handle->response_msg, "Chip ID %#x dont match expected ID %#x", chip_id, handle->chip_id);
firestarter_response_format(error_code, "Chip ID %#x dont match expected ID %#x", chip_id, handle->chip_id);
}
}
4 changes: 2 additions & 2 deletions src/proms/flash_type_3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ void flash3_erase_execute(firestarter_handle_t* handle) {
void flash3_check_chip_id_execute(firestarter_handle_t* handle) {
uint16_t chip_id = flash3_get_chip_id(handle);
if (chip_id != handle->chip_id) {
handle->response_code = is_flag_set(FLAG_FORCE) ? RESPONSE_CODE_WARNING : RESPONSE_CODE_ERROR;
format(handle->response_msg, "Chip ID %#04x dont match expected ID %#04x", chip_id, handle->chip_id);
int response_code = is_flag_set(FLAG_FORCE) ? RESPONSE_CODE_WARNING : RESPONSE_CODE_ERROR;
firestarter_response_format(response_code, "Chip ID %#04x dont match expected ID %#04x", chip_id, handle->chip_id);
}
}

Expand Down
11 changes: 5 additions & 6 deletions src/proms/flash_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,13 @@ void f_util_verify_operation(firestarter_handle_t* handle, uint8_t expected_data
// Verify completion with an additional read
if ((fu_flash_data_poll() & 0x80) == (fu_flash_data_poll() & 0x80)) { //No assuming other bits
rurp_set_data_as_output();
rurp_set_control_pin(CHIP_ENABLE, 1);
rurp_set_control_pin(OUTPUT_ENABLE, 1);
rurp_chip_disable();
rurp_chip_input();
return; // Operation completed successfully
}
// }
}
handle->response_code = RESPONSE_CODE_ERROR;
copy_to_buffer(handle->response_msg, "Operation timed out");
firestarter_error_response("Operation timed out");
return;
}

Expand All @@ -52,8 +51,8 @@ void fu_flash_flip_data(firestarter_handle_t* handle, uint32_t address, uint8_t
fu_flash_fast_address(handle, address);
rurp_write_data_buffer(data);
rurp_chip_input();
rurp_set_control_pin(CHIP_ENABLE, 0);
rurp_set_control_pin(CHIP_ENABLE, 1);
rurp_chip_enable();
rurp_chip_disable();
}

void fu_flash_fast_address(firestarter_handle_t* handle, uint32_t address) {
Expand Down
23 changes: 6 additions & 17 deletions src/proms/memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void configure_memory(firestarter_handle_t* handle) {

handle->firestarter_set_control_register = memory_set_control_register;
handle->firestarter_get_control_register = memory_get_control_register;
handle->response_code = RESPONSE_CODE_OK;

#ifdef POWER_THROUGH_ADDRESS_LINES
m_util_set_address(handle, 0);
#endif
Expand All @@ -72,9 +72,7 @@ void configure_memory(firestarter_handle_t* handle) {
configure_flash3(handle);
return;
}
format(handle->response_msg, "Memory type 0x%02x not supported", handle->mem_type);
handle->response_code = RESPONSE_CODE_ERROR;
return;
firestarter_error_response_format("Memory type 0x%02x not supported", handle->mem_type);
}

void memory_set_control_register(firestarter_handle_t* handle, register_t bit, bool state) {
Expand Down Expand Up @@ -124,34 +122,27 @@ void m_util_set_address(firestarter_handle_t* handle, uint32_t address) {
}

void memory_read_execute(firestarter_handle_t* handle) {
// rurp_set_control_pin(CHIP_ENABLE, 0);
int buf_size = min(handle->mem_size - handle->address, DATA_BUFFER_SIZE);
debug_format("Reading from address 0x%06x", handle->address);
for (int i = 0; i < buf_size; i++) {
uint8_t data = handle->firestarter_get_data(handle, handle->address + i);
// debug_format("Data 0x%02x %c", data, data);
handle->data_buffer[i] = data;
}
// rurp_set_control_pin(CHIP_ENABLE, 1);
handle->data_size = buf_size;
}

uint8_t memory_get_data(firestarter_handle_t* handle, uint32_t address) {
// rurp_set_control_pin(OUTPUT_ENABLE, 0);
rurp_chip_output();
address = m_util_remap_address_bus(handle, address, READ_FLAG);

handle->firestarter_set_address(handle, address);
rurp_set_data_as_input();
// rurp_set_control_pin(CHIP_ENABLE , 0);
rurp_chip_enable();
delayMicroseconds(3);
uint8_t data = rurp_read_data_buffer();
rurp_chip_disable();

// rurp_set_control_pin(CHIP_ENABLE , 1);
// rurp_set_data_as_output();

return data;
}

Expand All @@ -167,18 +158,17 @@ void memory_set_data(firestarter_handle_t* handle, uint32_t address, uint8_t dat
handle->firestarter_set_address(handle, address);
rurp_write_data_buffer(data);
delayMicroseconds(3); //Needed for slower address changes like slow ROMs and "Power through address lines"
rurp_set_control_pin(CHIP_ENABLE, 0);
rurp_chip_enable();
delayMicroseconds(handle->pulse_delay);
rurp_set_control_pin(CHIP_ENABLE, 1);
rurp_chip_disable();
}

void memory_verify_execute(firestarter_handle_t* handle) {
for (uint32_t i = 0; i < handle->data_size; i++) {
uint8_t byte = handle->firestarter_get_data(handle, handle->address + i);
uint8_t expected = handle->data_buffer[i];
if (byte != expected) {
format(handle->response_msg, "Expecting 0x%02x got 0x%02x at 0x%04x", expected, byte, handle->address + i);
handle->response_code = RESPONSE_CODE_ERROR;
firestarter_error_response_format("Expecting 0x%02x got 0x%02x at 0x%04x", expected, byte, handle->address + i);
return;
}
}
Expand Down Expand Up @@ -208,8 +198,7 @@ void m_util_blank_check(firestarter_handle_t* handle) {
for (uint32_t i = 0; i < handle->mem_size; i++) {
uint8_t val = handle->firestarter_get_data(handle, i);
if (val != 0xFF) {
handle->response_code = RESPONSE_CODE_ERROR;
format(handle->response_msg, "Memory is not blank, at 0x%06x, value: 0x%02x", i, val);
firestarter_error_response_format("Memory is not blank, at 0x%06x, value: 0x%02x", i, val);
return;
}
}
Expand Down

0 comments on commit b07e015

Please sign in to comment.