⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ucosii_demo.c

📁 含t h r e a d x,u c o s 的b s p
💻 C
📖 第 1 页 / 共 2 页
字号:
 *
 * Returns: Nothing
 *
 * Notes: None
 *
 **********************************************************************/
static void Task3(void *data)
{
    SWIM_WINDOW_T win;
    INT_16 xp[4], yp[4], xinc[4], yinc[4];
    INT_32 index;
    COLOR_T clrs[4];

    /* Create window for this task */
    swim_window_open(&win, xsz, ysz, fblog, xmin[3], ymin[3],
       xmax[3], ymax[3], 1, WHITE, BLACK, BLUE);

    /* Add a title bar */
    swim_set_title(&win, "Task #4", YELLOW);

    /* Set position of boxes to center of window */
    for (index = 0; index < 4; index++)
    {
        xp[index] = 12 + ((xmax[index] - xmin[index]) / 2) - (index * 6);
        yp[index] = 12 + ((ymax[index] - ymin[index]) / 2) - (index * 6);
    }

    /* Set incremental movement values */
    xinc[0] = xinc[1] = yinc[1] = yinc[2] = -1;
    xinc[2] = xinc[3] = yinc[0] = yinc[3] = 1;

    /* Set box colors */
    clrs[0] = RED;
    clrs[1] = GREEN;
    clrs[2] = BLUE;
    clrs[3] = YELLOW;

    while(1)
    {
        for (index = 0; index < 4; index++)
        {
            /* Remove old box first */
            swim_set_pen_color(&win, BLACK);
            swim_set_fill_color(&win, BLACK);
            swim_put_box(&win, xp[index], yp[index],
                (xp[index] + 6), (yp[index] + 6));

            /* Update box position */
            xp[index] = xp[index] + xinc[index];
            if ((xp[index] < 0) ||
                (xp[index] >= (swim_get_horizontal_size(&win) - 6)))
            {
                xinc[index] = -xinc[index];
                xp[index] = xp[index] + xinc[index];
            }

            yp[index] = yp[index] + yinc[index];
            if ((yp[index] < 0) ||
                (yp[index] >= (swim_get_vertical_size(&win) - 10)))
            {
                yinc[index] = -yinc[index];
                yp[index] = yp[index] + yinc[index];
            }

            /* Draw new box */
            swim_set_pen_color(&win, WHITE);
            swim_set_fill_color(&win, clrs[index]);
            swim_put_box(&win, xp[index], yp[index],
                (xp[index] + 6), (yp[index] + 6));
        }

        OSTimeDly(2);
    }

}

/***********************************************************************
 *
 * Function: TaskFirst
 *
 * Purpose: First uCos-II task
 *
 * Processing:
 *     Set the uCos time to 0. Enable timer to start timer interrupts.
 *     Call the OSStatInit() to start uCos statistics. Call the
 *     OSTaskCreate() function to start tassk 1 and 2. Open a SWIM
 *     window in the left side of the window. Add a title bar to the
 *     window. Enter the task loop and loop forever. In the task loop,
 *     increment iteration and clr. Set the new window pen color based
 *     on clr. Output a string to the window with the iteration counter
 *     value.
 *
 * Parameters:
 *     data: Task input data value, not used
 *
 * Outputs: None
 *
 * Returns: Nothing
 *
 * Notes: None
 *
 **********************************************************************/
static void TaskFirst(void *data)
{
    SWIM_WINDOW_T win;
    CHAR str[32];
    const CHAR it_str[] = "Iteration #";
    COLOR_T clr = BLACK;
    INT_32 iteration = 0;
#if OS_CRITICAL_METHOD == 3 /* Storage for CPU status register */
    OS_CPU_SR  cpu_sr;
#endif

    OSTimeSet(0);

    OS_ENTER_CRITICAL();
    /* Start timer 1 */
    abl_ioctl(timer1dev, TIMER_ENABLE, 1);
    OS_EXIT_CRITICAL();

    OSStatInit();

    /* Create task #2 with priority 4 */
    OSTaskCreate(Task1, (void *) 0, &TaskStk[1][TASK_STK_SIZE - 1], 4);

    /* Create task #3 with priority 2 */
    OSTaskCreate(Task2, (void *) 0, &TaskStk[2][TASK_STK_SIZE - 1], 2);

    /* Create task #4 with priority 8 */
    OSTaskCreate(Task3, (void *) 0, &TaskStk[3][TASK_STK_SIZE - 1], 8);

    /* Open a window on the left side of the display */
    swim_window_open(&win, xsz, ysz, fblog, xmin[0], ymin[0],
       xmax[0], ymax[0], 1, WHITE, BLACK, DARKGRAY);

    /* Add a title bar on the window */
    swim_set_title(&win, "Task #1", LIGHTGRAY);

    /* Thread task loop */
    while(1)
    {
        /* Increment iteration counter */
        iteration++;

        /* Use a new pen color for the next iteration message */
        clr = clr + 0x2;
        swim_set_pen_color(&win, clr);

        /* Output the next iteration message in the window */
        swim_put_text(&win, it_str);
        make_number_str(str, iteration);
        swim_put_text(&win, str);
        swim_put_text(&win, "\n");
    }
}

/***********************************************************************
 *
 * Function: c_entry
 *
 * Purpose: Main entry point for image - transfers from startup code
 *
 * Processing:
 *     See function.
 *
 * Parameters: None
 *
 * Outputs: None
 *
 * Returns: Nothing
 *
 * Notes: None
 *
 **********************************************************************/
void c_entry()
{
    INT_32 regionsize;
    
    /* Set virtual address of MMU table (needed for VIC driver
       functions) */
    cp15_set_vmmu_addr((UNS_32 *) cp15_get_ttb());

    /* Initialize the IO system */
    abl_api_init((UNS_32 *) NULL);

    /* Register LCD, timer, and UART drivers in API - instead of
       registering, the direct driver functions can also be called
       directly. */
    abl_api_register((INT_32) CLCDC, (void *) lcd_open,
        (void *) lcd_close, (void *) lcd_read,
        (void *) lcd_write, (void *) lcd_ioctl);
    abl_api_register((INT_32) TIMER1, (void *) timer_open,
        (void *) timer_close, (void *) timer_read,
        (void *) timer_write, (void *) timer_ioctl);

    /* Initialize the CPLD interface driver */
    cpld_init();

    /* Initialize the interrupt system */
    vic_initialize(0xC0000000);

    /* Attach the uCos-II interrupt handler to the IRQ vector */
    vic_install_arm_handler(IRQ_VEC, (PFV) &ucos_irq_handler);

    /* Install VIC1 and VIC2 handlers (used in IRQ) */
    vic_install_arm_handler(VIC1_IRQ_VEC, (PFV) vic1_irq_dispatcher);
    vic_install_arm_handler(VIC2_IRQ_VEC, (PFV) vic2_irq_dispatcher);

    /* Enable GPIO signals PE4..7 and PD0..7 as LCD signals */
    gpio_lcd_signal_select(GPIO_LCDV_0_15);

    /* Open LCD with display */
    lcddev = abl_open((INT_32) CLCDC, (INT_32) &LCDPANEL);

    /* Set GPIO PE4 high to enable LCD4..7 via CPLD */
    gpio_set_data_dir(GPIO_PORT_E, GPIO_PORT_BIT4, GPIO_OUTPUT);
    gpio_data_write(GPIO_PORT_E, GPIO_PORT_BIT4);

    /* Also make sure that the CPLD_JTAG_OE signal is in the
       correct state */
    gpio_set_data_dir(GPIO_PORT_A, 0x04, GPIO_OUTPUT);
    gpio_data_write(GPIO_PORT_A, 0x04);

    /* Set color depth to 16 bits per pixel */
    abl_ioctl(lcddev, LCD_SET_BPP, 16);
 
    /* For displays that require more bandwidth, set DMA to request
       a transfer on 4 words empty instead of the default 8. This may
       help prevent 'display tearing' due to a starved LCD controller */
    regionsize = abl_ioctl(lcddev, LCD_GET_STATUS, LCD_XSIZE) *
        abl_ioctl(lcddev, LCD_GET_STATUS, LCD_YSIZE) *
        sizeof (COLOR_T);
    if (regionsize >= (800 * 600 * 2))
    {
        /* Displays of 800x600 pixels and 16-bits of color (or larger)
           will use faster DMA requests */
        abl_ioctl(lcddev, LCD_DMA_ON_4MT, 1);
    }

    /* HRTFT/TFT panel board initialization only */
    if ((abl_ioctl(lcddev, LCD_GET_STATUS, LCD_PANEL_TYPE) == HRTFT) ||
        (abl_ioctl(lcddev, LCD_GET_STATUS, LCD_PANEL_TYPE) == ADTFT) ||
        (abl_ioctl(lcddev, LCD_GET_STATUS, LCD_PANEL_TYPE) == TFT))
    {
        /* Enable power to the LCD panel (sets VDDEN on PC3) */
        gpio_set_data_dir(GPIO_PORT_C, 0x08, GPIO_OUTPUT);
        gpio_data_write(GPIO_PORT_C, 0x08);
    }
    else
    {
        /* Other displays - do nothing (yet) */
        ;
    }

    /* Save logical address of frame buffer */
    fblog = (COLOR_T *) FBLOG;
    fbphy = (COLOR_T *) cp15_map_virtual_to_physical(fblog);

    /* Set frame buffer and enable display */
    abl_ioctl(lcddev, LCD_SET_UP_FB, (INT_32) fbphy);
    abl_ioctl(lcddev, LCD_PWENABLE, 1);

    /* Save display size */
    xsz = abl_ioctl(lcddev, LCD_GET_STATUS, LCD_XSIZE);
    ysz = abl_ioctl(lcddev, LCD_GET_STATUS, LCD_YSIZE);

    /* Break the display into 4 equal sections for SWIM windows */
    xmin[0] = xmin[1] = ymin[0] = ymin[2] = 0;
    xmax[2] = xmax[3] = xsz - 1;
    ymax[1] = ymax[3] = ysz - 1;    
    xmax[0] = xmax[1] = (xsz / 2) - 1;
    ymax[0] = ymax[2] = (ysz / 2) - 1;
    xmin[2] = xmin[3] = xmax[0] + 1;
    ymin[1] = ymin[3] = ymax[0] + 1;
    
    /* Setup timer 1 */
    timer1dev = abl_open((INT_32) TIMER1, 0);

    /* Compute the timer tick time based on the uCos OS_TICKS_PER_SEC
       define */
    abl_ioctl(timer1dev, TIMER_SET_USECS,
        ((1000 / OS_TICKS_PER_SEC) * TIMER_MSEC));

    /* Normally, we would put the address of the timer interrupt in
       the IRQ dispatcher, but the uCos-II interrupt handler in the
       os_cpu_a.s file directly handles the timer 1 interrupt so the
       following statement is not needed */
    /*
    vic_install_handler(VIC_TC1UINTR, **some_timer_isr**);
    */

    /* Enable timer interrupt in the interrupt controller - note we do
       not actually start the timer clock yet - it will start in the
       first uCos-II task */
    vic_int_enable(VIC_TC1UINTR, TRUE);

    /* Initialize uC/OS-II */
    OSInit(); 

    /* Start the first task as the background task */ 
    OSTaskCreate(TaskFirst, (void *) 0,
        &TaskStk[0][TASK_STK_SIZE - 1], 32);
    
    /* Start uC/OS-II */ 
    OSStart();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -