[活动专区] 【杰发科技AC7840x测评】+ SPI方式读U盘数据

[复制链接]
4832|0
 楼主| tlled 发表于 2023-12-16 11:06 | 显示全部楼层 |阅读模式
测试开发板的SPI,使用SPI方式连接CH376模块读取U盘数据。
一、SPI硬件

连接CH376模块
20231216_110148.jpg

二、程序部分

2.1、spi.c
  1. #include "ac7840x.h"
  2. #include "gpio_drv.h"
  3. #include "osif.h"
  4. #include "spi/spi.h"
  5. #include "spi_hw.h"
  6. #include "spi_master_drv.h"
  7. #include "spi_slave_drv.h"
  8. #include "port_hw.h"
  9. #include "gpio_hw.h"


  10. spi_master_config_t spiMasteConfig =
  11. {
  12.     .bitsPerSec = 2000000UL,                    /*! ÉèÖÃSPI²¨ÌØÂÊ*/
  13.     .whichPcs = SPI_PCS2,                       /*! ÉèÖÃSPIƬѡÒý½Å*/
  14.     .pcsPolarity = SPI_ACTIVE_LOW,              /*! ÉèÖÃSPIƬѡµÍÓÐЧ*/
  15.     .isPcsContinuous = true,                    /*! ÉèÖÃÆ¬Ñ¡Á¬ÐøÄ£Ê½*/
  16.     .bitcount = 8UL,                            /*! SPI Ö¡³¤¶È,Ö§³Ö 4~32 bits*/
  17.     .spiSrcClk = 60000000UL,                    /*! SPIʱÖÓԴƵÂÊ*/
  18.     .clkPhase = SPI_CLOCK_PHASE_1ST_EDGE,       /*! ÉèÖÃÊý¾Ý²ÉÑùÏàλ,µÚ1¸ö±ßÑØ²ÉÑùÊý¾Ý*/
  19.     .clkPolarity = SPI_SCK_ACTIVE_HIGH,         /*! ÉèÖÃSCK¹¤×÷ʱ¼«ÐÔ,¹¤×÷ʱSCKΪ¸ß*/
  20.     .msbFirst = true,                           /*! Ñ¡Ôñ´Ó×î¸ßλ¿ªÊ¼ÊÕ·¢*/
  21.     .transferType = SPI_USING_INTERRUPTS,       /*! Ñ¡ÔñͨÐÅÀàÐÍΪÖжϷ½Ê½*/
  22.     .rxDMAChannel = 1UL,                        /*! ÉèÖÃSPI DMA½ÓÊÕͨµÀ£¬ÈôδʹÓÃDMA·½Ê½´«Ê䣬Ôò¸ÃÅäÖÿɺöÂÔ*/
  23.     .txDMAChannel = 0UL,                        /*! ÉèÖÃSPI DMA·¢ËÍͨµÀ£¬ÈôδʹÓÃDMA·½Ê½´«Ê䣬Ôò¸ÃÅäÖÿɺöÂÔ*/
  24.     .callback = NULL,                           /*! ×¢²á»Øµ÷º¯Êý */
  25.     .callbackParam = NULL,                      /*! »Øµ÷º¯Êý²ÎÊý */
  26. };

  27. static spi_state_t s_spiMasterState;

  28. uint8_t g_spiTxBuff1[DATA_SIZE] = {1, 1, 3, 4, 5, 6, 7, 8};
  29. uint8_t g_spiRxBuff1[DATA_SIZE] = {0};

  30. static void SPI_Init_Gpio(void)
  31. {
  32.     GPIO_DRV_SetMuxModeSel(PORT_SPI_SCK, PIN_SPI_SCK, PORT_MUX_ALT3);        /*! SCK */
  33.     GPIO_DRV_SetMuxModeSel(PORT_SPI_SOUT, PIN_SPI_SOUT, PORT_MUX_ALT3);      /*! SOUT */
  34.     GPIO_DRV_SetMuxModeSel(PORT_SPI_SIN, PIN_SPI_SIN, PORT_MUX_ALT3);        /*! SIN */
  35.     GPIO_DRV_SetMuxModeSel(PORT_SPI_CS, PIN_SPI_CS, PORT_MUX_ALT2);          /*! CS */
  36. }

  37. void SPI_Master_Init(void)
  38. {
  39.     SPI_Init_Gpio();                                                        /*! ³õʼ»¯¶ÔÓ¦Òý½ÅΪSPI¹¦ÄÜ */

  40.     SPI_DRV_MasterInit(SPI_INSTANCE, &s_spiMasterState, &spiMasteConfig);   /*! SPIÖ÷»úģʽ³õʼ»¯ */
  41.     NVIC_SetPriority(SPI_IRQ, 2);                                           /*! ÉèÖÃSPIÖжÏÓÅÏȼ¶ */
  42. }

  43. void init_spi(void)
  44. {
  45.         SPI_Master_Init();
  46. }

  47. uint8_t spi_readwrite(uint8_t dat)
  48. {
  49.         uint8_t ret;
  50.         uint8_t rxdat=0;
  51.         uint8_t g_spiTxBuff1[10];
  52.         uint8_t g_spiRxBuff1[10];
  53.         g_spiTxBuff1[0]=dat;
  54.         ret=SPI_DRV_MasterTransferBlocking(SPI_INSTANCE, g_spiTxBuff1, g_spiRxBuff1, 1, SPI_TIMEOUT);
  55.         if (ret != 0)
  56.         {
  57.                         printf("transfer error, ret %d\n", ret);
  58.         }
  59.         rxdat=g_spiRxBuff1[0];
  60.         return rxdat;
  61.        
  62. }




2.2、ch376s.c
  1. #include "ac7840x.h"
  2. #include "ch376s.h"
  3. #include "spi/spi.h"
  4. #include "gpio_drv.h"

  5. uint8_t spi1_read_write_byte(uint8_t txdata)
  6. {
  7.     uint8_t rxdata;
  8.     //HAL_SPI_TransmitReceive(&hspi1, &txdata, &rxdata, 1, 1000);
  9.                 rxdata=spi_readwrite(txdata);
  10.     return rxdata; /* ·µ»ØÊÕµ½µÄÊý¾Ý */
  11. }

  12. uint8_t CH376_ReadWrite(uint8_t txdata)
  13. {
  14.         uint8_t rtdat;
  15.         rtdat=spi1_read_write_byte(txdata);
  16.         return rtdat;
  17. }

  18. uint8_t mInitCH376Host( void )  
  19. {
  20.         uint8_t res;

  21.         xWriteCH376Cmd( CMD11_CHECK_EXIST );                          /* ²âÊÔµ¥Æ¬»úÓëCH376Ö®¼äµÄͨѶ½Ó¿Ú */
  22.         xWriteCH376Data( 0x65 );
  23.         res = xReadCH376Data( );
  24.         xEndCH376Cmd( );                                                                // ½áÊøÍ¨ÐŲâÊÔ
  25.         if ( res != 0x9A )
  26.         {
  27.                 printf("USBͨÐÅ´íÎó\r\n");
  28.                 return( ERR_USB_UNKNOWN );                                  /* ͨѶ½Ó¿Ú²»Õý³£,¿ÉÄÜÔ­ÒòÓÐ:½Ó¿ÚÁ¬½ÓÒì³£,ÆäËüÉ豸ӰÏì(Ƭѡ²»Î¨Ò»),´®¿Ú²¨ÌØÂÊ,Ò»Ö±ÔÚ¸´Î»,¾§Õñ²»¹¤×÷ */
  29.         }
  30.         xWriteCH376Cmd( CMD11_SET_USB_MODE );                  /* É豸USB¹¤×÷ģʽ */
  31.         xWriteCH376Data( 0x06 );                                                // ģʽ´úÂëΪ0x06,±íʾÇл»µ½ÒÑÆôÓõÄUSBÖ÷»ú·½Ê½£¬×Ô¶¯²úÉúSOF°ü
  32.         mDelayuS( 200 );
  33.         res = xReadCH376Data( );                                                // ·µ»Ø²Ù×÷״̬
  34.         xEndCH376Cmd( );                                                            // ¹¤×÷ģʽÉèÖýáÊø

  35.         if ( res == CMD_RET_SUCCESS )
  36.         return( USB_INT_SUCCESS );
  37.         else
  38.         {
  39.                 return( ERR_USB_UNKNOWN );                                  /* ÉèÖÃģʽ´íÎó */
  40.         }       
  41. }

  42. uint8_t mWaitInterrupt( void )
  43. {                                                                /* µÈ´ýCH376Öжϲ¢»ñȡ״̬,Ö÷»ú¶ËµÈ´ý²Ù×÷Íê³É,·µ»Ø²Ù×÷״̬ */
  44. while ( Query376Interrupt( ) == FALSE );                  /* Ò»Ö±µÈÖÐ¶Ï */                                
  45. xWriteCH376Cmd( CMD_GET_STATUS );                          /* ²úÉú²Ù×÷Íê³ÉÖжÏ,»ñÈ¡ÖжÏ״̬ */
  46. return( xReadCH376Data( ) );
  47. }

  48. void        xWriteCH376Cmd( uint8_t mCmd )  /* ÏòCH376дÃüÁî */
  49. {
  50.         CH376S_CS_HIGH();
  51.         mDelay0_5uS( );
  52.         CH376S_CS_LOW();
  53.         CH376_ReadWrite( mCmd );  /* ·¢³öÃüÁîÂë */
  54. }

  55. void        xWriteCH376Data( uint8_t mData )  /* ÏòCH376дÊý¾Ý */
  56. {
  57.         CH376_ReadWrite( mData );
  58. //        mDelay0_5uS( );  /* È·±£¶ÁдÖÜÆÚ´óÓÚ0.6uS */
  59. }

  60. uint8_t        xReadCH376Data( void )  /* ´ÓCH376¶ÁÊý¾Ý */
  61. {
  62.         mDelay0_5uS( );  /* È·±£¶ÁдÖÜÆÚ´óÓÚ0.6uS */
  63.         return( CH376_ReadWrite( 0xff) );
  64. }

  65. uint8_t        Query376Interrupt( void )
  66. {
  67.         return( (GPIO_DRV_ReadPins(CH376S_INT_GPIO)& (1<<CH376S_INT_PIN))? FALSE : TRUE);
  68. }

  69. void mDelayuS(uint8_t us)
  70. {
  71. while(us--);
  72. }


  73. void mDelaymS(uint8_t ms)
  74. {
  75. unsigned int i;
  76. for(;ms>0;ms--)
  77.      for(i=0;i<940;i++);
  78. }

  79. void        mDelay0_5uS( void )  /* ÖÁÉÙÑÓʱ0.5uS,¸ù¾Ýµ¥Æ¬»úÖ÷Ƶµ÷Õû */
  80. {
  81. uint8_t i;
  82. i=20;
  83. while(i--);
  84. }


  85. void init_ch376s(void)
  86. {
  87.         uint8_t sta=0;
  88.         init_spi();
  89.        
  90.         GPIO_DRV_SetMuxModeSel(CH376S_CS_PORT, CH376S_CS_PIN, PORT_MUX_AS_GPIO);
  91.         GPIO_DRV_SetPinDirection(CH376S_CS_GPIO, CH376S_CS_PIN, 1);

  92.         GPIO_DRV_SetMuxModeSel(CH376S_INT_PORT, CH376S_INT_PIN, PORT_MUX_AS_GPIO);
  93.         GPIO_DRV_SetPinDirection(CH376S_INT_GPIO, CH376S_INT_PIN, 0); /*ÉèÖð´¼üÒý½ÅΪÊäÈë*/
  94.         GPIO_DRV_SetPullSel(CH376S_INT_PORT, CH376S_INT_PIN, PORT_INTERNAL_PULL_UP_ENABLED);   

  95.         OSIF_TimeDelay(200);
  96.         sta=mInitCH376Host();
  97.         printf("mInitCH376Host()=%x\r\n",sta);
  98.        
  99. }

  100. uint8_t buf[250];
  101. uint16_t        RealCount;

  102. void ch376s_test(void)
  103. {
  104.         uint8_t i=0;
  105.         uint8_t sta=0;
  106.         if(CH376DiskConnect()==USB_INT_SUCCESS)
  107.         {
  108.                 printf("check usb\r\n");
  109.                 OSIF_TimeDelay( 200 );
  110.                
  111.                 printf("CH376DiskMount()=%x\r\n",CH376DiskMount());
  112.                 OSIF_TimeDelay(200);
  113.                 if(CH376DiskMount()==USB_INT_SUCCESS)
  114.                 {
  115.                         printf("CH376DiskMount()=%x\r\n",CH376DiskMount());
  116.                         strcpy(buf, "ATC.TXT" );       
  117.                         if(CH376FileOpen(buf)==USB_INT_SUCCESS)
  118.                         {
  119.                                 for(i=0;i<20;i++)
  120.                                         buf[i]=0;
  121.                                 CH376ByteRead( buf, 20, &RealCount );
  122.                                 printf("buf=%s\r\n",buf);
  123.                         }
  124.                 }
  125.         }
  126.         else
  127.         {
  128.                 printf("no check usb\r\n");
  129.         }
  130.        
  131. }


2.3、main.c
  1. #include "ac7840x.h"
  2. #include "clock_config.h"
  3. #include "debugout_ac7840x.h"
  4. #include "osif.h"
  5. #include "led/led.h"
  6. #include "key/key.h"
  7. #include "adc/adc.h"
  8. #include "spi/spi.h"
  9. #include "ch376s.h"

  10. void SystemClock_Config(void)
  11. {
  12.     CKGEN_SYS_Init(g_clockManConfigsArr, CLOCK_MANAGER_CONFIG_CNT,
  13.                    g_clockManCallbacksArr, CLOCK_MANAGER_CALLBACK_CNT);
  14.     CKGEN_DRV_UpdateConfiguration(0, CLOCK_MANAGER_POLICY_AGREEMENT);
  15. }

  16. int main(void)
  17. {
  18.         SystemClock_Config();
  19.         init_led();
  20.         init_key();
  21.         init_adc();
  22.         InitDebug();
  23.        
  24.         init_ch376s();
  25.        
  26.         ch376s_test();

  27.         while (1)
  28.         {
  29.                 led3_on();
  30.                 OSIF_TimeDelay(100);
  31.                 led3_off();
  32.                 OSIF_TimeDelay(100);
  33.                 //ADC_TriggerTest();
  34.         }
  35. }


三、运行结果

3.1、在U盘中创建ATX.TXT文件
20231216105751.png

3.2、串口打印读取U盘内容的数据
20231216105615.png

您需要登录后才可以回帖 登录 | 注册

本版积分规则

132

主题

701

帖子

7

粉丝
快速回复 在线客服 返回列表 返回顶部