2017-09-25 06:27 AM
I'm trying to use two AD7690 (18-bit ADC)
http://www.analog.com/media/en/technical-documentation/data-sheets/AD7690.pdf
(page 20)But when I accept the data, I get a mistake HAL_ERROR
However, I'm getting some data there
HAL_GPIO_WritePin(GPIOA, ADC_CVN_Pin, GPIO_PIN_SET);
HAL_Delay(10); HAL_SPI_Receive(&spi1, (uint8*)data, 8, 1000); HAL_GPIO_WritePin(GPIOA, ADC_CVN_Pin, GPIO_PIN_RESET);spi1.Instance = SPI1;
spi1.Init.Mode = SPI_MODE_MASTER; spi1.Init.Direction = SPI_DIRECTION_2LINES_RXONLY; spi1.Init.DataSize = SPI_DATASIZE_8BIT; spi1.Init.CLKPolarity = SPI_POLARITY_LOW; spi1.Init.CLKPhase = SPI_PHASE_1EDGE; spi1.Init.NSS = SPI_NSS_SOFT; spi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256; spi1.Init.FirstBit = SPI_FIRSTBIT_MSB; spi1.Init.TIMode = SPI_TIMODE_DISABLE; spi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; spi1.Init.CRCPolynomial = 10;How do I solve this problem?
#stm32 #adc #ad7690 #spi #hal2017-09-25 01:41 PM
Can you observe the SPI pins using oscilloscope or logic analyzer?
JW
2017-09-26 02:33 AM
That's what I see on oscilloscope
2017-09-26 02:40 AM
I don't use Cube but the usage of Rx-only mode may be tricky, as it churns out clocks and may easily lead to overflow if not handled rapidly enough. Use normal duplex mode - you don't need to set the MOSI pin in GPIO at all.
JW
2017-09-26 07:05 AM
Your scope trace doesn't match your code. your doing 8bit spi and askingfor 8bytes, i would have expect to see64clock cycles. Assuming your doing '3wire no busy mode' on your ADC, you only need 18 clk. So use 8bit x 3 (or maybe 7bit x 3).
I looked at the datasheet, your CONVERT seems wrong. Set Convert high, wait for DATA to go low, then read out data.
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_SPI1_Init();
/* USER CODE BEGIN 2 */
uint8_t buffer[3];
//start first conversion
HAL_GPIO_WritePin(CONVERT_GPIO_Port,CONVERT_Pin, GPIO_PIN_SET);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
//wait for end of conversion
while (HAL_GPIO_ReadPin(MISO_GPIO_Port, MISO_Pin) == GPIO_PIN_SET);
//set convert low for readout
HAL_GPIO_WritePin(CONVERT_GPIO_Port,CONVERT_Pin, GPIO_PIN_RESET);
//Do SPI Receive
HAL_SPI_Receive(&hspi1, (uint8_t*)buffer, 3, 1000);
uint32_t data = buffer[0] << 10 + buffer[1] << 2 + buffer[2] >> 6; //19bits
//restart conversion
HAL_GPIO_WritePin(CONVERT_GPIO_Port,CONVERT_Pin, GPIO_PIN_SET);
//Do other stuff while converting
HAL_Del(100);
}
/* USER CODE END 3 */
}�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?
data will have the 18bits that are clocked out.
data packing is based on 8bit setup.
void MX_SPI1_Init(void)
{
hspi1.Instance = SPI1;
hspi1.Init.Mode = SPI_MODE_MASTER;
hspi1.Init.Direction = SPI_DIRECTION_2LINES_RXONLY;
hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
hspi1.Init.NSS = SPI_NSS_SOFT;
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi1.Init.CRCPolynomial = 7;
hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
hspi1.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;
if (HAL_SPI_Init(&hspi1) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
}�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?
you could use 7bit frames to minimize the readout time.
hspi1.Init.DataSize = SPI_DATASIZE_7BIT;�?
then data would unpack something like;
uint32_t data = buffer[0] << 12 + buffer[1] << 4 + buffer[2] >> 3;