1 /* -------------------------------- Arctic Core ------------------------------
\r
2 * Arctic Core - the open source AUTOSAR platform http://arccore.com
\r
4 * Copyright (C) 2009 ArcCore AB <contact@arccore.com>
\r
6 * This source code is free software; you can redistribute it and/or modify it
\r
7 * under the terms of the GNU General Public License version 2 as published by the
\r
8 * Free Software Foundation; See <http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt>.
\r
10 * This program is distributed in the hope that it will be useful, but
\r
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
\r
14 * -------------------------------- Arctic Core ------------------------------*/
\r
29 #if defined(USE_DEM)
\r
33 #include "McuExtensions.h"
\r
37 * The state of the PDU router.
\r
39 PduR_StateType PduRState = PDUR_UNINIT; // 960, 31 LINT: Borde åtgärdas
\r
41 const PduR_PBConfigType * PduRConfig;
\r
44 /* ############### Zero Cost Operation Mode ############# */
\r
45 /* Only define the following functions if zero cost operation
\r
46 * mode is not used! They are defined as macros in PduR.h otherwise. */
\r
47 #if PDUR_ZERO_COST_OPERATION == STD_OFF
\r
50 * Initializes the PDU Router.
\r
52 void PduR_Init (const PduR_PBConfigType* ConfigPtr) {
\r
56 // Make sure the PDU Router is uninitialized.
\r
57 // Otherwise raise an error.
\r
58 if (PduRState != PDUR_UNINIT) {
\r
59 // Raise error and return.
\r
60 PDUR_DET_REPORTERROR(MODULE_ID_PDUR, PDUR_INSTANCE_ID, 0x00, PDUR_E_INVALID_REQUEST);
\r
64 if (ConfigPtr == NULL) {
\r
65 PDUR_DET_REPORTERROR(MODULE_ID_PDUR, PDUR_INSTANCE_ID, 0x00, PDUR_E_CONFIG_PTR_INVALID);
\r
68 PduRConfig = ConfigPtr;
\r
71 // Start initialization!
\r
72 DEBUG(DEBUG_LOW,"--Initialization of PDU router--\n");
\r
76 // Initialize buffers.
\r
79 PduRRoutingPath_type *path;
\r
80 PduRConfig->PduRRoutingTable->NRoutingPaths = 0;
\r
81 for (i = 0; (!PduRConfig->PduRRoutingTable->PduRRoutingPath[i].PduR_Arc_EOL) && (!failed); i++) {
\r
82 PduRConfig->PduRRoutingTable->NRoutingPaths++;
\r
83 path = &PduRConfig->PduRRoutingTable->PduRRoutingPath[i];
\r
85 if (path->PduRDestPdu.DataProvision != PDUR_NO_PROVISION) {
\r
86 // Allocate memory for new buffer.
\r
87 PduRTxBuffer_type *buffer = path->PduRDestPdu.TxBufferRef;
\r
89 if (bufferNr >= PDUR_MAX_TX_BUFFER_NUMBER) {
\r
90 DEBUG(DEBUG_LOW,"PduR_Init: Initialization of buffer failed due to erroneous configuration.\nThe number of buffer exceeded the maximum number of allowed buffers.\n");
\r
94 if ((buffer->Buffer = (uint8 *)malloc(buffer->Depth * sizeof(uint8) * path->SduLength)) == 0) {
\r
95 DEBUG(DEBUG_LOW,"PduR_Init: Initialization of buffer failed. Buffer space could not be allocated for buffer number %d\n", bufferNr);
\r
100 buffer->First = buffer->Buffer;
\r
101 buffer->Last = buffer->Buffer;
\r
104 // Initialize the new buffer.
\r
105 buffer->BufferId = i; // Set buffer id.
\r
106 buffer->BufferType = path->PduRDestPdu.DataProvision; // Set buffer data provision mode.
\r
107 buffer->Length = path->SduLength; // Set buffer sdu length.
\r
109 if (path->PduRDestPdu.DataProvision == PDUR_TRIGGER_TRANSMIT) {
\r
110 //memcpy(buffer->First, path->PduRDefaultValue.PduRDefaultValueElement->DefaultValueElement,path->SduLength);
\r
111 PduR_BufferQueue(buffer, path->PduRDefaultValue.PduRDefaultValueElement->DefaultValueElement);
\r
114 // Save pointer to the new buffer.
\r
115 //PduR_RTable_LoIf.RoutingTable[i].TxBufferRef = &PduRBuffers[bufferNr];
\r
117 DEBUG(DEBUG_LOW,"Initialized buffer %d. Id: %d, Type: %d, Depth: %d\n", bufferNr, buffer->BufferId, buffer->BufferType, buffer->Depth);
\r
123 // TODO Report PDUR_E_INIT_FAILED to Dem.
\r
124 PduRState = PDUR_REDUCED;
\r
125 DEBUG(DEBUG_LOW,"--Initialization of PDU router failed--\n");
\r
129 // The initialization succeeded!
\r
130 PduRState = PDUR_ONLINE;
\r
131 DEBUG(DEBUG_LOW,"--Initialization of PDU router completed --\n");
\r
136 void PduR_BufferInc(PduRTxBuffer_type *Buffer, uint8 **ptr) {
\r
137 (*ptr) = (*ptr) + Buffer->Length;
\r
139 // TODO make more efficient without multiplication.
\r
140 if ( *ptr >= ( Buffer->Buffer + (Buffer->Depth * Buffer->Length) ) ) {
\r
141 *ptr = Buffer->Buffer;
\r
143 //*val = (*val + 1) % Buffer->Depth;
\r
146 void PduR_BufferQueue(PduRTxBuffer_type *Buffer, const uint8 * SduPtr) {
\r
147 imask_t state = McuE_EnterCriticalSection();
\r
149 if (PduR_BufferIsFull(Buffer)) { // Buffer is full
\r
150 PduR_BufferFlush(Buffer);
\r
151 #if defined(USE_DEM)
\r
152 Dem_ReportErrorStatus(PDUR_E_PDU_INSTANCE_LOST, DEM_EVENT_STATUS_FAILED);
\r
156 // Copy data to last place in buffer
\r
157 memcpy(Buffer->Last, SduPtr, sizeof(uint8) * Buffer->Length);
\r
159 PduR_BufferInc(Buffer, &Buffer->Last);
\r
161 DEBUG(DEBUG_LOW,"\tBuffer %d: Queued data %d. Status: NrItems %d, First %d, Last %d\n", Buffer->BufferId, *SduPtr, Buffer->NrItems, *Buffer->First, *Buffer->Last);
\r
164 McuE_ExitCriticalSection(state);
\r
167 void PduR_BufferDeQueue(PduRTxBuffer_type *Buffer, uint8 *SduPtr) {
\r
168 imask_t state = McuE_EnterCriticalSection();
\r
170 if (Buffer->NrItems > 0) {
\r
171 memcpy(SduPtr, Buffer->First, sizeof(uint8) * Buffer->Length);
\r
172 PduR_BufferInc(Buffer, &Buffer->First);
\r
174 DEBUG(DEBUG_LOW,"\tBuffer %d: DeQueueing data. Status: NrItems %d, First %d, Last %d\n", Buffer->BufferId, Buffer->NrItems, *Buffer->First, *Buffer->Last);
\r
176 DEBUG(DEBUG_LOW,"\tBuffer %d: Buffer is empty, nothing to dequeue!\n", Buffer->BufferId);
\r
178 McuE_ExitCriticalSection(state);
\r
181 void PduR_BufferFlush(PduRTxBuffer_type *Buffer) {
\r
182 DEBUG(DEBUG_LOW,"\tBuffer %d: Flushing!\n", Buffer->BufferId);
\r
183 imask_t state = McuE_EnterCriticalSection();
\r
184 Buffer->NrItems = 0;
\r
185 Buffer->First = Buffer->Buffer;
\r
186 Buffer->Last = Buffer->Buffer;
\r
187 Buffer->TxConfP = 0;
\r
188 McuE_ExitCriticalSection(state);
\r
191 uint8 PduR_BufferIsFull(PduRTxBuffer_type *Buffer) {
\r
192 imask_t state = McuE_EnterCriticalSection();
\r
194 if (Buffer->NrItems < Buffer->Depth) {
\r
199 McuE_ExitCriticalSection(state);
\r
204 #if PDUR_VERSION_INFO_API == STD_ON
\r
205 void PduR_GetVersionInfo (Std_VersionInfoType* versionInfo){
\r
206 versionInfo->moduleID = (uint16)MODULE_ID_PDUR;
\r
207 versionInfo->vendorID = (uint16)1;
\r
209 // TODO Add vendor specific version numbers.
\r
213 uint32 PduR_GetConfigurationId () {
\r
214 //PduR_DevCheck(0,1,0x18,E_NOT_OK);
\r
215 return PduRConfig->PduRConfigurationId;
\r
217 #endif // End of not Zero Cost Operation Mode
\r
219 Std_ReturnType PduR_CancelTransmitRequest(PduR_CancelReasonType PduCancelReason, PduIdType PduId) {
\r
224 void PduR_ChangeParameterRequest(PduR_ParameterValueType PduParameterValue, PduIdType PduId) {
\r