Skip to content

Commit 91becec

Browse files
authored
Merge pull request #4470 from c1728p9/workshop_rebase_4064
Flash API support using the MCUXpresso drivers
2 parents f56d64f + b3b0d21 commit 91becec

File tree

5 files changed

+147
-182
lines changed

5 files changed

+147
-182
lines changed

targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K24F/flash_api.c

Lines changed: 0 additions & 87 deletions
This file was deleted.

targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/TARGET_MCU_K64F/flash_api.c

Lines changed: 0 additions & 91 deletions
This file was deleted.
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/*
2+
* Copyright (c) 2016, Freescale Semiconductor, Inc.
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without modification,
6+
* are permitted provided that the following conditions are met:
7+
*
8+
* o Redistributions of source code must retain the above copyright notice, this list
9+
* of conditions and the following disclaimer.
10+
*
11+
* o Redistributions in binary form must reproduce the above copyright notice, this
12+
* list of conditions and the following disclaimer in the documentation and/or
13+
* other materials provided with the distribution.
14+
*
15+
* o Neither the name of Freescale Semiconductor, Inc. nor the names of its
16+
* contributors may be used to endorse or promote products derived from this
17+
* software without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23+
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26+
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
31+
#include "flash_api.h"
32+
#include "mbed_critical.h"
33+
34+
#if DEVICE_FLASH
35+
36+
#include "fsl_flash.h"
37+
38+
int32_t flash_init(flash_t *obj)
39+
{
40+
status_t result;
41+
42+
/* Clean up Flash driver Structure*/
43+
memset(&obj->flash_config, 0, sizeof(flash_config_t));
44+
45+
/* Setup flash driver structure for device and initialize variables. */
46+
result = FLASH_Init(&obj->flash_config);
47+
if (kStatus_FLASH_Success != result) {
48+
return -1;
49+
} else {
50+
return 0;
51+
}
52+
}
53+
54+
int32_t flash_free(flash_t *obj)
55+
{
56+
return 0;
57+
}
58+
59+
int32_t flash_erase_sector(flash_t *obj, uint32_t address)
60+
{
61+
int status;
62+
63+
/* We need to prevent flash accesses during erase operation */
64+
core_util_critical_section_enter();
65+
status = FLASH_Erase(&obj->flash_config, address, obj->flash_config.PFlashSectorSize, kFLASH_ApiEraseKey);
66+
67+
if (status == kStatus_Success) {
68+
status = FLASH_VerifyErase(&obj->flash_config, address, obj->flash_config.PFlashSectorSize, kFLASH_MarginValueNormal);
69+
}
70+
core_util_critical_section_exit();
71+
72+
if (status == kStatus_Success) {
73+
return 0;
74+
} else {
75+
return -1;
76+
}
77+
}
78+
79+
int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, uint32_t size)
80+
{
81+
int status;
82+
83+
/* We need to prevent flash accesses during program operation */
84+
core_util_critical_section_enter();
85+
status = FLASH_Program(&obj->flash_config, address, (uint32_t *)data, size);
86+
87+
if (status == kStatus_Success) {
88+
// Must use kFlashMargin_User, or kFlashMargin_Factory for verify program
89+
status = FLASH_VerifyProgram(&obj->flash_config, address, size,
90+
(uint32_t *)data, kFLASH_MarginValueUser,
91+
NULL, NULL);
92+
}
93+
core_util_critical_section_exit();
94+
95+
return status;
96+
97+
}
98+
99+
uint32_t flash_get_sector_size(const flash_t *obj, uint32_t address)
100+
{
101+
uint32_t sectorsize = MBED_FLASH_INVALID_SIZE;
102+
uint32_t devicesize = 0;
103+
uint32_t startaddr = 0;
104+
105+
FLASH_GetProperty((flash_config_t *)&obj->flash_config, kFLASH_PropertyPflashBlockBaseAddr, &startaddr);
106+
FLASH_GetProperty((flash_config_t *)&obj->flash_config, kFLASH_PropertyPflashTotalSize, &devicesize);
107+
108+
if ((address >= startaddr) && (address < (startaddr + devicesize))) {
109+
FLASH_GetProperty((flash_config_t *)&obj->flash_config, kFLASH_PropertyPflashSectorSize, &sectorsize);
110+
}
111+
112+
return sectorsize;
113+
}
114+
115+
uint32_t flash_get_page_size(const flash_t *obj)
116+
{
117+
return FSL_FEATURE_FLASH_PFLASH_BLOCK_WRITE_UNIT_SIZE;
118+
}
119+
120+
uint32_t flash_get_start_address(const flash_t *obj)
121+
{
122+
uint32_t startaddr = 0;
123+
124+
FLASH_GetProperty((flash_config_t *)&obj->flash_config, kFLASH_PropertyPflashBlockBaseAddr, &startaddr);
125+
126+
return startaddr;
127+
}
128+
129+
uint32_t flash_get_size(const flash_t *obj)
130+
{
131+
uint32_t devicesize = 0;
132+
133+
FLASH_GetProperty((flash_config_t *)&obj->flash_config, kFLASH_PropertyPflashTotalSize, &devicesize);
134+
135+
return devicesize;
136+
}
137+
138+
#endif

targets/TARGET_Freescale/TARGET_MCUXpresso_MCUS/api/objects.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#if DEVICE_SERIAL_ASYNCH
2727
#include "fsl_uart_edma.h"
2828
#endif
29+
#include "fsl_flash.h"
2930
#include "dma_api_hal.h"
3031

3132
#ifdef __cplusplus
@@ -90,6 +91,10 @@ struct trng_s {
9091
uint8_t dummy;
9192
};
9293

94+
struct flash_s {
95+
flash_config_t flash_config;
96+
};
97+
9398
#include "gpio_object.h"
9499

95100
#ifdef __cplusplus

targets/targets.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@
583583
"MCU_K24F1M": {
584584
"core": "Cortex-M4F",
585585
"supported_toolchains": ["ARM", "GCC_ARM", "IAR"],
586-
"extra_labels": ["Freescale", "MCUXpresso_MCUS", "KSDK2_MCUS", "MCU_K24F", "KPSDK_MCUS", "KPSDK_CODE", "FLASH_CMSIS_ALGO"],
586+
"extra_labels": ["Freescale", "MCUXpresso_MCUS", "KSDK2_MCUS", "MCU_K24F", "KPSDK_MCUS", "KPSDK_CODE"],
587587
"is_disk_virtual": true,
588588
"public": false,
589589
"macros": ["CPU_MK24FN1M0VDC12", "FSL_RTOS_MBED"],
@@ -601,7 +601,7 @@
601601
"supported_form_factors": ["ARDUINO"],
602602
"core": "Cortex-M4F",
603603
"supported_toolchains": ["ARM", "GCC_ARM", "IAR"],
604-
"extra_labels": ["Freescale", "MCUXpresso_MCUS", "KSDK2_MCUS", "FRDM", "KPSDK_MCUS", "KPSDK_CODE", "MCU_K64F", "FLASH_CMSIS_ALGO"],
604+
"extra_labels": ["Freescale", "MCUXpresso_MCUS", "KSDK2_MCUS", "FRDM", "KPSDK_MCUS", "KPSDK_CODE", "MCU_K64F"],
605605
"is_disk_virtual": true,
606606
"macros": ["CPU_MK64FN1M0VMD12", "FSL_RTOS_MBED"],
607607
"inherits": ["Target"],
@@ -616,7 +616,7 @@
616616
"inherits": ["Target"],
617617
"core": "Cortex-M4F",
618618
"supported_toolchains": ["ARM", "GCC_ARM"],
619-
"extra_labels": ["Freescale", "MCUXpresso_MCUS", "KSDK2_MCUS", "KPSDK_MCUS", "KPSDK_CODE", "MCU_K64F", "FLASH_CMSIS_ALGO"],
619+
"extra_labels": ["Freescale", "MCUXpresso_MCUS", "KSDK2_MCUS", "KPSDK_MCUS", "KPSDK_CODE", "MCU_K64F"],
620620
"is_disk_virtual": true,
621621
"macros": ["CPU_MK64FN1M0VMD12", "FSL_RTOS_MBED", "TARGET_K64F"],
622622
"device_has": ["I2C", "I2CSLAVE", "INTERRUPTIN", "PORTIN", "PORTINOUT", "PORTOUT", "RTC", "SERIAL", "SERIAL_ASYNCH", "SLEEP", "SPI", "SPI_ASYNCH", "SPISLAVE", "STDIO_MESSAGES", "FLASH"],
@@ -625,7 +625,7 @@
625625
"HEXIWEAR": {
626626
"inherits": ["Target"],
627627
"core": "Cortex-M4F",
628-
"extra_labels": ["Freescale", "MCUXpresso_MCUS", "KSDK2_MCUS", "MCU_K64F", "FLASH_CMSIS_ALGO"],
628+
"extra_labels": ["Freescale", "MCUXpresso_MCUS", "KSDK2_MCUS", "MCU_K64F"],
629629
"supported_toolchains": ["ARM", "GCC_ARM", "IAR"],
630630
"macros": ["CPU_MK64FN1M0VMD12", "FSL_RTOS_MBED", "TARGET_K64F"],
631631
"is_disk_virtual": true,

0 commit comments

Comments
 (0)