opt: usb macros

This commit is contained in:
IOsetting 2022-02-08 01:11:50 +08:00
parent 27790cc05b
commit 78a88edd0c
4 changed files with 99 additions and 23 deletions

View File

@ -2,6 +2,13 @@
FwLib_STC8 是面向 STC8G/STC8H 系列 MCU 的C语言封装库
## 芯片用户手册
* STC8A/8F: [中文](http://www.stcmcudata.com/STC8F-DATASHEET/STC8A-STC8F.pdf), [英文](http://www.stcmcudata.com/STC8F-DATASHEET/STC8-english-20180320.pdf)
* STC8G: [中文](http://www.stcmcudata.com/STC8F-DATASHEET/STC8G.pdf), [英文](https://www.stcmcudata.com/STC8F-DATASHEET/STC8G-EN.pdf)
* STC8H: [中文](http://www.stcmcudata.com/STC8F-DATASHEET/STC8H.pdf), [英文](https://www.stcmcudata.com/STC8F-DATASHEET/STC8H-EN.pdf)
# 特性
* 与 SDCC 和 Keil C51 兼容
@ -15,7 +22,7 @@ FwLib_STC8 是面向 STC8G/STC8H 系列 MCU 的C语言封装库
```bash
# GitHub
git clone https://github.com/IOsetting/FwLib_STC8.git FwLib_STC8
# or Giteee (for Chinese users)
# or Giteee (内网用户)
git clone https://gitee.com/iosetting/fw-lib_-stc8.git FwLib_STC8
```
2. 复制(或移动)到 PlatformIO 项目的lib目录下

View File

@ -2,6 +2,12 @@
FwLib_STC8 is a lite firmware library for STC8G/STC8H series MCU.
## Official Datasheets, User Manuals
* STC8A/8F: [Chinese](http://www.stcmcudata.com/STC8F-DATASHEET/STC8A-STC8F.pdf), [English](http://www.stcmcudata.com/STC8F-DATASHEET/STC8-english-20180320.pdf)
* STC8G: [Chinese](http://www.stcmcudata.com/STC8F-DATASHEET/STC8G.pdf), [English](https://www.stcmcudata.com/STC8F-DATASHEET/STC8G-EN.pdf)
* STC8H: [Chinese](http://www.stcmcudata.com/STC8F-DATASHEET/STC8H.pdf), [English](https://www.stcmcudata.com/STC8F-DATASHEET/STC8H-EN.pdf)
# Features
* Compatible with SDCC and Keil C51

View File

@ -23,7 +23,7 @@ __CODE uint8_t PRODUCTDESC[30];
__XDATA uint8_t HidFreature[64];
__XDATA uint8_t HidInput[64];
__XDATA uint8_t HidOutput[64];
SETUP Setup;
usb_request_t usb_request;
EP0STAGE Ep0Stage;
void USB_Init();
@ -52,10 +52,11 @@ void main()
void USB_Init()
{
SYS_EnableOscillator48M();
USBCLK = 0x00;
USBCON = 0x90;
IP2H |= 0x80; // highest priority
IP2 |= 0x80;
USB_SetClockSource(USB_ClockSource_6M);
USB_SetEnabled(HAL_State_ON);
USB_SetDpDmPullUp(HAL_State_ON);
EXTI_USB_SetIntPriority(EXTI_IntPriority_Highest);
USB_WriteReg(FADDR, 0x00);
USB_WriteReg(POWER, 0x08);
USB_WriteReg(INTRIN1E, 0x3f);
@ -104,16 +105,16 @@ INTERRUPT(USB_Routine, EXTI_VectUSB)
if (csr & OPRDY)
{
Ep0Stage.bStage = EPSTATUS;
USB_ReadFIFO(FIFO0, (uint8_t *)&Setup);
((uint8_t *)&Ep0Stage.wResidue)[0] = Setup.wLengthH;
((uint8_t *)&Ep0Stage.wResidue)[1]= Setup.wLengthL;
switch (Setup.bmRequestType & REQUEST_MASK)
USB_ReadFIFO(FIFO0, (uint8_t *)&usb_request);
((uint8_t *)&Ep0Stage.wResidue)[0] = usb_request.wLength.bb.bh;
((uint8_t *)&Ep0Stage.wResidue)[1] = usb_request.wLength.bb.bl;
switch (usb_request.bmRequestType & REQUEST_MASK)
{
case STANDARD_REQUEST:
switch (Setup.bRequest)
switch (usb_request.bRequest)
{
case SET_ADDRESS:
USB_WriteReg(FADDR, Setup.wValueL);
USB_WriteReg(FADDR, usb_request.wValue.bb.bl);
break;
case SET_CONFIG:
@ -128,7 +129,7 @@ INTERRUPT(USB_Routine, EXTI_VectUSB)
case GET_DESCRIPTOR:
Ep0Stage.bStage = EPDATAIN;
switch (Setup.wValueH)
switch (usb_request.wValue.bb.bh)
{
case DESC_DEVICE:
Ep0Stage.pData = DEVICEDESC;
@ -141,7 +142,7 @@ INTERRUPT(USB_Routine, EXTI_VectUSB)
break;
case DESC_STRING:
switch (Setup.wValueL)
switch (usb_request.wValue.bb.bl)
{
case 0:
Ep0Stage.pData = LANGIDDESC;
@ -186,7 +187,7 @@ INTERRUPT(USB_Routine, EXTI_VectUSB)
break;
case CLASS_REQUEST:
switch (Setup.bRequest)
switch (usb_request.bRequest)
{
case GET_REPORT:
Ep0Stage.pData = HidFreature;

View File

@ -147,17 +147,79 @@
#define VENDOR_REQUEST 0x40
#define REQUEST_MASK 0x60
typedef struct
typedef enum
{
USB_ClockSource_6M = 0x00,
USB_ClockSource_12M = 0x01, // default value
USB_ClockSource_24M = 0x02,
USB_ClockSource_IRCDiv2 = 0x03,
} USB_ClockSource_t;
typedef enum
{
USB_PHYTest_Method_Normal = 0x00,
USB_PHYTest_Method_Force1 = 0x01,
USB_PHYTest_Method_Force0 = 0x02,
USB_PHYTest_Method_ForceOneEnd0 = 0x03,
} USB_PHYTest_Method_t;
#define USB_SetClockPPL(__STATE__) SFR_ASSIGN(USBCLK, 7, __STATE__)
#define USB_SetClockSource(__SOURCE__) SFR_ASSIGN2BIT(USBCLK, 5, __SOURCE__)
#define USB_SetClockCRE(__STATE__) SFR_ASSIGN(USBCLK, 4, __STATE__)
#define USB_SetUSBTestMode(__STATE__) SFR_ASSIGN(USBCLK, 3, __STATE__)
#define USB_SetPHYTestMode(__STATE__) SFR_ASSIGN(USBCLK, 2, __STATE__)
#define USB_SetPHYTestMethod(__TEST_METHOD__) SFR_ASSIGN2BIT(USBCLK, 0, __TEST_METHOD__)
#define USB_SetEnabled(__STATE__) SFR_ASSIGN(USBCON, 7, __STATE__)
#define USB_TurnOnReset() SFR_SET(USBCON, 6)
#define USB_TurnOffReset() SFR_RESET(USBCON, 6)
#define USB_SetPS2Mode(__STATE__) SFR_ASSIGN(USBCON, 5, __STATE__)
/**
* Enable/Disable 1.5KR pull up resistance on D+ and D-
*/
#define USB_SetDpDmPullUp(__STATE__) SFR_ASSIGN(USBCON, 4, __STATE__)
/**
* Enable/Disable 500KR pull down resistance on D+ and D-
*/
#define USB_SetDpDmPullDown(__STATE__) SFR_ASSIGN(USBCON, 3, __STATE__)
#define USB_GetDiffRecvMode() (USBCON & 0x04)
/**
* Read D+ level
*/
#define USB_GetDp() (USBCON & 0x02)
/**
* Write D+ level, writable when PS2 mode is 1
*/
#define USB_SetDp(__STATE__) SFR_ASSIGN(USBCON, 1, __STATE__)
/**
* Read D- level
*/
#define USB_GetDm() (USBCON & 0x01)
/**
* Write D- level, writable when PS2 mode is 1
*/
#define USB_SetDm(__STATE__) SFR_ASSIGN(USBCON, 0, __STATE__)
typedef union
{
uint16_t w;
struct _bb
{
uint8_t bl;
uint8_t bh;
} bb;
} uint16_2uint8_t;
typedef struct _usb_request_t
{
uint8_t bmRequestType;
uint8_t bRequest;
uint8_t wValueL;
uint8_t wValueH;
uint8_t wIndexL;
uint8_t wIndexH;
uint8_t wLengthL;
uint8_t wLengthH;
} SETUP;
uint16_2uint8_t wValue;
uint16_2uint8_t wIndex;
uint16_2uint8_t wLength;
} usb_request_t;
typedef struct
{