This commit is contained in:
cfif 2024-12-04 13:10:49 +03:00
commit 751dc3a7bc
34 changed files with 6085 additions and 0 deletions

83
SerialPort_USB.c Normal file
View File

@ -0,0 +1,83 @@
//
// Created by cfif on 13.11.2023.
//
#include "SerialPort_USB.h"
#include "SystemDelayInterface.h"
#include "hw_config.h"
#include "usb_lib.h"
static uint16_t impSerialPortReceiveQueue(
TSerialPortUsbNation *env, uint8_t *data, uint16_t size, uint32_t timeout, osMessageQueueId_t queueId
) {
uint16_t received = 0;
if (timeout) {
uint32_t endTimeout = SystemGetMs() + timeout;
uint32_t currentTimeout;
while (size && ((timeout == SystemWaitForever) || (endTimeout > SystemGetMs()))) {
currentTimeout = endTimeout - SystemGetMs();
if (osMessageQueueGet(queueId, data, NULL, currentTimeout) == osOK) {
--size;
++received;
++data;
}
}
} else {
while (size) {
if (osMessageQueueGet(queueId, data, NULL, 0) == osOK) {
--size;
++received;
++data;
} else {
return received;
}
}
}
return received;
}
static uint16_t impSerialPortUsbTransmit(TSerialPortUsbNation *env, uint8_t *data, uint16_t size, uint32_t timeout) {
uint16_t count = size / 64;
uint16_t tail = size % 64;
uint16_t i = 0;
for (; i < count; ++i) {
CDC_Send_DATA(&data[i * 64], 64);
SystemDelayMs(5);
}
if (tail > 0) {
CDC_Send_DATA(&data[i * 64], tail);
SystemDelayMs(5);
}
return size;
}
static uint16_t impSerialPortReceive(TSerialPortUsbNation *env, uint8_t *data, uint16_t size, uint32_t timeout) {
return impSerialPortReceiveQueue(env, data, size, timeout, env->rxDataQueue);
}
void SerialPortUsb_Init(
TSerialPortUsbNation *env,
uint32_t rxBufferLength
) {
env->rxDataQueue = osMessageQueueNew(rxBufferLength, 1, NULL);
Set_System();
USB_Interrupts_Config();
Set_USBClock();
USB_Init();
}
tSerialPortIO SerialPortUsb_GetIo(TSerialPortUsbNation *env) {
tSerialPortIO io = {
.env = env,
.receive = (SerialPortIOTransaction) impSerialPortReceive,
.transmit = (SerialPortIOTransaction) impSerialPortUsbTransmit
};
return io;
}

24
SerialPort_USB.h Normal file
View File

@ -0,0 +1,24 @@
//
// Created by cfif on 13.11.2023.
//
#ifndef EMERGCALLDEV_SERIALPORT_USB_H
#define EMERGCALLDEV_SERIALPORT_USB_H
#include "SerialPortIO.h"
#include "cmsis_os2.h"
#include "n32g45x.h"
typedef struct {
osMessageQueueId_t rxDataQueue;
} TSerialPortUsbNation;
void SerialPortUsb_Init(
TSerialPortUsbNation *env,
uint32_t rxBufferLength
);
tSerialPortIO SerialPortUsb_GetIo(TSerialPortUsbNation *env);
#endif //EMERGCALLDEV_SERIALPORT_USB_H

74
inc/hw_config.h Normal file
View File

@ -0,0 +1,74 @@
/*****************************************************************************
* Copyright (c) 2019, Nations Technologies Inc.
*
* All rights reserved.
* ****************************************************************************
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
*
* Nations' name may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ****************************************************************************/
/**
* @file hw_config.h
* @author Nations
* @version v1.0.1
*
* @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
*/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __HW_CONFIG_H
#define __HW_CONFIG_H
/* Includes ------------------------------------------------------------------*/
#include "usb_type.h"
#include "cmsis_os2.h"
//#define USB_LOW_PWR_MGMT_SUPPORT
/* Exported types ------------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/
/* Exported macro ------------------------------------------------------------*/
/* Exported define -----------------------------------------------------------*/
#define MASS_MEMORY_START 0x04002000
#define BULK_MAX_PACKET_SIZE 0x00000040
#define LED_ON 0xF0
#define LED_OFF 0xFF
#define USART_RX_DATA_SIZE 2048
/* Exported functions ------------------------------------------------------- */
void Set_System(void);
void Set_USBClock(void);
void Enter_LowPowerMode(void);
void Leave_LowPowerMode(void);
void USB_Interrupts_Config(void);
void USART_Config_Default(void);
bool USART_Config(void);
void USB_To_USART_Send_Data(uint8_t* data_buffer, uint8_t Nb_bytes);
void USART_To_USB_Send_Data(void);
void Handle_USBAsynchXfer (void);
void Get_SerialNum(void);
void CDC_Send_DATA(uint8_t *ptrBuffer, uint16_t Send_length);
/* External variables --------------------------------------------------------*/
#endif /*__HW_CONFIG_H*/

73
inc/main.h Normal file
View File

@ -0,0 +1,73 @@
/*****************************************************************************
* Copyright (c) 2019, Nations Technologies Inc.
*
* All rights reserved.
* ****************************************************************************
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
*
* Nations' name may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ****************************************************************************/
/**
* @file main.h
* @author Nations
* @version v1.0.0
*
* @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
*/
#ifndef __MAIN_H__
#define __MAIN_H__
#ifdef __cplusplus
extern "C" {
#endif
#include "n32g45x.h"
/*Led1-PE6,Led2-PE7,Led3-PF6,Led4-PF7,Led5-PF8*/
#define PORTE_GROUP GPIOE
#define PORTF_GROUP GPIOF
#define LED1_PORT PORTE_GROUP
#define LED2_PORT PORTE_GROUP
#define LED3_PORT PORTF_GROUP
#define LED4_PORT PORTF_GROUP
#define LED5_PORT PORTF_GROUP
#define LED1_PIN GPIO_PIN_6
#define LED2_PIN GPIO_PIN_7
#define LED3_PIN GPIO_PIN_6
#define LED4_PIN GPIO_PIN_7
#define LED5_PIN GPIO_PIN_8
#ifdef __cplusplus
}
#endif
#endif /* __MAIN_H__ */
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/

70
inc/n32g45x_it.h Normal file
View File

@ -0,0 +1,70 @@
/*****************************************************************************
* Copyright (c) 2019, Nations Technologies Inc.
*
* All rights reserved.
* ****************************************************************************
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
*
* Nations' name may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ****************************************************************************/
/**
* @file n32g45x_it.h
* @author Nations
* @version v1.0.0
*
* @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
*/
#ifndef __N32G45X_IT_H__
#define __N32G45X_IT_H__
#ifdef __cplusplus
extern "C" {
#endif
#include "n32g45x.h"
void NMI_Handler(void);
void HardFault_Handler(void);
void MemManage_Handler(void);
void BusFault_Handler(void);
void UsageFault_Handler(void);
void SVC_Handler(void);
void DebugMon_Handler(void);
void PendSV_Handler(void);
void SysTick_Handler(void);
void DMA1_Channel6_IRQHandler(void);
void USB_LP_CAN1_RX0_IRQHandler(void);
#ifdef __cplusplus
}
#endif
#endif /* __N32G45X_IT_H__ */
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/

109
inc/usb_conf.h Normal file
View File

@ -0,0 +1,109 @@
/*****************************************************************************
* Copyright (c) 2019, Nations Technologies Inc.
*
* All rights reserved.
* ****************************************************************************
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
*
* Nations' name may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ****************************************************************************/
/**
* @file usb_conf.h
* @author Nations
* @version v1.0.0
*
* @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
*/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __USB_CONF_H
#define __USB_CONF_H
/* Includes ------------------------------------------------------------------*/
/* Exported types ------------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/
/* Exported macro ------------------------------------------------------------*/
/* Exported functions ------------------------------------------------------- */
/* External variables --------------------------------------------------------*/
/*-------------------------------------------------------------*/
/* EP_NUM */
/* defines how many endpoints are used by the device */
/*-------------------------------------------------------------*/
#define EP_NUM (4)
/*-------------------------------------------------------------*/
/* -------------- Buffer Description Table -----------------*/
/*-------------------------------------------------------------*/
/* buffer table base address */
/* buffer table base address */
#define BTABLE_ADDRESS (0x00)
/* EP0 */
/* rx/tx buffer base address */
#define ENDP0_RXADDR (0x40)
#define ENDP0_TXADDR (0x80)
/* EP1 */
/* tx buffer base address */
#define ENDP1_TXADDR (0xC0)
#define ENDP2_TXADDR (0x100)
#define ENDP3_RXADDR (0x110)
/*-------------------------------------------------------------*/
/* ------------------- ISTR events -------------------------*/
/*-------------------------------------------------------------*/
/* IMR_MSK */
/* mask defining which events has to be handled */
/* by the device application software */
#define IMR_MSK (CTRL_CTRSM | CTRL_WKUPM | CTRL_SUSPDM | CTRL_SOFM \
| CTRL_RSTM )
/*#define CTR_CALLBACK*/
/*#define DOVR_CALLBACK*/
/*#define ERR_CALLBACK*/
/*#define WKUP_CALLBACK*/
/*#define SUSP_CALLBACK*/
/*#define RESET_CALLBACK*/
#define SOF_CALLBACK
/*#define ESOF_CALLBACK*/
/* CTR service routines */
/* associated to defined endpoints */
/*#define EP1_IN_Callback USB_ProcessNop*/
#define EP2_IN_Callback USB_ProcessNop
#define EP3_IN_Callback USB_ProcessNop
#define EP4_IN_Callback USB_ProcessNop
#define EP5_IN_Callback USB_ProcessNop
#define EP6_IN_Callback USB_ProcessNop
#define EP7_IN_Callback USB_ProcessNop
#define EP1_OUT_Callback USB_ProcessNop
#define EP2_OUT_Callback USB_ProcessNop
/*#define EP3_OUT_Callback USB_ProcessNop*/
#define EP4_OUT_Callback USB_ProcessNop
#define EP5_OUT_Callback USB_ProcessNop
#define EP6_OUT_Callback USB_ProcessNop
#define EP7_OUT_Callback USB_ProcessNop
#endif /* __USB_CONF_H */

73
inc/usb_desc.h Normal file
View File

@ -0,0 +1,73 @@
/*****************************************************************************
* Copyright (c) 2019, Nations Technologies Inc.
*
* All rights reserved.
* ****************************************************************************
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
*
* Nations' name may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ****************************************************************************/
/**
* @file usb_desc.h
* @author Nations
* @version v1.0.0
*
* @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
*/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __USB_DESC_H
#define __USB_DESC_H
/* Includes ------------------------------------------------------------------*/
/* Exported types ------------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/
/* Exported macro ------------------------------------------------------------*/
/* Exported define -----------------------------------------------------------*/
#define USB_DEVICE_DESCRIPTOR_TYPE 0x01
#define USB_CONFIGURATION_DESCRIPTOR_TYPE 0x02
#define USB_STRING_DESCRIPTOR_TYPE 0x03
#define USB_INTERFACE_DESCRIPTOR_TYPE 0x04
#define USB_ENDPOINT_DESCRIPTOR_TYPE 0x05
#define VIRTUAL_COM_PORT_DATA_SIZE 64
#define VIRTUAL_COM_PORT_INT_SIZE 8
#define VIRTUAL_COM_PORT_SIZ_DEVICE_DESC 18
#define VIRTUAL_COM_PORT_SIZ_CONFIG_DESC 67
#define VIRTUAL_COM_PORT_SIZ_STRING_LANGID 4
#define VIRTUAL_COM_PORT_SIZ_STRING_VENDOR 38
#define VIRTUAL_COM_PORT_SIZ_STRING_PRODUCT 50
#define VIRTUAL_COM_PORT_SIZ_STRING_SERIAL 26
#define STANDARD_ENDPOINT_DESC_SIZE 0x09
/* Exported functions ------------------------------------------------------- */
extern const uint8_t Virtual_Com_Port_DeviceDescriptor[VIRTUAL_COM_PORT_SIZ_DEVICE_DESC];
extern const uint8_t Virtual_Com_Port_ConfigDescriptor[VIRTUAL_COM_PORT_SIZ_CONFIG_DESC];
extern const uint8_t Virtual_Com_Port_StringLangID[VIRTUAL_COM_PORT_SIZ_STRING_LANGID];
extern const uint8_t Virtual_Com_Port_StringVendor[VIRTUAL_COM_PORT_SIZ_STRING_VENDOR];
extern const uint8_t Virtual_Com_Port_StringProduct[VIRTUAL_COM_PORT_SIZ_STRING_PRODUCT];
extern uint8_t Virtual_Com_Port_StringSerial[VIRTUAL_COM_PORT_SIZ_STRING_SERIAL];
#endif /* __USB_DESC_H */

104
inc/usb_istr.h Normal file
View File

@ -0,0 +1,104 @@
/*****************************************************************************
* Copyright (c) 2019, Nations Technologies Inc.
*
* All rights reserved.
* ****************************************************************************
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
*
* Nations' name may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ****************************************************************************/
/**
* @file usb_istr.h
* @author Nations
* @version v1.0.0
*
* @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
*/
#ifndef __USB_ISTR_H__
#define __USB_ISTR_H__
#include "usb_conf.h"
void USB_Istr(void);
/* function prototypes Automatically built defining related macros */
void EP1_IN_Callback(void);
void EP2_IN_Callback(void);
void EP3_IN_Callback(void);
void EP4_IN_Callback(void);
void EP5_IN_Callback(void);
void EP6_IN_Callback(void);
void EP7_IN_Callback(void);
void EP1_OUT_Callback(void);
void EP2_OUT_Callback(void);
void EP3_OUT_Callback(void);
void EP4_OUT_Callback(void);
void EP5_OUT_Callback(void);
void EP6_OUT_Callback(void);
void EP7_OUT_Callback(void);
#ifdef CTR_CALLBACK
void CTR_Callback(void);
#endif
#ifdef DOVR_CALLBACK
void DOVR_Callback(void);
#endif
#ifdef ERR_CALLBACK
void ERR_Callback(void);
#endif
#ifdef WKUP_CALLBACK
void WKUP_Callback(void);
#endif
#ifdef SUSP_CALLBACK
void SUSP_Callback(void);
#endif
#ifdef RESET_CALLBACK
void RESET_Callback(void);
#endif
#ifdef SOF_CALLBACK
void SOF_Callback(void);
#endif
#ifdef ESOF_CALLBACK
void ESOF_Callback(void);
#endif
#endif /*__USB_ISTR_H__*/
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/

94
inc/usb_prop.h Normal file
View File

@ -0,0 +1,94 @@
/*****************************************************************************
* Copyright (c) 2019, Nations Technologies Inc.
*
* All rights reserved.
* ****************************************************************************
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
*
* Nations' name may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ****************************************************************************/
/**
* @file usb_prop.h
* @author Nations
* @version v1.0.0
*
* @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
*/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __usb_prop_H
#define __usb_prop_H
#include "stdint.h"
#include "usb_core.h"
/* Includes ------------------------------------------------------------------*/
/* Exported types ------------------------------------------------------------*/
typedef struct
{
uint32_t bitrate;
uint8_t format;
uint8_t paritytype;
uint8_t datatype;
}LINE_CODING;
/* Exported constants --------------------------------------------------------*/
/* Exported macro ------------------------------------------------------------*/
/* Exported define -----------------------------------------------------------*/
#define Virtual_Com_Port_GetConfiguration USB_ProcessNop
//#define Virtual_Com_Port_SetConfiguration USB_ProcessNop
#define Virtual_Com_Port_GetInterface USB_ProcessNop
#define Virtual_Com_Port_SetInterface USB_ProcessNop
#define Virtual_Com_Port_GetStatus USB_ProcessNop
#define Virtual_Com_Port_ClearFeature USB_ProcessNop
#define Virtual_Com_Port_SetEndPointFeature USB_ProcessNop
#define Virtual_Com_Port_SetDeviceFeature USB_ProcessNop
//#define Virtual_Com_Port_SetDeviceAddress USB_ProcessNop
#define SEND_ENCAPSULATED_COMMAND 0x00
#define GET_ENCAPSULATED_RESPONSE 0x01
#define SET_COMM_FEATURE 0x02
#define GET_COMM_FEATURE 0x03
#define CLEAR_COMM_FEATURE 0x04
#define SET_LINE_CODING 0x20
#define GET_LINE_CODING 0x21
#define SET_CONTROL_LINE_STATE 0x22
#define SEND_BREAK 0x23
/* Exported functions ------------------------------------------------------- */
void Virtual_Com_Port_init(void);
void Virtual_Com_Port_Reset(void);
void Virtual_Com_Port_SetConfiguration(void);
void Virtual_Com_Port_SetDeviceAddress (void);
void Virtual_Com_Port_Status_In (void);
void Virtual_Com_Port_Status_Out (void);
USB_Result Virtual_Com_Port_Data_Setup(uint8_t);
USB_Result Virtual_Com_Port_NoData_Setup(uint8_t);
USB_Result Virtual_Com_Port_Get_Interface_Setting(uint8_t Interface, uint8_t AlternateSetting);
uint8_t *Virtual_Com_Port_GetDeviceDescriptor(uint16_t );
uint8_t *Virtual_Com_Port_GetConfigDescriptor(uint16_t);
uint8_t *Virtual_Com_Port_GetStringDescriptor(uint16_t);
uint8_t *Virtual_Com_Port_GetLineCoding(uint16_t Length);
uint8_t *Virtual_Com_Port_SetLineCoding(uint16_t Length);
#endif /* __usb_prop_H */

83
inc/usb_pwr.h Normal file
View File

@ -0,0 +1,83 @@
/*****************************************************************************
* Copyright (c) 2019, Nations Technologies Inc.
*
* All rights reserved.
* ****************************************************************************
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
*
* Nations' name may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ****************************************************************************/
/**
* @file usb_pwr.h
* @author Nations
* @version v1.0.0
*
* @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
*/
#ifndef __USB_PWR_H__
#define __USB_PWR_H__
#include "usb_core.h"
#include "usb_type.h"
typedef enum _RESUME_STATE
{
RESUME_EXTERNAL,
RESUME_INTERNAL,
RESUME_LATER,
RESUME_WAIT,
RESUME_START,
RESUME_ON,
RESUME_OFF,
RESUME_ESOF
} RESUME_STATE;
typedef enum _DEVICE_STATE
{
UNCONNECTED,
ATTACHED,
POWERED,
SUSPENDED,
ADDRESSED,
CONFIGURED
} DEVICE_STATE;
void Suspend(void);
void Resume_Init(void);
void Resume(RESUME_STATE eResumeSetVal);
USB_Result PowerOn(void);
USB_Result PowerOff(void);
extern __IO uint32_t bDeviceState; /* USB device status */
extern __IO bool fSuspendEnabled; /* true when suspend is possible */
#endif /*__USB_PWR_H__*/
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/

15
modular.json Normal file
View File

@ -0,0 +1,15 @@
{
"cmake": {
"inc_dirs": [
"./n32g45x_usbfs_driver/inc",
"./inc",
"./"
],
"srcs": [
"./n32g45x_usbfs_driver/src/*.c",
"./src/*.c",
"*.c",
"./**.s"
]
}
}

View File

@ -0,0 +1,264 @@
/*****************************************************************************
* Copyright (c) 2019, Nations Technologies Inc.
*
* All rights reserved.
* ****************************************************************************
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
*
* Nations' name may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ****************************************************************************/
/**
* @file usb_core.h
* @author Nations
* @version v1.0.0
*
* @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
*/
#ifndef __USB_CORE_H__
#define __USB_CORE_H__
#include "n32g45x.h"
/**
* @addtogroup N32G45X_USB_Driver
* @brief N32G45x USB low level driver
* @{
*/
typedef enum _CONTROL_STATE
{
WaitSetup, /* 0 */
SettingUp, /* 1 */
InData, /* 2 */
OutData, /* 3 */
LastInData, /* 4 */
LastOutData, /* 5 */
WaitStatusIn, /* 7 */
WaitStatusOut, /* 8 */
Stalled, /* 9 */
Pause /* 10 */
} USB_ControlState; /* The state machine states of a control pipe */
typedef struct OneDescriptor
{
uint8_t* Descriptor;
uint16_t Descriptor_Size;
} USB_OneDescriptor, *PONE_DESCRIPTOR;
/* All the request process routines return a value of this type
If the return value is not SUCCESS or NOT_READY,
the software will STALL the correspond endpoint */
typedef enum _RESULT
{
Success = 0, /* Process successfully */
Error,
UnSupport,
Not_Ready /* The process has not been finished, endpoint will be
NAK to further request */
} USB_Result;
/*-*-*-*-*-*-*-*-*-*-* Definitions for endpoint level -*-*-*-*-*-*-*-*-*-*-*-*/
typedef struct _ENDPOINT_INFO
{
/* When send data out of the device,
CopyData() is used to get data buffer 'Length' bytes data
if Length is 0,
CopyData() returns the total length of the data
if the request is not supported, returns 0
(NEW Feature )
if CopyData() returns -1, the calling routine should not proceed
further and will resume the SETUP process by the class device
if Length is not 0,
CopyData() returns a pointer to indicate the data location
Usb_wLength is the data remain to be sent,
Usb_wOffset is the Offset of original data
When receive data from the host,
CopyData() is used to get user data buffer which is capable
of Length bytes data to copy data from the endpoint buffer.
if Length is 0,
CopyData() returns the available data length,
if Length is not 0,
CopyData() returns user buffer address
Usb_rLength is the data remain to be received,
Usb_rPointer is the Offset of data buffer
*/
uint16_t Usb_wLength;
uint16_t Usb_wOffset;
uint16_t PacketSize;
uint8_t* (*CopyData)(uint16_t Length);
} USB_EndpointMess;
/*-*-*-*-*-*-*-*-*-*-*-* Definitions for device level -*-*-*-*-*-*-*-*-*-*-*-*/
typedef struct _DEVICE
{
uint8_t TotalEndpoint; /* Number of endpoints that are used */
uint8_t TotalConfiguration; /* Number of configuration available */
} USB_Device;
typedef union
{
uint16_t w;
struct BW
{
uint8_t bb1;
uint8_t bb0;
} bw;
} uint16_t_uint8_t;
typedef struct _DEVICE_INFO
{
uint8_t bmRequestType; /* bmRequestType */
uint8_t bRequest; /* bRequest */
uint16_t_uint8_t wValues; /* wValue */
uint16_t_uint8_t wIndexs; /* wIndex */
uint16_t_uint8_t wLengths; /* wLength */
uint8_t CtrlState; /* of type USB_ControlState */
uint8_t CurrentFeature;
uint8_t CurrentConfiguration; /* Selected configuration */
uint8_t CurrentInterface; /* Selected interface of current configuration */
uint8_t CurrentAlternateSetting; /* Selected Alternate Setting of current
interface*/
USB_EndpointMess Ctrl_Info;
} USB_DeviceMess;
typedef struct _DEVICE_PROP
{
void (*Init)(void); /* Initialize the device */
void (*Reset)(void); /* Reset routine of this device */
/* Device dependent process after the status stage */
void (*Process_Status_IN)(void);
void (*Process_Status_OUT)(void);
/* Procedure of process on setup stage of a class specified request with data stage */
/* All class specified requests with data stage are processed in Class_Data_Setup
Class_Data_Setup()
responses to check all special requests and fills USB_EndpointMess
according to the request
If IN tokens are expected, then wLength & wOffset will be filled
with the total transferring bytes and the starting position
If OUT tokens are expected, then rLength & rOffset will be filled
with the total expected bytes and the starting position in the buffer
If the request is valid, Class_Data_Setup returns SUCCESS, else UNSUPPORT
CAUTION:
Since GET_CONFIGURATION & GET_INTERFACE are highly related to
the individual classes, they will be checked and processed here.
*/
USB_Result (*Class_Data_Setup)(uint8_t RequestNo);
/* Procedure of process on setup stage of a class specified request without data stage */
/* All class specified requests without data stage are processed in Class_NoData_Setup
Class_NoData_Setup
responses to check all special requests and perform the request
CAUTION:
Since SET_CONFIGURATION & SET_INTERFACE are highly related to
the individual classes, they will be checked and processed here.
*/
USB_Result (*Class_NoData_Setup)(uint8_t RequestNo);
/*Class_Get_Interface_Setting
This function is used by the file usb_core.c to test if the selected Interface
and Alternate Setting (uint8_t Interface, uint8_t AlternateSetting) are supported by
the application.
This function is writing by user. It should return "SUCCESS" if the Interface
and Alternate Setting are supported by the application or "UNSUPPORT" if they
are not supported. */
USB_Result (*Class_Get_Interface_Setting)(uint8_t Interface, uint8_t AlternateSetting);
uint8_t* (*GetDeviceDescriptor)(uint16_t Length);
uint8_t* (*GetConfigDescriptor)(uint16_t Length);
uint8_t* (*GetStringDescriptor)(uint16_t Length);
/* This field is not used in current library version. It is kept only for
compatibility with previous versions */
void* RxEP_buffer;
uint8_t MaxPacketSize;
} DEVICE_PROP;
typedef struct _USER_STANDARD_REQUESTS
{
void (*User_GetConfiguration)(void); /* Get Configuration */
void (*User_SetConfiguration)(void); /* Set Configuration */
void (*User_GetInterface)(void); /* Get Interface */
void (*User_SetInterface)(void); /* Set Interface */
void (*User_GetStatus)(void); /* Get Status */
void (*User_ClearFeature)(void); /* Clear Feature */
void (*User_SetEndPointFeature)(void); /* Set Endpoint Feature */
void (*User_SetDeviceFeature)(void); /* Set Device Feature */
void (*User_SetDeviceAddress)(void); /* Set Device Address */
} USER_STANDARD_REQUESTS;
#define Type_Recipient (pInformation->bmRequestType & (REQUEST_TYPE | RECIPIENT))
#define Usb_rLength Usb_wLength
#define Usb_rOffset Usb_wOffset
#define USBwValue wValues.w
#define USBwValue0 wValues.bw.bb0
#define USBwValue1 wValues.bw.bb1
#define USBwIndex wIndexs.w
#define USBwIndex0 wIndexs.bw.bb0
#define USBwIndex1 wIndexs.bw.bb1
#define USBwLength wLengths.w
#define USBwLength0 wLengths.bw.bb0
#define USBwLength1 wLengths.bw.bb1
uint8_t USB_ProcessSetup0(void);
uint8_t USB_ProcessPost0(void);
uint8_t USB_ProcessOut0(void);
uint8_t USB_ProcessIn0(void);
USB_Result Standard_SetEndPointFeature(void);
USB_Result Standard_SetDeviceFeature(void);
uint8_t* Standard_GetConfiguration(uint16_t Length);
USB_Result Standard_SetConfiguration(void);
uint8_t* Standard_GetInterface(uint16_t Length);
USB_Result Standard_SetInterface(void);
uint8_t* Standard_GetDescriptorData(uint16_t Length, PONE_DESCRIPTOR pDesc);
uint8_t* Standard_GetStatus(uint16_t Length);
USB_Result Standard_ClearFeature(void);
void USB_SetDeviceAddress(uint8_t);
void USB_ProcessNop(void);
extern DEVICE_PROP Device_Property;
extern USER_STANDARD_REQUESTS User_Standard_Requests;
extern USB_Device Device_Table;
extern USB_DeviceMess Device_Info;
/* cells saving status during interrupt servicing */
extern __IO uint16_t SaveRState;
extern __IO uint16_t SaveTState;
/**
* @}
*/
#endif /* __USB_CORE_H__ */

View File

@ -0,0 +1,98 @@
/*****************************************************************************
* Copyright (c) 2019, Nations Technologies Inc.
*
* All rights reserved.
* ****************************************************************************
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
*
* Nations' name may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ****************************************************************************/
/**
* @file usb_def.h
* @author Nations
* @version v1.0.0
*
* @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
*/
#ifndef __USB_DEF_H__
#define __USB_DEF_H__
/**
* @addtogroup N32G45X_USB_Driver
* @{
*/
typedef enum _RECIPIENT_TYPE
{
DEVICE_RECIPIENT, /* Recipient device */
INTERFACE_RECIPIENT, /* Recipient interface */
ENDPOINT_RECIPIENT, /* Recipient endpoint */
OTHER_RECIPIENT
} RECIPIENT_TYPE;
typedef enum _STANDARD_REQUESTS
{
GET_STATUS = 0,
CLR_FEATURE,
RESERVED1,
SET_FEATURE,
RESERVED2,
SET_ADDRESS,
GET_DESCRIPTOR,
SET_DESCRIPTOR,
GET_CONFIGURATION,
SET_CONFIGURATION,
GET_INTERFACE,
SET_INTERFACE,
TOTAL_SREQUEST, /* Total number of Standard request */
SYNCH_FRAME = 12
} STANDARD_REQUESTS;
/* Definition of "USBwValue" */
typedef enum _DESCRIPTOR_TYPE
{
DEVICE_DESCRIPTOR = 1,
CONFIG_DESCRIPTOR,
STRING_DESCRIPTOR,
INTERFACE_DESCRIPTOR,
ENDPOINT_DESCRIPTOR
} DESCRIPTOR_TYPE;
/* Feature selector of a SET_FEATURE or CLR_FEATURE */
typedef enum _FEATURE_SELECTOR
{
ENDPOINT_STALL,
DEVICE_REMOTE_WAKEUP
} FEATURE_SELECTOR;
/* Definition of "bmRequestType" */
#define REQUEST_TYPE 0x60 /* Mask to get request type */
#define STANDARD_REQUEST 0x00 /* Standard request */
#define CLASS_REQUEST 0x20 /* Class request */
#define VENDOR_REQUEST 0x40 /* Vendor request */
#define RECIPIENT 0x1F /* Mask to get recipient */
/**
* @}
*/
#endif /* __USB_DEF_H__ */

View File

@ -0,0 +1,71 @@
/*****************************************************************************
* Copyright (c) 2019, Nations Technologies Inc.
*
* All rights reserved.
* ****************************************************************************
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
*
* Nations' name may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ****************************************************************************/
/**
* @file usb_init.h
* @author Nations
* @version v1.0.0
*
* @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
*/
#ifndef __USB_INIT_H__
#define __USB_INIT_H__
#include "n32g45x.h"
#include "usb_core.h"
/**
* @addtogroup N32G45X_USB_Driver
* @{
*/
void USB_Init(void);
/* The number of current endpoint, it will be used to specify an endpoint */
extern uint8_t EPindex;
/* The number of current device, it is an index to the Device_Table */
/*extern uint8_t Device_no; */
/* Points to the USB_DeviceMess structure of current device */
/* The purpose of this register is to speed up the execution */
extern USB_DeviceMess* pInformation;
/* Points to the DEVICE_PROP structure of current device */
/* The purpose of this register is to speed up the execution */
extern DEVICE_PROP* pProperty;
/* Temporary save the state of Rx & Tx status. */
/* Whenever the Rx or Tx state is changed, its value is saved */
/* in this variable first and will be set to the EPRB or EPRA */
/* at the end of interrupt process */
extern USER_STANDARD_REQUESTS* pUser_Standard_Requests;
extern uint16_t SaveState;
extern uint16_t wInterrupt_Mask;
/**
* @}
*/
#endif /* __USB_INIT_H__ */

View File

@ -0,0 +1,50 @@
/*****************************************************************************
* Copyright (c) 2019, Nations Technologies Inc.
*
* All rights reserved.
* ****************************************************************************
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
*
* Nations' name may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ****************************************************************************/
/**
* @file usb_int.h
* @author Nations
* @version v1.0.0
*
* @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
*/
#ifndef __USB_INT_H__
#define __USB_INT_H__
/**
* @addtogroup N32G45X_USB_Driver
* @{
*/
void USB_CorrectTransferLp(void);
void USB_CorrectTransferHp(void);
/**
* @}
*/
#endif /* __USB_INT_H__ */

View File

@ -0,0 +1,47 @@
/*****************************************************************************
* Copyright (c) 2019, Nations Technologies Inc.
*
* All rights reserved.
* ****************************************************************************
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
*
* Nations' name may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ****************************************************************************/
/**
* @file usb_lib.h
* @author Nations
* @version v1.0.0
*
* @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
*/
#ifndef __USB_LIB_H__
#define __USB_LIB_H__
#include "usb_type.h"
#include "usb_regs.h"
#include "usb_def.h"
#include "usb_core.h"
#include "usb_init.h"
#include "usb_sil.h"
#include "usb_mem.h"
#include "usb_int.h"
#endif /* __USB_LIB_H__ */

View File

@ -0,0 +1,52 @@
/*****************************************************************************
* Copyright (c) 2019, Nations Technologies Inc.
*
* All rights reserved.
* ****************************************************************************
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
*
* Nations' name may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ****************************************************************************/
/**
* @file usb_mem.h
* @author Nations
* @version v1.0.0
*
* @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
*/
#ifndef __USB_MEM_H__
#define __USB_MEM_H__
#include "n32g45x.h"
/**
* @addtogroup N32G45X_USB_Driver
* @{
*/
void USB_CopyUserToPMABuf(uint8_t* pbUsrBuf, uint16_t wPMABufAddr, uint16_t wNBytes);
void USB_CopyPMAToUserBuf(uint8_t* pbUsrBuf, uint16_t wPMABufAddr, uint16_t wNBytes);
/**
* @}
*/
#endif /*__USB_MEM_H__*/

View File

@ -0,0 +1,715 @@
/*****************************************************************************
* Copyright (c) 2019, Nations Technologies Inc.
*
* All rights reserved.
* ****************************************************************************
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
*
* Nations' name may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ****************************************************************************/
/**
* @file usb_regs.h
* @author Nations
* @version v1.0.0
*
* @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
*/
#ifndef __USB_REGS_H__
#define __USB_REGS_H__
#include "n32g45x.h"
/**
* @addtogroup N32G45X_USB_Driver
* @{
*/
typedef enum _EP_DBUF_DIR
{
/* double buffered endpoint direction */
EP_DBUF_ERR,
EP_DBUF_OUT,
EP_DBUF_IN
} EP_DBUF_DIR;
/* endpoint buffer number */
enum EP_BUF_NUM
{
EP_NOBUF,
EP_BUF0,
EP_BUF1
};
#define RegBase (0x40005C00L) /* USB_IP Peripheral Registers base address */
#define PMAAddr (0x40006000L) /* USB_IP Packet Memory Area base address */
/******************************************************************************/
/* Special registers */
/******************************************************************************/
/* Pull up controller register */
#define DP_CTRL ((__IO unsigned*)(0x40001820))
#define _EnPortPullup() (*DP_CTRL = (*DP_CTRL) | 0x10000000);
#define _DisPortPullup() (*DP_CTRL = (*DP_CTRL) & 0xEFFFFFFF);
/******************************************************************************/
/* General registers */
/******************************************************************************/
/* Control register */
#define USB_CTRL ((__IO unsigned*)(RegBase + 0x40))
/* Interrupt status register */
#define USB_STS ((__IO unsigned*)(RegBase + 0x44))
/* Frame number register */
#define USB_FN ((__IO unsigned*)(RegBase + 0x48))
/* Device address register */
#define USB_ADDR ((__IO unsigned*)(RegBase + 0x4C))
/* Buffer Table address register */
#define USB_BUFTAB ((__IO unsigned*)(RegBase + 0x50))
/******************************************************************************/
/* Endpoint registers */
/******************************************************************************/
#define EP0REG ((__IO unsigned*)(RegBase)) /* endpoint 0 register address */
/* Endpoint Addresses (w/direction) */
#define EP0_OUT ((uint8_t)0x00)
#define EP0_IN ((uint8_t)0x80)
#define EP1_OUT ((uint8_t)0x01)
#define EP1_IN ((uint8_t)0x81)
#define EP2_OUT ((uint8_t)0x02)
#define EP2_IN ((uint8_t)0x82)
#define EP3_OUT ((uint8_t)0x03)
#define EP3_IN ((uint8_t)0x83)
#define EP4_OUT ((uint8_t)0x04)
#define EP4_IN ((uint8_t)0x84)
#define EP5_OUT ((uint8_t)0x05)
#define EP5_IN ((uint8_t)0x85)
#define EP6_OUT ((uint8_t)0x06)
#define EP6_IN ((uint8_t)0x86)
#define EP7_OUT ((uint8_t)0x07)
#define EP7_IN ((uint8_t)0x87)
/* endpoints enumeration */
#define ENDP0 ((uint8_t)0)
#define ENDP1 ((uint8_t)1)
#define ENDP2 ((uint8_t)2)
#define ENDP3 ((uint8_t)3)
#define ENDP4 ((uint8_t)4)
#define ENDP5 ((uint8_t)5)
#define ENDP6 ((uint8_t)6)
#define ENDP7 ((uint8_t)7)
/******************************************************************************/
/* USB_STS interrupt events */
/******************************************************************************/
#define STS_CTRS (0x8000) /* Correct TRansfer (clear-only bit) */
#define STS_DOVR (0x4000) /* DMA OVeR/underrun (clear-only bit) */
#define STS_ERROR (0x2000) /* ERRor (clear-only bit) */
#define STS_WKUP (0x1000) /* WaKe UP (clear-only bit) */
#define STS_SUSPD (0x0800) /* SUSPend (clear-only bit) */
#define STS_RST (0x0400) /* RESET (clear-only bit) */
#define STS_SOF (0x0200) /* Start Of Frame (clear-only bit) */
#define STS_ESOF (0x0100) /* Expected Start Of Frame (clear-only bit) */
#define STS_DIR (0x0010) /* DIRection of transaction (read-only bit) */
#define STS_EP_ID (0x000F) /* EndPoint IDentifier (read-only bit) */
#define CLR_CTRS (~STS_CTRS) /* clear Correct TRansfer bit */
#define CLR_DOVR (~STS_DOVR) /* clear DMA OVeR/underrun bit*/
#define CLR_ERROR (~STS_ERROR) /* clear ERRor bit */
#define CLR_WKUP (~STS_WKUP) /* clear WaKe UP bit */
#define CLR_SUSPD (~STS_SUSPD) /* clear SUSPend bit */
#define CLR_RST (~STS_RST) /* clear RESET bit */
#define CLR_SOF (~STS_SOF) /* clear Start Of Frame bit */
#define CLR_ESOF (~STS_ESOF) /* clear Expected Start Of Frame bit */
/******************************************************************************/
/* USB_CTRL control register bits definitions */
/******************************************************************************/
#define CTRL_CTRSM (0x8000) /* Correct TRansfer Mask */
#define CTRL_DOVRM (0x4000) /* DMA OVeR/underrun Mask */
#define CTRL_ERRORM (0x2000) /* ERRor Mask */
#define CTRL_WKUPM (0x1000) /* WaKe UP Mask */
#define CTRL_SUSPDM (0x0800) /* SUSPend Mask */
#define CTRL_RSTM (0x0400) /* RESET Mask */
#define CTRL_SOFM (0x0200) /* Start Of Frame Mask */
#define CTRL_ESOFM (0x0100) /* Expected Start Of Frame Mask */
#define CTRL_RESUM (0x0010) /* RESUME request */
#define CTRL_FSUSPD (0x0008) /* Force SUSPend */
#define CTRL_LP_MODE (0x0004) /* Low-power MODE */
#define CTRL_PD (0x0002) /* Power DoWN */
#define CTRL_FRST (0x0001) /* Force USB RESet */
/******************************************************************************/
/* USB_FN Frame Number Register bit definitions */
/******************************************************************************/
#define FN_RXDP (0x8000) /* status of D+ data line */
#define FN_RXDM (0x4000) /* status of D- data line */
#define FN_LCK (0x2000) /* LoCKed */
#define FN_LSOF (0x1800) /* Lost SOF */
#define FN_FNUM (0x07FF) /* Frame Number */
/******************************************************************************/
/* USB_ADDR Device ADDRess bit definitions */
/******************************************************************************/
#define ADDR_EFUC (0x80)
#define ADDR_ADDR (0x7F)
/******************************************************************************/
/* Endpoint register */
/******************************************************************************/
/* bit positions */
#define EP_CTRS_RX (0x8000) /* EndPoint Correct TRansfer RX */
#define EP_DATTOG_RX (0x4000) /* EndPoint Data TOGGLE RX */
#define EPRX_STS (0x3000) /* EndPoint RX STATus bit field */
#define EP_SETUP (0x0800) /* EndPoint SETUP */
#define EP_T_FIELD (0x0600) /* EndPoint TYPE */
#define EP_KIND (0x0100) /* EndPoint KIND */
#define EP_CTRS_TX (0x0080) /* EndPoint Correct TRansfer TX */
#define EP_DATTOG_TX (0x0040) /* EndPoint Data TOGGLE TX */
#define EPTX_STS (0x0030) /* EndPoint TX STATus bit field */
#define EPADDR_FIELD (0x000F) /* EndPoint ADDRess FIELD */
/* EndPoint REGister INTEN (no toggle fields) */
#define EPREG_MASK (EP_CTRS_RX | EP_SETUP | EP_T_FIELD | EP_KIND | EP_CTRS_TX | EPADDR_FIELD)
/* EP_TYPE[1:0] EndPoint TYPE */
#define EP_TYPE_MASK (0x0600) /* EndPoint TYPE Mask */
#define EP_BULK (0x0000) /* EndPoint BULK */
#define EP_CONTROL (0x0200) /* EndPoint CONTROL */
#define EP_ISOCHRONOUS (0x0400) /* EndPoint ISOCHRONOUS */
#define EP_INTERRUPT (0x0600) /* EndPoint INTERRUPT */
#define EP_T_MASK (~EP_T_FIELD & EPREG_MASK)
/* EP_KIND EndPoint KIND */
#define EPKIND_MASK (~EP_KIND & EPREG_MASK)
/* STAT_TX[1:0] STATus for TX transfer */
#define EP_TX_DIS (0x0000) /* EndPoint TX DISabled */
#define EP_TX_STALL (0x0010) /* EndPoint TX STALLed */
#define EP_TX_NAK (0x0020) /* EndPoint TX NAKed */
#define EP_TX_VALID (0x0030) /* EndPoint TX VALID */
#define EPTX_DATTOG1 (0x0010) /* EndPoint TX Data TOGgle bit1 */
#define EPTX_DATTOG2 (0x0020) /* EndPoint TX Data TOGgle bit2 */
#define EPTX_DATTOGMASK (EPTX_STS | EPREG_MASK)
/* STAT_RX[1:0] STATus for RX transfer */
#define EP_RX_DIS (0x0000) /* EndPoint RX DISabled */
#define EP_RX_STALL (0x1000) /* EndPoint RX STALLed */
#define EP_RX_NAK (0x2000) /* EndPoint RX NAKed */
#define EP_RX_VALID (0x3000) /* EndPoint RX VALID */
#define EPRX_DATTOG1 (0x1000) /* EndPoint RX Data TOGgle bit1 */
#define EPRX_DATTOG2 (0x2000) /* EndPoint RX Data TOGgle bit1 */
#define EPRX_DATTOGMASK (EPRX_STS | EPREG_MASK)
/* USB_SetCtrl */
#define _SetCNTR(wRegValue) (*USB_CTRL = (uint16_t)wRegValue)
/* USB_SetSts */
#define _SetISTR(wRegValue) (*USB_STS = (uint16_t)wRegValue)
/* USB_SetAddr */
#define _SetDADDR(wRegValue) (*USB_ADDR = (uint16_t)wRegValue)
/* USB_SetBuftab */
#define _SetBTABLE(wRegValue) (*USB_BUFTAB = (uint16_t)(wRegValue & 0xFFF8))
/* USB_GetCtrl */
#define _GetCNTR() ((uint16_t)*USB_CTRL)
/* USB_GetSts */
#define _GetISTR() ((uint16_t)*USB_STS)
/* USB_GetFn */
#define _GetFNR() ((uint16_t)*USB_FN)
/* USB_GetAddr */
#define _GetDADDR() ((uint16_t)*USB_ADDR)
/* USB_GetBTABLE */
#define _GetBTABLE() ((uint16_t)*USB_BUFTAB)
/* USB_SetEndPoint */
#define _SetENDPOINT(bEpNum, wRegValue) (*(EP0REG + bEpNum) = (uint16_t)wRegValue)
/* USB_GetEndPoint */
#define _GetENDPOINT(bEpNum) ((uint16_t)(*(EP0REG + bEpNum)))
/*******************************************************************************
* Macro Name : USB_SetEpType
* Description : sets the type in the endpoint register(bits EP_TYPE[1:0])
* Input : bEpNum: Endpoint Number.
* wType
* Output : None.
* Return : None.
*******************************************************************************/
#define _SetEPType(bEpNum, wType) (_SetENDPOINT(bEpNum, ((_GetENDPOINT(bEpNum) & EP_T_MASK) | wType)))
/*******************************************************************************
* Macro Name : USB_GetEpType
* Description : gets the type in the endpoint register(bits EP_TYPE[1:0])
* Input : bEpNum: Endpoint Number.
* Output : None.
* Return : Endpoint Type
*******************************************************************************/
#define _GetEPType(bEpNum) (_GetENDPOINT(bEpNum) & EP_T_FIELD)
/*******************************************************************************
* Macro Name : SetEPTxStatus
* Description : sets the status for tx transfer (bits STAT_TX[1:0]).
* Input : bEpNum: Endpoint Number.
* wState: new state
* Output : None.
* Return : None.
*******************************************************************************/
#define _SetEPTxStatus(bEpNum, wState) \
{ \
register uint16_t _wRegVal; \
_wRegVal = _GetENDPOINT(bEpNum) & EPTX_DATTOGMASK; \
/* toggle first bit ? */ \
if ((EPTX_DATTOG1 & wState) != 0) \
_wRegVal ^= EPTX_DATTOG1; \
/* toggle second bit ? */ \
if ((EPTX_DATTOG2 & wState) != 0) \
_wRegVal ^= EPTX_DATTOG2; \
_SetENDPOINT(bEpNum, (_wRegVal | EP_CTRS_RX | EP_CTRS_TX)); \
} /* _SetEPTxStatus */
/*******************************************************************************
* Macro Name : SetEPRxStatus
* Description : sets the status for rx transfer (bits STAT_TX[1:0])
* Input : bEpNum: Endpoint Number.
* wState: new state.
* Output : None.
* Return : None.
*******************************************************************************/
#define _SetEPRxStatus(bEpNum, wState) \
{ \
register uint16_t _wRegVal; \
\
_wRegVal = _GetENDPOINT(bEpNum) & EPRX_DATTOGMASK; \
/* toggle first bit ? */ \
if ((EPRX_DATTOG1 & wState) != 0) \
_wRegVal ^= EPRX_DATTOG1; \
/* toggle second bit ? */ \
if ((EPRX_DATTOG2 & wState) != 0) \
_wRegVal ^= EPRX_DATTOG2; \
_SetENDPOINT(bEpNum, (_wRegVal | EP_CTRS_RX | EP_CTRS_TX)); \
} /* _SetEPRxStatus */
/*******************************************************************************
* Macro Name : SetEPRxTxStatus
* Description : sets the status for rx & tx (bits STAT_TX[1:0] & STAT_RX[1:0])
* Input : bEpNum: Endpoint Number.
* wStaterx: new state.
* wStatetx: new state.
* Output : None.
* Return : None.
*******************************************************************************/
#define _SetEPRxTxStatus(bEpNum, wStaterx, wStatetx) \
{ \
register uint32_t _wRegVal; \
\
_wRegVal = _GetENDPOINT(bEpNum) & (EPRX_DATTOGMASK | EPTX_STS); \
/* toggle first bit ? */ \
if ((EPRX_DATTOG1 & wStaterx) != 0) \
_wRegVal ^= EPRX_DATTOG1; \
/* toggle second bit ? */ \
if ((EPRX_DATTOG2 & wStaterx) != 0) \
_wRegVal ^= EPRX_DATTOG2; \
/* toggle first bit ? */ \
if ((EPTX_DATTOG1 & wStatetx) != 0) \
_wRegVal ^= EPTX_DATTOG1; \
/* toggle second bit ? */ \
if ((EPTX_DATTOG2 & wStatetx) != 0) \
_wRegVal ^= EPTX_DATTOG2; \
_SetENDPOINT(bEpNum, _wRegVal | EP_CTRS_RX | EP_CTRS_TX); \
} /* _SetEPRxTxStatus */
/*******************************************************************************
* Macro Name : USB_GetEpTxSts / USB_GetEpRxSts
* Description : gets the status for tx/rx transfer (bits STAT_TX[1:0]
* /STAT_RX[1:0])
* Input : bEpNum: Endpoint Number.
* Output : None.
* Return : status .
*******************************************************************************/
#define _GetEPTxStatus(bEpNum) ((uint16_t)_GetENDPOINT(bEpNum) & EPTX_STS)
#define _GetEPRxStatus(bEpNum) ((uint16_t)_GetENDPOINT(bEpNum) & EPRX_STS)
/*******************************************************************************
* Macro Name : USB_SetEpTxValid / USB_SetEpRxValid
* Description : sets directly the VALID tx/rx-status into the enpoint register
* Input : bEpNum: Endpoint Number.
* Output : None.
* Return : None.
*******************************************************************************/
#define _SetEPTxValid(bEpNum) (_SetEPTxStatus(bEpNum, EP_TX_VALID))
#define _SetEPRxValid(bEpNum) (_SetEPRxStatus(bEpNum, EP_RX_VALID))
/*******************************************************************************
* Macro Name : USB_GetTxStallSts / USB_GetRxStallSts.
* Description : checks stall condition in an endpoint.
* Input : bEpNum: Endpoint Number.
* Output : None.
* Return : TRUE = endpoint in stall condition.
*******************************************************************************/
#define _GetTxStallStatus(bEpNum) (_GetEPTxStatus(bEpNum) == EP_TX_STALL)
#define _GetRxStallStatus(bEpNum) (_GetEPRxStatus(bEpNum) == EP_RX_STALL)
/*******************************************************************************
* Macro Name : USB_SetEpKind / USB_ClrEpKind.
* Description : set & clear EP_KIND bit.
* Input : bEpNum: Endpoint Number.
* Output : None.
* Return : None.
*******************************************************************************/
#define _SetEP_KIND(bEpNum) \
(_SetENDPOINT(bEpNum, (EP_CTRS_RX | EP_CTRS_TX | ((_GetENDPOINT(bEpNum) | EP_KIND) & EPREG_MASK))))
#define _ClearEP_KIND(bEpNum) (_SetENDPOINT(bEpNum, (EP_CTRS_RX | EP_CTRS_TX | (_GetENDPOINT(bEpNum) & EPKIND_MASK))))
/*******************************************************************************
* Macro Name : USB_SetStsOut / USB_ClrStsOut.
* Description : Sets/clears directly STATUS_OUT bit in the endpoint register.
* Input : bEpNum: Endpoint Number.
* Output : None.
* Return : None.
*******************************************************************************/
#define _Set_Status_Out(bEpNum) _SetEP_KIND(bEpNum)
#define _Clear_Status_Out(bEpNum) _ClearEP_KIND(bEpNum)
/*******************************************************************************
* Macro Name : USB_SetEpDoubleBufer / USB_ClrEpDoubleBufer.
* Description : Sets/clears directly EP_KIND bit in the endpoint register.
* Input : bEpNum: Endpoint Number.
* Output : None.
* Return : None.
*******************************************************************************/
#define _SetEPDoubleBuff(bEpNum) _SetEP_KIND(bEpNum)
#define _ClearEPDoubleBuff(bEpNum) _ClearEP_KIND(bEpNum)
/*******************************************************************************
* Macro Name : USB_ClrEpCtrsRx / USB_ClrEpCtrsTx.
* Description : Clears bit CTR_RX / CTR_TX in the endpoint register.
* Input : bEpNum: Endpoint Number.
* Output : None.
* Return : None.
*******************************************************************************/
#define _ClearEP_CTR_RX(bEpNum) (_SetENDPOINT(bEpNum, _GetENDPOINT(bEpNum) & 0x7FFF & EPREG_MASK))
#define _ClearEP_CTR_TX(bEpNum) (_SetENDPOINT(bEpNum, _GetENDPOINT(bEpNum) & 0xFF7F & EPREG_MASK))
/*******************************************************************************
* Macro Name : USB_DattogRx / USB_DattogTx .
* Description : Toggles DTOG_RX / DTOG_TX bit in the endpoint register.
* Input : bEpNum: Endpoint Number.
* Output : None.
* Return : None.
*******************************************************************************/
#define _ToggleDTOG_RX(bEpNum) \
(_SetENDPOINT(bEpNum, EP_CTRS_RX | EP_CTRS_TX | EP_DATTOG_RX | (_GetENDPOINT(bEpNum) & EPREG_MASK)))
#define _ToggleDTOG_TX(bEpNum) \
(_SetENDPOINT(bEpNum, EP_CTRS_RX | EP_CTRS_TX | EP_DATTOG_TX | (_GetENDPOINT(bEpNum) & EPREG_MASK)))
/*******************************************************************************
* Macro Name : USB_ClrDattogRx / USB_ClrDattogTx.
* Description : Clears DTOG_RX / DTOG_TX bit in the endpoint register.
* Input : bEpNum: Endpoint Number.
* Output : None.
* Return : None.
*******************************************************************************/
#define _ClearDTOG_RX(bEpNum) \
if ((_GetENDPOINT(bEpNum) & EP_DATTOG_RX) != 0) \
_ToggleDTOG_RX(bEpNum)
#define _ClearDTOG_TX(bEpNum) \
if ((_GetENDPOINT(bEpNum) & EP_DATTOG_TX) != 0) \
_ToggleDTOG_TX(bEpNum)
/*******************************************************************************
* Macro Name : USB_SetEpAddress.
* Description : Sets address in an endpoint register.
* Input : bEpNum: Endpoint Number.
* bAddr: Address.
* Output : None.
* Return : None.
*******************************************************************************/
#define _SetEPAddress(bEpNum, bAddr) \
_SetENDPOINT(bEpNum, EP_CTRS_RX | EP_CTRS_TX | (_GetENDPOINT(bEpNum) & EPREG_MASK) | bAddr)
/*******************************************************************************
* Macro Name : USB_GetEpAddress.
* Description : Gets address in an endpoint register.
* Input : bEpNum: Endpoint Number.
* Output : None.
* Return : None.
*******************************************************************************/
#define _GetEPAddress(bEpNum) ((uint8_t)(_GetENDPOINT(bEpNum) & EPADDR_FIELD))
#define _pEPTxAddr(bEpNum) ((uint32_t*)((_GetBTABLE() + bEpNum * 8) * 2 + PMAAddr))
#define _pEPTxCount(bEpNum) ((uint32_t*)((_GetBTABLE() + bEpNum * 8 + 2) * 2 + PMAAddr))
#define _pEPRxAddr(bEpNum) ((uint32_t*)((_GetBTABLE() + bEpNum * 8 + 4) * 2 + PMAAddr))
#define _pEPRxCount(bEpNum) ((uint32_t*)((_GetBTABLE() + bEpNum * 8 + 6) * 2 + PMAAddr))
/*******************************************************************************
* Macro Name : USB_SetEpTxAddr / USB_SetEpRxAddr.
* Description : sets address of the tx/rx buffer.
* Input : bEpNum: Endpoint Number.
* wAddr: address to be set (must be word aligned).
* Output : None.
* Return : None.
*******************************************************************************/
#define _SetEPTxAddr(bEpNum, wAddr) (*_pEPTxAddr(bEpNum) = ((wAddr >> 1) << 1))
#define _SetEPRxAddr(bEpNum, wAddr) (*_pEPRxAddr(bEpNum) = ((wAddr >> 1) << 1))
/*******************************************************************************
* Macro Name : USB_GetEpTxAddr / USB_GetEpRxAddr.
* Description : Gets address of the tx/rx buffer.
* Input : bEpNum: Endpoint Number.
* Output : None.
* Return : address of the buffer.
*******************************************************************************/
#define _GetEPTxAddr(bEpNum) ((uint16_t)*_pEPTxAddr(bEpNum))
#define _GetEPRxAddr(bEpNum) ((uint16_t)*_pEPRxAddr(bEpNum))
/*******************************************************************************
* Macro Name : USB_SetEpCntRxReg.
* Description : Sets counter of rx buffer with no. of blocks.
* Input : pdwReg: pointer to counter.
* wCount: Counter.
* Output : None.
* Return : None.
*******************************************************************************/
#define _BlocksOf32(dwReg, wCount, wNBlocks) \
{ \
wNBlocks = wCount >> 5; \
if ((wCount & 0x1f) == 0) \
wNBlocks--; \
*pdwReg = (uint32_t)((wNBlocks << 10) | 0x8000); \
} /* _BlocksOf32 */
#define _BlocksOf2(dwReg, wCount, wNBlocks) \
{ \
wNBlocks = wCount >> 1; \
if ((wCount & 0x1) != 0) \
wNBlocks++; \
*pdwReg = (uint32_t)(wNBlocks << 10); \
} /* _BlocksOf2 */
#define _SetEPCountRxReg(dwReg, wCount) \
{ \
uint16_t wNBlocks; \
if (wCount > 62) \
{ \
_BlocksOf32(dwReg, wCount, wNBlocks); \
} \
else \
{ \
_BlocksOf2(dwReg, wCount, wNBlocks); \
} \
} /* _SetEPCountRxReg */
#define _SetEPRxDblBuf0Count(bEpNum, wCount) \
{ \
uint32_t* pdwReg = _pEPTxCount(bEpNum); \
_SetEPCountRxReg(pdwReg, wCount); \
}
/*******************************************************************************
* Macro Name : USB_SetEpTxCnt / USB_SetEpRxCnt.
* Description : sets counter for the tx/rx buffer.
* Input : bEpNum: endpoint number.
* wCount: Counter value.
* Output : None.
* Return : None.
*******************************************************************************/
#define _SetEPTxCount(bEpNum, wCount) (*_pEPTxCount(bEpNum) = wCount)
#define _SetEPRxCount(bEpNum, wCount) \
{ \
uint32_t* pdwReg = _pEPRxCount(bEpNum); \
_SetEPCountRxReg(pdwReg, wCount); \
}
/*******************************************************************************
* Macro Name : USB_GetEpTxCnt / USB_GetEpRxCnt.
* Description : gets counter of the tx buffer.
* Input : bEpNum: endpoint number.
* Output : None.
* Return : Counter value.
*******************************************************************************/
#define _GetEPTxCount(bEpNum) ((uint16_t)(*_pEPTxCount(bEpNum)) & 0x3ff)
#define _GetEPRxCount(bEpNum) ((uint16_t)(*_pEPRxCount(bEpNum)) & 0x3ff)
/*******************************************************************************
* Macro Name : USB_SetEpDblBuf0Addr / USB_SetEpDblBuf1Addr.
* Description : Sets buffer 0/1 address in a double buffer endpoint.
* Input : bEpNum: endpoint number.
* : wBuf0Addr: buffer 0 address.
* Output : None.
* Return : None.
*******************************************************************************/
#define _SetEPDblBuf0Addr(bEpNum, wBuf0Addr) \
{ \
_SetEPTxAddr(bEpNum, wBuf0Addr); \
}
#define _SetEPDblBuf1Addr(bEpNum, wBuf1Addr) \
{ \
_SetEPRxAddr(bEpNum, wBuf1Addr); \
}
/*******************************************************************************
* Macro Name : USB_SetEpDblBuferAddr.
* Description : Sets addresses in a double buffer endpoint.
* Input : bEpNum: endpoint number.
* : wBuf0Addr: buffer 0 address.
* : wBuf1Addr = buffer 1 address.
* Output : None.
* Return : None.
*******************************************************************************/
#define _SetEPDblBuffAddr(bEpNum, wBuf0Addr, wBuf1Addr) \
{ \
_SetEPDblBuf0Addr(bEpNum, wBuf0Addr); \
_SetEPDblBuf1Addr(bEpNum, wBuf1Addr); \
} /* _SetEPDblBuffAddr */
/*******************************************************************************
* Macro Name : USB_GetEpDblBuf0Addr / USB_GetEpDblBuf1Addr.
* Description : Gets buffer 0/1 address of a double buffer endpoint.
* Input : bEpNum: endpoint number.
* Output : None.
* Return : None.
*******************************************************************************/
#define _GetEPDblBuf0Addr(bEpNum) (_GetEPTxAddr(bEpNum))
#define _GetEPDblBuf1Addr(bEpNum) (_GetEPRxAddr(bEpNum))
/*******************************************************************************
* Macro Name : USB_SetEpDblBuferCnt / USB_SetEpDblBuf0Cnt / USB_SetEpDblBuf1Cnt.
* Description : Gets buffer 0/1 address of a double buffer endpoint.
* Input : bEpNum: endpoint number.
* : bDir: endpoint dir EP_DBUF_OUT = OUT
* EP_DBUF_IN = IN
* : wCount: Counter value
* Output : None.
* Return : None.
*******************************************************************************/
#define _SetEPDblBuf0Count(bEpNum, bDir, wCount) \
{ \
if (bDir == EP_DBUF_OUT) \
/* OUT endpoint */ \
{ \
_SetEPRxDblBuf0Count(bEpNum, wCount); \
} \
else if (bDir == EP_DBUF_IN) \
/* IN endpoint */ \
*_pEPTxCount(bEpNum) = (uint32_t)wCount; \
} /* USB_SetEpDblBuf0Cnt*/
#define _SetEPDblBuf1Count(bEpNum, bDir, wCount) \
{ \
if (bDir == EP_DBUF_OUT) \
/* OUT endpoint */ \
{ \
_SetEPRxCount(bEpNum, wCount); \
} \
else if (bDir == EP_DBUF_IN) \
/* IN endpoint */ \
*_pEPRxCount(bEpNum) = (uint32_t)wCount; \
} /* USB_SetEpDblBuf1Cnt */
#define _SetEPDblBuffCount(bEpNum, bDir, wCount) \
{ \
_SetEPDblBuf0Count(bEpNum, bDir, wCount); \
_SetEPDblBuf1Count(bEpNum, bDir, wCount); \
} /* _SetEPDblBuffCount */
/*******************************************************************************
* Macro Name : USB_GetEpDblBuf0Cnt / USB_GetEpDblBuf1Cnt.
* Description : Gets buffer 0/1 rx/tx counter for double buffering.
* Input : bEpNum: endpoint number.
* Output : None.
* Return : None.
*******************************************************************************/
#define _GetEPDblBuf0Count(bEpNum) (_GetEPTxCount(bEpNum))
#define _GetEPDblBuf1Count(bEpNum) (_GetEPRxCount(bEpNum))
extern __IO uint16_t wIstr; /* USB_STS register last read value */
void USB_SetCtrl(uint16_t /*wRegValue*/);
void USB_SetSts(uint16_t /*wRegValue*/);
void USB_SetAddr(uint16_t /*wRegValue*/);
void USB_SetBuftab(uint16_t /*wRegValue*/);
void USB_SetBuftab(uint16_t /*wRegValue*/);
uint16_t USB_GetCtrl(void);
uint16_t USB_GetSts(void);
uint16_t USB_GetFn(void);
uint16_t USB_GetAddr(void);
uint16_t USB_GetBTABLE(void);
void USB_SetEndPoint(uint8_t /*bEpNum*/, uint16_t /*wRegValue*/);
uint16_t USB_GetEndPoint(uint8_t /*bEpNum*/);
void USB_SetEpType(uint8_t /*bEpNum*/, uint16_t /*wType*/);
uint16_t USB_GetEpType(uint8_t /*bEpNum*/);
void SetEPTxStatus(uint8_t /*bEpNum*/, uint16_t /*wState*/);
void SetEPRxStatus(uint8_t /*bEpNum*/, uint16_t /*wState*/);
void USB_SetDouBleBuferEpStall(uint8_t /*bEpNum*/, uint8_t bDir);
uint16_t USB_GetEpTxSts(uint8_t /*bEpNum*/);
uint16_t USB_GetEpRxSts(uint8_t /*bEpNum*/);
void USB_SetEpTxValid(uint8_t /*bEpNum*/);
void USB_SetEpRxValid(uint8_t /*bEpNum*/);
uint16_t USB_GetTxStallSts(uint8_t /*bEpNum*/);
uint16_t USB_GetRxStallSts(uint8_t /*bEpNum*/);
void USB_SetEpKind(uint8_t /*bEpNum*/);
void USB_ClrEpKind(uint8_t /*bEpNum*/);
void USB_SetStsOut(uint8_t /*bEpNum*/);
void USB_ClrStsOut(uint8_t /*bEpNum*/);
void USB_SetEpDoubleBufer(uint8_t /*bEpNum*/);
void USB_ClrEpDoubleBufer(uint8_t /*bEpNum*/);
void USB_ClrEpCtrsRx(uint8_t /*bEpNum*/);
void USB_ClrEpCtrsTx(uint8_t /*bEpNum*/);
void USB_DattogRx(uint8_t /*bEpNum*/);
void USB_DattogTx(uint8_t /*bEpNum*/);
void USB_ClrDattogRx(uint8_t /*bEpNum*/);
void USB_ClrDattogTx(uint8_t /*bEpNum*/);
void USB_SetEpAddress(uint8_t /*bEpNum*/, uint8_t /*bAddr*/);
uint8_t USB_GetEpAddress(uint8_t /*bEpNum*/);
void USB_SetEpTxAddr(uint8_t /*bEpNum*/, uint16_t /*wAddr*/);
void USB_SetEpRxAddr(uint8_t /*bEpNum*/, uint16_t /*wAddr*/);
uint16_t USB_GetEpTxAddr(uint8_t /*bEpNum*/);
uint16_t USB_GetEpRxAddr(uint8_t /*bEpNum*/);
void USB_SetEpCntRxReg(uint32_t* /*pdwReg*/, uint16_t /*wCount*/);
void USB_SetEpTxCnt(uint8_t /*bEpNum*/, uint16_t /*wCount*/);
void USB_SetEpRxCnt(uint8_t /*bEpNum*/, uint16_t /*wCount*/);
uint16_t USB_GetEpTxCnt(uint8_t /*bEpNum*/);
uint16_t USB_GetEpRxCnt(uint8_t /*bEpNum*/);
void USB_SetEpDblBuf0Addr(uint8_t /*bEpNum*/, uint16_t /*wBuf0Addr*/);
void USB_SetEpDblBuf1Addr(uint8_t /*bEpNum*/, uint16_t /*wBuf1Addr*/);
void USB_SetEpDblBuferAddr(uint8_t /*bEpNum*/, uint16_t /*wBuf0Addr*/, uint16_t /*wBuf1Addr*/);
uint16_t USB_GetEpDblBuf0Addr(uint8_t /*bEpNum*/);
uint16_t USB_GetEpDblBuf1Addr(uint8_t /*bEpNum*/);
void USB_SetEpDblBuferCnt(uint8_t /*bEpNum*/, uint8_t /*bDir*/, uint16_t /*wCount*/);
void USB_SetEpDblBuf0Cnt(uint8_t /*bEpNum*/, uint8_t /*bDir*/, uint16_t /*wCount*/);
void USB_SetEpDblBuf1Cnt(uint8_t /*bEpNum*/, uint8_t /*bDir*/, uint16_t /*wCount*/);
uint16_t USB_GetEpDblBuf0Cnt(uint8_t /*bEpNum*/);
uint16_t USB_GetEpDblBuf1Cnt(uint8_t /*bEpNum*/);
EP_DBUF_DIR GetEPDblBufDir(uint8_t /*bEpNum*/);
void USB_FreeUserBuf(uint8_t bEpNum /*bEpNum*/, uint8_t bDir);
uint16_t USB_ToWord(uint8_t, uint8_t);
uint16_t USB_ByteSwap(uint16_t);
/**
* @}
*/
#endif /* __USB_REGS_H__ */

View File

@ -0,0 +1,53 @@
/*****************************************************************************
* Copyright (c) 2019, Nations Technologies Inc.
*
* All rights reserved.
* ****************************************************************************
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
*
* Nations' name may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ****************************************************************************/
/**
* @file usb_sil.h
* @author Nations
* @version v1.0.0
*
* @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
*/
#ifndef __USB_SIL_H__
#define __USB_SIL_H__
#include "n32g45x.h"
/**
* @addtogroup N32G45X_USB_Driver
* @{
*/
uint32_t USB_SilInit(void);
uint32_t USB_SilWrite(uint8_t bEpAddr, uint8_t* pBufferPointer, uint32_t wBufferSize);
uint32_t USB_SilRead(uint8_t bEpAddr, uint8_t* pBufferPointer);
/**
* @}
*/
#endif /* __USB_SIL_H__ */

View File

@ -0,0 +1,54 @@
/*****************************************************************************
* Copyright (c) 2019, Nations Technologies Inc.
*
* All rights reserved.
* ****************************************************************************
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
*
* Nations' name may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ****************************************************************************/
/**
* @file usb_type.h
* @author Nations
* @version v1.0.0
*
* @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
*/
#ifndef __USB_TYPE_H__
#define __USB_TYPE_H__
#include "usb_conf.h"
#include <stdbool.h>
/**
* @addtogroup N32G45X_USB_Driver
* @{
*/
#ifndef NULL
#define NULL ((void*)0)
#endif
/**
* @}
*/
#endif /* __USB_TYPE_H__ */

View File

@ -0,0 +1,950 @@
/*****************************************************************************
* Copyright (c) 2019, Nations Technologies Inc.
*
* All rights reserved.
* ****************************************************************************
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
*
* Nations' name may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ****************************************************************************/
/**
* @file usb_core.c
* @author Nations
* @version v1.0.0
*
* @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
*/
#include "usb_lib.h"
#define ValBit(VAR, Place) (VAR & (1 << Place))
#define SetBit(VAR, Place) (VAR |= (1 << Place))
#define ClrBit(VAR, Place) (VAR &= ((1 << Place) ^ 255))
#define Send0LengthData() \
{ \
_SetEPTxCount(ENDP0, 0); \
vSetEPTxStatus(EP_TX_VALID); \
}
#define vSetEPRxStatus(st) (SaveRState = st)
#define vSetEPTxStatus(st) (SaveTState = st)
#define USB_StatusIn() Send0LengthData()
#define USB_StatusOut() vSetEPRxStatus(EP_RX_VALID)
#define StatusInfo0 StatusInfo.bw.bb1 /* Reverse bb0 & bb1 */
#define StatusInfo1 StatusInfo.bw.bb0
uint16_t_uint8_t StatusInfo;
bool Data_Mul_MaxPacketSize = false;
static void DataStageOut(void);
static void DataStageIn(void);
static void NoData_Setup0(void);
static void Data_Setup0(void);
/**
* @brief Return the current configuration variable address.
* Input : Length - How many bytes are needed.
* @return Return 1 , if the request is invalid when "Length" is 0.
* Return "Buffer" if the "Length" is not 0.
*/
uint8_t* Standard_GetConfiguration(uint16_t Length)
{
if (Length == 0)
{
pInformation->Ctrl_Info.Usb_wLength = sizeof(pInformation->CurrentConfiguration);
return 0;
}
pUser_Standard_Requests->User_GetConfiguration();
return (uint8_t*)&pInformation->CurrentConfiguration;
}
/**
* @brief This routine is called to set the configuration value
* Then each class should configure device itself.
* @return
* - Success, if the request is performed.
* - UnSupport, if the request is invalid.
*/
USB_Result Standard_SetConfiguration(void)
{
if ((pInformation->USBwValue0 <= Device_Table.TotalConfiguration) && (pInformation->USBwValue1 == 0)
&& (pInformation->USBwIndex == 0)) /*call Back usb spec 2.0*/
{
pInformation->CurrentConfiguration = pInformation->USBwValue0;
pUser_Standard_Requests->User_SetConfiguration();
return Success;
}
else
{
return UnSupport;
}
}
/**
* @brief Return the Alternate Setting of the current interface.
* Input : Length - How many bytes are needed.
* @return
* - NULL, if the request is invalid when "Length" is 0.
* - "Buffer" if the "Length" is not 0.
*/
uint8_t* Standard_GetInterface(uint16_t Length)
{
if (Length == 0)
{
pInformation->Ctrl_Info.Usb_wLength = sizeof(pInformation->CurrentAlternateSetting);
return 0;
}
pUser_Standard_Requests->User_GetInterface();
return (uint8_t*)&pInformation->CurrentAlternateSetting;
}
/**
* @brief This routine is called to set the interface.
* Then each class should configure the interface them self.
* @return
* - Success, if the request is performed.
* - UnSupport, if the request is invalid.
*/
USB_Result Standard_SetInterface(void)
{
USB_Result Re;
/*Test if the specified Interface and Alternate Setting are supported by
the application Firmware*/
Re = (*pProperty->Class_Get_Interface_Setting)(pInformation->USBwIndex0, pInformation->USBwValue0);
if (pInformation->CurrentConfiguration != 0)
{
if ((Re != Success) || (pInformation->USBwIndex1 != 0) || (pInformation->USBwValue1 != 0))
{
return UnSupport;
}
else if (Re == Success)
{
pUser_Standard_Requests->User_SetInterface();
pInformation->CurrentInterface = pInformation->USBwIndex0;
pInformation->CurrentAlternateSetting = pInformation->USBwValue0;
return Success;
}
}
return UnSupport;
}
/**
* @brief Copy the device request data to "StatusInfo buffer".
* Input : - Length - How many bytes are needed.
* @return Return 0, if the request is at end of data block,
* or is invalid when "Length" is 0.
*/
uint8_t* Standard_GetStatus(uint16_t Length)
{
if (Length == 0)
{
pInformation->Ctrl_Info.Usb_wLength = 2;
return 0;
}
/* Reset Status Information */
StatusInfo.w = 0;
if (Type_Recipient == (STANDARD_REQUEST | DEVICE_RECIPIENT))
{
/*Get Device Status */
uint8_t Feature = pInformation->CurrentFeature;
/* Remote Wakeup enabled */
if (ValBit(Feature, 5))
{
SetBit(StatusInfo0, 1);
}
else
{
ClrBit(StatusInfo0, 1);
}
/* Bus-powered */
if (ValBit(Feature, 6))
{
SetBit(StatusInfo0, 0);
}
else /* Self-powered */
{
ClrBit(StatusInfo0, 0);
}
}
/*Interface Status*/
else if (Type_Recipient == (STANDARD_REQUEST | INTERFACE_RECIPIENT))
{
return (uint8_t*)&StatusInfo;
}
/*Get EndPoint Status*/
else if (Type_Recipient == (STANDARD_REQUEST | ENDPOINT_RECIPIENT))
{
uint8_t Related_Endpoint;
uint8_t wIndex0 = pInformation->USBwIndex0;
Related_Endpoint = (wIndex0 & 0x0f);
if (ValBit(wIndex0, 7))
{
/* IN endpoint */
if (_GetTxStallStatus(Related_Endpoint))
{
SetBit(StatusInfo0, 0); /* IN Endpoint stalled */
}
}
else
{
/* OUT endpoint */
if (_GetRxStallStatus(Related_Endpoint))
{
SetBit(StatusInfo0, 0); /* OUT Endpoint stalled */
}
}
}
else
{
return NULL;
}
pUser_Standard_Requests->User_GetStatus();
return (uint8_t*)&StatusInfo;
}
/**
* @brief Clear or disable a specific feature.
* @return - Return Success, if the request is performed.
* - Return UnSupport, if the request is invalid.
*/
USB_Result Standard_ClearFeature(void)
{
uint32_t Type_Rec = Type_Recipient;
uint32_t Status;
if (Type_Rec == (STANDARD_REQUEST | DEVICE_RECIPIENT))
{ /*Device Clear Feature*/
ClrBit(pInformation->CurrentFeature, 5);
return Success;
}
else if (Type_Rec == (STANDARD_REQUEST | ENDPOINT_RECIPIENT))
{ /*EndPoint Clear Feature*/
USB_Device* pDev;
uint32_t Related_Endpoint;
uint32_t wIndex0;
uint32_t rEP;
if ((pInformation->USBwValue != ENDPOINT_STALL) || (pInformation->USBwIndex1 != 0))
{
return UnSupport;
}
pDev = &Device_Table;
wIndex0 = pInformation->USBwIndex0;
rEP = wIndex0 & ~0x80;
Related_Endpoint = ENDP0 + rEP;
if (ValBit(pInformation->USBwIndex0, 7))
{
/*Get Status of endpoint & stall the request if the related_ENdpoint
is Disabled*/
Status = _GetEPTxStatus(Related_Endpoint);
}
else
{
Status = _GetEPRxStatus(Related_Endpoint);
}
if ((rEP >= pDev->TotalEndpoint) || (Status == 0) || (pInformation->CurrentConfiguration == 0))
{
return UnSupport;
}
if (wIndex0 & 0x80)
{
/* IN endpoint */
if (_GetTxStallStatus(Related_Endpoint))
{
USB_ClrDattogTx(Related_Endpoint);
SetEPTxStatus(Related_Endpoint, EP_TX_VALID);
}
}
else
{
/* OUT endpoint */
if (_GetRxStallStatus(Related_Endpoint))
{
if (Related_Endpoint == ENDP0)
{
/* After clear the STALL, enable the default endpoint receiver */
USB_SetEpRxCnt(Related_Endpoint, Device_Property.MaxPacketSize);
_SetEPRxStatus(Related_Endpoint, EP_RX_VALID);
}
else
{
USB_ClrDattogRx(Related_Endpoint);
_SetEPRxStatus(Related_Endpoint, EP_RX_VALID);
}
}
}
pUser_Standard_Requests->User_ClearFeature();
return Success;
}
return UnSupport;
}
/**
* @brief Set or enable a specific feature of EndPoint
* @return - Return Success, if the request is performed.
* - Return UnSupport, if the request is invalid.
*/
USB_Result Standard_SetEndPointFeature(void)
{
uint32_t wIndex0;
uint32_t Related_Endpoint;
uint32_t rEP;
uint32_t Status;
wIndex0 = pInformation->USBwIndex0;
rEP = wIndex0 & ~0x80;
Related_Endpoint = ENDP0 + rEP;
if (ValBit(pInformation->USBwIndex0, 7))
{
/* get Status of endpoint & stall the request if the related_ENdpoint
is Disabled*/
Status = _GetEPTxStatus(Related_Endpoint);
}
else
{
Status = _GetEPRxStatus(Related_Endpoint);
}
if (Related_Endpoint >= Device_Table.TotalEndpoint || pInformation->USBwValue != 0 || Status == 0
|| pInformation->CurrentConfiguration == 0)
{
return UnSupport;
}
else
{
if (wIndex0 & 0x80)
{
/* IN endpoint */
_SetEPTxStatus(Related_Endpoint, EP_TX_STALL);
}
else
{
/* OUT endpoint */
_SetEPRxStatus(Related_Endpoint, EP_RX_STALL);
}
}
pUser_Standard_Requests->User_SetEndPointFeature();
return Success;
}
/**
* @brief Set or enable a specific feature of Device.
* @return - Return Success, if the request is performed.
* - Return UnSupport, if the request is invalid.
*/
USB_Result Standard_SetDeviceFeature(void)
{
SetBit(pInformation->CurrentFeature, 5);
pUser_Standard_Requests->User_SetDeviceFeature();
return Success;
}
/**
* @brief Standard_GetDescriptorData is used for descriptors transfer.
* : This routine is used for the descriptors resident in Flash
* or RAM
* pDesc can be in either Flash or RAM
* The purpose of this routine is to have a versatile way to
* response descriptors request. It allows user to generate
* certain descriptors with software or read descriptors from
* external storage part by part.
* Input : - Length - Length of the data in this transfer.
* - pDesc - A pointer points to descriptor struct.
* The structure gives the initial address of the descriptor and
* its original size.
* @return Address of a part of the descriptor pointed by the Usb_
* wOffset The buffer pointed by this address contains at least
* Length bytes.
*/
uint8_t* Standard_GetDescriptorData(uint16_t Length, USB_OneDescriptor* pDesc)
{
uint32_t wOffset;
wOffset = pInformation->Ctrl_Info.Usb_wOffset;
if (Length == 0)
{
pInformation->Ctrl_Info.Usb_wLength = pDesc->Descriptor_Size - wOffset;
return 0;
}
return pDesc->Descriptor + wOffset;
}
/**
* @brief Data stage of a Control Write Transfer.
*/
void DataStageOut(void)
{
USB_EndpointMess* pEPinfo = &pInformation->Ctrl_Info;
uint32_t save_rLength;
save_rLength = pEPinfo->Usb_rLength;
if (pEPinfo->CopyData && save_rLength)
{
uint8_t* Buffer;
uint32_t Length;
Length = pEPinfo->PacketSize;
if (Length > save_rLength)
{
Length = save_rLength;
}
Buffer = (*pEPinfo->CopyData)(Length);
pEPinfo->Usb_rLength -= Length;
pEPinfo->Usb_rOffset += Length;
USB_CopyPMAToUserBuf(Buffer, USB_GetEpRxAddr(ENDP0), Length);
}
if (pEPinfo->Usb_rLength != 0)
{
vSetEPRxStatus(EP_RX_VALID); /* re-enable for next data reception */
USB_SetEpTxCnt(ENDP0, 0);
vSetEPTxStatus(EP_TX_VALID); /* Expect the host to abort the data OUT stage */
}
/* Set the next State*/
if (pEPinfo->Usb_rLength >= pEPinfo->PacketSize)
{
pInformation->CtrlState = OutData;
}
else
{
if (pEPinfo->Usb_rLength > 0)
{
pInformation->CtrlState = LastOutData;
}
else if (pEPinfo->Usb_rLength == 0)
{
pInformation->CtrlState = WaitStatusIn;
USB_StatusIn();
}
}
}
/**
* @brief Data stage of a Control Read Transfer.
*/
void DataStageIn(void)
{
USB_EndpointMess* pEPinfo = &pInformation->Ctrl_Info;
uint32_t save_wLength = pEPinfo->Usb_wLength;
uint32_t CtrlState = pInformation->CtrlState;
uint8_t* DataBuffer;
uint32_t Length;
if ((save_wLength == 0) && (CtrlState == LastInData))
{
if (Data_Mul_MaxPacketSize == true)
{
/* No more data to send and empty packet */
Send0LengthData();
CtrlState = LastInData;
Data_Mul_MaxPacketSize = false;
}
else
{
/* No more data to send so STALL the TX Status*/
CtrlState = WaitStatusOut;
vSetEPTxStatus(EP_TX_STALL);
}
goto Expect_Status_Out;
}
Length = pEPinfo->PacketSize;
CtrlState = (save_wLength <= Length) ? LastInData : InData;
if (Length > save_wLength)
{
Length = save_wLength;
}
DataBuffer = (*pEPinfo->CopyData)(Length);
USB_CopyUserToPMABuf(DataBuffer, USB_GetEpTxAddr(ENDP0), Length);
USB_SetEpTxCnt(ENDP0, Length);
pEPinfo->Usb_wLength -= Length;
pEPinfo->Usb_wOffset += Length;
vSetEPTxStatus(EP_TX_VALID);
USB_StatusOut(); /* Expect the host to abort the data IN stage */
Expect_Status_Out:
pInformation->CtrlState = CtrlState;
}
/**
* @brief Proceed the processing of setup request without data stage.
*/
void NoData_Setup0(void)
{
USB_Result Result = UnSupport;
uint32_t RequestNo = pInformation->bRequest;
uint32_t CtrlState;
if (Type_Recipient == (STANDARD_REQUEST | DEVICE_RECIPIENT))
{
/* Device Request*/
/* SET_CONFIGURATION*/
if (RequestNo == SET_CONFIGURATION)
{
Result = Standard_SetConfiguration();
}
/*SET ADDRESS*/
else if (RequestNo == SET_ADDRESS)
{
if ((pInformation->USBwValue0 > 127) || (pInformation->USBwValue1 != 0) || (pInformation->USBwIndex != 0)
|| (pInformation->CurrentConfiguration != 0))
/* Device Address should be 127 or less*/
{
CtrlState = Stalled;
goto exit_NoData_Setup0;
}
else
{
Result = Success;
}
}
/*SET FEATURE for Device*/
else if (RequestNo == SET_FEATURE)
{
if ((pInformation->USBwValue0 == DEVICE_REMOTE_WAKEUP) && (pInformation->USBwIndex == 0))
{
Result = Standard_SetDeviceFeature();
}
else
{
Result = UnSupport;
}
}
/*Clear FEATURE for Device */
else if (RequestNo == CLR_FEATURE)
{
if (pInformation->USBwValue0 == DEVICE_REMOTE_WAKEUP && pInformation->USBwIndex == 0
&& ValBit(pInformation->CurrentFeature, 5))
{
Result = Standard_ClearFeature();
}
else
{
Result = UnSupport;
}
}
}
/* Interface Request*/
else if (Type_Recipient == (STANDARD_REQUEST | INTERFACE_RECIPIENT))
{
/*SET INTERFACE*/
if (RequestNo == SET_INTERFACE)
{
Result = Standard_SetInterface();
}
}
/* EndPoint Request*/
else if (Type_Recipient == (STANDARD_REQUEST | ENDPOINT_RECIPIENT))
{
/*CLEAR FEATURE for EndPoint*/
if (RequestNo == CLR_FEATURE)
{
Result = Standard_ClearFeature();
}
/* SET FEATURE for EndPoint*/
else if (RequestNo == SET_FEATURE)
{
Result = Standard_SetEndPointFeature();
}
}
else
{
Result = UnSupport;
}
if (Result != Success)
{
Result = (*pProperty->Class_NoData_Setup)(RequestNo);
if (Result == Not_Ready)
{
CtrlState = Pause;
goto exit_NoData_Setup0;
}
}
if (Result != Success)
{
CtrlState = Stalled;
goto exit_NoData_Setup0;
}
CtrlState = WaitStatusIn; /* After no data stage SETUP */
USB_StatusIn();
exit_NoData_Setup0:
pInformation->CtrlState = CtrlState;
return;
}
/**
* @brief Proceed the processing of setup request with data stage.
*/
void Data_Setup0(void)
{
uint8_t* (*CopyRoutine)(uint16_t);
USB_Result Result;
uint32_t Request_No = pInformation->bRequest;
uint32_t Related_Endpoint, Reserved;
uint32_t wOffset, Status;
CopyRoutine = NULL;
wOffset = 0;
/*GET DESCRIPTOR*/
if (Request_No == GET_DESCRIPTOR)
{
if (Type_Recipient == (STANDARD_REQUEST | DEVICE_RECIPIENT))
{
uint8_t wValue1 = pInformation->USBwValue1;
if (wValue1 == DEVICE_DESCRIPTOR)
{
CopyRoutine = pProperty->GetDeviceDescriptor;
}
else if (wValue1 == CONFIG_DESCRIPTOR)
{
CopyRoutine = pProperty->GetConfigDescriptor;
}
else if (wValue1 == STRING_DESCRIPTOR)
{
CopyRoutine = pProperty->GetStringDescriptor;
} /* End of GET_DESCRIPTOR */
}
}
/*GET STATUS*/
else if ((Request_No == GET_STATUS) && (pInformation->USBwValue == 0) && (pInformation->USBwLength == 0x0002)
&& (pInformation->USBwIndex1 == 0))
{
/* GET STATUS for Device*/
if ((Type_Recipient == (STANDARD_REQUEST | DEVICE_RECIPIENT)) && (pInformation->USBwIndex == 0))
{
CopyRoutine = Standard_GetStatus;
}
/* GET STATUS for Interface*/
else if (Type_Recipient == (STANDARD_REQUEST | INTERFACE_RECIPIENT))
{
if (((*pProperty->Class_Get_Interface_Setting)(pInformation->USBwIndex0, 0) == Success)
&& (pInformation->CurrentConfiguration != 0))
{
CopyRoutine = Standard_GetStatus;
}
}
/* GET STATUS for EndPoint*/
else if (Type_Recipient == (STANDARD_REQUEST | ENDPOINT_RECIPIENT))
{
Related_Endpoint = (pInformation->USBwIndex0 & 0x0f);
Reserved = pInformation->USBwIndex0 & 0x70;
if (ValBit(pInformation->USBwIndex0, 7))
{
/*Get Status of endpoint & stall the request if the related_ENdpoint
is Disabled*/
Status = _GetEPTxStatus(Related_Endpoint);
}
else
{
Status = _GetEPRxStatus(Related_Endpoint);
}
if ((Related_Endpoint < Device_Table.TotalEndpoint) && (Reserved == 0) && (Status != 0))
{
CopyRoutine = Standard_GetStatus;
}
}
}
/*GET CONFIGURATION*/
else if (Request_No == GET_CONFIGURATION)
{
if (Type_Recipient == (STANDARD_REQUEST | DEVICE_RECIPIENT))
{
CopyRoutine = Standard_GetConfiguration;
}
}
/*GET INTERFACE*/
else if (Request_No == GET_INTERFACE)
{
if ((Type_Recipient == (STANDARD_REQUEST | INTERFACE_RECIPIENT)) && (pInformation->CurrentConfiguration != 0)
&& (pInformation->USBwValue == 0) && (pInformation->USBwIndex1 == 0) && (pInformation->USBwLength == 0x0001)
&& ((*pProperty->Class_Get_Interface_Setting)(pInformation->USBwIndex0, 0) == Success))
{
CopyRoutine = Standard_GetInterface;
}
}
if (CopyRoutine)
{
pInformation->Ctrl_Info.Usb_wOffset = wOffset;
pInformation->Ctrl_Info.CopyData = CopyRoutine;
/* sb in the original the cast to word was directly */
/* now the cast is made step by step */
(*CopyRoutine)(0);
Result = Success;
}
else
{
Result = (*pProperty->Class_Data_Setup)(pInformation->bRequest);
if (Result == Not_Ready)
{
pInformation->CtrlState = Pause;
return;
}
}
if (pInformation->Ctrl_Info.Usb_wLength == 0xFFFF)
{
/* Data is not ready, wait it */
pInformation->CtrlState = Pause;
return;
}
if ((Result == UnSupport) || (pInformation->Ctrl_Info.Usb_wLength == 0))
{
/* Unsupported request */
pInformation->CtrlState = Stalled;
return;
}
if (ValBit(pInformation->bmRequestType, 7))
{
/* Device ==> Host */
__IO uint32_t wLength = pInformation->USBwLength;
/* Restrict the data length to be the one host asks for */
if (pInformation->Ctrl_Info.Usb_wLength > wLength)
{
pInformation->Ctrl_Info.Usb_wLength = wLength;
}
else if (pInformation->Ctrl_Info.Usb_wLength < pInformation->USBwLength)
{
if (pInformation->Ctrl_Info.Usb_wLength < pProperty->MaxPacketSize)
{
Data_Mul_MaxPacketSize = false;
}
else if ((pInformation->Ctrl_Info.Usb_wLength % pProperty->MaxPacketSize) == 0)
{
Data_Mul_MaxPacketSize = true;
}
}
pInformation->Ctrl_Info.PacketSize = pProperty->MaxPacketSize;
DataStageIn();
}
else
{
pInformation->CtrlState = OutData;
vSetEPRxStatus(EP_RX_VALID); /* enable for next data reception */
}
return;
}
/**
* @brief Get the device request data and dispatch to individual process.
* @return USB_ProcessPost0.
*/
uint8_t USB_ProcessSetup0(void)
{
union
{
uint8_t* b;
uint16_t* w;
} pBuf;
uint16_t offset = 1;
pBuf.b = PMAAddr + (uint8_t*)(_GetEPRxAddr(ENDP0) * 2); /* *2 for 32 bits addr */
if (pInformation->CtrlState != Pause)
{
pInformation->bmRequestType = *pBuf.b++; /* bmRequestType */
pInformation->bRequest = *pBuf.b++; /* bRequest */
pBuf.w += offset; /* word not accessed because of 32 bits addressing */
pInformation->USBwValue = USB_ByteSwap(*pBuf.w++); /* wValue */
pBuf.w += offset; /* word not accessed because of 32 bits addressing */
pInformation->USBwIndex = USB_ByteSwap(*pBuf.w++); /* wIndex */
pBuf.w += offset; /* word not accessed because of 32 bits addressing */
pInformation->USBwLength = *pBuf.w; /* wLength */
}
pInformation->CtrlState = SettingUp;
if (pInformation->USBwLength == 0)
{
/* Setup with no data stage */
NoData_Setup0();
}
else
{
/* Setup with data stage */
Data_Setup0();
}
return USB_ProcessPost0();
}
/**
* @brief Process the IN token on all default endpoint.
* @return USB_ProcessPost0.
*/
uint8_t USB_ProcessIn0(void)
{
uint32_t CtrlState = pInformation->CtrlState;
if ((CtrlState == InData) || (CtrlState == LastInData))
{
DataStageIn();
/* CtrlState may be changed outside the function */
CtrlState = pInformation->CtrlState;
}
else if (CtrlState == WaitStatusIn)
{
if ((pInformation->bRequest == SET_ADDRESS) && (Type_Recipient == (STANDARD_REQUEST | DEVICE_RECIPIENT)))
{
USB_SetDeviceAddress(pInformation->USBwValue0);
pUser_Standard_Requests->User_SetDeviceAddress();
}
(*pProperty->Process_Status_IN)();
CtrlState = Stalled;
}
else
{
CtrlState = Stalled;
}
pInformation->CtrlState = CtrlState;
return USB_ProcessPost0();
}
/**
* @brief Process the OUT token on all default endpoint.
* @return USB_ProcessPost0.
*/
uint8_t USB_ProcessOut0(void)
{
uint32_t CtrlState = pInformation->CtrlState;
if ((CtrlState == InData) || (CtrlState == LastInData))
{
/* host aborts the transfer before finish */
CtrlState = Stalled;
}
else if ((CtrlState == OutData) || (CtrlState == LastOutData))
{
DataStageOut();
CtrlState = pInformation->CtrlState; /* may be changed outside the function */
}
else if (CtrlState == WaitStatusOut)
{
(*pProperty->Process_Status_OUT)();
CtrlState = Stalled;
}
/* Unexpect state, STALL the endpoint */
else
{
CtrlState = Stalled;
}
pInformation->CtrlState = CtrlState;
return USB_ProcessPost0();
}
/**
* @brief Stall the Endpoint 0 in case of error.
* @return
* - 0 if the control State is in Pause
* - 1 if not.
*/
uint8_t USB_ProcessPost0(void)
{
USB_SetEpRxCnt(ENDP0, Device_Property.MaxPacketSize);
if (pInformation->CtrlState == Stalled)
{
vSetEPRxStatus(EP_RX_STALL);
vSetEPTxStatus(EP_TX_STALL);
}
return (pInformation->CtrlState == Pause);
}
/**
* @brief Set the device and all the used Endpoints addresses.
* @param Val device address.
*/
void USB_SetDeviceAddress(uint8_t Val)
{
uint32_t i;
uint32_t nEP = Device_Table.TotalEndpoint;
/* set address in every used endpoint */
for (i = 0; i < nEP; i++)
{
_SetEPAddress((uint8_t)i, (uint8_t)i);
} /* for */
_SetDADDR(Val | ADDR_EFUC); /* set device address and enable function */
}
/**
* @brief No operation function.
*/
void USB_ProcessNop(void)
{
}

View File

@ -0,0 +1,69 @@
/*****************************************************************************
* Copyright (c) 2019, Nations Technologies Inc.
*
* All rights reserved.
* ****************************************************************************
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
*
* Nations' name may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ****************************************************************************/
/**
* @file usb_init.c
* @author Nations
* @version v1.0.0
*
* @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
*/
#include "usb_lib.h"
/* The number of current endpoint, it will be used to specify an endpoint */
uint8_t EPindex;
/* The number of current device, it is an index to the Device_Table */
/* uint8_t Device_no; */
/* Points to the USB_DeviceMess structure of current device */
/* The purpose of this register is to speed up the execution */
USB_DeviceMess* pInformation;
/* Points to the DEVICE_PROP structure of current device */
/* The purpose of this register is to speed up the execution */
DEVICE_PROP* pProperty;
/* Temporary save the state of Rx & Tx status. */
/* Whenever the Rx or Tx state is changed, its value is saved */
/* in this variable first and will be set to the EPRB or EPRA */
/* at the end of interrupt process */
uint16_t SaveState;
uint16_t wInterrupt_Mask;
USB_DeviceMess Device_Info;
USER_STANDARD_REQUESTS* pUser_Standard_Requests;
/**
* @brief USB system initialization
*/
void USB_Init(void)
{
pInformation = &Device_Info;
pInformation->CtrlState = 2;
pProperty = &Device_Property;
pUser_Standard_Requests = &User_Standard_Requests;
/* Initialize devices one by one */
pProperty->Init();
/*Pull up DP*/
_EnPortPullup();
}

View File

@ -0,0 +1,179 @@
/*****************************************************************************
* Copyright (c) 2019, Nations Technologies Inc.
*
* All rights reserved.
* ****************************************************************************
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
*
* Nations' name may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ****************************************************************************/
/**
* @file usb_int.c
* @author Nations
* @version v1.0.0
*
* @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
*/
#include "usb_lib.h"
__IO uint16_t SaveRState;
__IO uint16_t SaveTState;
extern void (*pEpInt_IN[7])(void); /* Handles IN interrupts */
extern void (*pEpInt_OUT[7])(void); /* Handles OUT interrupts */
/**
* @brief Low priority Endpoint Correct Transfer interrupt's service routine.
*/
void USB_CorrectTransferLp(void)
{
__IO uint16_t wEPVal = 0;
/* stay in loop while pending interrupts */
while (((wIstr = _GetISTR()) & STS_CTRS) != 0)
{
/* extract highest priority endpoint number */
EPindex = (uint8_t)(wIstr & STS_EP_ID);
if (EPindex == 0)
{
/* Decode and service control endpoint interrupt */
/* calling related service routine */
/* (USB_ProcessSetup0, USB_ProcessIn0, USB_ProcessOut0) */
/* save RX & TX status */
/* and set both to NAK */
SaveRState = _GetENDPOINT(ENDP0);
SaveTState = SaveRState & EPTX_STS;
SaveRState &= EPRX_STS;
_SetEPRxTxStatus(ENDP0, EP_RX_NAK, EP_TX_NAK);
/* DIR bit = origin of the interrupt */
if ((wIstr & STS_DIR) == 0)
{
/* DIR = 0 */
/* DIR = 0 => IN int */
/* DIR = 0 implies that (EP_CTRS_TX = 1) always */
_ClearEP_CTR_TX(ENDP0);
USB_ProcessIn0();
/* before terminate set Tx & Rx status */
_SetEPRxTxStatus(ENDP0, SaveRState, SaveTState);
return;
}
else
{
/* DIR = 1 */
/* DIR = 1 & CTR_RX => SETUP or OUT int */
/* DIR = 1 & (CTR_TX | CTR_RX) => 2 int pending */
wEPVal = _GetENDPOINT(ENDP0);
if ((wEPVal & EP_SETUP) != 0)
{
_ClearEP_CTR_RX(ENDP0); /* SETUP bit kept frozen while CTR_RX = 1 */
USB_ProcessSetup0();
/* before terminate set Tx & Rx status */
_SetEPRxTxStatus(ENDP0, SaveRState, SaveTState);
return;
}
else if ((wEPVal & EP_CTRS_RX) != 0)
{
_ClearEP_CTR_RX(ENDP0);
USB_ProcessOut0();
/* before terminate set Tx & Rx status */
_SetEPRxTxStatus(ENDP0, SaveRState, SaveTState);
return;
}
}
} /* if(EPindex == 0) */
else
{
/* Decode and service non control endpoints interrupt */
/* process related endpoint register */
wEPVal = _GetENDPOINT(EPindex);
if ((wEPVal & EP_CTRS_RX) != 0)
{
/* clear int flag */
_ClearEP_CTR_RX(EPindex);
/* call OUT service function */
(*pEpInt_OUT[EPindex - 1])();
} /* if((wEPVal & EP_CTRS_RX) */
if ((wEPVal & EP_CTRS_TX) != 0)
{
/* clear int flag */
_ClearEP_CTR_TX(EPindex);
/* call IN service function */
(*pEpInt_IN[EPindex - 1])();
} /* if((wEPVal & EP_CTRS_TX) != 0) */
} /* if(EPindex == 0) else */
} /* while(...) */
}
/**
* @brief High Priority Endpoint Correct Transfer interrupt's service routine.
*/
void USB_CorrectTransferHp(void)
{
uint32_t wEPVal = 0;
while (((wIstr = _GetISTR()) & STS_CTRS) != 0)
{
_SetISTR((uint16_t)CLR_CTRS); /* clear CTR flag */
/* extract highest priority endpoint number */
EPindex = (uint8_t)(wIstr & STS_EP_ID);
/* process related endpoint register */
wEPVal = _GetENDPOINT(EPindex);
if ((wEPVal & EP_CTRS_RX) != 0)
{
/* clear int flag */
_ClearEP_CTR_RX(EPindex);
/* call OUT service function */
(*pEpInt_OUT[EPindex - 1])();
} /* if((wEPVal & EP_CTRS_RX) */
else if ((wEPVal & EP_CTRS_TX) != 0)
{
/* clear int flag */
_ClearEP_CTR_TX(EPindex);
/* call IN service function */
(*pEpInt_IN[EPindex - 1])();
} /* if((wEPVal & EP_CTRS_TX) != 0) */
} /* while(...) */
}

View File

@ -0,0 +1,81 @@
/*****************************************************************************
* Copyright (c) 2019, Nations Technologies Inc.
*
* All rights reserved.
* ****************************************************************************
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
*
* Nations' name may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ****************************************************************************/
/**
* @file usb_mem.c
* @author Nations
* @version v1.0.0
*
* @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
*/
#include "usb_lib.h"
u8* EpOutDataPtrTmp;
u8* EpInDataPtrTmp;
/**
* @brief Copy a buffer from user memory area to packet memory area (PMA)
* @param pbUsrBuf pointer to user memory area.
* @param wPMABufAddr address into PMA.
* @param wNBytes no. of bytes to be copied.
*/
void USB_CopyUserToPMABuf(uint8_t* pbUsrBuf, uint16_t wPMABufAddr, uint16_t wNBytes)
{
uint32_t n = (wNBytes + 1) >> 1; /* n = (wNBytes + 1) / 2 */
uint32_t i, temp1, temp2;
uint16_t* pdwVal;
pdwVal = (uint16_t*)(wPMABufAddr * 2 + PMAAddr);
for (i = n; i != 0; i--)
{
temp1 = (uint16_t)*pbUsrBuf;
pbUsrBuf++;
temp2 = temp1 | (uint16_t)*pbUsrBuf << 8;
*pdwVal++ = temp2;
pdwVal++;
pbUsrBuf++;
EpInDataPtrTmp = pbUsrBuf;
}
}
/**
* @brief Copy a buffer from user memory area to packet memory area (PMA)
* @param pbUsrBuf pointer to user memory area.
* @param wPMABufAddr address into PMA.
* @param wNBytes no. of bytes to be copied.
*/
void USB_CopyPMAToUserBuf(uint8_t* pbUsrBuf, uint16_t wPMABufAddr, uint16_t wNBytes)
{
uint32_t n = (wNBytes + 1) >> 1; /* /2*/
uint32_t i;
uint32_t* pdwVal;
pdwVal = (uint32_t*)(wPMABufAddr * 2 + PMAAddr);
for (i = n; i != 0; i--)
{
*(uint16_t*)pbUsrBuf++ = *pdwVal++;
pbUsrBuf++;
EpOutDataPtrTmp = pbUsrBuf;
}
}

View File

@ -0,0 +1,598 @@
/*****************************************************************************
* Copyright (c) 2019, Nations Technologies Inc.
*
* All rights reserved.
* ****************************************************************************
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
*
* Nations' name may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ****************************************************************************/
/**
* @file usb_regs.c
* @author Nations
* @version v1.0.0
*
* @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
*/
#include "usb_lib.h"
/**
* @brief Set the CTRL register value.
* @param wRegValue new register value.
*/
void USB_SetCtrl(uint16_t wRegValue)
{
_SetCNTR(wRegValue);
}
/**
* @brief returns the CTRL register value.
* @return CTRL register Value.
*/
uint16_t USB_GetCtrl(void)
{
return (_GetCNTR());
}
/**
* @brief Set the STS register value.
* @param wRegValue new register value.
*/
void USB_SetSts(uint16_t wRegValue)
{
_SetISTR(wRegValue);
}
/**
* @brief Returns the STS register value.
* @return STS register Value
*/
uint16_t USB_GetSts(void)
{
return (_GetISTR());
}
/**
* @brief Returns the FN register value.
* @return FN register Value
*/
uint16_t USB_GetFn(void)
{
return (_GetFNR());
}
/**
* @brief Set the ADDR register value.
* @param wRegValue new register value.
*/
void USB_SetAddr(uint16_t wRegValue)
{
_SetDADDR(wRegValue);
}
/**
* @brief Returns the ADDR register value.
* @return ADDR register Value
*/
uint16_t USB_GetAddr(void)
{
return (_GetDADDR());
}
/**
* @brief Set the BUFTAB.
* @param wRegValue New register value.
*/
void USB_SetBuftab(uint16_t wRegValue)
{
_SetBTABLE(wRegValue);
}
/**
* @brief Returns the BUFTAB register value.
* @return BUFTAB address.
*/
uint16_t USB_GetBTABLE(void)
{
return (_GetBTABLE());
}
/**
* @brief Set the Endpoint register value.
* @param bEpNum Endpoint Number.
* @param wRegValue New register value.
*/
void USB_SetEndPoint(uint8_t bEpNum, uint16_t wRegValue)
{
_SetENDPOINT(bEpNum, wRegValue);
}
/**
* @brief Return the Endpoint register value.
* @param bEpNum Endpoint Number.
* @return Endpoint register value.
*/
uint16_t USB_GetEndPoint(uint8_t bEpNum)
{
return (_GetENDPOINT(bEpNum));
}
/**
* @brief sets the type in the endpoint register.
* @param bEpNum Endpoint Number.
* @param wType type definition.
*/
void USB_SetEpType(uint8_t bEpNum, uint16_t wType)
{
_SetEPType(bEpNum, wType);
}
/**
* @brief Returns the endpoint type.
* @param bEpNum Endpoint Number.
* @return Endpoint Type
*/
uint16_t USB_GetEpType(uint8_t bEpNum)
{
return (_GetEPType(bEpNum));
}
/**
* @brief Set the status of Tx endpoint.
* @param bEpNum Endpoint Number.
* @param wState new state.
*/
void SetEPTxStatus(uint8_t bEpNum, uint16_t wState)
{
_SetEPTxStatus(bEpNum, wState);
}
/**
* @brief Set the status of Rx endpoint.
* @param bEpNum Endpoint Number.
* @param wState new state.
*/
void SetEPRxStatus(uint8_t bEpNum, uint16_t wState)
{
_SetEPRxStatus(bEpNum, wState);
}
/**
* @brief sets the status for Double Buffer Endpoint to STALL
* @param bEpNum Endpoint Number.
* @param bDir Endpoint direction.
*/
void USB_SetDouBleBuferEpStall(uint8_t bEpNum, uint8_t bDir)
{
uint16_t Endpoint_DTOG_Status;
Endpoint_DTOG_Status = USB_GetEndPoint(bEpNum);
if (bDir == EP_DBUF_OUT)
{ /* OUT double buffered endpoint */
_SetENDPOINT(bEpNum, Endpoint_DTOG_Status & ~EPRX_DATTOG1);
}
else if (bDir == EP_DBUF_IN)
{ /* IN double buffered endpoint */
_SetENDPOINT(bEpNum, Endpoint_DTOG_Status & ~EPTX_DATTOG1);
}
}
/**
* @brief Returns the endpoint Tx status.
* @param bEpNum Endpoint Number.
* @return Endpoint TX Status
*/
uint16_t USB_GetEpTxSts(uint8_t bEpNum)
{
return (_GetEPTxStatus(bEpNum));
}
/**
* @brief Returns the endpoint Rx status.
* @param bEpNum Endpoint Number.
* @return Endpoint RX Status
*/
uint16_t USB_GetEpRxSts(uint8_t bEpNum)
{
return (_GetEPRxStatus(bEpNum));
}
/**
* @brief Valid the endpoint Tx Status.
* @param bEpNum Endpoint Number.
*/
void USB_SetEpTxValid(uint8_t bEpNum)
{
_SetEPTxStatus(bEpNum, EP_TX_VALID);
}
/**
* @brief Valid the endpoint Rx Status.
* @param bEpNum Endpoint Number.
*/
void USB_SetEpRxValid(uint8_t bEpNum)
{
_SetEPRxStatus(bEpNum, EP_RX_VALID);
}
/**
* @brief Clear the EP_KIND bit.
* @param bEpNum Endpoint Number.
*/
void USB_SetEpKind(uint8_t bEpNum)
{
_SetEP_KIND(bEpNum);
}
/**
* @brief set the EP_KIND bit.
* @param bEpNum Endpoint Number.
*/
void USB_ClrEpKind(uint8_t bEpNum)
{
_ClearEP_KIND(bEpNum);
}
/**
* @brief Clear the Status Out of the related Endpoint
* @param bEpNum Endpoint Number.
*/
void USB_ClrStsOut(uint8_t bEpNum)
{
_ClearEP_KIND(bEpNum);
}
/**
* @brief Set the Status Out of the related Endpoint
* @param bEpNum Endpoint Number.
*/
void USB_SetStsOut(uint8_t bEpNum)
{
_SetEP_KIND(bEpNum);
}
/**
* @brief Enable the double buffer feature for the endpoint.
* @param bEpNum Endpoint Number.
*/
void USB_SetEpDoubleBufer(uint8_t bEpNum)
{
_SetEP_KIND(bEpNum);
}
/**
* @brief Disable the double buffer feature for the endpoint.
* @param bEpNum Endpoint Number.
*/
void USB_ClrEpDoubleBufer(uint8_t bEpNum)
{
_ClearEP_KIND(bEpNum);
}
/**
* @brief Returns the Stall status of the Tx endpoint.
* @param bEpNum Endpoint Number.
* @return Tx Stall status.
*/
uint16_t USB_GetTxStallSts(uint8_t bEpNum)
{
return (_GetTxStallStatus(bEpNum));
}
/**
* @brief Returns the Stall status of the Rx endpoint.
* @param bEpNum Endpoint Number.
* @return Rx Stall status.
*/
uint16_t USB_GetRxStallSts(uint8_t bEpNum)
{
return (_GetRxStallStatus(bEpNum));
}
/**
* @brief Clear the CTR_RX bit.
* @param bEpNum Endpoint Number.
*/
void USB_ClrEpCtrsRx(uint8_t bEpNum)
{
_ClearEP_CTR_RX(bEpNum);
}
/**
* @brief Clear the CTR_TX bit.
* @param bEpNum Endpoint Number.
*/
void USB_ClrEpCtrsTx(uint8_t bEpNum)
{
_ClearEP_CTR_TX(bEpNum);
}
/**
* @brief Toggle the DTOG_RX bit.
* @param bEpNum Endpoint Number.
*/
void USB_DattogRx(uint8_t bEpNum)
{
_ToggleDTOG_RX(bEpNum);
}
/**
* @brief Toggle the DTOG_TX bit.
* @param bEpNum Endpoint Number.
*/
void USB_DattogTx(uint8_t bEpNum)
{
_ToggleDTOG_TX(bEpNum);
}
/**
* @brief Clear the DTOG_RX bit.
* @param bEpNum Endpoint Number.
*/
void USB_ClrDattogRx(uint8_t bEpNum)
{
_ClearDTOG_RX(bEpNum);
}
/**
* @brief Clear the DTOG_TX bit.
* @param bEpNum Endpoint Number.
*/
void USB_ClrDattogTx(uint8_t bEpNum)
{
_ClearDTOG_TX(bEpNum);
}
/**
* @brief Set the endpoint address.
* @param bEpNum Endpoint Number.
* @param bAddr New endpoint address.
*/
void USB_SetEpAddress(uint8_t bEpNum, uint8_t bAddr)
{
_SetEPAddress(bEpNum, bAddr);
}
/**
* @brief Get the endpoint address.
* @param bEpNum Endpoint Number.
* @return Endpoint address.
*/
uint8_t USB_GetEpAddress(uint8_t bEpNum)
{
return (_GetEPAddress(bEpNum));
}
/**
* @brief Set the endpoint Tx buffer address.
* @param bEpNum Endpoint Number.
* @param wAddr new address.
*/
void USB_SetEpTxAddr(uint8_t bEpNum, uint16_t wAddr)
{
_SetEPTxAddr(bEpNum, wAddr);
}
/**
* @brief Set the endpoint Rx buffer address.
* @param bEpNum Endpoint Number.
* @param wAddr new address.
*/
void USB_SetEpRxAddr(uint8_t bEpNum, uint16_t wAddr)
{
_SetEPRxAddr(bEpNum, wAddr);
}
/**
* @brief Returns the endpoint Tx buffer address.
* @param bEpNum Endpoint Number.
* @return Rx buffer address.
*/
uint16_t USB_GetEpTxAddr(uint8_t bEpNum)
{
return (_GetEPTxAddr(bEpNum));
}
/**
* @brief Returns the endpoint Rx buffer address.
* @param bEpNum Endpoint Number.
* @return Rx buffer address.
*/
uint16_t USB_GetEpRxAddr(uint8_t bEpNum)
{
return (_GetEPRxAddr(bEpNum));
}
/**
* @brief Set the Tx count.
* @param bEpNum Endpoint Number.
* @param wCount new count value.
*/
void USB_SetEpTxCnt(uint8_t bEpNum, uint16_t wCount)
{
_SetEPTxCount(bEpNum, wCount);
}
/**
* @brief Set the Count Rx Register value.
* @param pdwReg point to the register.
* @param wCount the new register value.
*/
void USB_SetEpCntRxReg(uint32_t* pdwReg, uint16_t wCount)
{
_SetEPCountRxReg(dwReg, wCount);
}
/**
* @brief Set the Rx count.
* @param bEpNum Endpoint Number.
* @param wCount the new count value.
*/
void USB_SetEpRxCnt(uint8_t bEpNum, uint16_t wCount)
{
_SetEPRxCount(bEpNum, wCount);
}
/**
* @brief Get the Tx count.
* @param bEpNum Endpoint Number.
* @return Tx count value.
*/
uint16_t USB_GetEpTxCnt(uint8_t bEpNum)
{
return (_GetEPTxCount(bEpNum));
}
/**
* @brief Get the Rx count.
* @param bEpNum Endpoint Number.
* @return Rx count value.
*/
uint16_t USB_GetEpRxCnt(uint8_t bEpNum)
{
return (_GetEPRxCount(bEpNum));
}
/**
* @brief Set the addresses of the buffer 0 and 1.
* @param bEpNum Endpoint Number.
* @param wBuf0Addr new address of buffer 0.
* @param wBuf1Addr new address of buffer 1.
*/
void USB_SetEpDblBuferAddr(uint8_t bEpNum, uint16_t wBuf0Addr, uint16_t wBuf1Addr)
{
_SetEPDblBuffAddr(bEpNum, wBuf0Addr, wBuf1Addr);
}
/**
* @brief Set the Buffer 1 address.
* @param bEpNum Endpoint Number
* @param wBuf0Addr new address.
*/
void USB_SetEpDblBuf0Addr(uint8_t bEpNum, uint16_t wBuf0Addr)
{
_SetEPDblBuf0Addr(bEpNum, wBuf0Addr);
}
/**
* @brief Set the Buffer 1 address.
* @param bEpNum Endpoint Number
* @param wBuf1Addr new address.
*/
void USB_SetEpDblBuf1Addr(uint8_t bEpNum, uint16_t wBuf1Addr)
{
_SetEPDblBuf1Addr(bEpNum, wBuf1Addr);
}
/**
* @brief Returns the address of the Buffer 0.
* @param bEpNum Endpoint Number.
*/
uint16_t USB_GetEpDblBuf0Addr(uint8_t bEpNum)
{
return (_GetEPDblBuf0Addr(bEpNum));
}
/**
* @brief Returns the address of the Buffer 1.
* @param bEpNum Endpoint Number.
* @return Address of the Buffer 1.
*/
uint16_t USB_GetEpDblBuf1Addr(uint8_t bEpNum)
{
return (_GetEPDblBuf1Addr(bEpNum));
}
/**
* @brief Set the number of bytes for a double Buffer endpoint.
* @param bEpNum
* @param bDir
* @param wCount
*/
void USB_SetEpDblBuferCnt(uint8_t bEpNum, uint8_t bDir, uint16_t wCount)
{
_SetEPDblBuffCount(bEpNum, bDir, wCount);
}
/**
* @brief Set the number of bytes in the buffer 0 of a double Buffer endpoint.
* @param bEpNum
* @param bDir
* @param wCount
*/
void USB_SetEpDblBuf0Cnt(uint8_t bEpNum, uint8_t bDir, uint16_t wCount)
{
_SetEPDblBuf0Count(bEpNum, bDir, wCount);
}
/**
* @brief Set the number of bytes in the buffer 0 of a double Buffer endpoint.
* @param bEpNum
* @param bDir
* @param wCount
*/
void USB_SetEpDblBuf1Cnt(uint8_t bEpNum, uint8_t bDir, uint16_t wCount)
{
_SetEPDblBuf1Count(bEpNum, bDir, wCount);
}
/**
* @brief Returns the number of byte received in the buffer 0 of a double Buffer endpoint.
* @param bEpNum Endpoint Number.
* @return Endpoint Buffer 0 count
*/
uint16_t USB_GetEpDblBuf0Cnt(uint8_t bEpNum)
{
return (_GetEPDblBuf0Count(bEpNum));
}
/**
* @brief Returns the number of data received in the buffer 1 of a double Buffer endpoint.
* @param bEpNum Endpoint Number.
* @return Endpoint Buffer 1 count.
*/
uint16_t USB_GetEpDblBuf1Cnt(uint8_t bEpNum)
{
return (_GetEPDblBuf1Count(bEpNum));
}
/**
* @brief gets direction of the double buffered endpoint
* @param bEpNum Endpoint Number.
* @return EP_DBUF_OUT, EP_DBUF_IN, EP_DBUF_ERR if the endpoint counter not yet programmed.
*/
EP_DBUF_DIR GetEPDblBufDir(uint8_t bEpNum)
{
if ((uint16_t)(*_pEPRxCount(bEpNum) & 0xFC00) != 0)
return (EP_DBUF_OUT);
else if (((uint16_t)(*_pEPTxCount(bEpNum)) & 0x03FF) != 0)
return (EP_DBUF_IN);
else
return (EP_DBUF_ERR);
}
/**
* @brief free buffer used from the application realizing it to the line toggles
* bit SW_BUF in the double buffered endpoint register
* @param bEpNum
* @param bDir
*/
void USB_FreeUserBuf(uint8_t bEpNum, uint8_t bDir)
{
if (bDir == EP_DBUF_OUT)
{ /* OUT double buffered endpoint */
_ToggleDTOG_TX(bEpNum);
}
else if (bDir == EP_DBUF_IN)
{ /* IN double buffered endpoint */
_ToggleDTOG_RX(bEpNum);
}
}
/**
* @brief merge two byte in a word.
* @param bh byte high
* @param bl bytes low.
* @return resulted word.
*/
uint16_t USB_ToWord(uint8_t bh, uint8_t bl)
{
uint16_t wRet;
wRet = (uint16_t)bl | ((uint16_t)bh << 8);
return (wRet);
}
/**
* @brief Swap two byte in a word.
* @param wSwW word to Swap.
* @return resulted word.
*/
uint16_t USB_ByteSwap(uint16_t wSwW)
{
uint8_t bTemp;
uint16_t wRet;
bTemp = (uint8_t)(wSwW & 0xff);
wRet = (wSwW >> 8) | ((uint16_t)bTemp << 8);
return (wRet);
}

View File

@ -0,0 +1,83 @@
/*****************************************************************************
* Copyright (c) 2019, Nations Technologies Inc.
*
* All rights reserved.
* ****************************************************************************
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
*
* Nations' name may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ****************************************************************************/
/**
* @file usb_sil.c
* @author Nations
* @version v1.0.0
*
* @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
*/
#include "usb_lib.h"
/**
* @brief Initialize the USB Device IP and the Endpoint 0.
* @return Status.
*/
uint32_t USB_SilInit(void)
{
/* USB interrupts initialization */
/* clear pending interrupts */
_SetISTR(0);
wInterrupt_Mask = IMR_MSK;
/* set interrupts mask */
_SetCNTR(wInterrupt_Mask);
return 0;
}
/**
* @brief Write a buffer of data to a selected endpoint.
* @param bEpAddr The address of the non control endpoint.
* @param pBufferPointer The pointer to the buffer of data to be written to the endpoint.
* @param wBufferSize Number of data to be written (in bytes).
* @return Status.
*/
uint32_t USB_SilWrite(uint8_t bEpAddr, uint8_t* pBufferPointer, uint32_t wBufferSize)
{
/* Use the memory interface function to write to the selected endpoint */
USB_CopyUserToPMABuf(pBufferPointer, USB_GetEpTxAddr(bEpAddr & 0x7F), wBufferSize);
/* Update the data length in the control register */
USB_SetEpTxCnt((bEpAddr & 0x7F), wBufferSize);
return 0;
}
/**
* @brief Write a buffer of data to a selected endpoint.
* @param bEpAddr The address of the non control endpoint.
* @param pBufferPointer The pointer to which will be saved the received data buffer.
* @return Number of received data (in Bytes).
*/
uint32_t USB_SilRead(uint8_t bEpAddr, uint8_t* pBufferPointer)
{
uint32_t DataLength = 0;
/* Get the number of received data on the selected Endpoint */
DataLength = USB_GetEpRxCnt(bEpAddr & 0x7F);
/* Use the memory interface function to write to the selected endpoint */
USB_CopyPMAToUserBuf(pBufferPointer, USB_GetEpRxAddr(bEpAddr & 0x7F), DataLength);
/* Return the number of received data */
return DataLength;
}

403
src/hw_config.c Normal file
View File

@ -0,0 +1,403 @@
/*****************************************************************************
* Copyright (c) 2019, Nations Technologies Inc.
*
* All rights reserved.
* ****************************************************************************
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
*
* Nations' name may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ****************************************************************************/
/**
* @file hw_config.c
* @author Nations
* @version v1.0.1
*
* @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
*/
/* Includes ------------------------------------------------------------------*/
#include "usb_lib.h"
#include "usb_prop.h"
#include "usb_desc.h"
#include "hw_config.h"
#include "usb_pwr.h"
#include "string.h"
#include "cmsis_os2.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
ErrorStatus HSEStartUpStatus;
USART_InitType USART_InitStructure;
EXTI_InitType EXTI_InitStructure;
uint8_t USART_Rx_Buffer[USART_RX_DATA_SIZE];
uint32_t USART_Rx_ptr_in = 0;
uint32_t USART_Rx_ptr_out = 0;
uint32_t USART_Rx_length = 0;
uint8_t USB_Tx_State = 0;
/* Extern variables ----------------------------------------------------------*/
extern LINE_CODING linecoding;
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/*******************************************************************************
* Function Name : Set_System
* Description : Configures Main system clocks & power
* Input : None.
* Return : None.
*******************************************************************************/
void Set_System(void) {
EXTI_InitType EXTI_InitStructure;
/* Configure the EXTI line 18 connected internally to the USB IP */
EXTI_ClrITPendBit(EXTI_LINE18);
EXTI_InitStructure.EXTI_Line = EXTI_LINE18;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_InitPeripheral(&EXTI_InitStructure);
}
/*******************************************************************************
* Function Name : Set_USBClock
* Description : Configures USB Clock input (48MHz)
* Input : None.
* Return : None.
*******************************************************************************/
void Set_USBClock(void) {
/* Select USBCLK source */
RCC_ConfigUsbClk(RCC_USBCLK_SRC_PLLCLK_DIV3);
/* Enable the USB clock */
RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_USB, ENABLE);
}
/*******************************************************************************
* Function Name : Enter_LowPowerMode
* Description : Power-off system clocks and power while entering suspend mode
* Input : None.
* Return : None.
*******************************************************************************/
void Enter_LowPowerMode(void) {
/* Set the device state to suspend */
bDeviceState = SUSPENDED;
}
/*******************************************************************************
* Function Name : Leave_LowPowerMode
* Description : Restores system clocks and power while exiting suspend mode
* Input : None.
* Return : None.
*******************************************************************************/
void Leave_LowPowerMode(void) {
USB_DeviceMess *pInfo = &Device_Info;
/* Set the device state to the correct state */
if (pInfo->CurrentConfiguration != 0) {
/* Device configured */
bDeviceState = CONFIGURED;
} else {
bDeviceState = ATTACHED;
}
}
/**
* @brief Configures USART1 port.
* @param USART_InitStruct: pointer to a USART_InitType structure that
* contains the configuration information for the specified USART peripheral.
* @retval None
*/
void USART_COM_Init(USART_InitType *USART_InitStruct) {
return;
GPIO_InitType GPIO_InitStructure;
NVIC_InitType NVIC_InitStructure;
/* Enable GPIO clock */
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOA | RCC_APB2_PERIPH_AFIO, ENABLE);
/* Enable USART1 Clock */
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_USART1, ENABLE);
/* Configure USART1 Tx as alternate function push-pull */
GPIO_InitStructure.Pin = GPIO_PIN_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);
/* Configure USART1 Rx as input floating */
GPIO_InitStructure.Pin = GPIO_PIN_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);
/* Enable the USARTy Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* USART1 configuration */
USART_Init(USART1, USART_InitStruct);
/* Enable the USART1 Receive interrupt */
USART_ConfigInt(USART1, USART_INT_RXDNE, ENABLE);
/* Enable USART1 */
USART_Enable(USART1, ENABLE);
}
/*******************************************************************************
* Function Name : USB_Interrupts_Config
* Description : Configures the USB interrupts
* Input : None.
* Return : None.
*******************************************************************************/
void USB_Interrupts_Config(void) {
NVIC_InitType NVIC_InitStructure;
/* 2 bit for pre-emption priority, 2 bits for subpriority */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
/* Enable the USB interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0xff;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Enable the USB Wake-up interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USBWakeUp_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0xff;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/*******************************************************************************
* Function Name : USART_Config_Default.
* Description : configure the EVAL_COM1 with default values.
* Input : None.
* Return : None.
*******************************************************************************/
void USART_Config_Default(void) {
return;
/* USART1 default configuration */
/* USART1 configured as follow:
- BaudRate = 115200 baud
- Word Length = 8 Bits
- One Stop Bit
- Parity Odd
- Hardware flow control disabled
- Receive and transmit enabled
*/
USART_InitStructure.BaudRate = 115200;
USART_InitStructure.WordLength = USART_WL_8B;
USART_InitStructure.StopBits = USART_STPB_1;
USART_InitStructure.Parity = USART_PE_NO;
USART_InitStructure.HardwareFlowControl = USART_HFCTRL_NONE;
USART_InitStructure.Mode = USART_MODE_RX | USART_MODE_TX;
/* Configure and enable the USART1 */
USART_COM_Init(&USART_InitStructure);
}
/*******************************************************************************
* Function Name : USART_Config.
* Description : Configure the EVAL_COM1 according to the line coding structure.
* Input : None.
* Return : Configuration status
TRUE : configuration done with success
FALSE : configuration aborted.
*******************************************************************************/
bool USART_Config(void) {
return true;
/* set the Stop bit*/
switch (linecoding.format) {
case 0:
USART_InitStructure.StopBits = USART_STPB_1;
break;
case 1:
USART_InitStructure.StopBits = USART_STPB_1_5;
break;
case 2:
USART_InitStructure.StopBits = USART_STPB_2;
break;
default : {
USART_Config_Default();
return (false);
}
}
/* set the parity bit*/
switch (linecoding.paritytype) {
case 0:
USART_InitStructure.Parity = USART_PE_NO;
break;
case 1:
USART_InitStructure.Parity = USART_PE_EVEN;
break;
case 2:
USART_InitStructure.Parity = USART_PE_ODD;
break;
default : {
USART_Config_Default();
return (false);
}
}
/*set the data type : only 8bits and 9bits is supported */
switch (linecoding.datatype) {
case 0x07:
/* With this configuration a parity (Even or Odd) should be set */
USART_InitStructure.WordLength = USART_WL_8B;
break;
case 0x08:
if (USART_InitStructure.Parity == USART_PE_NO) {
USART_InitStructure.WordLength = USART_WL_8B;
} else {
USART_InitStructure.WordLength = USART_WL_9B;
}
break;
default : {
USART_Config_Default();
return (false);
}
}
USART_InitStructure.BaudRate = linecoding.bitrate;
USART_InitStructure.HardwareFlowControl = USART_HFCTRL_NONE;
USART_InitStructure.Mode = USART_MODE_RX | USART_MODE_TX;
/* Configure and enable the USART */
USART_COM_Init(&USART_InitStructure);
return (true);
}
/*******************************************************************************
* Function Name : USB_To_USART_Send_Data.
* Description : send the received data from USB to the UART 0.
* Input : data_buffer: data address.
Nb_bytes: number of bytes to send.
* Return : none.
*******************************************************************************/
#include "SerialPorts.h"
//extern TSerialPorts *gp_SERIAL_PORTS;
//extern tSerialPorts SERIAL_PORTS;
void USB_To_USART_Send_Data(uint8_t *data_buffer, uint8_t Nb_bytes) {
for (uint8_t i = 0; i < Nb_bytes; ++i) {
osStatus_t status = osMessageQueuePut(SERIAL_PORTS.SerialPortUsb.rxDataQueue, &data_buffer[i], 0x0, 0U);
asm("nop");
}
// uint32_t i;
// for (i = 0; i < Nb_bytes; i++) {
// USART_SendData(USART1, *(data_buffer + i));
// while (USART_GetFlagStatus(USART1, USART_FLAG_TXDE) == RESET);
// }
}
/*******************************************************************************
* Function Name : Handle_USBAsynchXfer.
* Description : send data to USB.
* Input : None.
* Return : none.
*******************************************************************************/
void Handle_USBAsynchXfer(void) {
return;
uint16_t USB_Tx_ptr;
uint16_t USB_Tx_length;
if (USB_Tx_State != 1) {
if (USART_Rx_ptr_out == USART_RX_DATA_SIZE) {
USART_Rx_ptr_out = 0;
}
if (USART_Rx_ptr_out == USART_Rx_ptr_in) {
USB_Tx_State = 0;
return;
}
if (USART_Rx_ptr_out > USART_Rx_ptr_in) /* rollback */
{
USART_Rx_length = USART_RX_DATA_SIZE - USART_Rx_ptr_out;
} else {
USART_Rx_length = USART_Rx_ptr_in - USART_Rx_ptr_out;
}
if (USART_Rx_length > VIRTUAL_COM_PORT_DATA_SIZE) {
USB_Tx_ptr = USART_Rx_ptr_out;
USB_Tx_length = VIRTUAL_COM_PORT_DATA_SIZE;
USART_Rx_ptr_out += VIRTUAL_COM_PORT_DATA_SIZE;
USART_Rx_length -= VIRTUAL_COM_PORT_DATA_SIZE;
} else {
USB_Tx_ptr = USART_Rx_ptr_out;
USB_Tx_length = USART_Rx_length;
USART_Rx_ptr_out += USART_Rx_length;
USART_Rx_length = 0;
}
USB_Tx_State = 1;
USB_CopyUserToPMABuf(&USART_Rx_Buffer[USB_Tx_ptr], ENDP1_TXADDR, USB_Tx_length);
USB_SetEpTxCnt(ENDP1, USB_Tx_length);
USB_SetEpTxValid(ENDP1);
}
}
void CDC_Send_DATA(uint8_t *ptrBuffer, uint16_t Send_length) {
USB_CopyUserToPMABuf((unsigned char *) ptrBuffer, ENDP1_TXADDR, Send_length);
USB_SetEpTxCnt(ENDP1, Send_length);
USB_SetEpTxValid(ENDP1);
}
/*******************************************************************************
* Function Name : UART_To_USB_Send_Data.
* Description : send the received data from UART 0 to USB.
* Input : None.
* Return : none.
*******************************************************************************/
void USART_To_USB_Send_Data(void) {
if (linecoding.datatype == 7) {
USART_Rx_Buffer[USART_Rx_ptr_in] = USART_ReceiveData(USART1) & 0x7F;
} else if (linecoding.datatype == 8) {
USART_Rx_Buffer[USART_Rx_ptr_in] = USART_ReceiveData(USART1);
}
USART_Rx_ptr_in++;
/* To avoid buffer overflow */
if (USART_Rx_ptr_in == USART_RX_DATA_SIZE) {
USART_Rx_ptr_in = 0;
}
}

102
src/main.c Normal file
View File

@ -0,0 +1,102 @@
/*****************************************************************************
* Copyright (c) 2019, Nations Technologies Inc.
*
* All rights reserved.
* ****************************************************************************
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
*
* Nations' name may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ****************************************************************************/
/**
* @file main.c
* @author Nations
* @version v1.0.0
*
* @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
*/
#include "n32g45x.h"
#include "hw_config.h"
#include "usb_lib.h"
#include "usb_pwr.h"
__IO uint32_t TimingDelay = 0;
/** @addtogroup DAC_OneChanneloutputNoiseWave
* @{
*/
void Delay(__IO uint32_t nCount);
#include "usb_desc.h"
/**
* @brief Main program.
*/
/*
uint8_t data[1024];
int main(void) {
Set_System();
USB_Interrupts_Config();
Set_USBClock();
USB_Init();
for (int i=0; i<1024; ++i) {
data[i] = i;
}
while (1) {
// CDC_Send_DATA(data, 128);
}
}
void Delay(__IO uint32_t nCount) {
TimingDelay = nCount;
for (; nCount != 0; nCount--);
}
*/
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param expr: If expr is false, it calls assert_failed function which reports
* the name of the source file and the source line number of the call
* that failed. If expr is true, it returns no value.
* @param file: pointer to the source file name.
* @param line: assert_param error line source number.
*/
void assert_failed(const uint8_t *expr, const uint8_t *file, uint32_t line) {
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1) {
}
}
#endif

88
src/n32g45x_it.c Normal file
View File

@ -0,0 +1,88 @@
/*****************************************************************************
* Copyright (c) 2019, Nations Technologies Inc.
*
* All rights reserved.
* ****************************************************************************
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
*
* Nations' name may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ****************************************************************************/
/**
* @file n32g45x_it.c
* @author Nations
* @version v1.0.1
*
* @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
*/
#include "n32g45x_it.h"
#include "n32g45x.h"
#include "main.h"
#include "usb_istr.h"
#include "hw_config.h"
/** @addtogroup N32G45X_StdPeriph_Template
* @{
*/
extern __IO uint32_t CurrDataCounterEnd;
/******************************************************************************/
/* Cortex-M4 Processor Exceptions Handlers */
/******************************************************************************/
/**
* @brief This function handles USB_LP_CAN1_RX0_IRQ Handler.
*/
//void USB_LP_CAN1_RX0_IRQHandler(void)
//{
// USB_Istr();
//}
/*******************************************************************************
* Function Name : USBWakeUp_IRQHandler
* Description : This function handles USB WakeUp interrupt request.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void USBWakeUp_IRQHandler(void)
{
EXTI_ClrITPendBit(EXTI_LINE18);
}
/******************************************************************************/
/* N32G45X Peripherals Interrupt Handlers */
/* Add here the Interrupt Handler for the used peripheral(s) (PPP), for the */
/* available peripheral interrupt handler's name please refer to the startup */
/* file (startup_n32g45x.s). */
/******************************************************************************/
/**
* @brief This function handles PPP interrupt request.
*/
/*void PPP_IRQHandler(void)
{
}*/
/**
* @}
*/

177
src/usb_desc.c Normal file
View File

@ -0,0 +1,177 @@
/*****************************************************************************
* Copyright (c) 2019, Nations Technologies Inc.
*
* All rights reserved.
* ****************************************************************************
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
*
* Nations' name may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ****************************************************************************/
/**
* @file usb_desc.c
* @author Nations
* @version v1.0.0
*
* @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
*/
/* Includes ------------------------------------------------------------------*/
#include "usb_lib.h"
#include "usb_desc.h"
/* USB Standard Device Descriptor */
const uint8_t Virtual_Com_Port_DeviceDescriptor[] =
{
0x12, /* bLength */
USB_DEVICE_DESCRIPTOR_TYPE, /* bDescriptorType */
0x00,
0x02, /* bcdUSB = 2.00 */
0x02, /* bDeviceClass: CDC */
0x00, /* bDeviceSubClass */
0x00, /* bDeviceProtocol */
0x40, /* bMaxPacketSize0 */
0xF5, /*idVendor (0x19F5)*/
0x19,
0x40,
0x57, /* idProduct = 0x7540 */
0x00,
0x02, /* bcdDevice = 2.00 */
1, /* Index of string descriptor describing manufacturer */
2, /* Index of string descriptor describing product */
3, /* Index of string descriptor describing the device's serial number */
0x01 /* bNumConfigurations */
};
const uint8_t Virtual_Com_Port_ConfigDescriptor[] =
{
/*Configuration Descriptor*/
0x09, /* bLength: Configuration Descriptor size */
USB_CONFIGURATION_DESCRIPTOR_TYPE, /* bDescriptorType: Configuration */
VIRTUAL_COM_PORT_SIZ_CONFIG_DESC, /* wTotalLength:no of returned bytes */
0x00,
0x02, /* bNumInterfaces: 2 interface */
0x01, /* bConfigurationValue: Configuration value */
0x00, /* iConfiguration: Index of string descriptor describing the configuration */
0xC0, /* bmAttributes: self powered */
0x32, /* MaxPower 0 mA */
/*Interface Descriptor*/
0x09, /* bLength: Interface Descriptor size */
USB_INTERFACE_DESCRIPTOR_TYPE, /* bDescriptorType: Interface */
/* Interface descriptor type */
0x00, /* bInterfaceNumber: Number of Interface */
0x00, /* bAlternateSetting: Alternate setting */
0x01, /* bNumEndpoints: One endpoints used */
0x02, /* bInterfaceClass: Communication Interface Class */
0x02, /* bInterfaceSubClass: Abstract Control Model */
0x01, /* bInterfaceProtocol: Common AT commands */
0x00, /* iInterface: */
/*Header Functional Descriptor*/
0x05, /* bLength: Endpoint Descriptor size */
0x24, /* bDescriptorType: CS_INTERFACE */
0x00, /* bDescriptorSubtype: Header Func Desc */
0x10, /* bcdCDC: spec release number */
0x01,
/*Call Management Functional Descriptor*/
0x05, /* bFunctionLength */
0x24, /* bDescriptorType: CS_INTERFACE */
0x01, /* bDescriptorSubtype: Call Management Func Desc */
0x00, /* bmCapabilities: D0+D1 */
0x01, /* bDataInterface: 1 */
/*ACM Functional Descriptor*/
0x04, /* bFunctionLength */
0x24, /* bDescriptorType: CS_INTERFACE */
0x02, /* bDescriptorSubtype: Abstract Control Management desc */
0x02, /* bmCapabilities */
/*Union Functional Descriptor*/
0x05, /* bFunctionLength */
0x24, /* bDescriptorType: CS_INTERFACE */
0x06, /* bDescriptorSubtype: Union func desc */
0x00, /* bMasterInterface: Communication class interface */
0x01, /* bSlaveInterface0: Data Class Interface */
/*Endpoint 2 Descriptor*/
0x07, /* bLength: Endpoint Descriptor size */
USB_ENDPOINT_DESCRIPTOR_TYPE, /* bDescriptorType: Endpoint */
0x82, /* bEndpointAddress: (IN2) */
0x03, /* bmAttributes: Interrupt */
VIRTUAL_COM_PORT_INT_SIZE, /* wMaxPacketSize: */
0x00,
0xFF, /* bInterval: */
/*Data class interface descriptor*/
0x09, /* bLength: Endpoint Descriptor size */
USB_INTERFACE_DESCRIPTOR_TYPE, /* bDescriptorType: */
0x01, /* bInterfaceNumber: Number of Interface */
0x00, /* bAlternateSetting: Alternate setting */
0x02, /* bNumEndpoints: Two endpoints used */
0x0A, /* bInterfaceClass: CDC */
0x00, /* bInterfaceSubClass: */
0x00, /* bInterfaceProtocol: */
0x00, /* iInterface: */
/*Endpoint 3 Descriptor*/
0x07, /* bLength: Endpoint Descriptor size */
USB_ENDPOINT_DESCRIPTOR_TYPE, /* bDescriptorType: Endpoint */
0x03, /* bEndpointAddress: (OUT3) */
0x02, /* bmAttributes: Bulk */
VIRTUAL_COM_PORT_DATA_SIZE, /* wMaxPacketSize: */
0x00,
0x00, /* bInterval: ignore for Bulk transfer */
/*Endpoint 1 Descriptor*/
0x07, /* bLength: Endpoint Descriptor size */
USB_ENDPOINT_DESCRIPTOR_TYPE, /* bDescriptorType: Endpoint */
0x81, /* bEndpointAddress: (IN1) */
0x02, /* bmAttributes: Bulk */
VIRTUAL_COM_PORT_DATA_SIZE, /* wMaxPacketSize: */
0x00,
0x00 /* bInterval */
};
/* USB String Descriptors */
const uint8_t Virtual_Com_Port_StringLangID[VIRTUAL_COM_PORT_SIZ_STRING_LANGID] =
{
VIRTUAL_COM_PORT_SIZ_STRING_LANGID,
USB_STRING_DESCRIPTOR_TYPE,
0x09,
0x04 /* LangID = 0x0409: U.S. English */
};
const uint8_t Virtual_Com_Port_StringVendor[VIRTUAL_COM_PORT_SIZ_STRING_VENDOR] =
{
VIRTUAL_COM_PORT_SIZ_STRING_VENDOR, /* Size of Vendor string */
USB_STRING_DESCRIPTOR_TYPE, /* bDescriptorType*/
/* Manufacturer: "NATIONS" */
'N', 0, 'A', 0, 'T', 0, 'I', 0, 'O', 0, 'N', 0, 'S', 0
};
const uint8_t Virtual_Com_Port_StringProduct[VIRTUAL_COM_PORT_SIZ_STRING_PRODUCT] =
{
VIRTUAL_COM_PORT_SIZ_STRING_PRODUCT, /* bLength */
USB_STRING_DESCRIPTOR_TYPE, /* bDescriptorType */
/* Product name: "N32G45x Port" */
'N', 0, '3', 0, '2', 0, 'G', 0, '4', 0, '5', 0, 'x', 0, ' ', 0,
'P', 0, 'o', 0, 'r', 0, 't', 0, ' ', 0, ' ', 0
};
uint8_t Virtual_Com_Port_StringSerial[VIRTUAL_COM_PORT_SIZ_STRING_SERIAL] =
{
VIRTUAL_COM_PORT_SIZ_STRING_SERIAL, /* bLength */
USB_STRING_DESCRIPTOR_TYPE, /* bDescriptorType */
'N', 0, '3', 0, '2', 0, 'G', 0, '4', 0, '5', 0, 'x', 0,
};

151
src/usb_endp.c Normal file
View File

@ -0,0 +1,151 @@
/*****************************************************************************
* Copyright (c) 2019, Nations Technologies Inc.
*
* All rights reserved.
* ****************************************************************************
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
*
* Nations' name may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ****************************************************************************/
/**
* @file usb_endp.c
* @author Nations
* @version v1.0.1
*
* @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
*/
/* Includes ------------------------------------------------------------------*/
#include "usb_lib.h"
#include "usb_desc.h"
#include "usb_mem.h"
#include "hw_config.h"
#include "usb_istr.h"
#include "usb_pwr.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Interval between sending IN packets in frame number (1 frame = 1ms) */
#define VCOMPORT_IN_FRAME_INTERVAL 5
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
uint8_t USB_Rx_Buffer[VIRTUAL_COM_PORT_DATA_SIZE];
extern uint8_t USART_Rx_Buffer[];
extern uint32_t USART_Rx_ptr_out;
extern uint32_t USART_Rx_length;
extern uint8_t USB_Tx_State;
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/*******************************************************************************
* Function Name : EP1_IN_Callback
* Description :
* Input : None.
* Output : None.
* Return : None.
*******************************************************************************/
void EP1_IN_Callback (void)
{
uint16_t USB_Tx_ptr;
uint16_t USB_Tx_length;
if (USB_Tx_State == 1)
{
if (USART_Rx_length == 0)
{
USB_Tx_State = 0;
}
else
{
if (USART_Rx_length > VIRTUAL_COM_PORT_DATA_SIZE)
{
USB_Tx_ptr = USART_Rx_ptr_out;
USB_Tx_length = VIRTUAL_COM_PORT_DATA_SIZE;
USART_Rx_ptr_out += VIRTUAL_COM_PORT_DATA_SIZE;
USART_Rx_length -= VIRTUAL_COM_PORT_DATA_SIZE;
}
else
{
USB_Tx_ptr = USART_Rx_ptr_out;
USB_Tx_length = USART_Rx_length;
USART_Rx_ptr_out += USART_Rx_length;
USART_Rx_length = 0;
}
USB_CopyUserToPMABuf(&USART_Rx_Buffer[USB_Tx_ptr], ENDP1_TXADDR, USB_Tx_length);
USB_SetEpTxCnt(ENDP1, USB_Tx_length);
USB_SetEpTxValid(ENDP1);
}
}
}
/*******************************************************************************
* Function Name : EP3_OUT_Callback
* Description :
* Input : None.
* Output : None.
* Return : None.
*******************************************************************************/
void EP3_OUT_Callback(void)
{
uint16_t USB_Rx_Cnt;
/* Get the received data buffer and update the counter */
USB_Rx_Cnt = USB_SilRead(EP3_OUT, USB_Rx_Buffer);
/* USB data will be immediately processed, this allow next USB traffic being
NAKed till the end of the USART Xfer */
USB_To_USART_Send_Data(USB_Rx_Buffer, USB_Rx_Cnt);
/* Enable the receive of data on EP3 */
USB_SetEpRxValid(ENDP3);
}
/*******************************************************************************
* Function Name : SOF_Callback / INTR_SOFINTR_Callback
* Description :
* Input : None.
* Output : None.
* Return : None.
*******************************************************************************/
void SOF_Callback(void)
{
static uint32_t FrameCount = 0;
if(bDeviceState == CONFIGURED)
{
if (FrameCount++ == VCOMPORT_IN_FRAME_INTERVAL)
{
/* Reset the frame counter */
FrameCount = 0;
/* Check the data to be sent through IN pipe */
Handle_USBAsynchXfer();
}
}
}

237
src/usb_istr.c Normal file
View File

@ -0,0 +1,237 @@
/*****************************************************************************
* Copyright (c) 2019, Nations Technologies Inc.
*
* All rights reserved.
* ****************************************************************************
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
*
* Nations' name may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ****************************************************************************/
/**
* @file usb_istr.c
* @author Nations
* @version v1.0.1
*
* @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
*/
#include "usb_lib.h"
#include "usb_prop.h"
#include "usb_pwr.h"
#include "usb_istr.h"
__IO uint16_t wIstr; /* STS register last read value */
__IO uint8_t bIntPackSOF = 0; /* SOFs received between 2 consecutive packets */
__IO uint32_t esof_counter = 0; /* expected SOF counter */
__IO uint32_t wCNTR = 0;
/* function pointers to non-control endpoints service routines */
void (*pEpInt_IN[7])(void) = {
EP1_IN_Callback,
EP2_IN_Callback,
EP3_IN_Callback,
EP4_IN_Callback,
EP5_IN_Callback,
EP6_IN_Callback,
EP7_IN_Callback,
};
void (*pEpInt_OUT[7])(void) = {
EP1_OUT_Callback,
EP2_OUT_Callback,
EP3_OUT_Callback,
EP4_OUT_Callback,
EP5_OUT_Callback,
EP6_OUT_Callback,
EP7_OUT_Callback,
};
/*******************************************************************************
* Function Name : USB_Istr
* Description : STS events interrupt service routine
* Input : None.
* Output : None.
* Return : None.
*******************************************************************************/
void USB_Istr(void)
{
__IO uint32_t EP[8];
wIstr = _GetISTR();
#if (IMR_MSK & STS_CTRS)
if (wIstr & STS_CTRS & wInterrupt_Mask)
{
/* servicing of the endpoint correct transfer interrupt */
/* clear of the CTR flag into the sub */
USB_CorrectTransferLp();
#ifdef CTR_CALLBACK
CTR_Callback();
#endif
}
#endif
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
#if (IMR_MSK & STS_RST)
if (wIstr & STS_RST & wInterrupt_Mask)
{
_SetISTR((uint16_t)CLR_RST);
Device_Property.Reset();
#ifdef RESET_CALLBACK
RESET_Callback();
#endif
}
#endif
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
#if (IMR_MSK & STS_DOVR)
if (wIstr & STS_DOVR & wInterrupt_Mask)
{
_SetISTR((uint16_t)CLR_DOVR);
#ifdef DOVR_CALLBACK
DOVR_Callback();
#endif
}
#endif
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
#if (IMR_MSK & STS_ERROR)
if (wIstr & STS_ERROR & wInterrupt_Mask)
{
_SetISTR((uint16_t)CLR_ERROR);
#ifdef ERR_CALLBACK
ERR_Callback();
#endif
}
#endif
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
#if (IMR_MSK & STS_WKUP)
if (wIstr & STS_WKUP & wInterrupt_Mask)
{
_SetISTR((uint16_t)CLR_WKUP);
Resume(RESUME_EXTERNAL);
#ifdef WKUP_CALLBACK
WKUP_Callback();
#endif
}
#endif
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
#if (IMR_MSK & STS_SUSPD)
if (wIstr & STS_SUSPD & wInterrupt_Mask)
{
/* check if SUSPEND is possible */
if (fSuspendEnabled)
{
Suspend();
}
else
{
/* if not possible then resume after xx ms */
Resume(RESUME_LATER);
}
/* clear of the STS bit must be done after setting of CTRL_FSUSPD */
_SetISTR((uint16_t)CLR_SUSPD);
#ifdef SUSP_CALLBACK
SUSP_Callback();
#endif
}
#endif
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
#if (IMR_MSK & STS_SOF)
if (wIstr & STS_SOF & wInterrupt_Mask)
{
_SetISTR((uint16_t)CLR_SOF);
bIntPackSOF++;
#ifdef SOF_CALLBACK
SOF_Callback();
#endif
}
#endif
/*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
#if (IMR_MSK & STS_ESOF)
uint32_t i=0;
if (wIstr & STS_ESOF & wInterrupt_Mask)
{
/* clear ESOF flag in STS */
_SetISTR((uint16_t)CLR_ESOF);
if ((_GetFNR() & FN_RXDP) != 0)
{
/* increment ESOF counter */
esof_counter++;
/* test if we enter in ESOF more than 3 times with FSUSP =0 and RXDP =1=>> possible missing SUSP flag*/
if ((esof_counter > 3) && ((_GetCNTR() & CTRL_FSUSPD) == 0))
{
/* this a sequence to apply a force RESET*/
/*Store CTRL value */
wCNTR = _GetCNTR();
/*Store endpoints registers status */
for (i = 0; i < 8; i++)
EP[i] = _GetENDPOINT(i);
/*apply FRES */
wCNTR |= CTRL_FRST;
_SetCNTR(wCNTR);
/*clear FRES*/
wCNTR &= ~CTRL_FRST;
_SetCNTR(wCNTR);
/*poll for RESET flag in STS*/
while ((_GetISTR() & STS_RST) == 0)
;
/* clear RESET flag in STS */
_SetISTR((uint16_t)CLR_RST);
/*restore Enpoints*/
for (i = 0; i < 8; i++)
_SetENDPOINT(i, EP[i]);
esof_counter = 0;
}
}
else
{
esof_counter = 0;
}
/* resume handling timing is made with ESOFs */
Resume(RESUME_ESOF); /* request without change of the machine state */
#ifdef ESOF_CALLBACK
ESOF_Callback();
#endif
}
#endif
} /* USB_Istr */
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/

418
src/usb_prop.c Normal file
View File

@ -0,0 +1,418 @@
/*****************************************************************************
* Copyright (c) 2019, Nations Technologies Inc.
*
* All rights reserved.
* ****************************************************************************
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
*
* Nations' name may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ****************************************************************************/
/**
* @file usb_prop.c
* @author Nations
* @version v1.0.0
*
* @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
*/
/* Includes ------------------------------------------------------------------*/
#include "usb_lib.h"
#include "usb_conf.h"
#include "usb_prop.h"
#include "usb_desc.h"
#include "usb_pwr.h"
#include "hw_config.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
uint8_t Request = 0;
LINE_CODING linecoding =
{
115200, /* baud rate*/
0x00, /* stop bits-1*/
0x00, /* parity - none*/
0x08 /* no. of bits 8*/
};
/* -------------------------------------------------------------------------- */
/* Structures initializations */
/* -------------------------------------------------------------------------- */
USB_Device Device_Table =
{
EP_NUM,
1
};
DEVICE_PROP Device_Property =
{
Virtual_Com_Port_init,
Virtual_Com_Port_Reset,
Virtual_Com_Port_Status_In,
Virtual_Com_Port_Status_Out,
Virtual_Com_Port_Data_Setup,
Virtual_Com_Port_NoData_Setup,
Virtual_Com_Port_Get_Interface_Setting,
Virtual_Com_Port_GetDeviceDescriptor,
Virtual_Com_Port_GetConfigDescriptor,
Virtual_Com_Port_GetStringDescriptor,
0,
0x40 /*MAX PACKET SIZE*/
};
USER_STANDARD_REQUESTS User_Standard_Requests =
{
Virtual_Com_Port_GetConfiguration,
Virtual_Com_Port_SetConfiguration,
Virtual_Com_Port_GetInterface,
Virtual_Com_Port_SetInterface,
Virtual_Com_Port_GetStatus,
Virtual_Com_Port_ClearFeature,
Virtual_Com_Port_SetEndPointFeature,
Virtual_Com_Port_SetDeviceFeature,
Virtual_Com_Port_SetDeviceAddress
};
USB_OneDescriptor Device_Descriptor =
{
(uint8_t*)Virtual_Com_Port_DeviceDescriptor,
VIRTUAL_COM_PORT_SIZ_DEVICE_DESC
};
USB_OneDescriptor Config_Descriptor =
{
(uint8_t*)Virtual_Com_Port_ConfigDescriptor,
VIRTUAL_COM_PORT_SIZ_CONFIG_DESC
};
USB_OneDescriptor String_Descriptor[4] =
{
{(uint8_t*)Virtual_Com_Port_StringLangID, VIRTUAL_COM_PORT_SIZ_STRING_LANGID},
{(uint8_t*)Virtual_Com_Port_StringVendor, VIRTUAL_COM_PORT_SIZ_STRING_VENDOR},
{(uint8_t*)Virtual_Com_Port_StringProduct, VIRTUAL_COM_PORT_SIZ_STRING_PRODUCT},
{(uint8_t*)Virtual_Com_Port_StringSerial, VIRTUAL_COM_PORT_SIZ_STRING_SERIAL}
};
/* Extern variables ----------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Extern function prototypes ------------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/*******************************************************************************
* Function Name : Virtual_Com_Port_init.
* Description : Virtual COM Port Mouse init routine.
* Input : None.
* Output : None.
* Return : None.
*******************************************************************************/
void Virtual_Com_Port_init(void)
{
pInformation->CurrentConfiguration = 0;
/* Connect the device */
PowerOn();
/* Perform basic device initialization operations */
USB_SilInit();
/* configure the USART to the default settings */
USART_Config_Default();
bDeviceState = UNCONNECTED;
}
/*******************************************************************************
* Function Name : Virtual_Com_Port_Reset
* Description : Virtual_Com_Port Mouse reset routine
* Input : None.
* Output : None.
* Return : None.
*******************************************************************************/
void Virtual_Com_Port_Reset(void)
{
/* Set Virtual_Com_Port DEVICE as not configured */
pInformation->CurrentConfiguration = 0;
/* Current Feature initialization */
pInformation->CurrentFeature = Virtual_Com_Port_ConfigDescriptor[7];
/* Set Virtual_Com_Port DEVICE with the default Interface*/
pInformation->CurrentInterface = 0;
USB_SetBuftab(BTABLE_ADDRESS);
/* Initialize Endpoint 0 */
USB_SetEpType(ENDP0, EP_CONTROL);
SetEPTxStatus(ENDP0, EP_TX_STALL);
USB_SetEpRxAddr(ENDP0, ENDP0_RXADDR);
USB_SetEpTxAddr(ENDP0, ENDP0_TXADDR);
USB_ClrStsOut(ENDP0);
USB_SetEpRxCnt(ENDP0, Device_Property.MaxPacketSize);
USB_SetEpRxValid(ENDP0);
/* Initialize Endpoint 1 */
USB_SetEpType(ENDP1, EP_BULK);
USB_SetEpTxAddr(ENDP1, ENDP1_TXADDR);
SetEPTxStatus(ENDP1, EP_TX_NAK);
SetEPRxStatus(ENDP1, EP_RX_DIS);
/* Initialize Endpoint 2 */
USB_SetEpType(ENDP2, EP_INTERRUPT);
USB_SetEpTxAddr(ENDP2, ENDP2_TXADDR);
SetEPRxStatus(ENDP2, EP_RX_DIS);
SetEPTxStatus(ENDP2, EP_TX_NAK);
/* Initialize Endpoint 3 */
USB_SetEpType(ENDP3, EP_BULK);
USB_SetEpRxAddr(ENDP3, ENDP3_RXADDR);
USB_SetEpRxCnt(ENDP3, VIRTUAL_COM_PORT_DATA_SIZE);
SetEPRxStatus(ENDP3, EP_RX_VALID);
SetEPTxStatus(ENDP3, EP_TX_DIS);
/* Set this device to response on default address */
USB_SetDeviceAddress(0);
bDeviceState = ATTACHED;
}
/*******************************************************************************
* Function Name : Virtual_Com_Port_SetConfiguration.
* Description : Update the device state to configured.
* Input : None.
* Output : None.
* Return : None.
*******************************************************************************/
void Virtual_Com_Port_SetConfiguration(void)
{
USB_DeviceMess *pInfo = &Device_Info;
if (pInfo->CurrentConfiguration != 0)
{
/* Device configured */
bDeviceState = CONFIGURED;
}
}
/*******************************************************************************
* Function Name : Virtual_Com_Port_SetConfiguration.
* Description : Update the device state to addressed.
* Input : None.
* Output : None.
* Return : None.
*******************************************************************************/
void Virtual_Com_Port_SetDeviceAddress (void)
{
bDeviceState = ADDRESSED;
}
/*******************************************************************************
* Function Name : Virtual_Com_Port_Status_In.
* Description : Virtual COM Port Status In Routine.
* Input : None.
* Output : None.
* Return : None.
*******************************************************************************/
void Virtual_Com_Port_Status_In(void)
{
if (Request == SET_LINE_CODING)
{
USART_Config();
Request = 0;
}
}
/*******************************************************************************
* Function Name : Virtual_Com_Port_Status_Out
* Description : Virtual COM Port Status OUT Routine.
* Input : None.
* Output : None.
* Return : None.
*******************************************************************************/
void Virtual_Com_Port_Status_Out(void)
{}
/*******************************************************************************
* Function Name : Virtual_Com_Port_Data_Setup
* Description : handle the data class specific requests
* Input : Request Nb.
* Output : None.
* Return : UnSupport or Success.
*******************************************************************************/
USB_Result Virtual_Com_Port_Data_Setup(uint8_t RequestNo)
{
uint8_t *(*CopyRoutine)(uint16_t);
CopyRoutine = NULL;
if (RequestNo == GET_LINE_CODING)
{
if (Type_Recipient == (CLASS_REQUEST | INTERFACE_RECIPIENT))
{
CopyRoutine = Virtual_Com_Port_GetLineCoding;
}
}
else if (RequestNo == SET_LINE_CODING)
{
if (Type_Recipient == (CLASS_REQUEST | INTERFACE_RECIPIENT))
{
CopyRoutine = Virtual_Com_Port_SetLineCoding;
}
Request = SET_LINE_CODING;
}
if (CopyRoutine == NULL)
{
return UnSupport;
}
pInformation->Ctrl_Info.CopyData = CopyRoutine;
pInformation->Ctrl_Info.Usb_wOffset = 0;
(*CopyRoutine)(0);
return Success;
}
/*******************************************************************************
* Function Name : Virtual_Com_Port_NoData_Setup.
* Description : handle the no data class specific requests.
* Input : Request Nb.
* Output : None.
* Return : UnSupport or Success.
*******************************************************************************/
USB_Result Virtual_Com_Port_NoData_Setup(uint8_t RequestNo)
{
if (Type_Recipient == (CLASS_REQUEST | INTERFACE_RECIPIENT))
{
if (RequestNo == SET_COMM_FEATURE)
{
return Success;
}
else if (RequestNo == SET_CONTROL_LINE_STATE)
{
return Success;
}
}
return UnSupport;
}
/*******************************************************************************
* Function Name : Virtual_Com_Port_GetDeviceDescriptor.
* Description : Gets the device descriptor.
* Input : Length.
* Output : None.
* Return : The address of the device descriptor.
*******************************************************************************/
uint8_t *Virtual_Com_Port_GetDeviceDescriptor(uint16_t Length)
{
return Standard_GetDescriptorData(Length, &Device_Descriptor);
}
/*******************************************************************************
* Function Name : Virtual_Com_Port_GetConfigDescriptor.
* Description : get the configuration descriptor.
* Input : Length.
* Output : None.
* Return : The address of the configuration descriptor.
*******************************************************************************/
uint8_t *Virtual_Com_Port_GetConfigDescriptor(uint16_t Length)
{
return Standard_GetDescriptorData(Length, &Config_Descriptor);
}
/*******************************************************************************
* Function Name : Virtual_Com_Port_GetStringDescriptor
* Description : Gets the string descriptors according to the needed index
* Input : Length.
* Output : None.
* Return : The address of the string descriptors.
*******************************************************************************/
uint8_t *Virtual_Com_Port_GetStringDescriptor(uint16_t Length)
{
uint8_t wValue0 = pInformation->USBwValue0;
if (wValue0 > 4)
{
return NULL;
}
else
{
return Standard_GetDescriptorData(Length, &String_Descriptor[wValue0]);
}
}
/*******************************************************************************
* Function Name : Virtual_Com_Port_Get_Interface_Setting.
* Description : test the interface and the alternate setting according to the
* supported one.
* Input1 : uint8_t: Interface : interface number.
* Input2 : uint8_t: AlternateSetting : Alternate Setting number.
* Output : None.
* Return : The address of the string descriptors.
*******************************************************************************/
USB_Result Virtual_Com_Port_Get_Interface_Setting(uint8_t Interface, uint8_t AlternateSetting)
{
if (AlternateSetting > 0)
{
return UnSupport;
}
else if (Interface > 1)
{
return UnSupport;
}
return Success;
}
/*******************************************************************************
* Function Name : Virtual_Com_Port_GetLineCoding.
* Description : send the linecoding structure to the PC host.
* Input : Length.
* Output : None.
* Return : Linecoding structure base address.
*******************************************************************************/
uint8_t *Virtual_Com_Port_GetLineCoding(uint16_t Length)
{
if (Length == 0)
{
pInformation->Ctrl_Info.Usb_wLength = sizeof(linecoding);
return NULL;
}
return(uint8_t *)&linecoding;
}
/*******************************************************************************
* Function Name : Virtual_Com_Port_SetLineCoding.
* Description : Set the linecoding structure fields.
* Input : Length.
* Output : None.
* Return : Linecoding structure base address.
*******************************************************************************/
uint8_t *Virtual_Com_Port_SetLineCoding(uint16_t Length)
{
if (Length == 0)
{
pInformation->Ctrl_Info.Usb_wLength = sizeof(linecoding);
return NULL;
}
return(uint8_t *)&linecoding;
}

343
src/usb_pwr.c Normal file
View File

@ -0,0 +1,343 @@
/*****************************************************************************
* Copyright (c) 2019, Nations Technologies Inc.
*
* All rights reserved.
* ****************************************************************************
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the disclaimer below.
*
* Nations' name may not be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* ****************************************************************************/
/**
* @file usb_pwr.c
* @author Nations
* @version v1.0.1
*
* @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
*/
#include "usb_lib.h"
#include "usb_conf.h"
#include "usb_pwr.h"
#include "hw_config.h"
__IO uint32_t bDeviceState = UNCONNECTED; /* USB device status */
__IO bool fSuspendEnabled = true; /* true when suspend is possible */
__IO uint32_t EP[8];
struct
{
__IO RESUME_STATE eState;
__IO uint8_t bESOFcnt;
} ResumeS;
__IO uint32_t remotewakeupon = 0;
/* Extern function prototypes ------------------------------------------------*/
/*******************************************************************************
* Function Name : PowerOn
* Description :
* Input : None.
* Output : None.
* Return : Success.
*******************************************************************************/
USB_Result PowerOn(void)
{
uint16_t wRegVal;
/*** CNTR_PWDN = 0 ***/
wRegVal = CTRL_FRST;
_SetCNTR(wRegVal);
/*** CTRL_FRST = 0 ***/
wInterrupt_Mask = 0;
_SetCNTR(wInterrupt_Mask);
/*** Clear pending interrupts ***/
_SetISTR(0);
/*** Set interrupt mask ***/
wInterrupt_Mask = CTRL_RSTM | CTRL_SUSPDM | CTRL_WKUPM;
_SetCNTR(wInterrupt_Mask);
return Success;
}
/*******************************************************************************
* Function Name : PowerOff
* Description : handles switch-off conditions
* Input : None.
* Output : None.
* Return : Success.
*******************************************************************************/
USB_Result PowerOff()
{
/* disable all interrupts and force USB reset */
_SetCNTR(CTRL_FRST);
/* clear interrupt status register */
_SetISTR(0);
/* switch-off device */
_SetCNTR(CTRL_FRST + CTRL_PD);
/* sw variables reset */
/* ... */
return Success;
}
#ifdef USB_LOW_PWR_MGMT_SUPPORT
/**
* @brief Configures system clock after wake-up from STOP: enable HSE, PLL
* and select PLL as system clock source.
*/
void SYSCLKConfig_STOP(uint32_t RCC_PLLMULL)
{
__IO uint32_t StartUpCounter = 0, HSEStatus = 0;
/* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/
/* Enable HSE */
RCC->CTRL |= ((uint32_t)RCC_CTRL_HSEEN);
/* Wait till HSE is ready and if Time out is reached exit */
do
{
HSEStatus = RCC->CTRL & RCC_CTRL_HSERDF;
StartUpCounter++;
} while ((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
if ((RCC->CTRL & RCC_CTRL_HSERDF) != RESET)
{
HSEStatus = (uint32_t)0x01;
}
else
{
HSEStatus = (uint32_t)0x00;
}
if (HSEStatus == (uint32_t)0x01)
{
/* Enable Prefetch Buffer */
FLASH->AC |= FLASH_AC_PRFTBFEN;
/* Flash 2 wait state */
FLASH->AC &= (uint32_t)((uint32_t)~FLASH_AC_LATENCY);
FLASH->AC |= (uint32_t)FLASH_AC_LATENCY_4;
/* HCLK = SYSCLK */
RCC->CFG |= (uint32_t)RCC_CFG_AHBPRES_DIV1;
/* PCLK2 = HCLK */
RCC->CFG |= (uint32_t)RCC_CFG_APB2PRES_DIV2; // RCC_CFG_APB2PRES_DIV1
/* PCLK1 = HCLK */
RCC->CFG |= (uint32_t)RCC_CFG_APB1PRES_DIV4; // RCC_CFG_APB1PRES_DIV2
/* PLL configuration: PLLCLK = HSE * 18 = 144 MHz */
RCC->CFG &= (uint32_t)((uint32_t) ~(RCC_CFG_PLLSRC | RCC_CFG_PLLHSEPRES | RCC_CFG_PLLMULFCT));
RCC->CFG |= (uint32_t)(RCC_CFG_PLLSRC_HSE | RCC_PLLMULL);
/* Enable PLL */
RCC->CTRL |= RCC_CTRL_PLLEN;
/* Wait till PLL is ready */
while ((RCC->CTRL & RCC_CTRL_PLLRDF) == 0)
{
}
/* Select PLL as system clock source */
RCC->CFG &= (uint32_t)((uint32_t) ~(RCC_CFG_SCLKSW));
RCC->CFG |= (uint32_t)RCC_CFG_SCLKSW_PLL;
/* Wait till PLL is used as system clock source */
while ((RCC->CFG & (uint32_t)RCC_CFG_SCLKSTS) != (uint32_t)0x08)
{
}
}
else
{ /* If HSE fails to start-up, the application will have wrong clock
configuration. User can add here some code to deal with this error */
}
}
#endif /* USB_LOW_PWR_MGMT_SUPPORT */
/*******************************************************************************
* Function Name : Suspend
* Description : sets suspend mode operating conditions
* Input : None.
* Output : None.
* Return : Success.
*******************************************************************************/
void Suspend(void)
{
uint32_t i = 0;
uint16_t wCNTR;
/* suspend preparation */
/* ... */
/*Store CTRL value */
wCNTR = _GetCNTR();
/* This a sequence to apply a force RESET to handle a robustness case */
/*Store endpoints registers status */
for (i = 0; i < 8; i++)
EP[i] = _GetENDPOINT(i);
/* unmask RESET flag */
wCNTR |= CTRL_RSTM;
_SetCNTR(wCNTR);
/*apply FRES */
wCNTR |= CTRL_FRST;
_SetCNTR(wCNTR);
/*clear FRES*/
wCNTR &= ~CTRL_FRST;
_SetCNTR(wCNTR);
/*poll for RESET flag in STS*/
while ((_GetISTR() & STS_RST) == 0)
;
/* clear RESET flag in STS */
_SetISTR((uint16_t)CLR_RST);
/*restore Enpoints*/
for (i = 0; i < 8; i++)
_SetENDPOINT(i, EP[i]);
/* Now it is safe to enter macrocell in suspend mode */
wCNTR |= CTRL_FSUSPD;
_SetCNTR(wCNTR);
/* force low-power mode in the macrocell */
wCNTR = _GetCNTR();
wCNTR |= CTRL_LP_MODE;
_SetCNTR(wCNTR);
#ifdef USB_LOW_PWR_MGMT_SUPPORT
PWR_EnterSTOP2Mode(PWR_STOPENTRY_WFI);
SYSCLKConfig_STOP(RCC_CFG_PLLMULFCT18);
#endif /* USB_LOW_PWR_MGMT_SUPPORT */
}
/*******************************************************************************
* Function Name : Resume_Init
* Description : Handles wake-up restoring normal operations
* Input : None.
* Output : None.
* Return : Success.
*******************************************************************************/
void Resume_Init(void)
{
uint16_t wCNTR;
/* ------------------ ONLY WITH BUS-POWERED DEVICES ---------------------- */
/* restart the clocks */
/* ... */
/* CTRL_LP_MODE = 0 */
wCNTR = _GetCNTR();
wCNTR &= (~CTRL_LP_MODE);
_SetCNTR(wCNTR);
#ifdef USB_LOW_PWR_MGMT_SUPPORT
/* restore full power */
/* ... on connected devices */
Leave_LowPowerMode();
#endif /* USB_LOW_PWR_MGMT_SUPPORT */
/* reset FSUSP bit */
_SetCNTR(IMR_MSK);
/* reverse suspend preparation */
/* ... */
}
/*******************************************************************************
* Function Name : Resume
* Description : This is the state machine handling resume operations and
* timing sequence. The control is based on the Resume structure
* variables and on the ESOF interrupt calling this subroutine
* without changing machine state.
* Input : a state machine value (RESUME_STATE)
* RESUME_ESOF doesn't change ResumeS.eState allowing
* decrementing of the ESOF counter in different states.
* Output : None.
* Return : None.
*******************************************************************************/
void Resume(RESUME_STATE eResumeSetVal)
{
uint16_t wCNTR;
if (eResumeSetVal != RESUME_ESOF)
ResumeS.eState = eResumeSetVal;
switch (ResumeS.eState)
{
case RESUME_EXTERNAL:
if (remotewakeupon == 0)
{
Resume_Init();
ResumeS.eState = RESUME_OFF;
}
else /* RESUME detected during the RemoteWAkeup signalling => keep RemoteWakeup handling*/
{
ResumeS.eState = RESUME_ON;
}
break;
case RESUME_INTERNAL:
Resume_Init();
ResumeS.eState = RESUME_START;
remotewakeupon = 1;
break;
case RESUME_LATER:
ResumeS.bESOFcnt = 2;
ResumeS.eState = RESUME_WAIT;
break;
case RESUME_WAIT:
ResumeS.bESOFcnt--;
if (ResumeS.bESOFcnt == 0)
ResumeS.eState = RESUME_START;
break;
case RESUME_START:
wCNTR = _GetCNTR();
wCNTR |= CTRL_RESUM;
_SetCNTR(wCNTR);
ResumeS.eState = RESUME_ON;
ResumeS.bESOFcnt = 10;
break;
case RESUME_ON:
ResumeS.bESOFcnt--;
if (ResumeS.bESOFcnt == 0)
{
wCNTR = _GetCNTR();
wCNTR &= (~CTRL_RESUM);
_SetCNTR(wCNTR);
ResumeS.eState = RESUME_OFF;
remotewakeupon = 0;
}
break;
case RESUME_OFF:
case RESUME_ESOF:
default:
ResumeS.eState = RESUME_OFF;
break;
}
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/