@@ -26,6 +26,7 | |||
|
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; |
@@ -43,6 +44,7 int bsp_init() | |||
|
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); |
@@ -119,7 +121,7 void bsp_spi_init() | |||
|
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() |
@@ -97,10 +97,8 void bsp_spi_init() | |||
|
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() |
@@ -2,8 +2,8 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 |
@@ -6,16 +6,21 | |||
|
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++); |
@@ -24,7 +29,8 int main() | |||
|
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; |
@@ -34,16 +34,20 typedef struct INA226_t | |||
|
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 |
@@ -31,19 +31,18 int ina226open(INA226_t *dev, i2c_t i2cd | |||
|
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 | } |
@@ -54,39 +53,58 int ina226calibrate(INA226_t* dev,uint32 | |||
|
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