Post Go back to editing

SPI communication between STM32F103RBT6 and ADE9078

Category: Software
Software Version: STM32F103RBT6 and ADE9078

I am trying to read the Version of the IC ADE9078 (0x4FE) simply to verify if the SPI communication is correct, but it only returns 0xFFFF as a result.

I share my code and configuration for the SPI.

I would like to know if the configuration is correct and if the way to read the ADE9078 registers is correct.

I use STM32F103RBT6 (from STM32 CUBE IDE) and ADE9078.

...

static void MX_SPI2_Init(void)

{

/* USER CODE BEGIN SPI2_Init 0 */

/* USER CODE END SPI2_Init 0 */

/* USER CODE BEGIN SPI2_Init 1 */

/* USER CODE END SPI2_Init 1 */

/* SPI2 parameter configuration*/

hspi2.Instance = SPI2;

hspi2.Init.Mode = SPI_MODE_MASTER;

hspi2.Init.Direction = SPI_DIRECTION_2LINES;

hspi2.Init.DataSize = SPI_DATASIZE_16BIT;

hspi2.Init.CLKPolarity = SPI_POLARITY_LOW;

hspi2.Init.CLKPhase = SPI_PHASE_1EDGE;

hspi2.Init.NSS = SPI_NSS_SOFT;

hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64;

hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;

hspi2.Init.TIMode = SPI_TIMODE_DISABLE;

hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;

hspi2.Init.CRCPolynomial = 10;

if (HAL_SPI_Init(&hspi2) != HAL_OK)

{

Error_Handler();

}

/* USER CODE BEGIN SPI2_Init 2 */

/* USER CODE END SPI2_Init 2 */

}

void Receive (uint16_t myValue2){

char buffer2[10]; // Suficiente espacio para representar un uint16_t en formato hexadecimal

snprintf(buffer2, sizeof(buffer2), "0x%04X\r\n", myValue2);

HAL_UART_Transmit(&huart2, (uint8_t*)buffer2, strlen(buffer2), HAL_MAX_DELAY);

}

uint16_t registro = 0x4FE;

uint16_t cmd_hdr = (registro << 4) | 0x8;

uint8_t tx_buf[2] = {0};

tx_buf[0] = (uint8_t)(cmd_hdr >> 8);

tx_buf[1] = (uint8_t)(cmd_hdr);

uint8_t buffer[4] = {0};

uint16_t rec = 0;

/* USER CODE END 2 */

/* Infinite loop */

/* USER CODE BEGIN WHILE */

while (1)

{

HAL_GPIO_WritePin(GPIOC,PM1_Pin, GPIO_PIN_RESET); //pm1 bajo pc5

HAL_GPIO_WritePin(GPIOC,GPIO_PIN_6, GPIO_PIN_RESET); //pm1 bajo pc6

HAL_GPIO_WritePin(GPIOC,GPIO_PIN_7, GPIO_PIN_RESET); //Reset alto pc7

HAL_GPIO_WritePin(GPIOB,RESET_Pin, GPIO_PIN_SET); // reset alto

HAL_GPIO_WritePin(GPIOB, CS2_Pin, GPIO_PIN_RESET); //cs2 bajo

//HAL_GPIO_WritePin(GPIOB, CS1_Pin, GPIO_PIN_SET); //cs1 alto

HAL_SPI_Transmit(&hspi2, tx_buf, 1, 1000); //transmitir dato

while(HAL_SPI_GetState(&hspi2) != HAL_SPI_STATE_READY);

HAL_SPI_Receive(&hspi2, buffer, 4, 1000); // recibir el dato

rec = ((uint16_t)buffer[0] << 8) | buffer[1];

Receive(rec);

HAL_GPIO_WritePin(GPIOB, CS2_Pin, GPIO_PIN_SET); //cs2 alto

HAL_Delay(2000);

 

Thread Notes

Moved from Microcontroller no-OS Drivers to Energy Monitoring and Metering on Saturday, November 18, 2023 10:07:33 PM by colom