int main () { init (); gpioSetDir (3, 1, 1); gpioSetValue (3, 1, 1); int availBytes; char buf[32]; uint8_t frame[64]; for (int i = 0; i < 64; ++i) { frame[i] = i; } while (1) { systickDelay (1); USB_WriteEP (CDC_DEP_IN, frame, 64); // CDC_WrOutBuf (text, &textLen); CDC_OutBufAvailChar (&availBytes); if (availBytes > 0) { int bytesToRead = availBytes > 32 ? 32 : availBytes; int bytesRead = CDC_RdOutBuf (buf, &bytesToRead); gpioSetValue (3, 1, 0); } } }
char cGetCDCChar( void ) { int32_t lAvailableBytes, xBytes = 0; char cInputChar; do { /* Are there any characters already available? */ CDC_OutBufAvailChar( &lAvailableBytes ); if( lAvailableBytes > 0 ) { if( xSemaphoreTake( xCDCMutex, cmdMAX_MUTEX_WAIT ) == pdPASS ) { /* Attempt to read one character. */ xBytes = 1; xBytes = CDC_RdOutBuf( &cInputChar, &xBytes ); xSemaphoreGive( xCDCMutex ); } } if( xBytes == 0 ) { /* A character was not available. Wait until signalled by the CDC Rx callback function that new data has arrived. */ xSemaphoreTake( xNewDataSemaphore, portMAX_DELAY ); } } while( xBytes == 0 ); return cInputChar; }
void main_cdc(void) { int dx = 0; //lastTick = systickGetTicks(); // Used to control output/printf timing lcdPrintln("Init USB"); lcdRefresh(); CDC_Init(); // Initialise VCOM USB_Init(); // USB Initialization USB_Connect(TRUE); // USB Connect // Wait until USB is configured or timeout occurs uint32_t usbTimeout = 0; while ( usbTimeout < CFG_USBCDC_INITTIMEOUT / 10 ) { if (USB_Configuration) break; delayms(10); // Wait 10ms usbTimeout++; } lcdPrintln("Done"); lcdRefresh(); uint8_t buf[2] = {0,0}; int l; while(1){ CDC_OutBufAvailChar(&l); if( l ){ l = 1; CDC_RdOutBuf (buf, &l); puts(buf); dx=DoString(dx,0,buf); lcdDisplay(); } //puts("hello world\r\n"); //delayms(1); } }
void cmdPoll() { #if defined CFG_PRINTF_UART while (uartRxBufferDataPending()) { uint8_t c = uartRxBufferRead(); cmdRx(c); } #endif #if defined CFG_PRINTF_USBCDC int numBytesToRead, numBytesRead, numAvailByte; CDC_OutBufAvailChar (&numAvailByte); if (numAvailByte > 0) { numBytesToRead = numAvailByte > 32 ? 32 : numAvailByte; numBytesRead = CDC_RdOutBuf (&usbcdcBuf[0], &numBytesToRead); int i; for (i = 0; i < numBytesRead; i++) { cmdRx(usbcdcBuf[i]); } } #endif }
// Check once to see if 1 or more keys have been pressed, // fill the buffer & return # of chars collected int checkUSBCDC(char* usbcdcBuf, int len) { int numBytesToRead, numBytesRead, numAvailByte = 0; CDC_OutBufAvailChar (&numAvailByte); numBytesToRead = numAvailByte > len ? len : numAvailByte; numBytesRead = CDC_RdOutBuf (usbcdcBuf, &numBytesToRead); //printf("[%d]", numBytesRead); return numBytesRead; }
// Poll every 10 ms until 1 or more keys are pressed, // fill the buffer & return # of chars collected int pollUSBCDC(char* usbcdcBuf, int len) { int numBytesToRead, numBytesRead, numAvailByte = 0; while(numAvailByte == 0) { CDC_OutBufAvailChar (&numAvailByte); } numBytesToRead = numAvailByte > len ? len : numAvailByte; numBytesRead = CDC_RdOutBuf (usbcdcBuf, &numBytesToRead); //printf("[%d]", numBytesRead); return numBytesRead; }
int SerialCDC::read() { int numAvailByte = 0; char c; CheckSerialState(); do { CDC_OutBufAvailChar (&numAvailByte); if (numAvailByte > 0) { int numBytesToRead = 1; int numBytesRead = CDC_RdOutBuf (&c, &numBytesToRead); } } while (numAvailByte <= 0); return (c); }
/*---------------------------------------------------------------------------- Reads character from USB buffer and writes to serial port buffer *---------------------------------------------------------------------------*/ void VCOM_Usb2Serial (void) { static char serBuf[32]; int numBytesToRead, numBytesRead, numAvailByte; CDC_OutBufAvailChar (&numAvailByte); if (numAvailByte > 0) { numBytesToRead = numAvailByte > 32 ? 32 : numAvailByte; numBytesRead = CDC_RdOutBuf (&serBuf[0], &numBytesToRead); UARTSend ((uint8_t*)&serBuf, numBytesRead); } }
/*---------------------------------------------------------------------------- Reads character from USB buffer and writes to serial port buffer *---------------------------------------------------------------------------*/ void VCOM_Usb2Serial(void) { static char serBuf [32]; int numBytesToRead, numBytesRead, numAvailByte; CDC_OutBufAvailChar (&numAvailByte); if (numAvailByte > 0) { numBytesToRead = numAvailByte > 32 ? 32 : numAvailByte; numBytesRead = CDC_RdOutBuf (&serBuf[0], &numBytesToRead); //ser_Write (&serBuf[0], &numBytesRead); numBytesToRead = numBytesRead; for (numBytesRead = 0; numBytesRead < numBytesToRead; numBytesRead++) USART_write(&usart, serBuf[numBytesRead]); } }
/*---------------------------------------------------------------------------- Reads character from USB buffer and writes to serial port buffer *---------------------------------------------------------------------------*/ void VCOM_Usb2Serial(void) { static char serBuf [32]; int numBytesToRead, numBytesRead, numAvailByte; CDC_OutBufAvailChar (&numAvailByte); if (numAvailByte > 0) { numBytesToRead = numAvailByte > 32 ? 32 : numAvailByte; numBytesRead = CDC_RdOutBuf (&serBuf[0], &numBytesToRead); #if PORT_NUM ser_Write (1, &serBuf[0], &numBytesRead); #else ser_Write (0, &serBuf[0], &numBytesRead); #endif } }
void main_bridge(void) { GLOBAL(daytrig)=10; GLOBAL(lcdbacklight)=10; GLOBAL(privacy) = 3; char input[64]; char led1=0; char led2=0; usbCDCInit(); delayms(500); nrf_init(); nrf_config_set(&config); nrf_rcv_pkt_start(R_CONFIG_EN_CRC); while(1){ int l, i, status; CDC_OutBufAvailChar (&l); if(l>0){ gpioSetValue (RB_LED0, led1);led1=1-led1; CDC_RdOutBuf (input, &l); for(i=0; i<l; i++){ uint8_t cmd = serialmsg_put(input[i]); if( cmd != SERIAL_NONE ){ switch( cmd ){ case '1': // can we loose packets here? nrf_rcv_pkt_end(); status=snd_pkt_no_crc(serialmsg_len, serialmsg_message); //status=nrf_snd_pkt_crc(serialmsg_len, serialmsg_message); nrf_rcv_pkt_start(R_CONFIG_EN_CRC); break; case '3': memcpy(config.txmac, serialmsg_message, 5); nrf_write_long(C_W_REGISTER|R_TX_ADDR,5,config.txmac); break; case '4': memcpy(config.mac0, serialmsg_message, 5); nrf_write_long(C_W_REGISTER|R_RX_ADDR_P0,5,config.mac0); nrf_write_reg(R_EN_RXADDR,1); break; case '5': config.channel=serialmsg_message[0]; nrf_set_channel(config.channel); nrf_cmd(C_FLUSH_RX); break; case '6': config.maclen[0]=serialmsg_message[0]; nrf_write_reg(R_RX_PW_P0,config.maclen[0]); break; case '7': puts("\\7"); char s[sizeof(uint32_t)+1]; *((uint32_t*)s) =GetUUID32(); s[sizeof(uint32_t)]=0; puts(s); puts("\\0"); break; case '8': /* set mac width */ nrf_write_reg(R_SETUP_AW,serialmsg_message[0]); break; case '9': // Dis/Enable CRC nrf_write_reg(R_CONFIG, R_CONFIG_PRIM_RX|R_CONFIG_PWR_UP| ((serialmsg_message[0]&1)?R_CONFIG_EN_CRC :0)| ((serialmsg_message[0]&2)?R_CONFIG_CRCO :0) ); /* maybe add enhanced shockburst stuff here */ nrf_cmd(C_FLUSH_RX); nrf_write_reg(R_STATUS,0); break; }; puts("\\2\\0"); } } } int len; uint8_t buf[32]; len=nrf_rcv_pkt_poll(sizeof(buf),buf); if( len > 0 ){ gpioSetValue (RB_LED2, led2);led2=1-led2; puts("\\1"); dump_encoded(len, buf); puts("\\0"); } } }
/*************************************************************************************************** char UsbSerial_RxChar(void) *************************************************************************************************** * I/P Arguments: none. * Return value : char: Ascii value of the character received * description :This function is used to receive a char from Serial USB port . It waits till a char is received and returns it after reception. ***************************************************************************************************/ char UsbSerial_RxChar(void) { char ch = 0; CDC_RdOutBuf(&ch,&numOfBytes); return ch; }
void main_bridge(void) { GLOBAL(daytrig)=10; GLOBAL(lcdbacklight)=10; GLOBAL(privacy) = 3; char input[64]; char led1=0; char led2=0; usbCDCInit(); delayms(500); nrf_init(); nrf_config_set(&config); nrf_rcv_pkt_start(); while(1){ int l, i, status; CDC_OutBufAvailChar (&l); if(l>0){ gpioSetValue (RB_LED0, led1);led1=1-led1; CDC_RdOutBuf (input, &l); for(i=0; i<l; i++){ uint8_t cmd = serialmsg_put(input[i]); if( cmd != SERIAL_NONE ){ switch( cmd ){ case '1': // can we loose packets here? nrf_rcv_pkt_end(); status=snd_pkt_no_crc(serialmsg_len, serialmsg_message); //status=nrf_snd_pkt_crc(serialmsg_len, serialmsg_message); nrf_rcv_pkt_start(); break; case '3': memcpy(config.txmac, serialmsg_message, 5); nrf_config_set(&config); break; case '4': memcpy(config.mac0, serialmsg_message, 5); nrf_config_set(&config); break; case '5': config.channel=serialmsg_message[0]; nrf_config_set(&config); break; case '6': config.maclen[0]=serialmsg_message[0]; nrf_config_set(&config); break; case '7': puts("\\7"); char s[sizeof(uint32_t)+1]; *((uint32_t*)s) =GetUUID32(); s[sizeof(uint32_t)]=0; puts(s); puts("\\0"); break; }; puts("\\2\\0"); } } } int len; uint8_t buf[32]; len=nrf_rcv_pkt_poll(sizeof(buf),buf); if( len > 0 ){ gpioSetValue (RB_LED2, led2);led2=1-led2; puts("\\1"); dump_encoded(len, buf); puts("\\0"); } } }