@@ -26,6 +26,7 | |||
|
26 | 26 | #include <uart.h> |
|
27 | 27 | #include <stdio.h> |
|
28 | 28 | #include <stm32f4xx_gpio.h> |
|
29 | #include <stm32f4xx_fsmc.h> | |
|
29 | 30 | #include <i2c.h> |
|
30 | 31 | uint32_t OSC0 =8000000; |
|
31 | 32 | uint32_t INTOSC =16000000; |
@@ -45,6 +46,7 int bsp_init() | |||
|
45 | 46 | bsp_GPIO_init(); |
|
46 | 47 | bsp_uart_init(); |
|
47 | 48 | bsp_iic_init(); |
|
49 | bsp_FSMC_init(); | |
|
48 | 50 | printf("\r================================================================\n\r"); |
|
49 | 51 | printf("================================================================\n\r"); |
|
50 | 52 | printf(BSP); |
@@ -111,6 +113,52 void bsp_FSMC_init() | |||
|
111 | 113 | gpiosetconfig(&LCD_DBx); |
|
112 | 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 | 164 | void bsp_spi_init() |
@@ -13,24 +13,35 extern streamdevice* __opnfiles__[]; | |||
|
13 | 13 | |
|
14 | 14 | int main() |
|
15 | 15 | { |
|
16 | INA226_t Psens1; | |
|
16 | INA226_t ina5VSens,ina33VSens,ina15VSens; | |
|
17 | 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; | |
|
18 | volatile int32_t* regtest=(volatile int32_t*)0x60000000; | |
|
19 | volatile int32_t* regtest2=(volatile int32_t*)(0x60000000+(1<<20)); | |
|
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 | //ina226open(&ina15VSens,i2c2,INA226_MODE_SHUNT_VOLTAGE_CONTINUOUS|INA226_AVERAGES_16|INA226_BUS_CONV_8244us|INA226_SHUNT_CONV_8244us,1,0,15,1000000); | |
|
22 | *regtest=0; | |
|
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 | 31 | while(1) |
|
25 | 32 | { |
|
26 |
for(volatile int i=0;i<1024* |
|
|
33 | for(volatile int i=0;i<1024*1024;i++); | |
|
27 | 34 | gpioset(LED1); |
|
28 | 35 | gpioclr(LED2); |
|
29 |
for(volatile int i=0;i<1024* |
|
|
36 | for(volatile int i=0;i<1024*1024;i++); | |
|
30 | 37 | gpioclr(LED1); |
|
31 | 38 | gpioset(LED2); |
|
32 |
current = ina226getCurrent(& |
|
|
33 | printf("Current = %uοΏ½A %umA\n\r",current,current/1000); | |
|
39 | //current5V = ina226getCurrent(&ina5VSens); | |
|
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 | 46 | printf("hello world\n\r"); |
|
36 | 47 | return 0; |
@@ -41,6 +41,8 typedef int i2c_t; | |||
|
41 | 41 | #define i2c8 7 |
|
42 | 42 | #define i2c9 8 |
|
43 | 43 | |
|
44 | extern int i2ctimeout; | |
|
45 | ||
|
44 | 46 | extern i2c_t i2copen(int count); |
|
45 | 47 | extern i2c_t i2copenandconfig(int count ,uint32_t cfg,uint32_t speed,uint32_t SDA,uint32_t SCL); |
|
46 | 48 | extern int i2cclose(i2c_t dev); |
@@ -33,12 +33,12 typedef struct INA226_t | |||
|
33 | 33 | uint32_t CurrentRangeuAmp; |
|
34 | 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 | 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 |
|
|
41 | extern int32_t ina226getCurrent(INA226_t* dev); | |
|
42 | 42 | extern uint16_t ina226getReg(INA226_t* dev,char reg); |
|
43 | 43 | extern int ina226setReg(INA226_t* dev,char reg,int16_t value); |
|
44 | 44 | |
@@ -56,6 +56,44 extern int ina226setReg(INA226_t* dev,ch | |||
|
56 | 56 | #define INA226_Mask_Enable_Reg 6 |
|
57 | 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 | 97 | #endif |
|
60 | 98 | |
|
61 | 99 |
@@ -23,15 +23,18 | |||
|
23 | 23 | #include <stdio.h> |
|
24 | 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 | 29 | if(dev != NULL) |
|
29 | 30 | { |
|
30 | 31 | dev->i2cdev=i2cdev; |
|
31 | 32 | dev->devAddress = INA226_I2C_ADDRESS | (A0 & 1) | ((A1<<1) & 2); |
|
33 | printf("dev->devAddress = %x\n\r",dev->devAddress); | |
|
32 | 34 | dev->shuntmOhm = shuntmOhm; |
|
33 | 35 | dev->CurrentRangeuAmp = CurrentRangeuAmp; |
|
34 | 36 | ina226setReg(dev,INA226_Configuration_Reg,0x8000); |
|
37 | ina226setReg(dev,INA226_Configuration_Reg,config); | |
|
35 | 38 | return ina226calibrate(dev,shuntmOhm,CurrentRangeuAmp); |
|
36 | 39 | } |
|
37 | 40 | return -1; |
@@ -53,7 +56,6 int ina226calibrate(INA226_t* dev,uint32 | |||
|
53 | 56 | dev->shuntmOhm = shuntmOhm; |
|
54 | 57 | dev->CurrentRangeuAmp = CurrentRangeuAmp; |
|
55 | 58 | uint32_t CAL= (uint32_t)(5210000/(shuntmOhm*(CurrentRangeuAmp>>15))); |
|
56 | printf("CAL = %x\n\r",CAL); | |
|
57 | 59 | return ina226setReg(dev,INA226_Calibration_Reg,(uint16_t)CAL); |
|
58 | 60 | } |
|
59 | 61 | |
@@ -71,10 +73,12 uint32_t ina226getPower(INA226_t *dev) | |||
|
71 | 73 | return power; |
|
72 | 74 | } |
|
73 | 75 | |
|
74 |
|
|
|
76 | int32_t ina226getCurrent(INA226_t *dev) | |
|
75 | 77 | { |
|
76 |
|
|
|
77 | //current*= ((dev->CurrentRangeuAmp>>15)); | |
|
78 | int32_t current= ina226getReg(dev,INA226_Current_Reg); | |
|
79 | current<<=16; | |
|
80 | current>>=16; | |
|
81 | current = current*(dev->CurrentRangeuAmp>>15); | |
|
78 | 82 | return current; |
|
79 | 83 | } |
|
80 | 84 |
@@ -30,6 +30,7 | |||
|
30 | 30 | #define GPIOGETPORT(gpio) ((GPIO_TypeDef*)(((((uint32_t)gpio) & (uint32_t)0x0000FF00)*(uint32_t)4) + (uint32_t)GPIOA)) |
|
31 | 31 | #define GPIOPORTNUM(gpio) (((uint32_t)(gpio) & (uint32_t)0x0000FF00)>>(uint32_t)8) |
|
32 | 32 | |
|
33 | int i2ctimeout=1000*1000; | |
|
33 | 34 | |
|
34 | 35 | I2C_TypeDef* _i2c_dev_table[3]={I2C1,I2C2,I2C3}; |
|
35 | 36 | |
@@ -223,20 +224,44 int i2cwrite(i2c_t dev,char address,char | |||
|
223 | 224 | { |
|
224 | 225 | if((dev<3)&&(dev>=0)) |
|
225 | 226 | { |
|
227 | int timeout=i2ctimeout; | |
|
226 | 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 | 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 | 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 | 245 | address=_dev_->SR2; |
|
233 | 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 | 253 | _dev_->DR= data[i]; |
|
237 | 254 | } |
|
238 | while(!i2cStatusCheck(dev,1<<7)); | |
|
239 |
while(!i2cStatusCheck(dev,1<< |
|
|
255 | timeout=i2ctimeout; | |
|
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 | 265 | _dev_->CR1 |= 1<<9; |
|
241 | 266 | return count; |
|
242 | 267 | } |
@@ -248,12 +273,23 int i2cread(i2c_t dev,char address,char* | |||
|
248 | 273 | if((dev<3)&&(dev>=0)) |
|
249 | 274 | { |
|
250 | 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 | 281 | I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev]; |
|
253 | 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 | 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 | 293 | if(count==1) |
|
258 | 294 | { |
|
259 | 295 | _dev_->CR1 &= ~(1<<10); |
@@ -261,14 +297,26 int i2cread(i2c_t dev,char address,char* | |||
|
261 | 297 | address=_dev_->SR2; |
|
262 | 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 | 305 | data[i]=_dev_->DR; |
|
266 | 306 | } |
|
267 | 307 | _dev_->CR1 &= ~(1<<10); |
|
268 | 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 | 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 | 320 | _dev_->CR1 |= 1<<10; |
|
273 | 321 | return count; |
|
274 | 322 | } |
General Comments 0
You need to be logged in to leave comments.
Login now