mirror of
https://bitbucket.org/librepilot/librepilot.git
synced 2025-01-18 03:52:11 +01:00
Flight - #ifdefs to make it work with both normal and bootloader packet formats.
git-svn-id: svn://svn.openpilot.org/OpenPilot/trunk@1609 ebee16cc-31ac-478f-84a7-5cbb03baadba
This commit is contained in:
parent
41a23a8eeb
commit
66560786f4
@ -41,10 +41,10 @@
|
||||
#if defined(PIOS_INCLUDE_USB_HID)
|
||||
|
||||
const struct pios_com_driver pios_usb_com_driver = {
|
||||
.tx_nb = PIOS_USB_HID_TxBufferPutMoreNonBlocking,
|
||||
.tx = PIOS_USB_HID_TxBufferPutMore,
|
||||
.rx = PIOS_USB_HID_RxBufferGet,
|
||||
.rx_avail = PIOS_USB_HID_RxBufferUsed,
|
||||
.tx_nb = PIOS_USB_HID_TxBufferPutMoreNonBlocking,
|
||||
.tx = PIOS_USB_HID_TxBufferPutMore,
|
||||
.rx = PIOS_USB_HID_RxBufferGet,
|
||||
.rx_avail = PIOS_USB_HID_RxBufferUsed,
|
||||
};
|
||||
|
||||
// TODO: Eventually replace the transmit and receive buffers with bigger ring bufers
|
||||
@ -64,11 +64,11 @@ cBuffer txBuffer;
|
||||
static uint8_t rxBufferSpace[TX_BUFFER_SIZE];
|
||||
static uint8_t txBufferSpace[RX_BUFFER_SIZE];
|
||||
/**
|
||||
* Initialises USB COM layer
|
||||
* \param[in] mode currently only mode 0 supported
|
||||
* \return < 0 if initialisation failed
|
||||
* \note Applications shouldn't call this function directly, instead please use \ref PIOS_COM layer functions
|
||||
*/
|
||||
* Initialises USB COM layer
|
||||
* \param[in] mode currently only mode 0 supported
|
||||
* \return < 0 if initialisation failed
|
||||
* \note Applications shouldn't call this function directly, instead please use \ref PIOS_COM layer functions
|
||||
*/
|
||||
int32_t PIOS_USB_HID_Init(uint32_t mode)
|
||||
{
|
||||
/* Currently only mode 0 supported */
|
||||
@ -76,46 +76,46 @@ int32_t PIOS_USB_HID_Init(uint32_t mode)
|
||||
/* Unsupported mode */
|
||||
return -1;
|
||||
}
|
||||
|
||||
bufferInit(&rxBuffer, &rxBufferSpace[0], RX_BUFFER_SIZE);
|
||||
bufferInit(&txBuffer, &txBufferSpace[0], TX_BUFFER_SIZE);
|
||||
|
||||
PIOS_USB_HID_Reenumerate();
|
||||
|
||||
|
||||
bufferInit(&rxBuffer, &rxBufferSpace[0], RX_BUFFER_SIZE);
|
||||
bufferInit(&txBuffer, &txBufferSpace[0], TX_BUFFER_SIZE);
|
||||
|
||||
PIOS_USB_HID_Reenumerate();
|
||||
|
||||
/* Enable the USB Interrupts */
|
||||
/* 2 bit for pre-emption priority, 2 bits for subpriority */
|
||||
NVIC_InitTypeDef NVIC_InitStructure;
|
||||
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
|
||||
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
|
||||
NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
|
||||
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
|
||||
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
|
||||
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
||||
NVIC_Init(&NVIC_InitStructure);
|
||||
|
||||
|
||||
/* Select USBCLK source */
|
||||
RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);
|
||||
RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);
|
||||
/* Enable the USB clock */
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE);
|
||||
|
||||
/* Update the USB serial number from the chip */
|
||||
uint8_t sn[25];
|
||||
PIOS_SYS_SerialNumberGet((char *) sn);
|
||||
for(uint8_t i = 0; sn[i] != '\0' && (2 * i) < PIOS_HID_StringSerial[0]; i ++) {
|
||||
PIOS_HID_StringSerial[2+2*i] = sn[i];
|
||||
}
|
||||
|
||||
|
||||
/* Update the USB serial number from the chip */
|
||||
uint8_t sn[25];
|
||||
PIOS_SYS_SerialNumberGet((char *) sn);
|
||||
for(uint8_t i = 0; sn[i] != '\0' && (2 * i) < PIOS_HID_StringSerial[0]; i ++) {
|
||||
PIOS_HID_StringSerial[2+2*i] = sn[i];
|
||||
}
|
||||
|
||||
USB_Init();
|
||||
USB_SIL_Init();
|
||||
|
||||
|
||||
return 0; /* No error */
|
||||
}
|
||||
|
||||
/**
|
||||
* This function is called by the USB driver on cable connection/disconnection
|
||||
* \param[in] connected connection status (1 if connected)
|
||||
* \return < 0 on errors
|
||||
* \note Applications shouldn't call this function directly, instead please use \ref PIOS_COM layer functions
|
||||
*/
|
||||
* This function is called by the USB driver on cable connection/disconnection
|
||||
* \param[in] connected connection status (1 if connected)
|
||||
* \return < 0 on errors
|
||||
* \note Applications shouldn't call this function directly, instead please use \ref PIOS_COM layer functions
|
||||
*/
|
||||
int32_t PIOS_USB_HID_ChangeConnectionState(uint32_t Connected)
|
||||
{
|
||||
// In all cases: re-initialise USB HID driver
|
||||
@ -125,189 +125,191 @@ int32_t PIOS_USB_HID_ChangeConnectionState(uint32_t Connected)
|
||||
|
||||
//TODO: Check SetEPRxValid(ENDP1);
|
||||
|
||||
#if defined(USB_LED_ON)
|
||||
USB_LED_ON; // turn the USB led on
|
||||
#endif
|
||||
#if defined(USB_LED_ON)
|
||||
USB_LED_ON; // turn the USB led on
|
||||
#endif
|
||||
}
|
||||
else
|
||||
{
|
||||
// Cable disconnected: disable transfers
|
||||
transfer_possible = 0;
|
||||
|
||||
#if defined(USB_LED_OFF)
|
||||
USB_LED_OFF; // turn the USB led off
|
||||
#endif
|
||||
#if defined(USB_LED_OFF)
|
||||
USB_LED_OFF; // turn the USB led off
|
||||
#endif
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int32_t PIOS_USB_HID_Reenumerate()
|
||||
{
|
||||
/* Force USB reset and power-down (this will also release the USB pins for direct GPIO control) */
|
||||
_SetCNTR(CNTR_FRES | CNTR_PDWN);
|
||||
|
||||
/* Using a "dirty" method to force a re-enumeration: */
|
||||
/* Force DPM (Pin PA12) low for ca. 10 mS before USB Tranceiver will be enabled */
|
||||
/* This overrules the external Pull-Up at PA12, and at least Windows & MacOS will enumerate again */
|
||||
GPIO_InitTypeDef GPIO_InitStructure;
|
||||
GPIO_StructInit(&GPIO_InitStructure);
|
||||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
|
||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
|
||||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
||||
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
||||
|
||||
PIOS_DELAY_WaitmS(50);
|
||||
|
||||
/* Release power-down, still hold reset */
|
||||
_SetCNTR(CNTR_PDWN);
|
||||
PIOS_DELAY_WaituS(5);
|
||||
|
||||
/* CNTR_FRES = 0 */
|
||||
_SetCNTR(0);
|
||||
|
||||
/* Clear pending interrupts */
|
||||
_SetISTR(0);
|
||||
|
||||
/* Configure USB clock */
|
||||
/* USBCLK = PLLCLK / 1.5 */
|
||||
RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);
|
||||
/* Enable USB clock */
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE);
|
||||
|
||||
return 0;
|
||||
{
|
||||
/* Force USB reset and power-down (this will also release the USB pins for direct GPIO control) */
|
||||
_SetCNTR(CNTR_FRES | CNTR_PDWN);
|
||||
|
||||
/* Using a "dirty" method to force a re-enumeration: */
|
||||
/* Force DPM (Pin PA12) low for ca. 10 mS before USB Tranceiver will be enabled */
|
||||
/* This overrules the external Pull-Up at PA12, and at least Windows & MacOS will enumerate again */
|
||||
GPIO_InitTypeDef GPIO_InitStructure;
|
||||
GPIO_StructInit(&GPIO_InitStructure);
|
||||
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
|
||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
|
||||
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
||||
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
||||
|
||||
PIOS_DELAY_WaitmS(50);
|
||||
|
||||
/* Release power-down, still hold reset */
|
||||
_SetCNTR(CNTR_PDWN);
|
||||
PIOS_DELAY_WaituS(5);
|
||||
|
||||
/* CNTR_FRES = 0 */
|
||||
_SetCNTR(0);
|
||||
|
||||
/* Clear pending interrupts */
|
||||
_SetISTR(0);
|
||||
|
||||
/* Configure USB clock */
|
||||
/* USBCLK = PLLCLK / 1.5 */
|
||||
RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);
|
||||
/* Enable USB clock */
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function returns the connection status of the USB HID interface
|
||||
* \return 1: interface available
|
||||
* \return 0: interface not available
|
||||
* \note Applications shouldn't call this function directly, instead please use \ref PIOS_COM layer functions
|
||||
*/
|
||||
* This function returns the connection status of the USB HID interface
|
||||
* \return 1: interface available
|
||||
* \return 0: interface not available
|
||||
* \note Applications shouldn't call this function directly, instead please use \ref PIOS_COM layer functions
|
||||
*/
|
||||
int32_t PIOS_USB_HID_CheckAvailable(uint8_t id)
|
||||
{
|
||||
return (PIOS_USB_DETECT_GPIO_PORT->IDR & PIOS_USB_DETECT_GPIO_PIN) != 0 && transfer_possible ? 1 : 0;
|
||||
return (PIOS_USB_DETECT_GPIO_PORT->IDR & PIOS_USB_DETECT_GPIO_PIN) != 0 && transfer_possible ? 1 : 0;
|
||||
}
|
||||
|
||||
void sendChunk()
|
||||
{
|
||||
|
||||
uint32_t size = bufferBufferedData(&txBuffer);
|
||||
if(size > 0)
|
||||
{
|
||||
if(size > PIOS_USB_HID_DATA_LENGTH)
|
||||
size = PIOS_USB_HID_DATA_LENGTH;
|
||||
|
||||
bufferGetChunkFromFront(&txBuffer, &tx_buffer[2], size);
|
||||
tx_buffer[0] = 1; /* report ID */
|
||||
tx_buffer[1] = size; /* valid data length */
|
||||
|
||||
/* Wait for any pending transmissions to complete */
|
||||
while(GetEPTxStatus(ENDP1) == EP_TX_VALID)
|
||||
{
|
||||
#if defined(PIOS_INCLUDE_FREERTOS)
|
||||
taskYIELD();
|
||||
#endif
|
||||
}
|
||||
|
||||
UserToPMABufferCopy((uint8_t*) tx_buffer, GetEPTxAddr(EP1_IN & 0x7F), size+2);
|
||||
SetEPTxCount((EP1_IN & 0x7F), PIOS_USB_HID_DATA_LENGTH+2);
|
||||
|
||||
/* Send Buffer */
|
||||
SetEPTxValid(ENDP1);
|
||||
}
|
||||
|
||||
uint32_t size = bufferBufferedData(&txBuffer);
|
||||
if(size > 0)
|
||||
{
|
||||
if(size > PIOS_USB_HID_DATA_LENGTH)
|
||||
size = PIOS_USB_HID_DATA_LENGTH;
|
||||
#ifdef USB_HID
|
||||
bufferGetChunkFromFront(&txBuffer, &tx_buffer[1], size+1);
|
||||
tx_buffer[0] = 1; /* report ID */
|
||||
#else
|
||||
bufferGetChunkFromFront(&txBuffer, &tx_buffer[2], size);
|
||||
tx_buffer[0] = 1; /* report ID */
|
||||
tx_buffer[1] = size; /* valid data length */
|
||||
|
||||
#endif
|
||||
/* Wait for any pending transmissions to complete */
|
||||
while(GetEPTxStatus(ENDP1) == EP_TX_VALID)
|
||||
{
|
||||
#if defined(PIOS_INCLUDE_FREERTOS)
|
||||
taskYIELD();
|
||||
#endif
|
||||
}
|
||||
|
||||
UserToPMABufferCopy((uint8_t*) tx_buffer, GetEPTxAddr(EP1_IN & 0x7F), size+2);
|
||||
SetEPTxCount((EP1_IN & 0x7F), PIOS_USB_HID_DATA_LENGTH+2);
|
||||
|
||||
/* Send Buffer */
|
||||
SetEPTxValid(ENDP1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Puts more than one byte onto the transmit buffer (used for atomic sends)
|
||||
* \param[in] *buffer pointer to buffer which should be transmitted
|
||||
* \param[in] len number of bytes which should be transmitted
|
||||
* \return 0 if no error
|
||||
* \return -1 if too many bytes to be send
|
||||
* \note Applications shouldn't call this function directly, instead please use \ref PIOS_COM layer functions
|
||||
*/
|
||||
* Puts more than one byte onto the transmit buffer (used for atomic sends)
|
||||
* \param[in] *buffer pointer to buffer which should be transmitted
|
||||
* \param[in] len number of bytes which should be transmitted
|
||||
* \return 0 if no error
|
||||
* \return -1 if too many bytes to be send
|
||||
* \note Applications shouldn't call this function directly, instead please use \ref PIOS_COM layer functions
|
||||
*/
|
||||
int32_t PIOS_USB_HID_TxBufferPutMoreNonBlocking(uint8_t id, const uint8_t *buffer, uint16_t len)
|
||||
{
|
||||
/*if( len > PIOS_USB_HID_DATA_LENGTH )
|
||||
return - 1;*/
|
||||
|
||||
uint8_t previous_data = bufferBufferedData(&txBuffer);
|
||||
|
||||
if(len > bufferRemainingSpace(&txBuffer))
|
||||
return -1; /* Cannot send all requested bytes */
|
||||
|
||||
if(bufferAddChunkToEnd(&txBuffer, buffer, len) == 0)
|
||||
return -1;
|
||||
|
||||
/* If no previous data queued and not sending, then TX complete interrupt not likely so send manually */
|
||||
if(previous_data == 0 && GetEPTxStatus(ENDP1) != EP_TX_VALID)
|
||||
sendChunk();
|
||||
|
||||
return 0;
|
||||
/*if( len > PIOS_USB_HID_DATA_LENGTH )
|
||||
return - 1;*/
|
||||
|
||||
uint8_t previous_data = bufferBufferedData(&txBuffer);
|
||||
|
||||
if(len > bufferRemainingSpace(&txBuffer))
|
||||
return -1; /* Cannot send all requested bytes */
|
||||
|
||||
if(bufferAddChunkToEnd(&txBuffer, buffer, len) == 0)
|
||||
return -1;
|
||||
|
||||
/* If no previous data queued and not sending, then TX complete interrupt not likely so send manually */
|
||||
if(previous_data == 0 && GetEPTxStatus(ENDP1) != EP_TX_VALID)
|
||||
sendChunk();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Puts more than one byte onto the transmit buffer (used for atomic sends)<br>
|
||||
* (Blocking Function)
|
||||
* \param[in] *buffer pointer to buffer which should be transmitted
|
||||
* \param[in] len number of bytes which should be transmitted
|
||||
* \return 0 if no error
|
||||
* \return -1 if too many bytes to be send
|
||||
* \note Applications shouldn't call this function directly, instead please use \ref PIOS_COM layer functions
|
||||
*/
|
||||
* Puts more than one byte onto the transmit buffer (used for atomic sends)<br>
|
||||
* (Blocking Function)
|
||||
* \param[in] *buffer pointer to buffer which should be transmitted
|
||||
* \param[in] len number of bytes which should be transmitted
|
||||
* \return 0 if no error
|
||||
* \return -1 if too many bytes to be send
|
||||
* \note Applications shouldn't call this function directly, instead please use \ref PIOS_COM layer functions
|
||||
*/
|
||||
int32_t PIOS_USB_HID_TxBufferPutMore(uint8_t id, const uint8_t *buffer, uint16_t len)
|
||||
{
|
||||
uint32_t error;
|
||||
if((error = PIOS_USB_HID_TxBufferPutMoreNonBlocking(id, buffer, len)) != 0)
|
||||
return error;
|
||||
|
||||
while( bufferBufferedData(&txBuffer) )
|
||||
{
|
||||
#if defined(PIOS_INCLUDE_FREERTOS)
|
||||
taskYIELD();
|
||||
#endif
|
||||
}
|
||||
|
||||
return 0;
|
||||
uint32_t error;
|
||||
if((error = PIOS_USB_HID_TxBufferPutMoreNonBlocking(id, buffer, len)) != 0)
|
||||
return error;
|
||||
|
||||
while( bufferBufferedData(&txBuffer) )
|
||||
{
|
||||
#if defined(PIOS_INCLUDE_FREERTOS)
|
||||
taskYIELD();
|
||||
#endif
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a byte from the receive buffer
|
||||
* \return -1 if no new byte available
|
||||
* \return >= 0: received byte
|
||||
* \note Applications shouldn't call this function directly, instead please use \ref PIOS_COM layer functions
|
||||
*/
|
||||
* Gets a byte from the receive buffer
|
||||
* \return -1 if no new byte available
|
||||
* \return >= 0: received byte
|
||||
* \note Applications shouldn't call this function directly, instead please use \ref PIOS_COM layer functions
|
||||
*/
|
||||
int32_t PIOS_USB_HID_RxBufferGet(uint8_t id)
|
||||
{
|
||||
return bufferGetFromFront(&rxBuffer);
|
||||
return bufferGetFromFront(&rxBuffer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns number of used bytes in receive buffer
|
||||
* \return > 0: number of used bytes
|
||||
* \return 0 nothing available
|
||||
* \note Applications shouldn't call these functions directly, instead please use \ref PIOS_COM layer functions
|
||||
*/
|
||||
* Returns number of used bytes in receive buffer
|
||||
* \return > 0: number of used bytes
|
||||
* \return 0 nothing available
|
||||
* \note Applications shouldn't call these functions directly, instead please use \ref PIOS_COM layer functions
|
||||
*/
|
||||
int32_t PIOS_USB_HID_RxBufferUsed(uint8_t id)
|
||||
{
|
||||
return bufferBufferedData(&rxBuffer);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Callback used to indicate a transmission from device INto host completed
|
||||
* Checks if any data remains, pads it into HID packet and sends.
|
||||
*/
|
||||
void PIOS_USB_HID_EP1_IN_Callback(void)
|
||||
{
|
||||
sendChunk();
|
||||
sendChunk();
|
||||
}
|
||||
|
||||
/**
|
||||
* EP1 OUT Callback Routine
|
||||
*/
|
||||
* EP1 OUT Callback Routine
|
||||
*/
|
||||
void PIOS_USB_HID_EP1_OUT_Callback(void)
|
||||
{
|
||||
uint32_t DataLength = 0;
|
||||
@ -315,19 +317,22 @@ void PIOS_USB_HID_EP1_OUT_Callback(void)
|
||||
/* Read received data (63 bytes) */
|
||||
/* Get the number of received data on the selected Endpoint */
|
||||
DataLength = GetEPRxCount(ENDP1 & 0x7F);
|
||||
|
||||
|
||||
/* Use the memory interface function to write to the selected endpoint */
|
||||
PMAToUserBufferCopy((uint8_t *) rx_buffer, GetEPRxAddr(ENDP1 & 0x7F), DataLength);
|
||||
|
||||
/* The first byte is report ID (not checked), the second byte is the valid data length */
|
||||
bufferAddChunkToEnd(&rxBuffer, &rx_buffer[2], rx_buffer[1]);
|
||||
|
||||
/* The first byte is report ID (not checked), the second byte is the valid data length */
|
||||
#ifdef USB_HID
|
||||
bufferAddChunkToEnd(&rxBuffer, &rx_buffer[1], PIOS_USB_HID_DATA_LENGTH+1);
|
||||
#else
|
||||
bufferAddChunkToEnd(&rxBuffer, &rx_buffer[2], rx_buffer[1]);
|
||||
#endif
|
||||
SetEPRxStatus(ENDP1, EP_RX_VALID);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @}
|
||||
* @}
|
||||
*/
|
||||
* @}
|
||||
* @}
|
||||
*/
|
||||
|
Loading…
x
Reference in New Issue
Block a user