Documente Academic
Documente Profesional
Documente Cultură
An easy way to inspect what your application is doing is to augment your application with log
statements. In Arm Mbed, you can use a serial connection to send feedback from your
development board back to your computer. This uses the same USB cable that you use to
program your device.
Prerequisites
Install the serial port driver for your development board:
For ST boards: ST Link Driver.( https://os.mbed.com/teams/ST/wiki/ST-Link-Driver)
For all other boards: Arm Mbed Windows serial port driver - not required for Windows 10.
You also need a serial monitor:
TeraTerm. (https://osdn.net/projects/ttssh2/releases/)
Getting started
To send data over the serial connection, use the Serial object.
Example program
This program blinks the LED on your development board and prints a message every time the
LED changes state:
______________________________________________________________________________
#include "mbed.h"
/*------------------------------------------------------------------------------
Before to use this example, ensure that you an hyperterminal installed on your
computer. More info here: https://developer.mbed.org/handbook/Terminals
The default serial comm port uses the SERIAL_TX and SERIAL_RX pins (see their
definition in the PinNames.h file).
The default serial configuration in this case is 9600 bauds, 8-bit data, no parity
If you want to change the baudrate for example, you have to redeclare the
serial object in your code:
Then, you can modify the baudrate and print like this:
pc.baud(115200);
pc.printf("Hello World !\n");
------------------------------------------------------------------------------*/
DigitalOut led(LED1);
int main()
{
int i = 1;
while(1) {
wait(1); // 1 second
led = !led; // Toggle LED
printf("This program runs since %d seconds.\n", i++);
}
}
---------------------------------------------------------------------------------------------------------------------
Compile this program, and flash it on your development board. You now can inspect these
messages using a serial monitor.
Seeing the log messages
Windows
1. Open TeraTerm.
2. Click File > New Connection.
3. Select the Serial radio button.
4. Choose your development board from the drop-down menu (often called mbed Serial
Port or STLink Virtual Port).
5. Click OK.
6. Log messages appear in the main window.
DigitalOut led1(LED1);
DigitalOut led2(LED2);
Thread thread;
void led2_thread() {
while (true) {
led2 = !led2;
wait(1);
}
}
int main() {
thread.start(led2_thread);
while (true) {
led1 = !led1;
wait(0.5);
}
}
Exp2 :Call back thread example
#include "mbed.h"
Thread thread;
DigitalOut led1(LED2);
volatile bool running = true;
/* Send Thread */
void send_thread (void) {
uint32_t i = 0;
while (true) {
i++; // fake data update
message_t *message = mpool.alloc();
message->voltage = (i * 0.1) * 33;
message->current = (i * 0.1) * 11;
message->counter = i;
queue.put(message);
wait(1);
}
}
int main (void) {
thread.start(callback(send_thread));
while (true) {
osEvent evt = queue.get();
if (evt.status == osEventMessage) {
message_t *message = (message_t*)evt.value.p;
printf("\nVoltage: %.2f V\n\r" , message->voltage);
printf("Current: %.2f A\n\r" , message->current);
printf("Number of cycles: %u\n\r", message->counter);
mpool.free(message);
}
}
}
Exp 4: Semaphore
#include "mbed.h"
Semaphore one_slot(1);
Thread t2;
Thread t3;
Mutex stdio_mutex;
Thread t2;
Thread t3;
int main() {
t2.start(callback(test_thread, (void *)"Th 2"));
t3.start(callback(test_thread, (void *)"Th 3"));
#include "mbed.h"
/* Mail */
typedef struct {
float voltage; /* AD result of measured voltage */
float current; /* AD result of measured current */
uint32_t counter; /* A counter value */
} mail_t;
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);
}
}
}
Exp 7 : event flags
#include "mbed.h"
EventFlags event_flags;
void worker_thread_fun()
{
printf("Waiting for any flag from 0x%08lx.\r\n", SAMPLE_FLAG1 | SAMPLE_FLAG2);
uint32_t flags_read = 0;
while (true) {
flags_read = event_flags.wait_any(SAMPLE_FLAG1 | SAMPLE_FLAG2);
printf("Got: 0x%08lx\r\n", flags_read);
}
}
int main()
{
Thread worker_thread;
worker_thread.start(mbed::callback(worker_thread_fun));
while (true) {
wait(1.0);
event_flags.set(SAMPLE_FLAG1);
wait(0.5);
event_flags.set(SAMPLE_FLAG2);
}
}