void spi_free(spi_t *obj) { // Reset SPI and disable clock if (obj->spi == SPI_1) { __SPI1_FORCE_RESET(); __SPI1_RELEASE_RESET(); __SPI1_CLK_DISABLE(); } if (obj->spi == SPI_2) { __SPI2_FORCE_RESET(); __SPI2_RELEASE_RESET(); __SPI2_CLK_DISABLE(); } if (obj->spi == SPI_3) { __SPI3_FORCE_RESET(); __SPI3_RELEASE_RESET(); __SPI3_CLK_DISABLE(); } // Configure GPIOs pin_function(obj->pin_miso, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); pin_function(obj->pin_mosi, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); pin_function(obj->pin_sclk, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); pin_function(obj->pin_ssel, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); }
void spi_free(spi_t *obj) { // Reset SPI and disable clock switch(obj->spi.module) { case 0: __SPI1_FORCE_RESET(); __SPI1_RELEASE_RESET(); __SPI1_CLK_DISABLE(); break; case 1: __SPI2_FORCE_RESET(); __SPI2_RELEASE_RESET(); __SPI2_CLK_DISABLE(); break; case 2: __SPI3_FORCE_RESET(); __SPI3_RELEASE_RESET(); __SPI3_CLK_DISABLE(); break; #if MODULES_SIZE_SPI > 3 case 3: __SPI4_FORCE_RESET(); __SPI4_RELEASE_RESET(); __SPI4_CLK_DISABLE(); break; #endif #if MODULES_SIZE_SPI > 4 case 4: __SPI5_FORCE_RESET(); __SPI5_RELEASE_RESET(); __SPI5_CLK_DISABLE(); break; #endif #if MODULES_SIZE_SPI > 5 case 5: __SPI6_FORCE_RESET(); __SPI6_RELEASE_RESET(); __SPI6_CLK_DISABLE(); break; #endif default: break; } // Configure GPIOs pin_function(obj->spi.pin_miso, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); pin_function(obj->spi.pin_mosi, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); pin_function(obj->spi.pin_sclk, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0)); DEBUG_PRINTF("SPI%u: Free\n", obj->spi.module+1); }
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(); } }