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

TWI AND SPI in a single code

Hi ,

Need a bit of help here.

I am trying to use TWI and SPI in a single code for communicating with two different slave devices, the LTC2941 coulomb counter and the EM4325 RFID tag. I have written a switch case program similar to the one given in SDK 12.3.0 , and added an SPI case for the same. Here is a snippet of my main function (it is similar to the twis_master_with_twis_slave code provided in the SDK) :

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main(void)
{
ret_code_t err_code;
/* Initialization of UART */
bsp_board_leds_init();
APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
/* Initializing simulated EEPROM */
err_code = eeprom_simulator_init();
APP_ERROR_CHECK(err_code);
/* Initializing TWI master interface for EEPROM */
err_code = twi_master_init();
APP_ERROR_CHECK(err_code);
while (1)
{
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

EM4325 datasheet : http://www.emmicroelectronic.com/products/rf-identification-security/epc-and-uhf-ics/em4325

LTC 2941 datasheet : http://cds.linear.com/docs/en/datasheet/2941fb.pdf

Here is the rfid_spi() function which is being called inside the switch case statement for communicating with EM4325 :

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
static void rfid_spi(void)
{
uint32_t spike_clk = 24 ;
uint32_t spike_miso = 23;
uint32_t spike_mosi = 22 ;
nrf_spim_enable( (NRF_SPIM_Type *)SPI_INSTANCE);
//nrf_spim_event_clear((NRF_SPIM_Type *)SPI_INSTANCE , NRF_SPI_EVENT_READY ) ;
nrf_spim_configure((NRF_SPIM_Type *) SPI_INSTANCE , NRF_SPIM_MODE_0 , NRF_SPIM_BIT_ORDER_MSB_FIRST) ;
nrf_spim_pins_set( (NRF_SPIM_Type *)SPI_INSTANCE , spike_clk ,spike_mosi, spike_miso ) ;
nrf_spim_frequency_set((NRF_SPIM_Type *)SPI_INSTANCE, NRF_SPIM_FREQ_250K);
// nrf_spim_int_enable ((NRF_SPIM_Type *)SPI_INSTANCE,SPI_INTENSET_READY_Set) ;
//memset(m_rx_buf, 0, 20); //m_length
spi_xfer_done = false;
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

I am using the SPIM HAL to communicate with EM4325, this is because when I tried to integrate the spi and twi master codes (of SDK 12.3) , it was giving me garbage values in the receive buffer of the SPI, hence I tried to use the HAL. When I used the SPIM HAL, .( I have referred to the following webpage for using the HAL, my SPI Instance is 2 and TWI instance is 0 ----- https://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.sdk52.v0.9.2%2Fgroup__nrf__spim__hal.html ) 

I tried to use the event check function but it was returning me zero values , which indicated that the SPI was not being initiated by the HAL functions

What is the reason for this?? I have attached the serial window output below:

Can someone please help me with a switch case code in which I can use SPI for one switch case and I2C for another ? Is it possible to do this using HAL or with the API which have been provided in SDK 12.3 itself?

Also, when do I know if I should use EASY DMA or not?

Thanking you in advance,

Kunal.

  • Hi, I think you will  have to first combine the spi and twi examples, for instance (I believe the same examples are available in SDKv12.3):
    \nRF5_SDK_14.2.0_17b948a\nRF5_SDK_14.2.0_17b948a\examples\peripheral\spi\
    \nRF5_SDK_14.2.0_17b948a\nRF5_SDK_14.2.0_17b948a\examples\peripheral\twi_sensor\

    You can use EasyDMA if you are using the nRF52-series, this is configured in sdk_config.h, it is typically preferred since there is less CPU work.