/************************************************************************************************** * \fn nRF_WriteTXPayload() * \brief Send wireless packet * Send block of 4 to be sure de SPI fifo is not overflown * **************************************************************************************************/ void nRF_WriteTXPayload(GPIO_Handle HandleGPIO,SPI_Handle HandleSPI,uint16_t payloadLenght) { uint8_t i=0; uint8_t j=0; uint16_t entireBlock = payloadLenght/4; uint16_t restBlock = payloadLenght%4; nRF_setCSN(false,HandleGPIO); // CSN pin LOW SPI_write(HandleSPI, W_TX_PAYLOAD); while(SPI_getRxFifoStatus(HandleSPI) < SPI_FifoStatus_1_Word); HandleRF.Reg[R_STATUS] = SPI_read(HandleSPI); for(i = 0; i < entireBlock; ++i){ for(j=0; j<4; ++j){SPI_write(HandleSPI, HandleRF.TXPayload[j+(4*i)]<<8);} for(j=0;j<4;++j){ while(SPI_getRxFifoStatus(HandleSPI) < SPI_FifoStatus_1_Word); HandleRF.Reg[R_JUNK] = SPI_read(HandleSPI); } } for(j=0; j<restBlock; ++j){SPI_write(HandleSPI, HandleRF.TXPayload[j+(4*entireBlock)]<<8);} for(i = 0; i < restBlock; ++i){ while(SPI_getRxFifoStatus(HandleSPI) < SPI_FifoStatus_1_Word); HandleRF.Reg[R_JUNK] = SPI_read(HandleSPI); } nRF_setCSN(true,HandleGPIO); //System_100usDelay(100); //nRF_setCE(true,HandleGPIO); //minimum 10 us pulse to send data //System_100usDelay(100); //nRF_setCE(false,HandleGPIO); }
//the following command reads multiple blocks from the sd card starting at the specified block/sector void SD_read_multiple_blocks(uint32_t sector,uint8_t* data,int numOfBlocks){ PortEx_OUTCLR(BIT3_bm, PS_BANKB); //pull SD cs low SPIInit(SPI_MODE_0_gc); SPICS(TRUE); while(SD_command(SDHC_CMD_READ_MULTIPLE_BLOCKS,sector,SDHC_DUMMY_BYTE,8) != SDHC_CMD_SUCCESS); //send command to read data //do the following for however many sectors to be read in for (int j=0;j<numOfBlocks;j++){ Buffer[1]=SDHC_DUMMY_BYTE; while(Buffer[1] != SDHC_DATA_TOKEN){ Buffer[1] = SPI_write(SDHC_DUMMY_BYTE); //wait for start of data token } for (int i=0;i<SDHC_SECTOR_SIZE;i++){ data[(i+(j*SDHC_SECTOR_SIZE))] = SPI_write(SDHC_DUMMY_BYTE); //read in the data } for (int i=0;i<2;i++){ Buffer[i] = SPI_write(SDHC_DUMMY_BYTE); //read in the 2 CRC bytes } } SD_command(SDHC_CMD_STOP_TRANSMISSION,SDHC_NO_ARGUMENTS,SDHC_DUMMY_BYTE,8); //send command to stop reading data Buffer[0] = SPI_write(SDHC_DUMMY_BYTE); //read the stuff byte Buffer[1] = FILLER_BYTE; while (Buffer[1] != SDHC_DUMMY_BYTE) Buffer[1] = SPI_write(SDHC_DUMMY_BYTE); //wait for card to finish internal processes SPICS(FALSE); SPIDisable(); PortEx_OUTSET(BIT3_bm, PS_BANKB); //pull SD cs high }
/* ------------------------------------------------------------------------ * * SPI_EEPROM_read( src, dst, len ) * * ------------------------------------------------------------------------ */ Int16 SPI_EEPROM_read(CSL_SpiHandle spiHandle, Uint16 src, Uint32 dst, Uint32 length ) { Uint16 *dst_ptr; CSL_Status result; result = CSL_SOK; CSL_SPI_REGS->SPICMD1 = (Uint16)0x0000 | 0; /* Issue Write Enable command */ commandBuf[0] = (Uint16)SPI_CMD_WREN; result |= SPI_write(spiHandle, commandBuf, 1); CSL_SPI_REGS->SPICMD1 = (Uint16)0x0000 | length + 3 - 1; /* Send Read command */ commandBuf[0] = (Uint16)SPI_CMD_READ; commandBuf[1] = (Uint16)(src >> 8); commandBuf[2] = (Uint16)src; result |= SPI_write(spiHandle, commandBuf, 3); /* Read the requested EEPROM page */ dst_ptr = (Uint16 *)dst; result |= SPI_read(spiHandle, dst_ptr, length); return (result); }
/* ------------------------------------------------------------------------ * * SPI_EEPROM_write( src, dst, len ) * * ------------------------------------------------------------------------ */ Int16 SPI_EEPROM_write(CSL_SpiHandle spiHandle, Uint32 src, Uint16 dst, Uint32 length ) { Uint16 *src_ptr; CSL_Status result; result = CSL_SOK; /* Establish source */ src_ptr = (Uint16 *)src; CSL_SPI_REGS->SPICMD1 = (Uint16)0x0000 | 0; /* Issue Write Enable command */ commandBuf[0] = (Uint16)SPI_CMD_WREN; result |= SPI_write(spiHandle, commandBuf, 1); CSL_SPI_REGS->SPICMD1 = (Uint16)0x0000 | length + 3 - 1; commandBuf[0] = (Uint16)SPI_CMD_WRITE; commandBuf[1] = (Uint16)(dst >> 8); commandBuf[2] = (Uint16)dst; result |= SPI_write(spiHandle, commandBuf, 3); /* Write the Data Buffer to the requested EEPROM page */ result |= SPI_write(spiHandle, src_ptr, length); return (result); }
//the following command reads one sector from the sdhc card void SD_read_block(uint32_t sector,uint8_t* arrayOf512Bytes){ PortEx_OUTCLR(BIT3_bm, PS_BANKB); //pull SD cs low SPIInit(SPI_MODE_0_gc); SPICS(TRUE); for(int i=0;SD_command(SDHC_CMD_READ_SINGLE_BLOCK,sector,SDHC_DUMMY_BYTE,8) != SDHC_CMD_SUCCESS; i++) { //send command to read data if (i >= 10) { //there was no response to the command while(1); } } while(Buffer[0] != SDHC_DATA_TOKEN){ Buffer[0] = SPI_write(SDHC_DUMMY_BYTE); } for (int i=0;i<SDHC_SECTOR_SIZE;i++){ arrayOf512Bytes[i] = SPI_write(SDHC_DUMMY_BYTE); //read in the data } Buffer[12] = FILLER_BYTE; while (Buffer[12] != SDHC_DUMMY_BYTE){ Buffer[12] = SPI_write(SDHC_DUMMY_BYTE); } SPICS(FALSE); SPIDisable(); PortEx_OUTSET(BIT3_bm, PS_BANKB); //pull SD cs high }
int SDCard__cmdx(int cmd, int arg) { // _cs = 0; GPIO_clear(_cs); // printf("SDCMDx:%u ", cmd); // send a command SPI_write(0x40 | cmd); SPI_write(arg >> 24); SPI_write(arg >> 16); SPI_write(arg >> 8); SPI_write(arg >> 0); SPI_write(0x95); // wait for the repsonse (response[7] == 0) for(int i=0; i<SD_COMMAND_TIMEOUT; i++) { int response = SPI_write(0xFF); if(!(response & 0x80)) { // printf(" <%u\n", response); return response; } } // printf("Timeout\n"); // _cs = 1; GPIO_set(_cs); SPI_write(0xFF); return -1; // timeout }
int SDCard__cmd8() { // _cs = 0; GPIO_clear(_cs); // send a command SPI_write(0x40 | SDCMD_SEND_IF_COND); // CMD8 SPI_write(0x00); // reserved SPI_write(0x00); // reserved SPI_write(0x01); // 3.3v SPI_write(0xAA); // check pattern SPI_write(0x87); // crc // wait for the repsonse (response[7] == 0) for(int i=0; i<SD_COMMAND_TIMEOUT * 1000; i++) { char response[5]; response[0] = SPI_write(0xFF); if(!(response[0] & 0x80)) { for(int j=1; j<5; j++) { response[j] = SPI_write(0xFF); } // _cs = 1; GPIO_set(_cs); SPI_write(0xFF); return response[0]; } } // _cs = 1; GPIO_set(_cs); SPI_write(0xFF); return -1; // timeout }
void mrf24j40_long_addr_write(uns16 addr, uns8 data){ #ifndef __PIC32MX__ clear_pin(mrf24j40_cs_port, mrf24j40_cs_pin); addr = addr & 0b0000001111111111; // <9:0> bits addr = addr << 5; set_bit(addr, 15); // long addresss set_bit(addr, 4); // set for write spi_hw_transmit(addr >> 8 ); spi_hw_transmit(addr & 0x00ff); spi_hw_transmit(data); set_pin(mrf24j40_cs_port, mrf24j40_cs_pin); #endif #ifdef __PIC32MX__ ZIGCS=0; addr=((addr<<1)&0x7FF)|0x801; addr<<=4; SPI_write(addr>>8); SPI_write(addr); SPI_write(data); ZIGCS=1; #endif }
/**************************************************************************** * \brief Write data to CAN controller * * \param in address to which shall be write * \return data ****************************************************************************/ void mcp_write_byte(uint8_t address, uint8_t byte) { mcp_turn_on(); SPI_write(MCP_WRITE); SPI_write(address); SPI_write(byte); mcp_turn_off(); }
void mcp2515_write(uint8_t val, uint8_t addr){ SPI_chipselect(1); SPI_write(MCP_WRITE); SPI_write(addr); SPI_write(val); SPI_chipselect(0); }
/**************************************************************************** * \brief Modify bits of CAN controller * * \param in address of the register * \param in bit mask * \param in new value of the register ****************************************************************************/ void mcp_modify_bit(uint8_t address, uint8_t mask, uint8_t byte) { mcp_turn_on(); SPI_write(MCP_BITMOD); SPI_write(address); SPI_write(mask); SPI_write(byte); mcp_turn_off(); }
void enc28j60WriteOp(u8 op, u8 address, u8 bData) { CSACTIVE; // issue write command SPI_write( op | (address & ADDR_MASK) ); // write data SPI_write(bData); CSPASSIVE; }
void mcp2515_bit_modify(uint8_t addr, uint8_t mask, uint8_t val){ SPI_chipselect(1); SPI_write(MCP_BIT_MODIFY); SPI_write(addr); SPI_write(mask); SPI_write(val); SPI_chipselect(0); }
void nRF_WriteRegister (SPI_Handle HandleSPI,GPIO_Handle HandleGPIO,uint16_t Reg,uint16_t value) { nRF_setCSN(false,HandleGPIO); SPI_write(HandleSPI,W_REGISTER | Reg); //register adress SPI_write(HandleSPI, value << 8); while(SPI_getRxFifoStatus(HandleSPI) < SPI_FifoStatus_2_Words); //wait for status and junk HandleRF.Reg[R_STATUS] = SPI_read(HandleSPI); HandleRF.Reg[R_JUNK] = SPI_read(HandleSPI); nRF_setCSN(true,HandleGPIO); }
void inline apa102_setleds(struct cRGB *ledarray, uint16_t leds) { uint16_t i; uint8_t *rawarray=(uint8_t*)ledarray; SPI_init(); SPI_write(0x00); // Start Frame SPI_write(0x00); SPI_write(0x00); SPI_write(0x00); for (i=0; i<(leds+leds+leds); i+=3) { SPI_write(0xff); // Maximum global brightness SPI_write(rawarray[i+0]); SPI_write(rawarray[i+1]); SPI_write(rawarray[i+2]); } // End frame: 8+8*(leds >> 4) clock cycles for (i=0; i<leds; i+=16) { SPI_write(0xff); // 8 more clock cycles } }
int SDCard__cmd58(uint32_t *ocr) { // _cs = 0; GPIO_clear(_cs); int arg = 0; // send a command SPI_write(0x40 | 58); SPI_write(arg >> 24); SPI_write(arg >> 16); SPI_write(arg >> 8); SPI_write(arg >> 0); SPI_write(0x95); // wait for the repsonse (response[7] == 0) for(int i=0; i<SD_COMMAND_TIMEOUT; i++) { int response = SPI_write(0xFF); if(!(response & 0x80)) { *ocr = SPI_write(0xFF) << 24; *ocr |= SPI_write(0xFF) << 16; *ocr |= SPI_write(0xFF) << 8; *ocr |= SPI_write(0xFF) << 0; // printf("OCR = 0x%08X\n", *ocr); // _cs = 1; GPIO_set(_cs); SPI_write(0xFF); return response; } } // _cs = 1; GPIO_set(_cs); SPI_write(0xFF); return -1; // timeout }
uint16_t nRF_ReadRegister(SPI_Handle HandleSPI,GPIO_Handle HandleGPIO,uint16_t Reg){ uint16_t answer = 0; nRF_setCSN(false,HandleGPIO); //Set csn for spi communication SPI_write(HandleSPI, Reg); //register adress SPI_write(HandleSPI, Reg); //dummy data while(SPI_getRxFifoStatus(HandleSPI) < SPI_FifoStatus_2_Words); //wait for two words (STATUS + REG) HandleRF.Reg[R_STATUS] = SPI_read(HandleSPI); answer = SPI_read(HandleSPI); nRF_setCSN(true,HandleGPIO); return answer; }
uint8_t mcp2515_read(uint8_t addr){ SPI_chipselect(1); SPI_write(MCP_READ); SPI_write(addr); uint8_t val = SPI_read(); SPI_chipselect(0); return val; }
//------------------------------------------------------------------------------------------------------- // void halSpiWriteReg(BYTE addr, BYTE value) // // DESCRIPTION: // Function for writing to a single CC1100 register // // ARGUMENTS: // BYTE addr // Address of a specific CC1100 register to accessed. // BYTE value // Value to be written to the specified CC1100 register. //------------------------------------------------------------------------------------------------------- void halSpiWriteReg(BYTE addr, BYTE value) { DDB0=0; while(DDB3); DDB2=0; addr&=0x7F; SPI_write(addr); SPI_write(value); DDB2=0; DDB0=1; }// halSpiWriteReg
int SDCard__write(const uint8_t *buffer, int length) { // _cs = 0; GPIO_clear(_cs); // indicate start of block SPI_write(0xFE); // write the data for(int i=0; i<length; i++) { SPI_write(buffer[i]); } // write the checksum SPI_write(0xFF); SPI_write(0xFF); // check the repsonse token if((SPI_write(0xFF) & 0x1F) != 0x05) { // _cs = 1; GPIO_set(_cs); SPI_write(0xFF); return 1; } // wait for write to finish while(SPI_write(0xFF) == 0); // _cs = 1; GPIO_set(_cs); SPI_write(0xFF); return 0; }
void enc28j60WriteBuffer(u16 len, u8* buffer) { CSACTIVE; // issue write command SPI_write( ENC28J60_WRITE_BUF_MEM ); while(len) { len--; // write data SPI_write( *buffer ); buffer++; } CSPASSIVE; }
/**************************************************************************** * \brief Read data from CAN controller * * \param in address from which shall be read * \return data ****************************************************************************/ uint8_t mcp_read(uint8_t address) { volatile uint8_t retVal; mcp_turn_on(); SPI_write(MCP_READ); SPI_write(address); retVal = SPI_read(); mcp_turn_off(); return retVal; }
void output_waveform(int freq_value, int arr[]) { int i; for (i = 0; i < 50; i++) { SPI_write(arr[i]); apply_freq(freq_value); } }
uns8 mrf24j40_long_addr_read(uns16 addr){ uns8 result; #ifndef __PIC32MX__ clear_pin(mrf24j40_cs_port, mrf24j40_cs_pin); addr = addr & 0b0000001111111111; // <9:0> bits addr = addr << 5; set_bit(addr, 15); // long addresss spi_hw_transmit(addr >> 8); spi_hw_transmit(addr & 0x00ff); result = spi_hw_receive(); set_pin(mrf24j40_cs_port, mrf24j40_cs_pin); #endif #ifdef __PIC32MX__ ZIGCS=0; addr=((addr<<1)&0x7FE)|0x800; addr<<=4; SPI_write(addr>>8); SPI_write(addr); result=SPI_read(); ZIGCS=1; #endif return result; }
void mcp2515_request_to_send(uint8_t cmd){ SPI_chipselect(1); SPI_write(MCP_REQUEST_TO_SEND | (cmd & 7)); SPI_chipselect(0); }
int SDCard_initialise_card() { // Set to 25kHz for initialisation, and clock card with cs = 1 SPI_frequency(25000); GPIO_set(_cs); for(int i=0; i<16; i++) { SPI_write(0xFF); } // send CMD0, should return with all zeros except IDLE STATE set (bit 0) if(SDCard__cmd(SDCMD_GO_IDLE_STATE, 0) != R1_IDLE_STATE) { fprintf(stderr, "Could not put SD card in to SPI idle state\n"); return cardtype = SDCARD_FAIL; } // send CMD8 to determine whther it is ver 2.x int r = SDCard__cmd8(); if(r == R1_IDLE_STATE) { // printf("Looks like a SDHC Card\n"); return SDCard_initialise_card_v2(); } else if(r == (R1_IDLE_STATE | R1_ILLEGAL_COMMAND)) { return SDCard_initialise_card_v1(); } else { fprintf(stderr, "Not in idle state after sending CMD8 (not an SD card?)\n"); return cardtype = SDCARD_FAIL; } }
void mcp2515_reset(void){ SPI_chipselect(1); SPI_write(MCP_RESET); SPI_chipselect(0); }
/* * @ write SPI 16BITS */ __inline static uint16 VSPI_WRITE(int ch, uint16 dat) { uint16 cache = 0; cache = SPI_write(ch, dat >> 8) << 8; cache |= SPI_write(ch, dat & 0xff); return cache; }
/************************************************************************************************** * \fn nRF_FlushTX() * \brief Flush TX FIFO * **************************************************************************************************/ void nRF_FlushTX(SPI_Handle HandleSPI,GPIO_Handle HandleGPIO) { nRF_setCSN(false,HandleGPIO); SPI_write(HandleSPI, FLUSH_TX); while(SPI_getRxFifoStatus(HandleSPI) < SPI_FifoStatus_1_Word); //wait HandleRF.Reg[R_STATUS] = SPI_read(HandleSPI); nRF_setCSN(true,HandleGPIO); }
/* * @ VS1003 write data */ void VS1003_write_data(char *buf, int len) { VS_WAIT_DREQ(); SPI_enable(GPIOR_VS1003_DATCH); while(len --) { SPI_write(GPIOR_VS1003_DATCH, *(buf ++)); } SPI_disable(GPIOR_VS1003_DATCH); }