[SOLVED] spi remap

Working libraries, libraries being ported and related hardware
Post Reply
octavio
Posts: 44
Joined: Sat Apr 15, 2017 6:41 pm
Location: barcelona
Contact:

[SOLVED] spi remap

Post by octavio » Sun Jun 04, 2017 11:57 am

Hello i want to move spi1 (stm32f1 bluepill) from pins pa5-pa7 to alternate port pb3-pb5 ,and tried this:

Code: Select all


#define BOARD_SPI1_MOSI_PIN       PB5
#define BOARD_SPI1_MISO_PIN       PB4
#define BOARD_SPI1_SCK_PIN        PB3

#include <SdFat.h>
in the setup function :

Code: Select all

 AFIO_BASE->MAPR=AFIO_MAPR_SPI1_REMAP;
   if (!sd.begin(PB9, SPI_FULL_SPEED)) {
also tried do to this:

Code: Select all

 pinMode(PB3,OUTPUT);
    pinMode(PB4,INPUT);
     pinMode(PB5,OUTPUT);
and does not works ,some ideas about how to to it?

stevestrong
Posts: 1813
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany

Re: spi remap

Post by stevestrong » Sun Jun 04, 2017 2:40 pm

Check this post.

octavio
Posts: 44
Joined: Sat Apr 15, 2017 6:41 pm
Location: barcelona
Contact:

Re: spi remap

Post by octavio » Sun Jun 04, 2017 9:36 pm

Thanks,now i disabled debug with "AFIO_BASE->MAPR=AFIO_MAPR_SPI1_REMAP+AFIO_DEBUG_NONE;"
And also verified the wiring many times,but istill there is something wrong.

stevestrong
Posts: 1813
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany

Re: spi remap

Post by stevestrong » Mon Jun 05, 2017 7:31 am

Have you read the user's manual RM0008 chapter 9.4?
"9.4 AFIO registers
Refer to Section 2.1 on page 47for a list of abbreviations used in register descriptions.
Note: To read/write the AFIO_EVCR, AFIO_MAPR and AFIO_EXTICRX registers,
the AFIO clock
should first be enabled.
Refer to Section 7.3.7: APB2 peripheral clock enable register
(RCC_APB2ENR).
The peripheral registers have to be accessed by words (32-bit).
"

So I think you should try:

Code: Select all

RCC_BASE->APB2ENR |= (uint32_t)RCC_APB2ENR_AFIOEN; // enable AFIO clock

delay(1); // maybe not needed

AFIO_BASE->MAPR = (uint32_t)(AFIO_MAPR_SPI1_REMAP | AFIO_DEBUG_NONE); // set AFIO register

octavio
Posts: 44
Joined: Sat Apr 15, 2017 6:41 pm
Location: barcelona
Contact:

Re: spi remap

Post by octavio » Mon Jun 05, 2017 9:15 am

Clock is enabled by default,i have verified register values with "Serial.println".I will have to write some test code to know what happens.

stevestrong
Posts: 1813
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany

Re: spi remap

Post by stevestrong » Mon Jun 05, 2017 10:22 am

It may happen, that the SPI class device is initialized (declared) in the setup phase before jump into setup(), before you setup the AFIO register.

octavio
Posts: 44
Joined: Sat Apr 15, 2017 6:41 pm
Location: barcelona
Contact:

Re: spi remap

Post by octavio » Mon Jun 05, 2017 9:59 pm

After the spi remap and spi init with "spi_init(SPI1);" i still can change the ports with "digitalWrite(PB5,0);" .
Conclusion: spi alternate port do not works.I will connect the wires in the default ports.

sslobodyan
Posts: 2
Joined: Tue Jan 26, 2016 3:22 pm

Re: spi remap

Post by sslobodyan » Wed Aug 09, 2017 12:14 am

afio_cfg_debug_ports(AFIO_DEBUG_SW_ONLY); // release PB3 and PB5
afio_remap(AFIO_REMAP_SPI1); // remap SPI1

gpio_set_mode(GPIOB, 3, GPIO_AF_OUTPUT_PP);
gpio_set_mode(GPIOB, 4, GPIO_INPUT_FLOATING);
gpio_set_mode(GPIOB, 5, GPIO_AF_OUTPUT_PP);

This works ok.

octavio
Posts: 44
Joined: Sat Apr 15, 2017 6:41 pm
Location: barcelona
Contact:

Re: spi remap

Post by octavio » Tue Aug 15, 2017 8:26 pm

Yes it works,thanks.

octavio
Posts: 44
Joined: Sat Apr 15, 2017 6:41 pm
Location: barcelona
Contact:

Re: [SOLVED] spi remap

Post by octavio » Mon Nov 06, 2017 5:39 pm

It works but the spi library still configures the default spi pins making it useless for other purposes.how can i tell to the spi lib to use the remaped pins instead?

Post Reply