123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371 |
- //==================================================================
- //
- // Filename: app_uart2.c
- // Description:
- //
- //------------------------------------------------------------------
- //
- // version:
- // data: 18:24, 05.Seb.2019, written by dwd
- // Description:
- //
- //------------------------------------------------------------------
- //==================================================================
- /*------------------------------------------------------------------------------
- * I N C L U D E
- *------------------------------------------------------------------------------*/
- #include <string.h>
- #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 */
- /*------------------------------------------------------------------------------
- * 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 BOOL s_Uart1TxbufEmpty(void);
- /*----------------------------------------------------------------------
- // Function uses : Uart1CfgInit
- // 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_GPIOA,ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
- GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_1);
- 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_OD;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- /* USART3 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);
- }
- /*----------------------------------------------------------------------
- // 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);
-
- #ifdef WDFJKEY_MAC_ADD
- 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;
- }
- #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);
- }
- }
- /*----------------------------------------------------------------------
- // 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);
- }
- /*------------------------------------------------------------------------------
- - 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][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);
- }
- /*------------------------------------------------------------------------------
- - 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;
- }
- /*=======================================================
- - 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);
- }
- /*------------------------------- E O F ----------------------------------------*/
|