Пример #1
0
// CMSIS-DAP task
__task void hid_process(void * argv) {
    dapTask = os_tsk_self();
    while (1) {
        os_evt_wait_or(DAP_PAQUET_RECEIVED, 0xffff);
        usbd_hid_process ();
        main_blink_dap_led(0);
    }
}
Пример #2
0
/*----------------------------------------------------------------------------
 *   Task 1:  RTX Kernel starts this task with os_sys_init (task1)
 *---------------------------------------------------------------------------*/
__task void task1 (void) {
  /* Obtain own system task identification number */
  id1 = os_tsk_self ();
  /* Assign system identification number of task2 to id2 */
  id2 = os_tsk_create (task2, 1);
  for (;;) {    /* do-this */
    /* Indicate to task2 completion of do-this */
    os_evt_set (0x0004, id2);
    /* Wait for completion of do-that (0xffff means no time-out)*/
    os_evt_wait_or (0x0004, 0xffff);
    /* Wait now for 50 ms */
    os_dly_wait (5);
  }
}
Пример #3
0
/*----------------------------------------------------------------------------
 *  Task 2: RTX Kernel starts this task with os_tsk_create (consumer_task, 0)
 *---------------------------------------------------------------------------*/
__task void consumer_task (void) {

	OS_TID consumer_tskid;                          /* assigned identification for consumer */
	
	consumer_tskid = os_tsk_self();									/* get it's own task ID */
	
	T_NUM *random_num_rx;

	while( msg_counter_rec < N )
	{
		os_mbx_wait (MsgBox, (void **)&random_num_rx, 0xffff); /* wait for the message    */
		
		os_mut_wait(g_mut_uart, 0xFFFF);
		printf("[consumer_task [pid:(%d)] ]: Received %u\n", consumer_tskid, random_num_rx->number);
		msg_counter_rec += 1;
		os_mut_release(g_mut_uart);
		
		if( _free_box (mpool, random_num_rx) )           /* free memory allocated for message  */
		{
			os_mut_wait(g_mut_uart, 0xFFFF);
			printf("_free_box failed because memory couldn't be freed\n");
			os_mut_release(g_mut_uart);
			exit(1);
			
		}
	}


	//Get Time C 
	time_c = os_time_get();
	
	os_mut_wait(g_mut_uart, 0xFFFF);
	printf("Time to initialize system: %0.6f\n", (float)(((float)time_b - time_a)/1000000) );
	printf("Time to transmit data: %0.6f\n", (float)(((float)time_c - time_b)/1000000) );
	os_mut_release(g_mut_uart);

	// os_dly_wait(10);
	os_tsk_delete_self ();              /* We are done here, delete this task  */
}
Пример #4
0
/*---------------------------------------------------------------------*/
__task void init(void)
{
	//Initialize the memory pool
	_init_box(box_mem, sizeof(box_mem), 4);
	
  os_mut_init(&g_mut_uart);
  
  os_mut_wait(g_mut_uart, 0xFFFF);
  printf("init: TID = %d\n", os_tsk_self());
  os_mut_release(g_mut_uart);
  
	g_tid = os_tsk_create(task1, 1);  /* task 1 at priority 1 */
	os_mut_wait(g_mut_uart, 0xFFFF);
  printf("init: created task1 with TID %d\n", g_tid);
  os_mut_release(g_mut_uart);
  
  g_tid = os_tsk_create(task2, 4);  /* task 2 at priority 2 */
  os_mut_wait(g_mut_uart, 0xFFFF);
  printf("init: created task2 with TID %d\n", g_tid);
	os_mut_release(g_mut_uart);
	
	g_tid = os_tsk_create(task3, 3);  /* task 3 at priority 3 */
  os_mut_wait(g_mut_uart, 0xFFFF);
  printf("init: created task3 with TID %d\n", g_tid);
	os_mut_release(g_mut_uart);
	
	g_tid = os_tsk_create(task4, 4);  /* task 4 at priority 4 */
  os_mut_wait(g_mut_uart, 0xFFFF);
  printf("init: created task4 with TID %d\n", g_tid);
	os_mut_release(g_mut_uart);
	
	g_tid = os_tsk_create(task5, 1);  /* task 5 at priority 1 */
  os_mut_wait(g_mut_uart, 0xFFFF);
  printf("init: created task5 with TID %d\n", g_tid);
	os_mut_release(g_mut_uart);
	
  os_tsk_delete_self();     /* task MUST delete itself before exiting */
}
Пример #5
0
static void sync_assert_usb_thread(void)
{
    util_assert(os_tsk_self() == sync_thread);
}
Пример #6
0
static void sync_init(void)
{
    sync_thread = os_tsk_self();
    os_mut_init(&sync_mutex);
}
Пример #7
0
__task void main_task(void) {
    // State processing
    uint16_t flags;

    // LED
    uint8_t dap_led_value = 1;
    uint8_t cdc_led_value = 1;
    uint8_t msd_led_value = 1;

    // USB
    uint32_t usb_state_count;

    // thread running after usb connected started
    uint8_t thread_started = 0;

    // button state
    char button_activated;

    // string containing unique ID
    uint8_t * id_str;

    // Get a reference to this task
    main_task_id = os_tsk_self();

    // leds
    gpio_init();

    usbd_init();
    swd_init();

    // Turn on LED
    gpio_set_dap_led(1);
    gpio_set_cdc_led(1);
    gpio_set_msd_led(1);

    // Setup reset button
    gpio_enable_button_flag(main_task_id, FLAGS_MAIN_RESET);
    button_activated = 1;

    // USB
    usbd_connect(0);
    usb_busy = USB_IDLE;
    usb_busy_count = 0;
    usb_state = USB_CONNECTING;
    usb_state_count = USB_CONNECT_DELAY;

    // Update HTML version information file
    update_html_file();

    // Start timer tasks
    os_tsk_create_user(timer_task_30mS, TIMER_TASK_30_PRIORITY, (void *)stk_timer_30_task, TIMER_TASK_30_STACK);

    // Target running
    target_set_state(RESET_RUN_WITH_DEBUG);

    // start semihost task
    semihost_init();
    semihost_enable();

    while(1) {
        os_evt_wait_or(   FLAGS_MAIN_RESET              // Put target in reset state
                        | FLAGS_MAIN_90MS               // 90mS tick
                        | FLAGS_MAIN_30MS               // 30mS tick
                        | FLAGS_MAIN_POWERDOWN          // Power down interface
                        | FLAGS_MAIN_DISABLEDEBUG       // Power down interface
                        | FLAGS_MAIN_USB_DISCONNECT,    // Disable target debug
                        NO_TIMEOUT);

        // Find out what event happened
        flags = os_evt_get();

        if (flags & FLAGS_MAIN_USB_DISCONNECT) {
            usb_busy = USB_IDLE;                         // USB not busy
            usb_state_count = 4;
            usb_state = USB_DISCONNECT_CONNECT;        // disconnect the usb
        }

        if (flags & FLAGS_MAIN_RESET) {
            cdc_led_state = LED_OFF;
            gpio_set_cdc_led(0);
            //usbd_cdc_ser_flush();
            if (send_uID) {
                // set the target in reset to not receive char on the serial port
                target_set_state(RESET_HOLD);

                // send uid
                id_str = get_uid_string();
                USBD_CDC_ACM_DataSend(id_str, strlen((const char *)id_str));
                send_uID = 0;
            }
            // Reset target
            target_set_state(RESET_RUN);
            cdc_led_state = LED_FLASH;
            gpio_set_cdc_led(1);
            button_activated = 0;
        }

        if (flags & FLAGS_MAIN_POWERDOWN) {
            // Stop semihost task
            semihost_disable();

            // Disable debug
            target_set_state(NO_DEBUG);

            // Disconnect USB
            usbd_connect(0);

            // Turn off LED
            gpio_set_dap_led(0);
            gpio_set_cdc_led(0);
            gpio_set_msd_led(0);

            // TODO: put the interface chip in sleep mode
            while (1) {    }
        }

        if (flags & FLAGS_MAIN_DISABLEDEBUG) {
            // Stop semihost task
            semihost_disable();

            // Disable debug
            target_set_state(NO_DEBUG);
        }

        if (flags & FLAGS_MAIN_90MS) {
            if (!button_activated) {
                gpio_enable_button_flag(main_task_id, FLAGS_MAIN_RESET);
                button_activated = 1;
            }

            // Update USB busy status
            switch (usb_busy) {

                case USB_ACTIVE:
                    if (DECZERO(usb_busy_count) == 0) {
                        usb_busy=USB_IDLE;
                    }
                    break;

                case USB_IDLE:
                default:
                    break;
            }

            // Update USB connect status
            switch (usb_state) {

                case USB_DISCONNECTING:
                    // Wait until USB is idle before disconnecting
                    if (usb_busy == USB_IDLE) {
                        usbd_connect(0);
                        usb_state = USB_DISCONNECTED;
                    }
                    break;

                case USB_DISCONNECT_CONNECT:
                    // Wait until USB is idle before disconnecting
                    if ((usb_busy == USB_IDLE) && (DECZERO(usb_state_count) == 0)) {
                        usbd_connect(0);
                        usb_state = USB_CONNECTING;

                        // Update HTML file
                        update_html_file();
                    }
                    break;

                case USB_CONNECTING:
                    // Wait before connecting
                    if (DECZERO(usb_state_count) == 0) {
                        usbd_connect(1);
                        usb_state = USB_CHECK_CONNECTED;
                    }
                    break;

                case USB_CHECK_CONNECTED:
                    if(usbd_configured()) {
                        if (!thread_started) {
                            os_tsk_create_user(hid_process, DAP_TASK_PRIORITY, (void *)stk_dap_task, DAP_TASK_STACK);
                            serial_task_id = os_tsk_create_user(serial_process, SERIAL_TASK_PRIORITY, (void *)stk_serial_task, SERIAL_TASK_STACK);
                            thread_started = 1;
                        }
                        usb_state = USB_CONNECTED;
                    }
                    break;

                case USB_CONNECTED:
                case USB_DISCONNECTED:
                default:
                    break;
            }
         }

        // 30mS tick used for flashing LED when USB is busy
        if (flags & FLAGS_MAIN_30MS) {
            if (dap_led_usb_activity && ((dap_led_state == LED_FLASH) || (dap_led_state == LED_FLASH_PERMANENT))) {
                // Flash DAP LED ONCE
                if (dap_led_value) {
                    dap_led_value = 0;
                } else {
                    dap_led_value = 1; // Turn on
                    if (dap_led_state == LED_FLASH) {
                        dap_led_usb_activity = 0;
                    }
                }

                // Update hardware
                gpio_set_dap_led(dap_led_value);
            }

            if (msd_led_usb_activity && ((msd_led_state == LED_FLASH) || (msd_led_state == LED_FLASH_PERMANENT))) {
                // Flash MSD LED ONCE
                if (msd_led_value) {
                    msd_led_value = 0;
                } else {
                    msd_led_value = 1; // Turn on
                    if (msd_led_state == LED_FLASH) {
                        msd_led_usb_activity = 0;
                    }
                }

                // Update hardware
                gpio_set_msd_led(msd_led_value);
            }

            if (cdc_led_usb_activity && ((cdc_led_state == LED_FLASH) || (cdc_led_state == LED_FLASH_PERMANENT))) {
                // Flash CDC LED ONCE
                if (cdc_led_value) {
                    cdc_led_value = 0;
                } else {
                    cdc_led_value = 1; // Turn on
                    if (cdc_led_state == LED_FLASH) {
                        cdc_led_usb_activity = 0;
                    }
                }

                // Update hardware
                gpio_set_cdc_led(cdc_led_value);
            }

        }
    }
}
Пример #8
0
__task void main_task(void) {
    // State processing
    uint16_t flags;

    // LED
    uint8_t dap_led_value = 1;
    uint8_t cdc_led_value = 1;
    uint8_t msd_led_value = 1;

    // USB
    uint32_t usb_state_count;

    // thread running after usb connected started
    uint8_t thread_started = 0;

    // button state
    char button_activated;

    // string containing unique ID
    uint8_t * id_str;

    // Initialize our serial mailbox
    os_mbx_init(&serial_mailbox, sizeof(serial_mailbox));

    // Get a reference to this task
    main_task_id = os_tsk_self();

    // leds
    gpio_init();
    // Turn off LED
    gpio_set_dap_led(1);
    gpio_set_cdc_led(1);
    gpio_set_msd_led(1);

#ifdef BOARD_UBLOX_C027
    PORT_SWD_SETUP();
    // wait until reset output to the target is pulled high
    while (!PIN_nRESET_IN()) {
        /* wait doing nothing */
    }
    os_dly_wait(4);
    // if the reset input from button is low then enter isp programming mode
    if (!(LPC_GPIO->B[19/*RESET_PIN*/ + (1/*RESET_PORT*/ << 5)] & 1)) {
        enter_isp();
    }
#endif 

    usbd_init();
    swd_init();

    // Setup reset button
    gpio_enable_button_flag(main_task_id, FLAGS_MAIN_RESET);
    button_activated = 1;

    // USB
    usbd_connect(0);
    usb_busy = USB_IDLE;
    usb_busy_count = 0;
    usb_state = USB_CONNECTING;
    usb_state_count = USB_CONNECT_DELAY;

    // Update HTML version information file
    update_html_file();

    // Start timer tasks
    os_tsk_create_user(timer_task_30mS, TIMER_TASK_30_PRIORITY, (void *)stk_timer_30_task, TIMER_TASK_30_STACK);

#ifndef BOARD_UBLOX_C027
    // Target running
    //target_set_state(RESET_RUN_WITH_DEBUG);
#endif

#ifdef BOARD_NRF51822AA
    // Target running
    target_set_state(RESET_RUN);
#endif
    // start semihost task
    semihost_init();
    semihost_enable();

    while(1) {
        os_evt_wait_or(   FLAGS_MAIN_RESET              // Put target in reset state
                        | FLAGS_MAIN_90MS               // 90mS tick
                        | FLAGS_MAIN_30MS               // 30mS tick
                        | FLAGS_MAIN_POWERDOWN          // Power down interface
                        | FLAGS_MAIN_DISABLEDEBUG       // Power down interface
#ifdef USE_USB_EJECT_INSERT
                        | FLAGS_MAIN_USB_DISCONNECT     // Disable target debug
                        | FLAGS_MAIN_USB_MEDIA_EJECT,   // Eject file system
#else
                        | FLAGS_MAIN_USB_DISCONNECT,    // Disable target debug
#endif
                        NO_TIMEOUT);

        // Find out what event happened
        flags = os_evt_get();

        if (flags & FLAGS_MAIN_USB_DISCONNECT) {
            usb_busy = USB_IDLE;                         // USB not busy
            usb_state_count = 4;
            usb_state = USB_DISCONNECT_CONNECT;        // disconnect the usb
        }

#ifdef USE_USB_EJECT_INSERT
        if (flags & FLAGS_MAIN_USB_MEDIA_EJECT) {
            EjectInsertMediaMode = EJECT_INSERT_WAIT_TO_EJECT;
            EjectInsertMediaCounter = EJECT_INSERT_DELAY_500MS;
        }
#endif

        if (flags & FLAGS_MAIN_RESET) {
            cdc_led_state = LED_OFF;
            gpio_set_cdc_led(0);
            //usbd_cdc_ser_flush();
            if (send_uID) {
                // set the target in reset to not receive char on the serial port
                target_set_state(RESET_HOLD);

                // send uid
                id_str = get_uid_string();
                USBD_CDC_ACM_DataSend(id_str, strlen((const char *)id_str));
                send_uID = 0;
            }
            // Reset target
            target_set_state(RESET_RUN);
            cdc_led_state = LED_FLASH;
            gpio_set_cdc_led(1);
            button_activated = 0;
        }

        if (flags & FLAGS_MAIN_POWERDOWN) {
            // Stop semihost task
            semihost_disable();

            // Disable debug
            target_set_state(NO_DEBUG);

            // Disconnect USB
            usbd_connect(0);

            // Turn off LED
            gpio_set_dap_led(0);
            gpio_set_cdc_led(0);
            gpio_set_msd_led(0);

            // TODO: put the interface chip in sleep mode
            while (1) {    }
        }

        if (flags & FLAGS_MAIN_DISABLEDEBUG) {
            // Stop semihost task
            semihost_disable();

            // Disable debug
            target_set_state(NO_DEBUG);
        }

        if (flags & FLAGS_MAIN_90MS) {
            if (!button_activated) {
                gpio_enable_button_flag(main_task_id, FLAGS_MAIN_RESET);
                button_activated = 1;
            }

#ifdef USE_USB_EJECT_INSERT
            if (EjectInsertMediaMode == EJECT_INSERT_WAIT_TO_EJECT) {
                if (--EjectInsertMediaCounter == 0) {
                    // Have waited ~0.5 second, time to eject media
                    EjectInsertMediaMode = EJECT_INSERT_WAIT_TO_INSERT;
                    EjectInsertMediaCounter = EJECT_INSERT_DELAY_500MS;
                    USBD_MSC_MediaReady = __FALSE;
                }
            }
            if ((EjectInsertMediaMode == EJECT_INSERT_WAIT_TO_INSERT) && !USBD_MSC_MediaReadyEx) {
                // The host computer have questioned the state and received
                // the message that the media has been removed
                if (--EjectInsertMediaCounter == 0) {
                    // Have waited ~0.5 seconds after ejecting, time to insert media
                    EjectInsertMediaMode = EJECT_INSERT_INACTIVE;
                    USBD_MSC_MediaReady = __TRUE;
                }
            }
#endif

            // Update USB busy status
            switch (usb_busy) {

                case USB_ACTIVE:
                    if (DECZERO(usb_busy_count) == 0) {
                        usb_busy=USB_IDLE;
                    }
                    break;

                case USB_IDLE:
                default:
                    break;
            }

            // Update USB connect status
            switch (usb_state) {

                case USB_DISCONNECTING:
                    // Wait until USB is idle before disconnecting
                    if (usb_busy == USB_IDLE) {
                        usbd_connect(0);
                        usb_state = USB_DISCONNECTED;
                    }
                    break;

                case USB_DISCONNECT_CONNECT:
                    // Wait until USB is idle before disconnecting
                    if ((usb_busy == USB_IDLE) && (DECZERO(usb_state_count) == 0)) {
                        usbd_connect(0);
                        usb_state = USB_CONNECTING;
                        // Update HTML file
                        update_html_file();
						// Delay the connecting state before reconnecting to the host - improved usage with VMs
						usb_state_count = 10; //(90ms * 10 = 900ms)
                    }
                    break;

                case USB_CONNECTING:
                    // Wait before connecting
                    if (DECZERO(usb_state_count) == 0) {
                        usbd_connect(1);
                        usb_state = USB_CHECK_CONNECTED;
                    }
                    break;

                case USB_CHECK_CONNECTED:
                    if(usbd_configured()) {
                        if (!thread_started) {
                            os_tsk_create_user(hid_process, DAP_TASK_PRIORITY, (void *)stk_dap_task, DAP_TASK_STACK);
                            serial_task_id = os_tsk_create_user(serial_process, SERIAL_TASK_PRIORITY, (void *)stk_serial_task, SERIAL_TASK_STACK);
                            thread_started = 1;
                        }
                        usb_state = USB_CONNECTED;
                    }
                    break;

                case USB_CONNECTED:
                case USB_DISCONNECTED:
                default:
                    break;
            }
         }

        // 30mS tick used for flashing LED when USB is busy
        if (flags & FLAGS_MAIN_30MS) {
            if (dap_led_usb_activity && ((dap_led_state == LED_FLASH) || (dap_led_state == LED_FLASH_PERMANENT))) {
                // Flash DAP LED ONCE
                if (dap_led_value) {
                    dap_led_value = 0;
                } else {
                    dap_led_value = 1; // Turn on
                    if (dap_led_state == LED_FLASH) {
                        dap_led_usb_activity = 0;
                    }
                }

                // Update hardware
                gpio_set_dap_led(dap_led_value);
            }

            if (msd_led_usb_activity && ((msd_led_state == LED_FLASH) || (msd_led_state == LED_FLASH_PERMANENT))) {
                // Flash MSD LED ONCE
                if (msd_led_value) {
                    msd_led_value = 0;
                } else {
                    msd_led_value = 1; // Turn on
                    if (msd_led_state == LED_FLASH) {
                        msd_led_usb_activity = 0;
                    }
                }

                // Update hardware
                gpio_set_msd_led(msd_led_value);
            }

            if (cdc_led_usb_activity && ((cdc_led_state == LED_FLASH) || (cdc_led_state == LED_FLASH_PERMANENT))) {
                // Flash CDC LED ONCE
                if (cdc_led_value) {
                    cdc_led_value = 0;
                } else {
                    cdc_led_value = 1; // Turn on
                    if (cdc_led_state == LED_FLASH) {
                        cdc_led_usb_activity = 0;
                    }
                }

                // Update hardware
                gpio_set_cdc_led(cdc_led_value);
            }

        }
    }
}
Пример #9
0
int32_t LLMJVM_IMPL_vmTaskStarted(void){
	LLMJVM_RTX_taskID = os_tsk_self();
	return LLMJVM_OK;
}
Пример #10
0
int32_t LLMJVM_IMPL_getCurrentTaskID(void){
	return (int32_t)os_tsk_self();
}
Пример #11
0
osThreadId_t osThreadGetId(void)
{
    return (osThreadId_t)os_tsk_self();
}
Пример #12
0
void GUI_X_WaitEventTimed(int Period) {
  GUI_Task = os_tsk_self();
  os_evt_wait_or (0x0001, (Period > 0xFFFE) ? 0xFFFE : Period); 
}
Пример #13
0
void GUI_X_WaitEvent(void) {
  GUI_Task = os_tsk_self();
  os_evt_wait_or (0x0001, 0xFFFF); 
}
Пример #14
0
U32  GUI_X_GetTaskId(void) { return os_tsk_self(); }
Пример #15
0
/*----------------------------------------------------------------------------
 *  Task Producer:  RTX Kernel starts this task with os_sys_init (producer_task)
 *---------------------------------------------------------------------------*/
__task void producer_task (void) {
  
	OS_TID producer_tskid;                          /* assigned identification for producer  */

	T_NUM *random_num_tx;
	
	//Get Time A
	//time_a = os_time_get();
	
	//Get current task ID
	producer_tskid = os_tsk_self ();
	
	// fork the child process
	os_tsk_create (consumer_task, 0); /* start task 2                        */
  
	os_mut_wait(procon, 0xFFFF);
	if(cur_producer == 0)
	{
		os_mbx_init (MsgBox, sizeof(MsgBox));/* initialize the mailbox             */
 
		os_mut_wait(g_mut_uart, 0xFFFF);
		printf("[producer_task [pid:(%d)] ]: Mailbox Created\n", producer_tskid);
		os_mut_release(g_mut_uart);
		
		cur_producer = 1;
	}
	os_mut_release(procon);
		
	//Get Time B
	//time_b = os_time_get() ;

	// Check if the number is the required one
	// .. And also make sure that we still have to send numbers.
	while (msg_counter_send < N && ( (msg_counter_send % P) == producer_tskid ) )
	{
		random_num_tx = _alloc_box(mpool);			/* Allocate a memory for the message   */
	
		if( random_num_tx == NULL )
		{
			os_mut_wait(g_mut_uart, 0xFFFF);
			printf("_alloc_box failed because of no mem available!\n");
			os_mut_release(g_mut_uart);
			exit(1);
		}
		
		random_num_tx->number = (U32) ( msg_counter_send );
		
		os_mbx_send (MsgBox, random_num_tx, 0xffff); /* Send the message to the mailbox     */
		
		os_mut_wait(g_mut_uart, 0xFFFF);
		printf("[producer_task [pid:(%d)] ]: Sent %u\n", producer_tskid, random_num_tx->number);
		msg_counter_send++;
		os_mut_release(g_mut_uart);
		
		
		// os_dly_wait (100);
	}
	
	//msg_counter_send = 0;
		
	os_tsk_delete_self ();              /* We are done here, delete this task  */
}
Пример #16
0
__task void main_task(void)
{
    // State processing
    uint16_t flags = 0;
    // LED
    gpio_led_state_t hid_led_value = GPIO_LED_ON;
    gpio_led_state_t cdc_led_value = GPIO_LED_ON;
    gpio_led_state_t msc_led_value = GPIO_LED_ON;
    // USB
    uint32_t usb_state_count = USB_BUSY_TIME;
    // thread running after usb connected started
    uint8_t thread_started = 0;
    // button state
    main_reset_state_t main_reset_button_state = MAIN_RESET_RELEASED;

    // Initialize settings
    config_init();

    // Initialize our serial mailbox
    os_mbx_init(&serial_mailbox, sizeof(serial_mailbox));
    // Get a reference to this task
    main_task_id = os_tsk_self();

    // leds
    gpio_init();
    // Turn off LED
    gpio_set_hid_led(GPIO_LED_ON);
    gpio_set_cdc_led(GPIO_LED_ON);
    gpio_set_msc_led(GPIO_LED_ON);

    // do some init with the target before USB and files are configured
    prerun_target_config();

    // Update versions and IDs
    info_init();

    // USB
    usbd_init();
    vfs_user_enable(true);
    usbd_connect(0);
    usb_state = USB_CONNECTING;
    usb_state_count = USB_CONNECT_DELAY;

    // Start timer tasks
    os_tsk_create_user(timer_task_30mS, TIMER_TASK_30_PRIORITY, (void *)stk_timer_30_task, TIMER_TASK_30_STACK);

    // Target running
    target_set_state(RESET_RUN);

    while(1) {
        os_evt_wait_or(   FLAGS_MAIN_RESET              // Put target in reset state
                        | FLAGS_MAIN_90MS               // 90mS tick
                        | FLAGS_MAIN_30MS               // 30mS tick
                        | FLAGS_MAIN_POWERDOWN          // Power down interface
                        | FLAGS_MAIN_DISABLEDEBUG       // Disable target debug
                        | FLAGS_MAIN_PROC_USB           // process usb events
                        ,NO_TIMEOUT);

        // Find out what event happened
        flags = os_evt_get();

        if (flags & FLAGS_MAIN_PROC_USB) {
            USBD_Handler();
        }

        if (flags & FLAGS_MAIN_RESET) {
            target_set_state(RESET_RUN);
        }

        if (flags & FLAGS_MAIN_POWERDOWN) {
            // Disable debug
            target_set_state(NO_DEBUG);
            // Disconnect USB
            usbd_connect(0);
            // Turn off LED
            gpio_set_hid_led(GPIO_LED_OFF);
            gpio_set_cdc_led(GPIO_LED_OFF);
            gpio_set_msc_led(GPIO_LED_OFF);
            // TODO: put the interface chip in sleep mode
            while(1);
        }

        if (flags & FLAGS_MAIN_DISABLEDEBUG) {
            // Disable debug
            target_set_state(NO_DEBUG);
        }

        if (flags & FLAGS_MAIN_90MS) {
            // Update USB busy status
            vfs_user_periodic(90); // FLAGS_MAIN_90MS

            // Update USB connect status
            switch (usb_state) {
                case USB_DISCONNECTING:
                    usb_state = USB_DISCONNECTED;
                    usbd_connect(0);
                    break;

                case USB_CONNECTING:
                    // Wait before connecting
                    if (DECZERO(usb_state_count) == 0) {
                        usbd_connect(1);
                        usb_state = USB_CHECK_CONNECTED;
                    }
                    break;

                case USB_CHECK_CONNECTED:
                    if(usbd_configured()) {
                        if (!thread_started) {
                            os_tsk_create_user(hid_process, DAP_TASK_PRIORITY, (void *)stk_dap_task, DAP_TASK_STACK);
                            serial_task_id = os_tsk_create_user(serial_process, SERIAL_TASK_PRIORITY, (void *)stk_serial_task, SERIAL_TASK_STACK);
                            thread_started = 1;
                        }
                        usb_state = USB_CONNECTED;
                    }
                    break;

                case USB_CONNECTED:
                case USB_DISCONNECTED:
                default:
                    break;
            }
         }

        // 30mS tick used for flashing LED when USB is busy
        if (flags & FLAGS_MAIN_30MS) {
            // handle reset button without eventing
            switch (main_reset_button_state) {
                default:
                case MAIN_RESET_RELEASED:
                    if (0 == gpio_get_sw_reset()) {
                        main_reset_button_state = MAIN_RESET_PRESSED;
                        target_forward_reset(true);
                    }
                    break;

                case MAIN_RESET_PRESSED:
                    // ToDo: add a counter to do a mass erase or target recovery after xxx seconds of being held
                    if (1 == gpio_get_sw_reset()) {
                        main_reset_button_state = MAIN_RESET_TARGET;
                    }
                    break;

                case MAIN_RESET_TARGET:
                    target_forward_reset(false);
                    main_reset_button_state = MAIN_RESET_RELEASED;
                    break;
            }

            if (hid_led_usb_activity && ((hid_led_state == MAIN_LED_FLASH) || (hid_led_state == MAIN_LED_FLASH_PERMANENT))) {
                // Flash DAP LED ONCE
                if (hid_led_value) {
                    hid_led_value = GPIO_LED_OFF;
                } else {
                    hid_led_value = GPIO_LED_ON; // Turn on
                    if (hid_led_state == MAIN_LED_FLASH) {
                        hid_led_usb_activity = 0;
                    }
                }

                // Update hardware
                gpio_set_hid_led(hid_led_value);
            }

            if (msc_led_usb_activity && ((msc_led_state == MAIN_LED_FLASH) || (msc_led_state == MAIN_LED_FLASH_PERMANENT))) {
                // Flash MSD LED ONCE
                if (msc_led_value) {
                    msc_led_value = GPIO_LED_OFF;
                } else {
                    msc_led_value = GPIO_LED_ON; // Turn on
                    if (msc_led_state == MAIN_LED_FLASH) {
                        msc_led_usb_activity = 0;
                    }
                }

                // Update hardware
                gpio_set_msc_led(msc_led_value);
            }

            if (cdc_led_usb_activity && ((cdc_led_state == MAIN_LED_FLASH) || (cdc_led_state == MAIN_LED_FLASH_PERMANENT))) {
                // Flash CDC LED ONCE
                if (cdc_led_value) {
                    cdc_led_value = GPIO_LED_OFF;
                } else {
                    cdc_led_value = GPIO_LED_ON; // Turn on
                    if (cdc_led_state == MAIN_LED_FLASH) {
                        cdc_led_usb_activity = 0;
                    }
                }

                // Update hardware
                gpio_set_cdc_led(cdc_led_value);
            }

        }
    }
}
Пример #17
0
/*----------------------------------------------------------------------------
 *   Task 1:  RTX Kernel starts this task with os_sys_init (taskMenu)
 *---------------------------------------------------------------------------*/
__task void taskMenu (void) 
{
	// Variable declaration
	GPU_Image background;	
	GPU_Color aColor;	
	GPU_Color clearColor;
	int stateArrow = 0;
	uint8_t arrowX = 0;
	uint8_t arrowY = 0;
	
	uint32_t joystick;
	int stateOutput = 1;
	
	// Set the task priority
	id1 = os_tsk_self ();
  os_tsk_prio_self (1);
	
	// Configure the layer for the console menu
	GPU_ConfigureLayer(&Layer_1,LAYER1_START_ADDRESS,320,240);
	GPU_ConfigureLayer(&Layer_2,LAYER2_START_ADDRESS,320,240);
	GPU_ConfigureLayer(&Layer_3,LAYER3_START_ADDRESS,320,240);
	GPU_ConfigureLayer(&Layer_4,LAYER4_START_ADDRESS,320,240);

	Display_conf.Enable =1;
	Display_conf.Alpha_On =1;
	Display_conf.Plan_Enable = 0xF;
	Display_conf.Test_On=0;
	
	GPU_UpdateDisplayConfig();
	
	GPU_HScroll (&Layer_1, 0,1);
	GPU_VScroll (&Layer_1, 0,1);
	
	GPU_HScroll (&Layer_2, 0,1);
	GPU_VScroll (&Layer_2, 0,1);
	
	GPU_HScroll (&Layer_3, 0,1);
	GPU_VScroll (&Layer_3, 0,1);
	
	GPU_HScroll (&Layer_4, 0,1);
	GPU_VScroll (&Layer_4, 0,1);
						
	GPU_ConfigureOutput(MODE_VGA);
	
	GPU_ClearScreen(&Layer_1);
	GPU_ClearScreen(&Layer_2);
	clearColor.R = 0;
	clearColor.G = 0;
	clearColor.B = 0;
	clearColor.A = 0;
	GPU_FillRect (&Layer_2,0,0,320,240, clearColor);
	GPU_ClearScreen(&Layer_3);
	GPU_FillRect (&Layer_4,0,0,320,240, clearColor);
	GPU_ClearScreen(&Layer_4);
	
	// Load the image from the SD card to the graphic card
	GPU_NewImage(&background, 320, 240,"menu", 0x0500000);
	SD_LoadImage(&background, 320, 240, &fil);
	GPU_BitBlitI2L(&background, 0, 0, 320, 240,&Layer_1, 0,0);
	
	// Display the two optionnal menu
	aColor.R = 255;
	aColor.G = 255;
	aColor.B = 255;
	aColor.A = 255;
	GPU_WriteText(&Layer_3, 76, 188, "->  Demo", aColor);
	GPU_WriteText(&Layer_3, 40, 220, "Credits", aColor);
	GPU_WriteText(&Layer_3, 220, 220, "Output :", aColor);			
			
	// Display the selector to the first programm
	aColor.R = 7;
	aColor.G = 1;
	aColor.B = 15;
	aColor.A = 15;
	GPU_FillRect (&Layer_2,60,108,10,10, aColor);
	
	if ( stateOutput == 1)
	{
		GPU_WriteText(&Layer_4, 270, 220, "VGA", aColor);
	}
	else
	{
		GPU_WriteText(&Layer_4, 270, 220, "LCD", aColor);
	}
	
	while((is_button_pressed(USER) && joystick != JOY_RIGHT) || stateArrow == 5 )
	{
		joystick = JOY_GetKeys();
		if (joystick == JOY_UP)
		{
			if (stateArrow == 0)
			{
				stateArrow = 5;
			}
			else
			{
				stateArrow--;
			}
		}
		if (joystick == JOY_DOWN)
		{
			stateArrow++;
			stateArrow = stateArrow % 6;
		}
		
		if (stateArrow == 4)
		{
				arrowX = 20;
				arrowY = 215;
		}
		else if (stateArrow == 5)
		{
			arrowX = 200;
			arrowY = 215;					
		}
		else
		{
			arrowX = 60;
			arrowY = 108 + 26*stateArrow;
		}
		
			// Update the position of the selector
			GPU_ClearScreen(&Layer_2);
			GPU_FillRect (&Layer_2,arrowX ,arrowY,10,10, aColor);
		
		switch (stateArrow)
		{
			case 0:
			{
				Turn_Led(LED2,ON);
				Turn_Led(LED3,OFF);
				Turn_Led(LED4,OFF);
				Turn_Led(LED5,OFF);
				Turn_Led(LED6,OFF);
				Turn_Led(LED7,OFF);
				break;
			}
			case 1:
			{
				Turn_Led(LED2,OFF);
				Turn_Led(LED3,ON);
				Turn_Led(LED4,OFF);
				Turn_Led(LED5,OFF);
				Turn_Led(LED6,OFF);
				Turn_Led(LED7,OFF);
				break;
			}
			case 2:
			{
				Turn_Led(LED2,OFF);
				Turn_Led(LED3,OFF);
				Turn_Led(LED4,ON);
				Turn_Led(LED5,OFF);
				Turn_Led(LED6,OFF);
				Turn_Led(LED7,OFF);
				break;
			}
			case 3:
			{
				Turn_Led(LED2,OFF);
				Turn_Led(LED3,OFF);
				Turn_Led(LED4,OFF);
				Turn_Led(LED5,ON);
				Turn_Led(LED6,OFF);
				Turn_Led(LED7,OFF);
				break;
			}
			case 4:
			{
				Turn_Led(LED2,OFF);
				Turn_Led(LED3,OFF);
				Turn_Led(LED4,OFF);
				Turn_Led(LED5,OFF);
				Turn_Led(LED6,ON);
				Turn_Led(LED7,OFF);
				break;
			}
			case 5:
			{
				Turn_Led(LED2,OFF);
				Turn_Led(LED3,OFF);
				Turn_Led(LED4,OFF);
				Turn_Led(LED5,OFF);
				Turn_Led(LED6,OFF);
				Turn_Led(LED7,ON);
				break;
			}
		}

		if (stateArrow == 5 && !is_button_pressed(USER))
		{
			GPU_ClearScreen(&Layer_4);
			GPU_FillRect (&Layer_4,0,0,320,240, clearColor);
			if ( stateOutput == 1)
			{
				stateOutput = 0;
				GPU_WriteText(&Layer_4, 270, 220, "VGA", aColor);
				GPU_ConfigureOutput(MODE_VGA);
			}
			else
			{
				stateOutput = 1;
				GPU_WriteText(&Layer_4, 270, 220, "LCD", aColor);
				GPU_ConfigureOutput(MODE_LCD);
			}
			while(!is_button_pressed(USER));
		}		
		os_dly_wait(30);
	}
	
	// Start the task selected
	switch (stateArrow)
	{
		case 0:
		{
			os_tsk_create (taskUnicorn, 10);				
			break;
		}
		case 1:
		{
			os_tsk_create (taskSOR, 10);
			break;
		}
		case 2:
		{
			os_tsk_create (taskZelda, 10);
			break;
		}
		case 3:
		{
			os_tsk_create (taskDemo, 10);
			break;
		}
		case 4:
		{
			os_tsk_create (taskCredit, 10);
			break;
		}
		
	}
	
	// Destroy the task
	os_tsk_delete_self ();
	while (1){};
}