Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Problem with SPI communication

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
pinkfloyd11

Bascom Member



Joined: 02 Jul 2007
Posts: 247

italy.gif
PostPosted: Tue Mar 17, 2015 11:00 am    Post subject: Problem with SPI communication Reply with quote

Hello

I'm developing firmware on device that use C1101 Transceiver.
The device is already working. I should only rewrite code for add new functionality.
My test read 2 time the same Rom Register and make confrontation but not work because stop on while cicle.
Can you help me?
Thanks


'*********************************
'Direttive al compilatore
'*********************************
$Regfile="m48pdef.dat"
$Crystal=8000000
$hwstack=40
$swstack=16
$framesize=32
'*********************************


Open "COMB.0:57600,8,n,1" For Output As #1
Open "COMB.1:57600,8,n,1" For Input As #2

Print #1 , "CC1101 TEST BOARD"

CONFIG Spi = Hard , Interrupt = Off , Data Order = Msb , Master = Yes , Polarity = Low , Phase = 0 , Clockrate = 16, NOSS = 0
SPIINIT

Byte_Ricevuto_1 = 11
Byte_Ricevuto_2 = 22


'READ 1
SET LED_ROSSO
RESET CHIP_SELECT
Print #1, "Entro nel While"
while PINB.4=1
wend
Print #1, "Uscito dal While"

SPIout Reg_Part_Number, 1
SPIin Byte_Ricevuto_1, 1

SET CHIP_SELECT
waitms 200
RESET LED_ROSSO

Wait 1

'READ 2
SET LED_VERDE
RESET CHIP_SELECT
SPIout Reg_Part_Number, 1
SPIin Byte_Ricevuto_2, 1
SET CHIP_SELECT
waitms 200
RESET LED_VERDE

waitms 400

IF Byte_Ricevuto_1 = Byte_Ricevuto_2 THEN

Print #1, "PRIMA LETTURA"; Byte_Ricevuto_1
Print #1, "SECONDA LETTURA"; Byte_Ricevuto_2
Print #1, ""
SET LED_ROSSO
WAIT 10
END IF



Example followed:

-----------------------------------------------------------------
unsigned char CC1101::ReadReg(unsigned char addr)
{
unsigned char x;
wait(0.000005);
_csn = 0;
wait(0.000002);
while (_RDmiso);
_spi.write(addr | READ_SINGLE);
x = _spi.write(0);
wait(0.000002);
_csn = 1;
return x;
}// ReadReg

---------------------


---------------------------------------------------------------
byte CC1101::readReg(byte regAddr, byte regType)
{
byte addr, val;

addr = regAddr | regType;
cc1101_Select(); // Select CC1101
wait_Miso(); // Wait until MISO goes low
spi.send(addr); // Send register address
val = spi.send(0x00); // Read result
cc1101_Deselect(); // Deselect CC1101

return val;
}

----------------------------------------------------------------

(BASCOM-AVR version : 2.0.7.8 )
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 6198
Location: Holland

blank.gif
PostPosted: Tue Mar 17, 2015 1:33 pm    Post subject: Reply with quote

you use : NOSS = 0
this means bascom handles the SS signal. Maybe that is why you have CHIP_SELECT, but i do not see any definition for that pin.
your code will only work when CHIP_SELECT is not the chip SS pin.

i do not get the original code. i see _csn = 1; but also cc1101_Select(); i guess they control the same pin?
i also do not get why to wait for miso low? it is all handled by the protocol. Then there are waits (in us i guess). I would recommend to read the data sheet. and then use the bascom spi accordingly. when the clock rate of the SPI and the mode matches, there is no point in waiting for levels and put in delays.

when working with SPI it always works the same : you connect the proper pins. you chose the right mode, you init the pins for the proper level. you either select CS pin, or let bascom handle it (NOSS) and you write/read data.
In order to read a byte, you must write some value. When done you deselect the CS.

_________________
Mark
Back to top
View user's profile Visit poster's website
pinkfloyd11

Bascom Member



Joined: 02 Jul 2007
Posts: 247

italy.gif
PostPosted: Tue Mar 17, 2015 4:38 pm    Post subject: Reply with quote

Hello

With software SPI I see something on oscilloscope...

I will show you PROTEUS simulation of the follow code.

It write then read and finally compare the value. The register is 0x04 SYNC1


'////////////////////////////////////////////
' SPI SEND and RECEIVE COMMAND
'////////////////////////////////////////////

'*********************************
'Direttive al compilatore
'*********************************
$Regfile="m48pdef.dat"
$Crystal=8000000
$hwstack=40
$swstack=16
$framesize=32
'*********************************
'$Include "CC1101 REGISTER.inc"

Open "COMB.0:57600,8,n,1" For Output As #1
Open "COMB.1:57600,8,n,1" For Input As #2

Print #1 , "CC1101 TEST BOARD"
Config Spi = Soft , Din = Pinb.3 , Dout = Portb.4 , Ss = Portb.2 , Clock = Portb.5

SPIINIT

DECLARE SUB CONFRONTA_DUE_LETTURE()
DECLARE SUB SCRIVI_LEGGI_E_CONFRONTA()


LED_ROSSO Alias PortD.3
CONFIG LED_ROSSO = Output

LED_VERDE Alias PortC.2
CONFIG LED_VERDE = Output


CHIP_SELECT Alias Portb.2
CONFIG CHIP_SELECT = Output

Dim Reg_Part_Number as Byte
Dim Reg_Curr_Version as Byte
Dim Reg_Sync_W as Byte
Dim Reg_Sync_R as Byte

Dim Byte_Ricevuto_1 as Byte
Dim Byte_Ricevuto_2 as Byte

Dim Valore_Scritto as Byte
Dim Valore_Letto as Byte
Dim S As String * 8

'Dal Datasheet table 43
'0x04 SYNC1 Sync word, high byte
'[7] := Header --> 1
'[6] := Burst --> 0
'[5-0] := Indirizzo Registro --> 0x00 --> 000100
'[Byte completo] = 00000100
S = "00000100"
Reg_Sync_W = binval (S)


'Dal Datasheet table 43
'0x04 SYNC1 Sync word, high byte
'[7] := Header --> 1
'[6] := Burst --> 0
'[5-0] := Indirizzo Registro --> 0x00 --> 000100
'[Byte completo] = 00000100
S = "10000100"
Reg_Sync_R = binval (S)

'Dal Datasheet table 44:
'0x30 (0xF0) PARTNUM Part number
'[7] := Header --> 1
'[6] := Burst --> 0
'[5-0] := Indirizzo Registro --> 0x30 --> 110000
'[Byte completo] = 10110000
S = "10110000"
Reg_Part_Number = binval (S)

'0x31 (0xF1) VERSION Current version number
'[7] := Header --> 1
'[6] := Burst --> 0
'[5-0] := Indirizzo Registro --> 0x30 --> 110001
'[Byte completo] = 10110001
S = "10110001"
Reg_Curr_Version = binval (S)

DO


'CALL CONFRONTA_DUE_LETTURE
CALL SCRIVI_LEGGI_E_CONFRONTA


Wait 1

LOOP

'_______________________________________________________________________________________

SUB CONFRONTA_DUE_LETTURE()

Byte_Ricevuto_1 = 11
Byte_Ricevuto_2 = 22


'READ 1
SET LED_ROSSO

RESET CHIP_SELECT
while PINB.3=1
wend

SPIout Reg_Part_Number, 1
SPIin Byte_Ricevuto_1, 1
SET CHIP_SELECT

waitms 200
RESET LED_ROSSO

Wait 1

'READ 2
SET LED_VERDE

RESET CHIP_SELECT
while PINB.3=1
wend

SPIout Reg_Part_Number, 1
SPIin Byte_Ricevuto_2, 1
SET CHIP_SELECT

waitms 10
RESET LED_VERDE

waitms 400

IF Byte_Ricevuto_1 = Byte_Ricevuto_2 THEN

Print #1, "PRIMA LETTURA= "; Byte_Ricevuto_1
Print #1, "SECONDA LETTURA= "; Byte_Ricevuto_2
Print #1, ""

SET LED_ROSSO
WAIT 5
RESET LED_ROSSO


END IF

END SUB

'_______________________________________________________________________________________
SUB SCRIVI_LEGGI_E_CONFRONTA()

Valore_Scritto = 11
Valore_Letto = 22

'WRITE ROSSO
SET LED_ROSSO

RESET CHIP_SELECT

while PINB.3=1
wend

SPIout Reg_Sync_W, 1
SPIout Valore_Scritto, 1

SET CHIP_SELECT

Waitms 10
RESET LED_ROSSO


'READ VERDE
SET LED_VERDE

RESET CHIP_SELECT

while PINB.3=1
wend

SPIout Reg_Sync_R, 1
SPIin Valore_Letto, 1
SET CHIP_SELECT

waitms 200
RESET LED_VERDE

Print #1, "VALORE LETTO= "; Valore_Letto
Print #1, ""

IF Valore_Scritto = Valore_Letto THEN

Print #1, "VALORI UGUALI= "

SET LED_ROSSO
WAIT 5
RESET LED_ROSSO
WAIT 1
END IF

END SUB
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 6198
Location: Holland

blank.gif
PostPosted: Tue Mar 17, 2015 10:21 pm    Post subject: Reply with quote

now you switch from HW SPI to soft SPI. i really do not have time to check this out.
I already gave advise. What i should add : use a REAL micro. proteus is great but i have seen simulation fail where real hardware worked. And how is it to connect SPI pins to a SPI slave?
you should see SPI signals for HW SPI too. when you have the choice, always use the HW SPI.

_________________
Mark
Back to top
View user's profile Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum