OSDN Git Service

IDE MPLABXで使えるようにする
[scilog/cpu2010.git] / myuart.c
1 #include <p24FJ64GA004.h>
2 #include "myuart.h"
3
4 // UART1 ublox GPS受信用リングバッファ
5 static unsigned char   ubx_w, ubx_r;
6 static unsigned char    ubx_buf[256];
7
8 // UART2
9 #define UART2_BUFF_SIZE 16
10 static unsigned char   uart2_w, uart2_r;
11 static unsigned char   uart2_buf[UART2_BUFF_SIZE];
12
13 void __attribute__((interrupt, no_auto_psv)) _U1RXInterrupt(void)
14 {
15     // 割り込みフラグクリア
16     uart1_rx_intf_clear();
17     while(uart1_rx_is_rdy()) {
18         ubx_buf[ubx_w++] = uart1_getc();
19     }
20     if (U1STAbits.OERR == 1) U1STAbits.OERR = 0;
21 }
22 void __attribute__((interrupt, no_auto_psv)) _U2RXInterrupt(void)
23 {
24     // 割り込みフラグクリア
25     uart2_rx_intf_clear();
26     while(uart2_rx_is_rdy()) {
27         uart2_buf[uart2_w++] = uart2_getc();
28         if (uart2_w >= UART2_BUFF_SIZE) uart2_w = 0;
29     }
30     if (U2STAbits.OERR == 1) U2STAbits.OERR = 0;
31 }
32 // polling send
33 void uart1_puts(char *s)
34 {
35     while(*s != 0) {
36         while(uart1_tx_is_full());
37         U1TXREG = *s++;
38     }
39 }
40 void uart2_puts(char *s)
41 {
42     while(*s != 0) {
43         while(uart2_tx_is_full());
44         U2TXREG = *s++;
45     }
46 }
47 void uart1_myputc(char c)
48 {
49     while(uart1_tx_is_full());
50     U1TXREG = c;
51 }
52 void uart2_myputc(char c)
53 {
54     while(uart2_tx_is_full());
55     U2TXREG = c;
56 }
57 /*
58  * 受信バッファにデータあるか?
59  * return 0=なし 1=あり
60  */
61 int uart1_rcvbuf_is_data(void)
62 {
63     if (ubx_r == ubx_w) return 0;
64     return 1;
65 }
66 int uart2_rcvbuf_is_data(void)
67 {
68     if (uart2_r == uart2_w) return 0;
69     return 1;
70 }
71 /*
72  * 受信バッファから1byte get
73  * return データ -1=データ無し
74  */
75 int uart1_rcvbuf_getc(void)
76 {
77     if (ubx_r == ubx_w) return -1;
78     return ubx_buf[ubx_r++];
79 }
80 int uart2_rcvbuf_getc(void)
81 {
82     int out;
83     if (uart2_r == uart2_w) return -1;
84     out = uart2_buf[uart2_r++];
85     if (uart2_r >= UART2_BUFF_SIZE) uart2_r = 0;
86     return out;
87 }
88
89 void uart1_rcvbuf_clear(void)
90 {
91     ubx_r = ubx_w;
92 }
93
94 void uart2_rcvbuf_clear(void)
95 {
96     uart2_r = uart2_w;
97 }
98 void uart1_init(unsigned int mode, unsigned int sts, unsigned int baud)
99 {
100     U1MODE = mode;
101     U1STA = sts;
102     U1BRG = baud;
103     ubx_w = 0;
104     ubx_r = 0;
105 }
106 void uart2_init(unsigned int mode, unsigned int sts, unsigned int baud)
107 {
108     U2MODE = mode;
109     U2STA = sts;
110     U2BRG = baud;
111     uart2_w = 0;
112     uart2_r = 0;
113 }