Skip to content

Commit 14a14a0

Browse files
author
Rohit Grover
committed
make compile-time decision about async. vs. sync. operation
1 parent a2b6836 commit 14a14a0

File tree

1 file changed

+65
-63
lines changed

1 file changed

+65
-63
lines changed

hal/targets/hal/TARGET_Freescale/TARGET_KSDK2_MCUS/TARGET_K64F/storage_driver.c

Lines changed: 65 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,13 @@ static const ARM_DRIVER_VERSION version = {
204204
.drv = ARM_DRIVER_VERSION_MAJOR_MINOR(1,00)
205205
};
206206

207+
208+
#if (!defined(CONFIG_HARDWARE_MTD_ASYNC_OPS) || CONFIG_HARDWARE_MTD_ASYNC_OPS)
209+
#define ASYNC_OPS 1
210+
#else
211+
#define ASYNC_OPS 0
212+
#endif
213+
207214
static const ARM_STORAGE_CAPABILITIES caps = {
208215
/**< Signal Flash Ready event. In other words, can APIs like initialize,
209216
* read, erase, program, etc. operate in asynchronous mode?
@@ -214,11 +221,7 @@ static const ARM_STORAGE_CAPABILITIES caps = {
214221
* 1, drivers may still complete asynchronous operations synchronously as
215222
* necessary--in which case they return a positive error code to indicate
216223
* synchronous completion. */
217-
#ifndef CONFIG_HARDWARE_MTD_ASYNC_OPS
218-
.asynchronous_ops = 1,
219-
#else
220-
.asynchronous_ops = CONFIG_HARDWARE_MTD_ASYNC_OPS,
221-
#endif
224+
.asynchronous_ops = ASYNC_OPS,
222225

223226
/* Enable chip-erase functionality if we own all of block-1. */
224227
#if ((!defined (CONFIG_HARDWARE_MTD_START_ADDR) || (CONFIG_HARDWARE_MTD_START_ADDR == BLOCK1_START_ADDR)) && \
@@ -271,7 +274,6 @@ enum FlashCommandOps {
271274
SETRAM = (uint8_t)0x81, /* Set FlexRAM. (unused for now) */
272275
};
273276

274-
275277
/**
276278
* Read out the CCIF (Command Complete Interrupt Flag) to ensure all previous
277279
* operations have completed.
@@ -394,11 +396,6 @@ static inline bool commandCompletionInterruptEnabled(void)
394396
#endif
395397
}
396398

397-
static inline bool asyncOperationsEnabled(void)
398-
{
399-
return caps.asynchronous_ops;
400-
}
401-
402399
/**
403400
* Once all relevant command parameters have been loaded, the user launches the
404401
* command by clearing the FSTAT[CCIF] bit by writing a '1' to it. The CCIF flag
@@ -556,66 +553,68 @@ static int32_t executeCommand(struct mtd_k64f_data *context)
556553
* parameter checks and protection checks, if applicable, which are unique
557554
* to each command. */
558555

559-
if (asyncOperationsEnabled()) {
560-
/* Asynchronous operation */
556+
#if ASYNC_OPS
557+
/* Asynchronous operation */
558+
559+
(void)context; /* avoid compiler warning about un-used variables */
560+
561+
/* Spin waiting for the command execution to begin. */
562+
while (!controllerCurrentlyBusy() && !failedWithAccessError() && !failedWithProtectionError());
563+
if (failedWithAccessError() || failedWithProtectionError()) {
564+
clearErrorStatusBits();
565+
return ARM_DRIVER_ERROR_PARAMETER;
566+
}
567+
568+
enableCommandCompletionInterrupt();
561569

562-
/* Spin waiting for the command execution to begin. */
563-
while (!controllerCurrentlyBusy() && !failedWithAccessError() && !failedWithProtectionError());
570+
return ARM_DRIVER_OK; /* signal asynchronous completion. An interrupt will signal completion later. */
571+
#else /* #if ASYNC_OPS */
572+
/* Synchronous operation. */
573+
574+
while (1) {
575+
/* Spin waiting for the command execution to complete. */
576+
while (controllerCurrentlyBusy());
577+
578+
/* Execution may result in failure. Check for errors */
564579
if (failedWithAccessError() || failedWithProtectionError()) {
565580
clearErrorStatusBits();
566581
return ARM_DRIVER_ERROR_PARAMETER;
567582
}
583+
if (failedWithRunTimeError()) {
584+
return ARM_DRIVER_ERROR; /* unspecified runtime error. */
585+
}
568586

569-
enableCommandCompletionInterrupt();
570-
571-
return ARM_DRIVER_OK; /* signal asynchronous completion. An interrupt will signal completion later. */
572-
} else {
573-
/* Synchronous operation. */
587+
/* signal synchronous completion. */
588+
switch (context->currentCommand) {
589+
case ARM_STORAGE_OPERATION_PROGRAM_DATA:
590+
if (context->amountLeftToOperate == 0) {
591+
return context->sizeofCurrentOperation;
592+
}
574593

575-
while (1) {
594+
/* start the successive program operation */
595+
setupNextProgramData(context);
596+
launchCommand();
597+
/* continue on to the next iteration of the parent loop */
598+
break;
576599

577-
/* Spin waiting for the command execution to complete. */
578-
while (controllerCurrentlyBusy());
600+
case ARM_STORAGE_OPERATION_ERASE:
601+
if (context->amountLeftToOperate == 0) {
602+
return context->sizeofCurrentOperation;
603+
}
579604

580-
/* Execution may result in failure. Check for errors */
581-
if (failedWithAccessError() || failedWithProtectionError()) {
582-
clearErrorStatusBits();
583-
return ARM_DRIVER_ERROR_PARAMETER;
584-
}
585-
if (failedWithRunTimeError()) {
586-
return ARM_DRIVER_ERROR; /* unspecified runtime error. */
587-
}
605+
setupNextErase(context); /* start the successive erase operation */
606+
launchCommand();
607+
/* continue on to the next iteration of the parent loop */
608+
break;
588609

589-
/* signal synchronous completion. */
590-
switch (context->currentCommand) {
591-
case ARM_STORAGE_OPERATION_PROGRAM_DATA:
592-
if (context->amountLeftToOperate == 0) {
593-
return context->sizeofCurrentOperation;
594-
}
595-
596-
/* start the successive program operation */
597-
setupNextProgramData(context);
598-
launchCommand();
599-
/* continue on to the next iteration of the parent loop */
600-
break;
601-
602-
case ARM_STORAGE_OPERATION_ERASE:
603-
if (context->amountLeftToOperate == 0) {
604-
return context->sizeofCurrentOperation;
605-
}
606-
607-
setupNextErase(context); /* start the successive erase operation */
608-
launchCommand();
609-
/* continue on to the next iteration of the parent loop */
610-
break;
611-
612-
default:
613-
return 1;
614-
}
610+
default:
611+
return 1;
615612
}
616613
}
614+
#endif /* #ifdef ASYNC_OPS */
617615
}
618616

617+
#if ASYNC_OPS
619618
static void ftfe_ccie_irq_handler(void)
620619
{
621620
disbleCommandCompletionInterrupt();
@@ -691,6 +690,7 @@ static void ftfe_ccie_irq_handler(void)
691690
break;
692691
}
693692
}
693+
#endif /* #if ASYNC_OPS */
694694

695695
/**
696696
* This is a helper function which can be used to do arbitrary sanity checking
@@ -772,11 +772,11 @@ static int32_t initialize(ARM_Storage_Callback_t callback)
772772
context->commandCompletionCallback = callback;
773773

774774
/* Enable the command-completion interrupt. */
775-
if (asyncOperationsEnabled()) {
776-
NVIC_SetVector(FTFE_IRQn, (uint32_t)ftfe_ccie_irq_handler);
777-
NVIC_ClearPendingIRQ(FTFE_IRQn);
778-
NVIC_EnableIRQ(FTFE_IRQn);
779-
}
775+
#if ASYNC_OPS
776+
NVIC_SetVector(FTFE_IRQn, (uint32_t)ftfe_ccie_irq_handler);
777+
NVIC_ClearPendingIRQ(FTFE_IRQn);
778+
NVIC_EnableIRQ(FTFE_IRQn);
779+
#endif
780780

781781
context->initialized = true;
782782

@@ -792,11 +792,13 @@ static int32_t uninitialize(void) {
792792
}
793793

794794
/* Disable the command-completion interrupt. */
795-
if (asyncOperationsEnabled() && commandCompletionInterruptEnabled()) {
795+
#if ASYNC_OPS
796+
if (commandCompletionInterruptEnabled()) {
796797
disbleCommandCompletionInterrupt();
797798
NVIC_DisableIRQ(FTFE_IRQn);
798799
NVIC_ClearPendingIRQ(FTFE_IRQn);
799800
}
801+
#endif
800802

801803
context->commandCompletionCallback = NULL;
802804
context->initialized = false;

0 commit comments

Comments
 (0)