How to use lib_spi library with 2 peripherals

If you have a simple question and just want an answer.
usb2.0
Member++
Posts: 17
Joined: Sat Dec 16, 2023 11:44 pm

How to use lib_spi library with 2 peripherals

Post by usb2.0 »

Hello,

I am trying to use lib_spi 3.0.2 with 2 peripherals. It works with only 1. Using sw usb audio v 8.1.0 on 316-mc dev board

in main.xc:

Code: Select all

/* These ports are used for the SPI master */
out buffered port:32 p_sclk = on tile[1]: XS1_PORT_1L;
out port p_ss[2] = on tile[1]: {XS1_PORT_1I, XS1_PORT_1F};
in buffered port:32 p_miso = on tile[1]: XS1_PORT_1K;
out buffered port:32 p_mosi = on tile[1]: XS1_PORT_1J;

/* Main for USB Audio Applications */
int main()
{

 interface spi_master_if i_spi[2];
	
 USER_MAIN_DECLARATIONS

    par
    {
        USER_MAIN_CORES
        on tile[1]: spi_master(i_spi, 1, p_sclk, p_mosi, p_miso, p_ss, 2, null);
        on tile[1]: peripheral_1_setup(i_spi[0]);        
        on tile[1]: peripheral_2_setup(i_spi[1]);  
		...



It runs. I see the ss pin for peripheral 1 (1I) goes low and then the clock pin toggles and it works for the 1st peripheral. But the ss pin for 2nd peripheral is always high, it never toggles. Its like peripheral_2_setup() never even runs.

Inside peripheral_1_setup():

Code: Select all

	//spi.begin_transaction(device_index, speed_in_khz, mode)
	spi.begin_transaction(0, 100, SPI_MODE_1);
	rv = spi.transfer8(buf[0]);	
	rv = spi.transfer8(buf[1]);		
	rv = spi.transfer8(buf[2]);
	spi.end_transaction(100);

And inside peripheral_2_setup():

Code: Select all

	//spi.begin_transaction(device_index, speed_in_khz, mode)
	spi.begin_transaction(1, 100, SPI_MODE_1);
	rv = spi.transfer8(buf[0]);	
	rv = spi.transfer8(buf[1]);		
	rv = spi.transfer8(buf[2]);
	spi.end_transaction(100);	

What am I doing wrong? If I change peripheral_1_setup() like this:

Code: Select all

	//spi.begin_transaction(device_index, speed_in_khz, mode)
	spi.begin_transaction(1, 100, SPI_MODE_1);
	rv = spi.transfer8(buf[0]);	
	rv = spi.transfer8(buf[1]);		
	rv = spi.transfer8(buf[2]);
	spi.end_transaction(100);
Then I see the ss pin for peripheral 2 does toggle so I know that pin is ok to use. Any help is appreciated.
User avatar
xhuw
Verified
Member++
Posts: 23
Joined: Wed May 22, 2024 2:36 pm

Post by xhuw »

in your first snippet:

Code: Select all

on tile[1]: spi_master(i_spi, 1, p_sclk, p_mosi, p_miso, p_ss, 2, null);
The second argument should be 2 as you are providing 2 interfaces. https://github.com/xmos/lib_spi/blob/ea ... /spi.h#L96
XMOS Software Engineer

Image
usb2.0
Member++
Posts: 17
Joined: Sat Dec 16, 2023 11:44 pm

Post by usb2.0 »

I tried it, but I get a compiler error. Why is it complaining about a 2 in the second argument?

I ran clean and rebuild, and it's working.

Thank you for the help!

Code: Select all

int main()
{
    interface spi_master_if i_spi[2];
    
    USER_MAIN_DECLARATIONS

    par
    {
        USER_MAIN_CORES
        on tile[1]: spi_master(i_spi, 2, p_sclk, p_mosi, p_miso, p_ss, 2, null);
        on tile[1]: ad9508_setup(i_spi[0]);        
        on tile[1]: wm8804_setup(i_spi[1]);
        ...

Code: Select all

C:\xmos_usb_audio_8.1.0\sw_usb_audio\app_usb_aud_xk_316_mc_nick>xmake all
Checking build modules
Using build modules: lib_sw_pll(2.2.0) lib_xua(4.1.0) lib_i2c(6.2.0) lib_spi(3.0.2) lib_spdif(6.1.0) lib_xassert(4.2.0) lib_adat(1.2.0) lib_locks(2.2.0) lib_logging(3.2.0) lib_mic_array(4.6.0) lib_xud(2.3.1) lib_dsp(6.3.0)
Analyzing ad9508.xc
Analyzing wm8804.xc
Analyzing main.xc
C:/xmos_usb_audio_8.1.0/lib_xua/lib_xua/src/core/main.xc:570:19: warning: `c_spdif_rx' not used in two parallel statements [-Wunusual-code]
                , c_spdif_rx, c_adat_rx, c_clk_ctl, c_clk_int
                  ^~~~~~~~~~
Rebuild .build_2AMi2o2xxxxxx/_pca.rsp
Propagating analysis
Creating dependencies for main.xc
Creating dependencies for wm8804.xc
Creating dependencies for ad9508.xc
Compiling ad9508.xc
Compiling wm8804.xc
Compiling main.xc
C:/xmos_usb_audio_8.1.0/lib_xua/lib_xua/src/core/main.xc:570:19: warning: `c_spdif_rx' not used in two parallel statements [-Wunusual-code]
                , c_spdif_rx, c_adat_rx, c_clk_ctl, c_clk_int
                  ^~~~~~~~~~
Rebuild .build_2AMi2o2xxxxxx/_obj.rsp
Creating app_usb_aud_xk_316_mc_nick.xe
C:/xmos_usb_audio_8.1.0/lib_spi/lib_spi/src/spi_sync.xc: Error: Type of symbol `_Sspi_master_0.init.0' has mismatch with previous definition:
     found: `[[distributable]] [[combinable]] void _Sspi_master_0.init.0(unsigned int *unsafe , server interface spi_master_if[1], buffered out port:32, buffered out port:32?, buffered in port:32?, out port[])' in file `C:/xmos_usb_audio_8.1.0/lib_spi/lib_spi/src/spi_sync.xc'
  previous: `[[distributable]] [[combinable]] void _Sspi_master_0.init.0(unsigned int *unsafe , server interface spi_master_if[2], buffered out port:32, buffered out port:32?, buffered in port:32?, out port[])' in file `C:/xmos_usb_audio_8.1.0/lib_xua/lib_xua/src/core/main.xc'
xmake[1]: *** [bin/2AMi2o2xxxxxx/app_usb_aud_xk_316_mc_nick.xe] Error 1
xmake: *** [bin/2AMi2o2xxxxxx/app_usb_aud_xk_316_mc_nick.xe] Error 2

C:\xmos_usb_audio_8.1.0\sw_usb_audio\app_usb_aud_xk_316_mc_nick>
jarnot
Member++
Posts: 27
Joined: Thu Apr 15, 2010 4:52 pm

Post by jarnot »

I have a much more basic question about using the SPI library, having become somewhat rusty after not using the XMOS tools for over a decade. I wish to use spi_master_out_short and spi_master_in_short as described in 'XMOS SPI Component' Doc X3171A. but I have no idea how to install the contents of lib_spi-sw_3_0_2rc1 so that its contents are visible to the compiler. I cannot even find spi_master.h Any advice on how to overcome this basic obstacle would be very welcome.