uranyumx
Advanced Member level 4
Hello,
I want to read data from a custom designed ADC chip through SPI interface. I guess there is an issue about my SPI codes, do you have any feedback on it?
SPI Baudrate: 10 Mbits/s, and the maximum SCLK frequency is 24 MHz
Codes in while loop:
SPI Settings:
I want to read data from a custom designed ADC chip through SPI interface. I guess there is an issue about my SPI codes, do you have any feedback on it?
SPI Baudrate: 10 Mbits/s, and the maximum SCLK frequency is 24 MHz
Codes in while loop:
Code:
uint16_t ReadADCAmplifier(uint8_t regAddress) {
uint8_t txBuffer[2];
uint8_t rxBuffer[2];
txBuffer[0] = regAddress;
txBuffer[1] = 0x00;
HAL_GPIO_WritePin(ADCAmp_SPI2_CS_GPIO_Port, ADCAmp_SPI2_CS_Pin, GPIO_PIN_RESET);
HAL_SPI_TransmitReceive(&hspi2, txBuffer, rxBuffer, 2, 1000);
HAL_GPIO_WritePin(ADCAmp_SPI2_CS_GPIO_Port, ADCAmp_SPI2_CS_Pin, GPIO_PIN_SET);
uint16_t value = ((rxBuffer[0] << 8) | rxBuffer[1]);
return value;
SPI Settings:
Code:
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_8BIT;
hspi2.Init.CLKPolarity = SPI_POLARITY_LOW;
hspi2.Init.CLKPhase = SPI_PHASE_2EDGE;
hspi2.Init.NSS = SPI_NSS_SOFT;
hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;
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 */
}