static bool usbVcpFlush(vcpPort_t *port) { uint32_t count = port->txAt; port->txAt = 0; if (count == 0) { return true; } if (!usbIsConnected() || !usbIsConfigured()) { return false; } uint32_t start = millis(); uint8_t *p = port->txBuf; uint32_t txed = 0; while (count > 0) { txed = CDC_Send_DATA(p, count); count -= txed; p += txed; if (millis() - start > USB_TIMEOUT) { break; } } return count == 0; }
void CDC_TestSend(void) { if (USB_PacketSent) { int tCount = snprintf(StringBuffer, sizeof StringBuffer, "Test %p", &StringBuffer[0]); CDC_Send_DATA((uint8_t *) &StringBuffer[0], tCount); delayMillis(100); } }
/** * @brief Send a string over the VCOM port * @details Sends a string over the VCOM port. The string * must be less than 255 characters and terminated by '\0' * * @param data String to send */ void vcom_send(char *data) { //calculate string length uint8_t str_len = strlen(data); //send data CDC_Send_DATA((uint8_t *)data, str_len); }
void cliPrint(char *str) { uint32_t len; uint32_t oldTxed; uint32_t start; uint32_t txed; if (!(usbIsConnected() && usbIsConfigured()) || !str) { return; } len = strlen(str); txed = 0; oldTxed = 0; start = millis(); while (txed < len && (millis() - start < USB_TIMEOUT)) { txed += CDC_Send_DATA((uint8_t*)str + txed, len - txed); if (oldTxed != txed) { start = millis(); } oldTxed = txed; } }
void vDebugTask (void *pvParameters) { int Res; char strMsgDebug[64]; #ifdef DEBUG_OUTPUT_USART InitUSART(UART_DBG, DBG_BAUDRATE); InitDMA(UART_DBG); #endif while(1) { LED_TOGGLE; Res = HandlerCompass(strMsgDebug); strcat(strMsgDebug, "\r\n"); if(!(Res)) { #ifdef DEBUG_OUTPUT_USART USART_Write(UART_DBG, strMsgDebug, strlen(strMsgDebug)); #endif #ifdef DEBUG_OUTPUT_USB if(bDeviceState == CONFIGURED) { CDC_Send_DATA ((unsigned char *)strMsgDebug, strlen(strMsgDebug)); NVIC_EnableIRQ(USB_LP_CAN1_RX0_IRQn); } #endif } _delay_ms(1000); } }
/******************************************************************************* * Function Name : main. * Descriptioan : Main routine. * Input : None. * Output : None. * Return : None. *******************************************************************************/ int main(void) { int i; /* Set the Vector Table base adress at 0x8004000 */ NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x4000); // Disable JTAG not the SWD RCC_APB2PeriphClockCmd( RCC_APB2Periph_AFIO , ENABLE); GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE); Set_System(); Set_USBClock(); USB_Interrupts_Config(); USB_Init(); for(i = 0; i < 27; i++) TestBuffer[i] = i % 9; while (1) { if (bDeviceState == CONFIGURED) { //CDC_Send_DATA ((unsigned char*)TestBuffer,27); //Delay(0xFFF); CDC_Receive_DATA(); // Check to see if we have data yet if (Receive_length != 0) { if (packet_sent == 1) CDC_Send_DATA ((unsigned char*)Receive_Buffer,Receive_length); Receive_length = 0; } } } }
void RadioStart() { bool bTX = false; BoardInit( ); GreenLedBlink(); RedLedBlink(); Radio = RadioDriverInit(); Radio->Init(); Radio->StartRx( ); while(1) { switch(Radio->Process()) { case RF_RX_DONE: Radio->GetRxPacket( Buffer, ( uint16_t* )&BufferSize ); if( BufferSize > 0 ) { if (bDeviceState == CONFIGURED) { if (packet_sent == 1) { CDC_Send_DATA ((unsigned char*)Buffer,BufferSize); } } } GreenLedBlink(); Radio->StartRx( ); break; case RF_TX_DONE: RedLedBlink(); Radio->StartRx( ); bTX = false; break; default: if (bDeviceState == CONFIGURED) { if(bTX) break; if (Receive_length != 0) { Radio->SetTxPacket( (unsigned char*)Receive_Buffer,Receive_length ); CDC_Receive_DATA(); Receive_length = 0; bTX = true; } } break; } } }
int _write(int file, char *ptr, int len) { if(bDeviceState == CONFIGURED) { uint32_t timeout=180000; while((!packet_sent) && timeout--) {} // if loop quit without timeout, success if(timeout) { if(len>63) { uint8_t packetNo, finalPacket, i; packetNo = len/64; finalPacket = len; for(i=0;i<packetNo;i++) { CDC_Send_DATA((uint8_t*)ptr, 63); finalPacket -= 63; ptr += 63; timeout=180000; while(packet_sent==0 && timeout--); if(timeout==0) { bDeviceState = UNCONNECTED; return len; } } CDC_Send_DATA((uint8_t*)ptr,finalPacket); return len; } else { CDC_Send_DATA((uint8_t*)ptr,len); return len; } } // loop quit because of timeout, fail else { return -1; } } else { return len; } }
/********************************************************************************************* Function name : VCP_PutStr Author : Grant Phillips Date Modified : 10/04/2014 Compiler : Keil ARM-MDK (uVision V4.70.0.0) Description : Prints a string to the Virtual COM Port Special Note(s) : NONE Parameters : str - string (char array) to print Return value : NONE *********************************************************************************************/ void VCP_PutStr(char *str) { /* if (packet_sent == 1) //make sure previous data has been sent CDC_Send_DATA((unsigned char*)str, strlen(str)); */ while(packet_sent != 1) //make sure previous data has been sent {} CDC_Send_DATA((unsigned char*)str, strlen(str)); }
void usbPrint(serialPort_t *instance, uint8_t c) { uint32_t txed; uint32_t start = millis(); if (!(usbIsConnected() && usbIsConfigured())) { return; } do { txed = CDC_Send_DATA((uint8_t*)&c, 1); } while (txed < 1 && (millis() - start < USB_TIMEOUT)); }
void sendROSData(char ID, int data1, int data2, float data3) { struct ROSDataDef ROSDataValues;//Initialization of the structure struct ROSDataDef *ROSDataValuesPtr; ROSDataValuesPtr = &ROSDataValues; //Asignment of the values of each member of the struct ROSDataValues.ID = ID; ROSDataValues.data1 = data1; ROSDataValues.data2 = data2; ROSDataValues.data3 = data3; if(bDeviceState == CONFIGURED) { CDC_Send_DATA((uint8_t*)ROSDataValuesPtr,13); // 9 bytes is the length of the struct } }
uint8_t * CDC_Loopback(void) { if (bDeviceState == CONFIGURED) { CDC_Receive_DATA(); if (USB_PacketReceived) { USB_PacketReceived = false; if (USB_ReceiveLength >= CDC_RX_BUFFER_SIZE) { // adjust for trailing string delimiter USB_ReceiveLength--; } USB_ReceiveBuffer[USB_ReceiveLength] = '\0'; if (USB_PacketSent) { CDC_Send_DATA((unsigned char*) USB_ReceiveBuffer, USB_ReceiveLength); } return &USB_ReceiveBuffer[0]; } } return NULL ; }
void SysTick_Handler(void) { uint8_t Delay_Ticks = 1; uint8_t* Buf; uint32_t idx; if (Freq == 20) { Delay_Ticks = 100; } if (Freq == 2000) { Delay_Ticks = 1; } DataReady ++; if (DataReady >= Delay_Ticks) { STM_EVAL_LEDOn(LED3); Demo_GyroReadAngRate((float*)Buffer); Demo_CompassReadMag((float*)MagBuffer); Demo_CompassReadAcc((float*)AccBuffer); Buf = (uint8_t*)Buffer; for(idx = 0; idx < 12; idx++) Send_Buffer[idx] = Buf[idx]; Buf = (uint8_t*)MagBuffer; for(idx = 12; idx < 24; idx++) Send_Buffer[idx] = Buf[idx-12]; Buf = (uint8_t*)AccBuffer; for(idx = 24; idx < 36; idx++) Send_Buffer[idx] = Buf[idx-24]; Buf = (uint8_t*)ADC_Result; for(idx = 36; idx < 54; idx++) Send_Buffer[idx] = Buf[idx-36]; CDC_Send_DATA ((unsigned char*)Send_Buffer,54); DataReady = 0; STM_EVAL_LEDOff(LED3); } }
void usbIface::writeBytes( void ) { uint8_t byte; uint8_t buf[64]; uint8_t cnt = 0; if(!(GetEPTxStatus(ENDP1)==EP_TX_VALID)) { while ( !m_out->isempty() && cnt < 64 ) { m_out->dequeue( &byte ); buf[cnt] = byte; cnt++; } CDC_Send_DATA(buf, cnt); } /* if(!(GetEPTxStatus(ENDP1)==EP_TX_VALID) && !m_out->isempty()) { m_out->dequeue( &byte ); // Not checking for errors this is bad CDC_Send_DATA(&byte, 1); } */ }
static void usbVcpWriteBuf(serialPort_t *instance, void *data, int count) { UNUSED(instance); if (!(usbIsConnected() && usbIsConfigured())) { return; } uint32_t start = millis(); for (uint8_t *p = data; count > 0; ) { uint32_t txed = CDC_Send_DATA(p, count); count -= txed; p += txed; if (millis() - start > USB_TIMEOUT) { break; } } }
static bool usbVcpFlush(vcpPort_t *port) { uint8_t count = port->txAt; port->txAt = 0; if (count == 0) { return true; } if (!usbIsConnected() || !usbIsConfigured()) { return false; } uint32_t txed; uint32_t start = millis(); do { txed = CDC_Send_DATA(port->txBuf, count); } while (txed != count && (millis() - start < USB_TIMEOUT)); return txed == count; }
/******************************************************************************* * Function Name : main. * Descriptioan : Main routine. * Input : None. * Output : None. * Return : None. *******************************************************************************/ int main(void) { Set_System(); Set_USBClock(); USB_Interrupts_Config(); USB_Init(); while (1) { if (bDeviceState == CONFIGURED) { CDC_Receive_DATA(); /*Check to see if we have data yet */ if (Receive_length != 0) { if (packet_sent == 1) CDC_Send_DATA ((unsigned char*)Receive_Buffer,Receive_length); Receive_length = 0; } } } }
int main() { __disable_irq(); // ------------------------------------------------------------------ // Desc: System Clk Config // // HCLK = PCLK2 = SYSCLK = 72M // PCLK1 = 36M // ------------------------------------------------------------------ SystemInit(); // SysTick_Config(SystemCoreClock / 1000); #if 0 // ------------------------------------------------------------------ // Desc: CLK OUTPUT for measure // ------------------------------------------------------------------ //PA8 alternate mode GPIO_InitTypeDef GPIO_InitStructure; RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_0); RCC_MCOConfig(RCC_MCOSource_PLLCLK,RCC_MCOPrescaler_1); // while(1); #endif // ------------------------------------------------------------------ // Desc: Debug // ------------------------------------------------------------------ DebugInit(); // d_printf("UART debug Init OK\r\n"); // ------------------------------------------------------------------ // Desc: ILX511 CLK ON // ------------------------------------------------------------------ #if 1 { GPIO_InitTypeDef GPIO_InitStructure; RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOB, &GPIO_InitStructure); } #endif #if 0 ILX_Init(); __enable_irq(); ILX_CLKOn(); delay_nms(50);//CCD power up ConvertOn(); #endif #if 0 d_printf("\r\n-------------------\r\n"); { uint32_t i; for(i=32;i<2048+32;i++) d_printf("0x%04X ",data[i]); } #endif #if 0 USBInit(); #endif #if 0 while (1) { if (bDeviceState == CONFIGURED) { CDC_Receive_DATA(); if (Receive_length != 0) { if (packet_sent == 1) CDC_Send_DATA ((unsigned char*)Receive_Buffer,Receive_length); Receive_length = 0; } } } #endif while(1); return 0; }
/* State machine */ void Ptx_Process(Ptx_Control *ptp) { switch (ptp->state) { case PTX_WAIT_RQST: if (ptp->curr_rqst) /* if a request is pending */ { if (ptp->curr_rqst->type == PTX_CTRL_CH_RQST) { CDC_Send_DATA(&(ptp->curr_rqst->data), 1); Ptx_Ckout_Curr_Rqst(ptp); } else if (ptp->curr_rqst->type == PTX_CMD_RQST) { uint8_t cmd[3]; cmd[0] = HASH; cmd[1] = ptp->curr_rqst->data; cmd[2] = ~(ptp->curr_rqst->data); CDC_Send_DATA(cmd, 3); ptp->state = PTX_WAIT_ACK; } else if (ptp->curr_rqst->type == PTX_PKG_RQST) { uint8_t header[2]; header[0] = SOH; header[1] = *(ptp->sec_num); CDC_Send_DATA(header, 2); ptp->state = PTX_WAIT_1ST_SYN; }; }; break; case PTX_WAIT_ACK: if (Prx_Ctrl_Ch_Avail(&c_prx)) { uint8_t ctrl_ch = Prx_Get_Ctrl_Ch(&c_prx); if ( ctrl_ch == ACK ) { if (ptp->curr_rqst->type == PTX_PKG_RQST) (*(ptp->sec_num))++; Ptx_Ckout_Curr_Rqst(ptp); }; ptp->state = PTX_WAIT_RQST; }; break; case PTX_WAIT_1ST_SYN: if (Prx_Ctrl_Ch_Avail(&c_prx)) { uint8_t header[3]; uint8_t *header_p = header; uint8_t n = Prx_Get_Ctrl_Ch(&c_prx); if ( n == NAK ) { ptp->state = PTX_WAIT_RQST; break; }; if (ptp->curr_rqst->pkg.length == 256) /* a 256 length is represented by a zero */ header[0] = 0; else header[0] = ptp->curr_rqst->pkg.length; header[1] = ptp->curr_rqst->pkg.opts; header[2] = ptp->curr_rqst->pkg.ptsf; CDC_Send_DATA(header, 3); for (n = 3; n; n--, header_p++) ptp->cksum += *header_p; ptp->curr_rqst->sent_data_num = 0; ptp->state = PTX_WAIT_2ND_SYN; }; break; case PTX_WAIT_2ND_SYN: if (Prx_Ctrl_Ch_Avail(&c_prx)) { uint8_t n = Prx_Get_Ctrl_Ch(&c_prx); uint8_t data_length; uint8_t *data_p = &(ptp->curr_rqst->pkg.data[ptp->curr_rqst->sent_data_num]); /* points to the first byte to be sent */ if ( n == NAK ) { ptp->state = PTX_WAIT_RQST; break; }; if ( (ptp->curr_rqst->pkg.length - ptp->curr_rqst->sent_data_num) >= VCP_BUF_OUT_LEN ) /* if ( (pkg.length - sent_data_num) >= VCP_BUF_OUT_LEN */ { data_length = VCP_BUF_OUT_LEN; CDC_Send_DATA(data_p, VCP_BUF_OUT_LEN); ptp->curr_rqst->sent_data_num += VCP_BUF_OUT_LEN; } else { data_length = ptp->curr_rqst->pkg.length - ptp->curr_rqst->sent_data_num; CDC_Send_DATA(data_p, data_length); ptp->curr_rqst->sent_data_num += data_length; }; for (n = data_length; n; n--, data_p++) ptp->cksum += *data_p; if (ptp->curr_rqst->sent_data_num < ptp->curr_rqst->pkg.length) break; ptp->state = PTX_WAIT_3RD_SYN; }; break; case PTX_WAIT_3RD_SYN: if (Prx_Ctrl_Ch_Avail(&c_prx)) { uint8_t n = Prx_Get_Ctrl_Ch(&c_prx); if ( n == NAK ) { ptp->state = PTX_WAIT_RQST; break; }; CDC_Send_DATA(&ptp->cksum, 1); ptp->cksum = 0; ptp->state = PTX_WAIT_ACK; }; }; }