portserial.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * File : portserial.c
  3. * This file is part of freemodbus in RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2010-04-04 yi.qiu first version
  13. */
  14. #include <rtthread.h>
  15. #include "port.h"
  16. #include "serial.h"
  17. /* ----------------------- Modbus includes ----------------------------------*/
  18. #include "mb.h"
  19. #include "mbport.h"
  20. #define UART1 ((struct uartport *)&U1BASE)
  21. /* ----------------------- static functions ---------------------------------*/
  22. static void rt_serial1_handler(int vector);
  23. static void prvvUARTTxReadyISR(void);
  24. static void prvvUARTRxISR(void);
  25. extern rt_uint32_t PCLK;
  26. /* ----------------------- Start implementation -----------------------------*/
  27. void vMBPortSerialEnable( BOOL xRxEnable, BOOL xTxEnable )
  28. {
  29. if( xRxEnable )
  30. {
  31. INTSUBMSK &= ~(BIT_SUB_RXD1);
  32. }
  33. else
  34. {
  35. INTSUBMSK |= (BIT_SUB_RXD1);
  36. }
  37. if( xTxEnable )
  38. {
  39. INTSUBMSK &= ~(BIT_SUB_TXD1);
  40. prvvUARTTxReadyISR();
  41. }
  42. else
  43. {
  44. INTSUBMSK |= (BIT_SUB_TXD1);
  45. }
  46. }
  47. void vMBPortClose( void )
  48. {
  49. /* null */
  50. }
  51. BOOL xMBPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity )
  52. {
  53. int i;
  54. /* FIFO enable, Tx/Rx FIFO clear */
  55. UART1->ufcon = 0x0;
  56. /* disable the flow control */
  57. UART1->umcon = 0x0;
  58. /* Normal,No parity,1 stop,8 bit */
  59. UART1->ulcon = 0x3;
  60. /*
  61. * tx=level,rx=edge,disable timeout int.,enable rx error int.
  62. * normal,interrupt or polling
  63. */
  64. UART1->ucon = 0x245;
  65. /* Set uart0 bps */
  66. UART1->ubrd = (rt_int32_t)(PCLK / (ulBaudRate * 16)) - 1;
  67. for (i = 0; i < 100; i++);
  68. SUBSRCPND |= BIT_SUB_RXD1;
  69. SUBSRCPND |= BIT_SUB_TXD1;
  70. rt_hw_interrupt_install(INTUART1, rt_serial1_handler, RT_NULL);
  71. rt_hw_interrupt_umask(INTUART1);
  72. return TRUE;
  73. }
  74. BOOL xMBPortSerialPutByte( CHAR ucByte )
  75. {
  76. /* wait for room in the transmit FIFO */
  77. while(!(USTAT1 & USTAT_TXB_EMPTY));
  78. UTXH1 = (UCHAR)ucByte;
  79. return TRUE;
  80. }
  81. BOOL xMBPortSerialGetByte( CHAR * pucByte )
  82. {
  83. while (!(USTAT1 & USTAT_RCV_READY));
  84. *pucByte = URXH1;
  85. return TRUE;
  86. }
  87. static void rt_serial1_handler(int vector)
  88. {
  89. if (SUBSRCPND & BIT_SUB_RXD1)
  90. {
  91. SUBSRCPND |= BIT_SUB_RXD1;
  92. prvvUARTRxISR();
  93. }
  94. else if (SUBSRCPND & BIT_SUB_TXD1)
  95. {
  96. SUBSRCPND |= BIT_SUB_TXD1;
  97. prvvUARTTxReadyISR();
  98. }
  99. }
  100. /*
  101. * Create an interrupt handler for the transmit buffer empty interrupt
  102. * (or an equivalent) for your target processor. This function should then
  103. * call pxMBFrameCBTransmitterEmpty( ) which tells the protocol stack that
  104. * a new character can be sent. The protocol stack will then call
  105. * xMBPortSerialPutByte( ) to send the character.
  106. */
  107. static void prvvUARTTxReadyISR( void )
  108. {
  109. pxMBFrameCBTransmitterEmpty( );
  110. }
  111. /*
  112. * Create an interrupt handler for the receive interrupt for your target
  113. * processor. This function should then call pxMBFrameCBByteReceived( ). The
  114. * protocol stack will then call xMBPortSerialGetByte( ) to retrieve the
  115. * character.
  116. */
  117. static void prvvUARTRxISR( void )
  118. {
  119. pxMBFrameCBByteReceived( );
  120. }