Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.38 KB | None | 0 0
  1. /* ========================================
  2.  *
  3.  *              ELECH410 labs
  4.  *          FreeRTOS pandemic project
  5.  *
  6.  *                   2020
  7.  *
  8.  * ========================================
  9. */
  10. #include "project.h"
  11.  
  12. /* RTOS includes. */
  13. #include "FreeRTOS.h"
  14. #include "task.h"
  15. #include "timers.h"
  16. #include "queue.h"
  17. #include "semphr.h"
  18.  
  19. #include "pandemic.h"
  20.  
  21. #include "stdbool.h"
  22.  
  23. #define TASK_STACK_SIZE (1024)
  24.  
  25.  
  26. /* Task definitions */
  27. #define GAME_TASK_NAME ("game_task")
  28. #define LCD_TASK_NAME ("lcd_update")
  29. #define MAKE_VACCINE_TASK_NAME ("release_clue")
  30. #define MAKE_MEDICINE_TASK_NAME ("release_clue")
  31. #define APPLY_QUARANTINE_TASK_NAME ("apply_quarantine")
  32.  
  33.  
  34. #define GAME_PRIORITY (20)
  35. #define LCD_PRIORITY (18)
  36. #define MAKE_VACCINE_PRIORITY (17)
  37. #define MAKE_MEDICINE_PRIORITY (10)
  38. #define APPLY_QUARANTINE_PRIORITY (19)
  39. #define LCD_DELAY (33u)//30fps
  40.  
  41. /* Task handlers */
  42. TaskHandle_t gameHandler;
  43. TaskHandle_t lcdHandler;
  44. TaskHandle_t releaseClueHandler;
  45. TaskHandle_t makeMedicineHandler;
  46. TaskHandle_t quarantineHandler;
  47. //task prototypes
  48. void lcdUpdate(void *);
  49.  
  50. void applyQuarantine(void *);
  51. void makeMedicine(void *);
  52. void makeVaccine(void *);
  53. void clueExpired();
  54.  
  55. /* Mutex */
  56. SemaphoreHandle_t labMutex;
  57. SemaphoreHandle_t applyQuarantineMutex;
  58.  
  59. //queue
  60. QueueHandle_t clueQueue;
  61.  
  62. //timer
  63. TimerHandle_t clueTimerHandler;
  64. bool flagClueExpired = false;
  65.  
  66. /*
  67.  * Installs the RTOS interrupt handlers.
  68.  */
  69. static void freeRTOSInit( void );
  70.  
  71. int main(void)
  72. {
  73.     /* Enable global interrupts. */
  74.     CyGlobalIntEnable;
  75.    
  76.     freeRTOSInit();
  77.    
  78.     /* Place your initialization/startup code here (e.g. MyInst_Start()) */
  79.     LCD_Start();
  80.     KB_Start();
  81.    
  82.     // Create tasks
  83.     xTaskCreate( gameTask, GAME_TASK_NAME, TASK_STACK_SIZE, NULL, GAME_PRIORITY, &gameHandler );
  84.     xTaskCreate( lcdUpdate, LCD_TASK_NAME, TASK_STACK_SIZE, NULL, LCD_PRIORITY, &lcdHandler );
  85.     xTaskCreate( makeVaccine, MAKE_VACCINE_TASK_NAME, TASK_STACK_SIZE, NULL, MAKE_VACCINE_PRIORITY, &releaseClueHandler );
  86.     xTaskCreate( makeMedicine, MAKE_MEDICINE_TASK_NAME, TASK_STACK_SIZE, NULL, MAKE_MEDICINE_PRIORITY, &makeMedicineHandler );
  87.     xTaskCreate( applyQuarantine, APPLY_QUARANTINE_TASK_NAME , TASK_STACK_SIZE, NULL, APPLY_QUARANTINE_PRIORITY, &quarantineHandler );
  88.    
  89.     //mutex & semaphores
  90.     labMutex = xSemaphoreCreateMutex(); //mutex -> starts at 1 and priority inheritance
  91.     applyQuarantineMutex = xSemaphoreCreateBinary(); //semaphore -> starts at 0
  92.    
  93.     //create queue
  94.     clueQueue = xQueueCreate( 1, sizeof( uint8_t ) );
  95.    
  96.     //timer create for clue expiration
  97.     clueTimerHandler = xTimerCreate(
  98.     "clue",
  99.     pdMS_TO_TICKS( 450 ), //400 ms until deadline
  100.     pdFALSE,
  101.     NULL,
  102.     clueExpired
  103.     );
  104.    
  105.     // Launch freeRTOS
  106.     vTaskStartScheduler();    
  107.    
  108.     for(;;)
  109.     {
  110.     }
  111. }
  112.  
  113. void freeRTOSInit( void )
  114. {
  115.     /* Port layer functions that need to be copied into the vector table. */
  116.     extern void xPortPendSVHandler( void );
  117.     extern void xPortSysTickHandler( void );
  118.     extern void vPortSVCHandler( void );
  119.     extern cyisraddress CyRamVectors[];
  120.  
  121.     /* Install the OS Interrupt Handlers. */
  122.     CyRamVectors[ 11 ] = ( cyisraddress ) vPortSVCHandler;
  123.     CyRamVectors[ 14 ] = ( cyisraddress ) xPortPendSVHandler;
  124.     CyRamVectors[ 15 ] = ( cyisraddress ) xPortSysTickHandler;
  125. }
  126.  
  127. /*
  128.  * When a contamination occurs gameTask calls this function.
  129.  *
  130.  */
  131. void releaseContamination( void ){
  132.     xSemaphoreGive(applyQuarantineMutex);//release the mutex to trigger the quarantine
  133. }
  134.  
  135. //high priority task, waiting on mutex release from releaseContamination
  136. void applyQuarantine(void *arg){
  137.     (void) arg;
  138.     for(;;){
  139.         xSemaphoreTake(applyQuarantineMutex, portMAX_DELAY ); //waits to trigger quarantine
  140.         quarantine();
  141.     }
  142. }
  143.  
  144. /*
  145.  * When gameTask releases a vaccine clue it calls this function.
  146.  *
  147.  */
  148. //called by gameTask whenever a clue is released. Release the mutex for the clueto launch vaccine creation and start
  149. //...timer to check clue expiration
  150. void releaseClue( Token clue ){
  151.     flagClueExpired = false;
  152.     xTimerReset(clueTimerHandler, 0u); //starts the timer for clue expiration
  153.     xQueueSend( clueQueue, (void *) &clue, portMAX_DELAY ); //sends clue to makeVacine
  154. }
  155.  
  156. //Called by timer deadline, sets the flag to true
  157. void clueExpired(){
  158.     flagClueExpired = true;
  159. }
  160.  
  161. //create vaccine by using the lab then giving it back and shipping the vaccine.
  162. //...Checks that enough time is left, otherwise skip this clue and continue producing pills
  163. void makeVaccine(void *arg){
  164.     (void) arg;
  165.     Token clueReceived;
  166.     Token result;
  167.     for(;;){
  168.         xQueueReceive( clueQueue, (void *) &clueReceived, portMAX_DELAY ); //waits to receive a clue
  169.         xSemaphoreTake(labMutex, portMAX_DELAY); //takes lab resource
  170.         if(flagClueExpired){ //if lab was taken for too long and not enough time left before clue expiration
  171.             flagClueExpired = false;
  172.             xSemaphoreGive(labMutex); //give lab back
  173.             LCD_ClearDisplay();
  174.             LCD_Position(1,0);
  175.             LCD_PrintString("skip");
  176.         }
  177.         else{
  178.             result = assignMissionToLab(clueReceived);
  179.             xSemaphoreGive(labMutex); //lab can be already given since it should not be locked for shipping
  180.             shipVaccine(result); //no need for lab
  181.         }
  182.     }
  183. }
  184.  
  185. //makes medicine by taking the lab resource, lower priority than vaccine
  186. void makeMedicine(void *arg){
  187.        //background taks making medicine when not making vaccine
  188.     (void) arg;
  189.     Token result;
  190.     for(;;){
  191.         xSemaphoreTake(labMutex, portMAX_DELAY);
  192.         result = assignMissionToLab(0);
  193.         xSemaphoreGive(labMutex);//lab can be already given since it should not be locked for shipping
  194.         shipMedicine(result); //no need for lab resource for this
  195.         vTaskDelay(10u);
  196.     }
  197. }
  198.  
  199. //updates the lcd screen (high priority)
  200. void lcdUpdate(void *arg){
  201.     (void) arg;
  202.     for(;;){
  203.         LCD_Position(0,0);
  204.         LCD_PrintString("        "); //used to clear left half of the screen
  205.         LCD_Position(0,0);
  206.         LCD_PrintNumber(getPopulationCntr());
  207.         LCD_Position(0,3);
  208.         LCD_PrintNumber(getMedicineCntr());
  209.         LCD_Position(0,5);
  210.         LCD_PrintNumber(getVaccineCntr());
  211.         vTaskDelay(LCD_DELAY);
  212.     }
  213. }
  214.  
  215. /* [] END OF FILE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement