add basic project for tos_evb_aiot
This commit is contained in:
@@ -1,453 +1,502 @@
|
||||
/*
|
||||
* Copyright 2018 NXP
|
||||
* All rights reserved.
|
||||
*
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
#ifndef __HAL_I2C_ADAPTER_H__
|
||||
#define __HAL_I2C_ADAPTER_H__
|
||||
|
||||
/*!
|
||||
* @addtogroup hal_i2c_driver
|
||||
* @{
|
||||
*/
|
||||
|
||||
/*******************************************************************************
|
||||
* Definitions
|
||||
******************************************************************************/
|
||||
#define HAL_I2C_MASTER_HANDLE_SIZE (80U)
|
||||
#define HAL_I2C_SLAVE_HANDLE_SIZE (80U)
|
||||
|
||||
typedef enum _hal_i2c_status
|
||||
{
|
||||
kStatus_HAL_I2cSuccess = kStatus_Success, /*!< Successfully */
|
||||
kStatus_HAL_I2cError = MAKE_STATUS(kStatusGroup_HAL_I2C, 1), /*!< Error occurs on HAL I2C */
|
||||
kStatus_HAL_I2cBusy = MAKE_STATUS(kStatusGroup_HAL_I2C, 2), /*!< HAL I2C is busy with current transfer */
|
||||
kStatus_HAL_I2cIdle = MAKE_STATUS(kStatusGroup_HAL_I2C, 3), /*!< HAL I2C transmitter is idle */
|
||||
kStatus_HAL_I2cNak = MAKE_STATUS(kStatusGroup_HAL_I2C, 4), /*!< NAK received during transfer */
|
||||
kStatus_HAL_I2cArbitrationLost = MAKE_STATUS(kStatusGroup_HAL_I2C, 5), /*!< Arbitration lost during transfer */
|
||||
kStatus_HAL_I2cTimeout = MAKE_STATUS(kStatusGroup_HAL_I2C, 6), /*!< Timeout */
|
||||
kStatus_HAL_I2cAddrressNak = MAKE_STATUS(kStatusGroup_HAL_I2C, 7), /*!< NAK received during the address probe */
|
||||
} hal_i2c_status_t;
|
||||
|
||||
/*! @brief HAL I2C master user configuration. */
|
||||
typedef struct _hal_i2c_master_config
|
||||
{
|
||||
uint32_t srcClock_Hz; /*!< Clock source for I2C in Hz */
|
||||
uint32_t baudRate_Bps; /*!< Baud rate configuration of HAL I2C peripheral. */
|
||||
bool enableMaster; /*!< Enables the HAL I2C peripheral at initialization time. */
|
||||
uint8_t instance; /*!< Instance of the i2c */
|
||||
} hal_i2c_master_config_t;
|
||||
|
||||
/*! @brief HAL I2C slave user configuration. */
|
||||
typedef struct _hal_i2c_slave_config
|
||||
{
|
||||
uint32_t srcClock_Hz; /*!< Clock source for I2C in Hz */
|
||||
uint16_t slaveAddress; /*!< A slave address configuration. */
|
||||
bool enableSlave; /*!< Enables the HAL I2C peripheral at initialization time. */
|
||||
uint8_t instance; /*!< Instance of the i2c */
|
||||
} hal_i2c_slave_config_t;
|
||||
|
||||
/*! @brief Direction of master and slave transfers. */
|
||||
typedef enum _hal_i2c_direction
|
||||
{
|
||||
kHAL_I2cWrite = 0U, /*!< Master transmit. */
|
||||
kHAL_I2cRead = 1U /*!< Master receive. */
|
||||
} hal_i2c_direction_t;
|
||||
|
||||
/*! @brief I2C transfer control flag. */
|
||||
typedef enum _hal_i2c_master_transfer_flag
|
||||
{
|
||||
kHAL_I2cTransferDefaultFlag = 0x0U, /*!< A transfer starts with a start signal, stops with a stop signal. */
|
||||
kHAL_I2cTransferNoStartFlag = 0x1U, /*!< A transfer starts without a start signal, only support write only or
|
||||
write+read with no start flag, do not support read only with no start flag. */
|
||||
kHAL_I2cTransferRepeatedStartFlag = 0x2U, /*!< A transfer starts with a repeated start signal. */
|
||||
kHAL_I2cTransferNoStopFlag = 0x4U, /*!< A transfer ends without a stop signal. */
|
||||
} hal_i2c_master_transfer_flag_t;
|
||||
|
||||
/*!
|
||||
* @brief Set of events sent to the callback for nonblocking slave transfers.
|
||||
*
|
||||
* These event enumerations are used for two related purposes. First, a bit mask created by OR'ing together
|
||||
* events is passed to I2C_SlaveTransferNonBlocking() to specify which events to enable.
|
||||
* Then, when the slave callback is invoked, it is passed the current event through its @a transfer
|
||||
* parameter.
|
||||
*
|
||||
* @note These enumerations are meant to be OR'd together to form a bit mask of events.
|
||||
*/
|
||||
typedef enum _hal_i2c_slave_transfer_event
|
||||
{
|
||||
kHAL_I2cSlaveAddressMatchEvent = 0x01U, /*!< Received the slave address after a start or repeated start. */
|
||||
kHAL_I2cSlaveTransmitEvent = 0x02U, /*!< A callback is requested to provide data to transmit
|
||||
(slave-transmitter role). */
|
||||
kHAL_I2cSlaveReceiveEvent = 0x04U, /*!< A callback is requested to provide a buffer in which to place received
|
||||
data (slave-receiver role). */
|
||||
kHAL_I2cSlaveTransmitAckEvent = 0x08U, /*!< A callback needs to either transmit an ACK or NACK. */
|
||||
kHAL_I2cSlaveCompletionEvent = 0x20U, /*!< A stop was detected or finished transfer, completing the transfer. */
|
||||
kHAL_I2cSlaveStartEvent = 0x10U, /*!< A start/repeated start was detected. */
|
||||
kHAL_I2cSlaveGenaralcallEvent = 0x40U, /*!< Received the general call address after a start or repeated start. */
|
||||
/*! A bit mask of all available events. */
|
||||
kHAL_I2cSlaveAllEvents = kHAL_I2cSlaveAddressMatchEvent | kHAL_I2cSlaveTransmitEvent | kHAL_I2cSlaveReceiveEvent |
|
||||
kHAL_I2cSlaveTransmitAckEvent | kHAL_I2cSlaveCompletionEvent | kHAL_I2cSlaveStartEvent |
|
||||
kHAL_I2cSlaveGenaralcallEvent,
|
||||
} hal_i2c_slave_transfer_event_t;
|
||||
|
||||
/*! @brief HAL I2C master transfer structure. */
|
||||
typedef struct _hal_i2c_master_transfer
|
||||
{
|
||||
uint8_t *volatile data; /*!< A transfer buffer. */
|
||||
volatile size_t dataSize; /*!< A transfer size. */
|
||||
uint32_t flags; /*!< A transfer flag which controls the transfer. */
|
||||
uint32_t subaddress; /*!< A sub address. Transferred MSB first. */
|
||||
uint8_t subaddressSize; /*!< A size of the command buffer. */
|
||||
uint8_t slaveAddress; /*!< 7-bit slave address. */
|
||||
hal_i2c_direction_t direction; /*!< A transfer direction, read or write. */
|
||||
} hal_i2c_master_transfer_t;
|
||||
|
||||
/*! @brief HAL I2C slave transfer structure. */
|
||||
typedef struct _hal_i2c_slave_transfer
|
||||
{
|
||||
hal_i2c_slave_transfer_event_t event; /*!< A reason that the callback is invoked. */
|
||||
uint8_t *volatile data; /*!< A transfer buffer. */
|
||||
volatile size_t dataSize; /*!< A transfer size. */
|
||||
hal_i2c_status_t completionStatus; /*!< Success or error code describing how the transfer completed. Only applies
|
||||
for #kHAL_I2cSlaveCompletionEvent. */
|
||||
size_t transferredCount; /*!< A number of bytes actually transferred since the start or since the last repeated
|
||||
start. */
|
||||
} hal_i2c_slave_transfer_t;
|
||||
|
||||
typedef void *hal_i2c_master_handle_t;
|
||||
typedef void *hal_i2c_slave_handle_t;
|
||||
|
||||
/*!
|
||||
* @brief Master completion callback function pointer type.
|
||||
*
|
||||
* This callback is used only for the non-blocking master transfer API. Specify the callback you wish to use
|
||||
* in the call to HAL_I2cMasterTransferInstallCallback().
|
||||
*
|
||||
* @param handle i2c master handle pointer, this should be a static variable.
|
||||
* @param completionStatus Either #kStatus_HAL_I2cSuccess or an error code describing how the transfer completed.
|
||||
* @param callbackParam Arbitrary pointer-sized value passed from the application.
|
||||
*/
|
||||
typedef void (*hal_i2c_master_transfer_callback_t)(hal_i2c_master_handle_t handle,
|
||||
hal_i2c_status_t completionStatus,
|
||||
void *callbackParam);
|
||||
|
||||
/*!
|
||||
* @brief Slave event callback function pointer type.
|
||||
*
|
||||
* This callback is used only for the slave non-blocking transfer API. Specify the callback you wish to use
|
||||
* in the call to HAL_I2cSlaveTransferInstallCallback().
|
||||
*
|
||||
* @param handle i2c slave master handle pointer, this should be a static variable.
|
||||
* @param transfer Pointer to transfer descriptor containing values passed to and/or from the callback.
|
||||
* @param callbackParam Arbitrary pointer-sized value passed from the application.
|
||||
*/
|
||||
typedef void (*hal_i2c_slave_transfer_callback_t)(hal_i2c_slave_handle_t handle,
|
||||
hal_i2c_slave_transfer_t *transfer,
|
||||
void *callbackParam);
|
||||
|
||||
/*******************************************************************************
|
||||
* API
|
||||
******************************************************************************/
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
#endif /*_cplusplus. */
|
||||
|
||||
/*!
|
||||
* @name Initialization and de-initialization
|
||||
* @{
|
||||
*/
|
||||
|
||||
/*!
|
||||
* @brief Initializes the HAL I2C master peripheral.
|
||||
*
|
||||
* @note This API should be called at the beginning of the application.
|
||||
* Otherwise, any operation to the HAL I2C module can cause a hard fault
|
||||
* because the clock is not enabled. This function configures the i2c master
|
||||
* with user-defined settings. The user can configure the configuration
|
||||
* structure. The parameter handle is a pointer to point to a memory space
|
||||
* of size #HAL_I2C_MASTER_HANDLE_SIZE allocated by the caller.
|
||||
*
|
||||
* This is an example.
|
||||
* @code
|
||||
* uint32_t g_i2cMasterHandleBuffer[((HAL_I2C_MASTER_HANDLE_SIZE + sizeof(uint32_t) - 1) / sizeof(uitn32_t))];
|
||||
* hal_i2c_master_handle_t g_i2cMasterHandle = (hal_i2c_master_handle_t)&g_i2cMasterHandleBuffer[0];
|
||||
* hal_i2c_master_config_t masterConfig;
|
||||
* masterConfig.enableMaster = true;
|
||||
* masterConfig.baudRate_Bps = 100000U;
|
||||
* masterConfig.srcClock_Hz = 12000000U;
|
||||
* masterConfig.instance = 0;
|
||||
* HAL_I2cMasterInit(g_i2cMasterHandle, &masterConfig);
|
||||
* @endcode
|
||||
*
|
||||
* @param handle Pointer to point to a memory space of size #HAL_I2C_MASTER_HANDLE_SIZE allocated by the caller.
|
||||
* The handle should be 4 byte aligned, because unaligned access does not support on some devices.
|
||||
* @param config A pointer to the master configuration structure
|
||||
* @retval kStatus_HAL_I2cError An error occurred.
|
||||
* @retval kStatus_HAL_I2cSuccess i2c master initialization succeed
|
||||
*/
|
||||
hal_i2c_status_t HAL_I2cMasterInit(hal_i2c_master_handle_t handle, const hal_i2c_master_config_t *config);
|
||||
|
||||
/*!
|
||||
* @brief Initializes the HAL I2C peripheral.
|
||||
*
|
||||
* @note This API should be called at the beginning of the application.
|
||||
* Otherwise, any operation to the HAL I2C module can cause a hard fault
|
||||
* because the clock is not enabled. This function configures the i2c slave
|
||||
* with user-defined settings. The user can configure the configuration
|
||||
* structure. The parameter handle is a pointer to point to a memory space
|
||||
* of size #HAL_I2C_SLAVE_HANDLE_SIZE allocated by the caller.
|
||||
*
|
||||
* This is an example.
|
||||
* @code
|
||||
* uint8_t g_i2cSlaveHandleBuffer[HAL_I2C_SLAVE_HANDLE_SIZE];
|
||||
* hal_i2c_slave_handle_t g_i2cSlaveHandle = (hal_i2c_slave_handle_t)&g_i2cSlaveHandleBuffer[0];
|
||||
* hal_i2c_slave_config_t slaveConfig;
|
||||
* slaveConfig.enableSlave = true;
|
||||
* slaveConfig.slaveAddress = 0x01U;
|
||||
* slaveConfig.srcClock_Hz = 12000000U;
|
||||
* slaveConfig.instance = 0;
|
||||
* HAL_I2cSlaveInit(g_i2cSlaveHandle, &slaveConfig);
|
||||
* @endcode
|
||||
*
|
||||
* @param handle Pointer to point to a memory space of size #HAL_I2C_SLAVE_HANDLE_SIZE allocated by the caller.
|
||||
* The handle should be 4 byte aligned, because unaligned access does not support on some devices.
|
||||
* @param config A pointer to the slave configuration structure
|
||||
* @retval kStatus_HAL_I2cError An error occurred.
|
||||
* @retval kStatus_HAL_I2cSuccess i2c slave initialization succeed
|
||||
*/
|
||||
hal_i2c_status_t HAL_I2cSlaveInit(hal_i2c_slave_handle_t handle, const hal_i2c_slave_config_t *config);
|
||||
|
||||
/*!
|
||||
* @brief De-initializes the HAL I2C master peripheral. Call this API to gate the HAL I2C clock.
|
||||
* The HAL I2C master module can't work unless the HAL_I2cMasterInit is called.
|
||||
*
|
||||
* @param handle i2c master handle pointer, this should be a static variable.
|
||||
* @retval kStatus_HAL_I2cSuccess i2c master de-initialization succeed */
|
||||
hal_i2c_status_t HAL_I2cMasterDeinit(hal_i2c_master_handle_t handle);
|
||||
|
||||
/*!
|
||||
* @brief De-initializes the HAL I2C slave peripheral. Calling this API gates the HAL I2C clock.
|
||||
* The HAL I2C slave module can't work unless the HAL_I2cSlaveInit is called to enable the clock.
|
||||
*
|
||||
* @param handle i2c slave handle pointer, this should be a static variable.
|
||||
* @retval kStatus_HAL_I2cSuccess i2c slave de-initialization succeed
|
||||
*/
|
||||
hal_i2c_status_t HAL_I2cSlaveDeinit(hal_i2c_slave_handle_t handle);
|
||||
|
||||
/*! @} */
|
||||
|
||||
/*!
|
||||
* @name Bus Operations
|
||||
* @{
|
||||
*/
|
||||
|
||||
/*!
|
||||
* @brief Performs a polling send transaction on the HAL I2C bus.
|
||||
*
|
||||
* @param handle i2c master handle pointer, this should be a static variable.
|
||||
* @param txBuff The pointer to the data to be transferred.
|
||||
* @param txSize The length in bytes of the data to be transferred.
|
||||
* @param flags Transfer control flag to decide whether need to send a stop, use kHAL_I2cTransferDefaultFlag
|
||||
* to issue a stop and kHAL_I2cTransferNoStopFlag to not send a stop.
|
||||
* @retval kStatus_HAL_I2cSuccess Successfully complete the data transmission.
|
||||
* @retval kStatus_HAL_I2cArbitrationLost Transfer error, arbitration lost.
|
||||
* @retval kStatus_HAL_I2cNak Transfer error, receive NAK during transfer.
|
||||
*/
|
||||
hal_i2c_status_t HAL_I2cMasterWriteBlocking(hal_i2c_master_handle_t handle,
|
||||
const uint8_t *txBuff,
|
||||
size_t txSize,
|
||||
uint32_t flags);
|
||||
|
||||
/*!
|
||||
* @brief Performs a polling receive transaction on the HAL I2C bus.
|
||||
*
|
||||
* @note The HAL_I2cMasterReadBlocking function stops the bus before reading the final byte.
|
||||
* Without stopping the bus prior for the final read, the bus issues another read, resulting
|
||||
* in garbage data being read into the data register.
|
||||
*
|
||||
* @param handle i2c master handle pointer, this should be a static variable.
|
||||
* @param rxBuff The pointer to the data to store the received data.
|
||||
* @param rxSize The length in bytes of the data to be received.
|
||||
* @param flags Transfer control flag to decide whether need to send a stop, use kHAL_I2cTransferDefaultFlag
|
||||
* to issue a stop and kHAL_I2cTransferNoStopFlag to not send a stop.
|
||||
* @retval kStatus_HAL_I2cSuccess Successfully complete the data transmission.
|
||||
* @retval kStatus_HAL_I2cTimeout Send stop signal failed, timeout.
|
||||
*/
|
||||
hal_i2c_status_t HAL_I2cMasterReadBlocking(hal_i2c_master_handle_t handle,
|
||||
uint8_t *rxBuff,
|
||||
size_t rxSize,
|
||||
uint32_t flags);
|
||||
|
||||
/*!
|
||||
* @brief Performs a polling send transaction on the HAL I2C bus.
|
||||
*
|
||||
* @param handle i2c slave handle pointer, this should be a static variable.
|
||||
* @param txBuff The pointer to the data to be transferred.
|
||||
* @param txSize The length in bytes of the data to be transferred.
|
||||
* @retval kStatus_HAL_I2cSuccess Successfully complete the data transmission.
|
||||
* @retval kStatus_HAL_I2cArbitrationLost Transfer error, arbitration lost.
|
||||
* @retval kStatus_HAL_I2cNak Transfer error, receive NAK during transfer.
|
||||
*/
|
||||
hal_i2c_status_t HAL_I2cSlaveWriteBlocking(hal_i2c_slave_handle_t handle, const uint8_t *txBuff, size_t txSize);
|
||||
|
||||
/*!
|
||||
* @brief Performs a polling receive transaction on the HAL I2C bus.
|
||||
*
|
||||
* @param handle i2c slave handle pointer, this should be a static variable.
|
||||
* @param rxBuff The pointer to the data to store the received data.
|
||||
* @param rxSize The length in bytes of the data to be received.
|
||||
* @retval kStatus_HAL_I2cSuccess Successfully complete data receive.
|
||||
* @retval kStatus_HAL_I2cTimeout Wait status flag timeout.
|
||||
*/
|
||||
hal_i2c_status_t HAL_I2cSlaveReadBlocking(hal_i2c_slave_handle_t handle, uint8_t *rxBuff, size_t rxSize);
|
||||
|
||||
/*!
|
||||
* @brief Performs a master polling transfer on the HAL I2C bus.
|
||||
*
|
||||
* @note The API does not return until the transfer succeeds or fails due
|
||||
* to arbitration lost or receiving a NAK.
|
||||
*
|
||||
* @param handle i2c master handle pointer, this should be a static variable.
|
||||
* @param xfer Pointer to the transfer structure.
|
||||
* @retval kStatus_HAL_I2cSuccess Successfully complete the data transmission.
|
||||
* @retval kStatus_HAL_I2cBusy Previous transmission still not finished.
|
||||
* @retval kStatus_HAL_I2cTimeout Transfer error, wait signal timeout.
|
||||
* @retval kStatus_HAL_I2cArbitrationLost Transfer error, arbitration lost.
|
||||
* @retval kStatus_HAL_I2cNak Transfer error, receive NAK during transfer.
|
||||
*/
|
||||
hal_i2c_status_t HAL_I2cMasterTransferBlocking(hal_i2c_master_handle_t handle, hal_i2c_master_transfer_t *xfer);
|
||||
|
||||
/*! @} */
|
||||
|
||||
/*!
|
||||
* @name Transactional
|
||||
* @{
|
||||
*/
|
||||
|
||||
/*!
|
||||
* @brief Installs a callback and callback parameter.
|
||||
*
|
||||
* This function is used to install the callback and callback parameter for i2c master module.
|
||||
* When any status of the i2c master changed, the driver will notify the upper layer by the installed callback
|
||||
* function. And the status is also passed as status parameter when the callback is called.
|
||||
*
|
||||
* @param handle i2c master handle pointer, this should be a static variable.
|
||||
* @param callback pointer to user callback function.
|
||||
* @param callbackParam user parameter passed to the callback function.
|
||||
* @retval kStatus_HAL_I2cSuccess i2c master handle created
|
||||
*/
|
||||
hal_i2c_status_t HAL_I2cMasterTransferInstallCallback(hal_i2c_master_handle_t handle,
|
||||
hal_i2c_master_transfer_callback_t callback,
|
||||
void *callbackParam);
|
||||
|
||||
/*!
|
||||
* @brief Performs a master interrupt non-blocking transfer on the HAL I2C bus.
|
||||
*
|
||||
* @note Calling the API returns immediately after transfer initiates. The user needs
|
||||
* to call HAL_I2cMasterGetTransferCount to poll the transfer status to check whether
|
||||
* the transfer is finished. If the return status is not kStatus_HAL_I2cBusy, the transfer
|
||||
* is finished.
|
||||
*
|
||||
* @param handle i2c master handle pointer, this should be a static variable.
|
||||
* @param xfer pointer to hal_i2c_master_transfer_t structure.
|
||||
* @retval kStatus_HAL_I2cSuccess Successfully start the data transmission.
|
||||
* @retval kStatus_HAL_I2cBusy Previous transmission still not finished.
|
||||
* @retval kStatus_HAL_I2cTimeout Transfer error, wait signal timeout.
|
||||
*/
|
||||
hal_i2c_status_t HAL_I2cMasterTransferNonBlocking(hal_i2c_master_handle_t handle, hal_i2c_master_transfer_t *xfer);
|
||||
|
||||
/*!
|
||||
* @brief Gets the master transfer status during a interrupt non-blocking transfer.
|
||||
*
|
||||
* @param handle i2c master handle pointer, this should be a static variable.
|
||||
* @param count Number of bytes transferred so far by the non-blocking transaction.
|
||||
* @retval kStatus_HAL_I2cError An error occurred.
|
||||
* @retval kStatus_HAL_I2cSuccess Successfully return the count.
|
||||
*/
|
||||
hal_i2c_status_t HAL_I2cMasterTransferGetCount(hal_i2c_master_handle_t handle, size_t *count);
|
||||
|
||||
/*!
|
||||
* @brief Aborts an interrupt non-blocking transfer early.
|
||||
*
|
||||
* @note This API can be called at any time when an interrupt non-blocking transfer initiates
|
||||
* to abort the transfer early.
|
||||
*
|
||||
* @param handle i2c master handle pointer, this should be a static variable.
|
||||
* @retval kStatus_HAL_I2cTimeout Timeout during polling flag.
|
||||
* @retval kStatus_HAL_I2cSuccess Successfully abort the transfer.
|
||||
*/
|
||||
hal_i2c_status_t HAL_I2cMasterTransferAbort(hal_i2c_master_handle_t handle);
|
||||
|
||||
/*!
|
||||
* @brief Installs a callback and callback parameter.
|
||||
*
|
||||
* This function is used to install the callback and callback parameter for i2c slave module.
|
||||
* When any status of the i2c slave changed, the driver will notify the upper layer by the installed callback
|
||||
* function. And the status is also passed as status parameter when the callback is called.
|
||||
*
|
||||
* @param handle i2c slave handle pointer, this should be a static variable.
|
||||
* @param callback pointer to user callback function.
|
||||
* @param callbackParam user parameter passed to the callback function.
|
||||
* @retval kStatus_HAL_I2cSuccess i2c slave handle created
|
||||
*/
|
||||
hal_i2c_status_t HAL_I2cSlaveTransferInstallCallback(hal_i2c_slave_handle_t handle,
|
||||
hal_i2c_slave_transfer_callback_t callback,
|
||||
void *callbackParam);
|
||||
|
||||
/*!
|
||||
* @brief Starts accepting slave transfers.
|
||||
*
|
||||
* Call this API after calling the HAL_I2cSlaveInit() and HAL_I2cSlaveTransferInstallCallback() to start processing
|
||||
* transactions driven by an HAL I2C slave. The slave monitors the HAL I2C bus and passes events to the
|
||||
* callback that was passed into the call to HAL_I2cSlaveTransferInstallCallback(). The callback is always invoked
|
||||
* from the interrupt context.
|
||||
*
|
||||
* The set of events received by the callback is customizable. To do so, set the @a eventMask parameter to
|
||||
* the OR'd combination of #hal_i2c_slave_transfer_event_t enumerators for the events you wish to receive.
|
||||
* The #kHAL_I2cSlaveTransmitEvent and #kLPHAL_I2cSlaveReceiveEvent events are always enabled and do not need
|
||||
* to be included in the mask. Alternatively, pass 0 to get a default set of only the transmit and
|
||||
* receive events that are always enabled. In addition, the #kHAL_I2cSlaveAllEvents constant is provided as
|
||||
* a convenient way to enable all events.
|
||||
*
|
||||
* @param handle i2c slave handle pointer, this should be a static variable.
|
||||
* @param eventMask Bit mask formed by OR'ing together #hal_i2c_slave_transfer_event_t enumerators to specify
|
||||
* which events to send to the callback. Other accepted values are 0 to get a default set of
|
||||
* only the transmit and receive events, and #kHAL_I2cSlaveAllEvents to enable all events.
|
||||
*
|
||||
* @retval #kStatus_HAL_I2cSuccess Slave transfers were successfully started.
|
||||
* @retval #kStatus_HAL_I2cBusy Slave transfers have already been started on this handle.
|
||||
*/
|
||||
hal_i2c_status_t HAL_I2cSlaveTransferNonBlocking(hal_i2c_slave_handle_t handle, uint32_t eventMask);
|
||||
|
||||
/*!
|
||||
* @brief Aborts the slave transfer.
|
||||
*
|
||||
* @note This API can be called at any time to stop slave for handling the bus events.
|
||||
*
|
||||
* @param handle i2c slave handle pointer, this should be a static variable.
|
||||
* @retval kStatus_HAL_I2cSuccess Successfully return the count.
|
||||
*/
|
||||
hal_i2c_status_t HAL_I2cSlaveTransferAbort(hal_i2c_slave_handle_t handle);
|
||||
|
||||
/*!
|
||||
* @brief Gets the slave transfer remaining bytes during a interrupt non-blocking transfer.
|
||||
*
|
||||
* @param handle i2c slave handle pointer, this should be a static variable.
|
||||
* @param count Number of bytes transferred so far by the non-blocking transaction.
|
||||
* @retval kStatus_HAL_I2cError An error occurred.
|
||||
* @retval kStatus_HAL_I2cSuccess Successfully return the count.
|
||||
*/
|
||||
hal_i2c_status_t HAL_I2cSlaveTransferGetCount(hal_i2c_slave_handle_t handle, size_t *count);
|
||||
|
||||
/*! @} */
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
#endif /*_cplusplus. */
|
||||
/*! @} */
|
||||
|
||||
#endif /* __HAL_I2C_ADAPTER_H__*/
|
||||
/*
|
||||
* Copyright 2018-2019 NXP
|
||||
* All rights reserved.
|
||||
*
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
#ifndef __HAL_I2C_ADAPTER_H__
|
||||
#define __HAL_I2C_ADAPTER_H__
|
||||
|
||||
/*!
|
||||
* @addtogroup I2C_Adapter
|
||||
* @{
|
||||
*/
|
||||
|
||||
/*******************************************************************************
|
||||
* Definitions
|
||||
******************************************************************************/
|
||||
/*! @brief HAL I2C master handle size. */
|
||||
#define HAL_I2C_MASTER_HANDLE_SIZE (80U)
|
||||
|
||||
/*! @brief HAL I2C slave handle size. */
|
||||
#define HAL_I2C_SLAVE_HANDLE_SIZE (88U)
|
||||
|
||||
/*! @brief HAL I2C status. */
|
||||
typedef enum _hal_i2c_status
|
||||
{
|
||||
kStatus_HAL_I2cSuccess = kStatus_Success, /*!< Successfully */
|
||||
kStatus_HAL_I2cError = MAKE_STATUS(kStatusGroup_HAL_I2C, 1), /*!< Error occurs on HAL I2C */
|
||||
kStatus_HAL_I2cBusy = MAKE_STATUS(kStatusGroup_HAL_I2C, 2), /*!< HAL I2C is busy with current transfer */
|
||||
kStatus_HAL_I2cIdle = MAKE_STATUS(kStatusGroup_HAL_I2C, 3), /*!< HAL I2C transmitter is idle */
|
||||
kStatus_HAL_I2cNak = MAKE_STATUS(kStatusGroup_HAL_I2C, 4), /*!< NAK received during transfer */
|
||||
kStatus_HAL_I2cArbitrationLost = MAKE_STATUS(kStatusGroup_HAL_I2C, 5), /*!< Arbitration lost during transfer */
|
||||
kStatus_HAL_I2cTimeout = MAKE_STATUS(kStatusGroup_HAL_I2C, 6), /*!< Timeout */
|
||||
kStatus_HAL_I2cAddrressNak = MAKE_STATUS(kStatusGroup_HAL_I2C, 7), /*!< NAK received during the address probe */
|
||||
} hal_i2c_status_t;
|
||||
|
||||
/*! @brief HAL I2C master user configuration. */
|
||||
typedef struct _hal_i2c_master_config
|
||||
{
|
||||
uint32_t srcClock_Hz; /*!< Clock source for I2C in Hz */
|
||||
uint32_t baudRate_Bps; /*!< Baud rate configuration of HAL I2C peripheral. */
|
||||
bool enableMaster; /*!< Enables the HAL I2C peripheral at initialization time. */
|
||||
uint8_t instance; /*!< Instance of the i2c */
|
||||
} hal_i2c_master_config_t;
|
||||
|
||||
/*! @brief HAL I2C slave user configuration. */
|
||||
typedef struct _hal_i2c_slave_config
|
||||
{
|
||||
uint32_t srcClock_Hz; /*!< Clock source for I2C in Hz */
|
||||
uint16_t slaveAddress; /*!< A slave address configuration. */
|
||||
bool enableSlave; /*!< Enables the HAL I2C peripheral at initialization time. */
|
||||
uint8_t instance; /*!< Instance of the i2c */
|
||||
} hal_i2c_slave_config_t;
|
||||
|
||||
/*! @brief Direction of master and slave transfers. */
|
||||
typedef enum _hal_i2c_direction
|
||||
{
|
||||
kHAL_I2cWrite = 0U, /*!< Master transmit. */
|
||||
kHAL_I2cRead = 1U /*!< Master receive. */
|
||||
} hal_i2c_direction_t;
|
||||
|
||||
/*! @brief I2C transfer control flag. */
|
||||
typedef enum _hal_i2c_master_transfer_flag
|
||||
{
|
||||
kHAL_I2cTransferDefaultFlag = 0x0U, /*!< A transfer starts with a start signal, stops with a stop signal. */
|
||||
kHAL_I2cTransferNoStartFlag = 0x1U, /*!< A transfer starts without a start signal, only support write only or
|
||||
write+read with no start flag, do not support read only with no start flag. */
|
||||
kHAL_I2cTransferRepeatedStartFlag = 0x2U, /*!< A transfer starts with a repeated start signal. */
|
||||
kHAL_I2cTransferNoStopFlag = 0x4U, /*!< A transfer ends without a stop signal. */
|
||||
} hal_i2c_master_transfer_flag_t;
|
||||
|
||||
/*!
|
||||
* @brief Set of events sent to the callback for nonblocking slave transfers.
|
||||
*
|
||||
* These event enumerations are used for two related purposes. First, a bit mask created by OR'ing together
|
||||
* events is passed to I2C_SlaveTransferNonBlocking() to specify which events to enable.
|
||||
* Then, when the slave callback is invoked, it is passed the current event through its @a transfer
|
||||
* parameter.
|
||||
*
|
||||
* @note These enumerations are meant to be OR'd together to form a bit mask of events.
|
||||
*/
|
||||
typedef enum _hal_i2c_slave_transfer_event
|
||||
{
|
||||
kHAL_I2cSlaveAddressMatchEvent = 0x01U, /*!< Received the slave address after a start or repeated start. */
|
||||
kHAL_I2cSlaveTransmitEvent = 0x02U, /*!< A callback is requested to provide data to transmit
|
||||
(slave-transmitter role). */
|
||||
kHAL_I2cSlaveReceiveEvent = 0x04U, /*!< A callback is requested to provide a buffer in which to place received
|
||||
data (slave-receiver role). */
|
||||
kHAL_I2cSlaveTransmitAckEvent = 0x08U, /*!< A callback needs to either transmit an ACK or NACK. */
|
||||
kHAL_I2cSlaveCompletionEvent = 0x20U, /*!< A stop was detected or finished transfer, completing the transfer. */
|
||||
kHAL_I2cSlaveStartEvent = 0x10U, /*!< A start/repeated start was detected. */
|
||||
kHAL_I2cSlaveGenaralcallEvent = 0x40U, /*!< Received the general call address after a start or repeated start. */
|
||||
/*! A bit mask of all available events. */
|
||||
kHAL_I2cSlaveAllEvents = kHAL_I2cSlaveAddressMatchEvent | kHAL_I2cSlaveTransmitEvent | kHAL_I2cSlaveReceiveEvent |
|
||||
kHAL_I2cSlaveTransmitAckEvent | kHAL_I2cSlaveCompletionEvent | kHAL_I2cSlaveStartEvent |
|
||||
kHAL_I2cSlaveGenaralcallEvent,
|
||||
} hal_i2c_slave_transfer_event_t;
|
||||
|
||||
/*! @brief HAL I2C master transfer structure. */
|
||||
typedef struct _hal_i2c_master_transfer
|
||||
{
|
||||
uint8_t *volatile data; /*!< A transfer buffer. */
|
||||
volatile size_t dataSize; /*!< A transfer size. */
|
||||
uint32_t flags; /*!< A transfer flag which controls the transfer. */
|
||||
uint32_t subaddress; /*!< A sub address. Transferred MSB first. */
|
||||
uint8_t subaddressSize; /*!< A size of the command buffer. */
|
||||
uint8_t slaveAddress; /*!< 7-bit slave address. */
|
||||
hal_i2c_direction_t direction; /*!< A transfer direction, read or write. */
|
||||
} hal_i2c_master_transfer_t;
|
||||
|
||||
/*! @brief HAL I2C slave transfer structure. */
|
||||
typedef struct _hal_i2c_slave_transfer
|
||||
{
|
||||
hal_i2c_slave_transfer_event_t event; /*!< A reason that the callback is invoked. */
|
||||
uint8_t *volatile data; /*!< A transfer buffer. */
|
||||
volatile size_t dataSize; /*!< A transfer size. */
|
||||
hal_i2c_status_t completionStatus; /*!< Success or error code describing how the transfer completed. Only applies
|
||||
for #kHAL_I2cSlaveCompletionEvent. */
|
||||
size_t transferredCount; /*!< A number of bytes actually transferred since the start or since the last repeated
|
||||
start. */
|
||||
} hal_i2c_slave_transfer_t;
|
||||
|
||||
/*! @brief HAL I2C master handle. */
|
||||
typedef void *hal_i2c_master_handle_t;
|
||||
|
||||
/*! @brief HAL I2C slave handle. */
|
||||
typedef void *hal_i2c_slave_handle_t;
|
||||
|
||||
/*!
|
||||
* @brief Defines the I2C master handle
|
||||
*
|
||||
* This macro is used to define a 4 byte aligned I2C master handle.
|
||||
* Then use "(hal_i2c_master_handle_t)name" to get the I2C master handle.
|
||||
*
|
||||
* The macro should be global and could be optional. You could also define I2C master handle by yourself.
|
||||
*
|
||||
* This is an example,
|
||||
* @code
|
||||
* HAL_I2C_MASTER_HANDLE_DEFINE(i2cMasterHandle);
|
||||
* @endcode
|
||||
*
|
||||
* @param name The name string of the I2C master handle.
|
||||
*/
|
||||
#define HAL_I2C_MASTER_HANDLE_DEFINE(name) \
|
||||
uint32_t name[(HAL_I2C_MASTER_HANDLE_SIZE + sizeof(uint32_t) - 1U) / sizeof(uint32_t)]
|
||||
|
||||
/*!
|
||||
* @brief Defines the I2C slave handle
|
||||
*
|
||||
* This macro is used to define a 4 byte aligned I2C slave handle.
|
||||
* Then use "(hal_i2c_slave_handle_t)name" to get the I2C slave handle.
|
||||
*
|
||||
* The macro should be global and could be optional. You could also define I2C slave handle by yourself.
|
||||
*
|
||||
* This is an example,
|
||||
* @code
|
||||
* HAL_I2C_SLAVE_HANDLE_DEFINE(i2cSlaveHandle);
|
||||
* @endcode
|
||||
*
|
||||
* @param name The name string of the I2C slave handle.
|
||||
*/
|
||||
#define HAL_I2C_SLAVE_HANDLE_DEFINE(name) \
|
||||
uint32_t name[(HAL_I2C_SLAVE_HANDLE_SIZE + sizeof(uint32_t) - 1U) / sizeof(uint32_t)]
|
||||
|
||||
/*!
|
||||
* @brief Master completion callback function pointer type.
|
||||
*
|
||||
* This callback is used only for the non-blocking master transfer API. Specify the callback you wish to use
|
||||
* in the call to HAL_I2cMasterTransferInstallCallback().
|
||||
*
|
||||
* @param handle i2c master handle pointer, this should be a static variable.
|
||||
* @param completionStatus Either #kStatus_HAL_I2cSuccess or an error code describing how the transfer completed.
|
||||
* @param callbackParam Arbitrary pointer-sized value passed from the application.
|
||||
*/
|
||||
typedef void (*hal_i2c_master_transfer_callback_t)(hal_i2c_master_handle_t handle,
|
||||
hal_i2c_status_t completionStatus,
|
||||
void *callbackParam);
|
||||
|
||||
/*!
|
||||
* @brief Slave event callback function pointer type.
|
||||
*
|
||||
* This callback is used only for the slave non-blocking transfer API. Specify the callback you wish to use
|
||||
* in the call to HAL_I2cSlaveTransferInstallCallback().
|
||||
*
|
||||
* @param handle i2c slave master handle pointer, this should be a static variable.
|
||||
* @param transfer Pointer to transfer descriptor containing values passed to and/or from the callback.
|
||||
* @param callbackParam Arbitrary pointer-sized value passed from the application.
|
||||
*/
|
||||
typedef void (*hal_i2c_slave_transfer_callback_t)(hal_i2c_slave_handle_t handle,
|
||||
hal_i2c_slave_transfer_t *transfer,
|
||||
void *callbackParam);
|
||||
|
||||
/*******************************************************************************
|
||||
* API
|
||||
******************************************************************************/
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
#endif /*_cplusplus. */
|
||||
|
||||
/*!
|
||||
* @name Initialization and de-initialization
|
||||
* @{
|
||||
*/
|
||||
|
||||
/*!
|
||||
* @brief Initializes the HAL I2C master peripheral.
|
||||
*
|
||||
* @note This API should be called at the beginning of the application.
|
||||
* Otherwise, any operation to the HAL I2C module can cause a hard fault
|
||||
* because the clock is not enabled. This function configures the i2c master
|
||||
* with user-defined settings. The user can configure the configuration
|
||||
* structure. The parameter handle is a pointer to point to a memory space
|
||||
* of size #HAL_I2C_MASTER_HANDLE_SIZE allocated by the caller.
|
||||
*
|
||||
* Example below shows how to use this API to configure the I2C master.
|
||||
* @code
|
||||
* HAL_I2C_MASTER_HANDLE_DEFINE(i2cMasterHandle);
|
||||
* hal_i2c_master_config_t masterConfig;
|
||||
* masterConfig.enableMaster = true;
|
||||
* masterConfig.baudRate_Bps = 100000U;
|
||||
* masterConfig.srcClock_Hz = 12000000U;
|
||||
* masterConfig.instance = 0;
|
||||
* HAL_I2cMasterInit((hal_i2c_master_handle_t)i2cMasterHandle, &masterConfig);
|
||||
* @endcode
|
||||
*
|
||||
* @param handle Pointer to point to a memory space of size #HAL_I2C_MASTER_HANDLE_SIZE allocated by the caller.
|
||||
* The handle should be 4 byte aligned, because unaligned access doesn't be supported on some devices.
|
||||
* You can define the handle in the following two ways:
|
||||
* #HAL_I2C_MASTER_HANDLE_DEFINE(handle);
|
||||
* or
|
||||
* uint32_t handle[((HAL_I2C_MASTER_HANDLE_SIZE + sizeof(uint32_t) - 1U) / sizeof(uint32_t))];
|
||||
* @param config A pointer to the master configuration structure
|
||||
* @retval kStatus_HAL_I2cError An error occurred.
|
||||
* @retval kStatus_HAL_I2cSuccess i2c master initialization succeed
|
||||
*/
|
||||
hal_i2c_status_t HAL_I2cMasterInit(hal_i2c_master_handle_t handle, const hal_i2c_master_config_t *config);
|
||||
|
||||
/*!
|
||||
* @brief Initializes the HAL I2C peripheral.
|
||||
*
|
||||
* @note This API should be called at the beginning of the application.
|
||||
* Otherwise, any operation to the HAL I2C module can cause a hard fault
|
||||
* because the clock is not enabled. This function configures the i2c slave
|
||||
* with user-defined settings. The user can configure the configuration
|
||||
* structure. The parameter handle is a pointer to point to a memory space
|
||||
* of size #HAL_I2C_SLAVE_HANDLE_SIZE allocated by the caller.
|
||||
*
|
||||
* Example below shows how to use this API to configure the I2C slave.
|
||||
* @code
|
||||
* HAL_I2C_SLAVE_HANDLE_DEFINE(i2cSlaveHandle);
|
||||
* hal_i2c_slave_config_t slaveConfig;
|
||||
* slaveConfig.enableSlave = true;
|
||||
* slaveConfig.slaveAddress = 0x01U;
|
||||
* slaveConfig.srcClock_Hz = 12000000U;
|
||||
* slaveConfig.instance = 0;
|
||||
* HAL_I2cSlaveInit((hal_i2c_slave_handle_t)i2cSlaveHandle, &slaveConfig);
|
||||
* @endcode
|
||||
*
|
||||
* @param handle Pointer to point to a memory space of size #HAL_I2C_SLAVE_HANDLE_SIZE allocated by the caller.
|
||||
* The handle should be 4 byte aligned, because unaligned access doesn't be supported on some devices.
|
||||
* You can define the handle in the following two ways:
|
||||
* #HAL_I2C_SLAVE_HANDLE_DEFINE(handle);
|
||||
* or
|
||||
* uint32_t handle[((HAL_I2C_SLAVE_HANDLE_SIZE + sizeof(uint32_t) - 1U) / sizeof(uint32_t))];
|
||||
* @param config A pointer to the slave configuration structure
|
||||
* @retval kStatus_HAL_I2cError An error occurred.
|
||||
* @retval kStatus_HAL_I2cSuccess i2c slave initialization succeed
|
||||
*/
|
||||
hal_i2c_status_t HAL_I2cSlaveInit(hal_i2c_slave_handle_t handle, const hal_i2c_slave_config_t *config);
|
||||
|
||||
/*!
|
||||
* @brief De-initializes the HAL I2C master peripheral. Call this API to gate the HAL I2C clock.
|
||||
* The HAL I2C master module can't work unless the HAL_I2cMasterInit is called.
|
||||
*
|
||||
* @param handle i2c master handle pointer, this should be a static variable.
|
||||
* @retval kStatus_HAL_I2cSuccess i2c master de-initialization succeed */
|
||||
hal_i2c_status_t HAL_I2cMasterDeinit(hal_i2c_master_handle_t handle);
|
||||
|
||||
/*!
|
||||
* @brief De-initializes the HAL I2C slave peripheral. Calling this API gates the HAL I2C clock.
|
||||
* The HAL I2C slave module can't work unless the HAL_I2cSlaveInit is called to enable the clock.
|
||||
*
|
||||
* @param handle i2c slave handle pointer, this should be a static variable.
|
||||
* @retval kStatus_HAL_I2cSuccess i2c slave de-initialization succeed
|
||||
*/
|
||||
hal_i2c_status_t HAL_I2cSlaveDeinit(hal_i2c_slave_handle_t handle);
|
||||
|
||||
/*! @} */
|
||||
|
||||
/*!
|
||||
* @name Bus Operations
|
||||
* @{
|
||||
*/
|
||||
|
||||
/*!
|
||||
* @brief Performs a polling send transaction on the HAL I2C bus.
|
||||
*
|
||||
* @param handle i2c master handle pointer, this should be a static variable.
|
||||
* @param txBuff The pointer to the data to be transferred.
|
||||
* @param txSize The length in bytes of the data to be transferred.
|
||||
* @param flags Transfer control flag to decide whether need to send a stop, use kHAL_I2cTransferDefaultFlag
|
||||
* to issue a stop and kHAL_I2cTransferNoStopFlag to not send a stop.
|
||||
* @retval kStatus_HAL_I2cSuccess Successfully complete the data transmission.
|
||||
* @retval kStatus_HAL_I2cArbitrationLost Transfer error, arbitration lost.
|
||||
* @retval kStatus_HAL_I2cNak Transfer error, receive NAK during transfer.
|
||||
*/
|
||||
hal_i2c_status_t HAL_I2cMasterWriteBlocking(hal_i2c_master_handle_t handle,
|
||||
const uint8_t *txBuff,
|
||||
size_t txSize,
|
||||
uint32_t flags);
|
||||
|
||||
/*!
|
||||
* @brief Performs a polling receive transaction on the HAL I2C bus.
|
||||
*
|
||||
* @note The HAL_I2cMasterReadBlocking function stops the bus before reading the final byte.
|
||||
* Without stopping the bus prior for the final read, the bus issues another read, resulting
|
||||
* in garbage data being read into the data register.
|
||||
*
|
||||
* @param handle i2c master handle pointer, this should be a static variable.
|
||||
* @param rxBuff The pointer to the data to store the received data.
|
||||
* @param rxSize The length in bytes of the data to be received.
|
||||
* @param flags Transfer control flag to decide whether need to send a stop, use kHAL_I2cTransferDefaultFlag
|
||||
* to issue a stop and kHAL_I2cTransferNoStopFlag to not send a stop.
|
||||
* @retval kStatus_HAL_I2cSuccess Successfully complete the data transmission.
|
||||
* @retval kStatus_HAL_I2cTimeout Send stop signal failed, timeout.
|
||||
*/
|
||||
hal_i2c_status_t HAL_I2cMasterReadBlocking(hal_i2c_master_handle_t handle,
|
||||
uint8_t *rxBuff,
|
||||
size_t rxSize,
|
||||
uint32_t flags);
|
||||
|
||||
/*!
|
||||
* @brief Performs a polling send transaction on the HAL I2C bus.
|
||||
*
|
||||
* @param handle i2c slave handle pointer, this should be a static variable.
|
||||
* @param txBuff The pointer to the data to be transferred.
|
||||
* @param txSize The length in bytes of the data to be transferred.
|
||||
* @retval kStatus_HAL_I2cSuccess Successfully complete the data transmission.
|
||||
* @retval kStatus_HAL_I2cArbitrationLost Transfer error, arbitration lost.
|
||||
* @retval kStatus_HAL_I2cNak Transfer error, receive NAK during transfer.
|
||||
*/
|
||||
hal_i2c_status_t HAL_I2cSlaveWriteBlocking(hal_i2c_slave_handle_t handle, const uint8_t *txBuff, size_t txSize);
|
||||
|
||||
/*!
|
||||
* @brief Performs a polling receive transaction on the HAL I2C bus.
|
||||
*
|
||||
* @param handle i2c slave handle pointer, this should be a static variable.
|
||||
* @param rxBuff The pointer to the data to store the received data.
|
||||
* @param rxSize The length in bytes of the data to be received.
|
||||
* @retval kStatus_HAL_I2cSuccess Successfully complete data receive.
|
||||
* @retval kStatus_HAL_I2cTimeout Wait status flag timeout.
|
||||
*/
|
||||
hal_i2c_status_t HAL_I2cSlaveReadBlocking(hal_i2c_slave_handle_t handle, uint8_t *rxBuff, size_t rxSize);
|
||||
|
||||
/*!
|
||||
* @brief Performs a master polling transfer on the HAL I2C bus.
|
||||
*
|
||||
* @note The API does not return until the transfer succeeds or fails due
|
||||
* to arbitration lost or receiving a NAK.
|
||||
*
|
||||
* @param handle i2c master handle pointer, this should be a static variable.
|
||||
* @param xfer Pointer to the transfer structure.
|
||||
* @retval kStatus_HAL_I2cSuccess Successfully complete the data transmission.
|
||||
* @retval kStatus_HAL_I2cBusy Previous transmission still not finished.
|
||||
* @retval kStatus_HAL_I2cTimeout Transfer error, wait signal timeout.
|
||||
* @retval kStatus_HAL_I2cArbitrationLost Transfer error, arbitration lost.
|
||||
* @retval kStatus_HAL_I2cNak Transfer error, receive NAK during transfer.
|
||||
*/
|
||||
hal_i2c_status_t HAL_I2cMasterTransferBlocking(hal_i2c_master_handle_t handle, hal_i2c_master_transfer_t *xfer);
|
||||
|
||||
/*! @} */
|
||||
|
||||
/*!
|
||||
* @name Transactional
|
||||
* @{
|
||||
*/
|
||||
|
||||
/*!
|
||||
* @brief Installs a callback and callback parameter.
|
||||
*
|
||||
* This function is used to install the callback and callback parameter for i2c master module.
|
||||
* When any status of the i2c master changed, the driver will notify the upper layer by the installed callback
|
||||
* function. And the status is also passed as status parameter when the callback is called.
|
||||
*
|
||||
* @param handle i2c master handle pointer, this should be a static variable.
|
||||
* @param callback pointer to user callback function.
|
||||
* @param callbackParam user parameter passed to the callback function.
|
||||
* @retval kStatus_HAL_I2cSuccess i2c master handle created
|
||||
*/
|
||||
hal_i2c_status_t HAL_I2cMasterTransferInstallCallback(hal_i2c_master_handle_t handle,
|
||||
hal_i2c_master_transfer_callback_t callback,
|
||||
void *callbackParam);
|
||||
|
||||
/*!
|
||||
* @brief Performs a master interrupt non-blocking transfer on the HAL I2C bus.
|
||||
*
|
||||
* @note Calling the API returns immediately after transfer initiates. The user needs
|
||||
* to call HAL_I2cMasterGetTransferCount to poll the transfer status to check whether
|
||||
* the transfer is finished. If the return status is not kStatus_HAL_I2cBusy, the transfer
|
||||
* is finished.
|
||||
*
|
||||
* @param handle i2c master handle pointer, this should be a static variable.
|
||||
* @param xfer pointer to hal_i2c_master_transfer_t structure.
|
||||
* @retval kStatus_HAL_I2cSuccess Successfully start the data transmission.
|
||||
* @retval kStatus_HAL_I2cBusy Previous transmission still not finished.
|
||||
* @retval kStatus_HAL_I2cTimeout Transfer error, wait signal timeout.
|
||||
*/
|
||||
hal_i2c_status_t HAL_I2cMasterTransferNonBlocking(hal_i2c_master_handle_t handle, hal_i2c_master_transfer_t *xfer);
|
||||
|
||||
/*!
|
||||
* @brief Gets the master transfer status during a interrupt non-blocking transfer.
|
||||
*
|
||||
* @param handle i2c master handle pointer, this should be a static variable.
|
||||
* @param count Number of bytes transferred so far by the non-blocking transaction.
|
||||
* @retval kStatus_HAL_I2cError An error occurred.
|
||||
* @retval kStatus_HAL_I2cSuccess Successfully return the count.
|
||||
*/
|
||||
hal_i2c_status_t HAL_I2cMasterTransferGetCount(hal_i2c_master_handle_t handle, size_t *count);
|
||||
|
||||
/*!
|
||||
* @brief Aborts an interrupt non-blocking transfer early.
|
||||
*
|
||||
* @note This API can be called at any time when an interrupt non-blocking transfer initiates
|
||||
* to abort the transfer early.
|
||||
*
|
||||
* @param handle i2c master handle pointer, this should be a static variable.
|
||||
* @retval kStatus_HAL_I2cTimeout Timeout during polling flag.
|
||||
* @retval kStatus_HAL_I2cSuccess Successfully abort the transfer.
|
||||
*/
|
||||
hal_i2c_status_t HAL_I2cMasterTransferAbort(hal_i2c_master_handle_t handle);
|
||||
|
||||
/*!
|
||||
* @brief Installs a callback and callback parameter.
|
||||
*
|
||||
* This function is used to install the callback and callback parameter for i2c slave module.
|
||||
* When any status of the i2c slave changed, the driver will notify the upper layer by the installed callback
|
||||
* function. And the status is also passed as status parameter when the callback is called.
|
||||
*
|
||||
* @param handle i2c slave handle pointer, this should be a static variable.
|
||||
* @param callback pointer to user callback function.
|
||||
* @param callbackParam user parameter passed to the callback function.
|
||||
* @retval kStatus_HAL_I2cSuccess i2c slave handle created
|
||||
*/
|
||||
hal_i2c_status_t HAL_I2cSlaveTransferInstallCallback(hal_i2c_slave_handle_t handle,
|
||||
hal_i2c_slave_transfer_callback_t callback,
|
||||
void *callbackParam);
|
||||
|
||||
/*!
|
||||
* @brief Starts accepting slave transfers.
|
||||
*
|
||||
* Call this API after calling the HAL_I2cSlaveInit() and HAL_I2cSlaveTransferInstallCallback() to start processing
|
||||
* transactions driven by an HAL I2C slave. The slave monitors the HAL I2C bus and passes events to the
|
||||
* callback that was passed into the call to HAL_I2cSlaveTransferInstallCallback(). The callback is always invoked
|
||||
* from the interrupt context.
|
||||
*
|
||||
* The set of events received by the callback is customizable. To do so, set the @a eventMask parameter to
|
||||
* the OR'd combination of #hal_i2c_slave_transfer_event_t enumerators for the events you wish to receive.
|
||||
* The #kHAL_I2cSlaveTransmitEvent and #kHAL_I2cSlaveReceiveEvent events are always enabled and do not need
|
||||
* to be included in the mask. Alternatively, pass 0 to get a default set of only the transmit and
|
||||
* receive events that are always enabled. In addition, the #kHAL_I2cSlaveAllEvents constant is provided as
|
||||
* a convenient way to enable all events.
|
||||
*
|
||||
* @param handle i2c slave handle pointer, this should be a static variable.
|
||||
* @param eventMask Bit mask formed by OR'ing together #hal_i2c_slave_transfer_event_t enumerators to specify
|
||||
* which events to send to the callback. Other accepted values are 0 to get a default set of
|
||||
* only the transmit and receive events, and #kHAL_I2cSlaveAllEvents to enable all events.
|
||||
*
|
||||
* @retval #kStatus_HAL_I2cSuccess Slave transfers were successfully started.
|
||||
* @retval #kStatus_HAL_I2cBusy Slave transfers have already been started on this handle.
|
||||
*/
|
||||
hal_i2c_status_t HAL_I2cSlaveTransferNonBlocking(hal_i2c_slave_handle_t handle, uint32_t eventMask);
|
||||
|
||||
/*!
|
||||
* @brief Aborts the slave transfer.
|
||||
*
|
||||
* @note This API can be called at any time to stop slave for handling the bus events.
|
||||
*
|
||||
* @param handle i2c slave handle pointer, this should be a static variable.
|
||||
* @retval kStatus_HAL_I2cSuccess Successfully return the count.
|
||||
*/
|
||||
hal_i2c_status_t HAL_I2cSlaveTransferAbort(hal_i2c_slave_handle_t handle);
|
||||
|
||||
/*!
|
||||
* @brief Gets the slave transfer remaining bytes during a interrupt non-blocking transfer.
|
||||
*
|
||||
* @param handle i2c slave handle pointer, this should be a static variable.
|
||||
* @param count Number of bytes transferred so far by the non-blocking transaction.
|
||||
* @retval kStatus_HAL_I2cError An error occurred.
|
||||
* @retval kStatus_HAL_I2cSuccess Successfully return the count.
|
||||
*/
|
||||
hal_i2c_status_t HAL_I2cSlaveTransferGetCount(hal_i2c_slave_handle_t handle, size_t *count);
|
||||
|
||||
/*! @} */
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
#endif /*_cplusplus. */
|
||||
/*! @} */
|
||||
|
||||
#endif /* __HAL_I2C_ADAPTER_H__*/
|
@@ -1,369 +1,361 @@
|
||||
/*
|
||||
* Copyright 2018 NXP
|
||||
* All rights reserved.
|
||||
*
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
#include "fsl_common.h"
|
||||
#include "fsl_lpi2c.h"
|
||||
|
||||
#include "i2c.h"
|
||||
|
||||
/*******************************************************************************
|
||||
* Definitions
|
||||
******************************************************************************/
|
||||
|
||||
/*! @brief i2c master state structure. */
|
||||
typedef struct _hal_i2c_master
|
||||
{
|
||||
hal_i2c_master_transfer_callback_t callback;
|
||||
void *callbackParam;
|
||||
lpi2c_master_handle_t hardwareHandle;
|
||||
uint8_t instance;
|
||||
} hal_i2c_master_t;
|
||||
|
||||
/*! @brief i2c slave state structure. */
|
||||
typedef struct _hal_i2c_slave
|
||||
{
|
||||
hal_i2c_slave_transfer_callback_t callback;
|
||||
void *callbackParam;
|
||||
hal_i2c_slave_transfer_t transfer;
|
||||
lpi2c_slave_handle_t hardwareHandle;
|
||||
uint8_t instance;
|
||||
} hal_i2c_slave_t;
|
||||
|
||||
/*******************************************************************************
|
||||
* Prototypes
|
||||
******************************************************************************/
|
||||
|
||||
/*******************************************************************************
|
||||
* Variables
|
||||
******************************************************************************/
|
||||
|
||||
/*! @brief Pointers to i2c bases for each instance. */
|
||||
static LPI2C_Type *const s_i2cBases[] = LPI2C_BASE_PTRS;
|
||||
|
||||
/*******************************************************************************
|
||||
* Code
|
||||
******************************************************************************/
|
||||
|
||||
hal_i2c_status_t HAL_I2cGetStatus(status_t status)
|
||||
{
|
||||
hal_i2c_status_t returnStatus;
|
||||
switch (status)
|
||||
{
|
||||
case kStatus_Success:
|
||||
{
|
||||
returnStatus = kStatus_HAL_I2cSuccess;
|
||||
break;
|
||||
}
|
||||
case kStatus_LPI2C_Busy:
|
||||
{
|
||||
returnStatus = kStatus_HAL_I2cBusy;
|
||||
break;
|
||||
}
|
||||
case kStatus_LPI2C_Idle:
|
||||
{
|
||||
returnStatus = kStatus_HAL_I2cIdle;
|
||||
break;
|
||||
}
|
||||
case kStatus_LPI2C_Nak:
|
||||
{
|
||||
returnStatus = kStatus_HAL_I2cNak;
|
||||
break;
|
||||
}
|
||||
case kStatus_LPI2C_ArbitrationLost:
|
||||
{
|
||||
returnStatus = kStatus_HAL_I2cArbitrationLost;
|
||||
break;
|
||||
}
|
||||
case kStatus_LPI2C_Timeout:
|
||||
{
|
||||
returnStatus = kStatus_HAL_I2cTimeout;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
returnStatus = kStatus_HAL_I2cError;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return returnStatus;
|
||||
}
|
||||
|
||||
static void HAL_I2cMasterCallback(LPI2C_Type *base, lpi2c_master_handle_t *handle, status_t status, void *callbackParam)
|
||||
{
|
||||
hal_i2c_master_t *i2cMasterHandle;
|
||||
assert(callbackParam);
|
||||
|
||||
i2cMasterHandle = (hal_i2c_master_t *)callbackParam;
|
||||
|
||||
if (i2cMasterHandle->callback)
|
||||
{
|
||||
i2cMasterHandle->callback(i2cMasterHandle, HAL_I2cGetStatus(status), i2cMasterHandle->callbackParam);
|
||||
}
|
||||
}
|
||||
|
||||
static void HAL_I2cSlaveCallback(LPI2C_Type *base, lpi2c_slave_transfer_t *xfer, void *callbackParam)
|
||||
{
|
||||
hal_i2c_slave_t *i2cSlaveHandle;
|
||||
assert(callbackParam);
|
||||
|
||||
i2cSlaveHandle = (hal_i2c_slave_t *)callbackParam;
|
||||
|
||||
if (i2cSlaveHandle->callback)
|
||||
{
|
||||
i2cSlaveHandle->transfer.event = (hal_i2c_slave_transfer_event_t)xfer->event;
|
||||
i2cSlaveHandle->transfer.data = xfer->data;
|
||||
i2cSlaveHandle->transfer.dataSize = xfer->dataSize;
|
||||
i2cSlaveHandle->transfer.completionStatus = HAL_I2cGetStatus(xfer->completionStatus);
|
||||
i2cSlaveHandle->transfer.transferredCount = xfer->transferredCount;
|
||||
i2cSlaveHandle->callback(i2cSlaveHandle, &i2cSlaveHandle->transfer, i2cSlaveHandle->callbackParam);
|
||||
xfer->data = i2cSlaveHandle->transfer.data;
|
||||
xfer->dataSize = i2cSlaveHandle->transfer.dataSize;
|
||||
}
|
||||
}
|
||||
|
||||
hal_i2c_status_t HAL_I2cMasterInit(hal_i2c_master_handle_t handle, const hal_i2c_master_config_t *config)
|
||||
{
|
||||
hal_i2c_master_t *i2cMasterHandle;
|
||||
lpi2c_master_config_t i2cConfig;
|
||||
|
||||
assert(handle);
|
||||
assert(config);
|
||||
|
||||
if (HAL_I2C_MASTER_HANDLE_SIZE < sizeof(hal_i2c_master_t))
|
||||
{
|
||||
return kStatus_HAL_I2cError;
|
||||
}
|
||||
|
||||
i2cMasterHandle = (hal_i2c_master_t *)handle;
|
||||
|
||||
LPI2C_MasterGetDefaultConfig(&i2cConfig);
|
||||
i2cConfig.enableMaster = config->enableMaster;
|
||||
i2cConfig.baudRate_Hz = config->baudRate_Bps;
|
||||
i2cMasterHandle->instance = config->instance;
|
||||
|
||||
LPI2C_MasterInit(s_i2cBases[i2cMasterHandle->instance], &i2cConfig, config->srcClock_Hz);
|
||||
|
||||
return kStatus_HAL_I2cSuccess;
|
||||
}
|
||||
|
||||
hal_i2c_status_t HAL_I2cSlaveInit(hal_i2c_slave_handle_t handle, const hal_i2c_slave_config_t *config)
|
||||
{
|
||||
hal_i2c_slave_t *i2cSlaveHandle;
|
||||
lpi2c_slave_config_t i2cConfig;
|
||||
|
||||
assert(handle);
|
||||
assert(config);
|
||||
|
||||
if (HAL_I2C_SLAVE_HANDLE_SIZE < sizeof(hal_i2c_slave_t))
|
||||
{
|
||||
return kStatus_HAL_I2cError;
|
||||
}
|
||||
|
||||
i2cSlaveHandle = (hal_i2c_slave_t *)handle;
|
||||
|
||||
LPI2C_SlaveGetDefaultConfig(&i2cConfig);
|
||||
i2cConfig.enableSlave = config->enableSlave;
|
||||
i2cConfig.address0 = config->slaveAddress;
|
||||
i2cSlaveHandle->instance = config->instance;
|
||||
|
||||
LPI2C_SlaveInit(s_i2cBases[i2cSlaveHandle->instance], &i2cConfig, config->srcClock_Hz);
|
||||
|
||||
return kStatus_HAL_I2cSuccess;
|
||||
}
|
||||
|
||||
hal_i2c_status_t HAL_I2cMasterDeinit(hal_i2c_master_handle_t handle)
|
||||
{
|
||||
hal_i2c_master_t *i2cMasterHandle;
|
||||
|
||||
assert(handle);
|
||||
|
||||
i2cMasterHandle = (hal_i2c_master_t *)handle;
|
||||
|
||||
LPI2C_MasterDeinit(s_i2cBases[i2cMasterHandle->instance]);
|
||||
|
||||
return kStatus_HAL_I2cSuccess;
|
||||
}
|
||||
|
||||
hal_i2c_status_t HAL_I2cSlaveDeinit(hal_i2c_slave_handle_t handle)
|
||||
{
|
||||
hal_i2c_slave_t *i2cSlaveHandle;
|
||||
|
||||
assert(handle);
|
||||
|
||||
i2cSlaveHandle = (hal_i2c_slave_t *)handle;
|
||||
|
||||
LPI2C_SlaveDeinit(s_i2cBases[i2cSlaveHandle->instance]);
|
||||
|
||||
return kStatus_HAL_I2cSuccess;
|
||||
}
|
||||
|
||||
hal_i2c_status_t HAL_I2cMasterWriteBlocking(hal_i2c_master_handle_t handle,
|
||||
const uint8_t *txBuff,
|
||||
size_t txSize,
|
||||
uint32_t flags)
|
||||
{
|
||||
return kStatus_HAL_I2cError;
|
||||
}
|
||||
|
||||
hal_i2c_status_t HAL_I2cMasterReadBlocking(hal_i2c_master_handle_t handle,
|
||||
uint8_t *rxBuff,
|
||||
size_t rxSize,
|
||||
uint32_t flags)
|
||||
{
|
||||
return kStatus_HAL_I2cError;
|
||||
}
|
||||
|
||||
hal_i2c_status_t HAL_I2cSlaveWriteBlocking(hal_i2c_slave_handle_t handle, const uint8_t *txBuff, size_t txSize)
|
||||
{
|
||||
return kStatus_HAL_I2cError;
|
||||
}
|
||||
|
||||
hal_i2c_status_t HAL_I2cSlaveReadBlocking(hal_i2c_slave_handle_t handle, uint8_t *rxBuff, size_t rxSize)
|
||||
{
|
||||
return kStatus_HAL_I2cError;
|
||||
}
|
||||
|
||||
hal_i2c_status_t HAL_I2cMasterTransferBlocking(hal_i2c_master_handle_t handle, hal_i2c_master_transfer_t *xfer)
|
||||
{
|
||||
hal_i2c_master_t *i2cMasterHandle;
|
||||
lpi2c_master_transfer_t transfer;
|
||||
|
||||
assert(handle);
|
||||
assert(xfer);
|
||||
|
||||
i2cMasterHandle = (hal_i2c_master_t *)handle;
|
||||
|
||||
transfer.flags = xfer->flags;
|
||||
transfer.slaveAddress = xfer->slaveAddress;
|
||||
transfer.direction = (kHAL_I2cRead == xfer->direction) ? kLPI2C_Read : kLPI2C_Write;
|
||||
transfer.subaddress = xfer->subaddress;
|
||||
transfer.subaddressSize = xfer->subaddressSize;
|
||||
transfer.data = xfer->data;
|
||||
transfer.dataSize = xfer->dataSize;
|
||||
|
||||
return HAL_I2cGetStatus(LPI2C_MasterTransferBlocking(s_i2cBases[i2cMasterHandle->instance], &transfer));
|
||||
}
|
||||
|
||||
hal_i2c_status_t HAL_I2cMasterTransferInstallCallback(hal_i2c_master_handle_t handle,
|
||||
hal_i2c_master_transfer_callback_t callback,
|
||||
void *callbackParam)
|
||||
{
|
||||
hal_i2c_master_t *i2cMasterHandle;
|
||||
|
||||
assert(handle);
|
||||
|
||||
i2cMasterHandle = (hal_i2c_master_t *)handle;
|
||||
|
||||
i2cMasterHandle->callback = callback;
|
||||
i2cMasterHandle->callbackParam = callbackParam;
|
||||
LPI2C_MasterTransferCreateHandle(s_i2cBases[i2cMasterHandle->instance], &i2cMasterHandle->hardwareHandle,
|
||||
HAL_I2cMasterCallback, i2cMasterHandle);
|
||||
|
||||
return kStatus_HAL_I2cSuccess;
|
||||
}
|
||||
|
||||
hal_i2c_status_t HAL_I2cMasterTransferNonBlocking(hal_i2c_master_handle_t handle, hal_i2c_master_transfer_t *xfer)
|
||||
{
|
||||
hal_i2c_master_t *i2cMasterHandle;
|
||||
lpi2c_master_transfer_t transfer;
|
||||
|
||||
assert(handle);
|
||||
assert(xfer);
|
||||
|
||||
i2cMasterHandle = (hal_i2c_master_t *)handle;
|
||||
|
||||
transfer.flags = xfer->flags;
|
||||
transfer.slaveAddress = xfer->slaveAddress;
|
||||
transfer.direction = (kHAL_I2cRead == xfer->direction) ? kLPI2C_Read : kLPI2C_Write;
|
||||
transfer.subaddress = xfer->subaddress;
|
||||
transfer.subaddressSize = xfer->subaddressSize;
|
||||
transfer.data = xfer->data;
|
||||
transfer.dataSize = xfer->dataSize;
|
||||
return HAL_I2cGetStatus(LPI2C_MasterTransferNonBlocking(s_i2cBases[i2cMasterHandle->instance],
|
||||
&i2cMasterHandle->hardwareHandle, &transfer));
|
||||
}
|
||||
|
||||
hal_i2c_status_t HAL_I2cMasterTransferGetCount(hal_i2c_master_handle_t handle, size_t *count)
|
||||
{
|
||||
hal_i2c_master_t *i2cMasterHandle;
|
||||
|
||||
assert(handle);
|
||||
assert(count);
|
||||
|
||||
i2cMasterHandle = (hal_i2c_master_t *)handle;
|
||||
return HAL_I2cGetStatus(
|
||||
LPI2C_MasterTransferGetCount(s_i2cBases[i2cMasterHandle->instance], &i2cMasterHandle->hardwareHandle, count));
|
||||
}
|
||||
|
||||
hal_i2c_status_t HAL_I2cMasterTransferAbort(hal_i2c_master_handle_t handle)
|
||||
{
|
||||
hal_i2c_master_t *i2cMasterHandle;
|
||||
|
||||
assert(handle);
|
||||
|
||||
i2cMasterHandle = (hal_i2c_master_t *)handle;
|
||||
LPI2C_MasterTransferAbort(s_i2cBases[i2cMasterHandle->instance], &i2cMasterHandle->hardwareHandle);
|
||||
|
||||
return kStatus_HAL_I2cSuccess;
|
||||
}
|
||||
|
||||
hal_i2c_status_t HAL_I2cSlaveTransferInstallCallback(hal_i2c_slave_handle_t handle,
|
||||
hal_i2c_slave_transfer_callback_t callback,
|
||||
void *callbackParam)
|
||||
{
|
||||
hal_i2c_slave_t *i2cSlaveHandle;
|
||||
|
||||
assert(handle);
|
||||
|
||||
i2cSlaveHandle = (hal_i2c_slave_t *)handle;
|
||||
|
||||
i2cSlaveHandle->callback = callback;
|
||||
i2cSlaveHandle->callbackParam = callbackParam;
|
||||
LPI2C_SlaveTransferCreateHandle(s_i2cBases[i2cSlaveHandle->instance], &i2cSlaveHandle->hardwareHandle,
|
||||
HAL_I2cSlaveCallback, i2cSlaveHandle);
|
||||
|
||||
return kStatus_HAL_I2cSuccess;
|
||||
}
|
||||
|
||||
hal_i2c_status_t HAL_I2cSlaveTransferNonBlocking(hal_i2c_slave_handle_t handle, uint32_t eventMask)
|
||||
{
|
||||
hal_i2c_slave_t *i2cSlaveHandle;
|
||||
|
||||
assert(handle);
|
||||
|
||||
i2cSlaveHandle = (hal_i2c_slave_t *)handle;
|
||||
|
||||
return HAL_I2cGetStatus(LPI2C_SlaveTransferNonBlocking(s_i2cBases[i2cSlaveHandle->instance],
|
||||
&i2cSlaveHandle->hardwareHandle, eventMask));
|
||||
}
|
||||
|
||||
hal_i2c_status_t HAL_I2cSlaveTransferAbort(hal_i2c_slave_handle_t handle)
|
||||
{
|
||||
hal_i2c_slave_t *i2cSlaveHandle;
|
||||
|
||||
assert(handle);
|
||||
|
||||
i2cSlaveHandle = (hal_i2c_slave_t *)handle;
|
||||
|
||||
LPI2C_SlaveTransferAbort(s_i2cBases[i2cSlaveHandle->instance], &i2cSlaveHandle->hardwareHandle);
|
||||
|
||||
return kStatus_HAL_I2cSuccess;
|
||||
}
|
||||
|
||||
hal_i2c_status_t HAL_I2cSlaveTransferGetCount(hal_i2c_slave_handle_t handle, size_t *count)
|
||||
{
|
||||
hal_i2c_slave_t *i2cSlaveHandle;
|
||||
|
||||
assert(handle);
|
||||
assert(count);
|
||||
|
||||
i2cSlaveHandle = (hal_i2c_slave_t *)handle;
|
||||
|
||||
return HAL_I2cGetStatus(
|
||||
LPI2C_SlaveTransferGetCount(s_i2cBases[i2cSlaveHandle->instance], &i2cSlaveHandle->hardwareHandle, count));
|
||||
}
|
||||
/*
|
||||
* Copyright 2018 NXP
|
||||
* All rights reserved.
|
||||
*
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
#include "fsl_common.h"
|
||||
#include "fsl_lpi2c.h"
|
||||
|
||||
#include "fsl_adapter_i2c.h"
|
||||
|
||||
/*******************************************************************************
|
||||
* Definitions
|
||||
******************************************************************************/
|
||||
|
||||
/*! @brief i2c master state structure. */
|
||||
typedef struct _hal_i2c_master
|
||||
{
|
||||
hal_i2c_master_transfer_callback_t callback;
|
||||
void *callbackParam;
|
||||
lpi2c_master_handle_t hardwareHandle;
|
||||
uint8_t instance;
|
||||
} hal_i2c_master_t;
|
||||
|
||||
/*! @brief i2c slave state structure. */
|
||||
typedef struct _hal_i2c_slave
|
||||
{
|
||||
hal_i2c_slave_transfer_callback_t callback;
|
||||
void *callbackParam;
|
||||
hal_i2c_slave_transfer_t transfer;
|
||||
lpi2c_slave_handle_t hardwareHandle;
|
||||
uint8_t instance;
|
||||
} hal_i2c_slave_t;
|
||||
|
||||
/*******************************************************************************
|
||||
* Prototypes
|
||||
******************************************************************************/
|
||||
|
||||
/*******************************************************************************
|
||||
* Variables
|
||||
******************************************************************************/
|
||||
|
||||
/*! @brief Pointers to i2c bases for each instance. */
|
||||
static LPI2C_Type *const s_i2cBases[] = LPI2C_BASE_PTRS;
|
||||
|
||||
/*******************************************************************************
|
||||
* Code
|
||||
******************************************************************************/
|
||||
|
||||
static hal_i2c_status_t HAL_I2cGetStatus(status_t status)
|
||||
{
|
||||
hal_i2c_status_t returnStatus;
|
||||
switch (status)
|
||||
{
|
||||
case kStatus_Success:
|
||||
{
|
||||
returnStatus = kStatus_HAL_I2cSuccess;
|
||||
break;
|
||||
}
|
||||
case kStatus_LPI2C_Busy:
|
||||
{
|
||||
returnStatus = kStatus_HAL_I2cBusy;
|
||||
break;
|
||||
}
|
||||
case kStatus_LPI2C_Idle:
|
||||
{
|
||||
returnStatus = kStatus_HAL_I2cIdle;
|
||||
break;
|
||||
}
|
||||
case kStatus_LPI2C_Nak:
|
||||
{
|
||||
returnStatus = kStatus_HAL_I2cNak;
|
||||
break;
|
||||
}
|
||||
case kStatus_LPI2C_ArbitrationLost:
|
||||
{
|
||||
returnStatus = kStatus_HAL_I2cArbitrationLost;
|
||||
break;
|
||||
}
|
||||
case kStatus_LPI2C_Timeout:
|
||||
{
|
||||
returnStatus = kStatus_HAL_I2cTimeout;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
returnStatus = kStatus_HAL_I2cError;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return returnStatus;
|
||||
}
|
||||
|
||||
static void HAL_I2cMasterCallback(LPI2C_Type *base, lpi2c_master_handle_t *handle, status_t status, void *callbackParam)
|
||||
{
|
||||
hal_i2c_master_t *i2cMasterHandle;
|
||||
assert(callbackParam);
|
||||
|
||||
i2cMasterHandle = (hal_i2c_master_t *)callbackParam;
|
||||
|
||||
if (NULL != i2cMasterHandle->callback)
|
||||
{
|
||||
i2cMasterHandle->callback(i2cMasterHandle, HAL_I2cGetStatus(status), i2cMasterHandle->callbackParam);
|
||||
}
|
||||
}
|
||||
|
||||
static void HAL_I2cSlaveCallback(LPI2C_Type *base, lpi2c_slave_transfer_t *xfer, void *callbackParam)
|
||||
{
|
||||
hal_i2c_slave_t *i2cSlaveHandle;
|
||||
assert(callbackParam);
|
||||
|
||||
i2cSlaveHandle = (hal_i2c_slave_t *)callbackParam;
|
||||
|
||||
if (NULL != i2cSlaveHandle->callback)
|
||||
{
|
||||
i2cSlaveHandle->transfer.event = (hal_i2c_slave_transfer_event_t)xfer->event;
|
||||
i2cSlaveHandle->transfer.data = xfer->data;
|
||||
i2cSlaveHandle->transfer.dataSize = xfer->dataSize;
|
||||
i2cSlaveHandle->transfer.completionStatus = HAL_I2cGetStatus(xfer->completionStatus);
|
||||
i2cSlaveHandle->transfer.transferredCount = xfer->transferredCount;
|
||||
i2cSlaveHandle->callback(i2cSlaveHandle, &i2cSlaveHandle->transfer, i2cSlaveHandle->callbackParam);
|
||||
xfer->data = i2cSlaveHandle->transfer.data;
|
||||
xfer->dataSize = i2cSlaveHandle->transfer.dataSize;
|
||||
}
|
||||
}
|
||||
|
||||
hal_i2c_status_t HAL_I2cMasterInit(hal_i2c_master_handle_t handle, const hal_i2c_master_config_t *config)
|
||||
{
|
||||
hal_i2c_master_t *i2cMasterHandle;
|
||||
lpi2c_master_config_t i2cConfig;
|
||||
|
||||
assert(handle);
|
||||
assert(config);
|
||||
assert(HAL_I2C_MASTER_HANDLE_SIZE >= sizeof(hal_i2c_master_t));
|
||||
|
||||
i2cMasterHandle = (hal_i2c_master_t *)handle;
|
||||
|
||||
LPI2C_MasterGetDefaultConfig(&i2cConfig);
|
||||
i2cConfig.enableMaster = config->enableMaster;
|
||||
i2cConfig.baudRate_Hz = config->baudRate_Bps;
|
||||
i2cMasterHandle->instance = config->instance;
|
||||
|
||||
LPI2C_MasterInit(s_i2cBases[i2cMasterHandle->instance], &i2cConfig, config->srcClock_Hz);
|
||||
|
||||
return kStatus_HAL_I2cSuccess;
|
||||
}
|
||||
|
||||
hal_i2c_status_t HAL_I2cSlaveInit(hal_i2c_slave_handle_t handle, const hal_i2c_slave_config_t *config)
|
||||
{
|
||||
hal_i2c_slave_t *i2cSlaveHandle;
|
||||
lpi2c_slave_config_t i2cConfig;
|
||||
|
||||
assert(handle);
|
||||
assert(config);
|
||||
assert(HAL_I2C_SLAVE_HANDLE_SIZE >= sizeof(hal_i2c_slave_t));
|
||||
|
||||
i2cSlaveHandle = (hal_i2c_slave_t *)handle;
|
||||
|
||||
LPI2C_SlaveGetDefaultConfig(&i2cConfig);
|
||||
i2cConfig.enableSlave = config->enableSlave;
|
||||
i2cConfig.address0 = (uint8_t)config->slaveAddress;
|
||||
i2cSlaveHandle->instance = config->instance;
|
||||
|
||||
LPI2C_SlaveInit(s_i2cBases[i2cSlaveHandle->instance], &i2cConfig, config->srcClock_Hz);
|
||||
|
||||
return kStatus_HAL_I2cSuccess;
|
||||
}
|
||||
|
||||
hal_i2c_status_t HAL_I2cMasterDeinit(hal_i2c_master_handle_t handle)
|
||||
{
|
||||
hal_i2c_master_t *i2cMasterHandle;
|
||||
|
||||
assert(handle);
|
||||
|
||||
i2cMasterHandle = (hal_i2c_master_t *)handle;
|
||||
|
||||
LPI2C_MasterDeinit(s_i2cBases[i2cMasterHandle->instance]);
|
||||
|
||||
return kStatus_HAL_I2cSuccess;
|
||||
}
|
||||
|
||||
hal_i2c_status_t HAL_I2cSlaveDeinit(hal_i2c_slave_handle_t handle)
|
||||
{
|
||||
hal_i2c_slave_t *i2cSlaveHandle;
|
||||
|
||||
assert(handle);
|
||||
|
||||
i2cSlaveHandle = (hal_i2c_slave_t *)handle;
|
||||
|
||||
LPI2C_SlaveDeinit(s_i2cBases[i2cSlaveHandle->instance]);
|
||||
|
||||
return kStatus_HAL_I2cSuccess;
|
||||
}
|
||||
|
||||
hal_i2c_status_t HAL_I2cMasterWriteBlocking(hal_i2c_master_handle_t handle,
|
||||
const uint8_t *txBuff,
|
||||
size_t txSize,
|
||||
uint32_t flags)
|
||||
{
|
||||
return kStatus_HAL_I2cError;
|
||||
}
|
||||
|
||||
hal_i2c_status_t HAL_I2cMasterReadBlocking(hal_i2c_master_handle_t handle,
|
||||
uint8_t *rxBuff,
|
||||
size_t rxSize,
|
||||
uint32_t flags)
|
||||
{
|
||||
return kStatus_HAL_I2cError;
|
||||
}
|
||||
|
||||
hal_i2c_status_t HAL_I2cSlaveWriteBlocking(hal_i2c_slave_handle_t handle, const uint8_t *txBuff, size_t txSize)
|
||||
{
|
||||
return kStatus_HAL_I2cError;
|
||||
}
|
||||
|
||||
hal_i2c_status_t HAL_I2cSlaveReadBlocking(hal_i2c_slave_handle_t handle, uint8_t *rxBuff, size_t rxSize)
|
||||
{
|
||||
return kStatus_HAL_I2cError;
|
||||
}
|
||||
|
||||
hal_i2c_status_t HAL_I2cMasterTransferBlocking(hal_i2c_master_handle_t handle, hal_i2c_master_transfer_t *xfer)
|
||||
{
|
||||
hal_i2c_master_t *i2cMasterHandle;
|
||||
lpi2c_master_transfer_t transfer;
|
||||
|
||||
assert(handle);
|
||||
assert(xfer);
|
||||
|
||||
i2cMasterHandle = (hal_i2c_master_t *)handle;
|
||||
|
||||
transfer.flags = xfer->flags;
|
||||
transfer.slaveAddress = xfer->slaveAddress;
|
||||
transfer.direction = (kHAL_I2cRead == xfer->direction) ? kLPI2C_Read : kLPI2C_Write;
|
||||
transfer.subaddress = xfer->subaddress;
|
||||
transfer.subaddressSize = xfer->subaddressSize;
|
||||
transfer.data = xfer->data;
|
||||
transfer.dataSize = xfer->dataSize;
|
||||
|
||||
return HAL_I2cGetStatus(LPI2C_MasterTransferBlocking(s_i2cBases[i2cMasterHandle->instance], &transfer));
|
||||
}
|
||||
|
||||
hal_i2c_status_t HAL_I2cMasterTransferInstallCallback(hal_i2c_master_handle_t handle,
|
||||
hal_i2c_master_transfer_callback_t callback,
|
||||
void *callbackParam)
|
||||
{
|
||||
hal_i2c_master_t *i2cMasterHandle;
|
||||
|
||||
assert(handle);
|
||||
|
||||
i2cMasterHandle = (hal_i2c_master_t *)handle;
|
||||
|
||||
i2cMasterHandle->callback = callback;
|
||||
i2cMasterHandle->callbackParam = callbackParam;
|
||||
LPI2C_MasterTransferCreateHandle(s_i2cBases[i2cMasterHandle->instance], &i2cMasterHandle->hardwareHandle,
|
||||
HAL_I2cMasterCallback, i2cMasterHandle);
|
||||
|
||||
return kStatus_HAL_I2cSuccess;
|
||||
}
|
||||
|
||||
hal_i2c_status_t HAL_I2cMasterTransferNonBlocking(hal_i2c_master_handle_t handle, hal_i2c_master_transfer_t *xfer)
|
||||
{
|
||||
hal_i2c_master_t *i2cMasterHandle;
|
||||
lpi2c_master_transfer_t transfer;
|
||||
|
||||
assert(handle);
|
||||
assert(xfer);
|
||||
|
||||
i2cMasterHandle = (hal_i2c_master_t *)handle;
|
||||
|
||||
transfer.flags = xfer->flags;
|
||||
transfer.slaveAddress = xfer->slaveAddress;
|
||||
transfer.direction = (kHAL_I2cRead == xfer->direction) ? kLPI2C_Read : kLPI2C_Write;
|
||||
transfer.subaddress = xfer->subaddress;
|
||||
transfer.subaddressSize = xfer->subaddressSize;
|
||||
transfer.data = xfer->data;
|
||||
transfer.dataSize = xfer->dataSize;
|
||||
return HAL_I2cGetStatus(LPI2C_MasterTransferNonBlocking(s_i2cBases[i2cMasterHandle->instance],
|
||||
&i2cMasterHandle->hardwareHandle, &transfer));
|
||||
}
|
||||
|
||||
hal_i2c_status_t HAL_I2cMasterTransferGetCount(hal_i2c_master_handle_t handle, size_t *count)
|
||||
{
|
||||
hal_i2c_master_t *i2cMasterHandle;
|
||||
|
||||
assert(handle);
|
||||
assert(count);
|
||||
|
||||
i2cMasterHandle = (hal_i2c_master_t *)handle;
|
||||
return HAL_I2cGetStatus(
|
||||
LPI2C_MasterTransferGetCount(s_i2cBases[i2cMasterHandle->instance], &i2cMasterHandle->hardwareHandle, count));
|
||||
}
|
||||
|
||||
hal_i2c_status_t HAL_I2cMasterTransferAbort(hal_i2c_master_handle_t handle)
|
||||
{
|
||||
hal_i2c_master_t *i2cMasterHandle;
|
||||
|
||||
assert(handle);
|
||||
|
||||
i2cMasterHandle = (hal_i2c_master_t *)handle;
|
||||
LPI2C_MasterTransferAbort(s_i2cBases[i2cMasterHandle->instance], &i2cMasterHandle->hardwareHandle);
|
||||
|
||||
return kStatus_HAL_I2cSuccess;
|
||||
}
|
||||
|
||||
hal_i2c_status_t HAL_I2cSlaveTransferInstallCallback(hal_i2c_slave_handle_t handle,
|
||||
hal_i2c_slave_transfer_callback_t callback,
|
||||
void *callbackParam)
|
||||
{
|
||||
hal_i2c_slave_t *i2cSlaveHandle;
|
||||
|
||||
assert(handle);
|
||||
|
||||
i2cSlaveHandle = (hal_i2c_slave_t *)handle;
|
||||
|
||||
i2cSlaveHandle->callback = callback;
|
||||
i2cSlaveHandle->callbackParam = callbackParam;
|
||||
LPI2C_SlaveTransferCreateHandle(s_i2cBases[i2cSlaveHandle->instance], &i2cSlaveHandle->hardwareHandle,
|
||||
HAL_I2cSlaveCallback, i2cSlaveHandle);
|
||||
|
||||
return kStatus_HAL_I2cSuccess;
|
||||
}
|
||||
|
||||
hal_i2c_status_t HAL_I2cSlaveTransferNonBlocking(hal_i2c_slave_handle_t handle, uint32_t eventMask)
|
||||
{
|
||||
hal_i2c_slave_t *i2cSlaveHandle;
|
||||
|
||||
assert(handle);
|
||||
|
||||
i2cSlaveHandle = (hal_i2c_slave_t *)handle;
|
||||
|
||||
return HAL_I2cGetStatus(LPI2C_SlaveTransferNonBlocking(s_i2cBases[i2cSlaveHandle->instance],
|
||||
&i2cSlaveHandle->hardwareHandle, eventMask));
|
||||
}
|
||||
|
||||
hal_i2c_status_t HAL_I2cSlaveTransferAbort(hal_i2c_slave_handle_t handle)
|
||||
{
|
||||
hal_i2c_slave_t *i2cSlaveHandle;
|
||||
|
||||
assert(handle);
|
||||
|
||||
i2cSlaveHandle = (hal_i2c_slave_t *)handle;
|
||||
|
||||
LPI2C_SlaveTransferAbort(s_i2cBases[i2cSlaveHandle->instance], &i2cSlaveHandle->hardwareHandle);
|
||||
|
||||
return kStatus_HAL_I2cSuccess;
|
||||
}
|
||||
|
||||
hal_i2c_status_t HAL_I2cSlaveTransferGetCount(hal_i2c_slave_handle_t handle, size_t *count)
|
||||
{
|
||||
hal_i2c_slave_t *i2cSlaveHandle;
|
||||
|
||||
assert(handle);
|
||||
assert(count);
|
||||
|
||||
i2cSlaveHandle = (hal_i2c_slave_t *)handle;
|
||||
|
||||
return HAL_I2cGetStatus(
|
||||
LPI2C_SlaveTransferGetCount(s_i2cBases[i2cSlaveHandle->instance], &i2cSlaveHandle->hardwareHandle, count));
|
||||
}
|
@@ -1,423 +1,493 @@
|
||||
/*
|
||||
* Copyright 2018-2019 NXP
|
||||
* All rights reserved.
|
||||
*
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
/*! *********************************************************************************
|
||||
*************************************************************************************
|
||||
* Include
|
||||
*************************************************************************************
|
||||
********************************************************************************** */
|
||||
#include "fsl_common.h"
|
||||
#include "generic_list.h"
|
||||
|
||||
static list_status_t LIST_Scan(list_handle_t list, list_element_handle_t newElement)
|
||||
{
|
||||
list_element_handle_t element = list->head;
|
||||
|
||||
while (element != NULL)
|
||||
{
|
||||
if (element == newElement)
|
||||
{
|
||||
return kLIST_DuplicateError;
|
||||
}
|
||||
element = element->next;
|
||||
}
|
||||
return kLIST_Ok;
|
||||
}
|
||||
|
||||
/*! *********************************************************************************
|
||||
*************************************************************************************
|
||||
* Public functions
|
||||
*************************************************************************************
|
||||
********************************************************************************** */
|
||||
/*! *********************************************************************************
|
||||
* \brief Initialises the list descriptor.
|
||||
*
|
||||
* \param[in] list - LIST_ handle to init.
|
||||
* max - Maximum number of elements in list. 0 for unlimited.
|
||||
*
|
||||
* \return void.
|
||||
*
|
||||
* \pre
|
||||
*
|
||||
* \post
|
||||
*
|
||||
* \remarks
|
||||
*
|
||||
********************************************************************************** */
|
||||
void LIST_Init(list_handle_t list, uint32_t max)
|
||||
{
|
||||
list->head = NULL;
|
||||
list->tail = NULL;
|
||||
list->max = (uint16_t)max;
|
||||
list->size = 0;
|
||||
}
|
||||
|
||||
/*! *********************************************************************************
|
||||
* \brief Gets the list that contains the given element.
|
||||
*
|
||||
* \param[in] element - Handle of the element.
|
||||
*
|
||||
* \return NULL if element is orphan.
|
||||
* Handle of the list the element is inserted into.
|
||||
*
|
||||
* \pre
|
||||
*
|
||||
* \post
|
||||
*
|
||||
* \remarks
|
||||
*
|
||||
********************************************************************************** */
|
||||
list_handle_t LIST_GetList(list_element_handle_t element)
|
||||
{
|
||||
return element->list;
|
||||
}
|
||||
|
||||
/*! *********************************************************************************
|
||||
* \brief Links element to the tail of the list.
|
||||
*
|
||||
* \param[in] list - ID of list to insert into.
|
||||
* element - element to add
|
||||
*
|
||||
* \return kLIST_Full if list is full.
|
||||
* kLIST_Ok if insertion was successful.
|
||||
*
|
||||
* \pre
|
||||
*
|
||||
* \post
|
||||
*
|
||||
* \remarks
|
||||
*
|
||||
********************************************************************************** */
|
||||
list_status_t LIST_AddTail(list_handle_t list, list_element_handle_t element)
|
||||
{
|
||||
uint32_t regPrimask = DisableGlobalIRQ();
|
||||
|
||||
if ((list->max != 0U) && (list->max == list->size))
|
||||
{
|
||||
EnableGlobalIRQ(regPrimask);
|
||||
return kLIST_Full;
|
||||
}
|
||||
|
||||
if (kLIST_DuplicateError == LIST_Scan(list, element))
|
||||
{
|
||||
EnableGlobalIRQ(regPrimask);
|
||||
return kLIST_DuplicateError;
|
||||
}
|
||||
|
||||
if (list->size == 0U)
|
||||
{
|
||||
list->head = element;
|
||||
}
|
||||
else
|
||||
{
|
||||
list->tail->next = element;
|
||||
}
|
||||
element->prev = list->tail;
|
||||
element->next = NULL;
|
||||
element->list = list;
|
||||
list->tail = element;
|
||||
list->size++;
|
||||
|
||||
EnableGlobalIRQ(regPrimask);
|
||||
return kLIST_Ok;
|
||||
}
|
||||
|
||||
/*! *********************************************************************************
|
||||
* \brief Links element to the head of the list.
|
||||
*
|
||||
* \param[in] list - ID of list to insert into.
|
||||
* element - element to add
|
||||
*
|
||||
* \return kLIST_Full if list is full.
|
||||
* kLIST_Ok if insertion was successful.
|
||||
*
|
||||
* \pre
|
||||
*
|
||||
* \post
|
||||
*
|
||||
* \remarks
|
||||
*
|
||||
********************************************************************************** */
|
||||
list_status_t LIST_AddHead(list_handle_t list, list_element_handle_t element)
|
||||
{
|
||||
uint32_t regPrimask = DisableGlobalIRQ();
|
||||
|
||||
if ((list->max != 0U) && (list->max == list->size))
|
||||
{
|
||||
EnableGlobalIRQ(regPrimask);
|
||||
return kLIST_Full;
|
||||
}
|
||||
|
||||
if (kLIST_DuplicateError == LIST_Scan(list, element))
|
||||
{
|
||||
EnableGlobalIRQ(regPrimask);
|
||||
return kLIST_DuplicateError;
|
||||
}
|
||||
|
||||
if (list->size == 0U)
|
||||
{
|
||||
list->tail = element;
|
||||
}
|
||||
else
|
||||
{
|
||||
list->head->prev = element;
|
||||
}
|
||||
element->next = list->head;
|
||||
element->prev = NULL;
|
||||
element->list = list;
|
||||
list->head = element;
|
||||
list->size++;
|
||||
|
||||
EnableGlobalIRQ(regPrimask);
|
||||
return kLIST_Ok;
|
||||
}
|
||||
|
||||
/*! *********************************************************************************
|
||||
* \brief Unlinks element from the head of the list.
|
||||
*
|
||||
* \param[in] list - ID of list to remove from.
|
||||
*
|
||||
* \return NULL if list is empty.
|
||||
* ID of removed element(pointer) if removal was successful.
|
||||
*
|
||||
* \pre
|
||||
*
|
||||
* \post
|
||||
*
|
||||
* \remarks
|
||||
*
|
||||
********************************************************************************** */
|
||||
list_element_handle_t LIST_RemoveHead(list_handle_t list)
|
||||
{
|
||||
list_element_handle_t element;
|
||||
|
||||
uint32_t regPrimask = DisableGlobalIRQ();
|
||||
|
||||
if ((NULL == list) || (list->size == 0U))
|
||||
{
|
||||
EnableGlobalIRQ(regPrimask);
|
||||
return NULL; /*LIST_ is empty*/
|
||||
}
|
||||
|
||||
element = list->head;
|
||||
list->size--;
|
||||
if (list->size == 0U)
|
||||
{
|
||||
list->tail = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
element->next->prev = NULL;
|
||||
}
|
||||
list->head = element->next; /*Is NULL if element is head*/
|
||||
element->list = NULL;
|
||||
|
||||
EnableGlobalIRQ(regPrimask);
|
||||
return element;
|
||||
}
|
||||
|
||||
/*! *********************************************************************************
|
||||
* \brief Gets head element ID.
|
||||
*
|
||||
* \param[in] list - ID of list.
|
||||
*
|
||||
* \return NULL if list is empty.
|
||||
* ID of head element if list is not empty.
|
||||
*
|
||||
* \pre
|
||||
*
|
||||
* \post
|
||||
*
|
||||
* \remarks
|
||||
*
|
||||
********************************************************************************** */
|
||||
list_element_handle_t LIST_GetHead(list_handle_t list)
|
||||
{
|
||||
return list->head;
|
||||
}
|
||||
|
||||
/*! *********************************************************************************
|
||||
* \brief Gets next element ID.
|
||||
*
|
||||
* \param[in] element - ID of the element.
|
||||
*
|
||||
* \return NULL if element is tail.
|
||||
* ID of next element if exists.
|
||||
*
|
||||
* \pre
|
||||
*
|
||||
* \post
|
||||
*
|
||||
* \remarks
|
||||
*
|
||||
********************************************************************************** */
|
||||
list_element_handle_t LIST_GetNext(list_element_handle_t element)
|
||||
{
|
||||
return element->next;
|
||||
}
|
||||
|
||||
/*! *********************************************************************************
|
||||
* \brief Gets previous element ID.
|
||||
*
|
||||
* \param[in] element - ID of the element.
|
||||
*
|
||||
* \return NULL if element is head.
|
||||
* ID of previous element if exists.
|
||||
*
|
||||
* \pre
|
||||
*
|
||||
* \post
|
||||
*
|
||||
* \remarks
|
||||
*
|
||||
********************************************************************************** */
|
||||
list_element_handle_t LIST_GetPrev(list_element_handle_t element)
|
||||
{
|
||||
return element->prev;
|
||||
}
|
||||
|
||||
/*! *********************************************************************************
|
||||
* \brief Unlinks an element from its list.
|
||||
*
|
||||
* \param[in] element - ID of the element to remove.
|
||||
*
|
||||
* \return kLIST_OrphanElement if element is not part of any list.
|
||||
* kLIST_Ok if removal was successful.
|
||||
*
|
||||
* \pre
|
||||
*
|
||||
* \post
|
||||
*
|
||||
* \remarks
|
||||
*
|
||||
********************************************************************************** */
|
||||
list_status_t LIST_RemoveElement(list_element_handle_t element)
|
||||
{
|
||||
if (element->list == NULL)
|
||||
{
|
||||
return kLIST_OrphanElement; /*Element was previusly removed or never added*/
|
||||
}
|
||||
|
||||
uint32_t regPrimask = DisableGlobalIRQ();
|
||||
|
||||
if (element->prev == NULL) /*Element is head or solo*/
|
||||
{
|
||||
element->list->head = element->next; /*is null if solo*/
|
||||
}
|
||||
if (element->next == NULL) /*Element is tail or solo*/
|
||||
{
|
||||
element->list->tail = element->prev; /*is null if solo*/
|
||||
}
|
||||
if (element->prev != NULL) /*Element is not head*/
|
||||
{
|
||||
element->prev->next = element->next;
|
||||
}
|
||||
if (element->next != NULL) /*Element is not tail*/
|
||||
{
|
||||
element->next->prev = element->prev;
|
||||
}
|
||||
element->list->size--;
|
||||
element->list = NULL;
|
||||
|
||||
EnableGlobalIRQ(regPrimask);
|
||||
return kLIST_Ok;
|
||||
}
|
||||
|
||||
/*! *********************************************************************************
|
||||
* \brief Links an element in the previous position relative to a given member
|
||||
* of a list.
|
||||
*
|
||||
* \param[in] element - ID of a member of a list.
|
||||
* newElement - new element to insert before the given member.
|
||||
*
|
||||
* \return kLIST_OrphanElement if element is not part of any list.
|
||||
* kLIST_Full if list is full.
|
||||
* kLIST_Ok if insertion was successful.
|
||||
*
|
||||
* \pre
|
||||
*
|
||||
* \post
|
||||
*
|
||||
* \remarks
|
||||
*
|
||||
********************************************************************************** */
|
||||
list_status_t LIST_AddPrevElement(list_element_handle_t element, list_element_handle_t newElement)
|
||||
{
|
||||
if (element->list == NULL)
|
||||
{
|
||||
return kLIST_OrphanElement; /*Element was previusly removed or never added*/
|
||||
}
|
||||
uint32_t regPrimask = DisableGlobalIRQ();
|
||||
|
||||
if ((element->list->max != 0U) && (element->list->max == element->list->size))
|
||||
{
|
||||
EnableGlobalIRQ(regPrimask);
|
||||
return kLIST_Full;
|
||||
}
|
||||
|
||||
if (kLIST_DuplicateError == LIST_Scan(element->list, newElement))
|
||||
{
|
||||
EnableGlobalIRQ(regPrimask);
|
||||
return kLIST_DuplicateError;
|
||||
}
|
||||
|
||||
if (element->prev == NULL) /*Element is list head*/
|
||||
{
|
||||
element->list->head = newElement;
|
||||
}
|
||||
else
|
||||
{
|
||||
element->prev->next = newElement;
|
||||
}
|
||||
newElement->list = element->list;
|
||||
element->list->size++;
|
||||
newElement->next = element;
|
||||
newElement->prev = element->prev;
|
||||
element->prev = newElement;
|
||||
|
||||
EnableGlobalIRQ(regPrimask);
|
||||
return kLIST_Ok;
|
||||
}
|
||||
|
||||
/*! *********************************************************************************
|
||||
* \brief Gets the current size of a list.
|
||||
*
|
||||
* \param[in] list - ID of the list.
|
||||
*
|
||||
* \return Current size of the list.
|
||||
*
|
||||
* \pre
|
||||
*
|
||||
* \post
|
||||
*
|
||||
* \remarks
|
||||
*
|
||||
********************************************************************************** */
|
||||
uint32_t LIST_GetSize(list_handle_t list)
|
||||
{
|
||||
return list->size;
|
||||
}
|
||||
|
||||
/*! *********************************************************************************
|
||||
* \brief Gets the number of free places in the list.
|
||||
*
|
||||
* \param[in] list - ID of the list.
|
||||
*
|
||||
* \return Available size of the list.
|
||||
*
|
||||
* \pre
|
||||
*
|
||||
* \post
|
||||
*
|
||||
* \remarks
|
||||
*
|
||||
********************************************************************************** */
|
||||
uint32_t LIST_GetAvailableSize(list_handle_t list)
|
||||
{
|
||||
return ((uint32_t)list->max - (uint32_t)list->size);
|
||||
}
|
||||
/*
|
||||
* Copyright 2018-2019 NXP
|
||||
* All rights reserved.
|
||||
*
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
/*! *********************************************************************************
|
||||
*************************************************************************************
|
||||
* Include
|
||||
*************************************************************************************
|
||||
********************************************************************************** */
|
||||
#include "fsl_component_generic_list.h"
|
||||
|
||||
#if defined(OSA_USED)
|
||||
#include "fsl_os_abstraction.h"
|
||||
#if (defined(USE_RTOS) && (USE_RTOS > 0U))
|
||||
#define LIST_ENTER_CRITICAL() \
|
||||
OSA_SR_ALLOC(); \
|
||||
OSA_ENTER_CRITICAL()
|
||||
#define LIST_EXIT_CRITICAL() OSA_EXIT_CRITICAL()
|
||||
#else
|
||||
#define LIST_ENTER_CRITICAL()
|
||||
#define LIST_EXIT_CRITICAL()
|
||||
#endif
|
||||
#else
|
||||
#define LIST_ENTER_CRITICAL() uint32_t regPrimask = DisableGlobalIRQ();
|
||||
#define LIST_EXIT_CRITICAL() EnableGlobalIRQ(regPrimask);
|
||||
#endif
|
||||
|
||||
static list_status_t LIST_Error_Check(list_handle_t list, list_element_handle_t newElement)
|
||||
{
|
||||
list_status_t listStatus = kLIST_Ok;
|
||||
#if (defined(GENERIC_LIST_DUPLICATED_CHECKING) && (GENERIC_LIST_DUPLICATED_CHECKING > 0U))
|
||||
list_element_handle_t element = list->head;
|
||||
#endif
|
||||
if ((list->max != 0U) && (list->max == list->size))
|
||||
{
|
||||
listStatus = kLIST_Full; /*List is full*/
|
||||
}
|
||||
#if (defined(GENERIC_LIST_DUPLICATED_CHECKING) && (GENERIC_LIST_DUPLICATED_CHECKING > 0U))
|
||||
else
|
||||
{
|
||||
while (element != NULL) /*Scan list*/
|
||||
{
|
||||
/* Determine if element is duplicated */
|
||||
if (element == newElement)
|
||||
{
|
||||
listStatus = kLIST_DuplicateError;
|
||||
break;
|
||||
}
|
||||
element = element->next;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return listStatus;
|
||||
}
|
||||
|
||||
/*! *********************************************************************************
|
||||
*************************************************************************************
|
||||
* Public functions
|
||||
*************************************************************************************
|
||||
********************************************************************************** */
|
||||
/*! *********************************************************************************
|
||||
* \brief Initialises the list descriptor.
|
||||
*
|
||||
* \param[in] list - LIST_ handle to init.
|
||||
* max - Maximum number of elements in list. 0 for unlimited.
|
||||
*
|
||||
* \return void.
|
||||
*
|
||||
* \pre
|
||||
*
|
||||
* \post
|
||||
*
|
||||
* \remarks
|
||||
*
|
||||
********************************************************************************** */
|
||||
void LIST_Init(list_handle_t list, uint32_t max)
|
||||
{
|
||||
list->head = NULL;
|
||||
list->tail = NULL;
|
||||
list->max = (uint16_t)max;
|
||||
list->size = 0;
|
||||
}
|
||||
|
||||
/*! *********************************************************************************
|
||||
* \brief Gets the list that contains the given element.
|
||||
*
|
||||
* \param[in] element - Handle of the element.
|
||||
*
|
||||
* \return NULL if element is orphan.
|
||||
* Handle of the list the element is inserted into.
|
||||
*
|
||||
* \pre
|
||||
*
|
||||
* \post
|
||||
*
|
||||
* \remarks
|
||||
*
|
||||
********************************************************************************** */
|
||||
list_handle_t LIST_GetList(list_element_handle_t element)
|
||||
{
|
||||
return element->list;
|
||||
}
|
||||
|
||||
/*! *********************************************************************************
|
||||
* \brief Links element to the tail of the list.
|
||||
*
|
||||
* \param[in] list - ID of list to insert into.
|
||||
* element - element to add
|
||||
*
|
||||
* \return kLIST_Full if list is full.
|
||||
* kLIST_Ok if insertion was successful.
|
||||
*
|
||||
* \pre
|
||||
*
|
||||
* \post
|
||||
*
|
||||
* \remarks
|
||||
*
|
||||
********************************************************************************** */
|
||||
list_status_t LIST_AddTail(list_handle_t list, list_element_handle_t element)
|
||||
{
|
||||
LIST_ENTER_CRITICAL();
|
||||
list_status_t listStatus = kLIST_Ok;
|
||||
|
||||
listStatus = LIST_Error_Check(list, element);
|
||||
if (listStatus == kLIST_Ok) /* Avoiding list status error */
|
||||
{
|
||||
if (list->size == 0U)
|
||||
{
|
||||
list->head = element;
|
||||
}
|
||||
else
|
||||
{
|
||||
list->tail->next = element;
|
||||
}
|
||||
#if (defined(GENERIC_LIST_LIGHT) && (GENERIC_LIST_LIGHT > 0U))
|
||||
#else
|
||||
element->prev = list->tail;
|
||||
#endif
|
||||
element->list = list;
|
||||
element->next = NULL;
|
||||
list->tail = element;
|
||||
list->size++;
|
||||
}
|
||||
|
||||
LIST_EXIT_CRITICAL();
|
||||
return listStatus;
|
||||
}
|
||||
|
||||
/*! *********************************************************************************
|
||||
* \brief Links element to the head of the list.
|
||||
*
|
||||
* \param[in] list - ID of list to insert into.
|
||||
* element - element to add
|
||||
*
|
||||
* \return kLIST_Full if list is full.
|
||||
* kLIST_Ok if insertion was successful.
|
||||
*
|
||||
* \pre
|
||||
*
|
||||
* \post
|
||||
*
|
||||
* \remarks
|
||||
*
|
||||
********************************************************************************** */
|
||||
list_status_t LIST_AddHead(list_handle_t list, list_element_handle_t element)
|
||||
{
|
||||
LIST_ENTER_CRITICAL();
|
||||
list_status_t listStatus = kLIST_Ok;
|
||||
|
||||
listStatus = LIST_Error_Check(list, element);
|
||||
if (listStatus == kLIST_Ok) /* Avoiding list status error */
|
||||
{
|
||||
/* Links element to the head of the list */
|
||||
if (list->size == 0U)
|
||||
{
|
||||
list->tail = element;
|
||||
}
|
||||
#if (defined(GENERIC_LIST_LIGHT) && (GENERIC_LIST_LIGHT > 0U))
|
||||
#else
|
||||
else
|
||||
{
|
||||
list->head->prev = element;
|
||||
}
|
||||
element->prev = NULL;
|
||||
#endif
|
||||
element->list = list;
|
||||
element->next = list->head;
|
||||
list->head = element;
|
||||
list->size++;
|
||||
}
|
||||
|
||||
LIST_EXIT_CRITICAL();
|
||||
return listStatus;
|
||||
}
|
||||
|
||||
/*! *********************************************************************************
|
||||
* \brief Unlinks element from the head of the list.
|
||||
*
|
||||
* \param[in] list - ID of list to remove from.
|
||||
*
|
||||
* \return NULL if list is empty.
|
||||
* ID of removed element(pointer) if removal was successful.
|
||||
*
|
||||
* \pre
|
||||
*
|
||||
* \post
|
||||
*
|
||||
* \remarks
|
||||
*
|
||||
********************************************************************************** */
|
||||
list_element_handle_t LIST_RemoveHead(list_handle_t list)
|
||||
{
|
||||
list_element_handle_t element;
|
||||
|
||||
LIST_ENTER_CRITICAL();
|
||||
|
||||
if ((NULL == list) || (list->size == 0U))
|
||||
{
|
||||
element = NULL; /*LIST_ is empty*/
|
||||
}
|
||||
else
|
||||
{
|
||||
element = list->head;
|
||||
list->size--;
|
||||
if (list->size == 0U)
|
||||
{
|
||||
list->tail = NULL;
|
||||
}
|
||||
#if (defined(GENERIC_LIST_LIGHT) && (GENERIC_LIST_LIGHT > 0U))
|
||||
#else
|
||||
else
|
||||
{
|
||||
element->next->prev = NULL;
|
||||
}
|
||||
#endif
|
||||
element->list = NULL;
|
||||
list->head = element->next; /*Is NULL if element is head*/
|
||||
}
|
||||
|
||||
LIST_EXIT_CRITICAL();
|
||||
return element;
|
||||
}
|
||||
|
||||
/*! *********************************************************************************
|
||||
* \brief Gets head element ID.
|
||||
*
|
||||
* \param[in] list - ID of list.
|
||||
*
|
||||
* \return NULL if list is empty.
|
||||
* ID of head element if list is not empty.
|
||||
*
|
||||
* \pre
|
||||
*
|
||||
* \post
|
||||
*
|
||||
* \remarks
|
||||
*
|
||||
********************************************************************************** */
|
||||
list_element_handle_t LIST_GetHead(list_handle_t list)
|
||||
{
|
||||
return list->head;
|
||||
}
|
||||
|
||||
/*! *********************************************************************************
|
||||
* \brief Gets next element ID.
|
||||
*
|
||||
* \param[in] element - ID of the element.
|
||||
*
|
||||
* \return NULL if element is tail.
|
||||
* ID of next element if exists.
|
||||
*
|
||||
* \pre
|
||||
*
|
||||
* \post
|
||||
*
|
||||
* \remarks
|
||||
*
|
||||
********************************************************************************** */
|
||||
list_element_handle_t LIST_GetNext(list_element_handle_t element)
|
||||
{
|
||||
return element->next;
|
||||
}
|
||||
|
||||
/*! *********************************************************************************
|
||||
* \brief Gets previous element ID.
|
||||
*
|
||||
* \param[in] element - ID of the element.
|
||||
*
|
||||
* \return NULL if element is head.
|
||||
* ID of previous element if exists.
|
||||
*
|
||||
* \pre
|
||||
*
|
||||
* \post
|
||||
*
|
||||
* \remarks
|
||||
*
|
||||
********************************************************************************** */
|
||||
list_element_handle_t LIST_GetPrev(list_element_handle_t element)
|
||||
{
|
||||
#if (defined(GENERIC_LIST_LIGHT) && (GENERIC_LIST_LIGHT > 0U))
|
||||
return NULL;
|
||||
#else
|
||||
return element->prev;
|
||||
#endif
|
||||
}
|
||||
|
||||
/*! *********************************************************************************
|
||||
* \brief Unlinks an element from its list.
|
||||
*
|
||||
* \param[in] element - ID of the element to remove.
|
||||
*
|
||||
* \return kLIST_OrphanElement if element is not part of any list.
|
||||
* kLIST_Ok if removal was successful.
|
||||
*
|
||||
* \pre
|
||||
*
|
||||
* \post
|
||||
*
|
||||
* \remarks
|
||||
*
|
||||
********************************************************************************** */
|
||||
list_status_t LIST_RemoveElement(list_element_handle_t element)
|
||||
{
|
||||
list_status_t listStatus = kLIST_Ok;
|
||||
LIST_ENTER_CRITICAL();
|
||||
|
||||
if (element->list == NULL)
|
||||
{
|
||||
listStatus = kLIST_OrphanElement; /*Element was previusly removed or never added*/
|
||||
}
|
||||
else
|
||||
{
|
||||
#if (defined(GENERIC_LIST_LIGHT) && (GENERIC_LIST_LIGHT > 0U))
|
||||
list_element_handle_t element_list = element->list->head;
|
||||
while (NULL != element_list)
|
||||
{
|
||||
if (element->list->head == element)
|
||||
{
|
||||
element->list->head = element_list->next;
|
||||
break;
|
||||
}
|
||||
if (element_list->next == element)
|
||||
{
|
||||
element_list->next = element->next;
|
||||
break;
|
||||
}
|
||||
element_list = element_list->next;
|
||||
}
|
||||
#else
|
||||
if (element->prev == NULL) /*Element is head or solo*/
|
||||
{
|
||||
element->list->head = element->next; /*is null if solo*/
|
||||
}
|
||||
if (element->next == NULL) /*Element is tail or solo*/
|
||||
{
|
||||
element->list->tail = element->prev; /*is null if solo*/
|
||||
}
|
||||
if (element->prev != NULL) /*Element is not head*/
|
||||
{
|
||||
element->prev->next = element->next;
|
||||
}
|
||||
if (element->next != NULL) /*Element is not tail*/
|
||||
{
|
||||
element->next->prev = element->prev;
|
||||
}
|
||||
#endif
|
||||
element->list->size--;
|
||||
element->list = NULL;
|
||||
}
|
||||
|
||||
LIST_EXIT_CRITICAL();
|
||||
return listStatus;
|
||||
}
|
||||
|
||||
/*! *********************************************************************************
|
||||
* \brief Links an element in the previous position relative to a given member
|
||||
* of a list.
|
||||
*
|
||||
* \param[in] element - ID of a member of a list.
|
||||
* newElement - new element to insert before the given member.
|
||||
*
|
||||
* \return kLIST_OrphanElement if element is not part of any list.
|
||||
* kLIST_Full if list is full.
|
||||
* kLIST_Ok if insertion was successful.
|
||||
*
|
||||
* \pre
|
||||
*
|
||||
* \post
|
||||
*
|
||||
* \remarks
|
||||
*
|
||||
********************************************************************************** */
|
||||
list_status_t LIST_AddPrevElement(list_element_handle_t element, list_element_handle_t newElement)
|
||||
{
|
||||
list_status_t listStatus = kLIST_Ok;
|
||||
LIST_ENTER_CRITICAL();
|
||||
|
||||
if (element->list == NULL)
|
||||
{
|
||||
listStatus = kLIST_OrphanElement; /*Element was previusly removed or never added*/
|
||||
}
|
||||
else
|
||||
{
|
||||
listStatus = LIST_Error_Check(element->list, newElement);
|
||||
if (listStatus == kLIST_Ok)
|
||||
{
|
||||
#if (defined(GENERIC_LIST_LIGHT) && (GENERIC_LIST_LIGHT > 0U))
|
||||
list_element_handle_t element_list = element->list->head;
|
||||
while (NULL != element_list)
|
||||
{
|
||||
if ((element_list->next == element) || (element_list == element))
|
||||
{
|
||||
if (element_list == element)
|
||||
{
|
||||
element->list->head = newElement;
|
||||
}
|
||||
else
|
||||
{
|
||||
element_list->next = newElement;
|
||||
}
|
||||
newElement->list = element->list;
|
||||
newElement->next = element;
|
||||
element->list->size++;
|
||||
break;
|
||||
}
|
||||
element_list = element_list->next;
|
||||
}
|
||||
|
||||
#else
|
||||
if (element->prev == NULL) /*Element is list head*/
|
||||
{
|
||||
element->list->head = newElement;
|
||||
}
|
||||
else
|
||||
{
|
||||
element->prev->next = newElement;
|
||||
}
|
||||
newElement->list = element->list;
|
||||
element->list->size++;
|
||||
newElement->next = element;
|
||||
newElement->prev = element->prev;
|
||||
element->prev = newElement;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
LIST_EXIT_CRITICAL();
|
||||
return listStatus;
|
||||
}
|
||||
|
||||
/*! *********************************************************************************
|
||||
* \brief Gets the current size of a list.
|
||||
*
|
||||
* \param[in] list - ID of the list.
|
||||
*
|
||||
* \return Current size of the list.
|
||||
*
|
||||
* \pre
|
||||
*
|
||||
* \post
|
||||
*
|
||||
* \remarks
|
||||
*
|
||||
********************************************************************************** */
|
||||
uint32_t LIST_GetSize(list_handle_t list)
|
||||
{
|
||||
return list->size;
|
||||
}
|
||||
|
||||
/*! *********************************************************************************
|
||||
* \brief Gets the number of free places in the list.
|
||||
*
|
||||
* \param[in] list - ID of the list.
|
||||
*
|
||||
* \return Available size of the list.
|
||||
*
|
||||
* \pre
|
||||
*
|
||||
* \post
|
||||
*
|
||||
* \remarks
|
||||
*
|
||||
********************************************************************************** */
|
||||
uint32_t LIST_GetAvailableSize(list_handle_t list)
|
||||
{
|
||||
return ((uint32_t)list->max - (uint32_t)list->size); /*Gets the number of free places in the list*/
|
||||
}
|
@@ -1,191 +1,201 @@
|
||||
/*
|
||||
* Copyright 2018-2019 NXP
|
||||
* All rights reserved.
|
||||
*
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
#ifndef _GENERIC_LIST_H_
|
||||
#define _GENERIC_LIST_H_
|
||||
|
||||
/*!
|
||||
* @addtogroup GenericList
|
||||
* @{
|
||||
*/
|
||||
|
||||
/*!*********************************************************************************
|
||||
*************************************************************************************
|
||||
* Include
|
||||
*************************************************************************************
|
||||
********************************************************************************** */
|
||||
|
||||
/*! *********************************************************************************
|
||||
*************************************************************************************
|
||||
* Public macro definitions
|
||||
*************************************************************************************
|
||||
********************************************************************************** */
|
||||
|
||||
/*! *********************************************************************************
|
||||
*************************************************************************************
|
||||
* Public type definitions
|
||||
*************************************************************************************
|
||||
********************************************************************************** */
|
||||
/*! @brief The list status */
|
||||
typedef enum _list_status
|
||||
{
|
||||
kLIST_Ok = kStatus_Success, /*!< Success */
|
||||
kLIST_DuplicateError = MAKE_STATUS(kStatusGroup_LIST, 1), /*!< Duplicate Error */
|
||||
kLIST_Full = MAKE_STATUS(kStatusGroup_LIST, 2), /*!< FULL */
|
||||
kLIST_Empty = MAKE_STATUS(kStatusGroup_LIST, 3), /*!< Empty */
|
||||
kLIST_OrphanElement = MAKE_STATUS(kStatusGroup_LIST, 4), /*!< Orphan Element */
|
||||
} list_status_t;
|
||||
|
||||
/*! @brief The list structure*/
|
||||
typedef struct list_label
|
||||
{
|
||||
struct list_element_tag *head; /*!< list head */
|
||||
struct list_element_tag *tail; /*!< list tail */
|
||||
uint16_t size; /*!< list size */
|
||||
uint16_t max; /*!< list max number of elements */
|
||||
} list_label_t, *list_handle_t;
|
||||
|
||||
/*! @brief The list element*/
|
||||
typedef struct list_element_tag
|
||||
{
|
||||
struct list_element_tag *next; /*!< next list element */
|
||||
struct list_element_tag *prev; /*!< previous list element */
|
||||
struct list_label *list; /*!< pointer to the list */
|
||||
} list_element_t, *list_element_handle_t;
|
||||
|
||||
/*! *********************************************************************************
|
||||
*************************************************************************************
|
||||
* Public prototypes
|
||||
*************************************************************************************
|
||||
********************************************************************************** */
|
||||
/*******************************************************************************
|
||||
* API
|
||||
******************************************************************************/
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
#endif /* _cplusplus */
|
||||
/*!
|
||||
* @brief Initialize the list.
|
||||
*
|
||||
* This function initialize the list.
|
||||
*
|
||||
* @param list - List handle to initialize.
|
||||
* @param max - Maximum number of elements in list. 0 for unlimited.
|
||||
*/
|
||||
void LIST_Init(list_handle_t list, uint32_t max);
|
||||
|
||||
/*!
|
||||
* @brief Gets the list that contains the given element.
|
||||
*
|
||||
*
|
||||
* @param element - Handle of the element.
|
||||
* @retval NULL if element is orphan, Handle of the list the element is inserted into.
|
||||
*/
|
||||
list_handle_t LIST_GetList(list_element_handle_t element);
|
||||
|
||||
/*!
|
||||
* @brief Links element to the head of the list.
|
||||
*
|
||||
* @param list - Handle of the list.
|
||||
* @param element - Handle of the element.
|
||||
* @retval kLIST_Full if list is full, kLIST_Ok if insertion was successful.
|
||||
*/
|
||||
list_status_t LIST_AddHead(list_handle_t list, list_element_handle_t element);
|
||||
|
||||
/*!
|
||||
* @brief Links element to the tail of the list.
|
||||
*
|
||||
* @param list - Handle of the list.
|
||||
* @param element - Handle of the element.
|
||||
* @retval kLIST_Full if list is full, kLIST_Ok if insertion was successful.
|
||||
*/
|
||||
list_status_t LIST_AddTail(list_handle_t list, list_element_handle_t element);
|
||||
|
||||
/*!
|
||||
* @brief Unlinks element from the head of the list.
|
||||
*
|
||||
* @param list - Handle of the list.
|
||||
*
|
||||
* @retval NULL if list is empty, handle of removed element(pointer) if removal was successful.
|
||||
*/
|
||||
list_element_handle_t LIST_RemoveHead(list_handle_t list);
|
||||
|
||||
/*!
|
||||
* @brief Gets head element handle.
|
||||
*
|
||||
* @param list - Handle of the list.
|
||||
*
|
||||
* @retval NULL if list is empty, handle of removed element(pointer) if removal was successful.
|
||||
*/
|
||||
list_element_handle_t LIST_GetHead(list_handle_t list);
|
||||
|
||||
/*!
|
||||
* @brief Gets next element handle for given element handle.
|
||||
*
|
||||
* @param element - Handle of the element.
|
||||
*
|
||||
* @retval NULL if list is empty, handle of removed element(pointer) if removal was successful.
|
||||
*/
|
||||
list_element_handle_t LIST_GetNext(list_element_handle_t element);
|
||||
|
||||
/*!
|
||||
* @brief Gets previous element handle for given element handle.
|
||||
*
|
||||
* @param element - Handle of the element.
|
||||
*
|
||||
* @retval NULL if list is empty, handle of removed element(pointer) if removal was successful.
|
||||
*/
|
||||
list_element_handle_t LIST_GetPrev(list_element_handle_t element);
|
||||
|
||||
/*!
|
||||
* @brief Unlinks an element from its list.
|
||||
*
|
||||
* @param element - Handle of the element.
|
||||
*
|
||||
* @retval kLIST_OrphanElement if element is not part of any list.
|
||||
* @retval kLIST_Ok if removal was successful.
|
||||
*/
|
||||
list_status_t LIST_RemoveElement(list_element_handle_t element);
|
||||
|
||||
/*!
|
||||
* @brief Links an element in the previous position relative to a given member of a list.
|
||||
*
|
||||
* @param element - Handle of the element.
|
||||
* @param newElement - New element to insert before the given member.
|
||||
*
|
||||
* @retval kLIST_OrphanElement if element is not part of any list.
|
||||
* @retval kLIST_Ok if removal was successful.
|
||||
*/
|
||||
list_status_t LIST_AddPrevElement(list_element_handle_t element, list_element_handle_t newElement);
|
||||
|
||||
/*!
|
||||
* @brief Gets the current size of a list.
|
||||
*
|
||||
* @param list - Handle of the list.
|
||||
*
|
||||
* @retval Current size of the list.
|
||||
*/
|
||||
uint32_t LIST_GetSize(list_handle_t list);
|
||||
|
||||
/*!
|
||||
* @brief Gets the number of free places in the list.
|
||||
*
|
||||
* @param list - Handle of the list.
|
||||
*
|
||||
* @retval Available size of the list.
|
||||
*/
|
||||
uint32_t LIST_GetAvailableSize(list_handle_t list);
|
||||
|
||||
/* @} */
|
||||
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
#endif
|
||||
/*! @}*/
|
||||
#endif /*_GENERIC_LIST_H_*/
|
||||
/*
|
||||
* Copyright 2018-2020 NXP
|
||||
* All rights reserved.
|
||||
*
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
#ifndef _GENERIC_LIST_H_
|
||||
#define _GENERIC_LIST_H_
|
||||
|
||||
#include "fsl_common.h"
|
||||
/*!
|
||||
* @addtogroup GenericList
|
||||
* @{
|
||||
*/
|
||||
|
||||
/**********************************************************************************
|
||||
* Include
|
||||
***********************************************************************************/
|
||||
|
||||
/**********************************************************************************
|
||||
* Public macro definitions
|
||||
***********************************************************************************/
|
||||
/*! @brief Definition to determine whether use list light. */
|
||||
#ifndef GENERIC_LIST_LIGHT
|
||||
#define GENERIC_LIST_LIGHT (1)
|
||||
#endif
|
||||
|
||||
/*! @brief Definition to determine whether enable list duplicated checking. */
|
||||
#ifndef GENERIC_LIST_DUPLICATED_CHECKING
|
||||
#define GENERIC_LIST_DUPLICATED_CHECKING (0)
|
||||
#endif
|
||||
|
||||
/**********************************************************************************
|
||||
* Public type definitions
|
||||
***********************************************************************************/
|
||||
/*! @brief The list status */
|
||||
typedef enum _list_status
|
||||
{
|
||||
kLIST_Ok = kStatus_Success, /*!< Success */
|
||||
kLIST_DuplicateError = MAKE_STATUS(kStatusGroup_LIST, 1), /*!< Duplicate Error */
|
||||
kLIST_Full = MAKE_STATUS(kStatusGroup_LIST, 2), /*!< FULL */
|
||||
kLIST_Empty = MAKE_STATUS(kStatusGroup_LIST, 3), /*!< Empty */
|
||||
kLIST_OrphanElement = MAKE_STATUS(kStatusGroup_LIST, 4), /*!< Orphan Element */
|
||||
kLIST_NotSupport = MAKE_STATUS(kStatusGroup_LIST, 5), /*!< Not Support */
|
||||
} list_status_t;
|
||||
|
||||
/*! @brief The list structure*/
|
||||
typedef struct list_label
|
||||
{
|
||||
struct list_element_tag *head; /*!< list head */
|
||||
struct list_element_tag *tail; /*!< list tail */
|
||||
uint16_t size; /*!< list size */
|
||||
uint16_t max; /*!< list max number of elements */
|
||||
} list_label_t, *list_handle_t;
|
||||
#if (defined(GENERIC_LIST_LIGHT) && (GENERIC_LIST_LIGHT > 0U))
|
||||
/*! @brief The list element*/
|
||||
typedef struct list_element_tag
|
||||
{
|
||||
struct list_element_tag *next; /*!< next list element */
|
||||
struct list_label *list; /*!< pointer to the list */
|
||||
} list_element_t, *list_element_handle_t;
|
||||
#else
|
||||
/*! @brief The list element*/
|
||||
typedef struct list_element_tag
|
||||
{
|
||||
struct list_element_tag *next; /*!< next list element */
|
||||
struct list_element_tag *prev; /*!< previous list element */
|
||||
struct list_label *list; /*!< pointer to the list */
|
||||
} list_element_t, *list_element_handle_t;
|
||||
#endif
|
||||
/**********************************************************************************
|
||||
* Public prototypes
|
||||
***********************************************************************************/
|
||||
/**********************************************************************************
|
||||
* API
|
||||
**********************************************************************************/
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
#endif /* _cplusplus */
|
||||
/*!
|
||||
* @brief Initialize the list.
|
||||
*
|
||||
* This function initialize the list.
|
||||
*
|
||||
* @param list - List handle to initialize.
|
||||
* @param max - Maximum number of elements in list. 0 for unlimited.
|
||||
*/
|
||||
void LIST_Init(list_handle_t list, uint32_t max);
|
||||
|
||||
/*!
|
||||
* @brief Gets the list that contains the given element.
|
||||
*
|
||||
*
|
||||
* @param element - Handle of the element.
|
||||
* @retval NULL if element is orphan, Handle of the list the element is inserted into.
|
||||
*/
|
||||
list_handle_t LIST_GetList(list_element_handle_t element);
|
||||
|
||||
/*!
|
||||
* @brief Links element to the head of the list.
|
||||
*
|
||||
* @param list - Handle of the list.
|
||||
* @param element - Handle of the element.
|
||||
* @retval kLIST_Full if list is full, kLIST_Ok if insertion was successful.
|
||||
*/
|
||||
list_status_t LIST_AddHead(list_handle_t list, list_element_handle_t element);
|
||||
|
||||
/*!
|
||||
* @brief Links element to the tail of the list.
|
||||
*
|
||||
* @param list - Handle of the list.
|
||||
* @param element - Handle of the element.
|
||||
* @retval kLIST_Full if list is full, kLIST_Ok if insertion was successful.
|
||||
*/
|
||||
list_status_t LIST_AddTail(list_handle_t list, list_element_handle_t element);
|
||||
|
||||
/*!
|
||||
* @brief Unlinks element from the head of the list.
|
||||
*
|
||||
* @param list - Handle of the list.
|
||||
*
|
||||
* @retval NULL if list is empty, handle of removed element(pointer) if removal was successful.
|
||||
*/
|
||||
list_element_handle_t LIST_RemoveHead(list_handle_t list);
|
||||
|
||||
/*!
|
||||
* @brief Gets head element handle.
|
||||
*
|
||||
* @param list - Handle of the list.
|
||||
*
|
||||
* @retval NULL if list is empty, handle of removed element(pointer) if removal was successful.
|
||||
*/
|
||||
list_element_handle_t LIST_GetHead(list_handle_t list);
|
||||
|
||||
/*!
|
||||
* @brief Gets next element handle for given element handle.
|
||||
*
|
||||
* @param element - Handle of the element.
|
||||
*
|
||||
* @retval NULL if list is empty, handle of removed element(pointer) if removal was successful.
|
||||
*/
|
||||
list_element_handle_t LIST_GetNext(list_element_handle_t element);
|
||||
|
||||
/*!
|
||||
* @brief Gets previous element handle for given element handle.
|
||||
*
|
||||
* @param element - Handle of the element.
|
||||
*
|
||||
* @retval NULL if list is empty, handle of removed element(pointer) if removal was successful.
|
||||
*/
|
||||
list_element_handle_t LIST_GetPrev(list_element_handle_t element);
|
||||
|
||||
/*!
|
||||
* @brief Unlinks an element from its list.
|
||||
*
|
||||
* @param element - Handle of the element.
|
||||
*
|
||||
* @retval kLIST_OrphanElement if element is not part of any list.
|
||||
* @retval kLIST_Ok if removal was successful.
|
||||
*/
|
||||
list_status_t LIST_RemoveElement(list_element_handle_t element);
|
||||
|
||||
/*!
|
||||
* @brief Links an element in the previous position relative to a given member of a list.
|
||||
*
|
||||
* @param element - Handle of the element.
|
||||
* @param newElement - New element to insert before the given member.
|
||||
*
|
||||
* @retval kLIST_OrphanElement if element is not part of any list.
|
||||
* @retval kLIST_Ok if removal was successful.
|
||||
*/
|
||||
list_status_t LIST_AddPrevElement(list_element_handle_t element, list_element_handle_t newElement);
|
||||
|
||||
/*!
|
||||
* @brief Gets the current size of a list.
|
||||
*
|
||||
* @param list - Handle of the list.
|
||||
*
|
||||
* @retval Current size of the list.
|
||||
*/
|
||||
uint32_t LIST_GetSize(list_handle_t list);
|
||||
|
||||
/*!
|
||||
* @brief Gets the number of free places in the list.
|
||||
*
|
||||
* @param list - Handle of the list.
|
||||
*
|
||||
* @retval Available size of the list.
|
||||
*/
|
||||
uint32_t LIST_GetAvailableSize(list_handle_t list);
|
||||
|
||||
/* @} */
|
||||
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
#endif
|
||||
/*! @}*/
|
||||
#endif /*_GENERIC_LIST_H_*/
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -1,99 +1,130 @@
|
||||
/*
|
||||
* Copyright 2019 NXP
|
||||
* All rights reserved.
|
||||
*
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
#ifndef __SERIAL_PORT_INTERNAL_H__
|
||||
#define __SERIAL_PORT_INTERNAL_H__
|
||||
|
||||
/*******************************************************************************
|
||||
* Definitions
|
||||
******************************************************************************/
|
||||
|
||||
/*******************************************************************************
|
||||
* API
|
||||
******************************************************************************/
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
#endif /* _cplusplus */
|
||||
|
||||
#if (defined(SERIAL_PORT_TYPE_UART) && (SERIAL_PORT_TYPE_UART > 0U))
|
||||
serial_manager_status_t Serial_UartInit(serial_handle_t serialHandle, void *serialConfig);
|
||||
serial_manager_status_t Serial_UartDeinit(serial_handle_t serialHandle);
|
||||
serial_manager_status_t Serial_UartWrite(serial_handle_t serialHandle, uint8_t *buffer, uint32_t length);
|
||||
#if !(defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
|
||||
serial_manager_status_t Serial_UartRead(serial_handle_t serialHandle, uint8_t *buffer, uint32_t length);
|
||||
#endif
|
||||
|
||||
#if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
|
||||
serial_manager_status_t Serial_UartCancelWrite(serial_handle_t serialHandle);
|
||||
serial_manager_status_t Serial_UartInstallTxCallback(serial_handle_t serialHandle,
|
||||
serial_manager_callback_t callback,
|
||||
void *callbackParam);
|
||||
serial_manager_status_t Serial_UartInstallRxCallback(serial_handle_t serialHandle,
|
||||
serial_manager_callback_t callback,
|
||||
void *callbackParam);
|
||||
void Serial_UartIsrFunction(serial_handle_t serialHandle);
|
||||
#endif
|
||||
serial_manager_status_t Serial_UartEnterLowpower(serial_handle_t serialHandle);
|
||||
serial_manager_status_t Serial_UartExitLowpower(serial_handle_t serialHandle);
|
||||
#endif
|
||||
|
||||
#if (defined(SERIAL_PORT_TYPE_USBCDC) && (SERIAL_PORT_TYPE_USBCDC > 0U))
|
||||
serial_manager_status_t Serial_UsbCdcInit(serial_handle_t serialHandle, void *config);
|
||||
serial_manager_status_t Serial_UsbCdcDeinit(serial_handle_t serialHandle);
|
||||
serial_manager_status_t Serial_UsbCdcWrite(serial_handle_t serialHandle, uint8_t *buffer, uint32_t length);
|
||||
serial_manager_status_t Serial_UsbCdcRead(serial_handle_t serialHandle, uint8_t *buffer, uint32_t length);
|
||||
serial_manager_status_t Serial_UsbCdcCancelWrite(serial_handle_t serialHandle);
|
||||
serial_manager_status_t Serial_UsbCdcInstallTxCallback(serial_handle_t serialHandle,
|
||||
serial_manager_callback_t callback,
|
||||
void *callbackParam);
|
||||
serial_manager_status_t Serial_UsbCdcInstallRxCallback(serial_handle_t serialHandle,
|
||||
serial_manager_callback_t callback,
|
||||
void *callbackParam);
|
||||
void Serial_UsbCdcIsrFunction(serial_handle_t serialHandle);
|
||||
#endif
|
||||
|
||||
#if (defined(SERIAL_PORT_TYPE_SWO) && (SERIAL_PORT_TYPE_SWO > 0U))
|
||||
serial_manager_status_t Serial_SwoInit(serial_handle_t serialHandle, void *config);
|
||||
serial_manager_status_t Serial_SwoDeinit(serial_handle_t serialHandle);
|
||||
serial_manager_status_t Serial_SwoWrite(serial_handle_t serialHandle, uint8_t *buffer, uint32_t length);
|
||||
#if !(defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
|
||||
serial_manager_status_t Serial_SwoRead(serial_handle_t serialHandle, uint8_t *buffer, uint32_t length);
|
||||
#endif
|
||||
#if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
|
||||
serial_manager_status_t Serial_SwoCancelWrite(serial_handle_t serialHandle);
|
||||
serial_manager_status_t Serial_SwoInstallTxCallback(serial_handle_t serialHandle,
|
||||
serial_manager_callback_t callback,
|
||||
void *callbackParam);
|
||||
serial_manager_status_t Serial_SwoInstallRxCallback(serial_handle_t serialHandle,
|
||||
serial_manager_callback_t callback,
|
||||
void *callbackParam);
|
||||
void Serial_SwoIsrFunction(serial_handle_t serialHandle);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if (defined(SERIAL_PORT_TYPE_USBCDC_VIRTUAL) && (SERIAL_PORT_TYPE_USBCDC_VIRTUAL > 0U))
|
||||
serial_manager_status_t Serial_UsbCdcVirtualInit(serial_handle_t serialHandle, void *config);
|
||||
serial_manager_status_t Serial_UsbCdcVirtualDeinit(serial_handle_t serialHandle);
|
||||
serial_manager_status_t Serial_UsbCdcVirtualWrite(serial_handle_t serialHandle, uint8_t *buffer, uint32_t length);
|
||||
serial_manager_status_t Serial_UsbCdcVirtualRead(serial_handle_t serialHandle, uint8_t *buffer, uint32_t length);
|
||||
serial_manager_status_t Serial_UsbCdcVirtualCancelWrite(serial_handle_t serialHandle);
|
||||
serial_manager_status_t Serial_UsbCdcVirtualInstallTxCallback(serial_handle_t serialHandle,
|
||||
serial_manager_callback_t callback,
|
||||
void *callbackParam);
|
||||
serial_manager_status_t Serial_UsbCdcVirtualInstallRxCallback(serial_handle_t serialHandle,
|
||||
serial_manager_callback_t callback,
|
||||
void *callbackParam);
|
||||
void Serial_UsbCdcVirtualIsrFunction(serial_handle_t serialHandle);
|
||||
#endif
|
||||
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __SERIAL_PORT_INTERNAL_H__ */
|
||||
/*
|
||||
* Copyright 2019-2020 NXP
|
||||
* All rights reserved.
|
||||
*
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
#ifndef __SERIAL_PORT_INTERNAL_H__
|
||||
#define __SERIAL_PORT_INTERNAL_H__
|
||||
|
||||
/*******************************************************************************
|
||||
* Definitions
|
||||
******************************************************************************/
|
||||
|
||||
/*******************************************************************************
|
||||
* API
|
||||
******************************************************************************/
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
#endif /* _cplusplus */
|
||||
|
||||
#if (defined(SERIAL_PORT_TYPE_UART) && (SERIAL_PORT_TYPE_UART > 0U))
|
||||
serial_manager_status_t Serial_UartInit(serial_handle_t serialHandle, void *serialConfig);
|
||||
serial_manager_status_t Serial_UartDeinit(serial_handle_t serialHandle);
|
||||
serial_manager_status_t Serial_UartWrite(serial_handle_t serialHandle, uint8_t *buffer, uint32_t length);
|
||||
|
||||
#if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
|
||||
serial_manager_status_t Serial_UartWriteBlocking(serial_handle_t serialHandle, uint8_t *buffer, uint32_t length);
|
||||
#endif /* SERIAL_MANAGER_NON_BLOCKING_MODE */
|
||||
|
||||
#if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
|
||||
#if (defined(SERIAL_MANAGER_NON_BLOCKING_DUAL_MODE) && (SERIAL_MANAGER_NON_BLOCKING_DUAL_MODE > 0U))
|
||||
serial_manager_status_t Serial_UartRead(serial_handle_t serialHandle, uint8_t *buffer, uint32_t length);
|
||||
#endif
|
||||
#else
|
||||
serial_manager_status_t Serial_UartRead(serial_handle_t serialHandle, uint8_t *buffer, uint32_t length);
|
||||
#endif
|
||||
|
||||
#if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
|
||||
serial_manager_status_t Serial_UartCancelWrite(serial_handle_t serialHandle);
|
||||
serial_manager_status_t Serial_UartInstallTxCallback(serial_handle_t serialHandle,
|
||||
serial_manager_callback_t callback,
|
||||
void *callbackParam);
|
||||
serial_manager_status_t Serial_UartInstallRxCallback(serial_handle_t serialHandle,
|
||||
serial_manager_callback_t callback,
|
||||
void *callbackParam);
|
||||
void Serial_UartIsrFunction(serial_handle_t serialHandle);
|
||||
#endif
|
||||
serial_manager_status_t Serial_UartEnterLowpower(serial_handle_t serialHandle);
|
||||
serial_manager_status_t Serial_UartExitLowpower(serial_handle_t serialHandle);
|
||||
#endif
|
||||
|
||||
#if (defined(SERIAL_PORT_TYPE_RPMSG) && (SERIAL_PORT_TYPE_RPMSG > 0U))
|
||||
serial_manager_status_t Serial_RpmsgInit(serial_handle_t serialHandle, void *serialConfig);
|
||||
serial_manager_status_t Serial_RpmsgDeinit(serial_handle_t serialHandle);
|
||||
serial_manager_status_t Serial_RpmsgWrite(serial_handle_t serialHandle, uint8_t *buffer, uint32_t length);
|
||||
serial_manager_status_t Serial_RpmsgWriteBlocking(serial_handle_t serialHandle, uint8_t *buffer, uint32_t length);
|
||||
#if !(defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
|
||||
serial_manager_status_t Serial_RpmsgRead(serial_handle_t serialHandle, uint8_t *buffer, uint32_t length);
|
||||
#endif
|
||||
|
||||
#if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
|
||||
serial_manager_status_t Serial_RpmsgCancelWrite(serial_handle_t serialHandle);
|
||||
serial_manager_status_t Serial_RpmsgInstallTxCallback(serial_handle_t serialHandle,
|
||||
serial_manager_callback_t callback,
|
||||
void *callbackParam);
|
||||
serial_manager_status_t Serial_RpmsgInstallRxCallback(serial_handle_t serialHandle,
|
||||
serial_manager_callback_t callback,
|
||||
void *callbackParam);
|
||||
#endif
|
||||
serial_manager_status_t Serial_RpmsgEnterLowpower(serial_handle_t serialHandle);
|
||||
serial_manager_status_t Serial_RpmsgExitLowpower(serial_handle_t serialHandle);
|
||||
#endif
|
||||
|
||||
#if (defined(SERIAL_PORT_TYPE_USBCDC) && (SERIAL_PORT_TYPE_USBCDC > 0U))
|
||||
serial_manager_status_t Serial_UsbCdcInit(serial_handle_t serialHandle, void *config);
|
||||
serial_manager_status_t Serial_UsbCdcDeinit(serial_handle_t serialHandle);
|
||||
serial_manager_status_t Serial_UsbCdcWrite(serial_handle_t serialHandle, uint8_t *buffer, uint32_t length);
|
||||
serial_manager_status_t Serial_UsbCdcRead(serial_handle_t serialHandle, uint8_t *buffer, uint32_t length);
|
||||
serial_manager_status_t Serial_UsbCdcCancelWrite(serial_handle_t serialHandle);
|
||||
serial_manager_status_t Serial_UsbCdcInstallTxCallback(serial_handle_t serialHandle,
|
||||
serial_manager_callback_t callback,
|
||||
void *callbackParam);
|
||||
serial_manager_status_t Serial_UsbCdcInstallRxCallback(serial_handle_t serialHandle,
|
||||
serial_manager_callback_t callback,
|
||||
void *callbackParam);
|
||||
void Serial_UsbCdcIsrFunction(serial_handle_t serialHandle);
|
||||
#endif
|
||||
|
||||
#if (defined(SERIAL_PORT_TYPE_SWO) && (SERIAL_PORT_TYPE_SWO > 0U))
|
||||
serial_manager_status_t Serial_SwoInit(serial_handle_t serialHandle, void *config);
|
||||
serial_manager_status_t Serial_SwoDeinit(serial_handle_t serialHandle);
|
||||
serial_manager_status_t Serial_SwoWrite(serial_handle_t serialHandle, uint8_t *buffer, uint32_t length);
|
||||
#if !(defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
|
||||
serial_manager_status_t Serial_SwoRead(serial_handle_t serialHandle, uint8_t *buffer, uint32_t length);
|
||||
#endif
|
||||
#if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
|
||||
serial_manager_status_t Serial_SwoCancelWrite(serial_handle_t serialHandle);
|
||||
serial_manager_status_t Serial_SwoInstallTxCallback(serial_handle_t serialHandle,
|
||||
serial_manager_callback_t callback,
|
||||
void *callbackParam);
|
||||
serial_manager_status_t Serial_SwoInstallRxCallback(serial_handle_t serialHandle,
|
||||
serial_manager_callback_t callback,
|
||||
void *callbackParam);
|
||||
void Serial_SwoIsrFunction(serial_handle_t serialHandle);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if (defined(SERIAL_PORT_TYPE_VIRTUAL) && (SERIAL_PORT_TYPE_VIRTUAL > 0U))
|
||||
serial_manager_status_t Serial_PortVirtualInit(serial_handle_t serialHandle, void *config);
|
||||
serial_manager_status_t Serial_PortVirtualDeinit(serial_handle_t serialHandle);
|
||||
serial_manager_status_t Serial_PortVirtualWrite(serial_handle_t serialHandle, uint8_t *buffer, uint32_t length);
|
||||
serial_manager_status_t Serial_PortVirtualRead(serial_handle_t serialHandle, uint8_t *buffer, uint32_t length);
|
||||
serial_manager_status_t Serial_PortVirtualCancelWrite(serial_handle_t serialHandle);
|
||||
serial_manager_status_t Serial_PortVirtualInstallTxCallback(serial_handle_t serialHandle,
|
||||
serial_manager_callback_t callback,
|
||||
void *callbackParam);
|
||||
serial_manager_status_t Serial_PortVirtualInstallRxCallback(serial_handle_t serialHandle,
|
||||
serial_manager_callback_t callback,
|
||||
void *callbackParam);
|
||||
void Serial_PortVirtualIsrFunction(serial_handle_t serialHandle);
|
||||
#endif
|
||||
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __SERIAL_PORT_INTERNAL_H__ */
|
@@ -1,198 +1,206 @@
|
||||
/*
|
||||
* Copyright 2018 NXP
|
||||
* All rights reserved.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
#include "fsl_common.h"
|
||||
#include "serial_manager.h"
|
||||
|
||||
#if (defined(SERIAL_PORT_TYPE_SWO) && (SERIAL_PORT_TYPE_SWO > 0U))
|
||||
#include "serial_port_swo.h"
|
||||
|
||||
/*******************************************************************************
|
||||
* Definitions
|
||||
******************************************************************************/
|
||||
#ifndef NDEBUG
|
||||
#if (defined(DEBUG_CONSOLE_ASSERT_DISABLE) && (DEBUG_CONSOLE_ASSERT_DISABLE > 0U))
|
||||
#undef assert
|
||||
#define assert(n)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
typedef struct _serial_swo_state
|
||||
{
|
||||
serial_manager_callback_t txCallback;
|
||||
void *txCallbackParam;
|
||||
uint32_t port;
|
||||
} serial_swo_state_t;
|
||||
|
||||
/*******************************************************************************
|
||||
* Prototypes
|
||||
******************************************************************************/
|
||||
|
||||
/*******************************************************************************
|
||||
* Variables
|
||||
******************************************************************************/
|
||||
|
||||
/*******************************************************************************
|
||||
* Code
|
||||
******************************************************************************/
|
||||
serial_manager_status_t Serial_SwoInit(serial_handle_t serialHandle, void *serialConfig)
|
||||
{
|
||||
serial_swo_state_t *serialSwoHandle;
|
||||
serial_port_swo_config_t *swoConfig;
|
||||
uint32_t prescaler;
|
||||
|
||||
assert(serialConfig);
|
||||
assert(serialHandle);
|
||||
|
||||
if (SERIAL_PORT_SWO_HANDLE_SIZE < sizeof(serial_swo_state_t))
|
||||
{
|
||||
return kStatus_SerialManager_Error;
|
||||
}
|
||||
|
||||
swoConfig = (serial_port_swo_config_t *)serialConfig;
|
||||
|
||||
assert(swoConfig->baudRate <= swoConfig->clockRate);
|
||||
assert((swoConfig->clockRate / swoConfig->baudRate) <= TPI_ACPR_PRESCALER_Msk);
|
||||
assert((TPI->DEVID & (TPI_DEVID_NRZVALID_Msk | TPI_DEVID_MANCVALID_Msk)) != 0U);
|
||||
|
||||
serialSwoHandle = (serial_swo_state_t *)serialHandle;
|
||||
|
||||
serialSwoHandle->port = swoConfig->port;
|
||||
|
||||
prescaler = swoConfig->clockRate / swoConfig->baudRate - 1U;
|
||||
|
||||
/* enable the ITM and DWT units */
|
||||
CoreDebug->DEMCR = CoreDebug_DEMCR_TRCENA_Msk;
|
||||
|
||||
if ((CoreDebug->DEMCR & CoreDebug_DEMCR_TRCENA_Msk) == 0U)
|
||||
{
|
||||
return kStatus_SerialManager_Error;
|
||||
}
|
||||
|
||||
/* Lock access */
|
||||
ITM->LAR = 0xC5ACCE55U;
|
||||
/* Disable ITM */
|
||||
ITM->TER &= ~(1U << swoConfig->port);
|
||||
ITM->TCR = 0U;
|
||||
/* select SWO encoding protocol */
|
||||
TPI->SPPR = swoConfig->protocol;
|
||||
/* select asynchronous clock prescaler */
|
||||
TPI->ACPR = prescaler & 0xFFFFU;
|
||||
/* allow unprivilege access */
|
||||
ITM->TPR = 0U;
|
||||
/* enable ITM */
|
||||
ITM->TCR = ITM_TCR_ITMENA_Msk | ITM_TCR_SYNCENA_Msk
|
||||
#ifdef ITM_TCR_TraceBusID_Msk
|
||||
| ITM_TCR_TraceBusID_Msk
|
||||
#elif defined(ITM_TCR_TRACEBUSID_Msk)
|
||||
| ITM_TCR_TRACEBUSID_Msk
|
||||
#else
|
||||
#endif
|
||||
| ITM_TCR_SWOENA_Msk | ITM_TCR_DWTENA_Msk;
|
||||
/* enable the port bits */
|
||||
ITM->TER = 1U << swoConfig->port;
|
||||
|
||||
return kStatus_SerialManager_Success;
|
||||
}
|
||||
|
||||
serial_manager_status_t Serial_SwoDeinit(serial_handle_t serialHandle)
|
||||
{
|
||||
serial_swo_state_t *serialSwoHandle;
|
||||
|
||||
assert(serialHandle);
|
||||
|
||||
serialSwoHandle = (serial_swo_state_t *)serialHandle;
|
||||
/* disable ITM */
|
||||
ITM->TER &= ~(1U << serialSwoHandle->port);
|
||||
|
||||
return kStatus_SerialManager_Success;
|
||||
}
|
||||
|
||||
serial_manager_status_t Serial_SwoWrite(serial_handle_t serialHandle, uint8_t *buffer, uint32_t length)
|
||||
{
|
||||
serial_swo_state_t *serialSwoHandle;
|
||||
uint8_t *strAddr;
|
||||
#if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
|
||||
serial_manager_callback_message_t msg;
|
||||
#endif
|
||||
|
||||
assert(serialHandle);
|
||||
assert(buffer);
|
||||
assert(length);
|
||||
|
||||
serialSwoHandle = (serial_swo_state_t *)serialHandle;
|
||||
|
||||
assert((ITM->TCR & ITM_TCR_ITMENA_Msk) != 0U);
|
||||
assert((ITM->TER & (1U << serialSwoHandle->port)) != 0U);
|
||||
|
||||
strAddr = buffer;
|
||||
#if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
|
||||
msg.buffer = buffer;
|
||||
msg.length = length;
|
||||
#endif
|
||||
while (length)
|
||||
{
|
||||
/* wait FIFO ready */
|
||||
while (ITM->PORT[serialSwoHandle->port].u32 == 0U)
|
||||
{
|
||||
}
|
||||
|
||||
length -= 1U;
|
||||
ITM->PORT[serialSwoHandle->port].u8 = *strAddr;
|
||||
|
||||
strAddr++;
|
||||
}
|
||||
|
||||
#if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
|
||||
if (NULL != serialSwoHandle->txCallback)
|
||||
{
|
||||
serialSwoHandle->txCallback(serialSwoHandle->txCallbackParam, &msg, kStatus_SerialManager_Success);
|
||||
}
|
||||
#endif
|
||||
return kStatus_SerialManager_Success;
|
||||
}
|
||||
|
||||
#if !(defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
|
||||
serial_manager_status_t Serial_SwoRead(serial_handle_t serialHandle, uint8_t *buffer, uint32_t length)
|
||||
{
|
||||
return kStatus_SerialManager_Error;
|
||||
}
|
||||
#endif
|
||||
|
||||
serial_manager_status_t Serial_SwoCancelWrite(serial_handle_t serialHandle)
|
||||
{
|
||||
return kStatus_SerialManager_Error;
|
||||
}
|
||||
|
||||
serial_manager_status_t Serial_SwoInstallTxCallback(serial_handle_t serialHandle,
|
||||
serial_manager_callback_t callback,
|
||||
void *callbackParam)
|
||||
{
|
||||
serial_swo_state_t *serialSwoHandle;
|
||||
|
||||
assert(serialHandle);
|
||||
|
||||
serialSwoHandle = (serial_swo_state_t *)serialHandle;
|
||||
|
||||
serialSwoHandle->txCallback = callback;
|
||||
serialSwoHandle->txCallbackParam = callbackParam;
|
||||
|
||||
return kStatus_SerialManager_Success;
|
||||
}
|
||||
|
||||
serial_manager_status_t Serial_SwoInstallRxCallback(serial_handle_t serialHandle,
|
||||
serial_manager_callback_t callback,
|
||||
void *callbackParam)
|
||||
{
|
||||
return kStatus_SerialManager_Error;
|
||||
}
|
||||
|
||||
void Serial_SwoIsrFunction(serial_handle_t serialHandle)
|
||||
{
|
||||
}
|
||||
|
||||
#endif
|
||||
/*
|
||||
* Copyright 2018 NXP
|
||||
* All rights reserved.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
#include "fsl_common.h"
|
||||
#include "fsl_component_serial_manager.h"
|
||||
#include "fsl_component_serial_port_internal.h"
|
||||
|
||||
#if (defined(SERIAL_PORT_TYPE_SWO) && (SERIAL_PORT_TYPE_SWO > 0U))
|
||||
#include "fsl_component_serial_port_swo.h"
|
||||
|
||||
/*******************************************************************************
|
||||
* Definitions
|
||||
******************************************************************************/
|
||||
#ifndef NDEBUG
|
||||
#if (defined(DEBUG_CONSOLE_ASSERT_DISABLE) && (DEBUG_CONSOLE_ASSERT_DISABLE > 0U))
|
||||
#undef assert
|
||||
#define assert(n)
|
||||
#else
|
||||
/* MISRA C-2012 Rule 17.2 */
|
||||
#undef assert
|
||||
#define assert(n) \
|
||||
while (!(n)) \
|
||||
{ \
|
||||
; \
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
typedef struct _serial_swo_state
|
||||
{
|
||||
serial_manager_callback_t txCallback;
|
||||
void *txCallbackParam;
|
||||
uint32_t port;
|
||||
} serial_swo_state_t;
|
||||
|
||||
/*******************************************************************************
|
||||
* Prototypes
|
||||
******************************************************************************/
|
||||
|
||||
/*******************************************************************************
|
||||
* Variables
|
||||
******************************************************************************/
|
||||
|
||||
/*******************************************************************************
|
||||
* Code
|
||||
******************************************************************************/
|
||||
serial_manager_status_t Serial_SwoInit(serial_handle_t serialHandle, void *config)
|
||||
{
|
||||
serial_swo_state_t *serialSwoHandle;
|
||||
serial_port_swo_config_t *swoConfig;
|
||||
uint32_t prescaler;
|
||||
|
||||
assert(config);
|
||||
assert(serialHandle);
|
||||
|
||||
assert(SERIAL_PORT_SWO_HANDLE_SIZE >= sizeof(serial_swo_state_t));
|
||||
|
||||
swoConfig = (serial_port_swo_config_t *)config;
|
||||
|
||||
assert(swoConfig->baudRate <= swoConfig->clockRate);
|
||||
assert((swoConfig->clockRate / swoConfig->baudRate) <= TPI_ACPR_PRESCALER_Msk);
|
||||
assert((TPI->DEVID & (TPI_DEVID_NRZVALID_Msk | TPI_DEVID_MANCVALID_Msk)) != 0U);
|
||||
|
||||
serialSwoHandle = (serial_swo_state_t *)serialHandle;
|
||||
|
||||
serialSwoHandle->port = swoConfig->port;
|
||||
|
||||
prescaler = swoConfig->clockRate / swoConfig->baudRate - 1U;
|
||||
|
||||
/* enable the ITM and DWT units */
|
||||
CoreDebug->DEMCR = CoreDebug_DEMCR_TRCENA_Msk;
|
||||
|
||||
if ((CoreDebug->DEMCR & CoreDebug_DEMCR_TRCENA_Msk) == 0U)
|
||||
{
|
||||
return kStatus_SerialManager_Error;
|
||||
}
|
||||
|
||||
/* Lock access */
|
||||
ITM->LAR = 0xC5ACCE55U;
|
||||
/* Disable ITM */
|
||||
ITM->TER &= ~(1UL << swoConfig->port);
|
||||
ITM->TCR = 0U;
|
||||
/* select SWO encoding protocol */
|
||||
TPI->SPPR = (uint32_t)swoConfig->protocol;
|
||||
/* select asynchronous clock prescaler */
|
||||
TPI->ACPR = prescaler & 0xFFFFU;
|
||||
/* allow unprivilege access */
|
||||
ITM->TPR = 0U;
|
||||
/* enable ITM */
|
||||
ITM->TCR = ITM_TCR_ITMENA_Msk | ITM_TCR_SYNCENA_Msk
|
||||
#ifdef ITM_TCR_TraceBusID_Msk
|
||||
| ITM_TCR_TraceBusID_Msk
|
||||
#elif defined(ITM_TCR_TRACEBUSID_Msk)
|
||||
| ITM_TCR_TRACEBUSID_Msk
|
||||
#else
|
||||
#endif
|
||||
| ITM_TCR_SWOENA_Msk | ITM_TCR_DWTENA_Msk;
|
||||
/* enable the port bits */
|
||||
ITM->TER = 1UL << swoConfig->port;
|
||||
|
||||
return kStatus_SerialManager_Success;
|
||||
}
|
||||
|
||||
serial_manager_status_t Serial_SwoDeinit(serial_handle_t serialHandle)
|
||||
{
|
||||
serial_swo_state_t *serialSwoHandle;
|
||||
|
||||
assert(serialHandle);
|
||||
|
||||
serialSwoHandle = (serial_swo_state_t *)serialHandle;
|
||||
/* disable ITM */
|
||||
ITM->TER &= ~(1UL << serialSwoHandle->port);
|
||||
|
||||
return kStatus_SerialManager_Success;
|
||||
}
|
||||
|
||||
serial_manager_status_t Serial_SwoWrite(serial_handle_t serialHandle, uint8_t *buffer, uint32_t length)
|
||||
{
|
||||
serial_swo_state_t *serialSwoHandle;
|
||||
uint8_t *strAddr;
|
||||
#if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
|
||||
serial_manager_callback_message_t msg;
|
||||
#endif
|
||||
|
||||
assert(serialHandle);
|
||||
assert(buffer);
|
||||
assert(length);
|
||||
|
||||
serialSwoHandle = (serial_swo_state_t *)serialHandle;
|
||||
|
||||
assert((ITM->TCR & ITM_TCR_ITMENA_Msk) != 0U);
|
||||
assert((ITM->TER & (1UL << serialSwoHandle->port)) != 0U);
|
||||
|
||||
strAddr = buffer;
|
||||
#if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
|
||||
msg.buffer = buffer;
|
||||
msg.length = length;
|
||||
#endif
|
||||
while (0U != length)
|
||||
{
|
||||
/* wait FIFO ready */
|
||||
while (ITM->PORT[serialSwoHandle->port].u32 == 0U)
|
||||
{
|
||||
}
|
||||
|
||||
length -= 1U;
|
||||
ITM->PORT[serialSwoHandle->port].u8 = *strAddr;
|
||||
|
||||
strAddr++;
|
||||
}
|
||||
|
||||
#if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
|
||||
if (NULL != serialSwoHandle->txCallback)
|
||||
{
|
||||
serialSwoHandle->txCallback(serialSwoHandle->txCallbackParam, &msg, kStatus_SerialManager_Success);
|
||||
}
|
||||
#endif
|
||||
return kStatus_SerialManager_Success;
|
||||
}
|
||||
|
||||
#if !(defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
|
||||
serial_manager_status_t Serial_SwoRead(serial_handle_t serialHandle, uint8_t *buffer, uint32_t length)
|
||||
{
|
||||
return kStatus_SerialManager_Error;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
|
||||
serial_manager_status_t Serial_SwoCancelWrite(serial_handle_t serialHandle)
|
||||
{
|
||||
return kStatus_SerialManager_Error;
|
||||
}
|
||||
|
||||
serial_manager_status_t Serial_SwoInstallTxCallback(serial_handle_t serialHandle,
|
||||
serial_manager_callback_t callback,
|
||||
void *callbackParam)
|
||||
{
|
||||
serial_swo_state_t *serialSwoHandle;
|
||||
|
||||
assert(serialHandle);
|
||||
|
||||
serialSwoHandle = (serial_swo_state_t *)serialHandle;
|
||||
|
||||
serialSwoHandle->txCallback = callback;
|
||||
serialSwoHandle->txCallbackParam = callbackParam;
|
||||
|
||||
return kStatus_SerialManager_Success;
|
||||
}
|
||||
|
||||
serial_manager_status_t Serial_SwoInstallRxCallback(serial_handle_t serialHandle,
|
||||
serial_manager_callback_t callback,
|
||||
void *callbackParam)
|
||||
{
|
||||
return kStatus_SerialManager_Error;
|
||||
}
|
||||
|
||||
void Serial_SwoIsrFunction(serial_handle_t serialHandle)
|
||||
{
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@@ -1,38 +1,38 @@
|
||||
/*
|
||||
* Copyright 2018 NXP
|
||||
* All rights reserved.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
#ifndef __SERIAL_PORT_SWO_H__
|
||||
#define __SERIAL_PORT_SWO_H__
|
||||
|
||||
/*!
|
||||
* @addtogroup serial_port_swo
|
||||
* @{
|
||||
*/
|
||||
|
||||
/*******************************************************************************
|
||||
* Definitions
|
||||
******************************************************************************/
|
||||
/*! @brief serial port swo handle size*/
|
||||
#define SERIAL_PORT_SWO_HANDLE_SIZE (12U)
|
||||
|
||||
/*! @brief serial port swo protocol*/
|
||||
typedef enum _serial_port_swo_protocol
|
||||
{
|
||||
kSerialManager_SwoProtocolManchester = 1U, /*!< SWO Manchester protocol */
|
||||
kSerialManager_SwoProtocolNrz = 2U, /*!< SWO UART/NRZ protocol */
|
||||
} serial_port_swo_protocol_t;
|
||||
|
||||
/*! @brief serial port swo config struct*/
|
||||
typedef struct _serial_port_swo_config
|
||||
{
|
||||
uint32_t clockRate; /*!< clock rate */
|
||||
uint32_t baudRate; /*!< baud rate */
|
||||
uint32_t port; /*!< Port used to transfer data */
|
||||
serial_port_swo_protocol_t protocol; /*!< SWO protocol */
|
||||
} serial_port_swo_config_t;
|
||||
/*! @} */
|
||||
#endif /* __SERIAL_PORT_SWO_H__ */
|
||||
/*
|
||||
* Copyright 2018 NXP
|
||||
* All rights reserved.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
#ifndef __SERIAL_PORT_SWO_H__
|
||||
#define __SERIAL_PORT_SWO_H__
|
||||
|
||||
/*!
|
||||
* @addtogroup serial_port_swo
|
||||
* @{
|
||||
*/
|
||||
|
||||
/*******************************************************************************
|
||||
* Definitions
|
||||
******************************************************************************/
|
||||
/*! @brief serial port swo handle size*/
|
||||
#define SERIAL_PORT_SWO_HANDLE_SIZE (12U)
|
||||
|
||||
/*! @brief serial port swo protocol*/
|
||||
typedef enum _serial_port_swo_protocol
|
||||
{
|
||||
kSerialManager_SwoProtocolManchester = 1U, /*!< SWO Manchester protocol */
|
||||
kSerialManager_SwoProtocolNrz = 2U, /*!< SWO UART/NRZ protocol */
|
||||
} serial_port_swo_protocol_t;
|
||||
|
||||
/*! @brief serial port swo config struct*/
|
||||
typedef struct _serial_port_swo_config
|
||||
{
|
||||
uint32_t clockRate; /*!< clock rate */
|
||||
uint32_t baudRate; /*!< baud rate */
|
||||
uint32_t port; /*!< Port used to transfer data */
|
||||
serial_port_swo_protocol_t protocol; /*!< SWO protocol */
|
||||
} serial_port_swo_config_t;
|
||||
/*! @} */
|
||||
#endif /* __SERIAL_PORT_SWO_H__ */
|
@@ -1,403 +1,460 @@
|
||||
/*
|
||||
* Copyright 2018 NXP
|
||||
* All rights reserved.
|
||||
*
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
#include "fsl_common.h"
|
||||
#include "serial_manager.h"
|
||||
#include "serial_port_internal.h"
|
||||
|
||||
#if (defined(SERIAL_PORT_TYPE_UART) && (SERIAL_PORT_TYPE_UART > 0U))
|
||||
#include "uart.h"
|
||||
|
||||
#include "serial_port_uart.h"
|
||||
|
||||
/*******************************************************************************
|
||||
* Definitions
|
||||
******************************************************************************/
|
||||
#ifndef NDEBUG
|
||||
#if (defined(DEBUG_CONSOLE_ASSERT_DISABLE) && (DEBUG_CONSOLE_ASSERT_DISABLE > 0U))
|
||||
#undef assert
|
||||
#define assert(n)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
|
||||
#define SERIAL_PORT_UART_RECEIVE_DATA_LENGTH 1U
|
||||
|
||||
typedef struct _serial_uart_send_state
|
||||
{
|
||||
serial_manager_callback_t callback;
|
||||
void *callbackParam;
|
||||
uint8_t *buffer;
|
||||
uint32_t length;
|
||||
volatile uint8_t busy;
|
||||
} serial_uart_send_state_t;
|
||||
|
||||
typedef struct _serial_uart_recv_state
|
||||
{
|
||||
serial_manager_callback_t callback;
|
||||
void *callbackParam;
|
||||
volatile uint8_t busy;
|
||||
uint8_t readBuffer[SERIAL_PORT_UART_RECEIVE_DATA_LENGTH];
|
||||
} serial_uart_recv_state_t;
|
||||
#endif
|
||||
|
||||
typedef struct _serial_uart_state
|
||||
{
|
||||
#if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
|
||||
serial_uart_send_state_t tx;
|
||||
serial_uart_recv_state_t rx;
|
||||
#endif
|
||||
uint8_t usartHandleBuffer[HAL_UART_HANDLE_SIZE];
|
||||
} serial_uart_state_t;
|
||||
|
||||
/*******************************************************************************
|
||||
* Prototypes
|
||||
******************************************************************************/
|
||||
|
||||
/*******************************************************************************
|
||||
* Code
|
||||
******************************************************************************/
|
||||
|
||||
#if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
|
||||
/* UART user callback */
|
||||
static void Serial_UartCallback(hal_uart_handle_t handle, hal_uart_status_t status, void *userData)
|
||||
{
|
||||
serial_uart_state_t *serialUartHandle;
|
||||
serial_manager_callback_message_t msg;
|
||||
#if (defined(HAL_UART_TRANSFER_MODE) && (HAL_UART_TRANSFER_MODE > 0U))
|
||||
hal_uart_transfer_t transfer;
|
||||
#endif
|
||||
|
||||
if (NULL == userData)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
serialUartHandle = (serial_uart_state_t *)userData;
|
||||
|
||||
if ((hal_uart_status_t)kStatus_HAL_UartRxIdle == status)
|
||||
{
|
||||
if ((NULL != serialUartHandle->rx.callback))
|
||||
{
|
||||
msg.buffer = &serialUartHandle->rx.readBuffer[0];
|
||||
msg.length = sizeof(serialUartHandle->rx.readBuffer);
|
||||
serialUartHandle->rx.callback(serialUartHandle->rx.callbackParam, &msg, kStatus_SerialManager_Success);
|
||||
}
|
||||
#if (defined(HAL_UART_TRANSFER_MODE) && (HAL_UART_TRANSFER_MODE > 0U))
|
||||
transfer.data = &serialUartHandle->rx.readBuffer[0];
|
||||
transfer.dataSize = sizeof(serialUartHandle->rx.readBuffer);
|
||||
if (kStatus_HAL_UartSuccess ==
|
||||
HAL_UartTransferReceiveNonBlocking(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]), &transfer))
|
||||
#else
|
||||
if ((hal_uart_status_t)kStatus_HAL_UartSuccess ==
|
||||
HAL_UartReceiveNonBlocking(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]),
|
||||
&serialUartHandle->rx.readBuffer[0], sizeof(serialUartHandle->rx.readBuffer)))
|
||||
#endif
|
||||
{
|
||||
serialUartHandle->rx.busy = 1U;
|
||||
}
|
||||
else
|
||||
{
|
||||
serialUartHandle->rx.busy = 0U;
|
||||
}
|
||||
}
|
||||
else if ((hal_uart_status_t)kStatus_HAL_UartTxIdle == status)
|
||||
{
|
||||
if (0U != serialUartHandle->tx.busy)
|
||||
{
|
||||
serialUartHandle->tx.busy = 0U;
|
||||
if ((NULL != serialUartHandle->tx.callback))
|
||||
{
|
||||
msg.buffer = serialUartHandle->tx.buffer;
|
||||
msg.length = serialUartHandle->tx.length;
|
||||
serialUartHandle->tx.callback(serialUartHandle->tx.callbackParam, &msg, kStatus_SerialManager_Success);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
serial_manager_status_t Serial_UartInit(serial_handle_t serialHandle, void *serialConfig)
|
||||
{
|
||||
serial_uart_state_t *serialUartHandle;
|
||||
serial_port_uart_config_t *uartConfig;
|
||||
hal_uart_config_t config;
|
||||
#if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
|
||||
#if (defined(HAL_UART_TRANSFER_MODE) && (HAL_UART_TRANSFER_MODE > 0U))
|
||||
hal_uart_transfer_t transfer;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
assert(serialConfig);
|
||||
assert(serialHandle);
|
||||
assert(SERIAL_PORT_UART_HANDLE_SIZE >= sizeof(serial_uart_state_t));
|
||||
|
||||
uartConfig = (serial_port_uart_config_t *)serialConfig;
|
||||
serialUartHandle = (serial_uart_state_t *)serialHandle;
|
||||
|
||||
config.baudRate_Bps = uartConfig->baudRate;
|
||||
config.parityMode = (hal_uart_parity_mode_t)uartConfig->parityMode;
|
||||
config.stopBitCount = (hal_uart_stop_bit_count_t)uartConfig->stopBitCount;
|
||||
config.enableRx = uartConfig->enableRx;
|
||||
config.enableTx = uartConfig->enableTx;
|
||||
config.srcClock_Hz = uartConfig->clockRate;
|
||||
config.instance = uartConfig->instance;
|
||||
|
||||
if (kStatus_HAL_UartSuccess != HAL_UartInit(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]), &config))
|
||||
{
|
||||
return kStatus_SerialManager_Error;
|
||||
}
|
||||
|
||||
#if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
|
||||
|
||||
#if (defined(HAL_UART_TRANSFER_MODE) && (HAL_UART_TRANSFER_MODE > 0U))
|
||||
if (kStatus_HAL_UartSuccess !=
|
||||
HAL_UartTransferInstallCallback(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]),
|
||||
Serial_UartCallback, serialUartHandle))
|
||||
#else
|
||||
if (kStatus_HAL_UartSuccess != HAL_UartInstallCallback(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]),
|
||||
Serial_UartCallback, serialUartHandle))
|
||||
#endif
|
||||
{
|
||||
return kStatus_SerialManager_Error;
|
||||
}
|
||||
|
||||
if (0U != uartConfig->enableRx)
|
||||
{
|
||||
serialUartHandle->rx.busy = 1U;
|
||||
#if (defined(HAL_UART_TRANSFER_MODE) && (HAL_UART_TRANSFER_MODE > 0U))
|
||||
transfer.data = &serialUartHandle->rx.readBuffer[0];
|
||||
transfer.dataSize = sizeof(serialUartHandle->rx.readBuffer);
|
||||
if (kStatus_HAL_UartSuccess !=
|
||||
HAL_UartTransferReceiveNonBlocking(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]), &transfer))
|
||||
#else
|
||||
if (kStatus_HAL_UartSuccess !=
|
||||
HAL_UartReceiveNonBlocking(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]),
|
||||
&serialUartHandle->rx.readBuffer[0], sizeof(serialUartHandle->rx.readBuffer)))
|
||||
#endif
|
||||
{
|
||||
serialUartHandle->rx.busy = 0U;
|
||||
return kStatus_SerialManager_Error;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
return kStatus_SerialManager_Success;
|
||||
}
|
||||
|
||||
serial_manager_status_t Serial_UartDeinit(serial_handle_t serialHandle)
|
||||
{
|
||||
serial_uart_state_t *serialUartHandle;
|
||||
|
||||
assert(serialHandle);
|
||||
|
||||
serialUartHandle = (serial_uart_state_t *)serialHandle;
|
||||
|
||||
#if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
|
||||
#if (defined(HAL_UART_TRANSFER_MODE) && (HAL_UART_TRANSFER_MODE > 0U))
|
||||
(void)HAL_UartTransferAbortReceive(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]));
|
||||
#else
|
||||
(void)HAL_UartAbortReceive(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]));
|
||||
#endif
|
||||
#endif
|
||||
(void)HAL_UartDeinit(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]));
|
||||
|
||||
#if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
|
||||
serialUartHandle->tx.busy = 0U;
|
||||
serialUartHandle->rx.busy = 0U;
|
||||
#endif
|
||||
|
||||
return kStatus_SerialManager_Success;
|
||||
}
|
||||
|
||||
#if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
|
||||
|
||||
serial_manager_status_t Serial_UartWrite(serial_handle_t serialHandle, uint8_t *buffer, uint32_t length)
|
||||
{
|
||||
serial_uart_state_t *serialUartHandle;
|
||||
#if (defined(HAL_UART_TRANSFER_MODE) && (HAL_UART_TRANSFER_MODE > 0U))
|
||||
hal_uart_transfer_t transfer;
|
||||
#endif
|
||||
|
||||
assert(serialHandle);
|
||||
assert(buffer);
|
||||
assert(length);
|
||||
|
||||
serialUartHandle = (serial_uart_state_t *)serialHandle;
|
||||
|
||||
if (0U != serialUartHandle->tx.busy)
|
||||
{
|
||||
return kStatus_SerialManager_Busy;
|
||||
}
|
||||
serialUartHandle->tx.busy = 1U;
|
||||
|
||||
serialUartHandle->tx.buffer = buffer;
|
||||
serialUartHandle->tx.length = length;
|
||||
|
||||
#if (defined(HAL_UART_TRANSFER_MODE) && (HAL_UART_TRANSFER_MODE > 0U))
|
||||
transfer.data = buffer;
|
||||
transfer.dataSize = length;
|
||||
if (kStatus_HAL_UartSuccess !=
|
||||
HAL_UartTransferSendNonBlocking(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]), &transfer))
|
||||
#else
|
||||
if (kStatus_HAL_UartSuccess !=
|
||||
HAL_UartSendNonBlocking(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]), buffer, length))
|
||||
#endif
|
||||
{
|
||||
serialUartHandle->tx.busy = 0U;
|
||||
return kStatus_SerialManager_Error;
|
||||
}
|
||||
return kStatus_SerialManager_Success;
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
serial_manager_status_t Serial_UartWrite(serial_handle_t serialHandle, uint8_t *buffer, uint32_t length)
|
||||
{
|
||||
serial_uart_state_t *serialUartHandle;
|
||||
|
||||
assert(serialHandle);
|
||||
assert(buffer);
|
||||
assert(length);
|
||||
|
||||
serialUartHandle = (serial_uart_state_t *)serialHandle;
|
||||
|
||||
return (serial_manager_status_t)HAL_UartSendBlocking(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]),
|
||||
buffer, length);
|
||||
}
|
||||
|
||||
serial_manager_status_t Serial_UartRead(serial_handle_t serialHandle, uint8_t *buffer, uint32_t length)
|
||||
{
|
||||
serial_uart_state_t *serialUartHandle;
|
||||
|
||||
assert(serialHandle);
|
||||
assert(buffer);
|
||||
assert(length);
|
||||
|
||||
serialUartHandle = (serial_uart_state_t *)serialHandle;
|
||||
|
||||
return (serial_manager_status_t)HAL_UartReceiveBlocking(
|
||||
((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]), buffer, length);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
|
||||
serial_manager_status_t Serial_UartCancelWrite(serial_handle_t serialHandle)
|
||||
{
|
||||
serial_uart_state_t *serialUartHandle;
|
||||
serial_manager_callback_message_t msg;
|
||||
uint32_t primask;
|
||||
uint8_t isBusy = 0U;
|
||||
|
||||
assert(serialHandle);
|
||||
|
||||
serialUartHandle = (serial_uart_state_t *)serialHandle;
|
||||
|
||||
primask = DisableGlobalIRQ();
|
||||
isBusy = serialUartHandle->tx.busy;
|
||||
serialUartHandle->tx.busy = 0U;
|
||||
EnableGlobalIRQ(primask);
|
||||
|
||||
#if (defined(HAL_UART_TRANSFER_MODE) && (HAL_UART_TRANSFER_MODE > 0U))
|
||||
(void)HAL_UartTransferAbortSend(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]));
|
||||
#else
|
||||
(void)HAL_UartAbortSend(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]));
|
||||
#endif
|
||||
if (0U != isBusy)
|
||||
{
|
||||
if ((NULL != serialUartHandle->tx.callback))
|
||||
{
|
||||
msg.buffer = serialUartHandle->tx.buffer;
|
||||
msg.length = serialUartHandle->tx.length;
|
||||
serialUartHandle->tx.callback(serialUartHandle->tx.callbackParam, &msg, kStatus_SerialManager_Canceled);
|
||||
}
|
||||
}
|
||||
return kStatus_SerialManager_Success;
|
||||
}
|
||||
|
||||
serial_manager_status_t Serial_UartInstallTxCallback(serial_handle_t serialHandle,
|
||||
serial_manager_callback_t callback,
|
||||
void *callbackParam)
|
||||
{
|
||||
serial_uart_state_t *serialUartHandle;
|
||||
|
||||
assert(serialHandle);
|
||||
|
||||
serialUartHandle = (serial_uart_state_t *)serialHandle;
|
||||
|
||||
serialUartHandle->tx.callback = callback;
|
||||
serialUartHandle->tx.callbackParam = callbackParam;
|
||||
|
||||
return kStatus_SerialManager_Success;
|
||||
}
|
||||
|
||||
serial_manager_status_t Serial_UartInstallRxCallback(serial_handle_t serialHandle,
|
||||
serial_manager_callback_t callback,
|
||||
void *callbackParam)
|
||||
{
|
||||
serial_uart_state_t *serialUartHandle;
|
||||
|
||||
assert(serialHandle);
|
||||
|
||||
serialUartHandle = (serial_uart_state_t *)serialHandle;
|
||||
|
||||
serialUartHandle->rx.callback = callback;
|
||||
serialUartHandle->rx.callbackParam = callbackParam;
|
||||
|
||||
return kStatus_SerialManager_Success;
|
||||
}
|
||||
|
||||
void Serial_UartIsrFunction(serial_handle_t serialHandle)
|
||||
{
|
||||
serial_uart_state_t *serialUartHandle;
|
||||
|
||||
assert(serialHandle);
|
||||
|
||||
serialUartHandle = (serial_uart_state_t *)serialHandle;
|
||||
|
||||
HAL_UartIsrFunction(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]));
|
||||
}
|
||||
#endif
|
||||
|
||||
serial_manager_status_t Serial_UartEnterLowpower(serial_handle_t serialHandle)
|
||||
{
|
||||
serial_uart_state_t *serialUartHandle;
|
||||
|
||||
assert(serialHandle);
|
||||
|
||||
serialUartHandle = (serial_uart_state_t *)serialHandle;
|
||||
|
||||
if (kStatus_HAL_UartSuccess != HAL_UartEnterLowpower(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0])))
|
||||
{
|
||||
return kStatus_SerialManager_Error;
|
||||
}
|
||||
|
||||
return kStatus_SerialManager_Success;
|
||||
}
|
||||
|
||||
serial_manager_status_t Serial_UartExitLowpower(serial_handle_t serialHandle)
|
||||
{
|
||||
serial_uart_state_t *serialUartHandle;
|
||||
|
||||
assert(serialHandle);
|
||||
|
||||
serialUartHandle = (serial_uart_state_t *)serialHandle;
|
||||
|
||||
if (kStatus_HAL_UartSuccess != HAL_UartExitLowpower(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0])))
|
||||
{
|
||||
return kStatus_SerialManager_Error;
|
||||
}
|
||||
|
||||
return kStatus_SerialManager_Success;
|
||||
}
|
||||
|
||||
#endif
|
||||
/*
|
||||
* Copyright 2018 NXP
|
||||
* All rights reserved.
|
||||
*
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
#include "fsl_component_serial_manager.h"
|
||||
#include "fsl_component_serial_port_internal.h"
|
||||
|
||||
#if (defined(SERIAL_PORT_TYPE_UART) && (SERIAL_PORT_TYPE_UART > 0U))
|
||||
#include "fsl_adapter_uart.h"
|
||||
|
||||
#include "fsl_component_serial_port_uart.h"
|
||||
|
||||
/*******************************************************************************
|
||||
* Definitions
|
||||
******************************************************************************/
|
||||
#ifndef NDEBUG
|
||||
#if (defined(DEBUG_CONSOLE_ASSERT_DISABLE) && (DEBUG_CONSOLE_ASSERT_DISABLE > 0U))
|
||||
#undef assert
|
||||
#define assert(n)
|
||||
#else
|
||||
/* MISRA C-2012 Rule 17.2 */
|
||||
#undef assert
|
||||
#define assert(n) \
|
||||
while (!(n)) \
|
||||
{ \
|
||||
; \
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
|
||||
#define SERIAL_PORT_UART_RECEIVE_DATA_LENGTH 1U
|
||||
#define SERIAL_MANAGER_BLOCK_OFFSET (12U)
|
||||
typedef struct _serial_uart_send_state
|
||||
{
|
||||
uint8_t *buffer;
|
||||
uint32_t length;
|
||||
serial_manager_callback_t callback;
|
||||
void *callbackParam;
|
||||
volatile uint8_t busy;
|
||||
} serial_uart_send_state_t;
|
||||
|
||||
typedef struct _serial_uart_recv_state
|
||||
{
|
||||
serial_manager_callback_t callback;
|
||||
void *callbackParam;
|
||||
volatile uint8_t busy;
|
||||
volatile uint8_t rxEnable;
|
||||
uint8_t readBuffer[SERIAL_PORT_UART_RECEIVE_DATA_LENGTH];
|
||||
} serial_uart_recv_state_t;
|
||||
typedef struct _serial_uart_block_state
|
||||
{
|
||||
UART_HANDLE_DEFINE(usartHandleBuffer);
|
||||
} serial_uart_block_state_t;
|
||||
#endif
|
||||
|
||||
typedef struct _serial_uart_state
|
||||
{
|
||||
UART_HANDLE_DEFINE(usartHandleBuffer);
|
||||
#if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
|
||||
serial_uart_send_state_t tx;
|
||||
serial_uart_recv_state_t rx;
|
||||
#endif
|
||||
} serial_uart_state_t;
|
||||
|
||||
/*******************************************************************************
|
||||
* Prototypes
|
||||
******************************************************************************/
|
||||
|
||||
/*******************************************************************************
|
||||
* Code
|
||||
******************************************************************************/
|
||||
|
||||
#if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
|
||||
static serial_manager_status_t Serial_UartEnableReceiving(serial_uart_state_t *serialUartHandle)
|
||||
{
|
||||
#if (defined(HAL_UART_TRANSFER_MODE) && (HAL_UART_TRANSFER_MODE > 0U))
|
||||
hal_uart_transfer_t transfer;
|
||||
#endif
|
||||
if (1U == serialUartHandle->rx.rxEnable)
|
||||
{
|
||||
serialUartHandle->rx.busy = 1U;
|
||||
#if (defined(HAL_UART_TRANSFER_MODE) && (HAL_UART_TRANSFER_MODE > 0U))
|
||||
transfer.data = &serialUartHandle->rx.readBuffer[0];
|
||||
transfer.dataSize = sizeof(serialUartHandle->rx.readBuffer);
|
||||
if (kStatus_HAL_UartSuccess !=
|
||||
HAL_UartTransferReceiveNonBlocking(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]), &transfer))
|
||||
#else
|
||||
if (kStatus_HAL_UartSuccess !=
|
||||
HAL_UartReceiveNonBlocking(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]),
|
||||
&serialUartHandle->rx.readBuffer[0], sizeof(serialUartHandle->rx.readBuffer)))
|
||||
#endif
|
||||
{
|
||||
serialUartHandle->rx.busy = 0U;
|
||||
return kStatus_SerialManager_Error;
|
||||
}
|
||||
}
|
||||
return kStatus_SerialManager_Success;
|
||||
}
|
||||
|
||||
/* UART user callback */
|
||||
static void Serial_UartCallback(hal_uart_handle_t handle, hal_uart_status_t status, void *userData)
|
||||
{
|
||||
serial_uart_state_t *serialUartHandle;
|
||||
serial_manager_callback_message_t msg;
|
||||
#if (defined(HAL_UART_TRANSFER_MODE) && (HAL_UART_TRANSFER_MODE > 0U))
|
||||
hal_uart_transfer_t transfer;
|
||||
#endif
|
||||
|
||||
assert(userData);
|
||||
serialUartHandle = (serial_uart_state_t *)userData;
|
||||
|
||||
if ((hal_uart_status_t)kStatus_HAL_UartRxIdle == status)
|
||||
{
|
||||
if ((NULL != serialUartHandle->rx.callback))
|
||||
{
|
||||
msg.buffer = &serialUartHandle->rx.readBuffer[0];
|
||||
msg.length = sizeof(serialUartHandle->rx.readBuffer);
|
||||
serialUartHandle->rx.callback(serialUartHandle->rx.callbackParam, &msg, kStatus_SerialManager_Success);
|
||||
}
|
||||
#if (defined(HAL_UART_TRANSFER_MODE) && (HAL_UART_TRANSFER_MODE > 0U))
|
||||
transfer.data = &serialUartHandle->rx.readBuffer[0];
|
||||
transfer.dataSize = sizeof(serialUartHandle->rx.readBuffer);
|
||||
serialUartHandle->rx.busy = 0U;
|
||||
if (kStatus_HAL_UartSuccess ==
|
||||
HAL_UartTransferReceiveNonBlocking(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]), &transfer))
|
||||
#else
|
||||
if ((hal_uart_status_t)kStatus_HAL_UartSuccess ==
|
||||
HAL_UartReceiveNonBlocking(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]),
|
||||
&serialUartHandle->rx.readBuffer[0], sizeof(serialUartHandle->rx.readBuffer)))
|
||||
#endif
|
||||
{
|
||||
serialUartHandle->rx.busy = 1U;
|
||||
}
|
||||
}
|
||||
else if ((hal_uart_status_t)kStatus_HAL_UartTxIdle == status)
|
||||
{
|
||||
if (0U != serialUartHandle->tx.busy)
|
||||
{
|
||||
serialUartHandle->tx.busy = 0U;
|
||||
if ((NULL != serialUartHandle->tx.callback))
|
||||
{
|
||||
msg.buffer = serialUartHandle->tx.buffer;
|
||||
msg.length = serialUartHandle->tx.length;
|
||||
serialUartHandle->tx.callback(serialUartHandle->tx.callbackParam, &msg, kStatus_SerialManager_Success);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
serial_manager_status_t Serial_UartInit(serial_handle_t serialHandle, void *serialConfig)
|
||||
{
|
||||
serial_uart_state_t *serialUartHandle;
|
||||
#if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
|
||||
serial_port_uart_config_t *uartConfig = (serial_port_uart_config_t *)serialConfig;
|
||||
#endif
|
||||
serial_manager_status_t serialManagerStatus = kStatus_SerialManager_Success;
|
||||
#if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
|
||||
#if (defined(HAL_UART_TRANSFER_MODE) && (HAL_UART_TRANSFER_MODE > 0U))
|
||||
#if 0 /* Not used below! */
|
||||
hal_uart_transfer_t transfer;
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
assert(serialConfig);
|
||||
assert(serialHandle);
|
||||
assert(SERIAL_PORT_UART_HANDLE_SIZE >= sizeof(serial_uart_state_t));
|
||||
|
||||
serialUartHandle = (serial_uart_state_t *)serialHandle;
|
||||
serialManagerStatus = (serial_manager_status_t)HAL_UartInit(
|
||||
((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]), (const hal_uart_config_t *)serialConfig);
|
||||
assert(kStatus_SerialManager_Success == serialManagerStatus);
|
||||
(void)serialManagerStatus;
|
||||
|
||||
#if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
|
||||
#if (defined(SERIAL_MANAGER_NON_BLOCKING_DUAL_MODE) && (SERIAL_MANAGER_NON_BLOCKING_DUAL_MODE > 0U))
|
||||
serial_manager_type_t type = *(serial_manager_type_t *)((uint32_t)serialHandle - SERIAL_MANAGER_BLOCK_OFFSET);
|
||||
if (type == kSerialManager_Blocking)
|
||||
{
|
||||
return serialManagerStatus;
|
||||
}
|
||||
#endif /* SERIAL_MANAGER_NON_BLOCKING_DUAL_MODE */
|
||||
serialUartHandle->rx.rxEnable = uartConfig->enableRx;
|
||||
#if (defined(HAL_UART_TRANSFER_MODE) && (HAL_UART_TRANSFER_MODE > 0U))
|
||||
|
||||
(void)HAL_UartTransferInstallCallback(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]),
|
||||
Serial_UartCallback, serialUartHandle);
|
||||
#else
|
||||
(void)HAL_UartInstallCallback(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]), Serial_UartCallback,
|
||||
serialUartHandle);
|
||||
#endif
|
||||
#endif
|
||||
#if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
|
||||
serialManagerStatus = Serial_UartEnableReceiving(serialUartHandle);
|
||||
#endif
|
||||
|
||||
return serialManagerStatus;
|
||||
}
|
||||
|
||||
serial_manager_status_t Serial_UartDeinit(serial_handle_t serialHandle)
|
||||
{
|
||||
serial_uart_state_t *serialUartHandle;
|
||||
|
||||
assert(serialHandle);
|
||||
|
||||
serialUartHandle = (serial_uart_state_t *)serialHandle;
|
||||
|
||||
#if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
|
||||
#if (defined(HAL_UART_TRANSFER_MODE) && (HAL_UART_TRANSFER_MODE > 0U))
|
||||
(void)HAL_UartTransferAbortReceive(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]));
|
||||
#else
|
||||
(void)HAL_UartAbortReceive(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]));
|
||||
#endif
|
||||
#endif
|
||||
(void)HAL_UartDeinit(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]));
|
||||
|
||||
#if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
|
||||
serialUartHandle->tx.busy = 0U;
|
||||
serialUartHandle->rx.busy = 0U;
|
||||
#endif
|
||||
|
||||
return kStatus_SerialManager_Success;
|
||||
}
|
||||
|
||||
#if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
|
||||
|
||||
serial_manager_status_t Serial_UartWrite(serial_handle_t serialHandle, uint8_t *buffer, uint32_t length)
|
||||
{
|
||||
serial_uart_state_t *serialUartHandle;
|
||||
hal_uart_status_t uartstatus;
|
||||
#if (defined(HAL_UART_TRANSFER_MODE) && (HAL_UART_TRANSFER_MODE > 0U))
|
||||
hal_uart_transfer_t transfer;
|
||||
#endif
|
||||
|
||||
assert(serialHandle);
|
||||
assert(buffer);
|
||||
assert(length);
|
||||
|
||||
serialUartHandle = (serial_uart_state_t *)serialHandle;
|
||||
#if (defined(SERIAL_MANAGER_NON_BLOCKING_DUAL_MODE) && (SERIAL_MANAGER_NON_BLOCKING_DUAL_MODE > 0U))
|
||||
serial_manager_type_t type = *(serial_manager_type_t *)((uint32_t)serialHandle - SERIAL_MANAGER_BLOCK_OFFSET);
|
||||
if (type == kSerialManager_Blocking)
|
||||
{
|
||||
return (serial_manager_status_t)HAL_UartSendBlocking(
|
||||
((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]), buffer, length);
|
||||
}
|
||||
#endif /* SERIAL_MANAGER_NON_BLOCKING_DUAL_MODE */
|
||||
if (0U != serialUartHandle->tx.busy)
|
||||
{
|
||||
return kStatus_SerialManager_Busy;
|
||||
}
|
||||
serialUartHandle->tx.busy = 1U;
|
||||
|
||||
serialUartHandle->tx.buffer = buffer;
|
||||
serialUartHandle->tx.length = length;
|
||||
|
||||
#if (defined(HAL_UART_TRANSFER_MODE) && (HAL_UART_TRANSFER_MODE > 0U))
|
||||
transfer.data = buffer;
|
||||
transfer.dataSize = length;
|
||||
uartstatus =
|
||||
HAL_UartTransferSendNonBlocking(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]), &transfer);
|
||||
#else
|
||||
|
||||
uartstatus = HAL_UartSendNonBlocking(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]), buffer, length);
|
||||
#endif
|
||||
assert(kStatus_HAL_UartSuccess == uartstatus);
|
||||
(void)uartstatus;
|
||||
|
||||
return kStatus_SerialManager_Success;
|
||||
}
|
||||
serial_manager_status_t Serial_UartWriteBlocking(serial_handle_t serialHandle, uint8_t *buffer, uint32_t length)
|
||||
{
|
||||
serial_uart_state_t *serialUartHandle;
|
||||
|
||||
assert(serialHandle);
|
||||
assert(buffer);
|
||||
assert(length);
|
||||
|
||||
serialUartHandle = (serial_uart_state_t *)serialHandle;
|
||||
|
||||
return (serial_manager_status_t)HAL_UartSendBlocking(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]),
|
||||
buffer, length);
|
||||
}
|
||||
#if (defined(SERIAL_MANAGER_NON_BLOCKING_DUAL_MODE) && (SERIAL_MANAGER_NON_BLOCKING_DUAL_MODE > 0U))
|
||||
serial_manager_status_t Serial_UartRead(serial_handle_t serialHandle, uint8_t *buffer, uint32_t length)
|
||||
{
|
||||
serial_uart_state_t *serialUartHandle;
|
||||
|
||||
assert(serialHandle);
|
||||
assert(buffer);
|
||||
assert(length);
|
||||
|
||||
serialUartHandle = (serial_uart_state_t *)serialHandle;
|
||||
|
||||
return (serial_manager_status_t)HAL_UartReceiveBlocking(
|
||||
((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]), buffer, length);
|
||||
}
|
||||
#endif /* SERIAL_MANAGER_NON_BLOCKING_DUAL_MODE */
|
||||
#else
|
||||
|
||||
serial_manager_status_t Serial_UartWrite(serial_handle_t serialHandle, uint8_t *buffer, uint32_t length)
|
||||
{
|
||||
serial_uart_state_t *serialUartHandle;
|
||||
|
||||
assert(serialHandle);
|
||||
assert(buffer);
|
||||
assert(length);
|
||||
|
||||
serialUartHandle = (serial_uart_state_t *)serialHandle;
|
||||
|
||||
return (serial_manager_status_t)HAL_UartSendBlocking(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]),
|
||||
buffer, length);
|
||||
}
|
||||
|
||||
serial_manager_status_t Serial_UartRead(serial_handle_t serialHandle, uint8_t *buffer, uint32_t length)
|
||||
{
|
||||
serial_uart_state_t *serialUartHandle;
|
||||
|
||||
assert(serialHandle);
|
||||
assert(buffer);
|
||||
assert(length);
|
||||
|
||||
serialUartHandle = (serial_uart_state_t *)serialHandle;
|
||||
|
||||
return (serial_manager_status_t)HAL_UartReceiveBlocking(
|
||||
((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]), buffer, length);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
|
||||
serial_manager_status_t Serial_UartCancelWrite(serial_handle_t serialHandle)
|
||||
{
|
||||
serial_uart_state_t *serialUartHandle;
|
||||
serial_manager_callback_message_t msg;
|
||||
uint32_t primask;
|
||||
uint8_t isBusy = 0U;
|
||||
|
||||
assert(serialHandle);
|
||||
|
||||
serialUartHandle = (serial_uart_state_t *)serialHandle;
|
||||
|
||||
primask = DisableGlobalIRQ();
|
||||
isBusy = serialUartHandle->tx.busy;
|
||||
serialUartHandle->tx.busy = 0U;
|
||||
EnableGlobalIRQ(primask);
|
||||
|
||||
#if (defined(HAL_UART_TRANSFER_MODE) && (HAL_UART_TRANSFER_MODE > 0U))
|
||||
(void)HAL_UartTransferAbortSend(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]));
|
||||
#else
|
||||
(void)HAL_UartAbortSend(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]));
|
||||
#endif
|
||||
if (0U != isBusy)
|
||||
{
|
||||
if ((NULL != serialUartHandle->tx.callback))
|
||||
{
|
||||
msg.buffer = serialUartHandle->tx.buffer;
|
||||
msg.length = serialUartHandle->tx.length;
|
||||
serialUartHandle->tx.callback(serialUartHandle->tx.callbackParam, &msg, kStatus_SerialManager_Canceled);
|
||||
}
|
||||
}
|
||||
return kStatus_SerialManager_Success;
|
||||
}
|
||||
|
||||
serial_manager_status_t Serial_UartInstallTxCallback(serial_handle_t serialHandle,
|
||||
serial_manager_callback_t callback,
|
||||
void *callbackParam)
|
||||
{
|
||||
serial_uart_state_t *serialUartHandle;
|
||||
|
||||
assert(serialHandle);
|
||||
|
||||
serialUartHandle = (serial_uart_state_t *)serialHandle;
|
||||
|
||||
serialUartHandle->tx.callback = callback;
|
||||
serialUartHandle->tx.callbackParam = callbackParam;
|
||||
|
||||
return kStatus_SerialManager_Success;
|
||||
}
|
||||
|
||||
serial_manager_status_t Serial_UartInstallRxCallback(serial_handle_t serialHandle,
|
||||
serial_manager_callback_t callback,
|
||||
void *callbackParam)
|
||||
{
|
||||
serial_uart_state_t *serialUartHandle;
|
||||
|
||||
assert(serialHandle);
|
||||
|
||||
serialUartHandle = (serial_uart_state_t *)serialHandle;
|
||||
|
||||
serialUartHandle->rx.callback = callback;
|
||||
serialUartHandle->rx.callbackParam = callbackParam;
|
||||
|
||||
return kStatus_SerialManager_Success;
|
||||
}
|
||||
|
||||
void Serial_UartIsrFunction(serial_handle_t serialHandle)
|
||||
{
|
||||
serial_uart_state_t *serialUartHandle;
|
||||
|
||||
assert(serialHandle);
|
||||
|
||||
serialUartHandle = (serial_uart_state_t *)serialHandle;
|
||||
|
||||
HAL_UartIsrFunction(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]));
|
||||
}
|
||||
#endif
|
||||
|
||||
serial_manager_status_t Serial_UartEnterLowpower(serial_handle_t serialHandle)
|
||||
{
|
||||
serial_uart_state_t *serialUartHandle;
|
||||
hal_uart_status_t uartstatus;
|
||||
|
||||
assert(serialHandle);
|
||||
|
||||
serialUartHandle = (serial_uart_state_t *)serialHandle;
|
||||
|
||||
uartstatus = HAL_UartEnterLowpower(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]));
|
||||
assert(kStatus_HAL_UartSuccess == uartstatus);
|
||||
(void)uartstatus;
|
||||
|
||||
return kStatus_SerialManager_Success;
|
||||
}
|
||||
|
||||
serial_manager_status_t Serial_UartExitLowpower(serial_handle_t serialHandle)
|
||||
{
|
||||
serial_uart_state_t *serialUartHandle;
|
||||
serial_manager_status_t status = kStatus_SerialManager_Success;
|
||||
hal_uart_status_t uartstatus;
|
||||
|
||||
assert(serialHandle);
|
||||
|
||||
serialUartHandle = (serial_uart_state_t *)serialHandle;
|
||||
|
||||
uartstatus = HAL_UartExitLowpower(((hal_uart_handle_t)&serialUartHandle->usartHandleBuffer[0]));
|
||||
assert(kStatus_HAL_UartSuccess == uartstatus);
|
||||
(void)uartstatus;
|
||||
|
||||
#if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
|
||||
serial_manager_type_t type =
|
||||
*(serial_manager_type_t *)(void *)((uint8_t *)serialHandle - SERIAL_MANAGER_BLOCK_OFFSET);
|
||||
if (type != kSerialManager_Blocking)
|
||||
{
|
||||
status = Serial_UartEnableReceiving(serialUartHandle);
|
||||
}
|
||||
#endif
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
#endif
|
@@ -1,57 +1,80 @@
|
||||
/*
|
||||
* Copyright 2018 NXP
|
||||
* All rights reserved.
|
||||
*
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
#ifndef __SERIAL_PORT_UART_H__
|
||||
#define __SERIAL_PORT_UART_H__
|
||||
|
||||
#include "uart.h"
|
||||
|
||||
/*!
|
||||
* @addtogroup serial_port_uart
|
||||
* @{
|
||||
*/
|
||||
|
||||
/*******************************************************************************
|
||||
* Definitions
|
||||
******************************************************************************/
|
||||
/*! @brief serial port uart handle size*/
|
||||
#if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
|
||||
#define SERIAL_PORT_UART_HANDLE_SIZE (76U + HAL_UART_HANDLE_SIZE)
|
||||
#else
|
||||
#define SERIAL_PORT_UART_HANDLE_SIZE (HAL_UART_HANDLE_SIZE)
|
||||
#endif
|
||||
|
||||
/*! @brief serial port uart parity mode*/
|
||||
typedef enum _serial_port_uart_parity_mode
|
||||
{
|
||||
kSerialManager_UartParityDisabled = 0x0U, /*!< Parity disabled */
|
||||
kSerialManager_UartParityEven = 0x1U, /*!< Parity even enabled */
|
||||
kSerialManager_UartParityOdd = 0x2U, /*!< Parity odd enabled */
|
||||
} serial_port_uart_parity_mode_t;
|
||||
|
||||
/*! @brief serial port uart stop bit count*/
|
||||
typedef enum _serial_port_uart_stop_bit_count
|
||||
{
|
||||
kSerialManager_UartOneStopBit = 0U, /*!< One stop bit */
|
||||
kSerialManager_UartTwoStopBit = 1U, /*!< Two stop bits */
|
||||
} serial_port_uart_stop_bit_count_t;
|
||||
|
||||
/*! @brief serial port uart config struct*/
|
||||
typedef struct _serial_port_uart_config
|
||||
{
|
||||
uint32_t clockRate; /*!< clock rate */
|
||||
uint32_t baudRate; /*!< baud rate */
|
||||
serial_port_uart_parity_mode_t parityMode; /*!< Parity mode, disabled (default), even, odd */
|
||||
serial_port_uart_stop_bit_count_t stopBitCount; /*!< Number of stop bits, 1 stop bit (default) or 2 stop bits */
|
||||
uint8_t instance; /*!< Instance (0 - UART0, 1 - UART1, ...), detail information
|
||||
please refer to the SOC corresponding RM. */
|
||||
uint8_t enableRx; /*!< Enable RX */
|
||||
uint8_t enableTx; /*!< Enable TX */
|
||||
} serial_port_uart_config_t;
|
||||
/*! @} */
|
||||
#endif /* __SERIAL_PORT_UART_H__ */
|
||||
/*
|
||||
* Copyright 2018 NXP
|
||||
* All rights reserved.
|
||||
*
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
#ifndef __SERIAL_PORT_UART_H__
|
||||
#define __SERIAL_PORT_UART_H__
|
||||
|
||||
#include "fsl_adapter_uart.h"
|
||||
|
||||
/*!
|
||||
* @addtogroup serial_port_uart
|
||||
* @{
|
||||
*/
|
||||
|
||||
/*******************************************************************************
|
||||
* Definitions
|
||||
******************************************************************************/
|
||||
/*! @brief serial port uart handle size*/
|
||||
#if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
|
||||
#define SERIAL_PORT_UART_HANDLE_SIZE (76U + HAL_UART_HANDLE_SIZE)
|
||||
#define SERIAL_PORT_UART_BLOCK_HANDLE_SIZE (HAL_UART_BLOCK_HANDLE_SIZE)
|
||||
#else
|
||||
#define SERIAL_PORT_UART_HANDLE_SIZE (HAL_UART_HANDLE_SIZE)
|
||||
#endif
|
||||
|
||||
#ifndef SERIAL_USE_CONFIGURE_STRUCTURE
|
||||
#define SERIAL_USE_CONFIGURE_STRUCTURE (0U) /*!< Enable or disable the confgure structure pointer */
|
||||
#endif
|
||||
|
||||
/*! @brief serial port uart parity mode*/
|
||||
typedef enum _serial_port_uart_parity_mode
|
||||
{
|
||||
kSerialManager_UartParityDisabled = 0x0U, /*!< Parity disabled */
|
||||
kSerialManager_UartParityEven = 0x1U, /*!< Parity even enabled */
|
||||
kSerialManager_UartParityOdd = 0x2U, /*!< Parity odd enabled */
|
||||
} serial_port_uart_parity_mode_t;
|
||||
|
||||
/*! @brief serial port uart stop bit count*/
|
||||
typedef enum _serial_port_uart_stop_bit_count
|
||||
{
|
||||
kSerialManager_UartOneStopBit = 0U, /*!< One stop bit */
|
||||
kSerialManager_UartTwoStopBit = 1U, /*!< Two stop bits */
|
||||
} serial_port_uart_stop_bit_count_t;
|
||||
|
||||
#if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
|
||||
/*! @brief serial port uart block mode*/
|
||||
typedef enum _serial_port_uart_block_mode
|
||||
{
|
||||
kSerialManager_UartNonBlockMode = 0x0U, /*!< Uart NonBlock Mode */
|
||||
kSerialManager_UartBlockMode = 0x1U, /*!< Uart Block Mode */
|
||||
} serial_port_uart_block_mode_t;
|
||||
#endif /* SERIAL_MANAGER_NON_BLOCKING_MODE */
|
||||
|
||||
typedef struct _serial_port_uart_config
|
||||
{
|
||||
uint32_t clockRate; /*!< clock rate */
|
||||
uint32_t baudRate; /*!< baud rate */
|
||||
serial_port_uart_parity_mode_t parityMode; /*!< Parity mode, disabled (default), even, odd */
|
||||
serial_port_uart_stop_bit_count_t stopBitCount; /*!< Number of stop bits, 1 stop bit (default) or 2 stop bits */
|
||||
|
||||
uint8_t enableRx; /*!< Enable RX */
|
||||
uint8_t enableTx; /*!< Enable TX */
|
||||
uint8_t enableRxRTS; /*!< Enable RX RTS */
|
||||
uint8_t enableTxCTS; /*!< Enable TX CTS */
|
||||
uint8_t instance; /*!< Instance (0 - UART0, 1 - UART1, ...), detail information
|
||||
please refer to the SOC corresponding RM. */
|
||||
#if (defined(SERIAL_MANAGER_NON_BLOCKING_MODE) && (SERIAL_MANAGER_NON_BLOCKING_MODE > 0U))
|
||||
serial_port_uart_block_mode_t mode; /*!< serial port uart block mode */
|
||||
#endif /* SERIAL_MANAGER_NON_BLOCKING_MODE */
|
||||
#if (defined(HAL_UART_ADAPTER_FIFO) && (HAL_UART_ADAPTER_FIFO > 0u))
|
||||
uint8_t txFifoWatermark;
|
||||
uint8_t rxFifoWatermark;
|
||||
#endif
|
||||
} serial_port_uart_config_t;
|
||||
/*! @} */
|
||||
#endif /* __SERIAL_PORT_UART_H__ */
|
File diff suppressed because it is too large
Load Diff
@@ -1,76 +1,82 @@
|
||||
/*
|
||||
* Copyright (c) 2015 - 2016, Freescale Semiconductor, Inc.
|
||||
* Copyright 2016 - 2018 NXP
|
||||
* All rights reserved.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
#ifndef __SERIAL_PORT_USB_H__
|
||||
#define __SERIAL_PORT_USB_H__
|
||||
|
||||
#if defined(FSL_RTOS_FREE_RTOS)
|
||||
#include "FreeRTOS.h"
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* @addtogroup serial_port_usb
|
||||
* @{
|
||||
*/
|
||||
|
||||
/*******************************************************************************
|
||||
* Definitions
|
||||
******************************************************************************/
|
||||
/*! @brief serial port usb handle size*/
|
||||
#define SERIAL_PORT_USB_CDC_HANDLE_SIZE (72)
|
||||
|
||||
/*! @brief USB interrupt priority*/
|
||||
#if defined(__GIC_PRIO_BITS)
|
||||
#define USB_DEVICE_INTERRUPT_PRIORITY (25U)
|
||||
#else
|
||||
#if defined(configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY)
|
||||
#define USB_DEVICE_INTERRUPT_PRIORITY (configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY)
|
||||
#else
|
||||
/* The default value 3 is used to support different ARM Core, such as CM0P, CM4, CM7, and CM33, etc.
|
||||
* The minimum number of priority bits implemented in the NVIC is 2 on these SOCs. The value of mininum
|
||||
* priority is 3 (2^2 - 1). So, the default value is 3.
|
||||
*/
|
||||
#define USB_DEVICE_INTERRUPT_PRIORITY (3U)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/*! @brief USB controller ID */
|
||||
typedef enum _serial_port_usb_cdc_controller_index
|
||||
{
|
||||
kSerialManager_UsbControllerKhci0 = 0U, /*!< KHCI 0U */
|
||||
kSerialManager_UsbControllerKhci1 = 1U, /*!< KHCI 1U, Currently, there are no platforms which have two KHCI IPs,
|
||||
this is reserved to be used in the future. */
|
||||
kSerialManager_UsbControllerEhci0 = 2U, /*!< EHCI 0U */
|
||||
kSerialManager_UsbControllerEhci1 = 3U, /*!< EHCI 1U, Currently, there are no platforms which have two EHCI IPs,
|
||||
this is reserved to be used in the future. */
|
||||
|
||||
kSerialManager_UsbControllerLpcIp3511Fs0 = 4U, /*!< LPC USB IP3511 FS controller 0 */
|
||||
kSerialManager_UsbControllerLpcIp3511Fs1 = 5U, /*!< LPC USB IP3511 FS controller 1, there are no platforms which
|
||||
have two IP3511 IPs, this is reserved to be used in the future. */
|
||||
|
||||
kSerialManager_UsbControllerLpcIp3511Hs0 = 6U, /*!< LPC USB IP3511 HS controller 0 */
|
||||
kSerialManager_UsbControllerLpcIp3511Hs1 = 7U, /*!< LPC USB IP3511 HS controller 1, there are no platforms which
|
||||
have two IP3511 IPs, this is reserved to be used in the future. */
|
||||
|
||||
kSerialManager_UsbControllerOhci0 = 8U, /*!< OHCI 0U */
|
||||
kSerialManager_UsbControllerOhci1 = 9U, /*!< OHCI 1U, Currently, there are no platforms which have two OHCI IPs,
|
||||
this is reserved to be used in the future. */
|
||||
|
||||
kSerialManager_UsbControllerIp3516Hs0 = 10U, /*!< IP3516HS 0U */
|
||||
kSerialManager_UsbControllerIp3516Hs1 = 11U, /*!< IP3516HS 1U, Currently, there are no platforms which have two
|
||||
IP3516HS IPs, this is reserved to be used in the future. */
|
||||
} serial_port_usb_cdc_controller_index_t;
|
||||
|
||||
/*! @brief serial port usb config struct*/
|
||||
typedef struct _serial_port_usb_cdc_config
|
||||
{
|
||||
serial_port_usb_cdc_controller_index_t controllerIndex; /*!< controller index */
|
||||
} serial_port_usb_cdc_config_t;
|
||||
|
||||
/*! @} */
|
||||
#endif /* __SERIAL_PORT_USB_H__ */
|
||||
/*
|
||||
* Copyright (c) 2015 - 2016, Freescale Semiconductor, Inc.
|
||||
* Copyright 2016 - 2018 NXP
|
||||
* All rights reserved.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
#ifndef __SERIAL_PORT_USB_H__
|
||||
#define __SERIAL_PORT_USB_H__
|
||||
|
||||
#if defined(FSL_RTOS_FREE_RTOS)
|
||||
#include "FreeRTOS.h"
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* @addtogroup serial_port_usb
|
||||
* @{
|
||||
*/
|
||||
|
||||
/*******************************************************************************
|
||||
* Definitions
|
||||
******************************************************************************/
|
||||
/*! @brief serial port usb handle size*/
|
||||
#define SERIAL_PORT_USB_CDC_HANDLE_SIZE (72U)
|
||||
|
||||
/*! @brief USB interrupt priority*/
|
||||
#if defined(__GIC_PRIO_BITS)
|
||||
#ifndef USB_DEVICE_INTERRUPT_PRIORITY
|
||||
#define USB_DEVICE_INTERRUPT_PRIORITY (25U)
|
||||
#endif
|
||||
#else
|
||||
#if defined(configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY)
|
||||
#ifndef USB_DEVICE_INTERRUPT_PRIORITY
|
||||
#define USB_DEVICE_INTERRUPT_PRIORITY (configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY)
|
||||
#endif
|
||||
#else
|
||||
/* The default value 3 is used to support different ARM Core, such as CM0P, CM4, CM7, and CM33, etc.
|
||||
* The minimum number of priority bits implemented in the NVIC is 2 on these SOCs. The value of mininum
|
||||
* priority is 3 (2^2 - 1). So, the default value is 3.
|
||||
*/
|
||||
#ifndef USB_DEVICE_INTERRUPT_PRIORITY
|
||||
#define USB_DEVICE_INTERRUPT_PRIORITY (3U)
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/*! @brief USB controller ID */
|
||||
typedef enum _serial_port_usb_cdc_controller_index
|
||||
{
|
||||
kSerialManager_UsbControllerKhci0 = 0U, /*!< KHCI 0U */
|
||||
kSerialManager_UsbControllerKhci1 = 1U, /*!< KHCI 1U, Currently, there are no platforms which have two KHCI IPs,
|
||||
this is reserved to be used in the future. */
|
||||
kSerialManager_UsbControllerEhci0 = 2U, /*!< EHCI 0U */
|
||||
kSerialManager_UsbControllerEhci1 = 3U, /*!< EHCI 1U, Currently, there are no platforms which have two EHCI IPs,
|
||||
this is reserved to be used in the future. */
|
||||
|
||||
kSerialManager_UsbControllerLpcIp3511Fs0 = 4U, /*!< LPC USB IP3511 FS controller 0 */
|
||||
kSerialManager_UsbControllerLpcIp3511Fs1 = 5U, /*!< LPC USB IP3511 FS controller 1, there are no platforms which
|
||||
have two IP3511 IPs, this is reserved to be used in the future. */
|
||||
|
||||
kSerialManager_UsbControllerLpcIp3511Hs0 = 6U, /*!< LPC USB IP3511 HS controller 0 */
|
||||
kSerialManager_UsbControllerLpcIp3511Hs1 = 7U, /*!< LPC USB IP3511 HS controller 1, there are no platforms which
|
||||
have two IP3511 IPs, this is reserved to be used in the future. */
|
||||
|
||||
kSerialManager_UsbControllerOhci0 = 8U, /*!< OHCI 0U */
|
||||
kSerialManager_UsbControllerOhci1 = 9U, /*!< OHCI 1U, Currently, there are no platforms which have two OHCI IPs,
|
||||
this is reserved to be used in the future. */
|
||||
|
||||
kSerialManager_UsbControllerIp3516Hs0 = 10U, /*!< IP3516HS 0U */
|
||||
kSerialManager_UsbControllerIp3516Hs1 = 11U, /*!< IP3516HS 1U, Currently, there are no platforms which have two
|
||||
IP3516HS IPs, this is reserved to be used in the future. */
|
||||
} serial_port_usb_cdc_controller_index_t;
|
||||
|
||||
/*! @brief serial port usb config struct*/
|
||||
typedef struct _serial_port_usb_cdc_config
|
||||
{
|
||||
serial_port_usb_cdc_controller_index_t controllerIndex; /*!< controller index */
|
||||
} serial_port_usb_cdc_config_t;
|
||||
|
||||
/*! @} */
|
||||
#endif /* __SERIAL_PORT_USB_H__ */
|
@@ -0,0 +1,400 @@
|
||||
/*
|
||||
* Copyright 2019 NXP
|
||||
* All rights reserved.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
#include "fsl_common.h"
|
||||
#include "fsl_component_serial_manager.h"
|
||||
#include "fsl_component_serial_port_internal.h"
|
||||
|
||||
#if (defined(SERIAL_PORT_TYPE_VIRTUAL) && (SERIAL_PORT_TYPE_VIRTUAL > 0U))
|
||||
|
||||
#include "fsl_component_serial_port_virtual.h"
|
||||
|
||||
/*******************************************************************************
|
||||
* Definitions
|
||||
******************************************************************************/
|
||||
|
||||
#ifndef NDEBUG
|
||||
#if (defined(DEBUG_CONSOLE_ASSERT_DISABLE) && (DEBUG_CONSOLE_ASSERT_DISABLE > 0U))
|
||||
#undef assert
|
||||
#define assert(n)
|
||||
#else
|
||||
/* MISRA C-2012 Rule 17.2 */
|
||||
#undef assert
|
||||
#define assert(n) \
|
||||
while (!(n)) \
|
||||
{ \
|
||||
; \
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
/* Weak function. */
|
||||
#if defined(__GNUC__)
|
||||
#define __WEAK_FUNC __attribute__((weak))
|
||||
#elif defined(__ICCARM__)
|
||||
#define __WEAK_FUNC __weak
|
||||
#elif defined(__CC_ARM) || defined(__ARMCC_VERSION)
|
||||
#define __WEAK_FUNC __attribute__((weak))
|
||||
#endif
|
||||
|
||||
typedef struct _serial_port_virtual_send_state
|
||||
{
|
||||
serial_manager_callback_t callback;
|
||||
void *callbackParam;
|
||||
} serial_port_virtual_send_state_t;
|
||||
|
||||
typedef struct _serial_port_virtual_recv_state
|
||||
{
|
||||
serial_manager_callback_t callback;
|
||||
void *callbackParam;
|
||||
} serial_port_virtual_recv_state_t;
|
||||
|
||||
/* Define the types for application */
|
||||
typedef struct _serial_port_virtual_state
|
||||
{
|
||||
struct _serial_port_virtual_state *next; /* Next pointer of the interface */
|
||||
serial_port_virtual_send_state_t tx;
|
||||
serial_port_virtual_recv_state_t rx;
|
||||
uint8_t instance; /* The instance of the interface */
|
||||
} serial_port_virtual_state_t;
|
||||
|
||||
/*******************************************************************************
|
||||
* Prototypes
|
||||
******************************************************************************/
|
||||
|
||||
/*******************************************************************************
|
||||
* Variables
|
||||
******************************************************************************/
|
||||
|
||||
static serial_port_virtual_state_t *s_serialPortVirtualHead;
|
||||
|
||||
/*******************************************************************************
|
||||
* Code
|
||||
******************************************************************************/
|
||||
|
||||
static serial_manager_status_t Serial_PortVirtualAddItem(serial_port_virtual_state_t **head,
|
||||
serial_port_virtual_state_t *node)
|
||||
{
|
||||
serial_port_virtual_state_t *p = *head;
|
||||
uint32_t regPrimask;
|
||||
|
||||
regPrimask = DisableGlobalIRQ();
|
||||
|
||||
if (NULL == p)
|
||||
{
|
||||
*head = node;
|
||||
}
|
||||
else
|
||||
{
|
||||
while (NULL != p->next)
|
||||
{
|
||||
if (p == node)
|
||||
{
|
||||
EnableGlobalIRQ(regPrimask);
|
||||
return kStatus_SerialManager_Error;
|
||||
}
|
||||
p = p->next;
|
||||
}
|
||||
|
||||
p->next = node;
|
||||
}
|
||||
node->next = NULL;
|
||||
EnableGlobalIRQ(regPrimask);
|
||||
return kStatus_SerialManager_Success;
|
||||
}
|
||||
|
||||
static serial_manager_status_t Serial_PortVirtualRemoveItem(serial_port_virtual_state_t **head,
|
||||
serial_port_virtual_state_t *node)
|
||||
{
|
||||
serial_port_virtual_state_t *p = *head;
|
||||
serial_port_virtual_state_t *q = NULL;
|
||||
uint32_t regPrimask;
|
||||
|
||||
regPrimask = DisableGlobalIRQ();
|
||||
while (NULL != p)
|
||||
{
|
||||
if (p == node)
|
||||
{
|
||||
if (NULL == q)
|
||||
{
|
||||
*head = p->next;
|
||||
}
|
||||
else
|
||||
{
|
||||
q->next = p->next;
|
||||
}
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
q = p;
|
||||
p = p->next;
|
||||
}
|
||||
}
|
||||
EnableGlobalIRQ(regPrimask);
|
||||
return kStatus_SerialManager_Success;
|
||||
}
|
||||
|
||||
static serial_manager_status_t Serial_PortVirtualGetInstance(uint8_t controller, serial_port_virtual_state_t **handle)
|
||||
{
|
||||
serial_port_virtual_state_t *p = s_serialPortVirtualHead;
|
||||
uint32_t regPrimask;
|
||||
|
||||
*handle = NULL;
|
||||
regPrimask = DisableGlobalIRQ();
|
||||
while (NULL != p)
|
||||
{
|
||||
if (p->instance == controller)
|
||||
{
|
||||
*handle = p;
|
||||
break;
|
||||
}
|
||||
}
|
||||
EnableGlobalIRQ(regPrimask);
|
||||
return (NULL != p) ? kStatus_SerialManager_Success : kStatus_SerialManager_Error;
|
||||
}
|
||||
|
||||
static void Serial_PortVirtualTxCallback(void *callbackParam,
|
||||
serial_manager_callback_message_t *message,
|
||||
serial_manager_status_t status)
|
||||
{
|
||||
serial_port_virtual_state_t *serialPortVirtual;
|
||||
|
||||
if (NULL != callbackParam)
|
||||
{
|
||||
serialPortVirtual = (serial_port_virtual_state_t *)callbackParam;
|
||||
if (NULL != serialPortVirtual->tx.callback)
|
||||
{
|
||||
serialPortVirtual->tx.callback(serialPortVirtual->tx.callbackParam, message, status);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void Serial_PortVirtualRxCallback(void *callbackParam,
|
||||
serial_manager_callback_message_t *message,
|
||||
serial_manager_status_t status)
|
||||
{
|
||||
serial_port_virtual_state_t *serialPortVirtual;
|
||||
|
||||
if (NULL != callbackParam)
|
||||
{
|
||||
serialPortVirtual = (serial_port_virtual_state_t *)callbackParam;
|
||||
if (NULL != serialPortVirtual->rx.callback)
|
||||
{
|
||||
serialPortVirtual->rx.callback(serialPortVirtual->rx.callbackParam, message, status);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
__WEAK_FUNC status_t USB_DeviceVcomInit(uint8_t controller);
|
||||
__WEAK_FUNC status_t USB_DeviceVcomInit(uint8_t controller)
|
||||
{
|
||||
return kStatus_Success;
|
||||
}
|
||||
|
||||
__WEAK_FUNC status_t USB_DeviceVcomDeinit(uint8_t controller);
|
||||
__WEAK_FUNC status_t USB_DeviceVcomDeinit(uint8_t controller)
|
||||
{
|
||||
return kStatus_Success;
|
||||
}
|
||||
|
||||
__WEAK_FUNC status_t USB_DeviceVcomWrite(uint8_t controller, uint8_t *buffer, uint32_t length);
|
||||
__WEAK_FUNC status_t USB_DeviceVcomWrite(uint8_t controller, uint8_t *buffer, uint32_t length)
|
||||
{
|
||||
serial_port_virtual_state_t *serialPortVirtual;
|
||||
serial_manager_callback_message_t msg;
|
||||
|
||||
if (kStatus_SerialManager_Success != Serial_PortVirtualGetInstance(controller, &serialPortVirtual))
|
||||
{
|
||||
return kStatus_Fail;
|
||||
}
|
||||
|
||||
if ((NULL != serialPortVirtual->tx.callback))
|
||||
{
|
||||
msg.buffer = buffer;
|
||||
msg.length = length;
|
||||
serialPortVirtual->tx.callback(serialPortVirtual->tx.callbackParam, &msg, kStatus_SerialManager_Success);
|
||||
}
|
||||
return kStatus_Success;
|
||||
}
|
||||
|
||||
__WEAK_FUNC status_t USB_DeviceVcomRead(uint8_t controller, uint8_t *buffer, uint32_t length);
|
||||
__WEAK_FUNC status_t USB_DeviceVcomRead(uint8_t controller, uint8_t *buffer, uint32_t length)
|
||||
{
|
||||
return kStatus_Fail;
|
||||
}
|
||||
|
||||
__WEAK_FUNC status_t USB_DeviceVcomCancelWrite(uint8_t controller);
|
||||
__WEAK_FUNC status_t USB_DeviceVcomCancelWrite(uint8_t controller)
|
||||
{
|
||||
return kStatus_Success;
|
||||
}
|
||||
|
||||
__WEAK_FUNC status_t USB_DeviceVcomInstallTxCallback(uint8_t controller,
|
||||
serial_manager_callback_t callback,
|
||||
void *callbackParam);
|
||||
__WEAK_FUNC status_t USB_DeviceVcomInstallTxCallback(uint8_t controller,
|
||||
serial_manager_callback_t callback,
|
||||
void *callbackParam)
|
||||
{
|
||||
return kStatus_Success;
|
||||
}
|
||||
|
||||
__WEAK_FUNC status_t USB_DeviceVcomInstallRxCallback(uint8_t controller,
|
||||
serial_manager_callback_t callback,
|
||||
void *callbackParam);
|
||||
__WEAK_FUNC status_t USB_DeviceVcomInstallRxCallback(uint8_t controller,
|
||||
serial_manager_callback_t callback,
|
||||
void *callbackParam)
|
||||
{
|
||||
return kStatus_Success;
|
||||
}
|
||||
|
||||
__WEAK_FUNC void USB_DeviceVcomIsrFunction(uint8_t controller);
|
||||
__WEAK_FUNC void USB_DeviceVcomIsrFunction(uint8_t controller)
|
||||
{
|
||||
}
|
||||
|
||||
serial_manager_status_t Serial_PortVirtualInit(serial_handle_t serialHandle, void *config)
|
||||
{
|
||||
serial_port_virtual_state_t *serialPortVirtual;
|
||||
serial_port_virtual_config_t *serialPortVirtualConfig;
|
||||
serial_manager_status_t ret;
|
||||
|
||||
assert(NULL != config);
|
||||
assert(NULL != serialHandle);
|
||||
assert(SERIAL_PORT_VIRTUAL_HANDLE_SIZE >= sizeof(serial_port_virtual_state_t));
|
||||
|
||||
serialPortVirtual = (serial_port_virtual_state_t *)serialHandle;
|
||||
serialPortVirtualConfig = (serial_port_virtual_config_t *)config;
|
||||
|
||||
serialPortVirtual->instance = (uint8_t)serialPortVirtualConfig->controllerIndex;
|
||||
|
||||
if ((status_t)kStatus_Success != USB_DeviceVcomInit((uint8_t)serialPortVirtualConfig->controllerIndex))
|
||||
{
|
||||
return kStatus_SerialManager_Error;
|
||||
}
|
||||
|
||||
ret = Serial_PortVirtualAddItem(&s_serialPortVirtualHead, serialPortVirtual);
|
||||
|
||||
if (kStatus_SerialManager_Success != ret)
|
||||
{
|
||||
return ret;
|
||||
}
|
||||
|
||||
return kStatus_SerialManager_Success;
|
||||
}
|
||||
|
||||
serial_manager_status_t Serial_PortVirtualDeinit(serial_handle_t serialHandle)
|
||||
{
|
||||
serial_port_virtual_state_t *serialPortVirtual;
|
||||
|
||||
assert(serialHandle);
|
||||
|
||||
serialPortVirtual = (serial_port_virtual_state_t *)serialHandle;
|
||||
|
||||
(void)USB_DeviceVcomDeinit(serialPortVirtual->instance);
|
||||
|
||||
(void)Serial_PortVirtualRemoveItem(&s_serialPortVirtualHead, serialPortVirtual);
|
||||
|
||||
return kStatus_SerialManager_Success;
|
||||
}
|
||||
|
||||
serial_manager_status_t Serial_PortVirtualWrite(serial_handle_t serialHandle, uint8_t *buffer, uint32_t length)
|
||||
{
|
||||
serial_port_virtual_state_t *serialPortVirtual;
|
||||
|
||||
assert(serialHandle);
|
||||
|
||||
serialPortVirtual = (serial_port_virtual_state_t *)serialHandle;
|
||||
|
||||
if (kStatus_Success != USB_DeviceVcomWrite(serialPortVirtual->instance, buffer, length))
|
||||
{
|
||||
return kStatus_SerialManager_Error;
|
||||
}
|
||||
|
||||
return kStatus_SerialManager_Success;
|
||||
}
|
||||
|
||||
serial_manager_status_t Serial_PortVirtualRead(serial_handle_t serialHandle, uint8_t *buffer, uint32_t length)
|
||||
{
|
||||
serial_port_virtual_state_t *serialPortVirtual;
|
||||
|
||||
assert(serialHandle);
|
||||
|
||||
serialPortVirtual = (serial_port_virtual_state_t *)serialHandle;
|
||||
|
||||
if (kStatus_Success != USB_DeviceVcomRead(serialPortVirtual->instance, buffer, length))
|
||||
{
|
||||
return kStatus_SerialManager_Error;
|
||||
}
|
||||
|
||||
return kStatus_SerialManager_Success;
|
||||
}
|
||||
|
||||
serial_manager_status_t Serial_PortVirtualCancelWrite(serial_handle_t serialHandle)
|
||||
{
|
||||
serial_port_virtual_state_t *serialPortVirtual;
|
||||
|
||||
assert(serialHandle);
|
||||
|
||||
serialPortVirtual = (serial_port_virtual_state_t *)serialHandle;
|
||||
|
||||
if (kStatus_Success != USB_DeviceVcomCancelWrite(serialPortVirtual->instance))
|
||||
{
|
||||
return kStatus_SerialManager_Error;
|
||||
}
|
||||
|
||||
return kStatus_SerialManager_Success;
|
||||
}
|
||||
|
||||
serial_manager_status_t Serial_PortVirtualInstallTxCallback(serial_handle_t serialHandle,
|
||||
serial_manager_callback_t callback,
|
||||
void *callbackParam)
|
||||
{
|
||||
serial_port_virtual_state_t *serialPortVirtual;
|
||||
|
||||
assert(serialHandle);
|
||||
|
||||
serialPortVirtual = (serial_port_virtual_state_t *)serialHandle;
|
||||
|
||||
serialPortVirtual->tx.callback = callback;
|
||||
serialPortVirtual->tx.callbackParam = callbackParam;
|
||||
|
||||
return (serial_manager_status_t)USB_DeviceVcomInstallTxCallback(serialPortVirtual->instance,
|
||||
Serial_PortVirtualTxCallback, serialHandle);
|
||||
}
|
||||
|
||||
serial_manager_status_t Serial_PortVirtualInstallRxCallback(serial_handle_t serialHandle,
|
||||
serial_manager_callback_t callback,
|
||||
void *callbackParam)
|
||||
{
|
||||
serial_port_virtual_state_t *serialPortVirtual;
|
||||
|
||||
assert(serialHandle);
|
||||
|
||||
serialPortVirtual = (serial_port_virtual_state_t *)serialHandle;
|
||||
|
||||
serialPortVirtual->rx.callback = callback;
|
||||
serialPortVirtual->rx.callbackParam = callbackParam;
|
||||
|
||||
return (serial_manager_status_t)USB_DeviceVcomInstallRxCallback(serialPortVirtual->instance,
|
||||
Serial_PortVirtualRxCallback, serialHandle);
|
||||
}
|
||||
|
||||
void Serial_PortVirtualIsrFunction(serial_handle_t serialHandle)
|
||||
{
|
||||
serial_port_virtual_state_t *serialPortVirtual;
|
||||
|
||||
assert(serialHandle);
|
||||
|
||||
serialPortVirtual = (serial_port_virtual_state_t *)serialHandle;
|
||||
|
||||
USB_DeviceVcomIsrFunction(serialPortVirtual->instance);
|
||||
}
|
||||
|
||||
#endif /* SERIAL_PORT_TYPE_VIRTUAL */
|
@@ -1,57 +1,59 @@
|
||||
/*
|
||||
* Copyright 2019 NXP
|
||||
* All rights reserved.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
#ifndef __SERIAL_PORT_USB_VIRTUAL_H__
|
||||
#define __SERIAL_PORT_USB_VIRTUAL_H__
|
||||
|
||||
/*!
|
||||
* @addtogroup serial_port_usb_virtual
|
||||
* @{
|
||||
*/
|
||||
|
||||
/*******************************************************************************
|
||||
* Definitions
|
||||
******************************************************************************/
|
||||
/*! @brief serial port USB handle size*/
|
||||
#define SERIAL_PORT_USB_VIRTUAL_HANDLE_SIZE (40U)
|
||||
|
||||
/*! @brief USB controller ID */
|
||||
typedef enum _serial_port_usb_cdc_virtual_controller_index
|
||||
{
|
||||
kSerialManager_UsbVirtualControllerKhci0 = 0U, /*!< KHCI 0U */
|
||||
kSerialManager_UsbVirtualControllerKhci1 = 1U, /*!< KHCI 1U, Currently, there are no platforms which have two KHCI
|
||||
IPs, this is reserved to be used in the future. */
|
||||
kSerialManager_UsbVirtualControllerEhci0 = 2U, /*!< EHCI 0U */
|
||||
kSerialManager_UsbVirtualControllerEhci1 = 3U, /*!< EHCI 1U, Currently, there are no platforms which have two EHCI
|
||||
IPs, this is reserved to be used in the future. */
|
||||
|
||||
kSerialManager_UsbVirtualControllerLpcIp3511Fs0 = 4U, /*!< LPC USB IP3511 FS controller 0 */
|
||||
kSerialManager_UsbVirtualControllerLpcIp3511Fs1 =
|
||||
5U, /*!< LPC USB IP3511 FS controller 1, there are no platforms which
|
||||
have two IP3511 IPs, this is reserved to be used in the future. */
|
||||
|
||||
kSerialManager_UsbVirtualControllerLpcIp3511Hs0 = 6U, /*!< LPC USB IP3511 HS controller 0 */
|
||||
kSerialManager_UsbVirtualControllerLpcIp3511Hs1 =
|
||||
7U, /*!< LPC USB IP3511 HS controller 1, there are no platforms which
|
||||
have two IP3511 IPs, this is reserved to be used in the future. */
|
||||
|
||||
kSerialManager_UsbVirtualControllerOhci0 = 8U, /*!< OHCI 0U */
|
||||
kSerialManager_UsbVirtualControllerOhci1 = 9U, /*!< OHCI 1U, Currently, there are no platforms which have two OHCI
|
||||
IPs, this is reserved to be used in the future. */
|
||||
|
||||
kSerialManager_UsbVirtualControllerIp3516Hs0 = 10U, /*!< IP3516HS 0U */
|
||||
kSerialManager_UsbVirtualControllerIp3516Hs1 = 11U, /*!< IP3516HS 1U, Currently, there are no platforms which have
|
||||
two IP3516HS IPs, this is reserved to be used in the future. */
|
||||
} serial_port_usb_cdc_virtual_controller_index_t;
|
||||
|
||||
/*! @brief serial port usb config struct*/
|
||||
typedef struct _serial_port_usb_cdc_virtual_config
|
||||
{
|
||||
serial_port_usb_cdc_virtual_controller_index_t controllerIndex; /*!< controller index */
|
||||
} serial_port_usb_cdc_virtual_config_t;
|
||||
|
||||
#endif /* __SERIAL_PORT_USB_VIRTUAL_H__ */
|
||||
/*
|
||||
* Copyright 2019 - 2020, NXP
|
||||
* All rights reserved.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
#ifndef __SERIAL_PORT_VIRTUAL_H__
|
||||
#define __SERIAL_PORT_VIRTUAL_H__
|
||||
|
||||
/*!
|
||||
* @addtogroup serial_port_virtual
|
||||
* @{
|
||||
*/
|
||||
|
||||
/*******************************************************************************
|
||||
* Definitions
|
||||
******************************************************************************/
|
||||
/*! @brief serial port USB handle size*/
|
||||
#define SERIAL_PORT_VIRTUAL_HANDLE_SIZE (40U)
|
||||
|
||||
/*! @brief USB controller ID */
|
||||
typedef enum _serial_port_virtual_controller_index
|
||||
{
|
||||
kSerialManager_UsbVirtualControllerKhci0 = 0U, /*!< KHCI 0U */
|
||||
kSerialManager_UsbVirtualControllerKhci1 = 1U, /*!< KHCI 1U, Currently, there are no platforms which have two KHCI
|
||||
IPs, this is reserved to be used in the future. */
|
||||
kSerialManager_UsbVirtualControllerEhci0 = 2U, /*!< EHCI 0U */
|
||||
kSerialManager_UsbVirtualControllerEhci1 = 3U, /*!< EHCI 1U, Currently, there are no platforms which have two EHCI
|
||||
IPs, this is reserved to be used in the future. */
|
||||
|
||||
kSerialManager_UsbVirtualControllerLpcIp3511Fs0 = 4U, /*!< LPC USB IP3511 FS controller 0 */
|
||||
kSerialManager_UsbVirtualControllerLpcIp3511Fs1 =
|
||||
5U, /*!< LPC USB IP3511 FS controller 1, there are no platforms which
|
||||
have two IP3511 IPs, this is reserved to be used in the future. */
|
||||
|
||||
kSerialManager_UsbVirtualControllerLpcIp3511Hs0 = 6U, /*!< LPC USB IP3511 HS controller 0 */
|
||||
kSerialManager_UsbVirtualControllerLpcIp3511Hs1 =
|
||||
7U, /*!< LPC USB IP3511 HS controller 1, there are no platforms which
|
||||
have two IP3511 IPs, this is reserved to be used in the future. */
|
||||
|
||||
kSerialManager_UsbVirtualControllerOhci0 = 8U, /*!< OHCI 0U */
|
||||
kSerialManager_UsbVirtualControllerOhci1 = 9U, /*!< OHCI 1U, Currently, there are no platforms which have two OHCI
|
||||
IPs, this is reserved to be used in the future. */
|
||||
|
||||
kSerialManager_UsbVirtualControllerIp3516Hs0 = 10U, /*!< IP3516HS 0U */
|
||||
kSerialManager_UsbVirtualControllerIp3516Hs1 = 11U, /*!< IP3516HS 1U, Currently, there are no platforms which have
|
||||
two IP3516HS IPs, this is reserved to be used in the future. */
|
||||
} serial_port_virtual_controller_index_t;
|
||||
|
||||
/*! @brief serial port usb config struct*/
|
||||
typedef struct _serial_port_virtual_config
|
||||
{
|
||||
serial_port_virtual_controller_index_t controllerIndex; /*!< controller index */
|
||||
} serial_port_virtual_config_t;
|
||||
|
||||
/*@}*/
|
||||
|
||||
#endif /* __SERIAL_PORT_VIRTUAL_H__ */
|
@@ -1,376 +0,0 @@
|
||||
/*
|
||||
* Copyright 2019 NXP
|
||||
* All rights reserved.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
#include "fsl_common.h"
|
||||
#include "serial_manager.h"
|
||||
|
||||
#if (defined(SERIAL_PORT_TYPE_USBCDC_VIRTUAL) && (SERIAL_PORT_TYPE_USBCDC_VIRTUAL > 0U))
|
||||
|
||||
#include "serial_port_usb_virtual.h"
|
||||
|
||||
/*******************************************************************************
|
||||
* Definitions
|
||||
******************************************************************************/
|
||||
|
||||
#if (defined(DEBUG_CONSOLE_ASSERT_DISABLE) && (DEBUG_CONSOLE_ASSERT_DISABLE > 0U))
|
||||
#undef assert
|
||||
#define assert(n)
|
||||
#endif
|
||||
|
||||
/* Weak function. */
|
||||
#if defined(__GNUC__)
|
||||
#define __WEAK_FUNC __attribute__((weak))
|
||||
#elif defined(__ICCARM__)
|
||||
#define __WEAK_FUNC __weak
|
||||
#elif defined(__CC_ARM) || defined(__ARMCC_VERSION)
|
||||
#define __WEAK_FUNC __attribute__((weak))
|
||||
#endif
|
||||
|
||||
typedef struct _serial_usb_cdc_virtual_send_state
|
||||
{
|
||||
serial_manager_callback_t callback;
|
||||
void *callbackParam;
|
||||
} serial_usb_cdc_virtual_send_state_t;
|
||||
|
||||
typedef struct _serial_usb_cdc_virtual_recv_state
|
||||
{
|
||||
serial_manager_callback_t callback;
|
||||
void *callbackParam;
|
||||
} serial_usb_cdc_virtual_recv_state_t;
|
||||
|
||||
/* Define the types for application */
|
||||
typedef struct _serial_usb_cdc_virtual_state
|
||||
{
|
||||
struct _serial_usb_cdc_virtual_state *next; /* Next pointer of the interface */
|
||||
serial_usb_cdc_virtual_send_state_t tx;
|
||||
serial_usb_cdc_virtual_recv_state_t rx;
|
||||
uint8_t instance; /* The instance of the interface */
|
||||
} serial_usb_cdc_virtual_state_t;
|
||||
|
||||
/*******************************************************************************
|
||||
* Prototypes
|
||||
******************************************************************************/
|
||||
|
||||
/*******************************************************************************
|
||||
* Variables
|
||||
******************************************************************************/
|
||||
|
||||
static serial_usb_cdc_virtual_state_t *s_UsbCdcVirtualHead;
|
||||
|
||||
/*******************************************************************************
|
||||
* Code
|
||||
******************************************************************************/
|
||||
|
||||
static serial_manager_status_t Serial_UsbCdcVirtualAddItem(serial_usb_cdc_virtual_state_t **head,
|
||||
serial_usb_cdc_virtual_state_t *node)
|
||||
{
|
||||
serial_usb_cdc_virtual_state_t *p = *head;
|
||||
uint32_t regPrimask;
|
||||
|
||||
regPrimask = DisableGlobalIRQ();
|
||||
|
||||
if (NULL == p)
|
||||
{
|
||||
*head = node;
|
||||
}
|
||||
else
|
||||
{
|
||||
while (p->next)
|
||||
{
|
||||
if (p == node)
|
||||
{
|
||||
EnableGlobalIRQ(regPrimask);
|
||||
return kStatus_SerialManager_Error;
|
||||
}
|
||||
p = p->next;
|
||||
}
|
||||
|
||||
p->next = node;
|
||||
}
|
||||
node->next = NULL;
|
||||
EnableGlobalIRQ(regPrimask);
|
||||
return kStatus_SerialManager_Success;
|
||||
}
|
||||
|
||||
static serial_manager_status_t Serial_UsbCdcVirtualRemoveItem(serial_usb_cdc_virtual_state_t **head,
|
||||
serial_usb_cdc_virtual_state_t *node)
|
||||
{
|
||||
serial_usb_cdc_virtual_state_t *p = *head;
|
||||
serial_usb_cdc_virtual_state_t *q = NULL;
|
||||
uint32_t regPrimask;
|
||||
|
||||
regPrimask = DisableGlobalIRQ();
|
||||
while (p)
|
||||
{
|
||||
if (p == node)
|
||||
{
|
||||
if (NULL == q)
|
||||
{
|
||||
*head = p->next;
|
||||
}
|
||||
else
|
||||
{
|
||||
q->next = p->next;
|
||||
}
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
q = p;
|
||||
p = p->next;
|
||||
}
|
||||
}
|
||||
EnableGlobalIRQ(regPrimask);
|
||||
return kStatus_SerialManager_Success;
|
||||
}
|
||||
|
||||
static serial_manager_status_t Serial_UsbCdcVirtualGetInstance(uint8_t controller,
|
||||
serial_usb_cdc_virtual_state_t **handle)
|
||||
{
|
||||
serial_usb_cdc_virtual_state_t *p = s_UsbCdcVirtualHead;
|
||||
uint32_t regPrimask;
|
||||
|
||||
*handle = NULL;
|
||||
regPrimask = DisableGlobalIRQ();
|
||||
while (p)
|
||||
{
|
||||
if (p->instance == controller)
|
||||
{
|
||||
*handle = p;
|
||||
break;
|
||||
}
|
||||
}
|
||||
EnableGlobalIRQ(regPrimask);
|
||||
return (p) ? kStatus_SerialManager_Success : kStatus_SerialManager_Error;
|
||||
}
|
||||
|
||||
static void Serial_UsbCdcVirtualTxCallback(void *callbackParam,
|
||||
serial_manager_callback_message_t *message,
|
||||
serial_manager_status_t status)
|
||||
{
|
||||
serial_usb_cdc_virtual_state_t *serialUsbCdcVirtual;
|
||||
|
||||
if (callbackParam)
|
||||
{
|
||||
serialUsbCdcVirtual = (serial_usb_cdc_virtual_state_t *)callbackParam;
|
||||
if (serialUsbCdcVirtual->tx.callback)
|
||||
{
|
||||
serialUsbCdcVirtual->tx.callback(serialUsbCdcVirtual->tx.callbackParam, message, status);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void Serial_UsbCdcVirtualRxCallback(void *callbackParam,
|
||||
serial_manager_callback_message_t *message,
|
||||
serial_manager_status_t status)
|
||||
{
|
||||
serial_usb_cdc_virtual_state_t *serialUsbCdcVirtual;
|
||||
|
||||
if (callbackParam)
|
||||
{
|
||||
serialUsbCdcVirtual = (serial_usb_cdc_virtual_state_t *)callbackParam;
|
||||
if (serialUsbCdcVirtual->rx.callback)
|
||||
{
|
||||
serialUsbCdcVirtual->rx.callback(serialUsbCdcVirtual->rx.callbackParam, message, status);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
__WEAK_FUNC status_t USB_DeviceVcomInit(uint8_t controller)
|
||||
{
|
||||
return kStatus_Success;
|
||||
}
|
||||
|
||||
__WEAK_FUNC status_t USB_DeviceVcomDeinit(uint8_t controller)
|
||||
{
|
||||
return kStatus_Success;
|
||||
}
|
||||
|
||||
__WEAK_FUNC status_t USB_DeviceVcomWrite(uint8_t controller, uint8_t *buffer, uint32_t length)
|
||||
{
|
||||
serial_usb_cdc_virtual_state_t *serialUsbCdcVirtual;
|
||||
serial_manager_callback_message_t msg;
|
||||
|
||||
if (kStatus_SerialManager_Success != Serial_UsbCdcVirtualGetInstance(controller, &serialUsbCdcVirtual))
|
||||
{
|
||||
return kStatus_Fail;
|
||||
}
|
||||
|
||||
if ((NULL != serialUsbCdcVirtual->tx.callback))
|
||||
{
|
||||
msg.buffer = buffer;
|
||||
msg.length = length;
|
||||
serialUsbCdcVirtual->tx.callback(serialUsbCdcVirtual->tx.callbackParam, &msg, kStatus_SerialManager_Success);
|
||||
}
|
||||
return kStatus_Success;
|
||||
}
|
||||
|
||||
__WEAK_FUNC status_t USB_DeviceVcomRead(uint8_t controller, uint8_t *buffer, uint32_t length)
|
||||
{
|
||||
return kStatus_Fail;
|
||||
}
|
||||
|
||||
__WEAK_FUNC status_t USB_DeviceVcomCancelWrite(uint8_t controller)
|
||||
{
|
||||
return kStatus_Success;
|
||||
}
|
||||
|
||||
__WEAK_FUNC status_t USB_DeviceVcomInstallTxCallback(uint8_t controller,
|
||||
serial_manager_callback_t callback,
|
||||
void *callbackParam)
|
||||
{
|
||||
return kStatus_Success;
|
||||
}
|
||||
|
||||
__WEAK_FUNC status_t USB_DeviceVcomInstallRxCallback(uint8_t controller,
|
||||
serial_manager_callback_t callback,
|
||||
void *callbackParam)
|
||||
{
|
||||
return kStatus_Success;
|
||||
}
|
||||
|
||||
__WEAK_FUNC void USB_DeviceVcomIsrFunction(uint8_t controller)
|
||||
{
|
||||
}
|
||||
|
||||
serial_manager_status_t Serial_UsbCdcVirtualInit(serial_handle_t serialHandle, void *config)
|
||||
{
|
||||
serial_usb_cdc_virtual_state_t *serialUsbCdcVirtual;
|
||||
serial_port_usb_cdc_virtual_config_t *usbCdcVirtualConfig;
|
||||
serial_manager_status_t ret;
|
||||
|
||||
assert(config && serialHandle && (SERIAL_PORT_USB_VIRTUAL_HANDLE_SIZE >= sizeof(serial_usb_cdc_virtual_state_t)));
|
||||
|
||||
serialUsbCdcVirtual = (serial_usb_cdc_virtual_state_t *)serialHandle;
|
||||
usbCdcVirtualConfig = (serial_port_usb_cdc_virtual_config_t *)config;
|
||||
|
||||
serialUsbCdcVirtual->instance = usbCdcVirtualConfig->controllerIndex;
|
||||
|
||||
if (kStatus_Success != USB_DeviceVcomInit(usbCdcVirtualConfig->controllerIndex))
|
||||
{
|
||||
return kStatus_SerialManager_Error;
|
||||
}
|
||||
|
||||
ret = Serial_UsbCdcVirtualAddItem(&s_UsbCdcVirtualHead, serialUsbCdcVirtual);
|
||||
|
||||
if (kStatus_SerialManager_Success != ret)
|
||||
{
|
||||
return ret;
|
||||
}
|
||||
|
||||
return kStatus_SerialManager_Success;
|
||||
}
|
||||
|
||||
serial_manager_status_t Serial_UsbCdcVirtualDeinit(serial_handle_t serialHandle)
|
||||
{
|
||||
serial_usb_cdc_virtual_state_t *serialUsbCdcVirtual;
|
||||
|
||||
assert(serialHandle);
|
||||
|
||||
serialUsbCdcVirtual = (serial_usb_cdc_virtual_state_t *)serialHandle;
|
||||
|
||||
USB_DeviceVcomDeinit(serialUsbCdcVirtual->instance);
|
||||
|
||||
Serial_UsbCdcVirtualRemoveItem(&s_UsbCdcVirtualHead, serialUsbCdcVirtual);
|
||||
|
||||
return kStatus_SerialManager_Success;
|
||||
}
|
||||
|
||||
serial_manager_status_t Serial_UsbCdcVirtualWrite(serial_handle_t serialHandle, uint8_t *buffer, uint32_t length)
|
||||
{
|
||||
serial_usb_cdc_virtual_state_t *serialUsbCdcVirtual;
|
||||
|
||||
assert(serialHandle);
|
||||
|
||||
serialUsbCdcVirtual = (serial_usb_cdc_virtual_state_t *)serialHandle;
|
||||
|
||||
if (kStatus_Success != USB_DeviceVcomWrite(serialUsbCdcVirtual->instance, buffer, length))
|
||||
{
|
||||
return kStatus_SerialManager_Error;
|
||||
}
|
||||
|
||||
return kStatus_SerialManager_Success;
|
||||
}
|
||||
|
||||
serial_manager_status_t Serial_UsbCdcVirtualRead(serial_handle_t serialHandle, uint8_t *buffer, uint32_t length)
|
||||
{
|
||||
serial_usb_cdc_virtual_state_t *serialUsbCdcVirtual;
|
||||
|
||||
assert(serialHandle);
|
||||
|
||||
serialUsbCdcVirtual = (serial_usb_cdc_virtual_state_t *)serialHandle;
|
||||
|
||||
if (kStatus_Success != USB_DeviceVcomRead(serialUsbCdcVirtual->instance, buffer, length))
|
||||
{
|
||||
return kStatus_SerialManager_Error;
|
||||
}
|
||||
|
||||
return kStatus_SerialManager_Success;
|
||||
}
|
||||
|
||||
serial_manager_status_t Serial_UsbCdcVirtualCancelWrite(serial_handle_t serialHandle)
|
||||
{
|
||||
serial_usb_cdc_virtual_state_t *serialUsbCdcVirtual;
|
||||
|
||||
assert(serialHandle);
|
||||
|
||||
serialUsbCdcVirtual = (serial_usb_cdc_virtual_state_t *)serialHandle;
|
||||
|
||||
if (kStatus_Success != USB_DeviceVcomCancelWrite(serialUsbCdcVirtual->instance))
|
||||
{
|
||||
return kStatus_SerialManager_Error;
|
||||
}
|
||||
|
||||
return kStatus_SerialManager_Success;
|
||||
}
|
||||
|
||||
serial_manager_status_t Serial_UsbCdcVirtualInstallTxCallback(serial_handle_t serialHandle,
|
||||
serial_manager_callback_t callback,
|
||||
void *callbackParam)
|
||||
{
|
||||
serial_usb_cdc_virtual_state_t *serialUsbCdcVirtual;
|
||||
|
||||
assert(serialHandle);
|
||||
|
||||
serialUsbCdcVirtual = (serial_usb_cdc_virtual_state_t *)serialHandle;
|
||||
|
||||
serialUsbCdcVirtual->tx.callback = callback;
|
||||
serialUsbCdcVirtual->tx.callbackParam = callbackParam;
|
||||
|
||||
return (serial_manager_status_t)USB_DeviceVcomInstallTxCallback(serialUsbCdcVirtual->instance,
|
||||
Serial_UsbCdcVirtualTxCallback, serialHandle);
|
||||
}
|
||||
|
||||
serial_manager_status_t Serial_UsbCdcVirtualInstallRxCallback(serial_handle_t serialHandle,
|
||||
serial_manager_callback_t callback,
|
||||
void *callbackParam)
|
||||
{
|
||||
serial_usb_cdc_virtual_state_t *serialUsbCdcVirtual;
|
||||
|
||||
assert(serialHandle);
|
||||
|
||||
serialUsbCdcVirtual = (serial_usb_cdc_virtual_state_t *)serialHandle;
|
||||
|
||||
serialUsbCdcVirtual->rx.callback = callback;
|
||||
serialUsbCdcVirtual->rx.callbackParam = callbackParam;
|
||||
|
||||
return (serial_manager_status_t)USB_DeviceVcomInstallRxCallback(serialUsbCdcVirtual->instance,
|
||||
Serial_UsbCdcVirtualRxCallback, serialHandle);
|
||||
}
|
||||
|
||||
void Serial_UsbCdcVirtualIsrFunction(serial_handle_t serialHandle)
|
||||
{
|
||||
serial_usb_cdc_virtual_state_t *serialUsbCdcVirtual;
|
||||
|
||||
assert(serialHandle);
|
||||
|
||||
serialUsbCdcVirtual = (serial_usb_cdc_virtual_state_t *)serialHandle;
|
||||
|
||||
USB_DeviceVcomIsrFunction(serialUsbCdcVirtual->instance);
|
||||
}
|
||||
|
||||
#endif /* SERIAL_PORT_TYPE_USBCDC_VIRTUAL */
|
@@ -1,103 +1,103 @@
|
||||
;
|
||||
; NXP Communication Device Class driver installation file
|
||||
; Copyright 2016 NXP
|
||||
;
|
||||
|
||||
[Version]
|
||||
Signature="$Windows NT$"
|
||||
Class=Ports
|
||||
ClassGuid={4D36E978-E325-11CE-BFC1-08002BE10318}
|
||||
Provider=%MFGNAME%
|
||||
CatalogFile=%MFGFILENAME%.cat
|
||||
DriverVer=02/16/2011,1.0
|
||||
|
||||
[Manufacturer]
|
||||
%MFGNAME%=DeviceList, NTamd64
|
||||
|
||||
[DestinationDirs]
|
||||
DefaultDestDir=12
|
||||
|
||||
|
||||
;------------------------------------------------------------------------------
|
||||
; Windows 2000/XP/Vista-32bit Sections
|
||||
;------------------------------------------------------------------------------
|
||||
|
||||
[DriverInstall.nt]
|
||||
include=mdmcpq.inf
|
||||
AddReg=DriverInstall.nt.AddReg
|
||||
|
||||
[DriverInstall.nt.AddReg]
|
||||
HKR,,DevLoader,,*ntkern
|
||||
HKR,,NTMPDriver,,%DRIVERFILENAME%.sys
|
||||
HKR,,EnumPropPages32,,"MsPorts.dll,SerialPortPropPageProvider"
|
||||
|
||||
[DriverInstall.nt.Services]
|
||||
AddService=usbser, 0x00000002, DriverService.nt
|
||||
|
||||
[DriverService.nt]
|
||||
DisplayName=%SERVICE%
|
||||
ServiceType=1
|
||||
StartType=3
|
||||
ErrorControl=1
|
||||
ServiceBinary=%12%\%DRIVERFILENAME%.sys
|
||||
|
||||
;------------------------------------------------------------------------------
|
||||
; Vista-64bit Sections
|
||||
;------------------------------------------------------------------------------
|
||||
|
||||
[DriverInstall.NTamd64]
|
||||
include=mdmcpq.inf
|
||||
AddReg=DriverInstall.NTamd64.AddReg
|
||||
|
||||
[DriverInstall.NTamd64.AddReg]
|
||||
HKR,,DevLoader,,*ntkern
|
||||
HKR,,NTMPDriver,,%DRIVERFILENAME%.sys
|
||||
HKR,,EnumPropPages32,,"MsPorts.dll,SerialPortPropPageProvider"
|
||||
|
||||
[DriverInstall.NTamd64.Services]
|
||||
AddService=usbser, 0x00000002, DriverService.NTamd64
|
||||
|
||||
[DriverService.NTamd64]
|
||||
DisplayName=%SERVICE%
|
||||
ServiceType=1
|
||||
StartType=3
|
||||
ErrorControl=1
|
||||
ServiceBinary=%12%\%DRIVERFILENAME%.sys
|
||||
|
||||
|
||||
;------------------------------------------------------------------------------
|
||||
; Vendor and Product ID Definitions
|
||||
;------------------------------------------------------------------------------
|
||||
; When developing your USB device, the VID and PID used in the PC side
|
||||
; application program and the firmware on the microcontroller must match.
|
||||
; Modify the below line to use your VID and PID. Use the format as shown below.
|
||||
; Note: One INF file can be used for multiple devices with different VID and PIDs.
|
||||
; For each supported device, append ",USB\VID_xxxx&PID_yyyy" to the end of the line.
|
||||
;------------------------------------------------------------------------------
|
||||
[SourceDisksFiles]
|
||||
[SourceDisksNames]
|
||||
[DeviceList]
|
||||
%DESCRIPTION%=DriverInstall, USB\VID_1FC9&PID_0094
|
||||
%DESCRIPTION%=DriverInstall, USB\VID_1FC9&PID_009E&MI_00
|
||||
%DESCRIPTION%=DriverInstall, USB\VID_1FC9&PID_009F&MI_00
|
||||
%DESCRIPTION%=DriverInstall, USB\VID_1FC9&PID_00A3&MI_00
|
||||
|
||||
[DeviceList.NTamd64]
|
||||
%DESCRIPTION% = DriverInstall, USB\VID_1FC9&PID_0094
|
||||
%DESCRIPTION% = DriverInstall, USB\VID_1FC9&PID_009E&MI_00
|
||||
%DESCRIPTION% = DriverInstall, USB\VID_1FC9&PID_009F&MI_00
|
||||
%DESCRIPTION% = DriverInstall, USB\VID_1FC9&PID_00A3&MI_00
|
||||
|
||||
;------------------------------------------------------------------------------
|
||||
; String Definitions
|
||||
;------------------------------------------------------------------------------
|
||||
;Modify these strings to customize your device
|
||||
;------------------------------------------------------------------------------
|
||||
[Strings]
|
||||
MFGFILENAME="CDC"
|
||||
DRIVERFILENAME ="usbser"
|
||||
MFGNAME="NXP"
|
||||
INSTDISK="NXP CDC Driver Installer"
|
||||
DESCRIPTION="Virtual Com Port"
|
||||
SERVICE="NXP Virtual COM Driver"
|
||||
|
||||
;
|
||||
; NXP Communication Device Class driver installation file
|
||||
; Copyright 2016 NXP
|
||||
;
|
||||
|
||||
[Version]
|
||||
Signature="$Windows NT$"
|
||||
Class=Ports
|
||||
ClassGuid={4D36E978-E325-11CE-BFC1-08002BE10318}
|
||||
Provider=%MFGNAME%
|
||||
CatalogFile=%MFGFILENAME%.cat
|
||||
DriverVer=02/16/2011,1.0
|
||||
|
||||
[Manufacturer]
|
||||
%MFGNAME%=DeviceList, NTamd64
|
||||
|
||||
[DestinationDirs]
|
||||
DefaultDestDir=12
|
||||
|
||||
|
||||
;------------------------------------------------------------------------------
|
||||
; Windows 2000/XP/Vista-32bit Sections
|
||||
;------------------------------------------------------------------------------
|
||||
|
||||
[DriverInstall.nt]
|
||||
include=mdmcpq.inf
|
||||
AddReg=DriverInstall.nt.AddReg
|
||||
|
||||
[DriverInstall.nt.AddReg]
|
||||
HKR,,DevLoader,,*ntkern
|
||||
HKR,,NTMPDriver,,%DRIVERFILENAME%.sys
|
||||
HKR,,EnumPropPages32,,"MsPorts.dll,SerialPortPropPageProvider"
|
||||
|
||||
[DriverInstall.nt.Services]
|
||||
AddService=usbser, 0x00000002, DriverService.nt
|
||||
|
||||
[DriverService.nt]
|
||||
DisplayName=%SERVICE%
|
||||
ServiceType=1
|
||||
StartType=3
|
||||
ErrorControl=1
|
||||
ServiceBinary=%12%\%DRIVERFILENAME%.sys
|
||||
|
||||
;------------------------------------------------------------------------------
|
||||
; Vista-64bit Sections
|
||||
;------------------------------------------------------------------------------
|
||||
|
||||
[DriverInstall.NTamd64]
|
||||
include=mdmcpq.inf
|
||||
AddReg=DriverInstall.NTamd64.AddReg
|
||||
|
||||
[DriverInstall.NTamd64.AddReg]
|
||||
HKR,,DevLoader,,*ntkern
|
||||
HKR,,NTMPDriver,,%DRIVERFILENAME%.sys
|
||||
HKR,,EnumPropPages32,,"MsPorts.dll,SerialPortPropPageProvider"
|
||||
|
||||
[DriverInstall.NTamd64.Services]
|
||||
AddService=usbser, 0x00000002, DriverService.NTamd64
|
||||
|
||||
[DriverService.NTamd64]
|
||||
DisplayName=%SERVICE%
|
||||
ServiceType=1
|
||||
StartType=3
|
||||
ErrorControl=1
|
||||
ServiceBinary=%12%\%DRIVERFILENAME%.sys
|
||||
|
||||
|
||||
;------------------------------------------------------------------------------
|
||||
; Vendor and Product ID Definitions
|
||||
;------------------------------------------------------------------------------
|
||||
; When developing your USB device, the VID and PID used in the PC side
|
||||
; application program and the firmware on the microcontroller must match.
|
||||
; Modify the below line to use your VID and PID. Use the format as shown below.
|
||||
; Note: One INF file can be used for multiple devices with different VID and PIDs.
|
||||
; For each supported device, append ",USB\VID_xxxx&PID_yyyy" to the end of the line.
|
||||
;------------------------------------------------------------------------------
|
||||
[SourceDisksFiles]
|
||||
[SourceDisksNames]
|
||||
[DeviceList]
|
||||
%DESCRIPTION%=DriverInstall, USB\VID_1FC9&PID_0094
|
||||
%DESCRIPTION%=DriverInstall, USB\VID_1FC9&PID_009E&MI_00
|
||||
%DESCRIPTION%=DriverInstall, USB\VID_1FC9&PID_009F&MI_00
|
||||
%DESCRIPTION%=DriverInstall, USB\VID_1FC9&PID_00A3&MI_00
|
||||
|
||||
[DeviceList.NTamd64]
|
||||
%DESCRIPTION% = DriverInstall, USB\VID_1FC9&PID_0094
|
||||
%DESCRIPTION% = DriverInstall, USB\VID_1FC9&PID_009E&MI_00
|
||||
%DESCRIPTION% = DriverInstall, USB\VID_1FC9&PID_009F&MI_00
|
||||
%DESCRIPTION% = DriverInstall, USB\VID_1FC9&PID_00A3&MI_00
|
||||
|
||||
;------------------------------------------------------------------------------
|
||||
; String Definitions
|
||||
;------------------------------------------------------------------------------
|
||||
;Modify these strings to customize your device
|
||||
;------------------------------------------------------------------------------
|
||||
[Strings]
|
||||
MFGFILENAME="CDC"
|
||||
DRIVERFILENAME ="usbser"
|
||||
MFGNAME="NXP"
|
||||
INSTDISK="NXP CDC Driver Installer"
|
||||
DESCRIPTION="Virtual Com Port"
|
||||
SERVICE="NXP Virtual COM Driver"
|
||||
|
||||
|
@@ -1,876 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2015, Freescale Semiconductor, Inc.
|
||||
* Copyright 2016, 2019 NXP
|
||||
* All rights reserved.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "usb_device_config.h"
|
||||
#include "usb.h"
|
||||
#include "usb_device.h"
|
||||
|
||||
#include "usb_device_class.h"
|
||||
|
||||
#if USB_DEVICE_CONFIG_CDC_ACM
|
||||
#include "usb_device_cdc_acm.h"
|
||||
|
||||
/*******************************************************************************
|
||||
* Definitions
|
||||
******************************************************************************/
|
||||
#define USB_CDC_ACM_ENTER_CRITICAL() \
|
||||
OSA_SR_ALLOC(); \
|
||||
OSA_ENTER_CRITICAL()
|
||||
|
||||
#define USB_CDC_ACM_EXIT_CRITICAL() OSA_EXIT_CRITICAL()
|
||||
|
||||
/*******************************************************************************
|
||||
* Variables
|
||||
******************************************************************************/
|
||||
/* CDC ACM device instance */
|
||||
|
||||
USB_GLOBAL USB_RAM_ADDRESS_ALIGNMENT(USB_DATA_ALIGN_SIZE)
|
||||
usb_device_cdc_acm_struct_t g_cdcAcmHandle[USB_DEVICE_CONFIG_CDC_ACM];
|
||||
|
||||
/*******************************************************************************
|
||||
* Code
|
||||
******************************************************************************/
|
||||
|
||||
/*!
|
||||
* @brief Allocates the CDC ACM device handle.
|
||||
*
|
||||
* This function allocates the CDC ACM device handle.
|
||||
*
|
||||
* @param handle The class handle of the CDC ACM class.
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
*/
|
||||
static usb_status_t USB_DeviceCdcAcmAllocateHandle(usb_device_cdc_acm_struct_t **handle)
|
||||
{
|
||||
uint32_t count;
|
||||
for (count = 0; count < USB_DEVICE_CONFIG_CDC_ACM; count++)
|
||||
{
|
||||
if (NULL == g_cdcAcmHandle[count].handle)
|
||||
{
|
||||
*handle = &g_cdcAcmHandle[count];
|
||||
return kStatus_USB_Success;
|
||||
}
|
||||
}
|
||||
|
||||
return kStatus_USB_Busy;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Frees the CDC ACM device handle.
|
||||
*
|
||||
* This function frees the CDC ACM device handle.
|
||||
*
|
||||
* @param handle The class handle of the CDC ACM class.
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
*/
|
||||
static usb_status_t USB_DeviceCdcAcmFreeHandle(usb_device_cdc_acm_struct_t *handle)
|
||||
{
|
||||
handle->handle = NULL;
|
||||
handle->configStruct = NULL;
|
||||
handle->configuration = 0;
|
||||
handle->alternate = 0;
|
||||
return kStatus_USB_Success;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Responds to the interrupt in endpoint event.
|
||||
*
|
||||
* This function responds to the interrupt in endpoint event.
|
||||
*
|
||||
* @param handle The device handle of the CDC ACM device.
|
||||
* @param message The pointer to the message of the endpoint callback.
|
||||
* @param callbackParam The pointer to the parameter of the callback.
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
*/
|
||||
usb_status_t USB_DeviceCdcAcmInterruptIn(usb_device_handle handle,
|
||||
usb_device_endpoint_callback_message_struct_t *message,
|
||||
void *callbackParam)
|
||||
{
|
||||
usb_device_cdc_acm_struct_t *cdcAcmHandle;
|
||||
usb_status_t error = kStatus_USB_Error;
|
||||
cdcAcmHandle = (usb_device_cdc_acm_struct_t *)callbackParam;
|
||||
if (!cdcAcmHandle)
|
||||
{
|
||||
return kStatus_USB_InvalidHandle;
|
||||
}
|
||||
|
||||
cdcAcmHandle->interruptIn.isBusy = 0;
|
||||
|
||||
if ((NULL != cdcAcmHandle->configStruct) && (cdcAcmHandle->configStruct->classCallback))
|
||||
{
|
||||
/*classCallback is initialized in classInit of s_UsbDeviceClassInterfaceMap,
|
||||
it is from the second parameter of classInit */
|
||||
error = cdcAcmHandle->configStruct->classCallback((class_handle_t)cdcAcmHandle,
|
||||
kUSB_DeviceCdcEventSerialStateNotif, message);
|
||||
}
|
||||
return error;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Responds to the bulk in endpoint event.
|
||||
*
|
||||
* This function responds to the bulk in endpoint event.
|
||||
*
|
||||
* @param handle The device handle of the CDC ACM device.
|
||||
* @param message The pointer to the message of the endpoint callback.
|
||||
* @param callbackParam The pointer to the parameter of the callback.
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
*/
|
||||
usb_status_t USB_DeviceCdcAcmBulkIn(usb_device_handle handle,
|
||||
usb_device_endpoint_callback_message_struct_t *message,
|
||||
void *callbackParam)
|
||||
{
|
||||
usb_device_cdc_acm_struct_t *cdcAcmHandle;
|
||||
usb_status_t error = kStatus_USB_Error;
|
||||
cdcAcmHandle = (usb_device_cdc_acm_struct_t *)callbackParam;
|
||||
|
||||
if (!cdcAcmHandle)
|
||||
{
|
||||
return kStatus_USB_InvalidHandle;
|
||||
}
|
||||
|
||||
cdcAcmHandle->bulkIn.isBusy = 0;
|
||||
|
||||
if ((NULL != cdcAcmHandle->configStruct) && (cdcAcmHandle->configStruct->classCallback))
|
||||
{
|
||||
/*classCallback is initialized in classInit of s_UsbDeviceClassInterfaceMap,
|
||||
it is from the second parameter of classInit */
|
||||
error = cdcAcmHandle->configStruct->classCallback((class_handle_t)cdcAcmHandle, kUSB_DeviceCdcEventSendResponse,
|
||||
message);
|
||||
}
|
||||
return error;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Responds to the bulk out endpoint event.
|
||||
*
|
||||
* This function responds to the bulk out endpoint event.
|
||||
*
|
||||
* @param handle The device handle of the CDC ACM device.
|
||||
* @param message The pointer to the message of the endpoint callback.
|
||||
* @param callbackParam The pointer to the parameter of the callback.
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
*/
|
||||
usb_status_t USB_DeviceCdcAcmBulkOut(usb_device_handle handle,
|
||||
usb_device_endpoint_callback_message_struct_t *message,
|
||||
void *callbackParam)
|
||||
{
|
||||
usb_device_cdc_acm_struct_t *cdcAcmHandle;
|
||||
usb_status_t error = kStatus_USB_Error;
|
||||
cdcAcmHandle = (usb_device_cdc_acm_struct_t *)callbackParam;
|
||||
|
||||
if (!cdcAcmHandle)
|
||||
{
|
||||
return kStatus_USB_InvalidHandle;
|
||||
}
|
||||
|
||||
cdcAcmHandle->bulkOut.isBusy = 0;
|
||||
|
||||
if ((NULL != cdcAcmHandle->configStruct) && (cdcAcmHandle->configStruct->classCallback))
|
||||
{
|
||||
/*classCallback is initialized in classInit of s_UsbDeviceClassInterfaceMap,
|
||||
it is from the second parameter of classInit */
|
||||
error = cdcAcmHandle->configStruct->classCallback((class_handle_t)cdcAcmHandle, kUSB_DeviceCdcEventRecvResponse,
|
||||
message);
|
||||
}
|
||||
return error;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Initializes the endpoints in CDC ACM class.
|
||||
*
|
||||
* This function initializes the endpoints in CDC ACM class.
|
||||
*
|
||||
* @param cdcAcmHandle The class handle of the CDC ACM class.
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
*/
|
||||
usb_status_t USB_DeviceCdcAcmEndpointsInit(usb_device_cdc_acm_struct_t *cdcAcmHandle)
|
||||
{
|
||||
usb_device_interface_list_t *interfaceList;
|
||||
usb_device_interface_struct_t *interface = NULL;
|
||||
usb_status_t error = kStatus_USB_Error;
|
||||
uint32_t count;
|
||||
uint32_t index;
|
||||
|
||||
if (!cdcAcmHandle)
|
||||
{
|
||||
return error;
|
||||
}
|
||||
|
||||
/* return error when configuration is invalid (0 or more than the configuration number) */
|
||||
if ((cdcAcmHandle->configuration == 0U) ||
|
||||
(cdcAcmHandle->configuration > cdcAcmHandle->configStruct->classInfomation->configurations))
|
||||
{
|
||||
return error;
|
||||
}
|
||||
|
||||
interfaceList = &cdcAcmHandle->configStruct->classInfomation->interfaceList[cdcAcmHandle->configuration - 1];
|
||||
|
||||
for (count = 0; count < interfaceList->count; count++)
|
||||
{
|
||||
if (USB_DEVICE_CONFIG_CDC_COMM_CLASS_CODE == interfaceList->interfaces[count].classCode)
|
||||
{
|
||||
for (index = 0; index < interfaceList->interfaces[count].count; index++)
|
||||
{
|
||||
if (interfaceList->interfaces[count].interface[index].alternateSetting == cdcAcmHandle->alternate)
|
||||
{
|
||||
interface = &interfaceList->interfaces[count].interface[index];
|
||||
break;
|
||||
}
|
||||
}
|
||||
cdcAcmHandle->interfaceNumber = interfaceList->interfaces[count].interfaceNumber;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!interface)
|
||||
{
|
||||
return error;
|
||||
}
|
||||
cdcAcmHandle->commInterfaceHandle = interface;
|
||||
for (count = 0; count < interface->endpointList.count; count++)
|
||||
{
|
||||
usb_device_endpoint_init_struct_t epInitStruct;
|
||||
usb_device_endpoint_callback_struct_t epCallback;
|
||||
epInitStruct.zlt = 0;
|
||||
epInitStruct.interval = interface->endpointList.endpoint[count].interval;
|
||||
epInitStruct.endpointAddress = interface->endpointList.endpoint[count].endpointAddress;
|
||||
epInitStruct.maxPacketSize = interface->endpointList.endpoint[count].maxPacketSize;
|
||||
epInitStruct.transferType = interface->endpointList.endpoint[count].transferType;
|
||||
|
||||
if ((USB_IN == ((epInitStruct.endpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) >>
|
||||
USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT)) &&
|
||||
(USB_ENDPOINT_INTERRUPT == epInitStruct.transferType))
|
||||
{
|
||||
cdcAcmHandle->interruptIn.ep = (epInitStruct.endpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_NUMBER_MASK);
|
||||
cdcAcmHandle->interruptIn.isBusy = 0;
|
||||
cdcAcmHandle->interruptIn.pipeDataBuffer = (uint8_t *)USB_UNINITIALIZED_VAL_32;
|
||||
cdcAcmHandle->interruptIn.pipeStall = 0U;
|
||||
cdcAcmHandle->interruptIn.pipeDataLen = 0U;
|
||||
epCallback.callbackFn = USB_DeviceCdcAcmInterruptIn;
|
||||
}
|
||||
|
||||
epCallback.callbackParam = cdcAcmHandle;
|
||||
|
||||
error = USB_DeviceInitEndpoint(cdcAcmHandle->handle, &epInitStruct, &epCallback);
|
||||
if (kStatus_USB_Success != error)
|
||||
{
|
||||
return error;
|
||||
}
|
||||
}
|
||||
|
||||
for (count = 0; count < interfaceList->count; count++)
|
||||
{
|
||||
if (USB_DEVICE_CONFIG_CDC_DATA_CLASS_CODE == interfaceList->interfaces[count].classCode)
|
||||
{
|
||||
for (index = 0; index < interfaceList->interfaces[count].count; index++)
|
||||
{
|
||||
if (interfaceList->interfaces[count].interface[index].alternateSetting == cdcAcmHandle->alternate)
|
||||
{
|
||||
interface = &interfaceList->interfaces[count].interface[index];
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
cdcAcmHandle->dataInterfaceHandle = interface;
|
||||
|
||||
for (count = 0; count < interface->endpointList.count; count++)
|
||||
{
|
||||
usb_device_endpoint_init_struct_t epInitStruct;
|
||||
usb_device_endpoint_callback_struct_t epCallback;
|
||||
epInitStruct.zlt = 0;
|
||||
epInitStruct.interval = interface->endpointList.endpoint[count].interval;
|
||||
epInitStruct.endpointAddress = interface->endpointList.endpoint[count].endpointAddress;
|
||||
epInitStruct.maxPacketSize = interface->endpointList.endpoint[count].maxPacketSize;
|
||||
epInitStruct.transferType = interface->endpointList.endpoint[count].transferType;
|
||||
|
||||
if ((USB_IN == ((epInitStruct.endpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) >>
|
||||
USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT)) &&
|
||||
(USB_ENDPOINT_BULK == epInitStruct.transferType))
|
||||
{
|
||||
cdcAcmHandle->bulkIn.ep = (epInitStruct.endpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_NUMBER_MASK);
|
||||
cdcAcmHandle->bulkIn.isBusy = 0;
|
||||
cdcAcmHandle->bulkIn.pipeDataBuffer = (uint8_t *)USB_UNINITIALIZED_VAL_32;
|
||||
cdcAcmHandle->bulkIn.pipeStall = 0U;
|
||||
cdcAcmHandle->bulkIn.pipeDataLen = 0U;
|
||||
epCallback.callbackFn = USB_DeviceCdcAcmBulkIn;
|
||||
}
|
||||
else if ((USB_OUT == ((epInitStruct.endpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) >>
|
||||
USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT)) &&
|
||||
(USB_ENDPOINT_BULK == epInitStruct.transferType))
|
||||
{
|
||||
cdcAcmHandle->bulkOut.ep = (epInitStruct.endpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_NUMBER_MASK);
|
||||
cdcAcmHandle->bulkOut.isBusy = 0;
|
||||
cdcAcmHandle->bulkOut.pipeDataBuffer = (uint8_t *)USB_UNINITIALIZED_VAL_32;
|
||||
cdcAcmHandle->bulkOut.pipeStall = 0U;
|
||||
cdcAcmHandle->bulkOut.pipeDataLen = 0U;
|
||||
epCallback.callbackFn = USB_DeviceCdcAcmBulkOut;
|
||||
}
|
||||
else
|
||||
{
|
||||
}
|
||||
epCallback.callbackParam = cdcAcmHandle;
|
||||
|
||||
error = USB_DeviceInitEndpoint(cdcAcmHandle->handle, &epInitStruct, &epCallback);
|
||||
}
|
||||
return error;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief De-initializes the endpoints in CDC ACM class.
|
||||
*
|
||||
* This function de-initializes the endpoints in CDC ACM class.
|
||||
*
|
||||
* @param cdcAcmHandle The class handle of the CDC ACM class.
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
*/
|
||||
usb_status_t USB_DeviceCdcAcmEndpointsDeinit(usb_device_cdc_acm_struct_t *cdcAcmHandle)
|
||||
{
|
||||
usb_status_t error = kStatus_USB_Error;
|
||||
uint32_t count;
|
||||
|
||||
if ((!cdcAcmHandle->commInterfaceHandle) || (!cdcAcmHandle->dataInterfaceHandle))
|
||||
{
|
||||
return error;
|
||||
}
|
||||
for (count = 0; count < cdcAcmHandle->commInterfaceHandle->endpointList.count; count++)
|
||||
{
|
||||
error = USB_DeviceDeinitEndpoint(
|
||||
cdcAcmHandle->handle, cdcAcmHandle->commInterfaceHandle->endpointList.endpoint[count].endpointAddress);
|
||||
}
|
||||
for (count = 0; count < cdcAcmHandle->dataInterfaceHandle->endpointList.count; count++)
|
||||
{
|
||||
error = USB_DeviceDeinitEndpoint(
|
||||
cdcAcmHandle->handle, cdcAcmHandle->dataInterfaceHandle->endpointList.endpoint[count].endpointAddress);
|
||||
}
|
||||
cdcAcmHandle->commInterfaceHandle = NULL;
|
||||
cdcAcmHandle->dataInterfaceHandle = NULL;
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Handles the CDC ACM class event.
|
||||
*
|
||||
* This function responses to various events including the common device events and the class specific events.
|
||||
* For class specific events, it calls the class callback defined in the application to deal with the class specific
|
||||
* event.
|
||||
*
|
||||
* @param handle The class handle of the CDC ACM class.
|
||||
* @param event The event type.
|
||||
* @param param The class handle of the CDC ACM class.
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
*/
|
||||
usb_status_t USB_DeviceCdcAcmEvent(void *handle, uint32_t event, void *param)
|
||||
{
|
||||
usb_device_cdc_acm_struct_t *cdcAcmHandle;
|
||||
usb_device_cdc_acm_request_param_struct_t reqParam;
|
||||
usb_status_t error = kStatus_USB_Error;
|
||||
uint32_t count;
|
||||
uint16_t interfaceAlternate;
|
||||
uint8_t *temp8;
|
||||
uint8_t alternate;
|
||||
|
||||
if ((!param) || (!handle))
|
||||
{
|
||||
return kStatus_USB_InvalidHandle;
|
||||
}
|
||||
|
||||
cdcAcmHandle = (usb_device_cdc_acm_struct_t *)handle;
|
||||
|
||||
switch (event)
|
||||
{
|
||||
case kUSB_DeviceClassEventDeviceReset:
|
||||
/* Bus reset, clear the configuration. */
|
||||
cdcAcmHandle->configuration = 0;
|
||||
break;
|
||||
case kUSB_DeviceClassEventSetConfiguration:
|
||||
temp8 = ((uint8_t *)param);
|
||||
if (!cdcAcmHandle->configStruct)
|
||||
{
|
||||
break;
|
||||
}
|
||||
if (*temp8 == cdcAcmHandle->configuration)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
error = USB_DeviceCdcAcmEndpointsDeinit(cdcAcmHandle);
|
||||
cdcAcmHandle->configuration = *temp8;
|
||||
cdcAcmHandle->alternate = 0;
|
||||
error = USB_DeviceCdcAcmEndpointsInit(cdcAcmHandle);
|
||||
if (kStatus_USB_Success != error)
|
||||
{
|
||||
#ifdef DEBUG
|
||||
usb_echo("kUSB_DeviceClassEventSetConfiguration, USB_DeviceInitEndpoint fail\r\n");
|
||||
#endif
|
||||
}
|
||||
break;
|
||||
case kUSB_DeviceClassEventSetInterface:
|
||||
if (!cdcAcmHandle->configStruct)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
interfaceAlternate = *((uint16_t *)param);
|
||||
alternate = (uint8_t)(interfaceAlternate & 0xFFU);
|
||||
|
||||
if (cdcAcmHandle->interfaceNumber != ((uint8_t)(interfaceAlternate >> 8U)))
|
||||
{
|
||||
break;
|
||||
}
|
||||
if (alternate == cdcAcmHandle->alternate)
|
||||
{
|
||||
break;
|
||||
}
|
||||
error = USB_DeviceCdcAcmEndpointsDeinit(cdcAcmHandle);
|
||||
cdcAcmHandle->alternate = alternate;
|
||||
error = USB_DeviceCdcAcmEndpointsInit(cdcAcmHandle);
|
||||
if (kStatus_USB_Success != error)
|
||||
{
|
||||
#ifdef DEBUG
|
||||
usb_echo("kUSB_DeviceClassEventSetInterface, USB_DeviceInitEndpoint fail\r\n");
|
||||
#endif
|
||||
}
|
||||
break;
|
||||
case kUSB_DeviceClassEventSetEndpointHalt:
|
||||
if ((!cdcAcmHandle->configStruct) || (!cdcAcmHandle->commInterfaceHandle) ||
|
||||
(!cdcAcmHandle->dataInterfaceHandle))
|
||||
{
|
||||
break;
|
||||
}
|
||||
temp8 = ((uint8_t *)param);
|
||||
for (count = 0; count < cdcAcmHandle->commInterfaceHandle->endpointList.count; count++)
|
||||
{
|
||||
if (*temp8 == cdcAcmHandle->commInterfaceHandle->endpointList.endpoint[count].endpointAddress)
|
||||
{
|
||||
cdcAcmHandle->interruptIn.pipeStall = 1U;
|
||||
error = USB_DeviceStallEndpoint(cdcAcmHandle->handle, *temp8);
|
||||
}
|
||||
}
|
||||
for (count = 0; count < cdcAcmHandle->dataInterfaceHandle->endpointList.count; count++)
|
||||
{
|
||||
if (*temp8 == cdcAcmHandle->dataInterfaceHandle->endpointList.endpoint[count].endpointAddress)
|
||||
{
|
||||
if (USB_IN == (((*temp8) & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) >>
|
||||
USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT))
|
||||
{
|
||||
cdcAcmHandle->bulkIn.pipeStall = 1U;
|
||||
}
|
||||
else
|
||||
{
|
||||
cdcAcmHandle->bulkOut.pipeStall = 1U;
|
||||
}
|
||||
error = USB_DeviceStallEndpoint(cdcAcmHandle->handle, *temp8);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case kUSB_DeviceClassEventClearEndpointHalt:
|
||||
if ((!cdcAcmHandle->configStruct) || (!cdcAcmHandle->commInterfaceHandle) ||
|
||||
(!cdcAcmHandle->dataInterfaceHandle))
|
||||
{
|
||||
break;
|
||||
}
|
||||
temp8 = ((uint8_t *)param);
|
||||
for (count = 0; count < cdcAcmHandle->commInterfaceHandle->endpointList.count; count++)
|
||||
{
|
||||
if (*temp8 == cdcAcmHandle->commInterfaceHandle->endpointList.endpoint[count].endpointAddress)
|
||||
{
|
||||
error = USB_DeviceUnstallEndpoint(cdcAcmHandle->handle, *temp8);
|
||||
if (USB_IN == (((*temp8) & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) >>
|
||||
USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT))
|
||||
{
|
||||
if (cdcAcmHandle->interruptIn.pipeStall)
|
||||
{
|
||||
cdcAcmHandle->interruptIn.pipeStall = 0U;
|
||||
if ((uint8_t *)USB_UNINITIALIZED_VAL_32 != cdcAcmHandle->interruptIn.pipeDataBuffer)
|
||||
{
|
||||
error = USB_DeviceSendRequest(cdcAcmHandle->handle, (cdcAcmHandle->interruptIn.ep),
|
||||
cdcAcmHandle->interruptIn.pipeDataBuffer,
|
||||
cdcAcmHandle->interruptIn.pipeDataLen);
|
||||
if (kStatus_USB_Success != error)
|
||||
{
|
||||
usb_device_endpoint_callback_message_struct_t endpointCallbackMessage;
|
||||
endpointCallbackMessage.buffer = cdcAcmHandle->interruptIn.pipeDataBuffer;
|
||||
endpointCallbackMessage.length = cdcAcmHandle->interruptIn.pipeDataLen;
|
||||
endpointCallbackMessage.isSetup = 0U;
|
||||
USB_DeviceCdcAcmBulkIn(cdcAcmHandle->handle, (void *)&endpointCallbackMessage,
|
||||
handle);
|
||||
}
|
||||
cdcAcmHandle->interruptIn.pipeDataBuffer = (uint8_t *)USB_UNINITIALIZED_VAL_32;
|
||||
cdcAcmHandle->interruptIn.pipeDataLen = 0U;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for (count = 0; count < cdcAcmHandle->dataInterfaceHandle->endpointList.count; count++)
|
||||
{
|
||||
if (*temp8 == cdcAcmHandle->dataInterfaceHandle->endpointList.endpoint[count].endpointAddress)
|
||||
{
|
||||
error = USB_DeviceUnstallEndpoint(cdcAcmHandle->handle, *temp8);
|
||||
if (USB_IN == (((*temp8) & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) >>
|
||||
USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT))
|
||||
{
|
||||
if (cdcAcmHandle->bulkIn.pipeStall)
|
||||
{
|
||||
cdcAcmHandle->bulkIn.pipeStall = 0U;
|
||||
if ((uint8_t *)USB_UNINITIALIZED_VAL_32 != cdcAcmHandle->bulkIn.pipeDataBuffer)
|
||||
{
|
||||
error = USB_DeviceSendRequest(cdcAcmHandle->handle, (cdcAcmHandle->bulkIn.ep),
|
||||
cdcAcmHandle->bulkIn.pipeDataBuffer,
|
||||
cdcAcmHandle->bulkIn.pipeDataLen);
|
||||
if (kStatus_USB_Success != error)
|
||||
{
|
||||
usb_device_endpoint_callback_message_struct_t endpointCallbackMessage;
|
||||
endpointCallbackMessage.buffer = cdcAcmHandle->bulkIn.pipeDataBuffer;
|
||||
endpointCallbackMessage.length = cdcAcmHandle->bulkIn.pipeDataLen;
|
||||
endpointCallbackMessage.isSetup = 0U;
|
||||
USB_DeviceCdcAcmBulkIn(cdcAcmHandle->handle, (void *)&endpointCallbackMessage,
|
||||
handle);
|
||||
}
|
||||
cdcAcmHandle->bulkIn.pipeDataBuffer = (uint8_t *)USB_UNINITIALIZED_VAL_32;
|
||||
cdcAcmHandle->bulkIn.pipeDataLen = 0U;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (cdcAcmHandle->bulkOut.pipeStall)
|
||||
{
|
||||
cdcAcmHandle->bulkOut.pipeStall = 0U;
|
||||
if ((uint8_t *)USB_UNINITIALIZED_VAL_32 != cdcAcmHandle->bulkOut.pipeDataBuffer)
|
||||
{
|
||||
error = USB_DeviceRecvRequest(cdcAcmHandle->handle, (cdcAcmHandle->bulkOut.ep),
|
||||
cdcAcmHandle->bulkOut.pipeDataBuffer,
|
||||
cdcAcmHandle->bulkOut.pipeDataLen);
|
||||
if (kStatus_USB_Success != error)
|
||||
{
|
||||
usb_device_endpoint_callback_message_struct_t endpointCallbackMessage;
|
||||
endpointCallbackMessage.buffer = cdcAcmHandle->bulkOut.pipeDataBuffer;
|
||||
endpointCallbackMessage.length = cdcAcmHandle->bulkOut.pipeDataLen;
|
||||
endpointCallbackMessage.isSetup = 0U;
|
||||
USB_DeviceCdcAcmBulkOut(cdcAcmHandle->handle, (void *)&endpointCallbackMessage,
|
||||
handle);
|
||||
}
|
||||
cdcAcmHandle->bulkOut.pipeDataBuffer = (uint8_t *)USB_UNINITIALIZED_VAL_32;
|
||||
cdcAcmHandle->bulkOut.pipeDataLen = 0U;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case kUSB_DeviceClassEventClassRequest:
|
||||
if (param)
|
||||
{
|
||||
usb_device_control_request_struct_t *controlRequest = (usb_device_control_request_struct_t *)param;
|
||||
|
||||
if ((controlRequest->setup->wIndex & 0xFFU) != cdcAcmHandle->interfaceNumber)
|
||||
{
|
||||
break;
|
||||
}
|
||||
/* Standard CDC request */
|
||||
if (USB_REQUEST_TYPE_TYPE_CLASS == (controlRequest->setup->bmRequestType & USB_REQUEST_TYPE_TYPE_MASK))
|
||||
{
|
||||
reqParam.buffer = &(controlRequest->buffer);
|
||||
reqParam.length = &(controlRequest->length);
|
||||
reqParam.interfaceIndex = controlRequest->setup->wIndex;
|
||||
reqParam.setupValue = controlRequest->setup->wValue;
|
||||
reqParam.isSetup = controlRequest->isSetup;
|
||||
switch (controlRequest->setup->bRequest)
|
||||
{
|
||||
case USB_DEVICE_CDC_REQUEST_SEND_ENCAPSULATED_COMMAND:
|
||||
/* classCallback is initialized in classInit of s_UsbDeviceClassInterfaceMap,
|
||||
it is from the second parameter of classInit */
|
||||
error = cdcAcmHandle->configStruct->classCallback(
|
||||
(class_handle_t)cdcAcmHandle, kUSB_DeviceCdcEventSendEncapsulatedCommand, &reqParam);
|
||||
break;
|
||||
case USB_DEVICE_CDC_REQUEST_GET_ENCAPSULATED_RESPONSE:
|
||||
/* classCallback is initialized in classInit of s_UsbDeviceClassInterfaceMap,
|
||||
it is from the second parameter of classInit */
|
||||
error = cdcAcmHandle->configStruct->classCallback(
|
||||
(class_handle_t)cdcAcmHandle, kUSB_DeviceCdcEventGetEncapsulatedResponse, &reqParam);
|
||||
break;
|
||||
case USB_DEVICE_CDC_REQUEST_SET_COMM_FEATURE:
|
||||
/* classCallback is initialized in classInit of s_UsbDeviceClassInterfaceMap,
|
||||
it is from the second parameter of classInit */
|
||||
error = cdcAcmHandle->configStruct->classCallback(
|
||||
(class_handle_t)cdcAcmHandle, kUSB_DeviceCdcEventSetCommFeature, &reqParam);
|
||||
break;
|
||||
case USB_DEVICE_CDC_REQUEST_GET_COMM_FEATURE:
|
||||
/* classCallback is initialized in classInit of s_UsbDeviceClassInterfaceMap,
|
||||
it is from the second parameter of classInit */
|
||||
error = cdcAcmHandle->configStruct->classCallback(
|
||||
(class_handle_t)cdcAcmHandle, kUSB_DeviceCdcEventGetCommFeature, &reqParam);
|
||||
break;
|
||||
case USB_DEVICE_CDC_REQUEST_CLEAR_COMM_FEATURE:
|
||||
/* classCallback is initialized in classInit of s_UsbDeviceClassInterfaceMap,
|
||||
it is from the second parameter of classInit */
|
||||
error = cdcAcmHandle->configStruct->classCallback(
|
||||
(class_handle_t)cdcAcmHandle, kUSB_DeviceCdcEventClearCommFeature, &reqParam);
|
||||
break;
|
||||
case USB_DEVICE_CDC_REQUEST_GET_LINE_CODING:
|
||||
/* classCallback is initialized in classInit of s_UsbDeviceClassInterfaceMap,
|
||||
it is from the second parameter of classInit */
|
||||
error = cdcAcmHandle->configStruct->classCallback(
|
||||
(class_handle_t)cdcAcmHandle, kUSB_DeviceCdcEventGetLineCoding, &reqParam);
|
||||
break;
|
||||
case USB_DEVICE_CDC_REQUEST_SET_LINE_CODING:
|
||||
/* classCallback is initialized in classInit of s_UsbDeviceClassInterfaceMap,
|
||||
it is from the second parameter of classInit */
|
||||
error = cdcAcmHandle->configStruct->classCallback(
|
||||
(class_handle_t)cdcAcmHandle, kUSB_DeviceCdcEventSetLineCoding, &reqParam);
|
||||
break;
|
||||
case USB_DEVICE_CDC_REQUEST_SET_CONTROL_LINE_STATE:
|
||||
/* classCallback is initialized in classInit of s_UsbDeviceClassInterfaceMap,
|
||||
it is from the second parameter of classInit */
|
||||
error = cdcAcmHandle->configStruct->classCallback(
|
||||
(class_handle_t)cdcAcmHandle, kUSB_DeviceCdcEventSetControlLineState, &reqParam);
|
||||
break;
|
||||
case USB_DEVICE_CDC_REQUEST_SEND_BREAK:
|
||||
/* classCallback is initialized in classInit of s_UsbDeviceClassInterfaceMap,
|
||||
it is from the second parameter of classInit */
|
||||
error = cdcAcmHandle->configStruct->classCallback((class_handle_t)cdcAcmHandle,
|
||||
kUSB_DeviceCdcEventSendBreak, &reqParam);
|
||||
break;
|
||||
default:
|
||||
error = kStatus_USB_InvalidRequest;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return error;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Initializes the USB CDC ACM class.
|
||||
*
|
||||
* This function obtains a usb device handle according to the controller id, initializes the CDC ACM class
|
||||
* with the class configure parameters and creates the mutex for each pipe.
|
||||
*
|
||||
* @param controllerId The id of the controller. The value can be choosen from kUSB_ControllerKhci0,
|
||||
* kUSB_ControllerKhci1, kUSB_ControllerEhci0 or kUSB_ControllerEhci1.
|
||||
* @param config The user configuration structure of type usb_device_class_config_struct_t. The user
|
||||
* populates the members of this structure and passes the pointer of this structure
|
||||
* into this function.
|
||||
* @param handle It is out parameter. The class handle of the CDC ACM class.
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
*/
|
||||
usb_status_t USB_DeviceCdcAcmInit(uint8_t controllerId,
|
||||
usb_device_class_config_struct_t *config,
|
||||
class_handle_t *handle)
|
||||
{
|
||||
usb_device_cdc_acm_struct_t *cdcAcmHandle;
|
||||
usb_status_t error = kStatus_USB_Error;
|
||||
|
||||
error = USB_DeviceCdcAcmAllocateHandle(&cdcAcmHandle);
|
||||
|
||||
if (kStatus_USB_Success != error)
|
||||
{
|
||||
return error;
|
||||
}
|
||||
|
||||
error = USB_DeviceClassGetDeviceHandle(controllerId, &cdcAcmHandle->handle);
|
||||
|
||||
if (kStatus_USB_Success != error)
|
||||
{
|
||||
return error;
|
||||
}
|
||||
|
||||
if (!cdcAcmHandle->handle)
|
||||
{
|
||||
return kStatus_USB_InvalidHandle;
|
||||
}
|
||||
cdcAcmHandle->configStruct = config;
|
||||
cdcAcmHandle->configuration = 0;
|
||||
cdcAcmHandle->alternate = 0xFF;
|
||||
|
||||
cdcAcmHandle->bulkIn.mutex = (osa_mutex_handle_t)&cdcAcmHandle->bulkIn.mutexBuffer[0];
|
||||
if (KOSA_StatusSuccess != OSA_MutexCreate((cdcAcmHandle->bulkIn.mutex)))
|
||||
{
|
||||
#ifdef DEBUG
|
||||
usb_echo("mutex create error!");
|
||||
#endif
|
||||
}
|
||||
cdcAcmHandle->bulkOut.mutex = (osa_mutex_handle_t)&cdcAcmHandle->bulkOut.mutexBuffer[0];
|
||||
if (KOSA_StatusSuccess != OSA_MutexCreate((cdcAcmHandle->bulkOut.mutex)))
|
||||
{
|
||||
#ifdef DEBUG
|
||||
usb_echo("mutex create error!");
|
||||
#endif
|
||||
}
|
||||
cdcAcmHandle->interruptIn.mutex = (osa_mutex_handle_t)&cdcAcmHandle->interruptIn.mutexBuffer[0];
|
||||
if (KOSA_StatusSuccess != OSA_MutexCreate((cdcAcmHandle->interruptIn.mutex)))
|
||||
{
|
||||
#ifdef DEBUG
|
||||
usb_echo("mutex create error!");
|
||||
#endif
|
||||
}
|
||||
*handle = (class_handle_t)cdcAcmHandle;
|
||||
return error;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief De-Initializes the USB CDC ACM class.
|
||||
*
|
||||
* This function destroys the mutex for each pipe, deinit each endpoint of the CDC ACM class and free
|
||||
* the CDC ACM class handle.
|
||||
*
|
||||
* @param handle The class handle of the CDC ACM class.
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
*/
|
||||
usb_status_t USB_DeviceCdcAcmDeinit(class_handle_t handle)
|
||||
{
|
||||
usb_device_cdc_acm_struct_t *cdcAcmHandle;
|
||||
usb_status_t error = kStatus_USB_Error;
|
||||
|
||||
cdcAcmHandle = (usb_device_cdc_acm_struct_t *)handle;
|
||||
|
||||
if (!cdcAcmHandle)
|
||||
{
|
||||
return kStatus_USB_InvalidHandle;
|
||||
}
|
||||
if (KOSA_StatusSuccess != OSA_MutexDestroy((cdcAcmHandle->bulkIn.mutex)))
|
||||
{
|
||||
#ifdef DEBUG
|
||||
usb_echo("mutex destroy error!");
|
||||
#endif
|
||||
}
|
||||
if (KOSA_StatusSuccess != OSA_MutexDestroy((cdcAcmHandle->bulkOut.mutex)))
|
||||
{
|
||||
#ifdef DEBUG
|
||||
usb_echo("mutex destroy error!");
|
||||
#endif
|
||||
}
|
||||
if (KOSA_StatusSuccess != OSA_MutexDestroy((cdcAcmHandle->interruptIn.mutex)))
|
||||
{
|
||||
#ifdef DEBUG
|
||||
usb_echo("mutex destroy error!");
|
||||
#endif
|
||||
}
|
||||
error = USB_DeviceCdcAcmEndpointsDeinit(cdcAcmHandle);
|
||||
USB_DeviceCdcAcmFreeHandle(cdcAcmHandle);
|
||||
return error;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Prime the endpoint to send packet to host.
|
||||
*
|
||||
* This function checks whether the endpoint is sending packet, then it primes the endpoint
|
||||
* with the buffer address and the buffer length if the pipe is not busy. Otherwise, it ignores this transfer by
|
||||
* returning an error code.
|
||||
*
|
||||
* @param handle The class handle of the CDC ACM class.
|
||||
* @param ep The endpoint number of the transfer.
|
||||
* @param buffer The pointer to the buffer to be transferred.
|
||||
* @param length The length of the buffer to be transferred.
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
*/
|
||||
usb_status_t USB_DeviceCdcAcmSend(class_handle_t handle, uint8_t ep, uint8_t *buffer, uint32_t length)
|
||||
{
|
||||
usb_device_cdc_acm_struct_t *cdcAcmHandle;
|
||||
usb_status_t error = kStatus_USB_Error;
|
||||
usb_device_cdc_acm_pipe_t *cdcAcmPipe = NULL;
|
||||
|
||||
if (!handle)
|
||||
{
|
||||
return kStatus_USB_InvalidHandle;
|
||||
}
|
||||
cdcAcmHandle = (usb_device_cdc_acm_struct_t *)handle;
|
||||
|
||||
if (cdcAcmHandle->bulkIn.ep == ep)
|
||||
{
|
||||
cdcAcmPipe = &(cdcAcmHandle->bulkIn);
|
||||
}
|
||||
else if (cdcAcmHandle->interruptIn.ep == ep)
|
||||
{
|
||||
cdcAcmPipe = &(cdcAcmHandle->interruptIn);
|
||||
}
|
||||
else
|
||||
{
|
||||
}
|
||||
|
||||
if (NULL != cdcAcmPipe)
|
||||
{
|
||||
if (1U == cdcAcmPipe->isBusy)
|
||||
{
|
||||
return kStatus_USB_Busy;
|
||||
}
|
||||
cdcAcmPipe->isBusy = 1U;
|
||||
|
||||
if (cdcAcmPipe->pipeStall)
|
||||
{
|
||||
cdcAcmPipe->pipeDataBuffer = buffer;
|
||||
cdcAcmPipe->pipeDataLen = length;
|
||||
return kStatus_USB_Success;
|
||||
}
|
||||
|
||||
error = USB_DeviceSendRequest(cdcAcmHandle->handle, ep, buffer, length);
|
||||
if (kStatus_USB_Success != error)
|
||||
{
|
||||
cdcAcmPipe->isBusy = 0U;
|
||||
}
|
||||
}
|
||||
return error;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Prime the endpoint to receive packet from host.
|
||||
*
|
||||
* This function checks whether the endpoint is receiving packet, then it primes the endpoint
|
||||
* with the buffer address and the buffer length if the pipe is not busy. Otherwise, it ignores this transfer by
|
||||
* returning an error code.
|
||||
*
|
||||
* @param handle The class handle of the CDC ACM class.
|
||||
* @param ep The endpoint number of the transfer.
|
||||
* @param buffer The pointer to the buffer to be transferred.
|
||||
* @param length The length of the buffer to be transferred.
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
*/
|
||||
usb_status_t USB_DeviceCdcAcmRecv(class_handle_t handle, uint8_t ep, uint8_t *buffer, uint32_t length)
|
||||
{
|
||||
usb_device_cdc_acm_struct_t *cdcAcmHandle;
|
||||
usb_status_t error = kStatus_USB_Error;
|
||||
if (!handle)
|
||||
{
|
||||
return kStatus_USB_InvalidHandle;
|
||||
}
|
||||
cdcAcmHandle = (usb_device_cdc_acm_struct_t *)handle;
|
||||
|
||||
if (1U == cdcAcmHandle->bulkOut.isBusy)
|
||||
{
|
||||
return kStatus_USB_Busy;
|
||||
}
|
||||
cdcAcmHandle->bulkOut.isBusy = 1U;
|
||||
|
||||
if (cdcAcmHandle->bulkOut.pipeStall)
|
||||
{
|
||||
cdcAcmHandle->bulkOut.pipeDataBuffer = buffer;
|
||||
cdcAcmHandle->bulkOut.pipeDataLen = length;
|
||||
return kStatus_USB_Success;
|
||||
}
|
||||
|
||||
error = USB_DeviceRecvRequest(cdcAcmHandle->handle, ep, buffer, length);
|
||||
if (kStatus_USB_Success != error)
|
||||
{
|
||||
cdcAcmHandle->bulkOut.isBusy = 0U;
|
||||
}
|
||||
return error;
|
||||
}
|
||||
|
||||
#endif /* USB_DEVICE_CONFIG_CDC_ACM */
|
@@ -1,270 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2015, Freescale Semiconductor, Inc.
|
||||
* Copyright 2016,2019 NXP
|
||||
* All rights reserved.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
#ifndef _USB_DEVICE_CDC_ACM_H_
|
||||
#define _USB_DEVICE_CDC_ACM_H_
|
||||
|
||||
/*!
|
||||
* @addtogroup cdc_acm
|
||||
* @{
|
||||
*/
|
||||
|
||||
/*******************************************************************************
|
||||
* Definitions
|
||||
******************************************************************************/
|
||||
#define USB_DEVICE_CONFIG_CDC_ACM_MAX_INSTANCE (1) /*!< The maximum number of CDC device instance. */
|
||||
#define USB_DEVICE_CONFIG_CDC_COMM_CLASS_CODE (0x02) /*!< The CDC communication class code. */
|
||||
#define USB_DEVICE_CONFIG_CDC_DATA_CLASS_CODE (0x0A) /*!< The CDC data class code. */
|
||||
|
||||
#define USB_DEVICE_CDC_REQUEST_SEND_ENCAPSULATED_COMMAND \
|
||||
(0x00) /*!< The CDC class request code for SEND_ENCAPSULATED_COMMAND. */
|
||||
#define USB_DEVICE_CDC_REQUEST_GET_ENCAPSULATED_RESPONSE \
|
||||
(0x01) /*!< The CDC class request code for GET_ENCAPSULATED_RESPONSE. */
|
||||
#define USB_DEVICE_CDC_REQUEST_SET_COMM_FEATURE (0x02) /*!< The CDC class request code for SET_COMM_FEATURE. */
|
||||
#define USB_DEVICE_CDC_REQUEST_GET_COMM_FEATURE (0x03) /*!< The CDC class request code for GET_COMM_FEATURE. */
|
||||
#define USB_DEVICE_CDC_REQUEST_CLEAR_COMM_FEATURE (0x04) /*!< The CDC class request code for CLEAR_COMM_FEATURE. */
|
||||
#define USB_DEVICE_CDC_REQUEST_SET_AUX_LINE_STATE (0x10) /*!< The CDC class request code for SET_AUX_LINE_STATE. */
|
||||
#define USB_DEVICE_CDC_REQUEST_SET_HOOK_STATE (0x11) /*!< The CDC class request code for SET_HOOK_STATE. */
|
||||
#define USB_DEVICE_CDC_REQUEST_PULSE_SETUP (0x12) /*!< The CDC class request code for PULSE_SETUP. */
|
||||
#define USB_DEVICE_CDC_REQUEST_SEND_PULSE (0x13) /*!< The CDC class request code for SEND_PULSE. */
|
||||
#define USB_DEVICE_CDC_REQUEST_SET_PULSE_TIME (0x14) /*!< The CDC class request code for SET_PULSE_TIME. */
|
||||
#define USB_DEVICE_CDC_REQUEST_RING_AUX_JACK (0x15) /*!< The CDC class request code for RING_AUX_JACK. */
|
||||
#define USB_DEVICE_CDC_REQUEST_SET_LINE_CODING (0x20) /*!< The CDC class request code for SET_LINE_CODING. */
|
||||
#define USB_DEVICE_CDC_REQUEST_GET_LINE_CODING (0x21) /*!< The CDC class request code for GET_LINE_CODING. */
|
||||
#define USB_DEVICE_CDC_REQUEST_SET_CONTROL_LINE_STATE \
|
||||
(0x22) /*!< The CDC class request code for SET_CONTROL_LINE_STATE. */
|
||||
#define USB_DEVICE_CDC_REQUEST_SEND_BREAK (0x23) /*!< The CDC class request code for SEND_BREAK. */
|
||||
#define USB_DEVICE_CDC_REQUEST_SET_RINGER_PARAMS (0x30) /*!< The CDC class request code for SET_RINGER_PARAMS. */
|
||||
#define USB_DEVICE_CDC_REQUEST_GET_RINGER_PARAMS (0x31) /*!< The CDC class request code for GET_RINGER_PARAMS. */
|
||||
#define USB_DEVICE_CDC_REQUEST_SET_OPERATION_PARAM (0x32) /*!< The CDC class request code for SET_OPERATION_PARAM. */
|
||||
#define USB_DEVICE_CDC_REQUEST_GET_OPERATION_PARAM (0x33) /*!< The CDC class request code for GET_OPERATION_PARAM. */
|
||||
#define USB_DEVICE_CDC_REQUEST_SET_LINE_PARAMS (0x34) /*!< The CDC class request code for SET_LINE_PARAMS. */
|
||||
#define USB_DEVICE_CDC_REQUEST_GET_LINE_PARAMS (0x35) /*!< The CDC class request code for GET_LINE_PARAMS. */
|
||||
#define USB_DEVICE_CDC_REQUEST_DIAL_DIGITS (0x36) /*!< The CDC class request code for DIAL_DIGITS. */
|
||||
#define USB_DEVICE_CDC_REQUEST_SET_UNIT_PARAMETER (0x37) /*!< The CDC class request code for SET_UNIT_PARAMETER. */
|
||||
#define USB_DEVICE_CDC_REQUEST_GET_UNIT_PARAMETER (0x38) /*!< The CDC class request code for GET_UNIT_PARAMETER. */
|
||||
#define USB_DEVICE_CDC_REQUEST_CLEAR_UNIT_PARAMETER \
|
||||
(0x39) /*!< The CDC class request code for CLEAR_UNIT_PARAMETER. */
|
||||
#define USB_DEVICE_CDC_REQUEST_SET_ETHERNET_MULTICAST_FILTERS \
|
||||
(0x40) /*!< The CDC class request code for SET_ETHERNET_MULTICAST_FILTERS. */
|
||||
#define USB_DEVICE_CDC_REQUEST_SET_ETHERNET_POW_PATTER_FILTER \
|
||||
(0x41) /*!< The CDC class request code for SET_ETHERNET_POW_PATTER_FILTER. */
|
||||
#define USB_DEVICE_CDC_REQUEST_GET_ETHERNET_POW_PATTER_FILTER \
|
||||
(0x42) /*!< The CDC class request code for GET_ETHERNET_POW_PATTER_FILTER. */
|
||||
#define USB_DEVICE_CDC_REQUEST_SET_ETHERNET_PACKET_FILTER \
|
||||
(0x43) /*!< The CDC class request code for SET_ETHERNET_PACKET_FILTER. */
|
||||
#define USB_DEVICE_CDC_REQUEST_GET_ETHERNET_STATISTIC \
|
||||
(0x44) /*!< The CDC class request code for GET_ETHERNET_STATISTIC. */
|
||||
#define USB_DEVICE_CDC_REQUEST_SET_ATM_DATA_FORMAT (0x50) /*!< The CDC class request code for SET_ATM_DATA_FORMAT. */
|
||||
#define USB_DEVICE_CDC_REQUEST_GET_ATM_DEVICE_STATISTICS \
|
||||
(0x51) /*!< The CDC class request code for GET_ATM_DEVICE_STATISTICS. */
|
||||
#define USB_DEVICE_CDC_REQUEST_SET_ATM_DEFAULT_VC (0x52) /*!< The CDC class request code for SET_ATM_DEFAULT_VC. */
|
||||
#define USB_DEVICE_CDC_REQUEST_GET_ATM_VC_STATISTICS \
|
||||
(0x53) /*!< The CDC class request code for GET_ATM_VC_STATISTICS. */
|
||||
#define USB_DEVICE_CDC_REQUEST_MDLM_SPECIFIC_REQUESTS_MASK \
|
||||
(0x7F) /*!< The CDC class request code for MDLM_SPECIFIC_REQUESTS_MASK. */
|
||||
|
||||
#define USB_DEVICE_CDC_NOTIF_NETWORK_CONNECTION (0x00) /*!< The CDC class notify code for NETWORK_CONNECTION. */
|
||||
#define USB_DEVICE_CDC_NOTIF_RESPONSE_AVAIL (0x01) /*!< The CDC class notify code for RESPONSE_AVAIL. */
|
||||
#define USB_DEVICE_CDC_NOTIF_AUX_JACK_HOOK_STATE (0x08) /*!< The CDC class notify code for AUX_JACK_HOOK_STATE. */
|
||||
#define USB_DEVICE_CDC_NOTIF_RING_DETECT (0x09) /*!< The CDC class notify code for RING_DETECT. */
|
||||
#define USB_DEVICE_CDC_NOTIF_SERIAL_STATE (0x20) /*!< The CDC class notify code for SERIAL_STATE. */
|
||||
#define USB_DEVICE_CDC_NOTIF_CALL_STATE_CHANGE (0x28) /*!< The CDC class notify code for CALL_STATE_CHANGE. */
|
||||
#define USB_DEVICE_CDC_NOTIF_LINE_STATE_CHANGE (0x29) /*!< The CDC class notify code for LINE_STATE_CHANGE. */
|
||||
#define USB_DEVICE_CDC_NOTIF_CONNECTION_SPEED_CHANGE \
|
||||
(0x2A) /*!< The CDC class notify code for CONNECTION_SPEED_CHANGE. */
|
||||
|
||||
#define USB_DEVICE_CDC_FEATURE_ABSTRACT_STATE (0x01) /*!< The CDC class feature select code for ABSTRACT_STATE. */
|
||||
#define USB_DEVICE_CDC_FEATURE_COUNTRY_SETTING (0x02) /*!< The CDC class feature select code for COUNTRY_SETTING. */
|
||||
|
||||
#define USB_DEVICE_CDC_CONTROL_SIG_BITMAP_CARRIER_ACTIVATION \
|
||||
(0x02) /*!< The CDC class control signal bitmap value for CARRIER_ACTIVATION. */
|
||||
#define USB_DEVICE_CDC_CONTROL_SIG_BITMAP_DTE_PRESENCE \
|
||||
(0x01) /*!< The CDC class control signal bitmap value for DTE_PRESENCE. */
|
||||
#define USB_DEVICE_CDC_UART_STATE_RX_CARRIER (0x01) /*!< The UART state bitmap value of RX_CARRIER. */
|
||||
#define USB_DEVICE_CDC_UART_STATE_TX_CARRIER (0x02) /*!< The UART state bitmap value of TX_CARRIER. */
|
||||
#define USB_DEVICE_CDC_UART_STATE_BREAK (0x04) /*!< The UART state bitmap value of BREAK. */
|
||||
#define USB_DEVICE_CDC_UART_STATE_RING_SIGNAL (0x08) /*!< The UART state bitmap value of RING_SIGNAL. */
|
||||
#define USB_DEVICE_CDC_UART_STATE_FRAMING (0x10) /*!< The UART state bitmap value of FRAMING. */
|
||||
#define USB_DEVICE_CDC_UART_STATE_PARITY (0x20) /*!< The UART state bitmap value of PARITY. */
|
||||
#define USB_DEVICE_CDC_UART_STATE_OVERRUN (0x40) /*!< The UART state bitmap value of OVERRUN. */
|
||||
|
||||
/*! @brief Definition of CDC class event. */
|
||||
typedef enum _usb_device_cdc_acm_event
|
||||
{
|
||||
kUSB_DeviceCdcEventSendResponse = 0x01, /*!< This event indicates the bulk send transfer is complete or cancelled etc. */
|
||||
kUSB_DeviceCdcEventRecvResponse, /*!< This event indicates the bulk receive transfer is complete or cancelled etc.. */
|
||||
kUSB_DeviceCdcEventSerialStateNotif, /*!< This event indicates the serial state has been sent to the host. */
|
||||
kUSB_DeviceCdcEventSendEncapsulatedCommand, /*!< This event indicates the device received the
|
||||
SEND_ENCAPSULATED_COMMAND request. */
|
||||
kUSB_DeviceCdcEventGetEncapsulatedResponse, /*!< This event indicates the device received the
|
||||
GET_ENCAPSULATED_RESPONSE request. */
|
||||
kUSB_DeviceCdcEventSetCommFeature, /*!< This event indicates the device received the SET_COMM_FEATURE request. */
|
||||
kUSB_DeviceCdcEventGetCommFeature, /*!< This event indicates the device received the GET_COMM_FEATURE request. */
|
||||
kUSB_DeviceCdcEventClearCommFeature, /*!< This event indicates the device received the CLEAR_COMM_FEATURE request.
|
||||
*/
|
||||
kUSB_DeviceCdcEventGetLineCoding, /*!< This event indicates the device received the GET_LINE_CODING request. */
|
||||
kUSB_DeviceCdcEventSetLineCoding, /*!< This event indicates the device received the SET_LINE_CODING request. */
|
||||
kUSB_DeviceCdcEventSetControlLineState, /*!< This event indicates the device received the SET_CONTRL_LINE_STATE
|
||||
request. */
|
||||
kUSB_DeviceCdcEventSendBreak /*!< This event indicates the device received the SEND_BREAK request. */
|
||||
} usb_device_cdc_acm_event_t;
|
||||
|
||||
/*! @brief Definition of parameters for CDC ACM request. */
|
||||
typedef struct _usb_device_cdc_acm_request_param_struct
|
||||
{
|
||||
uint8_t **buffer; /*!< The pointer to the address of the buffer for CDC class request. */
|
||||
uint32_t *length; /*!< The pointer to the length of the buffer for CDC class request. */
|
||||
uint16_t interfaceIndex; /*!< The interface index of the setup packet. */
|
||||
uint16_t setupValue; /*!< The wValue field of the setup packet. */
|
||||
uint8_t isSetup; /*!< The flag indicates if it is a setup packet, 1: yes, 0: no. */
|
||||
} usb_device_cdc_acm_request_param_struct_t;
|
||||
|
||||
/*! @brief Definition of pipe structure. */
|
||||
typedef struct _usb_device_cdc_acm_pipe
|
||||
{
|
||||
osa_mutex_handle_t mutex; /*!< The mutex of the pipe. */
|
||||
uint32_t mutexBuffer[(OSA_MUTEX_HANDLE_SIZE + 3)/4];
|
||||
uint8_t *pipeDataBuffer; /*!< pipe data buffer backup when stall */
|
||||
uint32_t pipeDataLen; /*!< pipe data length backup when stall */
|
||||
uint8_t pipeStall; /*!< pipe is stall */
|
||||
uint8_t ep; /*!< The endpoint number of the pipe. */
|
||||
uint8_t isBusy; /*!< 1: The pipe is transferring packet, 0: The pipe is idle. */
|
||||
} usb_device_cdc_acm_pipe_t;
|
||||
|
||||
/*! @brief Definition of structure for CDC ACM device. */
|
||||
typedef struct _usb_device_cdc_acm_struct
|
||||
{
|
||||
usb_device_handle handle; /*!< The handle of the USB device. */
|
||||
usb_device_class_config_struct_t *configStruct; /*!< The class configure structure. */
|
||||
usb_device_interface_struct_t *commInterfaceHandle; /*!< The CDC communication interface handle. */
|
||||
usb_device_interface_struct_t *dataInterfaceHandle; /*!< The CDC data interface handle. */
|
||||
usb_device_cdc_acm_pipe_t bulkIn; /*!< The bulk in pipe for sending packet to host. */
|
||||
usb_device_cdc_acm_pipe_t bulkOut; /*!< The bulk out pipe for receiving packet from host. */
|
||||
usb_device_cdc_acm_pipe_t interruptIn; /*!< The interrupt in pipe for notifying the device state to host. */
|
||||
uint8_t configuration; /*!< The current configuration value. */
|
||||
uint8_t interfaceNumber; /*!< The current interface number. */
|
||||
uint8_t alternate; /*!< The alternate setting value of the interface. */
|
||||
uint8_t hasSentState; /*!< 1: The device has primed the state in interrupt pipe, 0: Not primed the state. */
|
||||
} usb_device_cdc_acm_struct_t;
|
||||
|
||||
/*******************************************************************************
|
||||
* API
|
||||
******************************************************************************/
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* @name USB CDC ACM Class Driver
|
||||
* @{
|
||||
*/
|
||||
/*!
|
||||
* @brief Initializes the USB CDC ACM class.
|
||||
*
|
||||
* This function obtains a USB device handle according to the controller ID, initializes the CDC ACM class
|
||||
* with the class configure parameters and creates the mutex for each pipe.
|
||||
*
|
||||
* @param controllerId The ID of the controller. The value can be chosen from the kUSB_ControllerKhci0,
|
||||
* kUSB_ControllerKhci1, kUSB_ControllerEhci0, or kUSB_ControllerEhci1.
|
||||
* @param config The user configuration structure of type usb_device_class_config_struct_t. The user
|
||||
* populates the members of this structure and passes the pointer of this structure
|
||||
* into this function.
|
||||
* @param handle It is out parameter. The class handle of the CDC ACM class.
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
* @retval kStatus_USB_Success The CDC ACM class is initialized successfully.
|
||||
* @retval kStatus_USB_Busy No CDC ACM device handle available for allocation.
|
||||
* @retval kStatus_USB_InvalidHandle The CDC ACM device handle allocation failure.
|
||||
* @retval kStatus_USB_InvalidParameter The USB device handle allocation failure.
|
||||
*/
|
||||
extern usb_status_t USB_DeviceCdcAcmInit(uint8_t controllerId,
|
||||
usb_device_class_config_struct_t *config,
|
||||
class_handle_t *handle);
|
||||
/*!
|
||||
* @brief Deinitializes the USB CDC ACM class.
|
||||
*
|
||||
* This function destroys the mutex for each pipe, deinitializes each endpoint of the CDC ACM class and frees
|
||||
* the CDC ACM class handle.
|
||||
*
|
||||
* @param handle The class handle of the CDC ACM class.
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
* @retval kStatus_USB_Success The CDC ACM class is de-initialized successfully.
|
||||
* @retval kStatus_USB_Error The endpoint deinitialization failure.
|
||||
* @retval kStatus_USB_InvalidHandle The CDC ACM device handle or the CDC ACM class handle is invalid.
|
||||
* @retval kStatus_USB_InvalidParameter The endpoint number of the CDC ACM class handle is invalid.
|
||||
*/
|
||||
extern usb_status_t USB_DeviceCdcAcmDeinit(class_handle_t handle);
|
||||
/*!
|
||||
* @brief Handles the CDC ACM class event.
|
||||
*
|
||||
* This function responds to various events including the common device events and the class-specific events.
|
||||
* For class-specific events, it calls the class callback defined in the application to deal with the class-specific
|
||||
* event.
|
||||
*
|
||||
* @param handle The class handle of the CDC ACM class.
|
||||
* @param event The event type.
|
||||
* @param param The class handle of the CDC ACM class.
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
* @retval kStatus_USB_Success The CDC ACM class is de-initialized successfully.
|
||||
* @retval kStatus_USB_Error The configure structure of the CDC ACM class handle is invalid.
|
||||
* @retval kStatus_USB_InvalidHandle The CDC ACM device handle or the CDC ACM class handle is invalid.
|
||||
* @retval kStatus_USB_InvalidParameter The endpoint number of the CDC ACM class handle is invalid.
|
||||
* @retval Others The error code returned by class callback in application.
|
||||
*/
|
||||
extern usb_status_t USB_DeviceCdcAcmEvent(void *handle, uint32_t event, void *param);
|
||||
|
||||
/*!
|
||||
* @brief Primes the endpoint to send packet to host.
|
||||
*
|
||||
* This function checks whether the endpoint is sending packet, then it primes the endpoint
|
||||
* with the buffer address and the buffer length if the pipe is not busy. Otherwise, it ignores this transfer by
|
||||
* returning an error code.
|
||||
*
|
||||
* @param handle The class handle of the CDC ACM class.
|
||||
* @param ep The endpoint number of the transfer.
|
||||
* @param buffer The pointer to the buffer to be transferred.
|
||||
* @param length The length of the buffer to be transferred.
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
* @retval kStatus_USB_Success Prime to send packet successfully.
|
||||
* @retval kStatus_USB_Busy The endpoint is busy in transferring.
|
||||
* @retval kStatus_USB_InvalidHandle The CDC ACM device handle or the CDC ACM class handle is invalid.
|
||||
* @retval kStatus_USB_ControllerNotFound The controller interface is invalid.
|
||||
*
|
||||
* @note The function can only be called in the same context.
|
||||
*/
|
||||
extern usb_status_t USB_DeviceCdcAcmSend(class_handle_t handle, uint8_t ep, uint8_t *buffer, uint32_t length);
|
||||
/*!
|
||||
* @brief Primes the endpoint to receive packet from host.
|
||||
*
|
||||
* This function checks whether the endpoint is receiving packet, then it primes the endpoint
|
||||
* with the buffer address and the buffer length if the pipe is not busy. Otherwise, it ignores this transfer by
|
||||
* returning an error code.
|
||||
*
|
||||
* @param handle The class handle of the CDC ACM class.
|
||||
* @param ep The endpoint number of the transfer.
|
||||
* @param buffer The pointer to the buffer to be transferred.
|
||||
* @param length The length of the buffer to be transferred.
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
* @retval kStatus_USB_Success Prime to receive packet successfully.
|
||||
* @retval kStatus_USB_Busy The endpoint is busy in transferring.
|
||||
* @retval kStatus_USB_InvalidHandle The CDC ACM device handle or the CDC ACM class handle is invalid.
|
||||
* @retval kStatus_USB_ControllerNotFound The controller interface is invalid.
|
||||
*
|
||||
* @note The function can only be called in the same context.
|
||||
*/
|
||||
extern usb_status_t USB_DeviceCdcAcmRecv(class_handle_t handle, uint8_t ep, uint8_t *buffer, uint32_t length);
|
||||
|
||||
/*! @}*/
|
||||
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
#endif
|
||||
|
||||
/*! @}*/
|
||||
|
||||
#endif /* _USB_DEVICE_CDC_ACM_H_ */
|
@@ -1,952 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2015 - 2016, Freescale Semiconductor, Inc.
|
||||
* Copyright 2016 NXP
|
||||
* All rights reserved.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
#include "usb_device_config.h"
|
||||
#include "usb.h"
|
||||
|
||||
#include "usb_device.h"
|
||||
#include "usb_device_dci.h"
|
||||
#include "usb_device_class.h"
|
||||
#include "usb_device_ch9.h"
|
||||
#if ((defined(USB_DEVICE_CONFIG_NUM)) && (USB_DEVICE_CONFIG_NUM > 0U))
|
||||
/*******************************************************************************
|
||||
* Definitions
|
||||
******************************************************************************/
|
||||
|
||||
/*!
|
||||
* @brief Standard request callback function typedef.
|
||||
*
|
||||
* This function is used to handle the standard request.
|
||||
*
|
||||
* @param handle The device handle. It equals the value returned from USB_DeviceInit.
|
||||
* @param setup The pointer of the setup packet.
|
||||
* @param buffer It is an out parameter, is used to save the buffer address to response the host's request.
|
||||
* @param length It is an out parameter, the data length.
|
||||
*
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
*/
|
||||
typedef usb_status_t (*usb_standard_request_callback_t)(usb_device_common_class_struct_t *classHandle,
|
||||
usb_setup_struct_t *setup,
|
||||
uint8_t **buffer,
|
||||
uint32_t *length);
|
||||
|
||||
/*******************************************************************************
|
||||
* Prototypes
|
||||
******************************************************************************/
|
||||
|
||||
static usb_status_t USB_DeviceCh9GetStatus(usb_device_common_class_struct_t *classHandle,
|
||||
usb_setup_struct_t *setup,
|
||||
uint8_t **buffer,
|
||||
uint32_t *length);
|
||||
static usb_status_t USB_DeviceCh9SetClearFeature(usb_device_common_class_struct_t *classHandle,
|
||||
usb_setup_struct_t *setup,
|
||||
uint8_t **buffer,
|
||||
uint32_t *length);
|
||||
|
||||
static usb_status_t USB_DeviceCh9SetAddress(usb_device_common_class_struct_t *classHandle,
|
||||
usb_setup_struct_t *setup,
|
||||
uint8_t **buffer,
|
||||
uint32_t *length);
|
||||
static usb_status_t USB_DeviceCh9GetDescriptor(usb_device_common_class_struct_t *classHandle,
|
||||
usb_setup_struct_t *setup,
|
||||
uint8_t **buffer,
|
||||
uint32_t *length);
|
||||
static usb_status_t USB_DeviceCh9GetConfiguration(usb_device_common_class_struct_t *classHandle,
|
||||
usb_setup_struct_t *setup,
|
||||
uint8_t **buffer,
|
||||
uint32_t *length);
|
||||
static usb_status_t USB_DeviceCh9SetConfiguration(usb_device_common_class_struct_t *classHandle,
|
||||
usb_setup_struct_t *setup,
|
||||
uint8_t **buffer,
|
||||
uint32_t *length);
|
||||
static usb_status_t USB_DeviceCh9GetInterface(usb_device_common_class_struct_t *classHandle,
|
||||
usb_setup_struct_t *setup,
|
||||
uint8_t **buffer,
|
||||
uint32_t *length);
|
||||
static usb_status_t USB_DeviceCh9SetInterface(usb_device_common_class_struct_t *classHandle,
|
||||
usb_setup_struct_t *setup,
|
||||
uint8_t **buffer,
|
||||
uint32_t *length);
|
||||
static usb_status_t USB_DeviceCh9SynchFrame(usb_device_common_class_struct_t *classHandle,
|
||||
usb_setup_struct_t *setup,
|
||||
uint8_t **buffer,
|
||||
uint32_t *length);
|
||||
|
||||
/*******************************************************************************
|
||||
* Variables
|
||||
******************************************************************************/
|
||||
|
||||
/* The function list to handle the standard request. */
|
||||
static const usb_standard_request_callback_t s_UsbDeviceStandardRequest[] = {
|
||||
USB_DeviceCh9GetStatus,
|
||||
USB_DeviceCh9SetClearFeature,
|
||||
(usb_standard_request_callback_t)NULL,
|
||||
USB_DeviceCh9SetClearFeature,
|
||||
(usb_standard_request_callback_t)NULL,
|
||||
USB_DeviceCh9SetAddress,
|
||||
USB_DeviceCh9GetDescriptor,
|
||||
(usb_standard_request_callback_t)NULL,
|
||||
USB_DeviceCh9GetConfiguration,
|
||||
USB_DeviceCh9SetConfiguration,
|
||||
USB_DeviceCh9GetInterface,
|
||||
USB_DeviceCh9SetInterface,
|
||||
USB_DeviceCh9SynchFrame,
|
||||
};
|
||||
|
||||
/*******************************************************************************
|
||||
* Code
|
||||
******************************************************************************/
|
||||
|
||||
/*!
|
||||
* @brief Handle get status request.
|
||||
*
|
||||
* This function is used to handle get status request.
|
||||
*
|
||||
* @param handle The device handle. It equals the value returned from USB_DeviceInit.
|
||||
* @param setup The pointer of the setup packet.
|
||||
* @param buffer It is an out parameter, is used to save the buffer address to response the host's request.
|
||||
* @param length It is an out parameter, the data length.
|
||||
*
|
||||
* @retval kStatus_USB_Success The request is handled successfully.
|
||||
* @retval kStatus_USB_InvalidRequest The request can not be handle in current device state,
|
||||
* or, the request is unsupported.
|
||||
*/
|
||||
static usb_status_t USB_DeviceCh9GetStatus(usb_device_common_class_struct_t *classHandle,
|
||||
usb_setup_struct_t *setup,
|
||||
uint8_t **buffer,
|
||||
uint32_t *length)
|
||||
{
|
||||
usb_status_t error = kStatus_USB_InvalidRequest;
|
||||
uint8_t state;
|
||||
|
||||
USB_DeviceGetStatus(classHandle->handle, kUSB_DeviceStatusDeviceState, &state);
|
||||
|
||||
if ((kUSB_DeviceStateAddress != state) && (kUSB_DeviceStateConfigured != state))
|
||||
{
|
||||
return error;
|
||||
}
|
||||
|
||||
if ((setup->bmRequestType & USB_REQUEST_TYPE_RECIPIENT_MASK) == USB_REQUEST_TYPE_RECIPIENT_DEVICE)
|
||||
{
|
||||
#if (defined(USB_DEVICE_CONFIG_OTG) && (USB_DEVICE_CONFIG_OTG))
|
||||
if (setup->wIndex == USB_REQUEST_STANDARD_GET_STATUS_OTG_STATUS_SELECTOR)
|
||||
{
|
||||
error =
|
||||
USB_DeviceGetStatus(classHandle->handle, kUSB_DeviceStatusOtg, &classHandle->standardTranscationBuffer);
|
||||
classHandle->standardTranscationBuffer = USB_SHORT_TO_LITTLE_ENDIAN(classHandle->standardTranscationBuffer);
|
||||
/* The device status length must be USB_DEVICE_STATUS_SIZE. */
|
||||
*length = 1;
|
||||
}
|
||||
else /* Get the device status */
|
||||
{
|
||||
#endif
|
||||
error = USB_DeviceGetStatus(classHandle->handle, kUSB_DeviceStatusDevice,
|
||||
&classHandle->standardTranscationBuffer);
|
||||
classHandle->standardTranscationBuffer =
|
||||
classHandle->standardTranscationBuffer & USB_GET_STATUS_DEVICE_MASK;
|
||||
classHandle->standardTranscationBuffer = USB_SHORT_TO_LITTLE_ENDIAN(classHandle->standardTranscationBuffer);
|
||||
/* The device status length must be USB_DEVICE_STATUS_SIZE. */
|
||||
*length = USB_DEVICE_STATUS_SIZE;
|
||||
#if (defined(USB_DEVICE_CONFIG_OTG) && (USB_DEVICE_CONFIG_OTG))
|
||||
}
|
||||
#endif
|
||||
}
|
||||
else if ((setup->bmRequestType & USB_REQUEST_TYPE_RECIPIENT_MASK) == USB_REQUEST_TYPE_RECIPIENT_INTERFACE)
|
||||
{
|
||||
/* Get the interface status */
|
||||
error = kStatus_USB_Success;
|
||||
classHandle->standardTranscationBuffer = 0U;
|
||||
/* The interface status length must be USB_INTERFACE_STATUS_SIZE. */
|
||||
*length = USB_INTERFACE_STATUS_SIZE;
|
||||
}
|
||||
else if ((setup->bmRequestType & USB_REQUEST_TYPE_RECIPIENT_MASK) == USB_REQUEST_TYPE_RECIPIENT_ENDPOINT)
|
||||
{
|
||||
/* Get the endpoint status */
|
||||
usb_device_endpoint_status_struct_t endpointStatus;
|
||||
endpointStatus.endpointAddress = (uint8_t)setup->wIndex;
|
||||
endpointStatus.endpointStatus = kUSB_DeviceEndpointStateIdle;
|
||||
error = USB_DeviceGetStatus(classHandle->handle, kUSB_DeviceStatusEndpoint, &endpointStatus);
|
||||
classHandle->standardTranscationBuffer = endpointStatus.endpointStatus & USB_GET_STATUS_ENDPOINT_MASK;
|
||||
classHandle->standardTranscationBuffer = USB_SHORT_TO_LITTLE_ENDIAN(classHandle->standardTranscationBuffer);
|
||||
/* The endpoint status length must be USB_INTERFACE_STATUS_SIZE. */
|
||||
*length = USB_ENDPOINT_STATUS_SIZE;
|
||||
}
|
||||
else
|
||||
{
|
||||
}
|
||||
*buffer = (uint8_t *)&classHandle->standardTranscationBuffer;
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Handle set or clear device feature request.
|
||||
*
|
||||
* This function is used to handle set or clear device feature request.
|
||||
*
|
||||
* @param handle The device handle. It equals the value returned from USB_DeviceInit.
|
||||
* @param setup The pointer of the setup packet.
|
||||
* @param buffer It is an out parameter, is used to save the buffer address to response the host's request.
|
||||
* @param length It is an out parameter, the data length.
|
||||
*
|
||||
* @retval kStatus_USB_Success The request is handled successfully.
|
||||
* @retval kStatus_USB_InvalidRequest The request can not be handle in current device state,
|
||||
* or, the request is unsupported.
|
||||
*/
|
||||
static usb_status_t USB_DeviceCh9SetClearFeature(usb_device_common_class_struct_t *classHandle,
|
||||
usb_setup_struct_t *setup,
|
||||
uint8_t **buffer,
|
||||
uint32_t *length)
|
||||
{
|
||||
usb_status_t error = kStatus_USB_InvalidRequest;
|
||||
uint8_t state;
|
||||
uint8_t isSet = 0U;
|
||||
|
||||
USB_DeviceGetStatus(classHandle->handle, kUSB_DeviceStatusDeviceState, &state);
|
||||
|
||||
if ((kUSB_DeviceStateAddress != state) && (kUSB_DeviceStateConfigured != state))
|
||||
{
|
||||
return error;
|
||||
}
|
||||
|
||||
/* Identify the request is set or clear the feature. */
|
||||
if (USB_REQUEST_STANDARD_SET_FEATURE == setup->bRequest)
|
||||
{
|
||||
isSet = 1U;
|
||||
}
|
||||
|
||||
if ((setup->bmRequestType & USB_REQUEST_TYPE_RECIPIENT_MASK) == USB_REQUEST_TYPE_RECIPIENT_DEVICE)
|
||||
{
|
||||
/* Set or Clear the device feature. */
|
||||
if (USB_REQUEST_STANDARD_FEATURE_SELECTOR_DEVICE_REMOTE_WAKEUP == setup->wValue)
|
||||
{
|
||||
#if ((defined(USB_DEVICE_CONFIG_REMOTE_WAKEUP)) && (USB_DEVICE_CONFIG_REMOTE_WAKEUP > 0U))
|
||||
USB_DeviceSetStatus(classHandle->handle, kUSB_DeviceStatusRemoteWakeup, &isSet);
|
||||
#endif
|
||||
/* Set or Clear the device remote wakeup feature. */
|
||||
error = USB_DeviceClassCallback(classHandle->handle, kUSB_DeviceEventSetRemoteWakeup, &isSet);
|
||||
}
|
||||
#if ((defined(USB_DEVICE_CONFIG_EHCI) && (USB_DEVICE_CONFIG_EHCI > 0U)) || \
|
||||
(defined(USB_DEVICE_CONFIG_LPCIP3511HS) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U))) && \
|
||||
(defined(USB_DEVICE_CONFIG_USB20_TEST_MODE) && (USB_DEVICE_CONFIG_USB20_TEST_MODE > 0U))
|
||||
else if (USB_REQUEST_STANDARD_FEATURE_SELECTOR_DEVICE_TEST_MODE == setup->wValue)
|
||||
{
|
||||
state = kUSB_DeviceStateTestMode;
|
||||
error = USB_DeviceSetStatus(classHandle->handle, kUSB_DeviceStatusDeviceState, &state);
|
||||
}
|
||||
#endif
|
||||
#if (defined(USB_DEVICE_CONFIG_OTG) && (USB_DEVICE_CONFIG_OTG))
|
||||
else if (USB_REQUEST_STANDARD_FEATURE_SELECTOR_B_HNP_ENABLE == setup->wValue)
|
||||
{
|
||||
error = USB_DeviceClassCallback(classHandle->handle, kUSB_DeviceEventSetBHNPEnable, &isSet);
|
||||
}
|
||||
#endif
|
||||
else
|
||||
{
|
||||
}
|
||||
}
|
||||
else if ((setup->bmRequestType & USB_REQUEST_TYPE_RECIPIENT_MASK) == USB_REQUEST_TYPE_RECIPIENT_ENDPOINT)
|
||||
{
|
||||
/* Set or Clear the endpoint feature. */
|
||||
if (USB_REQUEST_STANDARD_FEATURE_SELECTOR_ENDPOINT_HALT == setup->wValue)
|
||||
{
|
||||
if (USB_CONTROL_ENDPOINT == (setup->wIndex & USB_ENDPOINT_NUMBER_MASK))
|
||||
{
|
||||
/* Set or Clear the control endpoint status(halt or not). */
|
||||
if (isSet)
|
||||
{
|
||||
USB_DeviceStallEndpoint(classHandle->handle, (uint8_t)setup->wIndex);
|
||||
}
|
||||
else
|
||||
{
|
||||
USB_DeviceUnstallEndpoint(classHandle->handle, (uint8_t)setup->wIndex);
|
||||
}
|
||||
}
|
||||
|
||||
/* Set or Clear the endpoint status feature. */
|
||||
if (isSet)
|
||||
{
|
||||
error = USB_DeviceClassEvent(classHandle->handle, kUSB_DeviceClassEventSetEndpointHalt, &setup->wIndex);
|
||||
}
|
||||
else
|
||||
{
|
||||
error =
|
||||
USB_DeviceClassEvent(classHandle->handle, kUSB_DeviceClassEventClearEndpointHalt, &setup->wIndex);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
}
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Handle set address request.
|
||||
*
|
||||
* This function is used to handle set address request.
|
||||
*
|
||||
* @param handle The device handle. It equals the value returned from USB_DeviceInit.
|
||||
* @param setup The pointer of the setup packet.
|
||||
* @param buffer It is an out parameter, is used to save the buffer address to response the host's request.
|
||||
* @param length It is an out parameter, the data length.
|
||||
*
|
||||
* @retval kStatus_USB_Success The request is handled successfully.
|
||||
* @retval kStatus_USB_InvalidRequest The request can not be handle in current device state.
|
||||
*/
|
||||
static usb_status_t USB_DeviceCh9SetAddress(usb_device_common_class_struct_t *classHandle,
|
||||
usb_setup_struct_t *setup,
|
||||
uint8_t **buffer,
|
||||
uint32_t *length)
|
||||
{
|
||||
usb_status_t error = kStatus_USB_InvalidRequest;
|
||||
uint8_t state;
|
||||
|
||||
USB_DeviceGetStatus(classHandle->handle, kUSB_DeviceStatusDeviceState, &state);
|
||||
|
||||
if ((kUSB_DeviceStateAddressing != state) && (kUSB_DeviceStateAddress != state) &&
|
||||
(kUSB_DeviceStateDefault != state) && (kUSB_DeviceStateConfigured != state))
|
||||
{
|
||||
return error;
|
||||
}
|
||||
|
||||
if (kUSB_DeviceStateAddressing != state)
|
||||
{
|
||||
/* If the device address is not setting, pass the address and the device state will change to
|
||||
* kUSB_DeviceStateAddressing internally. */
|
||||
state = setup->wValue & 0xFFU;
|
||||
error = USB_DeviceSetStatus(classHandle->handle, kUSB_DeviceStatusAddress, &state);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* If the device address is setting, set device address and the address will be write into the controller
|
||||
* internally. */
|
||||
error = USB_DeviceSetStatus(classHandle->handle, kUSB_DeviceStatusAddress, NULL);
|
||||
/* And then change the device state to kUSB_DeviceStateAddress. */
|
||||
if (kStatus_USB_Success == error)
|
||||
{
|
||||
state = kUSB_DeviceStateAddress;
|
||||
error = USB_DeviceSetStatus(classHandle->handle, kUSB_DeviceStatusDeviceState, &state);
|
||||
}
|
||||
}
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Handle get descriptor request.
|
||||
*
|
||||
* This function is used to handle get descriptor request.
|
||||
*
|
||||
* @param handle The device handle. It equals the value returned from USB_DeviceInit.
|
||||
* @param setup The pointer of the setup packet.
|
||||
* @param buffer It is an out parameter, is used to save the buffer address to response the host's request.
|
||||
* @param length It is an out parameter, the data length.
|
||||
*
|
||||
* @retval kStatus_USB_Success The request is handled successfully.
|
||||
* @retval kStatus_USB_InvalidRequest The request can not be handle in current device state,
|
||||
* or, the request is unsupported.
|
||||
*/
|
||||
static usb_status_t USB_DeviceCh9GetDescriptor(usb_device_common_class_struct_t *classHandle,
|
||||
usb_setup_struct_t *setup,
|
||||
uint8_t **buffer,
|
||||
uint32_t *length)
|
||||
{
|
||||
usb_device_get_descriptor_common_union_t commonDescriptor;
|
||||
usb_status_t error = kStatus_USB_InvalidRequest;
|
||||
uint8_t state;
|
||||
uint8_t descriptorType = (uint8_t)((setup->wValue & 0xFF00U) >> 8U);
|
||||
uint8_t descriptorIndex = (uint8_t)((setup->wValue & 0x00FFU));
|
||||
|
||||
USB_DeviceGetStatus(classHandle->handle, kUSB_DeviceStatusDeviceState, &state);
|
||||
|
||||
if ((kUSB_DeviceStateAddress != state) && (kUSB_DeviceStateConfigured != state) &&
|
||||
(kUSB_DeviceStateDefault != state))
|
||||
{
|
||||
return error;
|
||||
}
|
||||
commonDescriptor.commonDescriptor.length = setup->wLength;
|
||||
if (USB_DESCRIPTOR_TYPE_DEVICE == descriptorType)
|
||||
{
|
||||
/* Get the device descriptor */
|
||||
error = USB_DeviceClassCallback(classHandle->handle, kUSB_DeviceEventGetDeviceDescriptor,
|
||||
&commonDescriptor.deviceDescriptor);
|
||||
}
|
||||
else if (USB_DESCRIPTOR_TYPE_CONFIGURE == descriptorType)
|
||||
{
|
||||
/* Get the configuration descriptor */
|
||||
commonDescriptor.configurationDescriptor.configuration = descriptorIndex;
|
||||
error = USB_DeviceClassCallback(classHandle->handle, kUSB_DeviceEventGetConfigurationDescriptor,
|
||||
&commonDescriptor.configurationDescriptor);
|
||||
}
|
||||
else if (USB_DESCRIPTOR_TYPE_STRING == descriptorType)
|
||||
{
|
||||
/* Get the string descriptor */
|
||||
commonDescriptor.stringDescriptor.stringIndex = descriptorIndex;
|
||||
commonDescriptor.stringDescriptor.languageId = setup->wIndex;
|
||||
error = USB_DeviceClassCallback(classHandle->handle, kUSB_DeviceEventGetStringDescriptor,
|
||||
&commonDescriptor.stringDescriptor);
|
||||
}
|
||||
#if (defined(USB_DEVICE_CONFIG_HID) && (USB_DEVICE_CONFIG_HID > 0U))
|
||||
else if (USB_DESCRIPTOR_TYPE_HID == descriptorType)
|
||||
{
|
||||
/* Get the hid descriptor */
|
||||
commonDescriptor.hidDescriptor.interfaceNumber = setup->wIndex;
|
||||
error = USB_DeviceClassCallback(classHandle->handle, kUSB_DeviceEventGetHidDescriptor,
|
||||
&commonDescriptor.hidDescriptor);
|
||||
}
|
||||
else if (USB_DESCRIPTOR_TYPE_HID_REPORT == descriptorType)
|
||||
{
|
||||
/* Get the hid report descriptor */
|
||||
commonDescriptor.hidReportDescriptor.interfaceNumber = setup->wIndex;
|
||||
error = USB_DeviceClassCallback(classHandle->handle, kUSB_DeviceEventGetHidReportDescriptor,
|
||||
&commonDescriptor.hidReportDescriptor);
|
||||
}
|
||||
else if (USB_DESCRIPTOR_TYPE_HID_PHYSICAL == descriptorType)
|
||||
{
|
||||
/* Get the hid physical descriptor */
|
||||
commonDescriptor.hidPhysicalDescriptor.index = descriptorIndex;
|
||||
commonDescriptor.hidPhysicalDescriptor.interfaceNumber = setup->wIndex;
|
||||
error = USB_DeviceClassCallback(classHandle->handle, kUSB_DeviceEventGetHidPhysicalDescriptor,
|
||||
&commonDescriptor.hidPhysicalDescriptor);
|
||||
}
|
||||
#endif
|
||||
#if (defined(USB_DEVICE_CONFIG_CV_TEST) && (USB_DEVICE_CONFIG_CV_TEST > 0U))
|
||||
else if (USB_DESCRIPTOR_TYPE_DEVICE_QUALITIER == descriptorType)
|
||||
{
|
||||
/* Get the device descriptor */
|
||||
error = USB_DeviceClassCallback(classHandle->handle, kUSB_DeviceEventGetDeviceQualifierDescriptor,
|
||||
&commonDescriptor.deviceDescriptor);
|
||||
}
|
||||
#endif
|
||||
#if (defined(USB_DEVICE_CONFIG_LPM_L1) && (USB_DEVICE_CONFIG_LPM_L1 > 0U))
|
||||
else if (USB_DESCRIPTOR_TYPE_BOS == descriptorType)
|
||||
{
|
||||
/* Get the configuration descriptor */
|
||||
commonDescriptor.configurationDescriptor.configuration = descriptorIndex;
|
||||
error = USB_DeviceClassCallback(classHandle->handle, kUSB_DeviceEventGetBOSDescriptor,
|
||||
&commonDescriptor.configurationDescriptor);
|
||||
}
|
||||
#endif
|
||||
else
|
||||
{
|
||||
}
|
||||
*buffer = commonDescriptor.commonDescriptor.buffer;
|
||||
*length = commonDescriptor.commonDescriptor.length;
|
||||
return error;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Handle get current configuration request.
|
||||
*
|
||||
* This function is used to handle get current configuration request.
|
||||
*
|
||||
* @param handle The device handle. It equals the value returned from USB_DeviceInit.
|
||||
* @param setup The pointer of the setup packet.
|
||||
* @param buffer It is an out parameter, is used to save the buffer address to response the host's request.
|
||||
* @param length It is an out parameter, the data length.
|
||||
*
|
||||
* @retval kStatus_USB_Success The request is handled successfully.
|
||||
* @retval kStatus_USB_InvalidRequest The request can not be handle in current device state,
|
||||
* or, the request is unsupported.
|
||||
*/
|
||||
static usb_status_t USB_DeviceCh9GetConfiguration(usb_device_common_class_struct_t *classHandle,
|
||||
usb_setup_struct_t *setup,
|
||||
uint8_t **buffer,
|
||||
uint32_t *length)
|
||||
{
|
||||
uint8_t state;
|
||||
|
||||
USB_DeviceGetStatus(classHandle->handle, kUSB_DeviceStatusDeviceState, &state);
|
||||
|
||||
if ((kUSB_DeviceStateAddress != state) && ((kUSB_DeviceStateConfigured != state)))
|
||||
{
|
||||
return kStatus_USB_InvalidRequest;
|
||||
}
|
||||
|
||||
*length = USB_CONFIGURE_SIZE;
|
||||
*buffer = (uint8_t *)&classHandle->standardTranscationBuffer;
|
||||
return USB_DeviceClassCallback(classHandle->handle, kUSB_DeviceEventGetConfiguration,
|
||||
&classHandle->standardTranscationBuffer);
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Handle set current configuration request.
|
||||
*
|
||||
* This function is used to handle set current configuration request.
|
||||
*
|
||||
* @param handle The device handle. It equals the value returned from USB_DeviceInit.
|
||||
* @param setup The pointer of the setup packet.
|
||||
* @param buffer It is an out parameter, is used to save the buffer address to response the host's request.
|
||||
* @param length It is an out parameter, the data length.
|
||||
*
|
||||
* @retval kStatus_USB_Success The request is handled successfully.
|
||||
* @retval kStatus_USB_InvalidRequest The request can not be handle in current device state,
|
||||
* or, the request is unsupported.
|
||||
*/
|
||||
static usb_status_t USB_DeviceCh9SetConfiguration(usb_device_common_class_struct_t *classHandle,
|
||||
usb_setup_struct_t *setup,
|
||||
uint8_t **buffer,
|
||||
uint32_t *length)
|
||||
{
|
||||
uint8_t state;
|
||||
|
||||
USB_DeviceGetStatus(classHandle->handle, kUSB_DeviceStatusDeviceState, &state);
|
||||
|
||||
if ((kUSB_DeviceStateAddress != state) && (kUSB_DeviceStateConfigured != state))
|
||||
{
|
||||
return kStatus_USB_InvalidRequest;
|
||||
}
|
||||
|
||||
/* The device state is changed to kUSB_DeviceStateConfigured */
|
||||
state = kUSB_DeviceStateConfigured;
|
||||
USB_DeviceSetStatus(classHandle->handle, kUSB_DeviceStatusDeviceState, &state);
|
||||
if (!setup->wValue)
|
||||
{
|
||||
/* If the new configuration is zero, the device state is changed to kUSB_DeviceStateAddress */
|
||||
state = kUSB_DeviceStateAddress;
|
||||
USB_DeviceSetStatus(classHandle->handle, kUSB_DeviceStatusDeviceState, &state);
|
||||
}
|
||||
|
||||
/* Notify the class layer the configuration is changed */
|
||||
USB_DeviceClassEvent(classHandle->handle, kUSB_DeviceClassEventSetConfiguration, &setup->wValue);
|
||||
/* Notify the application the configuration is changed */
|
||||
return USB_DeviceClassCallback(classHandle->handle, kUSB_DeviceEventSetConfiguration, &setup->wValue);
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Handle get the alternate setting of a interface request.
|
||||
*
|
||||
* This function is used to handle get the alternate setting of a interface request.
|
||||
*
|
||||
* @param handle The device handle. It equals the value returned from USB_DeviceInit.
|
||||
* @param setup The pointer of the setup packet.
|
||||
* @param buffer It is an out parameter, is used to save the buffer address to response the host's request.
|
||||
* @param length It is an out parameter, the data length.
|
||||
*
|
||||
* @retval kStatus_USB_Success The request is handled successfully.
|
||||
* @retval kStatus_USB_InvalidRequest The request can not be handle in current device state,
|
||||
* or, the request is unsupported.
|
||||
*/
|
||||
static usb_status_t USB_DeviceCh9GetInterface(usb_device_common_class_struct_t *classHandle,
|
||||
usb_setup_struct_t *setup,
|
||||
uint8_t **buffer,
|
||||
uint32_t *length)
|
||||
{
|
||||
usb_status_t error = kStatus_USB_InvalidRequest;
|
||||
uint8_t state;
|
||||
|
||||
USB_DeviceGetStatus(classHandle->handle, kUSB_DeviceStatusDeviceState, &state);
|
||||
|
||||
if (state != kUSB_DeviceStateConfigured)
|
||||
{
|
||||
return error;
|
||||
}
|
||||
*length = USB_INTERFACE_SIZE;
|
||||
*buffer = (uint8_t *)&classHandle->standardTranscationBuffer;
|
||||
classHandle->standardTranscationBuffer = (uint16_t)(((uint32_t)setup->wIndex & 0xFFU) << 8U);
|
||||
/* The Bit[15~8] is used to save the interface index, and the alternate setting will be saved in Bit[7~0] by
|
||||
* application. */
|
||||
error = USB_DeviceClassCallback(classHandle->handle, kUSB_DeviceEventGetInterface,
|
||||
&classHandle->standardTranscationBuffer);
|
||||
classHandle->standardTranscationBuffer = USB_SHORT_TO_LITTLE_ENDIAN(classHandle->standardTranscationBuffer);
|
||||
return error;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Handle set the alternate setting of a interface request.
|
||||
*
|
||||
* This function is used to handle set the alternate setting of a interface request.
|
||||
*
|
||||
* @param handle The device handle. It equals the value returned from USB_DeviceInit.
|
||||
* @param setup The pointer of the setup packet.
|
||||
* @param buffer It is an out parameter, is used to save the buffer address to response the host's request.
|
||||
* @param length It is an out parameter, the data length.
|
||||
*
|
||||
* @retval kStatus_USB_Success The request is handled successfully.
|
||||
* @retval kStatus_USB_InvalidRequest The request can not be handle in current device state,
|
||||
* or, the request is unsupported.
|
||||
*/
|
||||
static usb_status_t USB_DeviceCh9SetInterface(usb_device_common_class_struct_t *classHandle,
|
||||
usb_setup_struct_t *setup,
|
||||
uint8_t **buffer,
|
||||
uint32_t *length)
|
||||
{
|
||||
uint8_t state;
|
||||
|
||||
USB_DeviceGetStatus(classHandle->handle, kUSB_DeviceStatusDeviceState, &state);
|
||||
|
||||
if (state != kUSB_DeviceStateConfigured)
|
||||
{
|
||||
return kStatus_USB_InvalidRequest;
|
||||
}
|
||||
classHandle->standardTranscationBuffer = ((setup->wIndex & 0xFFU) << 8U) | (setup->wValue & 0xFFU);
|
||||
/* Notify the class driver the alternate setting of the interface is changed. */
|
||||
/* The Bit[15~8] is used to save the interface index, and the alternate setting is saved in Bit[7~0]. */
|
||||
USB_DeviceClassEvent(classHandle->handle, kUSB_DeviceClassEventSetInterface,
|
||||
&classHandle->standardTranscationBuffer);
|
||||
/* Notify the application the alternate setting of the interface is changed. */
|
||||
/* The Bit[15~8] is used to save the interface index, and the alternate setting will is saved in Bit[7~0]. */
|
||||
return USB_DeviceClassCallback(classHandle->handle, kUSB_DeviceEventSetInterface,
|
||||
&classHandle->standardTranscationBuffer);
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Handle get sync frame request.
|
||||
*
|
||||
* This function is used to handle get sync frame request.
|
||||
*
|
||||
* @param handle The device handle. It equals the value returned from USB_DeviceInit.
|
||||
* @param setup The pointer of the setup packet.
|
||||
* @param buffer It is an out parameter, is used to save the buffer address to response the host's request.
|
||||
* @param length It is an out parameter, the data length.
|
||||
*
|
||||
* @retval kStatus_USB_Success The request is handled successfully.
|
||||
* @retval kStatus_USB_InvalidRequest The request can not be handle in current device state,
|
||||
* or, the request is unsupported.
|
||||
*/
|
||||
static usb_status_t USB_DeviceCh9SynchFrame(usb_device_common_class_struct_t *classHandle,
|
||||
usb_setup_struct_t *setup,
|
||||
uint8_t **buffer,
|
||||
uint32_t *length)
|
||||
{
|
||||
usb_status_t error = kStatus_USB_InvalidRequest;
|
||||
uint8_t state;
|
||||
|
||||
USB_DeviceGetStatus(classHandle->handle, kUSB_DeviceStatusDeviceState, &state);
|
||||
|
||||
if (state != kUSB_DeviceStateConfigured)
|
||||
{
|
||||
return error;
|
||||
}
|
||||
|
||||
classHandle->standardTranscationBuffer = USB_SHORT_FROM_LITTLE_ENDIAN(setup->wIndex);
|
||||
/* Get the sync frame value */
|
||||
error =
|
||||
USB_DeviceGetStatus(classHandle->handle, kUSB_DeviceStatusSynchFrame, &classHandle->standardTranscationBuffer);
|
||||
*buffer = (uint8_t *)&classHandle->standardTranscationBuffer;
|
||||
*length = sizeof(classHandle->standardTranscationBuffer);
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Send the response to the host.
|
||||
*
|
||||
* This function is used to send the response to the host.
|
||||
*
|
||||
* There are two cases this function will be called.
|
||||
* Case one when a setup packet is received in control endpoint callback function:
|
||||
* 1. If there is not data phase in the setup transfer, the function will prime an IN transfer with the data
|
||||
* length is zero for status phase.
|
||||
* 2. If there is an IN data phase, the function will prime an OUT transfer with the actual length to need to
|
||||
* send for data phase. And then prime an IN transfer with the data length is zero for status phase.
|
||||
* 3. If there is an OUT data phase, the function will prime an IN transfer with the actual length to want to
|
||||
* receive for data phase.
|
||||
*
|
||||
* Case two when is not a setup packet received in control endpoint callback function:
|
||||
* 1. The function will prime an IN transfer with data length is zero for status phase.
|
||||
*
|
||||
* @param handle The device handle. It equals the value returned from USB_DeviceInit.
|
||||
* @param setup The pointer of the setup packet.
|
||||
* @param error The error code returned from the standard request function.
|
||||
* @param stage The stage of the control transfer.
|
||||
* @param buffer It is an out parameter, is used to save the buffer address to response the host's request.
|
||||
* @param length It is an out parameter, the data length.
|
||||
*
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
*/
|
||||
static usb_status_t USB_DeviceControlCallbackFeedback(usb_device_handle handle,
|
||||
usb_setup_struct_t *setup,
|
||||
usb_status_t error,
|
||||
usb_device_control_read_write_sequence_t stage,
|
||||
uint8_t **buffer,
|
||||
uint32_t *length)
|
||||
{
|
||||
usb_status_t errorCode = kStatus_USB_Error;
|
||||
uint8_t direction = USB_IN;
|
||||
|
||||
if (kStatus_USB_InvalidRequest == error)
|
||||
{
|
||||
/* Stall the control pipe when the request is unsupported. */
|
||||
if ((!((setup->bmRequestType & USB_REQUEST_TYPE_TYPE_MASK) == USB_REQUEST_TYPE_TYPE_STANDARD)) &&
|
||||
((setup->bmRequestType & USB_REQUEST_TYPE_DIR_MASK) == USB_REQUEST_TYPE_DIR_OUT) && (setup->wLength) &&
|
||||
(kUSB_DeviceControlPipeSetupStage == stage))
|
||||
{
|
||||
direction = USB_OUT;
|
||||
}
|
||||
errorCode = USB_DeviceStallEndpoint(
|
||||
handle,
|
||||
(USB_CONTROL_ENDPOINT) | (uint8_t)((uint32_t)direction << USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT));
|
||||
}
|
||||
else
|
||||
{
|
||||
if (*length > setup->wLength)
|
||||
{
|
||||
*length = setup->wLength;
|
||||
}
|
||||
errorCode = USB_DeviceSendRequest(handle, (USB_CONTROL_ENDPOINT), *buffer, *length);
|
||||
|
||||
if ((kStatus_USB_Success == errorCode) &&
|
||||
(USB_REQUEST_TYPE_DIR_IN == (setup->bmRequestType & USB_REQUEST_TYPE_DIR_MASK)))
|
||||
{
|
||||
errorCode = USB_DeviceRecvRequest(handle, (USB_CONTROL_ENDPOINT), (uint8_t *)NULL, 0U);
|
||||
}
|
||||
}
|
||||
return errorCode;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Control endpoint callback function.
|
||||
*
|
||||
* This callback function is used to notify uplayer the transfser result of a transfer.
|
||||
* This callback pointer is passed when a specified endpoint initialized by calling API USB_DeviceInitEndpoint.
|
||||
*
|
||||
* @param handle The device handle. It equals the value returned from USB_DeviceInit.
|
||||
* @param message The result of a transfer, includes transfer buffer, transfer length and whether is in setup
|
||||
* phase for control pipe.
|
||||
* @param callbackParam The parameter for this callback. It is same with
|
||||
* usb_device_endpoint_callback_struct_t::callbackParam.
|
||||
*
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
*/
|
||||
usb_status_t USB_DeviceControlCallback(usb_device_handle handle,
|
||||
usb_device_endpoint_callback_message_struct_t *message,
|
||||
void *callbackParam)
|
||||
{
|
||||
usb_setup_struct_t *deviceSetup, *setup;
|
||||
usb_device_common_class_struct_t *classHandle;
|
||||
uint8_t *buffer = (uint8_t *)NULL;
|
||||
uint32_t length = 0U;
|
||||
usb_status_t error = kStatus_USB_InvalidRequest;
|
||||
uint8_t state;
|
||||
|
||||
if ((0xFFFFFFFFU == message->length) || (NULL == callbackParam))
|
||||
{
|
||||
return error;
|
||||
}
|
||||
|
||||
classHandle = (usb_device_common_class_struct_t *)callbackParam;
|
||||
deviceSetup = (usb_setup_struct_t *)&classHandle->setupBuffer[0];
|
||||
USB_DeviceGetStatus(handle, kUSB_DeviceStatusDeviceState, &state);
|
||||
|
||||
if (message->isSetup)
|
||||
{
|
||||
if ((USB_SETUP_PACKET_SIZE != message->length) || (NULL == message->buffer))
|
||||
{
|
||||
/* If a invalid setup is received, the control pipes should be de-init and init again.
|
||||
* Due to the IP can not meet this require, it is reserved for feature.
|
||||
*/
|
||||
/*
|
||||
USB_DeviceDeinitEndpoint(handle,
|
||||
USB_CONTROL_ENDPOINT | (USB_IN << USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT));
|
||||
USB_DeviceDeinitEndpoint(handle,
|
||||
USB_CONTROL_ENDPOINT | (USB_OUT << USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT));
|
||||
USB_DeviceControlPipeInit(handle, callbackParam);
|
||||
*/
|
||||
return error;
|
||||
}
|
||||
/* Receive a setup request */
|
||||
setup = (usb_setup_struct_t *)(message->buffer);
|
||||
|
||||
/* Copy the setup packet to the application buffer */
|
||||
deviceSetup->wValue = USB_SHORT_FROM_LITTLE_ENDIAN(setup->wValue);
|
||||
deviceSetup->wIndex = USB_SHORT_FROM_LITTLE_ENDIAN(setup->wIndex);
|
||||
deviceSetup->wLength = USB_SHORT_FROM_LITTLE_ENDIAN(setup->wLength);
|
||||
deviceSetup->bRequest = setup->bRequest;
|
||||
deviceSetup->bmRequestType = setup->bmRequestType;
|
||||
|
||||
if ((deviceSetup->bmRequestType & USB_REQUEST_TYPE_TYPE_MASK) == USB_REQUEST_TYPE_TYPE_STANDARD)
|
||||
{
|
||||
/* Handle the standard request, only handle the request in request array. */
|
||||
if(deviceSetup->bRequest < (sizeof(s_UsbDeviceStandardRequest)/4))
|
||||
{
|
||||
if (s_UsbDeviceStandardRequest[deviceSetup->bRequest] != (usb_standard_request_callback_t)NULL)
|
||||
{
|
||||
error = s_UsbDeviceStandardRequest[deviceSetup->bRequest](classHandle, deviceSetup, &buffer, &length);
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if ((deviceSetup->wLength) &&
|
||||
((deviceSetup->bmRequestType & USB_REQUEST_TYPE_DIR_MASK) == USB_REQUEST_TYPE_DIR_OUT))
|
||||
{
|
||||
/* Class or vendor request with the OUT data phase. */
|
||||
if ((deviceSetup->wLength) &&
|
||||
((deviceSetup->bmRequestType & USB_REQUEST_TYPE_TYPE_CLASS) == USB_REQUEST_TYPE_TYPE_CLASS))
|
||||
{
|
||||
/* Get data buffer to receive the data from the host. */
|
||||
usb_device_control_request_struct_t controlRequest;
|
||||
controlRequest.buffer = (uint8_t *)NULL;
|
||||
controlRequest.isSetup = 1U;
|
||||
controlRequest.setup = deviceSetup;
|
||||
controlRequest.length = deviceSetup->wLength;
|
||||
error = USB_DeviceClassEvent(handle, kUSB_DeviceClassEventClassRequest, &controlRequest);
|
||||
length = controlRequest.length;
|
||||
buffer = controlRequest.buffer;
|
||||
}
|
||||
else if ((deviceSetup->wLength) &&
|
||||
((deviceSetup->bmRequestType & USB_REQUEST_TYPE_TYPE_VENDOR) == USB_REQUEST_TYPE_TYPE_VENDOR))
|
||||
{
|
||||
/* Get data buffer to receive the data from the host. */
|
||||
usb_device_control_request_struct_t controlRequest;
|
||||
controlRequest.buffer = (uint8_t *)NULL;
|
||||
controlRequest.isSetup = 1U;
|
||||
controlRequest.setup = deviceSetup;
|
||||
controlRequest.length = deviceSetup->wLength;
|
||||
error = USB_DeviceClassCallback(handle, kUSB_DeviceEventVendorRequest, &controlRequest);
|
||||
length = controlRequest.length;
|
||||
buffer = controlRequest.buffer;
|
||||
}
|
||||
else
|
||||
{
|
||||
}
|
||||
if (kStatus_USB_Success == error)
|
||||
{
|
||||
/* Prime an OUT transfer */
|
||||
error = USB_DeviceRecvRequest(handle, USB_CONTROL_ENDPOINT, buffer, deviceSetup->wLength);
|
||||
return error;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Class or vendor request with the IN data phase. */
|
||||
if (((deviceSetup->bmRequestType & USB_REQUEST_TYPE_TYPE_CLASS) == USB_REQUEST_TYPE_TYPE_CLASS))
|
||||
{
|
||||
/* Get data buffer to response the host. */
|
||||
usb_device_control_request_struct_t controlRequest;
|
||||
controlRequest.buffer = (uint8_t *)NULL;
|
||||
controlRequest.isSetup = 1U;
|
||||
controlRequest.setup = deviceSetup;
|
||||
controlRequest.length = deviceSetup->wLength;
|
||||
error = USB_DeviceClassEvent(handle, kUSB_DeviceClassEventClassRequest, &controlRequest);
|
||||
length = controlRequest.length;
|
||||
buffer = controlRequest.buffer;
|
||||
}
|
||||
else if (((deviceSetup->bmRequestType & USB_REQUEST_TYPE_TYPE_VENDOR) == USB_REQUEST_TYPE_TYPE_VENDOR))
|
||||
{
|
||||
/* Get data buffer to response the host. */
|
||||
usb_device_control_request_struct_t controlRequest;
|
||||
controlRequest.buffer = (uint8_t *)NULL;
|
||||
controlRequest.isSetup = 1U;
|
||||
controlRequest.setup = deviceSetup;
|
||||
controlRequest.length = deviceSetup->wLength;
|
||||
error = USB_DeviceClassCallback(handle, kUSB_DeviceEventVendorRequest, &controlRequest);
|
||||
length = controlRequest.length;
|
||||
buffer = controlRequest.buffer;
|
||||
}
|
||||
else
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
/* Send the response to the host. */
|
||||
error = USB_DeviceControlCallbackFeedback(handle, deviceSetup, error, kUSB_DeviceControlPipeSetupStage, &buffer,
|
||||
&length);
|
||||
}
|
||||
else if (kUSB_DeviceStateAddressing == state)
|
||||
{
|
||||
/* Set the device address to controller. */
|
||||
error = s_UsbDeviceStandardRequest[deviceSetup->bRequest](classHandle, deviceSetup, &buffer, &length);
|
||||
}
|
||||
#if ((defined(USB_DEVICE_CONFIG_EHCI) && (USB_DEVICE_CONFIG_EHCI > 0U)) || \
|
||||
(defined(USB_DEVICE_CONFIG_LPCIP3511HS) && (USB_DEVICE_CONFIG_LPCIP3511HS > 0U))) && \
|
||||
(defined(USB_DEVICE_CONFIG_USB20_TEST_MODE) && (USB_DEVICE_CONFIG_USB20_TEST_MODE > 0U))
|
||||
else if (kUSB_DeviceStateTestMode == state)
|
||||
{
|
||||
uint8_t portTestControl = (uint8_t)(deviceSetup->wIndex >> 8);
|
||||
/* Set the controller.into test mode. */
|
||||
error = USB_DeviceSetStatus(handle, kUSB_DeviceStatusTestMode, &portTestControl);
|
||||
}
|
||||
#endif
|
||||
else if ((message->length) && (deviceSetup->wLength) &&
|
||||
((deviceSetup->bmRequestType & USB_REQUEST_TYPE_DIR_MASK) == USB_REQUEST_TYPE_DIR_OUT))
|
||||
{
|
||||
if (((deviceSetup->bmRequestType & USB_REQUEST_TYPE_TYPE_CLASS) == USB_REQUEST_TYPE_TYPE_CLASS))
|
||||
{
|
||||
/* Data received in OUT phase, and notify the class driver. */
|
||||
usb_device_control_request_struct_t controlRequest;
|
||||
controlRequest.buffer = message->buffer;
|
||||
controlRequest.isSetup = 0U;
|
||||
controlRequest.setup = deviceSetup;
|
||||
controlRequest.length = message->length;
|
||||
error = USB_DeviceClassEvent(handle, kUSB_DeviceClassEventClassRequest, &controlRequest);
|
||||
}
|
||||
else if (((deviceSetup->bmRequestType & USB_REQUEST_TYPE_TYPE_VENDOR) == USB_REQUEST_TYPE_TYPE_VENDOR))
|
||||
{
|
||||
/* Data received in OUT phase, and notify the application. */
|
||||
usb_device_control_request_struct_t controlRequest;
|
||||
controlRequest.buffer = message->buffer;
|
||||
controlRequest.isSetup = 0U;
|
||||
controlRequest.setup = deviceSetup;
|
||||
controlRequest.length = message->length;
|
||||
error = USB_DeviceClassCallback(handle, kUSB_DeviceEventVendorRequest, &controlRequest);
|
||||
}
|
||||
else
|
||||
{
|
||||
}
|
||||
/* Send the response to the host. */
|
||||
error = USB_DeviceControlCallbackFeedback(handle, deviceSetup, error, kUSB_DeviceControlPipeDataStage, &buffer,
|
||||
&length);
|
||||
}
|
||||
else
|
||||
{
|
||||
}
|
||||
return error;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Control endpoint initialization function.
|
||||
*
|
||||
* This callback function is used to initialize the control pipes.
|
||||
*
|
||||
* @param handle The device handle. It equals the value returned from USB_DeviceInit.
|
||||
* @param param The up layer handle.
|
||||
*
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
*/
|
||||
usb_status_t USB_DeviceControlPipeInit(usb_device_handle handle, void *param)
|
||||
{
|
||||
usb_device_endpoint_init_struct_t epInitStruct;
|
||||
usb_device_endpoint_callback_struct_t epCallback;
|
||||
usb_status_t error;
|
||||
|
||||
epCallback.callbackFn = USB_DeviceControlCallback;
|
||||
epCallback.callbackParam = param;
|
||||
|
||||
epInitStruct.zlt = 1U;
|
||||
epInitStruct.transferType = USB_ENDPOINT_CONTROL;
|
||||
epInitStruct.interval = 0;
|
||||
epInitStruct.endpointAddress = USB_CONTROL_ENDPOINT | (USB_IN << USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT);
|
||||
epInitStruct.maxPacketSize = USB_CONTROL_MAX_PACKET_SIZE;
|
||||
/* Initialize the control IN pipe */
|
||||
error = USB_DeviceInitEndpoint(handle, &epInitStruct, &epCallback);
|
||||
|
||||
if (kStatus_USB_Success != error)
|
||||
{
|
||||
return error;
|
||||
}
|
||||
epInitStruct.endpointAddress = USB_CONTROL_ENDPOINT | (USB_OUT << USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT);
|
||||
/* Initialize the control OUT pipe */
|
||||
error = USB_DeviceInitEndpoint(handle, &epInitStruct, &epCallback);
|
||||
|
||||
if (kStatus_USB_Success != error)
|
||||
{
|
||||
USB_DeviceDeinitEndpoint(handle,
|
||||
USB_CONTROL_ENDPOINT | (USB_IN << USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_SHIFT));
|
||||
return error;
|
||||
}
|
||||
|
||||
return kStatus_USB_Success;
|
||||
}
|
||||
#endif /* USB_DEVICE_CONFIG_NUM */
|
@@ -1,79 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2015, Freescale Semiconductor, Inc.
|
||||
* Copyright 2016 NXP
|
||||
* All rights reserved.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
#ifndef __USB_DEVICE_CH9_H__
|
||||
#define __USB_DEVICE_CH9_H__
|
||||
|
||||
/*******************************************************************************
|
||||
* Definitions
|
||||
******************************************************************************/
|
||||
/*!
|
||||
* @addtogroup usb_device_ch9
|
||||
* @{
|
||||
*/
|
||||
/*! @brief Defines USB device status size when the host request to get device status */
|
||||
#define USB_DEVICE_STATUS_SIZE (0x02U)
|
||||
|
||||
/*! @brief Defines USB device interface status size when the host request to get interface status */
|
||||
#define USB_INTERFACE_STATUS_SIZE (0x02U)
|
||||
|
||||
/*! @brief Defines USB device endpoint status size when the host request to get endpoint status */
|
||||
#define USB_ENDPOINT_STATUS_SIZE (0x02U)
|
||||
|
||||
/*! @brief Defines USB device configuration size when the host request to get current configuration */
|
||||
#define USB_CONFIGURE_SIZE (0X01U)
|
||||
|
||||
/*! @brief Defines USB device interface alternate setting size when the host request to get interface alternate setting
|
||||
*/
|
||||
#define USB_INTERFACE_SIZE (0X01U)
|
||||
|
||||
/*! @brief Defines USB device status mask */
|
||||
#define USB_GET_STATUS_DEVICE_MASK (0x03U)
|
||||
|
||||
/*! @brief Defines USB device interface status mask */
|
||||
#define USB_GET_STATUS_INTERFACE_MASK (0x03U)
|
||||
|
||||
/*! @brief Defines USB device endpoint status mask */
|
||||
#define USB_GET_STATUS_ENDPOINT_MASK (0x03U)
|
||||
|
||||
/*! @brief Control read and write sequence */
|
||||
typedef enum _usb_device_control_read_write_sequence
|
||||
{
|
||||
kUSB_DeviceControlPipeSetupStage = 0U, /*!< Setup stage */
|
||||
kUSB_DeviceControlPipeDataStage, /*!< Data stage */
|
||||
kUSB_DeviceControlPipeStatusStage, /*!< status stage */
|
||||
} usb_device_control_read_write_sequence_t;
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*******************************************************************************
|
||||
* API
|
||||
******************************************************************************/
|
||||
|
||||
/*!
|
||||
* @brief Initializes the control pipes.
|
||||
*
|
||||
* The function is used to initialize the control pipes. This function should be called when event
|
||||
* kUSB_DeviceEventBusReset is received.
|
||||
*
|
||||
* @param[in] handle The device handle.
|
||||
* @param[in] param The event parameter.
|
||||
*
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
*/
|
||||
extern usb_status_t USB_DeviceControlPipeInit(usb_device_handle handle, void *param);
|
||||
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
#endif
|
||||
|
||||
/*! @}*/
|
||||
|
||||
#endif /* __USB_DEVICE_CH9_H__ */
|
@@ -1,552 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2015 - 2016, Freescale Semiconductor, Inc.
|
||||
* Copyright 2016, 2019 NXP
|
||||
* All rights reserved.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
#include "usb_device_config.h"
|
||||
#include "usb.h"
|
||||
|
||||
#include "usb_device.h"
|
||||
#include "usb_device_ch9.h"
|
||||
#include "usb_device_class.h"
|
||||
|
||||
#if ((defined(USB_DEVICE_CONFIG_NUM)) && (USB_DEVICE_CONFIG_NUM > 0U))
|
||||
/* Include the class drivers according to the usb_device_config.h. */
|
||||
#if ((defined(USB_DEVICE_CONFIG_HID)) && (USB_DEVICE_CONFIG_HID > 0U))
|
||||
#include "usb_device_hid.h"
|
||||
#endif
|
||||
|
||||
#if ((defined(USB_DEVICE_CONFIG_CDC_ACM)) && (USB_DEVICE_CONFIG_CDC_ACM > 0U))
|
||||
#include "usb_device_cdc_acm.h"
|
||||
#endif
|
||||
|
||||
#if ((defined(USB_DEVICE_CONFIG_MSC)) && (USB_DEVICE_CONFIG_MSC > 0U))
|
||||
#include "usb_device_msc.h"
|
||||
#endif
|
||||
|
||||
#if ((defined(USB_DEVICE_CONFIG_AUDIO)) && (USB_DEVICE_CONFIG_AUDIO > 0U))
|
||||
#include "usb_device_audio.h"
|
||||
#endif
|
||||
|
||||
#if ((defined(USB_DEVICE_CONFIG_PHDC)) && (USB_DEVICE_CONFIG_PHDC > 0U))
|
||||
#include "usb_device_phdc.h"
|
||||
#endif
|
||||
|
||||
#if ((defined(USB_DEVICE_CONFIG_VIDEO)) && (USB_DEVICE_CONFIG_VIDEO > 0U))
|
||||
#include "usb_device_video.h"
|
||||
#endif
|
||||
|
||||
#if ((defined(USB_DEVICE_CONFIG_PRINTER)) && (USB_DEVICE_CONFIG_PRINTER > 0U))
|
||||
#include "usb_device_printer.h"
|
||||
#endif
|
||||
|
||||
#if ((defined(USB_DEVICE_CONFIG_DFU)) && (USB_DEVICE_CONFIG_DFU > 0U))
|
||||
#include "usb_device_dfu.h"
|
||||
#endif
|
||||
|
||||
#if ((defined(USB_DEVICE_CONFIG_CCID)) && (USB_DEVICE_CONFIG_CCID > 0U))
|
||||
#include "usb_device_ccid.h"
|
||||
#endif
|
||||
|
||||
/*******************************************************************************
|
||||
* Definitions
|
||||
******************************************************************************/
|
||||
|
||||
/*******************************************************************************
|
||||
* Prototypes
|
||||
******************************************************************************/
|
||||
static usb_status_t USB_DeviceClassAllocateHandle(uint8_t controllerId, usb_device_common_class_struct_t **handle);
|
||||
static usb_status_t USB_DeviceClassFreeHandle(uint8_t controllerId);
|
||||
static usb_status_t USB_DeviceClassGetHandleByControllerId(uint8_t controllerId,
|
||||
usb_device_common_class_struct_t **handle);
|
||||
static usb_status_t USB_DeviceClassGetHandleByDeviceHandle(usb_device_handle deviceHandle,
|
||||
usb_device_common_class_struct_t **handle);
|
||||
|
||||
/*******************************************************************************
|
||||
* Variables
|
||||
******************************************************************************/
|
||||
|
||||
/* The device class driver list. */
|
||||
static const usb_device_class_map_t s_UsbDeviceClassInterfaceMap[] = {
|
||||
#if ((defined(USB_DEVICE_CONFIG_HID)) && (USB_DEVICE_CONFIG_HID > 0U))
|
||||
{USB_DeviceHidInit, USB_DeviceHidDeinit, USB_DeviceHidEvent, kUSB_DeviceClassTypeHid},
|
||||
#endif
|
||||
|
||||
#if ((defined(USB_DEVICE_CONFIG_CDC_ACM)) && (USB_DEVICE_CONFIG_CDC_ACM > 0U))
|
||||
{USB_DeviceCdcAcmInit, USB_DeviceCdcAcmDeinit, USB_DeviceCdcAcmEvent, kUSB_DeviceClassTypeCdc},
|
||||
#endif
|
||||
|
||||
#if ((defined(USB_DEVICE_CONFIG_MSC)) && (USB_DEVICE_CONFIG_MSC > 0U))
|
||||
{USB_DeviceMscInit, USB_DeviceMscDeinit, USB_DeviceMscEvent, kUSB_DeviceClassTypeMsc},
|
||||
#endif
|
||||
|
||||
#if ((defined USB_DEVICE_CONFIG_AUDIO) && (USB_DEVICE_CONFIG_AUDIO > 0U))
|
||||
{USB_DeviceAudioInit, USB_DeviceAudioDeinit, USB_DeviceAudioEvent, kUSB_DeviceClassTypeAudio},
|
||||
#endif
|
||||
|
||||
#if ((defined USB_DEVICE_CONFIG_PHDC) && (USB_DEVICE_CONFIG_PHDC > 0U))
|
||||
{USB_DevicePhdcInit, USB_DevicePhdcDeinit, USB_DevicePhdcEvent, kUSB_DeviceClassTypePhdc},
|
||||
#endif
|
||||
|
||||
#if ((defined USB_DEVICE_CONFIG_VIDEO) && (USB_DEVICE_CONFIG_VIDEO > 0U))
|
||||
{USB_DeviceVideoInit, USB_DeviceVideoDeinit, USB_DeviceVideoEvent, kUSB_DeviceClassTypeVideo},
|
||||
#endif
|
||||
|
||||
#if ((defined USB_DEVICE_CONFIG_PRINTER) && (USB_DEVICE_CONFIG_PRINTER > 0U))
|
||||
{USB_DevicePrinterInit, USB_DevicePrinterDeinit, USB_DevicePrinterEvent, kUSB_DeviceClassTypePrinter},
|
||||
#endif
|
||||
|
||||
#if ((defined USB_DEVICE_CONFIG_DFU) && (USB_DEVICE_CONFIG_DFU > 0U))
|
||||
{USB_DeviceDfuInit, USB_DeviceDfuDeinit, USB_DeviceDfuEvent, kUSB_DeviceClassTypeDfu},
|
||||
#endif
|
||||
|
||||
#if ((defined USB_DEVICE_CONFIG_CCID) && (USB_DEVICE_CONFIG_CCID > 0U))
|
||||
{USB_DeviceCcidInit, USB_DeviceCcidDeinit, USB_DeviceCcidEvent, kUSB_DeviceClassTypeCcid},
|
||||
#endif
|
||||
|
||||
{(usb_device_class_init_call_t)NULL, (usb_device_class_deinit_call_t)NULL, (usb_device_class_event_callback_t)NULL,
|
||||
(usb_device_class_type_t)0},
|
||||
};
|
||||
|
||||
USB_GLOBAL USB_RAM_ADDRESS_ALIGNMENT(USB_DATA_ALIGN_SIZE) static usb_device_common_class_struct_t
|
||||
s_UsbDeviceCommonClassStruct[USB_DEVICE_CONFIG_NUM];
|
||||
USB_GLOBAL USB_RAM_ADDRESS_ALIGNMENT(USB_DATA_ALIGN_SIZE) static uint8_t
|
||||
s_UsbDeviceSetupBuffer[USB_DEVICE_CONFIG_NUM][USB_DATA_ALIGN_SIZE_MULTIPLE(USB_SETUP_PACKET_SIZE)];
|
||||
|
||||
/*******************************************************************************
|
||||
* Code
|
||||
******************************************************************************/
|
||||
|
||||
/*!
|
||||
* @brief Allocate a device common class handle.
|
||||
*
|
||||
* This function allocates a a device common class handle.
|
||||
*
|
||||
* @param controllerId The controller id of the USB IP. Please refer to the enumeration usb_controller_index_t.
|
||||
* @param handle It is out parameter, is used to return pointer of the device common class handle to the
|
||||
* caller.
|
||||
*
|
||||
* @retval kStatus_USB_Success Get a device handle successfully.
|
||||
* @retval kStatus_USB_Busy Cannot allocate a common class handle.
|
||||
* @retval kStatus_USB_Error The common class has been initialized.
|
||||
*/
|
||||
static usb_status_t USB_DeviceClassAllocateHandle(uint8_t controllerId, usb_device_common_class_struct_t **handle)
|
||||
{
|
||||
uint32_t count;
|
||||
OSA_SR_ALLOC();
|
||||
|
||||
OSA_ENTER_CRITICAL();
|
||||
/* Check the controller is initialized or not. */
|
||||
for (count = 0U; count < USB_DEVICE_CONFIG_NUM; count++)
|
||||
{
|
||||
if ((NULL != s_UsbDeviceCommonClassStruct[count].handle) &&
|
||||
(controllerId == s_UsbDeviceCommonClassStruct[count].controllerId))
|
||||
{
|
||||
OSA_EXIT_CRITICAL();
|
||||
return kStatus_USB_Error;
|
||||
}
|
||||
}
|
||||
/* Get a free common class handle. */
|
||||
for (count = 0U; count < USB_DEVICE_CONFIG_NUM; count++)
|
||||
{
|
||||
if (NULL == s_UsbDeviceCommonClassStruct[count].handle)
|
||||
{
|
||||
s_UsbDeviceCommonClassStruct[count].controllerId = controllerId;
|
||||
s_UsbDeviceCommonClassStruct[count].setupBuffer = s_UsbDeviceSetupBuffer[count];
|
||||
*handle = &s_UsbDeviceCommonClassStruct[count];
|
||||
OSA_EXIT_CRITICAL();
|
||||
return kStatus_USB_Success;
|
||||
}
|
||||
}
|
||||
|
||||
OSA_EXIT_CRITICAL();
|
||||
return kStatus_USB_Busy;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Free a device common class handle.
|
||||
*
|
||||
* This function frees a device common class handle.
|
||||
*
|
||||
* @param controllerId The controller id of the USB IP. Please refer to the enumeration usb_controller_index_t.
|
||||
*
|
||||
* @retval kStatus_USB_Success Free device handle successfully.
|
||||
* @retval kStatus_USB_InvalidParameter The common class can not be found.
|
||||
*/
|
||||
static usb_status_t USB_DeviceClassFreeHandle(uint8_t controllerId)
|
||||
{
|
||||
uint32_t count = 0U;
|
||||
OSA_SR_ALLOC();
|
||||
|
||||
OSA_ENTER_CRITICAL();
|
||||
for (; count < USB_DEVICE_CONFIG_NUM; count++)
|
||||
{
|
||||
if ((NULL != s_UsbDeviceCommonClassStruct[count].handle) &&
|
||||
(controllerId == s_UsbDeviceCommonClassStruct[count].controllerId))
|
||||
{
|
||||
s_UsbDeviceCommonClassStruct[count].handle = NULL;
|
||||
s_UsbDeviceCommonClassStruct[count].configList = (usb_device_class_config_list_struct_t *)NULL;
|
||||
s_UsbDeviceCommonClassStruct[count].controllerId = 0U;
|
||||
OSA_EXIT_CRITICAL();
|
||||
return kStatus_USB_Success;
|
||||
}
|
||||
}
|
||||
OSA_EXIT_CRITICAL();
|
||||
|
||||
return kStatus_USB_InvalidParameter;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Get the device common class handle according to the controller id.
|
||||
*
|
||||
* This function gets the device common class handle according to the controller id.
|
||||
*
|
||||
* @param controllerId The controller id of the USB IP. Please refer to the enumeration usb_controller_index_t.
|
||||
* @param handle It is out parameter, is used to return pointer of the device common class handle to the
|
||||
* caller.
|
||||
*
|
||||
* @retval kStatus_USB_Success Free device handle successfully.
|
||||
* @retval kStatus_USB_InvalidParameter The common class can not be found.
|
||||
*/
|
||||
static usb_status_t USB_DeviceClassGetHandleByControllerId(uint8_t controllerId,
|
||||
usb_device_common_class_struct_t **handle)
|
||||
{
|
||||
uint32_t count = 0U;
|
||||
OSA_SR_ALLOC();
|
||||
|
||||
OSA_ENTER_CRITICAL();
|
||||
for (; count < USB_DEVICE_CONFIG_NUM; count++)
|
||||
{
|
||||
if ((NULL != s_UsbDeviceCommonClassStruct[count].handle) &&
|
||||
(controllerId == s_UsbDeviceCommonClassStruct[count].controllerId))
|
||||
{
|
||||
*handle = &s_UsbDeviceCommonClassStruct[count];
|
||||
OSA_EXIT_CRITICAL();
|
||||
return kStatus_USB_Success;
|
||||
}
|
||||
}
|
||||
OSA_EXIT_CRITICAL();
|
||||
return kStatus_USB_InvalidParameter;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Get the device common class handle according to the device handle.
|
||||
*
|
||||
* This function gets the device common class handle according to the device handle.
|
||||
*
|
||||
* @param deviceHandle The device handle, got from the USB_DeviceInit.
|
||||
* @param handle It is out parameter, is used to return pointer of the device common class handle to the
|
||||
* caller.
|
||||
*
|
||||
* @retval kStatus_USB_Success Free device handle successfully.
|
||||
* @retval kStatus_USB_InvalidParameter The common class can not be found.
|
||||
*/
|
||||
static usb_status_t USB_DeviceClassGetHandleByDeviceHandle(usb_device_handle deviceHandle,
|
||||
usb_device_common_class_struct_t **handle)
|
||||
{
|
||||
uint32_t count = 0U;
|
||||
OSA_SR_ALLOC();
|
||||
|
||||
OSA_ENTER_CRITICAL();
|
||||
for (; count < USB_DEVICE_CONFIG_NUM; count++)
|
||||
{
|
||||
if (deviceHandle == s_UsbDeviceCommonClassStruct[count].handle)
|
||||
{
|
||||
*handle = &s_UsbDeviceCommonClassStruct[count];
|
||||
OSA_EXIT_CRITICAL();
|
||||
return kStatus_USB_Success;
|
||||
}
|
||||
}
|
||||
OSA_EXIT_CRITICAL();
|
||||
return kStatus_USB_InvalidParameter;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Get the device handle according to the controller id.
|
||||
*
|
||||
* This function gets the device handle according to the controller id.
|
||||
*
|
||||
* @param controllerId The controller id of the USB IP. Please refer to the enumeration usb_controller_index_t.
|
||||
* @param handle It is out parameter, is used to return pointer of the device handle to the caller.
|
||||
*
|
||||
* @retval kStatus_USB_Success Free device handle successfully.
|
||||
* @retval kStatus_USB_InvalidParameter The device handle not be found.
|
||||
*/
|
||||
usb_status_t USB_DeviceClassGetDeviceHandle(uint8_t controllerId, usb_device_handle *handle)
|
||||
{
|
||||
uint32_t count = 0U;
|
||||
OSA_SR_ALLOC();
|
||||
|
||||
OSA_ENTER_CRITICAL();
|
||||
for (; count < USB_DEVICE_CONFIG_NUM; count++)
|
||||
{
|
||||
if ((NULL != s_UsbDeviceCommonClassStruct[count].handle) &&
|
||||
(controllerId == s_UsbDeviceCommonClassStruct[count].controllerId))
|
||||
{
|
||||
*handle = s_UsbDeviceCommonClassStruct[count].handle;
|
||||
OSA_EXIT_CRITICAL();
|
||||
return kStatus_USB_Success;
|
||||
}
|
||||
}
|
||||
OSA_EXIT_CRITICAL();
|
||||
return kStatus_USB_InvalidParameter;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Handle the event passed to the class drivers.
|
||||
*
|
||||
* This function handles the event passed to the class drivers.
|
||||
*
|
||||
* @param handle The device handle, got from the USB_DeviceInit.
|
||||
* @param event The event codes. Please refer to the enumeration usb_device_class_event_t.
|
||||
* @param param The param type is determined by the event code.
|
||||
*
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
* @retval kStatus_USB_Success A valid request has been handled.
|
||||
* @retval kStatus_USB_InvalidParameter The device handle not be found.
|
||||
* @retval kStatus_USB_InvalidRequest The request is invalid, and the control pipe will be stalled by the caller.
|
||||
*/
|
||||
usb_status_t USB_DeviceClassEvent(usb_device_handle handle, usb_device_class_event_t event, void *param)
|
||||
{
|
||||
usb_device_common_class_struct_t *classHandle;
|
||||
uint8_t mapIndex;
|
||||
uint8_t classIndex;
|
||||
usb_status_t errorReturn = kStatus_USB_Error;
|
||||
usb_status_t error = kStatus_USB_Error;
|
||||
|
||||
if (NULL == param)
|
||||
{
|
||||
return kStatus_USB_InvalidParameter;
|
||||
}
|
||||
|
||||
/* Get the common class handle according to the device handle. */
|
||||
errorReturn = USB_DeviceClassGetHandleByDeviceHandle(handle, &classHandle);
|
||||
if (kStatus_USB_Success != errorReturn)
|
||||
{
|
||||
return kStatus_USB_InvalidParameter;
|
||||
}
|
||||
|
||||
for (classIndex = 0U; classIndex < classHandle->configList->count; classIndex++)
|
||||
{
|
||||
for (mapIndex = 0U; mapIndex < (sizeof(s_UsbDeviceClassInterfaceMap) / sizeof(usb_device_class_map_t));
|
||||
mapIndex++)
|
||||
{
|
||||
if (s_UsbDeviceClassInterfaceMap[mapIndex].type ==
|
||||
classHandle->configList->config[classIndex].classInfomation->type)
|
||||
{
|
||||
/* Call class event callback of supported class */
|
||||
errorReturn = s_UsbDeviceClassInterfaceMap[mapIndex].classEventCallback(
|
||||
(void *)classHandle->configList->config[classIndex].classHandle, event, param);
|
||||
/* Return the error code kStatus_USB_InvalidRequest immediately, when a class returns
|
||||
* kStatus_USB_InvalidRequest. */
|
||||
if (kStatus_USB_InvalidRequest == errorReturn)
|
||||
{
|
||||
return kStatus_USB_InvalidRequest;
|
||||
}
|
||||
/* For composite device, it should return kStatus_USB_Success once a valid request has been handled */
|
||||
if (kStatus_USB_Success == errorReturn)
|
||||
{
|
||||
error = kStatus_USB_Success;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Handle the common class callback.
|
||||
*
|
||||
* This function handles the common class callback.
|
||||
*
|
||||
* @param handle The device handle, got from the USB_DeviceInit.
|
||||
* @param event The event codes. Please refer to the enumeration usb_device_event_t.
|
||||
* @param param The param type is determined by the event code.
|
||||
*
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
*/
|
||||
usb_status_t USB_DeviceClassCallback(usb_device_handle handle, uint32_t event, void *param)
|
||||
{
|
||||
usb_device_common_class_struct_t *classHandle;
|
||||
usb_status_t error = kStatus_USB_Error;
|
||||
|
||||
/* Get the common class handle according to the device handle. */
|
||||
error = USB_DeviceClassGetHandleByDeviceHandle(handle, &classHandle);
|
||||
if (kStatus_USB_Success != error)
|
||||
{
|
||||
return error;
|
||||
}
|
||||
|
||||
if (kUSB_DeviceEventBusReset == event)
|
||||
{
|
||||
/* Initialize the control pipes */
|
||||
USB_DeviceControlPipeInit(handle, classHandle);
|
||||
|
||||
/* Notify the classes the USB bus reset signal detected. */
|
||||
USB_DeviceClassEvent(handle, kUSB_DeviceClassEventDeviceReset, classHandle);
|
||||
}
|
||||
|
||||
/* Call the application device callback function. deviceCallback is from the second parameter of
|
||||
USB_DeviceClassInit */
|
||||
error = classHandle->configList->deviceCallback(handle, event, param);
|
||||
return error;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Initialize the common class and the supported classes.
|
||||
*
|
||||
* This function is used to initialize the common class and the supported classes.
|
||||
*
|
||||
* @param[in] controllerId The controller id of the USB IP. Please refer to the enumeration #usb_controller_index_t.
|
||||
* @param[in] configList The class configurations. The pointer must point to the global variable.
|
||||
* Please refer to the structure #usb_device_class_config_list_struct_t.
|
||||
* @param[out] handle It is out parameter, is used to return pointer of the device handle to the caller.
|
||||
* The value of parameter is a pointer points the device handle, and this design is used to
|
||||
* make simple device align with composite device. For composite device, there are many
|
||||
* kinds of class handle, but there is only one device handle. So the handle points to
|
||||
* a device instead of a class. And the class handle can be got from the
|
||||
* #usb_device_class_config_struct_t::classHandle after the function successfully.
|
||||
*
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
*/
|
||||
usb_status_t USB_DeviceClassInit(
|
||||
uint8_t controllerId, /*!< [IN] Controller ID */
|
||||
usb_device_class_config_list_struct_t *configList, /*!< [IN] Pointer to class configuration list */
|
||||
usb_device_handle *handle /*!< [OUT] Pointer to the device handle */
|
||||
)
|
||||
{
|
||||
usb_device_common_class_struct_t *classHandle;
|
||||
usb_status_t error = kStatus_USB_Error;
|
||||
uint8_t mapIndex;
|
||||
uint8_t classIndex;
|
||||
|
||||
if ((NULL == handle) || (NULL == configList) || ((usb_device_callback_t)NULL == configList->deviceCallback))
|
||||
{
|
||||
return kStatus_USB_InvalidParameter;
|
||||
}
|
||||
|
||||
/* Allocate a common class driver handle. */
|
||||
error = USB_DeviceClassAllocateHandle(controllerId, &classHandle);
|
||||
if (kStatus_USB_Success != error)
|
||||
{
|
||||
return error;
|
||||
}
|
||||
/* Save the configuration list */
|
||||
classHandle->configList = configList;
|
||||
|
||||
/* Initialize the device stack. */
|
||||
error = USB_DeviceInit(controllerId, USB_DeviceClassCallback, &classHandle->handle);
|
||||
|
||||
if (kStatus_USB_Success != error)
|
||||
{
|
||||
USB_DeviceDeinit(classHandle->handle);
|
||||
USB_DeviceClassFreeHandle(controllerId);
|
||||
return error;
|
||||
}
|
||||
|
||||
/* Initialize the all supported classes according to the configuration list. */
|
||||
for (classIndex = 0U; classIndex < classHandle->configList->count; classIndex++)
|
||||
{
|
||||
for (mapIndex = 0U; mapIndex < (sizeof(s_UsbDeviceClassInterfaceMap) / sizeof(usb_device_class_map_t));
|
||||
mapIndex++)
|
||||
{
|
||||
if (classHandle->configList->config[classIndex].classInfomation->type ==
|
||||
s_UsbDeviceClassInterfaceMap[mapIndex].type)
|
||||
{
|
||||
(void)s_UsbDeviceClassInterfaceMap[mapIndex].classInit(
|
||||
controllerId, &classHandle->configList->config[classIndex],
|
||||
&classHandle->configList->config[classIndex].classHandle);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
*handle = classHandle->handle;
|
||||
return error;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief De-initialize the common class and the supported classes.
|
||||
*
|
||||
* This function is used to de-initialize the common class and the supported classes.
|
||||
*
|
||||
* @param controllerId The controller id of the USB IP. Please refer to the enumeration usb_controller_index_t.
|
||||
*
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
*/
|
||||
usb_status_t USB_DeviceClassDeinit(uint8_t controllerId /*!< [IN] Controller ID */
|
||||
)
|
||||
{
|
||||
usb_device_common_class_struct_t *classHandle;
|
||||
usb_status_t error = kStatus_USB_Error;
|
||||
uint8_t mapIndex;
|
||||
uint8_t classIndex;
|
||||
|
||||
/* Get the common class handle according to the controller id. */
|
||||
error = USB_DeviceClassGetHandleByControllerId(controllerId, &classHandle);
|
||||
|
||||
if (kStatus_USB_Success != error)
|
||||
{
|
||||
return error;
|
||||
}
|
||||
|
||||
/* De-initialize the all supported classes according to the configuration list. */
|
||||
for (classIndex = 0U; classIndex < classHandle->configList->count; classIndex++)
|
||||
{
|
||||
for (mapIndex = 0U; mapIndex < (sizeof(s_UsbDeviceClassInterfaceMap) / sizeof(usb_device_class_map_t));
|
||||
mapIndex++)
|
||||
{
|
||||
if (classHandle->configList->config[classIndex].classInfomation->type ==
|
||||
s_UsbDeviceClassInterfaceMap[mapIndex].type)
|
||||
{
|
||||
(void)s_UsbDeviceClassInterfaceMap[mapIndex].classDeinit(
|
||||
classHandle->configList->config[classIndex].classHandle);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* De-initialize the USB device stack. */
|
||||
error = USB_DeviceDeinit(classHandle->handle);
|
||||
if (kStatus_USB_Success == error)
|
||||
{
|
||||
/* Free the common class handle. */
|
||||
(void)USB_DeviceClassFreeHandle(controllerId);
|
||||
}
|
||||
return error;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief Get the USB bus speed.
|
||||
*
|
||||
* This function is used to get the USB bus speed.
|
||||
*
|
||||
* @param controllerId The controller id of the USB IP. Please refer to the enumeration usb_controller_index_t.
|
||||
* @param speed It is an OUT parameter, return current speed of the controller.
|
||||
*
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
*/
|
||||
usb_status_t USB_DeviceClassGetSpeed(uint8_t controllerId, /*!< [IN] Controller ID */
|
||||
uint8_t *speed /*!< [OUT] Current speed */
|
||||
)
|
||||
{
|
||||
usb_device_common_class_struct_t *classHandle;
|
||||
usb_status_t error = kStatus_USB_Error;
|
||||
|
||||
/* Get the common class handle according to the controller id. */
|
||||
error = USB_DeviceClassGetHandleByControllerId(controllerId, &classHandle);
|
||||
|
||||
if (kStatus_USB_Success != error)
|
||||
{
|
||||
return error;
|
||||
}
|
||||
|
||||
/* Get the current speed. */
|
||||
error = USB_DeviceGetStatus(classHandle->handle, kUSB_DeviceStatusSpeed, speed);
|
||||
|
||||
return error;
|
||||
}
|
||||
#endif /* USB_DEVICE_CONFIG_NUM */
|
@@ -1,421 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2015 - 2016, Freescale Semiconductor, Inc.
|
||||
* Copyright 2016 NXP
|
||||
* All rights reserved.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
#ifndef __USB_DEVICE_CLASS_H__
|
||||
#define __USB_DEVICE_CLASS_H__
|
||||
|
||||
/*!
|
||||
* @addtogroup usb_device_class_driver
|
||||
* @{
|
||||
*/
|
||||
|
||||
/*******************************************************************************
|
||||
* Definitions
|
||||
******************************************************************************/
|
||||
|
||||
/*! @brief Macro to define class handle */
|
||||
typedef void *class_handle_t;
|
||||
|
||||
/*! @brief Available class types. */
|
||||
typedef enum _usb_usb_device_class_type
|
||||
{
|
||||
kUSB_DeviceClassTypeHid = 1U,
|
||||
kUSB_DeviceClassTypeCdc,
|
||||
kUSB_DeviceClassTypeMsc,
|
||||
kUSB_DeviceClassTypeAudio,
|
||||
kUSB_DeviceClassTypePhdc,
|
||||
kUSB_DeviceClassTypeVideo,
|
||||
kUSB_DeviceClassTypePrinter,
|
||||
kUSB_DeviceClassTypeDfu,
|
||||
kUSB_DeviceClassTypeCcid,
|
||||
} usb_device_class_type_t;
|
||||
|
||||
/*! @brief Available common class events. */
|
||||
typedef enum _usb_device_class_event
|
||||
{
|
||||
kUSB_DeviceClassEventClassRequest = 1U,
|
||||
kUSB_DeviceClassEventDeviceReset,
|
||||
kUSB_DeviceClassEventSetConfiguration,
|
||||
kUSB_DeviceClassEventSetInterface,
|
||||
kUSB_DeviceClassEventSetEndpointHalt,
|
||||
kUSB_DeviceClassEventClearEndpointHalt,
|
||||
} usb_device_class_event_t;
|
||||
|
||||
/*!
|
||||
* @brief Obtains the endpoint data structure.
|
||||
*
|
||||
* Define the endpoint data structure.
|
||||
*
|
||||
*/
|
||||
typedef struct _usb_device_endpoint_struct
|
||||
{
|
||||
uint8_t endpointAddress; /*!< Endpoint address*/
|
||||
uint8_t transferType; /*!< Endpoint transfer type*/
|
||||
uint16_t maxPacketSize; /*!< Endpoint maximum packet size */
|
||||
uint8_t interval; /*!< Endpoint interval*/
|
||||
} usb_device_endpoint_struct_t;
|
||||
|
||||
/*!
|
||||
* @brief Obtains the endpoint group.
|
||||
*
|
||||
* Structure representing endpoints and the number of endpoints that the user wants.
|
||||
*
|
||||
*/
|
||||
typedef struct _usb_device_endpoint_list
|
||||
{
|
||||
uint8_t count; /*!< How many endpoints in current interface*/
|
||||
usb_device_endpoint_struct_t *endpoint; /*!< Endpoint structure list*/
|
||||
} usb_device_endpoint_list_t;
|
||||
|
||||
/*!
|
||||
* @brief Obtains the interface list data structure.
|
||||
*
|
||||
* Structure representing an interface.
|
||||
*
|
||||
*/
|
||||
typedef struct _usb_device_interface_struct
|
||||
{
|
||||
uint8_t alternateSetting; /*!< Alternate setting number*/
|
||||
usb_device_endpoint_list_t endpointList; /*!< Endpoints of the interface*/
|
||||
void *classSpecific; /*!< Class specific structure handle*/
|
||||
} usb_device_interface_struct_t;
|
||||
|
||||
/*!
|
||||
* @brief Obtains the interface data structure.
|
||||
*
|
||||
* Structure representing interface.
|
||||
*
|
||||
*/
|
||||
typedef struct _usb_device_interfaces_struct
|
||||
{
|
||||
uint8_t classCode; /*!< Class code of the interface*/
|
||||
uint8_t subclassCode; /*!< Subclass code of the interface*/
|
||||
uint8_t protocolCode; /*!< Protocol code of the interface*/
|
||||
uint8_t interfaceNumber; /*!< Interface number*/
|
||||
usb_device_interface_struct_t *interface; /*!< Interface structure list*/
|
||||
uint8_t count; /*!< Number of interfaces in the current interface*/
|
||||
} usb_device_interfaces_struct_t;
|
||||
|
||||
/*!
|
||||
* @brief Obtains the interface group.
|
||||
*
|
||||
* Structure representing how many interfaces in one class type.
|
||||
*
|
||||
*/
|
||||
typedef struct _usb_device_interface_list
|
||||
{
|
||||
uint8_t count; /*!< Number of interfaces of the class*/
|
||||
usb_device_interfaces_struct_t *interfaces; /*!< All interfaces*/
|
||||
} usb_device_interface_list_t;
|
||||
|
||||
/*!
|
||||
* @brief Obtains the class data structure.
|
||||
*
|
||||
* Structure representing how many configurations in one class type.
|
||||
*
|
||||
*/
|
||||
typedef struct _usb_device_class_struct
|
||||
{
|
||||
usb_device_interface_list_t *interfaceList; /*!< Interfaces of the class*/
|
||||
usb_device_class_type_t type; /*!< Class type*/
|
||||
uint8_t configurations; /*!< Number of configurations of the class*/
|
||||
} usb_device_class_struct_t;
|
||||
|
||||
/*callback function pointer structure for application to provide class parameters*/
|
||||
typedef usb_status_t (*usb_device_class_callback_t)(class_handle_t classHandle,
|
||||
uint32_t callbackEvent,
|
||||
void *eventParam);
|
||||
|
||||
/*!
|
||||
* @brief Obtains the device class information structure.
|
||||
*
|
||||
* Structure representing the device class information. This structure only can be stored in RAM space.
|
||||
*
|
||||
*/
|
||||
typedef struct _usb_device_class_config_struct
|
||||
{
|
||||
usb_device_class_callback_t classCallback; /*!< Class callback function to handle the device status-related event
|
||||
for the specified type of class*/
|
||||
class_handle_t classHandle; /*!< The class handle of the class, filled by the common driver.*/
|
||||
usb_device_class_struct_t *classInfomation; /*!< Detailed information of the class*/
|
||||
} usb_device_class_config_struct_t;
|
||||
|
||||
/*!
|
||||
* @brief Obtains the device class configuration structure.
|
||||
*
|
||||
* Structure representing the device class configuration information.
|
||||
*
|
||||
*/
|
||||
typedef struct _usb_device_class_config_list_struct
|
||||
{
|
||||
usb_device_class_config_struct_t *config; /*!< Array of class configuration structures */
|
||||
usb_device_callback_t deviceCallback; /*!< Device callback function */
|
||||
uint8_t count; /*!< Number of class supported */
|
||||
} usb_device_class_config_list_struct_t;
|
||||
|
||||
/*!
|
||||
* @brief Obtains the control request structure.
|
||||
*
|
||||
* This structure is used to pass the control request information.
|
||||
* The structure is used in following two cases.
|
||||
* 1. Case one, the host wants to send data to the device in the control data stage: @n
|
||||
* a. If a setup packet is received, the structure is used to pass the setup packet data and wants to get the
|
||||
* buffer to receive data sent from the host.
|
||||
* The field isSetup is 1.
|
||||
* The length is the requested buffer length.
|
||||
* The buffer is filled by the class or application by using the valid buffer address.
|
||||
* The setup is the setup packet address.
|
||||
* b. If the data received is sent by the host, the structure is used to pass the data buffer address and the
|
||||
* data
|
||||
* length sent by the host.
|
||||
* In this way, the field isSetup is 0.
|
||||
* The buffer is the address of the data sent from the host.
|
||||
* The length is the received data length.
|
||||
* The setup is the setup packet address. @n
|
||||
* 2. Case two, the host wants to get data from the device in control data stage: @n
|
||||
* If the setup packet is received, the structure is used to pass the setup packet data and wants to get the
|
||||
* data buffer address to send data to the host.
|
||||
* The field isSetup is 1.
|
||||
* The length is the requested data length.
|
||||
* The buffer is filled by the class or application by using the valid buffer address.
|
||||
* The setup is the setup packet address.
|
||||
*
|
||||
*/
|
||||
typedef struct _usb_device_control_request_struct
|
||||
{
|
||||
usb_setup_struct_t *setup; /*!< The pointer of the setup packet data. */
|
||||
uint8_t *buffer; /*!< Pass the buffer address. */
|
||||
uint32_t length; /*!< Pass the buffer length or requested length. */
|
||||
uint8_t isSetup; /*!< Indicates whether a setup packet is received. */
|
||||
} usb_device_control_request_struct_t;
|
||||
|
||||
/*! @brief Obtains the control get descriptor request common structure. */
|
||||
typedef struct _usb_device_get_descriptor_common_struct
|
||||
{
|
||||
uint8_t *buffer; /*!< Pass the buffer address. */
|
||||
uint32_t length; /*!< Pass the buffer length. */
|
||||
} usb_device_get_descriptor_common_struct_t;
|
||||
|
||||
/*! @brief Obtains the control get device descriptor request structure. */
|
||||
typedef struct _usb_device_get_device_descriptor_struct
|
||||
{
|
||||
uint8_t *buffer; /*!< Pass the buffer address. */
|
||||
uint32_t length; /*!< Pass the buffer length. */
|
||||
} usb_device_get_device_descriptor_struct_t;
|
||||
|
||||
/*! @brief Obtains the control get device qualifier descriptor request structure. */
|
||||
typedef struct _usb_device_get_device_qualifier_descriptor_struct
|
||||
{
|
||||
uint8_t *buffer; /*!< Pass the buffer address. */
|
||||
uint32_t length; /*!< Pass the buffer length. */
|
||||
} usb_device_get_device_qualifier_descriptor_struct_t;
|
||||
|
||||
/*! @brief Obtains the control get configuration descriptor request structure. */
|
||||
typedef struct _usb_device_get_configuration_descriptor_struct
|
||||
{
|
||||
uint8_t *buffer; /*!< Pass the buffer address. */
|
||||
uint32_t length; /*!< Pass the buffer length. */
|
||||
uint8_t configuration; /*!< The configuration number. */
|
||||
} usb_device_get_configuration_descriptor_struct_t;
|
||||
|
||||
/*! @brief Obtains the control get bos descriptor request structure. */
|
||||
typedef struct _usb_device_get_bos_descriptor_struct
|
||||
{
|
||||
uint8_t *buffer; /*!< Pass the buffer address. */
|
||||
uint32_t length; /*!< Pass the buffer length. */
|
||||
} usb_device_get_bos_descriptor_struct_t;
|
||||
|
||||
/*! @brief Obtains the control get string descriptor request structure. */
|
||||
typedef struct _usb_device_get_string_descriptor_struct
|
||||
{
|
||||
uint8_t *buffer; /*!< Pass the buffer address. */
|
||||
uint32_t length; /*!< Pass the buffer length. */
|
||||
uint16_t languageId; /*!< Language ID. */
|
||||
uint8_t stringIndex; /*!< String index. */
|
||||
} usb_device_get_string_descriptor_struct_t;
|
||||
|
||||
/*! @brief Obtains the control get HID descriptor request structure. */
|
||||
typedef struct _usb_device_get_hid_descriptor_struct
|
||||
{
|
||||
uint8_t *buffer; /*!< Pass the buffer address. */
|
||||
uint32_t length; /*!< Pass the buffer length. */
|
||||
uint8_t interfaceNumber; /*!< The interface number. */
|
||||
} usb_device_get_hid_descriptor_struct_t;
|
||||
|
||||
/*! @brief Obtains the control get HID report descriptor request structure. */
|
||||
typedef struct _usb_device_get_hid_report_descriptor_struct
|
||||
{
|
||||
uint8_t *buffer; /*!< Pass the buffer address. */
|
||||
uint32_t length; /*!< Pass the buffer length. */
|
||||
uint8_t interfaceNumber; /*!< The interface number. */
|
||||
} usb_device_get_hid_report_descriptor_struct_t;
|
||||
|
||||
/*! @brief Obtains the control get HID physical descriptor request structure. */
|
||||
typedef struct _usb_device_get_hid_physical_descriptor_struct
|
||||
{
|
||||
uint8_t *buffer; /*!< Pass the buffer address. */
|
||||
uint32_t length; /*!< Pass the buffer length. */
|
||||
uint8_t index; /*!< Physical index */
|
||||
uint8_t interfaceNumber; /*!< The interface number. */
|
||||
} usb_device_get_hid_physical_descriptor_struct_t;
|
||||
|
||||
/*! @brief Obtains the control get descriptor request common union. */
|
||||
typedef union _usb_device_get_descriptor_common_union
|
||||
{
|
||||
usb_device_get_descriptor_common_struct_t commonDescriptor; /*!< Common structure. */
|
||||
usb_device_get_device_descriptor_struct_t deviceDescriptor; /*!< The structure to get device descriptor. */
|
||||
usb_device_get_device_qualifier_descriptor_struct_t
|
||||
deviceQualifierDescriptor; /*!< The structure to get device qualifier descriptor. */
|
||||
usb_device_get_configuration_descriptor_struct_t
|
||||
configurationDescriptor; /*!< The structure to get configuration descriptor. */
|
||||
usb_device_get_string_descriptor_struct_t stringDescriptor; /*!< The structure to get string descriptor. */
|
||||
usb_device_get_hid_descriptor_struct_t hidDescriptor; /*!< The structure to get HID descriptor. */
|
||||
usb_device_get_hid_report_descriptor_struct_t
|
||||
hidReportDescriptor; /*!< The structure to get HID report descriptor. */
|
||||
usb_device_get_hid_physical_descriptor_struct_t
|
||||
hidPhysicalDescriptor; /*!< The structure to get HID physical descriptor. */
|
||||
} usb_device_get_descriptor_common_union_t;
|
||||
|
||||
/*! @brief Define function type for class device instance initialization */
|
||||
typedef usb_status_t (*usb_device_class_init_call_t)(uint8_t controllerId,
|
||||
usb_device_class_config_struct_t *classConfig,
|
||||
class_handle_t *classHandle);
|
||||
/*! @brief Define function type for class device instance deinitialization, internal */
|
||||
typedef usb_status_t (*usb_device_class_deinit_call_t)(class_handle_t handle);
|
||||
/*! @brief Define function type for class device instance Event change */
|
||||
typedef usb_status_t (*usb_device_class_event_callback_t)(void *classHandle, uint32_t event, void *param);
|
||||
|
||||
/*! @brief Define class driver interface structure. */
|
||||
typedef struct _usb_device_class_map
|
||||
{
|
||||
usb_device_class_init_call_t classInit; /*!< Class driver initialization- entry of the class driver */
|
||||
usb_device_class_deinit_call_t classDeinit; /*!< Class driver de-initialization*/
|
||||
usb_device_class_event_callback_t classEventCallback; /*!< Class driver event callback*/
|
||||
usb_device_class_type_t type; /*!< Class type*/
|
||||
} usb_device_class_map_t;
|
||||
|
||||
/*! @brief Structure holding common class state information */
|
||||
typedef struct _usb_device_common_class_struct
|
||||
{
|
||||
usb_device_handle handle; /*!< USB device handle*/
|
||||
usb_device_class_config_list_struct_t *configList; /*!< USB device configure list*/
|
||||
uint8_t *setupBuffer; /*!< Setup packet data buffer*/
|
||||
uint16_t standardTranscationBuffer; /*!<
|
||||
* This variable is used in:
|
||||
* get status request
|
||||
* get configuration request
|
||||
* get interface request
|
||||
* set interface request
|
||||
* get sync frame request
|
||||
*/
|
||||
uint8_t controllerId; /*!< Controller ID*/
|
||||
} usb_device_common_class_struct_t;
|
||||
|
||||
/*******************************************************************************
|
||||
* API
|
||||
******************************************************************************/
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*!
|
||||
* @brief Initializes the common class and the supported classes.
|
||||
*
|
||||
* This function is used to initialize the common class and the supported classes.
|
||||
*
|
||||
* @param[in] controllerId The controller ID of the USB IP. See the enumeration #usb_controller_index_t.
|
||||
* @param[in] configList The class configurations. The pointer must point to the global variable.
|
||||
* See the structure #usb_device_class_config_list_struct_t.
|
||||
* @param[out] handle A parameter used to return pointer of the device handle to the caller.
|
||||
* The value of the parameter is a pointer to the device handle. This design is used to
|
||||
* make a simple device align with the composite device. For the composite device, there are
|
||||
* many
|
||||
* kinds of class handles. However, there is only one device handle. Therefore, the handle
|
||||
* points to
|
||||
* a device instead of a class. The class handle can be received from the
|
||||
* #usb_device_class_config_struct_t::classHandle after the function successfully.
|
||||
*
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
*/
|
||||
usb_status_t USB_DeviceClassInit(uint8_t controllerId,
|
||||
usb_device_class_config_list_struct_t *configList,
|
||||
usb_device_handle *handle);
|
||||
|
||||
/*!
|
||||
* @brief Deinitializes the common class and the supported classes.
|
||||
*
|
||||
* This function is used to deinitialize the common class and the supported classes.
|
||||
*
|
||||
* @param[in] controllerId The controller ID of the USB IP. See the enumeration #usb_controller_index_t.
|
||||
*
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
*/
|
||||
usb_status_t USB_DeviceClassDeinit(uint8_t controllerId);
|
||||
|
||||
/*!
|
||||
* @brief Gets the USB bus speed.
|
||||
*
|
||||
* This function is used to get the USB bus speed.
|
||||
*
|
||||
* @param[in] controllerId The controller ID of the USB IP. See the enumeration #usb_controller_index_t.
|
||||
* @param[out] speed It is an OUT parameter, which returns the current speed of the controller.
|
||||
*
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
*/
|
||||
usb_status_t USB_DeviceClassGetSpeed(uint8_t controllerId, uint8_t *speed);
|
||||
|
||||
/*!
|
||||
* @brief Handles the event passed to the class drivers.
|
||||
*
|
||||
* This function handles the event passed to the class drivers.
|
||||
*
|
||||
* @param[in] handle The device handle received from the #USB_DeviceInit.
|
||||
* @param[in] event The event codes. See the enumeration #usb_device_class_event_t.
|
||||
* @param[in,out] param The parameter type is determined by the event code.
|
||||
*
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
* @retval kStatus_USB_Success A valid request has been handled.
|
||||
* @retval kStatus_USB_InvalidParameter The device handle not be found.
|
||||
* @retval kStatus_USB_InvalidRequest The request is invalid, and the control pipe is stalled by the caller.
|
||||
*/
|
||||
usb_status_t USB_DeviceClassEvent(usb_device_handle handle, usb_device_class_event_t event, void *param);
|
||||
|
||||
/*!
|
||||
* @brief Handles the common class callback.
|
||||
*
|
||||
* This function handles the common class callback.
|
||||
*
|
||||
* @param[in] handle The device handle received from the #USB_DeviceInit.
|
||||
* @param[in] event The event codes. See the enumeration #usb_device_event_t.
|
||||
* @param[in,out] param The parameter type is determined by the event code.
|
||||
*
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
*/
|
||||
usb_status_t USB_DeviceClassCallback(usb_device_handle handle, uint32_t event, void *param);
|
||||
|
||||
/*!
|
||||
* @brief Gets the device handle according to the controller ID.
|
||||
*
|
||||
* This function gets the device handle according to the controller ID.
|
||||
*
|
||||
* @param[in] controllerId The controller ID of the USB IP. See the enumeration #usb_controller_index_t.
|
||||
* @param[out] handle An out parameter used to return the pointer of the device handle to the caller.
|
||||
*
|
||||
* @retval kStatus_USB_Success Get device handle successfully.
|
||||
* @retval kStatus_USB_InvalidParameter The device handle can't be found.
|
||||
*/
|
||||
usb_status_t USB_DeviceClassGetDeviceHandle(uint8_t controllerId, usb_device_handle *handle);
|
||||
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
#endif
|
||||
|
||||
/*! @}*/
|
||||
|
||||
#endif /* __USB_DEVICE_CLASS_H__ */
|
@@ -1,492 +1,499 @@
|
||||
/*
|
||||
* Copyright (c) 2015 - 2016, Freescale Semiconductor, Inc.
|
||||
* Copyright 2016 NXP
|
||||
* All rights reserved.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
#include "usb_device_config.h"
|
||||
#include "usb.h"
|
||||
#include "usb_device.h"
|
||||
|
||||
#include "usb_device_class.h"
|
||||
#include "usb_device_cdc_acm.h"
|
||||
|
||||
#include "usb_device_descriptor.h"
|
||||
|
||||
/*******************************************************************************
|
||||
* Variables
|
||||
******************************************************************************/
|
||||
/* Define endpoint for communication class */
|
||||
usb_device_endpoint_struct_t g_UsbDeviceCdcVcomCicEndpoints[USB_CDC_VCOM_ENDPOINT_CIC_COUNT] = {
|
||||
{
|
||||
USB_CDC_VCOM_INTERRUPT_IN_ENDPOINT | (USB_IN << 7U),
|
||||
USB_ENDPOINT_INTERRUPT,
|
||||
FS_CDC_VCOM_INTERRUPT_IN_PACKET_SIZE,
|
||||
FS_CDC_VCOM_INTERRUPT_IN_INTERVAL,
|
||||
},
|
||||
};
|
||||
|
||||
/* Define endpoint for data class */
|
||||
usb_device_endpoint_struct_t g_UsbDeviceCdcVcomDicEndpoints[USB_CDC_VCOM_ENDPOINT_DIC_COUNT] = {
|
||||
{
|
||||
USB_CDC_VCOM_BULK_IN_ENDPOINT | (USB_IN << 7U),
|
||||
USB_ENDPOINT_BULK,
|
||||
FS_CDC_VCOM_BULK_IN_PACKET_SIZE,
|
||||
0U,
|
||||
},
|
||||
{
|
||||
USB_CDC_VCOM_BULK_OUT_ENDPOINT | (USB_OUT << 7U),
|
||||
USB_ENDPOINT_BULK,
|
||||
FS_CDC_VCOM_BULK_OUT_PACKET_SIZE,
|
||||
0U,
|
||||
}};
|
||||
|
||||
/* Define interface for communication class */
|
||||
usb_device_interface_struct_t g_UsbDeviceCdcVcomCommunicationInterface[] = {{0,
|
||||
{
|
||||
USB_CDC_VCOM_ENDPOINT_CIC_COUNT,
|
||||
g_UsbDeviceCdcVcomCicEndpoints,
|
||||
},
|
||||
NULL}};
|
||||
|
||||
/* Define interface for data class */
|
||||
usb_device_interface_struct_t g_UsbDeviceCdcVcomDataInterface[] = {{0,
|
||||
{
|
||||
USB_CDC_VCOM_ENDPOINT_DIC_COUNT,
|
||||
g_UsbDeviceCdcVcomDicEndpoints,
|
||||
},
|
||||
NULL}};
|
||||
|
||||
/* Define interfaces for virtual com */
|
||||
usb_device_interfaces_struct_t g_UsbDeviceCdcVcomInterfaces[USB_CDC_VCOM_INTERFACE_COUNT] = {
|
||||
{USB_CDC_VCOM_CIC_CLASS, USB_CDC_VCOM_CIC_SUBCLASS, USB_CDC_VCOM_CIC_PROTOCOL, USB_CDC_VCOM_COMM_INTERFACE_INDEX,
|
||||
g_UsbDeviceCdcVcomCommunicationInterface,
|
||||
sizeof(g_UsbDeviceCdcVcomCommunicationInterface) / sizeof(usb_device_interface_struct_t)},
|
||||
{USB_CDC_VCOM_DIC_CLASS, USB_CDC_VCOM_DIC_SUBCLASS, USB_CDC_VCOM_DIC_PROTOCOL, USB_CDC_VCOM_DATA_INTERFACE_INDEX,
|
||||
g_UsbDeviceCdcVcomDataInterface, sizeof(g_UsbDeviceCdcVcomDataInterface) / sizeof(usb_device_interface_struct_t)},
|
||||
};
|
||||
|
||||
/* Define configurations for virtual com */
|
||||
usb_device_interface_list_t g_UsbDeviceCdcVcomInterfaceLIST_[USB_DEVICE_CONFIGURATION_COUNT] = {
|
||||
{
|
||||
USB_CDC_VCOM_INTERFACE_COUNT,
|
||||
g_UsbDeviceCdcVcomInterfaces,
|
||||
},
|
||||
};
|
||||
|
||||
/* Define class information for virtual com */
|
||||
usb_device_class_struct_t g_UsbDeviceCdcVcomConfig = {
|
||||
g_UsbDeviceCdcVcomInterfaceLIST_,
|
||||
kUSB_DeviceClassTypeCdc,
|
||||
USB_DEVICE_CONFIGURATION_COUNT,
|
||||
};
|
||||
|
||||
/* Define device descriptor */
|
||||
USB_DMA_INIT_DATA_ALIGN(USB_DATA_ALIGN_SIZE)
|
||||
uint8_t g_UsbDeviceDescriptor[] = {
|
||||
/* Size of this descriptor in bytes */
|
||||
USB_DESCRIPTOR_LENGTH_DEVICE,
|
||||
/* DEVICE Descriptor Type */
|
||||
USB_DESCRIPTOR_TYPE_DEVICE,
|
||||
/* USB Specification Release Number in Binary-Coded Decimal (i.e., 2.10 is 210H). */
|
||||
USB_SHORT_GET_LOW(USB_DEVICE_SPECIFIC_BCD_VERSION),
|
||||
USB_SHORT_GET_HIGH(USB_DEVICE_SPECIFIC_BCD_VERSION),
|
||||
/* Class code (assigned by the USB-IF). */
|
||||
USB_DEVICE_CLASS,
|
||||
/* Subclass code (assigned by the USB-IF). */
|
||||
USB_DEVICE_SUBCLASS,
|
||||
/* Protocol code (assigned by the USB-IF). */
|
||||
USB_DEVICE_PROTOCOL,
|
||||
/* Maximum packet size for endpoint zero (only 8, 16, 32, or 64 are valid) */
|
||||
USB_CONTROL_MAX_PACKET_SIZE,
|
||||
/* Vendor ID (assigned by the USB-IF) */
|
||||
0xC9U,
|
||||
0x1FU,
|
||||
/* Product ID (assigned by the manufacturer) */
|
||||
0x94,
|
||||
0x00,
|
||||
/* Device release number in binary-coded decimal */
|
||||
USB_SHORT_GET_LOW(USB_DEVICE_DEMO_BCD_VERSION),
|
||||
USB_SHORT_GET_HIGH(USB_DEVICE_DEMO_BCD_VERSION),
|
||||
/* Index of string descriptor describing manufacturer */
|
||||
0x01,
|
||||
/* Index of string descriptor describing product */
|
||||
0x02,
|
||||
/* Index of string descriptor describing the device's serial number */
|
||||
0x00,
|
||||
/* Number of possible configurations */
|
||||
USB_DEVICE_CONFIGURATION_COUNT,
|
||||
};
|
||||
|
||||
/* Define configuration descriptor */
|
||||
USB_DMA_INIT_DATA_ALIGN(USB_DATA_ALIGN_SIZE)
|
||||
uint8_t g_UsbDeviceConfigurationDescriptor[] = {
|
||||
/* Size of this descriptor in bytes */
|
||||
USB_DESCRIPTOR_LENGTH_CONFIGURE,
|
||||
/* CONFIGURATION Descriptor Type */
|
||||
USB_DESCRIPTOR_TYPE_CONFIGURE,
|
||||
/* Total length of data returned for this configuration. */
|
||||
USB_SHORT_GET_LOW(USB_DESCRIPTOR_LENGTH_CONFIGURE + USB_DESCRIPTOR_LENGTH_INTERFACE +
|
||||
USB_DESCRIPTOR_LENGTH_CDC_HEADER_FUNC + USB_DESCRIPTOR_LENGTH_CDC_CALL_MANAG +
|
||||
USB_DESCRIPTOR_LENGTH_CDC_ABSTRACT + USB_DESCRIPTOR_LENGTH_CDC_UNION_FUNC +
|
||||
USB_DESCRIPTOR_LENGTH_ENDPOINT + USB_DESCRIPTOR_LENGTH_INTERFACE +
|
||||
USB_DESCRIPTOR_LENGTH_ENDPOINT + USB_DESCRIPTOR_LENGTH_ENDPOINT),
|
||||
USB_SHORT_GET_HIGH(USB_DESCRIPTOR_LENGTH_CONFIGURE + USB_DESCRIPTOR_LENGTH_INTERFACE +
|
||||
USB_DESCRIPTOR_LENGTH_CDC_HEADER_FUNC + USB_DESCRIPTOR_LENGTH_CDC_CALL_MANAG +
|
||||
USB_DESCRIPTOR_LENGTH_CDC_ABSTRACT + USB_DESCRIPTOR_LENGTH_CDC_UNION_FUNC +
|
||||
USB_DESCRIPTOR_LENGTH_ENDPOINT + USB_DESCRIPTOR_LENGTH_INTERFACE +
|
||||
USB_DESCRIPTOR_LENGTH_ENDPOINT + USB_DESCRIPTOR_LENGTH_ENDPOINT),
|
||||
/* Number of interfaces supported by this configuration */
|
||||
USB_CDC_VCOM_INTERFACE_COUNT,
|
||||
/* Value to use as an argument to the SetConfiguration() request to select this configuration */
|
||||
USB_CDC_VCOM_CONFIGURE_INDEX,
|
||||
/* Index of string descriptor describing this configuration */
|
||||
0,
|
||||
/* Configuration characteristics D7: Reserved (set to one) D6: Self-powered D5: Remote Wakeup D4...0: Reserved
|
||||
(reset to zero) */
|
||||
(USB_DESCRIPTOR_CONFIGURE_ATTRIBUTE_D7_MASK) |
|
||||
(USB_DEVICE_CONFIG_SELF_POWER << USB_DESCRIPTOR_CONFIGURE_ATTRIBUTE_SELF_POWERED_SHIFT) |
|
||||
(USB_DEVICE_CONFIG_REMOTE_WAKEUP << USB_DESCRIPTOR_CONFIGURE_ATTRIBUTE_REMOTE_WAKEUP_SHIFT),
|
||||
/* Maximum power consumption of the USB * device from the bus in this specific * configuration when the device is
|
||||
fully * operational. Expressed in 2 mA units * (i.e., 50 = 100 mA). */
|
||||
USB_DEVICE_MAX_POWER,
|
||||
|
||||
/* Communication Interface Descriptor */
|
||||
USB_DESCRIPTOR_LENGTH_INTERFACE, USB_DESCRIPTOR_TYPE_INTERFACE, USB_CDC_VCOM_COMM_INTERFACE_INDEX, 0x00,
|
||||
USB_CDC_VCOM_ENDPOINT_CIC_COUNT, USB_CDC_VCOM_CIC_CLASS, USB_CDC_VCOM_CIC_SUBCLASS, USB_CDC_VCOM_CIC_PROTOCOL,
|
||||
0x00, /* Interface Description String Index*/
|
||||
|
||||
/* CDC Class-Specific descriptor */
|
||||
USB_DESCRIPTOR_LENGTH_CDC_HEADER_FUNC, /* Size of this descriptor in bytes */
|
||||
USB_DESCRIPTOR_TYPE_CDC_CS_INTERFACE, /* CS_INTERFACE Descriptor Type */
|
||||
USB_CDC_HEADER_FUNC_DESC, 0x10,
|
||||
0x01, /* USB Class Definitions for Communications the Communication specification version 1.10 */
|
||||
|
||||
USB_DESCRIPTOR_LENGTH_CDC_CALL_MANAG, /* Size of this descriptor in bytes */
|
||||
USB_DESCRIPTOR_TYPE_CDC_CS_INTERFACE, /* CS_INTERFACE Descriptor Type */
|
||||
USB_CDC_CALL_MANAGEMENT_FUNC_DESC,
|
||||
0x01, /*Bit 0: Whether device handle call management itself 1, Bit 1: Whether device can send/receive call
|
||||
management information over a Data Class Interface 0 */
|
||||
0x01, /* Indicates multiplexed commands are handled via data interface */
|
||||
|
||||
USB_DESCRIPTOR_LENGTH_CDC_ABSTRACT, /* Size of this descriptor in bytes */
|
||||
USB_DESCRIPTOR_TYPE_CDC_CS_INTERFACE, /* CS_INTERFACE Descriptor Type */
|
||||
USB_CDC_ABSTRACT_CONTROL_FUNC_DESC,
|
||||
0x06, /* Bit 0: Whether device supports the request combination of Set_Comm_Feature, Clear_Comm_Feature, and
|
||||
Get_Comm_Feature 0, Bit 1: Whether device supports the request combination of Set_Line_Coding,
|
||||
Set_Control_Line_State, Get_Line_Coding, and the notification Serial_State 1, Bit ... */
|
||||
|
||||
USB_DESCRIPTOR_LENGTH_CDC_UNION_FUNC, /* Size of this descriptor in bytes */
|
||||
USB_DESCRIPTOR_TYPE_CDC_CS_INTERFACE, /* CS_INTERFACE Descriptor Type */
|
||||
USB_CDC_UNION_FUNC_DESC, 0x00, /* The interface number of the Communications or Data Class interface */
|
||||
0x01, /* Interface number of subordinate interface in the Union */
|
||||
|
||||
/*Notification Endpoint descriptor */
|
||||
USB_DESCRIPTOR_LENGTH_ENDPOINT, USB_DESCRIPTOR_TYPE_ENDPOINT, USB_CDC_VCOM_INTERRUPT_IN_ENDPOINT | (USB_IN << 7U),
|
||||
USB_ENDPOINT_INTERRUPT, USB_SHORT_GET_LOW(FS_CDC_VCOM_INTERRUPT_IN_PACKET_SIZE),
|
||||
USB_SHORT_GET_HIGH(FS_CDC_VCOM_INTERRUPT_IN_PACKET_SIZE), FS_CDC_VCOM_INTERRUPT_IN_INTERVAL,
|
||||
|
||||
/* Data Interface Descriptor */
|
||||
USB_DESCRIPTOR_LENGTH_INTERFACE, USB_DESCRIPTOR_TYPE_INTERFACE, USB_CDC_VCOM_DATA_INTERFACE_INDEX, 0x00,
|
||||
USB_CDC_VCOM_ENDPOINT_DIC_COUNT, USB_CDC_VCOM_DIC_CLASS, USB_CDC_VCOM_DIC_SUBCLASS, USB_CDC_VCOM_DIC_PROTOCOL,
|
||||
0x00, /* Interface Description String Index*/
|
||||
|
||||
/*Bulk IN Endpoint descriptor */
|
||||
USB_DESCRIPTOR_LENGTH_ENDPOINT, USB_DESCRIPTOR_TYPE_ENDPOINT, USB_CDC_VCOM_BULK_IN_ENDPOINT | (USB_IN << 7U),
|
||||
USB_ENDPOINT_BULK, USB_SHORT_GET_LOW(FS_CDC_VCOM_BULK_IN_PACKET_SIZE),
|
||||
USB_SHORT_GET_HIGH(FS_CDC_VCOM_BULK_IN_PACKET_SIZE), 0x00, /* The polling interval value is every 0 Frames */
|
||||
|
||||
/*Bulk OUT Endpoint descriptor */
|
||||
USB_DESCRIPTOR_LENGTH_ENDPOINT, USB_DESCRIPTOR_TYPE_ENDPOINT, USB_CDC_VCOM_BULK_OUT_ENDPOINT | (USB_OUT << 7U),
|
||||
USB_ENDPOINT_BULK, USB_SHORT_GET_LOW(FS_CDC_VCOM_BULK_OUT_PACKET_SIZE),
|
||||
USB_SHORT_GET_HIGH(FS_CDC_VCOM_BULK_OUT_PACKET_SIZE), 0x00, /* The polling interval value is every 0 Frames */
|
||||
};
|
||||
|
||||
/* Define string descriptor */
|
||||
USB_DMA_INIT_DATA_ALIGN(USB_DATA_ALIGN_SIZE)
|
||||
uint8_t g_UsbDeviceString0[] = {2U + 2U, USB_DESCRIPTOR_TYPE_STRING, 0x09, 0x04};
|
||||
|
||||
USB_DMA_INIT_DATA_ALIGN(USB_DATA_ALIGN_SIZE)
|
||||
uint8_t g_UsbDeviceString1[] = {
|
||||
2U + 2U * 18U, USB_DESCRIPTOR_TYPE_STRING,
|
||||
'N', 0x00U,
|
||||
'X', 0x00U,
|
||||
'P', 0x00U,
|
||||
' ', 0x00U,
|
||||
'S', 0x00U,
|
||||
'E', 0x00U,
|
||||
'M', 0x00U,
|
||||
'I', 0x00U,
|
||||
'C', 0x00U,
|
||||
'O', 0x00U,
|
||||
'N', 0x00U,
|
||||
'D', 0x00U,
|
||||
'U', 0x00U,
|
||||
'C', 0x00U,
|
||||
'T', 0x00U,
|
||||
'O', 0x00U,
|
||||
'R', 0x00U,
|
||||
'S', 0x00U,
|
||||
};
|
||||
|
||||
USB_DMA_INIT_DATA_ALIGN(USB_DATA_ALIGN_SIZE)
|
||||
uint8_t g_UsbDeviceString2[] = {2U + 2U * 20U, USB_DESCRIPTOR_TYPE_STRING,
|
||||
'M', 0,
|
||||
'C', 0,
|
||||
'U', 0,
|
||||
' ', 0,
|
||||
'V', 0,
|
||||
'I', 0,
|
||||
'R', 0,
|
||||
'T', 0,
|
||||
'U', 0,
|
||||
'A', 0,
|
||||
'L', 0,
|
||||
' ', 0,
|
||||
'C', 0,
|
||||
'O', 0,
|
||||
'M', 0,
|
||||
' ', 0,
|
||||
'D', 0,
|
||||
'E', 0,
|
||||
'M', 0,
|
||||
'O', 0};
|
||||
|
||||
uint8_t *g_UsbDeviceStringDescriptorArray[USB_DEVICE_STRING_COUNT] = {g_UsbDeviceString0, g_UsbDeviceString1,
|
||||
g_UsbDeviceString2};
|
||||
|
||||
/* Define string descriptor size */
|
||||
uint32_t g_UsbDeviceStringDescriptorLength[USB_DEVICE_STRING_COUNT] = {
|
||||
sizeof(g_UsbDeviceString0), sizeof(g_UsbDeviceString1), sizeof(g_UsbDeviceString2)};
|
||||
usb_language_t g_UsbDeviceLanguage[USB_DEVICE_LANGUAGE_COUNT] = {{
|
||||
g_UsbDeviceStringDescriptorArray,
|
||||
g_UsbDeviceStringDescriptorLength,
|
||||
(uint16_t)0x0409,
|
||||
}};
|
||||
|
||||
usb_language_list_t g_UsbDeviceLanguageList = {
|
||||
g_UsbDeviceString0,
|
||||
sizeof(g_UsbDeviceString0),
|
||||
g_UsbDeviceLanguage,
|
||||
USB_DEVICE_LANGUAGE_COUNT,
|
||||
};
|
||||
|
||||
/*******************************************************************************
|
||||
* Code
|
||||
******************************************************************************/
|
||||
/*!
|
||||
* @brief USB device get device descriptor function.
|
||||
*
|
||||
* This function gets the device descriptor of the USB device.
|
||||
*
|
||||
* @param handle The USB device handle.
|
||||
* @param deviceDescriptor The pointer to the device descriptor structure.
|
||||
*
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
*/
|
||||
usb_status_t USB_DeviceGetDeviceDescriptor(usb_device_handle handle,
|
||||
usb_device_get_device_descriptor_struct_t *deviceDescriptor)
|
||||
{
|
||||
deviceDescriptor->buffer = g_UsbDeviceDescriptor;
|
||||
deviceDescriptor->length = USB_DESCRIPTOR_LENGTH_DEVICE;
|
||||
return kStatus_USB_Success;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief USB device get configuration descriptor function.
|
||||
*
|
||||
* This function gets the configuration descriptor of the USB device.
|
||||
*
|
||||
* @param handle The USB device handle.
|
||||
* @param configurationDescriptor The pointer to the configuration descriptor structure.
|
||||
*
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
*/
|
||||
usb_status_t USB_DeviceGetConfigurationDescriptor(
|
||||
usb_device_handle handle, usb_device_get_configuration_descriptor_struct_t *configurationDescriptor)
|
||||
{
|
||||
if (USB_CDC_VCOM_CONFIGURE_INDEX > configurationDescriptor->configuration)
|
||||
{
|
||||
configurationDescriptor->buffer = g_UsbDeviceConfigurationDescriptor;
|
||||
configurationDescriptor->length = USB_DESCRIPTOR_LENGTH_CONFIGURATION_ALL;
|
||||
return kStatus_USB_Success;
|
||||
}
|
||||
return kStatus_USB_InvalidRequest;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief USB device get string descriptor function.
|
||||
*
|
||||
* This function gets the string descriptor of the USB device.
|
||||
*
|
||||
* @param handle The USB device handle.
|
||||
* @param stringDescriptor Pointer to the string descriptor structure.
|
||||
*
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
*/
|
||||
usb_status_t USB_DeviceGetStringDescriptor(usb_device_handle handle,
|
||||
usb_device_get_string_descriptor_struct_t *stringDescriptor)
|
||||
{
|
||||
if (stringDescriptor->stringIndex == 0U)
|
||||
{
|
||||
stringDescriptor->buffer = (uint8_t *)g_UsbDeviceLanguageList.languageString;
|
||||
stringDescriptor->length = g_UsbDeviceLanguageList.stringLength;
|
||||
}
|
||||
else
|
||||
{
|
||||
uint8_t languageId = 0U;
|
||||
uint8_t languageIndex = USB_DEVICE_STRING_COUNT;
|
||||
|
||||
for (; languageId < USB_DEVICE_LANGUAGE_COUNT; languageId++)
|
||||
{
|
||||
if (stringDescriptor->languageId == g_UsbDeviceLanguageList.languageList[languageId].languageId)
|
||||
{
|
||||
if (stringDescriptor->stringIndex < USB_DEVICE_STRING_COUNT)
|
||||
{
|
||||
languageIndex = stringDescriptor->stringIndex;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (USB_DEVICE_STRING_COUNT == languageIndex)
|
||||
{
|
||||
return kStatus_USB_InvalidRequest;
|
||||
}
|
||||
stringDescriptor->buffer = (uint8_t *)g_UsbDeviceLanguageList.languageList[languageId].string[languageIndex];
|
||||
stringDescriptor->length = g_UsbDeviceLanguageList.languageList[languageId].length[languageIndex];
|
||||
}
|
||||
return kStatus_USB_Success;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief USB device set speed function.
|
||||
*
|
||||
* This function sets the speed of the USB device.
|
||||
*
|
||||
* Due to the difference of HS and FS descriptors, the device descriptors and configurations need to be updated to match
|
||||
* current speed.
|
||||
* As the default, the device descriptors and configurations are configured by using FS parameters for both EHCI and
|
||||
* KHCI.
|
||||
* When the EHCI is enabled, the application needs to call this function to update device by using current speed.
|
||||
* The updated information includes endpoint max packet size, endpoint interval, etc.
|
||||
*
|
||||
* @param handle The USB device handle.
|
||||
* @param speed Speed type. USB_SPEED_HIGH/USB_SPEED_FULL/USB_SPEED_LOW.
|
||||
*
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
*/
|
||||
usb_status_t USB_DeviceSetSpeed(usb_device_handle handle, uint8_t speed)
|
||||
{
|
||||
usb_descriptor_union_t *ptr1;
|
||||
usb_descriptor_union_t *ptr2;
|
||||
|
||||
ptr1 = (usb_descriptor_union_t *)(&g_UsbDeviceConfigurationDescriptor[0]);
|
||||
ptr2 = (usb_descriptor_union_t *)(&g_UsbDeviceConfigurationDescriptor[USB_DESCRIPTOR_LENGTH_CONFIGURATION_ALL - 1]);
|
||||
|
||||
while (ptr1 < ptr2)
|
||||
{
|
||||
if (ptr1->common.bDescriptorType == USB_DESCRIPTOR_TYPE_ENDPOINT)
|
||||
{
|
||||
if (USB_SPEED_HIGH == speed)
|
||||
{
|
||||
if (((ptr1->endpoint.bEndpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) ==
|
||||
USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_IN) &&
|
||||
(USB_CDC_VCOM_INTERRUPT_IN_ENDPOINT ==
|
||||
(ptr1->endpoint.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK)))
|
||||
{
|
||||
ptr1->endpoint.bInterval = HS_CDC_VCOM_INTERRUPT_IN_INTERVAL;
|
||||
USB_SHORT_TO_LITTLE_ENDIAN_ADDRESS(HS_CDC_VCOM_INTERRUPT_IN_PACKET_SIZE,
|
||||
ptr1->endpoint.wMaxPacketSize);
|
||||
}
|
||||
else if (((ptr1->endpoint.bEndpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) ==
|
||||
USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_IN) &&
|
||||
(USB_CDC_VCOM_BULK_IN_ENDPOINT ==
|
||||
(ptr1->endpoint.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK)))
|
||||
{
|
||||
USB_SHORT_TO_LITTLE_ENDIAN_ADDRESS(HS_CDC_VCOM_BULK_IN_PACKET_SIZE, ptr1->endpoint.wMaxPacketSize);
|
||||
}
|
||||
else if (((ptr1->endpoint.bEndpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) ==
|
||||
USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_OUT) &&
|
||||
(USB_CDC_VCOM_BULK_OUT_ENDPOINT ==
|
||||
(ptr1->endpoint.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK)))
|
||||
{
|
||||
USB_SHORT_TO_LITTLE_ENDIAN_ADDRESS(HS_CDC_VCOM_BULK_OUT_PACKET_SIZE, ptr1->endpoint.wMaxPacketSize);
|
||||
}
|
||||
else
|
||||
{
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (((ptr1->endpoint.bEndpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) ==
|
||||
USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_IN) &&
|
||||
(USB_CDC_VCOM_INTERRUPT_IN_ENDPOINT ==
|
||||
(ptr1->endpoint.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK)))
|
||||
{
|
||||
ptr1->endpoint.bInterval = FS_CDC_VCOM_INTERRUPT_IN_INTERVAL;
|
||||
USB_SHORT_TO_LITTLE_ENDIAN_ADDRESS(FS_CDC_VCOM_INTERRUPT_IN_PACKET_SIZE,
|
||||
ptr1->endpoint.wMaxPacketSize);
|
||||
}
|
||||
else if (((ptr1->endpoint.bEndpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) ==
|
||||
USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_IN) &&
|
||||
(USB_CDC_VCOM_BULK_IN_ENDPOINT ==
|
||||
(ptr1->endpoint.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK)))
|
||||
{
|
||||
USB_SHORT_TO_LITTLE_ENDIAN_ADDRESS(FS_CDC_VCOM_BULK_IN_PACKET_SIZE, ptr1->endpoint.wMaxPacketSize);
|
||||
}
|
||||
else if (((ptr1->endpoint.bEndpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) ==
|
||||
USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_OUT) &&
|
||||
(USB_CDC_VCOM_BULK_OUT_ENDPOINT ==
|
||||
(ptr1->endpoint.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK)))
|
||||
{
|
||||
USB_SHORT_TO_LITTLE_ENDIAN_ADDRESS(FS_CDC_VCOM_BULK_OUT_PACKET_SIZE, ptr1->endpoint.wMaxPacketSize);
|
||||
}
|
||||
else
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
ptr1 = (usb_descriptor_union_t *)((uint8_t *)ptr1 + ptr1->common.bLength);
|
||||
}
|
||||
|
||||
for (int i = 0; i < USB_CDC_VCOM_ENDPOINT_CIC_COUNT; i++)
|
||||
{
|
||||
if (USB_SPEED_HIGH == speed)
|
||||
{
|
||||
g_UsbDeviceCdcVcomCicEndpoints[i].maxPacketSize = HS_CDC_VCOM_INTERRUPT_IN_PACKET_SIZE;
|
||||
}
|
||||
else
|
||||
{
|
||||
g_UsbDeviceCdcVcomCicEndpoints[i].maxPacketSize = FS_CDC_VCOM_INTERRUPT_IN_PACKET_SIZE;
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < USB_CDC_VCOM_ENDPOINT_DIC_COUNT; i++)
|
||||
{
|
||||
if (USB_SPEED_HIGH == speed)
|
||||
{
|
||||
if (g_UsbDeviceCdcVcomDicEndpoints[i].endpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK)
|
||||
{
|
||||
g_UsbDeviceCdcVcomDicEndpoints[i].maxPacketSize = HS_CDC_VCOM_BULK_IN_PACKET_SIZE;
|
||||
}
|
||||
else
|
||||
{
|
||||
g_UsbDeviceCdcVcomDicEndpoints[i].maxPacketSize = HS_CDC_VCOM_BULK_OUT_PACKET_SIZE;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (g_UsbDeviceCdcVcomDicEndpoints[i].endpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK)
|
||||
{
|
||||
g_UsbDeviceCdcVcomDicEndpoints[i].maxPacketSize = FS_CDC_VCOM_BULK_IN_PACKET_SIZE;
|
||||
}
|
||||
else
|
||||
{
|
||||
g_UsbDeviceCdcVcomDicEndpoints[i].maxPacketSize = FS_CDC_VCOM_BULK_OUT_PACKET_SIZE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return kStatus_USB_Success;
|
||||
}
|
||||
/*
|
||||
* Copyright (c) 2015 - 2016, Freescale Semiconductor, Inc.
|
||||
* Copyright 2016 NXP
|
||||
* All rights reserved.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
#include "usb_device_config.h"
|
||||
#include "usb.h"
|
||||
#include "usb_device.h"
|
||||
|
||||
#include "usb_device_class.h"
|
||||
#include "usb_device_cdc_acm.h"
|
||||
|
||||
#include "usb_device_descriptor.h"
|
||||
|
||||
/*******************************************************************************
|
||||
* Variables
|
||||
******************************************************************************/
|
||||
/* Define endpoint for communication class */
|
||||
usb_device_endpoint_struct_t g_UsbDeviceCdcVcomCicEndpoints[USB_CDC_VCOM_ENDPOINT_CIC_COUNT] = {
|
||||
{
|
||||
USB_CDC_VCOM_INTERRUPT_IN_ENDPOINT | (USB_IN << 7U),
|
||||
USB_ENDPOINT_INTERRUPT,
|
||||
FS_CDC_VCOM_INTERRUPT_IN_PACKET_SIZE,
|
||||
FS_CDC_VCOM_INTERRUPT_IN_INTERVAL,
|
||||
},
|
||||
};
|
||||
|
||||
/* Define endpoint for data class */
|
||||
usb_device_endpoint_struct_t g_UsbDeviceCdcVcomDicEndpoints[USB_CDC_VCOM_ENDPOINT_DIC_COUNT] = {
|
||||
{
|
||||
USB_CDC_VCOM_BULK_IN_ENDPOINT | (USB_IN << 7U),
|
||||
USB_ENDPOINT_BULK,
|
||||
FS_CDC_VCOM_BULK_IN_PACKET_SIZE,
|
||||
0U,
|
||||
},
|
||||
{
|
||||
USB_CDC_VCOM_BULK_OUT_ENDPOINT | (USB_OUT << 7U),
|
||||
USB_ENDPOINT_BULK,
|
||||
FS_CDC_VCOM_BULK_OUT_PACKET_SIZE,
|
||||
0U,
|
||||
}};
|
||||
|
||||
/* Define interface for communication class */
|
||||
usb_device_interface_struct_t g_UsbDeviceCdcVcomCommunicationInterface[] = {{0,
|
||||
{
|
||||
USB_CDC_VCOM_ENDPOINT_CIC_COUNT,
|
||||
g_UsbDeviceCdcVcomCicEndpoints,
|
||||
},
|
||||
NULL}};
|
||||
|
||||
/* Define interface for data class */
|
||||
usb_device_interface_struct_t g_UsbDeviceCdcVcomDataInterface[] = {{0,
|
||||
{
|
||||
USB_CDC_VCOM_ENDPOINT_DIC_COUNT,
|
||||
g_UsbDeviceCdcVcomDicEndpoints,
|
||||
},
|
||||
NULL}};
|
||||
|
||||
/* Define interfaces for virtual com */
|
||||
usb_device_interfaces_struct_t g_UsbDeviceCdcVcomInterfaces[USB_CDC_VCOM_INTERFACE_COUNT] = {
|
||||
{USB_CDC_VCOM_CIC_CLASS, USB_CDC_VCOM_CIC_SUBCLASS, USB_CDC_VCOM_CIC_PROTOCOL, USB_CDC_VCOM_COMM_INTERFACE_INDEX,
|
||||
g_UsbDeviceCdcVcomCommunicationInterface,
|
||||
sizeof(g_UsbDeviceCdcVcomCommunicationInterface) / sizeof(usb_device_interface_struct_t)},
|
||||
{USB_CDC_VCOM_DIC_CLASS, USB_CDC_VCOM_DIC_SUBCLASS, USB_CDC_VCOM_DIC_PROTOCOL, USB_CDC_VCOM_DATA_INTERFACE_INDEX,
|
||||
g_UsbDeviceCdcVcomDataInterface, sizeof(g_UsbDeviceCdcVcomDataInterface) / sizeof(usb_device_interface_struct_t)},
|
||||
};
|
||||
|
||||
/* Define configurations for virtual com */
|
||||
usb_device_interface_list_t g_UsbDeviceCdcVcomInterfaceLIST_[USB_DEVICE_CONFIGURATION_COUNT] = {
|
||||
{
|
||||
USB_CDC_VCOM_INTERFACE_COUNT,
|
||||
g_UsbDeviceCdcVcomInterfaces,
|
||||
},
|
||||
};
|
||||
|
||||
/* Define class information for virtual com */
|
||||
usb_device_class_struct_t g_UsbDeviceCdcVcomConfig = {
|
||||
g_UsbDeviceCdcVcomInterfaceLIST_,
|
||||
kUSB_DeviceClassTypeCdc,
|
||||
USB_DEVICE_CONFIGURATION_COUNT,
|
||||
};
|
||||
|
||||
/* Define device descriptor */
|
||||
USB_DMA_INIT_DATA_ALIGN(USB_DATA_ALIGN_SIZE)
|
||||
uint8_t g_UsbDeviceDescriptor[] = {
|
||||
/* Size of this descriptor in bytes */
|
||||
USB_DESCRIPTOR_LENGTH_DEVICE,
|
||||
/* DEVICE Descriptor Type */
|
||||
USB_DESCRIPTOR_TYPE_DEVICE,
|
||||
/* USB Specification Release Number in Binary-Coded Decimal (i.e., 2.10 is 210H). */
|
||||
USB_SHORT_GET_LOW(USB_DEVICE_SPECIFIC_BCD_VERSION),
|
||||
USB_SHORT_GET_HIGH(USB_DEVICE_SPECIFIC_BCD_VERSION),
|
||||
/* Class code (assigned by the USB-IF). */
|
||||
USB_DEVICE_CLASS,
|
||||
/* Subclass code (assigned by the USB-IF). */
|
||||
USB_DEVICE_SUBCLASS,
|
||||
/* Protocol code (assigned by the USB-IF). */
|
||||
USB_DEVICE_PROTOCOL,
|
||||
/* Maximum packet size for endpoint zero (only 8, 16, 32, or 64 are valid) */
|
||||
USB_CONTROL_MAX_PACKET_SIZE,
|
||||
/* Vendor ID (assigned by the USB-IF) */
|
||||
0xC9U,
|
||||
0x1FU,
|
||||
/* Product ID (assigned by the manufacturer) */
|
||||
0x94,
|
||||
0x00,
|
||||
/* Device release number in binary-coded decimal */
|
||||
USB_SHORT_GET_LOW(USB_DEVICE_DEMO_BCD_VERSION),
|
||||
USB_SHORT_GET_HIGH(USB_DEVICE_DEMO_BCD_VERSION),
|
||||
/* Index of string descriptor describing manufacturer */
|
||||
0x01,
|
||||
/* Index of string descriptor describing product */
|
||||
0x02,
|
||||
/* Index of string descriptor describing the device's serial number */
|
||||
0x00,
|
||||
/* Number of possible configurations */
|
||||
USB_DEVICE_CONFIGURATION_COUNT,
|
||||
};
|
||||
|
||||
/* Define configuration descriptor */
|
||||
USB_DMA_INIT_DATA_ALIGN(USB_DATA_ALIGN_SIZE)
|
||||
uint8_t g_UsbDeviceConfigurationDescriptor[] = {
|
||||
/* Size of this descriptor in bytes */
|
||||
USB_DESCRIPTOR_LENGTH_CONFIGURE,
|
||||
/* CONFIGURATION Descriptor Type */
|
||||
USB_DESCRIPTOR_TYPE_CONFIGURE,
|
||||
/* Total length of data returned for this configuration. */
|
||||
USB_SHORT_GET_LOW(USB_DESCRIPTOR_LENGTH_CONFIGURE + USB_DESCRIPTOR_LENGTH_INTERFACE +
|
||||
USB_DESCRIPTOR_LENGTH_CDC_HEADER_FUNC + USB_DESCRIPTOR_LENGTH_CDC_CALL_MANAG +
|
||||
USB_DESCRIPTOR_LENGTH_CDC_ABSTRACT + USB_DESCRIPTOR_LENGTH_CDC_UNION_FUNC +
|
||||
USB_DESCRIPTOR_LENGTH_ENDPOINT + USB_DESCRIPTOR_LENGTH_INTERFACE +
|
||||
USB_DESCRIPTOR_LENGTH_ENDPOINT + USB_DESCRIPTOR_LENGTH_ENDPOINT),
|
||||
USB_SHORT_GET_HIGH(USB_DESCRIPTOR_LENGTH_CONFIGURE + USB_DESCRIPTOR_LENGTH_INTERFACE +
|
||||
USB_DESCRIPTOR_LENGTH_CDC_HEADER_FUNC + USB_DESCRIPTOR_LENGTH_CDC_CALL_MANAG +
|
||||
USB_DESCRIPTOR_LENGTH_CDC_ABSTRACT + USB_DESCRIPTOR_LENGTH_CDC_UNION_FUNC +
|
||||
USB_DESCRIPTOR_LENGTH_ENDPOINT + USB_DESCRIPTOR_LENGTH_INTERFACE +
|
||||
USB_DESCRIPTOR_LENGTH_ENDPOINT + USB_DESCRIPTOR_LENGTH_ENDPOINT),
|
||||
/* Number of interfaces supported by this configuration */
|
||||
USB_CDC_VCOM_INTERFACE_COUNT,
|
||||
/* Value to use as an argument to the SetConfiguration() request to select this configuration */
|
||||
USB_CDC_VCOM_CONFIGURE_INDEX,
|
||||
/* Index of string descriptor describing this configuration */
|
||||
0,
|
||||
/* Configuration characteristics D7: Reserved (set to one) D6: Self-powered D5: Remote Wakeup D4...0: Reserved
|
||||
(reset to zero) */
|
||||
(USB_DESCRIPTOR_CONFIGURE_ATTRIBUTE_D7_MASK) |
|
||||
(USB_DEVICE_CONFIG_SELF_POWER << USB_DESCRIPTOR_CONFIGURE_ATTRIBUTE_SELF_POWERED_SHIFT) |
|
||||
(USB_DEVICE_CONFIG_REMOTE_WAKEUP << USB_DESCRIPTOR_CONFIGURE_ATTRIBUTE_REMOTE_WAKEUP_SHIFT),
|
||||
/* Maximum power consumption of the USB * device from the bus in this specific * configuration when the device is
|
||||
fully * operational. Expressed in 2 mA units * (i.e., 50 = 100 mA). */
|
||||
USB_DEVICE_MAX_POWER,
|
||||
|
||||
/* Communication Interface Descriptor */
|
||||
USB_DESCRIPTOR_LENGTH_INTERFACE, USB_DESCRIPTOR_TYPE_INTERFACE, USB_CDC_VCOM_COMM_INTERFACE_INDEX, 0x00,
|
||||
USB_CDC_VCOM_ENDPOINT_CIC_COUNT, USB_CDC_VCOM_CIC_CLASS, USB_CDC_VCOM_CIC_SUBCLASS, USB_CDC_VCOM_CIC_PROTOCOL,
|
||||
0x00, /* Interface Description String Index*/
|
||||
|
||||
/* CDC Class-Specific descriptor */
|
||||
USB_DESCRIPTOR_LENGTH_CDC_HEADER_FUNC, /* Size of this descriptor in bytes */
|
||||
USB_DESCRIPTOR_TYPE_CDC_CS_INTERFACE, /* CS_INTERFACE Descriptor Type */
|
||||
USB_CDC_HEADER_FUNC_DESC, 0x10,
|
||||
0x01, /* USB Class Definitions for Communications the Communication specification version 1.10 */
|
||||
|
||||
USB_DESCRIPTOR_LENGTH_CDC_CALL_MANAG, /* Size of this descriptor in bytes */
|
||||
USB_DESCRIPTOR_TYPE_CDC_CS_INTERFACE, /* CS_INTERFACE Descriptor Type */
|
||||
USB_CDC_CALL_MANAGEMENT_FUNC_DESC,
|
||||
0x01, /*Bit 0: Whether device handle call management itself 1, Bit 1: Whether device can send/receive call
|
||||
management information over a Data Class Interface 0 */
|
||||
0x01, /* Indicates multiplexed commands are handled via data interface */
|
||||
|
||||
USB_DESCRIPTOR_LENGTH_CDC_ABSTRACT, /* Size of this descriptor in bytes */
|
||||
USB_DESCRIPTOR_TYPE_CDC_CS_INTERFACE, /* CS_INTERFACE Descriptor Type */
|
||||
USB_CDC_ABSTRACT_CONTROL_FUNC_DESC,
|
||||
0x06, /* Bit 0: Whether device supports the request combination of Set_Comm_Feature, Clear_Comm_Feature, and
|
||||
Get_Comm_Feature 0, Bit 1: Whether device supports the request combination of Set_Line_Coding,
|
||||
Set_Control_Line_State, Get_Line_Coding, and the notification Serial_State 1, Bit ... */
|
||||
|
||||
USB_DESCRIPTOR_LENGTH_CDC_UNION_FUNC, /* Size of this descriptor in bytes */
|
||||
USB_DESCRIPTOR_TYPE_CDC_CS_INTERFACE, /* CS_INTERFACE Descriptor Type */
|
||||
USB_CDC_UNION_FUNC_DESC, 0x00, /* The interface number of the Communications or Data Class interface */
|
||||
0x01, /* Interface number of subordinate interface in the Union */
|
||||
|
||||
/*Notification Endpoint descriptor */
|
||||
USB_DESCRIPTOR_LENGTH_ENDPOINT, USB_DESCRIPTOR_TYPE_ENDPOINT, USB_CDC_VCOM_INTERRUPT_IN_ENDPOINT | (USB_IN << 7U),
|
||||
USB_ENDPOINT_INTERRUPT, USB_SHORT_GET_LOW(FS_CDC_VCOM_INTERRUPT_IN_PACKET_SIZE),
|
||||
USB_SHORT_GET_HIGH(FS_CDC_VCOM_INTERRUPT_IN_PACKET_SIZE), FS_CDC_VCOM_INTERRUPT_IN_INTERVAL,
|
||||
|
||||
/* Data Interface Descriptor */
|
||||
USB_DESCRIPTOR_LENGTH_INTERFACE, USB_DESCRIPTOR_TYPE_INTERFACE, USB_CDC_VCOM_DATA_INTERFACE_INDEX, 0x00,
|
||||
USB_CDC_VCOM_ENDPOINT_DIC_COUNT, USB_CDC_VCOM_DIC_CLASS, USB_CDC_VCOM_DIC_SUBCLASS, USB_CDC_VCOM_DIC_PROTOCOL,
|
||||
0x00, /* Interface Description String Index*/
|
||||
|
||||
/*Bulk IN Endpoint descriptor */
|
||||
USB_DESCRIPTOR_LENGTH_ENDPOINT, USB_DESCRIPTOR_TYPE_ENDPOINT, USB_CDC_VCOM_BULK_IN_ENDPOINT | (USB_IN << 7U),
|
||||
USB_ENDPOINT_BULK, USB_SHORT_GET_LOW(FS_CDC_VCOM_BULK_IN_PACKET_SIZE),
|
||||
USB_SHORT_GET_HIGH(FS_CDC_VCOM_BULK_IN_PACKET_SIZE), 0x00, /* The polling interval value is every 0 Frames */
|
||||
|
||||
/*Bulk OUT Endpoint descriptor */
|
||||
USB_DESCRIPTOR_LENGTH_ENDPOINT, USB_DESCRIPTOR_TYPE_ENDPOINT, USB_CDC_VCOM_BULK_OUT_ENDPOINT | (USB_OUT << 7U),
|
||||
USB_ENDPOINT_BULK, USB_SHORT_GET_LOW(FS_CDC_VCOM_BULK_OUT_PACKET_SIZE),
|
||||
USB_SHORT_GET_HIGH(FS_CDC_VCOM_BULK_OUT_PACKET_SIZE), 0x00, /* The polling interval value is every 0 Frames */
|
||||
};
|
||||
|
||||
/* Define string descriptor */
|
||||
USB_DMA_INIT_DATA_ALIGN(USB_DATA_ALIGN_SIZE)
|
||||
uint8_t g_UsbDeviceString0[] = {2U + 2U, USB_DESCRIPTOR_TYPE_STRING, 0x09, 0x04};
|
||||
|
||||
USB_DMA_INIT_DATA_ALIGN(USB_DATA_ALIGN_SIZE)
|
||||
uint8_t g_UsbDeviceString1[] = {
|
||||
2U + 2U * 18U, USB_DESCRIPTOR_TYPE_STRING,
|
||||
(uint8_t)'N', 0x00U,
|
||||
(uint8_t)'X', 0x00U,
|
||||
(uint8_t)'P', 0x00U,
|
||||
(uint8_t)' ', 0x00U,
|
||||
(uint8_t)'S', 0x00U,
|
||||
(uint8_t)'E', 0x00U,
|
||||
(uint8_t)'M', 0x00U,
|
||||
(uint8_t)'I', 0x00U,
|
||||
(uint8_t)'C', 0x00U,
|
||||
(uint8_t)'O', 0x00U,
|
||||
(uint8_t)'N', 0x00U,
|
||||
(uint8_t)'D', 0x00U,
|
||||
(uint8_t)'U', 0x00U,
|
||||
(uint8_t)'C', 0x00U,
|
||||
(uint8_t)'T', 0x00U,
|
||||
(uint8_t)'O', 0x00U,
|
||||
(uint8_t)'R', 0x00U,
|
||||
(uint8_t)'S', 0x00U,
|
||||
};
|
||||
|
||||
USB_DMA_INIT_DATA_ALIGN(USB_DATA_ALIGN_SIZE)
|
||||
uint8_t g_UsbDeviceString2[] = {
|
||||
2U + 2U * 20U, USB_DESCRIPTOR_TYPE_STRING,
|
||||
(uint8_t)'M', 0U,
|
||||
(uint8_t)'C', 0U,
|
||||
(uint8_t)'U', 0U,
|
||||
(uint8_t)' ', 0U,
|
||||
(uint8_t)'V', 0U,
|
||||
(uint8_t)'I', 0U,
|
||||
(uint8_t)'R', 0U,
|
||||
(uint8_t)'T', 0U,
|
||||
(uint8_t)'U', 0U,
|
||||
(uint8_t)'A', 0U,
|
||||
(uint8_t)'L', 0U,
|
||||
(uint8_t)' ', 0U,
|
||||
(uint8_t)'C', 0U,
|
||||
(uint8_t)'O', 0U,
|
||||
(uint8_t)'M', 0U,
|
||||
(uint8_t)' ', 0U,
|
||||
(uint8_t)'D', 0U,
|
||||
(uint8_t)'E', 0U,
|
||||
(uint8_t)'M', 0U,
|
||||
(uint8_t)'O', 0U,
|
||||
};
|
||||
|
||||
uint8_t *g_UsbDeviceStringDescriptorArray[USB_DEVICE_STRING_COUNT] = {g_UsbDeviceString0, g_UsbDeviceString1,
|
||||
g_UsbDeviceString2};
|
||||
|
||||
/* Define string descriptor size */
|
||||
uint32_t g_UsbDeviceStringDescriptorLength[USB_DEVICE_STRING_COUNT] = {
|
||||
sizeof(g_UsbDeviceString0), sizeof(g_UsbDeviceString1), sizeof(g_UsbDeviceString2)};
|
||||
usb_language_t g_UsbDeviceLanguage[USB_DEVICE_LANGUAGE_COUNT] = {{
|
||||
g_UsbDeviceStringDescriptorArray,
|
||||
g_UsbDeviceStringDescriptorLength,
|
||||
(uint16_t)0x0409,
|
||||
}};
|
||||
|
||||
usb_language_list_t g_UsbDeviceLanguageList = {
|
||||
g_UsbDeviceString0,
|
||||
sizeof(g_UsbDeviceString0),
|
||||
g_UsbDeviceLanguage,
|
||||
USB_DEVICE_LANGUAGE_COUNT,
|
||||
};
|
||||
|
||||
/*******************************************************************************
|
||||
* Code
|
||||
******************************************************************************/
|
||||
/*!
|
||||
* @brief USB device get device descriptor function.
|
||||
*
|
||||
* This function gets the device descriptor of the USB device.
|
||||
*
|
||||
* @param handle The USB device handle.
|
||||
* @param deviceDescriptor The pointer to the device descriptor structure.
|
||||
*
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
*/
|
||||
usb_status_t USB_DeviceGetDeviceDescriptor(usb_device_handle handle,
|
||||
usb_device_get_device_descriptor_struct_t *deviceDescriptor)
|
||||
{
|
||||
deviceDescriptor->buffer = g_UsbDeviceDescriptor;
|
||||
deviceDescriptor->length = USB_DESCRIPTOR_LENGTH_DEVICE;
|
||||
return kStatus_USB_Success;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief USB device get configuration descriptor function.
|
||||
*
|
||||
* This function gets the configuration descriptor of the USB device.
|
||||
*
|
||||
* @param handle The USB device handle.
|
||||
* @param configurationDescriptor The pointer to the configuration descriptor structure.
|
||||
*
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
*/
|
||||
usb_status_t USB_DeviceGetConfigurationDescriptor(
|
||||
usb_device_handle handle, usb_device_get_configuration_descriptor_struct_t *configurationDescriptor)
|
||||
{
|
||||
if ((uint8_t)USB_CDC_VCOM_CONFIGURE_INDEX > configurationDescriptor->configuration)
|
||||
{
|
||||
configurationDescriptor->buffer = g_UsbDeviceConfigurationDescriptor;
|
||||
configurationDescriptor->length = USB_DESCRIPTOR_LENGTH_CONFIGURATION_ALL;
|
||||
return kStatus_USB_Success;
|
||||
}
|
||||
return kStatus_USB_InvalidRequest;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief USB device get string descriptor function.
|
||||
*
|
||||
* This function gets the string descriptor of the USB device.
|
||||
*
|
||||
* @param handle The USB device handle.
|
||||
* @param stringDescriptor Pointer to the string descriptor structure.
|
||||
*
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
*/
|
||||
usb_status_t USB_DeviceGetStringDescriptor(usb_device_handle handle,
|
||||
usb_device_get_string_descriptor_struct_t *stringDescriptor)
|
||||
{
|
||||
if (stringDescriptor->stringIndex == 0U)
|
||||
{
|
||||
stringDescriptor->buffer = (uint8_t *)g_UsbDeviceLanguageList.languageString;
|
||||
stringDescriptor->length = g_UsbDeviceLanguageList.stringLength;
|
||||
}
|
||||
else
|
||||
{
|
||||
uint8_t languageId = 0U;
|
||||
uint8_t languageIndex = USB_DEVICE_STRING_COUNT;
|
||||
|
||||
for (; languageId < (uint8_t)USB_DEVICE_LANGUAGE_COUNT; languageId++)
|
||||
{
|
||||
if (stringDescriptor->languageId == g_UsbDeviceLanguageList.languageList[languageId].languageId)
|
||||
{
|
||||
if (stringDescriptor->stringIndex < (uint8_t)USB_DEVICE_STRING_COUNT)
|
||||
{
|
||||
languageIndex = stringDescriptor->stringIndex;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ((uint8_t)USB_DEVICE_STRING_COUNT == languageIndex)
|
||||
{
|
||||
return kStatus_USB_InvalidRequest;
|
||||
}
|
||||
stringDescriptor->buffer = (uint8_t *)g_UsbDeviceLanguageList.languageList[languageId].string[languageIndex];
|
||||
stringDescriptor->length = g_UsbDeviceLanguageList.languageList[languageId].length[languageIndex];
|
||||
}
|
||||
return kStatus_USB_Success;
|
||||
}
|
||||
|
||||
/*!
|
||||
* @brief USB device set speed function.
|
||||
*
|
||||
* This function sets the speed of the USB device.
|
||||
*
|
||||
* Due to the difference of HS and FS descriptors, the device descriptors and configurations need to be updated to match
|
||||
* current speed.
|
||||
* As the default, the device descriptors and configurations are configured by using FS parameters for both EHCI and
|
||||
* KHCI.
|
||||
* When the EHCI is enabled, the application needs to call this function to update device by using current speed.
|
||||
* The updated information includes endpoint max packet size, endpoint interval, etc.
|
||||
*
|
||||
* @param handle The USB device handle.
|
||||
* @param speed Speed type. USB_SPEED_HIGH/USB_SPEED_FULL/USB_SPEED_LOW.
|
||||
*
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
*/
|
||||
usb_status_t USB_DeviceSetSpeed(usb_device_handle handle, uint8_t speed)
|
||||
{
|
||||
usb_descriptor_union_t *ptr1;
|
||||
usb_descriptor_union_t *ptr2;
|
||||
|
||||
ptr1 = (usb_descriptor_union_t *)(void *)(&g_UsbDeviceConfigurationDescriptor[0]);
|
||||
ptr2 = (usb_descriptor_union_t
|
||||
*)(void *)(&g_UsbDeviceConfigurationDescriptor[USB_DESCRIPTOR_LENGTH_CONFIGURATION_ALL - 1U]);
|
||||
|
||||
while (ptr1 < ptr2)
|
||||
{
|
||||
if (ptr1->common.bDescriptorType == USB_DESCRIPTOR_TYPE_ENDPOINT)
|
||||
{
|
||||
if (USB_SPEED_HIGH == speed)
|
||||
{
|
||||
if (((ptr1->endpoint.bEndpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) ==
|
||||
USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_IN) &&
|
||||
((uint8_t)USB_CDC_VCOM_INTERRUPT_IN_ENDPOINT ==
|
||||
(ptr1->endpoint.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK)))
|
||||
{
|
||||
ptr1->endpoint.bInterval = HS_CDC_VCOM_INTERRUPT_IN_INTERVAL;
|
||||
USB_SHORT_TO_LITTLE_ENDIAN_ADDRESS(HS_CDC_VCOM_INTERRUPT_IN_PACKET_SIZE,
|
||||
ptr1->endpoint.wMaxPacketSize);
|
||||
}
|
||||
else if (((ptr1->endpoint.bEndpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) ==
|
||||
USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_IN) &&
|
||||
((uint8_t)USB_CDC_VCOM_BULK_IN_ENDPOINT ==
|
||||
(ptr1->endpoint.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK)))
|
||||
{
|
||||
USB_SHORT_TO_LITTLE_ENDIAN_ADDRESS(HS_CDC_VCOM_BULK_IN_PACKET_SIZE, ptr1->endpoint.wMaxPacketSize);
|
||||
}
|
||||
else if (((ptr1->endpoint.bEndpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) ==
|
||||
USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_OUT) &&
|
||||
((uint8_t)USB_CDC_VCOM_BULK_OUT_ENDPOINT ==
|
||||
(ptr1->endpoint.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK)))
|
||||
{
|
||||
USB_SHORT_TO_LITTLE_ENDIAN_ADDRESS(HS_CDC_VCOM_BULK_OUT_PACKET_SIZE, ptr1->endpoint.wMaxPacketSize);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* MISRA C-2012 Rule 15.7 */
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (((ptr1->endpoint.bEndpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) ==
|
||||
USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_IN) &&
|
||||
((uint8_t)USB_CDC_VCOM_INTERRUPT_IN_ENDPOINT ==
|
||||
(ptr1->endpoint.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK)))
|
||||
{
|
||||
ptr1->endpoint.bInterval = FS_CDC_VCOM_INTERRUPT_IN_INTERVAL;
|
||||
USB_SHORT_TO_LITTLE_ENDIAN_ADDRESS(FS_CDC_VCOM_INTERRUPT_IN_PACKET_SIZE,
|
||||
ptr1->endpoint.wMaxPacketSize);
|
||||
}
|
||||
else if (((ptr1->endpoint.bEndpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) ==
|
||||
USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_IN) &&
|
||||
((uint8_t)USB_CDC_VCOM_BULK_IN_ENDPOINT ==
|
||||
(ptr1->endpoint.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK)))
|
||||
{
|
||||
USB_SHORT_TO_LITTLE_ENDIAN_ADDRESS(FS_CDC_VCOM_BULK_IN_PACKET_SIZE, ptr1->endpoint.wMaxPacketSize);
|
||||
}
|
||||
else if (((ptr1->endpoint.bEndpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK) ==
|
||||
USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_OUT) &&
|
||||
((uint8_t)USB_CDC_VCOM_BULK_OUT_ENDPOINT ==
|
||||
(ptr1->endpoint.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK)))
|
||||
{
|
||||
USB_SHORT_TO_LITTLE_ENDIAN_ADDRESS(FS_CDC_VCOM_BULK_OUT_PACKET_SIZE, ptr1->endpoint.wMaxPacketSize);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* MISRA C-2012 Rule 15.7 */
|
||||
}
|
||||
}
|
||||
}
|
||||
ptr1 = (usb_descriptor_union_t *)(void *)((uint8_t *)ptr1 + ptr1->common.bLength);
|
||||
}
|
||||
|
||||
for (int i = 0; i < USB_CDC_VCOM_ENDPOINT_CIC_COUNT; i++)
|
||||
{
|
||||
if (USB_SPEED_HIGH == speed)
|
||||
{
|
||||
g_UsbDeviceCdcVcomCicEndpoints[i].maxPacketSize = HS_CDC_VCOM_INTERRUPT_IN_PACKET_SIZE;
|
||||
}
|
||||
else
|
||||
{
|
||||
g_UsbDeviceCdcVcomCicEndpoints[i].maxPacketSize = FS_CDC_VCOM_INTERRUPT_IN_PACKET_SIZE;
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < USB_CDC_VCOM_ENDPOINT_DIC_COUNT; i++)
|
||||
{
|
||||
if (USB_SPEED_HIGH == speed)
|
||||
{
|
||||
if (0U !=
|
||||
(g_UsbDeviceCdcVcomDicEndpoints[i].endpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK))
|
||||
{
|
||||
g_UsbDeviceCdcVcomDicEndpoints[i].maxPacketSize = HS_CDC_VCOM_BULK_IN_PACKET_SIZE;
|
||||
}
|
||||
else
|
||||
{
|
||||
g_UsbDeviceCdcVcomDicEndpoints[i].maxPacketSize = HS_CDC_VCOM_BULK_OUT_PACKET_SIZE;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (0U !=
|
||||
(g_UsbDeviceCdcVcomDicEndpoints[i].endpointAddress & USB_DESCRIPTOR_ENDPOINT_ADDRESS_DIRECTION_MASK))
|
||||
{
|
||||
g_UsbDeviceCdcVcomDicEndpoints[i].maxPacketSize = FS_CDC_VCOM_BULK_IN_PACKET_SIZE;
|
||||
}
|
||||
else
|
||||
{
|
||||
g_UsbDeviceCdcVcomDicEndpoints[i].maxPacketSize = FS_CDC_VCOM_BULK_OUT_PACKET_SIZE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return kStatus_USB_Success;
|
||||
}
|
||||
|
@@ -1,203 +1,247 @@
|
||||
/*
|
||||
* Copyright (c) 2015 - 2016, Freescale Semiconductor, Inc.
|
||||
* Copyright 2016 NXP
|
||||
* All rights reserved.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
#ifndef _USB_DEVICE_DESCRIPTOR_H_
|
||||
#define _USB_DEVICE_DESCRIPTOR_H_ 1
|
||||
|
||||
/*******************************************************************************
|
||||
* Definitions
|
||||
******************************************************************************/
|
||||
#define USB_DEVICE_SPECIFIC_BCD_VERSION (0x0200)
|
||||
#define USB_DEVICE_DEMO_BCD_VERSION (0x0101U)
|
||||
|
||||
/* Communication Class Codes */
|
||||
#define CDC_COMM_CLASS (0x02)
|
||||
/* Data Class Codes */
|
||||
#define CDC_DATA_CLASS (0x0A)
|
||||
|
||||
/* Communication Class SubClass Codes */
|
||||
#define USB_CDC_DIRECT_LINE_CONTROL_MODEL (0x01)
|
||||
#define USB_CDC_ABSTRACT_CONTROL_MODEL (0x02)
|
||||
#define USB_CDC_TELEPHONE_CONTROL_MODEL (0x03)
|
||||
#define USB_CDC_MULTI_CHANNEL_CONTROL_MODEL (0x04)
|
||||
#define USB_CDC_CAPI_CONTROL_MOPDEL (0x05)
|
||||
#define USB_CDC_ETHERNET_NETWORKING_CONTROL_MODEL (0x06)
|
||||
#define USB_CDC_ATM_NETWORKING_CONTROL_MODEL (0x07)
|
||||
#define USB_CDC_WIRELESS_HANDSET_CONTROL_MODEL (0x08)
|
||||
#define USB_CDC_DEVICE_MANAGEMENT (0x09)
|
||||
#define USB_CDC_MOBILE_DIRECT_LINE_MODEL (0x0A)
|
||||
#define USB_CDC_OBEX (0x0B)
|
||||
#define USB_CDC_ETHERNET_EMULATION_MODEL (0x0C)
|
||||
|
||||
/* Communication Class Protocol Codes */
|
||||
#define USB_CDC_NO_CLASS_SPECIFIC_PROTOCOL (0x00) /*also for Data Class Protocol Code */
|
||||
#define USB_CDC_AT_250_PROTOCOL (0x01)
|
||||
#define USB_CDC_AT_PCCA_101_PROTOCOL (0x02)
|
||||
#define USB_CDC_AT_PCCA_101_ANNEX_O (0x03)
|
||||
#define USB_CDC_AT_GSM_7_07 (0x04)
|
||||
#define USB_CDC_AT_3GPP_27_007 (0x05)
|
||||
#define USB_CDC_AT_TIA_CDMA (0x06)
|
||||
#define USB_CDC_ETHERNET_EMULATION_PROTOCOL (0x07)
|
||||
#define USB_CDC_EXTERNAL_PROTOCOL (0xFE)
|
||||
#define USB_CDC_VENDOR_SPECIFIC (0xFF) /*also for Data Class Protocol Code */
|
||||
|
||||
/* Data Class Protocol Codes */
|
||||
#define USB_CDC_PYHSICAL_INTERFACE_PROTOCOL (0x30)
|
||||
#define USB_CDC_HDLC_PROTOCOL (0x31)
|
||||
#define USB_CDC_TRANSPARENT_PROTOCOL (0x32)
|
||||
#define USB_CDC_MANAGEMENT_PROTOCOL (0x50)
|
||||
#define USB_CDC_DATA_LINK_Q931_PROTOCOL (0x51)
|
||||
#define USB_CDC_DATA_LINK_Q921_PROTOCOL (0x52)
|
||||
#define USB_CDC_DATA_COMPRESSION_V42BIS (0x90)
|
||||
#define USB_CDC_EURO_ISDN_PROTOCOL (0x91)
|
||||
#define USB_CDC_RATE_ADAPTION_ISDN_V24 (0x92)
|
||||
#define USB_CDC_CAPI_COMMANDS (0x93)
|
||||
#define USB_CDC_HOST_BASED_DRIVER (0xFD)
|
||||
#define USB_CDC_UNIT_FUNCTIONAL (0xFE)
|
||||
|
||||
/* Descriptor SubType in Communications Class Functional Descriptors */
|
||||
#define USB_CDC_HEADER_FUNC_DESC (0x00)
|
||||
#define USB_CDC_CALL_MANAGEMENT_FUNC_DESC (0x01)
|
||||
#define USB_CDC_ABSTRACT_CONTROL_FUNC_DESC (0x02)
|
||||
#define USB_CDC_DIRECT_LINE_FUNC_DESC (0x03)
|
||||
#define USB_CDC_TELEPHONE_RINGER_FUNC_DESC (0x04)
|
||||
#define USB_CDC_TELEPHONE_REPORT_FUNC_DESC (0x05)
|
||||
#define USB_CDC_UNION_FUNC_DESC (0x06)
|
||||
#define USB_CDC_COUNTRY_SELECT_FUNC_DESC (0x07)
|
||||
#define USB_CDC_TELEPHONE_MODES_FUNC_DESC (0x08)
|
||||
#define USB_CDC_TERMINAL_FUNC_DESC (0x09)
|
||||
#define USB_CDC_NETWORK_CHANNEL_FUNC_DESC (0x0A)
|
||||
#define USB_CDC_PROTOCOL_UNIT_FUNC_DESC (0x0B)
|
||||
#define USB_CDC_EXTENSION_UNIT_FUNC_DESC (0x0C)
|
||||
#define USB_CDC_MULTI_CHANNEL_FUNC_DESC (0x0D)
|
||||
#define USB_CDC_CAPI_CONTROL_FUNC_DESC (0x0E)
|
||||
#define USB_CDC_ETHERNET_NETWORKING_FUNC_DESC (0x0F)
|
||||
#define USB_CDC_ATM_NETWORKING_FUNC_DESC (0x10)
|
||||
#define USB_CDC_WIRELESS_CONTROL_FUNC_DESC (0x11)
|
||||
#define USB_CDC_MOBILE_DIRECT_LINE_FUNC_DESC (0x12)
|
||||
#define USB_CDC_MDLM_DETAIL_FUNC_DESC (0x13)
|
||||
#define USB_CDC_DEVICE_MANAGEMENT_FUNC_DESC (0x14)
|
||||
#define USB_CDC_OBEX_FUNC_DESC (0x15)
|
||||
#define USB_CDC_COMMAND_SET_FUNC_DESC (0x16)
|
||||
#define USB_CDC_COMMAND_SET_DETAIL_FUNC_DESC (0x17)
|
||||
#define USB_CDC_TELEPHONE_CONTROL_FUNC_DESC (0x18)
|
||||
#define USB_CDC_OBEX_SERVICE_ID_FUNC_DESC (0x19)
|
||||
|
||||
/* usb descritpor length */
|
||||
#define USB_DESCRIPTOR_LENGTH_CONFIGURATION_ALL (sizeof(g_UsbDeviceConfigurationDescriptor))
|
||||
#define USB_DESCRIPTOR_LENGTH_CDC_HEADER_FUNC (5)
|
||||
#define USB_DESCRIPTOR_LENGTH_CDC_CALL_MANAG (5)
|
||||
#define USB_DESCRIPTOR_LENGTH_CDC_ABSTRACT (4)
|
||||
#define USB_DESCRIPTOR_LENGTH_CDC_UNION_FUNC (5)
|
||||
|
||||
/* Configuration, interface and endpoint. */
|
||||
#define USB_DEVICE_CONFIGURATION_COUNT (1)
|
||||
#define USB_DEVICE_STRING_COUNT (3)
|
||||
#define USB_DEVICE_LANGUAGE_COUNT (1)
|
||||
|
||||
#define USB_CDC_VCOM_CONFIGURE_INDEX (1)
|
||||
|
||||
#define USB_CDC_VCOM_ENDPOINT_CIC_COUNT (1)
|
||||
#define USB_CDC_VCOM_ENDPOINT_DIC_COUNT (2)
|
||||
#define USB_CDC_VCOM_INTERRUPT_IN_ENDPOINT (1)
|
||||
#define USB_CDC_VCOM_BULK_IN_ENDPOINT (2)
|
||||
#define USB_CDC_VCOM_BULK_OUT_ENDPOINT (3)
|
||||
#define USB_CDC_VCOM_INTERFACE_COUNT (2)
|
||||
#define USB_CDC_VCOM_COMM_INTERFACE_INDEX (0)
|
||||
#define USB_CDC_VCOM_DATA_INTERFACE_INDEX (1)
|
||||
|
||||
/* Packet size. */
|
||||
#define HS_CDC_VCOM_INTERRUPT_IN_PACKET_SIZE (16)
|
||||
#define FS_CDC_VCOM_INTERRUPT_IN_PACKET_SIZE (16)
|
||||
#define HS_CDC_VCOM_INTERRUPT_IN_INTERVAL (0x07) /* 2^(7-1) = 8ms */
|
||||
#define FS_CDC_VCOM_INTERRUPT_IN_INTERVAL (0x08)
|
||||
#define HS_CDC_VCOM_BULK_IN_PACKET_SIZE (512)
|
||||
#define FS_CDC_VCOM_BULK_IN_PACKET_SIZE (64)
|
||||
#define HS_CDC_VCOM_BULK_OUT_PACKET_SIZE (512)
|
||||
#define FS_CDC_VCOM_BULK_OUT_PACKET_SIZE (64)
|
||||
|
||||
/* String descriptor length. */
|
||||
#define USB_DESCRIPTOR_LENGTH_STRING0 (sizeof(g_UsbDeviceString0))
|
||||
#define USB_DESCRIPTOR_LENGTH_STRING1 (sizeof(g_UsbDeviceString1))
|
||||
#define USB_DESCRIPTOR_LENGTH_STRING2 (sizeof(g_UsbDeviceString2))
|
||||
|
||||
#define USB_DESCRIPTOR_TYPE_CDC_CS_INTERFACE (0x24)
|
||||
#define USB_DESCRIPTOR_TYPE_CDC_CS_ENDPOINT (0x25)
|
||||
|
||||
/* Class code. */
|
||||
#define USB_DEVICE_CLASS (0x02)
|
||||
#define USB_DEVICE_SUBCLASS (0x00)
|
||||
#define USB_DEVICE_PROTOCOL (0x00)
|
||||
|
||||
#define USB_DEVICE_MAX_POWER (0x32)
|
||||
|
||||
#define USB_CDC_VCOM_CIC_CLASS (CDC_COMM_CLASS)
|
||||
#define USB_CDC_VCOM_CIC_SUBCLASS (USB_CDC_ABSTRACT_CONTROL_MODEL)
|
||||
#define USB_CDC_VCOM_CIC_PROTOCOL (USB_CDC_NO_CLASS_SPECIFIC_PROTOCOL)
|
||||
|
||||
#define USB_CDC_VCOM_DIC_CLASS (CDC_DATA_CLASS)
|
||||
#define USB_CDC_VCOM_DIC_SUBCLASS (0x00)
|
||||
#define USB_CDC_VCOM_DIC_PROTOCOL (USB_CDC_NO_CLASS_SPECIFIC_PROTOCOL)
|
||||
|
||||
/*******************************************************************************
|
||||
* API
|
||||
******************************************************************************/
|
||||
/*!
|
||||
* @brief USB device set speed function.
|
||||
*
|
||||
* This function sets the speed of the USB device.
|
||||
*
|
||||
* Due to the difference of HS and FS descriptors, the device descriptors and configurations need to be updated to match
|
||||
* current speed.
|
||||
* As the default, the device descriptors and configurations are configured by using FS parameters for both EHCI and
|
||||
* KHCI.
|
||||
* When the EHCI is enabled, the application needs to call this function to update device by using current speed.
|
||||
* The updated information includes endpoint max packet size, endpoint interval, etc.
|
||||
*
|
||||
* @param handle The USB device handle.
|
||||
* @param speed Speed type. USB_SPEED_HIGH/USB_SPEED_FULL/USB_SPEED_LOW.
|
||||
*
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
*/
|
||||
extern usb_status_t USB_DeviceSetSpeed(usb_device_handle handle, uint8_t speed);
|
||||
/*!
|
||||
* @brief USB device get device descriptor function.
|
||||
*
|
||||
* This function gets the device descriptor of the USB device.
|
||||
*
|
||||
* @param handle The USB device handle.
|
||||
* @param deviceDescriptor The pointer to the device descriptor structure.
|
||||
*
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
*/
|
||||
extern usb_status_t USB_DeviceGetDeviceDescriptor(usb_device_handle handle,
|
||||
usb_device_get_device_descriptor_struct_t *deviceDescriptor);
|
||||
/*!
|
||||
* @brief USB device get string descriptor function.
|
||||
*
|
||||
* This function gets the string descriptor of the USB device.
|
||||
*
|
||||
* @param handle The USB device handle.
|
||||
* @param stringDescriptor Pointer to the string descriptor structure.
|
||||
*
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
*/
|
||||
usb_status_t USB_DeviceGetStringDescriptor(usb_device_handle handle,
|
||||
usb_device_get_string_descriptor_struct_t *stringDescriptor);
|
||||
/*!
|
||||
* @brief USB device get configuration descriptor function.
|
||||
*
|
||||
* This function gets the configuration descriptor of the USB device.
|
||||
*
|
||||
* @param handle The USB device handle.
|
||||
* @param configurationDescriptor The pointer to the configuration descriptor structure.
|
||||
*
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
*/
|
||||
extern usb_status_t USB_DeviceGetConfigurationDescriptor(
|
||||
usb_device_handle handle, usb_device_get_configuration_descriptor_struct_t *configurationDescriptor);
|
||||
#endif /* _USB_DEVICE_DESCRIPTOR_H_ */
|
||||
/*
|
||||
* Copyright (c) 2015 - 2016, Freescale Semiconductor, Inc.
|
||||
* Copyright 2016 NXP
|
||||
* All rights reserved.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
#ifndef _USB_DEVICE_DESCRIPTOR_H_
|
||||
#define _USB_DEVICE_DESCRIPTOR_H_ 1
|
||||
|
||||
/*******************************************************************************
|
||||
* Definitions
|
||||
******************************************************************************/
|
||||
#define USB_DEVICE_SPECIFIC_BCD_VERSION (0x0200)
|
||||
#define USB_DEVICE_DEMO_BCD_VERSION (0x0101U)
|
||||
|
||||
/* Communication Class Codes */
|
||||
#define CDC_COMM_CLASS (0x02)
|
||||
/* Data Class Codes */
|
||||
#define CDC_DATA_CLASS (0x0A)
|
||||
|
||||
/* Communication Class SubClass Codes */
|
||||
#define USB_CDC_DIRECT_LINE_CONTROL_MODEL (0x01)
|
||||
#define USB_CDC_ABSTRACT_CONTROL_MODEL (0x02)
|
||||
#define USB_CDC_TELEPHONE_CONTROL_MODEL (0x03)
|
||||
#define USB_CDC_MULTI_CHANNEL_CONTROL_MODEL (0x04)
|
||||
#define USB_CDC_CAPI_CONTROL_MOPDEL (0x05)
|
||||
#define USB_CDC_ETHERNET_NETWORKING_CONTROL_MODEL (0x06)
|
||||
#define USB_CDC_ATM_NETWORKING_CONTROL_MODEL (0x07)
|
||||
#define USB_CDC_WIRELESS_HANDSET_CONTROL_MODEL (0x08)
|
||||
#define USB_CDC_DEVICE_MANAGEMENT (0x09)
|
||||
#define USB_CDC_MOBILE_DIRECT_LINE_MODEL (0x0A)
|
||||
#define USB_CDC_OBEX (0x0B)
|
||||
#define USB_CDC_ETHERNET_EMULATION_MODEL (0x0C)
|
||||
|
||||
/* Communication Class Protocol Codes */
|
||||
#define USB_CDC_NO_CLASS_SPECIFIC_PROTOCOL (0x00) /*also for Data Class Protocol Code */
|
||||
#define USB_CDC_AT_250_PROTOCOL (0x01)
|
||||
#define USB_CDC_AT_PCCA_101_PROTOCOL (0x02)
|
||||
#define USB_CDC_AT_PCCA_101_ANNEX_O (0x03)
|
||||
#define USB_CDC_AT_GSM_7_07 (0x04)
|
||||
#define USB_CDC_AT_3GPP_27_007 (0x05)
|
||||
#define USB_CDC_AT_TIA_CDMA (0x06)
|
||||
#define USB_CDC_ETHERNET_EMULATION_PROTOCOL (0x07)
|
||||
#define USB_CDC_EXTERNAL_PROTOCOL (0xFE)
|
||||
#define USB_CDC_VENDOR_SPECIFIC (0xFF) /*also for Data Class Protocol Code */
|
||||
|
||||
/* Data Class Protocol Codes */
|
||||
#define USB_CDC_PYHSICAL_INTERFACE_PROTOCOL (0x30)
|
||||
#define USB_CDC_HDLC_PROTOCOL (0x31)
|
||||
#define USB_CDC_TRANSPARENT_PROTOCOL (0x32)
|
||||
#define USB_CDC_MANAGEMENT_PROTOCOL (0x50)
|
||||
#define USB_CDC_DATA_LINK_Q931_PROTOCOL (0x51)
|
||||
#define USB_CDC_DATA_LINK_Q921_PROTOCOL (0x52)
|
||||
#define USB_CDC_DATA_COMPRESSION_V42BIS (0x90)
|
||||
#define USB_CDC_EURO_ISDN_PROTOCOL (0x91)
|
||||
#define USB_CDC_RATE_ADAPTION_ISDN_V24 (0x92)
|
||||
#define USB_CDC_CAPI_COMMANDS (0x93)
|
||||
#define USB_CDC_HOST_BASED_DRIVER (0xFD)
|
||||
#define USB_CDC_UNIT_FUNCTIONAL (0xFE)
|
||||
|
||||
/* Descriptor SubType in Communications Class Functional Descriptors */
|
||||
#define USB_CDC_HEADER_FUNC_DESC (0x00)
|
||||
#define USB_CDC_CALL_MANAGEMENT_FUNC_DESC (0x01)
|
||||
#define USB_CDC_ABSTRACT_CONTROL_FUNC_DESC (0x02)
|
||||
#define USB_CDC_DIRECT_LINE_FUNC_DESC (0x03)
|
||||
#define USB_CDC_TELEPHONE_RINGER_FUNC_DESC (0x04)
|
||||
#define USB_CDC_TELEPHONE_REPORT_FUNC_DESC (0x05)
|
||||
#define USB_CDC_UNION_FUNC_DESC (0x06)
|
||||
#define USB_CDC_COUNTRY_SELECT_FUNC_DESC (0x07)
|
||||
#define USB_CDC_TELEPHONE_MODES_FUNC_DESC (0x08)
|
||||
#define USB_CDC_TERMINAL_FUNC_DESC (0x09)
|
||||
#define USB_CDC_NETWORK_CHANNEL_FUNC_DESC (0x0A)
|
||||
#define USB_CDC_PROTOCOL_UNIT_FUNC_DESC (0x0B)
|
||||
#define USB_CDC_EXTENSION_UNIT_FUNC_DESC (0x0C)
|
||||
#define USB_CDC_MULTI_CHANNEL_FUNC_DESC (0x0D)
|
||||
#define USB_CDC_CAPI_CONTROL_FUNC_DESC (0x0E)
|
||||
#define USB_CDC_ETHERNET_NETWORKING_FUNC_DESC (0x0F)
|
||||
#define USB_CDC_ATM_NETWORKING_FUNC_DESC (0x10)
|
||||
#define USB_CDC_WIRELESS_CONTROL_FUNC_DESC (0x11)
|
||||
#define USB_CDC_MOBILE_DIRECT_LINE_FUNC_DESC (0x12)
|
||||
#define USB_CDC_MDLM_DETAIL_FUNC_DESC (0x13)
|
||||
#define USB_CDC_DEVICE_MANAGEMENT_FUNC_DESC (0x14)
|
||||
#define USB_CDC_OBEX_FUNC_DESC (0x15)
|
||||
#define USB_CDC_COMMAND_SET_FUNC_DESC (0x16)
|
||||
#define USB_CDC_COMMAND_SET_DETAIL_FUNC_DESC (0x17)
|
||||
#define USB_CDC_TELEPHONE_CONTROL_FUNC_DESC (0x18)
|
||||
#define USB_CDC_OBEX_SERVICE_ID_FUNC_DESC (0x19)
|
||||
|
||||
/* usb descritpor length */
|
||||
#define USB_DESCRIPTOR_LENGTH_CONFIGURATION_ALL (sizeof(g_UsbDeviceConfigurationDescriptor))
|
||||
#define USB_DESCRIPTOR_LENGTH_CDC_HEADER_FUNC (5)
|
||||
#define USB_DESCRIPTOR_LENGTH_CDC_CALL_MANAG (5)
|
||||
#define USB_DESCRIPTOR_LENGTH_CDC_ABSTRACT (4)
|
||||
#define USB_DESCRIPTOR_LENGTH_CDC_UNION_FUNC (5)
|
||||
|
||||
/* Configuration, interface and endpoint. */
|
||||
#define USB_DEVICE_CONFIGURATION_COUNT (1)
|
||||
#define USB_DEVICE_STRING_COUNT (3)
|
||||
#define USB_DEVICE_LANGUAGE_COUNT (1)
|
||||
|
||||
#define USB_CDC_VCOM_CONFIGURE_INDEX (1)
|
||||
|
||||
#define USB_CDC_VCOM_ENDPOINT_CIC_COUNT (1)
|
||||
#define USB_CDC_VCOM_ENDPOINT_DIC_COUNT (2)
|
||||
#define USB_CDC_VCOM_INTERRUPT_IN_ENDPOINT (1)
|
||||
#define USB_CDC_VCOM_BULK_IN_ENDPOINT (2)
|
||||
#define USB_CDC_VCOM_BULK_OUT_ENDPOINT (3)
|
||||
#define USB_CDC_VCOM_INTERFACE_COUNT (2)
|
||||
#define USB_CDC_VCOM_COMM_INTERFACE_INDEX (0)
|
||||
#define USB_CDC_VCOM_DATA_INTERFACE_INDEX (1)
|
||||
|
||||
/* Packet size. */
|
||||
#define HS_CDC_VCOM_INTERRUPT_IN_PACKET_SIZE (16)
|
||||
#define FS_CDC_VCOM_INTERRUPT_IN_PACKET_SIZE (16)
|
||||
#define HS_CDC_VCOM_INTERRUPT_IN_INTERVAL (0x07) /* 2^(7-1) = 8ms */
|
||||
#define FS_CDC_VCOM_INTERRUPT_IN_INTERVAL (0x08)
|
||||
#define HS_CDC_VCOM_BULK_IN_PACKET_SIZE (512)
|
||||
#define FS_CDC_VCOM_BULK_IN_PACKET_SIZE (64)
|
||||
#define HS_CDC_VCOM_BULK_OUT_PACKET_SIZE (512)
|
||||
#define FS_CDC_VCOM_BULK_OUT_PACKET_SIZE (64)
|
||||
|
||||
/* String descriptor length. */
|
||||
#define USB_DESCRIPTOR_LENGTH_STRING0 (sizeof(g_UsbDeviceString0))
|
||||
#define USB_DESCRIPTOR_LENGTH_STRING1 (sizeof(g_UsbDeviceString1))
|
||||
#define USB_DESCRIPTOR_LENGTH_STRING2 (sizeof(g_UsbDeviceString2))
|
||||
|
||||
#define USB_DESCRIPTOR_TYPE_CDC_CS_INTERFACE (0x24)
|
||||
#define USB_DESCRIPTOR_TYPE_CDC_CS_ENDPOINT (0x25)
|
||||
|
||||
/* Class code. */
|
||||
#define USB_DEVICE_CLASS (0x02)
|
||||
#define USB_DEVICE_SUBCLASS (0x00)
|
||||
#define USB_DEVICE_PROTOCOL (0x00)
|
||||
|
||||
#define USB_DEVICE_MAX_POWER (0x32)
|
||||
|
||||
#define USB_CDC_VCOM_CIC_CLASS (CDC_COMM_CLASS)
|
||||
#define USB_CDC_VCOM_CIC_SUBCLASS (USB_CDC_ABSTRACT_CONTROL_MODEL)
|
||||
#define USB_CDC_VCOM_CIC_PROTOCOL (USB_CDC_NO_CLASS_SPECIFIC_PROTOCOL)
|
||||
|
||||
#define USB_CDC_VCOM_DIC_CLASS (CDC_DATA_CLASS)
|
||||
#define USB_CDC_VCOM_DIC_SUBCLASS (0x00)
|
||||
#define USB_CDC_VCOM_DIC_PROTOCOL (USB_CDC_NO_CLASS_SPECIFIC_PROTOCOL)
|
||||
|
||||
/*******************************************************************************
|
||||
* Variables
|
||||
******************************************************************************/
|
||||
/* Define endpoint for communication class */
|
||||
extern usb_device_endpoint_struct_t g_UsbDeviceCdcVcomCicEndpoints[USB_CDC_VCOM_ENDPOINT_CIC_COUNT];
|
||||
|
||||
/* Define endpoint for data class */
|
||||
extern usb_device_endpoint_struct_t g_UsbDeviceCdcVcomDicEndpoints[USB_CDC_VCOM_ENDPOINT_DIC_COUNT];
|
||||
|
||||
/* Define interface for communication class */
|
||||
extern usb_device_interface_struct_t g_UsbDeviceCdcVcomCommunicationInterface[];
|
||||
|
||||
/* Define interface for data class */
|
||||
extern usb_device_interface_struct_t g_UsbDeviceCdcVcomDataInterface[];
|
||||
|
||||
/* Define interfaces for virtual com */
|
||||
extern usb_device_interfaces_struct_t g_UsbDeviceCdcVcomInterfaces[USB_CDC_VCOM_INTERFACE_COUNT];
|
||||
|
||||
/* Define configurations for virtual com */
|
||||
extern usb_device_interface_list_t g_UsbDeviceCdcVcomInterfaceLIST_[USB_DEVICE_CONFIGURATION_COUNT];
|
||||
|
||||
/* Define class information for virtual com */
|
||||
extern usb_device_class_struct_t g_UsbDeviceCdcVcomConfig;
|
||||
|
||||
/* Define device descriptor */
|
||||
extern uint8_t g_UsbDeviceDescriptor[];
|
||||
|
||||
/* Define configuration descriptor */
|
||||
extern uint8_t g_UsbDeviceConfigurationDescriptor[];
|
||||
|
||||
/* Define string descriptor */
|
||||
extern uint8_t g_UsbDeviceString0[];
|
||||
|
||||
extern uint8_t g_UsbDeviceString1[];
|
||||
|
||||
extern uint8_t g_UsbDeviceString2[];
|
||||
|
||||
extern uint8_t *g_UsbDeviceStringDescriptorArray[USB_DEVICE_STRING_COUNT];
|
||||
|
||||
/* Define string descriptor size */
|
||||
extern uint32_t g_UsbDeviceStringDescriptorLength[USB_DEVICE_STRING_COUNT];
|
||||
extern usb_language_t g_UsbDeviceLanguage[USB_DEVICE_LANGUAGE_COUNT];
|
||||
extern usb_language_list_t g_UsbDeviceLanguageList;
|
||||
|
||||
/*******************************************************************************
|
||||
* API
|
||||
******************************************************************************/
|
||||
/*!
|
||||
* @brief USB device set speed function.
|
||||
*
|
||||
* This function sets the speed of the USB device.
|
||||
*
|
||||
* Due to the difference of HS and FS descriptors, the device descriptors and configurations need to be updated to match
|
||||
* current speed.
|
||||
* As the default, the device descriptors and configurations are configured by using FS parameters for both EHCI and
|
||||
* KHCI.
|
||||
* When the EHCI is enabled, the application needs to call this function to update device by using current speed.
|
||||
* The updated information includes endpoint max packet size, endpoint interval, etc.
|
||||
*
|
||||
* @param handle The USB device handle.
|
||||
* @param speed Speed type. USB_SPEED_HIGH/USB_SPEED_FULL/USB_SPEED_LOW.
|
||||
*
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
*/
|
||||
extern usb_status_t USB_DeviceSetSpeed(usb_device_handle handle, uint8_t speed);
|
||||
/*!
|
||||
* @brief USB device get device descriptor function.
|
||||
*
|
||||
* This function gets the device descriptor of the USB device.
|
||||
*
|
||||
* @param handle The USB device handle.
|
||||
* @param deviceDescriptor The pointer to the device descriptor structure.
|
||||
*
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
*/
|
||||
extern usb_status_t USB_DeviceGetDeviceDescriptor(usb_device_handle handle,
|
||||
usb_device_get_device_descriptor_struct_t *deviceDescriptor);
|
||||
/*!
|
||||
* @brief USB device get string descriptor function.
|
||||
*
|
||||
* This function gets the string descriptor of the USB device.
|
||||
*
|
||||
* @param handle The USB device handle.
|
||||
* @param stringDescriptor Pointer to the string descriptor structure.
|
||||
*
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
*/
|
||||
usb_status_t USB_DeviceGetStringDescriptor(usb_device_handle handle,
|
||||
usb_device_get_string_descriptor_struct_t *stringDescriptor);
|
||||
/*!
|
||||
* @brief USB device get configuration descriptor function.
|
||||
*
|
||||
* This function gets the configuration descriptor of the USB device.
|
||||
*
|
||||
* @param handle The USB device handle.
|
||||
* @param configurationDescriptor The pointer to the configuration descriptor structure.
|
||||
*
|
||||
* @return A USB error code or kStatus_USB_Success.
|
||||
*/
|
||||
extern usb_status_t USB_DeviceGetConfigurationDescriptor(
|
||||
usb_device_handle handle, usb_device_get_configuration_descriptor_struct_t *configurationDescriptor);
|
||||
#endif /* _USB_DEVICE_DESCRIPTOR_H_ */
|
||||
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user