cleaning up serial.c

This commit is contained in:
Ben 2023-06-06 18:08:25 -04:00 committed by johnny9
parent 3d9d1692f2
commit b604e0ccc6
8 changed files with 33 additions and 33 deletions

View File

@ -62,7 +62,7 @@ static void _send_BM1397(uint8_t header, uint8_t * data, uint8_t data_len, bool
} }
//send serial data //send serial data
send_serial(buf, total_length, debug); SERIAL_send(buf, total_length, debug);
free(buf); free(buf);
} }
@ -263,10 +263,12 @@ void BM1397_set_default_baud(void){
_send_BM1397((TYPE_CMD | GROUP_ALL | CMD_WRITE), baudrate, 6, false); _send_BM1397((TYPE_CMD | GROUP_ALL | CMD_WRITE), baudrate, 6, false);
} }
void BM1397_set_max_baud(void){ int BM1397_set_max_baud(void){
// divider of 0 for 3,125,000 // divider of 0 for 3,125,000
ESP_LOGI(TAG, "Setting max baud");
unsigned char baudrate[9] = { 0x00, MISC_CONTROL, 0x00, 0x00, 0b01100000, 0b00110001 };; //baudrate - misc_control unsigned char baudrate[9] = { 0x00, MISC_CONTROL, 0x00, 0x00, 0b01100000, 0b00110001 };; //baudrate - misc_control
_send_BM1397((TYPE_CMD | GROUP_ALL | CMD_WRITE), baudrate, 6, false); _send_BM1397((TYPE_CMD | GROUP_ALL | CMD_WRITE), baudrate, 6, false);
return 3125000;
} }
void BM1397_set_job_difficulty_mask(int difficulty){ void BM1397_set_job_difficulty_mask(int difficulty){

View File

@ -67,7 +67,7 @@ void BM1397_init(void);
void BM1397_send_init(void); void BM1397_send_init(void);
void BM1397_send_work(struct job_packet *job); void BM1397_send_work(struct job_packet *job);
void BM1397_set_job_difficulty_mask(int); void BM1397_set_job_difficulty_mask(int);
void BM1397_set_max_baud(void); int BM1397_set_max_baud(void);
void BM1397_set_default_baud(void); void BM1397_set_default_baud(void);
#endif /* BM1397_H_ */ #endif /* BM1397_H_ */

View File

@ -3,11 +3,11 @@
#define CHUNK_SIZE 1024 #define CHUNK_SIZE 1024
int send_serial(uint8_t *, int, bool); int SERIAL_send(uint8_t *, int, bool);
void init_serial(void); void SERIAL_init(void);
void debug_serial_rx(void); void SERIAL_debug_rx(void);
int16_t serial_rx(uint8_t *, uint16_t, uint16_t); int16_t SERIAL_rx(uint8_t *, uint16_t, uint16_t);
void clear_serial_buffer(void); void SERIAL_clear_buffer(void);
void set_max_baud(void); void SERIAL_set_baud(int baud);
#endif /* SERIAL_H_ */ #endif /* SERIAL_H_ */

View File

@ -19,7 +19,7 @@
static const char *TAG = "serial"; static const char *TAG = "serial";
void init_serial(void) { void SERIAL_init(void) {
ESP_LOGI(TAG, "Initializing serial"); ESP_LOGI(TAG, "Initializing serial");
//Configure UART1 parameters //Configure UART1 parameters
uart_config_t uart_config = { uart_config_t uart_config = {
@ -41,14 +41,12 @@ void init_serial(void) {
uart_driver_install(UART_NUM_1, BUF_SIZE * 2, 0, 0, NULL, 0); uart_driver_install(UART_NUM_1, BUF_SIZE * 2, 0, 0, NULL, 0);
} }
void set_max_baud(void){ void SERIAL_set_baud(int baud){
ESP_LOGI("SERIAL", "SETTING CHIP MAX BAUD"); ESP_LOGI(TAG, "Changing UART baud to %i", baud);
BM1397_set_max_baud(); uart_set_baudrate(UART_NUM_1, baud);
ESP_LOGI("SERIAL", "SETTING UART MAX BAUD");
uart_set_baudrate(UART_NUM_1, 3125000);
} }
int send_serial(uint8_t *data, int len, bool debug) { int SERIAL_send(uint8_t *data, int len, bool debug) {
if (debug) { if (debug) {
printf("->"); printf("->");
prettyHex((unsigned char*)data, len); prettyHex((unsigned char*)data, len);
@ -62,7 +60,7 @@ int send_serial(uint8_t *data, int len, bool debug) {
/// @param buf buffer to read data into /// @param buf buffer to read data into
/// @param buf number of ms to wait before timing out /// @param buf number of ms to wait before timing out
/// @return number of bytes read, or -1 on error /// @return number of bytes read, or -1 on error
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) {
int16_t bytes_read = uart_read_bytes(UART_NUM_1, buf, size, timeout_ms / portTICK_PERIOD_MS); int16_t bytes_read = uart_read_bytes(UART_NUM_1, buf, size, timeout_ms / portTICK_PERIOD_MS);
// if (bytes_read > 0) { // if (bytes_read > 0) {
// printf("rx: "); // printf("rx: ");
@ -72,11 +70,11 @@ int16_t serial_rx(uint8_t * buf, uint16_t size, uint16_t timeout_ms) {
return bytes_read; return bytes_read;
} }
void debug_serial_rx(void) { void SERIAL_debug_rx(void) {
int ret; int ret;
uint8_t buf[CHUNK_SIZE]; uint8_t buf[CHUNK_SIZE];
ret = serial_rx(buf, 100, 20); ret = SERIAL_rx(buf, 100, 20);
if (ret < 0) { if (ret < 0) {
fprintf(stderr, "unable to read data\n"); fprintf(stderr, "unable to read data\n");
return; return;
@ -86,6 +84,6 @@ void debug_serial_rx(void) {
} }
void clear_serial_buffer(void) { void SERIAL_clear_buffer(void) {
uart_flush(UART_NUM_1); uart_flush(UART_NUM_1);
} }

View File

@ -11,13 +11,13 @@ TEST_CASE("Check known working midstate + job command", "[bm1397]")
{ {
if (!uart_initialized) if (!uart_initialized)
{ {
init_serial(); SERIAL_init();
uart_initialized = 1; uart_initialized = 1;
BM1397_init(); BM1397_init();
// read back response // read back response
debug_serial_rx(); SERIAL_debug_rx();
} }
@ -37,7 +37,7 @@ TEST_CASE("Check known working midstate + job command", "[bm1397]")
memset(buf, 0, 1024); memset(buf, 0, 1024);
BM1397_send_work(&test_job); BM1397_send_work(&test_job);
uint16_t received = serial_rx(buf, 9, 20); uint16_t received = SERIAL_rx(buf, 9, 20);
TEST_ASSERT_GREATER_OR_EQUAL_UINT16(sizeof(struct nonce_response), received); TEST_ASSERT_GREATER_OR_EQUAL_UINT16(sizeof(struct nonce_response), received);
int i; int i;

View File

@ -61,7 +61,7 @@ static bm1397Module BM1397_MODULE;
static void ASIC_task(void * pvParameters) static void ASIC_task(void * pvParameters)
{ {
init_serial(); SERIAL_init();
BM1397_init(); BM1397_init();
@ -79,7 +79,8 @@ static void ASIC_task(void * pvParameters)
uint32_t prev_nonce = 0; uint32_t prev_nonce = 0;
set_max_baud(); int baud = BM1397_set_max_baud();
SERIAL_set_baud(baud);
SYSTEM_notify_mining_started(&SYSTEM_MODULE); SYSTEM_notify_mining_started(&SYSTEM_MODULE);
ESP_LOGI(TAG, "Mining!"); ESP_LOGI(TAG, "Mining!");
@ -106,11 +107,11 @@ static void ASIC_task(void * pvParameters)
valid_jobs[job.job_id] = 1; valid_jobs[job.job_id] = 1;
pthread_mutex_unlock(&valid_jobs_lock); pthread_mutex_unlock(&valid_jobs_lock);
clear_serial_buffer(); SERIAL_clear_buffer();
BM1397_send_work(&job); //send the job to the ASIC BM1397_send_work(&job); //send the job to the ASIC
//wait for a response //wait for a response
int received = serial_rx(buf, 9, BM1397_FULLSCAN_MS); int received = SERIAL_rx(buf, 9, BM1397_FULLSCAN_MS);
if (received < 0) { if (received < 0) {
ESP_LOGI(TAG, "Error in serial RX"); ESP_LOGI(TAG, "Error in serial RX");
@ -368,7 +369,7 @@ void app_main(void)
ESP_ERROR_CHECK(esp_netif_init()); ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default()); ESP_ERROR_CHECK(esp_event_loop_create_default());
xTaskCreate(SYSTEM_task, "SYSTEM_task", 4096, &SYSTEM_MODULE, 10, &sysTaskHandle); xTaskCreate(SYSTEM_task, "SYSTEM_task", 4096, (void*)&SYSTEM_MODULE, 10, &sysTaskHandle);
/* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig. /* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
* Read "Establishing Wi-Fi or Ethernet Connection" section in * Read "Establishing Wi-Fi or Ethernet Connection" section in

View File

@ -180,10 +180,9 @@ static void _update_system_performance(SystemModule* module){
} }
void SYSTEM_task(SystemModule* module) { void SYSTEM_task(void *parameters) {
SystemModule *module = (SystemModule*)parameters;
_init_system(module); _init_system(module);
while(1){ while(1){

View File

@ -14,10 +14,10 @@ typedef struct {
uint16_t shares_accepted; uint16_t shares_accepted;
uint16_t shares_rejected; uint16_t shares_rejected;
int screen_page; int screen_page;
char oled_buf[20] char oled_buf[20];
} SystemModule; } SystemModule;
void SYSTEM_task(SystemModule* module); void SYSTEM_task(void *parameters);
void SYSTEM_notify_accepted_share(SystemModule* module); void SYSTEM_notify_accepted_share(SystemModule* module);