Sunteți pe pagina 1din 10

Debugging using printf() statements

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:

Serial pc(SERIAL_TX, SERIAL_RX);

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;

printf("Hello World !\n");

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.

Selecting the COM port


Exp 1 : Thread
#include "mbed.h"

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;

// Blink function toggles the led in a long running loop


void blink(DigitalOut *led) {
while (running) {
*led = !*led;
printf("i m inthread\n");
wait(1);
}
}

// Spawns a thread to run blink for 5 seconds


int main() {
thread.start(callback(blink, &led1));
wait(10);
running = false;
thread.join();
}
EXP 3 : Queue
#include "mbed.h"
typedef struct {
float voltage; /* AD result of measured voltage */
float current; /* AD result of measured current */
uint32_t counter; /* A counter value */
} message_t;

MemoryPool<message_t, 16> mpool;


Queue<message_t, 16> queue;
Thread thread;

/* 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;

void test_thread(void const *name) {


while (true) {
one_slot.wait();
printf("%s\n\r", (const char*)name);
wait(1);
one_slot.release();
}
}

int main (void) {


t2.start(callback(test_thread, (void *)"Th 2"));
t3.start(callback(test_thread, (void *)"Th 3"));

test_thread((void *)"Th 1");


}
Exp 5 : Mutex
#include "mbed.h"

Mutex stdio_mutex;
Thread t2;
Thread t3;

void notify(const char* name, int state) {


stdio_mutex.lock();
printf("%s: %d\n\r", name, state);
stdio_mutex.unlock();
}
void test_thread(void const *args) {
while (true) {
notify((const char*)args, 0); wait(1);
notify((const char*)args, 1); wait(1);
}
}

int main() {
t2.start(callback(test_thread, (void *)"Th 2"));
t3.start(callback(test_thread, (void *)"Th 3"));

test_thread((void *)"Th 1");


}
Exp 6 :- Mail box

#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;

Mail<mail_t, 16> mail_box;


Thread thread;

void send_thread (void) {


uint32_t i = 0;
while (true) {
i++; // fake data update
mail_t *mail = mail_box.alloc();
mail->voltage = (i * 0.1) * 33;
mail->current = (i * 0.1) * 11;
mail->counter = i;
mail_box.put(mail);
wait(1);
}
}
int main (void) {
thread.start(callback(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);
}
}
}
Exp 7 : event flags
#include "mbed.h"

#define SAMPLE_FLAG1 (1UL << 0)


#define SAMPLE_FLAG2 (1UL << 9)

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);
}
}

S-ar putea să vă placă și