int main (void) { MBED_HOSTTEST_TIMEOUT(20); MBED_HOSTTEST_SELECT(default_auto); MBED_HOSTTEST_DESCRIPTION(Mail messaging); MBED_HOSTTEST_START("RTOS_6"); 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; } } } MBED_HOSTTEST_RESULT(result); return 0; }
int main (void) { GREENTEA_SETUP(20, "default_auto"); Thread thread(osPriorityNormal, STACK_SIZE); thread.start(send_thread); 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; }
/** 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(); }
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 }