| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- /*
- * FreeModbus Libary: BARE Port
- * Copyright (C) 2006 Christian Walter <wolti@sil.at>
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
- *
- * File: $Id$
- */
- #include "port.h"
- /* ----------------------- Modbus includes ----------------------------------*/
- #include "mb.h"
- #include "mbport.h"
- #include "gd32e23x_usart.h"
- #include "gd32e23x_gpio.h"
- #include "gd32e23x_rcu.h"
- #include "gd32e23x_misc.h"
- #include "gd32e23x_it.h"
- /* ----------------------- static functions ---------------------------------*/
- static void prvvUARTTxReadyISR( void );
- static void prvvUARTRxISR( void );
- /* ----------------------- Start implementation -----------------------------*/
- void
- vMBPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable )
- {
- /* If xRXEnable enable serial receive interrupts. If xTxENable enable
- * transmitter empty interrupts.
- */
- if(TRUE == xRxEnable)
- {
- usart_interrupt_enable(USART0,USART_INT_RBNE);//使能接收中断
- gpio_bit_reset(GPIOA, GPIO_PIN_11); //接收
- }
- else
- {
- usart_interrupt_disable(USART0,USART_INT_RBNE);//失能接收中断
- gpio_bit_set(GPIOA, GPIO_PIN_11); //恢复发送
- }
-
- if(TRUE == xTxEnable)
- {
- usart_interrupt_enable(USART0,USART_INT_TC); //使能发送中断
- gpio_bit_set(GPIOA, GPIO_PIN_11); //发送
- }
- else
- {
- usart_interrupt_disable(USART0,USART_INT_TC); //使能发送完成中断
- gpio_bit_reset(GPIOA, GPIO_PIN_11); //发送
- }
- }
- BOOL
- xMBPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity )
- {
-
- nvic_irq_enable(USART0_IRQn, 0); //使能串口中断,设置优先级
- rcu_periph_clock_enable(RCU_GPIOA); //使能GPIOA时钟
- //配置TX的GPIO
- gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_9); //复用PA9
- gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_9);
- gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, GPIO_PIN_9);
-
- //配置RX的GPIO
- gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_10); //复用PA10
- gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_10);
- gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, GPIO_PIN_10);
-
- //配置TX_RX的切换
- gpio_mode_set(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_11); //设置GPIO模式
- gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_11); //设置GPIO输出模式及速度
-
- gpio_bit_reset(GPIOA, GPIO_PIN_11);
- //配置USART的参数
- rcu_periph_clock_enable(RCU_USART0); //使能串口时钟
- usart_deinit(USART0); //RCU配置恢复默认值
- usart_baudrate_set(USART0, ulBaudRate); //设置波特率
-
- usart_stop_bit_set(USART0,USART_STB_1BIT); //设置停止位
- usart_word_length_set(USART0,USART_WL_8BIT); //设置数据字长度
- usart_parity_config(USART0,USART_PM_NONE); //设置奇偶校验位
-
- usart_receive_config(USART0, USART_RECEIVE_ENABLE); //使能接收
- usart_transmit_config(USART0, USART_TRANSMIT_ENABLE); //使能发送
- usart_enable(USART0); //使能串口
-
-
-
- return TRUE;
- //return FALSE;
- }
- BOOL
- xMBPortSerialPutByte( CHAR ucByte )
- {
- /* Put a byte in the UARTs transmit buffer. This function is called
- * by the protocol stack if pxMBFrameCBTransmitterEmpty( ) has been
- * called. */
- usart_data_transmit(USART0,ucByte);
- return TRUE;
- }
- BOOL
- xMBPortSerialGetByte( CHAR * pucByte )
- {
- /* Return the byte in the UARTs receive buffer. This function is called
- * by the protocol stack after pxMBFrameCBByteReceived( ) has been called.
- */
- *pucByte = usart_data_receive(USART0);
- return TRUE;
- }
- void USART0_IRQHandler(void)
- {
- if(usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE) != RESET) //接收缓冲区非空中断
- {
- prvvUARTRxISR();
- usart_interrupt_flag_clear(USART0, USART_INT_FLAG_RBNE); //清除USART1中断挂起
- }
-
- if(usart_flag_get(USART0, USART_FLAG_ORERR) == SET) //溢出错误标志为1
- {
- usart_flag_clear(USART0, USART_FLAG_ORERR); //清除溢出错误标志
- prvvUARTRxISR();
- }
- if(usart_interrupt_flag_get(USART0, USART_INT_FLAG_TBE)!= RESET) //发送缓冲区空中断
- {
- prvvUARTTxReadyISR();
- usart_interrupt_flag_clear(USART0, USART_INT_FLAG_TBE); //清除发送中断标志
- }
- if(usart_interrupt_flag_get(USART0, USART_INT_FLAG_TC)!= RESET) //发送完成中断
- {
- prvvUARTTxReadyISR();
- usart_interrupt_flag_clear(USART0, USART_INT_FLAG_TC); //清除发送完成中断
- // gpio_bit_reset(GPIOA, GPIO_PIN_8); //接收
- }
- }
- /* Create an interrupt handler for the transmit buffer empty interrupt
- * (or an equivalent) for your target processor. This function should then
- * call pxMBFrameCBTransmitterEmpty( ) which tells the protocol stack that
- * a new character can be sent. The protocol stack will then call
- * xMBPortSerialPutByte( ) to send the character.
- */
- static void prvvUARTTxReadyISR( void )
- {
- pxMBFrameCBTransmitterEmpty( );
- }
- /* Create an interrupt handler for the receive interrupt for your target
- * processor. This function should then call pxMBFrameCBByteReceived( ). The
- * protocol stack will then call xMBPortSerialGetByte( ) to retrieve the
- * character.
- */
- static void prvvUARTRxISR( void )
- {
- pxMBFrameCBByteReceived( );
- }
|