~aleteoryx/muditaos

ref: 6912ec26dd6ae4afea3467bd5d33d72df8be75c8 muditaos/module-bsp/board/rt1051/eMMC/eMMC.cpp -rw-r--r-- 2.9 KiB
6912ec26 — Adam Wulkiewicz [BH-1656] Use fixed gt pressura 46 light 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "eMMC.hpp"

#include "bsp/BoardDefinitions.hpp"

#if defined(TARGET_RT1051)
#include "board/rt1051/bsp/eMMC/fsl_mmc.h"
#elif defined(TARGET_Linux)

#else
#error "Unsupported target 1"
#endif

namespace bsp
{

    using namespace drivers;

    RetCode eMMC::Init()
    {
        driverUSDHC = drivers::DriverUSDHC::Create(
            "EMMC", static_cast<drivers::USDHCInstances>(BoardDefinitions::EMMC_USDHC_INSTANCE));
#if defined(TARGET_RT1051)

        // pll = DriverPLL::Create(static_cast<PLLInstances >(BoardDefinitions::EMMC_PLL),DriverPLLParams{});

        mmcCard.busWidth                   = kMMC_DataBusWidth8bit;
        mmcCard.busTiming                  = kMMC_HighSpeedTiming;
        mmcCard.enablePreDefinedBlockCount = true;
        mmcCard.host.base                  = USDHC2; // TODO:M.P move it to BoardDefinitions.hpp
        mmcCard.host.sourceClock_Hz        = GetPerphSourceClock(PerphClock_USDHC2);

        auto ret = MMC_Init(&mmcCard);
        if (ret != kStatus_Success) {
            return RetCode::Failure;
        }
        else {
            userPartitionBlocks = mmcCard.userPartitionBlocks;
            return RetCode::Success;
        }
#else
#error "Unsupported target"
#endif
    }

    RetCode eMMC::DeInit()
    {
#if defined(TARGET_RT1051)
        MMC_Deinit(&mmcCard);
        return RetCode::Success;
#else
#error "Unsupported target"
#endif
    }

    RetCode eMMC::ReadBlocks(uint8_t *buffer, uint32_t startBlock, uint32_t blockCount)
    {
#if defined(TARGET_RT1051)
        driverUSDHC->Enable();
        auto ret = MMC_ReadBlocks(&mmcCard, buffer, startBlock, blockCount);
        driverUSDHC->Disable();
        if (ret != kStatus_Success) {
            return RetCode::Failure;
        }
        else {
            return RetCode::Success;
        }
#else
#error "Unsupported target"
#endif
    }

    RetCode eMMC::WriteBlocks(const uint8_t *buffer, uint32_t startBlock, uint32_t blockCount)
    {
#if defined(TARGET_RT1051)
        driverUSDHC->Enable();
        auto ret = MMC_WriteBlocks(&mmcCard, buffer, startBlock, blockCount);
        driverUSDHC->Disable();
        if (ret != kStatus_Success) {
            return RetCode::Failure;
        }
        else {
            return RetCode::Success;
        }
#else
#error "Unsupported target"
#endif
    }

    RetCode eMMC::SwitchPartition(eMMC::Partition partition)
    {
#if defined(TARGET_RT1051)
        driverUSDHC->Enable();
        auto ret = MMC_SelectPartition(&mmcCard, static_cast<mmc_access_partition_t>(partition));
        driverUSDHC->Disable();
        if (ret != kStatus_Success) {
            return RetCode::Failure;
        }
        else {
            return RetCode::Success;
        }
#else
#error "Unsupported target"
#endif
    }

} // namespace bsp