/** ****************************************************************************** * @file GPIO/GPIO_EXTI/Src/main.c * @author MCD Application Team * @brief This example describes how to configure and use GPIOs through * the STM32F7xx HAL API. ****************************************************************************** * @attention * * Copyright (c) 2016 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** */ /* Includes ------------------------------------------------------------------*/ #include "main.h" /** @addtogroup STM32F7xx_HAL_Examples * @{ */ /** @addtogroup GPIO_EXTI * @{ */ /* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ /* Private macro -------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ /* Task table. In our simple scheduler, the task table contains the fixed number of tasks Pa3cio Bulic, 26.10.2021 */ task_table_t task_table[MAX_TASKS]; /* Task stacks. In our simple scheduler, each task has 1KB stack. The stacks are put in a linear RAM region of size MAX_TASKS x 1KB. Pa3cio Bulic, 26.10.2021 */ uint32_t tasks_stack_region[MAX_TASKS * TASKS_STACK_SIZE]; // this is the address where all tasks stacks starts: void *pTasksStackBase = (void *)(&tasks_stack_region[MAX_TASKS * TASKS_STACK_SIZE]); int current_task = -1; /* Private function prototypes -----------------------------------------------*/ static void MPU_Config(void); static void SystemClock_Config(void); static void EXTI0_IRQHandler_Config(void); static void CPU_CACHE_Enable(void); /* Private functions ---------------------------------------------------------*/ /* The function reads PSP and returns its value in result. This function should be defined into the same translational module in which is used as it is defined as static inline! Pa3cio Bulic, 26.10.2021 */ static __attribute__((always_inline)) void* read_process_stack_pointer(void) { void* result; /* read special register PSP into a general-purpose register (picked by compiler) and write it to result: */ asm volatile ( "MRS %0, PSP\n\t" : "=r" (result) ); return (result); } /* The function writes the ptr to PSP. This function should be defined into the same translational module in which is used as it is defined as static inline! Pa3cio Bulic, 26.10.2021 */ static __attribute__((always_inline)) void write_process_stack_pointer(void* ptr) { asm volatile ( "MSR PSP, %0\n\t" : : "r" (ptr) ); } /* The is the context switcher. 1. save psp to sp of the current taks (in task table entry) 2. select new task in a round-robin fashion (cyclic increment of the task index) 3. write the sp of the new task into psp 4. that's all :) Pa3cio Bulic, 26.10.2021 */ void switch_context (void) { if (current_task != -1) { task_table[current_task].sp = read_process_stack_pointer(); } // select a new task in a round-robin fashion: current_task++; if (current_task == MAX_TASKS) { current_task = 0; } // select a new psp: write_process_stack_pointer( task_table[current_task].sp ); } /** * @brief Main program * @param None * @retval None */ int main(void) { /* Configure the MPU attributes */ MPU_Config(); /* Enable the CPU Cache */ CPU_CACHE_Enable(); /* STM32F7xx HAL library initialization: - Configure the Flash prefetch - Systick timer is configured by default as source of time base, but user can eventually implement his proper time base source (a general purpose timer for example or other time source), keeping in mind that Time base duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and handled in milliseconds basis. - Set NVIC Group Priority to 4 - Low Level Initialization */ HAL_Init(); /* Configure the system clock to 216 MHz */ SystemClock_Config(); /* -1- Initialize LED2 */ BSP_LED_Init(LED2); /* -2- Configure EXTI0 (connected to PA.00 pin) in interrupt mode */ EXTI0_IRQHandler_Config(); init_tasks(); /* Infinite loop */ while (1) { } } /* The function creates a new task. Creating a new task involves: 1. making space for the task's stake. Each task gets 4kB for the stack 2. populating the initial HW stack frame for the task. HW stack frame initially resides at the bottom of the task's stack. The HW stack frame is populated as follows: PSR = 0x21000000 - this is the default value in the state register PC = address of the task LR = the address of the delete_task() function (if needed). r12, r3-r0 = 0x00000000 - we may also pass the arguments into the tas via r0-r3, but this is not the case in our simple RTOS 3. makes room for the initial uninitialized SW stack frame. 4. sets the tasks stack pointer in the task table to point at the top of the SW stack frame After this steps a new task is ready to be executed for the first time when task switch occurs and the task is selected for execution. Pa3cio Bulic, 26.10.2021 */ void create_task (int task_id, void* task_stack_base_address, void (*pTask)(void)) { hw_stack_frame_t* ptask_hw_frame; /* set the start address of the HW frame structure */ ptask_hw_frame = (hw_stack_frame_t *) ((uint8_t *)task_stack_base_address - sizeof(hw_stack_frame_t)); /* populate the HW stack frame with initial values : */ /* NOTE: HW stack for Task0 (main()) is ignored as main() uses MAIN STACK */ ptask_hw_frame -> r0 = 0; ptask_hw_frame -> r1 = 0; ptask_hw_frame -> r2 = 0; ptask_hw_frame -> r3 = 0; ptask_hw_frame -> r12 = 0; ptask_hw_frame -> lr = 0; // in our simple RTOS the tasks never finish so there is no need to delete the task ptask_hw_frame -> pc = (uint32_t)pTask; ptask_hw_frame -> psr = 0x21000000; // make room for SW stack frame and set the task's stack pointer: /* task_table[task_id].sp = (void *)( (uint8_t *)task_stack_base_address - sizeof(hw_stack_frame_t) ); */ task_table[task_id].sp = (void *) ptask_hw_frame; //- sizeof(sw_stack_frame_t) ); } /* The function initializes and creates all tasks in the RTOS. Our RTOS is very simple and has fixed number of tasks that are created at the start of RTOS. The tasks never stop and the number of the running task never changes. Pa3cio Bulic, 26.10.2021 */ void init_tasks (void) { int i; void* task_stack_base_address; /* Task schedule sets tasks in the schedule: */ task_table[0].pTask = &Opravilo0; task_table[1].pTask = &Opravilo1; task_table[2].pTask = &Opravilo0; task_table[3].pTask = &Opravilo3; /* Create tasks....*/ for (i = 0; i < MAX_TASKS; i++) { task_stack_base_address = (uint8_t *)pTasksStackBase - i*sizeof(uint32_t)*TASKS_STACK_SIZE; create_task (i, task_stack_base_address, task_table[i].pTask); } /* PSP has not been set until now, so set PSP to point to the top of stack of the first interrupted task (Task 0): */ write_process_stack_pointer(task_table[0].sp); } // Opravila: void Opravilo0 (void) { while(1) { } } void Opravilo1 (void) { while(1) { } } void Opravilo2 (void) { while(1) { } } void Opravilo3 (void) { while(1) { } } /** * @brief System Clock Configuration * The system Clock is configured as follow : * System Clock source = PLL (HSE) * SYSCLK(Hz) = 216000000 * HCLK(Hz) = 216000000 * AHB Prescaler = 1 * APB1 Prescaler = 4 * APB2 Prescaler = 2 * HSE Frequency(Hz) = 25000000 * PLL_M = 25 * PLL_N = 432 * PLL_P = 2 * PLL_Q = 9 * PLL_R = 7 * VDD(V) = 3.3 * Main regulator output voltage = Scale1 mode * Flash Latency(WS) = 7 * @param None * @retval None */ static void SystemClock_Config(void) { RCC_ClkInitTypeDef RCC_ClkInitStruct; RCC_OscInitTypeDef RCC_OscInitStruct; HAL_StatusTypeDef ret = HAL_OK; /* Enable Power Control clock */ __HAL_RCC_PWR_CLK_ENABLE(); /* The voltage scaling allows optimizing the power consumption when the device is clocked below the maximum system frequency, to update the voltage scaling value regarding system frequency refer to product datasheet. */ __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); /* Enable HSE Oscillator and activate PLL with HSE as source */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLM = 25; RCC_OscInitStruct.PLL.PLLN = 432; RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; RCC_OscInitStruct.PLL.PLLQ = 9; RCC_OscInitStruct.PLL.PLLR = 7; ret = HAL_RCC_OscConfig(&RCC_OscInitStruct); if(ret != HAL_OK) { while(1) { ; } } /* Activate the OverDrive to reach the 216 MHz Frequency */ ret = HAL_PWREx_EnableOverDrive(); if(ret != HAL_OK) { while(1) { ; } } /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2 clocks dividers */ RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2); RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; ret = HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_7); if(ret != HAL_OK) { while(1) { ; } } } /** * @brief Configures EXTI line 0 (connected to PA.00 pin) in interrupt mode * @param None * @retval None */ static void EXTI0_IRQHandler_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; /* Enable GPIOC clock */ __HAL_RCC_GPIOA_CLK_ENABLE(); /* Configure PC.13 pin as input floating */ GPIO_InitStructure.Mode = GPIO_MODE_IT_RISING; GPIO_InitStructure.Pull = GPIO_NOPULL; GPIO_InitStructure.Pin = GPIO_PIN_0; HAL_GPIO_Init(GPIOA, &GPIO_InitStructure); /* Enable and set EXTI line 0 Interrupt to the lowest priority */ HAL_NVIC_SetPriority(EXTI0_IRQn, 2, 0); HAL_NVIC_EnableIRQ(EXTI0_IRQn); } /** * @brief EXTI line detection callbacks * @param GPIO_Pin: Specifies the pins connected EXTI line * @retval None */ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { if (GPIO_Pin == GPIO_PIN_0) { /* Toggle LED2 */ BSP_LED_Toggle(LED2); } } /** * @brief CPU L1-Cache enable. * @param None * @retval None */ static void CPU_CACHE_Enable(void) { /* Enable I-Cache */ SCB_EnableICache(); /* Enable D-Cache */ SCB_EnableDCache(); } /** * @brief Configure the MPU attributes * @param None * @retval None */ static void MPU_Config(void) { MPU_Region_InitTypeDef MPU_InitStruct; /* Disable the MPU */ HAL_MPU_Disable(); /* Configure the MPU as Strongly ordered for not defined regions */ MPU_InitStruct.Enable = MPU_REGION_ENABLE; MPU_InitStruct.BaseAddress = 0x00; MPU_InitStruct.Size = MPU_REGION_SIZE_4GB; MPU_InitStruct.AccessPermission = MPU_REGION_NO_ACCESS; MPU_InitStruct.IsBufferable = MPU_ACCESS_NOT_BUFFERABLE; MPU_InitStruct.IsCacheable = MPU_ACCESS_NOT_CACHEABLE; MPU_InitStruct.IsShareable = MPU_ACCESS_SHAREABLE; MPU_InitStruct.Number = MPU_REGION_NUMBER0; MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL0; MPU_InitStruct.SubRegionDisable = 0x87; MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_DISABLE; HAL_MPU_ConfigRegion(&MPU_InitStruct); /* Enable the MPU */ HAL_MPU_Enable(MPU_PRIVILEGED_DEFAULT); } #ifdef USE_FULL_ASSERT /** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @param file: pointer to the source file name * @param line: assert_param error line source number * @retval None */ void assert_failed(uint8_t *file, uint32_t line) { /* User can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* Infinite loop */ while (1) { } } #endif /** * @} */ /** * @} */