Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serial Synchronization #24 #48

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 1 addition & 18 deletions components/bm1397/bm1366.c
Original file line number Diff line number Diff line change
Expand Up @@ -633,24 +633,7 @@ void BM1366_send_work(void * pvParameters, bm_job * next_bm_job)

asic_result * BM1366_receive_work(void)
{
// wait for a response, wait time is pretty arbitrary
int received = SERIAL_rx(asic_response_buffer, 11, 60000);

if (received < 0) {
ESP_LOGI(TAG, "Error in serial RX");
return NULL;
} else if (received == 0) {
// Didn't find a solution, restart and try again
return NULL;
}

if (received != 11 || asic_response_buffer[0] != 0xAA || asic_response_buffer[1] != 0x55) {
ESP_LOGI(TAG, "Serial RX invalid %i", received);
ESP_LOG_BUFFER_HEX(TAG, asic_response_buffer, received);
return NULL;
}

return (asic_result *) asic_response_buffer;
return SERIAL_rx_aa55(asic_response_buffer,11);
}

uint16_t reverse_uint16(uint16_t num)
Expand Down
24 changes: 1 addition & 23 deletions components/bm1397/bm1397.c
Original file line number Diff line number Diff line change
Expand Up @@ -378,29 +378,7 @@ void BM1397_send_work(void *pvParameters, bm_job *next_bm_job)

asic_result *BM1397_receive_work(void)
{

// wait for a response, wait time is pretty arbitrary
int received = SERIAL_rx(asic_response_buffer, 9, 60000);

if (received < 0)
{
ESP_LOGI(TAG, "Error in serial RX");
return NULL;
}
else if (received == 0)
{
// Didn't find a solution, restart and try again
return NULL;
}

if (received != 9 || asic_response_buffer[0] != 0xAA || asic_response_buffer[1] != 0x55)
{
ESP_LOGI(TAG, "Serial RX invalid %i", received);
ESP_LOG_BUFFER_HEX(TAG, asic_response_buffer, received);
return NULL;
}

return (asic_result *)asic_response_buffer;
return SERIAL_rx_aa55(asic_response_buffer,9);
}

task_result *BM1397_proccess_work(void *pvParameters)
Expand Down
5 changes: 4 additions & 1 deletion components/bm1397/include/serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ int16_t SERIAL_rx(uint8_t *, uint16_t, uint16_t);
void SERIAL_clear_buffer(void);
void SERIAL_set_baud(int baud);

#endif /* SERIAL_H_ */
// recieve packet with 0xaa 0x55 header
void *SERIAL_rx_aa55(uint8_t *data,const int length);

#endif /* SERIAL_H_ */
40 changes: 40 additions & 0 deletions components/bm1397/serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,43 @@ void SERIAL_clear_buffer(void)
{
uart_flush(UART_NUM_1);
}

/**
* @brief recieve packet with 0xaa 0x55 header
* @param data - buffer for received serial data
* @param length - length of expected packet
*/
void *SERIAL_rx_aa55(uint8_t *data,const int length) {
for(int len=0,cnt=0;len<length;++cnt) {
// wait for a response, wait time is pretty arbitrary
int received = SERIAL_rx(data+len,length-len, 60000);
if (received < 0) {
ESP_LOGI(TAG, "Error in serial RX");
return NULL;
} else if (received == 0) {
// Didn't find a solution, restart and try again
return NULL;
}

if(len+received>2) {
// valid start
if(data[0] == 0xAA && data[1] == 0x55) {
tommywatson marked this conversation as resolved.
Show resolved Hide resolved
len+=received;
tommywatson marked this conversation as resolved.
Show resolved Hide resolved
}
else {
for(int count=1; count < len+received; ++count) {
tommywatson marked this conversation as resolved.
Show resolved Hide resolved
tommywatson marked this conversation as resolved.
Show resolved Hide resolved
if(*(data+count) == 0xAA) {
// move to head and adjust read length
memmove(data, data+count, len+received-count);
len+=received-count;
break;
}
}
}
}
tommywatson marked this conversation as resolved.
Show resolved Hide resolved
else {
len+=received;
}
}
return data;
}