Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to manually toggle SPI CS of Raspberry Pi?

emaq

Member level 3
Joined
Sep 17, 2015
Messages
65
Helped
0
Reputation
0
Reaction score
1
Trophy points
8
Activity points
622
How to manually toggle the chip select of Raspberry Pi 3 SPI in the following code?
My objective is to call transfer() twice for 16-bit transaction.

Code:
int
SPI::transfer(uint8_t *send, uint8_t *recv, unsigned len)
{
    if ((send == nullptr) && (recv == nullptr)) {
        return -EINVAL;
    }

    // set write mode of SPI
    int result = ::ioctl(_fd, SPI_IOC_WR_MODE, &_mode);

    if (result == -1) {
        PX4_ERR("can’t set spi mode");
        return PX4_ERROR;
    }

    spi_ioc_transfer spi_transfer{};

    spi_transfer.tx_buf = (uint64_t)send;
    spi_transfer.rx_buf = (uint64_t)recv;
    spi_transfer.len = len;
    spi_transfer.speed_hz = _frequency;
    spi_transfer.bits_per_word = 8;

    result = ::ioctl(_fd, SPI_IOC_MESSAGE(1), &spi_transfer);

    if (result != (int)len) {
        PX4_ERR("write failed. Reported %d bytes written (%s)", result, strerror(errno));
        return PX4_ERROR;
    }
 

KlausST

Super Moderator
Staff member
Joined
Apr 17, 2014
Messages
19,443
Helped
4,299
Reputation
8,603
Reaction score
4,260
Trophy points
1,393
Activity points
128,956
Hi,

Is the CS conneted directly to an RPi port pin? (or via external circuitry)

If connected to a port:
* How is the port pin defined? As GPIO or as dedicated SPI_hardware_controlled_CS pin?

Klaus
 

emaq

Member level 3
Joined
Sep 17, 2015
Messages
65
Helped
0
Reputation
0
Reaction score
1
Trophy points
8
Activity points
622
Hi,

Is the CS conneted directly to an RPi port pin? (or via external circuitry)

If connected to a port:
* How is the port pin defined? As GPIO or as dedicated SPI_hardware_controlled_CS pin?

Klaus
CS defined as dedicated SPI_hardware_controlled_CS pin.
 

KlausST

Super Moderator
Staff member
Joined
Apr 17, 2014
Messages
19,443
Helped
4,299
Reputation
8,603
Reaction score
4,260
Trophy points
1,393
Activity points
128,956
Hi,

then I expect it to work without additional coding.

What happens? Do you have a scope to check function?

Klaus
 

    emaq

    Points: 2
    Helpful Answer Positive Rating

emaq

Member level 3
Joined
Sep 17, 2015
Messages
65
Helped
0
Reputation
0
Reaction score
1
Trophy points
8
Activity points
622
Hi,

then I expect it to work without additional coding.

What happens? Do you have a scope to check function?

Klaus
It worked... the correct sequence is to send 2 consecutive bytes and after that receive 2 bytes.
Thanks.
 

Aussie Susan

Advanced Member level 4
Joined
Jan 5, 2015
Messages
1,247
Helped
377
Reputation
754
Reaction score
375
Trophy points
83
Activity points
13,504
Using the internally controlled CS signal from any hardware SPI module is notoriously difficult. Generally you nede to keep the input buffer full (or at least not empty) for the hardware to think that all bytes are part of the same 'message' and not toggle the CS signal after each byte.
Sending 2 bytes and then receiving 2 bytes will work in your case as you are probably dealing with hardware that has a FIFO that is larger (often 4 or 8 bytes). However this is not a good 'general' approach. When you need to exchange longer messages, you probably il need to go to a scheme where you either use interrupts or monitor the module status flags to 1) keep the Tx buffer full (to keep the CS signal in the required state) and 2) clear the Rx buffer so you don't get overflows.
Susan
 

LaTeX Commands Quick-Menu:

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top