@@ -1,161 +1,163 | |||
|
1 | 1 | /*------------------------------------------------------------------------------ |
|
2 | 2 | -- This file is a part of the libuc, microcontroler library |
|
3 | 3 | -- Copyright (C) 2011, Alexis Jeandet |
|
4 | 4 | -- |
|
5 | 5 | -- This program is free software; you can redistribute it and/or modify |
|
6 | 6 | -- it under the terms of the GNU General Public License as published by |
|
7 | 7 | -- the Free Software Foundation; either version 3 of the License, or |
|
8 | 8 | -- (at your option) any later version. |
|
9 | 9 | -- |
|
10 | 10 | -- This program is distributed in the hope that it will be useful, |
|
11 | 11 | -- but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 | 12 | -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 | 13 | -- GNU General Public License for more details. |
|
14 | 14 | -- |
|
15 | 15 | -- You should have received a copy of the GNU General Public License |
|
16 | 16 | -- along with this program; if not, write to the Free Software |
|
17 | 17 | -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
18 | 18 | ------------------------------------------------------------------------------- |
|
19 | 19 | -- Author : Alexis Jeandet |
|
20 | 20 | -- Mail : alexis.jeandet@gmail.com |
|
21 | 21 | -------------------------------------------------------------------------------*/ |
|
22 | 22 | #include "bsp.h" |
|
23 | 23 | #include <streamdevices.h> |
|
24 | 24 | #include <malloc.h> |
|
25 | 25 | #include <gpio.h> |
|
26 | 26 | #include <uart.h> |
|
27 | 27 | #include <stdio.h> |
|
28 | 28 | #include <stm32f4xx_gpio.h> |
|
29 | #include <i2c.h> | |
|
29 | 30 | uint32_t OSC0 =8000000; |
|
30 | 31 | uint32_t INTOSC =16000000; |
|
31 | 32 | uint32_t RTCOSC =32768; |
|
32 | 33 | uint32_t currentCpuFreq=0; |
|
33 | 34 | extern streamdevice* __opnfiles__[__MAX_OPENED_FILES__]; |
|
34 | 35 | |
|
35 | 36 | float VREF0 =(float)3.3; |
|
36 | 37 | |
|
37 | 38 | int bsp_init() |
|
38 | 39 | { |
|
39 | 40 | int i=0; |
|
40 | 41 | for(i=0;i<32;i++) |
|
41 | 42 | { |
|
42 | 43 | __opnfiles__[i] = NULL; |
|
43 | 44 | } |
|
44 | 45 | bsp_GPIO_init(); |
|
45 | 46 | bsp_uart_init(); |
|
47 | bsp_iic_init(); | |
|
46 | 48 | printf("\r================================================================\n\r"); |
|
47 | 49 | printf("================================================================\n\r"); |
|
48 | 50 | printf(BSP); |
|
49 | 51 | printf(" initialised\n\r"); |
|
50 | 52 | printf("================================================================\n\r"); |
|
51 | 53 | return 1; |
|
52 | 54 | } |
|
53 | 55 | |
|
54 | 56 | void bsp_GPIO_init() |
|
55 | 57 | { |
|
56 | 58 | gpio_t gpio1 = gpioopen(LED1); |
|
57 | 59 | gpio_t gpio2 = gpioopen(LED2); |
|
58 | 60 | gpio_t gpio3 = gpioopen(LED3); |
|
59 | 61 | gpio_t gpio4 = gpioopen(PSU_DISABLE); |
|
60 | 62 | gpio_t gpio5 = gpioopen(PSU_ALERT_5V); |
|
61 | 63 | gpio_t gpio6 = gpioopen(PSU_ALERT_1_5V); |
|
62 | 64 | gpio_t gpio7 = gpioopen(PSU_ALERT_3_3V); |
|
63 | 65 | gpiosetspeed(&gpio1,gpiohighspeed); |
|
64 | 66 | gpiosetspeed(&gpio2,gpiohighspeed); |
|
65 | 67 | gpiosetspeed(&gpio3,gpiohighspeed); |
|
66 | 68 | gpiosetspeed(&gpio4,gpiohighspeed); |
|
67 | 69 | gpiosetspeed(&gpio5,gpiohighspeed); |
|
68 | 70 | gpiosetspeed(&gpio6,gpiohighspeed); |
|
69 | 71 | gpiosetspeed(&gpio7,gpiohighspeed); |
|
70 | 72 | gpioclr(PSU_DISABLE); |
|
71 | 73 | gpiosetdir(&gpio1,gpiooutdir); |
|
72 | 74 | gpiosetdir(&gpio3,gpiooutdir); |
|
73 | 75 | gpiosetdir(&gpio2,gpiooutdir); |
|
74 | 76 | gpiosetdir(&gpio4,gpiooutdir); |
|
75 | 77 | gpiosetdir(&gpio5,gpioindir); |
|
76 | 78 | gpiosetdir(&gpio6,gpioindir); |
|
77 | 79 | gpiosetdir(&gpio7,gpioindir); |
|
78 | 80 | gpioclr(PSU_DISABLE); |
|
79 | 81 | } |
|
80 | 82 | |
|
81 | 83 | void bsp_uart_init() |
|
82 | 84 | { |
|
83 | 85 | if(__opnfiles__[1]==NULL) |
|
84 | 86 | { |
|
85 | 87 | //uart_t* uart1 = (uart_t*)malloc(sizeof(uart_t)); |
|
86 | 88 | streamdevice* fd1 = (streamdevice*)malloc(sizeof(streamdevice)); |
|
87 | 89 | uart_t uart = uartopenandconfig(uart1,uartparitynone | uart8bits | uartonestop,115200,PA9,PA10,-1,-1); |
|
88 | 90 | uartmkstreamdev(uart,fd1); |
|
89 | 91 | __opnfiles__[1] = fd1; |
|
90 | 92 | } |
|
91 | 93 | else |
|
92 | 94 | { |
|
93 | 95 | uartopenandconfig(0,uartparitynone | uart8bits | uartonestop,115200,PA9,PA10,-1,-1); |
|
94 | 96 | } |
|
95 | 97 | } |
|
96 | 98 | |
|
97 | 99 | |
|
98 | 100 | void bsp_FSMC_init() |
|
99 | 101 | { |
|
100 | 102 | #define GPIOGETPORT(gpio) ((GPIO_TypeDef*)(((((uint32_t)gpio) & (uint32_t)0x0000FF00)*(uint32_t)4) + (uint32_t)GPIOA)) |
|
101 | 103 | #define GPIOPORTNUM(gpio) (((uint32_t)(gpio) & (uint32_t)0x0000FF00)>>(uint32_t)8) |
|
102 | 104 | |
|
103 | 105 | gpio_t LCD_DBxList[]={PD14,PD15,PD0,PD1,PE7,PE8,PE9,PE10,PE11,PE12,PE13,PE14,PE15\ |
|
104 | 106 | ,PD8,PD9,PD10,PD4,PD5,PD7,PE4}; |
|
105 | 107 | for(int i=0;i<20;i++) |
|
106 | 108 | { |
|
107 | 109 | gpio_t LCD_DBx = gpioopen(LCD_DBxList[i]); |
|
108 | 110 | LCD_DBx |= gpiohighspeed | gpioaf | gpiopushpulltype | gpionopulltype; |
|
109 | 111 | gpiosetconfig(&LCD_DBx); |
|
110 | 112 | GPIO_PinAFConfig(GPIOGETPORT(LCD_DBx), (uint8_t)(LCD_DBx & 0xF), GPIO_AF_FSMC); |
|
111 | 113 | } |
|
112 | 114 | } |
|
113 | 115 | |
|
114 | 116 | void bsp_spi_init() |
|
115 | 117 | { |
|
116 | 118 | |
|
117 | 119 | } |
|
118 | 120 | |
|
119 | 121 | |
|
120 | 122 | void bsp_iic_init() |
|
121 | 123 | { |
|
122 | ||
|
124 | i2copenandconfig(i2c2,0,400000,PF0,PF1); | |
|
123 | 125 | } |
|
124 | 126 | |
|
125 | 127 | void bsp_SD_init() |
|
126 | 128 | { |
|
127 | 129 | |
|
128 | 130 | } |
|
129 | 131 | |
|
130 | 132 | void vs10XXclearXCS(){} |
|
131 | 133 | void vs10XXsetXCS(){} |
|
132 | 134 | int vs10XXDREQ() |
|
133 | 135 | { |
|
134 | 136 | return 1; |
|
135 | 137 | } |
|
136 | 138 | |
|
137 | 139 | |
|
138 | 140 | void bsppowersdcard(char onoff) //always ON |
|
139 | 141 | { |
|
140 | 142 | |
|
141 | 143 | } |
|
142 | 144 | |
|
143 | 145 | char bspsdcardpresent() |
|
144 | 146 | { |
|
145 | 147 | return 0; |
|
146 | 148 | } |
|
147 | 149 | |
|
148 | 150 | char bspsdcardwriteprotected() |
|
149 | 151 | { |
|
150 | 152 | return 0; |
|
151 | 153 | } |
|
152 | 154 | |
|
153 | 155 | void bspsdcardselect(char YESNO) |
|
154 | 156 | { |
|
155 | 157 | |
|
156 | 158 | } |
|
157 | 159 | |
|
158 | 160 | |
|
159 | 161 | |
|
160 | 162 | |
|
161 | 163 |
@@ -1,142 +1,140 | |||
|
1 | 1 | /*------------------------------------------------------------------------------ |
|
2 | 2 | -- This file is a part of the libuc, microcontroler library |
|
3 | 3 | -- Copyright (C) 2011, Alexis Jeandet |
|
4 | 4 | -- |
|
5 | 5 | -- This program is free software; you can redistribute it and/or modify |
|
6 | 6 | -- it under the terms of the GNU General Public License as published by |
|
7 | 7 | -- the Free Software Foundation; either version 3 of the License, or |
|
8 | 8 | -- (at your option) any later version. |
|
9 | 9 | -- |
|
10 | 10 | -- This program is distributed in the hope that it will be useful, |
|
11 | 11 | -- but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 | 12 | -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 | 13 | -- GNU General Public License for more details. |
|
14 | 14 | -- |
|
15 | 15 | -- You should have received a copy of the GNU General Public License |
|
16 | 16 | -- along with this program; if not, write to the Free Software |
|
17 | 17 | -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
18 | 18 | ------------------------------------------------------------------------------- |
|
19 | 19 | -- Author : Alexis Jeandet |
|
20 | 20 | -- Mail : alexis.jeandet@gmail.com |
|
21 | 21 | -------------------------------------------------------------------------------*/ |
|
22 | 22 | #include "bsp.h" |
|
23 | 23 | #include <streamdevices.h> |
|
24 | 24 | #include <malloc.h> |
|
25 | 25 | #include <gpio.h> |
|
26 | 26 | #include <uart.h> |
|
27 | 27 | #include <stdio.h> |
|
28 | 28 | #include <i2c.h> |
|
29 | 29 | |
|
30 | 30 | uint32_t OSC0 =8000000; |
|
31 | 31 | uint32_t INTOSC =16000000; |
|
32 | 32 | uint32_t RTCOSC =32768; |
|
33 | 33 | uint32_t currentCpuFreq=0; |
|
34 | 34 | extern streamdevice* __opnfiles__[__MAX_OPENED_FILES__]; |
|
35 | 35 | |
|
36 | 36 | float VREF0 =(float)3.3; |
|
37 | 37 | |
|
38 | 38 | int bsp_init() |
|
39 | 39 | { |
|
40 | 40 | int i=0; |
|
41 | 41 | for(i=0;i<32;i++) |
|
42 | 42 | { |
|
43 | 43 | __opnfiles__[i] = NULL; |
|
44 | 44 | } |
|
45 | 45 | bsp_GPIO_init(); |
|
46 | 46 | bsp_uart_init(); |
|
47 | 47 | bsp_iic_init(); |
|
48 | 48 | printf("\r================================================================\n\r"); |
|
49 | 49 | printf("================================================================\n\r"); |
|
50 | 50 | printf(BSP); |
|
51 | 51 | printf(" initialised\n\r"); |
|
52 | 52 | printf("================================================================\n\r"); |
|
53 | 53 | return 1; |
|
54 | 54 | } |
|
55 | 55 | |
|
56 | 56 | void bsp_GPIO_init() |
|
57 | 57 | { |
|
58 | 58 | gpio_t gpio1 = gpioopen(PD12);//gpioopen(LED1); //PD9 D=> 0x0300 9 => 0x0009 |
|
59 | 59 | gpio_t gpio2 = gpioopen(PD13);//gpioopen(LED2); |
|
60 | 60 | gpio_t gpio3 = gpioopen(PD14);//gpioopen(LED2); |
|
61 | 61 | gpio_t gpio4 = gpioopen(PD15);//gpioopen(LED2); |
|
62 | 62 | gpio_t dacRst=gpioopen(PD4); |
|
63 | 63 | gpiosetspeed(&gpio1,gpiohighspeed); |
|
64 | 64 | gpiosetspeed(&gpio2,gpiohighspeed); |
|
65 | 65 | gpiosetspeed(&gpio3,gpiohighspeed); |
|
66 | 66 | gpiosetspeed(&gpio4,gpiohighspeed); |
|
67 | 67 | gpiosetspeed(&dacRst,gpiohighspeed); |
|
68 | 68 | gpiosetdir(&gpio1,gpiooutdir); |
|
69 | 69 | gpiosetdir(&gpio3,gpiooutdir); |
|
70 | 70 | gpiosetdir(&gpio2,gpiooutdir); |
|
71 | 71 | gpiosetdir(&gpio4,gpiooutdir); |
|
72 | 72 | gpiosetdir(&dacRst,gpiooutdir); |
|
73 | 73 | gpioset(dacRst); |
|
74 | 74 | } |
|
75 | 75 | |
|
76 | 76 | void bsp_uart_init() |
|
77 | 77 | { |
|
78 | 78 | if(__opnfiles__[1]==NULL) |
|
79 | 79 | { |
|
80 | 80 | //uart_t* uart1 = (uart_t*)malloc(sizeof(uart_t)); |
|
81 | 81 | streamdevice* fd1 = (streamdevice*)malloc(sizeof(streamdevice)); |
|
82 | 82 | uart_t uart = uartopenandconfig(uart3,uartparitynone | uart8bits | uartonestop,115200,PB10,PB11,-1,-1); |
|
83 | 83 | uartmkstreamdev(uart,fd1); |
|
84 | 84 | __opnfiles__[1] = fd1; |
|
85 | 85 | } |
|
86 | 86 | else |
|
87 | 87 | { |
|
88 | 88 | uartopenandconfig(2,uartparitynone | uart8bits | uartonestop,115200,PB10,PB11,-1,-1); |
|
89 | 89 | } |
|
90 | 90 | } |
|
91 | 91 | |
|
92 | 92 | void bsp_spi_init() |
|
93 | 93 | { |
|
94 | 94 | |
|
95 | 95 | } |
|
96 | 96 | |
|
97 | 97 | |
|
98 | 98 | void bsp_iic_init() |
|
99 | 99 | { |
|
100 |
|
|
|
101 | i2cenable(i2c1); | |
|
102 | if(0==i2copenandconfig(i2c3,0,400000,PC9,PA8))printf("I2C3 opened\n\r"); | |
|
103 | i2cenable(i2c3); | |
|
100 | i2copenandconfig(i2c1,0,400000,PB9,PB6); | |
|
101 | i2copenandconfig(i2c3,0,400000,PC9,PA8); | |
|
104 | 102 | } |
|
105 | 103 | |
|
106 | 104 | void bsp_SD_init() |
|
107 | 105 | { |
|
108 | 106 | |
|
109 | 107 | } |
|
110 | 108 | |
|
111 | 109 | void vs10XXclearXCS(){} |
|
112 | 110 | void vs10XXsetXCS(){} |
|
113 | 111 | int vs10XXDREQ() |
|
114 | 112 | { |
|
115 | 113 | return 1; |
|
116 | 114 | } |
|
117 | 115 | |
|
118 | 116 | |
|
119 | 117 | void bsppowersdcard(char onoff) //always ON |
|
120 | 118 | { |
|
121 | 119 | |
|
122 | 120 | } |
|
123 | 121 | |
|
124 | 122 | char bspsdcardpresent() |
|
125 | 123 | { |
|
126 | 124 | return 0; |
|
127 | 125 | } |
|
128 | 126 | |
|
129 | 127 | char bspsdcardwriteprotected() |
|
130 | 128 | { |
|
131 | 129 | return 0; |
|
132 | 130 | } |
|
133 | 131 | |
|
134 | 132 | void bspsdcardselect(char YESNO) |
|
135 | 133 | { |
|
136 | 134 | |
|
137 | 135 | } |
|
138 | 136 | |
|
139 | 137 | |
|
140 | 138 | |
|
141 | 139 | |
|
142 | 140 |
@@ -1,12 +1,12 | |||
|
1 | 1 | TEMPLATE = app |
|
2 | 2 | CONFIG += console |
|
3 | 3 | CONFIG -= qt |
|
4 | 4 | |
|
5 |
|
|
|
6 | BSP = SOLAR_LFR_PSU | |
|
5 | BSP = BEAGLESYNTH | |
|
6 | #BSP = SOLAR_LFR_PSU | |
|
7 | 7 | include($$(libuc2)/bsp/cfg/$$BSP/bsp.pri) |
|
8 | 8 | #include($$(libuc2)/rules/stm32f4-arm-none-eabi-gcc/rules.pri) |
|
9 | 9 | |
|
10 | 10 | SOURCES += \ |
|
11 | 11 | main.c |
|
12 | 12 |
@@ -1,40 +1,46 | |||
|
1 | 1 | #include <stdio.h> |
|
2 | 2 | #include <fat32.h> |
|
3 | 3 | #include <gpio.h> |
|
4 | 4 | #include <uart.h> |
|
5 | 5 | #include <stm32f4xx.h> |
|
6 | 6 | #include <bsp.h> |
|
7 | 7 | #include <i2c.h> |
|
8 | 8 | #include <CS43L22.h> |
|
9 | #include <ina226.h> | |
|
9 | 10 | |
|
10 | 11 | |
|
11 | 12 | extern streamdevice* __opnfiles__[]; |
|
12 | 13 | |
|
13 | 14 | int main() |
|
14 | 15 | { |
|
15 | CS43L22_t audioDac1; | |
|
16 | cs43l22open(&audioDac1,i2c1,0); | |
|
17 | char id=cs43l22getID(&audioDac1); | |
|
18 | printf("DAC ID=%x\n\r",0x0ff&id); | |
|
16 | INA226_t Psens1; | |
|
17 | gpioset(PSU_DISABLE); | |
|
18 | ina226open(&Psens1,i2c2,0,0,15,300000); | |
|
19 | uint16_t id=ina226getID(&Psens1); | |
|
20 | printf("INA226 ID=%x\n\r",0x0ffff&id); | |
|
21 | id=ina226getReg(&Psens1,INA226_Calibration_Reg); | |
|
22 | printf("INA226 CAL=%x\n\r",0x0ffff&id); | |
|
23 | unsigned int current; | |
|
19 | 24 | while(1) |
|
20 | 25 | { |
|
21 | 26 | for(volatile int i=0;i<1024*2048;i++); |
|
22 | 27 | gpioset(LED1); |
|
23 | 28 | gpioclr(LED2); |
|
24 | 29 | for(volatile int i=0;i<1024*2048;i++); |
|
25 | 30 | gpioclr(LED1); |
|
26 | 31 | gpioset(LED2); |
|
27 | i2cwrite(i2c1,0x4a,"test",1); | |
|
32 | current = ina226getCurrent(&Psens1); | |
|
33 | printf("Current = %uοΏ½A %umA\n\r",current,current/1000); | |
|
28 | 34 | } |
|
29 | 35 | printf("hello world\n\r"); |
|
30 | 36 | return 0; |
|
31 | 37 | } |
|
32 | 38 | |
|
33 | 39 | |
|
34 | 40 | |
|
35 | 41 | |
|
36 | 42 | |
|
37 | 43 | |
|
38 | 44 | |
|
39 | 45 | |
|
40 | 46 |
@@ -1,62 +1,66 | |||
|
1 | 1 | /*------------------------------------------------------------------------------ |
|
2 | 2 | -- This file is a part of the libuc, microcontroler library |
|
3 | 3 | -- Copyright (C) 2011, Alexis Jeandet |
|
4 | 4 | -- |
|
5 | 5 | -- This program is free software; you can redistribute it and/or modify |
|
6 | 6 | -- it under the terms of the GNU General Public License as published by |
|
7 | 7 | -- the Free Software Foundation; either version 3 of the License, or |
|
8 | 8 | -- (at your option) any later version. |
|
9 | 9 | -- |
|
10 | 10 | -- This program is distributed in the hope that it will be useful, |
|
11 | 11 | -- but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 | 12 | -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 | 13 | -- GNU General Public License for more details. |
|
14 | 14 | -- |
|
15 | 15 | -- You should have received a copy of the GNU General Public License |
|
16 | 16 | -- along with this program; if not, write to the Free Software |
|
17 | 17 | -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
18 | 18 | ------------------------------------------------------------------------------- |
|
19 | 19 | -- Author : Alexis Jeandet |
|
20 | 20 | -- Mail : alexis.jeandet@gmail.com |
|
21 | 21 | -------------------------------------------------------------------------------*/ |
|
22 | 22 | #ifndef INA226_H |
|
23 | 23 | #define INA226_H |
|
24 | 24 | |
|
25 | 25 | #include <i2c.h> |
|
26 | 26 | #include <uhandle.h> |
|
27 | 27 | |
|
28 | 28 | typedef struct INA226_t |
|
29 | 29 | { |
|
30 | 30 | i2c_t i2cdev; |
|
31 | 31 | uint8_t devAddress; |
|
32 | 32 | uint32_t shuntmOhm; |
|
33 | 33 | uint32_t CurrentRangeuAmp; |
|
34 | 34 | }INA226_t; |
|
35 | 35 | |
|
36 | 36 | extern int ina226open(INA226_t* dev,i2c_t i2cdev,uint8_t A0,uint8_t A1,uint32_t shuntmOhm, uint32_t CurrentRangeuAmp); |
|
37 |
extern uint |
|
|
37 | extern uint16_t ina226getID(INA226_t* dev); | |
|
38 | 38 | extern int ina226calibrate(INA226_t* dev,uint32_t shuntmOhm, uint32_t CurrentRangeuAmp); |
|
39 | 39 | extern uint32_t ina226getBusVoltage(INA226_t* dev); |
|
40 | 40 | extern uint32_t ina226getPower(INA226_t* dev); |
|
41 | extern uint32_t ina226getCurrent(INA226_t* dev); | |
|
42 | extern uint16_t ina226getReg(INA226_t* dev,char reg); | |
|
43 | extern int ina226setReg(INA226_t* dev,char reg,int16_t value); | |
|
41 | 44 | |
|
42 | 45 | |
|
43 | 46 | #define INA226_I2C_ADDRESS 0x40 |
|
44 | 47 | |
|
45 | 48 | |
|
46 | 49 | #define INA226_Die_ID_Reg 0xFF |
|
50 | #define INA226_Configuration_Reg 0 | |
|
47 | 51 | #define INA226_Shunt_Voltage_Reg 1 |
|
48 | 52 | #define INA226_Bus_Voltage_Reg 2 |
|
49 | 53 | #define INA226_Power_Reg 3 |
|
50 | 54 | #define INA226_Current_Reg 4 |
|
51 | 55 | #define INA226_Calibration_Reg 5 |
|
52 | 56 | #define INA226_Mask_Enable_Reg 6 |
|
53 | 57 | #define INA226_Alert_Limit_Reg 7 |
|
54 | 58 | |
|
55 | 59 | #endif |
|
56 | 60 | |
|
57 | 61 | |
|
58 | 62 | |
|
59 | 63 | |
|
60 | 64 | |
|
61 | 65 | |
|
62 | 66 |
@@ -1,92 +1,110 | |||
|
1 | 1 | /*------------------------------------------------------------------------------ |
|
2 | 2 | -- This file is a part of the libuc, microcontroler library |
|
3 | 3 | -- Copyright (C) 2012, Alexis Jeandet |
|
4 | 4 | -- |
|
5 | 5 | -- This program is free software; you can redistribute it and/or modify |
|
6 | 6 | -- it under the terms of the GNU General Public License as published by |
|
7 | 7 | -- the Free Software Foundation; either version 3 of the License, or |
|
8 | 8 | -- (at your option) any later version. |
|
9 | 9 | -- |
|
10 | 10 | -- This program is distributed in the hope that it will be useful, |
|
11 | 11 | -- but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 | 12 | -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 | 13 | -- GNU General Public License for more details. |
|
14 | 14 | -- |
|
15 | 15 | -- You should have received a copy of the GNU General Public License |
|
16 | 16 | -- along with this program; if not, write to the Free Software |
|
17 | 17 | -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
18 | 18 | ------------------------------------------------------------------------------- |
|
19 | 19 | -- Author : Alexis Jeandet |
|
20 | 20 | -- Mail : alexis.jeandet@gmail.com |
|
21 | 21 | -------------------------------------------------------------------------------*/ |
|
22 | 22 | #include <ina226.h> |
|
23 | 23 | #include <stdio.h> |
|
24 | 24 | #include <stddef.h> |
|
25 | 25 | |
|
26 | 26 | int ina226open(INA226_t *dev, i2c_t i2cdev, uint8_t A0, uint8_t A1, uint32_t shuntmOhm, uint32_t CurrentRangeuAmp) |
|
27 | 27 | { |
|
28 | 28 | if(dev != NULL) |
|
29 | 29 | { |
|
30 | 30 | dev->i2cdev=i2cdev; |
|
31 | 31 | dev->devAddress = INA226_I2C_ADDRESS | (A0 & 1) | ((A1<<1) & 2); |
|
32 | 32 | dev->shuntmOhm = shuntmOhm; |
|
33 | 33 | dev->CurrentRangeuAmp = CurrentRangeuAmp; |
|
34 | return 1; | |
|
34 | ina226setReg(dev,INA226_Configuration_Reg,0x8000); | |
|
35 | return ina226calibrate(dev,shuntmOhm,CurrentRangeuAmp); | |
|
35 | 36 | } |
|
36 | 37 | return -1; |
|
37 | 38 | } |
|
38 | 39 | |
|
39 |
uint |
|
|
40 | uint16_t ina226getID(INA226_t* dev) | |
|
40 | 41 | { |
|
41 | 42 | if(dev != NULL) |
|
42 | 43 | { |
|
43 | char DATA[]={INA226_Die_ID_Reg}; | |
|
44 | i2cwrite(dev->i2cdev,dev->devAddress,DATA,1); | |
|
45 | i2cread(dev->i2cdev,dev->devAddress,DATA,1); | |
|
46 | return DATA[0]; | |
|
44 | uint16_t id=ina226getReg(dev,INA226_Die_ID_Reg); | |
|
45 | return id; | |
|
47 | 46 | } |
|
48 | 47 | return -1; |
|
49 | 48 | } |
|
50 | 49 | |
|
51 | 50 | |
|
52 | 51 | int ina226calibrate(INA226_t* dev,uint32_t shuntmOhm, uint32_t CurrentRangeuAmp) |
|
53 | 52 | { |
|
54 | 53 | dev->shuntmOhm = shuntmOhm; |
|
55 | 54 | dev->CurrentRangeuAmp = CurrentRangeuAmp; |
|
56 | 55 | uint32_t CAL= (uint32_t)(5210000/(shuntmOhm*(CurrentRangeuAmp>>15))); |
|
57 | char CMD[3]; | |
|
58 | CMD[0]=INA226_Calibration_Reg; | |
|
59 | CMD[1]=(char)(0xff & (CAL>>8)); | |
|
60 | CMD[2]=(char)(0xff & CAL); | |
|
61 | i2cwrite(dev->i2cdev,dev->devAddress,CMD,3); | |
|
56 | printf("CAL = %x\n\r",CAL); | |
|
57 | return ina226setReg(dev,INA226_Calibration_Reg,(uint16_t)CAL); | |
|
62 | 58 | } |
|
63 | 59 | |
|
64 | 60 | uint32_t ina226getBusVoltage(INA226_t *dev) |
|
65 | 61 | { |
|
66 | char CMD[2]; | |
|
67 | CMD[0]=INA226_Bus_Voltage_Reg; | |
|
68 | i2cwrite(dev->i2cdev,dev->devAddress,CMD,1); | |
|
69 | i2cread(dev->i2cdev,dev->devAddress,CMD,2); | |
|
70 | uint32_t busVoltage= CMD[0]; | |
|
71 | busVoltage = (busVoltage<<8) + CMD[1]; | |
|
62 | uint32_t busVoltage= ina226getReg(dev,INA226_Bus_Voltage_Reg); | |
|
72 | 63 | busVoltage*= 1250; |
|
73 | 64 | return busVoltage; |
|
74 | 65 | } |
|
75 | 66 | |
|
76 | 67 | uint32_t ina226getPower(INA226_t *dev) |
|
77 | 68 | { |
|
78 | char CMD[2]; | |
|
79 | CMD[0]=INA226_Power_Reg; | |
|
80 | i2cwrite(dev->i2cdev,dev->devAddress,CMD,1); | |
|
81 | i2cread(dev->i2cdev,dev->devAddress,CMD,2); | |
|
82 | uint32_t power= CMD[0]; | |
|
83 | power = (power<<8) + CMD[1]; | |
|
84 | power*= ((CurrentRangeuAmp>>15)*25); | |
|
85 | return busVoltage; | |
|
69 | uint32_t power= ina226getReg(dev,INA226_Power_Reg); | |
|
70 | power*= ((dev->CurrentRangeuAmp>>15)*25); | |
|
71 | return power; | |
|
72 | } | |
|
73 | ||
|
74 | uint32_t ina226getCurrent(INA226_t *dev) | |
|
75 | { | |
|
76 | uint32_t current= ina226getReg(dev,INA226_Current_Reg); | |
|
77 | //current*= ((dev->CurrentRangeuAmp>>15)); | |
|
78 | return current; | |
|
79 | } | |
|
80 | ||
|
81 | uint16_t ina226getReg(INA226_t* dev,char reg) | |
|
82 | { | |
|
83 | if(dev != NULL) | |
|
84 | { | |
|
85 | char DATA[2]; | |
|
86 | DATA[0]=reg; | |
|
87 | i2cwrite(dev->i2cdev,dev->devAddress,DATA,1); | |
|
88 | i2cread(dev->i2cdev,dev->devAddress,DATA,2); | |
|
89 | uint16_t val=DATA[0]; | |
|
90 | val=(val<<8)+DATA[1]; | |
|
91 | return val; | |
|
92 | } | |
|
93 | return -1; | |
|
86 | 94 | } |
|
87 | 95 | |
|
88 | 96 | |
|
97 | int ina226setReg(INA226_t* dev,char reg,int16_t value) | |
|
98 | { | |
|
99 | if(dev != NULL) | |
|
100 | { | |
|
101 | char DATA[3]; | |
|
102 | DATA[0]=INA226_Calibration_Reg; | |
|
103 | DATA[1]=(char)(0xff & (value>>8)); | |
|
104 | DATA[2]=(char)(0xff & value); | |
|
105 | if(3==i2cwrite(dev->i2cdev,dev->devAddress,DATA,3))return 1; | |
|
106 | } | |
|
107 | return -1; | |
|
108 | } | |
|
89 | 109 | |
|
90 | 110 | |
|
91 | ||
|
92 |
General Comments 0
You need to be logged in to leave comments.
Login now