DATAFLASH API Sectors test of nanoWipom board


DATAFLASH API Sectors test.

Toolkit:STM Development System

Location:/bipom/devtools/STM32/examples/nanoWipom/df_sectors

Code Example


#define WRITE_TEST

// ****************************************************************************

UCB		 			uart0;
DSCB				dfDriver;
UBYTE				dfBuffer[DATASTORAGEAPI_SECTOR_SIZE];

ERRCODE DFApiTest(void);

// ****************************************************************************

int main (void)
{
	
	ERRCODE ec;
	
	/* Initialize all port pins */
	NWDIO_Init();

	delayMs(250);

			
	/* Initialize Device Manager */
	DM_Init();
	/* 	Initialize SPI1 and register to Device Manager	*/
	SPI1_Init(SPI1_INTERFACE_TYPE,DEVICE_INTERFACE_IN_PROGRESS);
	

	/* Initialize UART0 */
	uart0.baudRate		= 115200;									// uart baudrate

	uart0.parity		= SERIALAPI_PARITY_NO;						// uart parity

	uart0.stop			= SERIALAPI_STOP_1;							// uart stop bit

	uart0.word_length	= SERIALAPI_WORLD_LENGTH_8;					// uart word length

	uart0.rxFifo		= SERIALAPI_FIFO_14;						// uart RX FIFO trigger level

	uart0.txFifo		= SERIALAPI_FIFO_14;						// uart TX FIFO trigger level

	// NATIVE UART0

	uart0.iface			= SERIALAPI_UART1_INTERFACE;				// interface

	//

	ec = serialAPI_init(&uart0);
	if(ec) 
	{
		// Print the error using the standard 19200 serial channel

		tprintf("\r\nUART0 ERROR: ec =%d",ec);
		for (;;);
	}
	/* Send messages to TERMINAL window */
	serialAPI_tiprintf(&uart0,"\r\nnanoWiPOM DF Test VER 1.01");
	serialAPI_tiprintf(&uart0,"\r\nUART0 init is OK");

	//

	dfDriver.dataBuffer 	= dfBuffer;
	dfDriver.iface			= DATASTORAGEAPI_SPI1_INTERFACE;
	dfDriver.clock			= DATAFLASH_SPI_CLOCK;						// SPI clock = 1MHz

	dataflash_spi1cs10_init(&dfDriver);
	if(ec)
	{
		serialAPI_tiprintf(&uart0,"\r\nERROR: DF INIT, ec=%d",ec);
		goto func_end;
	}

	serialAPI_tiprintf(&uart0,"\r\nDF init is OK");

	/* Run the test  */
	serialAPI_tiprintf(&uart0,"\r\n>");

	ec = DFApiTest();

func_end:	
	tprintf("\r\nEND, ec =%d",ec);
	for(;;);
	return 0;
}


// ****************************************************************************

ERRCODE DFApiTest(void)
{
	ERRCODE ec=SUCCESS;
	int dataInt=-1;
	int escape_cnt=0;
	UINT len;
	UINT dataFlashSector =0;
	DATAFLASH_STATUS dataFlashStatus;

	for (;;)
	{
		// try to get data byte from uart0

		if (uart0.getRxCounter())
		{
			dataInt = uart0.getData();
			if(dataInt != -1 ) 
			{
				// Check 

				if(dataInt == '-')
				{ 
					if (++escape_cnt == 3) break;
				}
				else
				{
					escape_cnt =0;
				}		
				
				// Print statistics

				if(dataInt == '?')
				{	
					// Wait for 1ms to allow to get the data byte back

					// to show correct statistics

					delayMs(1); 

					serialAPI_tiprintf(&uart0,"\n\rUART0: rx=%08lu tx=%08lu errors=%08lu overflows=%08lu", 
						uart0.getRxCnt(),uart0.getTxCnt(),uart0.getErrors(),uart0.getOverflows());

					serialAPI_tiprintf(&uart0,"\n\rDATAFLASH: rx=%08lu tx=%08lu errors=%08lu", 
						dfDriver.getRxCnt(),dfDriver.getTxCnt(),dfDriver.getErrors());

					serialAPI_tiprintf(&uart0,"\n\r");		
					continue;
				}
				#ifdef WRITE_TEST
				serialAPI_tiprintf(&uart0,"\n\rWRITE TEST");

				// WRITE NEXT SECTOR

				for(len=0;len<DATASTORAGEAPI_SECTOR_SIZE;len++)
					dfDriver.dataBuffer[len]= dataFlashSector & 0xFF;

				dfDriver.writeSector(dataFlashSector);
				
				do{	dataFlashStatus = dfDriver.getStatus(); } 
				while(DATAFLASH_BUSY == dataFlashStatus);
				
				serialAPI_tiprintf(&uart0,"\n\rDATAFLASH: status %d ",dataFlashStatus);

				//

				if (DATAFLASH_SUCCESS == dataFlashStatus)
				{
					serialAPI_tiprintf(&uart0,"\n\rDATAFLASH: sector %d is written",dataFlashSector);
				}
				if (DATAFLASH_ERROR == dataFlashStatus)
				{
					serialAPI_tiprintf(&uart0,"\n\rDATAFLASH: sector %d is NOT written",dataFlashSector);
				}
				#endif
				// READ NEXT SECTOR

				serialAPI_tiprintf(&uart0,"\n\rREAD TEST");
				for(len=0;len<DATASTORAGEAPI_SECTOR_SIZE;len++)
					dfDriver.dataBuffer[len]= 0x00;

				dfDriver.readSector(dataFlashSector);
				
				do{dataFlashStatus = dfDriver.getStatus();} 
				while(DATAFLASH_BUSY == dataFlashStatus);

				if (DATAFLASH_SUCCESS == dataFlashStatus)
				{
					serialAPI_tiprintf(&uart0,"\n\rDATAFLASH: sector %d is read",dataFlashSector);
					for(len=0;len<DATASTORAGEAPI_SECTOR_SIZE;len++)
					{
						if (!(len%16)) serialAPI_tiprintf(&uart0,"\n\r%04x",len);
						serialAPI_tiprintf(&uart0," %02X",dfDriver.dataBuffer[len]);
					} 
				}
				else
					serialAPI_tiprintf
					(&uart0,"\n\rDATAFLASH: sector %d is NOT read,status=%d",
						dataFlashSector,dataFlashStatus);

				if(((dfDriver.getMaxSector())<<1) <= ++dataFlashSector)
					dataFlashSector=0; 
			}
		}
	}
	
	return ec;
}