]> rtime.felk.cvut.cz Git - pes-rpp/rpp-test-sw.git/blob - rpp-test-sw/commands/cmd_lin.c
8e490e1d4322c125144f8f0a5e525c5794752ca1
[pes-rpp/rpp-test-sw.git] / rpp-test-sw / commands / cmd_lin.c
1 /*
2  * Copyright (C) 2012-2013 Czech Technical University in Prague
3  *
4  * Created on: 28.2.2013
5  *
6  * Authors:
7  *     - Michal Horn
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * File : cmd_lin.c
23  *
24  * Abstract:
25  *      This file contains commands for LIN control.
26  *
27  */
28
29
30 #include "cmd_lin.h"
31
32 #ifndef DOCGEN
33
34 #include "rpp/rpp.h"
35 #include "sys/sys.h"
36 #include "hal/hal.h"
37
38 /** Semaphore used to stop command, until message is received */
39 extern xSemaphoreHandle linMsgReceived;
40 /** Semaphore used to stop command, until ID is received */
41 extern xSemaphoreHandle linIDReceived;
42
43 /**     @brief Command for testing LIN loopback
44  *
45  * @param[in]   cmd_io  Pointer to IO functions
46  * @param[in]   cmd_des Pointer to command descriptor
47  * @param[in]   param   Pointer to an array of parameters
48  *
49  * @return
50  */
51 int cmd_do_lin_loop_back(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[])
52 {
53         uint8_t txData[8] = {'L', 'I', 'N', ' ', 'T', 'E', 'S', 'T'};
54         uint8_t rxData[8];
55         uint8_t txID = 0xAC;
56         uint8_t rxID = 0;
57
58         linEnableLoopback(linREG, Digital);
59         hal_gpio_pin_set_value(PIN_DSC_LIN1NSLP, 1);
60         linIDReceived = xSemaphoreCreateCounting(1, 0);
61         linMsgReceived = xSemaphoreCreateCounting(1, 0);
62
63         linSetLength(linREG, 8);
64         linSendHeader(linREG, txID);
65         while (!linIsTxReady(linREG)) ;
66         xSemaphoreTake(linIDReceived, portMAX_DELAY);
67         rxID = linGetIdentifier(linREG);
68         if (rxID == txID) {
69                 linSend(linREG, txData);
70                 while (!linIsTxReady(linREG)) ;
71                 xSemaphoreTake(linMsgReceived, portMAX_DELAY);
72                 linGetData(linREG, rxData);
73                 uint8_t errCnt = 0;
74                 uint8_t i;
75                 for (i = 0; i < 8; i++) {
76                         if (txData[i] != rxData[i]) errCnt++;
77                 }
78
79                 if (!errCnt)
80                         rpp_sci_printf("OK");
81                 else
82                         rpp_sci_printf("Transmission errors: %d\r\n", errCnt);
83         }
84         else
85                 rpp_sci_printf("FAILED: Sent and Received ID does not match.");
86         vSemaphoreDelete(linIDReceived);
87         vSemaphoreDelete(linMsgReceived);
88         hal_gpio_pin_set_value(PIN_DSC_LIN1NSLP, 0);
89         return 0;
90 }
91
92 #endif  /* DOCGEN */
93
94 /** Descriptor of command for lin loopback test */
95 cmd_des_t const cmd_des_lin_loop_back = {
96         0, 0,
97         "lintest","Test the digital loopback on LIN",
98         "### Command syntax ###\n"
99         "\n"
100         "    lintest\n"
101         "\n"
102         "### Description ###\n"
103         "\n"
104         "This command can be used for testing the LIN. The command starts to\n"
105         "send a short message on the LIN port and is testing if it is correctly\n"
106         "received via an internal loopback.\n",
107         CMD_HANDLER(cmd_do_lin_loop_back), (void *)&cmd_list_lin
108 };
109
110 /** List of commands for lin, defined as external */
111 cmd_des_t const *cmd_list_lin[] = {
112         &cmd_des_lin_loop_back,
113         NULL
114 };