brazos-trapezoidal
API006MemoryRW.c
Go to the documentation of this file.
1
15//-------------------------------------- Include Files ----------------------------------------------------------------
16
17#include "API006MemoryRW.h"
18
19#include <string.h>
20
21//-------------------------------------- PUBLIC (Variables) -----------------------------------------------------------
22
23//-------------------------------------- PRIVATE (Variables, Constants & Defines) -------------------------------------
24
26
27//-------------------------------------- PRIVATE (Function Prototypes) ------------------------------------------------
28
29
30//=====================================================================================================================
31//-------------------------------------- Public Functions -------------------------------------------------------------
32//=====================================================================================================================
33
34//---------------------------------------------------------------------------------------------------------------------
41{
42 MemRWCtrl.MemoryRW_State = API006_STATE_IDLE;
43 MemRWCtrl.MemoryRW_AddressToRW = 0;
44 MemRWCtrl.MemoryRW_NumTotalBytesRequested = 0;
45// memset(&MemRWCtrl.MemoryRW_BufferWriteAux, 0, sizeof(MemRWCtrl.MemoryRW_BufferWriteAux));
46 memset(&MemRWCtrl.MemoryRW_LastPackageSend, 0, sizeof(MemRWCtrl.MemoryRW_LastPackageSend));
47}
48
49//---------------------------------------------------------------------------------------------------------------------
64{
65 switch (MemRWCtrl.MemoryRW_State)
66 {
67 /* Standart state */
68 case API006_STATE_IDLE: ;
69
70 /* Indicator in Message if the operation is Read (0) or Write (1) */
71 volatile uint8_t RW_bit_indicator = 0;
72
73 /* Reset variables state */
74 MemRWCtrl.MemoryRW_AddressToRW = 0;
75 MemRWCtrl.MemoryRW_NumTotalBytesRequested = 0;
76// memset(&MemRWCtrl.MemoryRW_BufferWriteAux, 0, sizeof(MemRWCtrl.MemoryRW_BufferWriteAux));
77 memset(&MemRWCtrl.MemoryRW_LastPackageSend, 0, sizeof(MemRWCtrl.MemoryRW_LastPackageSend));
78
79 IntegrationCalibration__ClearWriteBuffer();
80
81 /* Bit indicator is a first bit (MSB) in first byte of data */
82 RW_bit_indicator = ((message->data[0] & 0x80) >> 7);
83
84 /* Clean the RW bit indicator to the message*/
85 message->data[0] &= 0x7F;
86
87 /* Address to RW is a four bytes in data */
88 for(int i = 0; i < 4; i++)
89 MemRWCtrl.MemoryRW_AddressToRW += message->data[i] << (3 - i) * 8;
90
91 /* Num bytes request is a two bytes in data and Pass Phrase another two */
92 for(int i = 0; i < 2; i++)
93 {
94 MemRWCtrl.MemoryRW_NumTotalBytesRequested += message->data[i + 4] << (1 - i) * 8;
95 }
96
97 /* Check the conditions to enter in Write or Read request state
98 *
99 * First: Address to RW inside the limits defined;
100 * Second: Number bytes requested inside the limits defined between 0 and MEMORYRW_MAX_DATA_NUMBER
101 *
102 * */
103 if((MemRWCtrl.MemoryRW_AddressToRW >= MEMORYRW_ADRESS_LOWER_LIMIT && MemRWCtrl.MemoryRW_AddressToRW < MEMORYRW_ADRESS_UPPER_LIMIT)
104 && (MemRWCtrl.MemoryRW_NumTotalBytesRequested > 0 && MemRWCtrl.MemoryRW_NumTotalBytesRequested <= MEMORYRW_MAX_BYTES_REQUESTED))
105 {
106 /* Set API State to WRITE_REQUEST */
107 if(RW_bit_indicator)
108 MemRWCtrl.MemoryRW_State = API006_STATE_WRITE_REQUEST;
109
110 /* Set API State to READ_REQUEST */
111 else
112 MemRWCtrl.MemoryRW_State = API006_STATE_READ_REQUEST;
113 }
114 else
115 message->api = 0; /* Return API Error in ACK */
116
117 break;
118 case API006_STATE_READ_REQUEST: ;
119
120 volatile uint16_t Num_R_byte_sequence = 0;
121 volatile uint16_t Num_R_bytes_requested = 0;
122
123 /* The first two bytes in data is a byte sequence and the next two bytes is a number bytes request in the next package */
124 for(int i = 0; i < 2; i++)
125 {
126 Num_R_byte_sequence += message->data[i] << (1 - i) * 8;
127 Num_R_bytes_requested += message->data[i + 2] << (1 - i) * 8;
128 }
129
130 if((Num_R_byte_sequence + Num_R_bytes_requested <= MemRWCtrl.MemoryRW_NumTotalBytesRequested)
131 && Num_R_bytes_requested <= MEMORYRW_MAX_BYTES_READ_PACKAGE)
132 {
133 /* Put the Byte Sequence in first two bytes */
134 for(int i = 0; i < 2; i++)
135 MemRWCtrl.MemoryRW_LastPackageSend[i] = message->data[i];
136
137 /* Second data is a Value in Memory with four bytes */
138 for(int i = 0;i < Num_R_bytes_requested;i++)
139 memcpy((MemRWCtrl.MemoryRW_LastPackageSend + i + 2), ((uint8_t *) MemRWCtrl.MemoryRW_AddressToRW + Num_R_byte_sequence + Num_R_bytes_requested - (1 + i)), sizeof(uint8_t));
140 //MemRWCtrl.MemoryRW_LastPackageSend[i + 2] = MEMORYRW_READ_BYTE((uint8_t *) MemRWCtrl.MemoryRW_AddressToRW, Num_R_byte_sequence + i);
141 //memcpy((MemRWCtrl.MemoryRW_LastPackageSend + 2), ((uint8_t *) MemRWCtrl.MemoryRW_AddressToRW + Num_R_byte_sequence), (Num_R_bytes_requested + 2));
142
143 /* Load the package in TSP and request send without ACK */
144 Tsp__LoadData(message->f_adress, Num_R_bytes_requested + 2, MEMORYRW_API, MemRWCtrl.MemoryRW_LastPackageSend);
145 Tsp__Send(FALSE_T);
146
147 /* If byte sequence is a last */
148 if(Num_R_byte_sequence + (Num_R_bytes_requested) == MemRWCtrl.MemoryRW_NumTotalBytesRequested)
149 MemRWCtrl.MemoryRW_State = API006_STATE_IDLE;
150 }
151 /* If data requested is finish or the number sequence is more high than Data Request */
152 else
153 MemRWCtrl.MemoryRW_State = API006_STATE_IDLE;
154
155 break;
156 case API006_STATE_WRITE_REQUEST: ;
157
158 volatile uint16_t Num_W_byte_sequence = 0;
159 volatile uint16_t Num_W_bytes_requested = message->size - 6; //2 bytes - byte sequence / 2 bytes - pass phrase / 2 bytes - CRC16
160
161 /* The first two bytes in data is a byte sequence */
162 for(int i = 0; i < 2; i++)
163 Num_W_byte_sequence += message->data[i] << (1 - i) * 8;
164
165 /* Check if byte sequence is less then bytes requested */
166 if(Num_W_byte_sequence + (Num_W_bytes_requested) <= MemRWCtrl.MemoryRW_NumTotalBytesRequested)
167 {
168 IntegrationCalibration__InsertDataInBuffer(&message->data[2], Num_W_bytes_requested, Num_W_byte_sequence);
169// /* Number bytes sent to write (2 first bytes in data is byte sequence) */
170// for(int i = 0;i < Num_W_bytes_requested; i++)
171// MemRWCtrl.MemoryRW_BufferWriteAux[i + Num_W_byte_sequence] = message->data[i + 2];
172
173 /* If byte sequence is a last */
174 if(Num_W_byte_sequence + (Num_W_bytes_requested) == MemRWCtrl.MemoryRW_NumTotalBytesRequested)
175 {
176 /* If exist data to Write in flash, write the buffer in memory */
177 if(MemRWCtrl.MemoryRW_NumTotalBytesRequested > 0)
178 {
179 /* If the request flash operation failed, set the api to 0 to indicate a error */
180 if(!(IntegrationCalibration__RequestFlashOperation(INTCALIB_FLASH_OPERATION_WRITE, MemRWCtrl.MemoryRW_AddressToRW, MemRWCtrl.MemoryRW_NumTotalBytesRequested)))
181 message->api = 0;
182
183// /* Value of 32 bits to write in memory */
184// volatile uint32_t value = 0;
185//
186// /* Clean the Memory Sector to Write */
187// for(int mem_addr = MemRWCtrl.MemoryRW_AddressToRW; mem_addr < (MemRWCtrl.MemoryRW_AddressToRW + MemRWCtrl.MemoryRW_NumTotalBytesRequested); mem_addr += 0x200)
188// MEMORYRW_CLEAN_SECTOR(mem_addr);
189//
190// /* Write all data storage in Write Buffer Aux in to Memory */
191// for(int i = 0; i < MemRWCtrl.MemoryRW_NumTotalBytesRequested; i += 4)
192// {
193// /* This format to write is specify in NEBO application, if MEMORYRW_WRITE_DATA support write all buffer in to memory, please change this method */
194// value = 0;
195// for(int j = 0; j < 4; j++)
196// value += MemRWCtrl.MemoryRW_BufferWriteAux[i + j] << (3 - j) * 8;
197//
198// MEMORYRW_WRITE_DATA(MemRWCtrl.MemoryRW_AddressToRW + i, value);
199// }
200//
201// /* Exclusive Calibrations Integration */
202// /* If the parameters written is less of the total parameters number, recovery the read only first parameters */
203// if((MemRWCtrl.MemoryRW_NumTotalBytesRequested / 4) < NUMBER_PARAMETERS)
204// {
205// UPDATE_FIRST_PARAMETERS_CALIBRATION(NUMBER_PARAMETERS - (MemRWCtrl.MemoryRW_NumTotalBytesRequested / 4));
206// }
207 /**************************************/
208 }
209 /* When transmitted finish, return the API_STATE to IDLE */
210 MemRWCtrl.MemoryRW_State = API006_STATE_IDLE;
211 }
212 }
213 break;
214 default:
215 MemRWCtrl.MemoryRW_State = API006_STATE_IDLE;
216 break;
217 }
218}
219
220
221//---------------------------------------------------------------------------------------------------------------------
228{
229 return MemRWCtrl.MemoryRW_State;
230}
231
232
233//---------------------------------------------------------------------------------------------------------------------
239{
240 MemRWCtrl.MemoryRW_State = API006_STATE_IDLE;
241}
242
243//=====================================================================================================================
244//-------------------------------------- Private Functions ------------------------------------------------------------
245//=====================================================================================================================
void API006MemoryRW__Initialize(void)
It Initializes the module API006MemoryRW and its variables.
void API006MemoryRW__ResetState(void)
Reset the state of API for Idle.
API006_MEMORYRW_CONTROL_TYPE MemRWCtrl
API006_STATE_TYPE API006MemoryRW__GetState(void)
Get actual state of the API.
void API006MemoryRW__ReceiveParser(TSP_TYPE *message)
This function Parse the data receive for API006 and change the state of the API, if needed.
Header file for API006MemoryRW.c.
API006_STATE_TYPE
States possibles in operation.
#define MEMORYRW_API
API of Library.
#define MEMORYRW_ADRESS_LOWER_LIMIT
Address limit to make operations.
#define MEMORYRW_MAX_BYTES_READ_PACKAGE
Number max of bytes in a read package (data only)
#define MEMORYRW_MAX_BYTES_REQUESTED
Number max of bytes possible requested to read or write in one operation.
BOOL_TYPE Tsp__Send(BOOL_TYPE wait_ack)
Method to send Tsp message.
Definition: Tsp.c:294
Struct for control parameters.
Definition: Tsp.h:23