Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.09 KB | None | 0 0
  1. /**
  2. ******************************************************************************
  3. * File Name : main.c
  4. * Description : Main program body
  5. ******************************************************************************
  6. *
  7. * COPYRIGHT(c) 2017 STMicroelectronics
  8. *
  9. * Redistribution and use in source and binary forms, with or without modification,
  10. * are permitted provided that the following conditions are met:
  11. * 1. Redistributions of source code must retain the above copyright notice,
  12. * this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright notice,
  14. * this list of conditions and the following disclaimer in the documentation
  15. * and/or other materials provided with the distribution.
  16. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  24. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  26. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  27. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. ******************************************************************************
  32. */
  33. /* Includes ------------------------------------------------------------------*/
  34. #include "main.h"
  35. #include "stm32f4xx_hal.h"
  36. #include "i2c.h"
  37. #include "gpio.h"
  38.  
  39. /* USER CODE BEGIN Includes */
  40. #include "lcd.h"
  41. #include "string.h"
  42. /* USER CODE END Includes */
  43.  
  44. /* Private variables ---------------------------------------------------------*/
  45.  
  46. /* USER CODE BEGIN PV */
  47. /* Private variables ---------------------------------------------------------*/
  48.  
  49. /* USER CODE END PV */
  50.  
  51. /* Private function prototypes -----------------------------------------------*/
  52. void SystemClock_Config(void);
  53. void Error_Handler(void);
  54.  
  55. /* USER CODE BEGIN PFP */
  56. /* Private function prototypes -----------------------------------------------*/
  57.  
  58. /* USER CODE END PFP */
  59.  
  60. /* USER CODE BEGIN 0 */
  61. uint8_t EEPROM_DEVICE_ADDRESS = 0b10100000;
  62. uint8_t * str = "Hello, TechMaker!";
  63. uint8_t buf[200] = "";
  64. uint8_t a = 25;
  65. uint8_t * b;
  66. /* USER CODE END 0 */
  67.  
  68. int main(void)
  69. {
  70.  
  71. /* USER CODE BEGIN 1 */
  72.  
  73. /* USER CODE END 1 */
  74.  
  75. /* MCU Configuration----------------------------------------------------------*/
  76.  
  77. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  78. HAL_Init();
  79.  
  80. /* Configure the system clock */
  81. SystemClock_Config();
  82.  
  83. /* Initialize all configured peripherals */
  84. MX_GPIO_Init();
  85. MX_I2C1_Init();
  86.  
  87. /* USER CODE BEGIN 2 */
  88. LCD_Init();
  89. HAL_I2C_Mem_Write(&hi2c1, EEPROM_DEVICE_ADDRESS, 0x0000, I2C_MEMADD_SIZE_16BIT, str, strlen(str)+1, 100);
  90.  
  91. while (HAL_I2C_IsDeviceReady(&hi2c1, EEPROM_DEVICE_ADDRESS, 100, 1000) != HAL_OK){
  92. ;
  93. }
  94.  
  95. HAL_I2C_Mem_Read(&hi2c1, EEPROM_DEVICE_ADDRESS, 0x0000, I2C_MEMADD_SIZE_16BIT, (uint8_t *)buf, strlen(str)+1, 1000);
  96. LCD_Printf("%s\n", buf);
  97.  
  98. b = &a;
  99. HAL_I2C_Mem_Write(&hi2c1, EEPROM_DEVICE_ADDRESS, 0x1111, I2C_MEMADD_SIZE_16BIT, *b, strlen(*b)+1, 100);
  100.  
  101. while (HAL_I2C_IsDeviceReady(&hi2c1, EEPROM_DEVICE_ADDRESS, 100, 1000) != HAL_OK){
  102. ;
  103. }
  104.  
  105. HAL_I2C_Mem_Read(&hi2c1, EEPROM_DEVICE_ADDRESS, 0x1111, I2C_MEMADD_SIZE_16BIT, (uint8_t *)buf, strlen(*b)+1, 1000);
  106. LCD_Printf("%s\n", buf);
  107.  
  108. /* USER CODE END 2 */
  109.  
  110. /* Infinite loop */
  111. /* USER CODE BEGIN WHILE */
  112. while (1)
  113. {
  114. /* USER CODE END WHILE */
  115.  
  116. /* USER CODE BEGIN 3 */
  117.  
  118. }
  119. /* USER CODE END 3 */
  120.  
  121. }
  122.  
  123. /** System Clock Configuration
  124. */
  125. void SystemClock_Config(void)
  126. {
  127.  
  128. RCC_OscInitTypeDef RCC_OscInitStruct;
  129. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  130.  
  131. /**Configure the main internal regulator output voltage
  132. */
  133. __HAL_RCC_PWR_CLK_ENABLE();
  134.  
  135. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  136.  
  137. /**Initializes the CPU, AHB and APB busses clocks
  138. */
  139. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  140. RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
  141. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  142. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  143. RCC_OscInitStruct.PLL.PLLM = 4;
  144. RCC_OscInitStruct.PLL.PLLN = 180;
  145. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  146. RCC_OscInitStruct.PLL.PLLQ = 2;
  147. RCC_OscInitStruct.PLL.PLLR = 2;
  148. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  149. {
  150. Error_Handler();
  151. }
  152.  
  153. /**Activate the Over-Drive mode
  154. */
  155. if (HAL_PWREx_EnableOverDrive() != HAL_OK)
  156. {
  157. Error_Handler();
  158. }
  159.  
  160. /**Initializes the CPU, AHB and APB busses clocks
  161. */
  162. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  163. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  164. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  165. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  166. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  167. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  168.  
  169. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  170. {
  171. Error_Handler();
  172. }
  173.  
  174. /**Configure the Systick interrupt time
  175. */
  176. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  177.  
  178. /**Configure the Systick
  179. */
  180. HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  181.  
  182. /* SysTick_IRQn interrupt configuration */
  183. HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  184. }
  185.  
  186. /* USER CODE BEGIN 4 */
  187.  
  188. /* USER CODE END 4 */
  189.  
  190. /**
  191. * @brief This function is executed in case of error occurrence.
  192. * @param None
  193. * @retval None
  194. */
  195. void Error_Handler(void)
  196. {
  197. /* USER CODE BEGIN Error_Handler */
  198. /* User can add his own implementation to report the HAL error return state */
  199. while(1)
  200. {
  201. }
  202. /* USER CODE END Error_Handler */
  203. }
  204.  
  205. #ifdef USE_FULL_ASSERT
  206.  
  207. /**
  208. * @brief Reports the name of the source file and the source line number
  209. * where the assert_param error has occurred.
  210. * @param file: pointer to the source file name
  211. * @param line: assert_param error line source number
  212. * @retval None
  213. */
  214. void assert_failed(uint8_t* file, uint32_t line)
  215. {
  216. /* USER CODE BEGIN 6 */
  217. /* User can add his own implementation to report the file name and line number,
  218. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  219. /* USER CODE END 6 */
  220.  
  221. }
  222.  
  223. #endif
  224.  
  225. /**
  226. * @}
  227. */
  228.  
  229. /**
  230. * @}
  231. */
  232.  
  233. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement