Advertisement
Guest User

Untitled

a guest
Oct 30th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.21 KB | None | 0 0
  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) 2019 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.  
  27. /* USER CODE END Includes */
  28.  
  29. /* Private typedef -----------------------------------------------------------*/
  30.  
  31. /* USER CODE BEGIN PTD */
  32.  
  33. // a struct that defines a GPIO device (maps GPIOs memory structure)
  34. typedef struct
  35. {
  36.     uint32_t MODER;
  37.     uint32_t OTYPER;
  38.     uint32_t OSPEEDR;
  39.     uint32_t PUPDR;
  40.     uint32_t IDR;
  41.     uint32_t ODR;
  42.     uint16_t BSR;
  43.     uint16_t BRR;
  44. } GPIO_device;
  45.  
  46. /* USER CODE END PTD */
  47.  
  48. /* Private define ------------------------------------------------------------*/
  49.  
  50. /* USER CODE BEGIN PD */
  51.  
  52. // constants for storing addresses of GPIOs
  53. #define GPIOAd ((GPIO_device*)0x40020000)
  54. #define GPIOBd ((GPIO_device*)0x40020400)
  55. #define GPIOCd ((GPIO_device*)0x40020800)
  56. #define GPIODd ((GPIO_device*)0x40020C00)
  57. #define GPIOEd ((GPIO_device*)0x40021000)
  58. #define GPIOFd ((GPIO_device*)0x40021400)
  59. #define GPIOGd ((GPIO_device*)0x40021800)
  60. #define GPIOHd ((GPIO_device*)0x40021C00)
  61. #define GPIOId ((GPIO_device*)0x40022000)
  62. #define RCC_AHB1ENR ((uint32_t*)0x40023830)
  63.  
  64. // MODE constants
  65. #define IN 0b00
  66. #define OUT 0b01
  67. #define AF 0b11
  68. #define ANALOG 0b10
  69.  
  70. // PUPD constants
  71. #define NO_PULL 0b00
  72. #define PULL_UP 0b01
  73. #define PULL_DOWN 0b10
  74.  
  75. // OTYPE constants
  76. #define PUSH_PULL 0b0
  77. #define OPEN_DRAIN 0b1
  78.  
  79. // OSPEED constants
  80. #define S2MHz 0b00
  81. #define S25MHz 0b01
  82. #define S50MHz 0b10
  83. #define S100MHz 0b11
  84.  
  85. /* USER CODE END PD */
  86.  
  87. /* Private macro -------------------------------------------------------------*/
  88. /* USER CODE BEGIN PM */
  89.  
  90. /* USER CODE END PM */
  91.  
  92. /* Private variables ---------------------------------------------------------*/
  93. /* USER CODE BEGIN PV */
  94.  
  95. /* USER CODE END PV */
  96.  
  97. /* Private function prototypes -----------------------------------------------*/
  98. void SystemClock_Config(void);
  99.  
  100. /* USER CODE BEGIN PFP */
  101.  
  102. void clock_on(GPIO_device * GPIO_addr);
  103. void init_GPIO(GPIO_device * GPIO_addr, uint32_t Pin, uint32_t Mode, uint32_t PUPD, uint32_t OType, uint32_t OSpeed);
  104. void GPIO_pin_write(GPIO_device * GPIO_addr, uint32_t Pin, uint32_t val);
  105. uint32_t GPIO_pin_read(GPIO_device * GPIO_addr, uint32_t Pin);
  106. void delay(void);
  107.  
  108. /* USER CODE END PFP */
  109.  
  110. /* Private user code ---------------------------------------------------------*/
  111. /* USER CODE BEGIN 0 */
  112.  
  113. /* USER CODE END 0 */
  114.  
  115. /**
  116.     * @brief    The application entry point.
  117.     * @retval int
  118.     */
  119. int main(void)
  120. {
  121.     /* USER CODE BEGIN 1 */
  122.  
  123.     /* USER CODE END 1 */
  124.  
  125.  
  126.     /* MCU Configuration--------------------------------------------------------*/
  127.  
  128.     /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  129.     HAL_Init();
  130.  
  131.     /* USER CODE BEGIN Init */
  132.  
  133.     /* USER CODE END Init */
  134.  
  135.     /* Configure the system clock */
  136.     SystemClock_Config();
  137.  
  138.     /* USER CODE BEGIN SysInit */
  139.     /* USER CODE END SysInit */
  140.  
  141.     /* Initialize all configured peripherals */
  142.     /* USER CODE BEGIN 2 */
  143.  
  144.     // turn on button's GPIO (button is on PA0)
  145.  
  146.     clock_on(GPIOAd);
  147.     // turn on led's GPIO (leds are on PD12, PD13, PD14, PD15)
  148.     clock_on(GPIODd);
  149.  
  150.     // init button
  151.     init_GPIO(GPIOAd, 0, IN, NO_PULL, PUSH_PULL, S2MHz);
  152.     // init leds
  153.     init_GPIO(GPIODd, 12, OUT, NO_PULL, PUSH_PULL, S2MHz);
  154.     init_GPIO(GPIODd, 13, OUT, NO_PULL, PUSH_PULL, S2MHz);
  155.     init_GPIO(GPIODd, 14, OUT, NO_PULL, PUSH_PULL, S2MHz);
  156.     init_GPIO(GPIODd, 15, OUT, NO_PULL, PUSH_PULL, S2MHz);
  157.  
  158.     /* USER CODE END 2 */
  159.  
  160.     /* Infinite loop */
  161.     /* USER CODE BEGIN WHILE */
  162.     while (1)
  163.     {
  164.         // read button and trigger led sequence
  165.         /* USER CODE END WHILE */
  166.         if(GPIO_pin_read(GPIOAd,0) == 1){
  167.             GPIO_pin_write(GPIODd,12,1);
  168.             delay();
  169.             GPIO_pin_write(GPIODd,13,1);
  170.             delay();
  171.             GPIO_pin_write(GPIODd,14,1);
  172.             delay();
  173.             GPIO_pin_write(GPIODd,15,1);
  174.             delay();
  175.             GPIO_pin_write(GPIODd,15,0);
  176.             delay();
  177.             GPIO_pin_write(GPIODd,14,0);
  178.             delay();
  179.             GPIO_pin_write(GPIODd,13,0);
  180.             delay();
  181.             GPIO_pin_write(GPIODd,12,0);
  182.             delay();
  183.         }
  184.         /* USER CODE BEGIN 3 */
  185.     }
  186.     /* USER CODE END 3 */
  187. }
  188.  
  189. /**
  190.     * @brief System Clock Configuration
  191.     * @retval None
  192.     */
  193. void SystemClock_Config(void)
  194. {
  195.     RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  196.     RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  197.  
  198.     /** Configure the main internal regulator output voltage
  199.     */
  200.     __HAL_RCC_PWR_CLK_ENABLE();
  201.     __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  202.     /** Initializes the CPU, AHB and APB busses clocks
  203.     */
  204.     RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  205.     RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  206.     RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  207.     RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  208.     if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  209.     {
  210.         Error_Handler();
  211.     }
  212.     /** Initializes the CPU, AHB and APB busses clocks
  213.     */
  214.     RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  215.                                                             |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  216.     RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  217.     RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  218.     RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  219.     RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  220.  
  221.     if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  222.     {
  223.         Error_Handler();
  224.     }
  225. }
  226.  
  227. /* USER CODE BEGIN 4 */
  228.  
  229. // function for turning on a particular GPIO (enables the port clock)
  230. void clock_on(GPIO_device * GPIO_addr)
  231. {
  232.     uint32_t x = (uint32_t)GPIO_addr-(uint32_t)GPIOAd;//Castanje pointerjev obvezno!
  233.     uint32_t y = x/0x400;
  234.     *RCC_AHB1ENR = *RCC_AHB1ENR | (1 << y);
  235. }
  236.  
  237. // function for initializing GPIOs
  238. void init_GPIO(GPIO_device * GPIO_addr, uint32_t Pin, uint32_t Mode, uint32_t PUPD, uint32_t OType, uint32_t OSpeed)
  239. {
  240.     GPIO_addr->MODER = GPIO_addr->MODER & (~(1 << (Pin*2)));
  241.     GPIO_addr->MODER = GPIO_addr->MODER & (~(1 << (Pin*2+1)));
  242.     GPIO_addr->MODER = GPIO_addr->MODER | ((Mode << (Pin*2)));
  243.  
  244.     GPIO_addr->PUPDR = GPIO_addr->PUPDR & (~(1 << (Pin*2)));
  245.     GPIO_addr->PUPDR = GPIO_addr->PUPDR & (~(1 << (Pin*2+1)));
  246.     GPIO_addr->PUPDR = GPIO_addr->PUPDR | ((PUPD << (Pin*2)));
  247.  
  248.     GPIO_addr->OTYPER = GPIO_addr->OTYPER & (~(1 << (Pin)));
  249.     GPIO_addr->OTYPER = GPIO_addr->OTYPER | ((OType << (Pin)));
  250.  
  251.     GPIO_addr->OSPEEDR = GPIO_addr->OSPEEDR & (~(1 << (Pin*2)));
  252.     GPIO_addr->OSPEEDR = GPIO_addr->OSPEEDR & (~(1 << (Pin*2+1)));
  253.     GPIO_addr->OSPEEDR = GPIO_addr->OSPEEDR | ((OSpeed << (Pin*2)));
  254. }
  255.  
  256. // function for setting the value of an output GPIO pin
  257. void GPIO_pin_write(GPIO_device * GPIO_addr, uint32_t Pin, uint32_t val)
  258. {
  259.     if(val == 0){
  260.         GPIO_addr->BRR = GPIO_addr->BRR | ((uint16_t)(1 << Pin));
  261.     }
  262.     else if(val == 1){
  263.         GPIO_addr->BSR = GPIO_addr->BSR | ((uint16_t)(1 << Pin));
  264.     }
  265. }
  266.  
  267. // function for reading the value of an input GPIO pin
  268. uint32_t GPIO_pin_read(GPIO_device * GPIO_addr, uint32_t Pin)
  269. {
  270.     if((GPIO_addr->IDR & (1 << Pin)) != 0) return 1;
  271.     else return 0;
  272. }
  273.  
  274. // hardcoded delay
  275. void delay(void) {
  276.     volatile int d = 500000;
  277.     while(d--);
  278. }
  279.  
  280. /* USER CODE END 4 */
  281.  
  282. /**
  283.     * @brief    This function is executed in case of error occurrence.
  284.     * @retval None
  285.     */
  286. void Error_Handler(void)
  287. {
  288.     /* USER CODE BEGIN Error_Handler_Debug */
  289.     /* User can add his own implementation to report the HAL error return state */
  290.  
  291.     /* USER CODE END Error_Handler_Debug */
  292. }
  293.  
  294. #ifdef    USE_FULL_ASSERT
  295. /**
  296.     * @brief    Reports the name of the source file and the source line number
  297.     *                 where the assert_param error has occurred.
  298.     * @param    file: pointer to the source file name
  299.     * @param    line: assert_param error line source number
  300.     * @retval None
  301.     */
  302. void assert_failed(uint8_t *file, uint32_t line)
  303. {
  304.     /* USER CODE BEGIN 6 */
  305.     /* User can add his own implementation to report the file name and line number,
  306.          tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  307.     /* USER CODE END 6 */
  308. }
  309. #endif /* USE_FULL_ASSERT */
  310.  
  311. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement