void oscWriteHeader(char *address, u08 arglen) { u08 len; // write OSC packet header bufferAddToEnd(uartGetTxBuffer(), OSC_HEADER); // determine padded length of address for(len=0; PRG_RDB(address+len); len++); len++; // count a minumum of one null for termination if(len&0x03) // are pad bytes necessary? len += 4-(len&0x03); // add null pad bytes to reach multiple of four // write length to packet header bufferAddToEnd(uartGetTxBuffer(), len+arglen); }
// UART Receive Complete Interrupt Function void uartReceiveService(u08 nUart) { u08 c; // get received char if(nUart) c = inb(UDR1); else c = inb(UDR0); // if there's a user function to handle this receive event if(UartRxFunc[nUart]) { // call it and pass the received data UartRxFunc[nUart](c); } else { // otherwise do default processing // put received char in buffer // check if there's space if( !bufferAddToEnd(&uartRxBuffer[nUart], c) ) { // no space in buffer // count overflow uartRxOverflow[nUart]++; } } }
u08 uartSendBuffer(u08 nUart, char *buffer, u16 nBytes) { register u08 first; register u16 i; // check if there's space (and that we have any bytes to send at all) if((uartTxBuffer[nUart].datalength + nBytes < uartTxBuffer[nUart].size) && nBytes) { // grab first character first = *buffer++; // copy user buffer to uart transmit buffer for(i = 0; i < nBytes-1; i++) { // put data bytes at end of buffer bufferAddToEnd(&uartTxBuffer[nUart], *buffer++); } // send the first byte to get things going by interrupts uartBufferedTx[nUart] = TRUE; uartSendByte(nUart, first); // return success return TRUE; } else { // return failure return FALSE; } }
void oscSendMessageInt(char *address, u32 arg) { // write OSC packet header, argument length = 4bytes oscWriteHeader(address, 4); // write OSC address to packet oscWriteString(address); // copy arg to buffer bufferAddToEnd(uartGetTxBuffer(), *(((unsigned char*)(&arg))+3) ); bufferAddToEnd(uartGetTxBuffer(), *(((unsigned char*)(&arg))+2) ); bufferAddToEnd(uartGetTxBuffer(), *(((unsigned char*)(&arg))+1) ); bufferAddToEnd(uartGetTxBuffer(), *(((unsigned char*)(&arg))+0) ); // apply checksum oscWriteChecksum(); // send buffer uartSendTxBuffer(); // wait for completion, transmitter to be ready while(!uartReadyTx); }
void oscWriteChecksum(void) { u08 i; u08 chksum = 0; // calculate checksum for(i=2; i<uartGetTxBuffer()->datalength; i++) chksum += bufferGetAtIndex(uartGetTxBuffer(), i); // write checksum to packet bufferAddToEnd(uartGetTxBuffer(), chksum); }
void ProcessErrors(void) { if (GetMessage(MSG_B_KEY_PRESSED)) { bufferAddToEnd(&speakerBuffer, CLICK); SendMessage(MSG_BEEP); rprintfChar(keyPressedCode); } }
/** * Called each time there are data in the input buffer */ void NMEAParser::processInputStream(char c) { if( !bufferAddToEnd(&gpsRxBuffer, c) ) { // no space in buffer // count overflow gpsRxOverflow++; return; } nmeaProcess(&gpsRxBuffer); }
void oscWriteString(char *string) { u08 temp=1; u08 len=0; // write OSC string to packet // copy string's null-termination intentionally while(temp) { temp = PRG_RDB(string++); bufferAddToEnd(uartGetTxBuffer(), temp); len++; } // pad the string as necessary to reach a 4-byte multiple // Note: (len&0x03) == (len%4) //for(; (len&0x03); len++) while(len&0x03) { bufferAddToEnd(uartGetTxBuffer(), 0); len++; } }
// UART Receive Complete Interrupt Function void uartReceiveService(u08 nUart) { assert(nUart < 4); u08 c; // get received char switch (nUart) { case 0: c = inb(UDR0); break; case 1: c = inb(UDR1); break; case 2: c = inb(UDR2); break; case 3: c = inb(UDR3); break; default: return; } // if there's a user function to handle this receive event if(UartRxFunc[nUart]) { // call it and pass the received data UartRxFunc[nUart](c); } else { // otherwise do default processing // put received char in buffer // check if there's space if( !bufferAddToEnd(&uartRxBuffer[nUart], c) ) { // no space in buffer // count overflow uartRxOverflow[nUart]++; } } }
void uart2Service(void) { unsigned int status; // read the channel status register status = pUSART2->US_CSR; status &= pUSART2->US_IMR; if(status & AT91C_US_RXRDY) { bufferAddToEnd(&uartRxBuffer[2], pUSART2->US_RHR); } if(status & AT91C_US_TXRDY) { } // reset error status bits pUSART2->US_CR = AT91C_US_RSTSTA; // clear AIC // AT91C_BASE_AIC->AIC_EOICR = 0; }
void uartAddToTxBuffer(u08 nUart, u08 data) { // add data byte to the end of the tx buffer bufferAddToEnd(&uartTxBuffer[nUart], data); }
// add byte to end of uart Tx buffer u08 uartAddToTxBuffer(u08 data) { // add data byte to the end of the tx buffer return bufferAddToEnd(&uartTxBuffer, data); }
void put_char( unsigned char c ) { printf( "%x ", c ); // Make a debug print bufferAddToEnd( &myBuff, c ); // serial_link[pos++]; // Simulate adding the character to the stream. }