This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

NRF51822 SPI Master

Hi,

I used the nrf51822 as the spi master,it could send out the data, but it didn't receive the slave data.Did I miss somthing?

static volatile bool m_transfer_completed = true;

static void spi_master_0_event_handler(spi_master_evt_t spi_master_evt)
{

    switch (spi_master_evt.evt_type)
    {
        case SPI_MASTER_EVT_TRANSFER_COMPLETED:
            m_transfer_completed = true;
            break;

        default:
            // No implementation needed.
            break;
    }
}



static void spi_master_init( )
{

    uint32_t sm_err_code;

    // Configure SPI master.
    spi_master_config_t spi_config = SPI_MASTER_INIT_DEFAULT;

    spi_config.SPI_Pin_SCK  = GT_CLK;
    spi_config.SPI_Pin_MISO = GT_DO;
    spi_config.SPI_Pin_MOSI = GT_DI;
    spi_config.SPI_Pin_SS   = GT_CS;

    spi_config.SPI_CONFIG_ORDER = SPI_CONFIG_ORDER_MsbFirst;

    sm_err_code = spi_master_open(SPI_MASTER_0, &spi_config);
    APP_ERROR_CHECK(sm_err_code);

    // Register event handler for SPI master.
    spi_master_evt_handler_reg(SPI_MASTER_0, spi_master_0_event_handler);
}


void r_dat_bat(unsigned long TAB_addr,unsigned int Num,unsigned char *p_arr)
{

	uint8_t tabAddr[4];
	uint16_t spiTimeOut=65535;
	uint32_t gt_err_code;
	
	TAB_addr |= 0x03000000;
	tabAddr[0] = (uint8_t)( TAB_addr>>24 );
	tabAddr[1] = (uint8_t)( TAB_addr>>16 );
	tabAddr[2] = (uint8_t)( TAB_addr>>8 );
	tabAddr[3] = (uint8_t)( TAB_addr&0x000000FF );
	
	spi_master_init( );
	
	m_transfer_completed = false;
	gt_err_code = spi_master_send_recv(SPI_MASTER_0, tabAddr, 4, p_arr, 2);
  if( gt_err_code != NRF_SUCCESS )
	 {
			gt_err_code = spi_master_send_recv(SPI_MASTER_0, tabAddr, 4, p_arr, 2);
	 }
	 
	if( gt_err_code == NRF_SUCCESS )
	 {
			while( ( spiTimeOut>0 ) && ( !m_transfer_completed ) )
			 {
					A_NOP;
					A_NOP;
					spiTimeOut--;
			 }
	 }
	 
	// Close SPI master.
  spi_master_close(SPI_MASTER_0);
	
	GT_CS_H;
	GT_CLK_L;
	GT_DI_L;
	GT_DO_PL;
}

image description

Parents
  • In addition to what Petter has said..

    It looks like you may have transposed MISO and MOSI, but its hard to tell as you named one of them MSIO which is not a valid name in terms of SPI

    Why don't you just used nrf_drv_spi_transfer() rather than separate send and receive calls

    The SPI protocol has simultaneous bidirectional communication. For every clock pulse, you will output 1 bit of data on MOSI but also receive 1 bit of data on MISO

    Also. Looking at your logic analyser image, the number of clock pulses look wrong tabAddr is 4 bytes, but the API is only writing 2 bytes probably caused by this line

     gt_err_code = spi_master_send_recv(SPI_MASTER_0, tabAddr, 4, p_arr, 2);
    

    So its not sending the 4 byte buffer in one hit

    I suspect the data you passed in to TAB_addr is 0x16xxxx hence its just sending 0x03 0x16 for the first 2 bytes

Reply
  • In addition to what Petter has said..

    It looks like you may have transposed MISO and MOSI, but its hard to tell as you named one of them MSIO which is not a valid name in terms of SPI

    Why don't you just used nrf_drv_spi_transfer() rather than separate send and receive calls

    The SPI protocol has simultaneous bidirectional communication. For every clock pulse, you will output 1 bit of data on MOSI but also receive 1 bit of data on MISO

    Also. Looking at your logic analyser image, the number of clock pulses look wrong tabAddr is 4 bytes, but the API is only writing 2 bytes probably caused by this line

     gt_err_code = spi_master_send_recv(SPI_MASTER_0, tabAddr, 4, p_arr, 2);
    

    So its not sending the 4 byte buffer in one hit

    I suspect the data you passed in to TAB_addr is 0x16xxxx hence its just sending 0x03 0x16 for the first 2 bytes

Children
No Data
Related