Advertisement
Guest User

Untitled

a guest
Apr 4th, 2020
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.50 KB | None | 0 0
  1. #include "stm32f4xx.h"                  // Device header
  2. #include "FreeRTOS.h"                   // ARM.FreeRTOS::RTOS:Core
  3. #include "FreeRTOSConfig.h"             // ARM.FreeRTOS::RTOS:Config
  4. #include "Task.h"
  5. #include "queue.h"
  6. #include "semphr.h"
  7. //MAP 0x40000000, 0x400FFFFF EXEC READ WRITE
  8.  
  9. SemaphoreHandle_t A5_Semaphore = NULL;
  10. TickType_t WakeTime1 = NULL, WakeTime2 = NULL;
  11. TaskHandle_t Task1Handle = NULL, Task2Handle = NULL;
  12.  
  13. void vTask1(void* pvParams)
  14. {
  15.     TickType_t WakeTime1 = xTaskGetTickCount();
  16.     while(1)
  17.     {
  18.         if( A5_Semaphore != NULL){
  19.          //If the semaphore exists
  20.             if( ( xSemaphoreTake( A5_Semaphore, 100 ) == pdTRUE )
  21.                 || ( xSemaphoreGetMutexHolder( A5_Semaphore ) == Task1Handle ) )
  22.             { //If the semaphore was succesfully taken or Task1 already has it
  23.                 for( char i=0; i<4; i++ ){
  24.                     GPIOA->ODR |= GPIO_ODR_ODR_5;
  25.                     vTaskDelay(pdMS_TO_TICKS(500));
  26.                     GPIOA->ODR &= ~GPIO_ODR_ODR_5;
  27.                     vTaskDelay(pdMS_TO_TICKS(500));
  28.                 }
  29.                 xSemaphoreGive(A5_Semaphore);
  30.                 vTaskDelayUntil(&WakeTime1, pdMS_TO_TICKS(4000));
  31.             }
  32.             else taskYIELD();
  33.         }
  34.     }
  35. }
  36.  
  37.  
  38. void vTask2(void* pvParams){
  39.     TickType_t WakeTime2 = xTaskGetTickCount();
  40.     while(1){
  41.         if( A5_Semaphore != NULL ){
  42.              //If the semaphore exists
  43.             if( ( xSemaphoreTake( A5_Semaphore, 100 ) == pdTRUE )
  44.                 || ( xSemaphoreGetMutexHolder( A5_Semaphore ) == Task2Handle ) )
  45.             { //If the semaphore was succesfully taken or Task2 already has it
  46.                 for( char i=0; i<3; i++ ){
  47.                     GPIOA->ODR |= GPIO_ODR_ODR_6;
  48.                     vTaskDelay(pdMS_TO_TICKS(133));
  49.                     GPIOA->ODR &= ~GPIO_ODR_ODR_6;
  50.                     vTaskDelay(pdMS_TO_TICKS(133));
  51.                 }
  52.                 for( char i=0; i<3; i++ ){
  53.                     GPIOA->ODR |= GPIO_ODR_ODR_6;
  54.                     vTaskDelay(pdMS_TO_TICKS(334));
  55.                     GPIOA->ODR &= ~GPIO_ODR_ODR_6;
  56.                     vTaskDelay(pdMS_TO_TICKS(334));
  57.                 }
  58.                 for( char i=0; i<3; i++ ){
  59.                     GPIOA->ODR |= GPIO_ODR_ODR_6;
  60.                     vTaskDelay(pdMS_TO_TICKS(133));
  61.                     GPIOA->ODR &= ~GPIO_ODR_ODR_6;
  62.                     vTaskDelay(pdMS_TO_TICKS(133));
  63.                 }
  64.                 xSemaphoreGive(A5_Semaphore);
  65.                 vTaskDelayUntil(&WakeTime2, pdMS_TO_TICKS(4000));
  66.             }
  67.             else taskYIELD();
  68.         }
  69.     }
  70. }
  71.  
  72.  
  73. int main(void){
  74.     RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
  75.     //pin A5
  76.     GPIOA->MODER |= GPIO_MODER_MODER5_0 | GPIO_MODER_MODER6_0;
  77.  
  78.     xTaskCreate(vTask2, "DiodeSOS", configMINIMAL_STACK_SIZE, NULL, 1, &Task2Handle);
  79.     xTaskCreate(vTask1, "DiodeBlink", configMINIMAL_STACK_SIZE, NULL, 1, &Task1Handle);
  80.     A5_Semaphore = xSemaphoreCreateMutex();
  81.     vTaskStartScheduler();
  82.     while(1){
  83.  
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement