STM8L15X+FM11Nc08(SPI接口)进行读写时串口打印的数据与发送数据不一致是乱码,希望又大神可以帮忙看看,找找原因,我把SPI驱...

2019-07-19 20:10发布

#include "spi.h"
#include "string.h"
#define CS_DELAY  35
#define _FM11_Sel()  {GPIO_ResetBits(FM11_CS,FM11_Pin_CS);}//拉低电平
#define _FM11_DeSel()  {GPIO_SetBits(FM11_CS,FM11_Pin_CS);}//拉高电平
void  FM11_InitPort(void)
{
   
   
   GPIO_Init(FM11_SCK , FM11_Pin_SCK, GPIO_Mode_Out_PP_High_Fast);
    GPIO_Init(FM11_MOSI , FM11_Pin_MOSI, GPIO_Mode_Out_PP_High_Fast);
    GPIO_Init(FM11_MISO , FM11_Pin_MISO, GPIO_Mode_In_PU_No_IT);

CLK_PeripheralClockConfig(CLK_Peripheral_SPI1,ENABLE);
GPIO_ExternalPullUpConfig(FM11_MISO , FM11_Pin_MISO,ENABLE);
SPI_Init(SPI1,SPI_FirstBit_MSB,SPI_BaudRatePrescaler_16,SPI_Mode_Master,SPI_CPOL_Low,SPI_CPHA_2Edge,SPI_Direction_2Lines_FullDuplex,SPI_NSS_Soft,0x07);
    SPI_Cmd(SPI1,ENABLE);
GPIO_Init(FM11_CS , FM11_Pin_CS, GPIO_Mode_Out_PP_High_Fast);
_FM11_DeSel();

}

/*********************************************************************************************************
** oˉêy??3?: FM11_CS_ON
** oˉêy1|?ü: FM11μ?spi?ú????ON
** ê?è?2?êy:    ?T
** ê?3?2?êy:    ?T
** ·μ???μ:      ?T
*********************************************************************************************************/
void FM11_CS_ON(void)
{
//disableInterrupts();
_FM11_Sel();  
Delay_us(120);   
}
/*********************************************************************************************************
** oˉêy??3?: FM11_CS_OFF
** oˉêy1|?ü: FM11μ?spi?ú????OFF
** ê?è?2?êy:    ?T
** ê?3?2?êy:    ?T
** ·μ???μ:      ?T
*********************************************************************************************************/
void FM11_CS_OFF(void)
{
_FM11_DeSel()
//enableInterrupts();
}

/**********************************************************************************************************
** oˉêy??3?£oFM11?áD′ò???×??ú(SPI)
**********************************************************************************************************/
uint8_t FM11_SPI_SingleRW(uint8_t wData)
{
//·¢?íêy?Y
while (SPI_GetFlagStatus(SPI1, SPI_FLAG_TXE) == RESET);
    SPI_SendData(SPI1,wData);
  
//while (SPI_GetFlagStatus(SPI1, SPI_FLAG_BSY) == SET);
while (SPI_GetFlagStatus(SPI1, SPI_FLAG_RXNE) == RESET);
//?óê?êy?Y
return SPI_ReceiveData(SPI1);
}

/**********************************************************************************************************
** oˉêy??3?£oFM11 D′?à??×??ú <=16??×??ú
**********************************************************************************************************/
void SPI_WriteByte(uint8_t *pData, uint32_t length)
{

  for(; length>0; length--)
{
  FM11_SPI_SingleRW(*pData);
  pData++;
}

}
/**********************************************************************************************************
** oˉêy??3?£oFM11?á?à??×??ú <=16??×??ú
**********************************************************************************************************/
void  SPI_readByte(uint8_t *pData, uint32_t length)
{

  while(length--)
  {
       *pData = FM11_SPI_SingleRW(0xFF);
          pData++;
  }
   
}

/*********************************************************************************************************
** oˉêy??3?: FM11_WriteE2Enable
** oˉêy1|?ü: FM11μ?spi?úE2D′ê1?ü
** ê?è?2?êy:    ?T
** ê?3?2?êy:    ?T
** ·μ???μ:      ?T
*********************************************************************************************************/
void FM11_WriteE2Enable(void)
{
FM11_CS_ON();
Delay_us(CS_DELAY);   //?óê±è·±£FM11é?μ?íê3é

FM11_SPI_SingleRW(0xCE); //ê1?üE2ì?êa??á?£??è·¢?íCE
    FM11_SPI_SingleRW(0x55); //ê1?üE2ì?êa??á?£??ù·¢?í55£?????μμμú30ò3

FM11_CS_OFF();
}
/*********************************************************************************************************
** oˉêy??3?: FM11_WriteE2Disable
** oˉêy1|?ü: FM11μ?spi?úE2D′???1
** ê?è?2?êy:    ?T
** ê?3?2?êy:    ?T
** ·μ???μ:      ?T
*********************************************************************************************************/
void FM11_WriteE2Disable(void)
{
FM11_CS_ON();
Delay_us(CS_DELAY);   //?óê±è·±£FM11é?μ?íê3é

FM11_SPI_SingleRW(0xCE); //ê§?üE2ì?êa??á?
FM11_SPI_SingleRW(0xAA); //ê§?üE2ì?êa??á?

FM11_CS_OFF();
}

/*********************************************************************************************************
** oˉêy??3?: FM11_WriteReg
** oˉêy1|?ü: D′FM11??′??÷
** ê?è?2?êy: reg:??′??÷μ??·
**      val′è?μ?2?êy
** ê?3?2?êy:    ?T
** ·μ???μ:      ?T
*********************************************************************************************************/
void FM11_WriteReg(uint8_t reg,uint8_t val)
{
uint8_t mode;
mode = reg & 0x0F;
FM11_CS_ON();
    Delay_us(35);
FM11_SPI_SingleRW(mode); //??á???ê?2áμú28ò3
FM11_SPI_SingleRW(val);
FM11_CS_OFF();
//  Delay_ms(100); //′?′|2??ü?óê±£?Dèòa?êò?????1¤£?ò??óê±??′??÷2ù×÷?í2??y3££?2??aμà?aê2?′
}

/*********************************************************************************************************
** oˉêy??3?: FM11_ReadReg
** oˉêy1|?ü: ?á??′??÷?μ
** ê?è?2?êy:    reg:??′??÷μ??·
** ê?3?2?êy:    ?T
** ·μ???μ:      val,?á3?μ???′??÷?μ
*********************************************************************************************************/
uint8_t FM11_ReadReg(uint8_t reg)
{
uint8_t mode;
uint8_t val;
mode = reg&0x0F; //??á???ê?2áμú28ò3
mode = mode | 0x20; //??á???ê?2áμú28ò3
FM11_CS_ON();
    FM11_SPI_SingleRW(mode);
val = FM11_SPI_SingleRW(0xFF);
FM11_CS_OFF();
return val;
}
/*********************************************************************************************************
** oˉêy??3?: FM11_WriteE2_Int
** oˉêy1|?ü: D′E2êy?Y
** ê?è?2?êy:    adr:E2μ??·
** len′è?μ?êy?Y3¤?è(<=16)
** *ibuf′è?μ?êy?Y
** ê?3?2?êy:    ?T
** ·μ???μ:      ?T
*********************************************************************************************************/
void FM11_WriteE2_Int(uint16_t adr,uint32_t len,uint8_t *ibuf)
{
uint8_t buf[2];
  buf[0] = (adr >> 8) & 0x03| 0x40; //′?′|è·è?ò???£?Dè2?Dèòa?ù′?·¢?íD′eeμ?μúò???×??úμ?ì?êa??á?
  buf[1] = adr & 0xFF;

//  printf("%s: adr = 0x%3x, len = %d, ibuf[0] = 0x%2x ", __func__, adr, len, ibuf[0]);
FM11_CS_ON();
Delay_us(100);
FM11_SPI_SingleRW(buf[0]);
FM11_SPI_SingleRW(buf[1]); //E2μ??·?a10bit£??a??×??ú????8bit£??1óDá????úaddr0
SPI_WriteByte(ibuf,len);
FM11_CS_OFF();
  Delay_ms(20);          //′?′|±?D?òa?ó?óê±£???ê?2áμú30ò3
}
/*********************************************************************************************************
** oˉêy??3?: FM11_WriteE2
** oˉêy1|?ü: D′E2êy?Y
** ê?è?2?êy: adr:E2μ??·
**      len′è?μ?êy?Y3¤?è?éò?′óóú16×??ú
**      *ibuf′è?μ?êy?Y
** ê?3?2?êy:    ?T
** ·μ???μ:      ?T
*********************************************************************************************************/
void FM11_WriteE2(uint16_t adr,uint32_t len,uint8_t *wbuf)
{
uint8_t offset;
FM11_WriteE2Enable();

//  printf("%s: adr = 0x%3x, len = %d, wbuf[0] = 0x%2x ", __func__, adr, len, wbuf[0]);

  if(adr < FM11_E2_USER_ADDR || adr >= FM11_E2_MANUF_ADDR)
     return;
  if(adr % FM11_E2_BLOCK_SIZE)
     {
        offset = FM11_E2_BLOCK_SIZE - (adr % FM11_E2_BLOCK_SIZE);
         if(len > offset)
              {
         FM11_WriteE2_Int(adr,offset,wbuf);
         adr += offset;
         wbuf += offset;
         len -= offset;
       }
         else
              {
                FM11_WriteE2_Int(adr,len,wbuf);
        len = 0;
              }
     }
  while(len)
        {
           if(len >= FM11_E2_BLOCK_SIZE)
              {
                  FM11_WriteE2_Int(adr,FM11_E2_BLOCK_SIZE,wbuf);
                  adr += FM11_E2_BLOCK_SIZE;
                  wbuf += FM11_E2_BLOCK_SIZE;
                  len -= FM11_E2_BLOCK_SIZE;
      
              }
           else
              {
                 FM11_WriteE2_Int(adr,len,wbuf);
                 len = 0;
              }
         }  

}

/*********************************************************************************************************
**  oˉêy??3?: FM11_ReadE2
** oˉêy1|?ü: ?áè?μ?E2êy?Y
** ê?è?2?êy:  adr:E2μ??·
**      len:?áè?μ?êy?Y3¤?è£??éò?′óóú16??×??ú
**      *rbuf:?áè?μ?êy?Y
** ê?3?2?êy:    ?T
** ·μ???μ:      ?T
*********************************************************************************************************/
void FM11_ReadE2(uint16_t adr,uint32_t len,uint8_t *rbuf)
{
uint8_t buf[2];
//????à-μíoó£??è·¢?íò???×??úμ??üá?±à??0X63£???′??ú·¢?íò???×??úμ?μ??·
  buf[0] = ((adr >> 8) & 0x03)| 0x60; //??á???ê?2áμú28ò3
  buf[1] = adr & 0xFF;

FM11_CS_ON();
Delay_us(CS_DELAY);   //?óê±è·±£FM11é?μ?íê3é
FM11_SPI_SingleRW(buf[0]);
FM11_SPI_SingleRW(buf[1]);
SPI_readByte(rbuf,len);
FM11_CS_OFF();
}

/*********************************************************************************************************
** oˉêy??3?: FM11_WriteFIFO
** oˉêy1|?ü: D′FIFO
** ê?è?2?êy:    ilen′êy?Y3¤?è(<=32 fifo????)
**      *ibuf′μ?êy?Y
** ê?3?2?êy:    ?T
** ·μ???μ:      ?T
*********************************************************************************************************/
void FM11_WriteFIFO(uint8_t *ibuf,uint32_t ilen)
{
FM11_CS_ON();

    FM11_SPI_SingleRW(0x80);

SPI_WriteByte(ibuf,ilen);
FM11_CS_OFF();
}
/*********************************************************************************************************
** oˉêy??3?: ReadFIFO
** oˉêy1|?ü: ·¢?íNAK??
** ê?è?2?êy:    *ilen:′y?áè?μ?êy?Y3¤?è
** ê?3?2?êy:    *rbuf:?áè?μ?êy?Y
** ·μ???μ:      ?áè?μ?êy?Y3¤?è
*********************************************************************************************************/
void FM11_ReadFIFO(uint32_t ilen,uint8_t *rbuf)
{
FM11_CS_ON();
FM11_SPI_SingleRW(0xA0);
SPI_readByte(rbuf,ilen);
FM11_CS_OFF();
}

/*********************************************************************************************************
** oˉêy??3?: FM11_Init
** oˉêy1|?ü: FM11μ?spi?ú3?ê??ˉ3ìDò
** ê?è?2?êy:    ?T
** ê?3?2?êy:    ?T
** ·μ???μ:      ?T
*********************************************************************************************************/
void FM11_Init(void)
{
  FM11_InitPort();
  FM11_CS_OFF();
//  FM11_WriteReg(MAIN_IRQ_MASK,0x44); //MAIN?D??????;?óê??aê??D??
//   FM11_WriteReg(FIFO_IRQ_MASK,0x07); //FIFO?D??????
//  FM11_WriteReg(NFC_CFG,0x03); //D′NFC????×?(??è??§3?-4)
FM11_WriteReg(FIFO_FLUSH,0xFF);  //??fifo??′??÷
}






#ifndef __SPI_H_
#define __SPI_H_
#include "systick.h"
#define  FM11_E2_BLOCK_SIZE  16
//&Ograve;&Ocirc;&Iuml;&Acirc;E2&Auml;&Uacute;°ü&ordm;&not;&Oacute;&Atilde;&raquo;§&Ecirc;&yacute;&frac34;&Yacute;&ordm;&Iacute;&sup3;&iexcl;&Auml;&Uuml;&Aacute;&iquest;&para;&Ocirc;&Iacute;&acirc;&sup1;&copy;&micro;&ccedil;&Aring;&auml;&Ouml;&Atilde;
#define  FM11_E2_USER_ADDR   0x000C
#define  FM11_E2_MANUF_ADDR  0x039F
#define FIFO_ACCESS  0x00  //MCU&para;&Aacute;&ETH;&acute;FIFO&micro;&Auml;&Egrave;&euml;&iquest;&Uacute;&micro;&Oslash;&Ouml;·&pound;&not;SPI&para;&Aacute;&ETH;&acute;FIFO&Ecirc;&sup1;&Oacute;&Atilde;ר&Oacute;&Atilde;&Ouml;&cedil;&Aacute;&icirc;&Acirc;&euml;
#define FIFO_FLUSH  0x01  //&sup1;&copy;MCU&Ccedil;&aring;&iquest;&Otilde;FIFO&Auml;&Uacute;&Egrave;&Yacute;
#define FIFO_WORDCNT 0x02  //&sup1;&copy;MCU&sup2;é&Ntilde;&macr;FIFO&Ecirc;&pound;&Oacute;à×&Ouml;&frac12;&Uacute;
#define RF_STATUS  0x03  //·&Ccedil;&frac12;&Oacute;&acute;&yen;&iquest;&Uacute;&sup1;¤×÷×&acute;&Igrave;&not;
#define RF_TXEN   0x04  //FIFO·&Atilde;&Icirc;&Ecirc;&frac14;&Auml;&acute;&aelig;&AElig;÷
#define RF_BAUD   0x05  //·&Ccedil;&frac12;&Oacute;&Iacute;¨&ETH;&Aring;&sup2;¨&Igrave;&Oslash;&Acirc;&Ecirc;&Ntilde;&iexcl;&Ocirc;&ntilde;
#define RF_RATS   0x06  //·&Ccedil;&frac12;&Oacute;&Iacute;¨&ETH;&Aring;&Ecirc;&Otilde;&micro;&frac12;&micro;&Auml;RATS&Ecirc;&yacute;&frac34;&Yacute;
#define MAIN_IRQ  0x07  //&Ouml;÷&Ouml;&ETH;&para;&Iuml;±ê&Ouml;&frac34;&frac14;&Auml;&acute;&aelig;&AElig;÷
#define FIFO_IRQ  0x08  //FIFO&Ouml;&ETH;&para;&Iuml;±ê&Ouml;&frac34;&frac14;&Auml;&acute;&aelig;&AElig;÷
#define AUX_IRQ   0x09  //&cedil;¨&Ouml;ú&Ouml;&ETH;&para;&Iuml;±ê&Ouml;&frac34;&frac14;&Auml;&acute;&aelig;&AElig;÷
#define MAIN_IRQ_MASK 0x0A  //&Ouml;÷&Ouml;&ETH;&para;&Iuml;&AElig;&Aacute;±&Icirc;&frac14;&Auml;&acute;&aelig;&AElig;÷
#define FIFO_IRQ_MASK 0x0B  //FIFO&Ouml;&ETH;&para;&Iuml;&AElig;&Aacute;±&Icirc;&frac14;&Auml;&acute;&aelig;&AElig;÷
#define AUX_IRQ_MASK 0x0C  //&cedil;¨&Ouml;ú&Ouml;&ETH;&para;&Iuml;&AElig;&Aacute;±&Icirc;&frac14;&Auml;&acute;&aelig;&AElig;÷
#define NFC_CFG   0x0D  //NFC&Aring;&auml;&Ouml;&Atilde;&frac14;&Auml;&acute;&aelig;&AElig;÷
#define REGU_CFG  0x0E  //Regulator&Aring;&auml;&Ouml;&Atilde;&frac14;&Auml;&acute;&aelig;&AElig;÷
//&Ograve;&Ocirc;&Iuml;&Acirc;&Ecirc;&Ccedil;&para;&Ocirc;&Ouml;÷&Ouml;&ETH;&para;&Iuml;±ê&Ouml;&frac34;&frac14;&Auml;&acute;&aelig;&AElig;÷(MAIN_IRQ)&Ouml;&ETH;&micro;&Auml;&Atilde;&iquest;&Ograve;&raquo;&Icirc;&raquo;&frac12;&oslash;&ETH;&ETH;&Eacute;è&Ouml;&Atilde;
#define MAIN_IRQ_RF_PWON 0x80
#define MAIN_IRQ_ACTIVE 0x40
#define MAIN_IRQ_RX_START 0x20
#define MAIN_IRQ_RX_DONE 0x10
#define MAIN_IRQ_TX_DONE 0x08
#define MAIN_IRQ_ARBIT 0x04
#define MAIN_IRQ_FIFO 0x02
#define MAIN_IRQ_AUX 0x01


void  FM11_InitPort(void);
void  FM11_CS_ON(void);
void  FM11_CS_OFF(void);
uint8_t FM11_SPI_SingleRW(uint8_t wData);
void  SPI_WriteByte(uint8_t *pData, uint32_t length);
void  SPI_readByte(uint8_t *pData, uint32_t length);
void  FM11_WriteE2Enable(void);
void  FM11_WriteE2Disable(void);
void  FM11_WriteReg(uint8_t reg,uint8_t val);
uint8_t FM11_ReadReg(uint8_t reg);
void FM11_WriteE2_Int(uint16_t adr,uint32_t len,uint8_t *ibuf);
void FM11_WriteE2(uint16_t adr,uint32_t len,uint8_t *wbuf);
void FM11_ReadE2(uint16_t adr,uint32_t len,uint8_t *rbuf);
void FM11_WriteFIFO(uint8_t *ibuf,uint32_t ilen);
void FM11_ReadFIFO(uint32_t ilen,uint8_t *rbuf);
void FM11_Init(void);

#endif

友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。