00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "config.h"
00018 #include "pic_utils.h"
00019 #include "pic_serial.h"
00020 #include <string.h>
00021 #include <stdlib.h>
00022
00024 uns8 tx_buffer[SERIAL_TX_BUFFER_SIZE];
00026 uns8 tx_start=0;
00028 uns8 tx_end=0;
00029
00031 uns8 rx_buffer[SERIAL_RX_BUFFER_SIZE];
00033 uns8 rx_start = 0;
00035 uns8 rx_end = 0;
00036
00037
00038
00039
00040 #ifdef SERIAL_DEBUG_ON
00041 uns8 rx_soft_overflow = 0;
00042 uns8 rx_hard_overflow = 0;
00043 uns8 rx_framing_error = 0;
00044 #endif
00045
00046
00047
00048 inline uns8 bin2Hex(uns8 x)
00049 {
00050 if (x < 10) {
00051 return '0' + x;
00052 } else {
00053 return 'A' -10 + x;
00054 }
00055 }
00056
00057
00058 void serial_setup(bit req_brgh, uns8 req_spbrg)
00059 {
00060 #ifdef _PIC16F88
00061 set_bit(trisb, 5);
00062 set_bit(trisb, 2);
00063 #define TRIS_SET
00064 #endif
00065 #ifdef _PIC16F876A
00066 set_bit(trisc,6);
00067 set_bit(trisc,7);
00068 #define TRIS_SET
00069 #endif
00070 #ifdef _PIC18F2620
00071 set_bit(trisc,6);
00072 set_bit(trisc,7);
00073 #define TRIS_SET
00074 #endif
00075 #ifdef _PIC18F4520
00076 set_bit(trisc,6);
00077 set_bit(trisc,7);
00078 #define TRIS_SET
00079 #endif
00080 #ifndef TRIS_SET
00081 #warning "You must set tris bits for serial use yourself, I don't know your pic"
00082 #warning "Please send your tris bits in so they can be included in the library"
00083 #endif
00084
00085 txsta.BRGH = req_brgh;
00086
00087 spbrg = req_spbrg;
00088
00089 clear_bit(txsta, SYNC);
00090 set_bit(rcsta, SPEN);
00091
00092
00093
00094 kill_interrupts();
00095
00096 clear_bit(txsta, TX9);
00097 clear_bit(txsta, TX9D);
00098
00099 set_bit(txsta, TXEN);
00100
00101
00102
00103 clear_bit(rcsta, RX9);
00104 clear_bit(rcsta, FERR);
00105
00106 _asm {
00107 MOVF _rcreg,W
00108 MOVF _rcreg,W
00109 MOVF _rcreg,W
00110 }
00111
00112 clear_bit(rcsta, CREN);
00113 set_bit(rcsta, CREN);
00114
00115 set_bit(pie1, RCIE);
00116
00117 }
00118
00119
00120
00121
00122 void serial_putc(uns8 c)
00123 {
00124 uns8 tx_next;
00125 bit my_store_gie;
00126 #ifdef SERIAL_IDE_DEBUG
00127 return;
00128 #endif
00129
00130 if ((tx_end == tx_start) &&
00131 test_bit(pir1, TXIF)) {
00132 txreg = c;
00133 } else {
00134 tx_next = tx_end + 1;
00135 if (tx_next == SERIAL_TX_BUFFER_SIZE) {
00136 tx_next = 0;
00137 }
00138 while (tx_next == tx_start);
00139
00140
00141
00142
00143 my_store_gie = intcon.GIE;
00144 kill_interrupts();
00145
00146 tx_buffer[tx_end] = c;
00147 tx_end = tx_next;
00148
00149 set_bit(pie1, TXIE);
00150 intcon.GIE = my_store_gie;
00151 }
00152 }
00153
00154
00155
00156 void serial_tx_isr()
00157 {
00158 uns8 tx_next;
00159
00160 if (tx_end == tx_start) {
00161 return;
00162 }
00163 tx_next = tx_start + 1;
00164 if (tx_next == SERIAL_TX_BUFFER_SIZE) {
00165 tx_next = 0;
00166 }
00167 if (tx_end == tx_next) {
00168 clear_bit(pie1, TXIE);
00169 }
00170 txreg = tx_buffer[tx_start];
00171 tx_start = tx_next;
00172
00173 }
00174
00175
00176 void serial_rx_isr()
00177 {
00178 uns8 rx_next;
00179
00180
00181 if (test_bit(rcsta, OERR)) {
00182 clear_bit(rcsta, CREN);
00183 _asm {
00184 MOVF _rcreg,W
00185 MOVF _rcreg,W
00186 MOVF _rcreg,W
00187 }
00188 #ifdef SERIAL_DEBUG_ON
00189 rx_hard_overflow++;
00190 #endif
00191 set_bit(rcsta, CREN);
00192 } else {
00193 if (test_bit(rcsta, FERR)) {
00194 #ifdef SERIAL_DEBUG_ON
00195 rx_framing_error++;
00196 #endif
00197 }
00198 rx_next = rx_end + 1;
00199 if (rx_next == SERIAL_RX_BUFFER_SIZE) {
00200 rx_next = 0;
00201 }
00202 if (rx_next != rx_start) {
00203 rx_buffer[rx_end] = rcreg;
00204 rx_end = rx_next;
00205 } else {
00206 _asm MOVF _rcreg,W
00207 #ifdef SERIAL_DEBUG_ON
00208 rx_soft_overflow++;
00209 #endif
00210 }
00211 }
00212 }
00213
00214
00215
00216 uns8 serial_getc(void)
00217 {
00218 uns8 rx_char, rx_next;
00219
00220 while(rx_end == rx_start);
00221
00222 start_crit_sec();
00223
00224 rx_char = rx_buffer[rx_start];
00225 rx_start++;
00226 if (rx_start == SERIAL_RX_BUFFER_SIZE) {
00227 rx_start = 0;
00228 }
00229
00230 end_crit_sec();
00231
00232 return (rx_char);
00233
00234 }
00235
00236
00237
00238 void serial_print_str(char *str) {
00239
00240 uns8 count;
00241
00242 for(count = 0 ; str[count] != 0; count++)
00243 {
00244 serial_putc(str[count]);
00245 }
00246 }
00247
00248
00249
00250 void serial_print_str(rom char *str) {
00251
00252 uns8 count;
00253
00254 for(count = 0 ; str[count] != 0; count++)
00255 {
00256 serial_putc(str[count]);
00257 }
00258 }
00259
00260
00261
00262 void serial_print_int(uns16 i) {
00263
00264 char buffer[6];
00265 uns8 count = 5;
00266 buffer[5] = '\0';
00267 do {
00268 count--;
00269 buffer[count] = '0' + i % 10;
00270 i = i / 10;
00271 } while (i > 0);
00272 while (buffer[count]) {
00273 serial_putc(buffer[count]);
00274 count++;
00275 }
00276
00277
00278
00279
00280
00281 }
00282
00283 void serial_print_int_hex(uns8 i) {
00284
00285 serial_putc(bin2Hex(i >> 4));
00286 serial_putc(bin2Hex((i & 0x0f)));
00287
00288 }
00289
00290 void serial_print_int_hex_16bit(uns16 i) {
00291 serial_print_int_hex(i >> 8);
00292 serial_print_int_hex(i & 0xff);
00293 }
00294
00295
00296 void serial_print_spc() {
00297 serial_putc(' ');
00298 }
00299
00300
00301 void serial_print_nl() {
00302 serial_putc('\n');
00303 }
00304
00305
00306
00307 uns8 serial_rx_avail() { return rx_start != rx_end; }
00308 uns8 serial_tx_empty() { return tx_start == tx_end; }
00309