cancel
Showing results for 
Search instead for 
Did you mean: 

SPI Not communicating with aurdino

KuroGitsune
Associate II

So , I wrote all driver my self so dont know how to ask this but i can provide you with few code.

This is main.c file and other driver file is attached below........

#include <string.h>
#include "stm3f407xx.h"

void delay(void){
for(uint32_t i =0;i<500000/2;i++);
}

/*
* PB 12 --> NSS
* PB 13 --> SCLK
* PB 14 --> MISO
* PB 15 --> MOSI
*
* */

void SPI2_GPIOInint(void)
{
GPIO_Handle_t SPI_Pins;

SPI_Pins.pGPIOx = GPIOB;

SPI_Pins.GPIO_PinConfig.GPIO_PinMode = GPIO_MODE_ATLFN;
SPI_Pins.GPIO_PinConfig.GPIO_PinAFMode = 5;
SPI_Pins.GPIO_PinConfig.GPIO_PinOType = GPIO_OP_TYPE_PP;
SPI_Pins.GPIO_PinConfig.GPIO_PinPuPdControl = GPIO_NO_PUPD;
SPI_Pins.GPIO_PinConfig.GPIO_PinSpeed = GPIO_OP_SPEED_HI;

// SCLK
SPI_Pins.GPIO_PinConfig.GPIO_PinNumber = GPIO_PIN_NO_13;
GPIO_Init(&SPI_Pins);

 

// MISO
//SPI_Pins.GPIO_PinConfig.GPIO_PinNumber = GPIO_PIN_NO_14;
//GPIO_Init(&SPI_Pins);

// MOSI
SPI_Pins.GPIO_PinConfig.GPIO_PinNumber = GPIO_PIN_NO_15;
GPIO_Init(&SPI_Pins);

//NSS
SPI_Pins.pGPIOx = GPIOB;
SPI_Pins.GPIO_PinConfig.GPIO_PinMode = GPIO_MODE_OUT;
SPI_Pins.GPIO_PinConfig.GPIO_PinOType = GPIO_OP_TYPE_PP;
SPI_Pins.GPIO_PinConfig.GPIO_PinPuPdControl = GPIO_NO_PUPD;
SPI_Pins.GPIO_PinConfig.GPIO_PinSpeed = GPIO_OP_SPEED_MED;
SPI_Pins.GPIO_PinConfig.GPIO_PinNumber = GPIO_PIN_NO_12;
GPIO_Init(&SPI_Pins);
}

void SPI2_Inint(void)
{
SPI_Handle_t SPI_Handle;

SPI_Handle.pSPIx = SPI2;

SPI_Handle.SPIConfig.SPI_BusConfig = SPI_BUS_CONFIG_FD;
SPI_Handle.SPIConfig.SPI_DeviceMode = SPI_DEVICE_MODE_MASTER;
SPI_Handle.SPIConfig.SPI_SclkSpeed = SPI_SCLK_SPEED_DIV32; // to provide 2MHz clock
SPI_Handle.SPIConfig.SPI_SSM = SPI_SSM_DI; // Software slave management mode as we dont actually need NSS
SPI_Handle.SPIConfig.SPI_DFF = SPI_DFF_8BITS;
SPI_Handle.SPIConfig.SPI_CPHA = SPI_CPHA_LOW;
SPI_Handle.SPIConfig.SPI_CPOL = SPI_CPOL_LOW;


SPI_Init(&SPI_Handle);
}

/* FOR BUTTON */
void GPIO_Buttoninit()
{
GPIO_Handle_t Gpiobut;

Gpiobut.pGPIOx = GPIOA;
Gpiobut.GPIO_PinConfig.GPIO_PinNumber = GPIO_PIN_NO_0;
Gpiobut.GPIO_PinConfig.GPIO_PinMode = GPIO_MODE_IN;
Gpiobut.GPIO_PinConfig.GPIO_PinSpeed = GPIO_OP_SPEED_HI;
// Gpiobut.GPIO_PinConfig.GPIO_PinOType = GPIO_OP_TYPE_PP;
Gpiobut.GPIO_PinConfig.GPIO_PinPuPdControl = GPIO_NO_PUPD;

// GPIO_PeriClockControl(GPIOA,ENABLE); NO NEED AS WE ARE ENABLING IN INIT FUNCTION

GPIO_Init(&Gpiobut);
}

void GPIO_Led()
{
GPIO_Handle_t Gpioled;

Gpioled.pGPIOx = GPIOD;
Gpioled.GPIO_PinConfig.GPIO_PinNumber = GPIO_PIN_NO_12;
Gpioled.GPIO_PinConfig.GPIO_PinMode = GPIO_MODE_OUT;
Gpioled.GPIO_PinConfig.GPIO_PinSpeed = GPIO_OP_SPEED_HI;
Gpioled.GPIO_PinConfig.GPIO_PinOType = GPIO_OP_TYPE_PP;
Gpioled.GPIO_PinConfig.GPIO_PinPuPdControl = GPIO_NO_PUPD;

// GPIO_PeriClockControl(GPIOD,ENABLE);

GPIO_Init(&Gpioled);
}

int main(void)
{
char Userdata[] = "Hello World";

// Enable GPIOB
SPI2_GPIOInint();

GPIO_WriteToOutputPin(GPIOB, GPIO_PIN_NO_12, SET);
// Enable SPI2
SPI2_Inint();

GPIO_Buttoninit();

GPIO_Led();
GPIO_WriteToOutputPin(GPIOD, GPIO_PIN_NO_12, SET);

// This will enable SSI and make NSS High internally and avoid MODF Error
// SPI_SSIConfig(SPI2,ENABLE);

SPI_SSOEConfig(SPI2,ENABLE);

while(1){


// if(GPIO_ReadFromInputPin(GPIOA, GPIO_PIN_NO_0) == ENABLE){
// delay(); // To avoid pin bouncing
// GPIO_ToggleOutputPin(GPIOD,GPIO_PIN_NO_12);
// }
while(!GPIO_ReadFromInputPin(GPIOA,GPIO_PIN_NO_0));

delay();


// Enable SPI in CR1 register
SPI_PeripheralControl(SPI2,ENABLE);

// Slave need to know LENGTH INFO (1BYTE)
uint8_t datalen = strlen(Userdata);

GPIO_WriteToOutputPin(GPIOB, GPIO_PIN_NO_12, RESET);
GPIO_WriteToOutputPin(GPIOD, GPIO_PIN_NO_12, RESET);

SPI_SendData(SPI2,&datalen,1);

SPI_SendData(SPI2,(uint8_t*)Userdata,strlen(Userdata));

// Cannot disable abruptly
// So check if SPI is busy or not using STATUS REGISTER
while(SPI_GetFlagStatus(SPI2,SPI_BUSY_FLAG));

SPI_PeripheralControl(SPI2,DISABLE);

GPIO_WriteToOutputPin(GPIOB, GPIO_PIN_NO_12, SET);
GPIO_WriteToOutputPin(GPIOD, GPIO_PIN_NO_12, SET);

}

return 0;
}

@TDK 

@Peter BENSCH 

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

It would take me quite a lot of time to dig into the code and understand it, let alone figure out what may be the problem. I'm not sure I have that sort of time to devote, so I will provide some general advice instead:

  • Developing new drivers can be a good learning experience, but learning to use what is already there is useful as well. Especially if it's generally adopted by a large portion of the community. It's unlikely your employer will ask you to remake HAL. Perhaps they may ask you to make it faster.
  • I can see more benefit in accessing registers directly rather than making HAL-like functions. The reference manual will be a good source of information.

> So while debuggging First while loop so it works fine , it loops back as expected then i checked for register values and they do seem fine

Respectfully, this is still more non-information. If everything looks fine, then what is the problem? Are the signals coming out of the SPI correct? If not, how are they incorrect? I don't want to dig through 10 pictures in order to try to guess and find what the problem.

If you're very stuck, I would recommend getting some HAL SPI examples to run and comparing your register values between that working code and your own, in order to narrow down on the problem.

If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

7 REPLIES 7
TDK
Guru

Why redo functions that HAL already created? Going to be quite a bit of work and won't be very portable.

Can you provide more to go off of than just saying it isn't communicating? Have you done any debugging or verification at all?

If you feel a post has answered your question, please click "Accept as Solution".
KuroGitsune
Associate II

Yes I am aware that HAL created functions for tis task but its part of what i was learning, i was learning to write Whole drivers from scratch.

"Can you provide more to go off of than just saying it isn't communicating? Have you done any debugging or verification at all?"

Yes So I have done a lot of debugging and searching on communities for solution but didnt find any, So while debuggging First while loop so it works fine , it loops back as expected then i checked for register values and they do seem fine, i will attach values if you want.

here are more

KuroGitsune
Associate II

BTW i really appriciate you taking out some time for me

KuroGitsune
Associate II

Last of it

TDK
Guru

It would take me quite a lot of time to dig into the code and understand it, let alone figure out what may be the problem. I'm not sure I have that sort of time to devote, so I will provide some general advice instead:

  • Developing new drivers can be a good learning experience, but learning to use what is already there is useful as well. Especially if it's generally adopted by a large portion of the community. It's unlikely your employer will ask you to remake HAL. Perhaps they may ask you to make it faster.
  • I can see more benefit in accessing registers directly rather than making HAL-like functions. The reference manual will be a good source of information.

> So while debuggging First while loop so it works fine , it loops back as expected then i checked for register values and they do seem fine

Respectfully, this is still more non-information. If everything looks fine, then what is the problem? Are the signals coming out of the SPI correct? If not, how are they incorrect? I don't want to dig through 10 pictures in order to try to guess and find what the problem.

If you're very stuck, I would recommend getting some HAL SPI examples to run and comparing your register values between that working code and your own, in order to narrow down on the problem.

If you feel a post has answered your question, please click "Accept as Solution".
KuroGitsune
Associate II

Okay thank you I think I will try with hal and compare to see what's wrong on my side.