コード例 #1
0
/**
  * @brief  Writes block of data to the FLASH. In this function, the number of
  *         WRITE cycles are reduced, using Page WRITE sequence.
  * @param  pBuffer: pointer to the buffer  containing the data to be written
  *         to the FLASH.
  * @param  WriteAddr: FLASH's internal address to write to.
  * @param  NumByteToWrite: number of bytes to write to the FLASH.
  * @retval None
  */
void sFLASH_WriteBuffer(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite)
{
  uint8_t NumOfPage = 0, NumOfSingle = 0, Addr = 0, count = 0, temp = 0;

  Addr = WriteAddr % sFLASH_SPI_PAGESIZE;
  count = sFLASH_SPI_PAGESIZE - Addr;
  NumOfPage =  NumByteToWrite / sFLASH_SPI_PAGESIZE;
  NumOfSingle = NumByteToWrite % sFLASH_SPI_PAGESIZE;

  if (Addr == 0) /*!< WriteAddr is sFLASH_PAGESIZE aligned  */
  {
    if (NumOfPage == 0) /*!< NumByteToWrite < sFLASH_PAGESIZE */
    {
      sFLASH_WritePage(pBuffer, WriteAddr, NumByteToWrite);
    }
    else /*!< NumByteToWrite > sFLASH_PAGESIZE */
    {
      while (NumOfPage--)
      {
        sFLASH_WritePage(pBuffer, WriteAddr, sFLASH_SPI_PAGESIZE);
        WriteAddr +=  sFLASH_SPI_PAGESIZE;
        pBuffer += sFLASH_SPI_PAGESIZE;
      }

      sFLASH_WritePage(pBuffer, WriteAddr, NumOfSingle);
    }
  }
  else /*!< WriteAddr is not sFLASH_PAGESIZE aligned  */
  {
    if (NumOfPage == 0) /*!< NumByteToWrite < sFLASH_PAGESIZE */
    {
      if (NumOfSingle > count) /*!< (NumByteToWrite + WriteAddr) > sFLASH_PAGESIZE */
      {
        temp = NumOfSingle - count;

        sFLASH_WritePage(pBuffer, WriteAddr, count);
        WriteAddr +=  count;
        pBuffer += count;

        sFLASH_WritePage(pBuffer, WriteAddr, temp);
      }
      else
      {
        sFLASH_WritePage(pBuffer, WriteAddr, NumByteToWrite);
      }
    }
    else /*!< NumByteToWrite > sFLASH_PAGESIZE */
    {
      NumByteToWrite -= count;
      NumOfPage =  NumByteToWrite / sFLASH_SPI_PAGESIZE;
      NumOfSingle = NumByteToWrite % sFLASH_SPI_PAGESIZE;

      sFLASH_WritePage(pBuffer, WriteAddr, count);
      WriteAddr +=  count;
      pBuffer += count;

      while (NumOfPage--)
      {
        sFLASH_WritePage(pBuffer, WriteAddr, sFLASH_SPI_PAGESIZE);
        WriteAddr +=  sFLASH_SPI_PAGESIZE;
        pBuffer += sFLASH_SPI_PAGESIZE;
      }

      if (NumOfSingle != 0)
      {
        sFLASH_WritePage(pBuffer, WriteAddr, NumOfSingle);
      }
    }
  }
}
コード例 #2
0
void processCommand(){
	uint32_t lengthTxData = 0;
	uint32_t addr = 0;
	uint32_t i;
	uint32_t temp;
	//At minimun resent length of trame and command name (minimun header)
	//Data_buffer_Transmit[0] = 0; //Low byte of length  set in  sendAndWaitIfNotReadyAndValidCommand before send
	//Data_buffer_Transmit[1] = 0; // High byte of length set in  sendAndWaitIfNotReadyAndValidCommand before send
	Data_buffer_Transmit[2] = rx_raw_Frame[2]; //at minimum, answer is Low byte of command
	Data_buffer_Transmit[3] = rx_raw_Frame[3]; //at minimum, answer is Hight byte of command

switch (lowHighByteToInt(rx_raw_Frame[2],rx_raw_Frame[3])  ){
		case FPGA_COMMAND :
			receiveRawDataFromFPGABySPI[2] = LOBYTE(FPGA_DATA);
			receiveRawDataFromFPGABySPI[3] = HIBYTE(FPGA_DATA);
			HAL_SPI_Receive_DMA(&hspi1, *(&receiveRawDataFromFPGABySPI) + 4, 8186 ); //Offset + 4   lenght & command type
			lengthTxData = 	lowHighByteToInt(rx_raw_Frame[0],rx_raw_Frame[1]); //resend same length to receive the answer of SPI COMMAND

			//Cs low to start spi
			HAL_GPIO_WritePin(GPIOC,GPIO_PIN_9,0);
  		    HAL_SPI_Transmit_DMA(&hspi3,*(&rx_raw_Frame)+4, lengthTxData-4);

		  break;

		  case LOOPBACK :

 			//Copy frame in tx buffer to send the same thing just a loopback for testing purpose
 			memcpy (Data_buffer_Transmit,rx_raw_Frame,rx_raw_FrameLen);

 			mustValidCommandAfterSend = 1;
 			sendUSB(Data_buffer_Transmit,rx_raw_FrameLen);
 			//endProcessCommandAllowReceiveAgain();
 			//sendAndWaitIfNotReady(rx_raw_FrameLen);

	 	 break;
		 case LED1 :
				Data_buffer_Transmit[4] = rx_raw_Frame[4];  //resend value of led
		 		lengthTxData = 4+1;  //increment length because send a value of led


		 		HAL_GPIO_WritePin(GPIOA,GPIO_PIN_9,rx_raw_Frame[4] & 1 ); //Change value of led

		 		//sendAndWaitIfNotReadyAndValidCommand(lengthTxData);
		 		//If you don't want answer just
		 		endProcessCommandAllowReceiveAgain();
		 break;
		 case LED2 :
				Data_buffer_Transmit[4] = rx_raw_Frame[4] ; //resend value of led
				lengthTxData = 4 + 1;  //increment length because send a value of led



				HAL_GPIO_WritePin(GPIOA,GPIO_PIN_10,rx_raw_Frame[4] & 1 );//Change value of led
				//sendAndWaitIfNotReadyAndValidCommand(lengthTxData);
				//If you don't want answer just
				endProcessCommandAllowReceiveAgain();
		 break;
		 	case START_FPGA :

		 		HAL_SPI_DeInit(&hspi2);

				//Nce FPGA   enable
				HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,0);

				//short pulse nconfig on release reconfigure fpga
				//HAL_GPIO_WritePin(GPIOC,GPIO_PIN_0,0);
				Delay(1000);
				HAL_GPIO_WritePin(GPIOC,GPIO_PIN_0,1);

				Delay(5000);

				//Restart SPI1 to receive data from FPGA
				__SPI1_RELEASE_RESET();
				MX_SPI1_Init();

				endProcessCommandAllowReceiveAgain();

		 	break;
		 	case STOP_FPGA :
		 		//Stop SPI1  ( receive data from FPGA)
		 		//To avoid some problem, when reset fpga (upload) 100ns pulse generate fake edge and shift (1bit) the reveived data
		 		__SPI1_FORCE_RESET();

		 		//HAL_SPI_MspDeInit(&hspi1);
		 		//Nce FPGA   disable
				HAL_GPIO_WritePin(GPIOD,GPIO_PIN_2,1);
				//nconfig on release reconfigure fpga
				HAL_GPIO_WritePin(GPIOC,GPIO_PIN_0,0);

				Delay(5000); //wait to be sure that the fpga was stopped

				MX_SPI2_Init();
				sFLASH_READID(); //Must read ID to wake up the memory
				Delay(5000);
				//HAL_SPI_MspInit(&hspi1);
				endProcessCommandAllowReceiveAgain();

		 	break;

		 	case ERASE_FIRMWARE :
		 		Data_buffer_Transmit[4] = 1;
		 		lengthTxData =   4 + 1;  //increment length because send a value (1=ok)
		 		//sFLASH_ERASE_BULK(); //slow replaced by just erasing 2 sectors
		 		sFLASH_ERASE();
		 		mustValidCommandAfterSend = 1;
		 		sendUSB(Data_buffer_Transmit,lengthTxData);

		 	break;
		 	case READ_PAGE_FIRMWARE :

		 		if (rx_raw_Frame[6] <= 31) {//Just check number of page if not less than 31 do nothing
		 			lengthTxData =   (uint32_t)rx_raw_Frame[6] * 256;

		 			Data_buffer_Transmit[4] = rx_raw_Frame[4]; //low byte Number of the first page
		 			Data_buffer_Transmit[5] = rx_raw_Frame[5]; //high byte Number of the first page
		 			Data_buffer_Transmit[6] = rx_raw_Frame[6]; //Nb of page

		 			addr=lowHighByteToInt(rx_raw_Frame[4],rx_raw_Frame[5])*256;
					sFLASH_Read(*(&Data_buffer_Transmit)+7,addr,lengthTxData);

					mustValidCommandAfterSend = 1;
					sendUSB(Data_buffer_Transmit,lengthTxData+7);

		 		}else{
		 			Data_buffer_Transmit[2] = LOBYTE(COMMAND_NOT_FOUND);
		 			Data_buffer_Transmit[3] = HIBYTE(COMMAND_NOT_FOUND);
		 			mustValidCommandAfterSend = 1;
		 			sendUSB(Data_buffer_Transmit,4);
		 		}

		 	break;
		 	case WRITE_PAGE_FIRMWARE :
		 		//Data_buffer_Transmit[4] = rx_raw_Frame[4]; //low byte Number of the first page
		 		//Data_buffer_Transmit[5] = rx_raw_Frame[5]; //high byte Number of the first page
		 		//Data_buffer_Transmit[6] = rx_raw_Frame[6]; //Nb of page  (31 max)

	 			addr=lowHighByteToInt(rx_raw_Frame[4],rx_raw_Frame[5])*256;
		 		for (int i=0;i< rx_raw_Frame[6];i++){
		 			sFLASH_WritePage(*(&rx_raw_Frame)+7+i*256,addr+i*256,256);
		 		}

		 		endProcessCommandAllowReceiveAgain();

		 	break;

		 	case GET_VERSION_NUMBER :
					lengthTxData = 18;
					memcpy(&Data_buffer_Transmit[4],&versionNumber,lengthTxData);
					mustValidCommandAfterSend = 1;
					sendUSB(Data_buffer_Transmit,lengthTxData+4);
			break;


		 	default:
				//mustValidCommandAfterSend = 1;
				//sendAndWaitIfNotReady(lengthTxData);
				//If you don't want answer just
				endProcessCommandAllowReceiveAgain();


	}
}