portserial_m.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * FreeModbus Libary: STM32 Port
  3. * Copyright (C) 2013 Armink <armink.ztl@gmail.com>
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. *
  19. * File: $Id: portserial_m.c,v 1.60 2013/08/13 15:07:05 Armink add Master Functions $
  20. */
  21. #include "port.h"
  22. /* ----------------------- Modbus includes ----------------------------------*/
  23. #include "mb.h"
  24. #include "mbport.h"
  25. #if MB_MASTER_RTU_ENABLED > 0 || MB_MASTER_ASCII_ENABLED
  26. /* ----------------------- static functions ---------------------------------*/
  27. static void prvvUARTTxReadyISR(void);
  28. static void prvvUARTRxISR(void);
  29. /* ----------------------- Start implementation -----------------------------*/
  30. void vMBMasterPortSerialEnable(BOOL xRxEnable, BOOL xTxEnable)
  31. {
  32. if (xRxEnable)
  33. {
  34. MASTER_RS485_RECEIVE_MODE;
  35. USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
  36. }
  37. else
  38. {
  39. MASTER_RS485_SEND_MODE;
  40. USART_ITConfig(USART2, USART_IT_RXNE, DISABLE);
  41. }
  42. if (xTxEnable)
  43. {
  44. USART_ITConfig(USART2, USART_IT_TXE, ENABLE);
  45. }
  46. else
  47. {
  48. USART_ITConfig(USART2, USART_IT_TXE, DISABLE);
  49. }
  50. }
  51. void vMBMasterPortClose(void)
  52. {
  53. USART_ITConfig(USART2, USART_IT_TXE | USART_IT_RXNE, DISABLE);
  54. USART_Cmd(USART2, DISABLE);
  55. }
  56. //默认一个主机 串口2 波特率可设置 奇偶检验可设置
  57. BOOL xMBMasterPortSerialInit(UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits,
  58. eMBParity eParity)
  59. {
  60. GPIO_InitTypeDef GPIO_InitStructure;
  61. USART_InitTypeDef USART_InitStructure;
  62. NVIC_InitTypeDef NVIC_InitStructure;
  63. //======================时钟初始化=======================================
  64. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
  65. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  66. //======================IO初始化=======================================
  67. //USART2_TX
  68. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  69. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  70. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  71. GPIO_Init(GPIOA, &GPIO_InitStructure);
  72. //USART2_RX
  73. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  74. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  75. GPIO_Init(GPIOA, &GPIO_InitStructure);
  76. //配置485发送和接收模式
  77. // TODO 暂时先写B13 等之后组网测试时再修改
  78. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  79. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
  80. GPIO_Init(GPIOB, &GPIO_InitStructure);
  81. //======================串口初始化=======================================
  82. USART_InitStructure.USART_BaudRate = ulBaudRate;
  83. //设置校验模式
  84. switch (eParity)
  85. {
  86. case MB_PAR_NONE: //无校验
  87. USART_InitStructure.USART_Parity = USART_Parity_No;
  88. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  89. break;
  90. case MB_PAR_ODD: //奇校验
  91. USART_InitStructure.USART_Parity = USART_Parity_Odd;
  92. USART_InitStructure.USART_WordLength = USART_WordLength_9b;
  93. break;
  94. case MB_PAR_EVEN: //偶校验
  95. USART_InitStructure.USART_Parity = USART_Parity_Even;
  96. USART_InitStructure.USART_WordLength = USART_WordLength_9b;
  97. break;
  98. default:
  99. return FALSE;
  100. }
  101. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  102. USART_InitStructure.USART_HardwareFlowControl =
  103. USART_HardwareFlowControl_None;
  104. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  105. if (ucPORT != 2)
  106. return FALSE;
  107. ENTER_CRITICAL_SECTION(); //关全局中断
  108. USART_Init(USART2, &USART_InitStructure);
  109. USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
  110. USART_Cmd(USART2, ENABLE);
  111. //=====================中断初始化======================================
  112. //设置NVIC优先级分组为Group2:0-3抢占式优先级,0-3的响应式优先级
  113. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  114. NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
  115. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  116. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  117. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  118. NVIC_Init(&NVIC_InitStructure);
  119. EXIT_CRITICAL_SECTION(); //开全局中断
  120. return TRUE;
  121. }
  122. BOOL xMBMasterPortSerialPutByte(CHAR ucByte)
  123. {
  124. USART_SendData(USART2, ucByte);
  125. return TRUE;
  126. }
  127. BOOL xMBMasterPortSerialGetByte(CHAR * pucByte)
  128. {
  129. *pucByte = USART_ReceiveData(USART2);
  130. return TRUE;
  131. }
  132. /*
  133. * Create an interrupt handler for the transmit buffer empty interrupt
  134. * (or an equivalent) for your target processor. This function should then
  135. * call pxMBFrameCBTransmitterEmpty( ) which tells the protocol stack that
  136. * a new character can be sent. The protocol stack will then call
  137. * xMBPortSerialPutByte( ) to send the character.
  138. */
  139. void prvvUARTTxReadyISR(void)
  140. {
  141. pxMBMasterFrameCBTransmitterEmpty();
  142. }
  143. /*
  144. * Create an interrupt handler for the receive interrupt for your target
  145. * processor. This function should then call pxMBFrameCBByteReceived( ). The
  146. * protocol stack will then call xMBPortSerialGetByte( ) to retrieve the
  147. * character.
  148. */
  149. void prvvUARTRxISR(void)
  150. {
  151. pxMBMasterFrameCBByteReceived();
  152. }
  153. /*******************************************************************************
  154. * Function Name : USART2_IRQHandler
  155. * Description : This function handles USART2 global interrupt request.
  156. * Input : None
  157. * Output : None
  158. * Return : None
  159. *******************************************************************************/
  160. void USART2_IRQHandler(void)
  161. {
  162. rt_interrupt_enter();
  163. //接收中断
  164. if (USART_GetITStatus(USART2, USART_IT_RXNE) == SET)
  165. {
  166. USART_ClearITPendingBit(USART2, USART_IT_RXNE);
  167. prvvUARTRxISR();
  168. }
  169. //发送中断
  170. if (USART_GetITStatus(USART2, USART_IT_TXE) == SET)
  171. {
  172. prvvUARTTxReadyISR();
  173. }
  174. rt_interrupt_leave();
  175. }
  176. #endif