00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <avr/io.h>
00019 #include <avr/interrupt.h>
00020 #include <avr/signal.h>
00021
00022 #include "global.h"
00023 #include "timer.h"
00024 #include "uartsw.h"
00025
00026
00027
00028
00029
00030
00031 volatile u08 UartswTxBusy;
00032 volatile u16 UartswBaudRateDiv;
00033 volatile u08 UartswTxData;
00034 volatile u08 UartswTxBitNum;
00035
00036
00037 cBuffer uartswRxBuffer;
00038
00039 static char uartswRxData[UARTSW_RX_BUFFER_SIZE];
00040
00041 volatile u08 UartswRxStartBit;
00042 volatile u08 UartswRxData;
00043 volatile u08 UartswRxBitNum;
00044
00045
00046
00047
00048
00049 void uartswInit(void)
00050 {
00051
00052 uartswInitBuffers();
00053
00054 sbi(DDRD, 5);
00055 cbi(DDRD, 6);
00056 cbi(PORTD, 6);
00057
00058 uartswSetBaudRate(9600);
00059
00060 timer1SetPrescaler(TIMER_CLK_DIV1);
00061
00062 timerAttach(TIMER1OUTCOMPAREA_INT, uartswTxBitService);
00063
00064 timerAttach(TIMER1INPUTCAPTURE_INT, uartswRxBitService);
00065
00066 timerAttach(TIMER1OUTCOMPAREB_INT, uartswRxBitService);
00067
00068 sbi(TIMSK, OCIE1A);
00069
00070 sbi(TIMSK, TICIE1);
00071
00072 sbi(TCCR1B, ICES1);
00073
00074 sei();
00075 }
00076
00077
00078 void uartswInitBuffers(void)
00079 {
00080
00081 bufferInit(&uartswRxBuffer, uartswRxData, UARTSW_RX_BUFFER_SIZE);
00082 }
00083
00084
00085 void uartswOff(void)
00086 {
00087
00088 cbi(TIMSK, OCIE1A);
00089 cbi(TIMSK, OCIE1B);
00090 cbi(TIMSK, TICIE1);
00091
00092 timerDetach(TIMER1OUTCOMPAREA_INT);
00093 timerDetach(TIMER1OUTCOMPAREB_INT);
00094 timerDetach(TIMER1INPUTCAPTURE_INT);
00095 }
00096
00097 void uartswSetBaudRate(u32 baudrate)
00098 {
00099
00100 UartswBaudRateDiv = (u16)((F_CPU+(baudrate/2L))/(baudrate*1L));
00101 }
00102
00103
00104 cBuffer* uartswGetRxBuffer(void)
00105 {
00106
00107 return &uartswRxBuffer;
00108 }
00109
00110 void uartswSendByte(u08 data)
00111 {
00112
00113 while(UartswTxBusy);
00114
00115 UartswTxBusy = TRUE;
00116
00117 UartswTxData = data;
00118
00119 UartswTxBitNum = 9;
00120
00121
00122 sbi(PORTD, PD5);
00123
00124 outw(OCR1A, inw(TCNT1) + UartswBaudRateDiv);
00125 }
00126
00127
00128 u08 uartswReceiveByte(u08* rxData)
00129 {
00130
00131 if(uartswRxBuffer.size)
00132 {
00133
00134 if(uartswRxBuffer.datalength)
00135 {
00136
00137 *rxData = bufferGetFromFront(&uartswRxBuffer);
00138 return TRUE;
00139 }
00140 else
00141 {
00142
00143 return FALSE;
00144 }
00145 }
00146 else
00147 {
00148
00149 return FALSE;
00150 }
00151 }
00152
00153 void uartswTxBitService(void)
00154 {
00155 if(UartswTxBitNum)
00156 {
00157
00158 if(UartswTxBitNum > 1)
00159 {
00160
00161 if( !(UartswTxData & 0x01) )
00162 sbi(PORTD, PD5);
00163 else
00164 cbi(PORTD, PD5);
00165
00166 UartswTxData = UartswTxData>>1;
00167 }
00168 else
00169 {
00170
00171 cbi(PORTD, PD5);
00172 }
00173
00174 outw(OCR1A, inw(OCR1A) + UartswBaudRateDiv);
00175
00176 UartswTxBitNum--;
00177 }
00178 else
00179 {
00180
00181
00182 UartswTxBusy = FALSE;
00183 }
00184 }
00185
00186 void uartswRxBitService(void)
00187 {
00188
00189
00190
00191 if(!UartswRxStartBit)
00192 {
00193
00194
00195 cbi(TIMSK, TICIE1);
00196
00197 outw(OCR1B, inw(TCNT1) + UartswBaudRateDiv + UartswBaudRateDiv/2);
00198
00199 sbi(TIFR, OCF1B);
00200
00201 sbi(TIMSK, OCIE1B);
00202
00203 UartswRxStartBit = TRUE;
00204
00205 UartswRxBitNum = 0;
00206
00207 UartswRxData = 0;
00208 }
00209 else
00210 {
00211
00212
00213
00214
00215 UartswRxData = UartswRxData>>1;
00216
00217
00218 if( !(inb(PIND) & (1<<6)) )
00219 {
00220
00221
00222 UartswRxData |= 0x80;
00223 }
00224
00225
00226 UartswRxBitNum++;
00227
00228 outw(OCR1B, inw(OCR1B) + UartswBaudRateDiv);
00229
00230
00231 if(UartswRxBitNum >= 8)
00232 {
00233
00234 bufferAddToEnd(&uartswRxBuffer, UartswRxData);
00235
00236 cbi(TIMSK, OCIE1B);
00237
00238 sbi(TIFR, ICF1);
00239
00240 sbi(TIMSK, TICIE1);
00241
00242 UartswRxStartBit = FALSE;
00243 }
00244 }
00245 }
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342