//================================================================== // // Filename: app_uart2.c // Description: // //------------------------------------------------------------------ // // version: // data: 18:24, 05.Seb.2019, written by dwd // Description: // //------------------------------------------------------------------ //================================================================== /*------------------------------------------------------------------------------ * I N C L U D E *------------------------------------------------------------------------------*/ #include #include "stm32f0xx.h" #include "general.h" #include "main.h" #include "mapp_uart1.h" #include "mapp_pwr.h" #include "mapp_arm.h" #include "mapp_gpio.h" /*------------------------------------------------------------------------------ * D E F I N E *------------------------------------------------------------------------------*/ /*------------------------------------------------------------------------------ * S T R U C T *------------------------------------------------------------------------------*/ /*------------------------------------------------------------------------------ * S T A T I C - V A R I A B L E S *------------------------------------------------------------------------------*/ T_COM_UART1 s_tUart1Cfg = {0}; /* Com for DVD */ T_COM_UART1 s_tUart2Cfg = {0}; /*------------------------------------------------------------------------------ * G L O B A L *------------------------------------------------------------------------------*/ /*------------------------------------------------------------------------------ * P R O T O T Y P E *------------------------------------------------------------------------------*/ static void s_Uart1VarInit(void); static BOOL s_Uart1RxbufEmpty(void); static void s_Uart2VarInit(void); static BOOL s_Uart2RxbufEmpty(void); //static BOOL s_Uart1TxbufEmpty(void); /*---------------------------------------------------------------------- // Function uses : Uart1AndUart2CfgInit // Input parameter : // Output parameter : // Use Function : for DVD communication // Reserve date : 18:24, 05.Seb.2019, written by dwd ----------------------------------------------------------------------*/ //void Uart1CfgInit(void) //{ // GPIO_InitTypeDef GPIO_InitStructure; // USART_InitTypeDef USART_InitStructure = {0}; // /* Init variables ------------------------------------------------*/ // s_Uart1VarInit(); // /* Enable peripheral clocks ------------------------------------------------*/ // /* Enable USART Clock */ // RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB,ENABLE); // RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); // // // GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_0); //uart1——tx stm32f030C8 // GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_0); // // /* Configure pins as AF pushpull */ // GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;//TXRX // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; // GPIO_Init(GPIOA, &GPIO_InitStructure); // /* USART1 configuration ------------------------------------------------------*/ // USART_InitStructure.USART_BaudRate = 115200; // USART_InitStructure.USART_WordLength = USART_WordLength_8b; // USART_InitStructure.USART_StopBits = USART_StopBits_1; // USART_InitStructure.USART_Parity = USART_Parity_No; // USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; // USART_Init(USART1, &USART_InitStructure); // // /* Enable the USART */ // USART_Cmd(USART1, ENABLE); // /* Enable USART3 Receive and Transmit interrupts */ // USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); // USART_ITConfig(USART1, USART_IT_TC, ENABLE); // USART_ClearFlag(USART1, USART_FLAG_TC); // //} void Uart1CfgInit(void) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure = {0}; /* Init variables ------------------------------------------------*/ s_Uart1VarInit(); /* Enable peripheral clocks ------------------------------------------------*/ /* Enable USART Clock */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); // 应该是 GPIOB RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); /* Configure pins as AF pushpull */ GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_1); // uart1——tx -> PB6 GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_1); // uart1——rx -> PB7 /* Configure pins as AF pushpull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; // TX RX GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOB, &GPIO_InitStructure); // 应该是 GPIOB /* USART1 configuration ------------------------------------------------------*/ USART_InitStructure.USART_BaudRate = 115200; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USART1, &USART_InitStructure); /* Enable the USART */ USART_Cmd(USART1, ENABLE); /* Enable USART1 Receive and Transmit interrupts */ USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); USART_ITConfig(USART1, USART_IT_TC, ENABLE); USART_ClearFlag(USART1, USART_FLAG_TC); } void Uart2CfgInit(void) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure = {0}; /* Init variables ------------------------------------------------*/ s_Uart2VarInit(); /* Enable peripheral clocks ------------------------------------------------*/ /* Enable USART Clock */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_1); //uart2 stm32f030C8 GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1); /* Configure pins as AF pushpull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3;//TXRX GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOA, &GPIO_InitStructure); USART_InitStructure.USART_BaudRate = 115200; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USART2, &USART_InitStructure); /* Enable the USART */ USART_Cmd(USART2, ENABLE); /* Enable USART3 Receive and Transmit interrupts */ USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); USART_ITConfig(USART2, USART_IT_TC, ENABLE); USART_ClearFlag(USART2, USART_FLAG_TC); } /*---------------------------------------------------------------------- // Function uses : UartISRHandler // Input parameter : // Output parameter : // Use Function : // Reserve date : 18:24, 05.Seb.2019, written by dwd ----------------------------------------------------------------------*/ void Uart1ISRHandler(void) { T_COM_UART1 *pbuf; U08 rdata = 0; static U08 rstep = 0; pbuf = &s_tUart1Cfg; //------------------------------------------------------------------ // for Tx compelete interrupt routine // 19:50pm, 11.Oct.2012, modified by Sirius //------------------------------------------------------------------ if (USART_GetFlagStatus(USART1, USART_FLAG_TC) != RESET) { if (pbuf->TxCtr > 0) { /* See if buffer is empty */ pbuf->TxCtr--; /* No, decrement character count */ if (pbuf->TxDeque >= UART1_TX_BUF_SIZE) { /* Avoid bounding offset */ pbuf->TxDeque = 0; } USART_SendData(USART1, pbuf->BufTx[pbuf->TxDeque++]); /* Get character from buffer */ } else { pbuf->TxDeque = pbuf ->TxEnque; } USART_ClearFlag(USART1, USART_FLAG_TC); } //------------------------------------------------------------------ // for Rx not empty interrupt routine // 19:51pm, 11.Oct.2012, modified by Sirius //------------------------------------------------------------------ if (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) != RESET) { rdata = USART_ReceiveData(USART1); // USART2->DR = rdata; //发送到串口2 #ifdef WDFJKEY_MAC_ADD switch (rstep) { case 0: if (rdata == 0xFE) { rstep = 1; pbuf->BufRx[pbuf->RxInPag][0] = rdata; // gtArm.ledtestcnt = 10; } break; case 1: if (rdata < (UART1_RX_BUF_SIZE-2)&&(rdata!=0)) { pbuf->RxLen = rdata; pbuf->BufRx[pbuf->RxInPag][1] = rdata; //Lenght pbuf->RxCnt = 2; pbuf->RxLen += 2; rstep = 2; } else { rstep = 0; } break; case 2: pbuf->BufRx[pbuf->RxInPag][pbuf->RxCnt] = rdata; pbuf->RxCnt ++; if (pbuf->RxCnt >= pbuf->RxLen) { pbuf->RxInPag ++; if (pbuf->RxInPag >= UART1_RX_PAGE) { pbuf->RxInPag = 0; /* Wrap in pointer if page buffer full */ } rstep = 0; } break; default: rstep = 0; break; } #else switch (rstep) { case 0: if (rdata == '$') { rstep = 1; // gtArm.ledtestcnt = 10; } pbuf->RxCnt = 1; break; case 1: if (rdata == '#') { // gtArm.ledtestcnt = 30; pbuf->BufRx[pbuf->RxInPag][pbuf->RxCnt] = '#'; pbuf->BufRx[pbuf->RxInPag][0] = pbuf->RxCnt; //Lenght pbuf->RxInPag++; if (pbuf->RxInPag >= UART1_RX_PAGE) { pbuf->RxInPag = 0; /* Wrap in pointer if page buffer full */ } rstep = 0; } else if (rdata == '$') { pbuf->RxCnt = 1; } else { pbuf->BufRx[pbuf->RxInPag][pbuf->RxCnt] = rdata; pbuf->RxCnt++; } break; default: rstep = 0; break; } #endif // USART_ClearFlag(USART1, USART_FLAG_RXNE); // USART_SendData(USART2, rdata); //发送到串口2 // USART_ClearFlag(USART2, USART_FLAG_ORE); } } /*---------------------------------------------------------------------- // Function uses : UartISRHandler // Input parameter : // Output parameter : // Use Function : // Reserve date : 09:35, 2.Apr.2023, written by dwd ----------------------------------------------------------------------*/ void Uart2ISRHandler(void) { T_COM_UART1 *pbuf; U08 rdata = 0; static U08 rstep = 0; pbuf = &s_tUart2Cfg; //------------------------------------------------------------------ // for Tx compelete interrupt routine // 19:50pm, 11.Oct.2012, modified by Sirius //------------------------------------------------------------------ if (USART_GetFlagStatus(USART2, USART_FLAG_TC) != RESET) { if (pbuf->TxCtr > 0) { /* See if buffer is empty */ pbuf->TxCtr--; /* No, decrement character count */ if (pbuf->TxDeque >= UART1_TX_BUF_SIZE) { /* Avoid bounding offset */ pbuf->TxDeque = 0; } USART_SendData(USART2, pbuf->BufTx[pbuf->TxDeque++]); /* Get character from buffer */ } else { pbuf->TxDeque = pbuf ->TxEnque; } USART_ClearFlag(USART2, USART_FLAG_TC); } //------------------------------------------------------------------ // for Rx not empty interrupt routine // 19:51pm, 11.Oct.2012, modified by Sirius //------------------------------------------------------------------ if (USART_GetFlagStatus(USART2, USART_FLAG_RXNE) != RESET) { rdata = USART_ReceiveData(USART1); switch (rstep) { case 0: if (rdata == '$') { rstep = 1; // gtArm.ledtestcnt = 10; } pbuf->RxCnt = 1; break; case 1: if (rdata == HI_BYTE( gtArm.FJKeymac)) { rstep = 2 ; } else { rstep = 0; } break; case 2: if (rdata == LO_BYTE( gtArm.FJKeymac)) { rstep = 3; } else { rstep = 0; } break; case 3: if (rdata == '#') { // gtArm.ledtestcnt = 30; pbuf->BufRx[pbuf->RxInPag][pbuf->RxCnt] = '#'; pbuf->BufRx[pbuf->RxInPag][0] = pbuf->RxCnt; //Lenght pbuf->RxInPag++; if (pbuf->RxInPag >= UART1_RX_PAGE) { pbuf->RxInPag = 0; /* Wrap in pointer if page buffer full */ } rstep = 0; } else if (rdata == '$') { pbuf->RxCnt = 1; } else { pbuf->BufRx[pbuf->RxInPag][pbuf->RxCnt] = rdata; pbuf->RxCnt++; } break; default: rstep = 0; break; } USART_ClearFlag(USART2, USART_FLAG_RXNE); USART_SendData(USART1, rdata); //发送到串口1 USART_ClearFlag(USART1, USART_FLAG_ORE); } } /*---------------------------------------------------------------------- // Function name : Uart1CfgDisable // Input parameter : // Output parameter : // Use Function : // Reserve date : 18:24, 05.Seb.2019, written by dwd ----------------------------------------------------------------------*/ void Uart1CfgDisable(void) { /* Enable the USART */ USART_ITConfig(USART1,USART_IT_RXNE,DISABLE); USART_ITConfig(USART1,USART_IT_TC,DISABLE); USART_ClearFlag(USART1, USART_FLAG_TC); USART_Cmd(USART1, DISABLE); } void Uart2CfgDisable(void) { /* Enable the USART */ USART_ITConfig(USART2,USART_IT_RXNE,DISABLE); USART_ITConfig(USART2,USART_IT_TC,DISABLE); USART_ClearFlag(USART2, USART_FLAG_TC); USART_Cmd(USART2, DISABLE); } /*------------------------------------------------------------------------------ - Name: Uart1GetChar - DESCRIPTION: - To obtain a character from the communications channel. The function will wait for a character to be received on the serial channel or until the function times out. - - Input: 'to' is the amount of time (in clock ticks) that the calling function is willing to wait for a character to arrive. If you specify a timeout of 0, the function will wait forever for a character to arrive. - Output: - - modification history - -------------------- - 18:24, 05.Seb.2019, written by dwd * -------------------- -----------------------------------------------------------------------------*/ void Uart1GetChar(U08 *rbuf ,U16 to, U08 *err) { U08 i,len; T_COM_UART1 *pbuf; pbuf = &s_tUart1Cfg; /* Obtain pointer to communications channel */ if (!s_Uart1RxbufEmpty()) { *rbuf++ = pbuf->BufRx[pbuf->RxOutPag][0]; /* Copy data type */ len = pbuf->BufRx[pbuf->RxOutPag][1]; if (len > (UART1_RX_BUF_SIZE-2) ) { len = UART1_RX_BUF_SIZE -2; } *rbuf++ = len; /* Copy data length */ len ++; for (i = 0; i < len; i++) { /* Copy data to buffer */ *rbuf++ = pbuf->BufRx[pbuf->RxOutPag][i + 2]; } pbuf->RxOutPag++; if (pbuf->RxOutPag >= UART1_RX_PAGE) { /* Ready for next page */ pbuf->RxOutPag = 0; } *err = COM_NO_ERR; } else { /* See if characters received within timeout*/ *err = COM_RX_EMPTY; /* No, return error code */ } } void Uart2GetChar(U08 *rbuf ,U16 to, U08 *err) { U08 i,len; T_COM_UART1 *pbuf; pbuf = &s_tUart2Cfg; /* Obtain pointer to communications channel */ if (!s_Uart2RxbufEmpty()) { //*rbuf++ = pbuf->BufRx[pbuf->RxOutPag][0]; /* Copy data type */ len = pbuf->BufRx[pbuf->RxOutPag][0]; if (len > (UART1_RX_BUF_SIZE-2) ) { len = UART1_RX_BUF_SIZE -2; } *rbuf++ = len; /* Copy data length */ len ++; for (i = 0; i < len; i++) { /* Copy data to buffer */ *rbuf++ = pbuf->BufRx[pbuf->RxOutPag][i + 1]; } pbuf->RxOutPag++; if (pbuf->RxOutPag >= UART1_RX_PAGE) { /* Ready for next page */ pbuf->RxOutPag = 0; } *err = COM_NO_ERR; } else { /* See if characters received within timeout*/ *err = COM_RX_EMPTY; /* No, return error code */ } } U08 Uart1PutChar(U08 *c, U08 len) { BOOL newTras; T_COM_UART1 *pbuf; pbuf = &s_tUart1Cfg; /* Obtain pointer to communications channel */ if (len == 0) { return (COM_TX_EMPTY); } //if (len > s_Uart1TxbufLeft()) { /* Txbuffer left is not enough for this transmit */ // return (COM_TX_FULL); // } newTras = s_Uart1RxbufEmpty(); while (len--) { pbuf->TxCtr++; /* No, increment character count */ if (pbuf->TxEnque >= UART1_TX_BUF_SIZE) { /* Avoid bounding offset */ pbuf->TxEnque = 0; } pbuf->BufTx[pbuf->TxEnque++] = *c++; /* Put character into buffer */ } /* Tx first character to wake up Usart new transmit process */ if (newTras) { if (pbuf->TxCtr){ pbuf->TxCtr--; /* No, decrement character count */ } if (pbuf->TxDeque >= UART1_TX_BUF_SIZE) { /* Avoid bounding offset */ pbuf->TxDeque = 0; } USART_SendData(USART1, pbuf->BufTx[pbuf->TxDeque++]); /* Get character from buffer */ USART_ClearFlag(USART1, USART_FLAG_ORE); /* Clear TC flag in status register */ } return (COM_NO_ERR); } U08 Uart2PutChar(U08 *c, U08 len) { BOOL newTras; T_COM_UART1 *pbuf; pbuf = &s_tUart2Cfg; /* Obtain pointer to communications channel */ if (len == 0) { return (COM_TX_EMPTY); } //if (len > s_Uart1TxbufLeft()) { /* Txbuffer left is not enough for this transmit */ // return (COM_TX_FULL); // } newTras = s_Uart2RxbufEmpty(); while (len--) { pbuf->TxCtr++; /* No, increment character count */ if (pbuf->TxEnque >= UART1_TX_BUF_SIZE) { /* Avoid bounding offset */ pbuf->TxEnque = 0; } pbuf->BufTx[pbuf->TxEnque++] = *c++; /* Put character into buffer */ } /* Tx first character to wake up Usart new transmit process */ if (newTras) { if (pbuf->TxCtr){ pbuf->TxCtr--; /* No, decrement character count */ } if (pbuf->TxDeque >= UART1_TX_BUF_SIZE) { /* Avoid bounding offset */ pbuf->TxDeque = 0; } USART_SendData(USART2, pbuf->BufTx[pbuf->TxDeque++]); /* Get character from buffer */ USART_ClearFlag(USART2, USART_FLAG_ORE); /* Clear TC flag in status register */ } return (COM_NO_ERR); } /*------------------------------------------------------------------------------ - Name: Uart1VarInit - DESCRIPTION: - Initialize the ring buffer for COMM. Must call this function before calling any other functions. - - Input: - Output: - - modification history - -------------------- - 18:24, 05.Seb.2019, written by dwd * -------------------- -----------------------------------------------------------------------------*/ static void s_Uart1VarInit(void) { T_COM_UART1 *pbuf; pbuf = &s_tUart1Cfg; /* Obtain pointer to communications channel */ pbuf->RxInPag = 0; /* Initialize the ring buffer for COMM port */ pbuf->RxOutPag = 0; pbuf->RxLen = 0; pbuf->RxCnt = 0; } static void s_Uart2VarInit(void) { T_COM_UART1 *pbuf; pbuf = &s_tUart2Cfg; /* Obtain pointer to communications channel */ pbuf->RxInPag = 0; /* Initialize the ring buffer for COMM port */ pbuf->RxOutPag = 0; pbuf->RxLen = 0; pbuf->RxCnt = 0; } /*======================================================= - Name: s_Uart1RxbufEmpty - DESCRIPTION: - Judge whether rxbuf data valid. - - Input: - Output: - - modification history - -------------------- - 18:24, 05.Seb.2019, written by dwd * -------------------- =======================================================*/ static BOOL s_Uart1RxbufEmpty(void) { return (BOOL)(s_tUart1Cfg.RxInPag == s_tUart1Cfg.RxOutPag); } static BOOL s_Uart2RxbufEmpty(void) { return (BOOL)(s_tUart2Cfg.RxInPag == s_tUart2Cfg.RxOutPag); } /*------------------------------- E O F ----------------------------------------*/