X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=hardware%2Fasix-sigma%2Fasix-sigma.c;h=50bb932db26030405a611ebd684c916a8ff02521;hb=86f5e3d826fc9059e110f07221cb1a30652ceac2;hp=935412e5c0bd58d62513b8c611e651ba6e303d02;hpb=8a2efef2d5900cb3dd935af92a0e22528660c4be;p=libsigrok.git diff --git a/hardware/asix-sigma/asix-sigma.c b/hardware/asix-sigma/asix-sigma.c index 935412e5..50bb932d 100644 --- a/hardware/asix-sigma/asix-sigma.c +++ b/hardware/asix-sigma/asix-sigma.c @@ -20,7 +20,7 @@ */ /* - * ASIX Sigma Logic Analyzer Driver + * ASIX SIGMA Logic Analyzer Driver */ #include "config.h" @@ -30,6 +30,7 @@ #include #include #include +#include #include "asix-sigma.h" #define USB_VENDOR 0xa600 @@ -43,23 +44,23 @@ static GSList *device_instances = NULL; static uint64_t supported_samplerates[] = { - KHZ(200), - KHZ(250), - KHZ(500), - MHZ(1), - MHZ(5), - MHZ(10), - MHZ(25), - MHZ(50), - MHZ(100), - MHZ(200), + SR_KHZ(200), + SR_KHZ(250), + SR_KHZ(500), + SR_MHZ(1), + SR_MHZ(5), + SR_MHZ(10), + SR_MHZ(25), + SR_MHZ(50), + SR_MHZ(100), + SR_MHZ(200), 0, }; -static struct samplerates samplerates = { - KHZ(200), - MHZ(200), - 0, +static struct sr_samplerates samplerates = { + SR_KHZ(200), + SR_MHZ(200), + SR_HZ(0), supported_samplerates, }; @@ -105,8 +106,8 @@ static int sigma_read(void *buf, size_t size, struct sigma *sigma) ret = ftdi_read_data(&sigma->ftdic, (unsigned char *)buf, size); if (ret < 0) { - g_warning("ftdi_read_data failed: %s", - ftdi_get_error_string(&sigma->ftdic)); + sr_warn("ftdi_read_data failed: %s", + ftdi_get_error_string(&sigma->ftdic)); } return ret; @@ -118,10 +119,10 @@ static int sigma_write(void *buf, size_t size, struct sigma *sigma) ret = ftdi_write_data(&sigma->ftdic, (unsigned char *)buf, size); if (ret < 0) { - g_warning("ftdi_write_data failed: %s", - ftdi_get_error_string(&sigma->ftdic)); + sr_warn("ftdi_write_data failed: %s", + ftdi_get_error_string(&sigma->ftdic)); } else if ((size_t) ret != size) { - g_warning("ftdi_write_data did not complete write\n"); + sr_warn("ftdi_write_data did not complete write\n"); } return ret; @@ -169,7 +170,7 @@ static uint8_t sigma_get_register(uint8_t reg, struct sigma *sigma) uint8_t value; if (1 != sigma_read_register(reg, &value, 1, sigma)) { - g_warning("Sigma_get_register: 1 byte expected"); + sr_warn("sigma_get_register: 1 byte expected"); return 0; } @@ -316,26 +317,31 @@ static int bin2bitbang(const char *filename, f = g_fopen(filename, "rb"); if (!f) { - g_warning("g_fopen(\"%s\", \"rb\")", filename); - return -1; + sr_warn("g_fopen(\"%s\", \"rb\")", filename); + return SR_ERR; } if (-1 == fseek(f, 0, SEEK_END)) { - g_warning("fseek on %s failed", filename); + sr_warn("fseek on %s failed", filename); fclose(f); - return -1; + return SR_ERR; } file_size = ftell(f); fseek(f, 0, SEEK_SET); - compressed_buf = g_malloc(file_size); - firmware = g_malloc(buffer_size); + if (!(compressed_buf = g_try_malloc(file_size))) { + sr_err("asix: %s: compressed_buf malloc failed", __func__); + fclose(f); + return SR_ERR_MALLOC; + } - if (!compressed_buf || !firmware) { - g_warning("Error allocating buffers"); - return -1; + if (!(firmware = g_try_malloc(buffer_size))) { + sr_err("asix: %s: firmware malloc failed", __func__); + fclose(f); + g_free(compressed_buf); + return SR_ERR_MALLOC; } csize = 0; @@ -350,19 +356,20 @@ static int bin2bitbang(const char *filename, if (ret < 0) { g_free(compressed_buf); g_free(firmware); - g_warning("Could not unpack Sigma firmware. (Error %d)\n", ret); - return -1; + sr_warn("Could not unpack Sigma firmware. (Error %d)\n", ret); + return SR_ERR; } g_free(compressed_buf); *buf_size = fwsize * 2 * 8; - *buf = p = (unsigned char *)g_malloc(*buf_size); - + *buf = p = (unsigned char *)g_try_malloc(*buf_size); if (!p) { - g_warning("Error allocating buffers"); - return -1; + sr_err("asix: %s: buf/p malloc failed", __func__); + g_free(compressed_buf); + g_free(firmware); + return SR_ERR_MALLOC; } for (i = 0; i < fwsize; ++i) { @@ -377,25 +384,28 @@ static int bin2bitbang(const char *filename, if (offset != *buf_size) { g_free(*buf); - g_warning("Error reading firmware %s " - "offset=%ld, file_size=%ld, buf_size=%zd\n", - filename, offset, file_size, *buf_size); + sr_warn("Error reading firmware %s " + "offset=%ld, file_size=%ld, buf_size=%zd\n", + filename, offset, file_size, *buf_size); - return -1; + return SR_ERR; } - return 0; + return SR_OK; } -static int hw_init(char *deviceinfo) +static int hw_init(const char *deviceinfo) { struct sr_device_instance *sdi; - struct sigma *sigma = g_malloc(sizeof(struct sigma)); + struct sigma *sigma; + /* Avoid compiler warnings. */ deviceinfo = deviceinfo; - if (!sigma) - return 0; + if (!(sigma = g_try_malloc(sizeof(struct sigma)))) { + sr_err("asix: %s: sigma malloc failed", __func__); + return 0; /* FIXME: Should be SR_ERR_MALLOC. */ + } ftdi_init(&sigma->ftdic); @@ -427,7 +437,7 @@ static int hw_init(char *deviceinfo) return 1; free: - free(sigma); + g_free(sigma); return 0; } @@ -443,21 +453,21 @@ static int upload_firmware(int firmware_idx, struct sigma *sigma) /* Make sure it's an ASIX SIGMA. */ if ((ret = ftdi_usb_open_desc(&sigma->ftdic, USB_VENDOR, USB_PRODUCT, USB_DESCRIPTION, NULL)) < 0) { - g_warning("ftdi_usb_open failed: %s", - ftdi_get_error_string(&sigma->ftdic)); + sr_warn("ftdi_usb_open failed: %s", + ftdi_get_error_string(&sigma->ftdic)); return 0; } if ((ret = ftdi_set_bitmode(&sigma->ftdic, 0xdf, BITMODE_BITBANG)) < 0) { - g_warning("ftdi_set_bitmode failed: %s", - ftdi_get_error_string(&sigma->ftdic)); + sr_warn("ftdi_set_bitmode failed: %s", + ftdi_get_error_string(&sigma->ftdic)); return 0; } /* Four times the speed of sigmalogan - Works well. */ if ((ret = ftdi_set_baudrate(&sigma->ftdic, 750000)) < 0) { - g_warning("ftdi_set_baudrate failed: %s", - ftdi_get_error_string(&sigma->ftdic)); + sr_warn("ftdi_set_baudrate failed: %s", + ftdi_get_error_string(&sigma->ftdic)); return 0; } @@ -483,10 +493,10 @@ static int upload_firmware(int firmware_idx, struct sigma *sigma) snprintf(firmware_path, sizeof(firmware_path), "%s/%s", FIRMWARE_DIR, firmware_files[firmware_idx]); - if (-1 == bin2bitbang(firmware_path, &buf, &buf_size)) { - g_warning("An error occured while reading the firmware: %s", - firmware_path); - return SR_ERR; + if ((ret = bin2bitbang(firmware_path, &buf, &buf_size)) != SR_OK) { + sr_warn("An error occured while reading the firmware: %s", + firmware_path); + return ret; } /* Upload firmare. */ @@ -495,8 +505,8 @@ static int upload_firmware(int firmware_idx, struct sigma *sigma) g_free(buf); if ((ret = ftdi_set_bitmode(&sigma->ftdic, 0x00, BITMODE_RESET)) < 0) { - g_warning("ftdi_set_bitmode failed: %s", - ftdi_get_error_string(&sigma->ftdic)); + sr_warn("ftdi_set_bitmode failed: %s", + ftdi_get_error_string(&sigma->ftdic)); return SR_ERR; } @@ -513,7 +523,7 @@ static int upload_firmware(int firmware_idx, struct sigma *sigma) ret = sigma_read(result, 3, sigma); if (ret != 3 || result[0] != 0xa6 || result[1] != 0x55 || result[2] != 0xaa) { - g_warning("Configuration failed. Invalid reply received."); + sr_warn("Configuration failed. Invalid reply received."); return SR_ERR; } @@ -537,7 +547,7 @@ static int hw_opendev(int device_index) if ((ret = ftdi_usb_open_desc(&sigma->ftdic, USB_VENDOR, USB_PRODUCT, USB_DESCRIPTION, NULL)) < 0) { - g_warning("ftdi_usb_open failed: %s", + sr_warn("ftdi_usb_open failed: %s", ftdi_get_error_string(&sigma->ftdic)); return 0; @@ -561,15 +571,15 @@ static int set_samplerate(struct sr_device_instance *sdi, if (supported_samplerates[i] == 0) return SR_ERR_SAMPLERATE; - if (samplerate <= MHZ(50)) { + if (samplerate <= SR_MHZ(50)) { ret = upload_firmware(0, sigma); sigma->num_probes = 16; } - if (samplerate == MHZ(100)) { + if (samplerate == SR_MHZ(100)) { ret = upload_firmware(1, sigma); sigma->num_probes = 8; } - else if (samplerate == MHZ(200)) { + else if (samplerate == SR_MHZ(200)) { ret = upload_firmware(2, sigma); sigma->num_probes = 4; } @@ -578,7 +588,7 @@ static int set_samplerate(struct sr_device_instance *sdi, sigma->samples_per_event = 16 / sigma->num_probes; sigma->state.state = SIGMA_IDLE; - g_message("Firmware uploaded"); + sr_info("Firmware uploaded"); return ret; } @@ -608,12 +618,11 @@ static int configure_probes(struct sr_device_instance *sdi, GSList *probes) if (!probe->enabled || !probe->trigger) continue; - if (sigma->cur_samplerate >= MHZ(100)) { + if (sigma->cur_samplerate >= SR_MHZ(100)) { /* Fast trigger support. */ if (trigger_set) { - g_warning("Asix Sigma only supports a single " - "pin trigger in 100 and 200 " - "MHz mode."); + sr_warn("ASIX SIGMA only supports a single " + "pin trigger in 100 and 200MHz mode."); return SR_ERR; } if (probe->trigger[0] == 'f') @@ -621,9 +630,9 @@ static int configure_probes(struct sr_device_instance *sdi, GSList *probes) else if (probe->trigger[0] == 'r') sigma->trigger.risingmask |= probebit; else { - g_warning("Asix Sigma only supports " - "rising/falling trigger in 100 " - "and 200 MHz mode."); + sr_warn("ASIX SIGMA only supports " + "rising/falling trigger in 100 " + "and 200MHz mode."); return SR_ERR; } @@ -653,8 +662,8 @@ static int configure_probes(struct sr_device_instance *sdi, GSList *probes) * does not permit ORed triggers. */ if (trigger_set > 1) { - g_warning("Asix Sigma only supports 1 rising/" - "falling triggers."); + sr_warn("ASIX SIGMA only supports 1 rising/" + "falling triggers."); return SR_ERR; } } @@ -839,7 +848,7 @@ static int decode_chunk_ts(uint8_t *buf, uint16_t *lastts, /* Check if trigger is in this chunk. */ if (triggerpos != -1) { - if (sigma->cur_samplerate <= MHZ(50)) + if (sigma->cur_samplerate <= SR_MHZ(50)) triggerpos -= EVENTS_PER_CLUSTER - 1; if (triggerpos < 0) @@ -1000,8 +1009,8 @@ static int receive_data(int fd, int revents, void *user_data) newchunks = MIN(chunks_per_read, numchunks - sigma->state.chunks_downloaded); - g_message("Downloading sample data: %.0f %%", - 100.0 * sigma->state.chunks_downloaded / numchunks); + sr_info("Downloading sample data: %.0f %%", + 100.0 * sigma->state.chunks_downloaded / numchunks); bufsz = sigma_read_dram(sigma->state.chunks_downloaded, newchunks, buf, sigma); @@ -1205,11 +1214,10 @@ static int hw_start_acquisition(int device_index, gpointer session_device_id) struct sr_datafeed_packet packet; struct sr_datafeed_header header; struct clockselect_50 clockselect; - int frac; + int frac, triggerpin, ret; uint8_t triggerselect; struct triggerinout triggerinout_conf; struct triggerlut lut; - int triggerpin; session_device_id = session_device_id; @@ -1219,14 +1227,16 @@ static int hw_start_acquisition(int device_index, gpointer session_device_id) sigma = sdi->priv; /* If the samplerate has not been set, default to 200 KHz. */ - if (sigma->cur_firmware == -1) - set_samplerate(sdi, KHZ(200)); + if (sigma->cur_firmware == -1) { + if ((ret = set_samplerate(sdi, SR_KHZ(200))) != SR_OK) + return ret; + } /* Enter trigger programming mode. */ sigma_set_register(WRITE_TRIGGER_SELECT1, 0x20, sigma); /* 100 and 200 MHz mode. */ - if (sigma->cur_samplerate >= MHZ(100)) { + if (sigma->cur_samplerate >= SR_MHZ(100)) { sigma_set_register(WRITE_TRIGGER_SELECT1, 0x81, sigma); /* Find which pin to trigger on from mask. */ @@ -1243,7 +1253,7 @@ static int hw_start_acquisition(int device_index, gpointer session_device_id) triggerselect |= 1 << 3; /* All other modes. */ - } else if (sigma->cur_samplerate <= MHZ(50)) { + } else if (sigma->cur_samplerate <= SR_MHZ(50)) { build_basic_trigger(&lut, sigma); sigma_write_trigger_lut(&lut, sigma); @@ -1264,10 +1274,10 @@ static int hw_start_acquisition(int device_index, gpointer session_device_id) sigma_set_register(WRITE_TRIGGER_SELECT1, triggerselect, sigma); /* Set clock select register. */ - if (sigma->cur_samplerate == MHZ(200)) + if (sigma->cur_samplerate == SR_MHZ(200)) /* Enable 4 probes. */ sigma_set_register(WRITE_CLOCK_SELECT, 0xf0, sigma); - else if (sigma->cur_samplerate == MHZ(100)) + else if (sigma->cur_samplerate == SR_MHZ(100)) /* Enable 8 probes. */ sigma_set_register(WRITE_CLOCK_SELECT, 0x00, sigma); else { @@ -1275,7 +1285,7 @@ static int hw_start_acquisition(int device_index, gpointer session_device_id) * 50 MHz mode (or fraction thereof). Any fraction down to * 50 MHz / 256 can be used, but is not supported by sigrok API. */ - frac = MHZ(50) / sigma->cur_samplerate - 1; + frac = SR_MHZ(50) / sigma->cur_samplerate - 1; clockselect.async = 0; clockselect.fraction = frac; @@ -1309,7 +1319,7 @@ static int hw_start_acquisition(int device_index, gpointer session_device_id) sr_session_bus(session_device_id, &packet); /* Add capture source. */ - source_add(0, G_IO_IN, 10, receive_data, sdi); + sr_source_add(0, G_IO_IN, 10, receive_data, sdi); sigma->state.state = SIGMA_CAPTURE; @@ -1352,17 +1362,17 @@ static void hw_stop_acquisition(int device_index, gpointer session_device_id) } struct sr_device_plugin asix_sigma_plugin_info = { - "asix-sigma", - "ASIX SIGMA", - 1, - hw_init, - hw_cleanup, - hw_opendev, - hw_closedev, - hw_get_device_info, - hw_get_status, - hw_get_capabilities, - hw_set_configuration, - hw_start_acquisition, - hw_stop_acquisition, + .name = "asix-sigma", + .longname = "ASIX SIGMA", + .api_version = 1, + .init = hw_init, + .cleanup = hw_cleanup, + .opendev = hw_opendev, + .closedev = hw_closedev, + .get_device_info = hw_get_device_info, + .get_status = hw_get_status, + .get_capabilities = hw_get_capabilities, + .set_configuration = hw_set_configuration, + .start_acquisition = hw_start_acquisition, + .stop_acquisition = hw_stop_acquisition, };