@@ -1,163 +1,211 | |||||
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 <stm32f4xx_gpio.h> |
|
28 | #include <stm32f4xx_gpio.h> | |
|
29 | #include <stm32f4xx_fsmc.h> | |||
29 | #include <i2c.h> |
|
30 | #include <i2c.h> | |
30 | uint32_t OSC0 =8000000; |
|
31 | uint32_t OSC0 =8000000; | |
31 | uint32_t INTOSC =16000000; |
|
32 | uint32_t INTOSC =16000000; | |
32 | uint32_t RTCOSC =32768; |
|
33 | uint32_t RTCOSC =32768; | |
33 | uint32_t currentCpuFreq=0; |
|
34 | uint32_t currentCpuFreq=0; | |
34 | extern streamdevice* __opnfiles__[__MAX_OPENED_FILES__]; |
|
35 | extern streamdevice* __opnfiles__[__MAX_OPENED_FILES__]; | |
35 |
|
36 | |||
36 | float VREF0 =(float)3.3; |
|
37 | float VREF0 =(float)3.3; | |
37 |
|
38 | |||
38 | int bsp_init() |
|
39 | int bsp_init() | |
39 | { |
|
40 | { | |
40 | int i=0; |
|
41 | int i=0; | |
41 | for(i=0;i<32;i++) |
|
42 | for(i=0;i<32;i++) | |
42 | { |
|
43 | { | |
43 | __opnfiles__[i] = NULL; |
|
44 | __opnfiles__[i] = NULL; | |
44 | } |
|
45 | } | |
45 | bsp_GPIO_init(); |
|
46 | bsp_GPIO_init(); | |
46 | bsp_uart_init(); |
|
47 | bsp_uart_init(); | |
47 | bsp_iic_init(); |
|
48 | bsp_iic_init(); | |
|
49 | bsp_FSMC_init(); | |||
48 | printf("\r================================================================\n\r"); |
|
50 | printf("\r================================================================\n\r"); | |
49 | printf("================================================================\n\r"); |
|
51 | printf("================================================================\n\r"); | |
50 | printf(BSP); |
|
52 | printf(BSP); | |
51 | printf(" initialised\n\r"); |
|
53 | printf(" initialised\n\r"); | |
52 | printf("================================================================\n\r"); |
|
54 | printf("================================================================\n\r"); | |
53 | return 1; |
|
55 | return 1; | |
54 | } |
|
56 | } | |
55 |
|
57 | |||
56 | void bsp_GPIO_init() |
|
58 | void bsp_GPIO_init() | |
57 | { |
|
59 | { | |
58 | gpio_t gpio1 = gpioopen(LED1); |
|
60 | gpio_t gpio1 = gpioopen(LED1); | |
59 | gpio_t gpio2 = gpioopen(LED2); |
|
61 | gpio_t gpio2 = gpioopen(LED2); | |
60 | gpio_t gpio3 = gpioopen(LED3); |
|
62 | gpio_t gpio3 = gpioopen(LED3); | |
61 | gpio_t gpio4 = gpioopen(PSU_DISABLE); |
|
63 | gpio_t gpio4 = gpioopen(PSU_DISABLE); | |
62 | gpio_t gpio5 = gpioopen(PSU_ALERT_5V); |
|
64 | gpio_t gpio5 = gpioopen(PSU_ALERT_5V); | |
63 | gpio_t gpio6 = gpioopen(PSU_ALERT_1_5V); |
|
65 | gpio_t gpio6 = gpioopen(PSU_ALERT_1_5V); | |
64 | gpio_t gpio7 = gpioopen(PSU_ALERT_3_3V); |
|
66 | gpio_t gpio7 = gpioopen(PSU_ALERT_3_3V); | |
65 | gpiosetspeed(&gpio1,gpiohighspeed); |
|
67 | gpiosetspeed(&gpio1,gpiohighspeed); | |
66 | gpiosetspeed(&gpio2,gpiohighspeed); |
|
68 | gpiosetspeed(&gpio2,gpiohighspeed); | |
67 | gpiosetspeed(&gpio3,gpiohighspeed); |
|
69 | gpiosetspeed(&gpio3,gpiohighspeed); | |
68 | gpiosetspeed(&gpio4,gpiohighspeed); |
|
70 | gpiosetspeed(&gpio4,gpiohighspeed); | |
69 | gpiosetspeed(&gpio5,gpiohighspeed); |
|
71 | gpiosetspeed(&gpio5,gpiohighspeed); | |
70 | gpiosetspeed(&gpio6,gpiohighspeed); |
|
72 | gpiosetspeed(&gpio6,gpiohighspeed); | |
71 | gpiosetspeed(&gpio7,gpiohighspeed); |
|
73 | gpiosetspeed(&gpio7,gpiohighspeed); | |
72 | gpioclr(PSU_DISABLE); |
|
74 | gpioclr(PSU_DISABLE); | |
73 | gpiosetdir(&gpio1,gpiooutdir); |
|
75 | gpiosetdir(&gpio1,gpiooutdir); | |
74 | gpiosetdir(&gpio3,gpiooutdir); |
|
76 | gpiosetdir(&gpio3,gpiooutdir); | |
75 | gpiosetdir(&gpio2,gpiooutdir); |
|
77 | gpiosetdir(&gpio2,gpiooutdir); | |
76 | gpiosetdir(&gpio4,gpiooutdir); |
|
78 | gpiosetdir(&gpio4,gpiooutdir); | |
77 | gpiosetdir(&gpio5,gpioindir); |
|
79 | gpiosetdir(&gpio5,gpioindir); | |
78 | gpiosetdir(&gpio6,gpioindir); |
|
80 | gpiosetdir(&gpio6,gpioindir); | |
79 | gpiosetdir(&gpio7,gpioindir); |
|
81 | gpiosetdir(&gpio7,gpioindir); | |
80 | gpioclr(PSU_DISABLE); |
|
82 | gpioclr(PSU_DISABLE); | |
81 | } |
|
83 | } | |
82 |
|
84 | |||
83 | void bsp_uart_init() |
|
85 | void bsp_uart_init() | |
84 | { |
|
86 | { | |
85 | if(__opnfiles__[1]==NULL) |
|
87 | if(__opnfiles__[1]==NULL) | |
86 | { |
|
88 | { | |
87 | //uart_t* uart1 = (uart_t*)malloc(sizeof(uart_t)); |
|
89 | //uart_t* uart1 = (uart_t*)malloc(sizeof(uart_t)); | |
88 | streamdevice* fd1 = (streamdevice*)malloc(sizeof(streamdevice)); |
|
90 | streamdevice* fd1 = (streamdevice*)malloc(sizeof(streamdevice)); | |
89 | uart_t uart = uartopenandconfig(uart1,uartparitynone | uart8bits | uartonestop,115200,PA9,PA10,-1,-1); |
|
91 | uart_t uart = uartopenandconfig(uart1,uartparitynone | uart8bits | uartonestop,115200,PA9,PA10,-1,-1); | |
90 | uartmkstreamdev(uart,fd1); |
|
92 | uartmkstreamdev(uart,fd1); | |
91 | __opnfiles__[1] = fd1; |
|
93 | __opnfiles__[1] = fd1; | |
92 | } |
|
94 | } | |
93 | else |
|
95 | else | |
94 | { |
|
96 | { | |
95 | uartopenandconfig(0,uartparitynone | uart8bits | uartonestop,115200,PA9,PA10,-1,-1); |
|
97 | uartopenandconfig(0,uartparitynone | uart8bits | uartonestop,115200,PA9,PA10,-1,-1); | |
96 | } |
|
98 | } | |
97 | } |
|
99 | } | |
98 |
|
100 | |||
99 |
|
101 | |||
100 | void bsp_FSMC_init() |
|
102 | void bsp_FSMC_init() | |
101 | { |
|
103 | { | |
102 | #define GPIOGETPORT(gpio) ((GPIO_TypeDef*)(((((uint32_t)gpio) & (uint32_t)0x0000FF00)*(uint32_t)4) + (uint32_t)GPIOA)) |
|
104 | #define GPIOGETPORT(gpio) ((GPIO_TypeDef*)(((((uint32_t)gpio) & (uint32_t)0x0000FF00)*(uint32_t)4) + (uint32_t)GPIOA)) | |
103 | #define GPIOPORTNUM(gpio) (((uint32_t)(gpio) & (uint32_t)0x0000FF00)>>(uint32_t)8) |
|
105 | #define GPIOPORTNUM(gpio) (((uint32_t)(gpio) & (uint32_t)0x0000FF00)>>(uint32_t)8) | |
104 |
|
106 | |||
105 | gpio_t LCD_DBxList[]={PD14,PD15,PD0,PD1,PE7,PE8,PE9,PE10,PE11,PE12,PE13,PE14,PE15\ |
|
107 | gpio_t LCD_DBxList[]={PD14,PD15,PD0,PD1,PE7,PE8,PE9,PE10,PE11,PE12,PE13,PE14,PE15\ | |
106 | ,PD8,PD9,PD10,PD4,PD5,PD7,PE4}; |
|
108 | ,PD8,PD9,PD10,PD4,PD5,PD7,PE4}; | |
107 | for(int i=0;i<20;i++) |
|
109 | for(int i=0;i<20;i++) | |
108 | { |
|
110 | { | |
109 | gpio_t LCD_DBx = gpioopen(LCD_DBxList[i]); |
|
111 | gpio_t LCD_DBx = gpioopen(LCD_DBxList[i]); | |
110 | LCD_DBx |= gpiohighspeed | gpioaf | gpiopushpulltype | gpionopulltype; |
|
112 | LCD_DBx |= gpiohighspeed | gpioaf | gpiopushpulltype | gpionopulltype; | |
111 | gpiosetconfig(&LCD_DBx); |
|
113 | gpiosetconfig(&LCD_DBx); | |
112 | GPIO_PinAFConfig(GPIOGETPORT(LCD_DBx), (uint8_t)(LCD_DBx & 0xF), GPIO_AF_FSMC); |
|
114 | GPIO_PinAFConfig(GPIOGETPORT(LCD_DBx), (uint8_t)(LCD_DBx & 0xF), GPIO_AF_FSMC); | |
113 | } |
|
115 | } | |
|
116 | ||||
|
117 | FSMC_NORSRAMInitTypeDef FSMC_NORSRAMInitStructure; | |||
|
118 | FSMC_NORSRAMTimingInitTypeDef p; | |||
|
119 | ||||
|
120 | /* Enable FSMC clock */ | |||
|
121 | RCC_AHB3PeriphClockCmd(RCC_AHB3Periph_FSMC, ENABLE); | |||
|
122 | ||||
|
123 | /*-- FSMC Configuration ------------------------------------------------------*/ | |||
|
124 | /*----------------------- SRAM Bank 3 ----------------------------------------*/ | |||
|
125 | /* FSMC_Bank1_NORSRAM4 configuration */ | |||
|
126 | p.FSMC_AddressSetupTime = 5; | |||
|
127 | p.FSMC_AddressHoldTime = 0; | |||
|
128 | p.FSMC_DataSetupTime = 9; | |||
|
129 | p.FSMC_BusTurnAroundDuration = 0; | |||
|
130 | p.FSMC_CLKDivision = 0; | |||
|
131 | p.FSMC_DataLatency = 0; | |||
|
132 | p.FSMC_AccessMode = FSMC_AccessMode_A; | |||
|
133 | /* Color LCD configuration ------------------------------------ | |||
|
134 | LCD configured as follow: | |||
|
135 | - Data/Address MUX = Disable | |||
|
136 | - Memory Type = SRAM | |||
|
137 | - Data Width = 16bit | |||
|
138 | - Write Operation = Enable | |||
|
139 | - Extended Mode = Enable | |||
|
140 | - Asynchronous Wait = Disable */ | |||
|
141 | ||||
|
142 | FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM1; | |||
|
143 | FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable; | |||
|
144 | FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_SRAM; | |||
|
145 | FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b; | |||
|
146 | FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable; | |||
|
147 | FSMC_NORSRAMInitStructure.FSMC_AsynchronousWait = FSMC_AsynchronousWait_Disable; | |||
|
148 | FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low; | |||
|
149 | FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable; | |||
|
150 | FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState; | |||
|
151 | FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable; | |||
|
152 | FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable; | |||
|
153 | FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable; | |||
|
154 | FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable; | |||
|
155 | FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &p; | |||
|
156 | FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &p; | |||
|
157 | ||||
|
158 | FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure); | |||
|
159 | ||||
|
160 | /* Enable FSMC NOR/SRAM Bank3 */ | |||
|
161 | FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM1, ENABLE); | |||
114 | } |
|
162 | } | |
115 |
|
163 | |||
116 | void bsp_spi_init() |
|
164 | void bsp_spi_init() | |
117 | { |
|
165 | { | |
118 |
|
166 | |||
119 | } |
|
167 | } | |
120 |
|
168 | |||
121 |
|
169 | |||
122 | void bsp_iic_init() |
|
170 | void bsp_iic_init() | |
123 | { |
|
171 | { | |
124 | i2copenandconfig(i2c2,0,400000,PF0,PF1); |
|
172 | i2copenandconfig(i2c2,0,400000,PF0,PF1); | |
125 | } |
|
173 | } | |
126 |
|
174 | |||
127 | void bsp_SD_init() |
|
175 | void bsp_SD_init() | |
128 | { |
|
176 | { | |
129 |
|
177 | |||
130 | } |
|
178 | } | |
131 |
|
179 | |||
132 | void vs10XXclearXCS(){} |
|
180 | void vs10XXclearXCS(){} | |
133 | void vs10XXsetXCS(){} |
|
181 | void vs10XXsetXCS(){} | |
134 | int vs10XXDREQ() |
|
182 | int vs10XXDREQ() | |
135 | { |
|
183 | { | |
136 | return 1; |
|
184 | return 1; | |
137 | } |
|
185 | } | |
138 |
|
186 | |||
139 |
|
187 | |||
140 | void bsppowersdcard(char onoff) //always ON |
|
188 | void bsppowersdcard(char onoff) //always ON | |
141 | { |
|
189 | { | |
142 |
|
190 | |||
143 | } |
|
191 | } | |
144 |
|
192 | |||
145 | char bspsdcardpresent() |
|
193 | char bspsdcardpresent() | |
146 | { |
|
194 | { | |
147 | return 0; |
|
195 | return 0; | |
148 | } |
|
196 | } | |
149 |
|
197 | |||
150 | char bspsdcardwriteprotected() |
|
198 | char bspsdcardwriteprotected() | |
151 | { |
|
199 | { | |
152 | return 0; |
|
200 | return 0; | |
153 | } |
|
201 | } | |
154 |
|
202 | |||
155 | void bspsdcardselect(char YESNO) |
|
203 | void bspsdcardselect(char YESNO) | |
156 | { |
|
204 | { | |
157 |
|
205 | |||
158 | } |
|
206 | } | |
159 |
|
207 | |||
160 |
|
208 | |||
161 |
|
209 | |||
162 |
|
210 | |||
163 |
|
211 |
@@ -1,46 +1,57 | |||||
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 | #include <CS43L22.h> |
|
8 | #include <CS43L22.h> | |
9 | #include <ina226.h> |
|
9 | #include <ina226.h> | |
10 |
|
10 | |||
11 |
|
11 | |||
12 | extern streamdevice* __opnfiles__[]; |
|
12 | extern streamdevice* __opnfiles__[]; | |
13 |
|
13 | |||
14 | int main() |
|
14 | int main() | |
15 | { |
|
15 | { | |
16 | INA226_t Psens1; |
|
16 | INA226_t ina5VSens,ina33VSens,ina15VSens; | |
17 | gpioset(PSU_DISABLE); |
|
17 | gpioset(PSU_DISABLE); | |
18 | ina226open(&Psens1,i2c2,0,0,15,300000); |
|
18 | volatile int32_t* regtest=(volatile int32_t*)0x60000000; | |
19 | uint16_t id=ina226getID(&Psens1); |
|
19 | volatile int32_t* regtest2=(volatile int32_t*)(0x60000000+(1<<20)); | |
20 | printf("INA226 ID=%x\n\r",0x0ffff&id); |
|
20 | //ina226open(&ina5VSens,i2c2,INA226_MODE_SHUNT_VOLTAGE_CONTINUOUS|INA226_AVERAGES_16|INA226_BUS_CONV_8244us|INA226_SHUNT_CONV_8244us,0,0,15,1000000); | |
21 | id=ina226getReg(&Psens1,INA226_Calibration_Reg); |
|
21 | //ina226open(&ina15VSens,i2c2,INA226_MODE_SHUNT_VOLTAGE_CONTINUOUS|INA226_AVERAGES_16|INA226_BUS_CONV_8244us|INA226_SHUNT_CONV_8244us,1,0,15,1000000); | |
22 | printf("INA226 CAL=%x\n\r",0x0ffff&id); |
|
22 | *regtest=0; | |
23 | unsigned int current; |
|
23 | printf("LCD ID=%d\n\r",*regtest2); | |
|
24 | if(-1==ina226open(&ina33VSens,i2c2,INA226_MODE_SHUNT_VOLTAGE_CONTINUOUS|INA226_AVERAGES_16|INA226_BUS_CONV_8244us|INA226_SHUNT_CONV_8244us,0,1,15,1000000)) | |||
|
25 | { | |||
|
26 | printf("Can't open 3.3V monitor\n\r"); | |||
|
27 | } | |||
|
28 | ||||
|
29 | int current5V,current33V,current15V; | |||
|
30 | printf("\t5V\t3.3V\n\r"); | |||
24 | while(1) |
|
31 | while(1) | |
25 | { |
|
32 | { | |
26 |
for(volatile int i=0;i<1024* |
|
33 | for(volatile int i=0;i<1024*1024;i++); | |
27 | gpioset(LED1); |
|
34 | gpioset(LED1); | |
28 | gpioclr(LED2); |
|
35 | gpioclr(LED2); | |
29 |
for(volatile int i=0;i<1024* |
|
36 | for(volatile int i=0;i<1024*1024;i++); | |
30 | gpioclr(LED1); |
|
37 | gpioclr(LED1); | |
31 | gpioset(LED2); |
|
38 | gpioset(LED2); | |
32 |
current = ina226getCurrent(& |
|
39 | //current5V = ina226getCurrent(&ina5VSens); | |
33 | printf("Current = %uοΏ½A %umA\n\r",current,current/1000); |
|
40 | current33V = ina226getCurrent(&ina33VSens); | |
|
41 | //current15V = ina226getCurrent(&ina15VSens); | |||
|
42 | //current = ina226getReg(&Psens1,INA226_Current_Reg); | |||
|
43 | //printf("%duA %dmA\t%duA %dmA\n\r",current5V,current5V/1000,current33V,current33V/1000); | |||
|
44 | printf("%duA %dmA\n\r",current33V,current33V/1000); | |||
34 | } |
|
45 | } | |
35 | printf("hello world\n\r"); |
|
46 | printf("hello world\n\r"); | |
36 | return 0; |
|
47 | return 0; | |
37 | } |
|
48 | } | |
38 |
|
49 | |||
39 |
|
50 | |||
40 |
|
51 | |||
41 |
|
52 | |||
42 |
|
53 | |||
43 |
|
54 | |||
44 |
|
55 | |||
45 |
|
56 | |||
46 |
|
57 |
@@ -1,72 +1,74 | |||||
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 int i2ctimeout; | |||
|
45 | ||||
44 | extern i2c_t i2copen(int count); |
|
46 | 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); |
|
47 | 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); |
|
48 | extern int i2cclose(i2c_t dev); | |
47 | extern int i2csetpins(i2c_t dev,uint32_t SDA,uint32_t SCL); |
|
49 | extern int i2csetpins(i2c_t dev,uint32_t SDA,uint32_t SCL); | |
48 | extern int i2cenable(i2c_t dev); |
|
50 | extern int i2cenable(i2c_t dev); | |
49 | extern int i2cdisable(i2c_t dev); |
|
51 | extern int i2cdisable(i2c_t dev); | |
50 | //extern int i2csetconfig(i2c_t dev); |
|
52 | //extern int i2csetconfig(i2c_t dev); | |
51 | extern int i2csetspeed(i2c_t dev,uint32_t speed); |
|
53 | extern int i2csetspeed(i2c_t dev,uint32_t speed); | |
52 | //extern int i2csetdatabits(i2c_t* dev,uartbits_t databits); |
|
54 | //extern int i2csetdatabits(i2c_t* dev,uartbits_t databits); | |
53 | extern int i2cwrite(i2c_t dev,char address,char* data,int count); |
|
55 | 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); |
|
56 | extern int i2cread(i2c_t dev,char address,char* data,int count); | |
55 | extern int i2cbusy(i2c_t dev); |
|
57 | extern int i2cbusy(i2c_t dev); | |
56 | extern int i2cStatusCheck(i2c_t dev,int32_t flagMask); |
|
58 | extern int i2cStatusCheck(i2c_t dev,int32_t flagMask); | |
57 | #ifdef __cplusplus |
|
59 | #ifdef __cplusplus | |
58 | } |
|
60 | } | |
59 | #endif |
|
61 | #endif | |
60 | #endif //I2C_H |
|
62 | #endif //I2C_H | |
61 |
|
63 | |||
62 |
|
64 | |||
63 |
|
65 | |||
64 |
|
66 | |||
65 |
|
67 | |||
66 |
|
68 | |||
67 |
|
69 | |||
68 |
|
70 | |||
69 |
|
71 | |||
70 |
|
72 | |||
71 |
|
73 | |||
72 |
|
74 |
@@ -1,66 +1,104 | |||||
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 | #ifndef INA226_H |
|
22 | #ifndef INA226_H | |
23 | #define INA226_H |
|
23 | #define INA226_H | |
24 |
|
24 | |||
25 | #include <i2c.h> |
|
25 | #include <i2c.h> | |
26 | #include <uhandle.h> |
|
26 | #include <uhandle.h> | |
27 |
|
27 | |||
28 | typedef struct INA226_t |
|
28 | typedef struct INA226_t | |
29 | { |
|
29 | { | |
30 | i2c_t i2cdev; |
|
30 | i2c_t i2cdev; | |
31 | uint8_t devAddress; |
|
31 | uint8_t devAddress; | |
32 | uint32_t shuntmOhm; |
|
32 | uint32_t shuntmOhm; | |
33 | uint32_t CurrentRangeuAmp; |
|
33 | uint32_t CurrentRangeuAmp; | |
34 | }INA226_t; |
|
34 | }INA226_t; | |
35 |
|
35 | |||
36 | extern int ina226open(INA226_t* dev,i2c_t i2cdev,uint8_t A0,uint8_t A1,uint32_t shuntmOhm, uint32_t CurrentRangeuAmp); |
|
36 | extern int ina226open(INA226_t* dev,i2c_t i2cdev,uint16_t config,uint8_t A0,uint8_t A1,uint32_t shuntmOhm, uint32_t CurrentRangeuAmp); | |
37 | extern uint16_t ina226getID(INA226_t* dev); |
|
37 | extern uint16_t ina226getID(INA226_t* dev); | |
38 | extern int ina226calibrate(INA226_t* dev,uint32_t shuntmOhm, uint32_t CurrentRangeuAmp); |
|
38 | extern int ina226calibrate(INA226_t* dev,uint32_t shuntmOhm, uint32_t CurrentRangeuAmp); | |
39 | extern uint32_t ina226getBusVoltage(INA226_t* dev); |
|
39 | extern uint32_t ina226getBusVoltage(INA226_t* dev); | |
40 | extern uint32_t ina226getPower(INA226_t* dev); |
|
40 | extern uint32_t ina226getPower(INA226_t* dev); | |
41 |
extern |
|
41 | extern int32_t ina226getCurrent(INA226_t* dev); | |
42 | extern uint16_t ina226getReg(INA226_t* dev,char reg); |
|
42 | extern uint16_t ina226getReg(INA226_t* dev,char reg); | |
43 | extern int ina226setReg(INA226_t* dev,char reg,int16_t value); |
|
43 | extern int ina226setReg(INA226_t* dev,char reg,int16_t value); | |
44 |
|
44 | |||
45 |
|
45 | |||
46 | #define INA226_I2C_ADDRESS 0x40 |
|
46 | #define INA226_I2C_ADDRESS 0x40 | |
47 |
|
47 | |||
48 |
|
48 | |||
49 | #define INA226_Die_ID_Reg 0xFF |
|
49 | #define INA226_Die_ID_Reg 0xFF | |
50 | #define INA226_Configuration_Reg 0 |
|
50 | #define INA226_Configuration_Reg 0 | |
51 | #define INA226_Shunt_Voltage_Reg 1 |
|
51 | #define INA226_Shunt_Voltage_Reg 1 | |
52 | #define INA226_Bus_Voltage_Reg 2 |
|
52 | #define INA226_Bus_Voltage_Reg 2 | |
53 | #define INA226_Power_Reg 3 |
|
53 | #define INA226_Power_Reg 3 | |
54 | #define INA226_Current_Reg 4 |
|
54 | #define INA226_Current_Reg 4 | |
55 | #define INA226_Calibration_Reg 5 |
|
55 | #define INA226_Calibration_Reg 5 | |
56 | #define INA226_Mask_Enable_Reg 6 |
|
56 | #define INA226_Mask_Enable_Reg 6 | |
57 | #define INA226_Alert_Limit_Reg 7 |
|
57 | #define INA226_Alert_Limit_Reg 7 | |
58 |
|
58 | |||
|
59 | #define INA226_AVERAGES_1 0 | |||
|
60 | #define INA226_AVERAGES_4 (1<<9) | |||
|
61 | #define INA226_AVERAGES_16 (2<<9) | |||
|
62 | #define INA226_AVERAGES_64 (3<<9) | |||
|
63 | #define INA226_AVERAGES_128 (4<<9) | |||
|
64 | #define INA226_AVERAGES_256 (5<<9) | |||
|
65 | #define INA226_AVERAGES_512 (6<<9) | |||
|
66 | #define INA226_AVERAGES_1024 (7<<9) | |||
|
67 | ||||
|
68 | #define INA226_BUS_CONV_140us 0 | |||
|
69 | #define INA226_BUS_CONV_204us (1<<6) | |||
|
70 | #define INA226_BUS_CONV_332us (2<<6) | |||
|
71 | #define INA226_BUS_CONV_588us (3<<6) | |||
|
72 | #define INA226_BUS_CONV_1100us (4<<6) | |||
|
73 | #define INA226_BUS_CONV_2116us (5<<6) | |||
|
74 | #define INA226_BUS_CONV_4156us (6<<6) | |||
|
75 | #define INA226_BUS_CONV_8244us (7<<6) | |||
|
76 | ||||
|
77 | #define INA226_SHUNT_CONV_140us 0 | |||
|
78 | #define INA226_SHUNT_CONV_204us (1<<3) | |||
|
79 | #define INA226_SHUNT_CONV_332us (2<<3) | |||
|
80 | #define INA226_SHUNT_CONV_588us (3<<3) | |||
|
81 | #define INA226_SHUNT_CONV_1100us (4<<3) | |||
|
82 | #define INA226_SHUNT_CONV_2116us (5<<3) | |||
|
83 | #define INA226_SHUNT_CONV_4156us (6<<3) | |||
|
84 | #define INA226_SHUNT_CONV_8244us (7<<3) | |||
|
85 | ||||
|
86 | #define INA226_MODE_POWER_DOWN 0 | |||
|
87 | #define INA226_MODE_SHUNT_VOLTAGE_TRIGGERRED 1 | |||
|
88 | #define INA226_MODE_BUS_VOLTAGE_TRIGGERRED 2 | |||
|
89 | #define INA226_MODE_SHUNT_AND_BUS_TRIGGERRED 3 | |||
|
90 | #define INA226_MODE_SHUNT_VOLTAGE_CONTINUOUS 5 | |||
|
91 | #define INA226_MODE_BUS_VOLTAGE_CONTINUOUS 6 | |||
|
92 | #define INA226_MODE_SHUNT_AND_BUS_CONTINUOUS 7 | |||
|
93 | ||||
|
94 | ||||
|
95 | ||||
|
96 | ||||
59 | #endif |
|
97 | #endif | |
60 |
|
98 | |||
61 |
|
99 | |||
62 |
|
100 | |||
63 |
|
101 | |||
64 |
|
102 | |||
65 |
|
103 | |||
66 |
|
104 |
@@ -1,110 +1,114 | |||||
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 | #include <ina226.h> |
|
22 | #include <ina226.h> | |
23 | #include <stdio.h> |
|
23 | #include <stdio.h> | |
24 | #include <stddef.h> |
|
24 | #include <stddef.h> | |
25 |
|
25 | |||
26 | int ina226open(INA226_t *dev, i2c_t i2cdev, uint8_t A0, uint8_t A1, uint32_t shuntmOhm, uint32_t CurrentRangeuAmp) |
|
26 | ||
|
27 | int ina226open(INA226_t *dev, i2c_t i2cdev, uint16_t config, uint8_t A0, uint8_t A1, uint32_t shuntmOhm, uint32_t CurrentRangeuAmp) | |||
27 | { |
|
28 | { | |
28 | if(dev != NULL) |
|
29 | if(dev != NULL) | |
29 | { |
|
30 | { | |
30 | dev->i2cdev=i2cdev; |
|
31 | dev->i2cdev=i2cdev; | |
31 | dev->devAddress = INA226_I2C_ADDRESS | (A0 & 1) | ((A1<<1) & 2); |
|
32 | dev->devAddress = INA226_I2C_ADDRESS | (A0 & 1) | ((A1<<1) & 2); | |
|
33 | printf("dev->devAddress = %x\n\r",dev->devAddress); | |||
32 | dev->shuntmOhm = shuntmOhm; |
|
34 | dev->shuntmOhm = shuntmOhm; | |
33 | dev->CurrentRangeuAmp = CurrentRangeuAmp; |
|
35 | dev->CurrentRangeuAmp = CurrentRangeuAmp; | |
34 | ina226setReg(dev,INA226_Configuration_Reg,0x8000); |
|
36 | ina226setReg(dev,INA226_Configuration_Reg,0x8000); | |
|
37 | ina226setReg(dev,INA226_Configuration_Reg,config); | |||
35 | return ina226calibrate(dev,shuntmOhm,CurrentRangeuAmp); |
|
38 | return ina226calibrate(dev,shuntmOhm,CurrentRangeuAmp); | |
36 | } |
|
39 | } | |
37 | return -1; |
|
40 | return -1; | |
38 | } |
|
41 | } | |
39 |
|
42 | |||
40 | uint16_t ina226getID(INA226_t* dev) |
|
43 | uint16_t ina226getID(INA226_t* dev) | |
41 | { |
|
44 | { | |
42 | if(dev != NULL) |
|
45 | if(dev != NULL) | |
43 | { |
|
46 | { | |
44 | uint16_t id=ina226getReg(dev,INA226_Die_ID_Reg); |
|
47 | uint16_t id=ina226getReg(dev,INA226_Die_ID_Reg); | |
45 | return id; |
|
48 | return id; | |
46 | } |
|
49 | } | |
47 | return -1; |
|
50 | return -1; | |
48 | } |
|
51 | } | |
49 |
|
52 | |||
50 |
|
53 | |||
51 | int ina226calibrate(INA226_t* dev,uint32_t shuntmOhm, uint32_t CurrentRangeuAmp) |
|
54 | int ina226calibrate(INA226_t* dev,uint32_t shuntmOhm, uint32_t CurrentRangeuAmp) | |
52 | { |
|
55 | { | |
53 | dev->shuntmOhm = shuntmOhm; |
|
56 | dev->shuntmOhm = shuntmOhm; | |
54 | dev->CurrentRangeuAmp = CurrentRangeuAmp; |
|
57 | dev->CurrentRangeuAmp = CurrentRangeuAmp; | |
55 | uint32_t CAL= (uint32_t)(5210000/(shuntmOhm*(CurrentRangeuAmp>>15))); |
|
58 | uint32_t CAL= (uint32_t)(5210000/(shuntmOhm*(CurrentRangeuAmp>>15))); | |
56 | printf("CAL = %x\n\r",CAL); |
|
|||
57 | return ina226setReg(dev,INA226_Calibration_Reg,(uint16_t)CAL); |
|
59 | return ina226setReg(dev,INA226_Calibration_Reg,(uint16_t)CAL); | |
58 | } |
|
60 | } | |
59 |
|
61 | |||
60 | uint32_t ina226getBusVoltage(INA226_t *dev) |
|
62 | uint32_t ina226getBusVoltage(INA226_t *dev) | |
61 | { |
|
63 | { | |
62 | uint32_t busVoltage= ina226getReg(dev,INA226_Bus_Voltage_Reg); |
|
64 | uint32_t busVoltage= ina226getReg(dev,INA226_Bus_Voltage_Reg); | |
63 | busVoltage*= 1250; |
|
65 | busVoltage*= 1250; | |
64 | return busVoltage; |
|
66 | return busVoltage; | |
65 | } |
|
67 | } | |
66 |
|
68 | |||
67 | uint32_t ina226getPower(INA226_t *dev) |
|
69 | uint32_t ina226getPower(INA226_t *dev) | |
68 | { |
|
70 | { | |
69 | uint32_t power= ina226getReg(dev,INA226_Power_Reg); |
|
71 | uint32_t power= ina226getReg(dev,INA226_Power_Reg); | |
70 | power*= ((dev->CurrentRangeuAmp>>15)*25); |
|
72 | power*= ((dev->CurrentRangeuAmp>>15)*25); | |
71 | return power; |
|
73 | return power; | |
72 | } |
|
74 | } | |
73 |
|
75 | |||
74 |
|
|
76 | int32_t ina226getCurrent(INA226_t *dev) | |
75 | { |
|
77 | { | |
76 |
|
|
78 | int32_t current= ina226getReg(dev,INA226_Current_Reg); | |
77 | //current*= ((dev->CurrentRangeuAmp>>15)); |
|
79 | current<<=16; | |
|
80 | current>>=16; | |||
|
81 | current = current*(dev->CurrentRangeuAmp>>15); | |||
78 | return current; |
|
82 | return current; | |
79 | } |
|
83 | } | |
80 |
|
84 | |||
81 | uint16_t ina226getReg(INA226_t* dev,char reg) |
|
85 | uint16_t ina226getReg(INA226_t* dev,char reg) | |
82 | { |
|
86 | { | |
83 | if(dev != NULL) |
|
87 | if(dev != NULL) | |
84 | { |
|
88 | { | |
85 | char DATA[2]; |
|
89 | char DATA[2]; | |
86 | DATA[0]=reg; |
|
90 | DATA[0]=reg; | |
87 | i2cwrite(dev->i2cdev,dev->devAddress,DATA,1); |
|
91 | i2cwrite(dev->i2cdev,dev->devAddress,DATA,1); | |
88 | i2cread(dev->i2cdev,dev->devAddress,DATA,2); |
|
92 | i2cread(dev->i2cdev,dev->devAddress,DATA,2); | |
89 | uint16_t val=DATA[0]; |
|
93 | uint16_t val=DATA[0]; | |
90 | val=(val<<8)+DATA[1]; |
|
94 | val=(val<<8)+DATA[1]; | |
91 | return val; |
|
95 | return val; | |
92 | } |
|
96 | } | |
93 | return -1; |
|
97 | return -1; | |
94 | } |
|
98 | } | |
95 |
|
99 | |||
96 |
|
100 | |||
97 | int ina226setReg(INA226_t* dev,char reg,int16_t value) |
|
101 | int ina226setReg(INA226_t* dev,char reg,int16_t value) | |
98 | { |
|
102 | { | |
99 | if(dev != NULL) |
|
103 | if(dev != NULL) | |
100 | { |
|
104 | { | |
101 | char DATA[3]; |
|
105 | char DATA[3]; | |
102 | DATA[0]=INA226_Calibration_Reg; |
|
106 | DATA[0]=INA226_Calibration_Reg; | |
103 | DATA[1]=(char)(0xff & (value>>8)); |
|
107 | DATA[1]=(char)(0xff & (value>>8)); | |
104 | DATA[2]=(char)(0xff & value); |
|
108 | DATA[2]=(char)(0xff & value); | |
105 | if(3==i2cwrite(dev->i2cdev,dev->devAddress,DATA,3))return 1; |
|
109 | if(3==i2cwrite(dev->i2cdev,dev->devAddress,DATA,3))return 1; | |
106 | } |
|
110 | } | |
107 | return -1; |
|
111 | return -1; | |
108 | } |
|
112 | } | |
109 |
|
113 | |||
110 |
|
114 |
@@ -1,293 +1,341 | |||||
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 | int i2ctimeout=1000*1000; | |||
33 |
|
34 | |||
34 | I2C_TypeDef* _i2c_dev_table[3]={I2C1,I2C2,I2C3}; |
|
35 | I2C_TypeDef* _i2c_dev_table[3]={I2C1,I2C2,I2C3}; | |
35 |
|
36 | |||
36 | i2c_t i2copen(int count) |
|
37 | i2c_t i2copen(int count) | |
37 | { |
|
38 | { | |
38 | #define _INIT_DEV(_RCC_) \ |
|
39 | #define _INIT_DEV(_RCC_) \ | |
39 | RCC_APB1PeriphClockCmd(_RCC_, ENABLE); \ |
|
40 | RCC_APB1PeriphClockCmd(_RCC_, ENABLE); \ | |
40 | RCC_APB1PeriphResetCmd(_RCC_, ENABLE); \ |
|
41 | RCC_APB1PeriphResetCmd(_RCC_, ENABLE); \ | |
41 | RCC_APB1PeriphResetCmd(_RCC_, DISABLE); \ |
|
42 | RCC_APB1PeriphResetCmd(_RCC_, DISABLE); \ | |
42 | RCC_APB1PeriphClockCmd(_RCC_, ENABLE); |
|
43 | RCC_APB1PeriphClockCmd(_RCC_, ENABLE); | |
43 |
|
44 | |||
44 | switch(count) |
|
45 | switch(count) | |
45 | { |
|
46 | { | |
46 | case i2c1: |
|
47 | case i2c1: | |
47 | _INIT_DEV(RCC_APB1Periph_I2C1); |
|
48 | _INIT_DEV(RCC_APB1Periph_I2C1); | |
48 | return i2c1; |
|
49 | return i2c1; | |
49 | break; |
|
50 | break; | |
50 | case i2c2: |
|
51 | case i2c2: | |
51 | _INIT_DEV(RCC_APB1Periph_I2C2); |
|
52 | _INIT_DEV(RCC_APB1Periph_I2C2); | |
52 | return i2c2; |
|
53 | return i2c2; | |
53 | break; |
|
54 | break; | |
54 | case i2c3: |
|
55 | case i2c3: | |
55 | _INIT_DEV(RCC_APB1Periph_I2C3); |
|
56 | _INIT_DEV(RCC_APB1Periph_I2C3); | |
56 | return i2c3; |
|
57 | return i2c3; | |
57 | break; |
|
58 | break; | |
58 | default: |
|
59 | default: | |
59 | break; |
|
60 | break; | |
60 | } |
|
61 | } | |
61 | return -1; |
|
62 | return -1; | |
62 | } |
|
63 | } | |
63 |
|
64 | |||
64 | i2c_t i2copenandconfig(int count,uint32_t cfg,uint32_t speed,uint32_t SDA,uint32_t SCL) |
|
65 | i2c_t i2copenandconfig(int count,uint32_t cfg,uint32_t speed,uint32_t SDA,uint32_t SCL) | |
65 | { |
|
66 | { | |
66 | i2c_t dev = i2copen(count); |
|
67 | i2c_t dev = i2copen(count); | |
67 | if(dev!=-1) |
|
68 | if(dev!=-1) | |
68 | { |
|
69 | { | |
69 | i2cclose(dev); |
|
70 | i2cclose(dev); | |
70 | i2csetpins(dev,SDA,SCL); |
|
71 | i2csetpins(dev,SDA,SCL); | |
71 | i2copen(count); |
|
72 | i2copen(count); | |
72 | i2cenable(count); |
|
73 | i2cenable(count); | |
73 | //I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev]; |
|
74 | //I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev]; | |
74 | i2csetspeed(dev,speed); |
|
75 | i2csetspeed(dev,speed); | |
75 | i2cenable(count); |
|
76 | i2cenable(count); | |
76 | } |
|
77 | } | |
77 | return dev; |
|
78 | return dev; | |
78 | } |
|
79 | } | |
79 |
|
80 | |||
80 | int i2cclose(i2c_t dev) |
|
81 | int i2cclose(i2c_t dev) | |
81 | { |
|
82 | { | |
82 | switch(dev) |
|
83 | switch(dev) | |
83 | { |
|
84 | { | |
84 | case i2c1: |
|
85 | case i2c1: | |
85 | RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE); |
|
86 | RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE); | |
86 | break; |
|
87 | break; | |
87 | case i2c2: |
|
88 | case i2c2: | |
88 | RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, ENABLE); |
|
89 | RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, ENABLE); | |
89 | break; |
|
90 | break; | |
90 | case i2c3: |
|
91 | case i2c3: | |
91 | RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C3, ENABLE); |
|
92 | RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C3, ENABLE); | |
92 | break; |
|
93 | break; | |
93 | default: |
|
94 | default: | |
94 | break; |
|
95 | break; | |
95 | } |
|
96 | } | |
96 | return 1; |
|
97 | return 1; | |
97 | } |
|
98 | } | |
98 |
|
99 | |||
99 | int i2csetpins(i2c_t dev, uint32_t SDA, uint32_t SCL) |
|
100 | int i2csetpins(i2c_t dev, uint32_t SDA, uint32_t SCL) | |
100 | { |
|
101 | { | |
101 | if((dev<3)&&(dev>=0)) |
|
102 | if((dev<3)&&(dev>=0)) | |
102 | { |
|
103 | { | |
103 | gpio_t SDApin,SCLpin; |
|
104 | gpio_t SDApin,SCLpin; | |
104 | SDApin = gpioopen(SDA); |
|
105 | SDApin = gpioopen(SDA); | |
105 | SCLpin = gpioopen(SCL); |
|
106 | SCLpin = gpioopen(SCL); | |
106 | SDApin |= gpiolowspeed | gpioaf | gpioopendraintype | gpionopulltype; |
|
107 | SDApin |= gpiolowspeed | gpioaf | gpioopendraintype | gpionopulltype; | |
107 | SCLpin |= gpiolowspeed | gpiooutdir | gpioopendraintype | gpionopulltype; |
|
108 | SCLpin |= gpiolowspeed | gpiooutdir | gpioopendraintype | gpionopulltype; | |
108 | gpiosetconfig(&SCLpin); |
|
109 | gpiosetconfig(&SCLpin); | |
109 | for(int i=0;i<32;i++) |
|
110 | for(int i=0;i<32;i++) | |
110 | { |
|
111 | { | |
111 | gpioclr(SCLpin); |
|
112 | gpioclr(SCLpin); | |
112 | for(int l=0;l<200;l++) |
|
113 | for(int l=0;l<200;l++) | |
113 | {__asm__("nop");} |
|
114 | {__asm__("nop");} | |
114 | gpioset(SCLpin); |
|
115 | gpioset(SCLpin); | |
115 | for(int l=0;l<200;l++) |
|
116 | for(int l=0;l<200;l++) | |
116 | {__asm__("nop");} |
|
117 | {__asm__("nop");} | |
117 | } |
|
118 | } | |
118 | SCLpin = gpioopen(SCL); |
|
119 | SCLpin = gpioopen(SCL); | |
119 | SCLpin |= gpiolowspeed | gpioaf | gpioopendraintype | gpionopulltype; |
|
120 | SCLpin |= gpiolowspeed | gpioaf | gpioopendraintype | gpionopulltype; | |
120 | gpiosetconfig(&SDApin); |
|
121 | gpiosetconfig(&SDApin); | |
121 | gpiosetconfig(&SCLpin); |
|
122 | gpiosetconfig(&SCLpin); | |
122 | uint8_t gpioAFi2cx = GPIO_AF_I2C1; |
|
123 | uint8_t gpioAFi2cx = GPIO_AF_I2C1; | |
123 | switch(dev) |
|
124 | switch(dev) | |
124 | { |
|
125 | { | |
125 | case i2c1: |
|
126 | case i2c1: | |
126 | gpioAFi2cx = GPIO_AF_I2C1; |
|
127 | gpioAFi2cx = GPIO_AF_I2C1; | |
127 | break; |
|
128 | break; | |
128 | case i2c2: |
|
129 | case i2c2: | |
129 | gpioAFi2cx = GPIO_AF_I2C2; |
|
130 | gpioAFi2cx = GPIO_AF_I2C2; | |
130 | break; |
|
131 | break; | |
131 | case i2c3: |
|
132 | case i2c3: | |
132 | gpioAFi2cx = GPIO_AF_I2C3; |
|
133 | gpioAFi2cx = GPIO_AF_I2C3; | |
133 | break; |
|
134 | break; | |
134 | default: |
|
135 | default: | |
135 | break; |
|
136 | break; | |
136 | } |
|
137 | } | |
137 | GPIO_PinAFConfig(GPIOGETPORT(SDApin), (uint8_t)(SDApin & 0xF), gpioAFi2cx); |
|
138 | GPIO_PinAFConfig(GPIOGETPORT(SDApin), (uint8_t)(SDApin & 0xF), gpioAFi2cx); | |
138 | GPIO_PinAFConfig(GPIOGETPORT(SCLpin), (uint8_t)(SCLpin & 0xF), gpioAFi2cx); |
|
139 | GPIO_PinAFConfig(GPIOGETPORT(SCLpin), (uint8_t)(SCLpin & 0xF), gpioAFi2cx); | |
139 | return 0; |
|
140 | return 0; | |
140 | } |
|
141 | } | |
141 | return -1; |
|
142 | return -1; | |
142 | } |
|
143 | } | |
143 |
|
144 | |||
144 | int i2cenable(i2c_t dev) |
|
145 | int i2cenable(i2c_t dev) | |
145 | { |
|
146 | { | |
146 | if((dev<3)&&(dev>=0)) |
|
147 | if((dev<3)&&(dev>=0)) | |
147 | { |
|
148 | { | |
148 | I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev]; |
|
149 | I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev]; | |
149 | _dev_->CR1 |=1 ; |
|
150 | _dev_->CR1 |=1 ; | |
150 | return 0; |
|
151 | return 0; | |
151 | } |
|
152 | } | |
152 | return -1; |
|
153 | return -1; | |
153 | } |
|
154 | } | |
154 |
|
155 | |||
155 | int i2cdisable(i2c_t dev) |
|
156 | int i2cdisable(i2c_t dev) | |
156 | { |
|
157 | { | |
157 | if((dev<3)&&(dev>=0)) |
|
158 | if((dev<3)&&(dev>=0)) | |
158 | { |
|
159 | { | |
159 | I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev]; |
|
160 | I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev]; | |
160 | _dev_->CR1 &= ~1; |
|
161 | _dev_->CR1 &= ~1; | |
161 | return 0; |
|
162 | return 0; | |
162 | } |
|
163 | } | |
163 | return -1; |
|
164 | return -1; | |
164 | } |
|
165 | } | |
165 |
|
166 | |||
166 |
|
167 | |||
167 | int i2csetspeed(i2c_t dev,uint32_t speed) |
|
168 | int i2csetspeed(i2c_t dev,uint32_t speed) | |
168 | { |
|
169 | { | |
169 | if((dev<3)&&(dev>=0)) |
|
170 | if((dev<3)&&(dev>=0)) | |
170 | { |
|
171 | { | |
171 | I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev]; |
|
172 | I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev]; | |
172 | int32_t APB1Freq=getAPB1Freq()/1000000; |
|
173 | int32_t APB1Freq=getAPB1Freq()/1000000; | |
173 | if((APB1Freq>1)&&(APB1Freq<43)) |
|
174 | if((APB1Freq>1)&&(APB1Freq<43)) | |
174 | { |
|
175 | { | |
175 |
|
176 | |||
176 | uint16_t tmpreg=_dev_->CR2; |
|
177 | uint16_t tmpreg=_dev_->CR2; | |
177 | tmpreg &= ~(0x1f); |
|
178 | tmpreg &= ~(0x1f); | |
178 | tmpreg |= APB1Freq; |
|
179 | tmpreg |= APB1Freq; | |
179 | _dev_->CR2=tmpreg; |
|
180 | _dev_->CR2=tmpreg; | |
180 | i2cdisable(dev); |
|
181 | i2cdisable(dev); | |
181 | tmpreg=_dev_->CCR; |
|
182 | tmpreg=_dev_->CCR; | |
182 | APB1Freq=getAPB1Freq(); |
|
183 | APB1Freq=getAPB1Freq(); | |
183 | if(speed>100000) //100kHz= standard mode, 400kHz= fast mode |
|
184 | if(speed>100000) //100kHz= standard mode, 400kHz= fast mode | |
184 | { |
|
185 | { | |
185 | if(speed<=400000) |
|
186 | if(speed<=400000) | |
186 | { |
|
187 | { | |
187 | tmpreg |= 1<<15; |
|
188 | tmpreg |= 1<<15; | |
188 | tmpreg &= ~(1<<14); |
|
189 | tmpreg &= ~(1<<14); | |
189 | tmpreg &= ~(0xfff); |
|
190 | tmpreg &= ~(0xfff); | |
190 | tmpreg |= 0xfff & (APB1Freq/(3*speed)); |
|
191 | tmpreg |= 0xfff & (APB1Freq/(3*speed)); | |
191 | } |
|
192 | } | |
192 | } |
|
193 | } | |
193 | else |
|
194 | else | |
194 | { |
|
195 | { | |
195 | tmpreg &= ~(1<<15); |
|
196 | tmpreg &= ~(1<<15); | |
196 | tmpreg &= ~(0xfff); |
|
197 | tmpreg &= ~(0xfff); | |
197 | tmpreg |= 0xfff & (APB1Freq/(2*speed)); |
|
198 | tmpreg |= 0xfff & (APB1Freq/(2*speed)); | |
198 | } |
|
199 | } | |
199 | _dev_->CCR=tmpreg; |
|
200 | _dev_->CCR=tmpreg; | |
200 | tmpreg=_dev_->TRISE; |
|
201 | tmpreg=_dev_->TRISE; | |
201 | tmpreg &= ~(0x3f); |
|
202 | tmpreg &= ~(0x3f); | |
202 | tmpreg |= (APB1Freq/1000000)+1; |
|
203 | tmpreg |= (APB1Freq/1000000)+1; | |
203 | _dev_->TRISE = tmpreg; |
|
204 | _dev_->TRISE = tmpreg; | |
204 | i2cenable(dev); |
|
205 | i2cenable(dev); | |
205 | return 0; |
|
206 | return 0; | |
206 | } |
|
207 | } | |
207 | } |
|
208 | } | |
208 | return -1; |
|
209 | return -1; | |
209 | } |
|
210 | } | |
210 |
|
211 | |||
211 | int i2cbusy(i2c_t dev) |
|
212 | int i2cbusy(i2c_t dev) | |
212 | { |
|
213 | { | |
213 | if((dev<3)&&(dev>=0)) |
|
214 | if((dev<3)&&(dev>=0)) | |
214 | { |
|
215 | { | |
215 | I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev]; |
|
216 | I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev]; | |
216 | if((_dev_->SR2 & 2) ==2) return 1; /* Dev is busy */ |
|
217 | if((_dev_->SR2 & 2) ==2) return 1; /* Dev is busy */ | |
217 | return 0; /* Dev isn't busy */ |
|
218 | return 0; /* Dev isn't busy */ | |
218 | } |
|
219 | } | |
219 | return -1; /* Error, dev is out of range */ |
|
220 | return -1; /* Error, dev is out of range */ | |
220 | } |
|
221 | } | |
221 |
|
222 | |||
222 | int i2cwrite(i2c_t dev,char address,char* data,int count) |
|
223 | int i2cwrite(i2c_t dev,char address,char* data,int count) | |
223 | { |
|
224 | { | |
224 | if((dev<3)&&(dev>=0)) |
|
225 | if((dev<3)&&(dev>=0)) | |
225 | { |
|
226 | { | |
|
227 | int timeout=i2ctimeout; | |||
226 | I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev]; |
|
228 | I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev]; | |
227 |
while(i2cbusy(dev)) |
|
229 | while(i2cbusy(dev)) | |
|
230 | { | |||
|
231 | if(0==(timeout--))return -1; | |||
|
232 | } | |||
228 | _dev_->CR1 |= 1<<8; |
|
233 | _dev_->CR1 |= 1<<8; | |
229 | while(!i2cStatusCheck(dev,((uint32_t)0x00030001))); |
|
234 | timeout=i2ctimeout; | |
|
235 | while(!i2cStatusCheck(dev,((uint32_t)0x00030001))) | |||
|
236 | { | |||
|
237 | if(0==(timeout--))return -1; | |||
|
238 | } | |||
230 | _dev_->DR= address<<1; |
|
239 | _dev_->DR= address<<1; | |
231 | while(!i2cStatusCheck(dev, ((uint32_t)0x00070082))); |
|
240 | timeout=i2ctimeout; | |
|
241 | while(!i2cStatusCheck(dev, ((uint32_t)0x00070082))) | |||
|
242 | { | |||
|
243 | if(0==(timeout--))return -1; | |||
|
244 | } | |||
232 | address=_dev_->SR2; |
|
245 | address=_dev_->SR2; | |
233 | for(int i=0;i<count;i++) |
|
246 | for(int i=0;i<count;i++) | |
234 | { |
|
247 | { | |
235 | while(!i2cStatusCheck(dev,((uint32_t)0x00070080))); |
|
248 | timeout=i2ctimeout; | |
|
249 | while(!i2cStatusCheck(dev,((uint32_t)0x00070080))) | |||
|
250 | { | |||
|
251 | if(0==(timeout--))return -1; | |||
|
252 | } | |||
236 | _dev_->DR= data[i]; |
|
253 | _dev_->DR= data[i]; | |
237 | } |
|
254 | } | |
238 | while(!i2cStatusCheck(dev,1<<7)); |
|
255 | timeout=i2ctimeout; | |
239 |
while(!i2cStatusCheck(dev,1<< |
|
256 | while(!i2cStatusCheck(dev,1<<7)) | |
|
257 | { | |||
|
258 | if(0==(timeout--))return -1; | |||
|
259 | } | |||
|
260 | timeout=i2ctimeout; | |||
|
261 | while(!i2cStatusCheck(dev,1<<2)) | |||
|
262 | { | |||
|
263 | if(0==(timeout--))return -1; | |||
|
264 | } | |||
240 | _dev_->CR1 |= 1<<9; |
|
265 | _dev_->CR1 |= 1<<9; | |
241 | return count; |
|
266 | return count; | |
242 | } |
|
267 | } | |
243 | return -1; |
|
268 | return -1; | |
244 | } |
|
269 | } | |
245 |
|
270 | |||
246 | int i2cread(i2c_t dev,char address,char* data,int count) |
|
271 | int i2cread(i2c_t dev,char address,char* data,int count) | |
247 | { |
|
272 | { | |
248 | if((dev<3)&&(dev>=0)) |
|
273 | if((dev<3)&&(dev>=0)) | |
249 | { |
|
274 | { | |
250 | int i=0; |
|
275 | int i=0; | |
251 | while(i2cbusy(dev)); |
|
276 | int timeout=i2ctimeout; | |
|
277 | while(i2cbusy(dev)) | |||
|
278 | { | |||
|
279 | if(0==(timeout--))return -1; | |||
|
280 | } | |||
252 | I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev]; |
|
281 | I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev]; | |
253 | _dev_->CR1 |= (1<<8) + (1<<10); |
|
282 | _dev_->CR1 |= (1<<8) + (1<<10); | |
254 | while(!i2cStatusCheck(dev,0x00030001)); |
|
283 | timeout=i2ctimeout; | |
|
284 | while(!i2cStatusCheck(dev,0x00030001)) | |||
|
285 | { | |||
|
286 | if(0==(timeout--))return -1; | |||
|
287 | } | |||
255 | _dev_->DR= (address<<1) + 1; |
|
288 | _dev_->DR= (address<<1) + 1; | |
256 |
while(!i2cStatusCheck(dev,0x000002)) |
|
289 | while(!i2cStatusCheck(dev,0x000002)) | |
|
290 | { | |||
|
291 | if(0==(timeout--))return -1; | |||
|
292 | } | |||
257 | if(count==1) |
|
293 | if(count==1) | |
258 | { |
|
294 | { | |
259 | _dev_->CR1 &= ~(1<<10); |
|
295 | _dev_->CR1 &= ~(1<<10); | |
260 | } |
|
296 | } | |
261 | address=_dev_->SR2; |
|
297 | address=_dev_->SR2; | |
262 | for(i=0;i<(count-1);i++) |
|
298 | for(i=0;i<(count-1);i++) | |
263 | { |
|
299 | { | |
264 | while(!i2cStatusCheck(dev,0x0000040)); |
|
300 | timeout=i2ctimeout; | |
|
301 | while(!i2cStatusCheck(dev,0x0000040)) | |||
|
302 | { | |||
|
303 | if(0==(timeout--))return -1; | |||
|
304 | } | |||
265 | data[i]=_dev_->DR; |
|
305 | data[i]=_dev_->DR; | |
266 | } |
|
306 | } | |
267 | _dev_->CR1 &= ~(1<<10); |
|
307 | _dev_->CR1 &= ~(1<<10); | |
268 | _dev_->CR1 |= 1<<9; |
|
308 | _dev_->CR1 |= 1<<9; | |
269 | while(!i2cStatusCheck(dev,0x0000040)); |
|
309 | timeout=i2ctimeout; | |
|
310 | while(!i2cStatusCheck(dev,0x0000040)) | |||
|
311 | { | |||
|
312 | if(0==(timeout--))return -1; | |||
|
313 | } | |||
270 | data[i]=_dev_->DR; |
|
314 | data[i]=_dev_->DR; | |
271 | while(_dev_->CR1 & ((uint16_t)0x0200)); |
|
315 | timeout=i2ctimeout; | |
|
316 | while(_dev_->CR1 & ((uint16_t)0x0200)) | |||
|
317 | { | |||
|
318 | if(0==(timeout--))return -1; | |||
|
319 | } | |||
272 | _dev_->CR1 |= 1<<10; |
|
320 | _dev_->CR1 |= 1<<10; | |
273 | return count; |
|
321 | return count; | |
274 | } |
|
322 | } | |
275 | return -1; |
|
323 | return -1; | |
276 | } |
|
324 | } | |
277 |
|
325 | |||
278 |
|
326 | |||
279 | int i2cStatusCheck(i2c_t dev,int32_t flagMask) |
|
327 | int i2cStatusCheck(i2c_t dev,int32_t flagMask) | |
280 | { |
|
328 | { | |
281 | int32_t flag; |
|
329 | int32_t flag; | |
282 | if((dev<3)&&(dev>=0)) |
|
330 | if((dev<3)&&(dev>=0)) | |
283 | { |
|
331 | { | |
284 | I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev]; |
|
332 | I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev]; | |
285 | flag= _dev_->SR1 + (_dev_->SR2<<16); |
|
333 | flag= _dev_->SR1 + (_dev_->SR2<<16); | |
286 | if(flagMask==(flag & flagMask)) |
|
334 | if(flagMask==(flag & flagMask)) | |
287 | return 1; |
|
335 | return 1; | |
288 | return 0; |
|
336 | return 0; | |
289 | } |
|
337 | } | |
290 | return -1; |
|
338 | return -1; | |
291 | } |
|
339 | } | |
292 |
|
340 | |||
293 |
|
341 |
General Comments 0
You need to be logged in to leave comments.
Login now