00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include <cubeos.h>
00017 #include <mc68681.h>
00018 #include <tpu.h>
00019 #include <tpud.h>
00020 #include <ivtab.h>
00021
00022 #define WFSR 1000
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067 int TPU_init ()
00068 {
00069
00070 setTPUMCR (0x4acd);
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099 setTICR (0x480);
00100
00101
00102
00103
00104
00105 writeshort (TPU_CIER, 0);
00106
00107 return (0);
00108
00109 }
00110
00111
00116 int TPU_initchannel (unsigned char nr)
00117 {
00118 setCFSR (nr, 0);
00119 setHSRR (nr, 0);
00120 setCIER (nr, 0);
00121 setCPR (nr, 0);
00122 return (0);
00123 }
00124
00125
00126
00127
00128
00129 int TPU_makedio (unsigned char nr)
00130 {
00131
00132 setCFSR (nr, 8);
00133 setHSRR (nr, 2);
00134 setCIER (nr, 0);
00135 setCPR (nr, 1);
00136 return (0);
00137 }
00138
00139
00140
00141
00142
00143
00144 int TPU_setdio (unsigned char nr, unsigned char v)
00145 {
00146 if (nr > 15) {
00147 return (-1);
00148 }
00149 if (getCFSR (nr) != 8) {
00150 return (-1);
00151 }
00152 if (v == 255) {
00153 setHSQR (nr, 0);
00154 setHSRR (nr, 3);
00155 } else {
00156 if (v) {
00157 setHSRR (nr, 1);
00158 } else {
00159 setHSRR (nr, 2);
00160 }
00161 }
00162 return (0);
00163 }
00164
00165
00166
00167
00168
00169
00170
00171 int TPU_getdio (unsigned char nr)
00172 {
00173 if (nr > 15) {
00174 return (-1);
00175 }
00176 if (getCFSR (nr) != 8) {
00177 return (-1);
00178 }
00179 setHSQR (nr, 2);
00180 setHSRR (nr, 3);
00181 return ((getPAR(nr,1) & 0x8000)>0);
00182 }
00183
00184
00188 int TPU_makepwm_TCR1 (unsigned char nr)
00189 {
00190
00191 setCFSR (nr, 9);
00192 setPAR (nr, 0, 0x8c);
00193 setPAR (nr, 2, 0);
00194 setPAR (nr, 3, 20875);
00195 setHSRR (nr, 2);
00196 setCIER (nr, 0);
00197 setCPR (nr, 2);
00198 return (0);
00199
00200 }
00201
00205 int TPU_makepwm_TCR2 (unsigned char nr)
00206 {
00207
00208 setCFSR (nr, 9);
00209 setPAR (nr, 0, 0xdc);
00210 setPAR (nr, 2, 0);
00211 setPAR (nr, 3, 20875);
00212 setHSRR (nr, 2);
00213 setCIER (nr, 0);
00214 setCPR (nr, 2);
00215 return (0);
00216
00217 }
00218
00219
00223 int TPU_makepwm (unsigned char nr)
00224 {
00225 return TPU_makepwm_TCR1 (nr);
00226 }
00227
00233 int TPU_setpwmperiod (unsigned char nr, unsigned short period)
00234 {
00235 int i;
00236
00237 if (nr > 15) {
00238 return (-1);
00239 }
00240 if (getCFSR (nr) != 9) {
00241 return (-1);
00242 }
00243 if (period>0x8000){
00244 KERN_complain(4,"period for tp channel exceeds 0x8000");
00245 return(-1);
00246 }
00247
00248 i = 0;
00249 while ((getHSRR (nr) != 0) && (i < WFSR))
00250 i++;
00251 if (i >= WFSR) {
00252 return (-1);
00253 }
00254 setPAR (nr, 3, period);
00255 setHSRR (nr, 1);
00256 return (0);
00257 }
00258
00259 int TPU_setpwmdc (unsigned char nr, unsigned char hightime)
00260 {
00261 int i;
00262 unsigned long t;
00263
00264 if (nr > 15) {
00265 return (-1);
00266 }
00267 if (getCFSR (nr) != 9) {
00268 return (-1);
00269 }
00270 if (hightime > 100) {
00271 return (-1);
00272 }
00273 i = 0;
00274 while ((getHSRR (nr) != 0) && (i < WFSR))
00275 i++;
00276 if (i >= WFSR) {
00277 return (-1);
00278 }
00279 t = (getPAR (nr, 3) * hightime) / 100;
00280
00281 setPAR (nr, 2, (unsigned short) t);
00282 setHSRR (nr, 1);
00283 return (0);
00284 }
00285 int TPU_setpwmhigh (unsigned char nr, unsigned short hightime)
00286 {
00287 if (nr > 15) {
00288 return (-1);
00289 }
00290 if (getCFSR (nr) != 9) {
00291 return (-1);
00292 }
00293 setPAR (nr, 2, (unsigned short) hightime);
00294
00295 return (0);
00296 }
00297
00298 int TPU_makepac (unsigned char nr)
00299 {
00300 setCFSR (nr, 10);
00301 setHSQR (nr, 1);
00302 setPAR (nr, 0, 0x4);
00303 setPAR (nr, 2, 0xffff);
00304 setHSRR (nr, 1);
00305 setCIER (nr, 0);
00306 setCPR (nr, 2);
00307 return (0);
00308
00309 }
00310
00311 unsigned short TPU_getpac (unsigned char nr)
00312 {
00313 unsigned short v;
00314
00315 if (nr > 15) {
00316 return (0);
00317 }
00318 if (getCFSR (nr) != 10) {
00319 return (0);
00320 }
00321 v = getPAR (nr, 3);
00322 setHSRR (nr, 1);
00323 return (v);
00324
00325 }
00326
00327
00328
00329
00330 int TPU_makeqd (unsigned char ch1, unsigned char ch2)
00331 {
00332
00333 setCPR (ch1, 0);
00334 setCPR (ch2, 0);
00335
00336 setCFSR (ch1, TPU_FKT_QDEC);
00337 setCFSR (ch2, TPU_FKT_QDEC);
00338 setPAR (ch1, TPU_QDEC_CORR_PINSTATE_ADDR, ch2 * 16 + TPU_QDEC_CHAN_PINSTATE * 2);
00339 setPAR (ch2, TPU_QDEC_CORR_PINSTATE_ADDR, ch1 * 16 + TPU_QDEC_CHAN_PINSTATE * 2);
00340 setPAR (ch1, TPU_QDEC_EDGE_TIME_LSB_ADDR, ch2 * 16 + 1);
00341 setPAR (ch2, TPU_QDEC_EDGE_TIME_LSB_ADDR, ch2 * 16 + 1);
00342 setPAR (ch1, TPU_QDEC_POS_COUNT, 0x8000);
00343 setPAR (ch2, TPU_QDEC_POS_COUNT, 0x8000);
00344
00345
00346 setHSQR (ch1, 0);
00347 setHSQR (ch2, 1);
00348 setHSRR (ch1, 3);
00349 setHSRR (ch2, 3);
00350 setCPR (ch1, 2);
00351 setCPR (ch2, 2);
00352 return (0);
00353
00354 }
00355
00356 short TPU_getqd (unsigned char nr)
00357 {
00358 short v;
00359
00360 if (nr > 15) {
00361 return (0);
00362 }
00363 if (getCFSR (nr) != TPU_FKT_QDEC) {
00364 return (0);
00365 }
00366 v = getPAR (nr, TPU_QDEC_POS_COUNT);
00367 setPAR (nr, TPU_QDEC_POS_COUNT, 0x8000);
00368 return (0x8000 - v);
00369
00370 }
00371
00372 short TPU_readqd (unsigned char nr)
00373 {
00374 short v;
00375
00376 if (nr > 15) {
00377 return (0);
00378 }
00379 if (getCFSR (nr) != TPU_FKT_QDEC) {
00380 return (0);
00381 }
00382 v = getPAR (nr, TPU_QDEC_POS_COUNT);
00383 return (0x8000 - v);
00384
00385 }
00386
00387 int TPU_setisr (unsigned char ch, void (*isr) (void))
00388 {
00389 return _KERN_IVTab_setvector (TPU_VECTORBASE + ch, isr);
00390
00391 }
00392
00393 int TPU_clearisr (unsigned char ch, void (*isr) (void))
00394 {
00395 return _KERN_IVTab_clearvector (TPU_VECTORBASE + ch, isr);
00396
00397 }