@@ -1,136 +1,136 | |||||
1 | /*------------------------------------------------------------------------------ |
|
1 | /*------------------------------------------------------------------------------ | |
2 | -- This file is a part of the libuc, microcontroler library |
|
2 | -- This file is a part of the libuc, microcontroler library | |
3 | -- Copyright (C) 2011, Alexis Jeandet |
|
3 | -- Copyright (C) 2011, Alexis Jeandet | |
4 | -- |
|
4 | -- | |
5 | -- This program is free software; you can redistribute it and/or modify |
|
5 | -- This program is free software; you can redistribute it and/or modify | |
6 | -- it under the terms of the GNU General Public License as published by |
|
6 | -- it under the terms of the GNU General Public License as published by | |
7 | -- the Free Software Foundation; either version 3 of the License, or |
|
7 | -- the Free Software Foundation; either version 3 of the License, or | |
8 | -- (at your option) any later version. |
|
8 | -- (at your option) any later version. | |
9 | -- |
|
9 | -- | |
10 | -- This program is distributed in the hope that it will be useful, |
|
10 | -- This program is distributed in the hope that it will be useful, | |
11 | -- but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | -- but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | -- GNU General Public License for more details. |
|
13 | -- GNU General Public License for more details. | |
14 | -- |
|
14 | -- | |
15 | -- You should have received a copy of the GNU General Public License |
|
15 | -- You should have received a copy of the GNU General Public License | |
16 | -- along with this program; if not, write to the Free Software |
|
16 | -- along with this program; if not, write to the Free Software | |
17 | -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
17 | -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | ------------------------------------------------------------------------------- |
|
18 | ------------------------------------------------------------------------------- | |
19 | -- Author : Alexis Jeandet |
|
19 | -- Author : Alexis Jeandet | |
20 | -- Mail : alexis.jeandet@gmail.com |
|
20 | -- Mail : alexis.jeandet@gmail.com | |
21 | -------------------------------------------------------------------------------*/ |
|
21 | -------------------------------------------------------------------------------*/ | |
22 | #include "bsp.h" |
|
22 | #include "bsp.h" | |
23 | #include <streamdevices.h> |
|
23 | #include <streamdevices.h> | |
24 | #include <malloc.h> |
|
24 | #include <malloc.h> | |
25 | #include <gpio.h> |
|
25 | #include <gpio.h> | |
26 | #include <uart.h> |
|
26 | #include <uart.h> | |
27 | #include <stdio.h> |
|
27 | #include <stdio.h> | |
28 | #include <i2c.h> |
|
28 | #include <i2c.h> | |
29 |
|
29 | |||
30 | uint32_t OSC0 =8000000; |
|
30 | uint32_t OSC0 =8000000; | |
31 | uint32_t INTOSC =16000000; |
|
31 | uint32_t INTOSC =16000000; | |
32 | uint32_t RTCOSC =32768; |
|
32 | uint32_t RTCOSC =32768; | |
33 | uint32_t currentCpuFreq=0; |
|
33 | uint32_t currentCpuFreq=0; | |
34 | extern streamdevice* __opnfiles__[__MAX_OPENED_FILES__]; |
|
34 | extern streamdevice* __opnfiles__[__MAX_OPENED_FILES__]; | |
35 |
|
35 | |||
36 | float VREF0 =(float)3.3; |
|
36 | float VREF0 =(float)3.3; | |
37 |
|
37 | |||
38 | int bsp_init() |
|
38 | int bsp_init() | |
39 | { |
|
39 | { | |
40 | int i=0; |
|
40 | int i=0; | |
41 | for(i=0;i<32;i++) |
|
41 | for(i=0;i<32;i++) | |
42 | { |
|
42 | { | |
43 | __opnfiles__[i] = NULL; |
|
43 | __opnfiles__[i] = NULL; | |
44 | } |
|
44 | } | |
45 | bsp_GPIO_init(); |
|
45 | bsp_GPIO_init(); | |
46 | bsp_uart_init(); |
|
46 | bsp_uart_init(); | |
47 | bsp_iic_init(); |
|
47 | bsp_iic_init(); | |
48 | printf("\r================================================================\n\r"); |
|
48 | printf("\r================================================================\n\r"); | |
49 | printf("================================================================\n\r"); |
|
49 | printf("================================================================\n\r"); | |
50 | printf(BSP); |
|
50 | printf(BSP); | |
51 | printf(" initialised\n\r"); |
|
51 | printf(" initialised\n\r"); | |
52 | printf("================================================================\n\r"); |
|
52 | printf("================================================================\n\r"); | |
53 | return 1; |
|
53 | return 1; | |
54 | } |
|
54 | } | |
55 |
|
55 | |||
56 | void bsp_GPIO_init() |
|
56 | void bsp_GPIO_init() | |
57 | { |
|
57 | { | |
58 | gpio_t gpio1 = gpioopen(PD12);//gpioopen(LED1); //PD9 D=> 0x0300 9 => 0x0009 |
|
58 | gpio_t gpio1 = gpioopen(PD12);//gpioopen(LED1); //PD9 D=> 0x0300 9 => 0x0009 | |
59 | gpio_t gpio2 = gpioopen(PD13);//gpioopen(LED2); |
|
59 | gpio_t gpio2 = gpioopen(PD13);//gpioopen(LED2); | |
60 | gpio_t gpio3 = gpioopen(PD14);//gpioopen(LED2); |
|
60 | gpio_t gpio3 = gpioopen(PD14);//gpioopen(LED2); | |
61 | gpio_t gpio4 = gpioopen(PD15);//gpioopen(LED2); |
|
61 | gpio_t gpio4 = gpioopen(PD15);//gpioopen(LED2); | |
62 | gpiosetspeed(&gpio1,gpiohighspeed); |
|
62 | gpiosetspeed(&gpio1,gpiohighspeed); | |
63 | gpiosetspeed(&gpio2,gpiohighspeed); |
|
63 | gpiosetspeed(&gpio2,gpiohighspeed); | |
64 | gpiosetspeed(&gpio3,gpiohighspeed); |
|
64 | gpiosetspeed(&gpio3,gpiohighspeed); | |
65 | gpiosetspeed(&gpio4,gpiohighspeed); |
|
65 | gpiosetspeed(&gpio4,gpiohighspeed); | |
66 | gpiosetdir(&gpio1,gpiooutdir); |
|
66 | gpiosetdir(&gpio1,gpiooutdir); | |
67 | gpiosetdir(&gpio3,gpiooutdir); |
|
67 | gpiosetdir(&gpio3,gpiooutdir); | |
68 | gpiosetdir(&gpio2,gpiooutdir); |
|
68 | gpiosetdir(&gpio2,gpiooutdir); | |
69 | gpiosetdir(&gpio4,gpiooutdir); |
|
69 | gpiosetdir(&gpio4,gpiooutdir); | |
70 | } |
|
70 | } | |
71 |
|
71 | |||
72 | void bsp_uart_init() |
|
72 | void bsp_uart_init() | |
73 | { |
|
73 | { | |
74 | if(__opnfiles__[1]==NULL) |
|
74 | if(__opnfiles__[1]==NULL) | |
75 | { |
|
75 | { | |
76 | uart_t* uart1 = (uart_t*)malloc(sizeof(uart_t)); |
|
76 | uart_t* uart1 = (uart_t*)malloc(sizeof(uart_t)); | |
77 | streamdevice* fd1 = (streamdevice*)malloc(sizeof(streamdevice)); |
|
77 | streamdevice* fd1 = (streamdevice*)malloc(sizeof(streamdevice)); | |
78 | uartopenandconfig(2,uart1,uartparitynone | uart8bits | uartonestop,115200,PB10,PB11,-1,-1); |
|
78 | uartopenandconfig(2,uart1,uartparitynone | uart8bits | uartonestop,115200,PB10,PB11,-1,-1); | |
79 | uartmkstreamdev(uart1,fd1); |
|
79 | uartmkstreamdev(uart1,fd1); | |
80 | __opnfiles__[1] = fd1; |
|
80 | __opnfiles__[1] = fd1; | |
81 | } |
|
81 | } | |
82 | else |
|
82 | else | |
83 | { |
|
83 | { | |
84 | uartopenandconfig(2,(uart_t*)__opnfiles__[1]->_stream,uartparitynone | uart8bits | uartonestop,115200,PB10,PB11,-1,-1); |
|
84 | uartopenandconfig(2,(uart_t*)__opnfiles__[1]->_stream,uartparitynone | uart8bits | uartonestop,115200,PB10,PB11,-1,-1); | |
85 | } |
|
85 | } | |
86 | } |
|
86 | } | |
87 |
|
87 | |||
88 | void bsp_spi_init() |
|
88 | void bsp_spi_init() | |
89 | { |
|
89 | { | |
90 |
|
90 | |||
91 | } |
|
91 | } | |
92 |
|
92 | |||
93 |
|
93 | |||
94 | void bsp_iic_init() |
|
94 | void bsp_iic_init() | |
95 | { |
|
95 | { | |
96 |
if(0==i2copenandconfig(i2c1,0, |
|
96 | if(0==i2copenandconfig(i2c1,0,400000,PB9,PB6))printf("I2C1 opened\n\r"); | |
97 | i2cenable(i2c1); |
|
97 | i2cenable(i2c1); | |
98 | } |
|
98 | } | |
99 |
|
99 | |||
100 | void bsp_SD_init() |
|
100 | void bsp_SD_init() | |
101 | { |
|
101 | { | |
102 |
|
102 | |||
103 | } |
|
103 | } | |
104 |
|
104 | |||
105 | void vs10XXclearXCS(){} |
|
105 | void vs10XXclearXCS(){} | |
106 | void vs10XXsetXCS(){} |
|
106 | void vs10XXsetXCS(){} | |
107 | int vs10XXDREQ() |
|
107 | int vs10XXDREQ() | |
108 | { |
|
108 | { | |
109 | return 1; |
|
109 | return 1; | |
110 | } |
|
110 | } | |
111 |
|
111 | |||
112 |
|
112 | |||
113 | void bsppowersdcard(char onoff) //always ON |
|
113 | void bsppowersdcard(char onoff) //always ON | |
114 | { |
|
114 | { | |
115 |
|
115 | |||
116 | } |
|
116 | } | |
117 |
|
117 | |||
118 | char bspsdcardpresent() |
|
118 | char bspsdcardpresent() | |
119 | { |
|
119 | { | |
120 | return 0; |
|
120 | return 0; | |
121 | } |
|
121 | } | |
122 |
|
122 | |||
123 | char bspsdcardwriteprotected() |
|
123 | char bspsdcardwriteprotected() | |
124 | { |
|
124 | { | |
125 | return 0; |
|
125 | return 0; | |
126 | } |
|
126 | } | |
127 |
|
127 | |||
128 | void bspsdcardselect(char YESNO) |
|
128 | void bspsdcardselect(char YESNO) | |
129 | { |
|
129 | { | |
130 |
|
130 | |||
131 | } |
|
131 | } | |
132 |
|
132 | |||
133 |
|
133 | |||
134 |
|
134 | |||
135 |
|
135 | |||
136 |
|
136 |
@@ -1,41 +1,45 | |||||
1 | #include <stdio.h> |
|
1 | #include <stdio.h> | |
2 | #include <fat32.h> |
|
2 | #include <fat32.h> | |
3 | #include <gpio.h> |
|
3 | #include <gpio.h> | |
4 | #include <uart.h> |
|
4 | #include <uart.h> | |
5 | #include <stm32f4xx.h> |
|
5 | #include <stm32f4xx.h> | |
6 | #include <bsp.h> |
|
6 | #include <bsp.h> | |
7 | #include <i2c.h> |
|
7 | #include <i2c.h> | |
8 |
|
8 | |||
9 |
|
9 | |||
10 | extern streamdevice* __opnfiles__[]; |
|
10 | extern streamdevice* __opnfiles__[]; | |
11 |
|
11 | |||
12 | int main() |
|
12 | int main() | |
13 | { |
|
13 | { | |
14 | //gpioset(PSU_DISABLE); |
|
14 | //gpioset(PSU_DISABLE); | |
|
15 | gpio_t dacRst=gpioopen(PD4); | |||
|
16 | gpiosetspeed(&dacRst,gpiohighspeed); | |||
|
17 | gpiosetdir(&dacRst,gpiooutdir); | |||
|
18 | gpioset(dacRst); | |||
15 | printf("i2C Control register 1 (I2C_CR1) = %x\n\r",I2C1->CR1); |
|
19 | printf("i2C Control register 1 (I2C_CR1) = %x\n\r",I2C1->CR1); | |
16 | printf("i2C Control register 2 (I2C_CR2) = %x\n\r",I2C1->CR2); |
|
20 | printf("i2C Control register 2 (I2C_CR2) = %x\n\r",I2C1->CR2); | |
17 | printf("i2C Status register 1 (I2C_SR1) = %x\n\r",I2C1->SR1); |
|
21 | printf("i2C Status register 1 (I2C_SR1) = %x\n\r",I2C1->SR1); | |
18 | printf("i2C Status register 2 (I2C_SR2) = %x\n\r",I2C1->SR2); |
|
22 | printf("i2C Status register 2 (I2C_SR2) = %x\n\r",I2C1->SR2); | |
19 | printf("i2C Clock control register (I2C_CCR) = %x\n\r",I2C1->CCR); |
|
23 | printf("i2C Clock control register (I2C_CCR) = %x\n\r",I2C1->CCR); | |
20 | while(1) |
|
24 | while(1) | |
21 | { |
|
25 | { | |
22 | for(volatile int i=0;i<1024*2048;i++); |
|
26 | for(volatile int i=0;i<1024*2048;i++); | |
23 | gpioset(LED1); |
|
27 | gpioset(LED1); | |
24 | gpioclr(LED2); |
|
28 | gpioclr(LED2); | |
25 | for(volatile int i=0;i<1024*2048;i++); |
|
29 | for(volatile int i=0;i<1024*2048;i++); | |
26 | gpioclr(LED1); |
|
30 | gpioclr(LED1); | |
27 | gpioset(LED2); |
|
31 | gpioset(LED2); | |
28 |
i2cwrite(i2c1,0x4a,"test", |
|
32 | i2cwrite(i2c1,0x4a,"test",1); | |
29 | } |
|
33 | } | |
30 | printf("hello world\n\r"); |
|
34 | printf("hello world\n\r"); | |
31 | return 0; |
|
35 | return 0; | |
32 | } |
|
36 | } | |
33 |
|
37 | |||
34 |
|
38 | |||
35 |
|
39 | |||
36 |
|
40 | |||
37 |
|
41 | |||
38 |
|
42 | |||
39 |
|
43 | |||
40 |
|
44 | |||
41 |
|
45 |
@@ -1,71 +1,72 | |||||
1 | /*------------------------------------------------------------------------------ |
|
1 | /*------------------------------------------------------------------------------ | |
2 | -- This file is a part of the libuc, microcontroler library |
|
2 | -- This file is a part of the libuc, microcontroler library | |
3 | -- Copyright (C) 2012, Alexis Jeandet |
|
3 | -- Copyright (C) 2012, Alexis Jeandet | |
4 | -- |
|
4 | -- | |
5 | -- This program is free software; you can redistribute it and/or modify |
|
5 | -- This program is free software; you can redistribute it and/or modify | |
6 | -- it under the terms of the GNU General Public License as published by |
|
6 | -- it under the terms of the GNU General Public License as published by | |
7 | -- the Free Software Foundation; either version 3 of the License, or |
|
7 | -- the Free Software Foundation; either version 3 of the License, or | |
8 | -- (at your option) any later version. |
|
8 | -- (at your option) any later version. | |
9 | -- |
|
9 | -- | |
10 | -- This program is distributed in the hope that it will be useful, |
|
10 | -- This program is distributed in the hope that it will be useful, | |
11 | -- but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | -- but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | -- GNU General Public License for more details. |
|
13 | -- GNU General Public License for more details. | |
14 | -- |
|
14 | -- | |
15 | -- You should have received a copy of the GNU General Public License |
|
15 | -- You should have received a copy of the GNU General Public License | |
16 | -- along with this program; if not, write to the Free Software |
|
16 | -- along with this program; if not, write to the Free Software | |
17 | -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
17 | -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | ------------------------------------------------------------------------------- |
|
18 | ------------------------------------------------------------------------------- | |
19 | -- Author : Alexis Jeandet |
|
19 | -- Author : Alexis Jeandet | |
20 | -- Mail : alexis.jeandet@gmail.com |
|
20 | -- Mail : alexis.jeandet@gmail.com | |
21 | -------------------------------------------------------------------------------*/ |
|
21 | -------------------------------------------------------------------------------*/ | |
22 | #ifndef I2C_H |
|
22 | #ifndef I2C_H | |
23 | #define I2C_H |
|
23 | #define I2C_H | |
24 | #include <stdint.h> |
|
24 | #include <stdint.h> | |
25 | #include <uhandle.h> |
|
25 | #include <uhandle.h> | |
26 | #include <streamdevices.h> |
|
26 | #include <streamdevices.h> | |
27 |
|
27 | |||
28 | #ifdef __cplusplus |
|
28 | #ifdef __cplusplus | |
29 | extern "C" { |
|
29 | extern "C" { | |
30 | #endif |
|
30 | #endif | |
31 |
|
31 | |||
32 | typedef int i2c_t; |
|
32 | typedef int i2c_t; | |
33 |
|
33 | |||
34 | #define i2c1 0 |
|
34 | #define i2c1 0 | |
35 | #define i2c2 1 |
|
35 | #define i2c2 1 | |
36 | #define i2c3 2 |
|
36 | #define i2c3 2 | |
37 | #define i2c4 3 |
|
37 | #define i2c4 3 | |
38 | #define i2c5 4 |
|
38 | #define i2c5 4 | |
39 | #define i2c6 5 |
|
39 | #define i2c6 5 | |
40 | #define i2c7 6 |
|
40 | #define i2c7 6 | |
41 | #define i2c8 7 |
|
41 | #define i2c8 7 | |
42 | #define i2c9 8 |
|
42 | #define i2c9 8 | |
43 |
|
43 | |||
44 | extern i2c_t i2copen(int count); |
|
44 | extern i2c_t i2copen(int count); | |
45 | extern i2c_t i2copenandconfig(int count ,uint32_t cfg,uint32_t speed,uint32_t SDA,uint32_t SCL); |
|
45 | extern i2c_t i2copenandconfig(int count ,uint32_t cfg,uint32_t speed,uint32_t SDA,uint32_t SCL); | |
46 | extern int i2cclose(i2c_t dev); |
|
46 | extern int i2cclose(i2c_t dev); | |
47 | extern int i2csetpins(i2c_t dev,uint32_t SDA,uint32_t SCL); |
|
47 | extern int i2csetpins(i2c_t dev,uint32_t SDA,uint32_t SCL); | |
48 | extern int i2cenable(i2c_t dev); |
|
48 | extern int i2cenable(i2c_t dev); | |
49 | extern int i2cdisable(i2c_t dev); |
|
49 | extern int i2cdisable(i2c_t dev); | |
50 | //extern int i2csetconfig(i2c_t dev); |
|
50 | //extern int i2csetconfig(i2c_t dev); | |
51 | extern int i2csetspeed(i2c_t dev,uint32_t speed); |
|
51 | extern int i2csetspeed(i2c_t dev,uint32_t speed); | |
52 | //extern int i2csetdatabits(i2c_t* dev,uartbits_t databits); |
|
52 | //extern int i2csetdatabits(i2c_t* dev,uartbits_t databits); | |
53 | extern int i2cwrite(i2c_t dev,char address,char* data,int count); |
|
53 | extern int i2cwrite(i2c_t dev,char address,char* data,int count); | |
54 | extern int i2cread(i2c_t dev,char address,char* data,int count); |
|
54 | extern int i2cread(i2c_t dev,char address,char* data,int count); | |
55 |
|
55 | extern int i2cbusy(i2c_t dev); | ||
|
56 | extern int i2cStatusCheck(i2c_t dev,int32_t flagMask); | |||
56 | #ifdef __cplusplus |
|
57 | #ifdef __cplusplus | |
57 | } |
|
58 | } | |
58 | #endif |
|
59 | #endif | |
59 | #endif //I2C_H |
|
60 | #endif //I2C_H | |
60 |
|
61 | |||
61 |
|
62 | |||
62 |
|
63 | |||
63 |
|
64 | |||
64 |
|
65 | |||
65 |
|
66 | |||
66 |
|
67 | |||
67 |
|
68 | |||
68 |
|
69 | |||
69 |
|
70 | |||
70 |
|
71 | |||
71 |
|
72 |
@@ -1,254 +1,282 | |||||
1 | /*------------------------------------------------------------------------------ |
|
1 | /*------------------------------------------------------------------------------ | |
2 | -- This file is a part of the libuc, microcontroler library |
|
2 | -- This file is a part of the libuc, microcontroler library | |
3 | -- Copyright (C) 2012, Alexis Jeandet |
|
3 | -- Copyright (C) 2012, Alexis Jeandet | |
4 | -- |
|
4 | -- | |
5 | -- This program is free software; you can redistribute it and/or modify |
|
5 | -- This program is free software; you can redistribute it and/or modify | |
6 | -- it under the terms of the GNU General Public License as published by |
|
6 | -- it under the terms of the GNU General Public License as published by | |
7 | -- the Free Software Foundation; either version 3 of the License, or |
|
7 | -- the Free Software Foundation; either version 3 of the License, or | |
8 | -- (at your option) any later version. |
|
8 | -- (at your option) any later version. | |
9 | -- |
|
9 | -- | |
10 | -- This program is distributed in the hope that it will be useful, |
|
10 | -- This program is distributed in the hope that it will be useful, | |
11 | -- but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | -- but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | -- GNU General Public License for more details. |
|
13 | -- GNU General Public License for more details. | |
14 | -- |
|
14 | -- | |
15 | -- You should have received a copy of the GNU General Public License |
|
15 | -- You should have received a copy of the GNU General Public License | |
16 | -- along with this program; if not, write to the Free Software |
|
16 | -- along with this program; if not, write to the Free Software | |
17 | -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
17 | -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | ------------------------------------------------------------------------------- |
|
18 | ------------------------------------------------------------------------------- | |
19 | -- Author : Alexis Jeandet |
|
19 | -- Author : Alexis Jeandet | |
20 | -- Mail : alexis.jeandet@gmail.com |
|
20 | -- Mail : alexis.jeandet@gmail.com | |
21 | -------------------------------------------------------------------------------*/ |
|
21 | -------------------------------------------------------------------------------*/ | |
22 |
|
22 | |||
23 | #include <i2c.h> |
|
23 | #include <i2c.h> | |
24 | #include <stm32f4xx_usart.h> |
|
24 | #include <stm32f4xx_usart.h> | |
25 | #include <stm32f4xx_rcc.h> |
|
25 | #include <stm32f4xx_rcc.h> | |
26 | #include <stm32f4xx_gpio.h> |
|
26 | #include <stm32f4xx_gpio.h> | |
27 | #include <gpio.h> |
|
27 | #include <gpio.h> | |
28 | #include <core.h> |
|
28 | #include <core.h> | |
29 |
|
29 | |||
30 | #define GPIOGETPORT(gpio) ((GPIO_TypeDef*)(((((uint32_t)gpio) & (uint32_t)0x0000FF00)*(uint32_t)4) + (uint32_t)GPIOA)) |
|
30 | #define GPIOGETPORT(gpio) ((GPIO_TypeDef*)(((((uint32_t)gpio) & (uint32_t)0x0000FF00)*(uint32_t)4) + (uint32_t)GPIOA)) | |
31 | #define GPIOPORTNUM(gpio) (((uint32_t)(gpio) & (uint32_t)0x0000FF00)>>(uint32_t)8) |
|
31 | #define GPIOPORTNUM(gpio) (((uint32_t)(gpio) & (uint32_t)0x0000FF00)>>(uint32_t)8) | |
32 |
|
32 | |||
33 |
|
33 | |||
34 | I2C_TypeDef* _i2c_dev_table[3]={I2C1,I2C2,I2C3}; |
|
34 | I2C_TypeDef* _i2c_dev_table[3]={I2C1,I2C2,I2C3}; | |
35 |
|
35 | |||
36 | i2c_t i2copen(int count) |
|
36 | i2c_t i2copen(int count) | |
37 | { |
|
37 | { | |
38 | #define _INIT_DEV(_RCC_) \ |
|
38 | #define _INIT_DEV(_RCC_) \ | |
39 | RCC_APB1PeriphClockCmd(_RCC_, ENABLE); \ |
|
39 | RCC_APB1PeriphClockCmd(_RCC_, ENABLE); \ | |
40 | RCC_APB1PeriphResetCmd(_RCC_, ENABLE); \ |
|
40 | RCC_APB1PeriphResetCmd(_RCC_, ENABLE); \ | |
41 | RCC_APB1PeriphResetCmd(_RCC_, DISABLE); \ |
|
41 | RCC_APB1PeriphResetCmd(_RCC_, DISABLE); \ | |
42 | RCC_APB1PeriphClockCmd(_RCC_, ENABLE); |
|
42 | RCC_APB1PeriphClockCmd(_RCC_, ENABLE); | |
43 |
|
43 | |||
44 | switch(count) |
|
44 | switch(count) | |
45 | { |
|
45 | { | |
46 | case 0: |
|
46 | case 0: | |
47 | _INIT_DEV(RCC_APB1Periph_I2C1); |
|
47 | _INIT_DEV(RCC_APB1Periph_I2C1); | |
48 | return (i2c_t) 0; |
|
48 | return (i2c_t) 0; | |
49 | break; |
|
49 | break; | |
50 | case 1: |
|
50 | case 1: | |
51 | _INIT_DEV(RCC_APB1Periph_I2C2); |
|
51 | _INIT_DEV(RCC_APB1Periph_I2C2); | |
52 | return (i2c_t) 1; |
|
52 | return (i2c_t) 1; | |
53 | break; |
|
53 | break; | |
54 | case 2: |
|
54 | case 2: | |
55 | _INIT_DEV(RCC_APB1Periph_I2C3); |
|
55 | _INIT_DEV(RCC_APB1Periph_I2C3); | |
56 | return (i2c_t) 2; |
|
56 | return (i2c_t) 2; | |
57 | break; |
|
57 | break; | |
58 | default: |
|
58 | default: | |
59 | break; |
|
59 | break; | |
60 | } |
|
60 | } | |
61 | return -1; |
|
61 | return -1; | |
62 | } |
|
62 | } | |
63 |
|
63 | |||
64 | i2c_t i2copenandconfig(int count,uint32_t cfg,uint32_t speed,uint32_t SDA,uint32_t SCL) |
|
64 | i2c_t i2copenandconfig(int count,uint32_t cfg,uint32_t speed,uint32_t SDA,uint32_t SCL) | |
65 | { |
|
65 | { | |
66 | i2c_t dev = i2copen(count); |
|
66 | i2c_t dev = i2copen(count); | |
67 | if(dev!=-1) |
|
67 | if(dev!=-1) | |
68 | { |
|
68 | { | |
69 |
|
|
69 | i2cclose(dev); | |
70 | I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev]; |
|
|||
71 | _dev_->CR1 = (1<<15); |
|
|||
72 | _dev_->CR1 &=~(1<<15); |
|
|||
73 | i2cdisable(count); |
|
|||
74 | i2csetpins(dev,SDA,SCL); |
|
70 | i2csetpins(dev,SDA,SCL); | |
|
71 | i2copen(count); | |||
|
72 | i2cenable(count); | |||
|
73 | //I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev]; | |||
75 | i2csetspeed(dev,speed); |
|
74 | i2csetspeed(dev,speed); | |
76 | i2cenable(count); |
|
75 | i2cenable(count); | |
77 | _dev_->CR1|=1<<15; |
|
|||
78 | _dev_->CR1&=~(1<<15); |
|
|||
79 | } |
|
76 | } | |
80 | return dev; |
|
77 | return dev; | |
81 | } |
|
78 | } | |
82 |
|
79 | |||
83 | int i2cclose(i2c_t dev) |
|
80 | int i2cclose(i2c_t dev) | |
84 | { |
|
81 | { | |
85 | switch((int)dev) |
|
82 | switch((int)dev) | |
86 | { |
|
83 | { | |
87 | case (int)0: |
|
84 | case (int)0: | |
88 | RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE); |
|
85 | RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE); | |
89 | break; |
|
86 | break; | |
90 | case (int)1: |
|
87 | case (int)1: | |
91 | RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, ENABLE); |
|
88 | RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, ENABLE); | |
92 | break; |
|
89 | break; | |
93 | case (int)2: |
|
90 | case (int)2: | |
94 | RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C3, ENABLE); |
|
91 | RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C3, ENABLE); | |
95 | break; |
|
92 | break; | |
96 | default: |
|
93 | default: | |
97 | break; |
|
94 | break; | |
98 | } |
|
95 | } | |
99 | return 1; |
|
96 | return 1; | |
100 | } |
|
97 | } | |
101 |
|
98 | |||
102 | int i2csetpins(i2c_t dev, uint32_t SDA, uint32_t SCL) |
|
99 | int i2csetpins(i2c_t dev, uint32_t SDA, uint32_t SCL) | |
103 | { |
|
100 | { | |
104 | if((dev<3)&&(dev>=0)) |
|
101 | if((dev<3)&&(dev>=0)) | |
105 | { |
|
102 | { | |
106 | gpio_t SDApin,SCLpin; |
|
103 | gpio_t SDApin,SCLpin; | |
107 | SDApin = gpioopen(SDA); |
|
104 | SDApin = gpioopen(SDA); | |
108 | SCLpin = gpioopen(SCL); |
|
105 | SCLpin = gpioopen(SCL); | |
109 | SDApin |= gpiolowspeed | gpioaf | gpioopendraintype | gpionopulltype; |
|
106 | SDApin |= gpiolowspeed | gpioaf | gpioopendraintype | gpionopulltype; | |
110 | SCLpin |= gpiolowspeed | gpioaf | gpioopendraintype | gpionopulltype; |
|
107 | SCLpin |= gpiolowspeed | gpioaf | gpioopendraintype | gpionopulltype; | |
111 | gpiosetconfig(&SDApin); |
|
108 | gpiosetconfig(&SDApin); | |
112 | gpiosetconfig(&SCLpin); |
|
109 | gpiosetconfig(&SCLpin); | |
113 | uint8_t gpioAFi2cx = GPIO_AF_I2C1; |
|
110 | uint8_t gpioAFi2cx = GPIO_AF_I2C1; | |
114 | switch((int)dev) |
|
111 | switch((int)dev) | |
115 | { |
|
112 | { | |
116 | case 0: |
|
113 | case 0: | |
117 | gpioAFi2cx = GPIO_AF_I2C1; |
|
114 | gpioAFi2cx = GPIO_AF_I2C1; | |
118 | break; |
|
115 | break; | |
119 | case 1: |
|
116 | case 1: | |
120 | gpioAFi2cx = GPIO_AF_I2C2; |
|
117 | gpioAFi2cx = GPIO_AF_I2C2; | |
121 | break; |
|
118 | break; | |
122 | case 2: |
|
119 | case 2: | |
123 | gpioAFi2cx = GPIO_AF_I2C3; |
|
120 | gpioAFi2cx = GPIO_AF_I2C3; | |
124 | break; |
|
121 | break; | |
125 | default: |
|
122 | default: | |
126 | break; |
|
123 | break; | |
127 | } |
|
124 | } | |
128 | GPIO_PinAFConfig(GPIOGETPORT(SDApin), (uint8_t)(SDApin & 0xF), gpioAFi2cx); |
|
125 | GPIO_PinAFConfig(GPIOGETPORT(SDApin), (uint8_t)(SDApin & 0xF), gpioAFi2cx); | |
129 | GPIO_PinAFConfig(GPIOGETPORT(SCLpin), (uint8_t)(SCLpin & 0xF), gpioAFi2cx); |
|
126 | GPIO_PinAFConfig(GPIOGETPORT(SCLpin), (uint8_t)(SCLpin & 0xF), gpioAFi2cx); | |
130 | return 0; |
|
127 | return 0; | |
131 | } |
|
128 | } | |
132 | return -1; |
|
129 | return -1; | |
133 | } |
|
130 | } | |
134 |
|
131 | |||
135 | int i2cenable(i2c_t dev) |
|
132 | int i2cenable(i2c_t dev) | |
136 | { |
|
133 | { | |
137 | if((dev<3)&&(dev>=0)) |
|
134 | if((dev<3)&&(dev>=0)) | |
138 | { |
|
135 | { | |
139 | I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev]; |
|
136 | I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev]; | |
140 | _dev_->CR1 |=1 ; |
|
137 | _dev_->CR1 |=1 ; | |
141 | return 0; |
|
138 | return 0; | |
142 | } |
|
139 | } | |
143 | return -1; |
|
140 | return -1; | |
144 | } |
|
141 | } | |
145 |
|
142 | |||
146 | int i2cdisable(i2c_t dev) |
|
143 | int i2cdisable(i2c_t dev) | |
147 | { |
|
144 | { | |
148 | if((dev<3)&&(dev>=0)) |
|
145 | if((dev<3)&&(dev>=0)) | |
149 | { |
|
146 | { | |
150 | I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev]; |
|
147 | I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev]; | |
151 | _dev_->CR1 &= ~1; |
|
148 | _dev_->CR1 &= ~1; | |
152 | return 0; |
|
149 | return 0; | |
153 | } |
|
150 | } | |
154 | return -1; |
|
151 | return -1; | |
155 | } |
|
152 | } | |
156 |
|
153 | |||
157 |
|
154 | |||
158 | int i2csetspeed(i2c_t dev,uint32_t speed) |
|
155 | int i2csetspeed(i2c_t dev,uint32_t speed) | |
159 | { |
|
156 | { | |
160 | if((dev<3)&&(dev>=0)) |
|
157 | if((dev<3)&&(dev>=0)) | |
161 | { |
|
158 | { | |
162 | I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev]; |
|
159 | I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev]; | |
163 | int32_t APB1Freq=getAPB1Freq()/1000000; |
|
160 | int32_t APB1Freq=getAPB1Freq()/1000000; | |
164 | if((APB1Freq>1)&&(APB1Freq<43)) |
|
161 | if((APB1Freq>1)&&(APB1Freq<43)) | |
165 | { |
|
162 | { | |
166 | i2cdisable(dev); |
|
163 | ||
167 | uint16_t tmpreg=_dev_->CR2; |
|
164 | uint16_t tmpreg=_dev_->CR2; | |
168 | tmpreg &= ~(0x1f); |
|
165 | tmpreg &= ~(0x1f); | |
169 | tmpreg |= APB1Freq; |
|
166 | tmpreg |= APB1Freq; | |
170 | _dev_->CR2=tmpreg; |
|
167 | _dev_->CR2=tmpreg; | |
171 | tmpreg=_dev_->TRISE; |
|
168 | i2cdisable(dev); | |
172 | tmpreg &= ~(0x3f); |
|
|||
173 | tmpreg |= APB1Freq+1; |
|
|||
174 | _dev_->TRISE = tmpreg; |
|
|||
175 | tmpreg=_dev_->CCR; |
|
169 | tmpreg=_dev_->CCR; | |
176 | APB1Freq=getAPB1Freq(); |
|
170 | APB1Freq=getAPB1Freq(); | |
177 | if(speed>100000) //100kHz= standard mode, 400kHz= fast mode |
|
171 | if(speed>100000) //100kHz= standard mode, 400kHz= fast mode | |
178 | { |
|
172 | { | |
179 | if(speed<=400000) |
|
173 | if(speed<=400000) | |
180 | { |
|
174 | { | |
181 | tmpreg |= 1<<15; |
|
175 | tmpreg |= 1<<15; | |
182 | tmpreg &= ~(1<<14); |
|
176 | tmpreg &= ~(1<<14); | |
183 | tmpreg &= ~(0xfff); |
|
177 | tmpreg &= ~(0xfff); | |
184 | tmpreg |= 0xfff & (APB1Freq/(3*speed)); |
|
178 | tmpreg |= 0xfff & (APB1Freq/(3*speed)); | |
185 | } |
|
179 | } | |
186 | } |
|
180 | } | |
187 | else |
|
181 | else | |
188 | { |
|
182 | { | |
189 | tmpreg &= ~(1<<15); |
|
183 | tmpreg &= ~(1<<15); | |
190 | tmpreg &= ~(0xfff); |
|
184 | tmpreg &= ~(0xfff); | |
191 | tmpreg |= 0xfff & (APB1Freq/(2*speed)); |
|
185 | tmpreg |= 0xfff & (APB1Freq/(2*speed)); | |
192 | } |
|
186 | } | |
193 | _dev_->CCR=tmpreg; |
|
187 | _dev_->CCR=tmpreg; | |
|
188 | tmpreg=_dev_->TRISE; | |||
|
189 | tmpreg &= ~(0x3f); | |||
|
190 | tmpreg |= (APB1Freq/1000000)+1; | |||
|
191 | _dev_->TRISE = tmpreg; | |||
|
192 | i2cenable(dev); | |||
194 | return 0; |
|
193 | return 0; | |
195 | } |
|
194 | } | |
196 | } |
|
195 | } | |
197 | return -1; |
|
196 | return -1; | |
198 | } |
|
197 | } | |
199 |
|
198 | |||
|
199 | int i2cbusy(i2c_t dev) | |||
|
200 | { | |||
|
201 | if((dev<3)&&(dev>=0)) | |||
|
202 | { | |||
|
203 | I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev]; | |||
|
204 | if((_dev_->SR2 & 2) ==2) return 1; /* Dev is busy */ | |||
|
205 | return 0; /* Dev isn't busy */ | |||
|
206 | } | |||
|
207 | return -1; /* Error, dev is out of range */ | |||
|
208 | } | |||
|
209 | ||||
200 | int i2cwrite(i2c_t dev,char address,char* data,int count) |
|
210 | int i2cwrite(i2c_t dev,char address,char* data,int count) | |
201 | { |
|
211 | { | |
202 | if((dev<3)&&(dev>=0)) |
|
212 | if((dev<3)&&(dev>=0)) | |
203 | { |
|
213 | { | |
204 | I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev]; |
|
214 | I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev]; | |
|
215 | while(i2cbusy(dev)); | |||
205 | _dev_->CR1 |= 1<<8; |
|
216 | _dev_->CR1 |= 1<<8; | |
206 | while((_dev_->SR1&1)==0); |
|
217 | while(!i2cStatusCheck(dev,((uint32_t)0x00030001))); | |
207 | _dev_->DR= address<<1; |
|
218 | _dev_->DR= address<<1; | |
208 | while((_dev_->SR1 & (1<<3))==0); |
|
219 | while(!i2cStatusCheck(dev, ((uint32_t)0x00070082))); | |
209 | address=_dev_->SR2; |
|
220 | address=_dev_->SR2; | |
210 | for(int i=0;i<count;i++) |
|
221 | for(int i=0;i<count;i++) | |
211 | { |
|
222 | { | |
212 | while((_dev_->SR1 & (1<<7))==0); |
|
223 | while(!i2cStatusCheck(dev,((uint32_t)0x00070080))); | |
213 | _dev_->DR= data[i]; |
|
224 | _dev_->DR= data[i]; | |
214 | } |
|
225 | } | |
215 |
while(( |
|
226 | while(!i2cStatusCheck(dev,1<<7)); | |
216 |
while(( |
|
227 | while(!i2cStatusCheck(dev,1<<2)); | |
217 | _dev_->CR1 |= 1<<9; |
|
228 | _dev_->CR1 |= 1<<9; | |
218 | return count; |
|
229 | return count; | |
219 | } |
|
230 | } | |
220 | return -1; |
|
231 | return -1; | |
221 | } |
|
232 | } | |
222 |
|
233 | |||
223 | int i2cread(i2c_t dev,char address,char* data,int count) |
|
234 | int i2cread(i2c_t dev,char address,char* data,int count) | |
224 | { |
|
235 | { | |
225 | if((dev<3)&&(dev>=0)) |
|
236 | if((dev<3)&&(dev>=0)) | |
226 | { |
|
237 | { | |
|
238 | while(i2cbusy(dev)); | |||
227 | I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev]; |
|
239 | I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev]; | |
228 | _dev_->CR1 |= (1<<8) | (1<<10); |
|
240 | _dev_->CR1 |= (1<<8) | (1<<10); | |
229 | if(count==1) |
|
241 | if(count==1) | |
230 | { |
|
242 | { | |
231 | _dev_->CR1 &= ~(1<<10); |
|
243 | _dev_->CR1 &= ~(1<<10); | |
232 | } |
|
244 | } | |
233 | while((_dev_->SR1&1)==0); |
|
245 | while(!i2cStatusCheck(dev,0x00030001)); | |
234 | _dev_->DR= (address<<1) + 1; |
|
246 | _dev_->DR= (address<<1) + 1; | |
235 | while((_dev_->SR1 & (1<<3))==0); |
|
247 | while(!i2cStatusCheck(dev, ((uint32_t)0x00070082))); | |
236 | address=_dev_->SR2; |
|
248 | address=_dev_->SR2; | |
237 | for(int i=0;i<count-1;i++) |
|
249 | for(int i=0;i<count-1;i++) | |
238 | { |
|
250 | { | |
239 | while((_dev_->SR1 & (1<<6))==0); |
|
251 | while((_dev_->SR1 & (1<<6))==0); | |
240 | data[i]=_dev_->DR; |
|
252 | data[i]=_dev_->DR; | |
241 | } |
|
253 | } | |
242 | _dev_->CR1 &= ~(1<<10); |
|
254 | _dev_->CR1 &= ~(1<<10); | |
243 | _dev_->CR1 |= 1<<9; |
|
255 | _dev_->CR1 |= 1<<9; | |
244 | while((_dev_->SR1 & (1<<6))==0); |
|
256 | while((_dev_->SR1 & (1<<6))==0); | |
245 | data[count-1]=_dev_->DR; |
|
257 | data[count-1]=_dev_->DR; | |
246 | return count; |
|
258 | return count; | |
247 | } |
|
259 | } | |
248 | return -1; |
|
260 | return -1; | |
249 | } |
|
261 | } | |
250 |
|
262 | |||
251 |
|
263 | |||
|
264 | int i2cStatusCheck(i2c_t dev,int32_t flagMask) | |||
|
265 | { | |||
|
266 | int32_t flag; | |||
|
267 | __asm__("nop"); | |||
|
268 | __asm__("nop"); | |||
|
269 | __asm__("nop"); | |||
|
270 | __asm__("nop"); | |||
|
271 | if((dev<3)&&(dev>=0)) | |||
|
272 | { | |||
|
273 | I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev]; | |||
|
274 | flag= _dev_->SR1 + (_dev_->SR2<<16); | |||
|
275 | if(flagMask==(flag & flagMask)) | |||
|
276 | return 1; | |||
|
277 | return 0; | |||
|
278 | } | |||
|
279 | return -1; | |||
|
280 | } | |||
252 |
|
281 | |||
253 |
|
282 | |||
254 |
|
General Comments 0
You need to be logged in to leave comments.
Login now