270 lines
9.8 KiB
C++
Executable File
270 lines
9.8 KiB
C++
Executable File
#include "fx_api.h"
|
|
#include "sdmmc.h"
|
|
#include <stdio.h>
|
|
#include "prepherials.h"
|
|
|
|
|
|
extern "C" {
|
|
|
|
UINT _fx_partition_offset_calculate(void *partition_sector, UINT partition,
|
|
ULONG *partition_start, ULONG *partition_size);
|
|
ULONG partition_start;
|
|
ULONG partition_size;
|
|
|
|
VOID fx_ggeta_driver(FX_MEDIA *media_ptr) {
|
|
hal_status_e ret;
|
|
switch (media_ptr->fx_media_driver_request) {
|
|
case FX_DRIVER_INIT: {
|
|
// sdcard will be init_old by the user sperately
|
|
media_ptr->fx_media_driver_status = FX_SUCCESS;
|
|
break;
|
|
}
|
|
case FX_DRIVER_UNINIT:
|
|
break;
|
|
case FX_DRIVER_READ:
|
|
ret = sdmmc1.sdmmc_read_multiple_block((uint32_t)media_ptr->fx_media_driver_buffer,
|
|
media_ptr->fx_media_driver_logical_sector +
|
|
media_ptr->fx_media_hidden_sectors,
|
|
media_ptr->fx_media_driver_sectors);
|
|
|
|
|
|
if (ret == HAL_OK) media_ptr->fx_media_driver_status = FX_SUCCESS;
|
|
else
|
|
media_ptr->fx_media_driver_status = FX_IO_ERROR;
|
|
break;
|
|
case FX_DRIVER_WRITE:
|
|
|
|
ret = sdmmc1.sdmmc_write_multiple_block((uint32_t)media_ptr->fx_media_driver_buffer,
|
|
media_ptr->fx_media_driver_logical_sector +
|
|
media_ptr->fx_media_hidden_sectors,
|
|
media_ptr->fx_media_driver_sectors);
|
|
if (ret == HAL_OK) media_ptr->fx_media_driver_status = FX_SUCCESS;
|
|
else
|
|
media_ptr->fx_media_driver_status = FX_IO_ERROR;
|
|
break;
|
|
case FX_DRIVER_BOOT_READ:
|
|
ret = sdmmc1.sdmmc_read_multiple_block((uint32_t)media_ptr->fx_media_driver_buffer, 0,
|
|
media_ptr->fx_media_driver_sectors);
|
|
_fx_partition_offset_calculate(media_ptr->fx_media_driver_buffer, 0, &partition_start,
|
|
&partition_size);
|
|
IF_ERROR_STUCK(ret);
|
|
ret = sdmmc1.sdmmc_read_multiple_block((uint32_t)media_ptr->fx_media_driver_buffer,
|
|
partition_start, media_ptr->fx_media_driver_sectors);
|
|
IF_ERROR_STUCK(ret);
|
|
if (ret == HAL_OK) media_ptr->fx_media_driver_status = FX_SUCCESS;
|
|
else
|
|
media_ptr->fx_media_driver_status = FX_IO_ERROR;
|
|
|
|
break;
|
|
case FX_DRIVER_BOOT_WRITE: {
|
|
// ret = sdmmc_write_multiple_block(sd1,
|
|
// (uint32_t) media_ptr->fx_media_driver_buffer,
|
|
// partition_start,
|
|
// 1);
|
|
ret = sdmmc1.sdmmc_write_single_block((uint32_t)media_ptr->fx_media_driver_buffer,
|
|
partition_start);
|
|
|
|
IF_ERROR_STUCK(ret);
|
|
if (ret == HAL_OK) media_ptr->fx_media_driver_status = FX_SUCCESS;
|
|
else
|
|
media_ptr->fx_media_driver_status = FX_IO_ERROR;
|
|
break;
|
|
}
|
|
default:
|
|
media_ptr->fx_media_driver_status = FX_NOT_IMPLEMENTED;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
//void fx_stm32_sd_driver(FX_MEDIA *media_ptr) {
|
|
// UINT status;
|
|
// UINT unaligned_buffer;
|
|
// ULONG partition_start;
|
|
// ULONG partition_size;
|
|
//
|
|
//#if (FX_STM32_SD_INIT == 0)
|
|
// /* the SD was initialized by the application */
|
|
// is_initialized = 1;
|
|
//#endif
|
|
// /* before performing any operation, check the status of the SD IP */
|
|
// if (is_initialized == 1) {
|
|
// if (check_sd_status(FX_STM32_SD_INSTANCE) != 0) {
|
|
// media_ptr->fx_media_driver_status = FX_IO_ERROR;
|
|
// return;
|
|
// }
|
|
// }
|
|
//
|
|
//#if (FX_STM32_SD_DMA_API == 1)
|
|
// /* the SD DMA requires a 4-byte aligned buffers */
|
|
// unaligned_buffer = (UINT)(media_ptr->fx_media_driver_buffer) & 0x3;
|
|
//#else
|
|
// /* if the DMA is not used there isn't any constraint on buffer alignment */
|
|
// unaligned_buffer = 0;
|
|
//#endif
|
|
// /* Process the driver request specified in the media control block. */
|
|
// switch (media_ptr->fx_media_driver_request) {
|
|
// case FX_DRIVER_INIT: {
|
|
// media_ptr->fx_media_driver_status = FX_SUCCESS;
|
|
//
|
|
// FX_STM32_SD_PRE_INIT(media_ptr);
|
|
//
|
|
//#if (FX_STM32_SD_INIT == 1)
|
|
// /* Initialize the SD instance */
|
|
// if (is_initialized == 0)
|
|
// {
|
|
// status = fx_stm32_sd_init(FX_STM32_SD_INSTANCE);
|
|
//
|
|
// if (status == 0)
|
|
// {
|
|
// is_initialized = 1;
|
|
// }
|
|
// else
|
|
// {
|
|
// media_ptr->fx_media_driver_status = FX_IO_ERROR;
|
|
// }
|
|
// }
|
|
//#endif
|
|
// /* call post init_old user macro */
|
|
// FX_STM32_SD_POST_INIT(media_ptr);
|
|
// break;
|
|
// }
|
|
//
|
|
// case FX_DRIVER_UNINIT: {
|
|
// media_ptr->fx_media_driver_status = FX_SUCCESS;
|
|
//
|
|
//#if (FX_STM32_SD_INIT == 1)
|
|
// status = fx_stm32_sd_deinit(FX_STM32_SD_INSTANCE);
|
|
//
|
|
// if (status != 0)
|
|
// {
|
|
// media_ptr->fx_media_driver_status = FX_IO_ERROR;
|
|
// }
|
|
// else
|
|
// {
|
|
// is_initialized = 0;
|
|
// }
|
|
//#endif
|
|
// /* call post deinit processing */
|
|
// FX_STM32_SD_POST_DEINIT(media_ptr);
|
|
//
|
|
// break;
|
|
// }
|
|
//
|
|
// case FX_DRIVER_READ: {
|
|
// media_ptr->fx_media_driver_status = FX_IO_ERROR;
|
|
//
|
|
// if (sd_read_data(media_ptr, media_ptr->fx_media_driver_logical_sector + media_ptr->fx_media_hidden_sectors,
|
|
// media_ptr->fx_media_driver_sectors, unaligned_buffer) == FX_SUCCESS) {
|
|
// media_ptr->fx_media_driver_status = FX_SUCCESS;
|
|
// }
|
|
//
|
|
// break;
|
|
// }
|
|
//
|
|
// case FX_DRIVER_WRITE: {
|
|
// media_ptr->fx_media_driver_status = FX_IO_ERROR;
|
|
//
|
|
// if (sd_write_data(media_ptr, media_ptr->fx_media_driver_logical_sector + media_ptr->fx_media_hidden_sectors,
|
|
// media_ptr->fx_media_driver_sectors, unaligned_buffer) == FX_SUCCESS) {
|
|
// media_ptr->fx_media_driver_status = FX_SUCCESS;
|
|
// }
|
|
//
|
|
// break;
|
|
// }
|
|
//
|
|
// case FX_DRIVER_FLUSH: {
|
|
// /* Return driver success. */
|
|
// media_ptr->fx_media_driver_status = FX_SUCCESS;
|
|
// break;
|
|
// }
|
|
//
|
|
// case FX_DRIVER_ABORT: {
|
|
// /* Return driver success. */
|
|
// media_ptr->fx_media_driver_status = FX_SUCCESS;
|
|
//
|
|
// FX_STM32_SD_POST_ABORT(media_ptr);
|
|
// break;
|
|
// }
|
|
//
|
|
// case FX_DRIVER_BOOT_READ: {
|
|
// /* the boot sector is the sector zero */
|
|
// status = sd_read_data(media_ptr, 0, media_ptr->fx_media_driver_sectors, unaligned_buffer);
|
|
//
|
|
// if (status != FX_SUCCESS) {
|
|
// media_ptr->fx_media_driver_status = status;
|
|
// break;
|
|
// }
|
|
//
|
|
// /* Check if the sector 0 is the actual boot sector, otherwise calculate the offset into it.
|
|
// Please note that this should belong to higher level of MW to do this check and it is here
|
|
// as a temporary work solution */
|
|
//
|
|
// partition_start = 0;
|
|
//
|
|
// status = _fx_partition_offset_calculate(media_ptr->fx_media_driver_buffer, 0,
|
|
// &partition_start, &partition_size);
|
|
//
|
|
// /* Check partition read error. */
|
|
// if (status) {
|
|
// /* Unsuccessful driver request. */
|
|
// media_ptr->fx_media_driver_status = FX_IO_ERROR;
|
|
// break;
|
|
// }
|
|
//
|
|
// /* Now determine if there is a partition... */
|
|
// if (partition_start) {
|
|
//
|
|
// if (check_sd_status(FX_STM32_SD_INSTANCE) != 0) {
|
|
// media_ptr->fx_media_driver_status = FX_IO_ERROR;
|
|
// break;
|
|
// }
|
|
//
|
|
// /* Yes, now lets read the actual boot record. */
|
|
// status = sd_read_data(media_ptr, partition_start, media_ptr->fx_media_driver_sectors, unaligned_buffer);
|
|
//
|
|
// if (status != FX_SUCCESS) {
|
|
// media_ptr->fx_media_driver_status = status;
|
|
// break;
|
|
// }
|
|
// }
|
|
//
|
|
// /* Successful driver request. */
|
|
// media_ptr->fx_media_driver_status = FX_SUCCESS;
|
|
// break;
|
|
// }
|
|
//
|
|
// case FX_DRIVER_BOOT_WRITE: {
|
|
// status = sd_write_data(media_ptr, 0, media_ptr->fx_media_driver_sectors, unaligned_buffer);
|
|
//
|
|
// media_ptr->fx_media_driver_status = status;
|
|
//
|
|
// break;
|
|
// }
|
|
//
|
|
// default: {
|
|
// media_ptr->fx_media_driver_status = FX_IO_ERROR;
|
|
// break;
|
|
// }
|
|
// }
|
|
//}
|
|
|
|
/**
|
|
* @brief Read data from uSD into destination buffer
|
|
* @param FX_MEDIA *media_ptr a pointer the main FileX structure
|
|
* @param ULONG start_sector first sector to start reading from
|
|
* @param UINT num_sectors number of sectors to be read
|
|
* @param UINT use_scratch_buffer to enable scratch buffer usage or not.
|
|
* @retval FX_SUCCESS on success FX_BUFFER_ERROR / FX_ACCESS_ERROR / FX_IO_ERROR otherwise
|
|
*/
|
|
//static UINT sd_read_data(FX_MEDIA *media_ptr, ULONG start_sector, UINT num_sectors, UINT use_scratch_buffer)
|
|
|
|
/**
|
|
* @brief write data buffer into the uSD
|
|
* @param FX_MEDIA *media_ptr a pointer the main FileX structure
|
|
* @param ULONG start_sector first sector to start writing from
|
|
* @param UINT num_sectors number of sectors to be written
|
|
* @param UINT use_scratch_buffer to enable scratch buffer usage or not.
|
|
* @retval FX_SUCCESS on success FX_BUFFER_ERROR / FX_ACCESS_ERROR / FX_IO_ERROR otherwise
|
|
*/
|
|
// static UINT sd_write_data(FX_MEDIA *media_ptr, ULONG start_sector, UINT num_sectors, UINT use_scratch_buffer);
|