Enum SPI

这是学习网络编程时的代码,今天整理一下贴出来了:

/////////////////////////////////////////////////// //
//  AppTest.cpp文件
/////////////////////////////////////////////////// //
#include  < afxwin.h >
#include 
" SkinMagicLib.h "
#include 
" resource.h "
#include 
< commctrl.h >      //  通用控件
#include  < Ws2spi.h >          //  SPI函数定义

#pragma  comment(lib,"SkinMagicLibMT6Trial")     //  链接到Skin库
#pragma  comment(lib, "WS2_32")                 //  链接到WS2_32.lib
#pragma  comment(lib,"comctl32")                 //  通用控件库

/////////////////////////////////////////////////// //
//  外部函数
extern  LPWSAPROTOCOL_INFOW GetProvider(LPINT lpnTotalProtocols);
extern   void  FreeProvider(LPWSAPROTOCOL_INFOW pProtoInfo);

/////////////////////////////////////////////////// //
//  全局变量
LPWSAPROTOCOL_INFOW pProtoInfo;
HINSTANCE            hInstance;
HWND                hListProtocol;
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);

void  ShowProtocolList(HWND hListProtocol)
{
    
char     szBuffer[MAX_PATH];
    
int         nProtocols;
    LVITEM item 
=  {  0  };
    LVITEM lvi    
=  {  0  };
    
    pProtoInfo 
=  GetProvider( & nProtocols);
    
for  ( int  i  =   0 ; i  <  nProtocols;  ++ i)
    {
        item.iItem 
=  i;
        item.mask 
=  LVIF_TEXT;             //  指定pszText域有
        wsprintf(szBuffer, " %d " ,i + 1 );
        item.pszText 
=  szBuffer;
        ::SendMessage(hListProtocol, LVM_INSERTITEM, 
0 , ( long ) & item); // 设置Index域

        lvi.iSubItem 
=   1 ;
        wsprintf(szBuffer,
" %ws " ,pProtoInfo[i].szProtocol);
        lvi.pszText 
=  szBuffer;
        ::SendMessage(hListProtocol, LVM_SETITEMTEXT, i, (
long ) & lvi); // 设置Protocol域

        lvi.iSubItem 
=   2 ;
        wsprintf(szBuffer,
" %d " ,pProtoInfo[i].dwCatalogEntryId);
        lvi.pszText 
=  szBuffer;
        ::SendMessage(hListProtocol, LVM_SETITEMTEXT, i, (
long ) & lvi); // 设置CatalogEntryId域

        lvi.iSubItem 
=   3 ;
        wsprintf(szBuffer,
" %d " ,pProtoInfo[i].ProtocolChain.ChainLen);
        lvi.pszText 
=  szBuffer;
        ::SendMessage(hListProtocol, LVM_SETITEMTEXT, i, (
long ) & lvi); // 设置ChainLen域
    }
}

int  CALLBACK WinMain(HINSTANCE hInst,HINSTANCE,LPSTR, int )
{
    InitSkinMagicLib(hInst,
" AppTest " ,NULL,NULL);
    LoadSkinFile(
" AlphaOS.smf " );
    INITCOMMONCONTROLSEX icce;
    icce.dwSize 
=   sizeof (icce);
    
//  ListView
    icce.dwICC  =  ICC_LISTVIEW_CLASSES ;
    InitCommonControlsEx(
& icce);     // 初使化扩展通用控件库
    hInstance  =  hInst;
    ::DialogBoxParam(hInst,(LPCTSTR)IDD_MAIN,NULL,(DLGPROC)WndProc,NULL);
    ExitSkinMagicLib();
    
return   1 ;
}

LRESULT CALLBACK WndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
    POINT    p;
    HICON    hIcon;
    
switch (uMsg)
    {
    
case  WM_COMMAND:
        {            
            
switch (LOWORD(wParam))
            {
            
case  IDC_HOMELINK:
                ::GetCursorPos(
& p);
                ::TrackSkinPopupMenu(::LoadMenu(hInstance,
                                    MAKEINTRESOURCE(IDR_MENU2)),
                                    TPM_TOPALIGN,p.x,p.y,hWnd);
                
break ;
            
case  IDM_PEDIY:
                ::ShellExecute(hWnd,
" Open " , " http://bbs.pediy.com " ,NULL,NULL,SW_SHOW);
                
break ;
            
case  IDM_UNPACKCN:
                ::ShellExecute(hWnd,
" Open " , " http://www.unpack.cn " ,NULL,NULL,SW_SHOW);
                
break ;
            
case  IDM_BAJIAO:
                ::ShellExecute(hWnd,
" Open " , " http://www.bajiao123.com " ,NULL,NULL,SW_SHOW);
                
break ;
            }
        }
        
break ;
    
case  WM_INITDIALOG:
        ::SetWindowText (hWnd,
" Enum SPI -- By thinkSJ " );         // 设置标题栏
         if (LoadSkinFromResource(hInstance,(LPCTSTR)IDR_SKIN1, " Skin " ))
        {
            SetWindowSkin(hWnd,
" MainFrame " );             // 换肤
        }
        hIcon 
=  ::LoadIcon(hInstance,MAKEINTRESOURCE(IDI_ICON1));    
        ::SendMessage(hWnd,WM_SETICON,ICON_SMALL,(LPARAM)hIcon);    
// 设置图标

        hListProtocol 
=  ::GetDlgItem(hWnd,IDC_LISTPROTOCOL);
        ::SendMessage(hListProtocol, LVM_SETEXTENDEDLISTVIEWSTYLE, 
                        
0 , LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
        LVCOLUMN column;
        
//  指定LVCOLUMN结构中的pszText、fmt、cx域有效
        column.mask  =  LVCF_TEXT | LVCF_FMT | LVCF_WIDTH;    
        
//  设置有效的域的属性
        column.fmt  =  LVCFMT_LEFT;    
        column.pszText 
=   " Index " ;
        column.cx 
=   50 ;
        ::SendMessage(hListProtocol, LVM_INSERTCOLUMN, 
1 , (LPARAM) & column);        
        column.pszText 
=   " Protocol " ;
        column.cx 
=   150 ;
        ::SendMessage(hListProtocol, LVM_INSERTCOLUMN, 
2 , (LPARAM) & column);            
        column.pszText 
=   " CatalogEntryId " ;
        column.cx 
=   100 ;
        ::SendMessage (hListProtocol,LVM_INSERTCOLUMN,
3 ,(LPARAM) & column);
        column.pszText 
=   " ChainLen " ;
        column.cx 
=   70 ;
        ::SendMessage (hListProtocol,LVM_INSERTCOLUMN,
4 ,(LPARAM) & column);    

        ShowProtocolList(hListProtocol);    
//  显示
         return   true ;
    
case  WM_CLOSE:
        ::FreeProvider(pProtoInfo);
        ::EndDialog(hWnd,
0 );
        
break ;
    }
    
return   false ;
}

程序主要是通过WSCEnumProtocols函数来枚举SPI,不仅可以枚举基础服务和服务链,而且还可以枚举分层式

服务.

(注意:不要用浏览器下载,最好用工具下载) 

下载工程

 

uint8_t SD_Init(spi_index_enum spi_n) { uint8_t response; uint8_t ocr[4]; uint32_t timeout; // 初始化CS引脚(增加上拉) gpio_init(SD_CS_PIN, GPO, 1, GPI_PULL_UP ); gpio_high(SD_CS_PIN); // CS置高(未选中) // 1. 初始化SPI为低速模式(100-400kHz) spi_init(spi_n, SPI_MODE0, 400000, SPI1_MAP0_SCK_A5, SPI1_MAP0_MOSI_A7, SPI1_MAP0_MISO_A6, A4); // 2. 发送80+个时钟脉冲(确保SD卡完成上电) gpio_high(SD_CS_PIN); // 保持CS高电平 for(uint8_t i = 0; i < 10; i++) { spi_write_8bit(spi_n, 0xFF); system_delay_us(10); // 增加延时确保稳定 } // 3. 发送CMD0复位卡(需要精确CS控制) gpio_low(SD_CS_PIN); // 选中SD卡 response = SD_SendCmd(spi_n, CMD0, 0, 0x95); gpio_high(SD_CS_PIN); // 释放SD卡 spi_write_8bit(spi_n, 0xFF); // 额外时钟周期 if(response != 0x01) { return 0xFF; // 复位失败 } // 4. 发送CMD8检查SD卡版本(增加电压检查) gpio_low(SD_CS_PIN); response = SD_SendCmd(spi_n, CMD8, 0x000001AA, 0x87); // 固定参数0x1AA if(response == 0x01) { // 正确读取4字节响应 spi_read_8bit_array(spi_n, ocr, 4); // 检查电压范围(必须包含3.3V) if((ocr[2] & 0x0F) != 0x01) { gpio_high(SD_CS_PIN); return 0xFE; // 电压不兼容 } } else if(response != 0x05) { // 0x05是非法命令(SDv1卡) gpio_high(SD_CS_PIN); return 0xFD; // CMD8失败 } gpio_high(SD_CS_PIN); spi_write_8bit(spi_n, 0xFF); // 5. 初始化循环(修复逻辑错误) timeout = 1000; // 超时时间1s uint8_t sd_type = 0; do { // 先发送CMD55 gpio_low(SD_CS_PIN); SD_SendCmd(spi_n, CMD55, 0, 0x65); gpio_high(SD_CS_PIN); spi_write_8bit(spi_n, 0xFF); // 发送ACMD41(带HCS标志) gpio_low(SD_CS_PIN); response = SD_SendCmd(spi_n, ACMD41, 0x40000000, 0x77); // 必须带HCS gpio_high(SD_CS_PIN); spi_write_8bit(spi_n, 0xFF); system_delay_ms(10); // 增加延时 } while((response != 0x00) && timeout--); if(response != 0x00) { return 0xFC; // 初始化超时 } // 6. 发送CMD58读取OCR(修复读取逻辑) gpio_low(SD_CS_PIN); response = SD_SendCmd(spi_n, CMD58, 0, 0xFD); if(response != 0x00) { gpio_high(SD_CS_PIN); return 0xFB; // CMD58失败 } // 正确读取OCR spi_read_8bit_array(spi_n, ocr, 4); gpio_high(SD_CS_PIN); spi_write_8bit(spi_n, 0xFF); // 7. 设置块大小(必须步骤!) gpio_low(SD_CS_PIN); response = SD_SendCmd(spi_n, CMD16, 512, 0xFF); gpio_high(SD_CS_PIN); spi_write_8bit(spi_n, 0xFF); if(response != 0x00) { return 0xFA; // 设置块大小失败 } // 8. 切换高速SPI模式 spi_init(spi_n, SPI_MODE0, 25000000, SPI1_MAP0_SCK_A5, SPI1_MAP0_MOSI_A7, SPI1_MAP0_MISO_A6, SPI_CS_NULL); // 9. 返回卡类型 return (ocr[0] & 0x40) ? SD_TYPE_SDHC : SD_TYPE_SD2; } // SD卡命令发送函数 uint8_t SD_SendCmd(spi_index_enum spi_n, uint8_t cmd, uint32_t arg, uint8_t crc) { gpio_low(SD_CS_PIN); // CS置低 spi_write_8bit(spi_n, (uint8_t)(cmd | 0x40)); // 强制转换为uint8_t spi_write_8bit(spi_n, (uint8_t)((arg >> 24) & 0xFF)); spi_write_8bit(spi_n, (uint8_t)((arg >> 16) & 0xFF)); spi_write_8bit(spi_n, (uint8_t)((arg >> 8) & 0xFF)); spi_write_8bit(spi_n, (uint8_t)(arg & 0xFF)); spi_write_8bit(spi_n, crc); // CRC校验 // 等待响应(R1格式) uint8_t retry = 20; uint8_t response; do { response = (uint8_t)spi_read_8bit(spi_n); // 确保返回值为uint8_t } while((response == 0xFF) && retry--); return response; } 上面函数调用时显示超出超过屏幕分辨率范围,下面时oled报错的函数位置void oled_show_string (uint16 x, uint16 y, const char ch[]) { // 如果程序在输出了断言信息 并且提示出错位置在这里 // 那么一般是屏幕显示的时候超过屏幕分辨率范围了 // 检查一下你的显示调用的函数 自己计算一下哪里超过了屏幕显示范围 zf_assert(128 > x); zf_assert(8 > y); OLED_CS(0); uint8 c = 0, i = 0, j = 0; while ('\0' != ch[j]) { switch(oled_display_font) { case OLED_6X8_FONT: { c = ch[j] - 32; if(x > 126) { x = 0; y ++; } oled_set_coordinate(x, y); for(i = 0; 6 > i; i ++) { oled_write_data(ascii_font_6x8[c][i]); } x += 6; j ++; }break; case OLED_8X16_FONT: { c = ch[j] - 32; if(x > 120) { x = 0; y ++; } oled_set_coordinate(x, y); for(i = 0; i < 8; i ++) { oled_write_data(ascii_font_8x16[c][i]); } oled_set_coordinate(x, y + 1); for(i = 0; i < 8; i ++) { oled_write_data(ascii_font_8x16[c][i + 8]); } x += 8; j ++; }break; case OLED_16X16_FONT: { // 暂不支持 }break; } } OLED_CS(1); }
最新发布
07-07
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值