コード例 #1
0
ファイル: main.c プロジェクト: AndreyMostovov/asf
void main_remotewakeup_lpm_enable(void)
{
	ui_wakeup_enable();
}
コード例 #2
0
ファイル: main.c プロジェクト: thegeek82000/asf
/*! \brief Main function. Execution starts here.
 */
int main(void)
{

    uint8_t write_data[PATTERN_TEST_LENGTH];
    uint8_t read_data[PATTERN_TEST_LENGTH];
    uint32_t file_size = 0,remaining_len = 0;;

    struct i2c_master_packet tx_buf = {
        .address     = SLAVE_ADDRESS,
        .data_length = PATTERN_TEST_LENGTH,
        .data        = write_data,
        .ten_bit_address = false,
        .high_speed      = false,
        .hs_master_code  = 0x0,
    };
    struct i2c_master_packet rx_buf = {
        .address     = SLAVE_ADDRESS,
        .data_length = 1,
        .data        = read_data,
        .ten_bit_address = false,
        .high_speed      = false,
        .hs_master_code  = 0x0,
    };
    uint8_t nb_twi_packets_sent;
    uint16_t cdc_rx_size;

    irq_initialize_vectors();
    cpu_irq_enable();

    sleepmgr_init();
    system_init();
    configure_usart();

    ui_init();
    ui_powerdown();

    udc_start();
    printf("Start application\r\n");
    while (true) {

        if (!b_com_port_opened) {
            continue;
        }
        if (b_cdc_data_rx == true) {
            b_cdc_data_rx = false;
            cdc_rx_size = udi_cdc_get_nb_received_data();
            udi_cdc_read_buf((void *)cdc_data, cdc_rx_size);
            if (file_size == 0 && cdc_rx_size == 4) {
                MSB0W(file_size) = cdc_data[0];
                MSB1W(file_size) = cdc_data[1];
                MSB2W(file_size) = cdc_data[2];
                MSB3W(file_size) = cdc_data[3];
                printf("File size :%ld\r\n",file_size);
            }
            remaining_len += cdc_rx_size;

            if (cdc_rx_size == TARGET_PAGE_SIZE/2) {
                if (!b_wait) {
                    memcpy((void *)(write_data), (const void *)cdc_data, cdc_rx_size);
                    b_wait = true;
                    if (file_size + 4 == remaining_len) {
                        tx_buf.data_length = TARGET_PAGE_SIZE/2;
                        while (i2c_master_write_packet_wait(&i2c_master_instance, &tx_buf) != STATUS_OK) ;
                    }
                }
                else {
                    memcpy((void *)(write_data + (TARGET_PAGE_SIZE/2)), (const void *)cdc_data, cdc_rx_size);
                    tx_buf.data_length = TARGET_PAGE_SIZE;
                    while (i2c_master_write_packet_wait(&i2c_master_instance, &tx_buf) != STATUS_OK) ;
                    b_wait = false;
                }

            } else {
                if ((cdc_rx_size) <= PATTERN_TEST_LENGTH) {
                    tx_buf.data_length = cdc_rx_size;
                    memcpy((void *)(write_data), (const void *)cdc_data, cdc_rx_size);
                    while (i2c_master_write_packet_wait(&i2c_master_instance, &tx_buf) != STATUS_OK) ;
                } else {
                    nb_twi_packets_sent = 0;
                    while(cdc_rx_size / PATTERN_TEST_LENGTH) {
                        tx_buf.data_length = PATTERN_TEST_LENGTH;
                        memcpy((void *)(write_data), (const void *)(&cdc_data[(nb_twi_packets_sent++) * (PATTERN_TEST_LENGTH)]), PATTERN_TEST_LENGTH);
                        while (i2c_master_write_packet_wait(&i2c_master_instance, &tx_buf) != STATUS_OK) ;
                        cdc_rx_size -= (PATTERN_TEST_LENGTH);
                    }
                    if(cdc_rx_size) {
                        tx_buf.data_length = cdc_rx_size;
                        memcpy((void *)(write_data), (const void *)(&cdc_data[(nb_twi_packets_sent) * (PATTERN_TEST_LENGTH)]), cdc_rx_size);
                        while (i2c_master_write_packet_wait(&i2c_master_instance, &tx_buf) != STATUS_OK) ;
                        cdc_rx_size = 0;
                    }
                }
            }
        }
        if (i2c_master_read_packet_wait(&i2c_master_instance, &rx_buf) == STATUS_OK) {
            udi_cdc_write_buf((const void *)(rx_buf.data),(iram_size_t)read_data[0]);
            if (file_size + 4 == remaining_len) {
                printf("File transfer successfully, file size:%ld, transefer size :%ld\r\n",file_size,remaining_len-4);
            }
        }

    }
}

void main_suspend_action(void)
{
    ui_powerdown();
}

void main_resume_action(void)
{
    ui_wakeup();
}

void main_sof_action(void)
{
    if (!main_b_cdc_enable)
        return;
    ui_process(udd_get_frame_number());
}

#ifdef USB_DEVICE_LPM_SUPPORT
void main_suspend_lpm_action(void)
{
    ui_powerdown();
}

void main_remotewakeup_lpm_disable(void)
{
    ui_wakeup_disable();
}

void main_remotewakeup_lpm_enable(void)
{
    ui_wakeup_enable();
}
#endif

bool main_cdc_enable(uint8_t port)
{
    main_b_cdc_enable = true;
    configure_i2c_master();
    return true;
}

void main_cdc_disable(uint8_t port)
{
    main_b_cdc_enable = false;
    b_com_port_opened = false;
    i2c_master_disable(&i2c_master_instance);
}