/**************************************************** SpiTsRead ****************************************************/ int SpiTsRead(struct DibBridgeContext *pContext,unsigned int addr,unsigned char* buf, unsigned int size) { unsigned char command[6]; unsigned int commandsize; /****** Build Command ****** - b31: 0 => Start Bit - b30: 0 => Read - b29-28: => Transfer Size - b27: => Auto Increment - b26-b0: => Formatted Address - b15-b0: => Transfer Size ***************************/ command[0] = (addr & 0x3F000000) >> 24; command[1] = (addr & 0x00FF0000) >> 16; command[2] = (addr & 0x0000FF00) >> 8; command[3] = (addr & 0x000000FF) >> 0; commandsize = size; command[4] = (commandsize & 0xFF00) >> 8; command[5] = (commandsize & 0x00FF); /** Write Command **/ SpiWrite(pContext,command,6); SpiRead(pContext,buf,size); return 0; }
//===================================================== // nRF905 的检测程序,首先写入地址,然后再读出地址, // 如果读出来的值与原来的一样,那么就返回1 ,否则返回0 //===================================================== u8 nRF905_Check(void) { u8 i,AddressTemp[4]={0}; CSN = 0;//SPI valid SpiWrite(WTA);//写地址 for(i=0;i<4;i++) { SpiWrite(TxRxAddress[i]); } CSN = 1; delay_us(500); CSN = 0; SpiWrite(RTA);//读地址 for(i=0;i<4;i++) { AddressTemp[i]=SpiRead(); } CSN = 1; for(i=0;i<4;i++) { if(AddressTemp[i] != TxRxAddress[i]) { break; } } if(i!=4) return 0; return 1; }
DRESULT disk_read ( BYTE drv, /* Physical drive nmuber (0) */ BYTE *buff, /* Pointer to the data buffer to store read data */ DWORD sector, /* Start sector number (LBA) */ BYTE count /* Sector count (1..255) */ ) { DRESULT res; uint32_t size; if (drv) { res = (DRESULT)STA_NOINIT; return res; } if(count==0||count>=2) { res = (DRESULT)STA_NOINIT; return res; } size = count*512; if(bUseSDCard) SpiRead(sector, size, buff); else spiFlashReadData(sector*512, size, buff); res = RES_OK; /* Clear STA_NOINIT */; return res; }
unsigned char enc28j60ReadOp(unsigned char op, unsigned char address) { unsigned char dat = 0; ENC28J60_CSL(); SpiWrite(op | (address & ADDR_MASK)); dat =SpiRead(); // do dummy read if needed (for mac and mii, see datasheet page 29) if(address & 0x80) { dat =SpiRead(); } // release CS ENC28J60_CSH(); return dat; }
__attribute__((always_inline)) static inline void SpiReceive(struct spi_periph *p, struct spi_transaction *t) { while (bit_is_set(((sspRegs_t *)(p->reg_addr))->sr, RNE)) { if (p->rx_idx_buf < t->input_length) { uint16_t r; SpiRead(p, &r); if (t->dss == SPIDss8bit) { t->input_buf[p->rx_idx_buf] = (uint8_t)r; } else if (t->dss == SPIDss16bit) { t->input_buf[2 * p->rx_idx_buf] = (uint8_t)r; t->input_buf[2 * p->rx_idx_buf + 1] = (uint8_t)(r >> 8); } p->rx_idx_buf++; } else {
//===================================================== //使用905接收数据 // //===================================================== void RxPacket(void) { unsigned char i; TRX_CE = 0; //设置905进入待机模式 CSN = 0; SpiWrite(RRP); //准备读取接收到的数据 for(i=0;i<32;i++) { RxBuf[i] = SpiRead(); //通过SPI口从905读取数据 } CSN = 1; while(DR||AM); TRX_CE = 1; }
void enc28j60ReadBuffer(unsigned int len, unsigned char* data) { ENC28J60_CSL(); // issue read command SpiWrite(ENC28J60_READ_BUF_MEM); while(len) { len--; // read data *data = SpiRead(); data++; } *data='\0'; ENC28J60_CSH(); }
void RxPacket(void) { uchar i; TRX_CE=0; // Set nRF905 in standby mode CSN=0; // Spi enable for write a spi command SpiWrite(RRP); // Read payload command for (i=0;i<32;i++) { RxBuf[i]=SpiRead(); // Read data and save to buffer } CSN=1; // Disable spi while(DR||AM) //等待... /////////////如果接收不到程序将停滞 数据准备就绪以及地址匹配 ; //即如果没有匹配的905模块时,这一句需要注释掉 TRX_CE=1; //允许发生或接收 ?????????????????????????? if(RxBuf[0] == 1) { LED1 = 0 ; } /* if(RxBuf[1] == 1) { LED2 = 0 ; } if(RxBuf[2] == 1) { LED3 = 0 ; } if(RxBuf[3] == 1) { LED4 = 0 ; }*/ Delay(500); // ...light led LED1 = 1 ; LED2 = 1 ; LED3 = 1 ; LED4 = 1 ; }