/// Receive bit_cnt number of bits into buf (LSB format) starting at the bit offset. /// SWD Read Data request is not byte-aligned, so this will always cause overrun error. We clear the error in spi_transmit_resync(). static void spi_exchange_receive(uint8_t buf[], unsigned int offset, unsigned int bit_cnt) { // Handle SWD Write Ack, which is 5 bits and not byte-aligned: // ** trgt -> host offset 0 bits 5: 13 // We always force return OK (0x13) without actually receiving SPI bytes. We compensate the 5 bits during SWD Write Data later (33 bits). if (offset == 0 && bit_cnt == 5) { // printf("write ack force OK\n"); buf[0] = (buf[0] & 0b11100000) | 0x13; // Force lower 5 bits to be 0x13 return; } // Otherwise we must be receiving SWD Read Data, which is 38 bits and not byte-aligned. We will resync by transmitting JTAG-To-SWD below. // ** trgt -> host offset 0 bits 38: 73 47 01 ba a2 ... // Receive the LSB buffer from target. spi_receive(spi_fd, lsb_buf, byte_cnt); ...