/******************************************************************************* * Function Name : Read_Memory * Description : Handle the Read operation from the microSD card. * Input : None. * Output : None. * Return : None. *******************************************************************************/ void Read_Memory(uint8_t lun, uint32_t Memory_Offset, uint32_t Transfer_Length) { static uint32_t Offset, Length; if (TransferState == TXFR_IDLE ) { Offset = Memory_Offset * Mass_Block_Size[lun]; Length = Transfer_Length * Mass_Block_Size[lun]; TransferState = TXFR_ONGOING; } if (TransferState == TXFR_ONGOING ) { if (!Block_Read_count) { MAL_Read(lun , Offset , Data_Buffer, Mass_Block_Size[lun]); USB_SIL_Write(EP1_IN, (uint8_t *)Data_Buffer, BULK_MAX_PACKET_SIZE); Block_Read_count = Mass_Block_Size[lun] - BULK_MAX_PACKET_SIZE; Block_offset = BULK_MAX_PACKET_SIZE; } else { USB_SIL_Write(EP1_IN, (uint8_t *)Data_Buffer + Block_offset, BULK_MAX_PACKET_SIZE); Block_Read_count -= BULK_MAX_PACKET_SIZE; Block_offset += BULK_MAX_PACKET_SIZE; } SetEPTxCount(ENDP1, BULK_MAX_PACKET_SIZE); #ifndef USE_STM3210C_EVAL SetEPTxStatus(ENDP1, EP_TX_VALID); #endif Offset += BULK_MAX_PACKET_SIZE; Length -= BULK_MAX_PACKET_SIZE; CSW.dDataResidue -= BULK_MAX_PACKET_SIZE; Led_RW_ON(); } if (Length == 0) { Block_Read_count = 0; Block_offset = 0; Offset = 0; Bot_State = BOT_DATA_IN_LAST; TransferState = TXFR_IDLE; Led_RW_OFF(); } }
/******************************************************************************* * Function Name : Read_Memory * Description : Handle the Read operation from the microSD card. * Input : None. * Output : None. * Return : None. *******************************************************************************/ void Read_Memory(u8 lun, u32 Memory_Offset, u32 Transfer_Length) { static u32 Offset, Length; if (TransferState == TXFR_IDLE ) { Block_Read_count = 0; //20120830 joeÔö¼ÓµÄ Offset = Memory_Offset * Mass_Block_Size[lun]; Length = Transfer_Length * Mass_Block_Size[lun]; TransferState = TXFR_ONGOING; } if (TransferState == TXFR_ONGOING ) { if (!Block_Read_count) { MAL_Read(lun , Offset , Data_Buffer, Mass_Block_Size[lun]); USB_SIL_Write(EP1_IN, (u8 *)Data_Buffer, BULK_MAX_PACKET_SIZE); Block_Read_count = Mass_Block_Size[lun] - BULK_MAX_PACKET_SIZE; Block_offset = BULK_MAX_PACKET_SIZE; } else { USB_SIL_Write(EP1_IN, (u8 *)Data_Buffer + Block_offset, BULK_MAX_PACKET_SIZE); Block_Read_count -= BULK_MAX_PACKET_SIZE; Block_offset += BULK_MAX_PACKET_SIZE; } SetEPTxCount(ENDP1, BULK_MAX_PACKET_SIZE); SetEPTxStatus(ENDP1, EP_TX_VALID); Offset += BULK_MAX_PACKET_SIZE; Length -= BULK_MAX_PACKET_SIZE; CSW.dDataResidue -= BULK_MAX_PACKET_SIZE; //Led_RW_ON(); } if (Length == 0) { Block_Read_count = 0; Block_offset = 0; Offset = 0; Bot_State = BOT_DATA_IN_LAST; TransferState = TXFR_IDLE; //Led_RW_OFF(); } }
/******************************************************************************* * Function Name : Read_Memory * Description : Handle the Read operation from the microSD card. * Input : None. * Output : None. * Return : None. *******************************************************************************/ void Read_Memory(uint8_t lun, uint32_t Memory_Offset, uint32_t Transfer_Length) { static uint64_t Offset, Length;//要支持大于4G的SD卡,必须使用long long(64位)数据类型 u8 STA;//新增一个定义 if (TransferState == TXFR_IDLE ) { Offset = (long long)Memory_Offset * Mass_Block_Size[lun]; Length = (long long)Transfer_Length * Mass_Block_Size[lun]; TransferState = TXFR_ONGOING; } if (TransferState == TXFR_ONGOING ) { if (!Block_Read_count) { STA=MAL_Read(lun ,Offset,Data_Buffer,Mass_Block_Size[lun]); if(STA)USB_STATUS_REG|=0X08;//SD卡读错误! USB_SIL_Write(EP1_IN, (uint8_t *)Data_Buffer, BULK_MAX_PACKET_SIZE); Block_Read_count = Mass_Block_Size[lun] - BULK_MAX_PACKET_SIZE; Block_offset = BULK_MAX_PACKET_SIZE; }else { USB_SIL_Write(EP1_IN, (uint8_t *)Data_Buffer + Block_offset, BULK_MAX_PACKET_SIZE); Block_Read_count -= BULK_MAX_PACKET_SIZE; Block_offset += BULK_MAX_PACKET_SIZE; } SetEPTxCount(ENDP1, BULK_MAX_PACKET_SIZE); SetEPTxStatus(ENDP1, EP_TX_VALID); Offset += BULK_MAX_PACKET_SIZE; Length -= BULK_MAX_PACKET_SIZE; CSW.dDataResidue -= BULK_MAX_PACKET_SIZE; Led_RW_ON(); } if (Length == 0) { Block_Read_count = 0; Block_offset = 0; Offset = 0; Bot_State = BOT_DATA_IN_LAST; TransferState = TXFR_IDLE; Led_RW_OFF(); } }
// write to USB void EP1_IN_Callback (void) { //uint16_t USB_Tx_ptr; uint16_t USB_TX_Length; if (USB_Tx_State == 1) { if(BUFFER_IS_EMPTY(USB_TX)) { USB_Tx_State = 0; } else { USB_TX_Length = BUFFER_CONTIGUOUS_DATA_LENGTH(USB_TX); if (USB_TX_Length > VIRTUAL_COM_PORT_DATA_SIZE) USB_TX_Length = VIRTUAL_COM_PORT_DATA_SIZE; #ifdef USE_STM3210C_EVAL USB_SIL_Write(EP1_IN, &USART_Rx_Buffer[USB_Tx_ptr], USB_TX_Length); #else UserToPMABufferCopy(USB_TX_Head, ENDP1_TXADDR, USB_TX_Length); BUFFER_MOVE_HEAD(USB_TX, USB_TX_Length); SetEPTxCount(ENDP1, USB_TX_Length); SetEPTxValid(ENDP1); #endif } } }
void sample_packet_send(void) { USB_SIL_Write(EP1_IN, packet_buf, SAMPLE_PACKET_SIZE); // Mark that the endpoint has valid data SetEPTxValid(ENDP1); }
/******************************************************************************* * Function Name : Handle_USBAsynchXfer. * Description : send data to USB. * Input : None. * Return : none. *******************************************************************************/ void Handle_USBAsynchXfer (void) { if(USB_Tx_State != 1) { unsigned char USB_TX_Buffer[VIRTUAL_COM_PORT_DATA_SIZE]; int USB_Tx_length = 0; // try and fill the buffer int c; while (USB_Tx_length<VIRTUAL_COM_PORT_DATA_SIZE && ((c = jshGetCharToTransmit(EV_USBSERIAL)) >=0) ) { // get byte to transmit USB_TX_Buffer[USB_Tx_length++] = c; } // if nothing, set state to 0 if (USB_Tx_length==0) { USB_Tx_State = 0; return; } USB_Tx_State = 1; #ifdef USE_STM3210C_EVAL USB_SIL_Write(EP1_IN, &USB_TX_Buffer[0], USB_Tx_length); #else UserToPMABufferCopy(&USB_TX_Buffer[0], ENDP1_TXADDR, USB_Tx_length); SetEPTxCount(ENDP1, USB_Tx_length); SetEPTxValid(ENDP1); #endif /* USE_STM3210C_EVAL */ } }
/******************************************************************************* * Function Name : Read_Memory * Description : Handle the Read operation from the microSD card. * Input : None. * Output : None. * Return : None. *******************************************************************************/ void Read_Memory(uint8_t lun, uint32_t Memory_Offset, uint32_t Transfer_Length) { if (TransferState == TXFR_IDLE ) { Block_offset = Memory_Offset * Mass_Block_Size[lun]; Counter = Transfer_Length * Mass_Block_Size[lun]; TransferState = TXFR_ONGOING; } if (TransferState == TXFR_ONGOING ) { MAL_Read(lun, Block_offset, (uint32_t *)Bulk_Data_Buff, BULK_MAX_PACKET_SIZE); USB_SIL_Write(EP1_IN, Bulk_Data_Buff, BULK_MAX_PACKET_SIZE); Block_offset += BULK_MAX_PACKET_SIZE; Counter -= BULK_MAX_PACKET_SIZE; SetEPTxCount(ENDP1, BULK_MAX_PACKET_SIZE); SetEPTxStatus(ENDP1, EP_TX_VALID); CSW.dDataResidue -= BULK_MAX_PACKET_SIZE; } if (Counter == 0) { Block_offset = 0; Bot_State = BOT_DATA_IN_LAST; TransferState = TXFR_IDLE; } }
/******************************************************************************* * Function Name : Transfer_Data_Request * Description : Send the request response to the PC HOST. * Input : u8* Data_Address : point to the data to transfer. * u16 Data_Length : the nember of Bytes to transfer. * Output : None. * Return : None. *******************************************************************************/ void Transfer_Data_Request(u8* Data_Pointer, u16 Data_Len) { USB_SIL_Write(EP1_IN, Data_Pointer, Data_Len); SetEPTxStatus(ENDP1, EP_TX_VALID); Bot_State = BOT_DATA_IN_LAST; CSW.dDataResidue -= Data_Len; CSW.bStatus = CSW_CMD_PASSED; }
void usb_send_data(const void* buffer, uint32_t len) { memcpy(send_buffer,buffer, len < EP1_PACKET_SIZE ? len : EP1_PACKET_SIZE); USB_SIL_Write(EP1_IN, (uint8_t*) send_buffer, EP1_PACKET_SIZE); #ifndef STM32F10X_CL SetEPTxValid(ENDP1); #endif /* STM32F10X_CL */ PrevXferComplete = 0; }
/** * @brief Main program. * @param None * @retval None */ int main(void) { uint8_t i = 0; /* SysTick end of count event each 10ms */ RCC_GetClocksFreq(&RCC_Clocks); SysTick_Config(RCC_Clocks.HCLK_Frequency / 100); /* Configure the USB */ USB_Config(); /* Accelerometer Configuration */ Acc_Config(); /* Init push buttons */ STM_EVAL_PBInit(BUTTON_USER, BUTTON_MODE_GPIO); /* Init LEDs */ STM_EVAL_LEDInit(LED3); /* Infinite loop */ while (1) { /* Wait for data ready */ while (DataReady != 0x04) { } DataReady = 0x00; /* Get Data Accelerometer */ Acc_ReadData(AccBuffer); for (i = 0; i < 3; i++) AccBuffer[i] /= 100.0f; // Get mouse info Mouse_Buffer = USBD_HID_GetPos(); /* Update the cursor position */ if (!mouseZeroPacketOutputted || (Mouse_Buffer[0] != 0 || Mouse_Buffer[1] != 0 || Mouse_Buffer[2] != 0 || Mouse_Buffer[3] != 0)) { /* Reset the control token to inform upper layer that a transfer is ongoing */ PrevXferComplete = 0; /* Copy mouse position info in ENDP1 Tx Packet Memory Area*/ USB_SIL_Write(EP1_IN, Mouse_Buffer, 4); /* Enable endpoint for transmission */ SetEPTxValid(ENDP1 ); } // We find that the mouse buffer is zero so we know that we have outputted the zero packet // This means we can stop transferring data if((Mouse_Buffer[0] == 0 && Mouse_Buffer[1] == 0 && Mouse_Buffer[2] == 0 && Mouse_Buffer[3] == 0)) mouseZeroPacketOutputted = 1; else mouseZeroPacketOutputted = 0; } }
/******************************************************************************* * Function Name : Handle_USBAsynchXfer. * Description : send data to USB. * Input : None. * Return : none. *******************************************************************************/ void Handle_USBAsynchXfer (void) { uint16_t USB_Tx_ptr; uint16_t USB_Tx_length; if(USB_Tx_State != 1) { if (USART_Rx_ptr_out == USART_RX_DATA_SIZE) { USART_Rx_ptr_out = 0; } if(USART_Rx_ptr_out == USART_Rx_ptr_in) { USB_Tx_State = 0; return; } if(USART_Rx_ptr_out > USART_Rx_ptr_in) /* rollback */ { USART_Rx_length = USART_RX_DATA_SIZE - USART_Rx_ptr_out; } else { USART_Rx_length = USART_Rx_ptr_in - USART_Rx_ptr_out; } if (USART_Rx_length > VIRTUAL_COM_PORT_DATA_SIZE) { USB_Tx_ptr = USART_Rx_ptr_out; USB_Tx_length = VIRTUAL_COM_PORT_DATA_SIZE; USART_Rx_ptr_out += VIRTUAL_COM_PORT_DATA_SIZE; USART_Rx_length -= VIRTUAL_COM_PORT_DATA_SIZE; } else { USB_Tx_ptr = USART_Rx_ptr_out; USB_Tx_length = USART_Rx_length; USART_Rx_ptr_out += USART_Rx_length; USART_Rx_length = 0; } USB_Tx_State = 1; #ifdef USE_STM3210C_EVAL USB_SIL_Write(EP1_IN, &USART_Rx_Buffer[USB_Tx_ptr], USB_Tx_length); #else UserToPMABufferCopy(&USART_Rx_Buffer[USB_Tx_ptr], ENDP1_TXADDR, USB_Tx_length); SetEPTxCount(ENDP1, USB_Tx_length); SetEPTxValid(ENDP1); #endif /* USE_STM3210C_EVAL */ } }
/******************************************************************************* * Function Name : Transfer_Data_Request * Description : Send the request response to the PC HOST. * Input : uint8_t* Data_Address : point to the data to transfer. * uint16_t Data_Length : the nember of Bytes to transfer. * Output : None. * Return : None. *******************************************************************************/ void Transfer_Data_Request(uint8_t* Data_Pointer, uint16_t Data_Len) { USB_SIL_Write(EP1_IN, Data_Pointer, Data_Len); #ifndef USE_STM3210C_EVAL SetEPTxStatus(ENDP1, EP_TX_VALID); #endif Bot_State = BOT_DATA_IN_LAST; CSW.dDataResidue -= Data_Len; CSW.bStatus = CSW_CMD_PASSED; }
/******************************************************************************* * Function Name : EP1_IN_Callback * Description : * Input : None. * Output : None. * Return : None. *******************************************************************************/ void EP1_IN_Callback (void) { uint16_t USB_Tx_ptr; uint16_t USB_Tx_length; if (USB_Tx_State == 1) { if (USART_Rx_length == 0) { USB_Tx_State = 0; } else { if (USART_Rx_length > VIRTUAL_COM_PORT_DATA_SIZE){ USB_Tx_ptr = USART_Rx_ptr_out; USB_Tx_length = VIRTUAL_COM_PORT_DATA_SIZE; USART_Rx_ptr_out += VIRTUAL_COM_PORT_DATA_SIZE; USART_Rx_length -= VIRTUAL_COM_PORT_DATA_SIZE; } else { USB_Tx_ptr = USART_Rx_ptr_out; USB_Tx_length = USART_Rx_length; USART_Rx_ptr_out += USART_Rx_length; USART_Rx_length = 0; } #ifdef USE_STM3210C_EVAL USB_SIL_Write(EP1_IN, &USART_Rx_Buffer[USB_Tx_ptr], USB_Tx_length); #else USB_SIL_Write(EP1_IN, &USART_Rx_Buffer[USB_Tx_ptr], USB_Tx_length); //UserToPMABufferCopy(&USART_Rx_Buffer[USB_Tx_ptr], ENDP1_TXADDR, USB_Tx_length); SetEPTxCount(ENDP1, USB_Tx_length); SetEPTxValid(ENDP1); #endif } } }
/******************************************************************************* * Function Name : EP1_IN_Callback * Description : When data send to Host PC by USB port, this function is called automatically * Input : None. * Output : None. * Return : None. *******************************************************************************/ void EP1_IN_Callback (void) { #ifdef STM32F10X_CL USB_SIL_Write(EP1_IN, &USART_Rx_Buffer[USB_Tx_ptr], USB_Tx_length); #else //UserToPMABufferCopy(&USART_Rx_Buffer[USB_Tx_ptr], ENDP1_TXADDR, USB_Tx_length); // SetEPTxCount(ENDP1, USB_Tx_length); // SetEPTxValid(ENDP1); #endif }
/******************************************************************************* * Function Name : Set_CSW * Description : Set the SCW with the needed fields. * Input : uint8_t CSW_Status this filed can be CSW_CMD_PASSED,CSW_CMD_FAILED, * or CSW_PHASE_ERROR. * Output : None. * Return : None. *******************************************************************************/ void Set_CSW (uint8_t CSW_Status, uint8_t Send_Permission) { CSW.dSignature = BOT_CSW_SIGNATURE; CSW.bStatus = CSW_Status; USB_SIL_Write(EP1_IN, ((uint8_t *)& CSW), CSW_DATA_LENGTH); Bot_State = BOT_ERROR; if (Send_Permission) { Bot_State = BOT_CSW_Send; SetEPTxStatus(ENDP1, EP_TX_VALID); } }
int main(void) { uint32_t count = 0; NVIC_InitTypeDef NVIC_InitStructure; /* System Clocks Configuration */ RCC_Configuration(); /* NVIC configuration */ NVIC_Configuration(); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* Configure the GPIO ports */ GPIO_Configuration(); USB_Init(); while (1) { if ((count_out != 0) && (bDeviceState == CONFIGURED)) { uint8_t i; bli(); // USB_SIL_Write(EP1_IN, buffer_out, count_out); for (i = 0; i<count_out; i++) { buffer_in[(count_in+i)%64] = buffer_out[i]; } count_in += count_out; count_out = 0; count = 0; } count++; count%=10000000/2; if (count == 0 && count_in > 0) { bli(); USB_SIL_Write(EP1_IN, buffer_in, count_in); SetEPTxValid(ENDP1); } } while (1); }
/* write a full block or less out EndPoint1 every callback */ void EP1_IN_Callback(void) { static uint8_t buffer[VIRTUAL_COM_PORT_DATA_SIZE]; unsigned int bytes = GetDataFromRing(&tx_ring, VIRTUAL_COM_PORT_DATA_SIZE, buffer); if (bytes) { USB_SIL_Write(EP1_IN, buffer, bytes); #ifndef STM32F10X_CL SetEPTxValid(ENDP1); #endif /* STM32F10X_CL */ } else { write_ready = 1; } }
static char* parse_command(char *cmd) { int response_size; int pos = 0; do { char *next = (char*)strchr(cmd, '\n'); if (next) next[0] = 0; if (NULL == cmd[0]) break; response_size = parse_command_line(cmd, response); response[response_size] = NULL; /* Write the data to the USB endpoint */ //printf("cmd:%s, response: %s(%d byte)\n", cmd, response, response_size); if (response_size > 128) { //response[0] = '\n'; //response_size = 1; //printf("oops(avg=%d):%s\n", avg_count, response); } while(pos < response_size) { USB_SIL_Write(EP1_IN, (uint8_t*)response + pos, min(response_size - pos, tx_packet_size)); pos += tx_packet_size; #ifndef STM32F10X_CL SetEPTxValid(ENDP1); #endif /* STM32F10X_CL */ while (GetEPTxStatus(ENDP1) == EP_TX_VALID); } if (!next) break; cmd = next+1; }while (1); return cmd; }
/******************************************************************************* * Function Name : Set_CSW * Description : Set the SCW with the needed fields. * Input : uint8_t CSW_Status this filed can be CSW_CMD_PASSED,CSW_CMD_FAILED, * or CSW_PHASE_ERROR. * Output : None. * Return : None. *******************************************************************************/ void Set_CSW (uint8_t CSW_Status, uint8_t Send_Permission) { CSW.dSignature = BOT_CSW_SIGNATURE; CSW.bStatus = CSW_Status; USB_SIL_Write(EP4_IN, ((uint8_t *)& CSW), CSW_DATA_LENGTH); Bot_State = BOT_ERROR; if (Send_Permission) { Bot_State = BOT_CSW_Send; #ifndef USE_STM3210C_EVAL SetEPTxStatus(ENDP4, EP_TX_VALID); #endif } }
/** * @brief Main program. * @param None * @retval None */ int main(void) { uint8_t i = 0; /* SysTick end of count event each 10ms */ RCC_GetClocksFreq(&RCC_Clocks); SysTick_Config(RCC_Clocks.HCLK_Frequency / 100); /* Configure the USB */ USB_Config(); /* Accelerometer Configuration */ Acc_Config(); /* Infinite loop */ while (1) { /* Wait for data ready */ while(DataReady !=0x02) {} DataReady = 0x00; /* Get Data Accelerometer */ Acc_ReadData(AccBuffer); for(i=0;i<3;i++) AccBuffer[i] /= 100.0f; /* Get position */ Mouse_Buffer = USBD_HID_GetPos(); /* Update the cursor position */ if((Mouse_Buffer[1] != 0) ||(Mouse_Buffer[2] != 0)) { /* Reset the control token to inform upper layer that a transfer is ongoing */ PrevXferComplete = 0; /* Copy mouse position info in ENDP1 Tx Packet Memory Area*/ USB_SIL_Write(EP1_IN, Mouse_Buffer, 4); /* Enable endpoint for transmission */ SetEPTxValid(ENDP1); } } }
/******************************************************************************* * Function Name : USB_To_USART_Send_Data. * Description : send the received data from USB to the UART 0. * Input : data_buffer: data address. Nb_bytes: number of bytes to send. * Return : none. *******************************************************************************/ void USB_To_USART_Send_Data(uint8_t* data_buffer, uint8_t Nb_bytes) { uint32_t i; /** * send it back via. usb * * for (i = 0; i < Nb_bytes; i++) { USART_SendData(EVAL_COM1, *(data_buffer + i)); while(USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TXE) == RESET); } */ for (i = 0; i < Nb_bytes; i++) { mybuffer_out[i] = *(data_buffer + i) << 1; } /* Write the data to the USB endpoint */ USB_SIL_Write(EP1_IN, mybuffer_out, Nb_bytes); SetEPTxValid(ENDP1); }
void dfu_main(void) { int send_report; uint8_t kbd_report_loc[KBD_SIZE]; #if defined (USE_STM32L152D_EVAL) FLASH_Unlock(); FLASH_ClearFlag(FLASH_FLAG_OPTVERRUSR); #endif Pointer = (uint32_t)&_application_start; DeviceState = STATE_dfuERROR; DeviceStatus[0] = STATUS_ERRFIRMWARE; DeviceStatus[4] = DeviceState; usb_cable_reset(); Set_System(); matrix_init_all(); led_init_all(); matrix_scan_init(); leds_override = LEDS_OVERRIDE_STARTUP; usb_report_sent = 1; Set_USBClock(); USB_Init(); matrix_scan_start(); /* Main loop */ send_report = 0; while (1){ if (unlikely(do_usb_resume)){ /* Check if the remote wakeup feature is enabled (it could be disabled * by the host through ClearFeature request) */ if (bDeviceState != SUSPENDED) { /* do nothing */ do_usb_resume = 0; } else if (usb_remote_wakeup_enabled()) { Resume(RESUME_INTERNAL); do_usb_resume = 0; } else { /* resume disabled, disable kbd-triggered wakeup and go to the suspend state */ matrix_idle_wakeup_event_leave(); /* clear flag before suspend * because the flag can be set again on resume */ do_usb_resume = 0; Suspend_Low(); } continue; } if (bDeviceState != CONFIGURED){ __WFI(); /* Wait for interrupt */ continue; } if (unlikely(leds_override)){ handle_leds_override(); } __disable_irq(); if ((kbd_report_status & REPORT_STATUS_DONE) && usb_report_sent){ kbd_report_status &= ~REPORT_STATUS_DONE; send_report = 1; memcpy((void*)kbd_report_loc, (void*)kbd_report_fullscan, KBD_SIZE); } if (unlikely((usb_report_idle_timer == 0) && (usb_report_idle != 0))){ send_report = 1; usb_report_idle_timer = usb_report_idle; } __enable_irq(); if (send_report){ usb_report_sent = 0; send_report = 0; USB_SIL_Write(EP1_IN, kbd_report_loc, KBD_SIZE); SetEPTxCount(ENDP1, KBD_SIZE); SetEPTxValid(ENDP1); } __WFI(); /* Wait for interrupt */ } }
void USB_Hanlder(void) { uint8_t i = 0; uint8_t i2; uint8_t buf_addr; uint8_t buf_copy_len; if(usb_struct.state & E_USB_ENDPOIN_RX_COMPLETE) { switch(usb_struct.buf[1]) { /* Get status */ case USB_COMMAND_GET_STATUS: usb_struct.buf[i++] = USB_REPORT_ID_2; usb_struct.buf[i++] = USB_COMMAND_GET_STATUS; usb_struct.buf[i++] = USB_COMMAND_TYPE_BUF_SHORT; usb_struct.buf[i++] = ((VERSION_RF & 0xFF00)>>8); usb_struct.buf[i++] = VERSION_RF & 0xFF; usb_struct.buf[i++] = ((main_struct.id & 0xFF00)>>8); usb_struct.buf[i++] = (main_struct.id & 0xFF); usb_struct.buf[i++] = rf_struct.state; usb_struct.buf[i++] = rf_struct.power; /* CLear state reg */ rf_struct.state &=~ (E_RF_RECEIVE_COMPLETE | E_RF_TRANSMITE_COMPLETE); /* Write USB buffer */ USB_SIL_Write(EP1_IN, (uint8_t*)&usb_struct.buf, USB_REPORT_SIZE); break; /* Read RF buffer */ case USB_COMMAND_READ_RF_BUF: buf_addr = usb_struct.buf[4]; buf_copy_len = usb_struct.buf[5]; i = 0; usb_struct.buf[i++] = USB_REPORT_ID_2; usb_struct.buf[i++] = USB_COMMAND_READ_RF_BUF; usb_struct.buf[i++] = USB_COMMAND_TYPE_BUF_MAXIMUM; usb_struct.buf[i++] = (rf_struct.rssi & 0xFF); usb_struct.buf[i++] = ((rf_struct.rssi & 0xFF00)>>8); usb_struct.buf[i++] = ((rf_struct.rssi & 0xFF0000)>>16); usb_struct.buf[i++] = ((rf_struct.rssi & 0xFF000000)>>24); for(i2=0; i2<buf_copy_len; i2++) { usb_struct.buf[i++] = rf_struct.buf[buf_addr]; buf_addr++; } /* Write USB buffer */ USB_SIL_Write(EP1_IN, (uint8_t*)&usb_struct.buf, USB_REPORT_SIZE); /* Clear RF state */ rf_struct.state &=~ E_RF_RECEIVE_COMPLETE; break; /* Write RF Power */ case USB_COMMAND_WRITE_RF_POWER: usb_struct.buf[i++] = USB_COMMAND_WRITE_RF_POWER; usb_struct.buf[i++] = USB_COMMAND_TYPE_BUF_SHORT; rf_struct.power = usb_struct.buf[i++]; RF_Init(); USB_SIL_Write(EP1_IN, (uint8_t*)&usb_struct.buf, USB_REPORT_SIZE); break; case USB_COMMAND_WRITE_RF_PACKET: i = 8; rf_struct.length = usb_struct.buf[5]; buf_addr = usb_struct.buf[6]; buf_copy_len = usb_struct.buf[7]; if(buf_copy_len >0) /* copy rf buffer */ if(buf_copy_len < sizeof(rf_struct.buf)) memcpy(&rf_struct.buf[buf_addr], &usb_struct.buf[i], buf_copy_len); /* send packet, disable interrupt rx */ NVIC_DisableIRQ(EXTI0_IRQn); if(!buf_copy_len) { RF_Send_Packet((uint8_t*)&rf_struct.buf, rf_struct.length); if(main_struct.buzzer_state == 0) Buzzer_Start(); } /* Write USB buffer */ i = 0; usb_struct.buf[i++] = USB_REPORT_ID_2; usb_struct.buf[i++] = USB_COMMAND_WRITE_RF_POWER; usb_struct.buf[i++] = USB_COMMAND_TYPE_BUF_SHORT; USB_SIL_Write(EP1_IN, (uint8_t*)&usb_struct.buf, USB_REPORT_SIZE); break; case USB_COMMAND_WRITE_RF_SETTINGS: usb_struct.buf[i++] = USB_REPORT_ID_2; usb_struct.buf[i++] = USB_COMMAND_WRITE_RF_SETTINGS; usb_struct.buf[i++] = USB_COMMAND_TYPE_BUF_SHORT; Rf_Freq_Struct.freq0 = usb_struct.buf[i++]; Rf_Freq_Struct.freq1 = usb_struct.buf[i++]; Rf_Freq_Struct.freq2 = usb_struct.buf[i++]; Rf_Freq_Struct.channel = usb_struct.buf[i++]; USB_SIL_Write(EP1_IN, (uint8_t*)&usb_struct.buf, USB_REPORT_SIZE); /* Deinit RF */ RF_Init(); break; case USB_COMMAND_WRITE_BUZZER_STATE: usb_struct.buf[i++] = USB_REPORT_ID_2; usb_struct.buf[i++] = USB_COMMAND_WRITE_BUZZER_STATE; usb_struct.buf[i++] = USB_COMMAND_TYPE_BUF_SHORT; if(usb_struct.buf[i++] == 0xff) main_struct.buzzer_state = 0xff; else main_struct.buzzer_state = 0; USB_SIL_Write(EP1_IN, (uint8_t*)&usb_struct.buf, USB_REPORT_SIZE); RF_Init(); break; } Delay_ms(100); SetEPTxValid(ENDP1); } /* Clear state */ usb_struct.state = 0; }