@@ -8,7 +8,20 @@ extern crate panic_halt;
8
8
9
9
use cortex_m_rt:: entry;
10
10
use nb:: block;
11
- use stm32f1xx_hal:: { pac, prelude:: * , timer:: Timer } ;
11
+ use stm32f1xx_hal:: {
12
+ i2c:: { BlockingI2c , DutyCycle , Mode } ,
13
+ pac,
14
+ prelude:: * ,
15
+ timer:: Timer ,
16
+ } ;
17
+
18
+ use embedded_graphics:: {
19
+ mono_font:: { ascii:: FONT_6X10 , MonoTextStyle } ,
20
+ pixelcolor:: BinaryColor ,
21
+ prelude:: * ,
22
+ text:: Text ,
23
+ } ;
24
+ use sh1106:: { prelude:: * , Builder } ;
12
25
13
26
// use `main` as the entry point of this application
14
27
#[ entry]
@@ -25,6 +38,8 @@ fn main() -> ! {
25
38
// Freeze the configuration of all the clocks in the system and store the frozen frequencies in `clocks`
26
39
let clocks = rcc. cfgr . freeze ( & mut flash. acr ) ;
27
40
41
+ let mut afio = dp. AFIO . constrain ( ) ;
42
+
28
43
// Configure GPIOA PA0 and PA2 as output (push-pull)
29
44
let mut gpioa = dp. GPIOA . split ( ) ;
30
45
let mut led_a0 = gpioa. pa0 . into_push_pull_output ( & mut gpioa. crl ) ;
@@ -35,6 +50,36 @@ fn main() -> ! {
35
50
let button_b1 = gpiob. pb1 . into_floating_input ( & mut gpiob. crl ) ;
36
51
let button_b11 = gpiob. pb11 . into_floating_input ( & mut gpiob. crh ) ;
37
52
53
+ let scl = gpiob. pb8 . into_alternate_open_drain ( & mut gpiob. crh ) ;
54
+ let sda = gpiob. pb9 . into_alternate_open_drain ( & mut gpiob. crh ) ;
55
+
56
+ let i2c = BlockingI2c :: i2c1 (
57
+ dp. I2C1 ,
58
+ ( scl, sda) ,
59
+ & mut afio. mapr ,
60
+ Mode :: Fast {
61
+ frequency : 100 . kHz ( ) . into ( ) ,
62
+ duty_cycle : DutyCycle :: Ratio2to1 ,
63
+ } ,
64
+ clocks,
65
+ 1000 ,
66
+ 10 ,
67
+ 1000 ,
68
+ 1000 ,
69
+ ) ;
70
+
71
+ let mut display: GraphicsMode < _ > = Builder :: new ( ) . connect_i2c ( i2c) . into ( ) ;
72
+
73
+ display. init ( ) . unwrap ( ) ;
74
+ display. flush ( ) . unwrap ( ) ;
75
+
76
+ let style = MonoTextStyle :: new ( & FONT_6X10 , BinaryColor :: On ) ;
77
+ Text :: new ( "Hello world!" , Point :: zero ( ) , style)
78
+ . draw ( & mut display)
79
+ . unwrap ( ) ;
80
+
81
+ display. flush ( ) . unwrap ( ) ;
82
+
38
83
// Configure the syst timer to trigger an update every 0.1 second
39
84
let mut timer = Timer :: syst ( cp. SYST , & clocks) . counter_hz ( ) ;
40
85
timer. start ( 100 . Hz ( ) ) . unwrap ( ) ;
@@ -46,26 +91,14 @@ fn main() -> ! {
46
91
if button_b1. is_low ( ) {
47
92
while button_b1. is_low ( ) { } // do nothing while button is held down
48
93
49
- if led_a0. is_set_low ( ) {
50
- // if PA0 is currently on
51
- led_a0. set_high ( ) ; // Set PA0 to high, turning it off
52
- } else {
53
- // if PA0 is currently off
54
- led_a0. set_low ( ) ; // Set PA0 to low, turning it on
55
- }
94
+ led_a0. toggle ( ) // Set PA0 to the opposite state
56
95
}
57
96
58
97
// read PB11
59
98
if button_b11. is_low ( ) {
60
99
while button_b11. is_low ( ) { } // do nothing while button is held down
61
100
62
- if led_a2. is_set_low ( ) {
63
- // if PA2 is currently on
64
- led_a2. set_high ( ) ; // Set PA2 to high, turning it off
65
- } else {
66
- // if PA2 is currently off
67
- led_a2. set_low ( ) ; // Set PA2 to low, turning it on
68
- }
101
+ led_a2. toggle ( ) // Set PA0 to the opposite state
69
102
}
70
103
}
71
104
}
0 commit comments