Skip to content

Commit

Permalink
Update webserver example, works much better with latest lib
Browse files Browse the repository at this point in the history
  • Loading branch information
maxgerhardt committed Sep 15, 2024
1 parent 4bcdc9e commit 1c8f532
Show file tree
Hide file tree
Showing 11 changed files with 298 additions and 542 deletions.
30 changes: 26 additions & 4 deletions examples/webserver-ch32v307-none-os/lib/HTTP/HTTPS.c
Original file line number Diff line number Diff line change
Expand Up @@ -2339,6 +2339,28 @@ char * DataLocate(char *buf, char *name)
return p;
}

/*********************************************************************
* @fn atoh
*
* @brief Converting character to hexadecimal number
*
* @param src - character
*
* @return hexadecimal number
*/
uint8_t atoh(uint8_t *src)
{
uint8_t desc=0;

if((*src >= '0') && (*src <= '9'))
desc = *src - 0x30;
else if((*src >= 'a') && (*src <= 'f'))
desc = *src - 0x57;
else if((*src >= 'A') && (*src <= 'F'))
desc = *src - 0x37;

return desc;
}
/*********************************************************************
* @fn Refresh_Basic
*
Expand Down Expand Up @@ -2368,10 +2390,10 @@ void Refresh_Basic(u8 *buf)
if(q == NULL) return;
memcpy(temp, p, (q - p));
p = strtok(temp, ".");
BasicCfg.mac[0] = atoi(p);
(strlen(p) == 1) ? (BasicCfg.mac[0] = atoh(p)) : (BasicCfg.mac[0] = atoh(p) << 4 | atoh(p + 1));
for (i = 1; i < 6; i++) {
p = strtok(NULL, ".");
BasicCfg.mac[i] = atoi(p);
(strlen(p) == 1) ? (BasicCfg.mac[i] = atoh(p)) : (BasicCfg.mac[i] = atoh(p) << 4 | atoh(p + 1));
}
}
else return;
Expand Down Expand Up @@ -2790,11 +2812,11 @@ void Init_Para_Tab(void)

Para_Basic[0].para = "__AMAC";
memset(s, 0, 30);
snprintf(s, 30, "%d.%d.%d.%d.%d.%d", Basic_CfgBuf.mac[0], Basic_CfgBuf.mac[1],
snprintf(s, 30, "%x.%x.%x.%x.%x.%x", Basic_CfgBuf.mac[0], Basic_CfgBuf.mac[1],
Basic_CfgBuf.mac[2], Basic_CfgBuf.mac[3], Basic_CfgBuf.mac[4],
Basic_CfgBuf.mac[5]);
strcpy(Para_Basic[0].value, s);
printf("__ASIP = %s\n", Para_Basic[0].value);
printf("__ASMAC = %s\n", Para_Basic[0].value);

Para_Basic[1].para = "__ASIP";
memset(s, 0, 30);
Expand Down
199 changes: 102 additions & 97 deletions examples/webserver-ch32v307-none-os/lib/NetLib/eth_driver_10M.c
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,31 @@ void ETH_SetClock(void)
while(RESET == RCC_GetFlagStatus(RCC_FLAG_PLL3RDY));
}

/*********************************************************************
* @fn ETH_LinkUpCfg
*
* @brief When the PHY is connected, configure the relevant functions.
*
* @param regval BMSR register value
*
* @return none.
*/
void ETH_LinkUpCfg(uint16_t regval)
{
WCHNET_PhyStatus( regval );
ETH->MACCR &= ~(ETH_Speed_100M|ETH_Speed_1000M);
phyStatus = PHY_Linked_Status;

/* disable Filter function */
ETH->MACFFR |= (ETH_ReceiveAll_Enable | ETH_PromiscuousMode_Enable);

ETH->MMCCR |= ETH_MMCCR_CR; //Counters Reset
while(ETH->MMCCR & ETH_MMCCR_CR); //Wait for counters reset to complete
PhyPolarityDetect = 1;
LinkSuccTime = LocalTime;
ETH_Start( );
}

/*********************************************************************
* @fn ETH_PHYLink
*
Expand All @@ -428,52 +453,63 @@ void ETH_SetClock(void)
*/
void ETH_PHYLink( void )
{
u32 phy_stat;
u16 phy_anlpar;
u16 phy_bsr, phy_stat, phy_anlpar, phy_bcr;

phy_bsr = ETH_ReadPHYRegister( gPHYAddress, PHY_BSR);
phy_bcr = ETH_ReadPHYRegister( gPHYAddress, PHY_BCR);
phy_anlpar = ETH_ReadPHYRegister( gPHYAddress, PHY_ANLPAR);
phy_stat = ETH_ReadPHYRegister( gPHYAddress, PHY_BSR);

if((phy_stat&(PHY_Linked_Status))&&(phy_anlpar == 0)){ /* restart negotiation */
EXTEN->EXTEN_CTR &= ~EXTEN_ETH_10M_EN;
phyLinkReset = 1;
phyLinkTime = LocalTime;
return;
}
WCHNET_PhyStatus( phy_stat );

if( (phy_stat&PHY_Linked_Status) && (phy_stat&PHY_AutoNego_Complete) )
if(phy_bsr & PHY_Linked_Status) //LinkUp
{
phy_stat = ETH_ReadPHYRegister( gPHYAddress, PHY_STATUS );
if( phy_stat & (1<<2) )
if(phy_bcr & PHY_AutoNegotiation) //determine whether auto-negotiation is enable
{
ETH->MACCR |= ETH_Mode_FullDuplex;
}
else
{
if( (phy_anlpar&PHY_ANLPAR_SELECTOR_FIELD) != PHY_ANLPAR_SELECTOR_VALUE )
if(phy_anlpar == 0)
{
ETH->MACCR |= ETH_Mode_FullDuplex;
if(phy_bsr & PHY_AutoNego_Complete)
{
ETH->MACCR &= ~ETH_Mode_FullDuplex;
ETH_LinkUpCfg(phy_bsr);
}
else{
PHY_LINK_RESET();
}
}
else
{
ETH->MACCR &= ~ETH_Mode_FullDuplex;
else {
if(phy_bsr & PHY_AutoNego_Complete)
{
phy_stat = ETH_ReadPHYRegister( gPHYAddress, PHY_STATUS );
if( phy_stat & (1<<2) )
{
ETH->MACCR |= ETH_Mode_FullDuplex;
}
else
{
if( (phy_anlpar&PHY_ANLPAR_SELECTOR_FIELD) != PHY_ANLPAR_SELECTOR_VALUE )
{
ETH->MACCR |= ETH_Mode_FullDuplex;
}
else
{
ETH->MACCR &= ~ETH_Mode_FullDuplex;
}
}
ETH_LinkUpCfg(phy_bsr);
}
else{
WCHNET_PhyStatus( phy_bsr );
EXTEN->EXTEN_CTR &= ~EXTEN_ETH_10M_EN;
phyLinkReset = 1;
phyLinkTime = LocalTime;
}
}
}
ETH->MACCR &= ~(ETH_Speed_100M|ETH_Speed_1000M);
phyStatus = PHY_Linked_Status;

/* disable Filter function */
ETH->MACFFR |= (ETH_ReceiveAll_Enable | ETH_PromiscuousMode_Enable);

ETH->MMCCR |= ETH_MMCCR_CR; //Counters Reset
while(ETH->MMCCR & ETH_MMCCR_CR); //Wait for counters reset to complete
PhyPolarityDetect = 1;
LinkSuccTime = LocalTime;
ETH_Start( );
else {
ETH->MACCR &= ~ETH_Mode_FullDuplex;
ETH_LinkUpCfg(phy_bsr);
}
}
else
{
else { //LinkDown
WCHNET_PhyStatus( phy_bsr );
EXTEN->EXTEN_CTR &= ~EXTEN_ETH_10M_EN;
phyLinkReset = 1;
phyLinkTime = LocalTime;
Expand Down Expand Up @@ -517,29 +553,27 @@ void ReInitMACReg(void)
/*------------------------ MAC -----------------------------------*/
ETH_InitStructure.ETH_Mode = ETH_Mode_FullDuplex;
ETH_InitStructure.ETH_Speed = ETH_Speed_10M;
#if HARDWARE_CHECKSUM_CONFIG
#if HARDWARE_CHECKSUM_CONFIG
ETH_InitStructure.ETH_ChecksumOffload = ETH_ChecksumOffload_Enable;
#endif
#endif
ETH_InitStructure.ETH_AutoNegotiation = ETH_AutoNegotiation_Enable;
ETH_InitStructure.ETH_LoopbackMode = ETH_LoopbackMode_Disable;
ETH_InitStructure.ETH_RetryTransmission = ETH_RetryTransmission_Disable;
ETH_InitStructure.ETH_AutomaticPadCRCStrip = ETH_AutomaticPadCRCStrip_Disable;
/* Filter function configuration */
ETH_InitStructure.ETH_ReceiveAll = ETH_ReceiveAll_Disable;
ETH_InitStructure.ETH_BroadcastFramesReception = ETH_BroadcastFramesReception_Enable;
ETH_InitStructure.ETH_PromiscuousMode = ETH_PromiscuousMode_Disable;
ETH_InitStructure.ETH_BroadcastFramesReception = ETH_BroadcastFramesReception_Enable;
ETH_InitStructure.ETH_MulticastFramesFilter = ETH_MulticastFramesFilter_Perfect;
ETH_InitStructure.ETH_UnicastFramesFilter = ETH_UnicastFramesFilter_Perfect;
/*------------------------ DMA -----------------------------------*/
/* When we use the Checksum offload feature, we need to enable the Store and Forward mode:
the store and forward guarantee that a whole frame is stored in the FIFO, so the MAC can insert/verify the checksum,
if the checksum is OK the DMA can handle the frame otherwise the frame is dropped */
ETH_InitStructure.ETH_DropTCPIPChecksumErrorFrame = ETH_DropTCPIPChecksumErrorFrame_Enable;
ETH_InitStructure.ETH_ReceiveStoreForward = ETH_ReceiveStoreForward_Enable;
ETH_InitStructure.ETH_TransmitStoreForward = ETH_TransmitStoreForward_Enable;
ETH_InitStructure.ETH_ForwardErrorFrames = ETH_ForwardErrorFrames_Enable;
ETH_InitStructure.ETH_ForwardUndersizedGoodFrames = ETH_ForwardUndersizedGoodFrames_Enable;
ETH_InitStructure.ETH_SecondFrameOperate = ETH_SecondFrameOperate_Disable;
/* Configure Ethernet */
/*---------------------- Physical layer configuration -------------------*/
/* Set the SMI interface clock, set as the main frequency divided by 42 */
Expand All @@ -551,33 +585,24 @@ void ReInitMACReg(void)
/*------------------------ MAC register configuration ----------------------- --------------------*/
tmpreg = ETH->MACCR;
tmpreg &= MACCR_CLEAR_MASK;
tmpreg |= (uint32_t)(ETH_InitStructure.ETH_AutoNegotiation |
ETH_InitStructure.ETH_Watchdog |
ETH_InitStructure.ETH_Jabber |
ETH_InitStructure.ETH_InterFrameGap |
ETH_InitStructure.ETH_CarrierSense |
ETH_InitStructure.ETH_Speed |
ETH_InitStructure.ETH_ReceiveOwn |
ETH_InitStructure.ETH_LoopbackMode |
ETH_InitStructure.ETH_Mode |
ETH_InitStructure.ETH_ChecksumOffload |
ETH_InitStructure.ETH_RetryTransmission |
ETH_InitStructure.ETH_AutomaticPadCRCStrip |
ETH_InitStructure.ETH_BackOffLimit |
ETH_InitStructure.ETH_DeferralCheck);
tmpreg |= (uint32_t)(ETH_InitStructure.ETH_Watchdog |
ETH_InitStructure.ETH_Jabber |
ETH_InitStructure.ETH_InterFrameGap |
ETH_InitStructure.ETH_ChecksumOffload |
ETH_InitStructure.ETH_AutomaticPadCRCStrip |
ETH_InitStructure.ETH_DeferralCheck |
(1 << 20));
/* Write MAC Control Register */
ETH->MACCR = (uint32_t)tmpreg;
#if( PHY_MODE == USE_10M_BASE )
ETH->MACCR |= ETH_Internal_Pull_Up_Res_Enable; /*Turn on the internal pull-up resistor*/
#endif
ETH->MACFFR = (uint32_t)(ETH_InitStructure.ETH_ReceiveAll |
ETH_InitStructure.ETH_SourceAddrFilter |
ETH_InitStructure.ETH_PassControlFrames |
ETH_InitStructure.ETH_BroadcastFramesReception |
ETH_InitStructure.ETH_DestinationAddrFilter |
ETH_InitStructure.ETH_PromiscuousMode |
ETH_InitStructure.ETH_MulticastFramesFilter |
ETH_InitStructure.ETH_UnicastFramesFilter);
ETH_InitStructure.ETH_SourceAddrFilter |
ETH_InitStructure.ETH_PassControlFrames |
ETH_InitStructure.ETH_BroadcastFramesReception |
ETH_InitStructure.ETH_DestinationAddrFilter |
ETH_InitStructure.ETH_PromiscuousMode |
ETH_InitStructure.ETH_MulticastFramesFilter |
ETH_InitStructure.ETH_UnicastFramesFilter);
/*--------------- ETHERNET MACHTHR and MACHTLR Configuration ---------------*/
/* Write to ETHERNET MACHTHR */
ETH->MACHTHR = (uint32_t)ETH_InitStructure.ETH_HashTableHigh;
Expand All @@ -589,29 +614,24 @@ void ReInitMACReg(void)
/* Clear xx bits */
tmpreg &= MACFCR_CLEAR_MASK;
tmpreg |= (uint32_t)((ETH_InitStructure.ETH_PauseTime << 16) |
ETH_InitStructure.ETH_ZeroQuantaPause |
ETH_InitStructure.ETH_PauseLowThreshold |
ETH_InitStructure.ETH_UnicastPauseFrameDetect |
ETH_InitStructure.ETH_ReceiveFlowControl |
ETH_InitStructure.ETH_TransmitFlowControl);
ETH_InitStructure.ETH_UnicastPauseFrameDetect |
ETH_InitStructure.ETH_ReceiveFlowControl |
ETH_InitStructure.ETH_TransmitFlowControl);
ETH->MACFCR = (uint32_t)tmpreg;

ETH->MACVLANTR = (uint32_t)(ETH_InitStructure.ETH_VLANTagComparison |
ETH_InitStructure.ETH_VLANTagIdentifier);
ETH_InitStructure.ETH_VLANTagIdentifier);

tmpreg = ETH->DMAOMR;
tmpreg &= DMAOMR_CLEAR_MASK;
tmpreg |= (uint32_t)(ETH_InitStructure.ETH_DropTCPIPChecksumErrorFrame |
ETH_InitStructure.ETH_ReceiveStoreForward |
ETH_InitStructure.ETH_FlushReceivedFrame |
ETH_InitStructure.ETH_TransmitStoreForward |
ETH_InitStructure.ETH_TransmitThresholdControl |
ETH_InitStructure.ETH_ForwardErrorFrames |
ETH_InitStructure.ETH_ForwardUndersizedGoodFrames |
ETH_InitStructure.ETH_ReceiveThresholdControl |
ETH_InitStructure.ETH_SecondFrameOperate);
ETH_InitStructure.ETH_FlushReceivedFrame |
ETH_InitStructure.ETH_TransmitStoreForward |
ETH_InitStructure.ETH_ForwardErrorFrames |
ETH_InitStructure.ETH_ForwardUndersizedGoodFrames);
ETH->DMAOMR = (uint32_t)tmpreg;


/* Configure MAC address */
ETH->MACA0HR = (uint32_t)((MACAddr[5]<<8) | MACAddr[4]);
ETH->MACA0LR = (uint32_t)(MACAddr[0] | (MACAddr[1]<<8) | (MACAddr[2]<<16) | (MACAddr[3]<<24));
Expand Down Expand Up @@ -680,20 +700,13 @@ uint32_t ETH_RegInit( ETH_InitTypeDef* ETH_InitStruct, uint16_t PHYAddress )
/*------------------------ MAC register configuration ----------------------- --------------------*/
tmpreg = ETH->MACCR;
tmpreg &= MACCR_CLEAR_MASK;
tmpreg |= (uint32_t)(ETH_InitStruct->ETH_AutoNegotiation |
ETH_InitStruct->ETH_Watchdog |
tmpreg |= (uint32_t)(ETH_InitStruct->ETH_Watchdog |
ETH_InitStruct->ETH_Jabber |
ETH_InitStruct->ETH_InterFrameGap |
ETH_InitStruct->ETH_CarrierSense |
ETH_InitStruct->ETH_Speed |
ETH_InitStruct->ETH_ReceiveOwn |
ETH_InitStruct->ETH_LoopbackMode |
ETH_InitStruct->ETH_Mode |
ETH_InitStruct->ETH_ChecksumOffload |
ETH_InitStruct->ETH_RetryTransmission |
ETH_InitStruct->ETH_AutomaticPadCRCStrip |
ETH_InitStruct->ETH_BackOffLimit |
ETH_InitStruct->ETH_DeferralCheck);
ETH_InitStruct->ETH_DeferralCheck |
(1 << 20));
/* Write MAC Control Register */
ETH->MACCR = (uint32_t)tmpreg;
ETH->MACCR |= ETH_Internal_Pull_Up_Res_Enable; /*Turn on the internal pull-up resistor*/
Expand All @@ -716,8 +729,6 @@ uint32_t ETH_RegInit( ETH_InitTypeDef* ETH_InitStruct, uint16_t PHYAddress )
/* Clear xx bits */
tmpreg &= MACFCR_CLEAR_MASK;
tmpreg |= (uint32_t)((ETH_InitStruct->ETH_PauseTime << 16) |
ETH_InitStruct->ETH_ZeroQuantaPause |
ETH_InitStruct->ETH_PauseLowThreshold |
ETH_InitStruct->ETH_UnicastPauseFrameDetect |
ETH_InitStruct->ETH_ReceiveFlowControl |
ETH_InitStruct->ETH_TransmitFlowControl);
Expand All @@ -729,14 +740,10 @@ uint32_t ETH_RegInit( ETH_InitTypeDef* ETH_InitStruct, uint16_t PHYAddress )
tmpreg = ETH->DMAOMR;
tmpreg &= DMAOMR_CLEAR_MASK;
tmpreg |= (uint32_t)(ETH_InitStruct->ETH_DropTCPIPChecksumErrorFrame |
ETH_InitStruct->ETH_ReceiveStoreForward |
ETH_InitStruct->ETH_FlushReceivedFrame |
ETH_InitStruct->ETH_TransmitStoreForward |
ETH_InitStruct->ETH_TransmitThresholdControl |
ETH_InitStruct->ETH_ForwardErrorFrames |
ETH_InitStruct->ETH_ForwardUndersizedGoodFrames |
ETH_InitStruct->ETH_ReceiveThresholdControl |
ETH_InitStruct->ETH_SecondFrameOperate);
ETH_InitStruct->ETH_ForwardUndersizedGoodFrames);
ETH->DMAOMR = (uint32_t)tmpreg;

/* Reset the physical layer */
Expand Down Expand Up @@ -805,11 +812,9 @@ void ETH_Configuration( uint8_t *macAddr )
the store and forward guarantee that a whole frame is stored in the FIFO, so the MAC can insert/verify the checksum,
if the checksum is OK the DMA can handle the frame otherwise the frame is dropped */
ETH_InitStructure.ETH_DropTCPIPChecksumErrorFrame = ETH_DropTCPIPChecksumErrorFrame_Enable;
ETH_InitStructure.ETH_ReceiveStoreForward = ETH_ReceiveStoreForward_Enable;
ETH_InitStructure.ETH_TransmitStoreForward = ETH_TransmitStoreForward_Enable;
ETH_InitStructure.ETH_ForwardErrorFrames = ETH_ForwardErrorFrames_Enable;
ETH_InitStructure.ETH_ForwardUndersizedGoodFrames = ETH_ForwardUndersizedGoodFrames_Enable;
ETH_InitStructure.ETH_SecondFrameOperate = ETH_SecondFrameOperate_Disable;
/* Configure Ethernet */
ETH_RegInit( &ETH_InitStructure, gPHYAddress );

Expand Down
Loading

0 comments on commit 1c8f532

Please sign in to comment.