Welcome to mirror list, hosted at ThFree Co, Russian Federation.

spi_sd_hal.c « fatfs « f7 « targets « firmware - github.com/ClusterM/flipperzero-firmware.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 70e9bbf16833a8cb0bce7728b5b6d4b735bf1ba4 (plain)
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
#include "main.h"
#include <furi-hal.h>
#include <furi.h>

#define SD_DUMMY_BYTE 0xFF

const uint32_t SpiTimeout = 1000;
uint8_t SD_IO_WriteByte(uint8_t Data);

static const FuriHalSpiDevice* sd_spi_dev = &furi_hal_spi_devices[FuriHalSpiDeviceIdSdCardFast];

/******************************************************************************
                            BUS OPERATIONS
 *******************************************************************************/

/**
 * @brief  SPI Write byte(s) to device
 * @param  DataIn: Pointer to data buffer to write
 * @param  DataOut: Pointer to data buffer for read data
 * @param  DataLength: number of bytes to write
 * @retval None
 */
static void SPIx_WriteReadData(const uint8_t* DataIn, uint8_t* DataOut, uint16_t DataLength) {
    furi_check(furi_hal_spi_bus_trx(sd_spi_dev->bus, (uint8_t*)DataIn, DataOut, DataLength, SpiTimeout));
}

/**
 * @brief  SPI Write a byte to device
 * @param  Value: value to be written
 * @retval None
 */
__attribute__((unused)) static void SPIx_Write(uint8_t Value) {
    furi_check(furi_hal_spi_bus_tx(sd_spi_dev->bus, (uint8_t*)&Value, 1, SpiTimeout));
}

/******************************************************************************
                            LINK OPERATIONS
 *******************************************************************************/

/********************************* LINK SD ************************************/
/**
 * @brief  Initialize the SD Card and put it into StandBy State (Ready for
 *         data transfer).
 * @retval None
 */
void SD_IO_Init(void) {
    uint8_t counter = 0;

    /* SD chip select high */
    hal_gpio_write(sd_spi_dev->chip_select, true);
    delay_us(10);

    /* Send dummy byte 0xFF, 10 times with CS high */
    /* Rise CS and MOSI for 80 clocks cycles */
    for(counter = 0; counter <= 200; counter++) {
        /* Send dummy byte 0xFF */
        SD_IO_WriteByte(SD_DUMMY_BYTE);
    }
}

/**
 * @brief  Set SD interface Chip Select state
 * @param  val: 0 (low) or 1 (high) state
 * @retval None
 */
void SD_IO_CSState(uint8_t val) {
    /* Some SD Cards are prone to fail if CLK-ed too soon after CS transition. Worst case found: 8us */
    if(val == 1) {
        delay_us(10); // Exit guard time for some SD cards
        hal_gpio_write(sd_spi_dev->chip_select, true);
    } else {
        hal_gpio_write(sd_spi_dev->chip_select, false);
        delay_us(10); // Entry guard time for some SD cards
    }
}

/**
 * @brief  Write byte(s) on the SD
 * @param  DataIn: Pointer to data buffer to write
 * @param  DataOut: Pointer to data buffer for read data
 * @param  DataLength: number of bytes to write
 * @retval None
 */
void SD_IO_WriteReadData(const uint8_t* DataIn, uint8_t* DataOut, uint16_t DataLength) {
    /* Send the byte */
    SPIx_WriteReadData(DataIn, DataOut, DataLength);
}

/**
 * @brief  Write a byte on the SD.
 * @param  Data: byte to send.
 * @retval Data written
 */
uint8_t SD_IO_WriteByte(uint8_t Data) {
    uint8_t tmp;

    /* Send the byte */
    SPIx_WriteReadData(&Data, &tmp, 1);
    return tmp;
}