/******************************************************************************* SPI_FLASH_SendByte *******************************************************************************/ u8 SPI_FLASH_SendByte(u8 byte) { while(SPI_GetFlagStatus(SPI3, SPI_FLAG_TXE) == RESET); SPI_SendData(SPI3, byte); while(SPI_GetFlagStatus(SPI3, SPI_FLAG_RXNE) == RESET); return SPI_ReceiveData(SPI3); }
/******************************************************************************* * Read a Byte from MiniSD Card Return: Data *******************************************************************************/ unsigned char MSD_ReadByte(void) { while (SPI_GetFlagStatus(SPI2, SPI_FLAG_TXE) == RESET); SPI_SendData(SPI2, DUMMY); while (SPI_GetFlagStatus(SPI2, SPI_FLAG_RXNE) == RESET); return SPI_ReceiveData(SPI2); }
uint8_t SPISendByte(uint8_t Data) { #ifdef USE_SPI_MODULE /*!< Wait until the transmit buffer is empty */ while (SPI_GetFlagStatus(SPI_FLAG_TXE) == RESET) {} /*!< Send the byte */ SPI_SendData(Data); /*!< Wait to receive a byte*/ while (SPI_GetFlagStatus(SPI_FLAG_RXNE) == RESET) {} /*!< Return the byte read from the SPI bus */ return SPI_ReceiveData(); #else uint8_t i; for(i=0;i<8;i++) { GPIO_WriteLow(ENC_SCK_PORT,ENC_SCK_PIN); if(Data & 0x80) GPIO_WriteHigh(ENC_MOSI_PORT,ENC_MOSI_PIN); else GPIO_WriteLow(ENC_MOSI_PORT,ENC_MOSI_PIN); GPIO_WriteHigh(ENC_SCK_PORT,ENC_SCK_PIN); Data <<=1; } GPIO_WriteLow(ENC_SCK_PORT,ENC_SCK_PIN); return 0; #endif }
unsigned char SpiTxRx_Byte(unsigned char data) { #if 1 u16 tmpdata; while(SPI_GetFlagStatus(SPI2, SPI_FLAG_TXE) == RESET); SPI_SendData(SPI2, data); while (SPI_GetFlagStatus(SPI2, SPI_FLAG_RXNE) == RESET); tmpdata = SPI_ReceiveData(SPI2); return tmpdata; #endif #if 0 // INT8U i,temp; // temp = 0; /*等待发送寄存器空*/ while((SPI2->SR & (uint8_t)SPI_FLAG_TXE)==RESET); /*发送一个字节*/ SPI1->DR = data; /* 等待接收寄存器有效*/ while((SPI2->SR & (uint8_t) SPI_FLAG_RXNE)==RESET); return(SPI2->DR); #endif }
/** * @brief Reset the ERSR status bit. * @param None * @retval Status */ uint8_t EepromResetSrwd(void) { uint8_t status; uint8_t cmd[] = {EEPROM_CMD_WRSR, EEPROM_STATUS_SRWD}; // SPI_ENTER_CRITICAL(); EepromWriteEnable(); /* Put the SPI chip select low to start the transaction */ EepromSPICSLow(); // for(volatile uint16_t i=0;i<CS_TO_SCLK_DELAY;i++); /* Send command */ for(uint8_t k=0;k<2;k++) { while (SPI_GetFlagStatus(s_EepromSpiPort, SPI_FLAG_TXE) == RESET); SPI_SendData(s_EepromSpiPort, cmd[k]); while (SPI_GetFlagStatus(s_EepromSpiPort, SPI_FLAG_RXNE) == RESET); status = SPI_ReceiveData(s_EepromSpiPort); } while (SPI_GetFlagStatus(s_EepromSpiPort, SPI_FLAG_TXE) == RESET); /* Put the SPI chip select high to end the transaction */ EepromSPICSHigh(); // SPI_EXIT_CRITICAL(); return status; }
/** * @brief Wait polling the SPI until the internal WIP flag is RESET. * The flag is SET when a write operation is running. * @param None * @retval None */ void EepromWaitEndWriteOperation(void) { uint8_t cmd = EEPROM_CMD_RDSR; uint8_t dummy = 0xFF; uint8_t status; // SPI_ENTER_CRITICAL(); /* Put the SPI chip select low to start the transaction */ EepromSPICSLow(); // for(volatile uint16_t i=0;i<CS_TO_SCLK_DELAY;i++); /* Send command */ while (SPI_GetFlagStatus(s_EepromSpiPort, SPI_FLAG_TXE) == RESET); SPI_SendData(s_EepromSpiPort, cmd); while (SPI_GetFlagStatus(s_EepromSpiPort, SPI_FLAG_RXNE) == RESET); SPI_ReceiveData(s_EepromSpiPort); /* Polling on status register */ do{ while (SPI_GetFlagStatus(s_EepromSpiPort, SPI_FLAG_TXE) == RESET); SPI_SendData(s_EepromSpiPort, dummy); while (SPI_GetFlagStatus(s_EepromSpiPort, SPI_FLAG_RXNE) == RESET); status = SPI_ReceiveData(s_EepromSpiPort); }while(status&EEPROM_STATUS_WIP); while (SPI_GetFlagStatus(s_EepromSpiPort, SPI_FLAG_TXE) == RESET); /* Put the SPI chip select high to end the transaction */ EepromSPICSHigh(); // SPI_EXIT_CRITICAL(); }
/** * @brief 通过SPI0发送、读取一个字节 * @param txData 要发送的字节 * @retval 读取的字节 */ u8 SPI0_ReadWriteByte(u8 txData) { u8 timeout = 200; /* 检查SPI是否可发送 */ while (!SPI_GetFlagStatus(HT_SPI0, SPI_FLAG_TXBE)) { if(--timeout == 0) return 0; } /* 通过SPI0发送一个字节 */ SPI_SendData(HT_SPI0, (u32)txData); timeout = 200; /* 检查SPI是否可接收 */ while (!SPI_GetFlagStatus(HT_SPI0, SPI_FLAG_RXBNE)) { if(--timeout == 0) return 0; } /* 返回通过SPI0接收的字节 */ return SPI_ReceiveData(HT_SPI0); }
uint16_t spirit1_arch_refresh_status(void) { volatile uint16_t mcstate = 0x0000; uint8_t header[2]; int i; header[0]=0x01; header[1]=0; /* CS is active low */ SPIRIT_SPI_PERIPH_CS_PORT->BSRRH = SPIRIT_SPI_PERIPH_CS_PIN; { volatile uint32_t iv; for(iv = 0; iv < CS_TO_SCLK_DELAY; iv++); } /* send arbitrary header bytes and read out MC_STATE from SPI rx buffer */ for(i = 0; i < 2; i++) { while (SPI_GetFlagStatus(SPIRIT_SPI_PERIPH_NB, SPI_FLAG_TXE) == RESET); SPI_SendData(SPIRIT_SPI_PERIPH_NB, header[i]); while (SPI_GetFlagStatus(SPIRIT_SPI_PERIPH_NB, SPI_FLAG_RXNE) == RESET); mcstate += ((uint16_t)(SPI_ReceiveData(SPIRIT_SPI_PERIPH_NB)))<<((1-i)*8); } /* finish up */ while (SPI_GetFlagStatus(SPIRIT_SPI_PERIPH_NB, SPI_FLAG_TXE) == RESET); SPIRIT_SPI_PERIPH_CS_PORT->BSRRL = SPIRIT_SPI_PERIPH_CS_PIN; return mcstate; }
/** * @brief Wait polling the SPI until the internal WIP flag is RESET. * The flag is SET when a write operation is running. * @param None * @retval None */ void EepromWaitEndWriteOperation(void) { uint8_t cmd = EEPROM_CMD_RDSR; uint8_t dummy = 0xFF; uint8_t status; /* Put the SPI chip select low to start the transaction */ EepromSPICSLow(Get_vectEepromSpiCsPort(),Get_vectEepromSpiCsPin()); /* Send command */ while (SPI_GetFlagStatus(Get_EepromSpiPort(), SPI_FLAG_TXE) == RESET); SPI_SendData(Get_EepromSpiPort(), cmd); while (SPI_GetFlagStatus(Get_EepromSpiPort(), SPI_FLAG_RXNE) == RESET); SPI_ReceiveData(Get_EepromSpiPort()); /* Polling on status register */ do{ while (SPI_GetFlagStatus(Get_EepromSpiPort(), SPI_FLAG_TXE) == RESET); SPI_SendData(Get_EepromSpiPort(), dummy); while (SPI_GetFlagStatus(Get_EepromSpiPort(), SPI_FLAG_RXNE) == RESET); status = SPI_ReceiveData(Get_EepromSpiPort()); }while(status&EEPROM_STATUS_WIP); while (SPI_GetFlagStatus(Get_EepromSpiPort(), SPI_FLAG_TXE) == RESET); /* Put the SPI chip select high to end the transaction */ EepromSPICSHigh(Get_vectEepromSpiCsPort(),Get_vectEepromSpiCsPin()); }/* end EepromWaitEndWriteOperation() */
/******************************************************************************* * Write a Byte to MiniSD Card Input: Data *******************************************************************************/ unsigned char MSD_WriteByte(u8 Data) { while (SPI_GetFlagStatus(SPI2, SPI_FLAG_TXE) == RESET); SPI_SendData(SPI2, Data); while (SPI_GetFlagStatus(SPI2, SPI_FLAG_RXNE) == RESET); return SPI_ReceiveData(SPI2); }
/** * @brief Reset the ERSR status bit. * @param None * @retval Status */ uint8_t EepromResetSrwd(void) { uint8_t status; uint8_t cmd[] = {EEPROM_CMD_WRSR, EEPROM_STATUS_SRWD}; EepromWriteEnable(); /* Put the SPI chip select low to start the transaction */ EepromSPICSLow(Get_vectEepromSpiCsPort(),Get_vectEepromSpiCsPin()); /* Send command */ for(uint8_t k=0;k<2;k++) { while (SPI_GetFlagStatus(Get_EepromSpiPort(), SPI_FLAG_TXE) == RESET); SPI_SendData(Get_EepromSpiPort(), cmd[k]); while (SPI_GetFlagStatus(Get_EepromSpiPort(), SPI_FLAG_RXNE) == RESET); status = SPI_ReceiveData(Get_EepromSpiPort()); } while (SPI_GetFlagStatus(Get_EepromSpiPort(), SPI_FLAG_TXE) == RESET); /* Put the SPI chip select high to end the transaction */ EepromSPICSHigh(Get_vectEepromSpiCsPort(),Get_vectEepromSpiCsPin()); return status; }/* end EepromResetSrwd() */
//spi basic read/write function u8 EF_spiFlashRW(u8 data) { while( DMA_Tx_Busy == 1) __nop(); while( SPI_GetFlagStatus(SPIFLASH_SPI,SPI_I2S_FLAG_TXE)==RESET ) __nop(); SPI_SendData(SPIFLASH_SPI,(u16)data); while( SPI_GetFlagStatus(SPIFLASH_SPI,SPI_I2S_FLAG_RXNE)==RESET ) __nop(); return (u8)SPI_ReceiveData(SPIFLASH_SPI); }
/** * @brief Write data into TX FIFO * @param cNbBytes: number of bytes to be written into TX FIFO * @param pcBuffer: pointer to data to write * @retval SPIRIT status */ SpiritStatus SpiritSpiWriteLinearFifo(uint8_t cNbBytes, uint8_t* pcBuffer) { uint16_t tmpstatus = 0x0000; uint8_t header[2]; int i, index; SpiritStatus *status=(SpiritStatus *)&tmpstatus; /* Built the header bytes */ header[0]=WRITE_HEADER; header[1]=LINEAR_FIFO_ADDRESS; SPI_ENTER_CRITICAL(); /* Put the SPI chip select low to start the transaction */ SpiritSPICSLow(); { volatile uint16_t iv; for(iv=0; iv < CS_TO_SCLK_DELAY; iv++); } /* Write the header bytes and read the SPIRIT status bytes */ for(i=0; i<2; i++) { while (SPI_GetFlagStatus(SPIRIT_SPI_PERIPH_NB, SPI_FLAG_TXE) == RESET); SPI_SendData(SPIRIT_SPI_PERIPH_NB, header[i]); while (SPI_GetFlagStatus(SPIRIT_SPI_PERIPH_NB, SPI_FLAG_RXNE) == RESET); tmpstatus += ((uint16_t)(SPI_ReceiveData(SPIRIT_SPI_PERIPH_NB)))<<((1-i)*8); } /* Write the data into the FIFO according to the number of bytes */ for(index=0; index<cNbBytes; index++) { while (SPI_GetFlagStatus(SPIRIT_SPI_PERIPH_NB, SPI_FLAG_TXE) == RESET); SPI_SendData(SPIRIT_SPI_PERIPH_NB, pcBuffer[index]); } /* To be sure to don't rise the Chip Select before the end of last sending */ while (SPI_GetFlagStatus(SPIRIT_SPI_PERIPH_NB, SPI_FLAG_TXE) == RESET); if(SPI_GetFlagStatus(SPIRIT_SPI_PERIPH_NB, SPI_FLAG_RXNE) == SET) { SPI_ReceiveData(SPIRIT_SPI_PERIPH_NB); } /* Put the SPI chip select high to end the transaction */ SpiritSPICSHigh(); SPI_EXIT_CRITICAL(); return *status; }
/*************************************************************************** Declaration : char spi_byte(char data_byte) Description : Transmit and receive one byte on the SPI port ***************************************************************************/ char spi_byte(char data_byte) { while(SPI_GetFlagStatus(SPI1, SPI_FLAG_TXE) == RESET); /* Send byte through the SPI1 peripheral */ SPI_SendData(SPI1, data_byte); /* Wait to receive a byte */ while(SPI_GetFlagStatus(SPI1, SPI_FLAG_RXNE) == RESET); data_byte=SPI_ReceiveData(SPI1); /* Return the byte read from the SPI bus */ return data_byte; }
/** * @brief Read single or multiple SPIRIT register * @param cRegAddress: base register's address to be read * @param cNbBytes: number of registers and bytes to be read * @param pcBuffer: pointer to the buffer of registers' values read * @retval SPIRIT status */ SpiritStatus SpiritSpiReadRegisters(uint8_t cRegAddress, uint8_t cNbBytes, uint8_t* pcBuffer) { uint16_t tmpstatus = 0x0000; uint8_t header[2]; uint8_t dummy=0xFF; int i, index; SpiritStatus *status=(SpiritStatus *)&tmpstatus; /* Built the header bytes */ header[0]=READ_HEADER; header[1]=cRegAddress; SPI_ENTER_CRITICAL(); /* Put the SPI chip select low to start the transaction */ SpiritSPICSLow(); { volatile uint16_t iv; for(iv = 0; iv < CS_TO_SCLK_DELAY; iv++); } /* Write the header bytes and read the SPIRIT status bytes */ for(i=0; i<2; i++) { while (SPI_GetFlagStatus(SPIRIT_SPI_PERIPH_NB, SPI_FLAG_TXE) == RESET); SPI_SendData(SPIRIT_SPI_PERIPH_NB, header[i]); while (SPI_GetFlagStatus(SPIRIT_SPI_PERIPH_NB, SPI_FLAG_RXNE) == RESET); tmpstatus += ((uint16_t)(SPI_ReceiveData(SPIRIT_SPI_PERIPH_NB)))<<((1-i)*8); } /* Read the registers according to the number of bytes */ for(index=0; index<cNbBytes; index++) { while (SPI_GetFlagStatus(SPIRIT_SPI_PERIPH_NB, SPI_FLAG_TXE) == RESET); SPI_SendData(SPIRIT_SPI_PERIPH_NB, dummy); while (SPI_GetFlagStatus(SPIRIT_SPI_PERIPH_NB, SPI_FLAG_RXNE) == RESET); *pcBuffer = SPI_ReceiveData(SPIRIT_SPI_PERIPH_NB); pcBuffer++; } while (SPI_GetFlagStatus(SPIRIT_SPI_PERIPH_NB, SPI_FLAG_TXE) == RESET); /* Put the SPI chip select high to end the transaction */ SpiritSPICSHigh(); SPI_EXIT_CRITICAL(); return *status; }
char SendStrobe(char strobe) { unsigned char out; ss_low; //lo while (SPI_GetFlagStatus(SPI1,SPI_FLAG_TXE)== RESET); SPI_SendData(SPI1, strobe); //SEND STROBE BYTE while (SPI_GetFlagStatus(SPI1,SPI_FLAG_RXNE) == RESET); out = SPI_ReceiveData(SPI1); ss_high; // hi return out; }
/******************************************************************************* * Function Name : SPI_FLASH_SendByte * Description : Sends a byte through the SPI interface and return the byte * received from the SPI bus. * Input : byte : byte to send. * Output : None * Return : The value of the received byte. *******************************************************************************/ u8 SPI_FLASH_SendByte(u8 byte) { /* Loop while DR register in not emplty */ while(SPI_GetFlagStatus(SPI1, SPI_FLAG_TXE) == RESET); /* Send byte through the SPI1 peripheral */ SPI_SendData(SPI1, byte); /* Wait to receive a byte */ while(SPI_GetFlagStatus(SPI1, SPI_FLAG_RXNE) == RESET); /* Return the byte read from the SPI bus */ return SPI_ReceiveData(SPI1); }
/** * @brief Read data from RX FIFO * @param cNbBytes: number of bytes to read from RX FIFO * @param pcBuffer: pointer to data read from RX FIFO * @retval SPIRIT status */ StatusBytes SdkEvalSpiReadFifo(uint8_t cNbBytes, uint8_t* pcBuffer) { uint16_t tmpstatus = 0x0000; StatusBytes *status=(StatusBytes *)&tmpstatus; uint8_t header[2]; uint8_t dummy=0xFF; /* Built the header bytes */ header[0]=READ_HEADER; header[1]=LINEAR_FIFO_ADDRESS; SPI_ENTER_CRITICAL(); /* Put the SPI chip select low to start the transaction */ SdkEvalSPICSLow(); for(volatile uint16_t i=0;i<CS_TO_SCLK_DELAY;i++); /* Write the header bytes and read the SPIRIT status bytes */ for(int i=0; i<2; i++) { while (SPI_GetFlagStatus(s_SpiPort, SPI_FLAG_TXE) == RESET); SPI_SendData(s_SpiPort, header[i]); while (SPI_GetFlagStatus(s_SpiPort, SPI_FLAG_RXNE) == RESET); tmpstatus += ((uint16_t)(SPI_ReceiveData(s_SpiPort)))<<((1-i)*8); } /* Read the data from the FIFO according to the number of bytes */ for(int index=0; index<cNbBytes; index++) { while (SPI_GetFlagStatus(s_SpiPort, SPI_FLAG_TXE) == RESET); SPI_SendData(s_SpiPort, dummy); while (SPI_GetFlagStatus(s_SpiPort, SPI_FLAG_RXNE) == RESET); *pcBuffer = SPI_ReceiveData(s_SpiPort); pcBuffer++; } /* To be sure to don't rise the Chip Select before the end of last sending */ while (SPI_GetFlagStatus(s_SpiPort, SPI_FLAG_TXE) == RESET); /* Put the SPI chip select high to end the transaction */ SdkEvalSPICSHigh(); SPI_EXIT_CRITICAL(); return *status; }
/******************************************************************************* * Function Name : SPI_FLASH_SendHalfWord * Description : Sends a Half Word through the SPI interface and return the * Half Word received from the SPI bus. * Input : Half Word : Half Word to send. * Output : None * Return : The value of the received Half Word. *******************************************************************************/ u16 SPI_FLASH_SendHalfWord(u16 HalfWord) { /* Loop while DR register in not emplty */ while(SPI_GetFlagStatus(SPI1, SPI_FLAG_TXE) == RESET); /* Send Half Word through the SPI1 peripheral */ SPI_SendData(SPI1, HalfWord); /* Wait to receive a Half Word */ while(SPI_GetFlagStatus(SPI1, SPI_FLAG_RXNE) == RESET); /* Return the Half Word read from the SPI bus */ return SPI_ReceiveData(SPI1); }
/******************************************************************************************************************* ** 函数名称: void SPI_SendByte() Name: void SPI_SendByte() ** 功能描述: 通过SPI接口发送一个字节 Function: send a byte by SPI interface ** 输 入: INT8U byte: 发送的字节 Input: INT8U byte: the byte that will be send ** 输 出: 无 Output: NULL ********************************************************************************************************************/ void SPI_SendByte(INT8U byte) { /* Wait until the transmit buffer is empty */ while (SPI_GetFlagStatus(SPI1, SPI_FLAG_TXE) == RESET); /* Send the byte */ SPI_SendData(SPI1, byte); }
/** * @brief Write a byte on the SD. * @param Data: byte to send. * @retval None */ uint8_t SD_WriteByte(uint8_t Data) { /*!< Wait until the transmit buffer is empty */ while (SPI_GetFlagStatus(SPI_FLAG_TXE) == RESET) {} /*!< Send the byte */ SPI_SendData(Data); /*!< Wait to receive a byte*/ while (SPI_GetFlagStatus(SPI_FLAG_RXNE) == RESET) {} /*!< Return the byte read from the SPI bus */ return SPI_ReceiveData(); }
/** * @brief Write single or multiple SPIRIT register * @param cRegAddress: base register's address to be write * @param cNbBytes: number of registers and bytes to be write * @param pcBuffer: pointer to the buffer of values have to be written into registers * @retval SPIRIT status */ StatusBytes SdkEvalSpiWriteRegisters(uint8_t cRegAddress, uint8_t cNbBytes, uint8_t* pcBuffer) { uint8_t header[2]; uint16_t tmpstatus = 0x0000; StatusBytes *status=(StatusBytes *)&tmpstatus; /* Built the header bytes */ header[0]=WRITE_HEADER; header[1]=cRegAddress; SPI_ENTER_CRITICAL(); /* Puts the SPI chip select low to start the transaction */ SdkEvalSPICSLow(); for(volatile uint16_t i=0;i<CS_TO_SCLK_DELAY;i++); /* Writes the header bytes and read the SPIRIT status bytes */ for(int i=0; i<2; i++) { while (SPI_GetFlagStatus(s_SpiPort, SPI_FLAG_TXE) == RESET); SPI_SendData(s_SpiPort, header[i]); while (SPI_GetFlagStatus(s_SpiPort, SPI_FLAG_RXNE) == RESET); tmpstatus += ((uint16_t)(SPI_ReceiveData(s_SpiPort)))<<((1-i)*8); } /* Writes the registers according to the number of bytes */ for(int index=0; index<cNbBytes; index++) { while (SPI_GetFlagStatus(s_SpiPort, SPI_FLAG_TXE) == RESET); SPI_SendData(s_SpiPort, pcBuffer[index]); while (SPI_GetFlagStatus(s_SpiPort, SPI_FLAG_RXNE) == RESET); SPI_ReceiveData(s_SpiPort); } /* To be sure to don't rise the Chip Select before the end of last sending */ while (SPI_GetFlagStatus(s_SpiPort, SPI_FLAG_TXE) == RESET); /* Puts the SPI chip select high to end the transaction */ SdkEvalSPICSHigh(); SPI_EXIT_CRITICAL(); return *status; }
void WriteReg(char add, char data) { unsigned char y; ss_low; //lo while (SPI_GetFlagStatus(SPI1,SPI_FLAG_TXE)== RESET); SPI_SendData(SPI1, add); // putcSPI1(add); //SEND ADR BYTE while (SPI_GetFlagStatus(SPI1,SPI_FLAG_RXNE) == RESET); y = SPI_ReceiveData(SPI1); while (SPI_GetFlagStatus(SPI1,SPI_FLAG_TXE)== RESET); SPI_SendData(SPI1, data); //SEND PROXY DATA while (SPI_GetFlagStatus(SPI1,SPI_FLAG_RXNE) == RESET); y = SPI_ReceiveData(SPI1); //getcSPI1(); ss_high; // hi }
/******************************************************************************************************************* ** 函数名称: INT8U SPI_RecByte() Name: INT8U SPI_RecByte() ** 功能描述: 从SPI接口接收一个字节 Function: receive a byte from SPI interface ** 输 入: 无 Input: NULL ** 输 出: 收到的字节 Output: the byte that be received ********************************************************************************************************************/ INT8U SPI_RecByte(void) { INT8U Data = 0; /* Wait until the transmit buffer is empty */ while (SPI_GetFlagStatus(SPI1, SPI_FLAG_TXE) == RESET); /* Send the byte */ SPI_SendData(SPI1, 0xFF); /* Wait until a data is received */ while (SPI_GetFlagStatus(SPI1, SPI_FLAG_RXNE) == RESET); /* Get the received data */ Data = SPI_ReceiveData(SPI1); /* Return the shifted data */ return Data; }
/** * @brief Read a page of the EEPROM. * A page size is 32 bytes. * The pages are 256. * Page 0 address: 0x0000 * Page 1 address: 0x0020 * ... * Page 255 address: 0x1FE0 * @param None * @retval None */ void EepromRead(uint16_t nAddress, uint8_t cNbBytes, uint8_t* pcBuffer) { uint8_t cmd[3]; cmd[0] = EEPROM_CMD_READ; for(uint8_t k=0; k<2; k++) { cmd[k+1] = (uint8_t)(nAddress>>((1-k)*8)); } /* Wait the end of a previous write operation */ EepromWaitEndWriteOperation(); // SPI_ENTER_CRITICAL(); /* Put the SPI chip select low to start the transaction */ EepromSPICSLow(); // for(volatile uint16_t i=0;i<CS_TO_SCLK_DELAY;i++); /* Write the header bytes and read the SPIRIT status bytes */ for(uint8_t i=0; i<3; i++) { while (SPI_GetFlagStatus(s_EepromSpiPort, SPI_FLAG_TXE) == RESET); SPI_SendData(s_EepromSpiPort, cmd[i]); while (SPI_GetFlagStatus(s_EepromSpiPort, SPI_FLAG_RXNE) == RESET); SPI_ReceiveData(s_EepromSpiPort); } /* Read the registers according to the number of bytes */ for(int index=0; index<cNbBytes; index++) { while (SPI_GetFlagStatus(s_EepromSpiPort, SPI_FLAG_TXE) == RESET); SPI_SendData(s_EepromSpiPort, 0xFF); while (SPI_GetFlagStatus(s_EepromSpiPort, SPI_FLAG_RXNE) == RESET); *pcBuffer = SPI_ReceiveData(s_EepromSpiPort); pcBuffer++; } while (SPI_GetFlagStatus(s_EepromSpiPort, SPI_FLAG_TXE) == RESET); /* Put the SPI chip select high to end the transaction */ EepromSPICSHigh(); // SPI_EXIT_CRITICAL(); }
uint16_t GYACC_txrx(bool which, uint16_t data) { const uint32_t pin = which ? 0x80 : 0x40; GPIOD->ODR &= ~pin; SPI3->DR = data; while(SPI_GetFlagStatus(SPI3, SPI_I2S_FLAG_BSY)); GPIOD->ODR |= pin; return SPI3->DR; }
/** * @brief Set the internal WEL flag to allow write operation. * @param None * @retval None */ void EepromWriteEnable(void) { /* Put the SPI chip select low to start the transaction */ EepromSPICSLow(Get_vectEepromSpiCsPort(),Get_vectEepromSpiCsPin()); /* Send command */ while (SPI_GetFlagStatus(Get_EepromSpiPort(), SPI_FLAG_TXE) == RESET); SPI_SendData(Get_EepromSpiPort(), EEPROM_CMD_WREN); while (SPI_GetFlagStatus(Get_EepromSpiPort(), SPI_FLAG_RXNE) == RESET); SPI_ReceiveData(Get_EepromSpiPort()); while (SPI_GetFlagStatus(Get_EepromSpiPort(), SPI_FLAG_TXE) == RESET); /* Put the SPI chip select high to end the transaction */ EepromSPICSHigh(Get_vectEepromSpiCsPort(),Get_vectEepromSpiCsPin()); }/* end EepromWriteEnable() */
/** * @brief Read a byte from the SD. * @param None * @retval The received byte. */ uint8_t SD_ReadByte(void) { uint8_t Data = 0; /*!< Wait until the transmit buffer is empty */ while (SPI_GetFlagStatus(SPI_FLAG_TXE) == RESET) {} /*!< Send the byte */ SPI_SendData(SD_DUMMY_BYTE); /*!< Wait until a data is received */ while (SPI_GetFlagStatus(SPI_FLAG_RXNE) == RESET) {} /*!< Get the received data */ Data = SPI_ReceiveData(); /*!< Return the shifted data */ return Data; }
void LCD_flush_buf_to_lcd(void) { uint8_t i; SPI_STB_LOW; SPI->DR = 0x40; //write data to LCD ram/ auto increment while(SPI_GetFlagStatus(SPI_FLAG_BSY)); SPI_STB_HIGH; SPI_STB_LOW; SPI->DR = 0xC2; //begin at address 0x02 while(SPI_GetFlagStatus(SPI_FLAG_BSY)); for(i=2; i<15; i++){ SPI->DR = _lcd_buf[i]; while(SPI_GetFlagStatus(SPI_FLAG_BSY)); } SPI_STB_HIGH; }
void LCD_to_default_config(void) { SPI_to_default_config(); while(SPI_GetFlagStatus(SPI_FLAG_BSY)); SPI_STB_HIGH; SPI->DR = 0x00; while(SPI_GetFlagStatus(SPI_FLAG_BSY)); SPI_STB_HIGH; SPI_STB_HIGH; SPI_STB_HIGH; SPI_STB_LOW; SPI->DR = 0x97; while(SPI_GetFlagStatus(SPI_FLAG_BSY)); SPI_STB_HIGH; SPI_STB_HIGH; SPI_STB_HIGH; }