/
hw_config.c
105 lines (91 loc) · 3.48 KB
/
hw_config.c
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
/* hw_config.c
Copyright 2021 Carl John Kugler III
Licensed under the Apache License, Version 2.0 (the License); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
*/
/*
This file should be tailored to match the hardware design.
There should be one element of the spi[] array for each hardware SPI used.
There should be one element of the sd_cards[] array for each SD card slot.
The name is should correspond to the FatFs "logical drive" identifier.
(See http://elm-chan.org/fsw/ff/doc/filename.html#vol)
The rest of the constants will depend on the type of
socket, which SPI it is driven by, and how it is wired.
*/
#include <string.h>
//
#include "my_debug.h"
//
#include "hw_config.h"
//
#include "ff.h" /* Obtains integer types */
//
#include "diskio.h" /* Declarations of disk functions */
void spi0_dma_isr();
// Hardware Configuration of SPI "objects"
// Note: multiple SD cards can be driven by one SPI if they use different slave
// selects.
static spi_t spis[] = { // One for each SPI.
{
/* .hw_inst = spi1, // SPI component
.miso_gpio = 12, // GPIO number (not pin number)
.mosi_gpio = 11,
.sck_gpio = 10,
*/
.hw_inst = spi0, // SPI component
.miso_gpio = 4, // GPIO number (not pin number)
.mosi_gpio = 3,
.sck_gpio = 2,
/* The choice of SD card matters! SanDisk runs at the highest speed. PNY
can only mangage 5 MHz. Those are all I've tried. */
//.baud_rate = 1000 * 1000, //Try this if SD doesnt work.
// .baud_rate = 5 * 1000 * 1000
.baud_rate = 10000 *1000,
// .baud_rate = 12500 * 1000, // The limitation here is SPI slew rate.
//.baud_rate = 25 * 1000 * 1000, // Actual frequency: 20833333. Has
// worked for me with SanDisk.
// Following attributes are dynamically assigned
.dma_isr = spi0_dma_isr,
.initialized = false, // initialized flag
}
};
// Hardware Configuration of the SD Card "objects"
static sd_card_t sd_cards[] = { // One for each SD card
{
.pcName = "0:", // Name used to mount device
.spi = &spis[0], // Pointer to the SPI driving this card
.ss_gpio = 5, // The SPI slave select GPIO for this SD card
.card_detect_gpio = 22, // Card detect
.card_detected_true = -1, // What the GPIO read returns when a card is
// present. Use -1 if there is no card detect.
// Following attributes are dynamically assigned
.m_Status = STA_NOINIT,
.sectors = 0,
.card_type = 0,
}
};
void spi0_dma_isr() { spi_irq_handler(&spis[0]); }
/* ********************************************************************** */
size_t sd_get_num() { return count_of(sd_cards); }
sd_card_t *sd_get_by_num(size_t num) {
if (num <= sd_get_num()) {
return &sd_cards[num];
} else {
return NULL;
}
}
size_t spi_get_num() { return count_of(spis); }
spi_t *spi_get_by_num(size_t num) {
if (num <= sd_get_num()) {
return &spis[num];
} else {
return NULL;
}
}
/* [] END OF FILE */