- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试在 Cypress PSoC 上初始化 2 GB SanDisk SD 卡5 使用SPI接口(interface)。我正在使用 FatFs - Generic FAT File System Module 中的示例位碰撞代码。我已经在逻辑分析仪上分析了接口(interface)(参见 )。问题是 SD 卡的数据输出线总是高电平,我应该从卡初始化命令中得到 0x01。然而,SD 卡在 PC 上读取良好。我正在使用的代码如下,可能是什么问题?
我正在使用来自 http://ucontroller.com/documentation/SDCardDoc.html 的 SD 插槽.
/*------------------------------------------------------------------------/
/ Bitbanging MMCv3/SDv1/SDv2 (in SPI mode) control module
/-------------------------------------------------------------------------/
/
/ Copyright (C) 2010, ChaN, all right reserved.
/
/ * This software is a free software and there is NO WARRANTY.
/ * No restriction on use. You can use, modify and redistribute it for
/ personal, non-profit or commercial products UNDER YOUR RESPONSIBILITY.
/ * Redistributions of source code must retain the above copyright notice.
/
/--------------------------------------------------------------------------/
Features and Limitations:
* Very Easy to Port
It uses only 4-6 bit of GPIO port. No interrupt, no SPI port is used.
* Platform Independent
You need to modify only a few macros to control GPIO ports.
* Low Speed
The data transfer rate will be several times slower than hardware SPI.
* No Media Change Detection
Application program must re-mount the volume after media change or it
results a hard error.
/-------------------------------------------------------------------------*/
#include <device.h>
#include "diskio.h" /* Common include file for FatFs and disk I/O layer */
/*-------------------------------------------------------------------------*/
/* Platform dependent macros and functions needed to be modified */
/*-------------------------------------------------------------------------*/
#include <device.h> /* Include device specific declareation file here */
#define INIT_PORT() /* Initialize MMC control port (CS/CLK/DI:output, DO/WP/INS:input) */
#define DLY_US(n) CyDelayUs(n) /* Delay n microseconds */
#define CLK_DELAY() CyDelayUs(1)
#define CS_H() CyPins_SetPin(ss_0) /* Set MMC CS "high" */
#define CS_L() CyPins_ClearPin(ss_0) /* Set MMC CS "low" */
#define CK_H() CyPins_SetPin(clk_0) /* Set MMC SCLK "high" */
#define CK_L() CyPins_ClearPin(clk_0) /* Set MMC SCLK "low" */
#define DI_H() CyPins_SetPin(spiout_0) /* Set MMC DI "high" */
#define DI_L() CyPins_ClearPin(spiout_0) /* Set MMC DI "low" */
#define DO CyPins_ReadPin(spiin_0) /* Get MMC DO value (high:true, low:false) */
#define INS (1) /* Card is inserted (yes:true, no:false, default:true) */
#define WP (0) /* Card is write protected (yes:true, no:false, default:false) */
/*--------------------------------------------------------------------------
Module Private Functions
---------------------------------------------------------------------------*/
/* MMC/SD command (SPI mode) */
#define CMD0 (0) /* GO_IDLE_STATE */
#define CMD1 (1) /* SEND_OP_COND */
#define ACMD41 (0x80+41) /* SEND_OP_COND (SDC) */
#define CMD8 (8) /* SEND_IF_COND */
#define CMD9 (9) /* SEND_CSD */
#define CMD10 (10) /* SEND_CID */
#define CMD12 (12) /* STOP_TRANSMISSION */
#define ACMD13 (0x80+13) /* SD_STATUS (SDC) */
#define CMD16 (16) /* SET_BLOCKLEN */
#define CMD17 (17) /* READ_SINGLE_BLOCK */
#define CMD18 (18) /* READ_MULTIPLE_BLOCK */
#define CMD23 (23) /* SET_BLOCK_COUNT */
#define ACMD23 (0x80+23) /* SET_WR_BLK_ERASE_COUNT (SDC) */
#define CMD24 (24) /* WRITE_BLOCK */
#define CMD25 (25) /* WRITE_MULTIPLE_BLOCK */
#define CMD41 (41) /* SEND_OP_COND (ACMD) */
#define CMD55 (55) /* APP_CMD */
#define CMD58 (58) /* READ_OCR */
/* Card type flags (CardType) */
#define CT_MMC 0x01 /* MMC ver 3 */
#define CT_SD1 0x02 /* SD ver 1 */
#define CT_SD2 0x04 /* SD ver 2 */
#define CT_SDC (CT_SD1|CT_SD2) /* SD */
#define CT_BLOCK 0x08 /* Block addressing */
static
DSTATUS Stat = STA_NOINIT; /* Disk status */
static
BYTE CardType; /* b0:MMC, b1:SDv1, b2:SDv2, b3:Block addressing */
static char buf[80];
/*-----------------------------------------------------------------------*/
/* Transmit bytes to the MMC (bitbanging) */
/*-----------------------------------------------------------------------*/
static
void xmit_mmc (
const BYTE* buff, /* Data to be sent */
UINT bc /* Number of bytes to send */
)
{
BYTE d;
do {
d = *buff++; /* Get a byte to be sent */
if (d & 0x80) DI_H(); else DI_L(); /* bit7 */
CK_H(); CLK_DELAY(); CK_L(); CLK_DELAY();
if (d & 0x40) DI_H(); else DI_L(); /* bit6 */
CK_H(); CLK_DELAY(); CK_L(); CLK_DELAY();
if (d & 0x20) DI_H(); else DI_L(); /* bit5 */
CK_H(); CLK_DELAY(); CK_L(); CLK_DELAY();
if (d & 0x10) DI_H(); else DI_L(); /* bit4 */
CK_H(); CLK_DELAY(); CK_L(); CLK_DELAY();
if (d & 0x08) DI_H(); else DI_L(); /* bit3 */
CK_H(); CLK_DELAY(); CK_L(); CLK_DELAY();
if (d & 0x04) DI_H(); else DI_L(); /* bit2 */
CK_H(); CLK_DELAY(); CK_L(); CLK_DELAY();
if (d & 0x02) DI_H(); else DI_L(); /* bit1 */
CK_H(); CLK_DELAY(); CK_L(); CLK_DELAY();
if (d & 0x01) DI_H(); else DI_L(); /* bit0 */
CK_H(); CLK_DELAY(); CK_L(); CLK_DELAY();
DLY_US(10);
//snprintf(buf, sizeof buf, "sent 0x%02x\r\n", d);
//UART_1_PutString(buf);
} while (--bc);
}
/*-----------------------------------------------------------------------*/
/* Receive bytes from the MMC (bitbanging) */
/*-----------------------------------------------------------------------*/
static
void rcvr_mmc (
BYTE *buff, /* Pointer to read buffer */
UINT bc /* Number of bytes to receive */
)
{
BYTE r;
DI_H(); /* Send 0xFF */
do {
r = 0; if (DO) r++; /* bit7 */
CK_H(); CLK_DELAY(); CK_L(); CLK_DELAY();
r <<= 1; if (DO) r++; /* bit6 */
CK_H(); CLK_DELAY(); CK_L(); CLK_DELAY();
r <<= 1; if (DO) r++; /* bit5 */
CK_H(); CLK_DELAY(); CK_L(); CLK_DELAY();
r <<= 1; if (DO) r++; /* bit4 */
CK_H(); CLK_DELAY(); CK_L(); CLK_DELAY();
r <<= 1; if (DO) r++; /* bit3 */
CK_H(); CLK_DELAY(); CK_L(); CLK_DELAY();
r <<= 1; if (DO) r++; /* bit2 */
CK_H(); CLK_DELAY(); CK_L(); CLK_DELAY();
r <<= 1; if (DO) r++; /* bit1 */
CK_H(); CLK_DELAY(); CK_L(); CLK_DELAY();
r <<= 1; if (DO) r++; /* bit0 */
CK_H(); CLK_DELAY(); CK_L(); CLK_DELAY();
*buff++ = r; /* Store a received byte */
DLY_US(10);
//snprintf(buf, sizeof buf, "received 0x%02x\r\n", r);
//UART_1_PutString(buf);
} while (--bc);
}
/*-----------------------------------------------------------------------*/
/* Wait for card ready */
/*-----------------------------------------------------------------------*/
static
int wait_ready (void) /* 1:OK, 0:Timeout */
{
BYTE d;
UINT tmr;
for (tmr = 5000; tmr; tmr--) { /* Wait for ready in timeout of 500ms */
rcvr_mmc(&d, 1);
if (d == 0xFF) return 1;
DLY_US(100);
}
return 0;
}
/*-----------------------------------------------------------------------*/
/* Deselect the card and release SPI bus */
/*-----------------------------------------------------------------------*/
static
void deselect (void)
{
BYTE d;
CS_H();
rcvr_mmc(&d, 1);
}
/*-----------------------------------------------------------------------*/
/* Select the card and wait for ready */
/*-----------------------------------------------------------------------*/
static
int select (void) /* 1:OK, 0:Timeout */
{
CS_L();
if (!wait_ready()) {
deselect();
return 0;
}
return 1;
}
/*-----------------------------------------------------------------------*/
/* Receive a data packet from MMC */
/*-----------------------------------------------------------------------*/
static
int rcvr_datablock ( /* 1:OK, 0:Failed */
BYTE *buff, /* Data buffer to store received data */
UINT btr /* Byte count */
)
{
BYTE d[2];
UINT tmr;
for (tmr = 1000; tmr; tmr--) { /* Wait for data packet in timeout of 100ms */
rcvr_mmc(d, 1);
if (d[0] != 0xFF) break;
DLY_US(100);
}
if (d[0] != 0xFE)
return 0; /* If not valid data token, retutn with error */
rcvr_mmc(buff, btr); /* Receive the data block into buffer */
rcvr_mmc(d, 2); /* Discard CRC */
return 1; /* Return with success */
}
/*-----------------------------------------------------------------------*/
/* Send a data packet to MMC */
/*-----------------------------------------------------------------------*/
static
int xmit_datablock ( /* 1:OK, 0:Failed */
const BYTE *buff, /* 512 byte data block to be transmitted */
BYTE token /* Data/Stop token */
)
{
BYTE d[2];
if (!wait_ready())
return 0;
d[0] = token;
xmit_mmc(d, 1); /* Transmit a token */
if (token != 0xFD) { /* Is it data token? */
xmit_mmc(buff, 512); /* Transmit the 512 byte data block to MMC */
rcvr_mmc(d, 2); /* Dummy CRC (FF,FF) */
rcvr_mmc(d, 1); /* Receive data response */
if ((d[0] & 0x1F) != 0x05) /* If not accepted, return with error */
return 0;
}
return 1;
}
/*-----------------------------------------------------------------------*/
/* Send a command packet to MMC */
/*-----------------------------------------------------------------------*/
static
BYTE send_cmd ( /* Returns command response (bit7==1:Send failed)*/
BYTE cmd, /* Command byte */
DWORD arg /* Argument */
)
{
BYTE n, d, buf[6];
if (cmd & 0x80) { /* ACMD<n> is the command sequense of CMD55-CMD<n> */
cmd &= 0x7F;
n = send_cmd(CMD55, 0);
if (n > 1) return n;
}
/* Select the card and wait for ready */
deselect();
if (!select()) {
return 0xFF;
}
/* Send a command packet */
buf[0] = 0x40 | cmd; /* Start + Command index */
buf[1] = (BYTE)(arg >> 24); /* Argument[31..24] */
buf[2] = (BYTE)(arg >> 16); /* Argument[23..16] */
buf[3] = (BYTE)(arg >> 8); /* Argument[15..8] */
buf[4] = (BYTE)arg; /* Argument[7..0] */
n = 0x01; /* Dummy CRC + Stop */
if (cmd == CMD0)
n = 0x95; /* (valid CRC for CMD0(0)) */
if (cmd == CMD8)
n = 0x87; /* (valid CRC for CMD8(0x1AA)) */
buf[5] = n;
xmit_mmc(buf, 6);
/* Receive command response */
if (cmd == CMD12)
rcvr_mmc(&d, 1); /* Skip a stuff byte when stop reading */
n = 10; /* Wait for a valid response in timeout of 10 attempts */
do
rcvr_mmc(&d, 1);
while ((d & 0x80) && --n);
return d; /* Return with the response value */
}
/*--------------------------------------------------------------------------
Public Functions
---------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------*/
/* Get Disk Status */
/*-----------------------------------------------------------------------*/
DSTATUS disk_status (
BYTE drv /* Drive number (0) */
)
{
DSTATUS s = Stat;
if (drv || !INS) {
s = STA_NODISK | STA_NOINIT;
} else {
s &= ~STA_NODISK;
if (WP)
s |= STA_PROTECT;
else
s &= ~STA_PROTECT;
}
Stat = s;
return s;
}
/*-----------------------------------------------------------------------*/
/* Initialize Disk Drive */
/*-----------------------------------------------------------------------*/
DSTATUS disk_initialize (
BYTE drv /* Physical drive nmuber (0) */
)
{
BYTE n, ty, cmd, buf[4];
UINT tmr;
DSTATUS s;
INIT_PORT(); /* Initialize control port */
s = disk_status(drv); /* Check if card is in the socket */
if (s & STA_NODISK) return s;
CS_H();
for (n = 10; n; n--) rcvr_mmc(buf, 1); /* 80 dummy clocks */
ty = 0;
if (send_cmd(CMD0, 0) == 1) { /* Enter Idle state */
if (send_cmd(CMD8, 0x1AA) == 1) { /* SDv2? */
rcvr_mmc(buf, 4); /* Get trailing return value of R7 resp */
if (buf[2] == 0x01 && buf[3] == 0xAA) { /* The card can work at vdd range of 2.7-3.6V */
for (tmr = 1000; tmr; tmr--) { /* Wait for leaving idle state (ACMD41 with HCS bit) */
if (send_cmd(ACMD41, 1UL << 30) == 0) break;
DLY_US(1000);
}
if (tmr && send_cmd(CMD58, 0) == 0) { /* Check CCS bit in the OCR */
rcvr_mmc(buf, 4);
ty = (buf[0] & 0x40) ? CT_SD2 | CT_BLOCK : CT_SD2; /* SDv2 */
}
}
} else { /* SDv1 or MMCv3 */
if (send_cmd(ACMD41, 0) <= 1) {
ty = CT_SD1; cmd = ACMD41; /* SDv1 */
} else {
ty = CT_MMC; cmd = CMD1; /* MMCv3 */
}
for (tmr = 1000; tmr; tmr--) { /* Wait for leaving idle state */
if (send_cmd(ACMD41, 0) == 0) break;
DLY_US(1000);
}
if (!tmr || send_cmd(CMD16, 512) != 0) /* Set R/W block length to 512 */
ty = 0;
}
}
else {
UART_1_PutString("send CMD 0 failed");
}
CardType = ty;
if (ty) /* Initialization succeded */
s &= ~STA_NOINIT;
else /* Initialization failed */
s |= STA_NOINIT;
Stat = s;
deselect();
return s;
}
/*-----------------------------------------------------------------------*/
/* Read Sector(s) */
/*-----------------------------------------------------------------------*/
DRESULT disk_read (
BYTE drv, /* Physical drive nmuber (0) */
BYTE *buff, /* Pointer to the data buffer to store read data */
DWORD sector, /* Start sector number (LBA) */
BYTE count /* Sector count (1..128) */
)
{
DSTATUS s;
s = disk_status(drv);
if (s & STA_NOINIT) return RES_NOTRDY;
if (!count) return RES_PARERR;
if (!(CardType & CT_BLOCK)) sector *= 512; /* Convert LBA to byte address if needed */
if (count == 1) { /* Single block read */
if ((send_cmd(CMD17, sector) == 0) /* READ_SINGLE_BLOCK */
&& rcvr_datablock(buff, 512))
count = 0;
}
else { /* Multiple block read */
if (send_cmd(CMD18, sector) == 0) { /* READ_MULTIPLE_BLOCK */
do {
if (!rcvr_datablock(buff, 512)) break;
buff += 512;
} while (--count);
send_cmd(CMD12, 0); /* STOP_TRANSMISSION */
}
}
deselect();
return count ? RES_ERROR : RES_OK;
}
/*-----------------------------------------------------------------------*/
/* Write Sector(s) */
/*-----------------------------------------------------------------------*/
DRESULT disk_write (
BYTE drv, /* Physical drive nmuber (0) */
const BYTE *buff, /* Pointer to the data to be written */
DWORD sector, /* Start sector number (LBA) */
BYTE count /* Sector count (1..128) */
)
{
DSTATUS s;
s = disk_status(drv);
if (s & STA_NOINIT) return RES_NOTRDY;
if (s & STA_PROTECT) return RES_WRPRT;
if (!count) return RES_PARERR;
if (!(CardType & CT_BLOCK)) sector *= 512; /* Convert LBA to byte address if needed */
if (count == 1) { /* Single block write */
if ((send_cmd(CMD24, sector) == 0) /* WRITE_BLOCK */
&& xmit_datablock(buff, 0xFE))
count = 0;
}
else { /* Multiple block write */
if (CardType & CT_SDC) send_cmd(ACMD23, count);
if (send_cmd(CMD25, sector) == 0) { /* WRITE_MULTIPLE_BLOCK */
do {
if (!xmit_datablock(buff, 0xFC)) break;
buff += 512;
} while (--count);
if (!xmit_datablock(0, 0xFD)) /* STOP_TRAN token */
count = 1;
}
}
deselect();
return count ? RES_ERROR : RES_OK;
}
/*-----------------------------------------------------------------------*/
/* Miscellaneous Functions */
/*-----------------------------------------------------------------------*/
DRESULT disk_ioctl (
BYTE drv, /* Physical drive nmuber (0) */
BYTE ctrl, /* Control code */
void *buff /* Buffer to send/receive control data */
)
{
DRESULT res;
BYTE n, csd[16];
WORD cs;
if (disk_status(drv) & STA_NOINIT) /* Check if card is in the socket */
return RES_NOTRDY;
res = RES_ERROR;
switch (ctrl) {
case CTRL_SYNC : /* Make sure that no pending write process */
if (select()) {
deselect();
res = RES_OK;
}
break;
case GET_SECTOR_COUNT : /* Get number of sectors on the disk (DWORD) */
if ((send_cmd(CMD9, 0) == 0) && rcvr_datablock(csd, 16)) {
if ((csd[0] >> 6) == 1) { /* SDC ver 2.00 */
cs= csd[9] + ((WORD)csd[8] << 8) + 1;
*(DWORD*)buff = (DWORD)cs << 10;
} else { /* SDC ver 1.XX or MMC */
n = (csd[5] & 15) + ((csd[10] & 128) >> 7) + ((csd[9] & 3) << 1) + 2;
cs = (csd[8] >> 6) + ((WORD)csd[7] << 2) + ((WORD)(csd[6] & 3) << 10) + 1;
*(DWORD*)buff = (DWORD)cs << (n - 9);
}
res = RES_OK;
}
break;
case GET_BLOCK_SIZE : /* Get erase block size in unit of sector (DWORD) */
*(DWORD*)buff = 128;
res = RES_OK;
break;
default:
res = RES_PARERR;
}
deselect();
return res;
}
最佳答案
一般情况下,多张SD卡连接到同一接口(interface)线上时,可以加上拉电阻。特别是,当您取消选择并选择另一张卡时,DI、DO 和 CLK 信号会变为 float 。
否则,我可以想象它也会影响主机和从机之间的轨道长度,在本例中为 microSD 卡。
我在您在 Internet 上找到的许多示例电路中都看到了上拉电阻。
关于C - 在 SPI 模式下初始化 SD 卡,始终读回 0xFF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5048450/
我的内部类如下: public class ClassWithInnerObject { private final InnerObject innerObject; public Class
我在 STM32F4 Discovery 上安装了 STM32F4407VGT6 Controller 。我尝试使用 SPI + DMA 从 AD7683 ADC 读取数据,但 DMA 接收缓冲区始终
假设您有一个只有一个片选的 SPI 总线。 是否有可以将 8 个或更多设备连接到该 SPI 总线的芯片? 为简化起见,您可以假设所有设备都同意 SPI 模式(数据需要在上升沿有效)。此外,所有设备都是
我有一个 32 GB 的金士顿 SDHC microSD 卡,它必须与 MSP430F2618 通信通过 SPI .我无法通过使用 CMD55 + ACMD41(bit30 设置为 1)来初始化它,如
我正在使用 chai-spies 来确保调用 Controller 中的函数,这是我的测试: it('Should show right season and analysts when compet
我刚开始进行 JS 单元测试,但很难理解如何使用 Jasmine spy 创建有意义的测试。 it('should take an array of shopping items', function
我一直在阅读 SPI 以及如何创建内核驱动程序,但我仍然不确定所有这些是如何工作的。 例如: static struct spi_driver ds1305_driver = {
这是我正在测试的代码 eventsApp.factory('userData', ['userResource', function(userResource){ return{ ge
我有一个简单的组件: .html: {{title}} Change title .ts: export class AppComponent { title = 'app works!'
当我从 SPI 总线上的 PIC-18F4520 向我的卡发出地址为 (0x00000000) 的 cmd17 时,我从命令问题中获得了正确的返回 R1 token 。然后,经过几次循环检查后,我从发
我正在使用rspec-spies我想知道在打完所有电话后是否有办法检查 spy 。 例如,如果我做类似的事情 # setup Post.stub(:find).with(@post.id).and_r
我正在使用 rspec-spies并且想知道是否有办法在所有电话都打完后检查 spy 。 例如,如果我做类似的事情 # setup Post.stub(:find).with(@post.id).an
我正在尝试制作一个可以点击另一个程序按钮的程序。我被告知我需要使用 spy++ 来获取我想要点击的按钮的 ID,所以我现在正在使用它。我找到了包含我希望从中获取按钮 ID 的按钮的窗口(窗口中有 3
问题 在我们的代码库中,我们有一个 sinon 问题,可以使用下面的代码片段重现。问题是,它似乎是间接调用的 spy 返回力 false,console.log 明确指出该方法被调用但 spy.cal
考虑以下 react 组件。 import React, { Component } from "react"; import { reduxForm, Field } from "redux-for
我正在开发一个 Linux spi 驱动程序来处理通过 SPI 端口的通信。 我的 SoC 提供了三个 spi 模块(我将其理解为端口),称为 ecspi1/ecspi2/ecspi3。 我需要使用
当我将类的方法包装成这样的 Sinon-spy 时: sinon.spy(myObject, "myMethod") spy 内部会发生什么? 我猜 spy 对象有一个指向“myObject.myMe
我正在为遗留代码编写一些 JUnit 测试,并且我非常喜欢使用注释。我想知道是否可以创建一个 spy 对象的声明,然后实例化它。我问的原因是因为我有一个带有非空构造函数的类。该构造函数的值直到测试用例
我有两个不同的设备要连接 Arduino .一个Ethernet屏蔽和轴编码器。第一个有 SPI模式 0 和第二个 SPI 模式 2。它们冲突。这个问题有解决方案吗? 我使用不同的芯片选择引脚,这两个
我正在运行使用 Yocto (Pyro) 构建的嵌入式 Linux (4.14.16)。我在具有 i.MX6DL 且 SPI 连接到 FPGA(Xilinx Artix 7)的定制板上运行。我目前正在
我是一名优秀的程序员,十分优秀!