2023-10-25 11:07 AM
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;
}
Solved! Go to Solution.
2023-10-26 07:53 AM
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:
> 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.
2023-10-25 12:13 PM
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?
2023-10-26 04:38 AM - edited 2023-10-26 04:43 AM
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.
2023-10-26 04:39 AM
2023-10-26 04:40 AM
2023-10-26 04:41 AM
2023-10-26 07:53 AM
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:
> 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.
2023-10-26 07:34 PM
Okay thank you I think I will try with hal and compare to see what's wrong on my side.