现在的现象,自己做的板子,使用的是STM32L476这款芯片,通过SPI接口控制MB85铁电存储器,用示波器观察MOSI,CS,CLK,MISO引脚,发现CS、MOSI、CLK这三个引脚的波形图都正确,但是MISO引脚却始终为低电平。[mw_shl_code=applescript,true]#define MB85_WREN_INST 0x06 //éèÖÃD′ê1ÄüËø′æÆ÷ //MB85RS256A?????
#define MB85_WRDI_INST 0x04 //D′½ûÖ1
#define MB85_RDSR_INST 0x05 //¶á×′쬼Ä′æÆ÷
#define MB85_WRSR_INST 0x01 //D′×′쬼Ä′æÆ÷
#define MB85_READ_INST 0x03 //¶á′æ′¢Æ÷êy¾Y
#define MB85_WRITE_INST 0x02 //D′′æ′¢Æ÷êy¾Y
#define MB85_STATUS_REG 0x0 //
#define MB85_INIT_STATE 0x09 //
#define MB85_RDID_INST 0x9F //¶áÆ÷¼tID
#define MB_CS_L HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_RESET)
#define MB_CS_H HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_SET)
/* USER CODE END 0 */
SPI_HandleTypeDef hspi1;
/* SPI1 init function */
void MX_SPI1_Init(void)
{
hspi1.Instance = SPI1;
hspi1.Init.Mode = SPI_MODE_MASTER;
hspi1.Init.Direction = SPI_DIRECTION_2LINES;
hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;
hspi1.Init.CLKPhase = SPI_PHASE_2EDGE;
hspi1.Init.NSS = SPI_NSS_SOFT;
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi1.Init.CRCPolynomial = 7;
hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
hspi1.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
if (HAL_SPI_Init(&hspi1) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
}
void HAL_SPI_MspInit(SPI_HandleTypeDef* spiHandle)
{
GPIO_InitTypeDef GPIO_InitStruct;
if(spiHandle->Instance==SPI1)
{
/* USER CODE BEGIN SPI1_MspInit 0 */
__HAL_RCC_GPIOB_CLK_ENABLE();
/* USER CODE END SPI1_MspInit 0 */
/* SPI1 clock enable */
__HAL_RCC_SPI1_CLK_ENABLE();
/**SPI1 GPIO Configuration
PB3 (JTDO-TRACESWO) ------> SPI1_SCK
PB4 (NJTRST) ------> SPI1_MISO
PB5 ------> SPI1_MOSI
*/
GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* USER CODE BEGIN SPI1_MspInit 1 */
// GPIO_InitStruct.Pin = GPIO_PIN_4;
// GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
// GPIO_InitStruct.Pull = GPIO_NOPULL;
// GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
// GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
// HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,GPIO_PIN_SET);
GPIO_InitStruct.Pin=GPIO_PIN_1;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
HAL_GPIO_Init(GPIOB,&GPIO_InitStruct);
/* USER CODE END SPI1_MspInit 1 */
}
}
void HAL_SPI_MspDeInit(SPI_HandleTypeDef* spiHandle)
{
if(spiHandle->Instance==SPI1)
{
/* USER CODE BEGIN SPI1_MspDeInit 0 */
__HAL_RCC_GPIOB_CLK_DISABLE();
/* USER CODE END SPI1_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_SPI1_CLK_DISABLE();
/**SPI1 GPIO Configuration
PB3 (JTDO-TRACESWO) ------> SPI1_SCK
PB4 (NJTRST) ------> SPI1_MISO
PB5 ------> SPI1_MOSI
*/
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5);
/* USER CODE BEGIN SPI1_MspDeInit 1 */
/* USER CODE END SPI1_MspDeInit 1 */
}
}
/* USER CODE BEGIN 1 */
uint8_t SPI_FRAM_SendByte(uint8_t byte)
{
uint8_t d_read,d_send=byte;
if (HAL_SPI_TransmitReceive(&hspi1,&d_send,&d_read,1,0xFFFFFF)!=HAL_OK)
d_read=0xff;
return d_read;
}
void SPI_FRAM_BufferRead(uint8_t* pBuffer, uint16_t ReadAddr, uint16_t NumByteToRead)
{
/* Ñ¡ÔñFRAM: CS μíμçƽ*/
MB_CS_L;
/* ·¢Ëí¶áÖ¸áî */
SPI_FRAM_SendByte(MB85_READ_INST);
/* ·¢Ëí¶áμØÖ·¸ßλ */
// SPI_FRAM_SendByte((ReadAddr& 0xFF0000) >> 16);
SPI_FRAM_SendByte((ReadAddr& 0x00FF00) >> 8);
/* ·¢Ëí¶áμØÖ·μíλ */
SPI_FRAM_SendByte(ReadAddr & 0x0000FF);
while (NumByteToRead--) { /* ¶áè¡êy¾Y */
*pBuffer = SPI_FRAM_SendByte(0x00);
/* Ö¸ÏòÏÂò»¸ö×Ö½ú»o3åÇø */
pBuffer++;
}
/* ½ûÖ1FRAM: CS ¸ßμçƽ */
MB_CS_H;
}
void SPI_FRAM_BufferWrite(uint8_t* pBuffer, uint16_t WriteAddr, uint16_t NumByteToWrite)
{
/* Ñ¡ÔñFRAM: CS μíμçƽ*/
MB_CS_L;
/* ·¢Ëí¶áÖ¸áî */
SPI_FRAM_SendByte(MB85_WRITE_INST);
/* ·¢Ëí¶áμØÖ·¸ßλ */
// SPI_FRAM_SendByte((WriteAddr& 0xFF0000) >> 16);
SPI_FRAM_SendByte((WriteAddr& 0x00FF00) >> 8);
/* ·¢Ëí¶áμØÖ·μíλ */
SPI_FRAM_SendByte(WriteAddr & 0x0000FF);
while (NumByteToWrite--) { /* ¶áè¡êy¾Y */
SPI_FRAM_SendByte(*pBuffer++);
/* Ö¸ÏòÏÂò»¸ö×Ö½ú»o3åÇø */
}
/* ½ûÖ1 FRAM: CS ¸ßμçƽ */
MB_CS_H;
}
uint8_t SPI_read_MB85(uint32_t address)//¶áè¡ìض¨μØÖ·μÄêy¾Y
{
uint8_t dat,addr_tempH,addr_tempL,addr_tempM;
addr_tempH = (uint8_t)((address&0xff0000)>>16);
addr_tempM = (uint8_t)((address&0x00ff00)>>8);
addr_tempL = (uint8_t)(address&0x0000ff);
MB_CS_L;
SPI_FRAM_SendByte(MB85_READ_INST); //¶á′æ′¢Æ÷¼Ä′æÆ÷2ù×÷Âë
SPI_FRAM_SendByte(addr_tempH);
SPI_FRAM_SendByte(addr_tempM);
SPI_FRAM_SendByte(addr_tempL);
dat=SPI_FRAM_SendByte(0xaa); //¶áè¡êy¾Y,0xAA¸øóè¶áè¡êy¾YËùDèμÄê±Öó
MB_CS_H;
return (dat);
}
void SPI_write_MB85(uint8_t data,uint32_t address)//D′ò»¸ö×Ö½úμ½ìض¨μÄμØÖ·¿Õ¼ä
{
uint8_t addr_tempH,addr_tempL,addr_tempM;
addr_tempH = (uint8_t)((address&0xff0000)>>16);
addr_tempM = (uint8_t)((address&0x00ff00)>>8); //»ñ衸ß8λμØÖ·
addr_tempL = (uint8_t)(address&0x0000ff); //»ñè¡μí8λμØÖ·
MB_CS_L;
SPI_FRAM_SendByte(MB85_WRITE_INST); //D′ê1Äü
SPI_FRAM_SendByte(addr_tempH); //D′èëμØÖ·
SPI_FRAM_SendByte(addr_tempM);
SPI_FRAM_SendByte(addr_tempL); //
SPI_FRAM_SendByte(data); //D′èëêy¾Y
MB_CS_H;
}[/mw_shl_code]
main函数
[mw_shl_code=applescript,true]while (1)
{
SPI_write_MB85(0x19,0x000011);
value=SPI_read_MB85(0x000011);
if(value==0x19)
{
HAL_GPIO_WritePin(GPIOA,GPIO_PIN_12,GPIO_PIN_SET);
}
HAL_Delay(10);
}[/mw_shl_code]
绿 {MOD}为MOSI,橙 {MOD}为SCK
这是一个方面,还有可能是初始化有问题,时序不对,比如高低电平,相位之类的。
一周热门 更多>