Numworks Epsilon  1.4.1
Graphing Calculator Operating System
sd_card.cpp
Go to the documentation of this file.
1 #include "sd_card.h"
2 #include "regs/regs.h"
3 extern "C" {
4 #include <assert.h>
5 }
6 
7 namespace Ion {
8 namespace SDCard {
9 namespace Device {
10 
11 void init() {
12  initGPIO();
13  initCard();
14 }
15 
16 void initGPIO() {
17  // Configure GPIOs to use AF
24 
25  // More precisely, AF12 which correspond to SDIO alternate functions
32 }
33 
34 void initCard() {
35 
36  // Power on
37  SDIO.POWER()->setPWRCTRL(SDIO::POWER::PWRCTRL::On);
38  while (SDIO.POWER()->getPWRCTRL() != SDIO::POWER::PWRCTRL::On) {
39  }
40 
41  // Clock set
42  SDIO.CLKCR()->setCLKDIV(254);
43  SDIO.CLKCR()->setCLKEN(true);
44 
45  sendCommand(0, 0);
46  // CMD8 : 0b0001 = 2.7 - 3.6V
47  // 0xB7 = Pattern to see back in response
48  sendCommand(8, 0x1B7);
49 
50  assert(SDIO.RESP(1)->get() == 0x1B7);
51 }
52 
53 void sendCommand(uint32_t cmd, uint32_t arg) {
54  class SDIO::ICR icr(0);
55  icr.setCCRCFAILC(true);
56  icr.setCTIMEOUTC(true);
57  icr.setCMDRENDC(true);
58  icr.setCMDSENTC(true);
59  SDIO.ICR()->set(icr);
60 
61  SDIO.ARG()->set(arg);
62 
64  switch (cmd) {
65  case 0:
66  responseType = SDIO::CMD::WAITRESP::None;
67  break;
68  case 2:
69  case 9:
70  case 10:
71  responseType = SDIO::CMD::WAITRESP::Long;
72  default:
73  break;
74  }
75 
76  class SDIO::CMD command(0);
77  command.setCMDINDEX(cmd);
78  command.setCPSMEN(true);
79  command.setWAITRESP(responseType);
80  SDIO.CMD()->set(command);
81 
82  if (responseType == SDIO::CMD::WAITRESP::None) {
83  // Wait for timeout or command sent
84  while (!SDIO.STA()->getCTIMEOUT() && !SDIO.STA()->getCMDSENT()) {
85  }
86  } else {
87  while (!SDIO.STA()->getCTIMEOUT() && !SDIO.STA()->getCMDREND() && !SDIO.STA()->getCCRCFAIL()) {
88  }
89  }
90 
91 }
92 
93 }
94 }
95 }
Definition: sdio.h:33
Definition: gpio.h:53
#define assert(e)
Definition: assert.h:9
Definition: sdio.h:6
void sendCommand(uint32_t cmd, uint32_t arg)
Definition: sd_card.cpp:53
Definition: sdio.h:36
void set(Register< T > value) volatile
Definition: register.h:12
T get() volatile
Definition: register.h:18
constexpr GPIO GPIOB(1)
unsigned int uint32_t
Definition: stdint.h:6
WAITRESP
Definition: sdio.h:39
void setAlternateFunction(int index, AlternateFunction af) volatile
Definition: gpio.h:66
constexpr GPIO GPIOD(3)
volatile RESP * RESP(int i) const
Definition: sdio.h:101
void setMode(int index, Mode mode) volatile
Definition: gpio.h:17
constexpr GPIO GPIOA(0)
Definition: backlight.h:6
Definition: sdio.h:80
Definition: sdio.h:54
constexpr GPIO GPIOC(2)