Gopal Nair
/
frdm_rtos_serial
new
main.cpp@0:2d65e3de0b15, 2017-07-06 (annotated)
- Committer:
- naray23
- Date:
- Thu Jul 06 17:15:03 2017 +0000
- Revision:
- 0:2d65e3de0b15
new
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
naray23 | 0:2d65e3de0b15 | 1 | #include "mbed.h" |
naray23 | 0:2d65e3de0b15 | 2 | #include "rtos.h" |
naray23 | 0:2d65e3de0b15 | 3 | |
naray23 | 0:2d65e3de0b15 | 4 | |
naray23 | 0:2d65e3de0b15 | 5 | DigitalOut led1(LED1); |
naray23 | 0:2d65e3de0b15 | 6 | DigitalOut led2(LED2); |
naray23 | 0:2d65e3de0b15 | 7 | |
naray23 | 0:2d65e3de0b15 | 8 | InterruptIn sw2(PTD4); |
naray23 | 0:2d65e3de0b15 | 9 | uint32_t button_pressed; |
naray23 | 0:2d65e3de0b15 | 10 | RawSerial pc(USBTX,USBRX); |
naray23 | 0:2d65e3de0b15 | 11 | |
naray23 | 0:2d65e3de0b15 | 12 | Thread *thread2; |
naray23 | 0:2d65e3de0b15 | 13 | Thread *thread3; |
naray23 | 0:2d65e3de0b15 | 14 | |
naray23 | 0:2d65e3de0b15 | 15 | char ch; |
naray23 | 0:2d65e3de0b15 | 16 | void sw2_press(void) |
naray23 | 0:2d65e3de0b15 | 17 | { |
naray23 | 0:2d65e3de0b15 | 18 | thread2->signal_set(0x1); |
naray23 | 0:2d65e3de0b15 | 19 | } |
naray23 | 0:2d65e3de0b15 | 20 | |
naray23 | 0:2d65e3de0b15 | 21 | void RX_callback() { |
naray23 | 0:2d65e3de0b15 | 22 | // Note: you need to actually read from the serial to clear the RX interrupt |
naray23 | 0:2d65e3de0b15 | 23 | while(pc.readable()) |
naray23 | 0:2d65e3de0b15 | 24 | { |
naray23 | 0:2d65e3de0b15 | 25 | ch=pc.getc(); |
naray23 | 0:2d65e3de0b15 | 26 | } |
naray23 | 0:2d65e3de0b15 | 27 | led2 = !led2; |
naray23 | 0:2d65e3de0b15 | 28 | thread3->signal_set(0x1); |
naray23 | 0:2d65e3de0b15 | 29 | } |
naray23 | 0:2d65e3de0b15 | 30 | void led_thread(void const *argument) |
naray23 | 0:2d65e3de0b15 | 31 | { |
naray23 | 0:2d65e3de0b15 | 32 | while (true) { |
naray23 | 0:2d65e3de0b15 | 33 | led1 = !led1; |
naray23 | 0:2d65e3de0b15 | 34 | Thread::wait(1000); |
naray23 | 0:2d65e3de0b15 | 35 | } |
naray23 | 0:2d65e3de0b15 | 36 | } |
naray23 | 0:2d65e3de0b15 | 37 | |
naray23 | 0:2d65e3de0b15 | 38 | void button_thread(void const *argument) |
naray23 | 0:2d65e3de0b15 | 39 | { |
naray23 | 0:2d65e3de0b15 | 40 | while (true) { |
naray23 | 0:2d65e3de0b15 | 41 | Thread::signal_wait(0x1); |
naray23 | 0:2d65e3de0b15 | 42 | button_pressed++; |
naray23 | 0:2d65e3de0b15 | 43 | } |
naray23 | 0:2d65e3de0b15 | 44 | } |
naray23 | 0:2d65e3de0b15 | 45 | |
naray23 | 0:2d65e3de0b15 | 46 | void Serial_thread(void const *argument) |
naray23 | 0:2d65e3de0b15 | 47 | { |
naray23 | 0:2d65e3de0b15 | 48 | while (true) { |
naray23 | 0:2d65e3de0b15 | 49 | Thread::signal_wait(0x1); |
naray23 | 0:2d65e3de0b15 | 50 | pc.putc(ch); |
naray23 | 0:2d65e3de0b15 | 51 | } |
naray23 | 0:2d65e3de0b15 | 52 | } |
naray23 | 0:2d65e3de0b15 | 53 | |
naray23 | 0:2d65e3de0b15 | 54 | int main() |
naray23 | 0:2d65e3de0b15 | 55 | { |
naray23 | 0:2d65e3de0b15 | 56 | Thread thread(led_thread); |
naray23 | 0:2d65e3de0b15 | 57 | thread2 = new Thread(button_thread); |
naray23 | 0:2d65e3de0b15 | 58 | thread3 = new Thread(Serial_thread); |
naray23 | 0:2d65e3de0b15 | 59 | |
naray23 | 0:2d65e3de0b15 | 60 | button_pressed = 0; |
naray23 | 0:2d65e3de0b15 | 61 | sw2.fall(&sw2_press); |
naray23 | 0:2d65e3de0b15 | 62 | pc.attach(&RX_callback,Serial::RxIrq); |
naray23 | 0:2d65e3de0b15 | 63 | while (true) { |
naray23 | 0:2d65e3de0b15 | 64 | Thread::wait(5000); |
naray23 | 0:2d65e3de0b15 | 65 | //pc.printf("SW2 was pressed (last 5 seconds): %d \n", button_pressed); |
naray23 | 0:2d65e3de0b15 | 66 | //fflush(stdout); |
naray23 | 0:2d65e3de0b15 | 67 | button_pressed = 0; |
naray23 | 0:2d65e3de0b15 | 68 | } |
naray23 | 0:2d65e3de0b15 | 69 | } |