2022-05-30 03:10 AM
Hello everyone, I have a problem about mcp23s17, I try to make communication using mcp23s17 and SPI on the stm32f407vg but although My code is correct, ıt doesnt work, When I make debug, I can see 8 bit number in the register DR of SPI1 using HAL_SPI_Receive(&hspi1, (uint8_t *)Rxbuffer, 8, 500); but the number only is 0xff. If you help me , I will be happy.
**Question moved @bünyamin terzi
#include "main.h"
SPI_HandleTypeDef hspi1;
GPIO_InitTypeDef CS;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_SPI1_Init(void);
int main(void)
{
uint8_t Rxbuffer[8] ={0};
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_SPI1_Init();
CS.Pin = GPIO_PIN_8;
CS.Mode = GPIO_MODE_OUTPUT_PP;
CS.Pull = GPIO_PULLDOWN;
CS.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOD, &CS);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_8,SET );
__HAL_SPI_ENABLE(&hspi1);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_8,RESET );
HAL_SPI_Receive(&hspi1, (uint8_t *)Rxbuffer, 8, 500);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_8,SET );
while (1)
{
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
}
static void MX_SPI1_Init(void)
{
hspi1.Instance = SPI1;
hspi1.Init.Mode = SPI_MODE_MASTER;
hspi1.Init.Direction = SPI_DIRECTION_2LINES;
hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;
hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
hspi1.Init.NSS = SPI_NSS_SOFT;
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64;
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi1.Init.CRCPolynomial = 7;
if (HAL_SPI_Init(&hspi1) != HAL_OK)
{
Error_Handler();
}
}
static void MX_GPIO_Init(void)
{
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOH_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_SPI1_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
2022-05-30 03:57 AM
The post was "moved" because it was posted in a section saying "don't post technical questions here"
Perhaps ha a delay or fencing command after driving the chip select low.
Consider using a TransmitReceive variant so as to generate clocks, and send a command or request to the part.
Check the SPI pin interface with a scope or logic-analyzer.
Here's a data sheet on the part https://ww1.microchip.com/downloads/en/devicedoc/20001952c.pdf
2022-05-30 05:43 AM
> Check the SPI pin interface with a scope or logic-analyzer.
+1
SPI is synchronous, so you can also bit-bang, i.e. manually clock out/in data, by not using the hardware SPI but setting SCK and MOSI to Out and MISO to In, doing it with whatever timing you want, even slow enough to be able to observe it using LEDs.
JW
2022-05-30 06:47 AM
thank you for help , sorry, I wrote in wrong place ,I will try that you said
2022-05-30 07:15 AM
I have tried but it doesnt work , should I send fake data before I read data ?
2022-06-01 08:41 AM