Rev 56 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 56 | Rev 64 | ||
|---|---|---|---|
| 1 | /********************************************************************* |
1 | /********************************************************************* |
| 2 | * |
2 | * |
| 3 | * UART |
3 | * UART |
| 4 | * |
4 | * |
| 5 | ********************************************************************* |
5 | ********************************************************************* |
| 6 | * FileName: uart.c |
6 | * FileName: uart.c |
| 7 | * |
7 | * |
| 8 | * Author Date Comment |
8 | * Author Date Comment |
| 9 | *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
9 | *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 10 | * Johan Böhlin 21-10-06 Original (Rev 1.0) |
10 | * Johan Böhlin 21-10-06 Original (Rev 1.0) |
| 11 | ********************************************************************/ |
11 | ********************************************************************/ |
| 12 | 12 | ||
| 13 | #include <uart.h> |
13 | #include <uart.h> |
| 14 | #include <stackTasks.h> |
14 | #include <stackTasks.h> |
| 15 | 15 | ||
| 16 | #ifdef USE_UART |
16 | #ifdef USE_UART |
| 17 | 17 | ||
| 18 | static char uartBusy(void); |
18 | static char uartBusy(void); |
| 19 | 19 | ||
| 20 | 20 | ||
| 21 | /* |
21 | /* |
| 22 | * Function: uartInit |
22 | * Function: uartInit |
| 23 | * |
23 | * |
| 24 | * Input: none |
24 | * Input: none |
| 25 | * Output: none |
25 | * Output: none |
| 26 | * Pre-conditions: none |
26 | * Pre-conditions: none |
| 27 | * Affects: none |
27 | * Affects: none |
| 28 | * Depends: none |
28 | * Depends: none |
| 29 | */ |
29 | */ |
| 30 | void uartInit() |
30 | void uartInit() |
| 31 | { |
31 | { |
| 32 | // Configure USART |
32 | // Configure USART |
| 33 | TRISCbits.TRISC7=1; |
33 | TRISCbits.TRISC7=1; |
| 34 | TRISCbits.TRISC6=0; |
34 | TRISCbits.TRISC6=0; |
| - | 35 | TXSTA=0b00100000; |
|
| - | 36 | #ifdef HIGH_BRG |
|
| 35 |
|
37 | TXSTAbits.BRGH=1; // High BRG speed |
| - | 38 | BAUDCONbits.BRG16=1; |
|
| 36 |
|
39 | SPBRGH = SPBRGH_VAL; |
| 37 | SPBRG = SPBRG_VAL; |
40 | SPBRG = SPBRG_VAL; |
| - | 41 | #else |
|
| - | 42 | TXSTAbits.BRGH=0; // Low BRG speed |
|
| - | 43 | SPBRG = SPBRG_VAL; |
|
| - | 44 | #endif |
|
| - | 45 | ||
| - | 46 | RCSTA=0b10010000; |
|
| - | 47 | ||
| 38 | // USART RX interrupt |
48 | // USART RX interrupt |
| 39 | PIR1bits.RCIF=0; |
49 | PIR1bits.RCIF=0; |
| 40 | PIE1bits.RCIE=1; |
50 | PIE1bits.RCIE=1; |
| 41 | // High interrupt |
51 | // High interrupt |
| 42 | IPR1bits.RCIP=1; |
52 | IPR1bits.RCIP=1; |
| 43 | } |
53 | } |
| 44 | 54 | ||
| 45 | 55 | ||
| 46 | /* |
56 | /* |
| 47 | * Function: uartPutc |
57 | * Function: uartPutc |
| 48 | * |
58 | * |
| 49 | * Input: BYTE c to send on uart |
59 | * Input: BYTE c to send on uart |
| 50 | * Output: none |
60 | * Output: none |
| 51 | * Pre-conditions: call to uartInit() |
61 | * Pre-conditions: call to uartInit() |
| 52 | * Affects: none |
62 | * Affects: none |
| 53 | * Depends: none |
63 | * Depends: none |
| 54 | */ |
64 | */ |
| 55 | void uartPutc(BYTE c) |
65 | void uartPutc(BYTE c) |
| 56 | { |
66 | { |
| 57 | // Write the data byte to the USART |
67 | // Write the data byte to the USART |
| 58 | TXREG = c; |
68 | TXREG = c; |
| 59 | while(uartBusy()); |
69 | while(uartBusy()); |
| 60 | } |
70 | } |
| 61 | 71 | ||
| 62 | 72 | ||
| 63 | /* |
73 | /* |
| 64 | * Function: uartPuts |
74 | * Function: uartPuts |
| 65 | * |
75 | * |
| 66 | * Input: Byte array to send on uart |
76 | * Input: Byte array to send on uart |
| 67 | * Output: none |
77 | * Output: none |
| 68 | * Pre-conditions: call to uartInit() |
78 | * Pre-conditions: call to uartInit() |
| 69 | * Affects: none |
79 | * Affects: none |
| 70 | * Depends: none |
80 | * Depends: none |
| 71 | */ |
81 | */ |
| 72 | void uartPuts(BYTE c[]) |
82 | void uartPuts(BYTE c[]) |
| 73 | { |
83 | { |
| 74 | do |
84 | do |
| 75 | { // Transmit a byte |
85 | { // Transmit a byte |
| 76 | while(uartBusy()); |
86 | while(uartBusy()); |
| 77 | uartPutc(*c); |
87 | uartPutc(*c); |
| 78 | } while( *c++ ); |
88 | } while( *c++ ); |
| 79 | } |
89 | } |
| 80 | 90 | ||
| 81 | /* |
91 | /* |
| 82 | * Function: uartPutrs |
92 | * Function: uartPutrs |
| 83 | * |
93 | * |
| 84 | * Input: Byte array to send on uart (static) |
94 | * Input: Byte array to send on uart (static) |
| 85 | * Output: none |
95 | * Output: none |
| 86 | * Pre-conditions: call to uartInit() |
96 | * Pre-conditions: call to uartInit() |
| 87 | * Affects: none |
97 | * Affects: none |
| 88 | * Depends: none |
98 | * Depends: none |
| 89 | */ |
99 | */ |
| 90 | void uartPutrs(const rom char *c) |
100 | void uartPutrs(const rom char *c) |
| 91 | { |
101 | { |
| 92 | do |
102 | do |
| 93 | { // Transmit a byte |
103 | { // Transmit a byte |
| 94 | while(uartBusy()); |
104 | while(uartBusy()); |
| 95 | uartPutc(*c); |
105 | uartPutc(*c); |
| 96 | } while( *c++ ); |
106 | } while( *c++ ); |
| 97 | } |
107 | } |
| 98 | 108 | ||
| 99 | 109 | ||
| 100 | 110 | ||
| 101 | 111 | ||
| 102 | /* |
112 | /* |
| 103 | * Function: uartBusy |
113 | * Function: uartBusy |
| 104 | * |
114 | * |
| 105 | * Input: none. |
115 | * Input: none. |
| 106 | * Output: If usart busy or not. |
116 | * Output: If usart busy or not. |
| 107 | * Pre-conditions: call to uartInit() |
117 | * Pre-conditions: call to uartInit() |
| 108 | * Affects: none |
118 | * Affects: none |
| 109 | * Depends: none |
119 | * Depends: none |
| 110 | */ |
120 | */ |
| 111 | char uartBusy(void) |
121 | char uartBusy(void) |
| 112 | { |
122 | { |
| 113 | return !TXSTAbits.TRMT; |
123 | return !TXSTAbits.TRMT; |
| 114 | } |
124 | } |
| 115 | 125 | ||
| 116 | 126 | ||
| 117 | /* |
127 | /* |
| 118 | * Function: uartGet |
128 | * Function: uartGet |
| 119 | * |
129 | * |
| 120 | * Input: none |
130 | * Input: none |
| 121 | * Output: Byte read |
131 | * Output: Byte read |
| 122 | * Pre-conditions: call to uartInit() |
132 | * Pre-conditions: call to uartInit() |
| 123 | * Affects: none |
133 | * Affects: none |
| 124 | * Depends: none. |
134 | * Depends: none. |
| 125 | */ |
135 | */ |
| 126 | BYTE uartGet(void) |
136 | BYTE uartGet(void) |
| 127 | { |
137 | { |
| 128 | // Return the received data |
138 | // Return the received data |
| 129 | return RCREG; |
139 | return RCREG; |
| 130 | } |
140 | } |
| 131 | 141 | ||
| 132 | 142 | ||
| 133 | /* |
143 | /* |
| 134 | * Function: uartISR |
144 | * Function: uartISR |
| 135 | * |
145 | * |
| 136 | * Input: none |
146 | * Input: none |
| 137 | * Output: none |
147 | * Output: none |
| 138 | * Pre-conditions: call to uartInit() |
148 | * Pre-conditions: call to uartInit() |
| 139 | * Affects: call to uartParse |
149 | * Affects: call to uartParse |
| 140 | * Depends: none. |
150 | * Depends: none. |
| 141 | */ |
151 | */ |
| 142 | void uartISR(void) |
152 | void uartISR(void) |
| 143 | { |
153 | { |
| 144 | if (PIR1bits.RCIF==1 && PIE1bits.RCIE==1) |
154 | if (PIR1bits.RCIF==1 && PIE1bits.RCIE==1) |
| 145 | { |
155 | { |
| 146 | if (RCSTAbits.OERR) { RCSTAbits.CREN=0; RCSTAbits.CREN=1; } |
156 | if (RCSTAbits.OERR) { RCSTAbits.CREN=0; RCSTAbits.CREN=1; } |
| 147 | 157 | ||
| 148 | uartParse(uartGet()); |
158 | uartParse(uartGet()); |
| 149 | PIR1bits.RCIF=0; |
159 | PIR1bits.RCIF=0; |
| 150 | } |
160 | } |
| 151 | } |
161 | } |
| 152 | 162 | ||
| 153 | 163 | ||
| 154 | /* |
164 | /* |
| 155 | * Function: uartDataRedy |
165 | * Function: uartDataRedy |
| 156 | * |
166 | * |
| 157 | * Input: none. |
167 | * Input: none. |
| 158 | * Output: If usart data are redy to be read. |
168 | * Output: If usart data are redy to be read. |
| 159 | * Pre-conditions: call to uartInit() |
169 | * Pre-conditions: call to uartInit() |
| 160 | * Affects: none |
170 | * Affects: none |
| 161 | * Depends: none |
171 | * Depends: none |
| 162 | */ |
172 | */ |
| 163 | char uartDataRedy(void) |
173 | char uartDataRedy(void) |
| 164 | { |
174 | { |
| 165 | if(RCSTAbits.OERR) |
175 | if(RCSTAbits.OERR) |
| 166 | { |
176 | { |
| 167 | RCSTAbits.CREN = 0; |
177 | RCSTAbits.CREN = 0; |
| 168 | RCSTAbits.CREN = 1; |
178 | RCSTAbits.CREN = 1; |
| 169 | } |
179 | } |
| 170 | return PIR1bits.RCIF; |
180 | return PIR1bits.RCIF; |
| 171 | } |
181 | } |
| 172 | 182 | ||
| 173 | #endif |
183 | #endif |
| 174 | 184 | ||