removed global receive buffer to not loose nonces

This commit is contained in:
shufps 2024-09-13 19:09:52 +02:00
parent def93a8579
commit 087253656f
No known key found for this signature in database
GPG Key ID: 371CB8C24D8CB455
6 changed files with 39 additions and 44 deletions

View File

@ -48,9 +48,6 @@ typedef struct __attribute__((__packed__))
static const char *TAG = "bm1397Module"; static const char *TAG = "bm1397Module";
static uint8_t asic_response_buffer[CHUNK_SIZE];
static task_result result;
uint32_t increment_bitmask(const uint32_t value, const uint32_t mask); uint32_t increment_bitmask(const uint32_t value, const uint32_t mask);
/// @brief /// @brief
@ -203,10 +200,11 @@ static uint8_t _send_init(uint64_t frequency, uint16_t asic_count)
{ {
// send the init command // send the init command
_send_read_address(); _send_read_address();
uint8_t buf[11] = {0};
int chip_counter = 0; int chip_counter = 0;
while (true) { while (true) {
int received = SERIAL_rx(asic_response_buffer, 11, 1000); int received = SERIAL_rx(buf, 11, 1000);
if (received > 0) { if (received > 0) {
//ESP_LOG_BUFFER_HEX(TAG, asic_response_buffer, received); //ESP_LOG_BUFFER_HEX(TAG, asic_response_buffer, received);
chip_counter++; chip_counter++;
@ -271,8 +269,6 @@ uint8_t BM1397_init(uint64_t frequency, uint16_t asic_count)
{ {
Serial.println("Initializing BM1397"); Serial.println("Initializing BM1397");
memset(asic_response_buffer, 0, sizeof(asic_response_buffer));
gpio_set_direction(NERD_NOS_GPIO_PEN, GPIO_MODE_OUTPUT); gpio_set_direction(NERD_NOS_GPIO_PEN, GPIO_MODE_OUTPUT);
gpio_set_level(NERD_NOS_GPIO_PEN, 1); gpio_set_level(NERD_NOS_GPIO_PEN, 1);
@ -361,46 +357,47 @@ void BM1397_send_work(bm_job_t *next_bm_job, uint8_t job_id)
_send_BM1397((TYPE_JOB | GROUP_SINGLE | CMD_WRITE), (uint8_t*) &job, sizeof(job_packet_t), BM1397_DEBUG_WORK); _send_BM1397((TYPE_JOB | GROUP_SINGLE | CMD_WRITE), (uint8_t*) &job, sizeof(job_packet_t), BM1397_DEBUG_WORK);
} }
asic_result *BM1397_receive_work(uint16_t timeout) bool BM1397_receive_work(uint16_t timeout, asic_result *result)
{ {
uint8_t *rcv_buf = (uint8_t*) result;
// wait for a response, wait time is pretty arbitrary // wait for a response, wait time is pretty arbitrary
int received = SERIAL_rx(asic_response_buffer, 9, timeout); int received = SERIAL_rx(rcv_buf, 9, timeout);
if (received < 0) if (received < 0)
{ {
Serial.println("Error in serial RX"); Serial.println("Error in serial RX");
return NULL; return false;
} }
else if (received == 0) else if (received == 0)
{ {
// Didn't find a solution, restart and try again // Didn't find a solution, restart and try again
return NULL; return false;
} }
if (received != 9 || asic_response_buffer[0] != 0xAA || asic_response_buffer[1] != 0x55) if (received != 9 || rcv_buf[0] != 0xAA || rcv_buf[1] != 0x55)
{ {
Serial.println("Serial RX invalid. Resetting receive buffer ..."); Serial.println("Serial RX invalid. Resetting receive buffer ...");
//ESP_LOG_BUFFER_HEX(TAG, asic_response_buffer, received); //ESP_LOG_BUFFER_HEX(TAG, asic_response_buffer, received);
SERIAL_clear_buffer(); SERIAL_clear_buffer();
return NULL; return false;
} }
return true;
return (asic_result *)asic_response_buffer;
} }
task_result *BM1397_proccess_work(uint32_t version, uint16_t timeout) bool BM1397_proccess_work(uint32_t version, uint16_t timeout, task_result *result)
{ {
asic_result *asic_result = BM1397_receive_work(timeout); asic_result asic_result;
if (asic_result == NULL) if (!BM1397_receive_work(timeout, &asic_result))
{ {
ESP_LOGI(TAG, "return null"); ESP_LOGI(TAG, "return null");
return NULL; return false;
} }
uint8_t rx_job_id = (asic_result->job_id & 0xfc) >> 2; uint8_t rx_job_id = (asic_result.job_id & 0xfc) >> 2;
uint8_t rx_midstate_index = asic_result->job_id & 0x03; uint8_t rx_midstate_index = asic_result.job_id & 0x03;
uint32_t rolled_version = version; uint32_t rolled_version = version;
for (int i = 0; i < rx_midstate_index; i++) for (int i = 0; i < rx_midstate_index; i++)
@ -408,10 +405,10 @@ task_result *BM1397_proccess_work(uint32_t version, uint16_t timeout)
rolled_version = increment_bitmask(rolled_version, 0x1fffe000); rolled_version = increment_bitmask(rolled_version, 0x1fffe000);
} }
result.job_id = rx_job_id; result->job_id = rx_job_id;
result.nonce = asic_result->nonce; result->nonce = asic_result.nonce;
result.rolled_version = rolled_version; result->rolled_version = rolled_version;
return &result; return true;
} }

View File

@ -17,5 +17,5 @@ void BM1397_set_job_difficulty_mask(int);
int BM1397_set_max_baud(void); int BM1397_set_max_baud(void);
int BM1397_set_default_baud(void); int BM1397_set_default_baud(void);
void BM1397_send_hash_frequency(float frequency); void BM1397_send_hash_frequency(float frequency);
task_result *BM1397_proccess_work(uint32_t version, uint16_t timeout); bool BM1397_proccess_work(uint32_t version, uint16_t timeout, task_result *result);

View File

@ -160,8 +160,8 @@ void nerdnos_send_work(bm_job_t *next_bm_job, uint8_t job_id) {
BM1397_send_work(next_bm_job, job_id); BM1397_send_work(next_bm_job, job_id);
} }
task_result *nerdnos_proccess_work(uint32_t version, uint16_t timeout) { bool nerdnos_proccess_work(uint32_t version, uint16_t timeout, task_result *result) {
return BM1397_proccess_work(version, timeout); return BM1397_proccess_work(version, timeout, result);
} }
void nerdnos_free_bm_job(bm_job_t *job) { void nerdnos_free_bm_job(bm_job_t *job) {

View File

@ -16,7 +16,7 @@ void nerdnos_create_job(mining_subscribe *mWorker, mining_job *job, bm_job_t *ne
void nerdnos_send_work(bm_job_t *next_bm_job, uint8_t job_id); void nerdnos_send_work(bm_job_t *next_bm_job, uint8_t job_id);
// receive and process responses // receive and process responses
task_result *nerdnos_proccess_work(uint32_t version, uint16_t timeout); bool nerdnos_proccess_work(uint32_t version, uint16_t timeout, task_result *result);
// test difficulty // test difficulty
double nerdnos_test_nonce_value(const bm_job_t *job, const uint32_t nonce, const uint32_t rolled_version, uint8_t hash_result[32]); double nerdnos_test_nonce_value(const bm_job_t *job, const uint32_t nonce, const uint32_t rolled_version, uint8_t hash_result[32]);

View File

@ -51,8 +51,8 @@ int SERIAL_send(uint8_t *data, int len, bool debug)
return uart_write_bytes(UART_NUM_1, (const char *)data, len); return uart_write_bytes(UART_NUM_1, (const char *)data, len);
} }
int SERIAL_check_for_data() { size_t SERIAL_check_for_data() {
int length; size_t length;
uart_get_buffered_data_len(UART_NUM_1, (size_t*)&length); uart_get_buffered_data_len(UART_NUM_1, (size_t*)&length);
return length; return length;
} }
@ -65,7 +65,9 @@ int SERIAL_check_for_data() {
int16_t SERIAL_rx(uint8_t *buf, uint16_t size, uint16_t timeout_ms) int16_t SERIAL_rx(uint8_t *buf, uint16_t size, uint16_t timeout_ms)
{ {
// don't return incomplete data // don't return incomplete data
if (SERIAL_check_for_data() < size) { size_t available = SERIAL_check_for_data();
if (available && available < size) {
Serial.printf("not returning parts of data ... %d vs %d\n", (int) available, (int) size);
return 0; return 0;
} }

View File

@ -172,15 +172,11 @@ void runASIC(void * task_id) {
// send the job and // send the job and
nerdnos_send_work(&asic_jobs[asic_job_id], asic_job_id); nerdnos_send_work(&asic_jobs[asic_job_id], asic_job_id);
// the pointer returned is the RS232 receive buffer :shushing-face: task_result result = {0};
// but we only have a single thread so it should be okay while (nerdnos_proccess_work(version, 1, &result)) {
// process all results if we have more than one
// this is okay because serial uses a buffer and (most likely^^) DMA
task_result *result = NULL;
while ((result = nerdnos_proccess_work(version, 1)) != NULL) {
// check if the ID is in the valid range and the slot is not empty // check if the ID is in the valid range and the slot is not empty
if (result->job_id >= ASIC_JOB_COUNT || !asic_jobs[result->job_id].ntime) { if (result.job_id >= ASIC_JOB_COUNT || !asic_jobs[result.job_id].ntime) {
Serial.printf("Invalid job ID or no job found for ID %02x\n", result->job_id); Serial.printf("Invalid job ID or no job found for ID %02x\n", result.job_id);
continue; continue;
} }
@ -188,9 +184,9 @@ void runASIC(void * task_id) {
// check the nonce difficulty // check the nonce difficulty
double diff_hash = nerdnos_test_nonce_value( double diff_hash = nerdnos_test_nonce_value(
&asic_jobs[result->job_id], &asic_jobs[result.job_id],
result->nonce, result.nonce,
result->rolled_version, result.rolled_version,
hash); hash);
// update best diff // update best diff
@ -199,14 +195,14 @@ void runASIC(void * task_id) {
} }
// calculate the hashrate // calculate the hashrate
if (diff_hash >= asic_jobs[result->job_id].pool_diff) { if (diff_hash >= asic_jobs[result.job_id].pool_diff) {
calculate_hashrate(&history, asic_jobs[result->job_id].pool_diff); calculate_hashrate(&history, asic_jobs[result.job_id].pool_diff);
Serial.printf("avg hashrate: %.2fGH/s (history spans %.2fs, %d shares)\n", history.avg_gh, history.duration, history.shares); Serial.printf("avg hashrate: %.2fGH/s (history spans %.2fs, %d shares)\n", history.avg_gh, history.duration, history.shares);
} }
if(diff_hash > mMiner.poolDifficulty) if(diff_hash > mMiner.poolDifficulty)
{ {
tx_mining_submit_asic(client, mWorker, &asic_jobs[result->job_id], result); tx_mining_submit_asic(client, mWorker, &asic_jobs[result.job_id], &result);
Serial.println("valid share!"); Serial.println("valid share!");
Serial.printf(" - Current diff share: %.3f\n", diff_hash); Serial.printf(" - Current diff share: %.3f\n", diff_hash);
Serial.printf(" - Current pool diff : %.3f\n", mMiner.poolDifficulty); Serial.printf(" - Current pool diff : %.3f\n", mMiner.poolDifficulty);