Page 1 of 1

spiExchange problem

Posted: Tue Jun 18, 2019 3:30 pm
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?

Re: spiExchange problem

Posted: Tue Jun 18, 2019 3:47 pm
by Giovanni
Are pins programmed in the correct alternate mode?

Giovanni

Re: spiExchange problem

Posted: Tue Jun 18, 2019 3:55 pm
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

Re: spiExchange problem

Posted: Tue Jun 18, 2019 4:08 pm
by Giovanni
No idea, try looking at the SPI demo under /testhal, it works with loopback.

Giovanni

Re: spiExchange problem

Posted: Wed Jun 19, 2019 5:52 am
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.

Re: spiExchange problem

Posted: Wed Jun 19, 2019 7:41 am
by Giovanni
Consider that those files are customized, it is not what is generated by ChibiOS XML processor.

Giovanni

Re: spiExchange problem

Posted: Wed Jun 19, 2019 7:57 am
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?

Re: spiExchange problem

Posted: Wed Jun 19, 2019 8:23 am
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

Re: spiExchange problem

Posted: Wed Jun 19, 2019 9:37 am
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.