cancel
Showing results for 
Search instead for 
Did you mean: 

NUCLEO-H753ZI SPI Communication

Aamir1033
Associate II

 

hi, 

I am integrating ADS1256 ADC, which communicates on SPI with NUCLEO-H753ZI board. 

Firstly, I can communicate when I am writing my code in Arduino, which is simple. It is working fine and showing me the converted output. 

But for some reasons I have to use cubide and perform some work on ethernet after establishing communication on SPI. But through cubeIDE I am not able to establish cube ide. below is Clock config

But with same flow as in Arduino, I cannot stablish communication. Cannot read registers of the device, nor i can get the output

Aamir1033_0-1745658400470.png

and here is my SPI initiazation.

hspi3.Instance = SPI3;

hspi3.Init.Mode = SPI_MODE_MASTER;

hspi3.Init.Direction = SPI_DIRECTION_2LINES;

hspi3.Init.DataSize = SPI_DATASIZE_8BIT;

hspi3.Init.CLKPolarity = SPI_POLARITY_LOW;

hspi3.Init.CLKPhase = SPI_PHASE_1EDGE;

hspi3.Init.NSS = SPI_NSS_SOFT;

hspi3.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64;

hspi3.Init.FirstBit = SPI_FIRSTBIT_MSB;

hspi3.Init.TIMode = SPI_TIMODE_DISABLE;

hspi3.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;

hspi3.Init.CRCPolynomial = 0x0;

hspi3.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;

hspi3.Init.NSSPolarity = SPI_NSS_POLARITY_LOW;

hspi3.Init.FifoThreshold = SPI_FIFO_THRESHOLD_01DATA;

hspi3.Init.TxCRCInitializationPattern = SPI_CRC_INITIALIZATION_ALL_ZERO_PATTERN;

hspi3.Init.RxCRCInitializationPattern = SPI_CRC_INITIALIZATION_ALL_ZERO_PATTERN;

hspi3.Init.MasterSSIdleness = SPI_MASTER_SS_IDLENESS_00CYCLE;

hspi3.Init.MasterInterDataIdleness = SPI_MASTER_INTERDATA_IDLENESS_00CYCLE;

hspi3.Init.MasterReceiverAutoSusp = SPI_MASTER_RX_AUTOSUSP_DISABLE;

hspi3.Init.MasterKeepIOState = SPI_MASTER_KEEP_IO_STATE_DISABLE;

hspi3.Init.IOSwap = SPI_IO_SWAP_DISABLE;

if (HAL_SPI_Init(&hspi3) != HAL_OK)

{

Error_Handler();

}

 

void delay_us(uint32_t us)

{

// Reset the counter to 0

__HAL_TIM_SET_COUNTER(&htim2, 0);

 

// Wait until the counter reaches the desired delay

while (__HAL_TIM_GET_COUNTER(&htim2) < us);

}

 

uint8_t ADS1256_DRDY_Wait()

{

unsigned int DRDY_State = 1;

while(DRDY_State)

{

DRDY_State = HAL_GPIO_ReadPin(DRDY_PIN_GPIO_Port, DRDY_PIN_Pin);

}

return 0;

}

uint8_t ADS1256_Reset()

{

HAL_GPIO_WritePin(RST_PIN_GPIO_Port, RST_PIN_Pin, 0);

HAL_Delay(100);

HAL_GPIO_WritePin(RST_PIN_GPIO_Port, RST_PIN_Pin, 1);

 

uint8_t resetCmd = 0xfe;

HAL_GPIO_WritePin(SPI_CS_GPIO_Port, SPI_CS_Pin, GPIO_PIN_RESET); // CS Low

HAL_SPI_Transmit(&hspi3, &resetCmd, 1, HAL_MAX_DELAY);

HAL_GPIO_WritePin(SPI_CS_GPIO_Port, SPI_CS_Pin, GPIO_PIN_SET); // CS High

return 0;

}

 

void ADS1256_SelectChannel()

{

uint8_t wregCmd[3];

wregCmd[0] = 0x50 | 0x01; // WREG command for the MUX register (0x01)

wregCmd[1] = 0x00; // Number of registers to write minus 1 (just the MUX register)

wregCmd[2] = 0x2f; // Channel configuration

 

ADS1256_DRDY_Wait();

HAL_GPIO_WritePin(SPI_CS_GPIO_Port, SPI_CS_Pin, GPIO_PIN_RESET); // CS Low

HAL_SPI_Transmit(&hspi3, wregCmd, 2, HAL_MAX_DELAY); // Send WREG command and data

delay_us(5);

HAL_SPI_Transmit(&hspi3, &wregCmd[2], 1, HAL_MAX_DELAY);

HAL_GPIO_WritePin(SPI_CS_GPIO_Port, SPI_CS_Pin, GPIO_PIN_SET); // CS High

}

 

uint8_t ADS1256_ReadMuxRegister()

{

uint8_t rregCmd[2];

uint8_t regValue = 0x01;

 

rregCmd[0] = 0x10 | 0x01; // RREG command for the MUX register (0x01)

rregCmd[1] = 0x00; // Number of registers to read minus 1 (just the MUX register)

 

ADS1256_DRDY_Wait();

HAL_GPIO_WritePin(SPI_CS_GPIO_Port, SPI_CS_Pin, GPIO_PIN_RESET); // CS Low

HAL_SPI_Transmit(&hspi3, rregCmd, 2, HAL_MAX_DELAY); // Send RREG command

delay_us(5);//5 microsec delay required

HAL_SPI_Receive(&hspi3, &regValue, 1, HAL_MAX_DELAY); // Receive the register value

HAL_GPIO_WritePin(SPI_CS_GPIO_Port, SPI_CS_Pin, GPIO_PIN_SET); // CS High

printf("\nRegister Value = %x", regValue);

return regValue;

}

 

void ADS1256_ReadData(uint32_t *adcValue)

{

uint8_t rdataCmd = 0x01; // RDATA command

uint8_t rxBuffer[3]; // Buffer for 24-bit ADC data

 

ADS1256_DRDY_Wait();

HAL_GPIO_WritePin(SPI_CS_GPIO_Port, SPI_CS_Pin, GPIO_PIN_RESET); // CS Low

HAL_SPI_Transmit(&hspi3, &rdataCmd, 1, HAL_MAX_DELAY); // Send RDATA command

delay_us(10);

HAL_SPI_Receive(&hspi3, rxBuffer, 3, HAL_MAX_DELAY); // Receive 24-bit data

HAL_GPIO_WritePin(SPI_CS_GPIO_Port, SPI_CS_Pin, GPIO_PIN_SET); // CS High

 

// Combine the 3 bytes into a 24-bit integer

*adcValue = ((uint32_t)rxBuffer[0] << 16) |

((uint32_t)rxBuffer[1] << 8) |

(uint32_t)rxBuffer[2];

}

 

double ConvertToVoltage(int32_t registerData)

{

double Voltage = 0;

double VREF = 2.5;

long minus = registerData >> 23;

if (minus == 1) //if the 24th bit (sign) is 1, the number is negative

{

registerData = registerData - 16777216; //conversion for the negative sign

//"mirroring" around zero

}

 

Voltage = ((2*VREF) / 8388608)*registerData; //2.5 = Vref; 8388608 = 2^{23} - 1

return (Voltage);

}

 

/* USER CODE END 0 */

 

/**

* @brief The application entry point.

* @retval int

*/

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();

 

/* Configure the peripherals common clocks */

PeriphCommonClock_Config();

 

/* USER CODE BEGIN SysInit */

 

/* USER CODE END SysInit */

 

/* Initialize all configured peripherals */

MX_GPIO_Init();

MX_SPI3_Init();

MX_TIM2_Init();

 

/* Initialize interrupts */

MX_NVIC_Init();

/* USER CODE BEGIN 2 */

 

/* USER CODE END 2 */

 

/* Initialize leds */

BSP_LED_Init(LED_GREEN);

BSP_LED_Init(LED_BLUE);

BSP_LED_Init(LED_RED);

 

/* Initialize USER push-button, will be used to trigger an interrupt each time it's pressed.*/

BSP_PB_Init(BUTTON_USER, BUTTON_MODE_EXTI);

 

/* Initialize COM1 port (115200, 8 bits (7-bit data + 1 stop bit), no parity */

BspCOMInit.BaudRate = 115200;

BspCOMInit.WordLength = COM_WORDLENGTH_8B;

BspCOMInit.StopBits = COM_STOPBITS_1;

BspCOMInit.Parity = COM_PARITY_NONE;

BspCOMInit.HwFlowCtl = COM_HWCONTROL_NONE;

if (BSP_COM_Init(COM1, &BspCOMInit) != BSP_ERROR_NONE)

{

Error_Handler();

}

 

/* USER CODE BEGIN BSP */

 

/* -- Sample board code to send message over COM1 port ---- */

printf("Welcome to STM32 world !\n\r");

 

/* -- Sample board code to switch on leds ---- */

BSP_LED_On(LED_GREEN);

BSP_LED_On(LED_BLUE);

BSP_LED_On(LED_RED);

 

/* USER CODE END BSP */

 

/* Infinite loop */

/* USER CODE BEGIN WHILE */

HAL_TIM_Base_Start(&htim2);

ADS1256_Reset();

ADS1256_ReadMuxRegister();

ADS1256_SelectChannel();

ADS1256_ReadMuxRegister();

 

uint32_t adcValue = 0;

double Voltage = 0;

 

while (1)

{

ADS1256_SelectChannel();

ADS1256_ReadMuxRegister();

ADS1256_ReadData(&adcValue);

printf("\nADC Value: %d", adcValue);

ConvertToVoltage(adcValue);

Voltage = ConvertToVoltage(adcValue);

printf("\nConverted Voltage: %f\n\n", Voltage);

 

BSP_LED_Toggle(LED_GREEN);

BSP_LED_Toggle(LED_BLUE);

BSP_LED_Toggle(LED_RED);

HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);

delay_us(500000);

 

/* USER CODE END WHILE */

 

/* USER CODE BEGIN 3 */

}

/* USER CODE END 3 */

}

 

But with same flow as in Arduino, I canot stablish communication. Cannot read the registers of the device, nor can I get the output. I am new, so can't guess what is going on. With SPI communication I am using "timer" which is working well and i am blinking LEDs also. Moreover, I am sending data to virtual "COM" which also works. Help is requested in this regard. i can also upload Arduinoi code as well if required.

 
10 REPLIES 10

Yes. I am. I am also getting desired conversion through arduino. But the problem as above stated that I cant program the controller through STM CUBEIDE.