I'm running into a weird bug where the first 4 bits received by my SPIM are always 1 - the next 64.5 bytes are all correct though...
I've built 3 FWs out of provided examples.
- I have a single devkit acting as a spi_slave built off the spis example from SDK 17.1. It waits for a spi transaction and as soon as it occurs it increments a few bytes (including the first one) in a buffer and sets it to be sent out again. Pertinent code, below:
static uint8_t m_tx_buf[] = {2,2,2,2,2,3,3,3,3,4,5,5,6,2,2,3,3,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18, 19,20,21,22,23,15,15,16,17,18,19,19,1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,16,1,1,1}; static uint8_t m_rx_buf[sizeof(m_tx_buf) + 1]; // RX buffer. ... ... void spis_event_handler(nrf_drv_spis_event_t event) { if (event.evt_type == NRF_DRV_SPIS_XFER_DONE) { spis_xfer_done = true; bsp_board_led_invert(BSP_BOARD_LED_2); NRF_LOG_INFO(" packet_id: %d",m_tx_buf[0]); counter = counter + 1; m_tx_buf[0] = counter; m_tx_buf[10] = m_tx_buf[10] + 3; m_tx_buf[20] = m_tx_buf[20] + 4; m_tx_buf[30] = m_tx_buf[30] + 5; m_tx_buf[40] = m_tx_buf[40] + 6; m_tx_buf[50] = m_tx_buf[50] + 7; m_tx_buf[60] = counter + 2; } } - I have a spi master (built off the ble_app_uart and the nrfx_spim examples from SDK 16). Ultimately it triggers spi transactions based on a timer interrupt and then sends the 65 received bytes over BLE to a central device. Pertinent code below:
static uint8_t spi_tx_buf[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,16,0,0,0}; static uint8_t ble_tx_buff_t[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; static uint8_t spi_rx_buf[sizeof(spi_tx_buf) + 1]; /**< RX buffer. */ ... static void trx_timer_handler(void * p_context) { // update spi_tx_buff to have read data commands spi_tx_buf[0] = read_data_instr[0]; spi_tx_buf[1] = read_data_instr[1]; spi_tx_buf[63] = read_data_instr[2]; spi_tx_buf[64] = read_data_instr[3]; // fire off a SPI transaction NRF_LOG_INFO("%s", spi_tx_buf); if (spi_xfer_done == true) { spi_xfer_done = false; nrfx_spim_xfer_desc_t xfer_desc = NRFX_SPIM_XFER_TRX(spi_tx_buf, spi_length, ble_tx_buff_t, spi_length); APP_ERROR_CHECK(nrfx_spim_xfer(&spi, &xfer_desc, 0)); } void spim_event_handler(nrfx_spim_evt_t const * p_event, void * p_context) { spi_xfer_done = true; NRF_LOG_INFO("SPI TRX COMPLETE"); if (spi_cmd_flag == SPI_STREAM) { // block to convert and send single char packet char buf[131]; uint16_t size_of_buff = sizeof(char)*131; for (int i = 0 ; i != 65 ; i++) { sprintf(buf + (2*i), "%02x", ble_tx_buff_t[i]); } NRF_LOG_INFO("packet_id: %d",buf[0]); while(ble_nus_data_send(&m_nus, (uint8_t *) buf, &size_of_buff, m_conn_handle) != NRF_SUCCESS); } } - A central device that I use to send commands to and read data from the spi_master (built on top of ble_usbd_c from SDK 16).
My issues and current debugging steps are below:
- The slave has the correct data in its tx buff but the received data is 'incorrect' on the SPIM side. It's odd because it's just the first 4 bits - everything else is correct. The ble_tx_buff_t[0] contents will increment from 128 to 255 and then wrap back to 255 instead of 0. I don't have access to an oscope right now but I'm trying to get one to take a look at the MISO line. Dollars to donuts that the problem is somewhere on the master side...
- When I try running the SPIM at 4M or above all the data gets corrupted. Are the limits to what the spis default pins can handle? Or are there other issues I have to keep in mind for running spi at faster speeds?
- This is just a question - not an issue - why is the spi_rx_buff +1 element larger than the tx_buff? This is something I copied over from the examples but never understood why. I tried making the buffers the same size and that didn't seem to change anything...
Thank you for your help!
Ryan