static int cin_wait(unsigned timeout) { int ret = -1; u8 c; vTaskDelay( timeout / portTICK_RATE_MS ); if (VCP_get_char(&c)) { ret = (int)c; } return ret; }
void TIM3_IRQHandler(void) { if(TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) { A = !GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_0) * 0xFF; S = !GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_1) * 0xFF; D = !GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_2) * 0xFF; F = !GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_3) * 0xFF; if(VCP_get_char(&theByte)) { if(theByte == 'H') { if(recieved) { VCP_put_char('I'); connected = true; recieved = false; }else { VCP_put_char('I'); recieved = true; } }else if (theByte == 'B') { if(recieved) { VCP_put_char('Y'); connected = false; recieved = false; }else { VCP_put_char('Y'); recieved = true; } } } if(connected) { VCP_put_char(0x38); VCP_put_char(A); VCP_put_char(S); VCP_put_char(D); VCP_put_char(F); } // wyzerowanie flagi wyzwolonego przerwania TIM_ClearITPendingBit(TIM3, TIM_IT_Update); } }
int _read(int file, char *ptr, int len) { if (file != 0) { return 0; } // Use USB CDC Port for stdin while(!VCP_get_char((uint8_t*)ptr)){}; // Echo typed characters VCP_put_char((uint8_t)*ptr); return 1; }
/** * @brief Receive from usb */ void communication_receive_usb(void) { mavlink_message_t msg; mavlink_status_t status = { 0 }; uint8_t character; while (VCP_get_char(&character)) { /* Try to get a new message */ if (mavlink_parse_char(MAVLINK_COMM_2, character, &msg, &status)) { /* Handle message */ handle_mavlink_message(MAVLINK_COMM_2, &msg); } /* And get the next one */ } }
int _read(int file, char *ptr, int len) { char *p = ptr; if (file != 0) { return 0; } if (!VCP_get_char_test()) { return 0; } while(VCP_get_char_test() && len > 0) { VCP_get_char((uint8_t*)p++); len -= 1; } return p - ptr; }
int main(void) { /* Set up the system clocks */ SystemInit(); /* Initialize USB, IO, SysTick, and all those other things you do in the morning */ init(); while (1) { /* Blink the orange LED at 1Hz */ if (500 == ticker) { GPIOD->BSRRH = GPIO_Pin_13; } else if (1000 == ticker) { ticker = 0; GPIOD->BSRRL = GPIO_Pin_13; } /* If there's data on the virtual serial port: * - Send it to usart * - Turn the green LED on for 10ms */ uint8_t theByte; if (VCP_get_char(&theByte)) { //VCP_put_char(theByte); USART_SendData(USART1, theByte); GPIOD->BSRRL = GPIO_Pin_12; downTicker = 10; } if (0 == downTicker) { GPIOD->BSRRH = GPIO_Pin_12; } } return 0; }
void car_conn_rcv_buf_clear(void) { uint8_t tmp_char; while (VCP_get_char(&tmp_char)); }
int main(void) { uint8_t acc_x, acc_y, acc_z; tickerSendData = 0; LIS302DL_InitTypeDef LIS302DL_InitStruct; /* Set configuration of LIS302DL*/ LIS302DL_InitStruct.Power_Mode = LIS302DL_LOWPOWERMODE_ACTIVE; LIS302DL_InitStruct.Output_DataRate = LIS302DL_DATARATE_100; LIS302DL_InitStruct.Axes_Enable = LIS302DL_X_ENABLE | LIS302DL_Y_ENABLE | LIS302DL_Z_ENABLE; LIS302DL_InitStruct.Full_Scale = LIS302DL_FULLSCALE_2_3; LIS302DL_InitStruct.Self_Test = LIS302DL_SELFTEST_NORMAL; LIS302DL_Init(&LIS302DL_InitStruct); GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; /* SPI SCK pin configuration */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; GPIO_Init(GPIOB, &GPIO_InitStructure); /* SPI MOSI pin configuration */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15; GPIO_Init(GPIOB, &GPIO_InitStructure); /* SPI MISO pin configuration */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14; GPIO_Init(GPIOB, &GPIO_InitStructure); /* Set up the system clocks */ SystemInit(); /* Initialize USB, IO, SysTick, and all those other things you do in the morning */ init(); while (1) { LIS302DL_Read(&acc_x, LIS302DL_OUT_X_ADDR, 1); LIS302DL_Read(&acc_y, LIS302DL_OUT_Y_ADDR, 1); LIS302DL_Read(&acc_z, LIS302DL_OUT_Z_ADDR, 1); if ((tickerSendData) >= 50){ tickerSendData=0; VCP_put_char(acc_x); } /* Blink the orange LED at 1Hz */ if (500 == ticker) { GPIOD->BSRRH = GPIO_Pin_13; } else if (1000 == ticker) { ticker = 0; GPIOD->BSRRL = GPIO_Pin_13; } /* If there's data on the virtual serial port: * - Echo it back * - Turn the green LED on for 10ms */ uint8_t theByte; if (VCP_get_char(&theByte)) { GPIOD->BSRRL = GPIO_Pin_12; downTicker = 10; } if (0 == downTicker) { GPIOD->BSRRH = GPIO_Pin_12; } } return 0; }
void vTaskVCP(void *pvParameters) { static u32 address = 0; static uint8_t usb_inited = 0; uint8_t c; int arg; uint8_t tmpbuf[EERROM_SIZE]; for(;;) { //Give the change to the OS scheduling. It is really a fool idea. Change me! //TODO - should use semaphore vTaskDelay( 1 / portTICK_RATE_MS ); // xSemaphoreTake(onVCPSemaphore, portMAX_DELAY); if(usb_inited == 0) { GPIO_InitTypeDef gpio; //This is a trick to perform a USB re-enumerate gpio.GPIO_Pin = GPIO_Pin_12; gpio.GPIO_Speed = GPIO_Speed_100MHz; gpio.GPIO_Mode = GPIO_Mode_OUT; gpio.GPIO_OType = GPIO_OType_PP; gpio.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOB, &gpio); GPIO_SetBits(GPIOB, GPIO_Pin_12 ); vTaskDelay(500 / portTICK_RATE_MS); GPIO_ResetBits(GPIOB, GPIO_Pin_12 ); // Initialize USB VCP. Do it ASAP USBD_Init(&USB_OTG_dev, USB_OTG_FS_CORE_ID, &USR_desc, &USBD_CDC_cb, &USR_cb); usb_inited = 1; } if (VCP_get_char(&c)) { switch(c) { case PROTO_GET_SYNC: if (cin_wait(usb_vcp_timeout) != PROTO_EOC) goto cmd_bad; break; case PROTO_GET_DEVICE: arg = cin_wait(usb_vcp_timeout); if (arg < 0) goto cmd_bad; if (cin_wait(usb_vcp_timeout) != PROTO_EOC) goto cmd_bad; switch (arg) { case PROTO_DEVICE_BL_REV: VCP_send_str((uint8_t*)&bl_proto_rev); break; default: goto cmd_bad; } break; case PROTO_START_TRANS: if (cin_wait(usb_vcp_timeout) != PROTO_EOC) goto cmd_bad; address = 0; memset(tmpbuf,0,EERROM_SIZE); break; case PROTO_SET_PARAMS: arg = cin_wait(usb_vcp_timeout); if (arg < 0) goto cmd_bad; // sanity-check arguments if (arg % 4) goto cmd_bad; if ((address + arg) > EERROM_SIZE) goto cmd_bad; for (int i = 0; i < arg; i++) { c = cin_wait(usb_vcp_timeout); if (c < 0) goto cmd_bad; tmpbuf[address++] = c; } break; case PROTO_END_TRANS: if (cin_wait(usb_vcp_timeout) != PROTO_EOC) goto cmd_bad; //ensure we receive right size if(address == EERROM_SIZE) { memcpy(eeprom_buffer.c,tmpbuf,EERROM_SIZE); // bool ret = StoreParams(); // if(!ret) // { // //TODO - handle flash write error here // } } break; case PROTO_SAVE_TO_FLASH: if (cin_wait(usb_vcp_timeout) != PROTO_EOC) goto cmd_bad; //ensure we receive right size if(address == EERROM_SIZE) { bool ret = StoreParams(); if(!ret) { //TODO - handle flash write error here } } break; case PROTO_GET_PARAMS: if (cin_wait(usb_vcp_timeout) != PROTO_EOC) goto cmd_bad; for (int i = 0; i < EERROM_SIZE; i++) { VCP_put_char(eeprom_buffer.c[i]); //TM_USB_VCP_Putc(testbuf[i]); //vTaskDelay( 1 / portTICK_RATE_MS ); } break; case PROTO_BL_UPLOAD: if (cin_wait(usb_vcp_timeout) != PROTO_EOC) goto cmd_bad; sync_response(); vTaskDelay( 200 / portTICK_RATE_MS ); GPIO_SetBits(GPIOB,GPIO_Pin_12); vTaskDelay( 500 / portTICK_RATE_MS ); GPIO_ResetBits(GPIOB,GPIO_Pin_12); __DSB(); /* Ensure all outstanding memory accesses included buffered write are completed before reset */ SCB->AIRCR = ((0x5FA << SCB_AIRCR_VECTKEY_Pos) | (SCB->AIRCR & SCB_AIRCR_PRIGROUP_Msk) | SCB_AIRCR_SYSRESETREQ_Msk); /* Keep priority group unchanged */ __DSB(); /* Ensure completion of memory access */ while(1); break; default: continue; } // send the sync response for this command sync_response(); continue; } vTaskDelay( 10 / portTICK_RATE_MS ); continue; cmd_bad: // send an 'invalid' response but don't kill the timeout - could be garbage invalid_response(); continue; cmd_fail: // send a 'command failed' response but don't kill the timeout - could be garbage failure_response(); continue; } }