Skip to main content

G++ LIN Master Simulation Program

Building the Runtime Environment​

Change the current directory to the directory that contains the source code, and then run make.
When the build completes normally, the executable file mpx_lin_mstsample is generated.

Operation Specifications​

ItemDescription
CH1Operates in master simulation mode.
Sends LIN frames using the schedule transmission function and interrupt transmission function (for the LIN frame contents, see Transmission Schedule and Interrupt Transmission Frame).
Also calls MPXGetLog every 100 ms to acquire logs and displays them on the console.
CH2Not used

Transmission Schedule​

The contents of each transmission schedule are as follows.

No.DirIDDelayDLData (Hex)
D1D2D3D4D5D6D7D8
0
(*1)
Tx10H500ms240HC8H
Rx11H500ms-
1
(*1)
Tx20H500ms420H64HA8HECH
Rx21H500ms-

*1: During monitoring, schedule No. 0 is used in the first half and schedule No. 1 is used in the second half (the schedule is automatically switched during monitoring).

Interrupt Transmission Frame​

The interrupt transmission frame contents are as follows.

DirIDDLData (Hex)
D1D2D3D4D5D6D7D8
Tx30H801H23H45H67H89HABHCDHEFH

Log Display Details​

ItemDescription
TimeDisplays the timestamp.
Dir.Displays the log direction.
Rx: receive log / Tx: transmit log
TypeDisplays the log type.
IDDisplays the LIN ID (hexadecimal).
SBLDisplays the SyncBreak width.
DLDisplays the data length (0 to 64).
DataDisplays data (D1 to D64) in hexadecimal. Data bytes are separated by spaces.
CSDisplays the checksum in hexadecimal.
WPLDisplays the wakeup pulse width.

Sample Source Code​

Source: MPXCtrl_LIN_MstSample/mpx_lin_mstsample.c

#include "MPXCtrlFree.h"
#include "stdio.h"
#include "unistd.h"
#include <chrono>
#include <deque>
#include <iostream>
#include <pthread.h>
#include <sstream>
#include <stdint.h>
#include <string.h>
#include <thread>

// グローバル
uint32_t serial;

// 表示文字列
std::deque<std::string> print_que;

void DispMonitor(void);

pthread_mutex_t que_mutex;
std::string strbuf;

void *thread_routine(void *data) {
while (1) {
usleep(1000 * 5); // wait1ms
pthread_mutex_lock(&que_mutex);
if (!print_que.empty()) {
strbuf = print_que.front();
print_que.pop_front(); // 先頭要素を削除
std::cout << strbuf; // バッファを表示
}
pthread_mutex_unlock(&que_mutex);
pthread_testcancel();
}
return 0;
}

int main() {
ER ret;
uint32_t MSec;
uint16_t USec;

pthread_t print_thread;

printf("LIN Master Sample Start\n");

/* ADLバージョン取得 */
StMPXAPIVersion apiVersion;
MPXGetAPIVersion(&apiVersion);
printf("API Version : [%s]\n", apiVersion.APIVersion);

/* MicroPeckerXオープン */
StMPXDeviceInfo devices[4];
uint8_t count;

ret = MPXOpen(devices, 4, &count);
if (ret != E_OK) {
printf("MPXOpen : error[%d]\n", ret);
return 1;
} else if (count == 0) {
printf("MPXOpen : no devices\n");
return 1;
} else {
/* シリアル番号 */
serial = devices[0].Serial;
/* LED消灯 */
MPXSetLED(serial, MPX_LED_OFF, MPX_LED_OFF, MPX_LED_OFF, MPX_LED_OFF, MPX_LED_OFF);
}

for (int i = 0; i < count; i++) {
printf("Serial %d: [%d]\n", i, devices[i].Serial);
}

/* LIN通信パラメータ設定 */
StMPXLINParam param;
memset(&param, '\0', sizeof(param));
param.Mode = MPX_LIN_MODE_MASTER; /* マスタシミュレーションモード */
param.Revision = MPX_LIN_REV_21;
param.Baudrate = 19200;
param.SyncBreak = 13;
param.Delimiter = 1;
param.HeaderSpace = 1;
param.ResponseSpace = 1;
param.ByteSpace = 1;
param.WakeupWidth = 250;
param.WakeupWait = 0;
param.SleepTime = 4000;
for (int i = 0x00; i < 0x40; i++) {
/* DL */
if (i < 0x20) {
param.IDSetting[i].DL = 2;
} else if (i < 0x30) {
param.IDSetting[i].DL = 4;
} else {
param.IDSetting[i].DL = 8;
}

/* CheckSum */
param.IDSetting[i].CheckSumType = MPX_LIN_PARAM_CS_CLASSIC;
}

ret = MPXLINSetParam(serial, &param);
if (ret != E_OK)
{
printf("\n MPXLINSetParam : error[%d]\n", ret);
return -1;
}

/* LINマスタシミュレーションスケジュール設定 */
MPXLINSetMasterSchedule(serial, 0, MPX_LIN_SIM_ENA, MPX_LIN_SIM_DIS);
MPXLINSetMasterSchedule(serial, 1, MPX_LIN_SIM_ENA, MPX_LIN_SIM_DIS);

/* LINマスタシミュレーション情報を入力 */
StMPXLINMasterSim MasterSim;

/* スケジュール0 */
memset(&MasterSim, '\0', sizeof(MasterSim));
MasterSim.Item[0].Enabled = MPX_LIN_SIM_ENA;
MasterSim.Item[0].OneTime = MPX_LIN_SIM_DIS;
MasterSim.Item[0].Delay = 500;
MasterSim.Item[0].FrameType = MPX_LIN_FRAME_TYPE_TX;
MasterSim.Item[0].Frame.ID = 0x10;
MasterSim.Item[0].Frame.DL = 2;
MasterSim.Item[0].Frame.Data[0] = 0x40;
MasterSim.Item[0].Frame.Data[1] = 0xC8;
MasterSim.Item[1].Enabled = MPX_LIN_SIM_ENA;
MasterSim.Item[1].OneTime = MPX_LIN_SIM_DIS;
MasterSim.Item[1].Delay = 500;
MasterSim.Item[1].FrameType = MPX_LIN_FRAME_TYPE_RX;
MasterSim.Item[1].Frame.ID = 0x11;

/* LINマスタシミュレーション設定(スケジュール0) */
ret = MPXLINSetMasterSim(serial, 0, &MasterSim, 2);
if (ret != E_OK) {
printf("\n MPXLINSetMasterSim Sch. 0 : error[%d]\n", ret);
return -1;
}

/* スケジュール1 */
memset(&MasterSim, '\0', sizeof(MasterSim));
MasterSim.Item[0].Enabled = MPX_LIN_SIM_ENA;
MasterSim.Item[0].OneTime = MPX_LIN_SIM_DIS;
MasterSim.Item[0].Delay = 500;
MasterSim.Item[0].FrameType = MPX_LIN_FRAME_TYPE_TX;
MasterSim.Item[0].Frame.ID = 0x20;
MasterSim.Item[0].Frame.DL = 4;
MasterSim.Item[0].Frame.Data[0] = 0x20;
MasterSim.Item[0].Frame.Data[1] = 0x64;
MasterSim.Item[0].Frame.Data[2] = 0xA8;
MasterSim.Item[0].Frame.Data[3] = 0xEC;
MasterSim.Item[1].Enabled = MPX_LIN_SIM_ENA;
MasterSim.Item[1].OneTime = MPX_LIN_SIM_DIS;
MasterSim.Item[1].Delay = 500;
MasterSim.Item[1].FrameType = MPX_LIN_FRAME_TYPE_RX;
MasterSim.Item[1].Frame.ID = 0x21;

/* LINマスタシミュレーション設定(スケジュール1) */
ret = MPXLINSetMasterSim(serial, 1, &MasterSim, 2);
if (ret != E_OK) {
printf("\n MPXLINSetMasterSim Sch. 1 : error[%d]\n", ret);
return -1;
}

/* LINマスタシミュレーション実行対象スケジュール設定 */
MPXLINChangeMasterSchedule(serial, 0); /* スケジュール0 */

/* LINステータス設定 */
MPXLINSetStatus(serial, MPX_LIN_STS_RUN); /* Run(Awake)状態 */

printf("\n Monitor Start ...\n");
/* モニタリング開始 */
ret = MPXMonitorStart(serial, MPX_SYNC_MASTER);
if (ret != E_OK) {
printf("\n Monitor Start : error[%d]\n", ret);
return -1;
}

int pth_arg;
// 表示スレッド起動
pthread_mutex_init(&que_mutex, NULL);
if (pthread_create(&print_thread, NULL, thread_routine, (void *)&pth_arg) != 0) {
perror("pthread_create\n");
return 1;
}

/* モニタリング */
for (int i = 0; i < 100; i++) {
/* usleep (コメントアウト中):
ソフトウェア制御では周期送信間隔を正確に維持できない
(スケジューリングにより設定したSleep時間より多くの時間を待たされる) */
// usleep(50)
std::this_thread::sleep_for(std::chrono::milliseconds(100));

if(i % 10 == 0) {
StMPXLINMasterSimItem InterruptItem;

/* 割り込み送信情報を入力 */
InterruptItem.Enabled = MPX_LIN_SIM_ENA;
InterruptItem.OneTime = MPX_LIN_SIM_ENA;
InterruptItem.Delay = 200;
InterruptItem.FrameType = MPX_LIN_FRAME_TYPE_TX;
InterruptItem.Frame.ID = 0x30;
InterruptItem.Frame.DL = 8;
for (int i = 0; i < InterruptItem.Frame.DL; i++) {
InterruptItem.Frame.Data[i] = 0x22 * i + 0x01;
}
InterruptItem.Frame.ErrorCode = MPX_LIN_ERR_NONE;

/* 割り込み送信 */
MPXLINMasterInterrupt(serial, &InterruptItem);
}

if(i == 50) {
/* LINマスタシミュレーション実行対象スケジュール設定 */
MPXLINChangeMasterSchedule(serial, 1); /* スケジュール1 */
}

DispMonitor();
}

ret = MPXMonitorStop(serial, &MSec, &USec);
if (ret != E_OK) {
printf("MPXMonitorStop : error[%d]\n", ret);
return -1;
}
printf("\n Monitor Stop ...\n");

pthread_mutex_lock(&que_mutex);
print_que.clear();
pthread_mutex_unlock(&que_mutex);

usleep(100 * 1000); // wait100ms

pthread_cancel(print_thread);
pthread_join(print_thread, NULL);

// 表示用キューを削除
if (!print_que.empty())
print_que.clear();

MPXClose();
printf("MPXClose\n");
printf("LIN Master Sample End\n");

return 0;
}

void DispMonitor(void) {
/* モニタログ取得 */
uint16_t Count;
uint8_t BufferOver;

ER ret;
StMPXLog *MPXLog;
std::stringstream dispTmp;

ret = MPXGetLog(serial, 1, &MPXLog, &Count, &BufferOver);

if (ret != E_OK) {
printf("MPXGetLog : error[%d]\n", ret);
return;
}
/* ログ表示 */
for (int i = 0; i < (int)Count; i++) {

// 空ログ無視
if (MPXLog[i].Protocol == MPX_LOG_TYPE_EMPTY) {
continue;
}
/* エラーチェック */
if (MPXLog[i].Misc.LIN.ErrorCode == MPX_LIN_ERR_NONE) {
/* 時間 */
dispTmp << "Time : " << std::dec << (int)MPXLog[i].mSec << "[ms] " << (int)MPXLog[i].uSec << "[us] , ";

if (MPXLog[i].Misc.LIN.Type == MPX_LIN_LOG_TYPE_HEADER) {
/* 方向 */
dispTmp << "Dir. : ";
if (MPXLog[i].Dir == MPX_LOG_DIR_RX)
{
dispTmp << "Rx , ";
}
else
{
dispTmp << "Tx , ";
}

/* ヘッダー */
dispTmp << "Type : Header , ";
/* ID */
dispTmp << "ID : " << std::hex << (int)MPXLog[i].Misc.LIN.ID << " , ";
/* SyncBreak幅 */
dispTmp << "SBL : " << std::dec << (int)MPXLog[i].Misc.LIN.SBLen << "[bit]";
} else if (MPXLog[i].Misc.LIN.Type == MPX_LIN_LOG_TYPE_RESPONSE) {
/* 方向 */
dispTmp << "Dir. : ";
if (MPXLog[i].Dir == MPX_LOG_DIR_RX)
{
dispTmp << "Rx , ";
}
else
{
dispTmp << "Tx , ";
}

/* レスポンス */
dispTmp << "Type : Response , ";
/* ID */
dispTmp << "ID : " << std::hex << (int)MPXLog[i].Misc.LIN.ID << " , ";
/* DL */
dispTmp << "DL : " << std::dec << (int)MPXLog[i].Misc.LIN.DL << " , ";
/* Data */
dispTmp << "Data : ";
for (int j = 0; j < MPXLog[i].Misc.LIN.DL; j++) {
dispTmp << std::hex << (int)MPXLog[i].Data[j] << " ";
}
dispTmp << ", ";
/* Checksum */
dispTmp << "CS : " << std::hex << (int)MPXLog[i].Data[MPXLog[i].Misc.LIN.DL];
} else if (MPXLog[i].Misc.LIN.Type == MPX_LIN_LOG_TYPE_WAKEUP) {
/* 方向 */
dispTmp << "Dir. : ";
if (MPXLog[i].Dir == MPX_LOG_DIR_RX)
{
dispTmp << "Rx , ";
}
else
{
dispTmp << "Tx , ";
}

/* ウェイクアップ */
dispTmp << "Type : Wakeup , ";
/* ウェイクアップパルス幅 */
dispTmp << "WPL : " << std::dec << (int)MPXLog[i].Misc.LIN.WakeupWidth << "[ms]";
} else if (MPXLog[i].Misc.LIN.Type == MPX_LIN_LOG_TYPE_SLEEP) {
/* 方向 */
dispTmp << "Dir. : ";
if (MPXLog[i].Dir == MPX_LOG_DIR_RX)
{
dispTmp << "Rx , ";
}
else
{
dispTmp << "Tx , ";
}

/* スリープ */
dispTmp << "Type : Sleep , ";
/* ID */
dispTmp << "ID : " << std::hex << (int)MPXLog[i].Misc.LIN.ID << " , ";
/* DL */
dispTmp << "DL : " << std::dec << (int)MPXLog[i].Misc.LIN.DL << " , ";
/* Data */
dispTmp << "Data : ";
for (int j = 0; j < MPXLog[i].Misc.LIN.DL; j++) {
dispTmp << std::hex << (int)MPXLog[i].Data[j] << " ";
}
dispTmp << ", ";
/* Checksum */
dispTmp << "CS : " << std::hex << (int)MPXLog[i].Data[MPXLog[i].Misc.LIN.DL];
} else if (MPXLog[i].Misc.LIN.Type == MPX_LIN_LOG_TYPE_BUS) {
/* バスエラー */
dispTmp << "Type : Bus Error";
} else if (MPXLog[i].Misc.LIN.Type == MPX_LIN_LOG_TYPE_BUSSLEEP) {
/* バススリープ */
dispTmp << "Type : Bus Sleep";
} else {
/* その他 */
dispTmp << "Type : Ext.";
}

dispTmp << "\n";
}
else
{
if (MPXLog[i].Misc.LIN.ErrorCode == MPX_LIN_ERR_SBL) {
dispTmp << "ERROR : SynchBreak Too Long\n";
} else if (MPXLog[i].Misc.LIN.ErrorCode == MPX_LIN_ERR_SBS) {
dispTmp << "ERROR : SynchBreak Too Short\n";
} else if (MPXLog[i].Misc.LIN.ErrorCode == MPX_LIN_ERR_PARITY) {
dispTmp << "ERROR : Parity Err.\n";
} else if (MPXLog[i].Misc.LIN.ErrorCode == MPX_LIN_ERR_SHORT) {
dispTmp << "ERROR : Short Msg.\n";
} else if (MPXLog[i].Misc.LIN.ErrorCode == MPX_LIN_ERR_CHKSUM) {
dispTmp << "ERROR : CheckSum Err.\n";
} else if (MPXLog[i].Misc.LIN.ErrorCode == MPX_LIN_ERR_NORES) {
dispTmp << "ERROR : Slave Not Responding\n";
} else if (MPXLog[i].Misc.LIN.ErrorCode == MPX_LIN_ERR_BIT) {
dispTmp << "ERROR : Bit Err.\n";
} else if (MPXLog[i].Misc.LIN.ErrorCode == MPX_LIN_ERR_FRAMING) {
dispTmp << "ERROR : Framing Err.\n";
} else if (MPXLog[i].Misc.LIN.ErrorCode == MPX_LIN_ERR_SFIELD) {
dispTmp << "ERROR : SynchField Err.\n";
} else if (MPXLog[i].Misc.LIN.ErrorCode == MPX_LIN_ERR_HTIMEOUT) {
dispTmp << "ERROR : Header Timeout\n";
} else if (MPXLog[i].Misc.LIN.ErrorCode == MPX_LIN_ERR_UART) {
dispTmp << "ERROR : UART Err.\n";
}
}
}
pthread_mutex_lock(&que_mutex);
print_que.push_back(dispTmp.str());
pthread_mutex_unlock(&que_mutex);
}