DanielMartensson / Open-SAE-J1939

SAE J1939 protocol free to use for embedded systems or PC with CAN-bus
MIT License
455 stars 164 forks source link

Need a flag for feedback #29

Closed payapars closed 11 months ago

payapars commented 11 months ago

thk for open source stack. 1-i think this stack needed RTOS for manegment send function or PNG ID and wait to flag feedback ,so this is need timeout for stack . I hope you understand what I mean!

DanielMartensson commented 11 months ago

What do you mean ?

payapars commented 11 months ago

i edited my answer!

DanielMartensson commented 11 months ago

i edited my answer!

Sorry, I dont understand. Use the read messages function in your RTOS thread.

payapars commented 11 months ago

this my My code:


 ej1939MasterReqErrCode WriteMultiBufferData (uint8_t ucSndAddr,uint32_t PNG,uint8_t *Data,uint16_t datalen,LONG lTimeOut )
{

    ej1939MasterReqErrCode    eErrStatus = J1939_MRE_NO_ERR;
    if ( ucSndAddr > J1939_MASTER_TOTAL_SLAVE_NUM ||datalen>MAX_TP_DT||datalen==NULL) eErrStatus = J1939_MRE_ILL_ARG;
    else if ( xj1939MasterRunResTake( lTimeOut ) == FALSE ) eErrStatus = J1939_MRE_MASTER_BUSY;
    else
    {
              j1939.from_other_ecu_tp_cm.PGN_of_the_packeted_message=PNG;
            j1939.from_other_ecu_tp_cm.control_byte=CONTROL_BYTE_TP_CM_RTS;
              j1939.this_ecu_tp_cm.number_of_packages=datalen/7;
                /* set number of packages, where each package will transmit up to 7 bytes */
                if (datalen % 7 > 0){
                    j1939.this_ecu_tp_cm.number_of_packages+=1; /* add extra frame if data rolls over */
                }
            j1939.from_other_ecu_tp_cm.total_message_size=datalen;

                memcpy(&j1939.data[0],Data,datalen);

       SAE_J1939_Send_Transport_Protocol_Connection_Management(&j1939, ucSndAddr);

           eErrStatus = ej1939MasterWaitRequestFinish(lTimeOut );
    }
    return eErrStatus;

}  

and function wait:


ej1939MasterReqErrCode  ej1939MasterWaitRequestFinish(long time_out ) {
  ej1939MasterReqErrCode    eErrStatus_Req = J1939_MRE_NO_ERR;
    uint32_t eErrStatus=J1939_MRE_NO_ERR;
            j1939_LEDRxCpltCallback(RESET);
        j1939_LEDTxCpltCallback(SET);   
    eErrStatus= osEventFlagsWait (J1939_evt_id,EV_MASTER_PROCESS_SUCESS  | EV_MASTER_ERROR_RESPOND_TIMEOUT| EV_MASTER_ERROR_RECEIVE_DATA| EV_MASTER_ERROR_EXECUTE_FUNCTION,osFlagsWaitAny, time_out);

    switch (eErrStatus)
    {
                    case EV_MASTER_PROCESS_SUCESS:
                    {
                        eErrStatus_Req= J1939_MRE_NO_ERR;   
                            break;
                    }
                    case EV_MASTER_ERROR_RESPOND_TIMEOUT:
                    {
                            eErrStatus_Req = J1939_MRE_TIMEDOUT;
                            break;
                    }
                    case EV_MASTER_ERROR_RECEIVE_DATA:
                    {
                            eErrStatus_Req = J1939_MRE_REV_DATA;
                            break;
                    }
                    case EV_MASTER_ERROR_EXECUTE_FUNCTION:
                    {
                            eErrStatus_Req = J1939_MRE_EXE_FUN;
                            break;
                    }
                    default:{
                      eErrStatus_Req =  J1939_MRE_TIMEDOUT;
                        break;
                    }
    }
        osEventFlagsClear (J1939_evt_id,0);
      vj1939MasterRunResRelease( );
    return eErrStatus_Req;
}

and send event of listen function massage at 25 code line


1.  boolOpen_SAE_J1939_Listen_For_Messages(J1939* j1939) {
2.  uint32_t ID = 0;
3.  uint8_t data[8] = {0};
4.  bool is_new_message = CAN_Read_Message(&ID, data);
5.  if(is_new_message){
6.      /* Save latest */
7.      j1939->ID = ID;
8.      memcpy(j1939->data, data, 8);
9.      j1939->ID_and_data_is_updated = true;
10. 
11.         uint8_t id0 = ID >> 24;
12.         uint8_t id1 = ID >> 16;
13.         uint8_t DA = ID >> 8;   
14.         uint8_t SA = ID;        
15. 
16.         /* Read request from other ECU */
17.         if (id0 == 0x18 && id1 == 0xEA && (DA == j1939->information_this_ECU.this_ECU_address || DA == 0xFF)){
18.             SAE_J1939_Read_Request(j1939, SA, data);
19.         }else if (id0 == 0x18 && id1 == 0xD9 && DA == j1939->information_this_ECU.this_ECU_address){
20.             SAE_J1939_Read_Request_DM14(j1939, SA, data);
21. 
22.         /* Read status from other ECU */
23.         }else if(id0 == 0x18 && id1 == 0xE8 && DA == j1939->information_this_ECU.this_ECU_address){
24.             SAE_J1939_Read_Acknowledgement(j1939, SA, data);
25.           vj1939MasterCBRequestScuuess();//this function send flag 
26.         }else if (id0 == 0x18 && id1 == 0xD8 && DA == j1939->information_this_ECU.this_ECU_address){
27.             SAE_J1939_Read_Response_DM15(j1939, SA, data);
28.         }else if(id0 == 0x18 && id1 == 0xD7 && DA == j1939->information_this_ECU.this_ECU_address){
29.             SAE_J1939_Read_Binary_Data_Transfer_DM16(j1939, SA, data);
30. 
31.         /* Read Transport Protocol information from other ECU */
32.         }else if(id0 == 0x1C && id1 == 0xEC && DA == j1939->information_this_ECU.this_ECU_address){
33.             SAE_J1939_Read_Transport_Protocol_Connection_Management(j1939, SA, data);
34.         }else if (id0 == 0x1C && id1 == 0xEB && DA == j1939->information_this_ECU.this_ECU_address){
35.             SAE_J1939_Read_Transport_Protocol_Data_Transfer(j1939, SA, data);
36. 
37.         /* Read response request from other ECU - This are response request. They are responses from other ECU about request from this ECU */
38.         }else if (id0 == 0x18 && id1 == 0xEE && DA == 0xFF && SA != 0xFE){
39.             SAE_J1939_Read_Response_Request_Address_Claimed(j1939, SA, data);                                   /* This is a broadcast response request */
40.         }else if (id0 == 0x18 && id1 == 0xEE && DA == 0xFF && SA == 0xFE){
41.             SAE_J1939_Read_Address_Not_Claimed(j1939, SA, data);                                                /* This is error */
42.         }else if (id0 == 0x18 && id1 == 0xFE && DA == 0xCA){
43.             SAE_J1939_Read_Response_Request_DM1(j1939, SA, data, 1);                                            /* Assume that errors_dm1_active = 1 */
44.         }else if (id0 == 0x18 && id1 == 0xFE && DA == 0xCB){
45.             SAE_J1939_Read_Response_Request_DM2(j1939, SA, data, 1);                                            /* Assume that errors_dm2_active = 1 */
46.         }else if (id0 == 0x18 && id1 == 0xFE && DA == 0xDA){
47.             SAE_J1939_Read_Response_Request_Software_Identification(j1939, SA, data);
48.         }else if (id0 == 0x18 && id1 == 0xFD && DA == 0xC5){
49.             SAE_J1939_Read_Response_Request_ECU_Identification(j1939, SA, data);
50.         }else if (id0 == 0x18 && id1 == 0xFE && DA == 0xEB){
51.             SAE_J1939_Read_Response_Request_Component_Identification(j1939, SA, data);
52.         }else if (id0 == 0x0C && id1 == 0xFE && DA >= 0x10 && DA <= 0x1F){
53.             ISO_11783_Read_Response_Request_Auxiliary_Estimated_Flow(j1939, SA, DA & 0xF, data);                /* DA & 0xF = Valve number. Total 16 valves from 0 to 15 */
54.         }else if (id0 == 0x0C && id1 == 0xC6 && DA == j1939->information_this_ECU.this_ECU_address){
55.             ISO_11783_Read_Response_Request_General_Purpose_Valve_Estimated_Flow(j1939, SA, data);
56.         }else if (id0 == 0x0C && id1 == 0xFF && DA >= 0x20 && DA <= 0x2F){
57.             ISO_11783_Read_Response_Request_Auxiliary_Valve_Measured_Position(j1939, SA, DA & 0xF, data);       /* DA & 0xF = Valve number. Total 16 valves from 0 to 15 */
58. 
59.         /* Read command from other ECU */
60.         }else if (id0 == 0x0C && id1 == 0xFE && DA >= 0x30 && DA <= 0x3F){
61.             ISO_11783_Read_Auxiliary_Valve_Command(j1939, SA, DA & 0xF, data);                                  /* DA & 0xF = Valve number. Total 16 valves from 0 to 15 */
62.         }else if (id0 == 0x0C && id1 == 0xC4 && DA == j1939->information_this_ECU.this_ECU_address){
63.             ISO_11783_Read_General_Purpose_Valve_Command(j1939, SA, data);                                      /* General Purpose Valve Command have only one valve */
64.         }else if (id0 == 0x0 && id1 == 0x2 && (DA == j1939->information_this_ECU.this_ECU_address || DA == 0xFF)){
65.             SAE_J1939_Read_Address_Delete(j1939, data);                                                         /* Not a SAE J1939 standard */
66.         }else if (id0 == 0x1D && id1 == 0xEB && DA == j1939->information_this_ECU.this_ECU_address){
67.             SAE_J1939_Read_Transport_Protocol_Data_Transfer(j1939, SA, data);
68.         }
69.         else{
70.             is_new_message = false;                                                                             /* The message was not meant for this ECU */
71.         }
72.         /* Add more else if statement here */
73.     }
74.     return is_new_message;
75. }

at line code 25 .is flag

DanielMartensson commented 11 months ago

What do you want to achieve?

payapars commented 11 months ago

In your code to send data if it is in master mode, there is no waiting time! Is this correct?

DanielMartensson commented 11 months ago

In your code to send data if it is in master mode, there is no waiting time! Is this correct?

@payapars you can add your own delay at hardware setup of this library

DanielMartensson commented 11 months ago

See here.

https://github.com/DanielMartensson/Open-SAE-J1939/blob/677c5839992d37b8bb032472abc9ab7fe5559723/Src/Hardware/CAN_Transmit_Receive.c#L183

If you want to add a call back, then send me a pull request

payapars commented 11 months ago

thanks