/* ** Configures the SPI1 instance for communication. */ static void SPISetUp(void) { unsigned char dcs = 0x01; unsigned int val = SIMO_SOMI_CLK_CS; /* Resets the SPI */ SPIReset(SOC_SPI_1_REGS); /* Brings SPI Out-of-Reset */ SPIOutOfReset(SOC_SPI_1_REGS); /* Configures SPI in Master Mode */ SPIModeConfigure(SOC_SPI_1_REGS, SPI_MASTER_MODE); /* Sets SPI Controller for 4-pin Mode with Chip Select */ SPIPinControl(SOC_SPI_1_REGS, 0, 0, &val); /* Configures the Prescale bit in Data Format register. */ SPIClkConfigure(SOC_SPI_1_REGS, 150000000, 10000000, SPI_DATA_FORMAT0); /* Chip Select Default Pattern is Set To 1 in SPIDEF Register*/ SPIDefaultCSSet(SOC_SPI_1_REGS, dcs); /* Configures SPI Data Format Register */ SPIConfigDataFmtReg(SPI_DATA_FORMAT0); }
/*! \brief closes an opened spi communication port \param fd - file descriptor of an opened SPI channel \return upon successful completion, the function shall return 0. Otherwise, -1 shall be returned \sa spi_Open \note \warning */ int spi_Close(Fd_t fd) { unsigned long ulBase = LSPI_BASE; g_SpiFd = 0; if(g_ucDMAEnabled) { //Simplelink_UDMADeInit(); #ifdef SL_PLATFORM_MULTI_THREADED osi_InterruptDeRegister(INT_LSPI); osi_MsgQDelete(&DMAMsgQ); #else SPIIntUnregister(ulBase); g_cDummy = 0; #endif SPIFIFODisable(ulBase,SPI_RX_FIFO); SPIFIFODisable(ulBase,SPI_TX_FIFO); SPIDmaDisable(ulBase,SPI_RX_DMA); SPIDmaDisable(ulBase,SPI_TX_DMA); } //Disable Chip Select SPICSDisable(LSPI_BASE); //Disable SPI Channel SPIDisable(ulBase); // Reset SPI SPIReset(ulBase); // Enable SPI Peripheral PRCMPeripheralClkDisable(PRCM_LSPI,PRCM_RUN_MODE_CLK|PRCM_SLP_MODE_CLK); return 0; }
int main() { DDRB = (1 << PB4); DDRD |= (1 << PD2)|(1 << PD3); sei(); initUSART(); initSPI(); initADC(); while(1){ if(PINB & (1 << PB2)) { SPIReset(); } } }
/****************************************************************************** * Function: void CANInit//(void) * * PreCondition: None * * Input: None * * Output: None * * Side Effects: None * * Overview: This function initializes the MCP2515. * * * Note: None *****************************************************************************/ void CANInit() { unsigned char n, EEPROM_ver; SPIReset(); //Wait state is in SPIReset() function EEPROM_ver = EEPROMCRCCheck(0, 128); //EEPROM_ver=0; for (n = 0; n < 250; n++); //Wait anyway //Set Config Mode and verify //CANCTRL Register if (EEPROM_ver) { CANCTRL_byte = EEPROMBYTERead(CANCTRL); } /* else { EEPROMBYTEWrite(CANCTRL,CANCTRL_byte); } */ SET_CONFIG; //Set Configuration mode CLEAR_OSM; //Not One-Shot-Mode CLEAR_ABORT; //Clear ABAT bit CLKOUT_ENABLED; //Enabled CLKOUT pin (will configure as SOF pin in CNF3) if (!EEPROM_ver) { EEPROMBYTEWrite(CANCTRL, CANCTRL_byte); } SPIByteWrite(CANCTRL, CANCTRL_byte); //Write to CANCTRL //Verify Mode change if ((SPIByteRead(CANSTAT)& 0xE0) != OPMODE_CONFIG) SPIByteWrite(CANCTRL, CANCTRL_byte); //Try again if (isUsbPowered) //Only if connected to USB. Traffic gen board doesn't process RX msgs { //Configure INTERRUPTS if (EEPROM_ver) { CANINTE_byte = EEPROMBYTERead(CANINTE); } /* else { EEPROMBYTEWrite(CANINTE,CANINTE_byte); } */ G_RXB_INT_ENABLED; G_TXB_INT_ENABLED; if (!EEPROM_ver) { EEPROMBYTEWrite(CANINTE, CANINTE_byte); } SPIByteWrite(CANINTE, CANINTE_byte); } //Use RXnBF pins as interrupts on RX //(BFPCTRL register) RXB0BF_PIN_INT; RXB1BF_PIN_INT; SPIByteWrite(BFPCTRL, BFPCTRL_byte); //Configure TXnRTS pins as RTS pins TXRTSCTRL_byte = 0x07; SPIByteWrite(TXRTSCTRL, TXRTSCTRL_byte); if (EEPROM_ver) { DataArray[0] = EEPROMBYTERead(RXB0CTRL); SPIByteWrite(RXB0CTRL, DataArray[0]); DataArray[0] = EEPROMBYTERead(RXB1CTRL); SPIByteWrite(RXB1CTRL, DataArray[0]); } else { DataArray[0] = 0; SPIByteWrite(RXB0CTRL, DataArray[0]); SPIByteWrite(RXB1CTRL, DataArray[0]); EEPROMBYTEWrite(RXB0CTRL, DataArray[0]); EEPROMBYTEWrite(RXB1CTRL, DataArray[0]); } //Configure MASKS DataArray[0] = 0x00; DataArray[1] = 0x00; DataArray[2] = 0x00; DataArray[3] = 0x00; if (EEPROM_ver) { DataArray[0] = EEPROMBYTERead(RXM0SIDH); DataArray[1] = EEPROMBYTERead(RXM0SIDL); DataArray[2] = EEPROMBYTERead(RXM0EID8); DataArray[3] = EEPROMBYTERead(RXM0EID0); } else { EEPROMDataWrite(RXM0SIDH, &DataArray[0], 4); } SPISeqWrite(RXM0SIDH, 4, DataArray); //RX Mask 0 DataArray[0] = 0x00; DataArray[1] = 0x00; DataArray[2] = 0x00; DataArray[3] = 0x00; if (EEPROM_ver) { DataArray[0] = EEPROMBYTERead(RXM1SIDH); DataArray[1] = EEPROMBYTERead(RXM1SIDL); DataArray[2] = EEPROMBYTERead(RXM1EID8); DataArray[3] = EEPROMBYTERead(RXM1EID0); } else { EEPROMDataWrite(RXM1SIDH, &DataArray[0], 4); } SPISeqWrite(RXM1SIDH, 4, DataArray); //RX Mask 1 //SPISeqWrite(RXB0CTRL, 1, 0); //RXB0 Control JM //SPISeqWrite(RXB1CTRL, 1, 0); //RXB1 Control JM //Configure FILTERS //Will RX standard messages into RXB0 and extended into RXB1 DataArray[0] = 0x00; DataArray[1] = 0x00; DataArray[2] = 0x00; DataArray[3] = 0x00; if (EEPROM_ver) { DataArray[0] = EEPROMBYTERead(RXF0SIDH); DataArray[1] = EEPROMBYTERead(RXF0SIDL); DataArray[2] = EEPROMBYTERead(RXF0EID8); DataArray[3] = EEPROMBYTERead(RXF0EID0); } else { EEPROMDataWrite(RXF0SIDH, &DataArray[0], 4); } SPISeqWrite(RXF0SIDH, 4, DataArray); //SPIByteWrite(RXF0SIDL, 0x00); //Clearing EXIDE... the rest is "don't care" DataArray[0] = 0x00; DataArray[1] = 0x00; DataArray[2] = 0x00; DataArray[3] = 0x00; if (EEPROM_ver) { DataArray[0] = EEPROMBYTERead(RXF1SIDH); DataArray[1] = EEPROMBYTERead(RXF1SIDL); DataArray[2] = EEPROMBYTERead(RXF1EID8); DataArray[3] = EEPROMBYTERead(RXF1EID0); } else { EEPROMDataWrite(RXF1SIDH, &DataArray[0], 4); } SPISeqWrite(RXF1SIDH, 4, DataArray); //SPIByteWrite(RXF1SIDL, 0x00); //Clearing EXIDE... the rest is "don't care" //JM DataArray[0] = 0x00; DataArray[1] = 0x08; DataArray[2] = 0x00; DataArray[3] = 0x00; if (EEPROM_ver) { DataArray[0] = EEPROMBYTERead(RXF2SIDH); DataArray[1] = EEPROMBYTERead(RXF2SIDL); DataArray[2] = EEPROMBYTERead(RXF2EID8); DataArray[3] = EEPROMBYTERead(RXF2EID0); } else { EEPROMDataWrite(RXF2SIDH, &DataArray[0], 4); } SPISeqWrite(RXF2SIDH, 4, DataArray); //SPIByteWrite(RXF2SIDL, 0x08); //Setting EXIDE... the rest is "don't care" DataArray[0] = 0x00; DataArray[1] = 0x08; DataArray[2] = 0x00; DataArray[3] = 0x00; if (EEPROM_ver) { DataArray[0] = EEPROMBYTERead(RXF3SIDH); DataArray[1] = EEPROMBYTERead(RXF3SIDL); DataArray[2] = EEPROMBYTERead(RXF3EID8); DataArray[3] = EEPROMBYTERead(RXF3EID0); } else { EEPROMDataWrite(RXF3SIDH, &DataArray[0], 4); } SPISeqWrite(RXF3SIDH, 4, DataArray); //SPIByteWrite(RXF3SIDL, 0x08); //Setting EXIDE... the rest is "don't care" //JM DataArray[0] = 0x00; DataArray[1] = 0x08; DataArray[2] = 0x00; DataArray[3] = 0x00; if (EEPROM_ver) { DataArray[0] = EEPROMBYTERead(RXF4SIDH); DataArray[1] = EEPROMBYTERead(RXF4SIDL); DataArray[2] = EEPROMBYTERead(RXF4EID8); DataArray[3] = EEPROMBYTERead(RXF4EID0); } else { EEPROMDataWrite(RXF4SIDH, &DataArray[0], 4); } SPISeqWrite(RXF4SIDH, 4, DataArray); //SPIByteWrite(RXF4SIDL, 0x08); //Setting EXIDE... the rest is "don't care" //JM DataArray[0] = 0x00; DataArray[1] = 0x08; DataArray[2] = 0x00; DataArray[3] = 0x00; if (EEPROM_ver) { DataArray[0] = EEPROMBYTERead(RXF5SIDH); DataArray[1] = EEPROMBYTERead(RXF5SIDL); DataArray[2] = EEPROMBYTERead(RXF5EID8); DataArray[3] = EEPROMBYTERead(RXF5EID0); } else { EEPROMDataWrite(RXF5SIDH, &DataArray[0], 4); } SPISeqWrite(RXF5SIDH, 4, DataArray); //SPIByteWrite(RXF5SIDL, 0x08); //Setting EXIDE... the rest is "don't care" //JM //Configure TX BUFFER 0 DataArray[0] = 0xA3; DataArray[1] = 0x09; DataArray[2] = 0x12; DataArray[3] = 0x34; DataArray[4] = 0x04; DataArray[5] = 0x00; DataArray[6] = 0x01; DataArray[7] = 0x02; DataArray[8] = 0x03; DataArray[9] = 0x04; DataArray[10] = 0x05; DataArray[11] = 0x06; DataArray[12] = 0x07; if (EEPROM_ver) { DataArray[0] = EEPROMBYTERead(TXB0SIDH); DataArray[1] = EEPROMBYTERead(TXB0SIDL); DataArray[2] = EEPROMBYTERead(TXB0EID8); DataArray[3] = EEPROMBYTERead(TXB0EID0); DataArray[4] = EEPROMBYTERead(TXB0DLC); DataArray[5] = EEPROMBYTERead(TXB0D0); DataArray[6] = EEPROMBYTERead(TXB0D1); DataArray[7] = EEPROMBYTERead(TXB0D2); DataArray[8] = EEPROMBYTERead(TXB0D3); DataArray[9] = EEPROMBYTERead(TXB0D4); DataArray[10] = EEPROMBYTERead(TXB0D5); DataArray[11] = EEPROMBYTERead(TXB0D6); DataArray[12] = EEPROMBYTERead(TXB0D7); } else { EEPROMDataWrite(TXB0SIDH, &DataArray, 13); } SPILoadTX(CAN_LD_TXB0_ID, 13, DataArray); //Configure TX BUFFER 1 DataArray[0] = 0x22; DataArray[1] = 0x00; DataArray[2] = 0x64; DataArray[3] = 0x68; DataArray[4] = 0x02; DataArray[5] = 0x00; DataArray[6] = 0x11; DataArray[7] = 0x22; DataArray[8] = 0x33; DataArray[9] = 0x44; DataArray[10] = 0x55; DataArray[11] = 0x66; DataArray[12] = 0x77; if (EEPROM_ver) { DataArray[0] = EEPROMBYTERead(TXB1SIDH); DataArray[1] = EEPROMBYTERead(TXB1SIDL); DataArray[2] = EEPROMBYTERead(TXB1EID8); DataArray[3] = EEPROMBYTERead(TXB1EID0); DataArray[4] = EEPROMBYTERead(TXB1DLC); DataArray[5] = EEPROMBYTERead(TXB1D0); DataArray[6] = EEPROMBYTERead(TXB1D1); DataArray[7] = EEPROMBYTERead(TXB1D2); DataArray[8] = EEPROMBYTERead(TXB1D3); DataArray[9] = EEPROMBYTERead(TXB1D4); DataArray[10] = EEPROMBYTERead(TXB1D5); DataArray[11] = EEPROMBYTERead(TXB1D6); DataArray[12] = EEPROMBYTERead(TXB1D7); } else { EEPROMDataWrite(TXB1SIDH, &DataArray, 13); } SPILoadTX(CAN_LD_TXB1_ID, 13, DataArray); //Configure TX BUFFER 2 DataArray[0] = 0xAA; DataArray[1] = 0x03; DataArray[2] = 0x33; DataArray[3] = 0x33; DataArray[4] = 0x08; DataArray[5] = 0xAA; DataArray[6] = 0xBB; DataArray[7] = 0xCC; DataArray[8] = 0xDD; DataArray[9] = 0xEE; DataArray[10] = 0xFF; DataArray[11] = 0x0A; DataArray[12] = 0x0B; SPILoadTX(CAN_LD_TXB2_ID, 13, DataArray); if (EEPROM_ver) { DataArray[0] = EEPROMBYTERead(TXB2SIDH); DataArray[1] = EEPROMBYTERead(TXB2SIDL); DataArray[2] = EEPROMBYTERead(TXB2EID8); DataArray[3] = EEPROMBYTERead(TXB2EID0); DataArray[4] = EEPROMBYTERead(TXB2DLC); DataArray[5] = EEPROMBYTERead(TXB2D0); DataArray[6] = EEPROMBYTERead(TXB2D1); DataArray[7] = EEPROMBYTERead(TXB2D2); DataArray[8] = EEPROMBYTERead(TXB2D3); DataArray[9] = EEPROMBYTERead(TXB2D4); DataArray[10] = EEPROMBYTERead(TXB2D5); DataArray[11] = EEPROMBYTERead(TXB2D6); DataArray[12] = EEPROMBYTERead(TXB2D7); } else { EEPROMDataWrite(TXB2SIDH, &DataArray, 13); } SPISeqRead(TXB1SIDH, 13, ReadArray); //Configure BIT TIMING SOF_ENABLED; //Start-of-Frame signal enabled. Will set when "SetBitTiming()" is called if (EEPROM_ver) { DataArray[0] = EEPROMBYTERead(CNF3); //Load array DataArray[1] = EEPROMBYTERead(CNF2); // DataArray[2] = EEPROMBYTERead(CNF1); // SPISeqWrite(CNF3, 3, DataArray); //Write registers } else { SetBitTiming(CAN_125kbps); //Note, this function puts device in Normal Mode } //Set Normal Mode and verify SET_NORMAL; SPIByteWrite(CANCTRL, CANCTRL_byte); //Write to CANCTRL old_CANCTRL = 0x00; //EEPROMCRCWrite(0,128); //Verify Mode change if ((SPIByteRead(CANSTAT)& 0xE0) != OPMODE_NORMAL) SPIByteWrite(CANCTRL, CANCTRL_byte); //Try again }
/****************************************************************************** * Function: void ProcessIO * * PreCondition: None * * Input: None * * Output: None * * Side Effects: None * * Overview: This function is a place holder for other user routines. * It is a mixture of both USB and non-USB tasks. * * Note: None *****************************************************************************/ void ProcessIO(void) { unsigned char n, b, c, y; int a; // User Application USB tasks if (!(isUsbPowered)) //Only generate traffic if NOT connected to USB { CheckLoadButton(); CANLoadTX(); return; } if ((usb_device_state < CONFIGURED_STATE) || (UCONbits.SUSPND == 1)) return; //----- Read USB buffer if it has data from the host ----- if (HIDRxReport(inbuffer, 64)) // USB receive buffer has data { LED_RX_ON(); //Turn LED on T0CONbits.TMR0ON = 1; //Start timer for TX LED on time gTimeout = 0; //Reset timout //---- CANmsgs: Check if host has requested CAN messages to be transmited switch (inbuffer[u_CANmsgs]) // interpret command { case 0x00: // No messages are available break; case 0x01: // Message 1 is available GetUSBData(m1_SIDH, 13, DataArray); n = SPILoadTX(CAN_LOAD_TX, 13, DataArray); if (n == CAN_LD_TXB0_ID) //if TX0 is loaded { RTS0_2515_LOW(); RTS0_2515_HIGH(); } else if (n == CAN_LD_TXB1_ID) //if TX1 is loaded { RTS1_2515_LOW(); RTS1_2515_HIGH(); } else if (n == CAN_LD_TXB2_ID) //if TX2 is loaded { RTS2_2515_LOW(); RTS2_2515_HIGH(); } break; case 0x02: // Message 1 and 2 are available //Message 1 GetUSBData(m1_SIDH, m1_DLC + 5, DataArray); n = SPILoadTX(CAN_LOAD_TX, m1_DLC + 5, DataArray); if (n == CAN_LD_TXB0_ID) //if TX0 is loaded { RTS0_2515_LOW(); RTS0_2515_HIGH(); } else if (n == CAN_LD_TXB1_ID) //if TX1 is loaded { RTS1_2515_LOW(); RTS1_2515_HIGH(); } else if (n == CAN_LD_TXB2_ID) //if TX2 is loaded { RTS2_2515_LOW(); RTS2_2515_HIGH(); } //Message 2 GetUSBData(m2_SIDH, m2_DLC + 5, DataArray); n = SPILoadTX(CAN_LOAD_TX, m2_DLC + 5, DataArray); if (n == CAN_LD_TXB0_ID) //if TX0 is loaded { RTS0_2515_LOW(); RTS0_2515_HIGH(); } else if (n == CAN_LD_TXB1_ID) //if TX1 is loaded { RTS1_2515_LOW(); RTS1_2515_HIGH(); } else if (n == CAN_LD_TXB2_ID) //if TX2 is loaded { RTS2_2515_LOW(); RTS2_2515_HIGH(); } break; case 0x03: // Message 1, 2, and 3 are available //Message 1 GetUSBData(m1_SIDH, m1_DLC + 5, DataArray); n = SPILoadTX(CAN_LOAD_TX, m1_DLC + 5, DataArray); if (n == CAN_LD_TXB0_ID) //if TX0 is loaded { RTS0_2515_LOW(); RTS0_2515_HIGH(); } else if (n == CAN_LD_TXB1_ID) //if TX1 is loaded { RTS1_2515_LOW(); RTS1_2515_HIGH(); } else if (n == CAN_LD_TXB2_ID) //if TX2 is loaded { RTS2_2515_LOW(); RTS2_2515_HIGH(); } //Message 2 GetUSBData(m2_SIDH, m2_DLC + 5, DataArray); n = SPILoadTX(CAN_LOAD_TX, m2_DLC + 5, DataArray); if (n == CAN_LD_TXB0_ID) //if TX0 is loaded { RTS0_2515_LOW(); RTS0_2515_HIGH(); } else if (n == CAN_LD_TXB1_ID) //if TX1 is loaded { RTS1_2515_LOW(); RTS1_2515_HIGH(); } else if (n == CAN_LD_TXB2_ID) //if TX2 is loaded { RTS2_2515_LOW(); RTS2_2515_HIGH(); } //Message 3 GetUSBData(m3_SIDH, m3_DLC + 5, DataArray); n = SPILoadTX(CAN_LOAD_TX, m3_DLC + 5, DataArray); if (n == CAN_LD_TXB0_ID) //if TX0 is loaded { RTS0_2515_LOW(); RTS0_2515_HIGH(); } else if (n == CAN_LD_TXB1_ID) //if TX1 is loaded { RTS1_2515_LOW(); RTS1_2515_HIGH(); } else if (n == CAN_LD_TXB2_ID) //if TX2 is loaded { RTS2_2515_LOW(); RTS2_2515_HIGH(); } break; case 0x04: //--FUTURE-- Message 1, 2, 3, and 4 are available break; default: // unrecognized or null command ; }// END switch: inbuffer[u_CANmsgs] //---- CANCTRL: Write to the CANCTRL register if changed if (inbuffer[u_CANCTRL] != old_CANCTRL) //If host sent new CANCTRL value { SPIByteWrite(CANCTRL, inbuffer[u_CANCTRL]); //Write to CANCTRL EEPROMBYTEWrite(CANCTRL, inbuffer[u_CANCTRL]); EEPROMCRCWrite(0, 128); old_CANCTRL = inbuffer[u_CANCTRL]; // outbuffer[u_CANSTAT] = SPIByteRead(CANSTAT); while ((outbuffer[u_CANSTAT] & 0xE0) != (inbuffer[u_CANCTRL] & 0xE0))//if didn't change modes yet { outbuffer[u_CANSTAT] = SPIByteRead(CANSTAT); } UserFlag.USBsend = 1; //Set flag so will send USB message } //---- SPI: SPI command from host if (inbuffer[u_SPI]) //If host sent SPI command (non-zero) { switch (inbuffer[u_SPI]) { case CAN_RESET: // SPIReset(); CANInit(); break; case CAN_READ: // if (!UserFlag.USBQueue) // If previous message is queued { outbuffer[u_SPI] = CAN_READ; //Send back to host outbuffer[u_REG] = inbuffer[u_REG]; //Send back to host outbuffer[u_DATA] = SPIByteRead(inbuffer[u_REG]); //Send back to host } UserFlag.USBsend = 1; //Set flag so will send USB message UserFlag.USBQueue = 1; //Indicates msg is queued, but not sent break; case CAN_WRITE: // //outbuffer[u_SPI] = 0; //Send back to host //JM SPIByteWrite(inbuffer[u_REG], inbuffer[u_DATA]); EEPROMBYTEWrite(inbuffer[u_REG], inbuffer[u_DATA]); EEPROMCRCWrite(0, 128); break; case CAN_RTS: // SPI_RTS(inbuffer[u_DATA]); break; case CAN_RD_STATUS: // outbuffer[u_DATA] = SPIReadStatus(); UserFlag.USBsend = 1; //Set flag so will send USB message break; case FIRMWARE_VER_RD: memmove(&outbuffer[u_STATUS], firmware_version, sizeof (firmware_version)); outbuffer[u_STATUS + sizeof (firmware_version)] = 0; UserFlag.USBsend = 1; //Set flag so will send USB message break; default: // unrecognized or null command ; }// END switch: inbuffer[u_SPI] } }//END if (HIDRxReport(inbuffer, 1) //---- Check RXnBF pins and service messages as needed --- switch (CheckCANRX()) // Check if CAN message received { case 0x01: // Message in RXB0 (Msgs in this buffer are Standard) SPIReadRX(CAN_RD_START_RXB0SIDH, 13, ReadArray); LoadUSBString(ReadArray); break; case 0x02: // Message in RXB1 (Msgs in this buffer are Extended) SPIReadRX(CAN_RD_START_RXB1SIDH, 13, ReadArray); LoadUSBString(ReadArray); break; case 0x03: // Message in RXB0 and RXB1 SPIReadRX(CAN_RD_START_RXB0SIDH, 13, ReadArray); LoadUSBString(ReadArray); SPIReadRX(CAN_RD_START_RXB1SIDH, 13, ReadArray); LoadUSBString(ReadArray); break; default: // unrecognized or null command ; }// END switch: CheckCANRX() //---- The following turns off the TX and RX USB indicator LEDs after some time //Inst. cycle = 200 ns; TMR0IF sets every 51 us if (INTCONbits.TMR0IF) { TimerCounter++; if (!TimerCounter) //if rolled over, set flag. User code will handle the rest. { LED_TX_OFF(); //Turn LED off LED_RX_OFF(); //Turn LED off T0CONbits.TMR0ON = 0; //Start timer for TX LED on time TimerCounter = 0xFE; gTimeout = 1; //Reset timout } INTCONbits.TMR0IF = 0; } //------ Load USB Data to be transmitted to the host -------- if (UserFlag.MCP_RXBn | UserFlag.USBsend) { if (!mHIDTxIsBusy()) { HIDTxReport(outbuffer, 64); outbuffer[0] = 0x00; //PKR$$$ Need this?? UserFlag.MCP_RXBn = 0; //clear flag UserFlag.USBsend = 0; //clear flag UserFlag.USBQueue = 0; //Clear message queue LED_TX_ON(); //Turn LED on T0CONbits.TMR0ON = 1; //Start timer for TX LED on time gTimeout = 0; //Reset timout outbuffer[u_SPI] = 0x00; //clear back to 00h so host doesn't detect "SPI response" USB_ptr = 0xFF; //Point to location 0xFF outbuffer[u_CANmsgs] = 0x00; //Clear message status } } }//end ProcessIO
/*! \brief open spi communication port to be used for communicating with a SimpleLink device Given an interface name and option flags, this function opens the spi communication port and creates a file descriptor. This file descriptor can be used afterwards to read and write data from and to this specific spi channel. The SPI speed, clock polarity, clock phase, chip select and all other attributes are all set to hardcoded values in this function. \param ifName - points to the interface name/path. The interface name is an optional attributes that the simple link driver receives on opening the device. in systems that the spi channel is not implemented as part of the os device drivers, this parameter could be NULL. \param flags - option flags \return upon successful completion, the function shall open the spi channel and return a non-negative integer representing the file descriptor. Otherwise, -1 shall be returned \sa spi_Close , spi_Read , spi_Write \note \warning */ Fd_t spi_Open(char *ifName, unsigned long flags) { unsigned long ulBase; //NWP master interface ulBase = LSPI_BASE; //Enable MCSPIA2 PRCMPeripheralClkEnable(PRCM_LSPI,PRCM_RUN_MODE_CLK|PRCM_SLP_MODE_CLK); //Disable Chip Select SPICSDisable(ulBase); //Disable SPI Channel SPIDisable(ulBase); // Reset SPI SPIReset(ulBase); // // Configure SPI interface // SPIConfigSetExpClk(ulBase,PRCMPeripheralClockGet(PRCM_LSPI), SPI_IF_BIT_RATE,SPI_MODE_MASTER,SPI_SUB_MODE_0, (SPI_SW_CTRL_CS | SPI_4PIN_MODE | SPI_TURBO_OFF | SPI_CS_ACTIVEHIGH | SPI_WL_32)); if(PRCMPeripheralStatusGet(PRCM_UDMA)) { g_ucDMAEnabled = (HWREG(UDMA_BASE + UDMA_O_CTLBASE) != 0x0) ? 1 : 0; } else { g_ucDMAEnabled = 0; } #ifdef SL_CPU_MODE g_ucDMAEnabled = 0; #endif if(g_ucDMAEnabled) { memset(g_ucDinDout,0xFF,sizeof(g_ucDinDout)); //g_ucDout[0]=0xFF; //Simplelink_UDMAInit(); // Set DMA channel cc_UDMAChannelSelect(UDMA_CH12_LSPI_RX); cc_UDMAChannelSelect(UDMA_CH13_LSPI_TX); SPIFIFOEnable(ulBase,SPI_RX_FIFO); SPIFIFOEnable(ulBase,SPI_TX_FIFO); SPIDmaEnable(ulBase,SPI_RX_DMA); SPIDmaEnable(ulBase,SPI_TX_DMA); SPIFIFOLevelSet(ulBase,1,1); #if defined(SL_PLATFORM_MULTI_THREADED) osi_InterruptRegister(INT_LSPI, (P_OSI_INTR_ENTRY)DmaSpiSwIntHandler,INT_PRIORITY_LVL_1); SPIIntEnable(ulBase,SPI_INT_EOW); osi_MsgQCreate(&DMAMsgQ,"DMAQueue",sizeof(int),1); #else IntRegister(INT_LSPI,(void(*)(void))DmaSpiSwIntHandler); IntPrioritySet(INT_LSPI, INT_PRIORITY_LVL_1); IntEnable(INT_LSPI); SPIIntEnable(ulBase,SPI_INT_EOW); g_cDummy = 0x0; #endif } SPIEnable(ulBase); g_SpiFd = 1; return g_SpiFd; }