Ejemplo n.º 1
0
int   fread2(MYFILE *fp, void *buffer, int nBytes)
{


    int startsector;
    int endsector;
    uint8_t realsector;
    int readbytes;
    int pagenum, blockoffset, pageoffset;

    if (fp->fpos + nBytes > fp->size)
        return -1;

    startsector = fp->fpos/2048;
    endsector = (fp->fpos+nBytes-1)/2048;

    if (startsector == endsector)
    {
        blockoffset = fp->fpos%2048;
        pageoffset = blockoffset%256;

        pagenum = blockoffset/256;
        realsector = read8uint(fp->addr, FILE_ADDRPAGEOFFSET+startsector);
        pagenum = pagenum+(realsector-1)*8;

        //now pagenum, offset2 means the actual start location just read it

        readFlash(pagenum, pageoffset, buffer, nBytes);
    }
    else
    {
        blockoffset = fp->fpos%2048;
        pageoffset = blockoffset%256;

        pagenum = blockoffset/256;
        realsector = read8uint(fp->addr, FILE_ADDRPAGEOFFSET+startsector);
        pagenum = pagenum+(realsector-1)*8;

        readbytes = 256 - pageoffset;

        //now pagenum, offset2 means the actual start location just read it
        readFlash(pagenum, pageoffset, buffer, readbytes);

        buffer = (void *)((char *)buffer + readbytes);

        blockoffset = (fp->fpos+nBytes-1)%2048;
        pageoffset = blockoffset%256;
        pagenum = blockoffset/256;

        realsector = read8uint(fp->addr, FILE_ADDRPAGEOFFSET+endsector);
        pagenum = pagenum+(realsector-1)*8;

        readbytes = nBytes - readbytes;
        //now pagenum, offset2 means the actual start location just read it
        readFlash(pagenum, 0, buffer, readbytes);


    }
    return 0;
}
Ejemplo n.º 2
0
void bq34z100::chg48Table(uint16_t designCap, uint16_t designEnergy, uint16_t CellVT1T2, uint16_t CellVT2T3, uint16_t CellVT3T4, uint16_t CellVT4T5)
{
    readFlash(48, 24);
    chgFlashPair(21, designCap);
    chgFlashPair(23, designEnergy);
    chgFlashPair(28, CellVT1T2);//0x0E10 = 3600
    chgFlashPair(30, CellVT2T3);//0x0E10 = 3600
    checkSum(48, 24);
    delay(300);
    readFlash(48, 35);
    chgFlashPair(32, CellVT3T4);//0x0E10 = 3600
    chgFlashPair(34, CellVT4T5);//0x0E10 = 3600
    checkSum(48, 35);
}
Ejemplo n.º 3
0
void bq34z100::chg82Table(uint16_t QMaxCell, uint16_t CellVAtMaxChargeTerm)
{
    readFlash(82, 5);
    chgFlashPair(0, QMaxCell);//0x4B00
    chgFlashPair(5, CellVAtMaxChargeTerm);//0x0E10
    checkSum(82, 5);
}
Ejemplo n.º 4
0
uint16_t bq34z100::readVDivider()
{
readFlash(0x68, 15);
    uint16_t val = (((uint16_t)flashbytes[14]) <<8) | flashbytes[15];
    return val;

}
Ejemplo n.º 5
0
void writeFlash(u8 address_num,u32 data) //address_num should start from 0 up to 255
 {

const u8 data_count=255;
	 
	 u32 flash_data[data_count];
	 
	 for (u8 i = 0; i < data_count; ++i) {
			flash_data[i] = readFlash(i);
		}
		
		flash_data[address_num] = data;
	 
	 
 FLASH_Unlock();// you need to unlcok flash first
 /* Clear All pending flags */
 FLASH_ClearFlag( FLASH_FLAG_EOP |  FLASH_FLAG_WRPERR |  FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);

//you need to erase entire sector before write anything
 FLASH_EraseSector(FLASH_Sector_11, VoltageRange_3);
 //VoltageRange_3        ((uint8_t)0x02)  /*!<Device operating range: 2.7V to 3.6V */

		for (u8 i = 0; i < data_count; ++i) {   //write back the whole sector memory
	
			FLASH_ProgramWord((u32)(startAddress + i * 4), flash_data[i]);
		}

 FLASH_Lock();//lock flash at the end

}
Ejemplo n.º 6
0
void bq34z100::CalibrateCurrentShunt(int16_t current)
{
    if(current>-200 && current<200)
        return;//too small to use to calibrate
    //current is in milliamps
    if(current<0)
        current=-current;
    int16_t currentReading = getCurrent();
    if(currentReading<0)
        currentReading = -currentReading;
    if(currentReading==0)
        currentReading=20;
    Serial.println(currentReading);
    readFlash(0x68, 15);
    delay(30);
  
    uint32_t curentGain = ((uint32_t)flashbytes[0])<<24 | ((uint32_t)flashbytes[1])<<16|((uint32_t)flashbytes[2])<<8|(uint32_t)flashbytes[3];
    Serial.println(curentGain,DEC);
    float currentGainResistance = (4.768/XemicsTofloat(curentGain));
    Serial.println(currentGainResistance);
    float newGain = (((float)currentReading)/((float)current)) * currentGainResistance;
    Serial.println(newGain);
    //we now have the new resistance calculated
Serial.println("--");
    chg104Table(0,newGain,newGain);
    //chg104Table(0,5,5);

    delay(30);
}
Ejemplo n.º 7
0
float bq34z100::readCurrentShunt()
{
    readFlash(0x68, 15);
    delay(30);
    uint32_t curentGain = ((uint32_t)flashbytes[0])<<24 | ((uint32_t)flashbytes[1])<<16|((uint32_t)flashbytes[2])<<8|(uint32_t)flashbytes[3];
    return (float) (4.768/XemicsTofloat(curentGain));
    
}
Ejemplo n.º 8
0
void bq34z100::chg64Table(uint16_t packConfigReg, uint16_t alertConfigReg, uint8_t ledChgReg, uint8_t CellsS)
{
    readFlash(64, 6);
    chgFlashPair(0, packConfigReg);
    chgFlash(4, ledChgReg);
    chgFlashPair(5, alertConfigReg);
    chgFlash(7, CellsS); //cell count
    checkSum(64, 6);
}
Ejemplo n.º 9
0
void wcycle_init ()
{
	initPins ();
	initClocks ();
	initFlash ();
	initUART ();
	initPWM ();
	initDHT ();

	wcycle_pwm_ctl (readFlash());
}
Ejemplo n.º 10
0
QString SystemConfig::serialNumber()
{
    QString number;
    const DeviceConfig * config = readFlash();
    if (config == 0)
    {
        return number;
    }
    number = QString::fromAscii(config->serial_no);
    return number;
}
Ejemplo n.º 11
0
QString SystemConfig::deviceId()
{
    QString id;
    const DeviceConfig * config = readFlash();
    if (config == 0)
    {
        return id;
    }
    id = QString::fromAscii(config->dev_id);
    return id;
}
Ejemplo n.º 12
0
void bq34z100::chg49Table(uint16_t UVLo, uint16_t UVLoR, uint16_t OVLo, uint16_t OVLoR)
{
    readFlash(49, 17);
    chgFlashPair(9 , UVLo);//2500

    chgFlashPair(12, UVLoR);//2900

    chgFlashPair(14, OVLo); //3900

    chgFlashPair(17, OVLoR); //3700

    checkSum(49, 17);
}
Ejemplo n.º 13
0
/*********************************************************************************************************
** Function name:       read_data
** Descriptions:        读取信息
** input parameters:    
** output parameters:   无
** Returned value:      无
*********************************************************************************************************/
static unsigned char read_data(void *ptr,unsigned short len,unsigned short addr)
{
	unsigned short crc;
	unsigned char buf[256] = {0};
	if(!ptr || !len || (len > 240))
			return 0;
	
	readFlash(addr,buf,256);	
	crc = CrcCheck(buf,len);
	if(crc == INTEG16(buf[len],buf[len + 1]))
	{
		memcpy(ptr,buf,len);
		return 1;
	}	
	return 0;
}
Ejemplo n.º 14
0
string Camera::getSerialNumber()
{
    string result = "";
    unsigned char serialNumber[10];
#ifdef _WIN32
	eSPAEAWB_ReadFlash(serialNumber, 10);
#elif __APPLE__
    readFlash(serialNumber, 10);
#endif
	for (int i = 0; i < 10; i++) {
		int digit = serialNumber[i];
		result += to_string(digit);
	}
	return result;

}
Ejemplo n.º 15
0
/*********************************************************************************************************
** Function name:       readDetailTradeInfo
** Descriptions:        读取交易记录
** input parameters:    无
** output parameters:   无
** Returned value:      无
*********************************************************************************************************/
unsigned char  readDetailTradeInfo(DETAIL_TRADE_INFO *stDetail,unsigned short pageNo)
{
	unsigned char buf[ADD_TRADE_PAGE_LEN] = {0};
	unsigned short flashPage,crc,len;	
	if(!stDetail || (pageNo > TRADE_MAX_PAGE))
		return 0;
	flashPage = ADD_TRADE_INFO + pageNo * ADD_TRADE_PAGE_LEN;
	len = sizeof(DETAIL_TRADE_INFO);
	readFlash(flashPage, buf,ADD_TRADE_PAGE_LEN);
	crc = CrcCheck(buf,len);
	if(crc == INTEG16(buf[len],buf[len + 1]))
	{
		memcpy((void *)stDetail,buf,len);
		return 1;
	}
	return 0;

}
Ejemplo n.º 16
0
unsigned int verifyFlash(uint32_t handle, uint32_t *buffer,
		uint32_t bufferLength, uint32_t startWordAddress) {
	int finish, i;
	unsigned int verify_err;
	int bp, wc, pp;
	uint32_t bpi_rd[32]; // 16-bit word read from flash

	wc = 0; // byte counter


	bp = 0; // byte pointer in the page
	finish = 0; // it goes high when eof is found
	verify_err = 0; // number of errors during verify

	pp = 0;

	while (wc < bufferLength) {

#ifdef PRINT_POINTS
		if (((wc % (32 * 1024)) == 0) || finish) {
			printf(".");
			fflush(stdout);
		}
#endif                             
		readFlash(handle, bpi_rd, (pp * 32) + startWordAddress, 32); // read page

		for (i = 0; i < 32; i++) { // verify page
			if (bpi_rd[i] != buffer[wc + i]) {
				printf("page = %d; bpi_rd[%d]=%04X, buffer[%d]=%04X  \n", pp,
						i, bpi_rd[i], wc + i, buffer[wc + i]);
				printf("\n\n Writing or Verify error at page %d - %d\n", pp, i);
				++verify_err;
			}
		}
		bp = 0;
		pp++;
		wc += 32;
	} // end of while loop


	return verify_err;

}
Ejemplo n.º 17
0
QString SystemConfig::version()
{
    QString version("1.0.20090922");
    const DeviceConfig * config = readFlash();
    if (config == 0)
    {
        return version;
    }
    QString result = QString::fromAscii(config->version);
    if (result.isEmpty())
    {
        return version;
    }
    if (result.size() != version.size() ||
        !result.at(0).isDigit())
    {
        return version;
    }
    return result;
}
Ejemplo n.º 18
0
//If vDiv is <500 dont change vdiv
//if CCG && CCD ==0 then dont change them
void bq34z100::chg104Table(uint16_t Vdivider,float CCGain,float CCDelta)
{
    if(Vdivider>32768)
        return;
    
        
    readFlash(0x68, 15);
    delay(30);
    if(Vdivider>500)
    chgFlashPair(14, Vdivider);
    if(!(CCGain==0 && CCDelta==0))
    {
        float GainDF = 4.768/CCGain;
        float DeltaDF = 5677445/CCDelta;
        chgFlashQuad(0,floatToXemics(GainDF));
        chgFlashQuad(4,floatToXemics(DeltaDF));
    }
    checkSum(0x68, 15);

}
Ejemplo n.º 19
0
/*********************************************************************************************************
** Function name:       readSystemParaFromFlash
** Descriptions:        从flash中读取系统参数 4个字节读取
** input parameters:    
** output parameters:   无
** Returned value:      无
*********************************************************************************************************/
unsigned char readSystemParaFromFlash()
{
	unsigned char buf[256] = {0};
	unsigned short len = 0,crc;
	len = (8 + 8 + HOPPER_NUMS) * 4 + sizeof(SYSPara);
	readFlash(ADD_SYSTEM_PARA, buf,len + 2);
	crc = CrcCheck(buf,len);
	if(crc == INTEG16(buf[len],buf[len + 1]))
	{
		len = 0;
		memcpy(stDevValue.BillValue,&buf[len],8 * 4);//纸币器通道面值
		len += 8 * 4;	
		memcpy(stDevValue.CoinValue,&buf[len],8 * 4);//硬币器通道面值
		len += 8 * 4;
		memcpy(stDevValue.HpValue,&buf[len],HOPPER_NUMS * 4);//hopper通道面值
		len += HOPPER_NUMS * 4;
		memcpy((unsigned char *)&SYSPara,&buf[len],sizeof(SYSPara));
		len += sizeof(SYSPara);
		return 1;
	}	
	return 0;

}
Ejemplo n.º 20
0
u16 read_dfu_data(StandardRequest *request, u8 *buffer, u16 max_length) {
	u16 length = 0;
	debug("read dfu\n");
	if (request->bRequest == DFU_GETSTATUS) {
		length = 6;
		buffer[0] = dfu_status.bStatus;
		buffer[1] = dfu_status.bwPollTimeout0;
		buffer[2] = dfu_status.bwPollTimeout1;
		buffer[3] = dfu_status.bwPollTimeout2;
		buffer[4] = dfu_status.bState;
		buffer[5] = dfu_status.iString;
	} else if (request->bRequest == DFU_GETSTATE) {
		length = 1;
		buffer[0] = dfu_status.bState;
	} else if (dfuSubCommand == DFU_CMD_GET_CMD) {
		length = 3;
		buffer[0] = GET_COMMAND_TOKEN;
		buffer[1] = SET_ADDRESS_TOKEN;
		buffer[2] = ERASE_PAGE_TOKEN;
	} else if (dfuSubCommand == DFU_CMD_UPLOAD) {
		u32 read_address;
		debug("upload\n");
		if (address < ENTRY) {
			address = ENTRY;
		}
		read_address = (request->wValue - 2) * DATA_BUFFER_SIZE + address;
		debug2("address: %lx\n", read_address);
		if (read_address >= FLASH_END) {
			length = 0;
			dfu_status.bState = dfuIDLE;
		} else {
			int counter;
			int readed = 0;
			length = DATA_BUFFER_SIZE;
			if (length > max_length) {
				length = max_length;
			}
			if (length > request->wLength) {
				length = request->wLength;
			}
			if ((FLASH_END - read_address + 1) < length) {
				length = FLASH_END - read_address + 1;
			}

			counter = length;
			while (counter > 0) {
				if (counter >= FLASH_WRITE_SIZE) {
					readFlash(read_address + readed, (u8 __data *)&buffer[readed], FLASH_WRITE_SIZE);
				} else {
					readFlash(read_address + readed, (u8 __data *)&buffer[readed], counter);
				}
				counter = counter - FLASH_WRITE_SIZE;
				readed = readed + FLASH_WRITE_SIZE;
			}

			debug2("1: %x\n", buffer[0]);

		}
	}
	return length;
}
Ejemplo n.º 21
0
int main(int argc, char **argv) {
	setLogLevel(LOGLEVEL_INFO);
	
	const char *device = DEFAULT_BASEBAND_DEVICE;
	unsigned int rate = 921600;
	char mode = '\0';
	char *secpack = NULL;
	char *binfile = NULL;
	unsigned int start = 0;
	unsigned int length = 0;
	
	int ch;
	while ((ch = getopt(argc, argv, "D:B:b:n:s:r:xw:vh")) != -1) {
		switch (ch) {
		case 'v':
			setLogLevel(LOGLEVEL_TRACE);
			break;
		case 'x':
			mode = 'x';
			break;
		case 'h':
			usage();
			return 0;
		case 'D':
			device = optarg;
			break;
		case 'B':
			rate = (unsigned int) strtoul(optarg, NULL, 10);
			break;
		case 'b':
			start = (unsigned int) strtoul(optarg, NULL, 0);
			if (start < 0xa0000000 || start >= 0xb0000000) {
				start = (start & ~0xf0000000) | 0xa0000000;
				fprintf(stderr, "Start address is not in flash range, corrected to 0x%08x.\n", start);
			}
			break;
		case 'n':
			length = (unsigned int) strtoul(optarg, NULL, 0);
			break;
		case 's':
			secpack = optarg;
			break;
		case 'r':
			mode = 'r';
			binfile = optarg;
			break;
		case 'w':
			mode = 'w';
			binfile = optarg;
			break;
		default:
			usage();
			return 1;
		}
	}
	
	resetBaseband();
	int fd = openBaseband(device);
	if (fd == -1) {
		fprintf(stderr, "Can't open %s: %s", device, strerror(errno));
		return 2;
	}
	setBaudRate(fd, rate);
	
	VersionPacket version = getBootVersion(fd);
	fprintf(stderr, "Got %d.%d, %s\n", version.major, version.minor, version.major <= 3 ? "GOOD." : "BAD. You need a secpack that is more recent than the installed firmware.");
	
	prepareFlash(fd);
	
	int err = 0;
	switch (mode) {
	case 'x':
		sendSecPack(fd, secpack);
		eraseBaseband(fd, start, start + length);
		endSecPack(fd);
		break;
	case 'r':
		err = readFlash(fd, binfile, start, length);
		break;
	case 'w':
		sendSecPack(fd, secpack);
		//eraseBaseband(fd, start, start + length);
		err = writeFlash(fd, binfile, start, length);
		endSecPack(fd);
		break;
	default:
		break;
	}
	
	close(fd);

	return err;
}
Ejemplo n.º 22
0
void bq34z100::chg83Table(uint16_t cellChemistry)
{
    readFlash(83, 1);
    chgFlashPair(0, cellChemistry); //LiFepo4 = 0401
    checkSum(83, 1);
}
Ejemplo n.º 23
0
void bq34z100::chg80Table(uint16_t CellTermVoltage)
{
    readFlash(80, 67);
    chgFlashPair(67, CellTermVoltage);//0A28
    checkSum(80, 67);
}
Ejemplo n.º 24
0
void bq34z100::chg68Table(uint16_t MinCellVoltage)
{
    readFlash(68, 0);
    chgFlashPair(0, MinCellVoltage);
    checkSum(68, 0);
}