Skip to content

Instantly share code, notes, and snippets.

@Scott31393
Created November 7, 2020 16:51
Show Gist options
  • Save Scott31393/8713d11b83dc3d95a6cbf767ffe09b0a to your computer and use it in GitHub Desktop.
Save Scott31393/8713d11b83dc3d95a6cbf767ffe09b0a to your computer and use it in GitHub Desktop.

Event Driven Mainloop example

static QueueHandle_t xMainQueue = NULL;

typedef struct 
{
   char id;
   unsigned int arg;
} message_t;

/*
 * Subcommands used for kMcuLinkCmd_RawSensorData
 */
typedef enum main_events
{
    kMain_StartNETWORK = 0,
    kMain_StartUSBCDC,
    kMain_StartCLI,
} main_events_t;

typedef enum main_net_type
{
    kMain_DYNAMIC_IP = 0,
    kMain_STATIC_IP,
} main_net_type_t;

static void main_equeue_msg(message_t msg2send)
{
    xQueueSend(xMainQueue, ( void * ) &msg2send, pdMS_TO_TICKS(100) );
}

void main_send_evt(main_events_t main_event)
{
    message_t msg2send;
    msg2send.id = main_event;
    msg2send.arg = 0;

    main_equeue_msg(msg2send);
}

void main_send_evt_with_arg(main_events_t main_event, unsigned int arg)
{
    message_t msg2send;
    msg2send.id = main_event;
    msg2send.arg = arg;

    main_equeue_msg(msg2send);
}

void main_network_dhcp_task(void *pvParameters)
{
    if (SYSTEM_Init() == pdPASS) {
        if (network_dhcp_init() != 0) {
            PRINTF("Network init FAIL!\r\n");
        }
        else {
            PRINTF("Network init OKAY!\r\n");
        }
    }
    vTaskDelete(NULL);
}

static void main_start_network(int mode)
{
    if(mode == kMain_DYNAMIC_IP)
        xTaskCreate(main_network_dhcp_task, "main_network_dhcp_task", configMINIMAL_STACK_SIZE * 8, NULL, tskIDLE_PRIORITY + 6, NULL);
    else if(mode == kMain_STATIC_IP)
        network_static_ip_init();
    else
        PRINTF("Network type not valid\r\n");
}

void main_task(void *pvParameters)
{
    // Main queue initialization
    xMainQueue = xQueueCreate(10, sizeof(message_t));
    message_t rx_msg;

    main_send_evt(kMain_StartUSBCDC);

    while (true)
    {
        memset(&rx_msg, 0, sizeof(message_t));

        /* Receive a message from the created queue to hold complex struct AMessage
        structure.  Block for 10 ticks if a message is not immediately available.
        The value is read into a struct AMessage variable, so after calling
        xQueueReceive() xRxedStructure will hold a copy of xMessage. */
        if(xQueueReceive(xMainQueue, &rx_msg, pdMS_TO_TICKS(200)) == pdPASS)
        {
            switch (rx_msg.id)
            {
            case kMain_StartUSBCDC:
                usb_cdc_qb95_init();
                main_send_evt_with_arg(kMain_StartNETWORK, kMain_STATIC_IP);
                break;

            case kMain_StartNETWORK:
                main_start_network(rx_msg.arg);
                main_send_evt(kMain_StartCLI);
                break;
            
            case kMain_StartCLI:
                cli_init();
                break;

            default:
                break;
            }
        }
    }
}

int main(void)
{
   gpio_pin_config_t gpio_config = {kGPIO_DigitalOutput, 0, kGPIO_NoIntmode};

   BOARD_ConfigMPU();
   BOARD_InitBootPins();
   BOARD_InitBootClocks();
   BOARD_I2C_ConfigurePins();
   BOARD_InitDebugConsole();
   BOARD_InitModuleClock();

   IOMUXC_EnableMode(IOMUXC_GPR, kIOMUXC_GPR_ENET1TxClkOutputDir, true);

   GPIO_PinInit(GPIO1, 9, &gpio_config);
   GPIO_PinInit(GPIO1, 10, &gpio_config);
   /* pull up the ENET_INT before RESET. */
   GPIO_WritePinOutput(GPIO1, 10, 1);
   GPIO_WritePinOutput(GPIO1, 9, 0);
   delay();
   GPIO_WritePinOutput(GPIO1, 9, 1);

   xTaskCreate(main_task, "main_task", configMINIMAL_STACK_SIZE * 8, NULL, tskIDLE_PRIORITY + 5, NULL);
   
   /* start scheduler */
   vTaskStartScheduler();
   for(;;);
   
   /* Will not get here unless a task calls vTaskEndScheduler ()*/
   return 0;
}

void *pvPortCalloc(size_t xNum, size_t xSize)
{
   void *pvReturn;

   pvReturn = pvPortMalloc(xNum * xSize);
   if (pvReturn != NULL)
   {
       memset(pvReturn, 0x00, xNum * xSize);
   }

   return pvReturn;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment