static int Send_Command(unsigned char *DataBuffer, unsigned int Size) { int Temp; Temp = -1; while(Temp == -1) { Temp = Send_Packet(DataBuffer, Size); } Temp = -1; while(Temp == -1) { Temp = Send_Packet(command_get_status, 1); } //Temp = 0; ReceiveData((unsigned char *)&Temp, 1); if(Temp != COMMAND_RET_SUCCESS) { return -1; } // This command is executed successfully return 0; }
BYTE CTL_Write_ND(BYTE *pSUD) { BYTE resultcode; Hwritebytes(rSUDFIFO,8,pSUD); // 1. Send the SETUP token and 8 setup bytes. Device should immediately ACK. resultcode=Send_Packet(tokSETUP,0); // SETUP packet to EP0 if (resultcode) return (resultcode); // should be 0, indicating ACK. // 2. No data stage, so the last operation is to send an IN token to the peripheral // as the STATUS (handshake) stage of this control transfer. We should get NAK or the // DATA1 PID. When we get the DATA1 PID the 3421 automatically sends the closing ACK. resultcode=Send_Packet(tokINHS,0); // This function takes care of NAK retries. if(resultcode) return (resultcode); // should be 0, indicating ACK. else return(0); }
// ----------------------------------------------------------------------------------- // IN Transfer to arbitrary endpoint. Handles multiple packets if necessary. Transfers // "length" bytes. // ----------------------------------------------------------------------------------- // Do an IN transfer to 'endpoint'. Keep sending INS and saving concatenated packet data // in array Xfr_Data[] until 'numbytes' bytes are received. If no errors, returns total // number of bytes read. If any errors, returns a byte with the MSB set and the 7 LSB // indicating the error code from the "launch transfer" function. // BYTE IN_Transfer(BYTE endpoint,WORD INbytes) { BYTE resultcode,j; BYTE pktsize; unsigned int xfrlen,xfrsize; xfrsize = INbytes; xfrlen = 0; while(1) // use a 'return' to exit this loop. { resultcode=Send_Packet(tokIN,endpoint); // IN packet to EP-'endpoint'. Function takes care of NAKS. if (resultcode) return (resultcode); // should be 0, indicating ACK. Else return error code. pktsize=Hrreg(rRCVBC); // number of received bytes for(j=0; j<pktsize; j++) // add this packet's data to XfrData array XfrData[j+xfrlen] = Hrreg(rRCVFIFO); Hwreg(rHIRQ,bmRCVDAVIRQ); // Clear the IRQ & free the buffer xfrlen += pktsize; // add this packet's byte count to total transfer length // // The transfer is complete under two conditions: // 1. The device sent a short packet (L.T. maxPacketSize) // 2. 'INbytes' have been transferred. // if ((pktsize < maxPacketSize) || (xfrlen >= xfrsize)) // have we transferred 'length' bytes? // return xfrlen; { last_transfer_size = xfrlen; return(resultcode); } } }
int SH7619_EDMAC_xmit(PIFACE pi, DCU msg) { RTP_UINT16 length = 0; EmacTDescriptor *pTxTd; char* send_packet; UINT16 count; long flag = FP1; UINT16 size = length; GLOBAL_LOCK(encIrq); if (!pi) { return (-1); } length = msg->length; if (length > (ETHERSIZE+4)) { RTP_DEBUG_ERROR("xmit - length is too large, truncated", NOVAR, 0, 0); length = ETHERSIZE+4; /* what a terriable hack! */ } send_packet = (char*)DCUTODATA(msg); for( count = 0 ; length > 0 ; length -= count ) { // Pointers to the current TxTd pTxTd = *(txTd.td + txTd.head); while( (count = Send_Packet(pTxTd, send_packet, length)) < 0 ); if( count == length ) { flag |= FP0; } pTxTd->status = (pTxTd->status & DL) | flag | ACT; flag = 0; // Driver manage the ring buffer CIRC_INC(txTd.head, TX_BUFFERS) send_packet += count; } // Now start to transmit if it is not already done if( EDTRR0 == 0x00000000 ) { EDTRR0 = 0x00000001; } return (0); }
// ---------------------------------------------------- // CONTROL-Read Transfer. Get the length from SUD[7:6]. // ---------------------------------------------------- BYTE CTL_Read(BYTE *pSUD) { BYTE resultcode; WORD bytes_to_read; bytes_to_read = pSUD[6] + 256*pSUD[7]; // SETUP packet Hwritebytes(rSUDFIFO,8,pSUD); // Load the Setup data FIFO resultcode=Send_Packet(tokSETUP,0); // SETUP packet to EP0 if (resultcode) return (resultcode); // should be 0, indicating ACK. Else return error code. // One or more IN packets (may be a multi-packet transfer) Hwreg(rHCTL,bmRCVTOG1); // FIRST Data packet in a CTL transfer uses DATA1 toggle. // last_transfer_size = IN_Transfer(0,bytes_to_read); // In transfer to EP-0 (IN_Transfer function handles multiple packets) resultcode = IN_Transfer(0,bytes_to_read); if(resultcode) return (resultcode); IN_nak_count=nak_count; // The OUT status stage resultcode=Send_Packet(tokOUTHS,0); if (resultcode) return (resultcode); // should be 0, indicating ACK. Else return error code. return(0); // success! }
int main () { // start delay _delay_ms(DEVICE_START_DELAY_MS); Init_Spi(); DDRC |= 0x01; PORTC |= 0x01; RFM73_Initialize(); RFM73_SetPower(MODULE_POWER); SwitchToRxMode(); DDRD = 0b00000000; PORTD = 0b00000100; SetupTIMER1(); sei(); sleep_enable(); while (1) { if (state == OFF){ DOWNBIT(PORTC, 0); sleep_cpu(); } else { SwitchToTxMode(); for(char i=0;i<POWERCOUNT;i++) { SwitchToTxMode(); Send_Packet(W_TX_PAYLOAD_NOACK_CMD,tx_buf,4); SwitchToRxMode(); //switch to Rx mode _delay_ms(150); INVBIT(PORTC, 0); } } } return 0; }
//======================================================================================= //inline void sendMachine_3(){ enum { DATAMAXLEN = 64, DATAMARKERPOSITION = 0, DATAMARKER = '?', DATACODELENPOSITION = 1, DATACODEPOSION = 2 }; #define ASKDATALEN 3 char ASKDATA[ASKDATALEN] = "***"; #define ANSWERDATALEN 3 char ANSWERDATA[ANSWERDATALEN] = "+++"; static char data[DATAMAXLEN]; #define REPCOUNTER 3 static char repeat_counter = REPCOUNTER; enum {WAITNEWCODE, WAITCODEDONE, WAITBOX, SENDTOBOX, WAITDELAY}; static char state = WAITNEWCODE; switch (state){ //======================== case WAITNEWCODE: //======================== if (getFlag(FLAG_NEW_NOCK) == TRUE) state = WAITCODEDONE; break; //======================== case WAITCODEDONE: //======================== if (getFlag(FLAG_NEW_NOCK) == FALSE){ //state = WAITBOX; //DEBUG state = SENDTOBOX; } break; //======================== case WAITBOX:{ //======================== char len = Receive_Packet(data, DATAMAXLEN); if (len > 0){ #ifdef LOGG loggerWriteToMarker((LogMesT)" WAITBOX get data: *", '*'); loggerWrite((LogMesT)data, len); #endif for (char i = 0; i < ANSWERDATALEN; i++) if (data[i] != ANSWERDATA[i]) return; state = SENDTOBOX; break; } SwitchToTxMode(); Send_Packet(W_TX_PAYLOAD_NOACK_CMD, (unsigned char *)ASKDATA, ASKDATALEN); //Send_Packet(0, ASKDATA, ASKDATALEN); SwitchToRxMode(); state = WAITDELAY; }break; //======================== case WAITDELAY: //======================== if (processWait() == TRUE) #ifdef LOGG loggerWriteToMarker((LogMesT)" goto WAITBOX\r*", '*'); #endif state = WAITBOX; break; //======================== case SENDTOBOX:{ //======================== #ifdef LOGG loggerWriteToMarker((LogMesT)" SENDTOBOX\r*", '*'); #endif SwitchToTxMode(); Nock * nock = getNock(NOCK_PATTERN); data[DATAMARKERPOSITION] = DATAMARKER; data[DATACODELENPOSITION] = nock->count; unsigned char i; for (i = 0; i < nock->count; i++){ int time = nock->nock[i]; char * p = &time; data[DATACODEPOSION + i*2] = *p; p++; data[DATACODEPOSION + i*2 + 1] = *p; } data[DATACODEPOSION + i * 2] = DATAMARKER; Send_Packet(W_TX_PAYLOAD_NOACK_CMD, (unsigned char *)data, DATACODEPOSION + nock->count * 2 + 1); //Send_Packet(0, (unsigned char *)data, DATACODEPOSION + nock->count + 1); #ifdef LOGG loggerWriteToMarker((LogMesT)" nock count: *", '*'); char ch = nock->count + 48; loggerWrite(&ch, 1); #endif if (repeat_counter-- <= 0){ repeat_counter = REPCOUNTER; state = WAITNEWCODE; } }break; //======================== default: break; //======================== } }