测试开发板的SPI,使用SPI方式连接CH376模块读取U盘数据。
一、SPI硬件
连接CH376模块
二、程序部分
2.1、spi.c
#include "ac7840x.h"
#include "gpio_drv.h"
#include "osif.h"
#include "spi/spi.h"
#include "spi_hw.h"
#include "spi_master_drv.h"
#include "spi_slave_drv.h"
#include "port_hw.h"
#include "gpio_hw.h"
spi_master_config_t spiMasteConfig =
{
.bitsPerSec = 2000000UL, /*! ÉèÖÃSPI²¨ÌØÂÊ*/
.whichPcs = SPI_PCS2, /*! ÉèÖÃSPIƬѡÒý½Å*/
.pcsPolarity = SPI_ACTIVE_LOW, /*! ÉèÖÃSPIƬѡµÍÓÐЧ*/
.isPcsContinuous = true, /*! ÉèÖÃƬѡÁ¬Ðøģʽ*/
.bitcount = 8UL, /*! SPI Ö¡³¤¶È,Ö§³Ö 4~32 bits*/
.spiSrcClk = 60000000UL, /*! SPIʱÖÓԴƵÂÊ*/
.clkPhase = SPI_CLOCK_PHASE_1ST_EDGE, /*! ÉèÖÃÊý¾Ý²ÉÑùÏàλ,µÚ1¸ö±ßÑزÉÑùÊý¾Ý*/
.clkPolarity = SPI_SCK_ACTIVE_HIGH, /*! ÉèÖÃSCK¹¤×÷ʱ¼«ÐÔ,¹¤×÷ʱSCKΪ¸ß*/
.msbFirst = true, /*! Ñ¡Ôñ´Ó×î¸ßλ¿ªÊ¼ÊÕ·¢*/
.transferType = SPI_USING_INTERRUPTS, /*! Ñ¡ÔñͨÐÅÀàÐÍΪÖжϷ½Ê½*/
.rxDMAChannel = 1UL, /*! ÉèÖÃSPI DMA½ÓÊÕͨµÀ£¬ÈôδʹÓÃDMA·½Ê½´«Ê䣬Ôò¸ÃÅäÖÿɺöÂÔ*/
.txDMAChannel = 0UL, /*! ÉèÖÃSPI DMA·¢ËÍͨµÀ£¬ÈôδʹÓÃDMA·½Ê½´«Ê䣬Ôò¸ÃÅäÖÿɺöÂÔ*/
.callback = NULL, /*! ×¢²á»Øµ÷º¯Êý */
.callbackParam = NULL, /*! »Øµ÷º¯Êý²ÎÊý */
};
static spi_state_t s_spiMasterState;
uint8_t g_spiTxBuff1[DATA_SIZE] = {1, 1, 3, 4, 5, 6, 7, 8};
uint8_t g_spiRxBuff1[DATA_SIZE] = {0};
static void SPI_Init_Gpio(void)
{
GPIO_DRV_SetMuxModeSel(PORT_SPI_SCK, PIN_SPI_SCK, PORT_MUX_ALT3); /*! SCK */
GPIO_DRV_SetMuxModeSel(PORT_SPI_SOUT, PIN_SPI_SOUT, PORT_MUX_ALT3); /*! SOUT */
GPIO_DRV_SetMuxModeSel(PORT_SPI_SIN, PIN_SPI_SIN, PORT_MUX_ALT3); /*! SIN */
GPIO_DRV_SetMuxModeSel(PORT_SPI_CS, PIN_SPI_CS, PORT_MUX_ALT2); /*! CS */
}
void SPI_Master_Init(void)
{
SPI_Init_Gpio(); /*! ³õʼ»¯¶ÔÓ¦Òý½ÅΪSPI¹¦ÄÜ */
SPI_DRV_MasterInit(SPI_INSTANCE, &s_spiMasterState, &spiMasteConfig); /*! SPIÖ÷»úģʽ³õʼ»¯ */
NVIC_SetPriority(SPI_IRQ, 2); /*! ÉèÖÃSPIÖжÏÓÅÏȼ¶ */
}
void init_spi(void)
{
SPI_Master_Init();
}
uint8_t spi_readwrite(uint8_t dat)
{
uint8_t ret;
uint8_t rxdat=0;
uint8_t g_spiTxBuff1[10];
uint8_t g_spiRxBuff1[10];
g_spiTxBuff1[0]=dat;
ret=SPI_DRV_MasterTransferBlocking(SPI_INSTANCE, g_spiTxBuff1, g_spiRxBuff1, 1, SPI_TIMEOUT);
if (ret != 0)
{
printf("transfer error, ret %d\n", ret);
}
rxdat=g_spiRxBuff1[0];
return rxdat;
}
2.2、ch376s.c
#include "ac7840x.h"
#include "ch376s.h"
#include "spi/spi.h"
#include "gpio_drv.h"
uint8_t spi1_read_write_byte(uint8_t txdata)
{
uint8_t rxdata;
//HAL_SPI_TransmitReceive(&hspi1, &txdata, &rxdata, 1, 1000);
rxdata=spi_readwrite(txdata);
return rxdata; /* ·µ»ØÊÕµ½µÄÊý¾Ý */
}
uint8_t CH376_ReadWrite(uint8_t txdata)
{
uint8_t rtdat;
rtdat=spi1_read_write_byte(txdata);
return rtdat;
}
uint8_t mInitCH376Host( void )
{
uint8_t res;
xWriteCH376Cmd( CMD11_CHECK_EXIST ); /* ²âÊÔµ¥Æ¬»úÓëCH376Ö®¼äµÄͨѶ½Ó¿Ú */
xWriteCH376Data( 0x65 );
res = xReadCH376Data( );
xEndCH376Cmd( ); // ½áÊøͨÐŲâÊÔ
if ( res != 0x9A )
{
printf("USBͨÐÅ´íÎó\r\n");
return( ERR_USB_UNKNOWN ); /* ͨѶ½Ó¿Ú²»Õý³£,¿ÉÄÜÔÒòÓÐ:½Ó¿ÚÁ¬½ÓÒì³£,ÆäËüÉ豸ӰÏì(Ƭѡ²»Î¨Ò»),´®¿Ú²¨ÌØÂÊ,Ò»Ö±ÔÚ¸´Î»,¾§Õñ²»¹¤×÷ */
}
xWriteCH376Cmd( CMD11_SET_USB_MODE ); /* É豸USB¹¤×÷ģʽ */
xWriteCH376Data( 0x06 ); // ģʽ´úÂëΪ0x06,±íʾÇл»µ½ÒÑÆôÓõÄUSBÖ÷»ú·½Ê½£¬×Ô¶¯²úÉúSOF°ü
mDelayuS( 200 );
res = xReadCH376Data( ); // ·µ»Ø²Ù×÷״̬
xEndCH376Cmd( ); // ¹¤×÷ģʽÉèÖýáÊø
if ( res == CMD_RET_SUCCESS )
return( USB_INT_SUCCESS );
else
{
return( ERR_USB_UNKNOWN ); /* ÉèÖÃģʽ´íÎó */
}
}
uint8_t mWaitInterrupt( void )
{ /* µÈ´ýCH376Öжϲ¢»ñȡ״̬,Ö÷»ú¶ËµÈ´ý²Ù×÷Íê³É,·µ»Ø²Ù×÷״̬ */
while ( Query376Interrupt( ) == FALSE ); /* Ò»Ö±µÈÖÐ¶Ï */
xWriteCH376Cmd( CMD_GET_STATUS ); /* ²úÉú²Ù×÷Íê³ÉÖжÏ,»ñÈ¡ÖжÏ״̬ */
return( xReadCH376Data( ) );
}
void xWriteCH376Cmd( uint8_t mCmd ) /* ÏòCH376дÃüÁî */
{
CH376S_CS_HIGH();
mDelay0_5uS( );
CH376S_CS_LOW();
CH376_ReadWrite( mCmd ); /* ·¢³öÃüÁîÂë */
}
void xWriteCH376Data( uint8_t mData ) /* ÏòCH376дÊý¾Ý */
{
CH376_ReadWrite( mData );
// mDelay0_5uS( ); /* È·±£¶ÁдÖÜÆÚ´óÓÚ0.6uS */
}
uint8_t xReadCH376Data( void ) /* ´ÓCH376¶ÁÊý¾Ý */
{
mDelay0_5uS( ); /* È·±£¶ÁдÖÜÆÚ´óÓÚ0.6uS */
return( CH376_ReadWrite( 0xff) );
}
uint8_t Query376Interrupt( void )
{
return( (GPIO_DRV_ReadPins(CH376S_INT_GPIO)& (1<<CH376S_INT_PIN))? FALSE : TRUE);
}
void mDelayuS(uint8_t us)
{
while(us--);
}
void mDelaymS(uint8_t ms)
{
unsigned int i;
for(;ms>0;ms--)
for(i=0;i<940;i++);
}
void mDelay0_5uS( void ) /* ÖÁÉÙÑÓʱ0.5uS,¸ù¾Ýµ¥Æ¬»úÖ÷Ƶµ÷Õû */
{
uint8_t i;
i=20;
while(i--);
}
void init_ch376s(void)
{
uint8_t sta=0;
init_spi();
GPIO_DRV_SetMuxModeSel(CH376S_CS_PORT, CH376S_CS_PIN, PORT_MUX_AS_GPIO);
GPIO_DRV_SetPinDirection(CH376S_CS_GPIO, CH376S_CS_PIN, 1);
GPIO_DRV_SetMuxModeSel(CH376S_INT_PORT, CH376S_INT_PIN, PORT_MUX_AS_GPIO);
GPIO_DRV_SetPinDirection(CH376S_INT_GPIO, CH376S_INT_PIN, 0); /*ÉèÖð´¼üÒý½ÅΪÊäÈë*/
GPIO_DRV_SetPullSel(CH376S_INT_PORT, CH376S_INT_PIN, PORT_INTERNAL_PULL_UP_ENABLED);
OSIF_TimeDelay(200);
sta=mInitCH376Host();
printf("mInitCH376Host()=%x\r\n",sta);
}
uint8_t buf[250];
uint16_t RealCount;
void ch376s_test(void)
{
uint8_t i=0;
uint8_t sta=0;
if(CH376DiskConnect()==USB_INT_SUCCESS)
{
printf("check usb\r\n");
OSIF_TimeDelay( 200 );
printf("CH376DiskMount()=%x\r\n",CH376DiskMount());
OSIF_TimeDelay(200);
if(CH376DiskMount()==USB_INT_SUCCESS)
{
printf("CH376DiskMount()=%x\r\n",CH376DiskMount());
strcpy(buf, "ATC.TXT" );
if(CH376FileOpen(buf)==USB_INT_SUCCESS)
{
for(i=0;i<20;i++)
buf[i]=0;
CH376ByteRead( buf, 20, &RealCount );
printf("buf=%s\r\n",buf);
}
}
}
else
{
printf("no check usb\r\n");
}
}
2.3、main.c
#include "ac7840x.h"
#include "clock_config.h"
#include "debugout_ac7840x.h"
#include "osif.h"
#include "led/led.h"
#include "key/key.h"
#include "adc/adc.h"
#include "spi/spi.h"
#include "ch376s.h"
void SystemClock_Config(void)
{
CKGEN_SYS_Init(g_clockManConfigsArr, CLOCK_MANAGER_CONFIG_CNT,
g_clockManCallbacksArr, CLOCK_MANAGER_CALLBACK_CNT);
CKGEN_DRV_UpdateConfiguration(0, CLOCK_MANAGER_POLICY_AGREEMENT);
}
int main(void)
{
SystemClock_Config();
init_led();
init_key();
init_adc();
InitDebug();
init_ch376s();
ch376s_test();
while (1)
{
led3_on();
OSIF_TimeDelay(100);
led3_off();
OSIF_TimeDelay(100);
//ADC_TriggerTest();
}
}
三、运行结果
3.1、在U盘中创建ATX.TXT文件
3.2、串口打印读取U盘内容的数据
|
此文章已获得独家原创/原创奖标签,著作权归21ic所有,未经允许禁止转载。
|