27 #define NULINK_READ_TIMEOUT LIBUSB_TIMEOUT_MS
29 #define NULINK_HID_MAX_SIZE (64)
30 #define NULINK2_HID_MAX_SIZE (1024)
31 #define V6M_MAX_COMMAND_LENGTH (NULINK_HID_MAX_SIZE - 2)
32 #define V7M_MAX_COMMAND_LENGTH (NULINK_HID_MAX_SIZE - 3)
34 #define NULINK2_USB_PID1 (0x5200)
35 #define NULINK2_USB_PID2 (0x5201)
49 int (*
xfer)(
void *handle, uint8_t *buf,
int size);
54 #define CMD_READ_REG 0xB5UL
55 #define CMD_READ_RAM 0xB1UL
56 #define CMD_WRITE_REG 0xB8UL
57 #define CMD_WRITE_RAM 0xB9UL
58 #define CMD_CHECK_ID 0xA3UL
59 #define CMD_MCU_RESET 0xE2UL
60 #define CMD_CHECK_MCU_STOP 0xD8UL
61 #define CMD_MCU_STEP_RUN 0xD1UL
62 #define CMD_MCU_STOP_RUN 0xD2UL
63 #define CMD_MCU_FREE_RUN 0xD3UL
64 #define CMD_SET_CONFIG 0xA2UL
66 #define ARM_SRAM_BASE 0x20000000UL
68 #define HARDWARE_CONFIG_NULINKPRO 1
69 #define HARDWARE_CONFIG_NULINK2 2
202 LOG_INFO(
"Nu-Link firmware_version %" PRIu32
", product_id (0x%08" PRIx32
")",
208 LOG_INFO(
"Adapter is Nu-Link-Pro, target_voltage_mv(%" PRIu16
"), usb_voltage_mv(%" PRIu16
")",
239 LOG_INFO(
"IDCODE: 0x%08" PRIX32, *idcode);
248 LOG_DEBUG(
"nulink_usb_write_debug_reg 0x%08" PRIX32
" 0x%08" PRIX32,
addr, val);
485 uint32_t bytes_remaining = 12;
488 LOG_DEBUG(
"nulink_usb_read_mem8: addr 0x%08" PRIx32
", len %" PRId16,
addr, len);
494 uint32_t aligned_addr =
addr / 4;
495 aligned_addr = aligned_addr * 4;
497 LOG_DEBUG(
"nulink_usb_read_mem8: unaligned address addr 0x%08" PRIx32
498 "/aligned addr 0x%08" PRIx32
" offset %" PRIu32,
507 if (len < bytes_remaining)
508 bytes_remaining = len;
532 for (
unsigned int i = 0; i <
count; i++) {
551 for (
unsigned int i = 0; i <
count; i++) {
555 memcpy(
buffer + 2 * i, h->
databuf + 4 * (2 * i + 1), len - 2);
558 if (len >= bytes_remaining)
559 len -= bytes_remaining;
570 uint32_t bytes_remaining = 12;
573 LOG_DEBUG(
"nulink_usb_write_mem8: addr 0x%08" PRIx32
", len %" PRIu16,
addr, len);
579 uint32_t aligned_addr =
addr / 4;
580 aligned_addr = aligned_addr * 4;
582 LOG_DEBUG(
"nulink_usb_write_mem8: address not aligned. addr(0x%08" PRIx32
583 ")/aligned_addr(0x%08" PRIx32
")/offset(%" PRIu32
")",
592 if (len < bytes_remaining)
593 bytes_remaining = len;
617 for (
unsigned int i = 0; i <
count; i++) {
623 u32buffer = (u32buffer <<
offset * 8);
631 LOG_DEBUG(
"nulink_usb_write_mem8: count(%u), mask: 0xFFFFFF00", i);
634 LOG_DEBUG(
"nulink_usb_write_mem8: count(%u), mask: 0xFFFF0000", i);
639 LOG_DEBUG(
"nulink_usb_write_mem8: count(%u), mask: 0xFF00FFFF", i);
643 LOG_DEBUG(
"nulink_usb_write_mem8: count(%u), mask: 0x0000FFFF", i);
649 LOG_DEBUG(
"nulink_usb_write_mem8: count(%u), mask: 0xFFFF0000", i);
652 LOG_DEBUG(
"nulink_usb_write_mem8: count(%u), mask: 0x00000000", i);
666 if (len >= bytes_remaining)
667 len -= bytes_remaining;
677 uint32_t bytes_remaining = 12;
683 if (len % 4 ||
addr % 4) {
689 if (len < bytes_remaining)
690 bytes_remaining = len;
692 unsigned int count = bytes_remaining / 4;
711 for (
unsigned int i = 0; i <
count; i++) {
728 for (
unsigned int i = 0; i <
count; i++) {
733 if (len >= bytes_remaining)
734 len -= bytes_remaining;
746 uint32_t bytes_remaining = 12;
752 if (len % 4 ||
addr % 4) {
758 if (len < bytes_remaining)
759 bytes_remaining = len;
761 unsigned int count = bytes_remaining / 4;
780 for (
unsigned int i = 0; i <
count; i++) {
799 if (len >= bytes_remaining)
800 len -= bytes_remaining;
810 uint32_t max_tar_block = (tar_autoincr_block - ((tar_autoincr_block - 1) & address));
812 if (max_tar_block == 0)
815 return max_tar_block;
830 if (
count < bytes_remaining)
831 bytes_remaining =
count;
833 if (bytes_remaining >= 4)
849 uint32_t head_bytes = 4 - (
addr % 4);
856 bytes_remaining -= head_bytes;
859 if (bytes_remaining % 4)
870 buffer += bytes_remaining;
871 addr += bytes_remaining;
872 count -= bytes_remaining;
885 LOG_DEBUG(
"nulink_usb_write_mem: address below ARM_SRAM_BASE, not supported.\n");
895 if (
count < bytes_remaining)
896 bytes_remaining =
count;
898 if (bytes_remaining >= 4)
914 uint32_t head_bytes = 4 - (
addr % 4);
921 bytes_remaining -= head_bytes;
924 if (bytes_remaining % 4)
936 buffer += bytes_remaining;
937 addr += bytes_remaining;
938 count -= bytes_remaining;
948 return !strcmp(targetname,
"cortex_m");
954 unsigned long max_ice_clock = khz;
956 LOG_DEBUG(
"nulink_speed: query %s", query ?
"yes" :
"no");
958 if (max_ice_clock > 12000)
959 max_ice_clock = 12000;
960 else if ((max_ice_clock == 3 * 512) || (max_ice_clock == 1500))
961 max_ice_clock = 1500;
962 else if (max_ice_clock >= 1000)
963 max_ice_clock = max_ice_clock / 1000 * 1000;
965 max_ice_clock = max_ice_clock / 100 * 100;
967 LOG_DEBUG(
"Nu-Link nulink_speed: %lu", max_ice_clock);
994 LOG_INFO(
"Nu-Link target_voltage_mv[0](%04" PRIx16
"), target_voltage_mv[1](%04" PRIx16
995 "), target_voltage_mv[2](%04" PRIx16
"), if_target_power_supplied(%d)",
1002 return max_ice_clock;
1023 struct hid_device_info *
devs, *cur_dev;
1024 uint16_t target_vid = 0;
1025 uint16_t target_pid = 0;
1026 wchar_t *target_serial =
NULL;
1033 if (!param->
vid[0] && !param->
pid[0]) {
1038 if (hid_init() != 0) {
1053 target_serial = calloc(len + 1,
sizeof(
wchar_t));
1054 if (!target_serial) {
1059 if (mbstowcs(target_serial,
serial, len + 1) == (
size_t)(-1)) {
1061 free(target_serial);
1062 target_serial =
NULL;
1066 devs = hid_enumerate(0, 0);
1071 for (
unsigned int i = 0; param->
vid[i] || param->
pid[i]; i++) {
1072 if (param->
vid[i] == cur_dev->vendor_id && param->
pid[i] == cur_dev->product_id) {
1081 if (cur_dev->serial_number && wcscmp(target_serial, cur_dev->serial_number) == 0)
1085 cur_dev = cur_dev->next;
1088 target_vid = cur_dev->vendor_id;
1089 target_pid = cur_dev->product_id;
1092 hid_free_enumeration(
devs);
1094 if (target_vid == 0 && target_pid == 0) {
1099 hid_device *dev = hid_open(target_vid, target_pid, target_serial);
1101 LOG_ERROR(
"unable to open Nu-Link device 0x%" PRIx16
":0x%" PRIx16, target_vid, target_pid);
1108 switch (target_pid) {
1128 LOG_DEBUG(
"nulink_usb_version failed with cmdSize(4 * 5)");
1132 LOG_DEBUG(
"nulink_usb_version failed with cmdSize(4 * 6)");
1142 LOG_DEBUG(
"nulink_usb_open: we manually perform nulink_usb_reset");
1147 free(target_serial);
1152 free(target_serial);
const char * adapter_get_required_serial(void)
Retrieves the serial number set with command 'adapter serial'.
Support functions to access arbitrary bits in a byte array.
static uint32_t buf_get_u32(const uint8_t *_buffer, unsigned int first, unsigned int num)
Retrieves num bits from _buffer, starting at the first bit, returning the bits in a 32-bit word.
static struct libusb_device ** devs
The usb device list.
#define LOG_WARNING(expr ...)
#define LOG_ERROR(expr ...)
#define LOG_INFO(expr ...)
#define LOG_DEBUG(expr ...)
static int nulink_usb_write_debug_reg(void *handle, uint32_t addr, uint32_t val)
static int nulink_usb_assert_srst(void *handle, int srst)
static int nulink2_usb_xfer(void *handle, uint8_t *buf, int size)
#define CMD_CHECK_MCU_STOP
#define HARDWARE_CONFIG_NULINKPRO
static int nulink_usb_read_mem32(void *handle, uint32_t addr, uint16_t len, uint8_t *buffer)
static int nulink_usb_version(void *handle)
static int nulink1_usb_xfer(void *handle, uint8_t *buf, int size)
#define NULINK_READ_TIMEOUT
static uint32_t nulink_max_block_size(uint32_t tar_autoincr_block, uint32_t address)
static int nulink_usb_xfer(void *handle, uint8_t *buf, int size)
#define NULINK_HID_MAX_SIZE
static int nulink_speed(void *handle, int khz, bool query)
static int nulink_usb_open(struct hl_interface_param *param, void **fd)
static int nulink_usb_run(void *handle)
#define NULINK2_HID_MAX_SIZE
#define V6M_MAX_COMMAND_LENGTH
static int nulink_usb_reset(void *handle)
static void nulink2_usb_init_buffer(void *handle, uint32_t size)
static int nulink_usb_read_mem(void *handle, uint32_t addr, uint32_t size, uint32_t count, uint8_t *buffer)
static void nulink_usb_init_buffer(void *handle, uint32_t size)
static int nulink_usb_idcode(void *handle, uint32_t *idcode)
static int nulink_usb_close(void *handle)
#define V7M_MAX_COMMAND_LENGTH
static int nulink_usb_write_reg(void *handle, unsigned int regsel, uint32_t val)
static int nulink_usb_read_mem8(void *handle, uint32_t addr, uint16_t len, uint8_t *buffer)
static int nulink_usb_halt(void *handle)
struct hl_layout_api nulink_usb_layout_api
static int nulink_usb_override_target(const char *targetname)
static int nulink_usb_write_mem(void *handle, uint32_t addr, uint32_t size, uint32_t count, const uint8_t *buffer)
static int nulink_usb_write_mem8(void *handle, uint32_t addr, uint16_t len, const uint8_t *buffer)
static int nulink_usb_write_mem32(void *handle, uint32_t addr, uint16_t len, const uint8_t *buffer)
static void nulink1_usb_init_buffer(void *handle, uint32_t size)
static int nulink_usb_xfer_rw(void *handle, uint8_t *buf)
#define HARDWARE_CONFIG_NULINK2
static int nulink_usb_step(void *handle)
static enum target_state nulink_usb_state(void *handle)
static int nulink_usb_read_reg(void *handle, unsigned int regsel, uint32_t *val)
target_addr_t addr
Start address to search for the control block.
size_t size
Size of the control block search area.
enum hl_transports transport
uint16_t pid[HLA_MAX_USB_IDS+1]
List of recognised PIDs.
uint16_t vid[HLA_MAX_USB_IDS+1]
List of recognised VIDs.
int(* open)(struct hl_interface_param *param, void **handle)
uint8_t cmdbuf[NULINK2_HID_MAX_SIZE+1]
void(* init_buffer)(void *handle, uint32_t size)
uint8_t databuf[NULINK2_HID_MAX_SIZE]
int(* xfer)(void *handle, uint8_t *buf, int size)
uint8_t tempbuf[NULINK2_HID_MAX_SIZE]
#define ERROR_TARGET_UNALIGNED_ACCESS
static uint16_t le_to_h_u16(const uint8_t *buf)
static void h_u32_to_le(uint8_t *buf, uint32_t val)
static void h_u16_to_le(uint8_t *buf, uint16_t val)
static uint32_t le_to_h_u32(const uint8_t *buf)