Facebook
From QWEWEQWE, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 235
  1. /**
  2.   ******************************************************************************
  3.   * @file           : main.c
  4.   * @brief          : Main program body
  5.   ******************************************************************************
  6.   ** This notice applies to any and all portions of this file
  7.   * that are not between comment pairs USER CODE BEGIN and
  8.   * USER CODE END. Other portions of this file, whether
  9.   * inserted by the user or by software development tools
  10.   * are owned by their respective copyright owners.
  11.   *
  12.   * COPYRIGHT(c) 2018 STMicroelectronics
  13.   *
  14.   * Redistribution and use in source and binary forms, with or without modification,
  15.   * are permitted provided that the following conditions are met:
  16.   *   1. Redistributions of source code must retain the above copyright notice,
  17.   *      this list of conditions and the following disclaimer.
  18.   *   2. Redistributions in binary form must reproduce the above copyright notice,
  19.   *      this list of conditions and the following disclaimer in the documentation
  20.   *      and/or other materials provided with the distribution.
  21.   *   3. Neither the name of STMicroelectronics nor the names of its contributors
  22.   *      may be used to endorse or promote products derived from this software
  23.   *      without specific prior written permission.
  24.   *
  25.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26.   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27.   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28.   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  29.   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30.   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  31.   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32.   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  33.   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34.   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35.   *
  36.   ******************************************************************************
  37.   */
  38. /* Includes ------------------------------------------------------------------*/
  39. #include "main.h"
  40. #include "stm32f3xx_hal.h"
  41. #include "i2c.h"
  42. #include "spi.h"
  43. #include "usb.h"
  44. #include "gpio.h"
  45.  
  46. /* USER CODE BEGIN Includes */
  47.  
  48. /* USER CODE END Includes */
  49.  
  50. /* Private variables ---------------------------------------------------------*/
  51.  
  52. /* USER CODE BEGIN PV */
  53. unsigned char dataTx,dataRx,rawValue[6] ;
  54. signed short rawMeasurement[3];
  55. float measurements[0];
  56. /* Private variables ---------------------------------------------------------*/
  57.  
  58. /* USER CODE END PV */
  59.  
  60. /* Private function prototypes -----------------------------------------------*/
  61. void SystemClock_Config(void);
  62.  
  63. /* USER CODE BEGIN PFP */
  64. /* Private function prototypes -----------------------------------------------*/
  65.  
  66. /* USER CODE END PFP */
  67.  
  68. /* USER CODE BEGIN 0 */
  69.  
  70. /* USER CODE END 0 */
  71.  
  72. /**
  73.   * @brief  The application entry point.
  74.   *
  75.   * @retval None
  76.   */
  77. int main(void)
  78. {
  79.   /* USER CODE BEGIN 1 */
  80.  
  81.   /* USER CODE END 1 */
  82.  
  83.   /* MCU Configuration----------------------------------------------------------*/
  84.  
  85.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  86.   HAL_Init();
  87.  
  88.   /* USER CODE BEGIN Init */
  89.  
  90.   /* USER CODE END Init */
  91.  
  92.   /* Configure the system clock */
  93.   SystemClock_Config();
  94.  
  95.   /* USER CODE BEGIN SysInit */
  96.  
  97.   /* USER CODE END SysInit */
  98.  
  99.   /* Initialize all configured peripherals */
  100.   MX_GPIO_Init();
  101.   MX_I2C1_Init();
  102.   MX_SPI1_Init();
  103.   MX_USB_PCD_Init();
  104.   /* USER CODE BEGIN 2 */
  105.         dataTx=0x0F|0x80;
  106.         HAL_GPIO_WritePin(GPIOE,GPIO_PIN_3,GPIO_PIN_RESET);
  107.   HAL_SPI_Transmit(&hspi1,&dataTx,1,100);
  108.         HAL_SPI_Receive(&hspi1,&dataRx,1,100);
  109.         HAL_GPIO_WritePin(GPIOE,GPIO_PIN_3,GPIO_PIN_RESET);
  110.  
  111. /* set configration for the CTRL1 */
  112. L3GD20Unit.Power.Mode=L3GD20_MODE_ACTIVE;
  113. L3GD20Unit.Band.Width=L3GD20_BANDWITH_1;
  114. L3GD20Unit.Axes.Enable=L3GD20_AXES_ENABLE;
  115. L3GD20Unit.Full_Scale=L3GD20_FULLSCALE_20;
  116. L3GD20Init.Output_DateRate=L3GD20_OUTPUT_DATARATE_380HZ;
  117. L3GD20Init.Endianness=L3GD20_BLE_MSB;
  118. LG3D20_Init(&L3GD20Init)
  119.  
  120.  
  121.  
  122.  
  123.   /* USER CODE END 2 */
  124.  
  125.   /* Infinite loop */
  126.   /* USER CODE BEGIN WHILE */
  127.   while (1)
  128.   {
  129.  
  130.   /* USER CODE END WHILE */
  131.  
  132.   /* USER CODE BEGIN 3 */
  133.         dataTx=0x28|0x80|0x40;
  134.         HAL_GPIO_WritePin(GPIOE,GPIO_PIN_3,GPIO_PIN_RESET);
  135.                 HAL_SPI_Transmit(&hspi1,&dataTx,1,100);
  136.         HAL_SPI_Receive(&hspi1,rawValue,6,100);
  137.         HAL_GPIO_WritePin(GPIOE,GPIO_PIN_3,GPIO_PIN_RESET);
  138.                
  139.                 /*  group seperate register value in 16bits measurements    */
  140.                 rawMeasurement[0]= (signed short)rawValue[0] + (signed short)rawValue[1]*256 ;
  141.                         rawMeasurement[1]=(signed short) rawValue[2] + (signed short)rawValue[3]*256 ;
  142.                                 rawMeasurement[2]=(signed short) rawValue[4] + (signed short)rawValue[5]*256 ;
  143.                
  144.   }
  145.   /* USER CODE END 3 */
  146.  
  147. }
  148.  
  149. /**
  150.   * @brief System Clock Configuration
  151.   * @retval None
  152.   */
  153. void SystemClock_Config(void)
  154. {
  155.  
  156.   RCC_OscInitTypeDef RCC_OscInitStruct;
  157.   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  158.   RCC_PeriphCLKInitTypeDef PeriphClkInit;
  159.  
  160.     /**Initializes the CPU, AHB and APB busses clocks
  161.     */
  162.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSE;
  163.   RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
  164.   RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  165.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  166.   RCC_OscInitStruct.HSICalibrationValue = 16;
  167.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  168.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  169.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL6;
  170.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  171.   {
  172.     _Error_Handler(__FILE__, __LINE__);
  173.   }
  174.  
  175.     /**Initializes the CPU, AHB and APB busses clocks
  176.     */
  177.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  178.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  179.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  180.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  181.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  182.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  183.  
  184.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  185.   {
  186.     _Error_Handler(__FILE__, __LINE__);
  187.   }
  188.  
  189.   PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB|RCC_PERIPHCLK_I2C1;
  190.   PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_HSI;
  191.   PeriphClkInit.USBClockSelection = RCC_USBCLKSOURCE_PLL;
  192.   if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  193.   {
  194.     _Error_Handler(__FILE__, __LINE__);
  195.   }
  196.  
  197.     /**Configure the Systick interrupt time
  198.     */
  199.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  200.  
  201.     /**Configure the Systick
  202.     */
  203.   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  204.  
  205.   /* SysTick_IRQn interrupt configuration */
  206.   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  207. }
  208.  
  209. /* USER CODE BEGIN 4 */
  210.  
  211. /* USER CODE END 4 */
  212.  
  213. /**
  214.   * @brief  This function is executed in case of error occurrence.
  215.   * @param  file: The file name as string.
  216.   * @param  line: The line in file as a number.
  217.   * @retval None
  218.   */
  219. void _Error_Handler(char *file, int line)
  220. {
  221.   /* USER CODE BEGIN Error_Handler_Debug */
  222.   /* User can add his own implementation to report the HAL error return state */
  223.   while(1)
  224.   {
  225.   }
  226.   /* USER CODE END Error_Handler_Debug */
  227. }
  228.  
  229. #ifdef  USE_FULL_ASSERT
  230. /**
  231.   * @brief  Reports the name of the source file and the source line number
  232.   *         where the assert_param error has occurred.
  233.   * @param  file: pointer to the source file name
  234.   * @param  line: assert_param error line source number
  235.   * @retval None
  236.   */
  237. void assert_failed(uint8_t* file, uint32_t line)
  238. {
  239.   /* USER CODE BEGIN 6 */
  240.   /* User can add his own implementation to report the file name and line number,
  241.      tex: printf("Wrong parameters value: file %s on line %drn", file, line) */
  242.   /* USER CODE END 6 */
  243. }
  244. #endif /* USE_FULL_ASSERT */
  245.  
  246. /**
  247.   * @}
  248.   */
  249.  
  250. /**
  251.   * @}
  252.   */
  253.  
  254. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  255.