1
1
#[ cfg( any( feature = "stm32f042" , feature = "stm32f030" ) ) ]
2
2
use crate :: stm32:: { I2C1 , RCC } ;
3
3
4
+ use crate :: stm32;
4
5
use embedded_hal:: blocking:: i2c:: { Write , WriteRead } ;
5
6
6
7
use crate :: gpio:: * ;
7
8
use crate :: time:: { KiloHertz , U32Ext } ;
8
9
use core:: cmp;
9
10
10
11
/// I2C abstraction
11
- pub struct I2c < I2C , PINS > {
12
+ pub struct I2c < I2C , SCLPIN , SDAPIN > {
12
13
i2c : I2C ,
13
- pins : PINS ,
14
+ pins : ( SCLPIN , SDAPIN ) ,
14
15
}
15
16
16
- pub trait Pins < I2c > { }
17
+ pub trait SclPin < I2C > { }
18
+ pub trait SdaPin < I2C > { }
19
+
20
+ macro_rules! i2c_pins {
21
+ ( $( $I2C: ident => {
22
+ scl => [ $( $scl: ty) ,+ $( , ) * ] ,
23
+ sda => [ $( $sda: ty) ,+ $( , ) * ] ,
24
+ } ) +) => {
25
+ $(
26
+ $(
27
+ impl SclPin <stm32:: $I2C> for $scl { }
28
+ ) +
29
+ $(
30
+ impl SdaPin <stm32:: $I2C> for $sda { }
31
+ ) +
32
+ ) +
33
+ }
34
+ }
17
35
36
+ #[ cfg( any( feature = "stm32f042" , feature = "stm32f030" ) ) ]
37
+ i2c_pins ! {
38
+ I2C1 => {
39
+ scl => [ gpioa:: PA11 <Alternate <AF5 >>, gpiob:: PB6 <Alternate <AF1 >>, gpiob:: PB8 <Alternate <AF1 >>] ,
40
+ sda => [ gpioa:: PA12 <Alternate <AF5 >>, gpiob:: PB7 <Alternate <AF1 >>, gpiob:: PB9 <Alternate <AF1 >>] ,
41
+ }
42
+ }
18
43
#[ cfg( any(
19
44
feature = "stm32f042" ,
20
45
feature = "stm32f030x6" ,
21
46
feature = "stm32f030xc"
22
47
) ) ]
23
- impl Pins < I2C1 > for ( gpioa:: PA9 < Alternate < AF4 > > , gpioa:: PA10 < Alternate < AF4 > > ) { }
24
- #[ cfg( any( feature = "stm32f042" , feature = "stm32f030" ) ) ]
25
- impl Pins < I2C1 > for ( gpioa:: PA11 < Alternate < AF5 > > , gpioa:: PA12 < Alternate < AF5 > > ) { }
26
- #[ cfg( any( feature = "stm32f042" , feature = "stm32f030" ) ) ]
27
- impl Pins < I2C1 > for ( gpiob:: PB6 < Alternate < AF1 > > , gpiob:: PB7 < Alternate < AF1 > > ) { }
28
- #[ cfg( any( feature = "stm32f042" , feature = "stm32f030" ) ) ]
29
- impl Pins < I2C1 > for ( gpiob:: PB8 < Alternate < AF1 > > , gpiob:: PB9 < Alternate < AF1 > > ) { }
48
+ i2c_pins ! {
49
+ I2C1 => {
50
+ scl => [ gpioa:: PA9 <Alternate <AF4 >>] ,
51
+ sda => [ gpioa:: PA10 <Alternate <AF4 >>] ,
52
+ }
53
+ }
30
54
#[ cfg( any( feature = "stm32f042" , feature = "stm32f030x6" ) ) ]
31
- impl Pins < I2C1 > for ( gpiob:: PB10 < Alternate < AF1 > > , gpiob:: PB11 < Alternate < AF1 > > ) { }
32
- #[ cfg( any( feature = "stm32f042" , feature = "stm32f030xc" ) ) ]
33
- impl Pins < I2C1 > for ( gpiob:: PB13 < Alternate < AF5 > > , gpiob:: PB14 < Alternate < AF5 > > ) { }
55
+ i2c_pins ! {
56
+ I2C1 => {
57
+ scl => [ gpiob:: PB10 <Alternate <AF1 >>] ,
58
+ sda => [ gpiob:: PB11 <Alternate <AF1 >>] ,
59
+ }
60
+ }
34
61
#[ cfg( any( feature = "stm32f042" , feature = "stm32f030xc" ) ) ]
35
- impl Pins < I2C1 > for ( gpiof:: PF1 < Alternate < AF1 > > , gpiof:: PF0 < Alternate < AF1 > > ) { }
62
+ i2c_pins ! {
63
+ I2C1 => {
64
+ scl => [ gpiob:: PB13 <Alternate <AF5 >>, gpiof:: PF1 <Alternate <AF1 >>] ,
65
+ sda => [ gpiob:: PB14 <Alternate <AF5 >>, gpiof:: PF0 <Alternate <AF1 >>] ,
66
+ }
67
+ }
36
68
37
69
#[ derive( Debug ) ]
38
70
pub enum Error {
@@ -41,10 +73,11 @@ pub enum Error {
41
73
}
42
74
43
75
#[ cfg( any( feature = "stm32f042" , feature = "stm32f030" ) ) ]
44
- impl < PINS > I2c < I2C1 , PINS > {
45
- pub fn i2c1 ( i2c : I2C1 , pins : PINS , speed : KiloHertz ) -> Self
76
+ impl < SCLPIN , SDAPIN > I2c < I2C1 , SCLPIN , SDAPIN > {
77
+ pub fn i2c1 ( i2c : I2C1 , pins : ( SCLPIN , SDAPIN ) , speed : KiloHertz ) -> Self
46
78
where
47
- PINS : Pins < I2C1 > ,
79
+ SCLPIN : SclPin < I2C1 > ,
80
+ SDAPIN : SdaPin < I2C1 > ,
48
81
{
49
82
// NOTE(unsafe) This executes only during initialisation
50
83
let rcc = unsafe { & ( * RCC :: ptr ( ) ) } ;
@@ -104,7 +137,7 @@ impl<PINS> I2c<I2C1, PINS> {
104
137
I2c { i2c, pins }
105
138
}
106
139
107
- pub fn release ( self ) -> ( I2C1 , PINS ) {
140
+ pub fn release ( self ) -> ( I2C1 , ( SCLPIN , SDAPIN ) ) {
108
141
( self . i2c , self . pins )
109
142
}
110
143
@@ -134,7 +167,7 @@ impl<PINS> I2c<I2C1, PINS> {
134
167
}
135
168
136
169
#[ cfg( any( feature = "stm32f042" , feature = "stm32f030" ) ) ]
137
- impl < PINS > WriteRead for I2c < I2C1 , PINS > {
170
+ impl < SCLPIN , SDAPIN > WriteRead for I2c < I2C1 , SCLPIN , SDAPIN > {
138
171
type Error = Error ;
139
172
140
173
fn write_read ( & mut self , addr : u8 , bytes : & [ u8 ] , buffer : & mut [ u8 ] ) -> Result < ( ) , Error > {
@@ -212,7 +245,7 @@ impl<PINS> WriteRead for I2c<I2C1, PINS> {
212
245
}
213
246
214
247
#[ cfg( any( feature = "stm32f042" , feature = "stm32f030" ) ) ]
215
- impl < PINS > Write for I2c < I2C1 , PINS > {
248
+ impl < SCLPIN , SDAPIN > Write for I2c < I2C1 , SCLPIN , SDAPIN > {
216
249
type Error = Error ;
217
250
218
251
fn write ( & mut self , addr : u8 , bytes : & [ u8 ] ) -> Result < ( ) , Error > {
0 commit comments