]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lib.git/blob - rpp/src/rpp/fr.c
Merge branch 'master' of rtime.felk.cvut.cz:pes-rpp/rpp-lib
[pes-rpp/rpp-lib.git] / rpp / src / rpp / fr.c
1 /* Copyright (C) 2013 Czech Technical University in Prague
2  *
3  * Authors:
4  *     - Carlos Jenkins <carlos@jenkins.co.cr>
5  *     - Michal Horn <hornmich@fel.cvut.cz>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  *
20  * File : fr.c
21  * Abstract:
22  *     FlexRay Communication RPP API implementation file.
23  *
24  * References:
25  *     fr.h
26  *     RPP API documentation.
27  */
28
29
30 #include "rpp/rpp.h"
31 #include "stdio.h"
32 #include "string.h"
33
34 #if rppCONFIG_INCLUDE_FR == 1
35
36 static rpp_fr_state_t rpp_fr_state = RPP_FR_NOT_INITIALIZED;    /**< Stores the actual state of the FlexRay module */
37
38 /* AUTOSAR-like API */
39
40 int8_t rpp_fr_init_driver(const Fr_ConfigType* config_ptr, uint32_t* error) {
41         if (rpp_fr_state >= RPP_FR_DRV_INITIALIZED)
42                 return FAILURE;
43         Fr_Init(config_ptr);
44         rpp_fr_state = RPP_FR_DRV_INITIALIZED;
45         return SUCCESS;
46 }
47
48 int8_t rpp_fr_init_controller(uint8_t ctrl, uint32_t* error) {
49         Std_ReturnType retVal = ERR_PARAM_NO_ERROR;
50         if (rpp_fr_state == RPP_FR_DRV_INITIALIZED ||
51                         rpp_fr_state == RPP_FR_HALTED ||
52                         rpp_fr_state == RPP_FR_ABORTED) {
53                 retVal = Fr_ControllerInit(ctrl);
54                 if (retVal & E_OK) {
55                         rpp_fr_state = RPP_FR_CTR_INITIALIZED;
56                         return SUCCESS;
57                 }
58                 else {
59                         *error = retVal;
60                         return FAILURE;
61                 }
62         }
63         return FAILURE;
64 }
65
66 int8_t rpp_fr_start_communication(uint8_t ctrl, uint32_t* error) {
67         Std_ReturnType retVal = ERR_PARAM_NO_ERROR;
68         if (rpp_fr_state == RPP_FR_CTR_INITIALIZED) {
69                 retVal = Fr_StartCommunication(ctrl);
70                 if (retVal & E_OK) {
71                         rpp_fr_state = RPP_FR_RUNNING;
72                         return SUCCESS;
73                 }
74                 else {
75                         *error = retVal;
76                         return FAILURE;
77                 }
78         }
79         return FAILURE;
80 }
81
82 int8_t rpp_fr_all_slots(uint8_t ctrl) {
83         if (rpp_fr_state == RPP_FR_RUNNING && Fr_AllSlots(ctrl) & E_OK) {
84                 return SUCCESS;
85         }
86         return FAILURE;
87 }
88
89 int8_t rpp_fr_halt_communication(uint8_t ctrl) {
90         if (rpp_fr_state == RPP_FR_RUNNING && Fr_HaltCommunication(ctrl) & E_OK) {
91                 rpp_fr_state = RPP_FR_HALTED;
92                 return SUCCESS;
93         }
94         return FAILURE;
95 }
96
97 int8_t rpp_fr_abort_communication(uint8_t ctrl) {
98         if (rpp_fr_state == RPP_FR_RUNNING && Fr_AbortCommunication(ctrl) & E_OK) {
99                 rpp_fr_state = RPP_FR_ABORTED;
100                 return SUCCESS;
101         }
102         return FAILURE;
103 }
104
105 int8_t rpp_fr_send_wup(uint8_t ctrl) {
106         if (rpp_fr_state == RPP_FR_CTR_INITIALIZED && Fr_SendWUP(ctrl) & E_OK) {
107                 return SUCCESS;
108         }
109         return FAILURE;
110 }
111
112 int8_t rpp_fr_set_wu_channel(uint8_t ctrl, Fr_ChannelType channel) {
113         if (rpp_fr_state >= RPP_FR_CTR_INITIALIZED && Fr_SetWakeupChannel(ctrl, channel) & E_OK) {
114                 return SUCCESS;
115         }
116         return FAILURE;
117 }
118
119 int8_t rpp_fr_get_poc_status(uint8_t ctrl, Fr_POCStatusType* poc_status_ptr) {
120         if (rpp_fr_state >= RPP_FR_DRV_INITIALIZED && Fr_GetPOCStatus(ctrl, poc_status_ptr) & E_OK) {
121                 return SUCCESS;
122         }
123         return FAILURE;
124 }
125
126 int8_t rpp_fr_transmit_lpdu(uint8_t ctrl, uint16_t lpdu_idx, const uint8_t* lsdu, uint8_t lsdu_length) {
127         if (rpp_fr_state == RPP_FR_RUNNING && Fr_TransmitTxLPdu(ctrl, lpdu_idx, lsdu, lsdu_length) & E_OK) {
128                 return SUCCESS;
129         }
130         return FAILURE;
131 }
132
133 int8_t rpp_fr_cancel_transmit_lpdu(uint8_t ctrl, uint16_t lpdu_idx) {
134         if (rpp_fr_state == RPP_FR_RUNNING && Fr_CancelTxLPdu(ctrl, lpdu_idx) & E_OK) {
135                 return SUCCESS;
136         }
137         return FAILURE;
138 }
139
140 int8_t rpp_fr_receive_lpdu(uint8_t ctrl, uint16_t lpdu_idx, uint8_t* lsdu, Fr_RxLPduStatusType* lpdu_status, uint8_t* lsdu_length) {
141         if (rpp_fr_state == RPP_FR_RUNNING && Fr_ReceiveRxLPdu(ctrl, lpdu_idx, lsdu, lpdu_status, lsdu_length) & E_OK) {
142                 return SUCCESS;
143         }
144         return FAILURE;
145 }
146
147 int8_t rpp_fr_check_tx_lpdu_status(uint8_t ctrl, uint16_t lpdu_idx, Fr_TxLPduStatusType* lpdu_status) {
148         if (rpp_fr_state == RPP_FR_RUNNING && Fr_CheckTxLPduStatus(ctrl, lpdu_idx, lpdu_status) & E_OK) {
149                 return SUCCESS;
150         }
151         return FAILURE;
152 }
153
154 int8_t rpp_fr_reconfigure_lpdu(uint8_t ctrl, uint16_t lpdu_idx, uint16_t frame_id, Fr_ChannelType channel, uint8_t cycle_set, uint8_t cycle_offset, uint8_t payload, uint16_t header_crc) {
155         if (rpp_fr_state >= RPP_FR_CTR_INITIALIZED && Fr_ReconfigLPdu(ctrl, lpdu_idx, frame_id, channel, cycle_set, cycle_offset, payload, header_crc) & E_OK) {
156                 return SUCCESS;
157         }
158         return FAILURE;
159 }
160
161 int8_t rpp_fr_disable_lpdu(uint8_t ctrl, uint16_t lpdu_idx) {
162         if (rpp_fr_state == RPP_FR_RUNNING && Fr_DisableLPdu(ctrl, lpdu_idx) & E_OK) {
163                 return SUCCESS;
164         }
165         return FAILURE;
166 }
167
168 int8_t rpp_fr_get_global_time(uint8_t ctrl, uint8_t* cycle, uint16_t* macroticks) {
169         if (rpp_fr_state >= RPP_FR_CTR_INITIALIZED && Fr_GetGlobalTime(ctrl, cycle, macroticks) & E_OK) {
170                 return SUCCESS;
171         }
172         return FAILURE;
173 }
174
175 int8_t rpp_fr_get_network_management_vector(uint8_t ctrl, uint8_t* nm_vector) {
176         if (rpp_fr_state >= RPP_FR_CTR_INITIALIZED && Fr_GetNmVector(ctrl, nm_vector) & E_OK) {
177                 return SUCCESS;
178         }
179         return FAILURE;
180 }
181
182 int8_t rpp_fr_get_channel_status(uint8_t ctrl, uint16_t* channel_a_status, uint16_t* channel_b_status) {
183         if (rpp_fr_state >= RPP_FR_CTR_INITIALIZED && Fr_GetChannelStatus(ctrl, channel_a_status, channel_b_status) & E_OK) {
184                 return SUCCESS;
185         }
186         return FAILURE;
187 }
188
189 int8_t rpp_fr_get_clock_correction(uint8_t ctrl, int16_t* rate_correction, int32_t* offset_correction) {
190         if (rpp_fr_state >= RPP_FR_RUNNING && Fr_GetClockCorrection(ctrl, rate_correction, offset_correction) & E_OK) {
191                 return SUCCESS;
192         }
193         return FAILURE;
194 }
195
196 int8_t rpp_fr_get_sync_frame_list(uint8_t ctrl, uint8_t list_size, uint16_t* channel_a_even_list, uint16_t* channel_b_even_list, uint16_t* channel_a_odd_list, uint16_t* channel_b_odd_list) {
197         if (rpp_fr_state >= RPP_FR_RUNNING && Fr_GetSyncFrameList(ctrl, list_size, channel_a_even_list, channel_b_even_list, channel_a_odd_list, channel_b_odd_list) & E_OK) {
198                 return SUCCESS;
199         }
200         return FAILURE;
201 }
202
203 int8_t rpp_fr_get_wakeup_rx_status(uint8_t ctrl, uint8_t* status) {
204         if (rpp_fr_state >= RPP_FR_DRV_INITIALIZED && Fr_GetWakeupRxStatus(ctrl, status) & E_OK) {
205                 return SUCCESS;
206         }
207         return FAILURE;
208 }
209
210 int8_t rpp_fr_set_timer(uint8_t ctrl, uint8_t timer_idx, uint8_t cycle_set, uint16_t offset_threshold) {
211         if (rpp_fr_state == RPP_FR_RUNNING && Fr_SetAbsoluteTimer(ctrl, timer_idx, cycle_set, offset_threshold) & E_OK) {
212                 return SUCCESS;
213         }
214         return FAILURE;
215 }
216
217 int8_t rpp_fr_cancel_timer(uint8_t ctrl, uint8_t timer_idx) {
218         if (rpp_fr_state == RPP_FR_RUNNING && Fr_CancelAbsoluteTimer(ctrl, timer_idx) & E_OK) {
219                 return SUCCESS;
220         }
221         return FAILURE;
222 }
223
224 int8_t rpp_fr_clear_timer_irq(uint8_t ctrl, uint8_t timer_idx) {
225         if (rpp_fr_state >= RPP_FR_DRV_INITIALIZED && Fr_AckAbsoluteTimerIRQ(ctrl, timer_idx) & E_OK) {
226                 return SUCCESS;
227         }
228         return FAILURE;
229 }
230
231 int8_t rpp_fr_get_timer_irq_status(uint8_t ctrl, uint8_t timer_idx, boolean_t* irq_pending) {
232         if (rpp_fr_state >= RPP_FR_DRV_INITIALIZED && Fr_GetAbsoluteTimerIRQStatus(ctrl, timer_idx, irq_pending) & E_OK) {
233                 return SUCCESS;
234         }
235         return FAILURE;
236 }
237
238 int8_t rpp_fr_get_driver_version(Std_VersionInfoType* version) {
239         Fr_GetVersionInfo(version);
240         return SUCCESS;
241 }
242
243 int8_t rpp_fr_read_com_ctrl_config(uint8_t ctrl, uint8_t param_idx, uint32_t* param_value) {
244         if (Fr_ReadCCConfig(ctrl, param_idx, param_value) & E_OK) {
245                 return SUCCESS;
246         }
247         return FAILURE;
248 }
249
250
251 #endif /* rppCONFIG_INCLUDE_FR */