usart.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*-------------------------------------------------------------------------
  2. usart.h - USART communications module library header
  3. Copyright (C) 2005, Vangelis Rokas <vrokas AT otenet.gr>
  4. This library is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 2, or (at your option) any
  7. later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this library; see the file COPYING. If not, write to the
  14. Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
  15. MA 02110-1301, USA.
  16. As a special exception, if you link this library with other files,
  17. some of which are compiled with SDCC, to produce an executable,
  18. this library does not by itself cause the resulting executable to
  19. be covered by the GNU General Public License. This exception does
  20. not however invalidate any other reasons why the executable file
  21. might be covered by the GNU General Public License.
  22. -------------------------------------------------------------------------*/
  23. #ifndef __USART_H__
  24. #define __USART_H__
  25. #pragma library io
  26. #define RAM_SCLS __data
  27. /* configuration bit masks for open function */
  28. #define USART_TX_INT_ON 0xff
  29. #define USART_TX_INT_OFF 0x7f
  30. #define USART_RX_INT_ON 0xff
  31. #define USART_RX_INT_OFF 0xbf
  32. #define USART_BRGH_HIGH 0xff
  33. #define USART_BRGH_LOW 0xef
  34. #define USART_CONT_RX 0xff
  35. #define USART_SINGLE_RX 0xf7
  36. #define USART_SYNC_MASTER 0xff
  37. #define USART_SYNC_SLAVE 0xfb
  38. #define USART_NINE_BIT 0xff
  39. #define USART_EIGHT_BIT 0xfd
  40. #define USART_SYNCH_MODE 0xff
  41. #define USART_ASYNCH_MODE 0xfe
  42. /*
  43. * USART styles:
  44. *
  45. * --- Families with 1 USART ---
  46. *
  47. * INIT:
  48. * RCSTA<7> = 1 (SPEN)
  49. * TXSTA<4> = 0 (SYNC)
  50. * TXSTA<5> = 1 (TXEN)
  51. *
  52. * 18f1220:
  53. * RB1/AN5/TX and RB4/AN6/RX
  54. *
  55. * TRISB<1> = TRISB<4> = 1 (TX, RX)
  56. * ADCON1<5> = ADCON1<6> = 1 (PCFG<5>, PCFG<6>)
  57. * SPBRGH:SPBRG
  58. *
  59. * 18f13k50:
  60. * RB7/TX and RB5/AN11/RX
  61. *
  62. * TRISB<7> = TRISB<5> = 1 (TX, RX)
  63. * ANSELH<3> = 0 (ANS11/RX)
  64. * SPBRGH:SPBRG
  65. *
  66. * 18f2220:
  67. * RC6/TX and RC7/RX
  68. *
  69. * TRISC<6> = 0 (TX)
  70. * TRISC<7> = 1 (RX)
  71. * SPBRG
  72. *
  73. * 18f2221/18f2331/18f23k20/18f2410/18f2420/18f2423/18f2455/18f24j10/18f2525:
  74. * RC6/TX and RC7/RX
  75. *
  76. * TRISC<6> = TRISC<7> = 1 (TX, RX)
  77. * SPBRGH:SPBRG
  78. *
  79. * 18f2450/18f2480/18f2585/18f2682/18f6585/18f6680/18f8585/18f8680:
  80. * RC6/TX and RC7/RX
  81. *
  82. * TRISC<6> = 0 (TX)
  83. * TRISC<7> = 1 (RX)
  84. * SPBRGH:SPBRG
  85. *
  86. * --- Families with 2+ USARTs ---
  87. *
  88. * INIT:
  89. * RCSTA1<7> = 1 (SPEN)
  90. * TXSTA1<4> = 0 (SYNC)
  91. * TXSTA1<5> = 1 (TXEN)
  92. *
  93. * 18f24j50/18f6527/18f65j50/18f66j60:
  94. * RC6/TX1 and RC7/RX1 (EUSART1)
  95. *
  96. * TRISC<6> = 0 (TX1)
  97. * TRISC<7> = 1 (RX1)
  98. * SPBRGH1:SPBRG1
  99. *
  100. * 18f6520:
  101. * RC6/TX1 and RC7/RX1 (EUSART1)
  102. *
  103. * TRISC<6> = 0 (TX1)
  104. * TRISC<7> = 1 (RX1)
  105. * SPBRG1
  106. *
  107. */
  108. #include "pic18fam.h"
  109. #if (__SDCC_USART_STYLE == 0)
  110. #warning The target device is not supported by the SDCC PIC16 USART library.
  111. #endif
  112. #if (__SDCC_USART_STYLE == 1822200) || \
  113. (__SDCC_USART_STYLE == 1865200)
  114. #define __SDCC_NO_SPBRGH 1
  115. #endif /* device lacks SPBRGH */
  116. #if __SDCC_NO_SPBRGH
  117. typedef unsigned char sdcc_spbrg_t;
  118. #else /* !__SDCC_NO_SPBRGH */
  119. typedef unsigned int sdcc_spbrg_t;
  120. #endif /* !__SDCC_NO_SPBRGH */
  121. /* status bits */
  122. union USART
  123. {
  124. unsigned char val;
  125. struct
  126. {
  127. unsigned RX_NINE:1;
  128. unsigned TX_NINE:1;
  129. unsigned FRAME_ERROR:1;
  130. unsigned OVERRUN_ERROR:1;
  131. unsigned fill:4;
  132. };
  133. };
  134. void usart_open (unsigned char config, sdcc_spbrg_t spbrg) __wparam;
  135. void usart_close (void);
  136. unsigned char usart_busy (void) __naked;
  137. unsigned char usart_drdy (void) __naked;
  138. unsigned char usart_getc (void);
  139. void usart_gets (RAM_SCLS char * buffer, unsigned char len);
  140. void usart_putc (unsigned char data) __wparam __naked;
  141. void usart_puts (char * data);
  142. void usart_baud (sdcc_spbrg_t baudconfig) __wparam;
  143. #endif