diff --git a/bsp/src/BEAGLESYNTH/bsp.c b/bsp/src/BEAGLESYNTH/bsp.c --- a/bsp/src/BEAGLESYNTH/bsp.c +++ b/bsp/src/BEAGLESYNTH/bsp.c @@ -26,6 +26,7 @@ #include #include #include +#include #include uint32_t OSC0 =8000000; uint32_t INTOSC =16000000; @@ -45,6 +46,7 @@ int bsp_init() bsp_GPIO_init(); bsp_uart_init(); bsp_iic_init(); + bsp_FSMC_init(); printf("\r================================================================\n\r"); printf("================================================================\n\r"); printf(BSP); @@ -111,6 +113,52 @@ void bsp_FSMC_init() gpiosetconfig(&LCD_DBx); GPIO_PinAFConfig(GPIOGETPORT(LCD_DBx), (uint8_t)(LCD_DBx & 0xF), GPIO_AF_FSMC); } + + FSMC_NORSRAMInitTypeDef FSMC_NORSRAMInitStructure; + FSMC_NORSRAMTimingInitTypeDef p; + + /* Enable FSMC clock */ + RCC_AHB3PeriphClockCmd(RCC_AHB3Periph_FSMC, ENABLE); + + /*-- FSMC Configuration ------------------------------------------------------*/ + /*----------------------- SRAM Bank 3 ----------------------------------------*/ + /* FSMC_Bank1_NORSRAM4 configuration */ + p.FSMC_AddressSetupTime = 5; + p.FSMC_AddressHoldTime = 0; + p.FSMC_DataSetupTime = 9; + p.FSMC_BusTurnAroundDuration = 0; + p.FSMC_CLKDivision = 0; + p.FSMC_DataLatency = 0; + p.FSMC_AccessMode = FSMC_AccessMode_A; + /* Color LCD configuration ------------------------------------ + LCD configured as follow: + - Data/Address MUX = Disable + - Memory Type = SRAM + - Data Width = 16bit + - Write Operation = Enable + - Extended Mode = Enable + - Asynchronous Wait = Disable */ + + FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM1; + FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable; + FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_SRAM; + FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b; + FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable; + FSMC_NORSRAMInitStructure.FSMC_AsynchronousWait = FSMC_AsynchronousWait_Disable; + FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low; + FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable; + FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState; + FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable; + FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable; + FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable; + FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable; + FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &p; + FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &p; + + FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure); + + /* Enable FSMC NOR/SRAM Bank3 */ + FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM1, ENABLE); } void bsp_spi_init() diff --git a/examples/BeagleSynthHello/main.c b/examples/BeagleSynthHello/main.c --- a/examples/BeagleSynthHello/main.c +++ b/examples/BeagleSynthHello/main.c @@ -13,24 +13,35 @@ extern streamdevice* __opnfiles__[]; int main() { - INA226_t Psens1; + INA226_t ina5VSens,ina33VSens,ina15VSens; gpioset(PSU_DISABLE); - ina226open(&Psens1,i2c2,0,0,15,300000); - uint16_t id=ina226getID(&Psens1); - printf("INA226 ID=%x\n\r",0x0ffff&id); - id=ina226getReg(&Psens1,INA226_Calibration_Reg); - printf("INA226 CAL=%x\n\r",0x0ffff&id); - unsigned int current; + volatile int32_t* regtest=(volatile int32_t*)0x60000000; + volatile int32_t* regtest2=(volatile int32_t*)(0x60000000+(1<<20)); + //ina226open(&ina5VSens,i2c2,INA226_MODE_SHUNT_VOLTAGE_CONTINUOUS|INA226_AVERAGES_16|INA226_BUS_CONV_8244us|INA226_SHUNT_CONV_8244us,0,0,15,1000000); + //ina226open(&ina15VSens,i2c2,INA226_MODE_SHUNT_VOLTAGE_CONTINUOUS|INA226_AVERAGES_16|INA226_BUS_CONV_8244us|INA226_SHUNT_CONV_8244us,1,0,15,1000000); + *regtest=0; + printf("LCD ID=%d\n\r",*regtest2); + 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)) + { + printf("Can't open 3.3V monitor\n\r"); + } + + int current5V,current33V,current15V; + printf("\t5V\t3.3V\n\r"); while(1) { - for(volatile int i=0;i<1024*2048;i++); + for(volatile int i=0;i<1024*1024;i++); gpioset(LED1); gpioclr(LED2); - for(volatile int i=0;i<1024*2048;i++); + for(volatile int i=0;i<1024*1024;i++); gpioclr(LED1); gpioset(LED2); - current = ina226getCurrent(&Psens1); - printf("Current = %uµA %umA\n\r",current,current/1000); + //current5V = ina226getCurrent(&ina5VSens); + current33V = ina226getCurrent(&ina33VSens); + //current15V = ina226getCurrent(&ina15VSens); + //current = ina226getReg(&Psens1,INA226_Current_Reg); + //printf("%duA %dmA\t%duA %dmA\n\r",current5V,current5V/1000,current33V,current33V/1000); + printf("%duA %dmA\n\r",current33V,current33V/1000); } printf("hello world\n\r"); return 0; diff --git a/lib/includes/i2c.h b/lib/includes/i2c.h --- a/lib/includes/i2c.h +++ b/lib/includes/i2c.h @@ -41,6 +41,8 @@ typedef int i2c_t; #define i2c8 7 #define i2c9 8 +extern int i2ctimeout; + extern i2c_t i2copen(int count); extern i2c_t i2copenandconfig(int count ,uint32_t cfg,uint32_t speed,uint32_t SDA,uint32_t SCL); extern int i2cclose(i2c_t dev); diff --git a/lib/includes/ina226.h b/lib/includes/ina226.h --- a/lib/includes/ina226.h +++ b/lib/includes/ina226.h @@ -33,12 +33,12 @@ typedef struct INA226_t uint32_t CurrentRangeuAmp; }INA226_t; -extern int ina226open(INA226_t* dev,i2c_t i2cdev,uint8_t A0,uint8_t A1,uint32_t shuntmOhm, uint32_t CurrentRangeuAmp); +extern int ina226open(INA226_t* dev,i2c_t i2cdev,uint16_t config,uint8_t A0,uint8_t A1,uint32_t shuntmOhm, uint32_t CurrentRangeuAmp); extern uint16_t ina226getID(INA226_t* dev); extern int ina226calibrate(INA226_t* dev,uint32_t shuntmOhm, uint32_t CurrentRangeuAmp); extern uint32_t ina226getBusVoltage(INA226_t* dev); extern uint32_t ina226getPower(INA226_t* dev); -extern uint32_t ina226getCurrent(INA226_t* dev); +extern int32_t ina226getCurrent(INA226_t* dev); extern uint16_t ina226getReg(INA226_t* dev,char reg); extern int ina226setReg(INA226_t* dev,char reg,int16_t value); @@ -56,6 +56,44 @@ extern int ina226setReg(INA226_t* dev,ch #define INA226_Mask_Enable_Reg 6 #define INA226_Alert_Limit_Reg 7 +#define INA226_AVERAGES_1 0 +#define INA226_AVERAGES_4 (1<<9) +#define INA226_AVERAGES_16 (2<<9) +#define INA226_AVERAGES_64 (3<<9) +#define INA226_AVERAGES_128 (4<<9) +#define INA226_AVERAGES_256 (5<<9) +#define INA226_AVERAGES_512 (6<<9) +#define INA226_AVERAGES_1024 (7<<9) + +#define INA226_BUS_CONV_140us 0 +#define INA226_BUS_CONV_204us (1<<6) +#define INA226_BUS_CONV_332us (2<<6) +#define INA226_BUS_CONV_588us (3<<6) +#define INA226_BUS_CONV_1100us (4<<6) +#define INA226_BUS_CONV_2116us (5<<6) +#define INA226_BUS_CONV_4156us (6<<6) +#define INA226_BUS_CONV_8244us (7<<6) + +#define INA226_SHUNT_CONV_140us 0 +#define INA226_SHUNT_CONV_204us (1<<3) +#define INA226_SHUNT_CONV_332us (2<<3) +#define INA226_SHUNT_CONV_588us (3<<3) +#define INA226_SHUNT_CONV_1100us (4<<3) +#define INA226_SHUNT_CONV_2116us (5<<3) +#define INA226_SHUNT_CONV_4156us (6<<3) +#define INA226_SHUNT_CONV_8244us (7<<3) + +#define INA226_MODE_POWER_DOWN 0 +#define INA226_MODE_SHUNT_VOLTAGE_TRIGGERRED 1 +#define INA226_MODE_BUS_VOLTAGE_TRIGGERRED 2 +#define INA226_MODE_SHUNT_AND_BUS_TRIGGERRED 3 +#define INA226_MODE_SHUNT_VOLTAGE_CONTINUOUS 5 +#define INA226_MODE_BUS_VOLTAGE_CONTINUOUS 6 +#define INA226_MODE_SHUNT_AND_BUS_CONTINUOUS 7 + + + + #endif diff --git a/lib/src/common/POWER/ina226/ina226.c b/lib/src/common/POWER/ina226/ina226.c --- a/lib/src/common/POWER/ina226/ina226.c +++ b/lib/src/common/POWER/ina226/ina226.c @@ -23,15 +23,18 @@ #include #include -int ina226open(INA226_t *dev, i2c_t i2cdev, uint8_t A0, uint8_t A1, uint32_t shuntmOhm, uint32_t CurrentRangeuAmp) + +int ina226open(INA226_t *dev, i2c_t i2cdev, uint16_t config, uint8_t A0, uint8_t A1, uint32_t shuntmOhm, uint32_t CurrentRangeuAmp) { if(dev != NULL) { dev->i2cdev=i2cdev; dev->devAddress = INA226_I2C_ADDRESS | (A0 & 1) | ((A1<<1) & 2); + printf("dev->devAddress = %x\n\r",dev->devAddress); dev->shuntmOhm = shuntmOhm; dev->CurrentRangeuAmp = CurrentRangeuAmp; ina226setReg(dev,INA226_Configuration_Reg,0x8000); + ina226setReg(dev,INA226_Configuration_Reg,config); return ina226calibrate(dev,shuntmOhm,CurrentRangeuAmp); } return -1; @@ -53,7 +56,6 @@ int ina226calibrate(INA226_t* dev,uint32 dev->shuntmOhm = shuntmOhm; dev->CurrentRangeuAmp = CurrentRangeuAmp; uint32_t CAL= (uint32_t)(5210000/(shuntmOhm*(CurrentRangeuAmp>>15))); - printf("CAL = %x\n\r",CAL); return ina226setReg(dev,INA226_Calibration_Reg,(uint16_t)CAL); } @@ -71,10 +73,12 @@ uint32_t ina226getPower(INA226_t *dev) return power; } -uint32_t ina226getCurrent(INA226_t *dev) +int32_t ina226getCurrent(INA226_t *dev) { - uint32_t current= ina226getReg(dev,INA226_Current_Reg); - //current*= ((dev->CurrentRangeuAmp>>15)); + int32_t current= ina226getReg(dev,INA226_Current_Reg); + current<<=16; + current>>=16; + current = current*(dev->CurrentRangeuAmp>>15); return current; } diff --git a/lib/src/stm32f4/I2C/i2c.c b/lib/src/stm32f4/I2C/i2c.c --- a/lib/src/stm32f4/I2C/i2c.c +++ b/lib/src/stm32f4/I2C/i2c.c @@ -30,6 +30,7 @@ #define GPIOGETPORT(gpio) ((GPIO_TypeDef*)(((((uint32_t)gpio) & (uint32_t)0x0000FF00)*(uint32_t)4) + (uint32_t)GPIOA)) #define GPIOPORTNUM(gpio) (((uint32_t)(gpio) & (uint32_t)0x0000FF00)>>(uint32_t)8) +int i2ctimeout=1000*1000; I2C_TypeDef* _i2c_dev_table[3]={I2C1,I2C2,I2C3}; @@ -223,20 +224,44 @@ int i2cwrite(i2c_t dev,char address,char { if((dev<3)&&(dev>=0)) { + int timeout=i2ctimeout; I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev]; - while(i2cbusy(dev)); + while(i2cbusy(dev)) + { + if(0==(timeout--))return -1; + } _dev_->CR1 |= 1<<8; - while(!i2cStatusCheck(dev,((uint32_t)0x00030001))); + timeout=i2ctimeout; + while(!i2cStatusCheck(dev,((uint32_t)0x00030001))) + { + if(0==(timeout--))return -1; + } _dev_->DR= address<<1; - while(!i2cStatusCheck(dev, ((uint32_t)0x00070082))); + timeout=i2ctimeout; + while(!i2cStatusCheck(dev, ((uint32_t)0x00070082))) + { + if(0==(timeout--))return -1; + } address=_dev_->SR2; for(int i=0;iDR= data[i]; } - while(!i2cStatusCheck(dev,1<<7)); - while(!i2cStatusCheck(dev,1<<2)); + timeout=i2ctimeout; + while(!i2cStatusCheck(dev,1<<7)) + { + if(0==(timeout--))return -1; + } + timeout=i2ctimeout; + while(!i2cStatusCheck(dev,1<<2)) + { + if(0==(timeout--))return -1; + } _dev_->CR1 |= 1<<9; return count; } @@ -248,12 +273,23 @@ int i2cread(i2c_t dev,char address,char* if((dev<3)&&(dev>=0)) { int i=0; - while(i2cbusy(dev)); + int timeout=i2ctimeout; + while(i2cbusy(dev)) + { + if(0==(timeout--))return -1; + } I2C_TypeDef* _dev_ = _i2c_dev_table[(int)dev]; _dev_->CR1 |= (1<<8) + (1<<10); - while(!i2cStatusCheck(dev,0x00030001)); + timeout=i2ctimeout; + while(!i2cStatusCheck(dev,0x00030001)) + { + if(0==(timeout--))return -1; + } _dev_->DR= (address<<1) + 1; - while(!i2cStatusCheck(dev,0x000002)); + while(!i2cStatusCheck(dev,0x000002)) + { + if(0==(timeout--))return -1; + } if(count==1) { _dev_->CR1 &= ~(1<<10); @@ -261,14 +297,26 @@ int i2cread(i2c_t dev,char address,char* address=_dev_->SR2; for(i=0;i<(count-1);i++) { - while(!i2cStatusCheck(dev,0x0000040)); + timeout=i2ctimeout; + while(!i2cStatusCheck(dev,0x0000040)) + { + if(0==(timeout--))return -1; + } data[i]=_dev_->DR; } _dev_->CR1 &= ~(1<<10); _dev_->CR1 |= 1<<9; - while(!i2cStatusCheck(dev,0x0000040)); + timeout=i2ctimeout; + while(!i2cStatusCheck(dev,0x0000040)) + { + if(0==(timeout--))return -1; + } data[i]=_dev_->DR; - while(_dev_->CR1 & ((uint16_t)0x0200)); + timeout=i2ctimeout; + while(_dev_->CR1 & ((uint16_t)0x0200)) + { + if(0==(timeout--))return -1; + } _dev_->CR1 |= 1<<10; return count; }