Guest User

Sensor

a guest
Jun 15th, 2020
58
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* USER CODE BEGIN Header */
  2. /**
  3.   ******************************************************************************
  4.   * @file           : main.c
  5.   * @brief          : Main program body
  6.   ******************************************************************************
  7.   * @attention
  8.   *
  9.   * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
  10.   * All rights reserved.</center></h2>
  11.   *
  12.   * This software component is licensed by ST under BSD 3-Clause license,
  13.   * the "License"; You may not use this file except in compliance with the
  14.   * License. You may obtain a copy of the License at:
  15.   *                        opensource.org/licenses/BSD-3-Clause
  16.   *
  17.   ******************************************************************************
  18.   */
  19. /* USER CODE END Header */
  20.  
  21. /* Includes ------------------------------------------------------------------*/
  22. #include "main.h"
  23.  
  24. /* Private includes ----------------------------------------------------------*/
  25. /* USER CODE BEGIN Includes */
  26. #include <string.h>
  27. #include <stdio.h>
  28. /* USER CODE END Includes */
  29.  
  30. /* Private typedef -----------------------------------------------------------*/
  31. /* USER CODE BEGIN PTD */
  32.  
  33. /* USER CODE END PTD */
  34.  
  35. /* Private define ------------------------------------------------------------*/
  36. /* USER CODE BEGIN PD */
  37. /* USER CODE END PD */
  38.  
  39. /* Private macro -------------------------------------------------------------*/
  40. /* USER CODE BEGIN PM */
  41.  
  42. /* USER CODE END PM */
  43.  
  44. /* Private variables ---------------------------------------------------------*/
  45. I2C_HandleTypeDef hi2c1;
  46.  
  47. UART_HandleTypeDef huart2;
  48.  
  49. /* USER CODE BEGIN PV */
  50.  
  51. /* USER CODE END PV */
  52.  
  53. /* Private function prototypes -----------------------------------------------*/
  54. void SystemClock_Config(void);
  55. static void MX_GPIO_Init(void);
  56. static void MX_I2C1_Init(void);
  57. static void MX_USART2_UART_Init(void);
  58. /* USER CODE BEGIN PFP */
  59.  
  60. /* USER CODE END PFP */
  61.  
  62. /* Private user code ---------------------------------------------------------*/
  63. /* USER CODE BEGIN 0 */
  64.  
  65. /* USER CODE END 0 */
  66.  
  67. /**
  68.   * @brief  The application entry point.
  69.   * @retval int
  70.   */
  71. int main(void)
  72. {
  73.   /* USER CODE BEGIN 1 */
  74.  
  75.   /* USER CODE END 1 */
  76.  
  77.   /* MCU Configuration--------------------------------------------------------*/
  78.  
  79.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  80.   HAL_Init();
  81.  
  82.   /* USER CODE BEGIN Init */
  83.  
  84.   /* USER CODE END Init */
  85.  
  86.   /* Configure the system clock */
  87.   SystemClock_Config();
  88.  
  89.   /* USER CODE BEGIN SysInit */
  90.  
  91.   /* USER CODE END SysInit */
  92.  
  93.   /* Initialize all configured peripherals */
  94.   MX_GPIO_Init();
  95.   MX_I2C1_Init();
  96.   MX_USART2_UART_Init();
  97.   /* USER CODE BEGIN 2 */
  98.  
  99.   /* USER CODE END 2 */
  100.    
  101.     uint8_t buf[12];
  102.     HAL_StatusTypeDef result;
  103.    
  104.     uint8_t SlaveAdress = (0x69<<1); // Accel and Gyro I2C Slave Adress that sent acknowledge
  105.     uint8_t CTRL_REG6_XL = 0x20; // Control Register to turn on the Accel
  106.     uint8_t sent_data[2]; // Setting ODDR_XL2 = 1, ODDR_XL1 = 1, ODDR_XL0 = 0, rest is 0, accel turned on
  107.     sent_data[0] = 0xC0;
  108.     uint8_t received_data;
  109.    
  110.    
  111.     //Writing to the control register to activate the accel
  112.     result = HAL_I2C_Mem_Write(&hi2c1, SlaveAdress, CTRL_REG6_XL, I2C_MEMADD_SIZE_8BIT, sent_data, 1, HAL_MAX_DELAY);
  113.     if ( result != HAL_OK ) {
  114.         strcpy((char*)buf, "There was an error while writing!\r\n");
  115.         HAL_UART_Transmit(&huart2, buf, strlen((char*) buf), HAL_MAX_DELAY);
  116.     } else {
  117.         strcpy((char*)buf, "Data sent succesfully!\r\n");
  118.         HAL_UART_Transmit(&huart2, buf, strlen((char*) buf), HAL_MAX_DELAY);
  119.     }
  120.     buf[0] = 0x00;
  121.     //Reading from the control register that we just wrote to
  122.     result = HAL_I2C_Mem_Read(&hi2c1, SlaveAdress, CTRL_REG6_XL, sizeof(uint8_t), &received_data, sizeof(uint8_t), HAL_MAX_DELAY);
  123.     if ( result != HAL_OK ) {
  124.         strcpy((char*)buf, "There was an error while reading!\r\n");
  125.         HAL_UART_Transmit(&huart2, buf, strlen((char*) buf), HAL_MAX_DELAY);
  126.     } else {
  127.         strcpy((char*)buf, "Data read succesfully!\r\n");
  128.         HAL_UART_Transmit(&huart2, buf, strlen((char*) buf), HAL_MAX_DELAY);
  129.     }
  130.    
  131.     strcpy((char*)buf, "CTRL_REG6_XL: ");
  132.   HAL_UART_Transmit(&huart2, buf, strlen((char*) buf), HAL_MAX_DELAY);
  133.     sprintf((char*)buf, "%X h", received_data);
  134.     HAL_UART_Transmit(&huart2, buf, strlen((char*) buf), HAL_MAX_DELAY);
  135.    
  136.   /* Infinite loop */
  137.   /* USER CODE BEGIN WHILE */
  138.   while (1)
  139.   {
  140.     /* USER CODE END WHILE */
  141.  
  142.     /* USER CODE BEGIN 3 */
  143.   }
  144.   /* USER CODE END 3 */
  145. }
  146.  
  147. /**
  148.   * @brief System Clock Configuration
  149.   * @retval None
  150.   */
  151. void SystemClock_Config(void)
  152. {
  153.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  154.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  155.   RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  156.  
  157.   /** Initializes the CPU, AHB and APB busses clocks
  158.   */
  159.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  160.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  161.   RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  162.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  163.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  164.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12;
  165.   RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
  166.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  167.   {
  168.     Error_Handler();
  169.   }
  170.   /** Initializes the CPU, AHB and APB busses clocks
  171.   */
  172.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  173.                               |RCC_CLOCKTYPE_PCLK1;
  174.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  175.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  176.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  177.  
  178.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  179.   {
  180.     Error_Handler();
  181.   }
  182.   PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_I2C1;
  183.   PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_HSI;
  184.   if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  185.   {
  186.     Error_Handler();
  187.   }
  188. }
  189.  
  190. /**
  191.   * @brief I2C1 Initialization Function
  192.   * @param None
  193.   * @retval None
  194.   */
  195. static void MX_I2C1_Init(void)
  196. {
  197.  
  198.   /* USER CODE BEGIN I2C1_Init 0 */
  199.  
  200.   /* USER CODE END I2C1_Init 0 */
  201.  
  202.   /* USER CODE BEGIN I2C1_Init 1 */
  203.  
  204.   /* USER CODE END I2C1_Init 1 */
  205.   hi2c1.Instance = I2C1;
  206.   hi2c1.Init.Timing = 0x2000090E;
  207.   hi2c1.Init.OwnAddress1 = 0;
  208.   hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  209.   hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  210.   hi2c1.Init.OwnAddress2 = 0;
  211.   hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
  212.   hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  213.   hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  214.   if (HAL_I2C_Init(&hi2c1) != HAL_OK)
  215.   {
  216.     Error_Handler();
  217.   }
  218.   /** Configure Analogue filter
  219.   */
  220.   if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
  221.   {
  222.     Error_Handler();
  223.   }
  224.   /** Configure Digital filter
  225.   */
  226.   if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK)
  227.   {
  228.     Error_Handler();
  229.   }
  230.   /* USER CODE BEGIN I2C1_Init 2 */
  231.  
  232.   /* USER CODE END I2C1_Init 2 */
  233.  
  234. }
  235.  
  236. /**
  237.   * @brief USART2 Initialization Function
  238.   * @param None
  239.   * @retval None
  240.   */
  241. static void MX_USART2_UART_Init(void)
  242. {
  243.  
  244.   /* USER CODE BEGIN USART2_Init 0 */
  245.  
  246.   /* USER CODE END USART2_Init 0 */
  247.  
  248.   /* USER CODE BEGIN USART2_Init 1 */
  249.  
  250.   /* USER CODE END USART2_Init 1 */
  251.   huart2.Instance = USART2;
  252.   huart2.Init.BaudRate = 115200;
  253.   huart2.Init.WordLength = UART_WORDLENGTH_8B;
  254.   huart2.Init.StopBits = UART_STOPBITS_1;
  255.   huart2.Init.Parity = UART_PARITY_NONE;
  256.   huart2.Init.Mode = UART_MODE_TX_RX;
  257.   huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  258.   huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  259.   huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  260.   huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  261.   if (HAL_UART_Init(&huart2) != HAL_OK)
  262.   {
  263.     Error_Handler();
  264.   }
  265.   /* USER CODE BEGIN USART2_Init 2 */
  266.  
  267.   /* USER CODE END USART2_Init 2 */
  268.  
  269. }
  270.  
  271. /**
  272.   * @brief GPIO Initialization Function
  273.   * @param None
  274.   * @retval None
  275.   */
  276. static void MX_GPIO_Init(void)
  277. {
  278.   GPIO_InitTypeDef GPIO_InitStruct = {0};
  279.  
  280.   /* GPIO Ports Clock Enable */
  281.   __HAL_RCC_GPIOC_CLK_ENABLE();
  282.   __HAL_RCC_GPIOF_CLK_ENABLE();
  283.   __HAL_RCC_GPIOA_CLK_ENABLE();
  284.   __HAL_RCC_GPIOB_CLK_ENABLE();
  285.  
  286.   /*Configure GPIO pin Output Level */
  287.   HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
  288.  
  289.   /*Configure GPIO pin : B1_Pin */
  290.   GPIO_InitStruct.Pin = B1_Pin;
  291.   GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
  292.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  293.   HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
  294.  
  295.   /*Configure GPIO pin : LD2_Pin */
  296.   GPIO_InitStruct.Pin = LD2_Pin;
  297.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  298.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  299.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  300.   HAL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct);
  301.  
  302. }
  303.  
  304. /* USER CODE BEGIN 4 */
  305.  
  306. /* USER CODE END 4 */
  307.  
  308. /**
  309.   * @brief  This function is executed in case of error occurrence.
  310.   * @retval None
  311.   */
  312. void Error_Handler(void)
  313. {
  314.   /* USER CODE BEGIN Error_Handler_Debug */
  315.   /* User can add his own implementation to report the HAL error return state */
  316.  
  317.   /* USER CODE END Error_Handler_Debug */
  318. }
  319.  
  320. #ifdef  USE_FULL_ASSERT
  321. /**
  322.   * @brief  Reports the name of the source file and the source line number
  323.   *         where the assert_param error has occurred.
  324.   * @param  file: pointer to the source file name
  325.   * @param  line: assert_param error line source number
  326.   * @retval None
  327.   */
  328. void assert_failed(uint8_t *file, uint32_t line)
  329. {
  330.   /* USER CODE BEGIN 6 */
  331.   /* User can add his own implementation to report the file name and line number,
  332.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  333.   /* USER CODE END 6 */
  334. }
  335. #endif /* USE_FULL_ASSERT */
  336.  
  337. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
RAW Paste Data