Problem in receivng data using SPI

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
nikhiledutech
Posts: 118
Joined: Wed Dec 27, 2017 8:52 am

Problem in receivng data using SPI

Post by nikhiledutech » Mon May 07, 2018 8:39 am

Hey ,

I am currently using SPI 2 on stm32f407 disc board. I have defined the Stm32 pins in the below given manner.
PB10 ------> SPI2_SCK
PC2 ------> SPI2_MISO
PC3 ------> SPI2_MOSI
my CS pin is " PB14" which i have defined
cs_pin = Pin('PB14', Pin.OUT_PP, Pin.PULL_NONE)


From my understanding of SPI used for serial communication, we have to take CS pin low/high to read or write any data.

Now to perform a write a single byte, I have to toggle CS pin from high to low state, than write the data and than toggle the CS pin state to higher state. Similar procedure goes with Read cycle.


But problem here is whenever i try to receive data, it gives me "0xff " in data buffer.

Code: Select all


from pyb import SPI , Pin

#Initialising Chip select pin
cs_pin = Pin('PB14', Pin.OUT_PP, Pin.PULL_NONE)

#Initialising SPI
spi = SPI(2,SPI.MASTER)



#Defined Register
EEP_WRITE	=  0x02 		# Write bit of IR     
EEP_READ	=   0x03  		# Read bit of IR      
EEP_WREN	=   0x06  		# Write Enable Latch  
EEP_WRDI	=   0x04  		# Write Disable Latch 
EEP_RDSR	=   0x05  		# Read Status Reg     
EEP_WRSR	=   0x01  		# Write Status Reg    



# @brief Drive CS output pin to low/high level to select slave device
# *        via /CS pin state
# * @param state   state State of CS output pin that will be driven:
# *        @arg 0 (DISABLE): Drive CS pin to low level
# *        @arg 1 (ENABLE) : Drive CS pin to high level
# * @return  None
DISABLE = 0 
ENABLE = 1


def SPI2_CS_Force (state):
	if (state):
		cs_pin.value(1)
	else:
		cs_pin.value(0)

# * @brief This Function configures and Initializes SPI2
# * @retval None
def ASK25_SPI2_Init( ):
	spi = SPI(2,SPI.MASTER, prescaler = 128, polarity = 0, phase = 0, bits = 8, firstbit = SPI.MSB , ti = False, crc = None)



# @brief Get the status about locked or unlocked memory blocks.
# @return  status register byte
def ASK25_25AA160A_Read_Status_Reg ():
	reg = bytearray(35)
	status = 0
	inst = bytearray(35)
	for x in range (35):
		inst[x] = x

	inst[0] = EEP_RDSR
	cs_pin.value(0)
	spi.send(inst, timeout = 500)
	cs_pin.value(1)
	
	pyb.delay(10)

	cs_pin.value(0)
	status = spi.recv(reg, timeout = 500)
	cs_pin.value(1)	
	print(reg)		



def main():
	while True:
		ASK25_25AA160A_Read_Status_Reg()
		pyb.delay(100)


main()



Have anyone faced the same issue ?

jickster
Posts: 629
Joined: Thu Sep 07, 2017 8:57 pm

Re: Problem in receivng data using SPI

Post by jickster » Mon May 07, 2018 11:12 pm

(1) you are bitbanging SPI in Python; that is a terrible terrible terrible idea. Simply terrible.

This will result in an excruciatingly slow SPI implementation.

Are you ok with that?

Python is not meant to be used to bitbang protocols. If you want to bitbang, you should do it in assembly or C.

Your micro has built SPI peripheral
http://www.st.com/en/microcontrollers/s ... 7-417.html

You should be using a uPy lib that accesses the SPI peripheral like in this

http://docs.micropython.org/en/v1.9.3/p ... b.SPI.html


Once again: terrible idea.

(2) you are confusing SPI and I2C.

In SPI: data is sampled/latched on the edge of a clock (whether pos vs neg is configurable)
CS is used to indicate to a slave that it is being addressed and that communication is active but it definitely is NOT toggled to latch the bits.

In I2C: there’s 2 lines sda, scl. Data is sampled on the edge of scl.







Sent from my iPhone using Tapatalk

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Problem in receivng data using SPI

Post by pythoncoder » Tue May 08, 2018 8:56 am

jickster wrote:
Mon May 07, 2018 11:12 pm
(1) you are bitbanging SPI in Python; that is a terrible terrible terrible idea. Simply terrible...
Not as far as I can see. From his code:

Code: Select all

from pyb import SPI , Pin
# lines omitted
spi = SPI(2,SPI.MASTER)
That is hardware SPI, and its use and the use of CS looks right to me.

However the function ASK25_SPI2_Init() is never called - @nikhiledutech is that your intention?

It's hard to debug this sort of thing without the hardware you're using. If you have access to an oscilloscope or logic analyser I'd use that to find out what's going on at the hardware level.
Peter Hinch
Index to my micropython libraries.

nikhiledutech
Posts: 118
Joined: Wed Dec 27, 2017 8:52 am

Re: Problem in receivng data using SPI

Post by nikhiledutech » Tue May 08, 2018 9:43 am

Hey @pythoncoder,
I intentionally didn't called Initialization function, as i already have defined the SPI in MASTER mode at the start of code.

Yeah, i will be checking this at hardware level.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Problem in receivng data using SPI

Post by pythoncoder » Tue May 08, 2018 4:27 pm

nikhiledutech wrote:
Tue May 08, 2018 9:43 am
...I intentionally didn't called Initialization function...
I guessed as much, and can't see anything obviously wrong. In general the purpose of the CS line is to enable one of N slave devices to be selected so that one master can talk to several slaves. Each device has its own CS controlled by its own pin on the master. So asserting the CS of the device you're addressing before each read or write, and de-asserting it afterwards, should be fine. But you need to check the datasheet of the device you're attached to. It may have specific requirements.

Good luck with the hardware debugging.
Peter Hinch
Index to my micropython libraries.

nikhiledutech
Posts: 118
Joined: Wed Dec 27, 2017 8:52 am

Re: Problem in receivng data using SPI

Post by nikhiledutech » Tue May 08, 2018 5:08 pm

Okay Sir. Thanks will surely be looking at it.

nikhiledutech
Posts: 118
Joined: Wed Dec 27, 2017 8:52 am

Re: Problem in receivng data using SPI

Post by nikhiledutech » Tue May 15, 2018 11:17 am

Hey Sir, sorry i didn't described my question correctly.

I wanted to interface SPI based EEPROM, but as you see we dont have any functions to write and read from specified address.

Post Reply