/** Test Serial / CDC line coding change * * Given the device transmits a set of line coding params to host * When the host updates serial port settings * Then line_coding_changed() callback is called * and the line coding is set as expected */ void test_serial_line_coding_change() { TestUSBSerial usb_serial(USB_SERIAL_VID, USB_SERIAL_PID, 1, usb_dev_sn); usb_serial.connect(); greentea_send_kv(MSG_KEY_CHANGE_LINE_CODING, MSG_VALUE_DUMMY); #if LINUX_HOST_DTR_FIX usb_serial.wait_ready(); wait_ms(LINUX_HOST_DTR_FIX_DELAY_MS); #endif usb_serial.wait_ready(); usb_serial.attach(line_coding_changed_cb); size_t num_line_codings = sizeof test_codings / sizeof test_codings[0]; line_coding_t *lc_prev = &default_lc; line_coding_t *lc_expected = NULL; line_coding_t *lc_actual = NULL; int num_expected_callbacks, rc; for (size_t i = 0; i < num_line_codings; i++) { lc_expected = &(test_codings[i]); num_expected_callbacks = lc_prev->get_num_diffs(*lc_expected); rc = usb_serial.printf("%06i,%02i,%01i,%01i", lc_expected->baud, lc_expected->bits, lc_expected->parity, lc_expected->stop); TEST_ASSERT_EQUAL_INT(LINE_CODING_STRLEN, rc); // The pyserial Python module does not update all line coding params // at once. It updates params one by one instead, and since every // update is followed by port reconfiguration we get multiple // calls to line_coding_changed callback on the device. while (num_expected_callbacks > 0) { num_expected_callbacks--; osEvent event = lc_mail.get(); TEST_ASSERT_EQUAL_UINT32(osEventMail, event.status); lc_actual = (line_coding_t *) event.value.p; if (lc_expected->get_num_diffs(*lc_actual) == 0) { break; } else if (num_expected_callbacks > 0) { // Discard lc_actual only if there is still a chance to get new // set of params. lc_mail.free(lc_actual); } } TEST_ASSERT_EQUAL_INT(lc_expected->baud, lc_actual->baud); TEST_ASSERT_EQUAL_INT(lc_expected->bits, lc_actual->bits); TEST_ASSERT_EQUAL_INT(lc_expected->parity, lc_actual->parity); TEST_ASSERT_EQUAL_INT(lc_expected->stop, lc_actual->stop); lc_mail.free(lc_actual); lc_prev = lc_expected; } // Wait for the host to close its port. while (usb_serial.ready()) { wait_ms(1); } usb_serial.disconnect(); }
/* * Starting thread * @return void * */ void UComDriverOut::start() { while(true) { osEvent evt = comDriverOutMail.get(); if (evt.status == osEventMail) { m_led = !m_led; UMsgHandlerMailType *mail = (UMsgHandlerMailType*)evt.value.p; if(USE_LWIP) { m_udpSocket.init(); //m_udpClient.set_address("192.168.15.102",8); //TODO msghandler mail doit avoir le endpoint. //printf("UDP Socket is sending response to %s on port %d\n\r", m_udpClient.get_address(), m_udpClient.get_port()); printf("UDP Socket is sending response to %s on port %d\n\r", mail->endPoint.get_address(), mail->endPoint.get_port()); m_rxCount = m_udpSocket.sendTo(mail->endPoint, mail->msg, strlen(mail->msg)); printf("Send succeed. %d bytes sended\n\r", m_rxCount); } else { m_uart.puts(mail->msg); } comDriverOutMail.free(mail); } } }
// Dump contents of feature (6) MAIL queue to the serial connection to the PC. // (Data will be passed through the MBED USB connection) // content is also dumped into a csv file // car mail semaphore used to protect messages // Repetition rate 0.05 Hz = 20 seconds void dumpContents(void const *args){ while(true){ CAR_MAIL_SEM.wait(); while(write > read){ osEvent evt = mail_box.get(); if (evt.status == osEventMail) { mail_t *mail = (mail_t*)evt.value.p; // values sent to csv file FILE *fp = fopen("/local/Car_Values.csv", "a"); fprintf(fp,"%f ,", mail->speedVal); fprintf(fp,"%f ,", mail->accelerometerVal); fprintf(fp,"%f ", mail->breakVal); fprintf(fp,"\r\n"); fclose(fp); // values sent to serial port serial.printf("average speed: %f ,", mail->speedVal); serial.printf("break value: %f ,", mail->breakVal); serial.printf("acceleration: %f ,", mail->accelerometerVal); serial.printf("\r\n"); mail_box.free(mail); read++; } } CAR_MAIL_SEM.release(); Thread::wait(20000); } }
int main (void) { GREENTEA_SETUP(20, "default_auto"); Thread thread(send_thread, NULL, osPriorityNormal, STACK_SIZE); bool result = true; int result_counter = 0; while (true) { osEvent evt = mail_box.get(); if (evt.status == osEventMail) { mail_t *mail = (mail_t*)evt.value.p; const float expected_voltage = CREATE_VOLTAGE(mail->counter); const float expected_current = CREATE_CURRENT(mail->counter); // Check using macros if received values correspond to values sent via queue bool expected_values = (expected_voltage == mail->voltage) && (expected_current == mail->current); result = result && expected_values; const char *result_msg = expected_values ? "OK" : "FAIL"; printf("%3d %.2fV %.2fA ... [%s]\r\n", mail->counter, mail->voltage, mail->current, result_msg); mail_box.free(mail); if (result == false || ++result_counter == QUEUE_SIZE) { break; } } } GREENTEA_TESTSUITE_RESULT(result); return 0; }
int main (void) { Thread thread(send_thread); while (true) { osEvent evt = mail_box.get(); if (evt.status == osEventMail) { mail_t *mail = (mail_t*)evt.value.p; printf("\nVoltage: %.2f V\n\r" , mail->voltage); printf("Current: %.2f A\n\r" , mail->current); printf("Number of cycles: %u\n\r", mail->counter); mail_box.free(mail); } } }
//Normal priority thread (consumer) void thread1() { static int count = 0; while (true) { //Block on the queue osEvent evt = mail_box.get(); //Check status if (evt.status == osEventMail) { message_t *pMessage = (message_t*)evt.value.p; //This is the pointer (address) //Make a copy message_t msg(pMessage->adcValue, pMessage->sw1State, pMessage->sw2State); //We are done with this, so give back the memory to the pool mail_box.free(pMessage); //Echo to the terminal printf("ADC Value: %.2f\t", msg.adcValue); printf("SW1: %u\t", msg.sw1State); printf("SW2: %u\n\r", msg.sw2State); //Update state if ((msg.sw1State == 1) && (msg.sw2State == 1)) { count++; } else { count = 0; } if (count == 10) { greenLED = !greenLED; count = 0; } } else { printf("ERROR: %x\n\r", evt.status); } } //end while }
// Call this on precise intervals void adcISR() { //Read sample - make a copy float sample = adcIn; //Grab switch state uint32_t switch1State = sw1; uint32_t switch2State = sw2; //Allocate a block from the memory pool message_t *message = mail_box.alloc(); if (message == NULL) { //Out of memory printf("Out of memory\n\r"); redLED = 1; return; } //Fill in the data message->adcValue = sample; message->sw1State = switch1State; message->sw2State = switch2State; //Write to queue osStatus stat = mail_box.put(message); //Note we are sending the "pointer" //Check if succesful if (stat == osErrorResource) { redLED = 1; printf("queue->put() Error code: %4Xh, Resource not available\r\n", stat); mail_box.free(message); return; } }