-
Notifications
You must be signed in to change notification settings - Fork 12
/
drv_memory.c.ftl
1289 lines (1090 loc) · 39.9 KB
/
drv_memory.c.ftl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************
MEMORY Driver Interface Implementation
Company:
Microchip Technology Inc.
File Name:
drv_memory.c
Summary:
MEMORY Driver Interface Definition
Description:
The MEMORY Driver provides a interface to access the MEMORY on the PIC32
microcontroller. This file implements the MEMORY Driver interface. This file
should be included in the project if MEMORY driver functionality is needed.
*******************************************************************************/
//DOM-IGNORE-BEGIN
/*******************************************************************************
* Copyright (C) 2018 Microchip Technology Inc. and its subsidiaries.
*
* Subject to your compliance with these terms, you may use Microchip software
* and any derivatives exclusively with Microchip products. It is your
* responsibility to comply with third party license terms applicable to your
* use of third party software (including open source software) that may
* accompany Microchip software.
*
* THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
* EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED
* WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A
* PARTICULAR PURPOSE.
*
* IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
* INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
* WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS
* BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE
* FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN
* ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
* THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
*******************************************************************************/
//DOM-IGNORE-END
// *****************************************************************************
// *****************************************************************************
// Section: Include Files
// *****************************************************************************
// *****************************************************************************
#include "driver/memory/src/drv_memory_local.h"
#include "system/time/sys_time.h"
#include "system/debug/sys_debug.h"
<#if DRV_MEMORY_COMMON_FS_ENABLE == true>
#include "driver/memory/src/drv_memory_file_system.h"
</#if>
// *****************************************************************************
// *****************************************************************************
// Section: Global objects
// *****************************************************************************
// *****************************************************************************
/*************************************************
* Hardware instance objects
*************************************************/
static DRV_MEMORY_OBJECT gDrvMemoryObj[DRV_MEMORY_INSTANCES_NUMBER];
/************************************************
* This token is incremented for every request added to the queue and is used
* to generate a different buffer handle for every request.
***********************************************/
static MEMORY_DEVICE_TRANSFER_STATUS DRV_MEMORY_HandleRead
(
DRV_MEMORY_OBJECT *dObj,
uint8_t *data,
uint32_t blockStart,
uint32_t nBlocks
);
static MEMORY_DEVICE_TRANSFER_STATUS DRV_MEMORY_HandleWrite
(
DRV_MEMORY_OBJECT *dObj,
uint8_t *data,
uint32_t blockStart,
uint32_t nBlocks
);
static MEMORY_DEVICE_TRANSFER_STATUS DRV_MEMORY_HandleErase
(
DRV_MEMORY_OBJECT *dObj,
uint8_t *data,
uint32_t blockStart,
uint32_t nBlocks
);
static MEMORY_DEVICE_TRANSFER_STATUS DRV_MEMORY_HandleEraseWrite
(
DRV_MEMORY_OBJECT *dObj,
uint8_t *data,
uint32_t blockStart,
uint32_t nBlocks
);
static const DRV_MEMORY_TransferOperation gMemoryXferFuncPtr[4] =
{
DRV_MEMORY_HandleRead,
DRV_MEMORY_HandleWrite,
DRV_MEMORY_HandleErase,
DRV_MEMORY_HandleEraseWrite,
};
// *****************************************************************************
// *****************************************************************************
// Section: MEMORY Driver Local Functions
// *****************************************************************************
// *****************************************************************************
static void DRV_MEMORY_EventHandler( MEMORY_DEVICE_TRANSFER_STATUS status, uintptr_t context )
{
DRV_MEMORY_OBJECT *dObj = (DRV_MEMORY_OBJECT *)context;
dObj->isTransferDone = true;
(void) OSAL_SEM_PostISR(&dObj->transferDone);
}
static void DRV_MEMORY_TimerHandler( uintptr_t context )
{
DRV_MEMORY_OBJECT *dObj = (DRV_MEMORY_OBJECT *)context;
(void) OSAL_SEM_PostISR(&dObj->transferDone);
}
static inline uint16_t DRV_MEMORY_UPDATE_TOKEN(uint16_t token)
{
token++;
if (token >= DRV_MEMORY_TOKEN_MAX)
{
token = 1;
}
return token;
}
/* This function populates buffer object with the transfer
* parameters. It also generates a new command handle for the request.
*/
static void DRV_MEMORY_AllocateBufferObject
(
DRV_MEMORY_CLIENT_OBJECT *clientObj,
DRV_MEMORY_COMMAND_HANDLE *handle,
void *buffer,
uint32_t blockStart,
uint32_t nBlocks,
DRV_MEMORY_OPERATION_TYPE opType
)
{
DRV_MEMORY_OBJECT *dObj = &gDrvMemoryObj[clientObj->drvIndex];
DRV_MEMORY_BUFFER_OBJECT *bufferObj = NULL;
bufferObj = &dObj->currentBufObj;
bufferObj->commandHandle = DRV_MEMORY_MAKE_HANDLE((uint32_t)dObj->bufferToken, (uint32_t)clientObj->drvIndex, 0U);
bufferObj->hClient = clientObj;
bufferObj->buffer = buffer;
bufferObj->blockStart = blockStart;
bufferObj->nBlocks = nBlocks;
bufferObj->opType = opType;
bufferObj->status = DRV_MEMORY_COMMAND_QUEUED;
/* Update the token number. */
dObj->bufferToken = DRV_MEMORY_UPDATE_TOKEN(dObj->bufferToken);
if (handle != NULL)
{
*handle = bufferObj->commandHandle;
}
}
/* This function validates the driver handle and returns the client object
* pointer associated with the driver handle if the handle is valid. If the
* driver handle is not valid or if the driver is in a not ready state then
* NULL is returned. */
static DRV_MEMORY_CLIENT_OBJECT * DRV_MEMORY_DriverHandleValidate
(
DRV_HANDLE handle
)
{
uint8_t instance = 0;
uint8_t clientIndex = 0;
DRV_MEMORY_CLIENT_OBJECT *clientObj = NULL;
DRV_MEMORY_OBJECT *dObj = NULL;
/* Validate the handle */
if ((handle != DRV_HANDLE_INVALID) && (handle != 0U))
{
instance = (uint8_t)((handle & DRV_MEMORY_INSTANCE_INDEX_MASK) >> 8);
clientIndex = (uint8_t)(handle & DRV_MEMORY_INDEX_MASK);
if (instance >= DRV_MEMORY_INSTANCES_NUMBER)
{
return (NULL);
}
if (clientIndex >= gDrvMemoryObj[instance].nClientsMax)
{
return (NULL);
}
/* See if the client has been opened */
clientObj = &((DRV_MEMORY_CLIENT_OBJECT *)gDrvMemoryObj[instance].clientObjPool)[clientIndex];
if ((clientObj->clientHandle != handle) || (clientObj->inUse == false))
{
return (NULL);
}
/* Check if the driver is ready for operation */
dObj = &gDrvMemoryObj[clientObj->drvIndex];
if (dObj->status != SYS_STATUS_READY)
{
return (NULL);
}
}
return (clientObj);
}
/* This function updates the driver object's geometry information for the memory
* device. */
static bool DRV_MEMORY_UpdateGeometry( DRV_MEMORY_OBJECT *dObj )
{
MEMORY_DEVICE_GEOMETRY memoryDeviceGeometry = { 0 };
if (dObj->memoryDevice->GeometryGet(dObj->memDevHandle, &memoryDeviceGeometry) == false)
{
return false;
}
/* Read block size and number of blocks */
dObj->mediaGeometryTable[SYS_MEDIA_GEOMETRY_TABLE_READ_ENTRY].blockSize = memoryDeviceGeometry.read_blockSize;
dObj->mediaGeometryTable[SYS_MEDIA_GEOMETRY_TABLE_READ_ENTRY].numBlocks = memoryDeviceGeometry.read_numBlocks;
/* Write block size and number of blocks */
dObj->mediaGeometryTable[SYS_MEDIA_GEOMETRY_TABLE_WRITE_ENTRY].blockSize = memoryDeviceGeometry.write_blockSize;
dObj->mediaGeometryTable[SYS_MEDIA_GEOMETRY_TABLE_WRITE_ENTRY].numBlocks = memoryDeviceGeometry.write_numBlocks;
dObj->writeBlockSize = memoryDeviceGeometry.write_blockSize;
/* Erase block size and number of blocks */
dObj->mediaGeometryTable[SYS_MEDIA_GEOMETRY_TABLE_ERASE_ENTRY].blockSize = memoryDeviceGeometry.erase_blockSize;
dObj->mediaGeometryTable[SYS_MEDIA_GEOMETRY_TABLE_ERASE_ENTRY].numBlocks = memoryDeviceGeometry.erase_numBlocks;
dObj->eraseBlockSize = memoryDeviceGeometry.erase_blockSize;
/* Update the Media Geometry Main Structure */
dObj->mediaGeometryObj.mediaProperty = (SYS_MEDIA_PROPERTY)((uint32_t)SYS_MEDIA_READ_IS_BLOCKING | (uint32_t)SYS_MEDIA_WRITE_IS_BLOCKING);
/* Number of read, write and erase entries in the table */
dObj->mediaGeometryObj.numReadRegions = memoryDeviceGeometry.numReadRegions;
dObj->mediaGeometryObj.numWriteRegions = memoryDeviceGeometry.numWriteRegions;
dObj->mediaGeometryObj.numEraseRegions = memoryDeviceGeometry.numEraseRegions;
dObj->mediaGeometryObj.geometryTable = (SYS_MEDIA_REGION_GEOMETRY *)&dObj->mediaGeometryTable;
dObj->blockStartAddress = memoryDeviceGeometry.blockStartAddress;
return true;
}
/* MISRA C-2012 Rule 16.1, 16.3, 16.5 and 16.6 deviated below.
Deviation record ID - H3_MISRAC_2012_R_11_3_DR_1 & H3_MISRAC_2012_R_11_8_DR_1*/
<#if core.COVERITY_SUPPRESS_DEVIATION?? && core.COVERITY_SUPPRESS_DEVIATION>
<#if core.COMPILER_CHOICE == "XC32">
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunknown-pragmas"
</#if>
#pragma coverity compliance block \
(deviate:4 "MISRA C-2012 Rule 16.1" "H3_MISRAC_2012_R_16_1_DR_1" )\
(deviate:8 "MISRA C-2012 Rule 16.3" "H3_MISRAC_2012_R_16_3_DR_1" )\
(deviate:4 "MISRA C-2012 Rule 16.5" "H3_MISRAC_2012_R_16_5_DR_1" )\
(deviate:3 "MISRA C-2012 Rule 16.6" "H3_MISRAC_2012_R_16_6_DR_1" )
</#if>
static MEMORY_DEVICE_TRANSFER_STATUS DRV_MEMORY_HandleRead
(
DRV_MEMORY_OBJECT *dObj,
uint8_t *data,
uint32_t blockStart,
uint32_t nBlocks
)
{
MEMORY_DEVICE_TRANSFER_STATUS transferStatus;
uint32_t address = 0;
switch (dObj->readState)
{
case DRV_MEMORY_READ_INIT:
default:
{
address = (blockStart * dObj->mediaGeometryTable[0].blockSize) + dObj->blockStartAddress;
dObj->readState = DRV_MEMORY_READ_MEM;
/* Fall through */
}
case DRV_MEMORY_READ_MEM:
{
if (dObj->memoryDevice->Read(dObj->memDevHandle, (void *)data, nBlocks, address) == true)
{
dObj->readState = DRV_MEMORY_READ_MEM_STATUS;
/* Fall through For immediate check */
}
else
{
/* Break in case of failure */
transferStatus = MEMORY_DEVICE_TRANSFER_ERROR_UNKNOWN;
break;
}
}
case DRV_MEMORY_READ_MEM_STATUS:
{
transferStatus = (MEMORY_DEVICE_TRANSFER_STATUS)dObj->memoryDevice->TransferStatusGet(dObj->memDevHandle);
break;
}
}
return transferStatus;
}
static MEMORY_DEVICE_TRANSFER_STATUS DRV_MEMORY_HandleWrite
(
DRV_MEMORY_OBJECT *dObj,
uint8_t *data,
uint32_t blockStart,
uint32_t nBlocks
)
{
MEMORY_DEVICE_TRANSFER_STATUS transferStatus;
switch (dObj->writeState)
{
case DRV_MEMORY_WRITE_INIT:
default:
{
dObj->blockAddress = ((blockStart * dObj->writeBlockSize) + dObj->blockStartAddress);
dObj->nBlocks = nBlocks;
dObj->writePtr = data;
dObj->writeState = DRV_MEMORY_WRITE_MEM;
/* Fall through */
}
case DRV_MEMORY_WRITE_MEM:
{
dObj->isTransferDone = false;
if (dObj->memoryDevice->PageWrite(dObj->memDevHandle, (void *)dObj->writePtr, dObj->blockAddress) == true)
{
dObj->writeState = DRV_MEMORY_WRITE_MEM_STATUS;
/* Fall through For immediate check */
}
else
{
/* Break in case of failure */
transferStatus = MEMORY_DEVICE_TRANSFER_ERROR_UNKNOWN;
break;
}
}
case DRV_MEMORY_WRITE_MEM_STATUS:
{
transferStatus = (MEMORY_DEVICE_TRANSFER_STATUS)dObj->memoryDevice->TransferStatusGet(dObj->memDevHandle);
if (transferStatus == MEMORY_DEVICE_TRANSFER_COMPLETED)
{
dObj->nBlocks--;
if (dObj->nBlocks != 0U)
{
/* There is still data to be programmed. */
dObj->blockAddress += dObj->writeBlockSize;
dObj->writePtr += dObj->writeBlockSize;
dObj->writeState = DRV_MEMORY_WRITE_MEM;
transferStatus = MEMORY_DEVICE_TRANSFER_BUSY;
}
}
break;
}
}
return transferStatus;
}
static MEMORY_DEVICE_TRANSFER_STATUS DRV_MEMORY_HandleErase
(
DRV_MEMORY_OBJECT *dObj,
uint8_t *data,
uint32_t blockStart,
uint32_t nBlocks
)
{
MEMORY_DEVICE_TRANSFER_STATUS transferStatus;
switch (dObj->eraseState)
{
case DRV_MEMORY_ERASE_INIT:
default:
{
dObj->blockAddress = ((blockStart * dObj->eraseBlockSize) + dObj->blockStartAddress);
dObj->nBlocks = nBlocks;
dObj->eraseState = DRV_MEMORY_ERASE_CMD;
/* Fall through */
}
case DRV_MEMORY_ERASE_CMD:
{
dObj->isTransferDone = false;
if (dObj->memoryDevice->SectorErase(dObj->memDevHandle, dObj->blockAddress) == true)
{
dObj->eraseState = DRV_MEMORY_ERASE_CMD_STATUS;
/* Fall through For immediate check */
}
else
{
/* Break in case of failure */
transferStatus = MEMORY_DEVICE_TRANSFER_ERROR_UNKNOWN;
break;
}
}
case DRV_MEMORY_ERASE_CMD_STATUS:
{
transferStatus = (MEMORY_DEVICE_TRANSFER_STATUS)dObj->memoryDevice->TransferStatusGet(dObj->memDevHandle);
if (transferStatus == MEMORY_DEVICE_TRANSFER_COMPLETED)
{
dObj->nBlocks--;
if (dObj->nBlocks != 0U)
{
/* There is still data to be programmed. */
dObj->blockAddress += dObj->eraseBlockSize;
dObj->eraseState = DRV_MEMORY_ERASE_CMD;
transferStatus = MEMORY_DEVICE_TRANSFER_BUSY;
}
}
break;
}
}
return transferStatus;
}
static MEMORY_DEVICE_TRANSFER_STATUS DRV_MEMORY_HandleEraseWrite
(
DRV_MEMORY_OBJECT *dObj,
uint8_t *data,
uint32_t blockStart,
uint32_t nBlocks
)
{
DRV_MEMORY_BUFFER_OBJECT *bufferObj = &dObj->currentBufObj;
uint8_t pagesPerSector = (uint8_t)(dObj->eraseBlockSize / dObj->writeBlockSize);
uint32_t readBlockStart = 0;
MEMORY_DEVICE_TRANSFER_STATUS transferStatus;
switch (dObj->ewState)
{
case DRV_MEMORY_EW_INIT:
default:
{
dObj->readState = DRV_MEMORY_READ_INIT;
dObj->eraseState = DRV_MEMORY_ERASE_INIT;
dObj->writeState = DRV_MEMORY_WRITE_INIT;
/* Find the sector for the starting page */
dObj->sectorNumber = bufferObj->blockStart / pagesPerSector;
/* Find the number of sectors to be updated in this block. */
dObj->blockOffsetInSector = (bufferObj->blockStart % pagesPerSector);
dObj->nBlocksToWrite = (pagesPerSector - dObj->blockOffsetInSector);
if (bufferObj->nBlocks < dObj->nBlocksToWrite)
{
dObj->nBlocksToWrite = bufferObj->nBlocks;
}
if (dObj->nBlocksToWrite != pagesPerSector)
{
dObj->writePtr = dObj->ewBuffer;
dObj->ewState = DRV_MEMORY_EW_READ_SECTOR;
}
else
{
dObj->writePtr = bufferObj->buffer;
dObj->ewState = DRV_MEMORY_EW_ERASE_SECTOR;
transferStatus = MEMORY_DEVICE_TRANSFER_BUSY;
break;
}
/* Fall through for read operation. */
}
case DRV_MEMORY_EW_READ_SECTOR:
{
readBlockStart = (dObj->sectorNumber * dObj->eraseBlockSize);
transferStatus = DRV_MEMORY_HandleRead (dObj, dObj->ewBuffer, readBlockStart, dObj->eraseBlockSize);
if (transferStatus == MEMORY_DEVICE_TRANSFER_COMPLETED)
{
/* Find the offset from which the data is to be overlaid. */
dObj->blockOffsetInSector *= dObj->writeBlockSize;
(void)memcpy((void *)&dObj->ewBuffer[dObj->blockOffsetInSector], (const void *)bufferObj->buffer, dObj->nBlocksToWrite * dObj->writeBlockSize);
dObj->ewState = DRV_MEMORY_EW_ERASE_SECTOR;
/* Fall through for Erase operation. */
}
else
{
break;
}
}
case DRV_MEMORY_EW_ERASE_SECTOR:
{
transferStatus = DRV_MEMORY_HandleErase(dObj, NULL, dObj->sectorNumber, 1);
if (transferStatus == MEMORY_DEVICE_TRANSFER_COMPLETED)
{
dObj->ewState = DRV_MEMORY_EW_WRITE_SECTOR;
transferStatus = MEMORY_DEVICE_TRANSFER_BUSY;
}
break;
}
case DRV_MEMORY_EW_WRITE_SECTOR:
{
transferStatus = DRV_MEMORY_HandleWrite (dObj, dObj->writePtr, dObj->sectorNumber * pagesPerSector, pagesPerSector);
if (transferStatus == MEMORY_DEVICE_TRANSFER_COMPLETED)
{
if ((bufferObj->nBlocks - dObj->nBlocksToWrite) == 0U)
{
/* This is the last write operation. */
break;
}
/* Update the number of block still to be written, sector address
* and the buffer pointer */
bufferObj->nBlocks -= dObj->nBlocksToWrite;
bufferObj->blockStart += dObj->nBlocksToWrite;
bufferObj->buffer += (dObj->nBlocksToWrite * dObj->writeBlockSize);
dObj->ewState = DRV_MEMORY_EW_INIT;
transferStatus = MEMORY_DEVICE_TRANSFER_BUSY;
}
break;
}
}
return transferStatus;
}
<#if core.COVERITY_SUPPRESS_DEVIATION?? && core.COVERITY_SUPPRESS_DEVIATION>
#pragma coverity compliance end_block "MISRA C-2012 Rule 16.1"
#pragma coverity compliance end_block "MISRA C-2012 Rule 16.3"
#pragma coverity compliance end_block "MISRA C-2012 Rule 16.5"
#pragma coverity compliance end_block "MISRA C-2012 Rule 16.6"
</#if>
/* MISRAC 2012 deviation block end */
static bool DRV_MEMORY_StartXfer( DRV_MEMORY_OBJECT *dObj )
{
DRV_MEMORY_CLIENT_OBJECT *clientObj = NULL;
DRV_MEMORY_BUFFER_OBJECT *bufferObj = NULL;
DRV_MEMORY_EVENT event = DRV_MEMORY_EVENT_COMMAND_ERROR;
MEMORY_DEVICE_TRANSFER_STATUS transferStatus = MEMORY_DEVICE_TRANSFER_ERROR_UNKNOWN;
bool isSuccess = false;
SYS_TIME_HANDLE handle = SYS_TIME_HANDLE_INVALID;
bufferObj = &dObj->currentBufObj;
/* Init the various sub state machines. */
dObj->readState = DRV_MEMORY_READ_INIT;
dObj->writeState = DRV_MEMORY_WRITE_INIT;
dObj->eraseState = DRV_MEMORY_ERASE_INIT;
dObj->ewState = DRV_MEMORY_EW_INIT;
bufferObj->status = DRV_MEMORY_COMMAND_IN_PROGRESS;
transferStatus = gMemoryXferFuncPtr[bufferObj->opType](dObj, &bufferObj->buffer[0], bufferObj->blockStart, bufferObj->nBlocks);
while (transferStatus == MEMORY_DEVICE_TRANSFER_BUSY)
{
if ((dObj->isMemDevInterruptEnabled == false) && (dObj->memDevStatusPollUs > 0U))
{
handle = SYS_TIME_CallbackRegisterUS(DRV_MEMORY_TimerHandler, (uintptr_t)dObj, dObj->memDevStatusPollUs, SYS_TIME_SINGLE);
if (handle == SYS_TIME_HANDLE_INVALID)
{
return false;
}
/* Wait for the request to process before checking status. This semaphore is released from the
* system timer handler
*/
if (OSAL_RESULT_SUCCESS == OSAL_SEM_Pend( &dObj->transferDone, OSAL_WAIT_FOREVER ))
{
(void) SYS_TIME_TimerDestroy(handle);
}
else
{
return false;
}
}
else if ((dObj->isMemDevInterruptEnabled == true) && (dObj->memDevStatusPollUs == 0U))
{
if (dObj->isTransferDone == false)
{
/* Wait for the request to process before checking status. This semaphore is released from the
* event handler called from attached memory device.
*/
if (OSAL_RESULT_SUCCESS != OSAL_SEM_Pend( &dObj->transferDone, OSAL_WAIT_FOREVER ))
{
return false;
}
}
}
else
{
/* Nothing to do */
}
transferStatus = gMemoryXferFuncPtr[bufferObj->opType](dObj, &bufferObj->buffer[0], bufferObj->blockStart, bufferObj->nBlocks);
}
if (transferStatus == MEMORY_DEVICE_TRANSFER_COMPLETED)
{
bufferObj->status = DRV_MEMORY_COMMAND_COMPLETED;
event = DRV_MEMORY_EVENT_COMMAND_COMPLETE;
isSuccess = true;
}
else if (transferStatus >= MEMORY_DEVICE_TRANSFER_ERROR_UNKNOWN)
{
/* The operation has failed. */
bufferObj->status = DRV_MEMORY_COMMAND_ERROR_UNKNOWN;
event = DRV_MEMORY_EVENT_COMMAND_ERROR;
}
else
{
/* Nothing to do */
}
clientObj = (DRV_MEMORY_CLIENT_OBJECT *)bufferObj->hClient;
if(clientObj->transferHandler != NULL)
{
/* Call the event handler */
clientObj->transferHandler((SYS_MEDIA_BLOCK_EVENT)event, (DRV_MEMORY_COMMAND_HANDLE)bufferObj->commandHandle, clientObj->context);
}
return isSuccess;
}
static bool DRV_MEMORY_SetupXfer
(
const DRV_HANDLE handle,
DRV_MEMORY_COMMAND_HANDLE *commandHandle,
void *buffer,
uint32_t blockStart,
uint32_t nBlock,
uint8_t geometry_type,
DRV_MEMORY_OPERATION_TYPE opType,
DRV_IO_INTENT io_intent
)
{
DRV_MEMORY_CLIENT_OBJECT *clientObj = NULL;
DRV_MEMORY_OBJECT *dObj = NULL;
bool isSuccess = false;
if (commandHandle != NULL)
{
*commandHandle = DRV_MEMORY_COMMAND_HANDLE_INVALID;
}
/* Validate the driver handle */
clientObj = DRV_MEMORY_DriverHandleValidate(handle);
if (clientObj == NULL)
{
SYS_DEBUG_MESSAGE(SYS_ERROR_INFO, "Invalid Memory driver handle.\n");
return isSuccess;
}
/* Check if the driver was opened with read intent */
if (((uint32_t)clientObj->intent & (uint32_t)io_intent) == 0U)
{
SYS_DEBUG_MESSAGE(SYS_ERROR_INFO, "Memory Driver Opened with invalid intent.\n");
return isSuccess;
}
dObj = &gDrvMemoryObj[clientObj->drvIndex];
if ((buffer == NULL) && (opType != DRV_MEMORY_OPERATION_TYPE_ERASE))
{
SYS_DEBUG_MESSAGE(SYS_ERROR_INFO, "Memory Driver Invalid Buffer.\n");
return isSuccess;
}
if ((nBlock == 0U) || ((blockStart + nBlock) > dObj->mediaGeometryTable[geometry_type].numBlocks))
{
SYS_DEBUG_MESSAGE(SYS_ERROR_INFO, "Memory Driver Invalid Block parameters.\n");
return isSuccess;
}
if (OSAL_MUTEX_Lock(&dObj->transferMutex, OSAL_WAIT_FOREVER ) == OSAL_RESULT_SUCCESS)
{
/* For Memory Device which do not support Erase */
if (dObj->memoryDevice->SectorErase == NULL)
{
if (opType == DRV_MEMORY_OPERATION_TYPE_ERASE)
{
(void) OSAL_MUTEX_Unlock(&dObj->transferMutex);
return isSuccess;
}
else if (opType == DRV_MEMORY_OPERATION_TYPE_ERASE_WRITE)
{
opType = DRV_MEMORY_OPERATION_TYPE_WRITE;
}
else
{
/* Nothing to do */
}
}
DRV_MEMORY_AllocateBufferObject (clientObj, commandHandle, buffer, blockStart, nBlock, opType);
isSuccess = DRV_MEMORY_StartXfer(dObj);
(void) OSAL_MUTEX_Unlock(&dObj->transferMutex);
}
return isSuccess;
}
// *****************************************************************************
// *****************************************************************************
// Section: MEMORY Driver System Routines
// *****************************************************************************
// *****************************************************************************
/*
* MISRA C-2012 Rule 11.1,11.3, and 11.8 deviated below. Deviation record ID -
* H3_MISRAC_2012_R_11_1_DR_1, H3_MISRAC_2012_R_11_3_DR_1 & H3_MISRAC_2012_R_11_8_DR_1
*/
<#if core.COVERITY_SUPPRESS_DEVIATION?? && core.COVERITY_SUPPRESS_DEVIATION>
#pragma coverity compliance block \
(deviate:1 "MISRA C-2012 Rule 11.1" "H3_MISRAC_2012_R_11_1_DR_1" )\
(deviate:1 "MISRA C-2012 Rule 11.3" "H3_MISRAC_2012_R_11_3_DR_1" )\
(deviate:2 "MISRA C-2012 Rule 11.8" "H3_MISRAC_2012_R_11_8_DR_1" )
</#if>
SYS_MODULE_OBJ DRV_MEMORY_Initialize
(
const SYS_MODULE_INDEX drvIndex,
const SYS_MODULE_INIT *const init
)
{
DRV_MEMORY_OBJECT *dObj = (DRV_MEMORY_OBJECT*) NULL;
DRV_MEMORY_INIT *memoryInit = NULL;
/* Validate the driver index */
if (drvIndex >= DRV_MEMORY_INSTANCES_NUMBER)
{
return SYS_MODULE_OBJ_INVALID;
}
dObj = &gDrvMemoryObj[drvIndex];
/* Check if the instance has already been initialized. */
if (dObj->inUse)
{
return SYS_MODULE_OBJ_INVALID;
}
dObj->status = SYS_STATUS_UNINITIALIZED;
dObj->isTransferDone = false;
/* Indicate that this object is in use */
dObj->inUse = true;
/* Assign to the local pointer the init data passed */
memoryInit = (DRV_MEMORY_INIT *)init;
/* Initialize number of clients */
dObj->numClients = 0;
dObj->clientObjPool = (DRV_MEMORY_CLIENT_OBJECT *)memoryInit->clientObjPool;
dObj->nClientsMax = memoryInit->nClientsMax;
dObj->bufferToken = 1;
dObj->clientToken = 1;
/* Initialize the attached memory device functions */
dObj->memoryDevice = memoryInit->memoryDevice;
dObj->memDevIndex = memoryInit->memDevIndex;
dObj->memDevHandle = DRV_HANDLE_INVALID;
dObj->isMemDevInterruptEnabled = memoryInit->isMemDevInterruptEnabled;
dObj->memDevStatusPollUs = memoryInit->memDevStatusPollUs;
/* Set the erase buffer */
dObj->ewBuffer = memoryInit->ewBuffer;
if (OSAL_MUTEX_Create(&dObj->clientMutex) == OSAL_RESULT_FAIL)
{
/* There was insufficient memory available for the mutex to be created */
return SYS_MODULE_OBJ_INVALID;
}
if (OSAL_MUTEX_Create(&dObj->transferMutex) == OSAL_RESULT_FAIL)
{
/* There was insufficient memory available for the mutex to be created */
return SYS_MODULE_OBJ_INVALID;
}
if (OSAL_RESULT_FAIL == OSAL_SEM_Create(&dObj->transferDone,OSAL_SEM_TYPE_BINARY, 0, 0))
{
/* There was insufficient memory available for the semaphore to be created */
return SYS_MODULE_OBJ_INVALID;
}
<#if DRV_MEMORY_COMMON_FS_ENABLE == true>
if (memoryInit->isFsEnabled == true)
{
DRV_MEMORY_RegisterWithSysFs(drvIndex, memoryInit->deviceMediaType);
}
</#if>
/* Set the driver state as busy as the attached memory device needs to be opened and
* queried for the geometry data. */
dObj->status = SYS_STATUS_BUSY;
/* Return the driver index */
return drvIndex;
}
SYS_STATUS DRV_MEMORY_Status
(
SYS_MODULE_OBJ object
)
{
/* Validate the object */
if ((object == SYS_MODULE_OBJ_INVALID) || (object >= DRV_MEMORY_INSTANCES_NUMBER))
{
SYS_DEBUG_MESSAGE(SYS_ERROR_INFO,"DRV_MEMORY_Status(): Invalid parameter.\n");
return SYS_STATUS_UNINITIALIZED;
}
/* Return the driver status */
return (gDrvMemoryObj[object].status);
}
static SYS_STATUS DRV_MEMORY_IsReady(DRV_MEMORY_OBJECT *dObj)
{
SYS_STATUS status = SYS_STATUS_BUSY;
if (dObj->memoryDevice->Status != NULL)
{
if (dObj->memoryDevice->Status(dObj->memDevIndex) != SYS_STATUS_READY)
{
/* Attached memory device is not ready */
return status;
}
}
if (dObj->memoryDevice->Open != NULL)
{
dObj->memDevHandle = dObj->memoryDevice->Open(dObj->memDevIndex, (DRV_IO_INTENT)((uint32_t)DRV_IO_INTENT_READWRITE | (uint32_t)DRV_IO_INTENT_EXCLUSIVE));
if (dObj->memDevHandle == DRV_HANDLE_INVALID)
{
return status;
}
}
if (true == DRV_MEMORY_UpdateGeometry(dObj))
{
status = SYS_STATUS_READY;
dObj->status = SYS_STATUS_READY;
}
return status;
}
// *****************************************************************************
// *****************************************************************************
// Section: MEMORY Driver Client Routines
// *****************************************************************************
// *****************************************************************************
DRV_HANDLE DRV_MEMORY_Open
(
const SYS_MODULE_INDEX drvIndex,
const DRV_IO_INTENT ioIntent
)
{
DRV_MEMORY_CLIENT_OBJECT *clientObj = NULL;
DRV_MEMORY_OBJECT *dObj = NULL;
uint32_t iClient;
/* Validate the driver index */
if (drvIndex >= DRV_MEMORY_INSTANCES_NUMBER)
{
SYS_DEBUG_MESSAGE(SYS_ERROR_INFO, "DRV_MEMORY_Open(): Invalid driver index.\n");
return DRV_HANDLE_INVALID;
}
dObj = &gDrvMemoryObj[drvIndex];
/* Check if the driver is ready to be opened */
if (dObj->status != SYS_STATUS_READY)
{
if (DRV_MEMORY_IsReady(dObj) != SYS_STATUS_READY)
{
SYS_DEBUG_MESSAGE(SYS_ERROR_INFO, "DRV_MEMORY_Open(): Driver is not ready.\n");
return DRV_HANDLE_INVALID;
}
}
/* Acquire the instance specific mutex to protect the instance specific
* client pool
*/
if (OSAL_MUTEX_Lock(&dObj->clientMutex , OSAL_WAIT_FOREVER ) == OSAL_RESULT_FAIL)
{
return DRV_HANDLE_INVALID;
}
/* Check if the driver has already been opened in exclusive mode */
if (dObj->isExclusive)
{
SYS_DEBUG_MESSAGE(SYS_ERROR_INFO, "DRV_MEMORY_Open(): Driver is already open in exclusive mode.\n");
(void) OSAL_MUTEX_Unlock( &dObj->clientMutex);
return DRV_HANDLE_INVALID;
}
/* Driver has already been opened and cannot be opened exclusively */
if ((dObj->numClients > 0U) && (((uint32_t)ioIntent & (uint32_t)DRV_IO_INTENT_EXCLUSIVE) != 0U))
{
SYS_DEBUG_MESSAGE(SYS_ERROR_INFO, "DRV_MEMORY_Open(): Driver is already open. Can't be opened in exclusive mode.\n");
(void) OSAL_MUTEX_Unlock( &dObj->clientMutex);
return DRV_HANDLE_INVALID;
}
/* Allocate a client object from client pool */
for(iClient = 0; iClient != dObj->nClientsMax; iClient++)
{
if (dObj->clientObjPool[iClient].inUse == false)
{
clientObj = &dObj->clientObjPool[iClient];
/* Found a client object that can be used */
clientObj->inUse = true;
clientObj->drvIndex = (uint8_t)drvIndex;
clientObj->intent = ioIntent;
clientObj->transferHandler = NULL;
if (((uint32_t)ioIntent & (uint32_t)DRV_IO_INTENT_EXCLUSIVE) != 0U)
{
/* Driver was opened in exclusive mode */
dObj->isExclusive = true;
}
dObj->numClients ++;
clientObj->clientHandle = DRV_MEMORY_MAKE_HANDLE((uint32_t)dObj->clientToken, (uint32_t)drvIndex, iClient);
dObj->clientToken = DRV_MEMORY_UPDATE_TOKEN(dObj->clientToken);
if (clientObj->clientHandle != DRV_HANDLE_INVALID)
{
if (dObj->isMemDevInterruptEnabled == true)
{
if (dObj->memoryDevice->EventHandlerSet != NULL)
{
dObj->memoryDevice->EventHandlerSet(dObj->memDevHandle, DRV_MEMORY_EventHandler, (uintptr_t)dObj);
}
}
}
break;
}
}
(void) OSAL_MUTEX_Unlock(&dObj->clientMutex);
return (clientObj != NULL) ? ((DRV_HANDLE)clientObj->clientHandle) : DRV_HANDLE_INVALID;