Example #1
0
void menuUp1(uint8_t event)
{
	FRESULT fr ;
	struct fileControl *fc = &FileControl ;
  static uint8_t mounted = 0 ;
	static uint32_t state ;
	static uint32_t firmwareAddress ;
	uint32_t i ;
	uint32_t width ;
	 
	if (UpdateItem == UPDATE_TYPE_BOOTLOADER )		// Bootloader
	{
  	TITLE( "UPDATE BOOT" ) ;
	}
	else
	{
#ifdef PCBX9D
		if (UpdateItem == UPDATE_TYPE_SPORT_INT )
		{
  		TITLE( "UPDATE Int. XJT" ) ;
		}
		else
		{
  		TITLE( "UPDATE Ext. SPort" ) ;
		}
#endif

#ifdef PCBSKY
 #ifndef REVX
 		if (UpdateItem == UPDATE_TYPE_COPROCESSOR )
		{
  		TITLE( "UPDATE COPROC" ) ;
		}
		else
		{
  		TITLE( "UPDATE SPort" ) ;
		}
 #else
 		if (UpdateItem == UPDATE_TYPE_SPORT_EXT )
		{
  		TITLE( "UPDATE SPort" ) ;
		}
 #endif
#endif
	}
	switch(event)
	{
    case EVT_ENTRY:
			state = UPDATE_NO_FILES ;
			if ( mounted == 0 )
			{
#if defined(PCBTARANIS)
  			fr = f_mount(0, &g_FATFS_Obj) ;
#else				
  			fr = f_mount(0, &g_FATFS) ;
#endif
#ifdef PCBX9D
				unlockFlash() ;
#endif
			}
			else
			{
				fr = FR_OK ;
			}
			if ( fr == FR_OK)
			{
				mounted = 1 ;
			}
			if ( mounted )
			{
				fr = f_chdir( (TCHAR *)"\\firmware" ) ;
				if ( fr == FR_OK )
				{
					state = UPDATE_NO_FILES ;
					fc->index = 0 ;
					fr = f_opendir( &Dj, (TCHAR *) "." ) ;
					if ( fr == FR_OK )
					{
 						if ( (UpdateItem > 1 ) )
						{
							fc->ext[0] = 'F' ;
							fc->ext[1] = 'R' ;
							fc->ext[2] = 'K' ;
						}
						else
						{
							fc->ext[0] = 'B' ;
							fc->ext[1] = 'I' ;
							fc->ext[2] = 'N' ;
						}
						fc->ext[3] = 0 ;
						fc->index = 0 ;
						fc->nameCount = fillNames( 0, fc ) ;
						fc->hpos = 0 ;
						fc->vpos = 0 ;
						if ( fc->nameCount )
						{
							state = UPDATE_FILE_LIST ;
						}
					}
				}
			}
    break ;
    
		case EVT_KEY_FIRST(KEY_EXIT):
			if ( state < UPDATE_ACTION )
			{
      	chainMenu(menuUpdate) ;
    		killEvents(event) ;
			}
    break ;
	}

	switch ( state )
	{
		case UPDATE_NO_FILES :
			lcd_puts_Pleft( 4*FH, "\005No Files" ) ;
	    lcd_outdez( 21*FW, 4*FH, mounted ) ;
    break ;
		
		case UPDATE_FILE_LIST :
			SportVerValid = 0 ;
			if ( fileList( event, &FileControl ) == 1 )
			{
				state = UPDATE_CONFIRM ;
			}
    break ;
		case UPDATE_CONFIRM :
 			if ( (UpdateItem > UPDATE_TYPE_BOOTLOADER ) )
			{
#ifdef PCBX9D
 				if ( (UpdateItem == UPDATE_TYPE_SPORT_INT ) )
				{
					lcd_puts_Pleft( 2*FH, "Flash Int. XJT from" ) ;
				}
				else
				{
					lcd_puts_Pleft( 2*FH, "Flash Ext.SP from" ) ;
				}
				SportVerValid = 0 ;
#else
 #ifndef REVX
 				if ( (UpdateItem == UPDATE_TYPE_COPROCESSOR ) )
				{
					lcd_puts_Pleft( 2*FH, "Flash Co-Proc. from" ) ;
				}
				else
				{
					lcd_puts_Pleft( 2*FH, "Flash SPort from" ) ;
				}
				CoProcReady = 0 ;
 #else
				lcd_puts_Pleft( 2*FH, "Flash SPort from" ) ;
 #endif
#endif
			}
			else
			{
				lcd_puts_Pleft( 2*FH, "Flash Bootloader from" ) ;
			}
			cpystr( cpystr( (uint8_t *)FlashFilename, (uint8_t *)"\\firmware\\" ), (uint8_t *)Filenames[fc->vpos] ) ;
#if defined(PCBTARANIS)
			lcd_putsnAtt( 0, 4*FH, Filenames[fc->vpos], DISPLAY_CHAR_WIDTH, 0 ) ;
#else
			lcd_putsnAtt0( 0, 4*FH, Filenames[fc->vpos], DISPLAY_CHAR_WIDTH, 0 ) ;
#endif
			if ( event == EVT_KEY_LONG(KEY_MENU) )
			{
				state = UPDATE_SELECTED ;
			}
			if ( event == EVT_KEY_LONG(KEY_EXIT) )
			{
				state = UPDATE_FILE_LIST ;		// Canceled
			}
    break ;
		case UPDATE_SELECTED :
			f_open( &FlashFile, FlashFilename, FA_READ ) ;
			f_read( &FlashFile, (BYTE *)FileData, 1024, &BlockCount ) ;
			i = 1 ;
			if (UpdateItem == UPDATE_TYPE_BOOTLOADER )		// Bootloader
			{
				i = validateFile( (uint32_t *) FileData ) ;
			}
			if ( i == 0 )
			{
				state = UPDATE_INVALID ;
			}
			else
			{
				if (UpdateItem == UPDATE_TYPE_BOOTLOADER )		// Bootloader
				{
#ifdef PCBX9D
					firmwareAddress = 0x08000000 ;
#endif
#ifdef PCBSKY
					firmwareAddress = 0x00400000 ;
#endif
				}
#ifdef PCBSKY
 #ifndef REVX
				else if (UpdateItem == UPDATE_TYPE_COPROCESSOR )		// Bootloader
				{
					firmwareAddress = 0x00000080 ;
					if ( check_ready() == 0 )
					{
						CoProcReady = 1 ;
					}
				}
 #endif
#endif
				else
				{
// SPort update
					SportState = SPORT_START ;
					FirmwareSize = FileSize[fc->vpos] ;
					BlockInUse = 0 ;
					f_read( &FlashFile, (BYTE *)ExtraFileData, 1024, &XblockCount ) ;
				}
				BytesFlashed = 0 ;
				BlockOffset = 0 ;
				ByteEnd = 1024 ;
				state = UPDATE_ACTION ;
			}
    break ;
		case UPDATE_INVALID :
			lcd_puts_Pleft( 2*FH, "Invalid File" ) ;
			lcd_puts_Pleft( 4*FH, "Press EXIT" ) ;
			if ( event == EVT_KEY_FIRST(KEY_EXIT) )
			{
				state = UPDATE_FILE_LIST ;		// Canceled
    		killEvents(event) ;
			}
    break ;
		case UPDATE_ACTION :
			// Do the flashing
			lcd_puts_Pleft( 3*FH, "Flashing" ) ;
			if (UpdateItem == UPDATE_TYPE_BOOTLOADER )		// Bootloader
			{
				width = ByteEnd >> 9 ;
				if ( BytesFlashed < ByteEnd )
				{
					program( (uint32_t *)firmwareAddress, &((uint32_t *)FileData)[BlockOffset] ) ;	// size is 256 bytes
					BlockOffset += 64 ;		// 32-bit words (256 bytes)
					firmwareAddress += 256 ;
					BytesFlashed += 256 ;
				}
				else
				{
					if ( ByteEnd >= 32768 )
					{
						state = UPDATE_COMPLETE ;
					}
					else
					{
						f_read( &FlashFile, (BYTE *)FileData, 1024, &BlockCount ) ;
						ByteEnd += 1024 ;
						BlockOffset = 0 ;
					}
				}
			}

#ifdef PCBSKY
 #ifndef REVX
			else if (UpdateItem == UPDATE_TYPE_COPROCESSOR )		// CoProcessor
Example #2
0
void arduinoDueSlave()
{
	int16_t byte ;
	uint16_t address ;
	// Initialise serial at 100K baud
  DDRE &= ~(1 << DDE0) ;    // set RXD0 pin as input
  PORTE &= ~(1 << PORTE0) ; // disable pullup on RXD0 pin

 	UCSR0A &= ~(1 << U2X0) ; // disable double speed operation.
 	UBRR0L = 4 ;		// 200000 baud
 	UBRR0H = 0 ;
  // set 8 N1
  UCSR0B = 0 | (0 << RXCIE0) | (0 << TXCIE0) | (0 << UDRIE0) | (0 << RXEN0) | (0 << TXEN0) | (0 << UCSZ02) ;
  UCSR0C = 0 | (1 << UCSZ01) | (1 << UCSZ00) ;
  
  while (UCSR0A & (1 << RXC0)) UDR0 ; // flush receive buffer

  UCSR0B |= (1 << RXEN0);  // enable RX
  UCSR0B |= (1 << RXCIE0); // enable Interrupt
  UCSR0B |= (1 << TXEN0) ; // enable TX, but not interrupt yet
	 
	Arduino = 1 ;
	
  lcd_clear() ;
  lcd_puts_Pleft( 24, PSTR("Arduino Slave") ) ;
	refreshDiplay();

	for(;;)
	{
    static uint8_t lastTMR ;
    wdt_reset() ;
		while ( ( byte = get_fifo64( &Arduino_fifo ) ) != -1 )
		{
			processSlaveByte( byte ) ;
			if (SlaveActionRequired)
			{
				SlaveActionRequired = 0 ;
				if ( SlaveType < 16)
				{
					SlaveDisplayRefresh = 1 ;
				}
				else
				{
					switch ( SlaveType )
					{
						case 0x10 :		// Protocol + channels
							Protocol = SlaveTempReceiveBuffer[0] ;
							SubProtocol = SlaveTempReceiveBuffer[1] ;
							{
								uint8_t i ;
								uint8_t index = 0 ;
								for ( i = 2 ; i < 34 ; i += 2 )
								{
									// May need cli/sei round this
									Channels[index] = SlaveTempReceiveBuffer[i] | ( SlaveTempReceiveBuffer[i+1] <<  8 ) ;
									index += 1 ;
								}
							}
						break ;
							
						case 0x12 :
// 0x01, 0x12, 16 bit address, 0x01  - send 32 bytes EEPROM data @ address
							address = SlaveTempReceiveBuffer[0] | ( SlaveTempReceiveBuffer[1] <<  8 ) ;
							EepromAddress = address ;
							EepromRequested = 1 ;
						break ;
					}
				}
			}
		}
		uint8_t t10ms ;
		t10ms = g_tmr10ms ;
    tick10ms = t10ms - lastTMR ;
    lastTMR = t10ms ;
    if(tick10ms) //make sure the rest happen only every 10ms.
		{
			ControlsRequested = 1 ;
			
			if ( SlaveDisplayRefresh )
			{
				SlaveDisplayRefresh = 0 ;
				refreshDiplay() ;
			}
		}
		if ( TxBusy == 0 )
		{
			if ( EepromRequested )
			{
				sendEeprom( EepromAddress ) ;
				EepromRequested = 0 ;
			}
			else if ( ControlsRequested )
			{
				sendControls() ;
				ControlsRequested = 0 ;
			}
		}
	}
}