Example #1
0
// 驱动对外接口
int driver_main_proc(struct charge_job *thiz, BMS_EVENT_CAN ev,
                        struct bms_event_struct *param, struct bmsdriver *drv)
{
    int ret;

    switch ( ev ) {
    case EVENT_CAN_INIT:
        // 事件循环函数初始化
        thiz->bms.can_bms_status = CAN_NORMAL;
        thiz->bms.can_heart_beat.Hachiko_notify_proc= heart_beart_notify_proc;
        ret = Hachiko_new(&thiz->bms.can_heart_beat, HACHIKO_AUTO_FEED, 1, thiz);
        if ( ret == ERR_OK ) {
            log_printf(INF, "BMS: CHARGER change stage to "RED("CHARGE_STAGE_HANDSHACKING"));
            thiz->bms.charge_stage = CHARGE_STAGE_HANDSHACKING;
            //thiz->charge_stage = CHARGE_STAGE_CONFIGURE;
        } else {
            log_printf(ERR, "BMS: 启动定时器错误.");
        }
        break;
    case EVENT_CAN_RESET:
        // 事件循环函数复位
        // 当发生通信中断时会收到该事件
        break;
    case EVENT_INVALID:
        // 无效事件,事件填充
        break;
    case EVENT_RX_DONE:
        // 数据包接受成功了
        about_packet_reciev_done(thiz, param);
        break;
    case EVENT_RX_ERROR:
        // 数据包接受失败了
        break;
    case EVENT_TX_FAILS:
        // 数据包发送失败了
        log_printf(ERR, "BMS: packet send faile.");
        break;
    case EVENT_TX_DONE:
        // 数据包发送完成了
        log_printf(DBG_LV0, "BMS: packet sent. %08X", param->can_id);
        if ( (param->can_id & 0x00FF0000) == (PGN_CRM << 8) &&
             bit_read(thiz, F_BMS_RECOGNIZED ) &&
             bit_read(thiz, F_VEHICLE_RECOGNIZED ) &&
             thiz->bms.charge_stage == CHARGE_STAGE_HANDSHACKING) {
            thiz->bms.charge_stage = CHARGE_STAGE_CONFIGURE;
            log_printf(INF, "BMS: CHARGER change stage to "RED("CHARGE_STAGE_CONFIGURE"));
        } else if ( (param->can_id & 0x00FF0000) == (PGN_CRO << 8) &&
             bit_read(thiz, F_CHARGER_READY) &&
             bit_read(thiz, F_BMS_READY ) &&
             thiz->bms.charge_stage == CHARGE_STAGE_CONFIGURE ) {
            thiz->bms.charge_stage = CHARGE_STAGE_CHARGING;
            log_printf(INF,
              "BMS: CHARGER change stage to "RED("CHARGE_STAGE_CHARGING"));
        } else {
            param->can_id = param->can_id >> 8;
            about_packet_reciev_done(thiz, param);
        }
        break;
    case EVENT_TX_PRE:
        // 决定是否要发送刚刚准备发送的数据包
        param->evt_param = EVT_RET_OK;
        break;
    case EVENT_TX_REQUEST:
        /*
         * 在这里进行CAN数据包的发送处理
         * 进行数据包发送的条件是:充电枪物理连接正常,进入车辆识别过程,或充电过程。
         *
         * 数据包的发送,优先级最高的是错误报文输出,若是遇到周期性发送的数据包在发送
         * 时序上有重叠的问题,那么在这里的处理方式是,先到先处理,例如在若干个循环内
         * 数据包A,B的发送周期是10ms, 30ms,那么A,B的发送时时序应该是如下方式
         * T (ms)      数据包
         * |             |
         * 0             A
         * 0  + $$       B
         * |             |
         * |             |
         * 10            A
         * |             |
         * |             |
         * 20            A
         * |             |
         * |             |
         * 30            A
         * 30 + $$       B
         * |             |
         * |             |
         * ...         ...
         *
         * $$ 表示最小的循环控制周期,一般来说是绝对小于数据包的发送周期的
         * 所以会有如下的控制逻辑结构
         * if ( ... ) {
         * } else if ( ... ) {
         * } else if ( ... ) {
         * } else ;
         * 在若干个循环控制周期内,数据包都能按照既定的周期发送完成.
         */
        switch ( thiz->bms.charge_stage ) {
        case CHARGE_STAGE_INVALID:
            param->evt_param = EVT_RET_ERR;
            break;
        case CHARGE_STAGE_HANDSHACKING:
            #if 1
            do {
                struct can_pack_generator *gen = gen_search(thiz->bms.generator,
                                                            thiz->bms.can_pack_gen_nr, PGN_CRM);
                if ( gen && gen->heartbeat >= gen->period ) {
                    gen_packet_PGN256(thiz, param);
                    gen->heartbeat = 0;
                } else {
                    //log_printf(DBG_LV0, "BMS: inner error. %d", __LINE__);
                }
                gen = gen_search(thiz->bms.generator, thiz->bms.can_pack_gen_nr, PGN_CEM);
                if ( gen && gen->heartbeat >= gen->period ) {
                    gen_packet_PGN7936(thiz, param);
                    gen->heartbeat = 0;
                } else {
                    //log_printf(DBG_LV0, "BMS: inner error. %d", __LINE__);
                }
            } while (0);
            #endif
            break;
        case CHARGE_STAGE_CONFIGURE:
            do {
                struct can_pack_generator *gen = gen_search(thiz->bms.generator, thiz->bms.can_pack_gen_nr, PGN_CTS);
                if ( gen && gen->heartbeat >= gen->period ) {
                    gen_packet_PGN1792(thiz, param);
                    gen->heartbeat = 0;
                    log_printf(DBG_LV2, "CTS sent.");
                }
                else if ( (gen = gen_search(thiz->bms.generator, thiz->bms.can_pack_gen_nr, PGN_CML)) &&
                          gen->heartbeat >= gen->period ) {
                    gen_packet_PGN2048(thiz, param);
                    gen->heartbeat = 0;
                    log_printf(DBG_LV2, "CML sent.");
                }
                else if ( (gen = gen_search(thiz->bms.generator, thiz->bms.can_pack_gen_nr, PGN_CRO)) &&
                         gen->heartbeat >= gen->period ) {
                    gen_packet_PGN2560(thiz, param);
                    gen->heartbeat = 0;
                    log_printf(DBG_LV2, "CRO sent.");
                } else {
                    if ( gen == NULL ) {
                        log_printf(DBG_LV2, "inner error.");
                    }
                }
                gen = gen_search(thiz->bms.generator, thiz->bms.can_pack_gen_nr, PGN_CEM);
                if ( gen && gen->heartbeat >= gen->period ) {
                    gen_packet_PGN7936(thiz, param);
                    gen->heartbeat = 0;
                }
            } while (0);
            break;
        case CHARGE_STAGE_CHARGING:
            do {
                struct can_pack_generator *gen = gen_search(thiz->bms.generator, thiz->bms.can_pack_gen_nr, PGN_CCS);
                if ( gen && gen->heartbeat >= gen->period ) {
                    gen_packet_PGN4608(thiz, param);
                    gen->heartbeat = 0;
                    log_printf(INF, "CCS sent.");
                }
                else if ( (gen = gen_search(thiz->bms.generator, thiz->bms.can_pack_gen_nr, PGN_CST)) &&
                          gen->heartbeat >= gen->period ) {
                    gen_packet_PGN6656(thiz, param);
                    gen->heartbeat = 0;
                }
                gen = gen_search(thiz->bms.generator, thiz->bms.can_pack_gen_nr, PGN_CEM);
                if ( gen && gen->heartbeat >= gen->period ) {
                    gen_packet_PGN7936(thiz, param);
                    gen->heartbeat = 0;
                }
            } while (0);
            break;
        case CHARGE_STAGE_DONE:
            do {
                struct can_pack_generator *gen = gen_search(thiz->bms.generator, thiz->bms.can_pack_gen_nr, PGN_CSD);
                if ( gen && gen->heartbeat >= gen->period ) {
                    gen_packet_PGN7424(thiz, param);
                    gen->heartbeat = 0;
                }
                gen = gen_search(thiz->bms.generator, thiz->bms.can_pack_gen_nr, PGN_CEM);
                if ( gen && gen->heartbeat >= gen->period ) {
                    gen_packet_PGN7936(thiz, param);
                    gen->heartbeat = 0;
                }
            } while (0);
            break;
        default:
            break;
        }
        break;
    case EVENT_TX_TP_RTS: // 本系统中BMS通信暂时不会使用
        //串口处于连接管理状态时,将会收到该传输数据报请求。
        break;
    case EVENT_TX_TP_CTS:
    {
        /*串口处于连接管理状态时,将会收到该传输数据报请求。
         * 当数据包接收完成后向BMS发送消息结束应答数据包
         *
         * byte[1]: 0x13
         * byte[2:3]: 消息大小,字节数目
         * byte[4]: 全部数据包数目
         * byte[5]: 0xFF
         * byte[6:8]: PGN
         */

        param->buff.tx_buff[0] = 0x11;
        // 目前的多数据包发送策略是: 无论要发送多少数据包,都一次传输完成
        param->buff.tx_buff[1] = thiz->bms.can_tp_param.tp_pack_nr;
        param->buff.tx_buff[2] = 1;
        param->buff.tx_buff[3] = 0xFF;
        param->buff.tx_buff[4] = 0xFF;
        param->buff.tx_buff[5] = (thiz->bms.can_tp_param.tp_pgn >> 16) & 0xFF;
        param->buff.tx_buff[6] = (thiz->bms.can_tp_param.tp_pgn >> 8 ) & 0xFF;
        param->buff.tx_buff[7] = thiz->bms.can_tp_param.tp_pgn & 0xFF;
        param->buff_payload = 8;
        param->can_id = 0x1cecf456 | CAN_EFF_FLAG;
        param->evt_param = EVT_RET_OK;
    }
        break;
    case EVENT_TX_TP_ACK:
    {
        //串口处于连接管理状态时,将会收到该传输数据报请求。
        param->buff.tx_buff[0] = 0x13;
        // 目前的多数据包发送策略是: 无论要发送多少数据包,都一次传输完成
        param->buff.tx_buff[1] = thiz->bms.can_tp_param.tp_size & 0xFF;
        param->buff.tx_buff[2] = (thiz->bms.can_tp_param.tp_size >> 8) & 0xFF;
        param->buff.tx_buff[3] = thiz->bms.can_tp_param.tp_pack_nr;
        param->buff.tx_buff[4] = 0xFF;
        param->buff.tx_buff[5] = (thiz->bms.can_tp_param.tp_pgn >> 16) & 0xFF;
        param->buff.tx_buff[6] = (thiz->bms.can_tp_param.tp_pgn >> 8 ) & 0xFF;
        param->buff.tx_buff[7] = thiz->bms.can_tp_param.tp_pgn & 0xFF;
        param->buff_payload = 8;
        param->can_id = 0x1cecf456 | CAN_EFF_FLAG;
        param->evt_param = EVT_RET_OK;
    }
        break;
    case EVENT_TX_TP_ABRT:
        //串口处于连接管理状态时,将会收到该传输数据报请求。
        break;
    default:
        break;
    }

    return ERR_OK;
}
Example #2
0
// 驱动对外接口
int driver_main_proc(struct charge_job *thiz, BMS_EVENT_CAN ev,
                        struct bms_event_struct *param, struct bmsdriver *drv)
{
    int ret;

    switch ( ev ) {
    case EVENT_CAN_INIT:
        // 事件循环函数初始化
        thiz->bms.can_bms_status = CAN_NORMAL;
        thiz->bms.can_heart_beat.Hachiko_notify_proc= heart_beart_notify_proc;
        ret = Hachiko_new(&thiz->bms.can_heart_beat, HACHIKO_AUTO_FEED, 1, thiz);
        if ( ret == ERR_OK ) {
            log_printf(INF, "BMS: CHARGER change stage to "RED("CHARGE_STAGE_HANDSHACKING"));
            thiz->bms.charge_stage = CHARGE_STAGE_HANDSHACKING;
            //thiz->charge_stage = CHARGE_STAGE_CONFIGURE;
        } else {
            log_printf(ERR, "BMS: 启动定时器错误.");
        }
        break;
    case EVENT_CAN_RESET:
        // 事件循环函数复位
        // 当发生通信中断时会收到该事件
        break;
    case EVENT_INVALID:
        // 无效事件,事件填充
        break;
    case EVENT_RX_DONE:
        // 数据包接受成功了
        about_packet_reciev_done(thiz, param);
        break;
    case EVENT_RX_ERROR:
        // 数据包接受失败了
        break;
    case EVENT_TX_FAILS:
        // 数据包发送失败了
        log_printf(ERR, "BMS: packet send faile.");
        break;
    case EVENT_TX_DONE:
        // 数据包发送完成了
        param->can_id = param->can_id >> 8;
        about_packet_reciev_done(thiz, param);
        break;
    case EVENT_TX_PRE:
        // 决定是否要发送刚刚准备发送的数据包
        param->evt_param = EVT_RET_OK;
        break;
    case EVENT_TX_REQUEST:
        switch ( thiz->bms.charge_stage ) {
        case CHARGE_STAGE_INVALID:
            param->evt_param = EVT_RET_ERR;
            break;
        case CHARGE_STAGE_HANDSHACKING:
            #if 1
            do {
                struct can_pack_generator *gen = gen_search(thiz->bms.generator,
                                                            thiz->bms.can_pack_gen_nr, PGN_CRM);
                if ( gen && gen->heartbeat >= gen->period ) {
                    gen_packet_PGN256(thiz, param);
                    gen->heartbeat = 0;
                } else {
                    //log_printf(DBG_LV0, "BMS: inner error. %d", __LINE__);
                }
                gen = gen_search(thiz->bms.generator, thiz->bms.can_pack_gen_nr, PGN_CEM);
                if ( gen && gen->heartbeat >= gen->period ) {
                    gen_packet_PGN7936(thiz, param);
                    gen->heartbeat = 0;
                } else {
                    //log_printf(DBG_LV0, "BMS: inner error. %d", __LINE__);
                }
            } while (0);
            #endif
            break;
        case CHARGE_STAGE_CONFIGURE:
            do {
                struct can_pack_generator *gen = gen_search(thiz->bms.generator, thiz->bms.can_pack_gen_nr, PGN_CTS);
                if ( gen && gen->heartbeat >= gen->period ) {
                    gen_packet_PGN1792(thiz, param);
                    gen->heartbeat = 0;
                    log_printf(DBG_LV3, "CTS sent.");
                }
                else if ( (gen = gen_search(thiz->bms.generator, thiz->bms.can_pack_gen_nr, PGN_CML)) &&
                          gen->heartbeat >= gen->period ) {
                    gen_packet_PGN2048(thiz, param);
                    gen->heartbeat = 0;
                    log_printf(DBG_LV3, "CML sent.");
                }
                else if ( (gen = gen_search(thiz->bms.generator, thiz->bms.can_pack_gen_nr, PGN_CRO)) &&
                         gen->heartbeat >= gen->period ) {
                    gen_packet_PGN2560(thiz, param);
                    gen->heartbeat = 0;
                    log_printf(DBG_LV3, "CRO sent.");
                } else {
                    if ( gen == NULL ) {
                        log_printf(ERR, "inner error.");
                    }
                }
                gen = gen_search(thiz->bms.generator, thiz->bms.can_pack_gen_nr, PGN_CEM);
                if ( gen && gen->heartbeat >= gen->period ) {
                    gen_packet_PGN7936(thiz, param);
                    gen->heartbeat = 0;
                }
            } while (0);
            break;
        case CHARGE_STAGE_CHARGING:
            do {
                struct can_pack_generator *gen = gen_search(thiz->bms.generator, thiz->bms.can_pack_gen_nr, PGN_CCS);
                if ( gen && gen->heartbeat >= gen->period ) {
                    gen_packet_PGN4608(thiz, param);
                    gen->heartbeat = 0;
                } else if ( (gen = gen_search(thiz->bms.generator, thiz->bms.can_pack_gen_nr, PGN_CST)) &&
                          gen->heartbeat >= gen->period ) {
                    gen_packet_PGN6656(thiz, param);
                    gen->heartbeat = 0;
                }
                gen = gen_search(thiz->bms.generator, thiz->bms.can_pack_gen_nr, PGN_CEM);
                if ( gen && gen->heartbeat >= gen->period ) {
                    gen_packet_PGN7936(thiz, param);
                    gen->heartbeat = 0;
                }
            } while (0);
            break;
        case CHARGE_STAGE_DONE:
            do {
                struct can_pack_generator *gen = gen_search(thiz->bms.generator, thiz->bms.can_pack_gen_nr, PGN_CSD);
                if ( gen && gen->heartbeat >= gen->period ) {
                    gen_packet_PGN7424(thiz, param);
                    gen->heartbeat = 0;
                }
                gen = gen_search(thiz->bms.generator, thiz->bms.can_pack_gen_nr, PGN_CEM);
                if ( gen && gen->heartbeat >= gen->period ) {
                    gen_packet_PGN7936(thiz, param);
                    gen->heartbeat = 0;
                }
            } while (0);
            break;
        default:
            param->evt_param = EVT_RET_ERR;
            break;
        }
        break;
    case EVENT_TX_TP_RTS: // 本系统中BMS通信暂时不会使用
        //串口处于连接管理状态时,将会收到该传输数据报请求。
        break;
    case EVENT_TX_TP_CTS:
    {
        /*串口处于连接管理状态时,将会收到该传输数据报请求。
         * 当数据包接收完成后向BMS发送消息结束应答数据包
         *
         * byte[1]: 0x13
         * byte[2:3]: 消息大小,字节数目
         * byte[4]: 全部数据包数目
         * byte[5]: 0xFF
         * byte[6:8]: PGN
         */

        param->buff.tx_buff[0] = 0x11;
        // 目前的多数据包发送策略是: 无论要发送多少数据包,都一次传输完成
        param->buff.tx_buff[1] = thiz->bms.can_tp_param.tp_pack_nr;
        param->buff.tx_buff[2] = 1;
        param->buff.tx_buff[3] = 0xFF;
        param->buff.tx_buff[4] = 0xFF;
        param->buff.tx_buff[5] = (thiz->bms.can_tp_param.tp_pgn >> 16) & 0xFF;
        param->buff.tx_buff[6] = (thiz->bms.can_tp_param.tp_pgn >> 8 ) & 0xFF;
        param->buff.tx_buff[7] = thiz->bms.can_tp_param.tp_pgn & 0xFF;
        param->buff_payload = 8;
        param->can_id = 0x1cecf456 | CAN_EFF_FLAG;
        param->evt_param = EVT_RET_OK;
    }
        break;
    case EVENT_TX_TP_ACK:
    {
        //串口处于连接管理状态时,将会收到该传输数据报请求。
        param->buff.tx_buff[0] = 0x13;
        // 目前的多数据包发送策略是: 无论要发送多少数据包,都一次传输完成
        param->buff.tx_buff[1] = thiz->bms.can_tp_param.tp_size & 0xFF;
        param->buff.tx_buff[2] = (thiz->bms.can_tp_param.tp_size >> 8) & 0xFF;
        param->buff.tx_buff[3] = thiz->bms.can_tp_param.tp_pack_nr;
        param->buff.tx_buff[4] = 0xFF;
        param->buff.tx_buff[5] = (thiz->bms.can_tp_param.tp_pgn >> 16) & 0xFF;
        param->buff.tx_buff[6] = (thiz->bms.can_tp_param.tp_pgn >> 8 ) & 0xFF;
        param->buff.tx_buff[7] = thiz->bms.can_tp_param.tp_pgn & 0xFF;
        param->buff_payload = 8;
        param->can_id = 0x1cecf456 | CAN_EFF_FLAG;
        param->evt_param = EVT_RET_OK;
    }
        break;
    case EVENT_TX_TP_ABRT:
        //串口处于连接管理状态时,将会收到该传输数据报请求。
        break;
    default:
        break;
    }

    return ERR_OK;
}