From a39501aac863381545b1da759105d5b0d2f32984 Mon Sep 17 00:00:00 2001 From: QuakeGod <QuakeGod@sina.com> Date: 星期三, 01 二月 2023 11:08:04 +0800 Subject: [PATCH] KLink update KLink and Kbus --- Src/functions.c | 384 ++++++++++++++++++++++++++++++++++++++++++------------ 1 files changed, 298 insertions(+), 86 deletions(-) diff --git a/Src/functions.c b/Src/functions.c index 077904b..feedbc0 100644 --- a/Src/functions.c +++ b/Src/functions.c @@ -8,6 +8,9 @@ #include "functions.h" #include "string.h" #include "stm32f0xx_hal.h" +#if (BOARD_TYPE == 14) +#include "fp0.h" +#endif extern __IO uint32_t uwTick; //#include "Myprotocol.h" int TickFreq=10000; @@ -16,16 +19,16 @@ unsigned int TickPrioduS; // volatile unsigned int nCurTick=0; volatile unsigned int CurTickuS=0; -//volatile unsigned int ThisRunTime=0; //开机时间 -//volatile unsigned int TotalRunTime=0; //总开机时间 -//volatile unsigned int PwrCount=0; //开机次数 -unsigned short ClkuS; //每个Clk的nS数, +//volatile unsigned int ThisRunTime=0; //寮�鏈烘椂闂� +//volatile unsigned int TotalRunTime=0; //鎬诲紑鏈烘椂闂� +//volatile unsigned int PwrCount=0; //寮�鏈烘鏁� +unsigned short ClkuS; //姣忎釜Clk鐨刵S鏁帮紝 int InituS(int TickFreq1) { - TickPrioduS=1000000/TickFreq1; //每个SysTick的微秒数 - CoreClkMHz=HAL_RCC_GetHCLKFreq()/1000000; //=SystemCoreClock/1000000;每uS的时钟数 - TickPriodClk=SystemCoreClock/TickFreq1; //每个SysTick的时钟数 + TickPrioduS=1000000/TickFreq1; //姣忎釜SysTick鐨勫井绉掓暟 + CoreClkMHz=HAL_RCC_GetHCLKFreq()/1000000; //=SystemCoreClock/1000000;姣弖S鐨勬椂閽熸暟 + TickPriodClk=SystemCoreClock/TickFreq1; //姣忎釜SysTick鐨勬椂閽熸暟 ClkuS=(1000000LL*65536)/SystemCoreClock; CurTickuS=TickPrioduS+100u; return 0; @@ -51,6 +54,7 @@ // unsigned short Clk1=SysTick->VAL; return nCurTick; } + const unsigned short crc16_table[256] = { 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf, 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7, @@ -89,11 +93,11 @@ unsigned short crc_check(const unsigned char * data, unsigned int length) { unsigned short crc_reg = 0xFFFF; -while (length--) -{ -crc_reg = (crc_reg >> 8) ^ crc16_table[(crc_reg ^ *data++) & 0xff]; -} -return (~crc_reg) & 0xFFFF; + while (length--) + { + crc_reg = (crc_reg >> 8) ^ crc16_table[(crc_reg ^ *data++) & 0xff]; + } + return (~crc_reg) & 0xFFFF; } const uint16_t polynom = 0xA001; @@ -123,7 +127,7 @@ return(crc); } -/* Table of CRC values for high杘rder byte */ +/* Table of CRC values for high-order byte */ const uint8_t crctablehi[] = { 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, @@ -144,7 +148,7 @@ 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40 }; -/* Table of CRC values for low杘rder byte */ +/* Table of CRC values for low-order byte */ const uint8_t crctablelo[] = { 0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, @@ -180,26 +184,6 @@ return (crchi << 8 | crclo); } -const uint16_t crctalbeabs[] = { - 0x0000, 0xCC01, 0xD801, 0x1400, 0xF001, 0x3C00, 0x2800, 0xE401, - 0xA001, 0x6C00, 0x7800, 0xB401, 0x5000, 0x9C01, 0x8801, 0x4400 -}; - -uint16_t crc16tablefast(const uint8_t *ptr, uint16_t len) -{ - uint16_t crc = 0xffff; - uint16_t i; - uint8_t ch; - - for (i = 0; i < len; i++) { - ch = *ptr++; - crc = crctalbeabs[(ch ^ crc) & 15] ^ (crc >> 4); - crc = crctalbeabs[((ch >> 4) ^ crc) & 15] ^ (crc >> 4); - } - - return crc; -} - void modbuscrc16test() { printf("\n"); @@ -207,7 +191,7 @@ printf("-----------------------------------------------------------------------\n"); uint8_t crc16_data[] = { 0x01, 0x04, 0x04, 0x43, 0x6b, 0x58, 0x0e }; // expected crc value 0xD825. printf(" modbus crc16table test, expected value : 0xd825, calculate value : 0x%x\n", crc16table(crc16_data, sizeof(crc16_data))); - printf(" modbus crc16tablefast test, expected value : 0xd825, calculate value : 0x%x\n", crc16tablefast(crc16_data, sizeof(crc16_data))); +// printf(" modbus crc16tablefast test, expected value : 0xd825, calculate value : 0x%x\n", crc16tablefast(crc16_data, sizeof(crc16_data))); printf(" modbus crc16bitbybit test, expected value : 0xd825, calculate value : 0x%x\n", crc16bitbybit(crc16_data, sizeof(crc16_data))); } @@ -243,6 +227,19 @@ return 0; } +int Uart2RecvDMA(void * pBuf, int nSize) +{ + LL_DMA_DisableChannel(DMA1,LL_DMA_CHANNEL_5); + LL_DMA_ConfigAddresses(DMA1,LL_DMA_CHANNEL_5, (uint32_t)&USART2->RDR, + (uint32_t)pBuf, LL_DMA_DIRECTION_PERIPH_TO_MEMORY); + LL_DMA_SetDataLength(DMA1,LL_DMA_CHANNEL_5,nSize); + LL_DMA_EnableChannel(DMA1,LL_DMA_CHANNEL_5); + Uart2Stat.DMARecvLen=nSize; + Uart2Stat.Recving=1; + LL_DMA_EnableIT_TC(DMA1,LL_DMA_CHANNEL_5); + LL_USART_EnableDMAReq_RX(USART2); + return 0; +} int Uart2SendDMA(void * pData, int nSize) { LL_DMA_DisableChannel(DMA1,LL_DMA_CHANNEL_4); @@ -265,6 +262,90 @@ } return 0; } + +void TriggerPendSV() +{ + SCB->ICSR=SCB_ICSR_PENDSVSET_Msk; //1<<SCB_ICSR_PENDSVSET_Pos; +} + +void PendSvCallBack() +{ +#if (BOARD_TYPE == 14) +///* + if (bSPI1RecvDone) + { + bSPI1RecvDone=0; + ParseFP0Pkg(SPI1RecvBuf,nSPI1RecvLenInBuf); + } +//*/ +#endif + if (Uart2Stat.bPacketRecved) + { + KBusParsePacket(2, (pPacket)Uart2RecvBuf1, Uart2RecvBuf1DataLen); + Uart2RecvBuf1DataLen=0; + Uart2Stat.bPacketRecved=0; + Uart2RecvDMA(Uart2RecvBuf1,sizeof(Uart2RecvBuf1)); + } +} + +void SPI1_IRQ_CallBack() +{ + uint8_t value; +// LL_GPIO_TogglePin(GPIOB,LL_GPIO_PIN_7); + if (LL_SPI_IsActiveFlag_RXNE(SPI1)) + { + value = LL_SPI_ReceiveData8( SPI1); +#if (BOARD_TYPE == 14) + KMem.SDD[62]++; + KMem.SDT[122]++; + if (!bSPI1Sending) + { + KMem.SDB[128+KMem.SDT[123]] = value; + KMem.SDT[123]++; if (KMem.SDT[123]>=100) {KMem.SDT[123]=81;} + } + if (!bSPI1Sending && (1 || bSPI1Recving)) + { + SPI1RecvBuf[nSPI1RecvPos]=value; + nSPI1RecvPos++; + + if (value==0x0d) + { + KMem.SDB[128+KMem.SDT[123]] = nSPI1RecvPos; + KMem.SDT[123]++; if (KMem.SDT[123]>=100) {KMem.SDT[123]=81;} + + nSPI1RecvLenInBuf=nSPI1RecvPos; + bSPI1RecvDone=1; + nSPI1RecvPos=0; + bSPI1Recving=0; + + + TriggerPendSV(); + } + } + if (bSPI1Sending) + { + nSPI1SentLen++; + KMem.SDD[63]++; + + if (nSPI1SentLen >= nSPI1ToSendLen) { + SetACKPin_1(); + bSPI1Sending=0; + bSPI1SendDone=1; + bSPI1Recving=1; + nSPI1RecvPos=0; + SetFP0DEPin_0(); + } + else { + value = SPI1SendBuf[nSPI1SentLen]; + LL_SPI_TransmitData8(SPI1,value); + KMem.SDB[128+KMem.SDT[123]] = value; + KMem.SDT[123]++; if (KMem.SDT[123]>=100) {KMem.SDT[123]=81;} + } + } +#endif + } +} + void Uart1SendDone() { Uart1Stat.TcCount++; @@ -275,9 +356,12 @@ { Uart1Stat.IdelCount++; // NVIC_SetPendingIRQ(PendSV_IRQn); +// SCB->ICSR=SCB_ICSR_PENDSVSET_Msk; //1<<SCB_ICSR_PENDSVSET_Pos; + if (Uart1RecvBuf1DataLen >0) { Uart1Stat.bPacketRecved=1; +// SCB->ICSR=SCB_ICSR_PENDSVSET_Msk; //1<<SCB_ICSR_PENDSVSET_Pos; // KLParsePacket(Uart1RecvBuf1,Uart1RecvBuf1DataLen); // Uart1RecvBuf1DataLen=0; } @@ -289,19 +373,21 @@ } void Uart2RecvDone() { + Uart2RecvBuf1DataLen=sizeof(Uart2RecvBuf1) - LL_DMA_GetDataLength(DMA1,LL_DMA_CHANNEL_5); Uart2Stat.bPacketRecved=1; Uart2Stat.IdelCount++; - ParsePacket((pPacket)Uart2RecvBuf1,Uart2RecvBuf1DataLen); - Uart2RecvBuf1DataLen=0; + if (Uart2RecvBuf1DataLen>0) + TriggerPendSV(); + // ParsePacket((pPacket)Uart2RecvBuf1,Uart2RecvBuf1DataLen); } int PutStr(char * str1, int len1) { // Uart1SendDMA(str1,len1); - //PushIn(&Uart1Stat.QTx,str1,len1); + PushIn(&Uart1Stat.QTx,str1,len1); // LL_USART_EnableIT_TXE(USART1); // LL_USART_EnableIT_TC(USART1); - //Uart1TriggerSendDMA(); + Uart1TriggerSendDMA(); return len1; } int PutStr1(char * str1, int len1) @@ -315,29 +401,30 @@ } int PutStr2(char * str1, int len1) { -// Uart1SendDMA(str1,len1); - PushIn(&Uart2Stat.QTx,str1,len1); + Uart2SendDMA(str1,len1); +// PushIn(&Uart2Stat.QTx,str1,len1); // LL_USART_EnableIT_TXE(USART1); // LL_USART_EnableIT_TC(USART1); - Uart2TriggerSendDMA(); +// Uart2TriggerSendDMA(); return len1; } -void clearscreen() +int SendPacket(int nChn, void * pBuf,int len1) { - PutStr("\33[2J\33[0;0H",10); - return; + if (nChn==1) { + PutStr1((char *)pBuf,len1); +// PushIn(&Uart1Stat.QTx,p1,len1); +// Uart1TriggerSendDMA(); + Uart1Stat.SentPacket++; + }else if (nChn==2){ + PutStr2((char *)pBuf,len1); +// PushIn(&Uart2Stat.QTx,p1,len1); +// Uart2TriggerSendDMA(); + Uart2Stat.SentPacket++; + } + return len1; } - -void Locate(int y,int x) -{ - char str[16]; - int len; - len=sprintf(str," \33[%d;%dH",y,x); - PutStr(str,len); - return; -} - +/* int SendPacket1(void * pBuf,int len1) { PutStr1((char *)pBuf,len1); @@ -354,9 +441,18 @@ Uart2Stat.SentPacket++; return len1; } - +*/ void ToggleRunLed() { LL_GPIO_TogglePin(GPIOC,LL_GPIO_PIN_13);} void ToggleErrLed() { LL_GPIO_TogglePin(GPIOC,LL_GPIO_PIN_14);} +#if (BOARD_TYPE == 14) +void ToggleOutStat() { LL_GPIO_TogglePin(GPIOC,LL_GPIO_PIN_15);} + +void SetOutStat(uchar bOn) +{ + if (bOn) {LL_GPIO_SetOutputPin(GPIOC,LL_GPIO_PIN_15);} + else {LL_GPIO_ResetOutputPin(GPIOC,LL_GPIO_PIN_15);} +} +#else void ToggleOutStat() { LL_GPIO_TogglePin(GPIOA,LL_GPIO_PIN_11);} void SetOutStat(uchar bOn) @@ -364,6 +460,7 @@ if (bOn) {LL_GPIO_SetOutputPin(GPIOA,LL_GPIO_PIN_11);} else {LL_GPIO_ResetOutputPin(GPIOA,LL_GPIO_PIN_11);} } +#endif void SetRunLed(uchar bOn) { @@ -390,6 +487,7 @@ unsigned int Input165_8() { int x1=0; + __disable_irq(); set165SL_0(); set165SL_1(); unsigned int mask1=0x0080; volatile uint32_t * p1=&GPIOA->IDR; @@ -405,12 +503,14 @@ mask1>>=1; set165CLK_1(); } + __enable_irq(); return x1; } unsigned int Input165(int nBit) { int x1=0; + __disable_irq(); set165SL_0(); set165SL_1(); unsigned int mask1=1<<(nBit-1); volatile uint32_t * p1=&GPIOA->IDR; @@ -426,11 +526,12 @@ mask1>>=1; set165CLK_1(); } + __enable_irq(); return x1; } unsigned int Input165_R(int nBit) { - + __disable_irq(); set165SL_0(); set165SL_1(); int nBytes = nBit /8; volatile uint32_t * p1=&GPIOA->IDR; @@ -458,13 +559,14 @@ } rdata.Bytes[i]=x1; } - + __enable_irq(); return rdata.intvalue; } unsigned int Input165Cfg(int nBit) { int x1=0; + __disable_irq(); set165SL_0(); set165SL_1(); unsigned int mask1=1<<(nBit-1); volatile uint32_t * p1=&GPIOA->IDR; @@ -480,6 +582,7 @@ mask1>>=1; set165CLK_1(); } + __enable_irq(); return x1; } @@ -507,6 +610,14 @@ return Input165_R(16); case 10: return Input165_R(8); + case 11: + return Input165_R(8); + case 13: + return Input165_R(16); + case 14: + return 0; //FP0 + case 15: + return Input165_R(16); default: break; } @@ -559,6 +670,14 @@ return x2; } +int ReadConfig_11() +{ + uchar x2=0; + x2=((GPIOB->IDR&0x2)<<2); + x2= ((~x2)&0x8)|1; + return x2; +} + int ReadJumperSW() { int BoradType=GetBoardType(); @@ -582,19 +701,27 @@ return ReadConfig_5(); //New Slave 8 in 8 o case 9: case 10: - return ReadConfig_5(); //New Slave 8 in 8 o + return ReadConfig_5(); //New Master Slave 8 in 8 o + case 11: + return ReadConfig_11(); //Mini Board + case 13: + return ReadConfig_5(); + case 14: + return (~(LL_GPIO_ReadInputPort(GPIOA)>>4))&0x0f; //FP0 + case 15: + return ReadConfig_5(); //Wireless Master Slave 8 in 8 o default: return 0; } } -#define SRCLK_0() LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_13) -#define SRCLK_1() LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_13) -#define STRCLK_0() LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_12) -#define STRCLK_1() LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_12) -#define SER_0() LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_15) -#define SER_1() LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_15) +#define SRCLK2_0() LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_13) +#define SRCLK2_1() LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_13) +#define STRCLK2_0() LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_12) +#define STRCLK2_1() LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_12) +#define SER2_0() LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_15) +#define SER2_1() LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_15) void Enable595(uchar bEnable) { @@ -605,53 +732,135 @@ void Output595_8(unsigned int cc) { //unsigned char i; -;// 74HC595输出程序,输出8位 +;// 74HC595杈撳嚭绋嬪簭锛岃緭鍑�8浣� // cc=~0x3f; - STRCLK_1(); + __disable_irq(); + STRCLK2_1(); unsigned int mask1=0x0080; //volatile uint32_t * p1 = &GPIOB->BRR; //volatile uint32_t * p2 = &GPIOB->BSRR; for (;mask1;) { - SRCLK_0(); + SRCLK2_0(); //*p1=LL_GPIO_PIN_13; - if (cc&mask1) {SER_1();} - else {SER_0();} + if (cc&mask1) {SER2_1();} + else {SER2_0();} mask1>>=1; - SRCLK_1(); + SRCLK2_1(); // __nop(); //*p2=LL_GPIO_PIN_13; } - STRCLK_0(); - STRCLK_1(); + STRCLK2_0(); + STRCLK2_1(); + __enable_irq(); } + void Output595_16(unsigned int cc) { //unsigned char i; -;// 74HC595输出程序,输出8位 +;// 74HC595杈撳嚭绋嬪簭锛岃緭鍑�8浣� // cc=~0x3f; - STRCLK_1(); + __disable_irq(); + STRCLK2_1(); unsigned int mask1=0x8000; //volatile uint32_t * p1 = &GPIOB->BRR; //volatile uint32_t * p2 = &GPIOB->BSRR; for (;mask1;) { - SRCLK_0(); + SRCLK2_0(); //*p1=LL_GPIO_PIN_13; - if (cc&mask1) {SER_1();} - else {SER_0();} + if (cc&mask1) {SER2_1();} + else {SER2_0();} mask1>>=1; - SRCLK_1(); + SRCLK2_1(); // __nop(); //*p2=LL_GPIO_PIN_13; } - STRCLK_0(); - STRCLK_1(); + STRCLK2_0(); + STRCLK2_1(); + __enable_irq(); } -void PutOutput(unsigned int Y) { Output595_16(Y);} -#if (BOARD_TYPE == 9 || BOARD_TYPE == 10 ) +/* +#define STRCLK12_0() LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_7) +#define STRCLK12_1() LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_7) + +void PutOutputSPI1(unsigned int Y) +{ + __disable_irq(); + STRCLK12_1(); + LL_SPI_TransmitData8(SPI1,Y>>8); + int i=0; + while (LL_SPI_IsActiveFlag_TXE(SPI1) == RESET) + { + } + KMem.SDD[28]=i; + i=0; + while (LL_SPI_IsActiveFlag_BSY(SPI1) == SET) + { + i++; + } + LL_SPI_TransmitData8(SPI1,Y); + while (LL_SPI_IsActiveFlag_TXE(SPI1) == RESET) + { + } + KMem.SDD[28]=i; + i=0; + while (LL_SPI_IsActiveFlag_BSY(SPI1) == SET) + { + i++; + } + KMem.SDD[30]=i; + STRCLK12_0(); + __nop(); + STRCLK12_1(); + __enable_irq(); +} +*/ + +void PutOutputSPI2(unsigned int Y) +{ + __disable_irq(); + STRCLK2_1(); + LL_SPI_TransmitData8(SPI2,Y>>8); + int i=0; + while (LL_SPI_IsActiveFlag_TXE(SPI2) == RESET) + { + } + KMem.SDD[28]=i; + i=0; + while (LL_SPI_IsActiveFlag_BSY(SPI2) == SET) + { + i++; + } + LL_SPI_TransmitData8(SPI2,Y); + while (LL_SPI_IsActiveFlag_TXE(SPI2) == RESET) + { + } + KMem.SDD[28]=i; + i=0; + while (LL_SPI_IsActiveFlag_BSY(SPI2) == SET) + { + i++; + } + KMem.SDD[30]=i; + STRCLK2_0(); + STRCLK2_1(); + __enable_irq(); +} + +void PutOutput(unsigned int Y) +{ +#if (BOARD_TYPE == 14) + return ; +#endif + PutOutputSPI2(Y); + //Output595_16(Y); +} + +#if (BOARD_TYPE == 9 || BOARD_TYPE == 10 || BOARD_TYPE == 15 ) //#pragma message("9,10") + // V4.2 绠¤剼鎺掑垪鍚戝彸绉诲姩浜嗕竴浣嶃�� #define SRCLK1_0() LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_1) #define SRCLK1_1() LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_1) #define STRCLK1_0() LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_2) @@ -660,7 +869,7 @@ #define OE1_1() LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_10) #define SER1_0() LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_11) #define SER1_1() LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_11) -#else +#else //鎸夌収鍘熸潵鐨勭鑴氭帓鍒� #define SRCLK1_0() LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_0) #define SRCLK1_1() LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_0) #define STRCLK1_0() LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_1) @@ -677,11 +886,13 @@ if (bEnable) {OE1_0();} else {OE1_1();} } + void displayInput(unsigned int cc) { //unsigned char i; -;// 74HC595输出程序,输出8位 +;// 74HC595杈撳嚭绋嬪簭锛岃緭鍑�8浣� // cc=~0x3f; + __disable_irq(); STRCLK1_1(); unsigned int mask1=0x8000; //volatile uint32_t * p1 = &GPIOB->BRR; @@ -698,5 +909,6 @@ //*p2=LL_GPIO_PIN_13; } STRCLK1_0(); - STRCLK1_1(); + STRCLK1_1(); + __enable_irq(); } -- Gitblit v1.9.1