我用STM32F207ZET6创建了一个 PCB ,它有一个通过 I2C 连接的NXP NFC芯片。我遇到的问题是我无法与 NFC 芯片通信。
我已经验证了 ARM 芯片和 NFC 之间的 I2C 连接。我已经验证了两个芯片都获得了正确的功率。
我正在使用带有 stlink-v3 的 stm32cube IDE。我配置了 I2C1 引脚并编写了以下代码。
我试过的
- 我试过用
HAL_I2C_IsDeviceReady()
看NFC是否连接。这会返回 true,但它也会在我输入的每个地址上返回 true。 - 我试图发送命令并将它们读回缓冲区数组。返回的都是0。
cmds[1] = 0x60;
cmds[2] = 0x00;
HAL_I2C_Master_Transmit(&hi2c1, nfc_Adr_w, cmds, 2, HAL_MAX_DELAY);
HAL_Delay(15);
HAL_I2C_Master_Transmit(&hi2c1, nfc_Adr_w, cmds, 1, HAL_MAX_DELAY);
HAL_I2C_Master_Receive(&hi2c1, nfc_Adr_r, buffer, 1, HAL_MAX_DELAY);
任何帮助将不胜感激,谢谢。
完整代码
#include "main.h"
/* Private variables ---------------------------------------------------------*/
I2C_HandleTypeDef hi2c1;
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_I2C1_Init(void);
unsigned char buffer[150];
unsigned char cmds[3];
_Bool results = 0;
uint8_t devices = 0u;
int deviceReady = 0u;
/* Private user code ---------------------------------------------------------*/
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
uint8_t nfc_Adr_w = 0b10101010;
uint8_t nfc_Adr_r = 0b10101011;
/* 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_I2C1_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
//HAL_I2C_Master_Transmit();
//HAL_I2C_Master_Receive();
cmds[0] = 0x00;
cmds[1] = 0x60;
cmds[2] = 0x00;
HAL_I2C_Master_Transmit(&hi2c1, nfc_Adr_w, cmds, 2, HAL_MAX_DELAY);
HAL_Delay(15);
HAL_I2C_Master_Transmit(&hi2c1, nfc_Adr_w, cmds, 1, HAL_MAX_DELAY);
HAL_I2C_Master_Receive(&hi2c1, nfc_Adr_r, buffer, 1, HAL_MAX_DELAY);
// deviceReady = HAL_I2C_IsDeviceReady(&hi2c1, 0x3C, 3u, 10u);
// printf("Searching for I2C devices on the bus...\n");
/* Values outside 0x03 and 0x77 are invalid. */
// for (uint8_t i = 0x03u; i < 0x78u; i++)
// {
// uint8_t address = i << 1u;
// /* In case there is a positive feedback, print it out. */
// if (HAL_OK == HAL_I2C_IsDeviceReady(&hi2c1, address, 3u, 10u))
// {
//// printf("Device found: 0x%02X\n", address);
// devices++;
// results = 1;
// }
// if (HAL_TIMEOUT == HAL_I2C_IsDeviceReady(&hi2c1, address, 3u, 10u)) {
// devices++;
// }
// deviceReady = HAL_I2C_IsDeviceReady(&hi2c1, address, 3u, 10u);
// }
// /* Feedback of the total number of devices. */
//// if (0u == devices)
//// {
////// printf("No device found.\n");
//// }
//// else
//// {
//// printf("Total found devices: %d\n", devices);
//// }
HAL_Delay(1000u);
devices = 0u;
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Initializes the CPU, AHB and APB busses clocks
*/
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 busses 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();
}
}
/**
* @brief I2C1 Initialization Function
* @param None
* @retval None
*/
static void MX_I2C1_Init(void)
{
/* USER CODE BEGIN I2C1_Init 0 */
/* USER CODE END I2C1_Init 0 */
/* USER CODE BEGIN I2C1_Init 1 */
/* USER CODE END I2C1_Init 1 */
hi2c1.Instance = I2C1;
hi2c1.Init.ClockSpeed = 100000;
hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
hi2c1.Init.OwnAddress1 = 0;
hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c1.Init.OwnAddress2 = 0;
hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if (HAL_I2C_Init(&hi2c1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN I2C1_Init 2 */
/* USER CODE END I2C1_Init 2 */
}
/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
/*Configure GPIO pin : LED_Pin */
GPIO_InitStruct.Pin = LED_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct);
}
/* 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 */
/* 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,
tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/