1+ #![ no_std]
2+ #![ no_main]
3+
4+ use gd32vf103xx_hal:: pac:: Interrupt ;
5+ use panic_halt as _;
6+ use longan_nano:: hal:: { pac, prelude:: * , pac:: * , eclic:: * } ;
7+ use gd32vf103xx_hal:: timer;
8+ use gd32vf103xx_hal:: timer:: Timer ;
9+ use longan_nano:: led:: { rgb, Led , RED } ;
10+ use riscv_rt:: entry;
11+
12+ static mut R_LED : Option < RED > = None ;
13+ static mut G_TIMER1 : Option < Timer < TIMER1 > > = None ;
14+
15+ #[ entry]
16+ fn main ( ) -> ! {
17+ let dp = pac:: Peripherals :: take ( ) . unwrap ( ) ;
18+ let mut rcu = dp
19+ . RCU
20+ . configure ( )
21+ . ext_hf_clock ( 8 . mhz ( ) )
22+ . sysclk ( 108 . mhz ( ) )
23+ . freeze ( ) ;
24+
25+ let gpioa = dp. GPIOA . split ( & mut rcu) ;
26+ let gpioc = dp. GPIOC . split ( & mut rcu) ;
27+
28+ let ( mut red, mut green, mut blue) = rgb ( gpioc. pc13 , gpioa. pa1 , gpioa. pa2 ) ;
29+ red. off ( ) ;
30+ green. off ( ) ;
31+ blue. off ( ) ;
32+ unsafe { R_LED = Some ( red) ; } ;
33+
34+ ECLIC :: reset ( ) ;
35+ ECLIC :: set_threshold_level ( Level :: L0 ) ;
36+ ECLIC :: set_level_priority_bits ( LevelPriorityBits :: L3P1 ) ;
37+
38+ // timer
39+ let mut timer = Timer :: timer1 ( dp. TIMER1 , 1 . hz ( ) , & mut rcu) ;
40+ timer. listen ( timer:: Event :: Update ) ;
41+ unsafe { G_TIMER1 = Some ( timer) } ;
42+
43+ ECLIC :: setup (
44+ Interrupt :: TIMER1 ,
45+ TriggerType :: Level ,
46+ Level :: L1 ,
47+ Priority :: P1 ,
48+ ) ;
49+ unsafe {
50+ ECLIC :: unmask ( Interrupt :: TIMER1 ) ;
51+ riscv:: interrupt:: enable ( ) ;
52+ } ;
53+
54+ loop { }
55+ }
56+
57+ #[ allow( non_snake_case) ]
58+ #[ no_mangle]
59+ fn TIMER1 ( ) {
60+ unsafe {
61+ if let Some ( timer1) = G_TIMER1 . as_mut ( ) {
62+ timer1. clear_update_interrupt_flag ( ) ;
63+ }
64+ if let Some ( led) = R_LED . as_mut ( ) {
65+ if led. is_on ( ) {
66+ led. off ( ) ;
67+ } else {
68+ led. on ( ) ;
69+ }
70+ }
71+ }
72+ }
0 commit comments