spiExchange problem Topic is solved

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderator: RoccoMarco

Post Reply
ceremcem
Posts: 66
Joined: Mon Aug 10, 2015 6:57 am
Has thanked: 7 times
Been thanked: 6 times

spiExchange problem

Post by ceremcem »

Hi,

I'm trying to echo what I send via MOSI with MISO pin, so I just short circuited the MOSI and MISO pins. [url=Here is the code:

Code: Select all

int main(void) {
  halInit();
  chSysInit();
  init_io();

  // start the blinker thread
  chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);

  spiStart(&SPID1, &test_spicfg);
  spiSelect(&SPID1);

  uint8_t txbuffer[2];
  uint8_t rxbuffer[2];
  uint8_t offset = 0;
  uint8_t num_zero = 48;
  rxbuffer[1] = 0x33;
 
  txbuffer[0] = 65; // A

  while (true) {
    chThdSleepMilliseconds(1); //// debugger
    txbuffer[1] = num_zero + offset;
    #if ! defined MOSI_MISO_CONNECTED
      // will send: "A0", "A1", ...
      spiSend(&SPID1, 2, &txbuffer);
    #else
      // will send: "A0", "01", "12", "23", ...
      spiExchange(&SPID1, 2, &txbuffer, &rxbuffer);

      txbuffer[0] = rxbuffer[1]; // debugger <--------------- this reads always {0, 0}
    #endif
    offset++;
    if (offset > 9u) {
      offset = 0; 
    }
  }
 
 


Exact code is here.. SPI Config struct:

Code: Select all

SPIConfig test_spicfg = {
    circular: false,
    end_cb: NULL,
    ssport: GPIOA,
    cr1: (SPI_CR1_BR_2 | SPI_CR1_BR_1 | SPI_CR1_MSTR | SPI_CR1_BIDIOE),
    cr2: (SPI_CR2_DATASIZE_8BIT)
};


Logic analyzer screenshot when I connected MISO to VDD:

Image

Problem

The "rxbuffer" is always {0,0}. What am I possibly doing wrong?
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: spiExchange problem

Post by Giovanni »

Are pins programmed in the correct alternate mode?

Giovanni
ceremcem
Posts: 66
Joined: Mon Aug 10, 2015 6:57 am
Has thanked: 7 times
Been thanked: 6 times

Re: spiExchange problem

Post by ceremcem »

Giovanni wrote:Are pins programmed in the correct alternate mode?

Giovanni


I think so: https://github.com/ceremcem/chibi-examp ... .h#L41-L67
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: spiExchange problem

Post by Giovanni »

No idea, try looking at the SPI demo under /testhal, it works with loopback.

Giovanni
ceremcem
Posts: 66
Joined: Mon Aug 10, 2015 6:57 am
Has thanked: 7 times
Been thanked: 6 times

Re: spiExchange problem

Post by ceremcem »

It turns out I had a problem with configuring the IO pin alternate mode. This line helped me to make this fix:

Code: Select all


 #define VAL_GPIOA_MODER             (GPIOA_MODER_DEFAULT \
                                         | PIN_MODE_ALTERNATE(GPIOA_SPI1_MOSI) \
-                                        | PIN_MODE_INPUT(GPIOA_SPI1_MISO) \
+                                        | PIN_MODE_ALTERNATE(GPIOA_SPI1_MISO) \
                                         | PIN_MODE_ALTERNATE(GPIOA_SPI1_SCLK) \
                                         )


Thank you.
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: spiExchange problem

Post by Giovanni »

Consider that those files are customized, it is not what is generated by ChibiOS XML processor.

Giovanni
ceremcem
Posts: 66
Joined: Mon Aug 10, 2015 6:57 am
Has thanked: 7 times
Been thanked: 6 times

Re: spiExchange problem

Post by ceremcem »

Giovanni wrote:Consider that those files are customized, it is not what is generated by ChibiOS XML processor.

Giovanni


I may not quite get how I should conclude this. Do you mean that the examples under /testhal are more reliable as a reference since they aren't autogenerated?
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: spiExchange problem

Post by Giovanni »

I mean, in general it is better to look at official demos first. For me is hard to provide support on modified code, I don't know how deep changes are nor the rationale for those.

Giovanni
ceremcem
Posts: 66
Joined: Mon Aug 10, 2015 6:57 am
Has thanked: 7 times
Been thanked: 6 times

Re: spiExchange problem

Post by ceremcem »

Giovanni wrote:For me is hard to provide support on modified code
Giovanni


That's quite understandable. I was not aware that there were examples in the /testhal directory (I thought they were only for tests, so they are for ChibiOS developers, not for ChibiOS users). That was very useful to know.
Post Reply