MCU as SPI Slave, read data - c

I have an master which i cant control it just sends clock with data. I congifured my stm32f4 as full-duplex slave. And i managed to synchronize them with HAL function:
HAL_SPI_TransmitReceive(&hspi1, (uint8_t*)spi_buffer, (uint8_t *)spi_buffer, 16, 2000);
// receive 16 bytes then send them for debugg
Im sending from slave the bytes which i get from master, in purpose of debugging,
and the data is same, so its perfectly synchronized and seems like slave reading all data master sends. Here is the images(sorry cant dowload it with stackoverflow cuz i low on points):
https://imgur.com/wa6rF5J - ocsillo's image
https://imgur.com/a/ZVCPaNT - zoomed up
The problem is that i dont know how to read that data, its 16bytes which i need somehow to read without slowing the spi down. All my attempts to do that are failed.
Thw whole code are:
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration----------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_USB_DEVICE_Init();
MX_SPI1_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
uint8_t collect_bits[16] = {0};
char string_array[21] = {0};
uint8_t spi_buffer[16] ={0} ;
uint8_t flag_when_start_print = 0;
spi_buffer[0] = 5;
uint8_t spi_transmit[16] = {0};
HAL_SPI_Init(&hspi1); //init spi
while (1)
{
HAL_SPI_TransmitReceive(&hspi1, (uint8_t*)spi_buffer, (uint8_t *)spi_buffer, 16, 2000);
if (spi_buffer[0] == 0xFE) // just tring to catch 0xFE, doesnt work
{
sprintf(string_array, "%X", spi_buffer[0]); // just simple convertation for PC output
CDC_Transmit_FS((uint8_t*)string_array, sizeof(string_array)); // print result. }
}
}
The important fact that the beggining of the messge i want to catch is always starts with 0xFE0010, so after those bytes i need to read next 13bytes

Related

Why STM32 I2C slave is returning unwanted NACK or indefinitely clock stretching?

I am trying to write an STM32 as an I2C slave device with a simple interface that works like this:
So master will always send a registered address every time, then either write or read from that register address.
The slave then needs to always receive 1 byte for a registered address, then it either sends what information is in that register back to the master if the next operation is a read or overwrites the register if the master's next operation is another write.
When I run my code however, I get some NACKS where ther should be ACKS
Here is the response when the master requests a buffer:
You can see the NACK at the end right after the slave finishes sending the last byte
This is a bit of a pain but the master receives the data ok so i can live with this
However when i try to write to a register on the slave this is what comes out:
Slave receives register address, then receives 1 byte and ack, then after receiving the second byte for some reason it just holds the line up (I need to use clock stretching here)
This is not ok, not only the slave didnt receive all the data but it also locks the line for any further communications. Why Am i having this? im scratching my head for months on this at this point
Here is the master code just for reference (running on a simple Arduino) since the focus is really on the STM32 slave code:
#include <Wire.h>
uint16_t read_register(int devAddr, unsigned char regAddr, unsigned char bytes, unsigned char * buffer){
unsigned char i = 0;
Wire.beginTransmission(devAddr);
Wire.write(regAddr);
Wire.endTransmission(false);
Wire.requestFrom(devAddr, bytes , true);
while(Wire.available()){
buffer[i] = Wire.read();
i++;
}
return true;
}
uint16_t write_register(int devAddr, unsigned char regAddr, unsigned char bytes, unsigned char * buffer){
unsigned char i = 0;
Wire.beginTransmission(devAddr);
Wire.write(regAddr); // Reg to write
for(i = 0; i < bytes; i++){
Wire.write(buffer[i]);
}
Wire.endTransmission(true);
return true;
}
void setup()
{
Wire.begin();
Wire.setClock(400);
Serial.begin(9600);
while (!Serial); // Leonardo: wait for serial monitor
Serial.println("Starting");
}
void loop()
{
unsigned char buffSize = 4;
unsigned char readBuff[buffSize];
unsigned char writeBuff[5] = {0xFB, 0xE3, 0XE2, 0xE1, 0xE0};
for (int i = 0; i < buffSize; i++) readBuff[i] = 0;
read_register(0x1F, 251, buffSize, readBuff);
Serial.print(readBuff[3], HEX);
Serial.print(readBuff[2], HEX);
Serial.print(readBuff[1], HEX);
Serial.println(readBuff[0], HEX);
write_register(0x1F, 0xFB, 5, writeBuff);
delay(2000);
}
Here is the I2C code section of the STM32 slave:
/* USER CODE BEGIN Header */
/**
******************************************************************************
* #file i2c.c
* #brief This file provides code for the configuration
* of the I2C instances.
******************************************************************************
* #attention
*
* Copyright (c) 2022 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "i2c.h"
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
I2C_HandleTypeDef hi2c1;
/* I2C1 init function */
void MX_I2C1_Init(void)
{
/* USER CODE BEGIN I2C1_Init 0 */
// Get I2C address code from hardware jumpers
// Address starts at I2C_ADDRESS_BASE and is offset by value read on jumpers array
uint8_t I2C_Address = 0x0;
I2C_Address = (I2C_ADDRESS_BASE + (
(HAL_GPIO_ReadPin(AD0_GPIO_Port, AD0_Pin) << 0)|
(HAL_GPIO_ReadPin(AD1_GPIO_Port, AD1_Pin) << 1)|
(HAL_GPIO_ReadPin(AD2_GPIO_Port, AD2_Pin) << 2)|
(HAL_GPIO_ReadPin(AD3_GPIO_Port, AD3_Pin) << 3)
)) << 1;
/* USER CODE END I2C1_Init 0 */
/* USER CODE BEGIN I2C1_Init 1 */
/* USER CODE END I2C1_Init 1 */
hi2c1.Instance = I2C1;
hi2c1.Init.Timing = 0x0000020B;
hi2c1.Init.OwnAddress1 = I2C_Address;
hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_ENABLE;
hi2c1.Init.OwnAddress2 = (I2C_ADDRESS_BASE + 16) << 1;
hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if (HAL_I2C_Init(&hi2c1) != HAL_OK)
{
Error_Handler();
}
/** Configure Analogue filter
*/
if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
{
Error_Handler();
}
/** Configure Digital filter
*/
if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN I2C1_Init 2 */
/* USER CODE END I2C1_Init 2 */
}
void HAL_I2C_MspInit(I2C_HandleTypeDef* i2cHandle)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(i2cHandle->Instance==I2C1)
{
/* USER CODE BEGIN I2C1_MspInit 0 */
/* USER CODE END I2C1_MspInit 0 */
__HAL_RCC_GPIOB_CLK_ENABLE();
/**I2C1 GPIO Configuration
PB6 ------> I2C1_SCL
PB7 ------> I2C1_SDA
*/
GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF4_I2C1;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* I2C1 clock enable */
__HAL_RCC_I2C1_CLK_ENABLE();
/* I2C1 interrupt Init */
HAL_NVIC_SetPriority(I2C1_EV_IRQn, 5, 0);
HAL_NVIC_EnableIRQ(I2C1_EV_IRQn);
HAL_NVIC_SetPriority(I2C1_ER_IRQn, 5, 0);
HAL_NVIC_EnableIRQ(I2C1_ER_IRQn);
/* USER CODE BEGIN I2C1_MspInit 1 */
/* USER CODE END I2C1_MspInit 1 */
}
}
void HAL_I2C_MspDeInit(I2C_HandleTypeDef* i2cHandle)
{
if(i2cHandle->Instance==I2C1)
{
/* USER CODE BEGIN I2C1_MspDeInit 0 */
/* USER CODE END I2C1_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_I2C1_CLK_DISABLE();
/**I2C1 GPIO Configuration
PB6 ------> I2C1_SCL
PB7 ------> I2C1_SDA
*/
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_6);
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_7);
/* I2C1 interrupt Deinit */
HAL_NVIC_DisableIRQ(I2C1_EV_IRQn);
HAL_NVIC_DisableIRQ(I2C1_ER_IRQn);
/* USER CODE BEGIN I2C1_MspDeInit 1 */
/* USER CODE END I2C1_MspDeInit 1 */
}
}
/* USER CODE BEGIN 1 */
#define I2C_BUFFER_SIZE 8
uint8_t i2c_buffer[I2C_BUFFER_SIZE];
uint8_t reg_addr_rcvd = 0;
#define I2C_REG_ADD_SIZE 1
#define I2C_PAYLOAD_SIZE 4
extern void HAL_I2C_AddrCallback(I2C_HandleTypeDef *hi2c, uint8_t TransferDirection, uint16_t AddrMatchCode){
UNUSED(AddrMatchCode);
// If is master write, listen to necessary amount of bytes
if(TransferDirection == I2C_DIRECTION_TRANSMIT){
// First write request is always 1 byte of the requested reg address
// Will saved it on the first position of I2C_buffer
if(!reg_addr_rcvd){
HAL_I2C_Slave_Sequential_Receive_IT(hi2c, (void*)i2c_buffer, I2C_REG_ADD_SIZE, I2C_FIRST_FRAME);
} else {
// If a subsequent write request is sent, will receve 4 bytes from master
// Save it on the rest of the buffer
HAL_I2C_Slave_Sequential_Receive_IT(hi2c, (void*)i2c_buffer, I2C_PAYLOAD_SIZE, I2C_NEXT_FRAME);
}
}
else {
// If a read request is sent by the master, return the value of the data in the requested register that was saved on 1st
// position of the I2C buffer
HAL_I2C_Slave_Sequential_Transmit_IT(hi2c, data_register[i2c_buffer[0]].mem_addr, data_register[i2c_buffer[0]].len, I2C_LAST_FRAME);
}
// Read address + data size. If it is a read command, data size will be zero
}
extern void HAL_I2C_SlaveRxCpltCallback(I2C_HandleTypeDef *hi2c){
// This is called after a master 'write' request. first time around it will be a register.
// Second time if its a write to register request, it will be a payload
if(!reg_addr_rcvd){
// If reg_addr_rcvd is false, means that it received a register
reg_addr_rcvd = 1;
} else {
// If reg_addr_rcvd is set, means that this callback was returned after the payload data has been received
reg_addr_rcvd = 0;
}
HAL_I2C_EnableListen_IT(hi2c);
HAL_GPIO_TogglePin(LED_G_GPIO_Port, LED_G_Pin);
}
extern void HAL_I2C_ListenCpltCallback (I2C_HandleTypeDef *hi2c){
HAL_I2C_EnableListen_IT(hi2c);
HAL_GPIO_TogglePin(LED_B_GPIO_Port, LED_B_Pin);
}
extern void HAL_I2C_SlaveTxCpltCallback(I2C_HandleTypeDef *hi2c){
// Reset reg_addr_rcvd after finish sending requested register
reg_addr_rcvd = 0;
HAL_I2C_EnableListen_IT(hi2c);
}
extern void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c)
{
HAL_GPIO_TogglePin(LED_R_GPIO_Port, LED_R_Pin);
//HAL_I2C_ERROR_NONE 0x00000000U /*!< No error */
//HAL_I2C_ERROR_BERR 0x00000001U /*!< BERR error */
//HAL_I2C_ERROR_ARLO 0x00000002U /*!< ARLO error */
//HAL_I2C_ERROR_AF 0x00000004U /*!< Ack Failure error */
//HAL_I2C_ERROR_OVR 0x00000008U /*!< OVR error */
//HAL_I2C_ERROR_DMA 0x00000010U /*!< DMA transfer error */
//HAL_I2C_ERROR_TIMEOUT 0x00000020U /*!< Timeout Error */
uint32_t error_code = HAL_I2C_GetError(hi2c);
if (error_code != HAL_I2C_ERROR_AF){}
HAL_I2C_EnableListen_IT(hi2c);
}
/* USER CODE END 1 */
And here is cubeMX config for the I2C slave
Appreciate any insigth you guys could have.
Thank you!
You have asked two questions. This is an answer to the first question, which is why do you get a nack after reading 4 data bytes?
This is the absolutely correct and expected behaviour, and anyway it is being done by the Arduino not the STM32.
To explain: the ack after each byte is always the responsibility of the side that didn't send the byte. When the master writes an address or data to the slave, the slave generates an ack if it received the byte and is ready for the master to start sending the next byte.
When the master reads data from the slave, the slave (STM32) sends the data byte, and it is the responsibility of the master (Arduino) to choose to send an ack or nack. If the master sends an ack it means "I have received this byte, prepare to send me another byte". If the master sends nack it means "I have finished getting data from to you".
It is perfectly legal in I2C to start reading without knowing how many bytes you want, in this case you will ack every byte and then send a stop condition when you have read enough. In your case however, you told the Arduino up front that it should read 4 bytes, so it proceeds to ack the first three and nacks the fourth.
In some cases this behaviour can save resources at the slave end because the slave knows straight away that it doesn't have to get a fifth byte ready.
You have asked two questions. This is an answer to the second question, which is why doesn't the STM32 slave ack more bytes written to it but instead stretch the clock?
In your ADDR interrupt function, if you have not received a register address, (reg_addr_rcvd is false) you start a receive of one byte. The master (Arduino) sends this one byte, and presumably the receive complete callback occurs.
If at this point the Arduino were to send a restart or a stop-start and the slave address again, then the ADDR interrupt would occur again, and upon finding reg_addr_rcvd true it would start a receive of 4 bytes, which would all be acked.
However, the Arduino doesn't send a restart, it just carries on blasting out the data straight after the register address. This is a perfectly normal and reasonable for a master to do. You need to handle both cases correctly. Probably this means starting a receive of data in the receive-complete interrupt once the register address is received. If you don't start a receive then the I2C peripheral will just stretch the clock because it has nowhere to put the data that has been buffered.
To write robust production quality software you also need to handle various other combinations. Eg: if the master sends more than 4 data bytes you will get never-ending stretching again. If the master sends a stop after less than 4 bytes then you need to be able to abort the receive and go back to listening etc.

Sending data using USB CDC (STM32)

I'm using NucleoF413ZH and STM32CubeIDE. I have 16 microphones, each connects to one ADC channel and thanks to the use of DMA it directs data to the memory. However, I would like to do a cross-correlation of the signals, so I came to the conclusion that I would send the data to the PC or RPi 4b, and I would do the calculations there. But the problem for me is sending so much data from the memory. I try to use CDC_TRANSMIT_FS but I cant send all 16 channel measurements, also transmision is very slow.
Is there any way to send whole array of 16 elements, or even better 16xM elements, so maybe it will be faster? I need 16mic x10bit x44.1khz ~= 7Mbit/s =882KBytes/s, also i can reduce some mics to 12 of them. Here is my code (full in link https://pastebin.pl/view/6059edc8 ):
Also, sorry if something is not good grammatically, English is my second language and I'm still learning
/* USER CODE BEGIN PV */
uint16_t Pomiar[16]; // measurments 16x1
char msg[10];
uint8_t DataToSend[40]; //
uint8_t MessageCounter = 0; //
uint8_t MessageLength = 0; // Msg lngth
/* USER CODE END PV */
/* USER CODE BEGIN 2 */
HAL_ADC_Start_DMA(&hadc1, (uint16_t*)Pomiar, 16);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
// CDC_Transmit_FS((uint8_t*)Pomiar, (512));
//++MessageCounter;
//MessageLength = sprintf(DataToSend, "Wiadomosc nr %d\n\r", MessageCounter);
// MessageLength = sprintf(DataToSend, "%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d:%d\n\r", Pomiar[0],Pomiar[1],Pomiar[2],Pomiar[3],Pomiar[4],Pomiar[5],Pomiar[6],Pomiar[7],Pomiar[8],Pomiar[9],Pomiar[10],Pomiar[11],Pomiar[12],Pomiar[13],Pomiar[14],Pomiar[15]);
MessageLength = sprintf(DataToSend, "%d:%d:%d:%d\n\r", Pomiar[0],Pomiar[1],Pomiar[2],Pomiar[3]);
CDC_Transmit_FS(DataToSend, MessageLength);
//HAL_Delay(1);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
usart settings
static void MX_USART3_UART_Init(void)
{
/* USER CODE BEGIN USART3_Init 0 */
/* USER CODE END USART3_Init 0 */
/* USER CODE BEGIN USART3_Init 1 */
// huart3.Init.BaudRate = 115200;
/* USER CODE END USART3_Init 1 */
huart3.Instance = USART3;
// huart3.Init.BaudRate = 115200;
huart3.Init.BaudRate = 921600;
huart3.Init.WordLength = UART_WORDLENGTH_8B;
huart3.Init.StopBits = UART_STOPBITS_1;
huart3.Init.Parity = UART_PARITY_NONE;
huart3.Init.Mode = UART_MODE_TX_RX;
huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart3.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart3) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART3_Init 2 */
/* USER CODE END USART3_Init 2 */
}
sprintf is very slow and you are sending ASCII 5 digits per ADC sample. You can send raw hex data 2 bytes per sample to save bandwidth like this
CDC_Transmit_FS(Pomiar, sizeof(Pomiar));

sprintf cause my program stop working in keil

This is my main function and I don't know why when I simulate it in proteos I figured that the code runs up to sprintf and next lines doesn't run and when I comment it the "salam" printed on lcd
I use mingw compiler and I have installed all librarys
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
/* USER CODE BEGIN 2 */
char q[5] = "salam" ;
int a = 6541;
//float b = a/7;
LCD1602_Begin8BIT(RS_GPIO_Port,RS_Pin,E_Pin,D0_GPIO_Port,D0_Pin,D1_Pin,D2_Pin,D3_Pin,D4_GPIO_Port,D4_
Pin,D5_Pin,D6_Pin,D7_Pin);
LCD1602_setCursor(1,1);
//snprintf(q, 20, "%10d", 1234567);
//LCDCHAR_Puts(0, 0, q);
//char command1[50], command2[50]; // Added
//char *temp[] = {NULL, command1, command2, NULL}; // Modified
//temp[0]="sum";
LCD1602_print("mahdi kahrizi");
LCD1602_2ndLine();
sprintf(q,"%d",a);
LCD1602_print(q);
the problem starts from here.
char q[5] = "salam" ;
q is too short to store this word. It has to be at least 6 chars long.
It is Undefined Behaviour as you write outside the array bounds.
When you change the length of the q to be longer
snprintf(q, sizeof(q), "%d", a);

stm32f051r8t6 TIM14 interrupt handler not working after reset

I am using STM32CubeMX to generate code into IAR, and I am using a stm32f051r8t6 microcontroller,
The problem I am having is that when first loading the code onto the chip, it all works perfect, however after pressing restart on either the board or IAR debugger, the TIM14 interrupt handler is not entered, but as soon as I leave the debugger and enter again, it starts working until I press restart. Has anybody come across this problem before? My code is below
static void MX_TIM14_Init(void);
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if (htim->Instance == TIM14)
{
HAL_GPIO_WritePin(GPIOA, USART1_TE_Pin, GPIO_PIN_SET);
}
}
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration---------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the
Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_ADC_Init();
MX_USART1_UART_Init();
MX_USART2_UART_Init();
MX_TIM14_Init();
HAL_TIM_Base_Start(&htim14);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
FslBufferControl();
MimModeCheck();
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
static void MX_TIM14_Init(void)
{
/* USER CODE BEGIN TIM14_Init 0 */
/* USER CODE END TIM14_Init 0 */
/* USER CODE BEGIN TIM14_Init 1 */
/* USER CODE END TIM14_Init 1 */
htim14.Instance = TIM14;
htim14.Init.Prescaler = 47999;
htim14.Init.CounterMode = TIM_COUNTERMODE_UP;
htim14.Init.Period = 1;
htim14.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim14.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
if (HAL_TIM_Base_Init(&htim14) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM14_Init 2 */
/* USER CODE END TIM14_Init 2 */
}
You need to add to enable the interrupt.
MX_TIM14_Init();
HAL_NVIC_EnableIRQ(TIM14_IRQn); // <----------------------------
HAL_TIM_Base_Start(&htim14);
Just check the IRQn TIM14 UG event number. They are defined in the IRQn_Type enum type defined in the STM32F___.h file where ___ is the model of your micro (you will find it in the include folder)

Writing data to micro SD card from microcontroller STM32F401RET6

I am using the board Nucleo F401RE based on micro-controller STM32F401RET6. I connected to the board a Micro SD slot, and interested in writing data to the SD Card and read data from it. I used the software STM32CubeX to generate code and in particular the SD library with built-in functions. I tried to write a simple code which writes an array to a specific array and tries to read the same data afterwords. The code is as follows:
uint32_t to_send[512] = {1, 2, 3, 4, 5};
uint32_t to_receive[512];
int main(void)
{
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART2_UART_Init();
MX_SDIO_SD_Init();
char buffer[14] = "Hello, world\n";
uint64_t address = 0x00;
HAL_SD_ErrorTypedef write_result = HAL_SD_WriteBlocks(&hsd, to_send, address, 512, 1);
HAL_SD_ErrorTypedef read_result = HAL_SD_ReadBlocks(&hsd, to_receive, 0x00, 512, 1);
HAL_UART_Transmit(&huart2, (uint8_t *) &write_result, 1, 1000);
HAL_UART_Transmit(&huart2, (uint8_t *) &read_result, 1, 1000);
while (1)
{
//HAL_UART_Transmit(&huart2, (uint8_t *)buffer, 14, 1000);
HAL_UART_Transmit(&huart2, (uint8_t *)to_receive, 512, 1000);
}
Though, I don't succeed in writing the data, the function HAL_SD_WriteBlocks() returns the value SD_CMD_CRC_FAIL, which means that : "Command response received (but CRC check failed)". What am I missing ? I checked the hardware configuration many times and the micro SD card is correctly connected to the microcontroller. I can add the implementation of the HAL built-in functions if needed. Thank you.
Make sure that your sdio clock is within valid limits (see function SystemClock_Config):
/* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLL_M) * PLL_N */
#define PLL_M 8
#define PLL_N 336
/* SYSCLK = PLL_VCO / PLL_P */
#define PLL_P 2
/* USB OTG FS, SDIO and RNG Clock = PLL_VCO / PLLQ */
#define PLL_Q 7
When using the defines above when initialize the clock (RCC_OscInitTypeDef) the SDIO clock will be 336 / (7*2) = 25Mhz
(Given that PLL_M is the same as the HSE / HSI)
If the frequency is too high (>50Mhz) then perhaps you will get errors in the communication which will explain your symptoms.
I didnt use usart for my project, i write the value to sd card and read the value ,
you must arrange the code for your expectations, my code is
#include "main.h"
#include "stm32f4xx_hal.h"
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private variables ---------------------------------------------------------*/
SD_HandleTypeDef hsd;
/* USER CODE BEGIN PV */
/* Private variables --------------------------------------------------------*/
uint8_t to_send[512] = "sener suat sd card";
uint8_t to_receive[512];
uint8_t sener[3]={7,5,4};
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_SDIO_SD_Init(void);
static void MX_USART2_UART_Init(void);
/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/
/* USER CODE END PFP */
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration----------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_SDIO_SD_Init();
/* USER CODE BEGIN 2 */
uint32_t address = 0x55;
HAL_SD_MspInit(&hsd);
HAL_SD_Init(&hsd);
HAL_SD_WriteBlocks(&hsd,to_send,address,1,500);
HAL_Delay(100);
HAL_SD_ReadBlocks(&hsd,to_receive,address,1,500);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
}

Resources