2023-07-06 06:54 AM
Hello, I'm using a STEVAL-PLC001V1 card. It is based on a STM32F746ZGT7 that is linked to a CLT01-38SQ7 for inputs and ISO8200AQ for outputs, using SPI.
I try to read datas sent through SPI by the CLT01-38SQ7 and then send it back to the ISO8200AQ.
My board is configured as follow :
I have the following code (only my custom code):
uint8_t inputData;
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* Enable I-Cache---------------------------------------------------------*/
SCB_EnableICache();
/* Enable D-Cache---------------------------------------------------------*/
SCB_EnableDCache();
/* 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_RTC_Init();
MX_SPI2_Init();
MX_DMA2D_Init();
/* USER CODE BEGIN 2 */
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_SET);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, GPIO_PIN_RESET); // Sélection du module d'entrée
HAL_SPI_Receive(&hspi2, &inputData, sizeof(inputData), HAL_MAX_DELAY);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_RESET); // Sélection du module de sortie
HAL_SPI_Transmit(&hspi2, &inputData, sizeof(inputData), HAL_MAX_DELAY);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_SET);
HAL_Delay(1000);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
I receive datas through the SPI only once, and if I want to update it I have to unplug power source and plug it again.
I may have not totally understood how SPI works yet, is there something that I am doing wrong ?
Solved! Go to Solution.
2023-07-26 05:07 AM
I checked again the doc and figured out I wasn't using the right pin for my input. The CS is configured on the PD6 pin. So I changed it, configured it as an output and it works just fine now, thanks for your help !
2023-07-06 11:53 AM
What exactly do you mean "only receive data once"? Does you code only go through the while() loop one time - does it get stuck on the Hal_Delay() call? Or do you call HAL_SPI_Receive() multiple times but the contents of inputData[] does not change?
Check the return values from the HAL receive/transmit functions.
And your PB9 is treated as an output, not as an input.
2023-07-12 01:19 AM
I call HAL_SPI_Receive() multiple times, but the content of inputData does not change. The return values from my fonction seems to work on the first call, but nothing happens on the following calls.
What do you mean by PB9 is not treated as an output ? How do I fix it ?
2023-07-12 10:23 AM - edited 2023-07-12 10:43 AM
PB9 - your code comment says it is an input, but your code uses it as an output. You don't show the MX_GPIO_Int() function so we can't see if it is really configured as an input or as an output. If it is configured as an input then it won't work as the chip select for your "receive" function.
> The return values from my function seems to work on the first call.....
Do you mean that HAL_SPI_Receive() always returns HAL_OK? Do you know that your SPI device is sending different data each time? Have you verified that with a scope or logic analyzer? What device(s) do you have connected to the SPI bus? [EDIT: never mind - I just re-read your initial post that listed these devices]
2023-07-17 12:05 AM
I've got the following configuration for my pins :
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOH_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_14, GPIO_PIN_RESET);
/*Configure GPIO pins : PB10 PB9 */
GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/*Configure GPIO pins : PB11 PB12 PB14 */
GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_14;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
}
As I said, I am new to the concept of SPI, is there something that I am misunderstanding with the functions I am using ? And if so, how should I use them ?
For the data sent, I have LEDs showing the output of my card. They turn on on the first call of the functions (I verified it using debug mode), but then are blocked in the position of the first call, no matter I change the input.
2023-07-24 11:44 AM
Yes - you have PB9 configured as an input pin, but you are trying to use it as an output. That won't work, so you are never really reading from the first device. Change PB9 to an output.
2023-07-26 05:07 AM
I checked again the doc and figured out I wasn't using the right pin for my input. The CS is configured on the PD6 pin. So I changed it, configured it as an output and it works just fine now, thanks for your help !