BYTE SD_WriteOneSector(DWORD sector,BYTE* buf){//写一个扇区(buf为512字节),成功返回0 BYTE rtn; WORD i,cnt=0; SPI_SetSpeedHigh();//设定SPI到高速模式 rtn=SD_SendCommand(24,sector<<9);//读命令 if(rtn!=0x00) return rtn; CS_LOW(); SPI_ByteRW(0xFF); SPI_ByteRW(0xFF); SPI_ByteRW(0xFF); SPI_ByteRW(0xFE);//发开始符 for(i=0;i<512;i++){//送512字节数据 SPI_ByteRW(*(buf++)); } SPI_ByteRW(0xFF);//dummy crc SPI_ByteRW(0xFF); rtn=SPI_ByteRW(0xFF); if((rtn & 0x1F)!=0x05){//等待是否成功 CS_HIGH(); return rtn; } //等待操作完 while(!SPI_ByteRW(0xFF)){ if((cnt++)==0xFFFF){ CS_HIGH(); return 1;//超时退出 } } CS_HIGH(); SPI_ByteRW(0xFF);//额外8个时钟周期 return 0; }
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; }
// 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
// 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()); }
char nrf_snd_pkt_crc_encr(int size, uint8_t * pkt, uint32_t const key[4]){ if(size > MAX_PKT) size=MAX_PKT; nrf_write_reg(R_CONFIG, R_CONFIG_PWR_UP| // Power on R_CONFIG_EN_CRC // CRC on, single byte ); // nrf_write_long(C_W_TX_PAYLOAD,size,pkt); uint16_t crc=crc16(pkt,size-2); pkt[size-2]=(crc >>8) & 0xff; pkt[size-1]=crc & 0xff; if(key !=NULL) xxtea_encode_words((uint32_t*)pkt,size/4,key); CS_LOW(); xmit_spi(C_W_TX_PAYLOAD); sspSend(0,pkt,size); CS_HIGH(); CE_HIGH(); delayms(1); // Send it. (only needs >10ys, i think) CE_LOW(); return nrf_cmd_status(C_NOP); };
void nrf_read_pkt_crc(int len, uint8_t* data, uint8_t* crc){ CS_LOW(); xmit_spi(C_R_RX_PAYLOAD); sspReceive(0,data,len); sspReceive(0,crc,2); CS_HIGH(); };
void sd_write(char __flash *str, unsigned long int bpos) { CS_LOW(); spi_sendbyte(0xFF); //converting bpos to 4 bytes and set write-start position spi_sendbyte(0x58); spi_sendbyte(((bpos & 0xFF000000)>>24)); spi_sendbyte(((bpos & 0x00FF0000)>>16)); spi_sendbyte(((bpos & 0x0000FF00)>>8)); spi_sendbyte(((bpos & 0x000000FF))); spi_sendbyte(0x00); //CRC while (spi_sendbyte(0xFF)!=0x0); spi_sendbyte(0xFF); spi_sendbyte(0xFE); _spi_sendtext(str); //str len up to 512 bytes spi_sendbyte(0x00); //1-st byte CRC spi_sendbyte(0x00); //2-nd byte CRC while (spi_sendbyte(0xFF)&0x5 != 0x5); //waiting for the command reception signal while (spi_sendbyte(0xFF)!=0x00); //waiting for the command reception signal test(2); CS_HIGH(); }
// TODO: This function will not exit gracefully if SD card does not do what it should void SD_read(unsigned long sector, unsigned short offset, unsigned char * buffer, unsigned short len) { unsigned short i, pos = 0; CS_LOW(); SPI_send(0x51); // sector * 512 = sector << 9 SPI_send(sector>>15); // sector*512 >> 24 SPI_send(sector>>7); // sector*512 >> 16 SPI_send(sector<<1); // sector*512 >> 8 SPI_send(0); // sector*512 SPI_send(0xFF); for(i=0; i<10 && SPI_send(0xFF) != 0x00; i++) {} // wait for 0 for(i=0; i<10 && SPI_send(0xFF) != 0xFE; i++) {} // wait for data start for(i=0; i<offset; i++) // "skip" bytes SPI_send(0xFF); for(i=0; i<len; i++) // read len bytes buffer[i] = SPI_send(0xFF); for(i+=offset; i<512; i++) // "skip" again SPI_send(0xFF); // skip checksum SPI_send(0xFF); SPI_send(0xFF); CS_HIGH(); }
uint8_t nrf_cmd_status(uint8_t cmd) { CS_LOW(); sspSendReceive(&cmd, 1); CS_HIGH(); return cmd; };
void nrf_read_pkt(int len, uint8_t* data) { CS_LOW(); sspSendByte(C_R_RX_PAYLOAD); sspReceive(data,len); CS_HIGH(); };
uint16_t max1168_read_adc(uint8_t reg, enum max1168_clk clk, enum max1168_mode mode) { #define CH2 0x20 uint16_t raw_data; max1168_init(); if (mode == MODE_8BIT) PORTB &= ~(1<<DSEL); else PORTB |= (1<<DSEL); CS_LOW(); /* TODO: remove hardcoded channel */ max1168_xfer_byte(CH2 | clk); if (mode == MODE_16BIT) max1168_xfer_byte(SPI_DUMMY_BYTE); if (clk == CLK_INTERNAL) while ((PINB & (1<<EOC))); raw_data = max1168_xfer_byte(SPI_DUMMY_BYTE) << 8; raw_data |= max1168_xfer_byte(SPI_DUMMY_BYTE); CS_HIGH(); return raw_data; }
STATIC mp_obj_t irq_callback(mp_obj_t line) { DEBUG_printf("<< IRQ; state=%lu >>\n", sSpiInformation.ulSpiState); switch (sSpiInformation.ulSpiState) { case eSPI_STATE_POWERUP: /* This means IRQ line was low call a callback of HCI Layer to inform on event */ DEBUG_printf(" - POWERUP\n"); sSpiInformation.ulSpiState = eSPI_STATE_INITIALIZED; break; case eSPI_STATE_IDLE: DEBUG_printf(" - IDLE\n"); sSpiInformation.ulSpiState = eSPI_STATE_READ_IRQ; /* IRQ line goes down - we are start reception */ CS_LOW(); // Wait for TX/RX Compete which will come as DMA interrupt SpiReadHeader(); sSpiInformation.ulSpiState = eSPI_STATE_READ_EOT; SSIContReadOperation(); break; case eSPI_STATE_WRITE_IRQ: DEBUG_printf(" - WRITE IRQ\n"); SpiWriteDataSynchronous(sSpiInformation.pTxPacket, sSpiInformation.usTxPacketLength); sSpiInformation.ulSpiState = eSPI_STATE_IDLE; CS_HIGH(); break; } return mp_const_none; }
DRESULT dataflash_random_read(BYTE *buff, DWORD offset, DWORD length) { if (!length) return RES_PARERR; if (status & STA_NOINIT) return RES_NOTRDY; if (offset+length > MAX_PAGE*256) return RES_PARERR; do { wait_for_ready(); DWORD pageaddr = ((offset/256) << 9) | (offset%256); DWORD remaining = 256 - offset%256; if (remaining > length) { remaining = length; } length -= remaining; offset += remaining; CS_LOW(); xmit_spi(OP_PAGEREAD); xmit_spi((BYTE)(pageaddr >> 16)); xmit_spi((BYTE)(pageaddr >> 8)); xmit_spi((BYTE)pageaddr); xmit_spi(0x00); // follow up with 4 don't care bytes xmit_spi(0x00); xmit_spi(0x00); xmit_spi(0x00); do { rcvr_spi_m(buff++); } while (--remaining); CS_HIGH(); } while (length); return length ? RES_ERROR : RES_OK; }
void fm25_hw_init() { int i = 0xFFFFF; fm25_spi_cfg(); while(i--); //spi_config(); CS_LOW(); spi_readwrite( FM25_WRDI ); CS_HIGH(); spi_flash_device.type = RT_Device_Class_Block; spi_flash_device.init = fm25_init; spi_flash_device.open = fm25_open; spi_flash_device.close = fm25_close; spi_flash_device.read = fm25_read; spi_flash_device.write = fm25_write; spi_flash_device.control = fm25_control; /* no private */ spi_flash_device.user_data = RT_NULL; rt_device_register(&spi_flash_device, "fram0", RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE); }
rt_size_t fm25_read(rt_device_t dev, rt_off_t offset, void * buf, rt_size_t size) { uint32_t index; uint8_t *buffer = (uint8_t*) buf; fram_lock(); //spi_config(); //rt_kprintf("READ: %d, size=%d\n", offset, size); CS_LOW(); spi_readwrite( FM25_READ); spi_readwrite( (offset >> 8)&0xFF ); spi_readwrite( offset & 0xFF ); for(index=0; index<size; index++) { *buffer++ = spi_readwrite(0xFF); if( spi_timeout_cnt > 0 ) { fram_unlock(); spi_timeout_cnt = 0; rt_kprintf("Read time out\n"); return -1; } offset++; } CS_HIGH(); fram_unlock(); return size; }
void nrf_write_reg(const uint8_t reg, const uint8_t val) { CS_LOW(); sspSendByte(C_W_REGISTER | reg); sspSendByte(val); CS_HIGH(); };
void finit3310( void ) { CS_HIGH(); RST_HIGH(); DC_HIGH(); delay3310( 100000 ); }
void nrf_write_long(const uint8_t cmd, int len, const uint8_t* data) { CS_LOW(); sspSendByte(cmd); sspSend(data,len); CS_HIGH(); };
char SD_initialize(void) { char i; cli(); // ]r:10 CS_HIGH(); for(i=0; i<10; i++) // idle for 1 bytes / 80 clocks SPI_send(0xFF); // [0x40 0x00 0x00 0x00 0x00 0x95 r:8] until we get "1" for(i=0; i<10 && SD_command(0x40, 0x00000000, 0x95, 8) != 1; i++) { delay_ms(100); } if(i == 10) // card did not respond to initialization return CARD_NOT_INIT; // CMD1 until card comes out of idle, but maximum of 10 times for(i=0; i<10 && SD_command(0x41, 0x00000000, 0xFF, 8) != 0; i++) { delay_ms(100); } if(i == 10) // card did not come out of idle return CARD_IN_IDLE; // SET_BLOCKLEN to 512 SD_command(0x50, 0x00000200, 0xFF, 8); sd_sector = sd_pos = 0; return CARD_INIT; }
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
void mmcUnmountBlock(void) { // get CRC bytes (not really needed by us, but required by MMC) spiSendByte(0xff); spiSendByte(0xff); CS_HIGH (); spiSendByte(0xff); }
uint8_t nrf_read_reg(const uint8_t reg){ uint8_t val; CS_LOW(); xmit_spi(C_R_REGISTER | reg); rcv_spi(&val); CS_HIGH(); return val; };
void SPI_initializeMaster(void) { PORTB |= (1 << SCK) | (1 << DO); //Configure SCK/MOSI/CS as output CS_HIGH(); DDRB |= (1 << SCK) | (1 << DO); DDRB &= ~(1 << DI); //MISO is input DDRD |= (1 << PD6); USICR = (1 << USIWM0) | (1 << USICS1); //spi mode | clock source is positive edge using USCK pin }
void nrf_read_long(const uint8_t cmd, int len, uint8_t* data){ CS_LOW(); xmit_spi(cmd); for(int i=0;i<len;i++) data[i] = 0x00; sspSendReceive(0,data,len); CS_HIGH(); };
void SpiOpen(gcSpiHandleRx pfRxHandler) { DEBUG_printf("SpiOpen\n"); /* initialize SPI state */ sSpiInformation.ulSpiState = eSPI_STATE_POWERUP; sSpiInformation.SPIRxHandler = pfRxHandler; sSpiInformation.usTxPacketLength = 0; sSpiInformation.pTxPacket = NULL; sSpiInformation.pRxPacket = (unsigned char *)spi_buffer; sSpiInformation.usRxPacketLength = 0; spi_buffer[CC3000_RX_BUFFER_SIZE - 1] = CC3000_BUFFER_MAGIC_NUMBER; wlan_tx_buffer[CC3000_TX_BUFFER_SIZE - 1] = CC3000_BUFFER_MAGIC_NUMBER; /* SPI configuration */ SPI_HANDLE->Init.Mode = SPI_MODE_MASTER; SPI_HANDLE->Init.Direction = SPI_DIRECTION_2LINES; SPI_HANDLE->Init.DataSize = SPI_DATASIZE_8BIT; SPI_HANDLE->Init.CLKPolarity = SPI_POLARITY_LOW; SPI_HANDLE->Init.CLKPhase = SPI_PHASE_2EDGE; SPI_HANDLE->Init.NSS = SPI_NSS_SOFT; SPI_HANDLE->Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8; SPI_HANDLE->Init.FirstBit = SPI_FIRSTBIT_MSB; SPI_HANDLE->Init.TIMode = SPI_TIMODE_DISABLED; SPI_HANDLE->Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED; SPI_HANDLE->Init.CRCPolynomial = 7; spi_init(SPI_HANDLE); // configure wlan CS and EN pins GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.Speed = GPIO_SPEED_FAST; GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStructure.Pull = GPIO_NOPULL; GPIO_InitStructure.Alternate = 0; GPIO_InitStructure.Pin = PIN_CS->pin_mask; HAL_GPIO_Init(PIN_CS->gpio, &GPIO_InitStructure); GPIO_InitStructure.Pin = PIN_EN->pin_mask; HAL_GPIO_Init(PIN_EN->gpio, &GPIO_InitStructure); HAL_GPIO_WritePin(PIN_CS->gpio, PIN_CS->pin_mask, GPIO_PIN_SET); HAL_GPIO_WritePin(PIN_EN->gpio, PIN_EN->pin_mask, GPIO_PIN_RESET); /* do a dummy read, this ensures SCLK is low before actual communications start, it might be required */ CS_LOW(); uint8_t buf[1]; HAL_SPI_Receive(SPI_HANDLE, buf, sizeof(buf), SPI_TIMEOUT); CS_HIGH(); // register EXTI extint_register((mp_obj_t)PIN_IRQ, GPIO_MODE_IT_FALLING, GPIO_PULLUP, (mp_obj_t)&irq_callback_obj, true, NULL); extint_enable(PIN_IRQ->pin); DEBUG_printf("SpiOpen finished; IRQ.pin=%d IRQ_LINE=%d\n", PIN_IRQ->pin, PIN_IRQ->pin); }
// 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()); }
static rt_err_t fm25_close(rt_device_t dev) { CS_LOW(); spi_readwrite( FM25_WRDI ); CS_HIGH(); SPI_Cmd(FM25_SPI, DISABLE); return RT_EOK; }
void SerialFlash_SectorErase(long address) { CS_LOW(); SPI_BYTE(CMD_SE); SPI_BYTE((unsigned char)(address>>16)); SPI_BYTE((unsigned char)(address>>8)); SPI_BYTE((unsigned char)(address & 0xFF)); CS_HIGH(); }
void SerialFlash_BlockErase2(long address) { CS_LOW(); SPI_BYTE(CMD_BE2); SPI_BYTE((unsigned char)(address>>16)); SPI_BYTE((unsigned char)(address>>8)); SPI_BYTE((unsigned char)(address & 0xFF)); CS_HIGH(); }
void SerialFlash_PageProgram(long address, unsigned char *buf, unsigned short size) { CS_LOW(); SPI_BYTE(CMD_PP); SPI_BYTE((unsigned char)(address>>16)); SPI_BYTE((unsigned char)(address>>8)); SPI_BYTE((unsigned char)(address & 0xFF)); SPI_BUF_SEND(buf, size); CS_HIGH(); }