portserial.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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.c,v 1.60 2013/08/13 15:07:05 Armink $
  20. */
  21. #include "port.h"
  22. /* ----------------------- Modbus includes ----------------------------------*/
  23. #include "mb.h"
  24. #include "mbport.h"
  25. /* ----------------------- static functions ---------------------------------*/
  26. static void prvvUARTTxReadyISR(void);
  27. static void prvvUARTRxISR(void);
  28. /* ----------------------- Start implementation -----------------------------*/
  29. void vMBPortSerialEnable(BOOL xRxEnable, BOOL xTxEnable)
  30. {
  31. if (xRxEnable)
  32. {
  33. SLAVE_RS485_RECEIVE_MODE;
  34. USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
  35. }
  36. else
  37. {
  38. SLAVE_RS485_SEND_MODE;
  39. USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);
  40. }
  41. if (xTxEnable)
  42. {
  43. USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
  44. }
  45. else
  46. {
  47. USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
  48. }
  49. }
  50. void vMBPortClose(void)
  51. {
  52. USART_ITConfig(USART1, USART_IT_TXE | USART_IT_RXNE, DISABLE);
  53. USART_Cmd(USART1, DISABLE);
  54. }
  55. //默认一个从机 串口1 波特率可设置 奇偶检验可设置
  56. BOOL xMBPortSerialInit(UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits,
  57. eMBParity eParity)
  58. {
  59. GPIO_InitTypeDef GPIO_InitStructure;
  60. USART_InitTypeDef USART_InitStructure;
  61. NVIC_InitTypeDef NVIC_InitStructure;
  62. //======================时钟初始化=======================================
  63. RCC_APB2PeriphClockCmd(
  64. RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_USART1,
  65. ENABLE);
  66. //======================IO初始化=======================================
  67. //USART1_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_9;
  71. GPIO_Init(GPIOA, &GPIO_InitStructure);
  72. //USART1_RX
  73. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  74. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  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 != 1)
  106. return FALSE;
  107. ENTER_CRITICAL_SECTION(); //关全局中断
  108. USART_Init(USART1, &USART_InitStructure);
  109. USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
  110. USART_Cmd(USART1, ENABLE);
  111. //=====================中断初始化======================================
  112. //设置NVIC优先级分组为Group2:0-3抢占式优先级,0-3的响应式优先级
  113. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  114. NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  115. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  116. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  117. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  118. NVIC_Init(&NVIC_InitStructure);
  119. EXIT_CRITICAL_SECTION(); //开全局中断
  120. return TRUE;
  121. }
  122. BOOL xMBPortSerialPutByte(CHAR ucByte)
  123. {
  124. USART_SendData(USART1, ucByte);
  125. return TRUE;
  126. }
  127. BOOL xMBPortSerialGetByte(CHAR * pucByte)
  128. {
  129. *pucByte = USART_ReceiveData(USART1);
  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. pxMBFrameCBTransmitterEmpty();
  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. pxMBFrameCBByteReceived();
  152. }
  153. /*******************************************************************************
  154. * Function Name : USART1_IRQHandler
  155. * Description : This function handles USART1 global interrupt request.
  156. * Input : None
  157. * Output : None
  158. * Return : None
  159. *******************************************************************************/
  160. void USART1_IRQHandler(void)
  161. {
  162. rt_interrupt_enter();
  163. //接收中断
  164. if (USART_GetITStatus(USART1, USART_IT_RXNE) == SET)
  165. {
  166. USART_ClearITPendingBit(USART1, USART_IT_RXNE);
  167. prvvUARTRxISR();
  168. }
  169. //发送中断
  170. if (USART_GetITStatus(USART1, USART_IT_TXE) == SET)
  171. {
  172. prvvUARTTxReadyISR();
  173. }
  174. rt_interrupt_leave();
  175. }