No SPI output - beginner issue?

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

User avatar
Tectu
Posts: 1225
Joined: Thu May 10, 2012 9:50 am

No SPI output - beginner issue?

Post by Tectu »

Hello There,

I try to interface my ADS7843 touchpad controller over SPI1, but it looks like I am doing something horrible wrong at the SPI part. When I write something using spiSend() or spiExchange() I cannot see any clock on my SPI_SCK (PORTA.5).

Here is my code:

Code: Select all

#include "touchpad.h"

static const SPIConfig spicfg = {
    NULL,
    GPIOC,
    GPIOC_TP_CS,
    SPI_CR1_DFF
};

void tpInit(void) {
    spiStart(&SPID1, &spicfg);
}

// just for testing the SPI output
void tpTest(void) {
    uint8_t tx;

    TP_CS(0);
    palClearPad(GPIOD, GPIOD_LED3);
    spiSend(&SPID1, 1, &tx);
    palSetPad(GPIOD, GPIOD_LED3);
    TP_CS(1);
}

uint32_t tpReadX(void) {
    uint8_t tx, rx;

    TP_CS(0);
    tx = 0x9b;
    spiExchange(&SPID1, 1, &tx, &rx);
    TP_CS(1);   

    return rx;
}

uint32_t tpReadY(void) {
    uint8_t tx, rx;

    TP_CS(0);
    tx = 0xd8;
    spiExchange(&SPID1, 1, &tx, &rx);
    TP_CS(1);

    return rx;
}


Well, when I run tpTest() in a loop, the LED flashes (to indicate that it dosen't crash or something), but nothing happens on the SPI1_CSK line or any of the other SPI1_MISO/MOSI lines.

I have really no clue what I am doing wrong. I did everything exactly like I read it in the documentation. No callback function when using manual CS lines etc. etc.
tpInit() gets called too.
I have SPI1 enabled in mcuconfig.h


Thanks for the help.

~ Tectu
utzig
Posts: 359
Joined: Sat Jan 07, 2012 6:22 pm
Has thanked: 1 time
Been thanked: 20 times

Re: No SPI output - beginner issue?

Post by utzig »

I pretty sure you should be calling spiSelect, spiUnselect. Like this:

Code: Select all

    TP_CS(0);
    palClearPad(GPIOD, GPIOD_LED3);
    spiSelect(&SPID1);
    spiSend(&SPID1, 1, &tx);
    spiUnselect(&SPID1);   
    palSetPad(GPIOD, GPIOD_LED3);
    TP_CS(1);


Not sure if this will solve your problem but it doesn't hurt trying...

Fabio Utzig
utzig
Posts: 359
Joined: Sat Jan 07, 2012 6:22 pm
Has thanked: 1 time
Been thanked: 20 times

Re: No SPI output - beginner issue?

Post by utzig »

Hmmm, I guess your TP_CS(0) macro already does the same that spiSelect.

Fabio Utzig
User avatar
Tectu
Posts: 1225
Joined: Thu May 10, 2012 9:50 am

Re: No SPI output - beginner issue?

Post by Tectu »

Thanks for the suggestion, but this didn't solve the problem, and I am not wondered that it didn't.
With my understanding of the documentation, spiSelect() and spiUnselect() just do the ChipSelect Line stuff which I am doing myself with TP_CS(0) and TP_CS(1).

I added a snapshot of the board setup. As you can see, also the SPI1_NSS line is not used for the SPI. TP_CS() does manipulate GPIOC 6.

Image
utzig
Posts: 359
Joined: Sat Jan 07, 2012 6:22 pm
Has thanked: 1 time
Been thanked: 20 times

Re: No SPI output - beginner issue?

Post by utzig »

Ok, are you sure you configured the pins correctly on the board.h file?

Based on your schematic, your board.h should have something like this:

Code: Select all

#define VAL_GPIOACRL   0xB4B44444


Ignore bits 0-4 from the configuration above. Basically PA5 and PA7 should be configured as Alternate outputs and PA6 as input floating.

Fabio Utzig
User avatar
Tectu
Posts: 1225
Joined: Thu May 10, 2012 9:50 am

Re: No SPI output - beginner issue?

Post by Tectu »

Well, this fixed the problem indeed...
That also explains why ADC didn't work last time...

When I created the board files for my board, I just did the LEDs and Buttons, to be sure that I am doing it right...

Now I only need to bring that ADS7843 to work. (Anyone already did?)

Thank you very very much for your help!
utzig
Posts: 359
Joined: Sat Jan 07, 2012 6:22 pm
Has thanked: 1 time
Been thanked: 20 times

Re: No SPI output - beginner issue?

Post by utzig »

I wrote an ADS7843 driver last year and it was based on the code of the ITDB02_Touch library for Arduino. You can get it here:

http://henningkarlsen.com/electronics/library.php?id=35

The code of this library uses GPIO pins to access the SPI interface of the ADS7843 but it should be quite simple to convert it to use the SPI driver.

Fabio Utzig
User avatar
Tectu
Posts: 1225
Joined: Thu May 10, 2012 9:50 am

Re: No SPI output - beginner issue?

Post by Tectu »

I came arround that link you postet too. I was just curios that they do software SPI.

Would you provide your ADS7843 code?
utzig
Posts: 359
Joined: Sat Jan 07, 2012 6:22 pm
Has thanked: 1 time
Been thanked: 20 times

Re: No SPI output - beginner issue?

Post by utzig »

It would not help you 'cause I also did it using GPIO pins! :P

The ADS7843 is extremely simple. You just have to send one byte of command and then read the ADC conversion (which is 12bits). It should be something like:

Code: Select all

uint8_t txbuf[1];
uint8_t rxbuf[2];
uint16_t x, y;

txbuf[0] = 0x90;
spiSend(&SPD1, 1, txbuf);
spiReceive(&SPD1, 2, rxbuf);

x = rxbuf[0] << 4;
x |= rxbuf[1] >> 4;

txbuf[0] = 0xD0;
spiSend(&SPD1, 1, txbuf);
spiReceive(&SPD1, 2, rxbuf);

y = rxbuf[0] << 4;
y |= rxbuf[1] >> 4;


This code is written here out of my mind and may not work exactly like this. But it should give you some idea!

The commands are really 0x90 and 0xd0 to get the horizontal and vertical ADC conversions. The returned data is 12bit long so you read 2 bytes but only use the most significant 12bits of data.

Usually you should do at least 3 ou 5 readings every time you receive an IRQ from the ADS7843 and calculate the mean 'cause usually there's noise in the conversions.

Fabio Utzig
User avatar
Tectu
Posts: 1225
Joined: Thu May 10, 2012 9:50 am

Re: No SPI output - beginner issue?

Post by Tectu »

I asked for the code to get a full overview, because I am still not sure in one poing: The timing diagrams do show that there is still a clock needed (of course). I am not sure if I have to provide that myself somehow, or if the underlying SPI abstraction does it for me.

I've never used SPI before, that's why I am not sure at that point.


~ Tectu
Post Reply