2 \defgroup storage_interface_gr Storage Interface
3 \brief Driver API for Storage Device Interface (%Driver_Storage.h)
5 This is an abstraction for a storage controller. It offers an interface to
6 access an address space of storage locations, comprising APIs for
7 initialization, erase, access, program, and status-fetch operations. It also
8 offers APIs to iterate over the available Storage Blocks (\ref
9 ARM_STORAGE_BLOCK), allowing the discovery of block attributes such as
10 write/erase granularities. Using the Storage abstraction, it becomes possible to
11 write generic algorithms, such as block copy, to operate on any conforming
14 \note The storage abstraction layer is not responsible for storage management.
15 Algorithms such as block-allocation, wear-leveling, erase-before-write and other
16 storage-management policies are the responsibility of modules external to the
17 storage abstraction layer. In essence, the storage interface is the lowest
18 abstraction upon which block management policies can be implemented.
20 Here's a picture to help locate the storage abstraction in the software stack.
21 The part below the box labeled 'Storage abstraction layer' is implemented by a
24 \image html storage_sw_stack.png
28 The following header files define the Application Programming Interface (API) for the Flash interface:
29 - \b %Driver_Storage.h : Driver API for Storage Device Interface
32 <b>Driver Functions</b>
34 The driver functions are published in the access struct as explained in \ref StorageDriverFunctions
35 - \ref ARM_DRIVER_STORAGE : access struct for Storage driver functions
37 A sample use for the driver can be found at: \ref SampleUseOfStorageDriver
38 *******************************************************************************************************************/
43 \addtogroup storage_interface_gr
45 *******************************************************************************************************************/
48 \struct ARM_STORAGE_BLOCK_ATTRIBUTES
50 - \ref ARM_STORAGE_BLOCK
51 *******************************************************************************************************************/
54 \struct ARM_STORAGE_BLOCK
55 \details Storage blocks combine to make up the address map of a storage controller.
56 *******************************************************************************************************************/
59 \struct ARM_STORAGE_INFO
61 It describes the characteristics of a Storage device. This includes total
62 storage, programming size, a default value for erased memory etc. This
63 information can be obtained from the Storage device datasheet and is used by the
64 middleware in order to properly interact with the Storage device.
66 Total available storage (in bytes) is contained in \em total_storage. Minimum
67 programming size (in bytes) is described by \em program_unit (applicable only if
68 the \em programmable attribute is set for a block). It defines the granularity
69 for programming data. The offset of the start of a program-range and the size
70 should also be aligned with \em program_unit.
71 \note: setting \em program_unit to 0 has the effect of disabling the size and
72 alignment restrictions (setting it to 1 also has the same effect).
74 Optimal programming page-size (in bytes) is specified by \em
75 optimal_program_unit. Some storage controllers have internal buffers into which
76 to receive data. Writing in chunks of \em optimal_program_unit would achieve
77 maximum programming speed. Like with \em program_unit, this is applicable only
78 if the \em programmable attribute is set for the underlying storage block(s).
80 \em program_cycles is a measure of endurance for reprogramming.
81 A value of \em ARM_STORAGE_PROGRAM_CYCLES_INFINITE may be used to signify
82 infinite or unknown endurance.
84 Contents of erased memory is specified by the \em erased_value. It is usually
85 \token{1} to indicate erased bytes with state 0xFF.
87 \em memory_mapped can be set to \token{1} to indicate that the storage device
88 has a mapping onto the processor's memory address space.
89 \note: For a memory-mapped block which isn't erasable but is programmable,
90 writes should be possible directly to the memory-mapped storage without going
91 through the \ref ARM_Storage_ProgramData operation.
93 The field \em programmability holds a value to indicate storage programmability.
94 Similarly, \em retention_level holds a for encoding data-retention levels for
98 These fields serve a different purpose than the ones contained in
99 \ref ARM_STORAGE_CAPABILITIES, which is another structure containing device-level
100 metadata. ARM_STORAGE_CAPABILITIES describes the API capabilities, whereas
101 ARM_STORAGE_INFO describes the device. Furthermore ARM_STORAGE_CAPABILITIES fits
102 within a single word, and is designed to be passed around by value;
103 ARM_STORAGE_INFO, on the other hand, contains metadata which doesn't fit into a
104 single word and requires the use of pointers to be moved around.
107 - \ref ARM_Storage_GetInfo
108 *******************************************************************************************************************/
111 \struct ARM_DRIVER_STORAGE
113 This is the set of operations constituting the Storage driver. Their
114 implementation is platform-specific, and needs to be supplied by the porting
115 effort. The functions of the Storage driver are accessed by function pointers
116 exposed by this structure. Refer to \ref StorageDriverFunctions for overview
119 Each instance of a Storage interface provides such an access structure.
120 The instance is identified by a postfix number in the symbol name of the access structure, for example:
121 - \b Driver_Storage0 is the name of the access struct of the first instance (no. 0).
122 - \b Driver_Storage1 is the name of the access struct of the second instance (no. 1).
124 A middleware configuration setting allows connecting the middleware to a specific driver instance \b %Driver_Flash<i>n</i>.
125 The default is \token{0}, which connects a middleware to the first instance of a driver.
126 *******************************************************************************************************************/
129 \defgroup StorageDriverFunctions Use of Storage APIs
131 Function pointers within \ref ARM_DRIVER_STORAGE form the set of operations
132 constituting the Storage driver. Their implementation is platform-specific, and
133 needs to be supplied by the porting effort.
135 Some of these APIs will always operate synchronously:
136 - \ref ARM_Storage_GetVersion
137 - \ref ARM_Storage_GetCapabilities
138 - \ref ARM_Storage_GetStatus
139 - \ref ARM_Storage_GetInfo
140 - \ref ARM_Storage_ResolveAddress
141 - \ref ARM_Storage_GetNextBlock and
142 - \ref ARM_Storage_GetBlock.
144 This means that control returns to the caller with a relevant status code only after the completion of the operation (or
145 the discovery of a failure condition).
147 The remainder of the APIs:
148 - \ref ARM_Storage_Initialize
149 - \ref ARM_Storage_Uninitialize
150 - \ref ARM_Storage_PowerControl
151 - \ref ARM_Storage_ReadData
152 - \ref ARM_Storage_ProgramData
153 - \ref ARM_Storage_Erase and
154 - \ref ARM_Storage_EraseAll
156 can function asynchronously if the underlying controller supports it; that is if ARM_STORAGE_CAPABILITIES::asynchronous_ops
157 is set. In the case of asynchronous operation, the invocation returns early (with ARM_DRIVER_OK) and results in a completion
158 callback later. If ARM_STORAGE_CAPABILITIES::asynchronous_ops is not set, then all such APIs execute synchronously, and
159 control returns to the caller with a status code only after the completion of the operation (or the discovery of a failure
162 If ARM_STORAGE_CAPABILITIES::asynchronous_ops is set, a storage driver may
163 still choose to execute asynchronous operations in a synchronous manner. If
164 so, the driver returns a positive value to indicate successful synchronous
165 completion (or an error code in case of failure) and no further invocation of
166 completion callback should be expected. The expected return value for
167 synchronous completion of such asynchronous operations varies depending on
168 the operation. For operations involving data access, it often equals the
169 amount of data transferred or affected. For non data-transfer operations,
170 such as EraseAll or Initialize, it is usually 1.
172 Here's a code snippet to suggest how asynchronous APIs might be used by
173 callers to handle both synchronous and asynchronous execution by the
174 underlying storage driver:
176 ASSERT(ARM_DRIVER_OK == 0); // this is a precondition; it doesn't need to be put in code
178 int32_t returnValue = drv->asynchronousAPI(...);
180 if (returnValue < ARM_DRIVER_OK) {
183 } else if (returnValue == ARM_DRIVER_OK) {
184 ASSERT(drv->GetCapabilities().asynchronous_ops == 1);
185 // handle early return from asynchronous execution; remainder of the work is done in the callback handler.
188 ASSERT(returnValue == EXPECTED_RETURN_VALUE_FOR_SYNCHRONOUS_COMPLETION);
189 // handle synchronous completion.
193 This example is mixing synchronous and asynchronous APIs: \ref SampleUseOfStorageDriver
194 *******************************************************************************************************************/
197 \struct ARM_STORAGE_CAPABILITIES
199 A Storage driver can be implemented with different capabilities. The data fields
200 of this struct encode the API capabilities implemented by this driver.
202 The element \em asynchronous_ops indicates if APIs like initialize, read, erase,
203 program, etc. can operate in asynchronous mode. Having this bit set to 1 means
204 that the driver is capable of launching asynchronous operations; command
205 completion for asynchronous operations is signaled by the invocation of a
206 completion callback. If set to 1, drivers may still complete asynchronous
207 operations synchronously as necessary--in which case they return a positive
208 error code to indicate synchronous completion. If \em asynchronous_ops is not
209 set, then all such APIs execute synchronously, and control returns to the caller
210 with a status code only after the completion of the operation (or the discovery
211 of a failure condition).
213 The element \em erase_all specifies that the \ref ARM_Storage_EraseAll function
214 is supported. Typically full chip erase is much faster than erasing the whole
215 device using \em ARM_Storage_Erase.
218 - \ref ARM_Storage_GetCapabilities
221 This data structure is designed to fit within a single word so that it can be
222 fetched cheaply using a call to driver->GetCapabilities().
223 *******************************************************************************************************************/
226 \struct ARM_STORAGE_STATUS
228 Structure with information about the status of the Storage device.
230 The flag \em busy indicates that the driver is busy executing read/program/erase operation.
232 The flag \em error flag is cleared on start of read/program/erase operation and is set at the end of the current operation in case of error.
235 - \ref ARM_Storage_GetStatus
236 *****************************************************************************************************************/
239 \typedef ARM_STORAGE_OPERATION
241 Command opcodes for the Storage interface. Completion callbacks use these codes
242 to refer to completing commands. Refer to \ref ARM_Storage_Callback_t.
243 *****************************************************************************************************************/
246 \typedef ARM_Storage_Callback_t
248 Provides the typedef for the callback function \ref ARM_Storage_Callback_t.
251 A code to indicate the status of the completed operation. For data
252 transfer operations, the status field is overloaded in case of
253 success to return the count of bytes successfully transferred; this
254 can be done safely because error codes are negative values.
256 \param [in] operation
257 The command op-code. This value isn't essential, but it is expected that
258 this information could be a quick and useful filter for the handler.
260 <b>Parameter for:</b>
261 - \ref ARM_Storage_Initialize
262 *******************************************************************************************************************/
269 ARM_DRIVER_VERSION ARM_Storage_GetVersion (void) {
273 \fn ARM_DRIVER_VERSION ARM_Storage_GetVersion (void)
275 The function \b ARM_Storage_GetVersion returns version information of the driver implementation in \ref ARM_DRIVER_VERSION.
276 - API version is the version of the CMSIS-Driver specification used to implement this driver.
277 - Driver version is source code version of the actual driver implementation.
281 extern ARM_DRIVER_STORAGE *drv_info;
283 void read_version (void) {
284 ARM_DRIVER_VERSION version;
286 version = drv_info->GetVersion ();
287 if (version.api < 0x10A) { // requires at minimum API version 1.10 or higher
294 \note HAPI returns synchronously--it does not result in an invocation
295 of a completion callback.
297 \note The function GetVersion() can be called any time to obtain the
298 required information from the driver (even before initialization). It
299 always returns the same information.
300 *******************************************************************************************************************/
302 ARM_STOR_CAPABILITIES ARM_Storage_GetCapabilities (void) {
306 \fn ARM_STORAGE_CAPABILITIES ARM_Storage_GetCapabilities (void)
309 The function \b ARM_Storage_GetCapabilities returns information about
310 capabilities in this driver implementation. The data fields of the struct
311 ARM_STORAGE_CAPABILITIES encode various capabilities, for example if the device
312 is able to execute operations asynchronously.
316 extern ARM_DRIVER_STORAGE *drv_info;
318 void read_capabilities (void) {
319 ARM_STORAGE_CAPABILITIES drv_capabilities;
321 drv_capabilities = drv_info->GetCapabilities ();
322 // interrogate capabilities
327 \note This API returns synchronously--it does not result in an invocation
328 of a completion callback.
330 \note The function GetCapabilities() can be called any time to obtain the
331 required information from the driver (even before initialization). It
332 always returns the same information.
333 *******************************************************************************************************************/
335 int32_t ARM_Storage_Initialize (ARM_Storage_Callback_t callback) {
339 \fn int32_t ARM_Storage_Initialize (ARM_Storage_Callback_t callback)
341 The function \b ARM_Storage_Initialize is called when the middleware component starts
342 operation. In addition to bringing the controller to a ready state,
343 Initialize() receives a callback handler to be invoked upon completion of
344 asynchronous operations.
346 ARM_Storage_Initialize() needs to be called explicitly before
347 powering the peripheral using ARM_Storage_PowerControl(), and before initiating other
348 accesses to the storage controller.
350 The function performs the following operations:
351 - Initializes the resources needed for the Storage interface.
352 - Registers the \ref ARM_Storage_Callback_t callback function.
354 To start working with a peripheral the functions ARM_Storage_Initialize and ARM_Storage_PowerControl() need to be called in this order:
356 drv->Initialize (...); // Allocate I/O pins
357 drv->PowerControl (ARM_POWER_FULL); // Power up peripheral, setup IRQ/DMA
360 - ARM_Storage_Initialize() typically allocates the I/O resources (pins) for the
361 peripheral. The function can be called multiple times; if the I/O resources
362 are already initialized it performs no operation and just returns with
365 - ARM_Storage_PowerControl (ARM_POWER_FULL) sets the peripheral registers including
366 interrupt (NVIC) and optionally DMA. The function can be called multiple
367 times; if the registers are already set it performs no operation and just
368 returns with ARM_DRIVER_OK.
370 To stop working with a peripheral the functions ARM_Storage_PowerControl() and ARM_Storage_Uninitialize() need to be called in this order:
372 drv->PowerControl (ARM_POWER_OFF); // Terminate any pending transfers, reset IRQ/DMA, power off peripheral
373 drv->Uninitialize (...); // Release I/O pins
376 The functions ARM_Storage_PowerControl() and ARM_Storage_Uninitialize() always execute and can be used
377 to put the peripheral into a Safe State, for example after any data
378 transmission errors. To restart the peripheral in an error condition,
379 you should first execute the Stop Sequence and then the Start Sequence.
381 \note This API may execute asynchronously if
382 ARM_STORAGE_CAPABILITIES::asynchronous_ops is set. Asynchronous
383 execution is optional even if 'asynchronous_ops' is set.
384 *******************************************************************************************************************/
386 int32_t ARM_Storage_Uninitialize (void) {
390 \fn int32_t ARM_Storage_Uninitialize (void)
392 It is called when the middleware component stops operation, and wishes to
393 release the software resources used by the interface.
395 \note This API may execute asynchronously if
396 ARM_STORAGE_CAPABILITIES::asynchronous_ops is set. Asynchronous
397 execution is optional even if 'asynchronous_ops' is set.
398 *******************************************************************************************************************/
400 int32_t ARM_Storage_PowerControl (ARM_POWER_STATE state) {
404 \fn int32_t ARM_Storage_PowerControl (ARM_POWER_STATE state)
406 The function \b ARM_Storage_PowerControl operates the power modes of the Storage interface.
408 To start working with a peripheral the functions Initialize and PowerControl need to be called in this order:
410 drv->Initialize (...); // Allocate I/O pins
411 drv->PowerControl (ARM_POWER_FULL); // Power up peripheral, setup IRQ/DMA
414 - ARM_Storage_Initialize() typically allocates the I/O resources (pins) for the
415 peripheral. The function can be called multiple times; if the I/O resources
416 are already initialized it performs no operation and just returns with
419 - PowerControl (ARM_POWER_FULL) sets the peripheral registers including
420 interrupt (NVIC) and optionally DMA. The function can be called multiple
421 times; if the registers are already set it performs no operation and just
422 returns with ARM_DRIVER_OK.
424 To stop working with a peripheral the functions PowerControl and Uninitialize need to be called in this order:
426 drv->PowerControl (ARM_POWER_OFF); // Terminate any pending transfers, reset IRQ/DMA, power off peripheral
427 drv->Uninitialize (...); // Release I/O pins
430 The functions ARM_Storage_PowerControl and ARM_Storage_Uninitialize always execute and can be used
431 to put the peripheral into a Safe State, for example after any data
432 transmission errors. To restart the peripheral in an error condition,
433 you should first execute the Stop Sequence and then the Start Sequence.
435 The parameter \em state can have the following values:
436 - \ref ARM_POWER_FULL : set-up the Storage device for data transfers, enable interrupts (NVIC) and optionally DMA. Can be called multiple times.
437 If the device is already in this mode, then the function performs no operation and returns with \ref ARM_DRIVER_OK.
438 - \ref ARM_POWER_LOW : may use power saving. Returns \ref ARM_DRIVER_ERROR_UNSUPPORTED when not implemented.
439 - \ref ARM_POWER_OFF : terminates any pending data transfers, disables peripheral, disables related interrupts and DMA.
441 \note This API may execute asynchronously if
442 ARM_STORAGE_CAPABILITIES::asynchronous_ops is set. Asynchronous
443 execution is optional even if 'asynchronous_ops' is set.
444 *******************************************************************************************************************/
446 int32_t ARM_Storage_ReadData (uint64_t addr, void *data, uint32_t size) {
450 \fn int32_t ARM_Storage_ReadData (uint64_t addr, void *data, uint32_t size)
452 Read the contents of a range of storage memory into a buffer
453 supplied by the caller. The buffer is owned by the caller and should
454 remain accessible for the lifetime of this command.
456 \note This API may execute asynchronously if
457 ARM_STORAGE_CAPABILITIES::asynchronous_ops is set. Asynchronous
458 execution is optional even if 'asynchronous_ops' is set.
459 *******************************************************************************************************************/
461 int32_t ARM_Storage_ProgramData (uint64_t addr, const void *data, uint32_t size) {
465 \fn int32_t ARM_Storage_ProgramData (uint64_t addr, const void *data, uint32_t size)
467 Write the contents of a given memory buffer into a range of
468 storage memory. In the case of flash memory, the destination range in
469 storage memory typically has its contents in an erased state from a
470 preceding erase operation. The source memory buffer is owned by the
471 caller and should remain accessible for the lifetime of this command.
473 \note It is best for the middleware to write in units of
474 'optimal_program_unit' (\ref ARM_STORAGE_INFO) of the device.
476 \note This API may execute asynchronously if
477 ARM_STORAGE_CAPABILITIES::asynchronous_ops is set. Asynchronous
478 execution is optional even if 'asynchronous_ops' is set.
479 *******************************************************************************************************************/
481 int32_t ARM_Storage_Erase (uint64_t addr, uint32_t size) {
485 \fn int32_t ARM_Storage_Erase (uint64_t addr, uint32_t size)
488 This function erases a range of storage specified by [addr, addr +
489 size). Both 'addr' and 'addr + size' should align with the
490 'erase_unit'(s) of the respective owning storage block(s) (see \ref
491 ARM_STORAGE_BLOCK and \ref ARM_STORAGE_BLOCK_ATTRIBUTES). The range to
492 be erased will have its contents returned to the un-programmed state--
493 i.e. to \ref ARM_STORAGE_INFO::erased_value, which
494 is usually 1 to indicate the pattern of all ones: 0xFF.
496 \note This API may execute asynchronously if
497 ARM_STORAGE_CAPABILITIES::asynchronous_ops is set. Asynchronous
498 execution is optional even if 'asynchronous_ops' is set.
500 \note Erase() may return a smaller (positive) value than the size of the
501 requested range. The returned value indicates the actual number of bytes
502 erased. It is the caller's responsibility to follow up with an appropriate
503 request to complete the operation.
505 \note in the case of a failed erase (except when
506 ARM_DRIVER_ERROR_PARAMETER, ARM_STORAGE_ERROR_PROTECTED, or
507 ARM_STORAGE_ERROR_NOT_ERASABLE is returned synchronously), the
508 requested range should be assumed to be in an unknown state. The
509 previous contents may not be retained.
510 *******************************************************************************************************************/
512 int32_t ARM_Storage_EraseAll (void) {
516 \fn int32_t ARM_Storage_EraseAll (void)
518 This optional function erases the complete device. If the device does not
519 support global erase then the function returns the error value \ref
520 ARM_DRIVER_ERROR_UNSUPPORTED. The data field \em 'erase_all' =
521 \token{1} of the structure \ref ARM_STORAGE_CAPABILITIES encodes that
522 \ref ARM_Storage_EraseAll is supported.
524 \note This API may execute asynchronously if
525 ARM_STORAGE_CAPABILITIES::asynchronous_ops is set. Asynchronous
526 execution is optional even if 'asynchronous_ops' is set.
527 *******************************************************************************************************************/
529 ARM_Storage_STATUS ARM_Storage_GetStatus (void) {
533 \fn ARM_STORAGE_STATUS ARM_Storage_GetStatus (void)
535 Get the status of the current (or previous) command executed by the
536 storage controller; stored in the structure \ref ARM_STORAGE_STATUS.
538 \note This API returns synchronously--it does not result in an invocation
539 of a completion callback.
540 *******************************************************************************************************************/
542 int32_t ARM_Storage_GetInfo (ARM_STORAGE_INFO *info) {
546 \fn int32_t ARM_Storage_GetInfo (ARM_STORAGE_INFO *info)
548 Get information about the Storage device; stored in the structure \ref ARM_STORAGE_INFO.
550 \note It is the caller's responsibility to ensure that the buffer passed in
551 is able to be initialized with a \ref ARM_STORAGE_INFO.
553 \note This API returns synchronously--it does not result in an invocation
554 of a completion callback.
555 *******************************************************************************************************************/
557 uint32_t ARM_Storage_ResolveAddress(uint64_t addr) {
561 \fn uint32_t ARM_Storage_ResolveAddress(uint64_t addr)
563 Only applicable to devices with memory-mapped storage.
565 \note This API returns synchronously. The invocation should return quickly,
566 and result in a resolved address.
567 *******************************************************************************************************************/
569 int32_t ARM_Storage_GetNextBlock(const ARM_STORAGE_BLOCK* prev_block, ARM_STORAGE_BLOCK *next_block) {
573 \fn int32_t ARM_Storage_GetNextBlock(const ARM_STORAGE_BLOCK* prev_block, ARM_STORAGE_BLOCK *next_block);
575 This helper function fetches (an iterator to) the next block (or
576 the first block if 'prev_block' is passed in as NULL). In the failure
577 case, a terminating, invalid block iterator is filled into the out
578 parameter: 'next_block'. In combination with \ref
579 ARM_STORAGE_VALID_BLOCK, it can be used to iterate over the sequence
580 of blocks within the storage map:
583 ARM_STORAGE_BLOCK block;
584 for (drv->GetNextBlock(NULL, &block); ARM_STORAGE_VALID_BLOCK(&block); drv->GetNextBlock(&block, &block)) {
589 \note This API returns synchronously--it does not result in an invocation
590 of a completion callback.
591 *******************************************************************************************************************/
593 int32_t ARM_Storage_GetBlock(uint64_t addr, ARM_STORAGE_BLOCK *block) {
597 \fn int32_t ARM_Storage_GetBlock(uint64_t addr, ARM_STORAGE_BLOCK *block);
598 \note This API returns synchronously--it does not result in an invocation
599 of a completion callback.
600 *******************************************************************************************************************/
607 \defgroup SampleUseOfStorageDriver Sample Use of Storage Driver
608 \ingroup storage_interface_gr
612 The following is a generic algorithm to erase
613 and program one \ref ARM_STORAGE_BLOCK_ATTRIBUTES::erase_unit worth of storage
614 and then read it back to be verified. It handles both synchronous and
615 asynchronous driver implementations.
618 // Copyright (c) 2006-2016, Arm Limited, All Rights Reserved
619 // SPDX-License-Identifier: Apache-2.0
621 // Licensed under the Apache License, Version 2.0 (the "License"); you may
622 // not use this file except in compliance with the License.
623 // You may obtain a copy of the License at
625 // https://www.apache.org/licenses/LICENSE-2.0
627 // Unless required by applicable law or agreed to in writing, software
628 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
629 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
630 // See the License for the specific language governing permissions and
631 // limitations under the License.
633 #include "Driver_Storage.h"
637 #define TEST_ASSERT(Expr) if (!(Expr)) { printf("%s:%u: assertion failure\n", __FUNCTION__, __LINE__); while (1) ;}
638 #define TEST_ASSERT_EQUAL(expected, actual) if ((expected) != (actual)) {printf("%s:%u: assertion failure\n", __FUNCTION__, __LINE__); while (1) ;}
639 #define TEST_ASSERT_NOT_EQUAL(expected, actual) if ((expected) == (actual)) {printf("%s:%u: assertion failure\n", __FUNCTION__, __LINE__); while (1) ;}
641 // forward declarations
642 void callbackHandler(int32_t status, ARM_STORAGE_OPERATION operation);
643 void progressStateMachine(void);
646 NEEDS_INITIALIZATION,
650 NEEDS_VERIFICATION_FOLLOWING_READ,
654 extern ARM_DRIVER_STORAGE ARM_Driver_Storage_(0);
655 ARM_DRIVER_STORAGE *drv = &ARM_Driver_Storage_(0);
657 static const unsigned BUFFER_SIZE = 16384;
658 static uint8_t buffer[BUFFER_SIZE];
660 void main(int argc __unused, char** argv __unused)
662 state = NEEDS_INITIALIZATION;
664 progressStateMachine();
666 // WFE(); // optional low-power sleep
670 void progressStateMachine(void)
674 static ARM_STORAGE_BLOCK firstBlock;
675 if (!ARM_STORAGE_VALID_BLOCK(&firstBlock)) {
676 // Get the first block. This block is entered only once.
677 rc = drv->GetNextBlock(NULL, &firstBlock); // get first block
678 TEST_ASSERT_EQUAL(ARM_DRIVER_OK, rc);
680 TEST_ASSERT(ARM_STORAGE_VALID_BLOCK(&firstBlock));
681 TEST_ASSERT(firstBlock.size > 0);
684 case NEEDS_INITIALIZATION:
685 rc = drv->Initialize(callbackHandler);
686 TEST_ASSERT(rc >= ARM_DRIVER_OK);
687 if (rc == ARM_DRIVER_OK) {
688 TEST_ASSERT_EQUAL(1, drv->GetCapabilities().asynchronous_ops);
690 return; // there is pending asynchronous activity which will lead to a completion callback later.
692 TEST_ASSERT_EQUAL(1, rc); // synchronous completion
694 // intentional fall-through
697 TEST_ASSERT(firstBlock.attributes.erase_unit > 0);
698 rc = drv->Erase(firstBlock.addr, firstBlock.attributes.erase_unit);
699 TEST_ASSERT(rc >= ARM_DRIVER_OK);
700 if (rc == ARM_DRIVER_OK) {
701 TEST_ASSERT_EQUAL(1, drv->GetCapabilities().asynchronous_ops);
702 state = NEEDS_PROGRAMMING;
703 return; // there is pending asynchronous activity which will lead to a completion callback later.
705 TEST_ASSERT_EQUAL(firstBlock.attributes.erase_unit, (uint32_t)rc); // synchronous completion
707 // intentional fall-through
709 case NEEDS_PROGRAMMING:
710 TEST_ASSERT(BUFFER_SIZE >= firstBlock.attributes.erase_unit);
712 memset(buffer, PATTERN, firstBlock.attributes.erase_unit);
713 rc = drv->ProgramData(firstBlock.addr, buffer, firstBlock.attributes.erase_unit);
714 TEST_ASSERT(rc >= ARM_DRIVER_OK);
715 if (rc == ARM_DRIVER_OK) {
716 TEST_ASSERT_EQUAL(1, drv->GetCapabilities().asynchronous_ops);
718 return; // there is pending asynchronous activity which will lead to a completion callback later.
720 TEST_ASSERT_EQUAL(firstBlock.attributes.erase_unit, (uint32_t)rc); // synchronous completion
722 // intentional fall-through
725 rc = drv->ReadData(firstBlock.addr, buffer, firstBlock.attributes.erase_unit);
726 TEST_ASSERT(rc >= ARM_DRIVER_OK);
727 if (rc == ARM_DRIVER_OK) {
728 TEST_ASSERT_EQUAL(1, drv->GetCapabilities().asynchronous_ops);
729 state = NEEDS_VERIFICATION_FOLLOWING_READ;
730 return; // there is pending asynchronous activity which will lead to a completion callback later.
732 TEST_ASSERT_EQUAL(firstBlock.attributes.erase_unit, (uint32_t)rc);
734 // intentional fall-through
736 case NEEDS_VERIFICATION_FOLLOWING_READ:
737 printf("verifying data\r\n");
738 for (unsigned i = 0; i < firstBlock.attributes.erase_unit; i++) {
739 TEST_ASSERT_EQUAL(PATTERN, buffer[i]);
750 void callbackHandler(int32_t status, ARM_STORAGE_OPERATION operation)
755 case ARM_STORAGE_OPERATION_INITIALIZE:
756 case ARM_STORAGE_OPERATION_READ_DATA:
757 case ARM_STORAGE_OPERATION_PROGRAM_DATA:
758 case ARM_STORAGE_OPERATION_ERASE:
759 progressStateMachine();
763 printf("callbackHandler: unexpected callback for opcode %u with status %ld\r\n", operation, status);
769 *******************************************************************************************************************/
770 // End Storage Interface