/* * Copyright (c) 2016, Freescale Semiconductor, Inc. * Copyright 2016-2017 NXP * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * o Redistributions of source code must retain the above copyright notice, this list * of conditions and the following disclaimer. * * o Redistributions in binary form must reproduce the above copyright notice, this * list of conditions and the following disclaimer in the documentation and/or * other materials provided with the distribution. * * o Neither the name of the copyright holder nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "FreeRTOS.h" #include "board.h" #include "fsl_host.h" #include "fsl_port.h" #include "semphr.h" #include "task.h" /******************************************************************************* * Definitions ******************************************************************************/ /******************************************************************************* * Prototypes ******************************************************************************/ /******************************************************************************* * Variables ******************************************************************************/ static sdhc_handle_t g_sdhcHandle; static uint32_t g_sdhcAdmaTable[SDHC_ADMA_TABLE_WORDS]; static volatile bool g_sdhcTransferSuccessFlag = true; static SemaphoreHandle_t s_cd_semphr = NULL; static SemaphoreHandle_t s_transfer_semphr = NULL; /******************************************************************************* * Code ******************************************************************************/ static bool DetectCardByHost(void) { if (SDHC->PRSSTAT & SDHC_PRSSTAT_CINS_MASK) { return true; } return false; } /* Card detect. */ status_t CardInsertDetect(HOST_TYPE *hostBase) { if (DetectCardByHost() == true) { return kStatus_Success; } SDHC_EnableInterruptSignal(SDHC, kSDHC_CardDetectFlag); for (;;) { if (xSemaphoreTake(s_cd_semphr, portMAX_DELAY) == pdPASS) { if (DetectCardByHost() == true) { break; } } } /* Delay some time to make card stable. */ vTaskDelay(pdMS_TO_TICKS(1000)); return kStatus_Success; } /* Card detect pin port interrupt handler. */ void CardInsertDetectHandle(void) { BaseType_t task_woken; SDHC_DisableInterruptSignal(SDHC, kSDHC_CardDetectFlag); if (s_cd_semphr == NULL) return; xSemaphoreGiveFromISR(s_cd_semphr, &task_woken); portYIELD_FROM_ISR(task_woken); } /* SDHC transfer complete callback function. */ static void SDHC_TransferCompleteCallback(SDHC_Type *base, sdhc_handle_t *handle, status_t status, void *userData) { if (status == kStatus_Success) { g_sdhcTransferSuccessFlag = true; } else { g_sdhcTransferSuccessFlag = false; } BaseType_t task_woken; xSemaphoreGiveFromISR(s_transfer_semphr, &task_woken); portYIELD_FROM_ISR(task_woken); } /* User defined transfer function. */ static status_t SDHC_TransferFunction(SDHC_Type *base, sdhc_transfer_t *content) { status_t error = kStatus_Success; do { error = SDHC_TransferNonBlocking(base, &g_sdhcHandle, g_sdhcAdmaTable, SDHC_ADMA_TABLE_WORDS, content); } while (error == kStatus_SDHC_BusyTransferring); if (error != kStatus_Success) { return kStatus_Fail; } if (xSemaphoreTake(s_transfer_semphr, pdMS_TO_TICKS(1000)) != pdPASS) { return kStatus_Fail; } if (!g_sdhcTransferSuccessFlag) { return kStatus_Fail; } return error; } status_t HOST_Init(void *host) { sdhc_transfer_callback_t sdhcCallback = {0}; sdhc_host_t *sdhcHost = (sdhc_host_t *)host; NVIC_SetPriority(SDHC_IRQn, 6); /* Initializes SDHC. */ sdhcHost->config.cardDetectDat3 = true; sdhcHost->config.endianMode = SDHC_ENDIAN_MODE; sdhcHost->config.dmaMode = SDHC_DMA_MODE; sdhcHost->config.readWatermarkLevel = SDHC_READ_WATERMARK_LEVEL; sdhcHost->config.writeWatermarkLevel = SDHC_WRITE_WATERMARK_LEVEL; SDHC_Init(sdhcHost->base, &(sdhcHost->config)); /* Create handle for SDHC driver */ sdhcCallback.TransferComplete = SDHC_TransferCompleteCallback; sdhcCallback.CardInserted = CardInsertDetectHandle; sdhcCallback.CardRemoved = CardInsertDetectHandle; SDHC_TransferCreateHandle(sdhcHost->base, &g_sdhcHandle, &sdhcCallback, NULL); /* Create transfer complete event. */ s_cd_semphr = xSemaphoreCreateBinary(); if (s_cd_semphr == NULL) { return kStatus_Fail; } s_transfer_semphr = xSemaphoreCreateBinary(); if (s_transfer_semphr == NULL) { return kStatus_Fail; } /* Define transfer function. */ sdhcHost->transfer = SDHC_TransferFunction; return kStatus_Success; } void HOST_Deinit(void *host) { sdhc_host_t *sdhcHost = (sdhc_host_t *)host; SDHC_Deinit(sdhcHost->base); vSemaphoreDelete(s_cd_semphr); vSemaphoreDelete(s_transfer_semphr); }