rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
shared_params.c
Go to the documentation of this file.
1/************************************************************************************//**
2* \file Demo/ARMCM4_STM32F4_Nucleo_F429ZI_GCC/Prog/shared_params.c
3* \brief Shared RAM parameters source file.
4* \ingroup Prog_ARMCM4_STM32F4_Nucleo_F429ZI_GCC
5* \internal
6*----------------------------------------------------------------------------------------
7* C O P Y R I G H T
8*----------------------------------------------------------------------------------------
9* Copyright (c) 2021 by Feaser http://www.feaser.com All rights reserved
10*
11*----------------------------------------------------------------------------------------
12* L I C E N S E
13*----------------------------------------------------------------------------------------
14* This file is part of OpenBLT. OpenBLT is free software: you can redistribute it and/or
15* modify it under the terms of the GNU General Public License as published by the Free
16* Software Foundation, either version 3 of the License, or (at your option) any later
17* version.
18*
19* OpenBLT is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
20* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
21* PURPOSE. See the GNU General Public License for more details.
22*
23* You have received a copy of the GNU General Public License along with OpenBLT. It
24* should be located in ".\Doc\license.html". If not, contact Feaser to obtain a copy.
25*
26* \endinternal
27****************************************************************************************/
28
29/****************************************************************************************
30* Include files
31****************************************************************************************/
32// To know CORTEX_MODEL
33#include "cmparams.h"
34
35#include <stddef.h> /* Standard definitions (NULL). */
36#include "shared_params.h" /* Shared parameters header. */
37
38
39/****************************************************************************************
40* Macro definitions
41****************************************************************************************/
42/** \brief Constant parameter buffer identifier. This value is always located as the
43 * start of the buffer to validate the the RAM contains valid shared parameters.
44 */
45#define SHARED_PARAMS_BUFFER_ID (0xCAFEBABEu)
46
47
48/****************************************************************************************
49* Type definitions
50****************************************************************************************/
51/** \brief Layout of the shared parameters RAM buffer. */
52typedef struct t_shared_params_buffer
53{
54 /** \brief Fixed buffer identifier to validate that the RAM contains valid shared
55 * parameters.
56 */
57 uint32_t identifier;
58 /** \brief Array for the actual parameter data. */
59 uint8_t data[SHARED_PARAMS_CFG_BUFFER_DATA_LEN];
60 /** \brief Checksum value of all the bytes in the buffer, excluding this checksum
61 * value of obvious reasons. The checksum is calculated as the Two's
62 * complement of the sum of the bytes.
63 */
64 uint16_t checksum;
66
67static tSharedParamsBuffer sharedParamsBuffer __attribute__ ((section (".shared")));
68
69/****************************************************************************************
70* Function prototypes
71****************************************************************************************/
72static bool SharedParamsValidateBuffer(void);
73static void SharedParamsWriteChecksum(void);
74static bool SharedParamsVerifyChecksum(void);
75static uint16_t SharedParamsCalculateChecksum(void);
76
77
78/************************************************************************************//**
79** \brief Initializes the shared RAM parameters module.
80** \return none.
81**
82****************************************************************************************/
84{
85 uint32_t byteIdx;
86
87 /* The shared parameter buffer does not get initialized by the C-startup code. Another
88 * previously running program could have initialized it, in which case it is ready
89 * for use and nothing more needs to be done.
90 */
92 {
93 /* The shared parameter buffer was not yet initialized by a running program. This
94 * typically happens after a cold reset where the RAM contents were lost. In this
95 * case we need to explicitly configure and initialize it, since the C-startup code
96 * was configured to not do this.
97 *
98 * The initialization consists of setting the buffer identifier, zeroing the
99 * actual parameter data and updating the checksum at the end.
100 */
101 sharedParamsBuffer.identifier = SHARED_PARAMS_BUFFER_ID;
102 for (byteIdx=0; byteIdx < SHARED_PARAMS_CFG_BUFFER_DATA_LEN; byteIdx++)
103 {
104 sharedParamsBuffer.data[byteIdx] = 0;
105 }
107 }
108} /*** end of SharedParamsInit ***/
109
110
111/************************************************************************************//**
112** \brief Reads a data byte from the shared parameter buffer at the specified index.
113** \param idx Index into the parameter data array. A valid value is between 0 and
114** (SHARED_PARAMS_CFG_BUFFER_DATA_LEN - 1).
115** \param value Pointer to where the read data value is stored.
116** \return True if successful, false otherwise.
117**
118****************************************************************************************/
119bool SharedParamsReadByIndex(uint32_t idx, uint8_t * value)
120{
121 bool result = false;
122
123 /* Only continue if the buffer and the specified parameters are valid. */
125 (idx < SHARED_PARAMS_CFG_BUFFER_DATA_LEN) &&
126 (value != NULL) )
127 {
128 /* Read the value and update the result. */
129 *value = sharedParamsBuffer.data[idx];
130 result = true;
131 }
132 /* Give the result back to the caller. */
133 return result;
134} /*** end of SharedParamsReadByIndex ***/
135
136
137/************************************************************************************//**
138** \brief Writes a data byte to the shared parameter buffer at the specified index.
139** \param idx Index into the parameter data array. A valid value is between 0 and
140** (SHARED_PARAMS_CFG_BUFFER_DATA_LEN - 1).
141** \param value Value to write.
142** \return True if successful, false otherwise.
143**
144****************************************************************************************/
145bool SharedParamsWriteByIndex(uint32_t idx, uint8_t value)
146{
147 bool result = false;
148
149 /* Only continue if the buffer and the specified parameters are valid. */
151 (idx < SHARED_PARAMS_CFG_BUFFER_DATA_LEN) )
152 {
153 /* Write the value. */
154 sharedParamsBuffer.data[idx] = value;
155 /* Update the checksum since the contents were just changed. */
157 /* Update the result. */
158 result = true;
159 #if CORTEX_MODEL == 7
160 // If we have a cache, drop the relevant cache lines.
161 SCB_CleanDCache_by_Addr((uint32_t*)&sharedParamsBuffer, sizeof(sharedParamsBuffer));
162 #endif
163 }
164 /* Give the result back to the caller. */
165 return result;
166} /*** end of SharedParamsWriteByIndex ***/
167
168
169/************************************************************************************//**
170** \brief Validates the shared parameter buffer contents by looking at the table
171** identifier and verifying its checksum.
172** \return True if successful, false otherwise.
173**
174****************************************************************************************/
176{
177 bool result = false;
178
179 /* Perform validation. */
180 if ( (sharedParamsBuffer.identifier == SHARED_PARAMS_BUFFER_ID) &&
182 {
183 /* The shared parameter buffer is valid, so update the result value. */
184 result = true;
185 }
186 /* Give the result back to the caller. */
187 return result;
188} /*** end of SharedParamsValitabeTable ***/
189
190
191/************************************************************************************//**
192** \brief Calculates and writes the checksum into the buffer.
193** \return none.
194**
195****************************************************************************************/
197{
198 /* Calculate and write the checksum. */
199 sharedParamsBuffer.checksum = SharedParamsCalculateChecksum();
200} /*** end of SharedParamsWriteChecksum ***/
201
202
203/************************************************************************************//**
204** \brief Calculates and verifies the checksum that is currently present in the
205** buffer.
206** \return True is the checksum is correct, false otherwise.
207**
208****************************************************************************************/
210{
211 bool result = false;
212
213 /* Calculate and verify the checksum. */
214 if (SharedParamsCalculateChecksum() == sharedParamsBuffer.checksum)
215 {
216 /* Checksum is correct, so update the result value. */
217 result = true;
218 }
219 /* Give the result back to the caller. */
220 return result;
221} /*** end of SharedParamsVerifyChecksum ***/
222
223
224/************************************************************************************//**
225** \brief Calculates and returns the checksum value for the current contents in the
226** buffer. The checksum is calculated by taking the sum of all bytes in the
227** parameter buffer (excluding the checksum at the end) and them taking the
228** two's complement value of it.
229** \return The calculated checksum value.
230**
231****************************************************************************************/
232static uint16_t SharedParamsCalculateChecksum(void)
233{
234 uint16_t result = 0;
235 uint32_t byteIdx;
236
237 /* Add the identifier bytes to the checksum. */
238 result += (uint8_t)sharedParamsBuffer.identifier;
239 result += (uint8_t)(sharedParamsBuffer.identifier >> 8u);
240 result += (uint8_t)(sharedParamsBuffer.identifier >> 16u);
241 result += (uint8_t)(sharedParamsBuffer.identifier >> 24u);
242 /* Loop through the parameter data array. */
243 for (byteIdx=0; byteIdx<SHARED_PARAMS_CFG_BUFFER_DATA_LEN; byteIdx++)
244 {
245 /* Add parameter data byte to the checksum. */
246 result += (uint8_t)sharedParamsBuffer.data[byteIdx];
247 }
248 /* Determine one's complement. */
249 result = ~result;
250 /* Determine two's complement. */
251 result += 1;
252 /* Give the result back to the caller. */
253 return result;
254} /*** end of SharedParamsCalculateChecksum ***/
255
256
257/*********************************** end of shared_params.c ****************************/
typedef __attribute__
Ignition Mode.
ARM Cortex-M4 parameters for the Kinetis KE1xF.
static void SharedParamsWriteChecksum(void)
Calculates and writes the checksum into the buffer.
static bool SharedParamsValidateBuffer(void)
Validates the shared parameter buffer contents by looking at the table identifier and verifying its c...
bool SharedParamsWriteByIndex(uint32_t idx, uint8_t value)
Writes a data byte to the shared parameter buffer at the specified index.
void SharedParamsInit(void)
Initializes the shared RAM parameters module.
static bool SharedParamsVerifyChecksum(void)
Calculates and verifies the checksum that is currently present in the buffer.
struct t_shared_params_buffer tSharedParamsBuffer
Layout of the shared parameters RAM buffer.
bool SharedParamsReadByIndex(uint32_t idx, uint8_t *value)
Reads a data byte from the shared parameter buffer at the specified index.
static uint16_t SharedParamsCalculateChecksum(void)
Calculates and returns the checksum value for the current contents in the buffer. The checksum is cal...