2020-12-14 15:21:17 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
|
|
#include "main.h"
|
|
|
|
|
|
|
|
UART_HandleTypeDef huart2;
|
2021-07-05 13:09:52 +00:00
|
|
|
I2C_HandleTypeDef hi2c1;
|
2020-12-14 15:21:17 +00:00
|
|
|
|
|
|
|
void SystemClock_Config(void);
|
|
|
|
static void MX_GPIO_Init(void);
|
|
|
|
static void MX_USART2_UART_Init(void);
|
2021-07-05 13:09:52 +00:00
|
|
|
static void MX_I2C1_Init(void);
|
|
|
|
|
2020-12-14 15:21:17 +00:00
|
|
|
|
|
|
|
#ifdef __GNUC__
|
|
|
|
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
|
|
|
|
#else
|
|
|
|
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
|
|
|
|
#endif /* __GNUC__ */
|
|
|
|
|
|
|
|
PUTCHAR_PROTOTYPE
|
|
|
|
{
|
|
|
|
HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, 0xFFFF);
|
|
|
|
|
|
|
|
return ch;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
HAL_Init();
|
|
|
|
SystemClock_Config();
|
|
|
|
MX_GPIO_Init();
|
|
|
|
MX_USART2_UART_Init();
|
2021-07-05 13:09:52 +00:00
|
|
|
MX_I2C1_Init();
|
|
|
|
|
|
|
|
printf("\r\nStarting I2C Scanning:\r\n");
|
|
|
|
for(uint8_t i = 1; i < 128; i++) {
|
|
|
|
uint8_t ret = HAL_I2C_IsDeviceReady(&hi2c1, (uint16_t)(i<<1), 3, 5);
|
|
|
|
if (ret != HAL_OK) { /* No ACK Received At That Address */
|
|
|
|
printf(" - ");
|
|
|
|
} else if(ret == HAL_OK) {
|
|
|
|
printf("0x%X", i);
|
2020-12-14 15:21:17 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-05 13:09:52 +00:00
|
|
|
printf("\r\nDone! \r\n\r\n");
|
|
|
|
|
|
|
|
while(1) {};
|
2020-12-14 15:21:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SystemClock_Config(void)
|
|
|
|
{
|
|
|
|
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
|
|
|
|
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
|
|
|
|
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
|
|
|
|
|
|
|
|
HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);
|
|
|
|
|
|
|
|
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSI;
|
|
|
|
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
|
|
|
|
RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV1;
|
|
|
|
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
|
|
|
|
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
|
|
|
|
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
|
|
|
|
RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV1;
|
|
|
|
RCC_OscInitStruct.PLL.PLLN = 8;
|
|
|
|
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
|
|
|
|
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
|
|
|
|
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
|
|
|
|
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
|
|
|
|
Error_Handler();
|
|
|
|
}
|
|
|
|
|
|
|
|
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1;
|
|
|
|
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
|
|
|
|
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
|
|
|
|
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
|
|
|
|
|
|
|
|
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) {
|
|
|
|
Error_Handler();
|
|
|
|
}
|
|
|
|
|
2021-07-05 13:09:52 +00:00
|
|
|
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART2 | RCC_PERIPHCLK_I2C1;
|
2020-12-14 15:21:17 +00:00
|
|
|
PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
|
|
|
|
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) {
|
|
|
|
Error_Handler();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void MX_USART2_UART_Init(void)
|
|
|
|
{
|
|
|
|
huart2.Instance = USART2;
|
|
|
|
huart2.Init.BaudRate = 115200;
|
|
|
|
huart2.Init.WordLength = UART_WORDLENGTH_8B;
|
|
|
|
huart2.Init.StopBits = UART_STOPBITS_1;
|
|
|
|
huart2.Init.Parity = UART_PARITY_NONE;
|
|
|
|
huart2.Init.Mode = UART_MODE_TX_RX;
|
|
|
|
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
|
|
|
|
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
|
|
|
|
huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
|
|
|
|
huart2.Init.ClockPrescaler = UART_PRESCALER_DIV1;
|
|
|
|
huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
|
|
|
|
if (HAL_UART_Init(&huart2) != HAL_OK) {
|
|
|
|
Error_Handler();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-05 13:09:52 +00:00
|
|
|
/**
|
|
|
|
* @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.Timing = 0x10707DBC;
|
|
|
|
hi2c1.Init.OwnAddress1 = 0;
|
|
|
|
hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
|
|
|
|
hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
|
|
|
|
hi2c1.Init.OwnAddress2 = 0;
|
|
|
|
hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
|
|
|
|
hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
|
|
|
|
hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
|
|
|
|
if (HAL_I2C_Init(&hi2c1) != HAL_OK)
|
|
|
|
{
|
|
|
|
Error_Handler();
|
|
|
|
}
|
|
|
|
/** Configure Analogue filter
|
|
|
|
*/
|
|
|
|
if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
|
|
|
|
{
|
|
|
|
Error_Handler();
|
|
|
|
}
|
|
|
|
/** Configure Digital filter
|
|
|
|
*/
|
|
|
|
if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK)
|
|
|
|
{
|
|
|
|
Error_Handler();
|
|
|
|
}
|
|
|
|
/* USER CODE BEGIN I2C1_Init 2 */
|
|
|
|
|
|
|
|
/* USER CODE END I2C1_Init 2 */
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-12-14 15:21:17 +00:00
|
|
|
static void MX_GPIO_Init(void)
|
|
|
|
{
|
|
|
|
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
|
|
|
|
|
|
|
__HAL_RCC_GPIOC_CLK_ENABLE();
|
|
|
|
__HAL_RCC_GPIOF_CLK_ENABLE();
|
|
|
|
__HAL_RCC_GPIOA_CLK_ENABLE();
|
|
|
|
|
|
|
|
HAL_GPIO_WritePin(LED_GREEN_GPIO_Port, LED_GREEN_Pin, GPIO_PIN_RESET);
|
|
|
|
|
|
|
|
GPIO_InitStruct.Pin = LED_GREEN_Pin;
|
|
|
|
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
|
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
|
|
|
|
HAL_GPIO_Init(LED_GREEN_GPIO_Port, &GPIO_InitStruct);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Error_Handler(void)
|
|
|
|
{
|
|
|
|
__disable_irq();
|
|
|
|
while (1) {
|
|
|
|
}
|
|
|
|
}
|