Skip to content

Commit acc7202

Browse files
authored
Return a flashair IP from the simulated SD (#290)
* Add support for returning a flashair IP from the SD * Add test case to check IP address read.
1 parent ef089ab commit acc7202

File tree

5 files changed

+48
-5
lines changed

5 files changed

+48
-5
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ jobs:
119119

120120
- name: Install packages
121121
run: |
122-
brew cask install xquartz
122+
brew install --cask xquartz
123123
brew tap osx-cross/avr
124124
brew install libelf freeglut glew SDL avr-gcc libpng
125125

parts/components/SDCard.cpp

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ const uint16_t SDCard::m_crctab[256] = {
158158
/* Debug macros. */
159159
// #define SD_CARD_DEBUG
160160
#ifdef SD_CARD_DEBUG
161-
#define DEBUG(m, ...) fprintf (stderr, "%lu: sdcard: " m "\n", avr->cycle, __VA_ARGS__);
161+
#define DEBUG(m, ...) fprintf(stderr, "%lu: sdcard:" m "\n", m_pAVR->cycle, __VA_ARGS__ )
162162
#else
163163
#define DEBUG(m, ...) do{}while(0)
164164
#endif
@@ -280,6 +280,29 @@ SDCard::State SDCard::ProcessCommand()
280280
/* Application-specific. TODO: No idea what this does. */
281281
COMMAND_RESPONSE_R1 (0x00);
282282
break;
283+
case Command::CMD48:
284+
{
285+
/* FlashAir specific, just for the IP */
286+
extRegRead cmd {.all = m_CmdIn.bits.address};
287+
if (cmd.bits.mio == 1 && cmd.bits.function == 0b0010 && cmd.bits.address == 0x550)
288+
{
289+
if (cmd.bits.length == 3)
290+
{
291+
// IP request. (12.34.56.78)
292+
COMMAND_RESPONSE_R1(0x00);
293+
next_state = State::DATA_READ_TOKEN;
294+
memset(m_tmpdata.begin(), 0, m_tmpdata.size_bytes());
295+
m_tmpdata[0] = 123;
296+
m_tmpdata[1] = 45;
297+
m_tmpdata[2] = 67;
298+
m_tmpdata[3] = 89;
299+
m_currOp.SetData(m_tmpdata);
300+
break;
301+
}
302+
}
303+
DEBUG("Unimplemented Extension Register (CMD48) request! %u", m_CmdIn.bits.address);
304+
break;
305+
}
283306
case Command::CMD55:
284307
/* APP_CMD. Indicates to the card that the next command is an application specific command. */
285308
COMMAND_RESPONSE_R1 (0x00);
@@ -302,7 +325,7 @@ SDCard::State SDCard::ProcessCommand()
302325
void SDCard::OnCSELIn (struct avr_irq_t *, uint32_t value)
303326
{
304327
m_bSelected = value==0;
305-
DEBUG ("SD card selected: %u. In state: %d", m_bSelected, m_state);
328+
//DEBUG ("SD card selected: %u. In state: %d", m_bSelected, static_cast<int>(m_state));
306329
if (!m_bSelected)
307330
{
308331
m_state = State::IDLE;
@@ -311,7 +334,7 @@ void SDCard::OnCSELIn (struct avr_irq_t *, uint32_t value)
311334

312335
uint8_t SDCard::OnSPIIn(struct avr_irq_t *, uint32_t value)
313336
{
314-
DEBUG ("Received byte %x (in state %d).", value, m_state);
337+
//DEBUG ("Received byte %x (in state %d).", value, static_cast<int>(m_state));
315338
uint8_t uiReply = 0xFF;
316339
/* Handle the command. */
317340
switch (m_state) {
@@ -494,7 +517,7 @@ void SDCard::SetCSDCSize(off_t c_size)
494517
m_csd[15] = CRC7(m_csd.subspan(0,m_csd.size()-1));
495518
#ifdef SD_CARD_DEBUG
496519
printf("CSD: ");
497-
for (int i = 0; i < sizeof(m_csd); i++)
520+
for (auto i = 0u; i < sizeof(m_csd); i++)
498521
{
499522
printf("%02hX", m_csd[i]);
500523
}

parts/components/SDCard.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ class SDCard:public SPIPeripheral, public Scriptable, private IKeyClient
128128
CMD17 = 17,
129129
CMD24 = 24,
130130
CMD41 = 41,
131+
CMD48 = 48,
131132
CMD55 = 55,
132133
CMD58 = 58,
133134
};
@@ -151,6 +152,18 @@ class SDCard:public SPIPeripheral, public Scriptable, private IKeyClient
151152

152153
uint8_t m_CmdCount = 0;
153154

155+
using extRegRead = union {
156+
uint32_t all;
157+
struct {
158+
uint32_t length :9;
159+
uint32_t address :17;
160+
uint32_t rev :1;
161+
uint32_t function :4;
162+
uint32_t mio :1;
163+
164+
} __attribute__ ((__packed__)) bits;
165+
};
166+
154167
struct {
155168
uint8_t data[5];
156169
uint8_t length; /* number of bytes of data which are valid */
@@ -189,6 +202,9 @@ class SDCard:public SPIPeripheral, public Scriptable, private IKeyClient
189202
uint8_t _m_ByteCRC[2] = {0,0};
190203
gsl::span<uint8_t> m_byteCRC {_m_ByteCRC};
191204

205+
uint8_t _m_tmpdata[512] = {0};
206+
gsl::span<uint8_t> m_tmpdata {_m_tmpdata};
207+
192208
/* Card data. */
193209
gsl::span<uint8_t> m_data; /* mmap()ed data */
194210
int m_data_fd = -1;

scripts/tests/test_SD.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@ int main()
200200
// Read a block, address OOR
201201
SDTX(0x11,0xFFFFFFFFFFULL,1);
202202

203+
// Get extended register for IP address (flashair)
204+
SDTX(48u,0x900AA003ULL,513);
205+
203206
// Read a block, address misalign
204207
SDTX(0x11,0x0ULL,516);
205208

scripts/tests/test_SD.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Serial0::NextLineMustBe(REPLY 00c0000000) #CMD58
2424
Serial0::NextLineMustBe(REPLY 00) #CMD41
2525
Serial0::NextLineMustBe(REPLY 00) #CMD55
2626
Serial0::NextLineMustBe(REPLY 40) #17 with bad address
27+
Serial0::NextLineMustBe(REPLY 00fe7b2d4359000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000) # IP check
2728
Serial0::NextLineMustBe(REPLY 00feeb58906d6b66732e66617400020120000200000000f80000200040000000000000000200f103000000000000020000000100060000000000000000000000000080002900000000202020202020202020202046415433322020200e1fbe777cac22c0740b56b40ebb0700cd105eebf032e4cd16cd19ebfe0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000055aa14cc)
2829
Serial0::NextLineMustBe(REPLY 40) #24 with bad address
2930
Serial0::NextLineMustBe(WRITTEN)

0 commit comments

Comments
 (0)