Rev 33 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 33 | Rev 48 | ||
|---|---|---|---|
| 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 "../Include/stackTasks.h" |
13 | #include "../Include/stackTasks.h" |
| 14 | #include "../Include/uart.h" |
14 | #include "../Include/uart.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; // Low BRG speed |
35 | TXSTA=0b00100000; // Low BRG speed |
| 36 | RCSTA=0b10010000; |
36 | RCSTA=0b10010000; |
| 37 | SPBRG = SPBRG_VAL; |
37 | SPBRG = SPBRG_VAL; |
| 38 | // USART RX interrupt |
38 | // USART RX interrupt |
| 39 | PIR1bits.RCIF=0; |
39 | PIR1bits.RCIF=0; |
| 40 | PIE1bits.RCIE=1; |
40 | PIE1bits.RCIE=1; |
| 41 | // High interrupt |
41 | // High interrupt |
| 42 | IPR1bits.RCIP=1; |
42 | IPR1bits.RCIP=1; |
| 43 | } |
43 | } |
| 44 | 44 | ||
| 45 | 45 | ||
| 46 | /* |
46 | /* |
| 47 | * Function: uartPutc |
47 | * Function: uartPutc |
| 48 | * |
48 | * |
| 49 | * Input: BYTE c to send on uart |
49 | * Input: BYTE c to send on uart |
| 50 | * Output: none |
50 | * Output: none |
| 51 | * Pre-conditions: call to uartInit() |
51 | * Pre-conditions: call to uartInit() |
| 52 | * Affects: none |
52 | * Affects: none |
| 53 | * Depends: none |
53 | * Depends: none |
| 54 | */ |
54 | */ |
| 55 | void uartPutc(BYTE c) |
55 | void uartPutc(BYTE c) |
| 56 | { |
56 | { |
| 57 | // Write the data byte to the USART |
57 | // Write the data byte to the USART |
| 58 | TXREG = c; |
58 | TXREG = c; |
| 59 | while(uartBusy()); |
59 | while(uartBusy()); |
| 60 | } |
60 | } |
| 61 | 61 | ||
| 62 | 62 | ||
| 63 | /* |
63 | /* |
| 64 | * Function: uartPuts |
64 | * Function: uartPuts |
| 65 | * |
65 | * |
| 66 | * Input: Byte array to send on uart |
66 | * Input: Byte array to send on uart |
| 67 | * Output: none |
67 | * Output: none |
| 68 | * Pre-conditions: call to uartInit() |
68 | * Pre-conditions: call to uartInit() |
| 69 | * Affects: none |
69 | * Affects: none |
| 70 | * Depends: none |
70 | * Depends: none |
| 71 | */ |
71 | */ |
| 72 | void uartPuts(BYTE c[]) |
72 | void uartPuts(BYTE c[]) |
| 73 | { |
73 | { |
| 74 | do |
74 | do |
| 75 | { // Transmit a byte |
75 | { // Transmit a byte |
| 76 | while(uartBusy()); |
76 | while(uartBusy()); |
| 77 | uartPutc(*c); |
77 | uartPutc(*c); |
| 78 | } while( *c++ ); |
78 | } while( *c++ ); |
| 79 | } |
79 | } |
| 80 | 80 | ||
| 81 | /* |
81 | /* |
| 82 | * Function: uartPutrs |
82 | * Function: uartPutrs |
| 83 | * |
83 | * |
| 84 | * Input: Byte array to send on uart (static) |
84 | * Input: Byte array to send on uart (static) |
| 85 | * Output: none |
85 | * Output: none |
| 86 | * Pre-conditions: call to uartInit() |
86 | * Pre-conditions: call to uartInit() |
| 87 | * Affects: none |
87 | * Affects: none |
| 88 | * Depends: none |
88 | * Depends: none |
| 89 | */ |
89 | */ |
| 90 | void uartPutrs(const rom char *c) |
90 | void uartPutrs(const rom char *c) |
| 91 | { |
91 | { |
| 92 | do |
92 | do |
| 93 | { // Transmit a byte |
93 | { // Transmit a byte |
| 94 | while(uartBusy()); |
94 | while(uartBusy()); |
| 95 | uartPutc(*c); |
95 | uartPutc(*c); |
| 96 | } while( *c++ ); |
96 | } while( *c++ ); |
| 97 | } |
97 | } |
| 98 | 98 | ||
| 99 | 99 | ||
| 100 | 100 | ||
| 101 | 101 | ||
| 102 | /* |
102 | /* |
| 103 | * Function: uartBusy |
103 | * Function: uartBusy |
| 104 | * |
104 | * |
| 105 | * Input: none. |
105 | * Input: none. |
| 106 | * Output: If usart busy or not. |
106 | * Output: If usart busy or not. |
| 107 | * Pre-conditions: call to uartInit() |
107 | * Pre-conditions: call to uartInit() |
| 108 | * Affects: none |
108 | * Affects: none |
| 109 | * Depends: none |
109 | * Depends: none |
| 110 | */ |
110 | */ |
| 111 | char uartBusy(void) |
111 | char uartBusy(void) |
| 112 | { |
112 | { |
| 113 | return !TXSTAbits.TRMT; |
113 | return !TXSTAbits.TRMT; |
| 114 | } |
114 | } |
| 115 | 115 | ||
| 116 | 116 | ||
| 117 | /* |
117 | /* |
| 118 | * Function: uartGet |
118 | * Function: uartGet |
| 119 | * |
119 | * |
| 120 | * Input: none |
120 | * Input: none |
| 121 | * Output: Byte read |
121 | * Output: Byte read |
| 122 | * Pre-conditions: call to uartInit() |
122 | * Pre-conditions: call to uartInit() |
| 123 | * Affects: none |
123 | * Affects: none |
| 124 | * Depends: none. |
124 | * Depends: none. |
| 125 | */ |
125 | */ |
| 126 | BYTE uartGet(void) |
126 | BYTE uartGet(void) |
| 127 | { |
127 | { |
| 128 | // Return the received data |
128 | // Return the received data |
| 129 | return RCREG; |
129 | return RCREG; |
| 130 | } |
130 | } |
| 131 | 131 | ||
| 132 | 132 | ||
| 133 | /* |
133 | /* |
| 134 | * Function: uartISR |
134 | * Function: uartISR |
| 135 | * |
135 | * |
| 136 | * Input: none |
136 | * Input: none |
| 137 | * Output: none |
137 | * Output: none |
| 138 | * Pre-conditions: call to uartInit() |
138 | * Pre-conditions: call to uartInit() |
| 139 | * Affects: call to uartParse |
139 | * Affects: call to uartParse |
| 140 | * Depends: none. |
140 | * Depends: none. |
| 141 | */ |
141 | */ |
| 142 | void uartISR(void) |
142 | void uartISR(void) |
| 143 | { |
143 | { |
| 144 | if (PIR1bits.RCIF==1 && PIE1bits.RCIE==1) |
144 | if (PIR1bits.RCIF==1 && PIE1bits.RCIE==1) |
| 145 | { |
145 | { |
| - | 146 | if (RCSTAbits.OERR) { RCSTAbits.CREN=0; RCSTAbits.CREN=1; } |
|
| - | 147 | ||
| 146 | uartParse(uartGet()); |
148 | uartParse(uartGet()); |
| 147 | PIR1bits.RCIF=0; |
149 | PIR1bits.RCIF=0; |
| 148 | } |
150 | } |
| 149 | } |
151 | } |
| 150 | 152 | ||
| 151 | 153 | ||
| 152 | /* |
154 | /* |
| 153 | * Function: uartDataRedy |
155 | * Function: uartDataRedy |
| 154 | * |
156 | * |
| 155 | * Input: none. |
157 | * Input: none. |
| 156 | * Output: If usart data are redy to be read. |
158 | * Output: If usart data are redy to be read. |
| 157 | * Pre-conditions: call to uartInit() |
159 | * Pre-conditions: call to uartInit() |
| 158 | * Affects: none |
160 | * Affects: none |
| 159 | * Depends: none |
161 | * Depends: none |
| 160 | */ |
162 | */ |
| 161 | char uartDataRedy(void) |
163 | char uartDataRedy(void) |
| 162 | { |
164 | { |
| 163 | if(RCSTAbits.OERR) |
165 | if(RCSTAbits.OERR) |
| 164 | { |
166 | { |
| 165 | RCSTAbits.CREN = 0; |
167 | RCSTAbits.CREN = 0; |
| 166 | RCSTAbits.CREN = 1; |
168 | RCSTAbits.CREN = 1; |
| 167 | } |
169 | } |
| 168 | return PIR1bits.RCIF; |
170 | return PIR1bits.RCIF; |
| 169 | } |
171 | } |
| 170 | 172 | ||
| 171 | #endif |
173 | #endif |
| 172 | 174 | ||