Dummy program to demonstrate problems: working code

Dependencies:   SLCD mbed-rtos mbed

Fork of MNG_TC by Shreesha S

main.cpp

Committer:
shreeshas95
Date:
2015-07-16
Revision:
10:024c2ef51cb1
Parent:
8:cb93c1d3209a
Child:
11:109f16cc35d7
Child:
13:7b27a8e9cbb4

File content as of revision 10:024c2ef51cb1:

//how to handle interrupt while running other thread ?

#include "mbed.h"
#include "rtos.h"
#define ENDL "\r" << endl

#include "SLCD.h"
SLCD lcd;

#define RX_TIMEOUT_LIMIT 2
#define PASS_TIME_LIMIT 1200

Serial PC(USBTX, USBRX);
RawSerial rx1m(PTE0, PTE1);

DigitalOut ledr(LED_RED);
DigitalOut ledg(LED_GREEN);

#include "Structures.h"

struct data_list{
   unsigned char val;
   struct data_list* next;
};

namespace VAR_SPACE{
    
    TC_list *Head_node = NULL;
    TC_list *last_node = NULL;
    
    int rx_state = 0;
    /*
    0 : idle
    1 : executing normal
    2 : executing obosc
    3 : idle 2 : obosc received incorrectly
    */
    
    struct data_list *head_data;
    data_list *data_node;
    struct data_list *rx_new_node;
    
    bool new_tc_received = false;
    bool execute_obosc = false;
    
    Thread *mng_tmtc_thread = NULL;
}

Timeout rx_timeout;
bool pass_over = false;
bool first_time = true;
Timeout pass_time;

#include "crc.h"
#include "SND_TM.h"
#include "COM_RCV_TC.h"
#include "MNG_TC.h"
#include "ThreadFunctions.h"

void after_pass(){
    pass_time.detach();
    pass_over = true;
}

void after_receive(void){
    rx_timeout.detach();
    VAR_SPACE::rx_new_node->val = 0x00;
    
    if(first_time){
        first_time = false;
        pass_time.attach(&after_pass, PASS_TIME_LIMIT);
    }
    
    VAR_SPACE::new_tc_received = true;
    VAR_SPACE::mng_tmtc_thread->signal_set(0x01);
}

unsigned char rx_char = 0;
bool byte_rx = false;

void rx_read() {
//    store value
//    rx_char = UART1->D;
    rx_char = rx1m.getc();
    
    byte_rx = true;
}

int main(){
    
    printf("welcome to mng_tm_tc\r\n");
    
    PC.baud(9600);
    
    rx1m.baud(1200);
    rx1m.attach(&rx_read, Serial::RxIrq);
    VAR_SPACE::head_data = new data_list;
    VAR_SPACE::head_data->next = NULL;
    VAR_SPACE::rx_new_node = VAR_SPACE::head_data;
    
    VAR_SPACE::mng_tmtc_thread = new Thread(com_mng_fun);
    MNG_TC::init();
    
    lcd.printf("0");
    
    struct data_list *hehe = VAR_SPACE::head_data;
    while( hehe != NULL ){
        printf("%x ", hehe->val);
        hehe = hehe->next;
    }
    printf("\r\n");
    
    while(true){
//        ledg = !ledg;
        if(byte_rx){
            ledg = !ledg;
            byte_rx = false;
            
            VAR_SPACE::rx_new_node->val = rx_char;
            
            //~ allocate new node
            VAR_SPACE::rx_new_node->next = new data_list;
            VAR_SPACE::rx_new_node = VAR_SPACE::rx_new_node->next;
            VAR_SPACE::rx_new_node->next = NULL;
            
            rx_timeout.attach(&after_receive, RX_TIMEOUT_LIMIT);
        }

        if(pass_over){
            pass_over = false;
            first_time = true;
            // pass got over reset all
            reset_all();
            //also consider frame_no
        }
    }
}