00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "CeylanARM7Base.h"
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #include "CeylanARM7Codes.h"
00041
00042
00043 #include "CeylanIPCCommands.h"
00044
00045
00046
00047
00048
00049
00050
00051 #define PM_BATTERY_STATUS BIT(0)
00052 #define PM_DSLITE_REG (4)
00053 #define PM_IS_LITE BIT(6)
00054
00055
00056 const InterruptMask AllInterruptsDisabled = 0 ;
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067 touchPosition first, tempPos ;
00068
00069
00070
00071 void VcountHandler()
00072 {
00073
00074
00075
00076 static int lastbut = -1 ;
00077
00078 uint16 but=0, x=0, y=0, xpx=0, ypx=0, z1=0, z2=0 ;
00079
00080 but = REG_KEYXY ;
00081
00082 if ( ! ( (but ^ lastbut) & (1<<6) ) )
00083 {
00084
00085 tempPos = touchReadXY() ;
00086
00087 if ( tempPos.x == 0 || tempPos.y == 0 )
00088 {
00089
00090 but |= (1 <<6) ;
00091 lastbut = but ;
00092
00093 }
00094 else
00095 {
00096
00097 x = tempPos.x ;
00098 y = tempPos.y ;
00099 xpx = tempPos.px ;
00100 ypx = tempPos.py ;
00101 z1 = tempPos.z1 ;
00102 z2 = tempPos.z2 ;
00103
00104 }
00105
00106 }
00107 else
00108 {
00109 lastbut = but ;
00110 but |= (1 <<6) ;
00111 }
00112
00113 IPC->touchX = x ;
00114 IPC->touchY = y ;
00115 IPC->touchXpx = xpx ;
00116 IPC->touchYpx = ypx ;
00117 IPC->touchZ1 = z1 ;
00118 IPC->touchZ2 = z2 ;
00119 IPC->buttons = but ;
00120
00121 }
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144 ARM7StatusWord volatile * volatile statusWordPointer = 0 ;
00145
00146
00147 ARM7ErrorCode volatile * volatile errorWordPointer = 0 ;
00148
00149
00150
00151 volatile bool IPCRunning = false ;
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163 volatile FIFOCommandCount localCommandCount = 0 ;
00164
00165
00166
00167
00168
00169
00170
00171 volatile FIFOCommandCount remoteCommandCount = 0 ;
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182 volatile FIFOCommandCount processedCount = 0 ;
00183
00184
00185
00186
00187
00188
00189
00190 volatile FIFOCommandCount sentCount = 0 ;
00191
00192
00193
00194
00195
00196
00197
00198
00199 void atomicSleep()
00200 {
00201
00202 swiWaitForVBlank() ;
00203
00204 }
00205
00206
00207
00208
00209
00210
00218 void setStatusWord( ARM7StatusWord newStatus )
00219 {
00220
00221 if ( statusWordPointer != 0 )
00222 {
00223
00224 if ( *statusWordPointer != ARM7InError )
00225 *statusWordPointer = newStatus ;
00226
00227 }
00228
00229 }
00230
00231
00232
00241 void setError( ARM7ErrorCode newError )
00242 {
00243
00244 setStatusWord( ARM7InError ) ;
00245
00246 if ( errorWordPointer != 0 )
00247 {
00248
00249 if ( *errorWordPointer == NoError )
00250 *errorWordPointer = newError ;
00251
00252 }
00253
00254 }
00255
00256
00257
00263 void unsetErrorStatus()
00264 {
00265
00266
00267 if ( statusWordPointer != 0 )
00268 *statusWordPointer = NoStatusAvailable ;
00269
00270 if ( errorWordPointer != 0 )
00271 *errorWordPointer = NoError ;
00272
00273 }
00274
00275
00276
00277
00278 FIFOElement prepareFIFOCommand( FIFOCommandID id )
00279 {
00280
00281 FIFOElement res = 0 ;
00282
00283 #if CEYLAN_SAFE_FIFO
00284
00285
00286 res |= ( id << 24 ) | ( localCommandCount << 16 ) ;
00287
00288
00289 localCommandCount++ ;
00290
00291 #else // CEYLAN_SAFE_FIFO
00292
00293 res |= id << 24 ;
00294
00295 #endif // CEYLAN_SAFE_FIFO
00296
00297 return res ;
00298
00299 }
00300
00301
00302
00303 FIFOCommandID getFIFOCommandIDFrom( FIFOElement element )
00304 {
00305
00306 return ( element & 0xff000000 ) >> 24 ;
00307
00308 }
00309
00310
00311
00312 FIFOCommandCount getFIFOCommandCountFrom( FIFOElement element )
00313 {
00314
00315 return ( element & 0x00ff0000 ) >> 16 ;
00316
00317 }
00318
00319
00320
00321 FIFOCommandCount getARM7ProcessedCount()
00322 {
00323
00324
00325 return REG_IPC_SYNC & 0x0f ;
00326
00327 }
00328
00329
00330
00331 FIFOCommandCount getARM9ProcessedCount()
00332 {
00333
00334
00335 return ( REG_IPC_SYNC & 0x0f00 ) >> 8 ;
00336
00337 }
00338
00339
00340
00341
00342 FIFOCommandCount getProcessedCount()
00343 {
00344
00345
00346 return ( processedCount & 0x0f ) ;
00347
00348 }
00349
00350
00351
00352 FIFOCommandCount getSentCount()
00353 {
00354
00355 return ( sentCount & 0x0f ) ;
00356
00357 }
00358
00359
00360
00361
00362
00363
00364
00365
00366 void sendSynchronizeInterruptToARM9()
00367 {
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378 REG_IPC_SYNC = REG_IPC_SYNC | IPC_SYNC_IRQ_REQUEST ;
00379
00380 }
00381
00382
00383
00384 void incrementProcessCount()
00385 {
00386
00387 processedCount++ ;
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401 }
00402
00403
00404
00417 InterruptMask setEnabledInterrupts( InterruptMask newMask )
00418 {
00419
00420 InterruptMask previousMask = REG_IME ;
00421
00422 REG_IME = newMask ;
00423
00424 return previousMask ;
00425
00426 }
00427
00428
00429
00430 void notifyCommandToARM9()
00431 {
00432
00433 sentCount++ ;
00434 sendSynchronizeInterruptToARM9() ;
00435
00436 }
00437
00438
00439
00440 bool dataAvailableForReading()
00441 {
00442
00443 return ! ( REG_IPC_FIFO_CR & IPC_FIFO_RECV_EMPTY ) ;
00444
00445 }
00446
00447
00448
00449 bool spaceAvailableForWriting()
00450 {
00451
00452 return ! ( REG_IPC_FIFO_CR & IPC_FIFO_SEND_FULL ) ;
00453
00454 }
00455
00456
00457
00458
00459 FIFOElement read()
00460 {
00461
00462 #if CEYLAN_SAFE_FIFO
00463
00464 if ( REG_IPC_FIFO_CR & IPC_FIFO_ERROR )
00465 setError( FIFOErrorWhileReading ) ;
00466
00467 #endif // CEYLAN_SAFE_FIFO
00468
00469
00470 if ( ! dataAvailableForReading() )
00471 setError( FIFOErrorWhileReading ) ;
00472
00473 FIFOElement res = REG_IPC_FIFO_RX ;
00474
00475
00476 #if CEYLAN_SAFE_FIFO
00477
00478 if ( REG_IPC_FIFO_CR & IPC_FIFO_ERROR )
00479 setError( FIFOErrorWhileReading ) ;
00480
00481 #endif // CEYLAN_SAFE_FIFO
00482
00483 return res ;
00484
00485 }
00486
00487
00488
00489
00490 FIFOElement readBlocking()
00491 {
00492
00493 #if CEYLAN_SAFE_FIFO
00494
00495 if ( REG_IPC_FIFO_CR & IPC_FIFO_ERROR )
00496 setError( FIFOErrorWhileReading ) ;
00497
00498 #endif // CEYLAN_SAFE_FIFO
00499
00500
00501 uint32 attemptCount = 100000 ;
00502
00503
00504
00505 while ( ! dataAvailableForReading() && attemptCount > 0 )
00506 attemptCount-- ;
00507
00508
00509 if ( attemptCount == 0 )
00510 {
00511
00512 setError( FIFOTimeOutWhileReading ) ;
00513
00514
00515 while( ! dataAvailableForReading() )
00516 ;
00517
00518
00519 unsetErrorStatus() ;
00520 setStatusWord( StatusReset ) ;
00521
00522 }
00523
00524
00525 #if CEYLAN_SAFE_FIFO
00526
00527 if ( REG_IPC_FIFO_CR & IPC_FIFO_ERROR )
00528 setError( FIFOErrorWhileReading ) ;
00529
00530 #endif // CEYLAN_SAFE_FIFO
00531
00532
00533 FIFOElement res = REG_IPC_FIFO_RX ;
00534
00535
00536 #if CEYLAN_SAFE_FIFO
00537
00538 if ( REG_IPC_FIFO_CR & IPC_FIFO_ERROR )
00539 setError( FIFOErrorWhileReading ) ;
00540
00541 #endif // CEYLAN_SAFE_FIFO
00542
00543
00544 return res ;
00545
00546 }
00547
00548
00549
00550
00551 void write( FIFOElement toSend )
00552 {
00553
00554 #if CEYLAN_SAFE_FIFO
00555
00556 if ( REG_IPC_FIFO_CR & IPC_FIFO_ERROR )
00557 setError( FIFOErrorWhileWriting ) ;
00558
00559 #endif // CEYLAN_SAFE_FIFO
00560
00561
00562 if ( ! spaceAvailableForWriting() )
00563 setError( FIFOErrorWhileWriting ) ;
00564
00565
00566 #if CEYLAN_SAFE_FIFO
00567
00568 if ( REG_IPC_FIFO_CR & IPC_FIFO_ERROR )
00569 setError( FIFOErrorWhileWriting ) ;
00570
00571 #endif // CEYLAN_SAFE_FIFO
00572
00573
00574 REG_IPC_FIFO_TX = toSend ;
00575
00576
00577 #if CEYLAN_SAFE_FIFO
00578
00579 if ( REG_IPC_FIFO_CR & IPC_FIFO_ERROR )
00580 setError( FIFOErrorWhileWriting ) ;
00581
00582 #endif // CEYLAN_SAFE_FIFO
00583
00584 }
00585
00586
00587
00588
00589
00590
00591
00592 void writeBlocking( FIFOElement toSend )
00593 {
00594
00595 #if CEYLAN_SAFE_FIFO
00596
00597 if ( REG_IPC_FIFO_CR & IPC_FIFO_ERROR )
00598 setError( FIFOErrorWhileWriting ) ;
00599
00600 #endif // CEYLAN_SAFE_FIFO
00601
00602
00603 uint32 attemptCount = 100000 ;
00604
00605
00606
00607 while ( ! spaceAvailableForWriting() && attemptCount > 0 )
00608 attemptCount-- ;
00609
00610
00611
00612 if ( attemptCount == 0 )
00613 {
00614
00615 setError( FIFOTimeOutWhileWriting ) ;
00616
00617
00618 sendSynchronizeInterruptToARM9() ;
00619
00620 while ( ! spaceAvailableForWriting() )
00621 ;
00622
00623
00624 unsetErrorStatus() ;
00625 setStatusWord( StatusReset ) ;
00626
00627 }
00628
00629 #if CEYLAN_SAFE_FIFO
00630
00631 if ( REG_IPC_FIFO_CR & IPC_FIFO_ERROR )
00632 setError( FIFOErrorWhileWriting ) ;
00633
00634 #endif // CEYLAN_SAFE_FIFO
00635
00636
00637 REG_IPC_FIFO_TX = toSend ;
00638
00639
00640 #if CEYLAN_SAFE_FIFO
00641
00642 if ( REG_IPC_FIFO_CR & IPC_FIFO_ERROR )
00643 setError( FIFOErrorWhileWriting ) ;
00644
00645 #endif // CEYLAN_SAFE_FIFO
00646
00647 }
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657 void handleStatusInitRequest()
00658 {
00659
00660 if ( statusWordPointer != 0 || IPCRunning )
00661 {
00662
00663 setError( IPCAlreadyStarted ) ;
00664 return ;
00665
00666 }
00667
00668
00669
00670
00671
00672
00673 statusWordPointer = (volatile ARM7StatusWord*) readBlocking() ;
00674
00675 if ( *statusWordPointer != NoStatusAvailable )
00676 {
00677
00678 setError( IncorrectInitialStatus ) ;
00679 return ;
00680
00681 }
00682
00683 *statusWordPointer = ARM7Running ;
00684
00685
00686 errorWordPointer = (volatile ARM7ErrorCode*) readBlocking() ;
00687
00688 if ( *errorWordPointer != NoErrorAvailable )
00689 {
00690
00691 setError( IncorrectInitialError ) ;
00692 return ;
00693
00694 }
00695
00696 *errorWordPointer = NoError ;
00697
00698
00699 IPCRunning = true ;
00700
00701 }
00702
00703
00704
00705
00706 void handleShutdownIPCRequest()
00707 {
00708
00709 if ( statusWordPointer == 0 || errorWordPointer == 0 || ! IPCRunning )
00710 {
00711
00712 setError( IPCAlreadyStopped ) ;
00713 return ;
00714
00715 }
00716
00717 IPCRunning = false ;
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728 *statusWordPointer = ARM7IPCShutdown ;
00729 *errorWordPointer = NoError ;
00730
00731 statusWordPointer = 0 ;
00732 errorWordPointer = 0 ;
00733
00734
00735 REG_IPC_FIFO_CR = REG_IPC_FIFO_CR & ~IPC_FIFO_ENABLE ;
00736
00737 irqDisable( IRQ_IPC_SYNC ) ;
00738
00739
00740
00741
00742
00743
00744 }
00745
00746
00747
00748
00749 void handleBatteryStatusRequest()
00750 {
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760 FIFOElement answer = prepareFIFOCommand( BatteryStatusAnswer ) ;
00761
00762 if ( ! ( readPowerManagement( PM_BATTERY_REG ) & PM_BATTERY_STATUS ) )
00763 answer |= 1 ;
00764
00765 writeBlocking( answer ) ;
00766
00767 notifyCommandToARM9() ;
00768
00769 }
00770
00771
00772
00773
00774 void handleDSTypeRequest()
00775 {
00776
00777
00778
00779
00780
00781
00782
00783
00784
00785 FIFOElement answer = prepareFIFOCommand( DSTypeAnswer ) ;
00786
00787
00788 if ( readPowerManagement( PM_DSLITE_REG ) & PM_IS_LITE )
00789 answer |= 2 ;
00790 else
00791 answer |= 1 ;
00792
00793 writeBlocking( answer ) ;
00794
00795 notifyCommandToARM9() ;
00796
00797 }
00798
00799
00800
00801
00802
00803
00804
00805
00806
00807
00808
00809 void handleReceivedSystemSpecificCommand( FIFOCommandID commandID,
00810 FIFOElement firstElement )
00811 {
00812
00813
00814
00815
00816
00817
00818
00819
00820
00821
00822
00823
00824
00825
00826
00827
00828
00829
00830
00831
00832
00833
00834
00835
00836
00837
00838
00839
00840
00841
00842
00843
00844
00845
00846
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857
00858
00859 if ( commandID == HelloToTheARM7 )
00860 setError( UnexpectedBehaviour ) ;
00861 else if ( commandID == ShutdownIPCRequest )
00862 handleShutdownIPCRequest() ;
00863 else if ( commandID == StatusInitRequest )
00864 handleStatusInitRequest() ;
00865 else if ( commandID == BatteryStatusRequest )
00866 handleBatteryStatusRequest() ;
00867 else if ( commandID == DSTypeRequest )
00868 handleDSTypeRequest() ;
00869 else
00870 setError( UnexpectedSystemCommand ) ;
00871
00872 }
00873
00874
00875
00876
00877
00878
00879
00880
00881
00882
00883
00884
00885
00886
00887
00888
00889
00890
00891
00892
00893 void handleReceivedCommand()
00894 {
00895
00896 if ( ! dataAvailableForReading() )
00897 {
00898
00899
00900
00901
00902
00903
00904
00905
00906
00907 return ;
00908
00909 }
00910
00911
00912
00913
00914
00915
00916
00917
00918 #if CEYLAN_SAFE_FIFO
00919
00920 static bool CommandInProgress = false ;
00921
00922
00923 if ( ! CommandInProgress )
00924 {
00925
00926 CommandInProgress = true ;
00927
00928 #endif // CEYLAN_SAFE_FIFO
00929
00930
00931 FIFOElement firstElement ;
00932 FIFOCommandID id ;
00933
00934
00935
00936 while ( dataAvailableForReading() )
00937 {
00938
00939
00940
00941
00942 firstElement = readBlocking() ;
00943
00944
00945 #if CEYLAN_SAFE_FIFO
00946
00947 FIFOCommandCount count = getFIFOCommandCountFrom( firstElement ) ;
00948
00949 if ( count != remoteCommandCount )
00950 {
00951
00952 setError( UnexpectedCommandCount ) ;
00953
00954
00955 return ;
00956
00957 }
00958
00959 remoteCommandCount++ ;
00960
00961 #endif // CEYLAN_SAFE_FIFO
00962
00963
00964 id = getFIFOCommandIDFrom( firstElement ) ;
00965
00966 if ( id > 127 )
00967 {
00968
00969
00970 handleReceivedApplicationCommand( id, firstElement ) ;
00971
00972 }
00973 else
00974 {
00975
00976 if ( id < 33 )
00977 {
00978
00979
00980 handleReceivedSystemSpecificCommand( id, firstElement ) ;
00981
00982 }
00983 else
00984 {
00985
00986
00987 handleReceivedIntegratingLibrarySpecificCommand( id,
00988 firstElement ) ;
00989
00990 }
00991
00992 }
00993
00994 incrementProcessCount() ;
00995
00996
00997 }
00998
00999
01000 #if CEYLAN_SAFE_FIFO
01001
01002 CommandInProgress = false ;
01003
01004 }
01005 else
01006 {
01007
01008 setError( CommandOverlapping ) ;
01009
01010 }
01011
01012 #endif // CEYLAN_SAFE_FIFO
01013
01014 }
01015
01016
01017
01018
01024 void initCeylanIPC()
01025 {
01026
01027
01028
01029
01030
01031
01032
01033
01034
01035
01036 REG_IPC_SYNC = IPC_SYNC_IRQ_ENABLE ;
01037
01038
01039
01040
01041
01042
01043
01044
01045
01046
01047
01048
01049 REG_IPC_FIFO_CR = IPC_FIFO_ENABLE | IPC_FIFO_SEND_CLEAR ;
01050
01051
01052
01053
01054
01055
01056
01057
01058
01059
01060
01061
01062
01063
01064
01065
01066
01067
01068 irqSet( IRQ_IPC_SYNC, handleReceivedCommand ) ;
01069
01070
01071
01072 irqSet( IRQ_VBLANK, handleReceivedCommand ) ;
01073
01074
01075 irqEnable( IRQ_IPC_SYNC | IRQ_VBLANK ) ;
01076
01077
01078
01079
01080
01081
01082 while( ! IPCRunning )
01083 ;
01084
01085
01086
01087 atomicSleep() ;
01088
01089 }
01090
01091
01092
01097 void initCeylanSound()
01098 {
01099
01100 InterruptMask previous = setEnabledInterrupts( AllInterruptsDisabled ) ;
01101
01102 powerON( POWER_SOUND ) ;
01103
01104 writePowerManagement( PM_CONTROL_REG, ( readPowerManagement(PM_CONTROL_REG)
01105 & ~PM_SOUND_MUTE ) | PM_SOUND_AMP ) ;
01106
01107 SOUND_CR = SOUND_ENABLE | SOUND_VOL( 0x7f ) ;
01108 IPC->soundData = 0;
01109
01110 setEnabledInterrupts( previous ) ;
01111
01112
01113 atomicSleep() ;
01114
01115 }
01116
01117
01118
01123 void initCeylan()
01124 {
01125
01126
01127 readUserSettings() ;
01128
01129
01130 rtcReset() ;
01131
01132 irqInit() ;
01133
01134 SetYtrigger( 80 ) ;
01135 irqSet( IRQ_VCOUNT, VcountHandler ) ;
01136 irqEnable( IRQ_VCOUNT ) ;
01137
01138 IPC->mailBusy = 0 ;
01139
01140
01141 initCeylanIPC() ;
01142
01143 initCeylanSound() ;
01144
01145 }
01146