Skip to content

Instantly share code, notes, and snippets.

@lonesometraveler
lonesometraveler / mbed_chrono.cpp
Created June 15, 2020 19:55
[mbed OS6] elapsed time
#include "mbed.h"
int main() {
Timer t;
t.start();
ThisThread::sleep_for(1s);
t.stop();
auto f = chrono::duration<float>(t.elapsed_time()).count();
auto s = chrono::duration_cast<chrono::seconds>(t.elapsed_time()).count();
auto ms = chrono::duration_cast<chrono::milliseconds>(t.elapsed_time()).count();
@lonesometraveler
lonesometraveler / k64f_ethernet_uart3_conflict.md
Last active April 16, 2020 13:36
[Mbed] How to solve a conflict between Ethernet and UART3 on K64

Mbed EthernetInterface and UART3 conflict on K64F

Due to a clock config conflict, the default config does not allow Ethernet and UART3 to be used at the same. See this thread.

A workaround for Mbed projects is to disable PORTC clock in mbed-os/features/netsocket/emac-drivers/TARGET_Freescale_EMAC/TARGET_K64F/hardware_init_MK64F12.c.

// CLOCK_EnableClock(kCLOCK_PortC);             // commented
CLOCK_EnableClock(kCLOCK_PortB);
/* Affects PORTC_PCR16 register */
@lonesometraveler
lonesometraveler / main.cpp
Created April 1, 2020 14:49
Mbed EventQueue example 3: Mbed OS6 UnbufferedSerial Interrupt Callback and deferred printf()
#include "mbed.h"
// EventQueue
EventQueue *queue = mbed_event_queue();
// Create a DigitalOutput object to toggle an LED whenever data is received.
static DigitalOut led(LED1);
// Create a UnbufferedSerial object with a default baud rate.
static UnbufferedSerial serial_port(USBTX, USBRX);
@lonesometraveler
lonesometraveler / main.cpp
Created March 31, 2020 11:21
Mbed EventQueue example 1: Ticker Callback and deferred printf()
// Execution of certain function in an ISR context is not safe.
// For example, printf() in an interrupt context causes a Mutex error.
// Using Mbed's EventQueue, you can defer execution of code from an interrupt context to a user context.
// More about EventQueue: https://os.mbed.com/docs/mbed-os/v5.15/tutorials/the-eventqueue-api.html
#include "mbed.h"
Ticker ticker;
EventQueue *queue = mbed_event_queue(); // event queue
int counter = 0;
@lonesometraveler
lonesometraveler / main.cpp
Created March 31, 2020 11:20
Mbed EventQueue example 2: Serial Interrupt Callback and deferred printf()
// Execution of certain function in an ISR context is not safe.
// For example, printf() in an interrupt context causes a Mutex error.
// Using Mbed's EventQueue, you can defer execution of code from an interrupt context to a user context.
// More about EventQueue: https://os.mbed.com/docs/mbed-os/v5.15/tutorials/the-eventqueue-api.html
#include "mbed.h"
EventQueue *queue = mbed_event_queue(); // event queue
RawSerial pc(USBTX, USBRX);