SPI getting stuck in write and read.

General discussion on mikroBasic PRO for ARM.
Post Reply
Author
Message
MG123
Posts: 54
Joined: 31 Jan 2017 12:47

SPI getting stuck in write and read.

#1 Post by MG123 » 04 Jul 2017 15:19

Hi.

I have the following SPI code for STM32 but is not working. I am using SPI3 with PB3, PB4, PB5 and PE1 for chip select.

When i debug the SPI3_Write(0x06); line the code gets stuck in a read loop. This section of assembly is run forever.

Code: Select all

L___Lib_SPI_123_SPIx_Read0:
; base start address is: 0 (R0)
0x0740	0xF2000208  ADDW	R2, R0, #8
0x0744	0x6813    LDR	R3, [R2, #0]
0x0746	0xF3C30200  UBFX	R2, R3, #0, #1
0x074A	0xB902    CBNZ	R2, L___Lib_SPI_123_SPIx_Read1
;__Lib_SPI_123.c, 70 :: 		
0x074C	0xE7F8    B	L___Lib_SPI_123_SPIx_Read0
L___Lib_SPI_123_SPIx_Read1:

Any idea why this is happening? This is my code:

Code: Select all

unsigned int take, buffer;

void main() {

  // Initialize the SPI1 module with default settings
  SPI3_Init();
  // PE1 on MCU is connected to CS on adc. Set high initially
  GPIO_Digital_Output(&GPIOE_ODR, _GPIO_PINMASK_1);        // Set PORTE Pin1 as output
  GPIOE_ODR = 1  ;                                         // Set PORTE high
  Delay_ms(1000);

  while(1) {
       // Select the device by seting chip select low
        GPIOE_ODR = 0  ;
        SPI1_Write(0x06);

        // read a word from the SPI bus
        take = SPI_Read(buffer);

        // Deselect the device
        GPIOE_ODR = 1  ;

        // delay some time before reading again
        Delay_ms(1000);
  }

}

MG123
Posts: 54
Joined: 31 Jan 2017 12:47

Re: SPI getting stuck in write and read.

#2 Post by MG123 » 05 Jul 2017 12:06

So there was a problem with the debugger and the device not being programmed. However the SPI does not work. Do these libraries actually work on the STM32? have they been tried and tested. I have tried them on 3 SPI's and am still not seeing any clock signal or data when viewed on the scope.

hexreader
Posts: 1786
Joined: 27 Jun 2010 12:07
Location: England

Re: SPI getting stuck in write and read.

#3 Post by hexreader » 05 Jul 2017 12:47

1 Please post C code on the C forum. Posting it on a BASIC forum is confusing (as happened on your last thread)

2 You forgot to tell us which STM32 you are using.

3 You do not give information on which ADC you are using

4 You have not given information on your configuration bits

5 " GPIOE_ODR = 1 ; // Set PORTE high" ... does NOT set PORTE high. It sets PORTE bit 0 high and other bits low

6 Have you disabled JTAG? - depending on which STM32 you use, this may, or may not be important.
Start every day with a smile...... (get it over with) :)

MG123
Posts: 54
Joined: 31 Jan 2017 12:47

Re: SPI getting stuck in write and read.

#4 Post by MG123 » 05 Jul 2017 13:30

Hi.

The processor I am using is the STM32F407VT and the ADC is the MCP3208.

When you say I haven't provided configuration bits, the code I provided is the complete code and I configured the SPI as described in the MikroC help files. Is there more to do here?

I am actually using GPIOE_ODR = 2 which sets pin PE1 and I can see it change so this is fine.

I have not disabled JTAG. JTAG is PA13-1PA15. I am using PB3-PB5 so this will not interfere.

I think the problem is with the SPI configuration. There is no activity on the clock or data pins. Is there a complete procedure available other to that in the help files.

hexreader
Posts: 1786
Joined: 27 Jun 2010 12:07
Location: England

Re: SPI getting stuck in write and read.

#5 Post by hexreader » 05 Jul 2017 14:00

I have adapted ADC click example for STM32F407 and attached it. (but I forgot to change the comments to new processor :( )

It is for ADC Click board in mikroBUS socket 1, EasyMxPRO v7 board and older, silver-surround EasyTFT display.

Unless you are using the same hardware, then it is unlikely to work, but you might get a few pointers from the code.

I have no idea whether the MCP3208 that you use is compatible with the MCP3204 code used in the example below.

As for configuration settings, load the below project, open "project" - "edit project" and you will see the configuration options.
Attachments
ADCclickSTM32F407.zip
(408.66 KiB) Downloaded 112 times
Start every day with a smile...... (get it over with) :)

MG123
Posts: 54
Joined: 31 Jan 2017 12:47

Re: SPI getting stuck in write and read.

#6 Post by MG123 » 05 Jul 2017 16:06

I stripped down that code for just the getADC bit and still have the same issue. The signals are not appearing on the lines and the return value is always 4095. The chip select goes high and low as expected, the other lines are as follows:

MOSI - High
MISO - Low
Clk - Low.

With regards to the config bits they are mainly to do with clock speed etc I dont think they will have any affect but they are set as follows:

HSI Oscillator - On
HSE Oscillator - Off
HSE Oscillator - Not bypassed
Clock Detector - Off
PLL - Off
PLLI2S - Off
PLLM = 16
PLLN = 0
PLLP = 2
HSI clock selected as PLL and PLLI2S clock entry
PLLQ = 4
HSI selected as system clock
SYSCLOCK not devided
HCLK not divided
no clock (HSE division factor for RTC clock)
HSI clock selected
PLLI2S clock used as I2S clock source
no devision (MC01 prescalor)
no devision (MC02 pre)
System clock (SYSCLK) selected (Mcu clock op 2)
Voltage renge = 2.7 to 3.6V
Prefetch and catch enabled

MG123
Posts: 54
Joined: 31 Jan 2017 12:47

Re: SPI getting stuck in write and read.

#7 Post by MG123 » 05 Jul 2017 16:38

So it turns out that I needed to change
&_GPIO_MODULE_SPI3_PB3_4_5
to
&_GPIO_MODULE_SPI3_PB345

No underscores needed. Its all working fine now.

hexreader
Posts: 1786
Joined: 27 Jun 2010 12:07
Location: England

Re: SPI getting stuck in write and read.

#8 Post by hexreader » 05 Jul 2017 16:57

All's well that ends well :)
Start every day with a smile...... (get it over with) :)

MG123
Posts: 54
Joined: 31 Jan 2017 12:47

Re: SPI getting stuck in write and read.

#9 Post by MG123 » 06 Jul 2017 09:13

Indeed it is.

Post Reply

Return to “mikroBasic PRO for ARM General”