// -------------------------------------------------------------------------- static void nrf24_enableFeatures(void) { NRF24_CSN_LOW(); spiSendByte(spi, NRF24_CMD_ACTIVATE); spiSendByte(spi, 0x73); NRF24_CSN_HIGH(); }
// Reading the contents of the CSD and CID registers in SPI mode is a simple // read-block transaction. char mmcReadRegister (const char cmd_register, const unsigned char length, unsigned char *pBuffer) { char uc; char rvalue = MMC_TIMEOUT_ERROR; if (mmcSetBlockLength (length) == MMC_SUCCESS) { MMC_CS_LOW (); mmcSendCmd(cmd_register, 0x000000, 0xff); // CRC not used: 0xff as last byte if (mmcGetResponse() == 0x00) { // Wait for R1 response (0x00 = OK) if (mmcGetXXResponse(0xfe)== 0xfe) for (uc = 0; uc < length; uc++) pBuffer[uc] = spiSendByte(DUMMY_CHAR); // get CRC bytes (not really needed by us, but required by MMC) spiSendByte(DUMMY_CHAR); spiSendByte(DUMMY_CHAR); rvalue = MMC_SUCCESS; } else rvalue = MMC_RESPONSE_ERROR; MMC_CS_HIGH (); spiSendByte(DUMMY_CHAR); // Send 8 Clock pulses of delay. } MMC_CS_HIGH (); return rvalue; } // mmc_read_register
// Reading the contents of the CSD and CID registers in SPI mode is a simple // read-block transaction. unsigned char mmcReadRegister (const char cmd_register, const unsigned char length, unsigned char *pBuffer) { unsigned char uc = 0; unsigned char rvalue = MMC_TIMEOUT_ERROR; if (mmcSetBlockLength (length) == MMC_SUCCESS) { CS_LOW (); // CRC not used: 0xff as last byte mmcSendCmd(cmd_register, 0x000000, 0xff); // wait for response // in the R1 format (0x00 is no errors) if (mmcGetResponse() == 0x00) { if (mmcGetXXResponse(0xfe)== 0xfe) for (uc = 0; uc < length; uc++) pBuffer[uc] = spiSendByte(0xff); //mmc_buffer[uc] = spiSendByte(0xff); // get CRC bytes (not really needed by us, but required by MMC) spiSendByte(0xff); spiSendByte(0xff); rvalue = MMC_SUCCESS; } else rvalue = MMC_RESPONSE_ERROR; // CS = HIGH (off) CS_HIGH (); // Send 8 Clock pulses of delay. spiSendByte(0xff); } CS_HIGH (); return rvalue; } // mmc_read_register
unsigned char mmc_GoIdle() { unsigned char response=0x01; CS_LOW(); //Send Command 0 to put MMC in SPI mode mmcSendCmd(MMC_GO_IDLE_STATE,0,0x95); //Now wait for READY RESPONSE if((mmc_error = mmcGetResponse())!=0x01) { CS_HIGH(); return MMC_INIT_ERROR; } while(response==0x01) { CS_HIGH(); spiSendByte(0xff); CS_LOW(); mmcSendCmd(MMC_SEND_OP_COND,0x00,0xff); response=mmcGetResponse(); } CS_HIGH(); spiSendByte(0xff); return MMC_SUCCESS; }
unsigned char mmcCheckBusy(void) { //Response comes 1-8bytes after command //the first bit will be a 0 //followed by an error code //data will be 0xff until response int i=0; unsigned char response; unsigned char rvalue; while(i<=64) { response=spiSendByte(0xff); response &= 0x1f; switch(response) { case 0x05: rvalue=MMC_SUCCESS;break; case 0x0b: return(MMC_CRC_ERROR); case 0x0d: return(MMC_WRITE_ERROR); default: rvalue = MMC_OTHER_ERROR; break; } if(rvalue==MMC_SUCCESS)break; i++; } i=0; do { response=spiSendByte(0xff); i++; }while(response==0); return response; }
void mmcUnmountBlock(void) { // get CRC bytes (not really needed by us, but required by MMC) spiSendByte(0xff); spiSendByte(0xff); CS_HIGH (); spiSendByte(0xff); }
// -------------------------------------------------------------------------- static void nrf24_writeRegister(UINT8 reg, BYTE *data, UINT8 len) { NRF24_CSN_LOW(); spiSendByte(spi, NRF24_CMD_W_REGISTER | (reg & NRF24_REG_MASK)); while (len > 0) { spiSendByte(spi, *data++); --len; } NRF24_CSN_HIGH(); }
u8 flashReadStateCom( void ) { u8 status; FILECS_LOW; //使能设备 spiSendByte(READ_STA_COM); //读状态寄存器 status = spiSendByte(0x00); //取得返回的数据 FILECS_HIGH; //禁能设备 return status; }
unsigned char nrfActivate() { unsigned char status; RADIO_EN_CS(); status = spiSendByte(CMD_ACTIVATE); spiSendByte(ACTIVATE_DATA); RADIO_DIS_CS(); return status; }
void spiFlashBufferRead(u8 *buff, u32 addr, u32 readNum) { FILECS_LOW; //使能设备 spiSendByte(READ_DATA_COM); //发送读数据命令 spiSendByte(addr>>16); //发送地址 spiSendByte(addr>>8); //发送地址 spiSendByte(addr); //发送地址 for (u32 i=0; i<readNum; i++){ *buff=spiSendByte(0x00); buff++; } FILECS_HIGH; }
u32 spiFlashReadID(void) { u32 Temp = 0, Temp0 = 0, Temp1 = 0, Temp2 = 0; FILECS_LOW; spiSendByte(JEDEC_DEV_ID_COM); Temp0 = spiSendByte(0); Temp1 = spiSendByte(0); Temp2 = spiSendByte(0); FILECS_HIGH; Temp = (Temp0 << 16) | (Temp1 << 8) | Temp2; return Temp; }
unsigned int mmcGetR2Response(void) { //Response comes 1-8bytes after command //the first bit will be a 0 //followed by an error code //data will be 0xff until response int i; unsigned char responseHi, responseLo; for(i=0; i<=64; i++) { responseHi=spiSendByte(DUMMY_CHAR); if(responseHi!=0xFF) break; } responseLo=spiSendByte(DUMMY_CHAR); return ((unsigned int)(responseHi << 8) | responseLo); }
char mmcMountBlock(unsigned long address) { char rvalue = MMC_RESPONSE_ERROR; // Set the block length to read if (mmcSetBlockLength (512) == MMC_SUCCESS) // block length could be set { // SS = LOW (on) CS_LOW (); // send read command MMC_READ_SINGLE_BLOCK=CMD17 mmcSendCmd (MMC_READ_SINGLE_BLOCK, address, 0xFF); // Send 8 Clock pulses of delay, check if the MMC acknowledged the read block command // it will do this by sending an affirmative response // in the R1 format (0x00 is no errors) if (mmcGetResponse() == 0x00) { // now look for the data token to signify the start of // the data if (mmcGetXXResponse(MMC_START_DATA_BLOCK_TOKEN) == MMC_START_DATA_BLOCK_TOKEN) { //success, data ready to read rvalue = MMC_SUCCESS; } else { // the data token was never received rvalue = MMC_DATA_TOKEN_ERROR; // 3 CS_HIGH (); spiSendByte(0xff); } } else { // the MMC never acknowledge the read command rvalue = MMC_RESPONSE_ERROR; // 2 CS_HIGH (); spiSendByte(0xff); } } else { rvalue = MMC_BLOCK_SET_ERROR; // 1 CS_HIGH (); spiSendByte(0xff); } return rvalue; }// mmc_read_block
// Initialize MMC card unsigned char initMMC (void) { //raise SS and MOSI for 80 clock cycles //SendByte(0xff) 10 times with SS high //RAISE SS int i; // Port 5 Function Dir On/Off // 5.1-Dout Out 0 - off 1 - On -> init in SPI_Init // 5.2-Din Inp 0 - off 1 - On -> init in SPI_Init // 5.3-Clk Out - -> init in SPI_Init // 5.0-mmcCS Out 0 - Active 1 - none Active /* P5SEL |= 0x0E; P5SEL &= ~0x11; P5OUT |= 0x11; P5DIR |= 0x1B; */ /* PIN_SET(port5_attr.sel, 0x0e, PIN_HI); PIN_CLEAR(port5_attr.sel, 0x01); PIN_SET(port5_attr.dir, 0x0B, PIN_HI); PIN_CLEAR(port5_attr.dir, 0x04); port_set_attr(5, 0x0f, &port5_attr); port_write(5, 0x01, PIN_HI); */ // PIN_SET(port5_attr.sel, 0x0e, PIN_HI); #ifndef PORT_WRITE mmcPower(MMC_ON); MMC_PWR_PDIR |= MMC_CARD_PWR; MMC_PWR_PSEL &= ~MMC_CARD_PWR; // MMC_POUT |= MMC_CS; CS_HIGH(); MMC_PDIR |= MMC_CS; MMC_PSEL &= ~MMC_CS; #else // !PORT_WRITE PIN_SET (mmc_pwr_port_attr.dir, MMC_CARD_PWR, PIN_HI); PIN_CLEAR(mmc_pwr_port_attr.sel, MMC_CARD_PWR); port_set_attr(MMC_PWR_PORT, MMC_CARD_PWR, &mmc_pwr_port_attr); mmcPower(MMC_ON); PIN_CLEAR(mmc_port_attr.sel, MMC_CS); PIN_SET (mmc_port_attr.dir, MMC_CS, PIN_HI); port_set_attr(MMC_PORT, MMC_CS, &mmc_port_attr); CS_HIGH(); //port_write(5, 0x01, PIN_HI); #endif // !PORT_WRITE // initSPI(); // SPI_init(); //initialization sequence on PowerUp // CS_HIGH(); for(i=0;i<=9;i++) spiSendByte(0xff); mmc_error = MMC_SUCCESS; return (mmc_GoIdle()); }
// Initialize MMC card //set DI and CS high and apply more than 74 pulses to SCLK char initMMC (void){ int i; CS_HIGH(); //sey CS high for(i=0;i<11;i++) spiSendByte(0xff);//set DI high with ff (10 times) return (mmc_GoIdle()); }
/* Write len bytes a nRF24L register. 5 Bytes max */ unsigned char nrfWriteReg(unsigned char address, char *buffer, int len) { unsigned char status; int i; RADIO_EN_CS(); /* Send the write command with the address */ status = spiSendByte( CMD_W_REG | (address&0x1F) ); /* Write LEN bytes */ for(i=0; i<len; i++) spiSendByte(buffer[i]); RADIO_DIS_CS(); return status; }
/* Sent the NOP command. Used to get the status byte */ unsigned char nrfNop() { unsigned char status; RADIO_EN_CS(); status = spiSendByte(CMD_NOP); RADIO_DIS_CS(); return status; }
// Write the ack payload of the pipe 0 unsigned char nrfWriteAck(unsigned int pipe, char *buffer, int len) { unsigned char status; int i; ASSERT(pipe<6); RADIO_EN_CS(); /* Send the read command with the address */ status = spiSendByte(CMD_W_ACK_PAYLOAD(pipe)); /* Read LEN bytes */ for(i=0; i<len; i++) spiSendByte(buffer[i]); RADIO_DIS_CS(); return status; }
unsigned char nrfFlushTx() { unsigned char status; RADIO_EN_CS(); status = spiSendByte(CMD_FLUSH_TX); RADIO_DIS_CS(); return status; }
u8 flashEraseSector(u32 sector) { u32 sectorAddr = ((u32)sector << 12); u8 timeout=TIMEOUT; flashWriteEnable(); //开启写使能 FILECS_LOW; //使能设备 spiSendByte(SECTOR_ERA_COM); //扇区擦除 spiSendByte((u8)(sectorAddr>>16)); //发送地址 spiSendByte((u8)(sectorAddr>>8)); //发送地址 spiSendByte((u8)(sectorAddr)); //发送地址 FILECS_HIGH; //禁能设备 while (flashReadStateCom()&STATUS_WIP){ atomTimerDelay (1); if (0==(timeout--))break; } return 0x01; }
// -------------------------------------------------------------------------- static UINT8 nrf24_writePayload(BYTE* buffer, UINT8 len, BOOL ack) { UINT8 fill; UINT8 tmp_len = len; if (len > NRF24_PAYLOAD_SIZE_MAX) len = NRF24_PAYLOAD_SIZE_MAX; fill = NRF24_PAYLOAD_SIZE_MAX - len; NRF24_CSN_LOW(); spiSendByte(spi, (ack)?(NRF24_CMD_W_TX_PAYLOAD):(NRF24_CMD_W_TX_PAYLOAD_NOACK)); while (tmp_len--) spiSendByte(spi, *buffer++); while (fill--) spiSendByte(spi, 0x00); NRF24_CSN_HIGH(); return (len); }
// set blocklength 2^n char mmcSetBlockLength (const unsigned long blocklength) { char response; MMC_CS_LOW (); mmcSendCmd(MMC_SET_BLOCKLEN, blocklength, 0xFF); // Set the block length to read response=mmcGetResponse(); // Test response = 0x00 (R1 OK format) MMC_CS_HIGH (); spiSendByte(DUMMY_CHAR); // Send 8 Clock pulses of delay. return response; } // Set block_length
// -------------------------------------------------------------------------- static UINT8 nrf24_readPayload(BYTE* buffer, UINT8 len) { UINT8 payload_size = nrf24_getRxPayloadLength(); UINT8 fill; UINT8 tmp_len = len; if (len > payload_size) len = payload_size; fill = payload_size - len; NRF24_CSN_LOW(); spiSendByte(spi, NRF24_CMD_R_RX_PAYLOAD); while (tmp_len--) *buffer++ = spiTransferByte(spi, NRF24_CMD_NOP); while (fill--) spiSendByte(spi, NRF24_CMD_NOP); NRF24_CSN_HIGH(); return (len); }
// -------------------------------------------------------------------------- static UINT8 nrf24_getRxPayloadLength(void) { UINT8 len; NRF24_CSN_LOW(); spiSendByte(spi, NRF24_CMD_R_RX_PL_WID); len = spiTransferByte(spi, NRF24_CMD_NOP); NRF24_CSN_HIGH(); return (len); }
// Return the payload length unsigned char nrfRxLength(unsigned int pipe) { unsigned char length; RADIO_EN_CS(); spiSendByte(CMD_RX_PL_WID); length = spiReceiveByte(); RADIO_DIS_CS(); return length; }
// static functions // -------------------------------------------------------------------------- static void nrf24_readRegister(UINT8 reg, BYTE *data, UINT8 len) { NRF24_CSN_LOW(); spiSendByte(spi, NRF24_CMD_R_REGISTER | (reg & NRF24_REG_MASK)); while (len > 0) { *data++ = spiTransferByte(spi, NRF24_CMD_NOP); --len; } NRF24_CSN_HIGH(); }
// Check if MMC card is still busy char mmcCheckBusy(void) { //Response comes 1-8bytes after command //the first bit will be a 0 //followed by an error code //data will be 0xff until response int i; char response, rvalue; for(i=0; i<=64; i++) { response=spiSendByte(DUMMY_CHAR); response &= 0x1f; switch(response) { case 0x05: rvalue=MMC_SUCCESS; break; case 0x0b: return(MMC_CRC_ERROR); case 0x0d: return(MMC_WRITE_ERROR); default: rvalue = MMC_OTHER_ERROR; break; } if(rvalue==MMC_SUCCESS) break; } i=0; do { // ADDRESS THIS ISSUE, WE GET HUNG HERE AND WATCHDOG AS A RESULT!!!!!!!!!!!!!!!!! response = spiSendByte(DUMMY_CHAR); i++; }while(response==0 && i<=10000); // I HAVE NO IDEA WHAT TO MAKE THIS TIMEOUT ACTUALLY BE!!! if(response==0) { rvalue = MMC_TIMEOUT_ERROR; } return rvalue; }
/* * flash最多写一个page,256字节 * *buf数据指针 * pageAddr写入地址 (不一定是page头) * writeNum写入数据byte量 */ void flashWritePage(u8 *buf, u32 pageAddr, u16 writeNum) { u8 timeout=TIMEOUT; flashWriteEnable(); FILECS_LOW; //使能设备 spiSendByte(PAGE_PRO_COM); //发送写数据命令 spiSendByte((u8)(pageAddr>>16)); //发送地址 spiSendByte((u8)(pageAddr>>8)); //发送地址 spiSendByte((u8)(pageAddr)); //发送地址 for (u16 i=0; i<writeNum; i++){ spiSendByte(*buf); //数据发送 buf++; } FILECS_HIGH; while (flashReadStateCom()&STATUS_WIP){ atomTimerDelay (1); if (0==(timeout--))break; } }
char mmcGetXXResponse(const char resp) { //Response comes 1-8bytes after command //the first bit will be a 0 //followed by an error code //data will be 0xff until response int i; char response; for(i=0; i<=1000; i++) { response=spiSendByte(DUMMY_CHAR); if(response==resp)break; } return response; }
int read_sensor_data(unsigned int *xxl,unsigned int *yxl,unsigned int *zxl){ //int read_sensor_data(int *xxl,int *yxl,int *zxl){ // static unsigned char status=0; static unsigned char acc_data[6]; //check new data available /* while((status&=0x0f)==0) { PORTB&=~(_BV(PB0)); spiSendByte(0xa7); status=spiTransferByte(0xFF); PORTB |= _BV(PB0); } */ //sensor data multiple read PORTC.OUT = PORTC.OUT & 0xfe;//Pc0을 0으로 만드는거 spiSendByte(0xe8); SPI_MasterTransceiveByte(&spiMasterC, 0xe8); SPI_MasterTransceiveByte(&spiMasterC, 0xff); acc_data[0]=spiTransferByte(0xFF); acc_data[1]=spiTransferByte(0xFF); acc_data[2]=spiTransferByte(0xFF); acc_data[3]=spiTransferByte(0xFF); acc_data[4]=spiTransferByte(0xFF); acc_data[5]=spiTransferByte(0xFF); PORTC.OUT = PORTC.OUT | 0x01;//pc1을 1로 만드는거 acc_data[1]=acc_data[1]+0x80; acc_data[3]=acc_data[3]+0x80; acc_data[5]=acc_data[5]+0x80; *xxl= (unsigned int)acc_data[1]*256+acc_data[0]; *yxl= (unsigned int)acc_data[3]*256+acc_data[2]; *zxl= (unsigned int)acc_data[5]*256+acc_data[4]; // *xxl= (int)acc_data[1]*256+acc_data[0]; // *yxl= (int)acc_data[3]*256+acc_data[2]; // *zxl= (int)acc_data[5]*256+acc_data[4]; return 0; }