//********************************************************************************************************* // // Sende Write Command // //********************************************************************************************************* void enc28j60WriteOp( char op, char address, char data) { char temp_sreg; temp_sreg = SREG; cli(); // CS aktive setzen #if defined(__AVR_XMEGA__) ENC28J60_CONTROL_PORT.OUTCLR = ( 1<<ENC28J60_CONTROL_CS ); #else ENC28J60_CONTROL_PORT &= ~( 1<<ENC28J60_CONTROL_CS ); #endif // schreibcomando senden SPI_ReadWrite( SPIBUS, op | (address & ADDR_MASK) ); // daten senden SPI_ReadWrite( SPIBUS, data ); // CS wieder freigeben _delay_us( 1 ); #if defined(__AVR_XMEGA__) ENC28J60_CONTROL_PORT.OUTSET = (1<<ENC28J60_CONTROL_CS); #else ENC28J60_CONTROL_PORT |= (1<<ENC28J60_CONTROL_CS); #endif SREG = temp_sreg; }
//********************************************************************************************************* // // Buffer schreiben // //********************************************************************************************************* void enc28j60WriteBuffer(unsigned int len, unsigned char * data) { #if DEBUG_AV && DEBUG_ENC_BUFFER_DATA putpgmstring("enc28j60WriteBuffer start\r\n"); #endif // assert CS ENC28J60_CONTROL_PORT &= ~(1<<ENC28J60_PIN_CS); // issue write command SPI_ReadWrite( ENC28J60_WRITE_BUF_MEM ); while(len--) { #if DEBUG_AV && DEBUG_ENC_BUFFER_DATA puthexbyte(*data); #endif // write data SPI_ReadWrite( *data++ ); } // release CS ENC28J60_CONTROL_PORT |= (1<<ENC28J60_PIN_CS); #if DEBUG_AV && DEBUG_ENC_BUFFER_DATA putpgmstring("\r\n"); putpgmstring("enc28j60WriteBuffer end\r\n"); #endif }
//********************************************************************************************************* // // Sende Rad Command // //********************************************************************************************************* char enc28j60ReadOp( char op, char address) { char temp_sreg; temp_sreg = SREG; cli(); unsigned char data; // CS aktive setzen #if defined(__AVR_XMEGA__) ENC28J60_CONTROL_PORT.OUTCLR = ( 1<<ENC28J60_CONTROL_CS ); #else ENC28J60_CONTROL_PORT &= ~( 1<<ENC28J60_CONTROL_CS ); #endif // lesecomando schreiben data = SPI_ReadWrite( SPIBUS, op | (address & ADDR_MASK) ); // dummy senden um ergebnis zu erhalten data = SPI_ReadWrite( SPIBUS, 0x00 ); // dummy read machen if ( address & 0x80 ) data = SPI_ReadWrite( SPIBUS, 0x00 ); // CS wieder freigeben _delay_us( 1 ); #if defined(__AVR_XMEGA__) ENC28J60_CONTROL_PORT.OUTSET = (1<<ENC28J60_CONTROL_CS); #else ENC28J60_CONTROL_PORT |= (1<<ENC28J60_CONTROL_CS); #endif SREG = temp_sreg; return data; }
bool drv8711::SPI_VerifiedWrite(unsigned int sendData) { unsigned int readData = 0; int attempts = 0; const int maxtries = 10; bool success = false; do { attempts++; // Write SPI_ReadWrite(sendData); // Readback readData = SPI_ReadWrite(REGREAD | sendData); // Compare if ((readData << 4) == (sendData << 4)) { success = true; } else { #if debug Serial.println ("SPI Write Error, attempt:" + String(attempts)); #endif delayMicroseconds(attempts); // delay before trying again } } while ( (success == false) && (attempts < maxtries) ) ; return success; }
//********************************************************************************************************* // // Buffer einlesen // //********************************************************************************************************* void enc28j60ReadBuffer(unsigned int len, unsigned char * data) { #if DEBUG_AV && DEBUG_ENC_BUFFER_DATA putpgmstring("enc28j60ReadBuffer start\r\n"); #endif // assert CS ENC28J60_CONTROL_PORT &= ~(1<<ENC28J60_PIN_CS); // issue read command SPI_ReadWrite( ENC28J60_READ_BUF_MEM ); while(len--) { // read data *data++ = SPI_ReadWrite( 0x00 ); #if DEBUG_AV && DEBUG_ENC_BUFFER_DATA puthexbyte(*(data-1)); #endif } // release CS ENC28J60_CONTROL_PORT |= (1<<ENC28J60_PIN_CS); #if DEBUG_AV && DEBUG_ENC_BUFFER_DATA putpgmstring("\r\n"); putpgmstring("enc28j60ReadBuffer end\r\n"); #endif }
//********************************************************************************************************* // // Buffer schreiben // //********************************************************************************************************* void enc28j60WriteBuffer( int len, char * data) { char temp_sreg; temp_sreg = SREG; cli(); // assert CS #if defined(__AVR_XMEGA__) ENC28J60_CONTROL_PORT.OUTCLR = ( 1<<ENC28J60_CONTROL_CS ); #else ENC28J60_CONTROL_PORT &= ~( 1<<ENC28J60_CONTROL_CS ); #endif // issue write command SPI_ReadWrite( SPIBUS, ENC28J60_WRITE_BUF_MEM ); // SPI1_FastMem2Write( data, len ); while(len--) { // write data SPI_ReadWrite( SPIBUS, *data++ ); } // release CS _delay_us( 1 ); #if defined(__AVR_XMEGA__) ENC28J60_CONTROL_PORT.OUTSET = (1<<ENC28J60_CONTROL_CS); #else ENC28J60_CONTROL_PORT |= (1<<ENC28J60_CONTROL_CS); #endif SREG = temp_sreg; }
//********************************************************************************************************* // // Sende Write Command // //********************************************************************************************************* void enc28j60WriteOp( unsigned char op, unsigned char address, unsigned char data) { // CS aktive setzen ENC28J60_CONTROL_PORT &= ~(1<<ENC28J60_PIN_CS); // schreibcomando senden SPI_ReadWrite( op | (address & ADDR_MASK) ); // daten senden SPI_ReadWrite( data ); // CS wieder freigeben ENC28J60_CONTROL_PORT |= (1<<ENC28J60_PIN_CS); }
/* **************************************************************************************** * @brief Starts a data transmission. * ***************************************************************************************** */ void spi_write(LPC_SPI_TypeDef *SPIx, uint8_t *bufptr, uint32_t size, void (*tx_callback)(void)) { SPI_DATA_SETUP_Type *dataCfg; dataCfg = (SPI_DATA_SETUP_Type *)spidat.txrx_setup; wr_ongoing = 1; if(rd_ongoing == 0) { xferConfig.tx_data = tx_buffer; memcpy(xferConfig.tx_data,bufptr,size); xferConfig.rx_data = NULL; xferConfig.length = size; xferConfig.wr_length = size; xferConfig.rd_length = size; xferConfig.callback = tx_callback; xferConfig.counter = 0; SPI_ReadWrite(LPC_SPI, &xferConfig, SPI_TRANSFER_INTERRUPT); }else { dataCfg->tx_data = xferConfig.tx_data; memcpy(xferConfig.tx_data,bufptr,size); dataCfg->wr_length = size; if(dataCfg->wr_length > (dataCfg->length - dataCfg->counter)) { dataCfg->length = dataCfg->wr_length + dataCfg->counter; } } }
//********************************************************************************************************* // // Buffer einlesen // //********************************************************************************************************* void enc28j60ReadBuffer( int len, char * data) { char temp_sreg; temp_sreg = SREG; cli(); // assert CS #if defined(__AVR_XMEGA__) ENC28J60_CONTROL_PORT.OUTCLR = ( 1<<ENC28J60_CONTROL_CS ); #else ENC28J60_CONTROL_PORT &= ~( 1<<ENC28J60_CONTROL_CS ); #endif // issue read command SPI_ReadWrite( SPIBUS, ENC28J60_READ_BUF_MEM ); SPI_ReadBlock( SPIBUS, data, len); /* while(len--) { // read data *data++ = SPI1_ReadWrite( 0x00 ); } */ // release CS _delay_us( 1 ); #if defined(__AVR_XMEGA__) ENC28J60_CONTROL_PORT.OUTSET = (1<<ENC28J60_CONTROL_CS); #else ENC28J60_CONTROL_PORT |= (1<<ENC28J60_CONTROL_CS); #endif SREG = temp_sreg; }
//********************************************************************************************************* // // Sende Read Command // //********************************************************************************************************* unsigned char enc28j60ReadOp( unsigned char op, unsigned char address) { unsigned char data; // CS aktive setzen ENC28J60_CONTROL_PORT &= ~(1<<ENC28J60_PIN_CS); // lesecomando schreiben data = SPI_ReadWrite( op | (address & ADDR_MASK) ); // dummy senden um ergebnis zu erhalten data = SPI_ReadWrite( 0x00 ); // dummy read machen if ( address & 0x80 ) data = SPI_ReadWrite( 0x00 ); // CS wieder freigeben ENC28J60_CONTROL_PORT |= (1<<ENC28J60_PIN_CS); return data; }
u16 TPReadY(SPI_TypeDef* SPIx) { u16 y=0; TP_CS(); SpiDelay(10); SPI_ReadWrite(SPIx, 0xD0); // SPI_ReadWrite(0x90); SpiDelay(10); y=SPI_ReadWrite(SPIx, 0x00); y<<=8; y+=SPI_ReadWrite(SPIx, 0x00); SpiDelay(10); TP_DCS(); y = y>>3; return (y); }
u16 TPReadX(SPI_TypeDef* SPIx) { u16 x=0; TP_CS(); SpiDelay(10); SPI_ReadWrite(SPIx, 0x90); // SPI_ReadWrite(0xd0); SpiDelay(10); x=SPI_ReadWrite(SPIx, 0x00); x<<=8; x+=SPI_ReadWrite(SPIx, 0x00); SpiDelay(10); TP_DCS(); x = x>>3; return (x); }
/** * \ingroup sd_raw * Receives a raw byte from the memory card. * * \returns The byte which should be read. * \see sd_raw_send_byte */ uint8_t sd_raw_rec_byte() { unsigned char data; data = SPI_ReadWrite( spi_bus_num, 0xff ); return( data ); }
void drv8711::ReadDECAYRegister() { unsigned int sendData = 0; unsigned int readData = 0; // Read DECAY Register sendData = REGREAD | (G_DECAY_REG.Address << 12); readData = SPI_ReadWrite(sendData); G_DECAY_REG.DECMOD = ((readData >> 8) & 0x0007); G_DECAY_REG.TDECAY = ((readData >> 0) & 0x00FF); }
void drv8711::ReadBLANKRegister() { unsigned int sendData = 0; unsigned int readData = 0; // Read BLANK Register sendData = REGREAD | (G_BLANK_REG.Address << 12); readData = SPI_ReadWrite(sendData); G_BLANK_REG.ABT = ((readData >> 8) & 0x0001); G_BLANK_REG.TBLANK = ((readData >> 0) & 0x00FF); }
void drv8711::ReadOFFRegister() { unsigned int sendData = 0; unsigned int readData = 0; // Read OFF Register sendData = REGREAD | (G_OFF_REG.Address << 12); readData = SPI_ReadWrite(sendData); G_OFF_REG.PWMMODE = ((readData >> 8) & 0x0001); G_OFF_REG.TOFF = ((readData >> 0) & 0x00FF); }
void drv8711::ReadTORQUERegister() { unsigned int sendData = 0; unsigned int readData = 0; // Read TORQUE Register sendData = REGREAD | (G_TORQUE_REG.Address << 12); readData = SPI_ReadWrite(sendData); G_TORQUE_REG.SIMPLTH = ((readData >> 8) & 0x0007); G_TORQUE_REG.TORQUE = ((readData >> 0) & 0x00FF); }
void drv8711::ReadSTALLRegister() { unsigned int sendData = 0; unsigned int readData = 0; // Read STALL Register sendData = REGREAD | (G_STALL_REG.Address << 12); readData = SPI_ReadWrite(sendData); G_STALL_REG.VDIV = ((readData >> 10) & 0x0003); G_STALL_REG.SDCNT = ((readData >> 8) & 0x0003); G_STALL_REG.SDTHR = ((readData >> 0) & 0x00FF); }
void drv8711::WriteSTATUSRegister() { unsigned int sendData = 0; // Write STATUS Register sendData = REGWRITE | (G_STATUS_REG.Address << 12) ; sendData |= (G_STATUS_REG.STDLAT << 7) | (G_STATUS_REG.STD << 6) | (G_STATUS_REG.UVLO << 5) | (G_STATUS_REG.BPDF << 4) | (G_STATUS_REG.APDF << 3) | (G_STATUS_REG.BOCP << 2) | (G_STATUS_REG.AOCP << 1) | (G_STATUS_REG.OTS); #if debug Serial.println("Writing STATUS Reg"); #endif SPI_ReadWrite(sendData); }
void drv8711::ReadDRIVERegister() { unsigned int sendData = 0; unsigned int readData = 0; // Read DRIVE Register sendData = REGREAD | (G_DRIVE_REG.Address << 12); readData = SPI_ReadWrite(sendData); G_DRIVE_REG.IDRIVEP = ((readData >> 10) & 0x0003); G_DRIVE_REG.IDRIVEN = ((readData >> 8) & 0x0003); G_DRIVE_REG.TDRIVEP = ((readData >> 6) & 0x0003); G_DRIVE_REG.TDRIVEN = ((readData >> 4) & 0x0003); G_DRIVE_REG.OCPDEG = ((readData >> 2) & 0x0003); G_DRIVE_REG.OCPTH = ((readData >> 0) & 0x0003); }
void loop(){ Serial.println(); Serial.println("************SPI communication begin.*************"); Serial.println(); const char* str = pSPIStr; while (*str) { unsigned char dataBuffer = (unsigned char)(*str); //Serial.print(dataBuffer, BYTE); dataBuffer = SPI_ReadWrite(dataBuffer); str++; Serial.print(dataBuffer, BYTE); } }
void drv8711::ReadCTRLRegister() { unsigned int sendData = 0; unsigned int readData = 0; // Read CTRL Register sendData = REGREAD | (G_CTRL_REG.Address << 12); readData = SPI_ReadWrite(sendData); G_CTRL_REG.DTIME = ((readData >> 10) & 0x0003); G_CTRL_REG.ISGAIN = ((readData >> 8) & 0x0003); G_CTRL_REG.EXSTALL = ((readData >> 7) & 0x0001); G_CTRL_REG.MODE = ((readData >> 3) & 0x000F); G_CTRL_REG.RSTEP = ((readData >> 2) & 0x0001); G_CTRL_REG.RDIR = ((readData >> 1) & 0x0001); G_CTRL_REG.ENBL = ((readData >> 0) & 0x0001); }
void drv8711::ReadSTATUSRegister() { unsigned int sendData = 0; unsigned int readData = 0; // Read STATUS Register sendData = REGREAD | (G_STATUS_REG.Address << 12); readData = SPI_ReadWrite(sendData); G_STATUS_REG.STDLAT = ((readData >> 7) & 0x0001); G_STATUS_REG.STD = ((readData >> 6) & 0x0001); G_STATUS_REG.UVLO = ((readData >> 5) & 0x0001); G_STATUS_REG.BPDF = ((readData >> 4) & 0x0001); G_STATUS_REG.APDF = ((readData >> 3) & 0x0001); G_STATUS_REG.BOCP = ((readData >> 2) & 0x0001); G_STATUS_REG.AOCP = ((readData >> 1) & 0x0001); G_STATUS_REG.OTS = ((readData >> 0) & 0x0001); }
/*********************************************************************//** * @brief c_entry: Main SPI program body * @param[in] None * @return int **********************************************************************/ int c_entry(void) { PINSEL_CFG_Type PinCfg; SPI_DATA_SETUP_Type xferConfig; /* * Initialize SPI pin connect * P0.15 - SCK; * P0.16 - SSEL - used as GPIO * P0.17 - MISO * P0.18 - MOSI */ PinCfg.Funcnum = 3; PinCfg.OpenDrain = 0; PinCfg.Pinmode = 0; PinCfg.Portnum = 0; PinCfg.Pinnum = 15; PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = 17; PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = 18; PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = 16; PinCfg.Funcnum = 0; PINSEL_ConfigPin(&PinCfg); /* Initialize debug via UART0 * – 115200bps * – 8 data bit * – No parity * – 1 stop bit * – No flow control */ debug_frmwrk_init(); // print welcome screen print_menu(); SPI_ConfigStruct.CPHA = SPI_CPHA_SECOND; SPI_ConfigStruct.CPOL = SPI_CPOL_LO; SPI_ConfigStruct.ClockRate = 2000000; SPI_ConfigStruct.DataOrder = SPI_DATA_MSB_FIRST; SPI_ConfigStruct.Databit = SPI_DATABIT_SIZE; SPI_ConfigStruct.Mode = SPI_MASTER_MODE; // Initialize SPI peripheral with parameter given in structure above SPI_Init(LPC_SPI, &SPI_ConfigStruct); /* Initialize Buffer */ Buffer_Init(); xferConfig.tx_data = Tx_Buf; xferConfig.rx_data = Rx_Buf; xferConfig.length = BUFFER_SIZE; SPI_ReadWrite(LPC_SPI, &xferConfig, SPI_TRANSFER_POLLING); // Verify buffer after transferring Buffer_Verify(); _DBG_("Verify complete"); SPI_DeInit(LPC_SPI); /* Loop forever */ while(1); return 1; }
/*********************************************************************//** * @brief c_entry: Main SPI program body * @param[in] None * @return int **********************************************************************/ int c_entry(void) { uint8_t tmpchar[2] = {0, 0}; PINSEL_CFG_Type PinCfg; __IO FlagStatus exitflag; /* * Initialize SPI pin connect * P0.15 - SCK; * P0.16 - SSEL - used as GPIO * P0.17 - MISO * P0.18 - MOSI */ PinCfg.Funcnum = 3; PinCfg.OpenDrain = 0; PinCfg.Pinmode = 0; PinCfg.Portnum = 0; PinCfg.Pinnum = 15; PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = 17; PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = 18; PINSEL_ConfigPin(&PinCfg); PinCfg.Pinnum = 16; PinCfg.Funcnum = 0; PINSEL_ConfigPin(&PinCfg); /* Initialize debug via UART0 * – 115200bps * – 8 data bit * – No parity * – 1 stop bit * – No flow control */ debug_frmwrk_init(); // print welcome screen print_menu(); // initialize SPI configuration structure to default SPI_ConfigStructInit(&SPI_ConfigStruct); // Initialize SPI peripheral with parameter given in structure above SPI_Init(LPC_SPI, &SPI_ConfigStruct); // Initialize /CS pin to GPIO function CS_Init(); /* preemption = 1, sub-priority = 1 */ NVIC_SetPriority(SPI_IRQn, ((0x01<<3)|0x01)); /* Enable SSP0 interrupt */ NVIC_EnableIRQ(SPI_IRQn); /* First, send some command to reset SC16IS740 chip via SPI bus interface * note driver /CS pin to low state before transferring by CS_Enable() function */ complete = RESET; CS_Force(0); xferConfig.tx_data = iocon_cfg; xferConfig.rx_data = spireadbuf; xferConfig.length = sizeof (iocon_cfg); SPI_ReadWrite(LPC_SPI, &xferConfig, SPI_TRANSFER_INTERRUPT); while (complete == RESET); CS_Force(1); complete = RESET; CS_Force(0); xferConfig.tx_data = iodir_cfg; xferConfig.rx_data = spireadbuf; xferConfig.length = sizeof (iodir_cfg); SPI_ReadWrite(LPC_SPI, &xferConfig, SPI_TRANSFER_INTERRUPT); while (complete == RESET); CS_Force(1); // Reset exit flag exitflag = RESET; // Start to use SPI polling mode /* Read some data from the buffer */ while (exitflag == RESET) { while((tmpchar[0] = _DG) == 0); if (tmpchar[0] == 27){ /* ESC key, set exit flag */ _DBG_(menu2); exitflag = SET; } else if (tmpchar[0] == 'r'){ print_menu(); } else { if (tmpchar[0] == '1') { // LEDs are ON now... CS_Force(0); xferConfig.tx_data = iostate_on; xferConfig.rx_data = spireadbuf; xferConfig.length = sizeof (iostate_on); SPI_ReadWrite(LPC_SPI, &xferConfig, SPI_TRANSFER_POLLING); CS_Force(1); } else if (tmpchar[0] == '2') { // LEDs are OFF now... CS_Force(0); xferConfig.tx_data = iostate_off; xferConfig.rx_data = spireadbuf; xferConfig.length = sizeof (iostate_off); SPI_ReadWrite(LPC_SPI, &xferConfig, SPI_TRANSFER_POLLING); CS_Force(1); } /* Then Echo it back */ _DBG_(tmpchar); } } // DeInitialize UART0 peripheral SPI_DeInit(LPC_SPI); /* Loop forever */ while(1); return 1; }
/** * \ingroup sd_raw * Sends a raw byte to the memory card. * * \param[in] b The byte to sent. * \see sd_raw_rec_byte */ void sd_raw_send_byte(uint8_t b) { SPI_ReadWrite( spi_bus_num, b ); }