]> rtime.felk.cvut.cz Git - pes-rpp/rpp-test-sw.git/blob - rpp/lib/cmdproc/src/commands/cmd_lin.c
Licence header added to all CMD files, comments to functions added
[pes-rpp/rpp-test-sw.git] / rpp / lib / cmdproc / src / 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 "rpp/rpp.h"
31 #include "sys/sys.h"
32 #include "hal/hal.h"
33 #include "commands/cmd_lin.h"
34
35 /** Semaphore used to stop command, until message is received */
36 xSemaphoreHandle linMsgReceived;
37 /** Semaphore used to stop command, until ID is received */
38 xSemaphoreHandle linIDReceived;
39
40 /**
41  *      @brief  Function called by LIN ISR
42  *      Gives semaphores according the type of interrupt
43  *
44  *      @param[in] lin  Pointer to LIN registers
45  *      @param[in] flags        Type of interrupt
46  */
47 void linNotification(linBASE_t *lin, uint32_t flags)
48 {
49                 if (flags & LIN_ID_INT) {
50                         xSemaphoreGiveFromISR(linIDReceived, NULL);
51                 }
52                 if (flags & LIN_RX_INT) {
53                         lin->FLR |= (1 << 9);
54                         xSemaphoreGiveFromISR(linMsgReceived, NULL);
55                 }
56 }
57
58 /**     @brief Command for testing LIN loopback
59  *
60  *      Command sends short data chunk and counts errors when receiving through loopback.
61  *
62  * @param[in]   cmd_io  Pointer to IO functions
63  * @param[in]   cmd_des Pointer to command descriptor
64  * @param[in]   param   Pointer to an array of parameters
65  *
66  * @return
67  */
68 int cmd_do_lin_loop_back(cmd_io_t *cmd_io, const struct cmd_des *des, char *param[]) {
69         uint8_t txData[8] = {'L', 'I', 'N', ' ', 'T', 'E', 'S', 'T'};
70         uint8_t rxData[8];
71         uint8_t txID = 0xAC;
72         uint8_t rxID = 0;
73
74         linEnableLoopback(linREG, Digital);
75         hal_gpio_pin_set_value(PIN_DSC_LIN1NSLP, 1);
76         linIDReceived = xSemaphoreCreateCounting(1, 0);
77         linMsgReceived = xSemaphoreCreateCounting(1, 0);
78
79         linSetLength(linREG, 8);
80         linSendHeader(linREG, txID);
81         while(!linIsTxReady(linREG)) ;
82         xSemaphoreTake(linIDReceived, portMAX_DELAY);
83         rxID = linGetIdentifier(linREG);
84         if (rxID == txID) {
85                 linSend(linREG, txData);
86                 while(!linIsTxReady(linREG)) ;
87                 xSemaphoreTake(linMsgReceived, portMAX_DELAY);
88                 linGetData(linREG, rxData);
89                 uint8_t errCnt = 0;
90                 uint8_t i;
91                 for (i = 0; i < 8; i++) {
92                         if (txData[i] != rxData[i]) errCnt++;
93                 }
94
95                 if (!errCnt) {
96                         rpp_sci_printf("OK");
97                 }
98                 else {
99                         rpp_sci_printf("Transmission errors: %d\r\n", errCnt);
100                 }
101         }
102         else {
103                 rpp_sci_printf("FAILED: Sent and Received ID does not match.");
104         }
105         vSemaphoreDelete(linIDReceived);
106         vSemaphoreDelete(linMsgReceived);
107         hal_gpio_pin_set_value(PIN_DSC_LIN1NSLP, 0);
108         return 0;
109 }
110
111 /** Descriptor of command for lin loopback test */
112 cmd_des_t const cmd_des_lin_loop_back={
113     0, 0,
114     "testlin","Test digital loopback on LIN",
115     cmd_do_lin_loop_back, (void *)&cmd_list_lin
116 };
117
118 /** List of commands for lin, defined as external */
119 cmd_des_t const *cmd_list_lin[]={
120   &cmd_des_lin_loop_back,
121   NULL
122 };