コード例 #1
0
ファイル: ep.c プロジェクト: pursuitxh/FreakUSB
void ep_send_zlp(U8 ep_num)
{
    if( ep_num == 0 )
    {
        SI32_USB_A_set_data_end_ep0( SI32_USB_0 );
        SI32_USB_A_clear_out_packet_ready_ep0( SI32_USB_0 );
    }
}
コード例 #2
0
// Finish handling the setup packet and move on to the IN/OUT data phase
// of the control request
void Finish_Setup(void)
{
  // Copy selected bytes from the Setup packet
  for (int i = 0; i < DFU_SETUP_PACKET_COPY_SIZE; i++)
  {
    RxBuffer[i] = ((uint8_t*)&Setup)[i + DFU_SETUP_PACKET_COPY_OFFSET];
  }

  // Set Serviced Out packet ready
  SI32_USB_A_clear_out_packet_ready_ep0(SI32_USB_0);

  // Handle out class requests
  if ((Setup.bmRequestType & DIR_BITMASK) == DIR_OUT)
  {
    // No data phase, end the control transfer
    if (Setup.wLength == 0)
    {
      // Set Serviced Out packet ready and
      // data end to indicate transaction
      // is over
      SI32_USB_A_set_data_end_ep0(SI32_USB_0);

      // Return to IDLE state
      EP_Status = EP_IDLE;

      // Control transfer complete
      USB0_RX_Complete(DFU_SETUP_PACKET_COPY_SIZE);
    }
    else
    {
      // Set the DataPtr to store data in the ClassOutBuffer (after the Setup packet data)
      ReadDataPtr = RxBuffer + DFU_SETUP_PACKET_COPY_SIZE;
      ReadDataSize = RxBufferSize - DFU_SETUP_PACKET_COPY_SIZE;
      ReadDataReceived = 0;

      // Setup the RX phase of the control transfer
      EP_Status = EP_RX;
    }
  }
  // Handle in class requests
  else
  {
    // Wait until TX data is ready from the App
    EP_Status = EP_WAIT_TX;

    // Control transfer setup complete
    USB0_RX_Complete(DFU_SETUP_PACKET_COPY_SIZE);
  }
}
コード例 #3
0
// Pass in the TX buffer used to send the IN data phase
void USB0_TX_Start (uint8_t* buffer, uint16_t size)
{
  __disable_irq();

  WriteDataPtr = buffer;
  WriteDataSize = size;
  WriteDataSent = 0;      // Reset Data Sent counter
  TxComplete = false;

  // Set Serviced Out packet ready
  SI32_USB_A_clear_out_packet_ready_ep0(SI32_USB_0);

  // Setup the TX phase of the control transfer
  EP_Status = EP_TX;

  __enable_irq();
}
コード例 #4
0
ファイル: dfu.c プロジェクト: cardieg/FreakUSB
void dfu_req_handler(req_t *req)
{
    U8 i;
    usb_pcb_t *pcb = usb_pcb_get();

    switch (req->req)
    {
    case DFU_DETACH:
        if (req->type & (HOST_TO_DEVICE | TYPE_CLASS | RECIPIENT_INTF))
        {
            // wvalue is wTimeout
            // wLength is zero
            // data is none
            dfu_status.bState = appDETACH;
            dfu_status.bStatus = OK;

        }
        break;

    case DFU_DNLOAD:
        if (req->type & (HOST_TO_DEVICE | TYPE_CLASS | RECIPIENT_INTF))
        {
            // wvalue is wBlockNum
            // wlength is Length
            // data is firmware
            if( dfu_status.bState == dfuIDLE )
            {
                if( req->len > 0 )
                {
                    hw_state_indicator( HW_STATE_TRANSFER );
                    dfu_status.bState = dfuDNLOAD_SYNC;
                }
                else
                {
                    dfu_status.bState  = dfuERROR;
                    dfu_status.bStatus = errNOTDONE;
                    hw_state_indicator( HW_STATE_ERROR );
                    ep_send_zlp(EP_CTRL);
                    return;
                }
            }
        	i = req->val;
            if( dfu_status.bState == dfuDNLOAD_IDLE )
            {
                if( req->len > 0 )
                {
                    dfu_status.bState = dfuDNLOAD_SYNC;
                }
                else
                {
                    if( flash_buffer_ptr > flash_buffer )
                    {
                        need_to_write = 1;
                        //flash_buffer_ptr = flash_buffer;
                    }
                    dfu_status.bState  = dfuMANIFEST_SYNC;
                    ep_send_zlp(EP_CTRL);
                    return;
                }
            }

            SI32_USB_A_clear_out_packet_ready_ep0(SI32_USB_0);

            while(pcb->fifo[EP_CTRL].len < req->len)
            {
                //ep_read(EP_CTRL);
            	i = pcb->fifo[EP_CTRL].len;
            }

            // clear the setup flag if needed
            pcb->flags &= ~(1<<SETUP_DATA_AVAIL);

            // send out a zero-length packet to ack to the host that we received
            // the new line coding
            U8* byte_buf_ptr = ( U8* )flash_buffer_ptr;
            U8 tmp_len = pcb->fifo[EP_CTRL].len;
            for(i = 0; i < tmp_len; i++)
            {
                *byte_buf_ptr = usb_buf_read(EP_CTRL);
                byte_buf_ptr++;
            }
            flash_buffer_ptr += i/4;

            if( flash_buffer_ptr == flash_buffer + BLOCK_SIZE_U32 )
            {
                // Reset buffer pointer
                //flash_buffer_ptr = flash_buffer;
                need_to_write = 1;
            }

            if( flash_buffer_ptr > flash_buffer + BLOCK_SIZE_U32)
            {
                dfu_status.bState  = dfuERROR;
                hw_state_indicator( HW_STATE_ERROR );
            }

            ep_send_zlp(EP_CTRL);
        }
        break;

    case DFU_UPLOAD:
        if (req->type & (DEVICE_TO_HOST | TYPE_CLASS | RECIPIENT_INTF))
        {
            // wvalue is zero
            // wlength is length
            // data is firmware
            // NOT SUPPORTED
            ep_set_stall(EP_CTRL);
        }
        break;

    case DFU_GETSTATUS:
        if (req->type & (DEVICE_TO_HOST | TYPE_CLASS | RECIPIENT_INTF))
        {
            if( dfu_communication_started == 0 )
                hw_state_indicator( HW_STATE_CONNECTED );

            dfu_communication_started = 1;
            // If we're still transmitting blocks
            if( dfu_status.bState == dfuDNLOAD_SYNC )
            {
                if( need_to_write == 0 )
                {
                    dfu_status.bState=dfuDNLOAD_IDLE;
                    dfu_status.bwPollTimeout0 = 0x00;
                }
                else
                {
                	dfu_status.bState=dfuDNBUSY;
                    dfu_status.bwPollTimeout0 = 0x3F;
                }
            }
            else if( dfu_status.bState == dfuDNBUSY )
            {
                if( need_to_write == 0)
                    dfu_status.bState=dfuDNLOAD_SYNC;
            }
            else if( dfu_status.bState == dfuMANIFEST_SYNC)
            {
            	dfu_status.bState=dfuMANIFEST;
                dfu_status.bwPollTimeout0 = 0xFF;
                hw_state_indicator( HW_STATE_DONE );
            }
            else if( dfu_status.bState == dfuMANIFEST &&
                     need_to_write == 0)
            {
                // Finish erasing flash
                while( flash_target < SI32_MCU_FLASH_SIZE)
                {
                    if( 0 != hw_flash_erase( flash_target, 1 ) )
                    {
                        dfu_status.bState  = dfuERROR;
                        dfu_status.bStatus = errERASE;
                        hw_state_indicator( HW_STATE_ERROR );
                    }
                    flash_target += BLOCK_SIZE_U8;
                }
                dfu_status.bState=dfuMANIFEST_WAIT_RESET;
            }

            for (i=0; i<STATUS_SZ; i++)
            {
                usb_buf_write(EP_CTRL, *((U8 *)&dfu_status + i));
            }
            ep_write(EP_CTRL);

            if( dfu_status.bState == dfuMANIFEST_WAIT_RESET )
            {
                hw_wait_ms(200);
                hw_boot_image( 1 );
            }

            if( need_to_write )
            {
                if( 0 != hw_flash_erase( flash_target, 1 ) )
                {
                    dfu_status.bState  = dfuERROR;
                    dfu_status.bStatus = errERASE;
                    hw_state_indicator( HW_STATE_ERROR );
                }
                if( 0 != hw_flash_write( flash_target, ( U32* )flash_buffer, flash_buffer_ptr - flash_buffer, 1 ) )
                {
                    dfu_status.bState  = dfuERROR;
                    dfu_status.bStatus = errVERIFY;
                    hw_state_indicator( HW_STATE_ERROR );
                }
                flash_buffer_ptr = flash_buffer;
                flash_target += BLOCK_SIZE_U8;
                need_to_write = 0;
                if( dfu_status.bState != dfuMANIFEST )
                    dfu_status.bState=dfuDNLOAD_SYNC;
            }
        }
        break;

    case DFU_CLRSTATUS:
        if (req->type & (HOST_TO_DEVICE | TYPE_CLASS | RECIPIENT_INTF))
        {
            // wvalue is zero
            // wlength is 0
            // data is  none
            if( dfu_status.bState == dfuERROR )
            {
                dfu_status.bStatus = OK;
                dfu_status.bState = dfuIDLE;
                hw_state_indicator( HW_STATE_ERROR_CLR );
            }
        }
        break;

    case DFU_GETSTATE:
        if (req->type & (DEVICE_TO_HOST | TYPE_CLASS | RECIPIENT_INTF))
        {
            // wvalue is zero
            // wlength is 1
            // data is  state
            // Transition?: No State Transition
            usb_buf_write( EP_CTRL, dfu_status.bState );
            ep_write(EP_CTRL);
        }
        break;

    case DFU_ABORT:
        if (req->type & (HOST_TO_DEVICE | TYPE_CLASS | RECIPIENT_INTF))
        {
            // wvalue is zero
            // wlength is 0
            // data is none
            if( dfu_status.bState == dfuIDLE )
            {
                dfu_status.bStatus = OK;
                dfu_status.bState = dfuIDLE;
            }
            else if ( dfu_status.bState == dfuDNLOAD_IDLE )
            {
                flash_target = FLASH_TARGET;
                if( 0 != hw_flash_erase( flash_target, 1 ) )
                {
                    dfu_status.bState  = dfuERROR;
                    dfu_status.bStatus = errERASE;
                    hw_state_indicator( HW_STATE_ERROR );
                }
                dfu_status.bStatus = OK;
                dfu_status.bState = dfuIDLE;
                ep_send_zlp(EP_CTRL);
            }
        }
        break;

    default:
        ep_set_stall(EP_CTRL);
        break;
    }
}