Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* ========================================
- *
- * ELECH410 labs
- * FreeRTOS pandemic project
- *
- * 2020
- *
- * ========================================
- */
- #include "project.h"
- /* RTOS includes. */
- #include "FreeRTOS.h"
- #include "task.h"
- #include "timers.h"
- #include "queue.h"
- #include "semphr.h"
- #include "pandemic.h"
- #include "stdbool.h"
- #define TASK_STACK_SIZE (1024)
- /* Task definitions */
- #define GAME_TASK_NAME ("game_task")
- #define LCD_TASK_NAME ("lcd_update")
- #define MAKE_VACCINE_TASK_NAME ("release_clue")
- #define MAKE_MEDICINE_TASK_NAME ("release_clue")
- #define APPLY_QUARANTINE_TASK_NAME ("apply_quarantine")
- #define GAME_PRIORITY (20)
- #define LCD_PRIORITY (18)
- #define MAKE_VACCINE_PRIORITY (17)
- #define MAKE_MEDICINE_PRIORITY (10)
- #define APPLY_QUARANTINE_PRIORITY (19)
- #define LCD_DELAY (33u)//30fps
- /* Task handlers */
- TaskHandle_t gameHandler;
- TaskHandle_t lcdHandler;
- TaskHandle_t releaseClueHandler;
- TaskHandle_t makeMedicineHandler;
- TaskHandle_t quarantineHandler;
- //task prototypes
- void lcdUpdate(void *);
- void applyQuarantine(void *);
- void makeMedicine(void *);
- void makeVaccine(void *);
- void clueExpired();
- /* Mutex */
- SemaphoreHandle_t labMutex;
- SemaphoreHandle_t applyQuarantineMutex;
- //queue
- QueueHandle_t clueQueue;
- //timer
- TimerHandle_t clueTimerHandler;
- bool flagClueExpired = false;
- /*
- * Installs the RTOS interrupt handlers.
- */
- static void freeRTOSInit( void );
- int main(void)
- {
- /* Enable global interrupts. */
- CyGlobalIntEnable;
- freeRTOSInit();
- /* Place your initialization/startup code here (e.g. MyInst_Start()) */
- LCD_Start();
- KB_Start();
- // Create tasks
- xTaskCreate( gameTask, GAME_TASK_NAME, TASK_STACK_SIZE, NULL, GAME_PRIORITY, &gameHandler );
- xTaskCreate( lcdUpdate, LCD_TASK_NAME, TASK_STACK_SIZE, NULL, LCD_PRIORITY, &lcdHandler );
- xTaskCreate( makeVaccine, MAKE_VACCINE_TASK_NAME, TASK_STACK_SIZE, NULL, MAKE_VACCINE_PRIORITY, &releaseClueHandler );
- xTaskCreate( makeMedicine, MAKE_MEDICINE_TASK_NAME, TASK_STACK_SIZE, NULL, MAKE_MEDICINE_PRIORITY, &makeMedicineHandler );
- xTaskCreate( applyQuarantine, APPLY_QUARANTINE_TASK_NAME , TASK_STACK_SIZE, NULL, APPLY_QUARANTINE_PRIORITY, &quarantineHandler );
- //mutex & semaphores
- labMutex = xSemaphoreCreateMutex(); //mutex -> starts at 1 and priority inheritance
- applyQuarantineMutex = xSemaphoreCreateBinary(); //semaphore -> starts at 0
- //create queue
- clueQueue = xQueueCreate( 1, sizeof( uint8_t ) );
- //timer create for clue expiration
- clueTimerHandler = xTimerCreate(
- "clue",
- pdMS_TO_TICKS( 450 ), //400 ms until deadline
- pdFALSE,
- NULL,
- clueExpired
- );
- // Launch freeRTOS
- vTaskStartScheduler();
- for(;;)
- {
- }
- }
- void freeRTOSInit( void )
- {
- /* Port layer functions that need to be copied into the vector table. */
- extern void xPortPendSVHandler( void );
- extern void xPortSysTickHandler( void );
- extern void vPortSVCHandler( void );
- extern cyisraddress CyRamVectors[];
- /* Install the OS Interrupt Handlers. */
- CyRamVectors[ 11 ] = ( cyisraddress ) vPortSVCHandler;
- CyRamVectors[ 14 ] = ( cyisraddress ) xPortPendSVHandler;
- CyRamVectors[ 15 ] = ( cyisraddress ) xPortSysTickHandler;
- }
- /*
- * When a contamination occurs gameTask calls this function.
- *
- */
- void releaseContamination( void ){
- xSemaphoreGive(applyQuarantineMutex);//release the mutex to trigger the quarantine
- }
- //high priority task, waiting on mutex release from releaseContamination
- void applyQuarantine(void *arg){
- (void) arg;
- for(;;){
- xSemaphoreTake(applyQuarantineMutex, portMAX_DELAY ); //waits to trigger quarantine
- quarantine();
- }
- }
- /*
- * When gameTask releases a vaccine clue it calls this function.
- *
- */
- //called by gameTask whenever a clue is released. Release the mutex for the clueto launch vaccine creation and start
- //...timer to check clue expiration
- void releaseClue( Token clue ){
- flagClueExpired = false;
- xTimerReset(clueTimerHandler, 0u); //starts the timer for clue expiration
- xQueueSend( clueQueue, (void *) &clue, portMAX_DELAY ); //sends clue to makeVacine
- }
- //Called by timer deadline, sets the flag to true
- void clueExpired(){
- flagClueExpired = true;
- }
- //create vaccine by using the lab then giving it back and shipping the vaccine.
- //...Checks that enough time is left, otherwise skip this clue and continue producing pills
- void makeVaccine(void *arg){
- (void) arg;
- Token clueReceived;
- Token result;
- for(;;){
- xQueueReceive( clueQueue, (void *) &clueReceived, portMAX_DELAY ); //waits to receive a clue
- xSemaphoreTake(labMutex, portMAX_DELAY); //takes lab resource
- if(flagClueExpired){ //if lab was taken for too long and not enough time left before clue expiration
- flagClueExpired = false;
- xSemaphoreGive(labMutex); //give lab back
- LCD_ClearDisplay();
- LCD_Position(1,0);
- LCD_PrintString("skip");
- }
- else{
- result = assignMissionToLab(clueReceived);
- xSemaphoreGive(labMutex); //lab can be already given since it should not be locked for shipping
- shipVaccine(result); //no need for lab
- }
- }
- }
- //makes medicine by taking the lab resource, lower priority than vaccine
- void makeMedicine(void *arg){
- //background taks making medicine when not making vaccine
- (void) arg;
- Token result;
- for(;;){
- xSemaphoreTake(labMutex, portMAX_DELAY);
- result = assignMissionToLab(0);
- xSemaphoreGive(labMutex);//lab can be already given since it should not be locked for shipping
- shipMedicine(result); //no need for lab resource for this
- vTaskDelay(10u);
- }
- }
- //updates the lcd screen (high priority)
- void lcdUpdate(void *arg){
- (void) arg;
- for(;;){
- LCD_Position(0,0);
- LCD_PrintString(" "); //used to clear left half of the screen
- LCD_Position(0,0);
- LCD_PrintNumber(getPopulationCntr());
- LCD_Position(0,3);
- LCD_PrintNumber(getMedicineCntr());
- LCD_Position(0,5);
- LCD_PrintNumber(getVaccineCntr());
- vTaskDelay(LCD_DELAY);
- }
- }
- /* [] END OF FILE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement