返回

嵌入式笔记00

目录

FileSystem

FATFS

选择格式化的文件系统类型

  • FAT12: 主要用于极小容量的介质(小于 2MB)
  • FAT16:32MB 到 2GB 之间的介质(适合较小的 SD 卡或 SPI Flash)
  • FAT32:适用于 512MB 到 2TB 的容量
  • exFAT:专门为大容量闪存设计(通常 > 32GB)需要使能FF_FS_EXFAT

配置文件

  • FF_CODE_PAGE: 配置代码页(CN:936,US:437)
  • FF_LFN_UNICODE
  • FF_USE_LFN:长文件名支持
  • FF_FS_REENTRANT:文件可重入(线程安全),允许重写ff与互斥相关的函数
  • FF_FS_NORTC:是否使用默认日期,允许重写并从RTC时钟读取当前日期

代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
FRESULT f_open(FIL *fp, const TCHAR *path, BYTE mode);                               /* Open or create a file */
FRESULT f_close(FIL *fp);                                                            /* Close an open file object */
FRESULT f_read(FIL *fp, void *buff, UINT btr, UINT *br);                             /* Read data from the file */
FRESULT f_write(FIL *fp, const void *buff, UINT btw, UINT *bw);                      /* Write data to the file */
FRESULT f_lseek(FIL *fp, FSIZE_t ofs);                                               /* Move file pointer of the file object */
FRESULT f_truncate(FIL *fp);                                                         /* Truncate the file */
FRESULT f_sync(FIL *fp);                                                             /* Flush cached data of the writing file */
FRESULT f_opendir(DIR *dp, const TCHAR *path);                                       /* Open a directory */
FRESULT f_closedir(DIR *dp);                                                         /* Close an open directory */
FRESULT f_readdir(DIR *dp, FILINFO *fno);                                            /* Read a directory item */
FRESULT f_findfirst(DIR *dp, FILINFO *fno, const TCHAR *path, const TCHAR *pattern); /* Find first file */
FRESULT f_findnext(DIR *dp, FILINFO *fno);                                           /* Find next file */
FRESULT f_mkdir(const TCHAR *path);                                                  /* Create a sub directory */
FRESULT f_unlink(const TCHAR *path);                                                 /* Delete an existing file or directory */
FRESULT f_rename(const TCHAR *path_old, const TCHAR *path_new);                      /* Rename/Move a file or directory */
FRESULT f_stat(const TCHAR *path, FILINFO *fno);                                     /* Get file status */
FRESULT f_chmod(const TCHAR *path, BYTE attr, BYTE mask);                            /* Change attribute of a file/dir */
FRESULT f_utime(const TCHAR *path, const FILINFO *fno);                              /* Change timestamp of a file/dir */
FRESULT f_chdir(const TCHAR *path);                                                  /* Change current directory */
FRESULT f_chdrive(const TCHAR *path);                                                /* Change current drive */
FRESULT f_getcwd(TCHAR *buff, UINT len);                                             /* Get current directory */
FRESULT f_getfree(const TCHAR *path, DWORD *nclst, FATFS **fatfs);                   /* Get number of free clusters on the drive */
FRESULT f_getlabel(const TCHAR *path, TCHAR *label, DWORD *vsn);                     /* Get volume label */
FRESULT f_setlabel(const TCHAR *label);                                              /* Set volume label */
FRESULT f_forward(FIL *fp, UINT (*func)(const BYTE *, UINT), UINT btf, UINT *bf);    /* Forward data to the stream */
FRESULT f_expand(FIL *fp, FSIZE_t fsz, BYTE opt);                                    /* Allocate a contiguous block to the file */
FRESULT f_mount(FATFS *fs, const TCHAR *path, BYTE opt);                             /* Mount/Unmount a logical drive */
FRESULT f_mkfs(const TCHAR *path, const MKFS_PARM *opt, void *work, UINT len);       /* Create a FAT volume */
FRESULT f_fdisk(BYTE pdrv, const LBA_t ptbl[], void *work);                          /* Divide a physical drive into some partitions */
FRESULT f_setcp(WORD cp);                                                            /* Set current code page */
int f_putc(TCHAR c, FIL *fp);                                                        /* Put a character to the file */
int f_puts(const TCHAR *str, FIL *cp);                                               /* Put a string to the file */
int f_printf(FIL *fp, const TCHAR *str, ...);                                        /* Put a formatted string to the file */
TCHAR *f_gets(TCHAR *buff, int len, FIL *fp);                                        /* Get a string from the file */

类型

存储类型
1
2
3
4
5
6
typedef unsigned int UINT;  /* int must be 16-bit or 32-bit */
typedef unsigned char BYTE; /* char must be 8-bit */
typedef uint16_t WORD;      /* 16-bit unsigned integer */
typedef uint32_t DWORD;     /* 32-bit unsigned integer */
typedef uint64_t QWORD;     /* 64-bit unsigned integer */
typedef WORD WCHAR;         /* UTF-16 character type */
FRESULT
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
typedef enum {
    FR_OK = 0,              /* (0) Succeeded */
    FR_DISK_ERR,            /* (1) A hard error occurred in the low level disk I/O layer */
    FR_INT_ERR,             /* (2) Assertion failed */
    FR_NOT_READY,           /* (3) The physical drive cannot work */
    FR_NO_FILE,             /* (4) Could not find the file */
    FR_NO_PATH,             /* (5) Could not find the path */
    FR_INVALID_NAME,        /* (6) The path name format is invalid */
    FR_DENIED,              /* (7) Access denied due to prohibited access or directory full */
    FR_EXIST,               /* (8) Access denied due to prohibited access */
    FR_INVALID_OBJECT,      /* (9) The file/directory object is invalid */
    FR_WRITE_PROTECTED,     /* (10) The physical drive is write protected */
    FR_INVALID_DRIVE,       /* (11) The logical drive number is invalid */
    FR_NOT_ENABLED,         /* (12) The volume has no work area */
    FR_NO_FILESYSTEM,       /* (13) There is no valid FAT volume */
    FR_MKFS_ABORTED,        /* (14) The f_mkfs() aborted due to any problem */
    FR_TIMEOUT,             /* (15) Could not get a grant to access the volume within defined period */
    FR_LOCKED,              /* (16) The operation is rejected according to the file sharing policy */
    FR_NOT_ENOUGH_CORE,     /* (17) LFN working buffer could not be allocated */
    FR_TOO_MANY_OPEN_FILES, /* (18) Number of open files > FF_FS_LOCK */
    FR_INVALID_PARAMETER    /* (19) Given parameter is invalid */
} FRESULT;
FILE Access
1
2
3
4
5
6
7
#define FA_READ          0x01
#define FA_WRITE         0x02
#define FA_OPEN_EXISTING 0x00
#define FA_CREATE_NEW    0x04
#define FA_CREATE_ALWAYS 0x08
#define FA_OPEN_ALWAYS   0x10
#define FA_OPEN_APPEND   0x30

exmaple

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
FIL fnew;
FRESULT res; 
UINT fnum;
char write_buf[] = "Hello! This is FatFs test.";
char read_buf[64];

// 1. 写文件测试
res = f_open(&fnew, "/sd/test.txt", FA_CREATE_ALWAYS | FA_WRITE);
if (res == FR_OK) {
    f_write(&fnew, write_buf, sizeof(write_buf), &fnum);
    f_close(&fnew);
    LOG_I("Test file written successfully!\r\n");
}

// 2. 读文件测试
res = f_open(&fnew, "/sd/test.txt", FA_READ);
if (res == FR_OK) {
    f_read(&fnew, read_buf, sizeof(read_buf), &fnum);
    LOG_I("Read content: %s\r\n", read_buf);
    f_close(&fnew);
}

FATFS fs;
__attribute((aligned(8))) static uint32_t workbuf[4096];

MKFS_PARM fs_para = {
    .fmt = FM_FAT32,     /* Format option (FM_FAT, FM_FAT32, FM_EXFAT and FM_SFD) */
    .n_fat = 1,          /* Number of FATs */
    .align = 0,          /* Data area alignment (sector) */
    .n_root = 1,         /* Number of root directory entries */
    .au_size = 512 * 32, /* Cluster size (byte) */
};

void filesystem_init(void)
{
    FRESULT ret;

    board_sdh_gpio_init();

    fatfs_sdh_driver_register();

    ret = f_mount(&fs, "/sd", 1);

    if (ret == FR_NO_FILESYSTEM) {
        LOG_W("No filesystem yet, try to be formatted...\r\n");

        ret = f_mkfs("/sd", &fs_para, workbuf, sizeof(workbuf));

        if (ret != FR_OK) {
            LOG_F("fail to make filesystem %d\r\n", ret);
            _CALL_ERROR();
        }

        if (ret == FR_OK) {
            LOG_I("done with formatting.\r\n");
            LOG_I("first start to unmount.\r\n");
            ret = f_mount(NULL, "/sd", 1);
            LOG_I("then start to remount.\r\n");
        }
    } else if (ret != FR_OK) {
        LOG_F("fail to mount filesystem,error= %d\r\n", ret);
       LOG_F("SD card might fail to initialise.\r\n");
        _CALL_ERROR();
    } else {
        LOG_D("Succeed to mount filesystem\r\n");
    }

    if (ret == FR_OK) {
        LOG_I("FileSystem cluster size:%d-sectors (%d-Byte)\r\n", fs.csize, fs.csize * 512);
    }
}

Connective

传统蓝牙

A2DP(高级音频分发协议)

HFP / HSP(语音电话)

BLE

简单理解

  1. 广播:灯不停发送广播包。
  2. 发现:手机扫描到了灯的 MAC 地址和 UUID。
  3. 连接:手机发起连接请求,双方握手。
  4. 发现服务 (Discovery):手机问灯:“你支持哪些 Service?” 灯回答:“我有一个 UUID 为 0xFF01 的灯控服务。”
  5. 找到特征值:手机继续问:“这个服务里有什么?” 灯回答:“有一个 UUID 为 0xFF02 的开关特征值,支持 Write。”
  6. 数据交互:
    • 手机控制灯:手机向 0xFF02 写入 0x01(开灯)。
    • 灯告知手机状态:如果灯有关机按键,按下去后,灯通过 0xFF03 特征值发送 Notify 给手机,手机收到 0x00。

关键层

  • GAP (Generic Access Profile):决定设备如何打广告、如何被发现、如何建立连接。
  • GATT (Generic Attribute Profile):连接后的数据传输框架(Server/Client 模式)。
  • ATT (Attribute Protocol):GATT 的底层支撑,定义了数据操作(Read/Write/Notify)。

代码

zigbee

matter

Thread

Licensed under CC BY-NC-SA 4.0