-1
\$\begingroup\$

I am trying to send the angle calculated(floating) from an external gyroscope sensor connected to a stm32f4 discovery board to a sd card via SPI.

I have the following code for initialization, sending data and receiving data and I am thinking that I can send data and then use the read function to read from the same address and check if both the functions are working properly or not but here is the issue. I do not know what should be the starting address for my micro sd hc card . Also, how should I increment for next angle address if my starting address is 0000 given that previous angle is floating in type(4 bytes).

SPI Initialize Code:

 void mySPI_Init(void){


RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);

SPI_InitTypeDef SPI_InitTypeDefStruct;


SPI_InitTypeDefStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitTypeDefStruct.SPI_Mode = SPI_Mode_Master;
SPI_InitTypeDefStruct.SPI_DataSize = SPI_DataSize_8b;
SPI_InitTypeDefStruct.SPI_CPOL = SPI_CPOL_High;
SPI_InitTypeDefStruct.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitTypeDefStruct.SPI_NSS = SPI_NSS_Soft;
SPI_InitTypeDefStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
SPI_InitTypeDefStruct.SPI_FirstBit = SPI_FirstBit_MSB;

SPI_Init(SPI1, &SPI_InitTypeDefStruct);


RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOE , ENABLE);

GPIO_InitTypeDef GPIO_InitTypeDefStruct;

GPIO_InitTypeDefStruct.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_7 | GPIO_Pin_6;
GPIO_InitTypeDefStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitTypeDefStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitTypeDefStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitTypeDefStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitTypeDefStruct);

GPIO_InitTypeDefStruct.GPIO_Pin = GPIO_Pin_3;
GPIO_InitTypeDefStruct.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitTypeDefStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitTypeDefStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitTypeDefStruct.GPIO_OType = GPIO_OType_PP;
GPIO_Init(GPIOE, &GPIO_InitTypeDefStruct);

GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1);

GPIO_SetBits(GPIOE, GPIO_Pin_3);


SPI_Cmd(SPI1, ENABLE);

}

Send Data Function:

void mySPI_SendData(uint8_t adress, uint8_t data){
//uint8_t adress is the address where data has to be sent to
//uint8_t data is the data that has to be sent i.e floating angle

GPIO_ResetBits(GPIOE, GPIO_Pin_3);

while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)); 
SPI_I2S_SendData(SPI1, adress);
while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE));
SPI_I2S_ReceiveData(SPI1);

while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)); 
SPI_I2S_SendData(SPI1, data);
while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE));
SPI_I2S_ReceiveData(SPI1);

GPIO_SetBits(GPIOE, GPIO_Pin_3);
}

Get data function:

uint8_t mySPI_GetData(uint8_t adress){

GPIO_ResetBits(GPIOE, GPIO_Pin_3); 

adress = 0x80 | adress;

while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)); 
SPI_I2S_SendData(SPI1, adress);
while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE));
SPI_I2S_ReceiveData(SPI1); //Clear RXNE bit

while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)); 
SPI_I2S_SendData(SPI1, 0x00); //Dummy byte to generate clock
while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE));

GPIO_SetBits(GPIOE, GPIO_Pin_3);

return  SPI_I2S_ReceiveData(SPI1);
}
\$\endgroup\$
2
\$\begingroup\$

I do not know what should be the starting address for my micro sd hc card .

SD/SDHC cards are much more complex to access. Your best bet is 3rd party software - see the FATFS example code in STM32Cube for example.

If you really wanted to know: The spec can be found on sdcard.org.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.