Skip to content

Instantly share code, notes, and snippets.

@YuuichiAkagawa
Created December 14, 2014 11:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save YuuichiAkagawa/fa01300bb6947c964785 to your computer and use it in GitHub Desktop.
Save YuuichiAkagawa/fa01300bb6947c964785 to your computer and use it in GitHub Desktop.
// Include ---------------------------------------------------------------------------------------
#include "mbed.h"
#include "rtos.h"
#define BAUD(x) pcx.baud(x)
#define GETC(x) pcx.getc(x)
#define PUTC(x) pcx.putc(x)
#define PRINTF(...) pcx.printf(__VA_ARGS__)
#define READABLE(x) pcx.readable(x)
#define TIME_BASE_S 0.01
#define TIME_BASE_MS ( TIME_BASE_S * 1000)
#define RATE 0.1
// LED's
DigitalOut LEDs[4] = {
DigitalOut(LED1), DigitalOut(LED2), DigitalOut(LED3), DigitalOut(LED4)
};
Serial pcx(USBTX, USBRX); // Communication with Host
Queue<uint32_t, 2> queue0;
Queue<uint32_t, 2> queue1;
/* 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;
uint8_t show_flag;
// ROM / Constant data ---------------------------------------------------------------------------
// Function prototypes ---------------------------------------------------------------------------
// Function prototypes ---------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
// Control Program
//-------------------------------------------------------------------------------------------------
void send_thread (void const *args) {
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);
Thread::wait(1000);
}
}
void blink(void const *n) {
LEDs[(int)n] = !LEDs[(int)n];
}
// Read switch status
int read_sw(uint8_t n){
}
// Monitor program
void monitor(void const *args){
}
// Interrupt routine
void queue_isr0() {
queue0.put((uint32_t*)1);
}
void queue_isr1() {
queue1.put((uint32_t*)1);
}
// Update sensor data
void update_angle(void const *args){
}
// Read angle and control an inertia rotor
void rotor_control(void const *args){
}
// Update sensor data
void display(void const *args){
}
// Thread definition
osThreadDef(update_angle, osPriorityRealtime, 4096);
osThreadDef(rotor_control, osPriorityAboveNormal, 4096);
osThreadDef(monitor, osPriorityNormal, 4096);
osThreadDef(display, osPriorityNormal, 4096);
int main(void) {
PRINTF("step1\r\n");
RtosTimer led_1_timer(blink, osTimerPeriodic, (void *)0);
RtosTimer led_2_timer(blink, osTimerPeriodic, (void *)1);
RtosTimer led_3_timer(blink, osTimerPeriodic, (void *)2);
RtosTimer led_4_timer(blink, osTimerPeriodic, (void *)3);
PRINTF("step2\r\n");
led_1_timer.start(2000);
led_2_timer.start(1000);
led_3_timer.start(500);
led_4_timer.start(250);
PRINTF("step3\r\n");
Thread thread(send_thread);
PRINTF("step4\r\n");
// IRQ
Ticker ticker0;
Ticker ticker1;
ticker0.attach(queue_isr0, TIME_BASE_S);
ticker1.attach(queue_isr1, RATE);
// rotor.set_max_speed(TIMEBASE);
PRINTF("step5\r\n");
// Starts 1st thread
osThreadCreate(osThread(update_angle), NULL);
// Starts 2nd thread
osThreadCreate(osThread(rotor_control), NULL);
// Starts 3rd thread
osThreadCreate(osThread(monitor), NULL);
// Starts 4th thread
osThreadCreate(osThread(display), NULL);
PRINTF("step6\r\n");
while (true) {
osEvent evt = mail_box.get();
if (evt.status == osEventMail) {
mail_t *mail = (mail_t*)evt.value.p;
if (show_flag){
PRINTF("This is dummy!, ");
PRINTF("Volt: %.2f V, " , mail->voltage);
PRINTF("Current: %.2f A, " , mail->current);
PRINTF("# of cycles: %u\r\n", mail->counter);
}
mail_box.free(mail);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment