replaces CDU_Mbed_26

Dependencies:   4DGL MODSERIAL mbed mbos

Fork of CDU_Mbed_26 by Engravity-CDU

Committer:
LvdK
Date:
Tue Jan 29 14:54:14 2013 +0000
Revision:
6:e0bf3b244d7c
Parent:
5:99594f4ab659
Child:
7:6576a287e563
Goed werkende mbos versie met 0.5 sec ALIVE message

Who changed what in which revision?

UserRevisionLine numberNew contents of line
LvdK 5:99594f4ab659 1 // L. van der Kolk, ELVEDEKA, Holland //
LvdK 6:e0bf3b244d7c 2 // File: main.cpp Mbos O.S testversion
LvdK 5:99594f4ab659 3
LvdK 0:6f71ca095e78 4 #include "mbed.h"
LvdK 6:e0bf3b244d7c 5 #include "MODSERIAL.h"
LvdK 6:e0bf3b244d7c 6 #include "mbos.h"
LvdK 6:e0bf3b244d7c 7
LvdK 6:e0bf3b244d7c 8 //--------------------------------------------------------------------------------
LvdK 6:e0bf3b244d7c 9 #include "TextLCD.h" // < LCD connected for testing only << !
LvdK 6:e0bf3b244d7c 10 TextLCD lcd(p8, p9, p10, p11, p12, p13); // rs, e, d4-d7 << for testing only << !
LvdK 6:e0bf3b244d7c 11 //--------------------------------------------------------------------------------
LvdK 6:e0bf3b244d7c 12
LvdK 6:e0bf3b244d7c 13 DigitalOut led1(LED1); // : LED 1 on Mbed board toggles when CDU is alive
LvdK 0:6f71ca095e78 14
LvdK 6:e0bf3b244d7c 15 extern MODSERIAL USB;
LvdK 6:e0bf3b244d7c 16 extern int key_hit_ID;
LvdK 6:e0bf3b244d7c 17 int CDU_FS_interface = 0; // : defines interface to FS: 0 = USB, 1 = Ethernet
LvdK 0:6f71ca095e78 18
LvdK 0:6f71ca095e78 19 void init_USB();
LvdK 6:e0bf3b244d7c 20 void set_initial_CDU_screen();
LvdK 6:e0bf3b244d7c 21 void Send_KEY_message(int button);
LvdK 6:e0bf3b244d7c 22 void Send_ALIVE_message(int seconds);
LvdK 6:e0bf3b244d7c 23 void collect_FSdata();
LvdK 6:e0bf3b244d7c 24
LvdK 6:e0bf3b244d7c 25 //----------------------------------------------------------------
LvdK 6:e0bf3b244d7c 26 void init_buttons_Mbed(); // << for testing only << !
LvdK 6:e0bf3b244d7c 27 void scan_buttons_Mbed(); // << for testing only << !
LvdK 6:e0bf3b244d7c 28 //----------------------------------------------------------------
LvdK 6:e0bf3b244d7c 29
LvdK 6:e0bf3b244d7c 30 // ALIVE_TASK properties:
LvdK 6:e0bf3b244d7c 31 #define ALIVE_TASK_ID 1
LvdK 6:e0bf3b244d7c 32 #define ALIVE_TASK_PRIO 50
LvdK 6:e0bf3b244d7c 33 #define ALIVE_TASK_STACK_SZ 100 // : must be 100 to prevent stack overflow error!
LvdK 6:e0bf3b244d7c 34
LvdK 6:e0bf3b244d7c 35 // ALIVE_TIMER properties:
LvdK 6:e0bf3b244d7c 36 #define ALIVE_TIMER_ID 1
LvdK 6:e0bf3b244d7c 37 #define ALIVE_TIMER_PERIOD 500 // = 5 sec
LvdK 6:e0bf3b244d7c 38
LvdK 6:e0bf3b244d7c 39 // RECEIVE_DECODE_TASK properties:
LvdK 6:e0bf3b244d7c 40 #define RECEIVE_DECODE_TASK_ID 2
LvdK 6:e0bf3b244d7c 41 #define RECEIVE_DECODE_TASK_PRIO 80
LvdK 6:e0bf3b244d7c 42 #define RECEIVE_DECODE_TASK_STACK_SZ 256 // : must be 256 to prevent stack overflow error!
LvdK 6:e0bf3b244d7c 43
LvdK 6:e0bf3b244d7c 44 // SEND_KEYMESSAGE_TASK properties:
LvdK 6:e0bf3b244d7c 45 #define SEND_KEYMESSAGE_TASK_ID 3
LvdK 6:e0bf3b244d7c 46 #define SEND_KEYMESSAGE_TASK_PRIO 60
LvdK 6:e0bf3b244d7c 47 #define SEND_KEYMESSAGE_TASK_STACK_SZ 100 // : must be 100 to prevent stack overflow error!
LvdK 6:e0bf3b244d7c 48
LvdK 6:e0bf3b244d7c 49 //------------------------------ LvdK button scan --------------
LvdK 6:e0bf3b244d7c 50 // SCAN_KEYS_TASK properties:
LvdK 6:e0bf3b244d7c 51 #define SCAN_KEYS_TASK_ID 4
LvdK 6:e0bf3b244d7c 52 #define SCAN_KEYS_TASK_PRIO 85
LvdK 6:e0bf3b244d7c 53 #define SCAN_KEYS_TASK_STACK_SZ 100
LvdK 6:e0bf3b244d7c 54
LvdK 6:e0bf3b244d7c 55 // SCAN_KEYS_TIMER properties: ( LvdK button scan )
LvdK 6:e0bf3b244d7c 56 #define SCAN_KEYS_TIMER_ID 2
LvdK 6:e0bf3b244d7c 57 #define SCAN_KEYS_TIMER_PERIOD 50 // = 50msec
LvdK 6:e0bf3b244d7c 58 //----------------------------------------------------------------
LvdK 5:99594f4ab659 59
LvdK 6:e0bf3b244d7c 60 // Mbos EVENTS:
LvdK 6:e0bf3b244d7c 61 #define ALIVE_EVENT 1
LvdK 6:e0bf3b244d7c 62 #define RX_EVENT 2 // : Event flag caused by serial RX interrupt
LvdK 6:e0bf3b244d7c 63 #define KEY_EVENT 4
LvdK 6:e0bf3b244d7c 64 #define SCAN_KEYS_TIMER_EVENT 8 // : LvdK button scan timer event
LvdK 6:e0bf3b244d7c 65
LvdK 6:e0bf3b244d7c 66 // Mbos resources:
LvdK 6:e0bf3b244d7c 67 #define USB_TX_RESOURCE 1 // : for locking USB TX buffer
LvdK 6:e0bf3b244d7c 68 #define USB_TX_PRIO 95 // : USB TX resource priority ( = highest! )
LvdK 6:e0bf3b244d7c 69
LvdK 6:e0bf3b244d7c 70
LvdK 6:e0bf3b244d7c 71 mbos CDU_OS( 8, 6, 5); //instantiate mbos O.S (tasks, timers, resources)
LvdK 5:99594f4ab659 72
LvdK 6:e0bf3b244d7c 73 // Task function prototypes:
LvdK 6:e0bf3b244d7c 74 void ALIVE_TASK(void);
LvdK 6:e0bf3b244d7c 75 void RECEIVE_DECODE_TASK(void);
LvdK 6:e0bf3b244d7c 76 void SCAN_KEYS_TASK(void);
LvdK 6:e0bf3b244d7c 77 void SEND_KEYMESSAGE_TASK(void);
LvdK 6:e0bf3b244d7c 78
LvdK 6:e0bf3b244d7c 79 void char_received(MODSERIAL_IRQ_INFO *q) {
LvdK 6:e0bf3b244d7c 80 // -- This functiom will be called on EACH rx interrupt --
LvdK 6:e0bf3b244d7c 81 // Note: this is Rx interrupt handling, NOT a mbos task.
LvdK 6:e0bf3b244d7c 82 // It only set an mbos Event.
LvdK 6:e0bf3b244d7c 83 CDU_OS.SetEvent(RX_EVENT,RECEIVE_DECODE_TASK_ID); // : set event flag for Read Serial Task
LvdK 6:e0bf3b244d7c 84 }
LvdK 6:e0bf3b244d7c 85
LvdK 6:e0bf3b244d7c 86 //------------ USB error handling ------------------------------------------------------------
LvdK 6:e0bf3b244d7c 87 void rx_buf_overflow(MODSERIAL_IRQ_INFO *q) {
LvdK 6:e0bf3b244d7c 88 // This functiom will be called when RX buffer overflows
LvdK 6:e0bf3b244d7c 89 // Blue-Leds-of-Death will be showed, USB is dead so message itself will not be seen !
LvdK 6:e0bf3b244d7c 90 error("RX OVERFLOW !");
LvdK 6:e0bf3b244d7c 91 }
LvdK 6:e0bf3b244d7c 92
LvdK 6:e0bf3b244d7c 93 void tx_buf_overflow(MODSERIAL_IRQ_INFO *q) {
LvdK 6:e0bf3b244d7c 94 // This functiom will be called when TX buffer overflows
LvdK 6:e0bf3b244d7c 95 // Blue-Leds-of-Death will be showed, USB is dead so message itself will not be seen !
LvdK 6:e0bf3b244d7c 96 error("TX OVERFLOW !");
LvdK 6:e0bf3b244d7c 97 }
LvdK 6:e0bf3b244d7c 98 //---------------------------------------------------------------------------------------------
LvdK 6:e0bf3b244d7c 99
LvdK 6:e0bf3b244d7c 100 void init_USB()
LvdK 6:e0bf3b244d7c 101 { // -- This function controls the setup for the CDU-FS USB communication --
LvdK 6:e0bf3b244d7c 102 // Function char_received() is attached to Rx interrupt.
LvdK 6:e0bf3b244d7c 103 USB.baud(38400); // allways set default baudrate to 38400
LvdK 6:e0bf3b244d7c 104 USB.attach(&char_received, MODSERIAL::RxIrq); // : attach function char_received to RX inerrupt
LvdK 6:e0bf3b244d7c 105 USB.attach(&rx_buf_overflow, MODSERIAL::RxOvIrq); //: attach function to call when RX buffer overflows
LvdK 6:e0bf3b244d7c 106 USB.attach(&tx_buf_overflow, MODSERIAL::TxOvIrq); //: attach function to call when TX buffer overflows
LvdK 6:e0bf3b244d7c 107 }
LvdK 0:6f71ca095e78 108
LvdK 0:6f71ca095e78 109 main()
LvdK 0:6f71ca095e78 110 {
LvdK 6:e0bf3b244d7c 111 init_USB(); // : setup USB communication
LvdK 6:e0bf3b244d7c 112 set_initial_CDU_screen();
LvdK 6:e0bf3b244d7c 113
LvdK 6:e0bf3b244d7c 114 //------------------ for testing only ! ----------------------------------
LvdK 6:e0bf3b244d7c 115 init_buttons_Mbed();
LvdK 6:e0bf3b244d7c 116 lcd.cls(); // : clear LCD display
LvdK 6:e0bf3b244d7c 117 lcd.printf("CDU USB Mbos\n");
LvdK 6:e0bf3b244d7c 118 USB.printf("\n--- CDU USB Mbos ---\n");
LvdK 6:e0bf3b244d7c 119 //------------------------------------------------------------------------
LvdK 5:99594f4ab659 120
LvdK 6:e0bf3b244d7c 121 // Configure all tasks, timers and resources:
LvdK 6:e0bf3b244d7c 122 CDU_OS.CreateTask(ALIVE_TASK_ID, ALIVE_TASK_PRIO, ALIVE_TASK_STACK_SZ, ALIVE_TASK);
LvdK 6:e0bf3b244d7c 123 CDU_OS.CreateTask(RECEIVE_DECODE_TASK_ID, RECEIVE_DECODE_TASK_PRIO, RECEIVE_DECODE_TASK_STACK_SZ, RECEIVE_DECODE_TASK);
LvdK 6:e0bf3b244d7c 124 CDU_OS.CreateTask(SEND_KEYMESSAGE_TASK_ID, SEND_KEYMESSAGE_TASK_PRIO, SEND_KEYMESSAGE_TASK_STACK_SZ, SEND_KEYMESSAGE_TASK);
LvdK 6:e0bf3b244d7c 125 CDU_OS.CreateTask(SCAN_KEYS_TASK_ID, SCAN_KEYS_TASK_PRIO, SCAN_KEYS_TASK_STACK_SZ, SCAN_KEYS_TASK );
LvdK 6:e0bf3b244d7c 126 CDU_OS.CreateTimer(ALIVE_TIMER_ID, ALIVE_TASK_ID, ALIVE_EVENT);
LvdK 6:e0bf3b244d7c 127 CDU_OS.CreateTimer(SCAN_KEYS_TIMER_ID, SCAN_KEYS_TASK_ID, SCAN_KEYS_TIMER_EVENT);
LvdK 6:e0bf3b244d7c 128 CDU_OS.CreateResource(USB_TX_RESOURCE, USB_TX_PRIO);
LvdK 6:e0bf3b244d7c 129 //CDU_OS.CreateTask(TASK4_ID, TASK4_PRIO, TASK4_STACK_SZ, task4);
LvdK 6:e0bf3b244d7c 130
LvdK 6:e0bf3b244d7c 131 // Start mbos O.S with final statement in main() :
LvdK 6:e0bf3b244d7c 132 CDU_OS.Start(); // : will never return!
LvdK 6:e0bf3b244d7c 133 }
LvdK 6:e0bf3b244d7c 134
LvdK 6:e0bf3b244d7c 135 void RECEIVE_DECODE_TASK(void)
LvdK 6:e0bf3b244d7c 136 { // Task waits for RX_EVENT, then
LvdK 6:e0bf3b244d7c 137 // collects and decodes data from FS
LvdK 6:e0bf3b244d7c 138 while(1){
LvdK 6:e0bf3b244d7c 139 CDU_OS.WaitEvent(RX_EVENT); // : wait for RX event
LvdK 6:e0bf3b244d7c 140 collect_FSdata();
LvdK 6:e0bf3b244d7c 141 }
LvdK 6:e0bf3b244d7c 142 }
LvdK 6:e0bf3b244d7c 143
LvdK 6:e0bf3b244d7c 144 void SEND_KEYMESSAGE_TASK(void)
LvdK 6:e0bf3b244d7c 145 {
LvdK 6:e0bf3b244d7c 146 while(1){
LvdK 6:e0bf3b244d7c 147 CDU_OS.WaitEvent(KEY_EVENT); // : wait for KEY event ( = CDU key pressed )
LvdK 6:e0bf3b244d7c 148 Send_KEY_message(key_hit_ID);
LvdK 6:e0bf3b244d7c 149 key_hit_ID = 0; // : reset Id flag
LvdK 0:6f71ca095e78 150 }
LvdK 6:e0bf3b244d7c 151 }
LvdK 6:e0bf3b244d7c 152
LvdK 6:e0bf3b244d7c 153 void ALIVE_TASK(void)
LvdK 6:e0bf3b244d7c 154 { // Mbos task started by mbos timer to send ALIVE message to FS every 5 sec.
LvdK 6:e0bf3b244d7c 155 CDU_OS.SetTimer( ALIVE_TIMER_ID, ALIVE_TIMER_PERIOD, ALIVE_TIMER_PERIOD );
LvdK 6:e0bf3b244d7c 156 static int seconds = 0;
LvdK 6:e0bf3b244d7c 157 while(1){ // : loop forever because it is a mbos taskfunction
LvdK 6:e0bf3b244d7c 158 CDU_OS.WaitEvent(ALIVE_EVENT ); // : wait for Mbos timer event (5 sec)
LvdK 6:e0bf3b244d7c 159 seconds += 5 ; // : increase seconds by 5
LvdK 6:e0bf3b244d7c 160 if (seconds >= 60) seconds = 0 ;
LvdK 6:e0bf3b244d7c 161 Send_ALIVE_message(seconds);
LvdK 6:e0bf3b244d7c 162 led1 = !led1;
LvdK 6:e0bf3b244d7c 163 }
LvdK 6:e0bf3b244d7c 164 }
LvdK 6:e0bf3b244d7c 165
LvdK 6:e0bf3b244d7c 166 // ------------- LvdK button scan, test only -------------------------------------------------
LvdK 6:e0bf3b244d7c 167 void SCAN_KEYS_TASK(void) // : Test if button pressed
LvdK 6:e0bf3b244d7c 168 { // Will be task written by W.B
LvdK 6:e0bf3b244d7c 169 // Is now LvdK test Task, scans some buttons attached to Mbed board.
LvdK 6:e0bf3b244d7c 170 // Task scans buttons every 50msec.
LvdK 6:e0bf3b244d7c 171 CDU_OS.SetTimer( SCAN_KEYS_TIMER_ID, SCAN_KEYS_TIMER_PERIOD, SCAN_KEYS_TIMER_PERIOD );
LvdK 6:e0bf3b244d7c 172
LvdK 6:e0bf3b244d7c 173 while(1){ // : loop forever because it is a mbos taskfunction
LvdK 6:e0bf3b244d7c 174 CDU_OS.WaitEvent(SCAN_KEYS_TIMER_EVENT ); // : scan buttons every 50msec
LvdK 6:e0bf3b244d7c 175 scan_buttons_Mbed();
LvdK 6:e0bf3b244d7c 176 }
LvdK 6:e0bf3b244d7c 177 }
LvdK 6:e0bf3b244d7c 178 // -------------------------------------------------------------------------------------------