Compare commits
7
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
555e0e5631 | ||
|
|
88c5107818 | ||
|
|
a8c83c519c | ||
|
|
8d8ac60256 | ||
|
|
8d0f9ec269 | ||
|
|
8b010a64a1 | ||
|
|
d6a605d6b7 |
No files matched your search
@@ -1,24 +0,0 @@
|
||||
FatFs License
|
||||
|
||||
FatFs has being developped as a personal project of the author, ChaN. It is free from the code anyone else wrote at current release. Following code block shows a copy of the FatFs license document that heading the source files.
|
||||
|
||||
/*----------------------------------------------------------------------------/
|
||||
/ FatFs - Generic FAT Filesystem Module Rx.xx /
|
||||
/-----------------------------------------------------------------------------/
|
||||
/
|
||||
/ Copyright (C) 20xx, ChaN, all right reserved.
|
||||
/
|
||||
/ FatFs module is an open source software. Redistribution and use of FatFs in
|
||||
/ source and binary forms, with or without modification, are permitted provided
|
||||
/ that the following condition is met:
|
||||
/
|
||||
/ 1. Redistributions of source code must retain the above copyright notice,
|
||||
/ this condition and the following disclaimer.
|
||||
/
|
||||
/ This software is provided by the copyright holder and contributors "AS IS"
|
||||
/ and any warranties related to this software are DISCLAIMED.
|
||||
/ The copyright owner or contributors be NOT LIABLE for any damages caused
|
||||
/ by use of this software.
|
||||
/----------------------------------------------------------------------------*/
|
||||
|
||||
Therefore FatFs license is one of the BSD-style licenses, but there is a significant feature. FatFs is mainly intended for embedded systems. In order to extend the usability for commercial products, the redistributions of FatFs in binary form, such as embedded code, binary library and any forms without source code, do not need to include about FatFs in the documentations. This is equivalent to the 1-clause BSD license. Of course FatFs is compatible with the most of open source software licenses include GNU GPL. When you redistribute the FatFs source code with changes or create a fork, the license can also be changed to GNU GPL, BSD-style license or any open source software license that not conflict with FatFs license.
|
||||
@@ -1,77 +0,0 @@
|
||||
/*-----------------------------------------------------------------------/
|
||||
/ Low level disk interface modlue include file (C)ChaN, 2019 /
|
||||
/-----------------------------------------------------------------------*/
|
||||
|
||||
#ifndef _DISKIO_DEFINED
|
||||
#define _DISKIO_DEFINED
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Status of Disk Functions */
|
||||
typedef BYTE DSTATUS;
|
||||
|
||||
/* Results of Disk Functions */
|
||||
typedef enum {
|
||||
RES_OK = 0, /* 0: Successful */
|
||||
RES_ERROR, /* 1: R/W Error */
|
||||
RES_WRPRT, /* 2: Write Protected */
|
||||
RES_NOTRDY, /* 3: Not Ready */
|
||||
RES_PARERR /* 4: Invalid Parameter */
|
||||
} DRESULT;
|
||||
|
||||
|
||||
/*---------------------------------------*/
|
||||
/* Prototypes for disk control functions */
|
||||
|
||||
|
||||
DSTATUS disk_initialize (BYTE pdrv);
|
||||
DSTATUS disk_status (BYTE pdrv);
|
||||
DRESULT disk_read (BYTE pdrv, BYTE* buff, LBA_t sector, UINT count);
|
||||
DRESULT disk_write (BYTE pdrv, const BYTE* buff, LBA_t sector, UINT count);
|
||||
DRESULT disk_ioctl (BYTE pdrv, BYTE cmd, void* buff);
|
||||
|
||||
|
||||
/* Disk Status Bits (DSTATUS) */
|
||||
|
||||
#define STA_NOINIT 0x01 /* Drive not initialized */
|
||||
#define STA_NODISK 0x02 /* No medium in the drive */
|
||||
#define STA_PROTECT 0x04 /* Write protected */
|
||||
|
||||
|
||||
/* Command code for disk_ioctrl fucntion */
|
||||
|
||||
/* Generic command (Used by FatFs) */
|
||||
#define CTRL_SYNC 0 /* Complete pending write process (needed at FF_FS_READONLY == 0) */
|
||||
#define GET_SECTOR_COUNT 1 /* Get media size (needed at FF_USE_MKFS == 1) */
|
||||
#define GET_SECTOR_SIZE 2 /* Get sector size (needed at FF_MAX_SS != FF_MIN_SS) */
|
||||
#define GET_BLOCK_SIZE 3 /* Get erase block size (needed at FF_USE_MKFS == 1) */
|
||||
#define CTRL_TRIM 4 /* Inform device that the data on the block of sectors is no longer used (needed at FF_USE_TRIM == 1) */
|
||||
|
||||
/* Generic command (Not used by FatFs) */
|
||||
#define CTRL_POWER 5 /* Get/Set power status */
|
||||
#define CTRL_LOCK 6 /* Lock/Unlock media removal */
|
||||
#define CTRL_EJECT 7 /* Eject media */
|
||||
#define CTRL_FORMAT 8 /* Create physical format on the media */
|
||||
|
||||
/* MMC/SDC specific ioctl command */
|
||||
#define MMC_GET_TYPE 10 /* Get card type */
|
||||
#define MMC_GET_CSD 11 /* Get CSD */
|
||||
#define MMC_GET_CID 12 /* Get CID */
|
||||
#define MMC_GET_OCR 13 /* Get OCR */
|
||||
#define MMC_GET_SDSTAT 14 /* Get SD status */
|
||||
#define ISDIO_READ 55 /* Read data form SD iSDIO register */
|
||||
#define ISDIO_WRITE 56 /* Write data to SD iSDIO register */
|
||||
#define ISDIO_MRITE 57 /* Masked write data to SD iSDIO register */
|
||||
|
||||
/* ATA/CF specific ioctl command */
|
||||
#define ATA_GET_REV 20 /* Get F/W revision */
|
||||
#define ATA_GET_MODEL 21 /* Get model name */
|
||||
#define ATA_GET_SN 22 /* Get serial number */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
-6982
File diff suppressed because it is too large.
Load diff
-422
@@ -1,422 +0,0 @@
|
||||
/*----------------------------------------------------------------------------/
|
||||
/ FatFs - Generic FAT Filesystem module R0.14b /
|
||||
/-----------------------------------------------------------------------------/
|
||||
/
|
||||
/ Copyright (C) 2021, ChaN, all right reserved.
|
||||
/
|
||||
/ FatFs module is an open source software. Redistribution and use of FatFs in
|
||||
/ source and binary forms, with or without modification, are permitted provided
|
||||
/ that the following condition is met:
|
||||
|
||||
/ 1. Redistributions of source code must retain the above copyright notice,
|
||||
/ this condition and the following disclaimer.
|
||||
/
|
||||
/ This software is provided by the copyright holder and contributors "AS IS"
|
||||
/ and any warranties related to this software are DISCLAIMED.
|
||||
/ The copyright owner or contributors be NOT LIABLE for any damages caused
|
||||
/ by use of this software.
|
||||
/
|
||||
/----------------------------------------------------------------------------*/
|
||||
|
||||
|
||||
#ifndef FF_DEFINED
|
||||
#define FF_DEFINED 86631 /* Revision ID */
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "ffconf.h" /* FatFs configuration options */
|
||||
|
||||
#if FF_DEFINED != FFCONF_DEF
|
||||
#error Wrong configuration file (ffconf.h).
|
||||
#endif
|
||||
|
||||
|
||||
/* Integer types used for FatFs API */
|
||||
|
||||
#if defined(_WIN32) /* Windows VC++ (for development only) */
|
||||
#define FF_INTDEF 2
|
||||
#include <windows.h>
|
||||
typedef unsigned __int64 QWORD;
|
||||
#include <float.h>
|
||||
#define isnan(v) _isnan(v)
|
||||
#define isinf(v) (!_finite(v))
|
||||
|
||||
#elif (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || defined(__cplusplus) /* C99 or later */
|
||||
#define FF_INTDEF 2
|
||||
#include <stdint.h>
|
||||
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 */
|
||||
|
||||
#else /* Earlier than C99 */
|
||||
#define FF_INTDEF 1
|
||||
typedef unsigned int UINT; /* int must be 16-bit or 32-bit */
|
||||
typedef unsigned char BYTE; /* char must be 8-bit */
|
||||
typedef unsigned short WORD; /* 16-bit unsigned integer */
|
||||
typedef unsigned long DWORD; /* 32-bit unsigned integer */
|
||||
typedef WORD WCHAR; /* UTF-16 character type */
|
||||
#endif
|
||||
|
||||
|
||||
/* Type of file size and LBA variables */
|
||||
|
||||
#if FF_FS_EXFAT
|
||||
#if FF_INTDEF != 2
|
||||
#error exFAT feature wants C99 or later
|
||||
#endif
|
||||
typedef QWORD FSIZE_t;
|
||||
#if FF_LBA64
|
||||
typedef QWORD LBA_t;
|
||||
#else
|
||||
typedef DWORD LBA_t;
|
||||
#endif
|
||||
#else
|
||||
#if FF_LBA64
|
||||
#error exFAT needs to be enabled when enable 64-bit LBA
|
||||
#endif
|
||||
typedef DWORD FSIZE_t;
|
||||
typedef DWORD LBA_t;
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/* Type of path name strings on FatFs API (TCHAR) */
|
||||
|
||||
#if FF_USE_LFN && FF_LFN_UNICODE == 1 /* Unicode in UTF-16 encoding */
|
||||
typedef WCHAR TCHAR;
|
||||
#define _T(x) L ## x
|
||||
#define _TEXT(x) L ## x
|
||||
#elif FF_USE_LFN && FF_LFN_UNICODE == 2 /* Unicode in UTF-8 encoding */
|
||||
typedef char TCHAR;
|
||||
#define _T(x) u8 ## x
|
||||
#define _TEXT(x) u8 ## x
|
||||
#elif FF_USE_LFN && FF_LFN_UNICODE == 3 /* Unicode in UTF-32 encoding */
|
||||
typedef DWORD TCHAR;
|
||||
#define _T(x) U ## x
|
||||
#define _TEXT(x) U ## x
|
||||
#elif FF_USE_LFN && (FF_LFN_UNICODE < 0 || FF_LFN_UNICODE > 3)
|
||||
#error Wrong FF_LFN_UNICODE setting
|
||||
#else /* ANSI/OEM code in SBCS/DBCS */
|
||||
typedef char TCHAR;
|
||||
#define _T(x) x
|
||||
#define _TEXT(x) x
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/* Definitions of volume management */
|
||||
|
||||
#if FF_MULTI_PARTITION /* Multiple partition configuration */
|
||||
typedef struct {
|
||||
BYTE pd; /* Physical drive number */
|
||||
BYTE pt; /* Partition: 0:Auto detect, 1-4:Forced partition) */
|
||||
} PARTITION;
|
||||
extern PARTITION VolToPart[]; /* Volume - Partition mapping table */
|
||||
#endif
|
||||
|
||||
#if FF_STR_VOLUME_ID
|
||||
#ifndef FF_VOLUME_STRS
|
||||
extern const char* VolumeStr[FF_VOLUMES]; /* User defied volume ID */
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/* Filesystem object structure (FATFS) */
|
||||
|
||||
typedef struct {
|
||||
BYTE fs_type; /* Filesystem type (0:not mounted) */
|
||||
BYTE pdrv; /* Associated physical drive */
|
||||
BYTE n_fats; /* Number of FATs (1 or 2) */
|
||||
BYTE wflag; /* win[] flag (b0:dirty) */
|
||||
BYTE fsi_flag; /* FSINFO flags (b7:disabled, b0:dirty) */
|
||||
WORD id; /* Volume mount ID */
|
||||
WORD n_rootdir; /* Number of root directory entries (FAT12/16) */
|
||||
WORD csize; /* Cluster size [sectors] */
|
||||
#if FF_MAX_SS != FF_MIN_SS
|
||||
WORD ssize; /* Sector size (512, 1024, 2048 or 4096) */
|
||||
#endif
|
||||
#if FF_USE_LFN
|
||||
WCHAR* lfnbuf; /* LFN working buffer */
|
||||
#endif
|
||||
#if FF_FS_EXFAT
|
||||
BYTE* dirbuf; /* Directory entry block scratchpad buffer for exFAT */
|
||||
#endif
|
||||
#if FF_FS_REENTRANT
|
||||
FF_SYNC_t sobj; /* Identifier of sync object */
|
||||
#endif
|
||||
#if !FF_FS_READONLY
|
||||
DWORD last_clst; /* Last allocated cluster */
|
||||
DWORD free_clst; /* Number of free clusters */
|
||||
#endif
|
||||
#if FF_FS_RPATH
|
||||
DWORD cdir; /* Current directory start cluster (0:root) */
|
||||
#if FF_FS_EXFAT
|
||||
DWORD cdc_scl; /* Containing directory start cluster (invalid when cdir is 0) */
|
||||
DWORD cdc_size; /* b31-b8:Size of containing directory, b7-b0: Chain status */
|
||||
DWORD cdc_ofs; /* Offset in the containing directory (invalid when cdir is 0) */
|
||||
#endif
|
||||
#endif
|
||||
DWORD n_fatent; /* Number of FAT entries (number of clusters + 2) */
|
||||
DWORD fsize; /* Size of an FAT [sectors] */
|
||||
LBA_t volbase; /* Volume base sector */
|
||||
LBA_t fatbase; /* FAT base sector */
|
||||
LBA_t dirbase; /* Root directory base sector/cluster */
|
||||
LBA_t database; /* Data base sector */
|
||||
#if FF_FS_EXFAT
|
||||
LBA_t bitbase; /* Allocation bitmap base sector */
|
||||
#endif
|
||||
LBA_t winsect; /* Current sector appearing in the win[] */
|
||||
BYTE win[FF_MAX_SS]; /* Disk access window for Directory, FAT (and file data at tiny cfg) */
|
||||
} FATFS;
|
||||
|
||||
|
||||
|
||||
/* Object ID and allocation information (FFOBJID) */
|
||||
|
||||
typedef struct {
|
||||
FATFS* fs; /* Pointer to the hosting volume of this object */
|
||||
WORD id; /* Hosting volume mount ID */
|
||||
BYTE attr; /* Object attribute */
|
||||
BYTE stat; /* Object chain status (b1-0: =0:not contiguous, =2:contiguous, =3:fragmented in this session, b2:sub-directory stretched) */
|
||||
DWORD sclust; /* Object data start cluster (0:no cluster or root directory) */
|
||||
FSIZE_t objsize; /* Object size (valid when sclust != 0) */
|
||||
#if FF_FS_EXFAT
|
||||
DWORD n_cont; /* Size of first fragment - 1 (valid when stat == 3) */
|
||||
DWORD n_frag; /* Size of last fragment needs to be written to FAT (valid when not zero) */
|
||||
DWORD c_scl; /* Containing directory start cluster (valid when sclust != 0) */
|
||||
DWORD c_size; /* b31-b8:Size of containing directory, b7-b0: Chain status (valid when c_scl != 0) */
|
||||
DWORD c_ofs; /* Offset in the containing directory (valid when file object and sclust != 0) */
|
||||
#endif
|
||||
#if FF_FS_LOCK
|
||||
UINT lockid; /* File lock ID origin from 1 (index of file semaphore table Files[]) */
|
||||
#endif
|
||||
} FFOBJID;
|
||||
|
||||
|
||||
|
||||
/* File object structure (FIL) */
|
||||
|
||||
typedef struct {
|
||||
FFOBJID obj; /* Object identifier (must be the 1st member to detect invalid object pointer) */
|
||||
BYTE flag; /* File status flags */
|
||||
BYTE err; /* Abort flag (error code) */
|
||||
FSIZE_t fptr; /* File read/write pointer (Zeroed on file open) */
|
||||
DWORD clust; /* Current cluster of fpter (invalid when fptr is 0) */
|
||||
LBA_t sect; /* Sector number appearing in buf[] (0:invalid) */
|
||||
#if !FF_FS_READONLY
|
||||
LBA_t dir_sect; /* Sector number containing the directory entry (not used at exFAT) */
|
||||
BYTE* dir_ptr; /* Pointer to the directory entry in the win[] (not used at exFAT) */
|
||||
#endif
|
||||
#if FF_USE_FASTSEEK
|
||||
DWORD* cltbl; /* Pointer to the cluster link map table (nulled on open, set by application) */
|
||||
#endif
|
||||
#if !FF_FS_TINY
|
||||
BYTE buf[FF_MAX_SS]; /* File private data read/write window */
|
||||
#endif
|
||||
} FIL;
|
||||
|
||||
|
||||
|
||||
/* Directory object structure (DIR) */
|
||||
|
||||
typedef struct {
|
||||
FFOBJID obj; /* Object identifier */
|
||||
DWORD dptr; /* Current read/write offset */
|
||||
DWORD clust; /* Current cluster */
|
||||
LBA_t sect; /* Current sector (0:Read operation has terminated) */
|
||||
BYTE* dir; /* Pointer to the directory item in the win[] */
|
||||
BYTE fn[12]; /* SFN (in/out) {body[8],ext[3],status[1]} */
|
||||
#if FF_USE_LFN
|
||||
DWORD blk_ofs; /* Offset of current entry block being processed (0xFFFFFFFF:Invalid) */
|
||||
#endif
|
||||
#if FF_USE_FIND
|
||||
const TCHAR* pat; /* Pointer to the name matching pattern */
|
||||
#endif
|
||||
} DIR;
|
||||
|
||||
|
||||
|
||||
/* File information structure (FILINFO) */
|
||||
|
||||
typedef struct {
|
||||
FSIZE_t fsize; /* File size */
|
||||
WORD fdate; /* Modified date */
|
||||
WORD ftime; /* Modified time */
|
||||
BYTE fattrib; /* File attribute */
|
||||
#if FF_USE_LFN
|
||||
TCHAR altname[FF_SFN_BUF + 1];/* Altenative file name */
|
||||
TCHAR fname[FF_LFN_BUF + 1]; /* Primary file name */
|
||||
#else
|
||||
TCHAR fname[12 + 1]; /* File name */
|
||||
#endif
|
||||
} FILINFO;
|
||||
|
||||
|
||||
|
||||
/* Format parameter structure (MKFS_PARM) */
|
||||
|
||||
typedef struct {
|
||||
BYTE fmt; /* Format option (FM_FAT, FM_FAT32, FM_EXFAT and FM_SFD) */
|
||||
BYTE n_fat; /* Number of FATs */
|
||||
UINT align; /* Data area alignment (sector) */
|
||||
UINT n_root; /* Number of root directory entries */
|
||||
DWORD au_size; /* Cluster size (byte) */
|
||||
} MKFS_PARM;
|
||||
|
||||
|
||||
|
||||
/* File function return code (FRESULT) */
|
||||
|
||||
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;
|
||||
|
||||
|
||||
|
||||
/*--------------------------------------------------------------*/
|
||||
/* FatFs module application interface */
|
||||
|
||||
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 */
|
||||
|
||||
#define f_eof(fp) ((int)((fp)->fptr == (fp)->obj.objsize))
|
||||
#define f_error(fp) ((fp)->err)
|
||||
#define f_tell(fp) ((fp)->fptr)
|
||||
#define f_size(fp) ((fp)->obj.objsize)
|
||||
#define f_rewind(fp) f_lseek((fp), 0)
|
||||
#define f_rewinddir(dp) f_readdir((dp), 0)
|
||||
#define f_rmdir(path) f_unlink(path)
|
||||
#define f_unmount(path) f_mount(0, path, 0)
|
||||
|
||||
|
||||
|
||||
|
||||
/*--------------------------------------------------------------*/
|
||||
/* Additional user defined functions */
|
||||
|
||||
/* RTC function */
|
||||
#if !FF_FS_READONLY && !FF_FS_NORTC
|
||||
DWORD get_fattime (void);
|
||||
#endif
|
||||
|
||||
/* LFN support functions */
|
||||
#if FF_USE_LFN >= 1 /* Code conversion (defined in unicode.c) */
|
||||
WCHAR ff_oem2uni (WCHAR oem, WORD cp); /* OEM code to Unicode conversion */
|
||||
WCHAR ff_uni2oem (DWORD uni, WORD cp); /* Unicode to OEM code conversion */
|
||||
DWORD ff_wtoupper (DWORD uni); /* Unicode upper-case conversion */
|
||||
#endif
|
||||
#if FF_USE_LFN == 3 /* Dynamic memory allocation */
|
||||
void* ff_memalloc (UINT msize); /* Allocate memory block */
|
||||
void ff_memfree (void* mblock); /* Free memory block */
|
||||
#endif
|
||||
|
||||
/* Sync functions */
|
||||
#if FF_FS_REENTRANT
|
||||
int ff_cre_syncobj (BYTE vol, FF_SYNC_t* sobj); /* Create a sync object */
|
||||
int ff_req_grant (FF_SYNC_t sobj); /* Lock sync object */
|
||||
void ff_rel_grant (FF_SYNC_t sobj); /* Unlock sync object */
|
||||
int ff_del_syncobj (FF_SYNC_t sobj); /* Delete a sync object */
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
/*--------------------------------------------------------------*/
|
||||
/* Flags and offset address */
|
||||
|
||||
|
||||
/* File access mode and open method flags (3rd argument of f_open) */
|
||||
#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
|
||||
|
||||
/* Fast seek controls (2nd argument of f_lseek) */
|
||||
#define CREATE_LINKMAP ((FSIZE_t)0 - 1)
|
||||
|
||||
/* Format options (2nd argument of f_mkfs) */
|
||||
#define FM_FAT 0x01
|
||||
#define FM_FAT32 0x02
|
||||
#define FM_EXFAT 0x04
|
||||
#define FM_ANY 0x07
|
||||
#define FM_SFD 0x08
|
||||
|
||||
/* Filesystem type (FATFS.fs_type) */
|
||||
#define FS_FAT12 1
|
||||
#define FS_FAT16 2
|
||||
#define FS_FAT32 3
|
||||
#define FS_EXFAT 4
|
||||
|
||||
/* File attribute bits for directory entry (FILINFO.fattrib) */
|
||||
#define AM_RDO 0x01 /* Read only */
|
||||
#define AM_HID 0x02 /* Hidden */
|
||||
#define AM_SYS 0x04 /* System */
|
||||
#define AM_DIR 0x10 /* Directory */
|
||||
#define AM_ARC 0x20 /* Archive */
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* FF_DEFINED */
|
||||
@@ -1,301 +0,0 @@
|
||||
/*---------------------------------------------------------------------------/
|
||||
/ FatFs Functional Configurations
|
||||
/---------------------------------------------------------------------------*/
|
||||
|
||||
#define FFCONF_DEF 86631 /* Revision ID */
|
||||
|
||||
/*---------------------------------------------------------------------------/
|
||||
/ Function Configurations
|
||||
/---------------------------------------------------------------------------*/
|
||||
|
||||
#define FF_FS_READONLY 0
|
||||
/* This option switches read-only configuration. (0:Read/Write or 1:Read-only)
|
||||
/ Read-only configuration removes writing API functions, f_write(), f_sync(),
|
||||
/ f_unlink(), f_mkdir(), f_chmod(), f_rename(), f_truncate(), f_getfree()
|
||||
/ and optional writing functions as well. */
|
||||
|
||||
|
||||
#define FF_FS_MINIMIZE 0
|
||||
/* This option defines minimization level to remove some basic API functions.
|
||||
/
|
||||
/ 0: Basic functions are fully enabled.
|
||||
/ 1: f_stat(), f_getfree(), f_unlink(), f_mkdir(), f_truncate() and f_rename()
|
||||
/ are removed.
|
||||
/ 2: f_opendir(), f_readdir() and f_closedir() are removed in addition to 1.
|
||||
/ 3: f_lseek() function is removed in addition to 2. */
|
||||
|
||||
|
||||
#define FF_USE_FIND 0
|
||||
/* This option switches filtered directory read functions, f_findfirst() and
|
||||
/ f_findnext(). (0:Disable, 1:Enable 2:Enable with matching altname[] too) */
|
||||
|
||||
|
||||
#define FF_USE_MKFS 0
|
||||
/* This option switches f_mkfs() function. (0:Disable or 1:Enable) */
|
||||
|
||||
|
||||
#define FF_USE_FASTSEEK 0
|
||||
/* This option switches fast seek function. (0:Disable or 1:Enable) */
|
||||
|
||||
|
||||
#define FF_USE_EXPAND 0
|
||||
/* This option switches f_expand function. (0:Disable or 1:Enable) */
|
||||
|
||||
|
||||
#define FF_USE_CHMOD 0
|
||||
/* This option switches attribute manipulation functions, f_chmod() and f_utime().
|
||||
/ (0:Disable or 1:Enable) Also FF_FS_READONLY needs to be 0 to enable this option. */
|
||||
|
||||
|
||||
#define FF_USE_LABEL 0
|
||||
/* This option switches volume label functions, f_getlabel() and f_setlabel().
|
||||
/ (0:Disable or 1:Enable) */
|
||||
|
||||
|
||||
#define FF_USE_FORWARD 0
|
||||
/* This option switches f_forward() function. (0:Disable or 1:Enable) */
|
||||
|
||||
|
||||
#define FF_USE_STRFUNC 0
|
||||
#define FF_PRINT_LLI 0
|
||||
#define FF_PRINT_FLOAT 0
|
||||
#define FF_STRF_ENCODE 0
|
||||
/* FF_USE_STRFUNC switches string functions, f_gets(), f_putc(), f_puts() and
|
||||
/ f_printf().
|
||||
/
|
||||
/ 0: Disable. FF_PRINT_LLI, FF_PRINT_FLOAT and FF_STRF_ENCODE have no effect.
|
||||
/ 1: Enable without LF-CRLF conversion.
|
||||
/ 2: Enable with LF-CRLF conversion.
|
||||
/
|
||||
/ FF_PRINT_LLI = 1 makes f_printf() support long long argument and FF_PRINT_FLOAT = 1/2
|
||||
makes f_printf() support floating point argument. These features want C99 or later.
|
||||
/ When FF_LFN_UNICODE >= 1 with LFN enabled, string functions convert the character
|
||||
/ encoding in it. FF_STRF_ENCODE selects assumption of character encoding ON THE FILE
|
||||
/ to be read/written via those functions.
|
||||
/
|
||||
/ 0: ANSI/OEM in current CP
|
||||
/ 1: Unicode in UTF-16LE
|
||||
/ 2: Unicode in UTF-16BE
|
||||
/ 3: Unicode in UTF-8
|
||||
*/
|
||||
|
||||
|
||||
/*---------------------------------------------------------------------------/
|
||||
/ Locale and Namespace Configurations
|
||||
/---------------------------------------------------------------------------*/
|
||||
|
||||
#define FF_CODE_PAGE 437
|
||||
/* This option specifies the OEM code page to be used on the target system.
|
||||
/ Incorrect code page setting can cause a file open failure.
|
||||
/
|
||||
/ 437 - U.S.
|
||||
/ 720 - Arabic
|
||||
/ 737 - Greek
|
||||
/ 771 - KBL
|
||||
/ 775 - Baltic
|
||||
/ 850 - Latin 1
|
||||
/ 852 - Latin 2
|
||||
/ 855 - Cyrillic
|
||||
/ 857 - Turkish
|
||||
/ 860 - Portuguese
|
||||
/ 861 - Icelandic
|
||||
/ 862 - Hebrew
|
||||
/ 863 - Canadian French
|
||||
/ 864 - Arabic
|
||||
/ 865 - Nordic
|
||||
/ 866 - Russian
|
||||
/ 869 - Greek 2
|
||||
/ 932 - Japanese (DBCS)
|
||||
/ 936 - Simplified Chinese (DBCS)
|
||||
/ 949 - Korean (DBCS)
|
||||
/ 950 - Traditional Chinese (DBCS)
|
||||
/ 0 - Include all code pages above and configured by f_setcp()
|
||||
*/
|
||||
|
||||
|
||||
#define FF_USE_LFN 0
|
||||
#define FF_MAX_LFN 255
|
||||
/* The FF_USE_LFN switches the support for LFN (long file name).
|
||||
/
|
||||
/ 0: Disable LFN. FF_MAX_LFN has no effect.
|
||||
/ 1: Enable LFN with static working buffer on the BSS. Always NOT thread-safe.
|
||||
/ 2: Enable LFN with dynamic working buffer on the STACK.
|
||||
/ 3: Enable LFN with dynamic working buffer on the HEAP.
|
||||
/
|
||||
/ To enable the LFN, ffunicode.c needs to be added to the project. The LFN function
|
||||
/ requiers certain internal working buffer occupies (FF_MAX_LFN + 1) * 2 bytes and
|
||||
/ additional (FF_MAX_LFN + 44) / 15 * 32 bytes when exFAT is enabled.
|
||||
/ The FF_MAX_LFN defines size of the working buffer in UTF-16 code unit and it can
|
||||
/ be in range of 12 to 255. It is recommended to be set it 255 to fully support LFN
|
||||
/ specification.
|
||||
/ When use stack for the working buffer, take care on stack overflow. When use heap
|
||||
/ memory for the working buffer, memory management functions, ff_memalloc() and
|
||||
/ ff_memfree() exemplified in ffsystem.c, need to be added to the project. */
|
||||
|
||||
|
||||
#define FF_LFN_UNICODE 0
|
||||
/* This option switches the character encoding on the API when LFN is enabled.
|
||||
/
|
||||
/ 0: ANSI/OEM in current CP (TCHAR = char)
|
||||
/ 1: Unicode in UTF-16 (TCHAR = WCHAR)
|
||||
/ 2: Unicode in UTF-8 (TCHAR = char)
|
||||
/ 3: Unicode in UTF-32 (TCHAR = DWORD)
|
||||
/
|
||||
/ Also behavior of string I/O functions will be affected by this option.
|
||||
/ When LFN is not enabled, this option has no effect. */
|
||||
|
||||
|
||||
#define FF_LFN_BUF 255
|
||||
#define FF_SFN_BUF 12
|
||||
/* This set of options defines size of file name members in the FILINFO structure
|
||||
/ which is used to read out directory items. These values should be suffcient for
|
||||
/ the file names to read. The maximum possible length of the read file name depends
|
||||
/ on character encoding. When LFN is not enabled, these options have no effect. */
|
||||
|
||||
|
||||
#define FF_FS_RPATH 0
|
||||
/* This option configures support for relative path.
|
||||
/
|
||||
/ 0: Disable relative path and remove related functions.
|
||||
/ 1: Enable relative path. f_chdir() and f_chdrive() are available.
|
||||
/ 2: f_getcwd() function is available in addition to 1.
|
||||
*/
|
||||
|
||||
|
||||
/*---------------------------------------------------------------------------/
|
||||
/ Drive/Volume Configurations
|
||||
/---------------------------------------------------------------------------*/
|
||||
|
||||
#define FF_VOLUMES 1
|
||||
/* Number of volumes (logical drives) to be used. (1-10) */
|
||||
|
||||
|
||||
#define FF_STR_VOLUME_ID 0
|
||||
#define FF_VOLUME_STRS "RAM","NAND","CF","SD","SD2","USB","USB2","USB3"
|
||||
/* FF_STR_VOLUME_ID switches support for volume ID in arbitrary strings.
|
||||
/ When FF_STR_VOLUME_ID is set to 1 or 2, arbitrary strings can be used as drive
|
||||
/ number in the path name. FF_VOLUME_STRS defines the volume ID strings for each
|
||||
/ logical drives. Number of items must not be less than FF_VOLUMES. Valid
|
||||
/ characters for the volume ID strings are A-Z, a-z and 0-9, however, they are
|
||||
/ compared in case-insensitive. If FF_STR_VOLUME_ID >= 1 and FF_VOLUME_STRS is
|
||||
/ not defined, a user defined volume string table needs to be defined as:
|
||||
/
|
||||
/ const char* VolumeStr[FF_VOLUMES] = {"ram","flash","sd","usb",...
|
||||
*/
|
||||
|
||||
|
||||
#define FF_MULTI_PARTITION 0
|
||||
/* This option switches support for multiple volumes on the physical drive.
|
||||
/ By default (0), each logical drive number is bound to the same physical drive
|
||||
/ number and only an FAT volume found on the physical drive will be mounted.
|
||||
/ When this function is enabled (1), each logical drive number can be bound to
|
||||
/ arbitrary physical drive and partition listed in the VolToPart[]. Also f_fdisk()
|
||||
/ funciton will be available. */
|
||||
|
||||
|
||||
#define FF_MIN_SS 512
|
||||
#define FF_MAX_SS 512
|
||||
/* This set of options configures the range of sector size to be supported. (512,
|
||||
/ 1024, 2048 or 4096) Always set both 512 for most systems, generic memory card and
|
||||
/ harddisk, but a larger value may be required for on-board flash memory and some
|
||||
/ type of optical media. When FF_MAX_SS is larger than FF_MIN_SS, FatFs is configured
|
||||
/ for variable sector size mode and disk_ioctl() function needs to implement
|
||||
/ GET_SECTOR_SIZE command. */
|
||||
|
||||
|
||||
#define FF_LBA64 0
|
||||
/* This option switches support for 64-bit LBA. (0:Disable or 1:Enable)
|
||||
/ To enable the 64-bit LBA, also exFAT needs to be enabled. (FF_FS_EXFAT == 1) */
|
||||
|
||||
|
||||
#define FF_MIN_GPT 0x10000000
|
||||
/* Minimum number of sectors to switch GPT as partitioning format in f_mkfs and
|
||||
/ f_fdisk function. 0x100000000 max. This option has no effect when FF_LBA64 == 0. */
|
||||
|
||||
|
||||
#define FF_USE_TRIM 0
|
||||
/* This option switches support for ATA-TRIM. (0:Disable or 1:Enable)
|
||||
/ To enable Trim function, also CTRL_TRIM command should be implemented to the
|
||||
/ disk_ioctl() function. */
|
||||
|
||||
|
||||
|
||||
/*---------------------------------------------------------------------------/
|
||||
/ System Configurations
|
||||
/---------------------------------------------------------------------------*/
|
||||
|
||||
#define FF_FS_TINY 1
|
||||
/* This option switches tiny buffer configuration. (0:Normal or 1:Tiny)
|
||||
/ At the tiny configuration, size of file object (FIL) is shrinked FF_MAX_SS bytes.
|
||||
/ Instead of private sector buffer eliminated from the file object, common sector
|
||||
/ buffer in the filesystem object (FATFS) is used for the file data transfer. */
|
||||
|
||||
|
||||
#define FF_FS_EXFAT 0
|
||||
/* This option switches support for exFAT filesystem. (0:Disable or 1:Enable)
|
||||
/ To enable exFAT, also LFN needs to be enabled. (FF_USE_LFN >= 1)
|
||||
/ Note that enabling exFAT discards ANSI C (C89) compatibility. */
|
||||
|
||||
|
||||
#define FF_FS_NORTC 1
|
||||
#define FF_NORTC_MON 1
|
||||
#define FF_NORTC_MDAY 1
|
||||
#define FF_NORTC_YEAR 2020
|
||||
/* The option FF_FS_NORTC switches timestamp functiton. If the system does not have
|
||||
/ any RTC function or valid timestamp is not needed, set FF_FS_NORTC = 1 to disable
|
||||
/ the timestamp function. Every object modified by FatFs will have a fixed timestamp
|
||||
/ defined by FF_NORTC_MON, FF_NORTC_MDAY and FF_NORTC_YEAR in local time.
|
||||
/ To enable timestamp function (FF_FS_NORTC = 0), get_fattime() function need to be
|
||||
/ added to the project to read current time form real-time clock. FF_NORTC_MON,
|
||||
/ FF_NORTC_MDAY and FF_NORTC_YEAR have no effect.
|
||||
/ These options have no effect in read-only configuration (FF_FS_READONLY = 1). */
|
||||
|
||||
|
||||
#define FF_FS_NOFSINFO 0
|
||||
/* If you need to know correct free space on the FAT32 volume, set bit 0 of this
|
||||
/ option, and f_getfree() function at first time after volume mount will force
|
||||
/ a full FAT scan. Bit 1 controls the use of last allocated cluster number.
|
||||
/
|
||||
/ bit0=0: Use free cluster count in the FSINFO if available.
|
||||
/ bit0=1: Do not trust free cluster count in the FSINFO.
|
||||
/ bit1=0: Use last allocated cluster number in the FSINFO if available.
|
||||
/ bit1=1: Do not trust last allocated cluster number in the FSINFO.
|
||||
*/
|
||||
|
||||
|
||||
#define FF_FS_LOCK 0
|
||||
/* The option FF_FS_LOCK switches file lock function to control duplicated file open
|
||||
/ and illegal operation to open objects. This option must be 0 when FF_FS_READONLY
|
||||
/ is 1.
|
||||
/
|
||||
/ 0: Disable file lock function. To avoid volume corruption, application program
|
||||
/ should avoid illegal open, remove and rename to the open objects.
|
||||
/ >0: Enable file lock function. The value defines how many files/sub-directories
|
||||
/ can be opened simultaneously under file lock control. Note that the file
|
||||
/ lock control is independent of re-entrancy. */
|
||||
|
||||
|
||||
/* #include <somertos.h> // O/S definitions */
|
||||
#define FF_FS_REENTRANT 0
|
||||
#define FF_FS_TIMEOUT 1000
|
||||
#define FF_SYNC_t HANDLE
|
||||
/* The option FF_FS_REENTRANT switches the re-entrancy (thread safe) of the FatFs
|
||||
/ module itself. Note that regardless of this option, file access to different
|
||||
/ volume is always re-entrant and volume control functions, f_mount(), f_mkfs()
|
||||
/ and f_fdisk() function, are always not re-entrant. Only file/directory access
|
||||
/ to the same volume is under control of this function.
|
||||
/
|
||||
/ 0: Disable re-entrancy. FF_FS_TIMEOUT and FF_SYNC_t have no effect.
|
||||
/ 1: Enable re-entrancy. Also user provided synchronization handlers,
|
||||
/ ff_req_grant(), ff_rel_grant(), ff_del_syncobj() and ff_cre_syncobj()
|
||||
/ function, must be added to the project. Samples are available in
|
||||
/ option/syscall.c.
|
||||
/
|
||||
/ The FF_FS_TIMEOUT defines timeout period in unit of time tick.
|
||||
/ The FF_SYNC_t defines O/S dependent sync object type. e.g. HANDLE, ID, OS_EVENT*,
|
||||
/ SemaphoreHandle_t and etc. A header file for O/S definitions needs to be
|
||||
/ included somewhere in the scope of ff.h. */
|
||||
|
||||
|
||||
|
||||
/*--- End of configuration options ---*/
|
||||
@@ -1,170 +0,0 @@
|
||||
/*------------------------------------------------------------------------*/
|
||||
/* Sample Code of OS Dependent Functions for FatFs */
|
||||
/* (C)ChaN, 2018 */
|
||||
/*------------------------------------------------------------------------*/
|
||||
|
||||
|
||||
#include "ff.h"
|
||||
|
||||
|
||||
#if FF_USE_LFN == 3 /* Dynamic memory allocation */
|
||||
|
||||
/*------------------------------------------------------------------------*/
|
||||
/* Allocate a memory block */
|
||||
/*------------------------------------------------------------------------*/
|
||||
|
||||
void* ff_memalloc ( /* Returns pointer to the allocated memory block (null if not enough core) */
|
||||
UINT msize /* Number of bytes to allocate */
|
||||
)
|
||||
{
|
||||
return malloc(msize); /* Allocate a new memory block with POSIX API */
|
||||
}
|
||||
|
||||
|
||||
/*------------------------------------------------------------------------*/
|
||||
/* Free a memory block */
|
||||
/*------------------------------------------------------------------------*/
|
||||
|
||||
void ff_memfree (
|
||||
void* mblock /* Pointer to the memory block to free (nothing to do if null) */
|
||||
)
|
||||
{
|
||||
free(mblock); /* Free the memory block with POSIX API */
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#if FF_FS_REENTRANT /* Mutal exclusion */
|
||||
|
||||
/*------------------------------------------------------------------------*/
|
||||
/* Create a Synchronization Object */
|
||||
/*------------------------------------------------------------------------*/
|
||||
/* This function is called in f_mount() function to create a new
|
||||
/ synchronization object for the volume, such as semaphore and mutex.
|
||||
/ When a 0 is returned, the f_mount() function fails with FR_INT_ERR.
|
||||
*/
|
||||
|
||||
//const osMutexDef_t Mutex[FF_VOLUMES]; /* Table of CMSIS-RTOS mutex */
|
||||
|
||||
|
||||
int ff_cre_syncobj ( /* 1:Function succeeded, 0:Could not create the sync object */
|
||||
BYTE vol, /* Corresponding volume (logical drive number) */
|
||||
FF_SYNC_t* sobj /* Pointer to return the created sync object */
|
||||
)
|
||||
{
|
||||
/* Win32 */
|
||||
*sobj = CreateMutex(NULL, FALSE, NULL);
|
||||
return (int)(*sobj != INVALID_HANDLE_VALUE);
|
||||
|
||||
/* uITRON */
|
||||
// T_CSEM csem = {TA_TPRI,1,1};
|
||||
// *sobj = acre_sem(&csem);
|
||||
// return (int)(*sobj > 0);
|
||||
|
||||
/* uC/OS-II */
|
||||
// OS_ERR err;
|
||||
// *sobj = OSMutexCreate(0, &err);
|
||||
// return (int)(err == OS_NO_ERR);
|
||||
|
||||
/* FreeRTOS */
|
||||
// *sobj = xSemaphoreCreateMutex();
|
||||
// return (int)(*sobj != NULL);
|
||||
|
||||
/* CMSIS-RTOS */
|
||||
// *sobj = osMutexCreate(&Mutex[vol]);
|
||||
// return (int)(*sobj != NULL);
|
||||
}
|
||||
|
||||
|
||||
/*------------------------------------------------------------------------*/
|
||||
/* Delete a Synchronization Object */
|
||||
/*------------------------------------------------------------------------*/
|
||||
/* This function is called in f_mount() function to delete a synchronization
|
||||
/ object that created with ff_cre_syncobj() function. When a 0 is returned,
|
||||
/ the f_mount() function fails with FR_INT_ERR.
|
||||
*/
|
||||
|
||||
int ff_del_syncobj ( /* 1:Function succeeded, 0:Could not delete due to an error */
|
||||
FF_SYNC_t sobj /* Sync object tied to the logical drive to be deleted */
|
||||
)
|
||||
{
|
||||
/* Win32 */
|
||||
return (int)CloseHandle(sobj);
|
||||
|
||||
/* uITRON */
|
||||
// return (int)(del_sem(sobj) == E_OK);
|
||||
|
||||
/* uC/OS-II */
|
||||
// OS_ERR err;
|
||||
// OSMutexDel(sobj, OS_DEL_ALWAYS, &err);
|
||||
// return (int)(err == OS_NO_ERR);
|
||||
|
||||
/* FreeRTOS */
|
||||
// vSemaphoreDelete(sobj);
|
||||
// return 1;
|
||||
|
||||
/* CMSIS-RTOS */
|
||||
// return (int)(osMutexDelete(sobj) == osOK);
|
||||
}
|
||||
|
||||
|
||||
/*------------------------------------------------------------------------*/
|
||||
/* Request Grant to Access the Volume */
|
||||
/*------------------------------------------------------------------------*/
|
||||
/* This function is called on entering file functions to lock the volume.
|
||||
/ When a 0 is returned, the file function fails with FR_TIMEOUT.
|
||||
*/
|
||||
|
||||
int ff_req_grant ( /* 1:Got a grant to access the volume, 0:Could not get a grant */
|
||||
FF_SYNC_t sobj /* Sync object to wait */
|
||||
)
|
||||
{
|
||||
/* Win32 */
|
||||
return (int)(WaitForSingleObject(sobj, FF_FS_TIMEOUT) == WAIT_OBJECT_0);
|
||||
|
||||
/* uITRON */
|
||||
// return (int)(wai_sem(sobj) == E_OK);
|
||||
|
||||
/* uC/OS-II */
|
||||
// OS_ERR err;
|
||||
// OSMutexPend(sobj, FF_FS_TIMEOUT, &err));
|
||||
// return (int)(err == OS_NO_ERR);
|
||||
|
||||
/* FreeRTOS */
|
||||
// return (int)(xSemaphoreTake(sobj, FF_FS_TIMEOUT) == pdTRUE);
|
||||
|
||||
/* CMSIS-RTOS */
|
||||
// return (int)(osMutexWait(sobj, FF_FS_TIMEOUT) == osOK);
|
||||
}
|
||||
|
||||
|
||||
/*------------------------------------------------------------------------*/
|
||||
/* Release Grant to Access the Volume */
|
||||
/*------------------------------------------------------------------------*/
|
||||
/* This function is called on leaving file functions to unlock the volume.
|
||||
*/
|
||||
|
||||
void ff_rel_grant (
|
||||
FF_SYNC_t sobj /* Sync object to be signaled */
|
||||
)
|
||||
{
|
||||
/* Win32 */
|
||||
ReleaseMutex(sobj);
|
||||
|
||||
/* uITRON */
|
||||
// sig_sem(sobj);
|
||||
|
||||
/* uC/OS-II */
|
||||
// OSMutexPost(sobj);
|
||||
|
||||
/* FreeRTOS */
|
||||
// xSemaphoreGive(sobj);
|
||||
|
||||
/* CMSIS-RTOS */
|
||||
// osMutexRelease(sobj);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
-15593
File diff suppressed because it is too large.
Load diff
@@ -300,70 +300,6 @@ int32_t button_gpio = %d, button_high = %d, button_pullup = %d; // "%s"
|
||||
|
||||
Handlers.append(HandleButton())
|
||||
|
||||
######################################################################
|
||||
# SDCARD SPI CS Pin
|
||||
######################################################################
|
||||
|
||||
class HandleSdcardChipSelectPin:
|
||||
def __init__(self):
|
||||
self.pin = None
|
||||
self.ctr_dispatch = { 'DECL_SD_SPI_CS_PIN': self.decl_cs_pin }
|
||||
def decl_cs_pin(self, req):
|
||||
pin = req.split(None, 1)[1].strip()
|
||||
if pin.startswith('"') and pin.endswith('"'):
|
||||
pin = pin[1:-1].strip()
|
||||
self.pin = pin
|
||||
def generate_code(self, options):
|
||||
cs_gpio = 0
|
||||
pin = self.pin
|
||||
if pin:
|
||||
cs_gpio = HandlerConstants.lookup_pin(pin)
|
||||
fmt = """
|
||||
uint32_t sdcard_cs_gpio = %d; // "%s"
|
||||
"""
|
||||
return fmt % (cs_gpio, self.pin)
|
||||
|
||||
Handlers.append(HandleSdcardChipSelectPin())
|
||||
|
||||
######################################################################
|
||||
# SDCARD SPI CS Pin
|
||||
######################################################################
|
||||
|
||||
class HandleSoftwareSPIPins:
|
||||
def __init__(self):
|
||||
self.pins = {}
|
||||
self.ctr_dispatch = {
|
||||
'DECL_SWSPI_MOSI_PIN': self.decl_mosi_pin,
|
||||
'DECL_SWSPI_MISO_PIN': self.decl_miso_pin,
|
||||
'DECL_SWSPI_SCLK_PIN': self.decl_sclk_pin
|
||||
}
|
||||
def _parse_pin(self, req):
|
||||
pin = req.split(None, 1)[1].strip()
|
||||
if pin.startswith('"') and pin.endswith('"'):
|
||||
pin = pin[1:-1].strip()
|
||||
return pin
|
||||
def decl_miso_pin(self, req):
|
||||
self.pins["miso"] = self._parse_pin(req)
|
||||
def decl_mosi_pin(self, req):
|
||||
self.pins["mosi"] = self._parse_pin(req)
|
||||
def decl_sclk_pin(self, req):
|
||||
self.pins["sclk"] = self._parse_pin(req)
|
||||
def generate_code(self, options):
|
||||
args = []
|
||||
for name in ["miso", "mosi", "sclk"]:
|
||||
gpio = 0
|
||||
pin = self.pins.get(name)
|
||||
if pin:
|
||||
gpio = HandlerConstants.lookup_pin(pin)
|
||||
args.extend([gpio, pin])
|
||||
fmt = """
|
||||
uint32_t swspi_miso_gpio = %d; // "%s"
|
||||
uint32_t swspi_mosi_gpio = %d; // "%s"
|
||||
uint32_t swspi_sclk_gpio = %d; // "%s"
|
||||
"""
|
||||
return fmt % (*args,)
|
||||
|
||||
Handlers.append(HandleSoftwareSPIPins())
|
||||
|
||||
######################################################################
|
||||
# Main code
|
||||
|
||||
+23
-12
@@ -1,6 +1,8 @@
|
||||
# Main Kconfig settings
|
||||
|
||||
mainmenu "CanBoot Configuration"
|
||||
VERSION := $(shell, git describe --tags --long --always --dirty 2> /dev/null)
|
||||
|
||||
mainmenu "CanBoot Configuration $(VERSION)"
|
||||
|
||||
config LOW_LEVEL_OPTIONS
|
||||
bool
|
||||
@@ -18,6 +20,8 @@ source "src/stm32/Kconfig"
|
||||
source "src/lpc176x/Kconfig"
|
||||
|
||||
# Generic configuration options for serial ports
|
||||
config SERIAL
|
||||
bool
|
||||
config SERIAL_BAUD
|
||||
depends on SERIAL
|
||||
int "Baud rate for serial port" if LOW_LEVEL_OPTIONS
|
||||
@@ -27,12 +31,14 @@ config SERIAL_BAUD
|
||||
to 250000. Read the FAQ before changing this value.
|
||||
|
||||
# Generic configuration options for USB
|
||||
config USBSERIAL
|
||||
bool
|
||||
config USB_VENDOR_ID
|
||||
default 0x1d50
|
||||
config USB_DEVICE_ID
|
||||
default 0x6177
|
||||
config USB_SERIAL_NUMBER_CHIPID
|
||||
depends on HAVE_CHIPID
|
||||
depends on HAVE_CHIPID && USBSERIAL
|
||||
default y
|
||||
config USB_SERIAL_NUMBER
|
||||
default "12345"
|
||||
@@ -40,15 +46,28 @@ config USB_SERIAL_NUMBER
|
||||
menu "USB ids"
|
||||
depends on USBSERIAL && LOW_LEVEL_OPTIONS
|
||||
config USB_VENDOR_ID
|
||||
hex "USB vendor ID"
|
||||
hex "USB vendor ID" if USBSERIAL
|
||||
config USB_DEVICE_ID
|
||||
hex "USB device ID"
|
||||
hex "USB device ID" if USBSERIAL
|
||||
config USB_SERIAL_NUMBER_CHIPID
|
||||
bool "USB serial number from CHIPID" if HAVE_CHIPID
|
||||
config USB_SERIAL_NUMBER
|
||||
string "USB serial number" if !USB_SERIAL_NUMBER_CHIPID
|
||||
endmenu
|
||||
|
||||
# Generic configuration options for CANbus
|
||||
config CANSERIAL
|
||||
bool
|
||||
config CANBUS
|
||||
bool
|
||||
default y if CANSERIAL
|
||||
config CANBUS_FREQUENCY
|
||||
int "CAN bus speed" if CANBUS
|
||||
default 500000
|
||||
config CANBUS_FILTER
|
||||
bool
|
||||
default y if CANSERIAL
|
||||
|
||||
config INITIAL_PINS
|
||||
string "GPIO pins to set on bootloader entry"
|
||||
depends on LOW_LEVEL_OPTIONS
|
||||
@@ -85,11 +104,3 @@ config STATUS_LED_PIN
|
||||
config HAVE_CHIPID
|
||||
bool
|
||||
default n
|
||||
|
||||
config HAVE_GPIO_SPI
|
||||
bool
|
||||
default n
|
||||
|
||||
config ENABLE_SDCARD
|
||||
bool
|
||||
default n
|
||||
@@ -2,9 +2,3 @@
|
||||
|
||||
src-y += sched.c bootentry.c command.c flashcmd.c initial_pins.c
|
||||
src-$(CONFIG_ENABLE_LED) += led.c
|
||||
src-$(CONFIG_ENABLE_SDCARD) += ../lib/fatfs/ff.c sdcard.c
|
||||
src-$(CONFIG_SDCARD_SPI) += sdcard_spi.c
|
||||
src-$(CONFIG_SDCARD_SOFTWARE_SPI) += spi_software.c
|
||||
|
||||
dirs-$(CONFIG_ENABLE_SDCARD) += lib/fatfs
|
||||
CFLAGS += -Ilib/fatfs
|
||||
@@ -10,14 +10,6 @@
|
||||
#include "byteorder.h" // cpu_to_le32
|
||||
#include "command.h" // send_ack
|
||||
|
||||
static uint8_t command_enable = 1;
|
||||
|
||||
void
|
||||
command_set_enable(uint8_t state)
|
||||
{
|
||||
command_enable = state;
|
||||
}
|
||||
|
||||
uint_fast8_t
|
||||
command_encode_and_frame(uint8_t *buf, const struct command_encoder *ce
|
||||
, va_list args)
|
||||
@@ -60,13 +52,6 @@ command_respond_nack(void)
|
||||
command_respond(out, RESPONSE_NACK, ARRAY_SIZE(out));
|
||||
}
|
||||
|
||||
static void
|
||||
command_respond_busy(void)
|
||||
{
|
||||
uint32_t out[2];
|
||||
command_respond(out, RESPONSE_COMMAND_BUSY, ARRAY_SIZE(out));
|
||||
}
|
||||
|
||||
int
|
||||
command_get_arg_count(uint32_t *data)
|
||||
{
|
||||
@@ -77,12 +62,6 @@ command_get_arg_count(uint32_t *data)
|
||||
void
|
||||
command_dispatch(uint8_t *buf, uint_fast8_t msglen)
|
||||
{
|
||||
if (CONFIG_ENABLE_SDCARD) {
|
||||
if (!command_enable) {
|
||||
command_respond_busy();
|
||||
return;
|
||||
}
|
||||
}
|
||||
uint32_t data[DIV_ROUND_UP(MESSAGE_MAX, 4)];
|
||||
memcpy(data, buf, msglen);
|
||||
uint32_t cmd = (le32_to_cpu(data[0]) >> 16) & 0xff;
|
||||
|
||||
@@ -28,7 +28,6 @@
|
||||
#define RESPONSE_ACK 0xa0
|
||||
#define RESPONSE_NACK 0xf1
|
||||
#define RESPONSE_COMMAND_ERROR 0xf2
|
||||
#define RESPONSE_COMMAND_BUSY 0xf3
|
||||
|
||||
// Command Format:
|
||||
// <2 byte header> <1 byte cmd> <1 byte data word count> <data> <2 byte crc> <2 byte trailer>
|
||||
@@ -59,7 +58,6 @@ void command_get_canbus_id(uint32_t *data);
|
||||
void command_respond_ack(uint32_t acked_cmd, uint32_t *out, uint32_t out_len);
|
||||
void command_respond_command_error(void);
|
||||
int command_get_arg_count(uint32_t *data);
|
||||
void command_set_enable(uint8_t state);
|
||||
|
||||
struct command_encoder {
|
||||
uint32_t *data;
|
||||
|
||||
+2
-15
@@ -35,19 +35,13 @@ command_connect(uint32_t *data)
|
||||
static uint8_t complete;
|
||||
static uint32_t complete_endtime;
|
||||
|
||||
void
|
||||
set_complete(void)
|
||||
{
|
||||
complete = 1;
|
||||
complete_endtime = timer_read_time() + timer_from_us(100000);
|
||||
}
|
||||
|
||||
void
|
||||
command_complete(uint32_t *data)
|
||||
{
|
||||
uint32_t out[3];
|
||||
command_respond_ack(CMD_COMPLETE, out, ARRAY_SIZE(out));
|
||||
set_complete();
|
||||
complete = 1;
|
||||
complete_endtime = timer_read_time() + timer_from_us(100000);
|
||||
}
|
||||
|
||||
void
|
||||
@@ -65,13 +59,6 @@ DECL_TASK(complete_task);
|
||||
|
||||
static uint8_t is_in_transfer;
|
||||
|
||||
void
|
||||
set_in_transfer(uint8_t in_transfer)
|
||||
{
|
||||
is_in_transfer = in_transfer;
|
||||
}
|
||||
|
||||
|
||||
int
|
||||
flashcmd_is_in_transfer(void)
|
||||
{
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
#ifndef __FLASHCMD_H
|
||||
#define __FLASHCMD_H
|
||||
|
||||
void set_complete(void);
|
||||
int flashcmd_is_in_transfer(void);
|
||||
void set_in_transfer(uint8_t in_transfer);
|
||||
|
||||
#endif // flashcmd.h
|
||||
@@ -19,6 +19,9 @@ extern uint32_t _data_start, _data_end, _data_flash;
|
||||
extern uint32_t _bss_start, _bss_end, _stack_start;
|
||||
extern uint32_t _stack_end;
|
||||
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Warray-bounds"
|
||||
|
||||
uint64_t
|
||||
get_bootup_code(void)
|
||||
{
|
||||
@@ -34,6 +37,8 @@ set_bootup_code(uint64_t code)
|
||||
barrier();
|
||||
}
|
||||
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
// Helper function to read area of flash
|
||||
void
|
||||
application_read_flash(uint32_t address, uint32_t *dest)
|
||||
|
||||
+17
-276
@@ -1,291 +1,32 @@
|
||||
// Generic handling of serial over CAN support
|
||||
// Wrapper functions connecting canserial.c to low-level can hardware
|
||||
//
|
||||
// Copyright (C) 2019 Eug Krashtan <eug.krashtan@gmail.com>
|
||||
// Copyright (C) 2020 Pontus Borg <glpontus@gmail.com>
|
||||
// Copyright (C) 2021 Kevin O'Connor <kevin@koconnor.net>
|
||||
// Copyright (C) 2022 Kevin O'Connor <kevin@koconnor.net>
|
||||
//
|
||||
// This file may be distributed under the terms of the GNU GPLv3 license.
|
||||
|
||||
#include <string.h> // memcpy
|
||||
#include "canbus.h" // canbus_set_uuid
|
||||
#include "command.h" // DECL_CONSTANT
|
||||
#include "generic/io.h" // readb
|
||||
#include "generic/irq.h" // irq_disable
|
||||
#include "sched.h" // sched_wake_task
|
||||
#include "canbus.h" // canbus_send
|
||||
#include "canserial.h" // canserial_send
|
||||
|
||||
static uint32_t canbus_assigned_id;
|
||||
static uint8_t canbus_uuid[CANBUS_UUID_LEN];
|
||||
int
|
||||
canserial_send(struct canbus_msg *msg)
|
||||
{
|
||||
return canbus_send(msg);
|
||||
}
|
||||
|
||||
|
||||
/****************************************************************
|
||||
* Data transmission over CAN
|
||||
****************************************************************/
|
||||
|
||||
static struct task_wake canbus_tx_wake;
|
||||
static uint8_t transmit_buf[96], transmit_pos, transmit_max;
|
||||
void
|
||||
canserial_set_filter(uint32_t id)
|
||||
{
|
||||
canbus_set_filter(id);
|
||||
}
|
||||
|
||||
void
|
||||
canbus_notify_tx(void)
|
||||
{
|
||||
sched_wake_task(&canbus_tx_wake);
|
||||
canserial_notify_tx();
|
||||
}
|
||||
|
||||
void
|
||||
canbus_tx_task(void)
|
||||
canbus_process_data(struct canbus_msg *msg)
|
||||
{
|
||||
if (!sched_check_wake(&canbus_tx_wake))
|
||||
return;
|
||||
uint32_t id = canbus_assigned_id;
|
||||
if (!id) {
|
||||
transmit_pos = transmit_max = 0;
|
||||
return;
|
||||
}
|
||||
uint32_t tpos = transmit_pos, tmax = transmit_max;
|
||||
for (;;) {
|
||||
int avail = tmax - tpos, now = avail > 8 ? 8 : avail;
|
||||
if (avail <= 0)
|
||||
break;
|
||||
int ret = canbus_send(id + 1, now, &transmit_buf[tpos]);
|
||||
if (ret <= 0)
|
||||
break;
|
||||
tpos += now;
|
||||
}
|
||||
transmit_pos = tpos;
|
||||
canserial_process_data(msg);
|
||||
}
|
||||
DECL_TASK(canbus_tx_task);
|
||||
|
||||
// Encode and transmit a "response" message
|
||||
void
|
||||
console_sendf(const struct command_encoder *ce, va_list args)
|
||||
{
|
||||
// Verify space for message
|
||||
uint32_t tpos = transmit_pos, tmax = transmit_max;
|
||||
if (tpos >= tmax)
|
||||
transmit_pos = transmit_max = tpos = tmax = 0;
|
||||
uint32_t max_size = ce->max_size;
|
||||
if (tmax + max_size > sizeof(transmit_buf)) {
|
||||
if (tmax + max_size - tpos > sizeof(transmit_buf))
|
||||
// Not enough space for message
|
||||
return;
|
||||
// Move buffer
|
||||
tmax -= tpos;
|
||||
memmove(&transmit_buf[0], &transmit_buf[tpos], tmax);
|
||||
transmit_pos = tpos = 0;
|
||||
transmit_max = tmax;
|
||||
}
|
||||
|
||||
// Generate message
|
||||
uint32_t msglen = command_encode_and_frame(&transmit_buf[tmax], ce, args);
|
||||
|
||||
// Start message transmit
|
||||
transmit_max = tmax + msglen;
|
||||
canbus_notify_tx();
|
||||
}
|
||||
|
||||
|
||||
/****************************************************************
|
||||
* CAN "admin" command handling
|
||||
****************************************************************/
|
||||
|
||||
// Available commands and responses
|
||||
#define CANBUS_CMD_QUERY_UNASSIGNED 0x00
|
||||
#define CANBUS_CMD_SET_CANBOOT_NODEID 0x11
|
||||
#define CANBUS_CMD_CLEAR_CANBOOT_NODEID 0x12
|
||||
#define CANBUS_RESP_NEED_NODEID 0x20
|
||||
|
||||
// Helper to verify a UUID in a command matches this chip's UUID
|
||||
static int
|
||||
can_check_uuid(uint32_t id, uint32_t len, uint8_t *data)
|
||||
{
|
||||
return len >= 7 && memcmp(&data[1], canbus_uuid, sizeof(canbus_uuid)) == 0;
|
||||
}
|
||||
|
||||
static uint32_t
|
||||
can_decode_nodeid(int nodeid)
|
||||
{
|
||||
return (nodeid << 1) + 0x100;
|
||||
}
|
||||
|
||||
static void
|
||||
can_process_query_unassigned(uint32_t id, uint32_t len, uint8_t *data)
|
||||
{
|
||||
if (canbus_assigned_id)
|
||||
return;
|
||||
uint8_t send[8];
|
||||
send[0] = CANBUS_RESP_NEED_NODEID;
|
||||
memcpy(&send[1], canbus_uuid, sizeof(canbus_uuid));
|
||||
send[7] = CANBUS_CMD_SET_CANBOOT_NODEID;
|
||||
// Send with retry
|
||||
for (;;) {
|
||||
int ret = canbus_send(CANBUS_ID_ADMIN_RESP, 8, send);
|
||||
if (ret >= 0)
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
can_process_clear_canboot_nodeid(void)
|
||||
{
|
||||
canbus_assigned_id = 0;
|
||||
canbus_set_filter(canbus_assigned_id);
|
||||
}
|
||||
|
||||
static void
|
||||
can_id_conflict(void)
|
||||
{
|
||||
canbus_assigned_id = 0;
|
||||
canbus_set_filter(canbus_assigned_id);
|
||||
// TODO: We should likely do something here, such as report back
|
||||
}
|
||||
|
||||
static void
|
||||
can_process_set_canboot_nodeid(uint32_t id, uint32_t len, uint8_t *data)
|
||||
{
|
||||
if (len < 8)
|
||||
return;
|
||||
uint32_t newid = can_decode_nodeid(data[7]);
|
||||
if (can_check_uuid(id, len, data)) {
|
||||
if (newid != canbus_assigned_id) {
|
||||
canbus_assigned_id = newid;
|
||||
canbus_set_filter(canbus_assigned_id);
|
||||
}
|
||||
} else if (newid == canbus_assigned_id) {
|
||||
can_id_conflict();
|
||||
}
|
||||
}
|
||||
|
||||
// Handle an "admin" command
|
||||
static void
|
||||
can_process(uint32_t id, uint32_t len, uint8_t *data)
|
||||
{
|
||||
if (!len)
|
||||
return;
|
||||
switch (data[0]) {
|
||||
case CANBUS_CMD_QUERY_UNASSIGNED:
|
||||
can_process_query_unassigned(id, len, data);
|
||||
break;
|
||||
case CANBUS_CMD_SET_CANBOOT_NODEID:
|
||||
can_process_set_canboot_nodeid(id, len, data);
|
||||
break;
|
||||
case CANBUS_CMD_CLEAR_CANBOOT_NODEID:
|
||||
can_process_clear_canboot_nodeid();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/****************************************************************
|
||||
* CAN packet reading
|
||||
****************************************************************/
|
||||
|
||||
static struct task_wake canbus_rx_wake;
|
||||
|
||||
void
|
||||
canbus_notify_rx(void)
|
||||
{
|
||||
sched_wake_task(&canbus_rx_wake);
|
||||
}
|
||||
|
||||
static uint8_t receive_buf[192], receive_pos;
|
||||
DECL_CONSTANT("RECEIVE_WINDOW", ARRAY_SIZE(receive_buf));
|
||||
|
||||
// Handle incoming data (called from IRQ handler)
|
||||
void
|
||||
canbus_process_data(uint32_t id, uint32_t len, uint8_t *data)
|
||||
{
|
||||
if (!id || id != canbus_assigned_id)
|
||||
return;
|
||||
int rpos = receive_pos;
|
||||
if (len > sizeof(receive_buf) - rpos)
|
||||
len = sizeof(receive_buf) - rpos;
|
||||
memcpy(&receive_buf[rpos], data, len);
|
||||
receive_pos = rpos + len;
|
||||
canbus_notify_rx();
|
||||
}
|
||||
|
||||
// Remove from the receive buffer the given number of bytes
|
||||
static void
|
||||
console_pop_input(int len)
|
||||
{
|
||||
int copied = 0;
|
||||
for (;;) {
|
||||
int rpos = readb(&receive_pos);
|
||||
int needcopy = rpos - len;
|
||||
if (needcopy) {
|
||||
memmove(&receive_buf[copied], &receive_buf[copied + len]
|
||||
, needcopy - copied);
|
||||
copied = needcopy;
|
||||
canbus_notify_rx();
|
||||
}
|
||||
irqstatus_t flag = irq_save();
|
||||
if (rpos != readb(&receive_pos)) {
|
||||
// Raced with irq handler - retry
|
||||
irq_restore(flag);
|
||||
continue;
|
||||
}
|
||||
receive_pos = needcopy;
|
||||
irq_restore(flag);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Task to process incoming commands and admin messages
|
||||
void
|
||||
canbus_rx_task(void)
|
||||
{
|
||||
if (!sched_check_wake(&canbus_rx_wake))
|
||||
return;
|
||||
|
||||
// Read any pending CAN packets
|
||||
for (;;) {
|
||||
uint8_t data[8];
|
||||
uint32_t id;
|
||||
int ret = canbus_read(&id, data);
|
||||
if (ret < 0)
|
||||
break;
|
||||
if (id && id == canbus_assigned_id + 1)
|
||||
can_id_conflict();
|
||||
else if (id == CANBUS_ID_ADMIN)
|
||||
can_process(id, ret, data);
|
||||
}
|
||||
|
||||
// Check for a complete message block and process it
|
||||
uint_fast8_t rpos = readb(&receive_pos), pop_count;
|
||||
int ret = command_find_block(receive_buf, rpos, &pop_count);
|
||||
if (ret > 0)
|
||||
command_dispatch(receive_buf, pop_count);
|
||||
if (ret) {
|
||||
console_pop_input(pop_count);
|
||||
if (ret > 0)
|
||||
command_send_ack();
|
||||
}
|
||||
}
|
||||
DECL_TASK(canbus_rx_task);
|
||||
|
||||
|
||||
/****************************************************************
|
||||
* Setup and shutdown
|
||||
****************************************************************/
|
||||
|
||||
void
|
||||
command_get_canbus_id(uint32_t *data)
|
||||
{
|
||||
uint32_t out[5] = {};
|
||||
memcpy(&out[2], canbus_uuid, 6);
|
||||
command_respond_ack(CMD_GET_CANBUS_ID, out, ARRAY_SIZE(out));
|
||||
}
|
||||
|
||||
void
|
||||
canbus_set_uuid(void *uuid)
|
||||
{
|
||||
memcpy(canbus_uuid, uuid, sizeof(canbus_uuid));
|
||||
canbus_notify_rx();
|
||||
}
|
||||
|
||||
void
|
||||
canbus_shutdown(void)
|
||||
{
|
||||
canbus_notify_tx();
|
||||
canbus_notify_rx();
|
||||
}
|
||||
DECL_SHUTDOWN(canbus_shutdown);
|
||||
+15
-8
@@ -3,19 +3,26 @@
|
||||
|
||||
#include <stdint.h> // uint32_t
|
||||
|
||||
#define CANBUS_ID_ADMIN 0x3f0
|
||||
#define CANBUS_ID_ADMIN_RESP 0x3f1
|
||||
#define CANBUS_UUID_LEN 6
|
||||
struct canbus_msg {
|
||||
uint32_t id;
|
||||
uint32_t dlc;
|
||||
union {
|
||||
uint8_t data[8];
|
||||
uint32_t data32[2];
|
||||
};
|
||||
};
|
||||
|
||||
#define CANMSG_ID_RTR (1<<30)
|
||||
#define CANMSG_ID_EFF (1<<31)
|
||||
|
||||
#define CANMSG_DATA_LEN(msg) ((msg)->dlc > 8 ? 8 : (msg)->dlc)
|
||||
|
||||
// callbacks provided by board specific code
|
||||
int canbus_read(uint32_t *id, uint8_t *data);
|
||||
int canbus_send(uint32_t id, uint32_t len, uint8_t *data);
|
||||
int canbus_send(struct canbus_msg *msg);
|
||||
void canbus_set_filter(uint32_t id);
|
||||
|
||||
// canbus.c
|
||||
void canbus_notify_tx(void);
|
||||
void canbus_notify_rx(void);
|
||||
void canbus_process_data(uint32_t id, uint32_t len, uint8_t *data);
|
||||
void canbus_set_uuid(void *data);
|
||||
void canbus_process_data(struct canbus_msg *msg);
|
||||
|
||||
#endif // canbus.h
|
||||
@@ -0,0 +1,342 @@
|
||||
// Generic handling of serial over CAN support
|
||||
//
|
||||
// Copyright (C) 2019 Eug Krashtan <eug.krashtan@gmail.com>
|
||||
// Copyright (C) 2020 Pontus Borg <glpontus@gmail.com>
|
||||
// Copyright (C) 2021 Kevin O'Connor <kevin@koconnor.net>
|
||||
//
|
||||
// This file may be distributed under the terms of the GNU GPLv3 license.
|
||||
|
||||
#include <string.h> // memcpy
|
||||
#include "board/io.h" // readb
|
||||
#include "board/irq.h" // irq_save
|
||||
#include "board/misc.h" // console_sendf
|
||||
#include "canbus.h" // canbus_set_uuid
|
||||
#include "canserial.h" // canserial_notify_tx
|
||||
#include "command.h" // DECL_CONSTANT
|
||||
#include "fasthash.h" // fasthash64
|
||||
#include "sched.h" // sched_wake_task
|
||||
|
||||
#define CANBUS_UUID_LEN 6
|
||||
|
||||
// Global storage
|
||||
static struct canbus_data {
|
||||
uint32_t assigned_id;
|
||||
uint8_t uuid[CANBUS_UUID_LEN];
|
||||
|
||||
// Tx data
|
||||
struct task_wake tx_wake;
|
||||
uint8_t transmit_pos, transmit_max;
|
||||
|
||||
// Rx data
|
||||
struct task_wake rx_wake;
|
||||
uint8_t receive_pos;
|
||||
uint32_t admin_pull_pos, admin_push_pos;
|
||||
|
||||
// Transfer buffers
|
||||
struct canbus_msg admin_queue[8];
|
||||
uint8_t transmit_buf[96];
|
||||
uint8_t receive_buf[192];
|
||||
} CanData;
|
||||
|
||||
|
||||
/****************************************************************
|
||||
* Data transmission over CAN
|
||||
****************************************************************/
|
||||
|
||||
void
|
||||
canserial_notify_tx(void)
|
||||
{
|
||||
sched_wake_task(&CanData.tx_wake);
|
||||
}
|
||||
|
||||
void
|
||||
canserial_tx_task(void)
|
||||
{
|
||||
if (!sched_check_wake(&CanData.tx_wake))
|
||||
return;
|
||||
uint32_t id = CanData.assigned_id;
|
||||
if (!id) {
|
||||
CanData.transmit_pos = CanData.transmit_max = 0;
|
||||
return;
|
||||
}
|
||||
struct canbus_msg msg;
|
||||
msg.id = id + 1;
|
||||
uint32_t tpos = CanData.transmit_pos, tmax = CanData.transmit_max;
|
||||
for (;;) {
|
||||
int avail = tmax - tpos, now = avail > 8 ? 8 : avail;
|
||||
if (avail <= 0)
|
||||
break;
|
||||
msg.dlc = now;
|
||||
memcpy(msg.data, &CanData.transmit_buf[tpos], now);
|
||||
int ret = canserial_send(&msg);
|
||||
if (ret <= 0)
|
||||
break;
|
||||
tpos += now;
|
||||
}
|
||||
CanData.transmit_pos = tpos;
|
||||
}
|
||||
DECL_TASK(canserial_tx_task);
|
||||
|
||||
// Encode and transmit a "response" message
|
||||
void
|
||||
console_sendf(const struct command_encoder *ce, va_list args)
|
||||
{
|
||||
// Verify space for message
|
||||
uint32_t tpos = CanData.transmit_pos, tmax = CanData.transmit_max;
|
||||
if (tpos >= tmax)
|
||||
CanData.transmit_pos = CanData.transmit_max = tpos = tmax = 0;
|
||||
uint32_t max_size = ce->max_size;
|
||||
if (tmax + max_size > sizeof(CanData.transmit_buf)) {
|
||||
if (tmax + max_size - tpos > sizeof(CanData.transmit_buf))
|
||||
// Not enough space for message
|
||||
return;
|
||||
// Move buffer
|
||||
tmax -= tpos;
|
||||
memmove(&CanData.transmit_buf[0], &CanData.transmit_buf[tpos], tmax);
|
||||
CanData.transmit_pos = tpos = 0;
|
||||
CanData.transmit_max = tmax;
|
||||
}
|
||||
|
||||
// Generate message
|
||||
uint32_t msglen = command_encode_and_frame(&CanData.transmit_buf[tmax]
|
||||
, ce, args);
|
||||
|
||||
// Start message transmit
|
||||
CanData.transmit_max = tmax + msglen;
|
||||
canserial_notify_tx();
|
||||
}
|
||||
|
||||
|
||||
/****************************************************************
|
||||
* CAN "admin" command handling
|
||||
****************************************************************/
|
||||
|
||||
// Available commands and responses
|
||||
#define CANBUS_CMD_QUERY_UNASSIGNED 0x00
|
||||
#define CANBUS_CMD_SET_CANBOOT_NODEID 0x11
|
||||
#define CANBUS_CMD_CLEAR_CANBOOT_NODEID 0x12
|
||||
#define CANBUS_RESP_NEED_NODEID 0x20
|
||||
|
||||
// Helper to verify a UUID in a command matches this chip's UUID
|
||||
static int
|
||||
can_check_uuid(struct canbus_msg *msg)
|
||||
{
|
||||
return (msg->dlc >= 7
|
||||
&& memcmp(&msg->data[1], CanData.uuid, sizeof(CanData.uuid)) == 0);
|
||||
}
|
||||
|
||||
// Helpers to encode/decode a CAN identifier to a 1-byte "nodeid"
|
||||
/*static int
|
||||
can_get_nodeid(void)
|
||||
{
|
||||
if (!CanData.assigned_id)
|
||||
return 0;
|
||||
return (CanData.assigned_id - 0x100) >> 1;
|
||||
}*/
|
||||
|
||||
static uint32_t
|
||||
can_decode_nodeid(int nodeid)
|
||||
{
|
||||
return (nodeid << 1) + 0x100;
|
||||
}
|
||||
|
||||
static void
|
||||
can_process_query_unassigned(struct canbus_msg *msg)
|
||||
{
|
||||
if (CanData.assigned_id)
|
||||
return;
|
||||
struct canbus_msg send;
|
||||
send.id = CANBUS_ID_ADMIN_RESP;
|
||||
send.dlc = 8;
|
||||
send.data[0] = CANBUS_RESP_NEED_NODEID;
|
||||
memcpy(&send.data[1], CanData.uuid, sizeof(CanData.uuid));
|
||||
send.data[7] = CANBUS_CMD_SET_CANBOOT_NODEID;
|
||||
// Send with retry
|
||||
for (;;) {
|
||||
int ret = canserial_send(&send);
|
||||
if (ret >= 0)
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
can_process_clear_canboot_nodeid(void)
|
||||
{
|
||||
CanData.assigned_id = 0;
|
||||
canbus_set_filter(CanData.assigned_id);
|
||||
}
|
||||
|
||||
static void
|
||||
can_id_conflict(void)
|
||||
{
|
||||
CanData.assigned_id = 0;
|
||||
canserial_set_filter(CanData.assigned_id);
|
||||
}
|
||||
|
||||
static void
|
||||
can_process_set_canboot_nodeid(struct canbus_msg *msg)
|
||||
{
|
||||
if (msg->dlc < 8)
|
||||
return;
|
||||
uint32_t newid = can_decode_nodeid(msg->data[7]);
|
||||
if (can_check_uuid(msg)) {
|
||||
if (newid != CanData.assigned_id) {
|
||||
CanData.assigned_id = newid;
|
||||
canserial_set_filter(CanData.assigned_id);
|
||||
}
|
||||
} else if (newid == CanData.assigned_id) {
|
||||
can_id_conflict();
|
||||
}
|
||||
}
|
||||
|
||||
// Handle an "admin" command
|
||||
static void
|
||||
can_process_admin(struct canbus_msg *msg)
|
||||
{
|
||||
if (!msg->dlc)
|
||||
return;
|
||||
switch (msg->data[0]) {
|
||||
case CANBUS_CMD_QUERY_UNASSIGNED:
|
||||
can_process_query_unassigned(msg);
|
||||
break;
|
||||
case CANBUS_CMD_SET_CANBOOT_NODEID:
|
||||
can_process_set_canboot_nodeid(msg);
|
||||
break;
|
||||
case CANBUS_CMD_CLEAR_CANBOOT_NODEID:
|
||||
can_process_clear_canboot_nodeid();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/****************************************************************
|
||||
* CAN packet reading
|
||||
****************************************************************/
|
||||
|
||||
static void
|
||||
canserial_notify_rx(void)
|
||||
{
|
||||
sched_wake_task(&CanData.rx_wake);
|
||||
}
|
||||
|
||||
DECL_CONSTANT("RECEIVE_WINDOW", ARRAY_SIZE(CanData.receive_buf));
|
||||
|
||||
// Handle incoming data (called from IRQ handler)
|
||||
int
|
||||
canserial_process_data(struct canbus_msg *msg)
|
||||
{
|
||||
uint32_t id = msg->id;
|
||||
if (CanData.assigned_id && id == CanData.assigned_id) {
|
||||
// Add to incoming data buffer
|
||||
int rpos = CanData.receive_pos;
|
||||
uint32_t len = CANMSG_DATA_LEN(msg);
|
||||
if (len > sizeof(CanData.receive_buf) - rpos)
|
||||
return -1;
|
||||
memcpy(&CanData.receive_buf[rpos], msg->data, len);
|
||||
CanData.receive_pos = rpos + len;
|
||||
canserial_notify_rx();
|
||||
} else if (id == CANBUS_ID_ADMIN
|
||||
|| (CanData.assigned_id && id == CanData.assigned_id + 1)) {
|
||||
// Add to admin command queue
|
||||
uint32_t pushp = CanData.admin_push_pos;
|
||||
if (pushp >= CanData.admin_pull_pos + ARRAY_SIZE(CanData.admin_queue))
|
||||
// No space - drop message
|
||||
return -1;
|
||||
uint32_t pos = pushp % ARRAY_SIZE(CanData.admin_queue);
|
||||
memcpy(&CanData.admin_queue[pos], msg, sizeof(*msg));
|
||||
CanData.admin_push_pos = pushp + 1;
|
||||
canserial_notify_rx();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Remove from the receive buffer the given number of bytes
|
||||
static void
|
||||
console_pop_input(int len)
|
||||
{
|
||||
int copied = 0;
|
||||
for (;;) {
|
||||
int rpos = readb(&CanData.receive_pos);
|
||||
int needcopy = rpos - len;
|
||||
if (needcopy) {
|
||||
memmove(&CanData.receive_buf[copied]
|
||||
, &CanData.receive_buf[copied + len], needcopy - copied);
|
||||
copied = needcopy;
|
||||
canserial_notify_rx();
|
||||
}
|
||||
irqstatus_t flag = irq_save();
|
||||
if (rpos != readb(&CanData.receive_pos)) {
|
||||
// Raced with irq handler - retry
|
||||
irq_restore(flag);
|
||||
continue;
|
||||
}
|
||||
CanData.receive_pos = needcopy;
|
||||
irq_restore(flag);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Task to process incoming commands and admin messages
|
||||
void
|
||||
canserial_rx_task(void)
|
||||
{
|
||||
if (!sched_check_wake(&CanData.rx_wake))
|
||||
return;
|
||||
|
||||
// Process pending admin messages
|
||||
for (;;) {
|
||||
uint32_t pushp = readl(&CanData.admin_push_pos);
|
||||
uint32_t pullp = CanData.admin_pull_pos;
|
||||
if (pushp == pullp)
|
||||
break;
|
||||
uint32_t pos = pullp % ARRAY_SIZE(CanData.admin_queue);
|
||||
struct canbus_msg *msg = &CanData.admin_queue[pos];
|
||||
uint32_t id = msg->id;
|
||||
if (CanData.assigned_id && id == CanData.assigned_id + 1)
|
||||
can_id_conflict();
|
||||
else if (id == CANBUS_ID_ADMIN)
|
||||
can_process_admin(msg);
|
||||
CanData.admin_pull_pos = pullp + 1;
|
||||
}
|
||||
|
||||
// Check for a complete message block and process it
|
||||
uint_fast8_t rpos = readb(&CanData.receive_pos), pop_count;
|
||||
int ret = command_find_block(CanData.receive_buf, rpos, &pop_count);
|
||||
if (ret > 0)
|
||||
command_dispatch(CanData.receive_buf, pop_count);
|
||||
if (ret) {
|
||||
console_pop_input(pop_count);
|
||||
if (ret > 0)
|
||||
command_send_ack();
|
||||
}
|
||||
}
|
||||
DECL_TASK(canserial_rx_task);
|
||||
|
||||
|
||||
/****************************************************************
|
||||
* Setup and shutdown
|
||||
****************************************************************/
|
||||
|
||||
void
|
||||
command_get_canbus_id(uint32_t *args)
|
||||
{
|
||||
uint32_t out[5] = {};
|
||||
memcpy(&out[2], CanData.uuid, 6);
|
||||
command_respond_ack(CMD_GET_CANBUS_ID, out, ARRAY_SIZE(out));
|
||||
}
|
||||
|
||||
void
|
||||
canserial_set_uuid(uint8_t *raw_uuid, uint32_t raw_uuid_len)
|
||||
{
|
||||
uint64_t hash = fasthash64(raw_uuid, raw_uuid_len, 0xA16231A7);
|
||||
memcpy(CanData.uuid, &hash, sizeof(CanData.uuid));
|
||||
canserial_notify_rx();
|
||||
}
|
||||
|
||||
void
|
||||
canserial_shutdown(void)
|
||||
{
|
||||
canserial_notify_tx();
|
||||
canserial_notify_rx();
|
||||
}
|
||||
DECL_SHUTDOWN(canserial_shutdown);
|
||||
@@ -0,0 +1,19 @@
|
||||
#ifndef __CANSERIAL_H__
|
||||
#define __CANSERIAL_H__
|
||||
|
||||
#include <stdint.h> // uint32_t
|
||||
|
||||
#define CANBUS_ID_ADMIN 0x3f0
|
||||
#define CANBUS_ID_ADMIN_RESP 0x3f1
|
||||
|
||||
// callbacks provided by board specific code
|
||||
struct canbus_msg;
|
||||
int canserial_send(struct canbus_msg *msg);
|
||||
void canserial_set_filter(uint32_t id);
|
||||
|
||||
// canserial.c
|
||||
void canserial_notify_tx(void);
|
||||
int canserial_process_data(struct canbus_msg *msg);
|
||||
void canserial_set_uuid(uint8_t *raw_uuid, uint32_t raw_uuid_len);
|
||||
|
||||
#endif // canserial.h
|
||||
@@ -6,6 +6,7 @@
|
||||
|
||||
#include <string.h> // memmove
|
||||
#include "autoconf.h" // CONFIG_USB_VENDOR_ID
|
||||
#include "board/misc.h" // console_sendf
|
||||
#include "board/pgm.h" // PROGMEM
|
||||
#include "board/usb_cdc_ep.h" // USB_CDC_EP_BULK_IN
|
||||
#include "byteorder.h" // cpu_to_le16
|
||||
|
||||
@@ -57,55 +57,6 @@ config FLASH_START
|
||||
hex
|
||||
default 0x0000
|
||||
|
||||
config SDCARD_SPI
|
||||
bool
|
||||
|
||||
config SDCARD_SOFTWARE_SPI
|
||||
bool
|
||||
|
||||
menu "SD Card Configuration"
|
||||
depends on HAVE_GPIO_SPI
|
||||
config ENABLE_SDCARD
|
||||
bool "Enable SD Card"
|
||||
default n
|
||||
choice
|
||||
depends on ENABLE_SDCARD
|
||||
prompt "Interface"
|
||||
config LPC_SD_SPI_SSP0
|
||||
bool "SSP0 (on P0.17/P0.18/P0.15)"
|
||||
select SDCARD_SPI
|
||||
config LPC_SD_SPI_SSP1
|
||||
bool "SSP1 (on P0.8/P0.9/P0.7)"
|
||||
select SDCARD_SPI
|
||||
config LPC_SOFTWARE_SPI
|
||||
bool "Software SPI"
|
||||
select SDCARD_SPI
|
||||
select SDCARD_SOFTWARE_SPI
|
||||
endchoice
|
||||
config SWSPI_MISO_PIN
|
||||
depends on SDCARD_SOFTWARE_SPI
|
||||
string "Software SPI MISO Pin"
|
||||
config SWSPI_MOSI_PIN
|
||||
depends on SDCARD_SOFTWARE_SPI
|
||||
string "Sofware SPI MOSI Pin"
|
||||
config SWSPI_SCLK_PIN
|
||||
depends on SDCARD_SOFTWARE_SPI
|
||||
string "Software SPI SCLK Pin"
|
||||
config SD_SPI_CS_PIN
|
||||
depends on ENABLE_SDCARD
|
||||
string "SPI CS Pin"
|
||||
endmenu
|
||||
|
||||
config SD_SPI_BUS
|
||||
int
|
||||
default 0 if LPC_SD_SPI_SSP0
|
||||
default 1 if LPC_SD_SPI_SSP1
|
||||
default 0
|
||||
|
||||
config USBSERIAL
|
||||
bool
|
||||
config SERIAL
|
||||
bool
|
||||
choice
|
||||
prompt "Communication interface"
|
||||
config LPC_USB
|
||||
|
||||
@@ -20,7 +20,6 @@ src-y += ../lib/lpc176x/device/system_LPC17xx.c
|
||||
src-$(CONFIG_USBSERIAL) += lpc176x/usbserial.c lpc176x/chipid.c
|
||||
src-$(CONFIG_USBSERIAL) += generic/usb_cdc.c
|
||||
src-$(CONFIG_SERIAL) += lpc176x/serial.c generic/serial_irq.c
|
||||
src-$(CONFIG_SDCARD_SPI) += lpc176x/spi.c
|
||||
|
||||
|
||||
# Build the additional bin output file
|
||||
|
||||
@@ -21,15 +21,4 @@ struct gpio_in gpio_in_setup(uint8_t pin, int8_t pull_up);
|
||||
void gpio_in_reset(struct gpio_in g, int8_t pull_up);
|
||||
uint8_t gpio_in_read(struct gpio_in g);
|
||||
|
||||
struct spi_config {
|
||||
void *spi;
|
||||
uint32_t cr0, cpsr;
|
||||
uint8_t bus;
|
||||
};
|
||||
struct spi_config spi_setup(uint32_t bus, uint8_t mode, uint32_t rate);
|
||||
void spi_prepare(struct spi_config config);
|
||||
void spi_transfer(struct spi_config config, uint8_t receive_data
|
||||
, uint16_t len, uint8_t *data);
|
||||
void spi_set_rate(struct spi_config *config, uint32_t rate);
|
||||
|
||||
#endif // gpio.h
|
||||
@@ -1,106 +0,0 @@
|
||||
// SPI support on lpc176x
|
||||
//
|
||||
// Copyright (C) 2018-2021 Kevin O'Connor <kevin@koconnor.net>
|
||||
//
|
||||
// This file may be distributed under the terms of the GNU GPLv3 license.
|
||||
|
||||
#include "command.h" // DECL_ENUMERATION
|
||||
#include "gpio.h" // spi_setup
|
||||
#include "internal.h" // gpio_peripheral
|
||||
|
||||
struct spi_info {
|
||||
LPC_SSP_TypeDef *spi;
|
||||
uint8_t miso_pin, mosi_pin, sck_pin, pclk;
|
||||
};
|
||||
|
||||
DECL_ENUMERATION("spi_bus", "ssp0", 0);
|
||||
DECL_CONSTANT_STR("BUS_PINS_ssp0", "P0.17,P0.18,P0.15");
|
||||
DECL_ENUMERATION("spi_bus", "ssp1", 1);
|
||||
DECL_CONSTANT_STR("BUS_PINS_ssp1", "P0.8,P0.9,P0.7");
|
||||
|
||||
static const struct spi_info spi_bus[] = {
|
||||
{ LPC_SSP0, GPIO(0, 17), GPIO(0, 18), GPIO(0, 15), PCLK_SSP0 },
|
||||
{ LPC_SSP1, GPIO(0, 8), GPIO(0, 9), GPIO(0, 7), PCLK_SSP1 },
|
||||
};
|
||||
|
||||
static void
|
||||
spi_init(uint32_t bus)
|
||||
{
|
||||
static int have_run_init[ARRAY_SIZE(spi_bus)];
|
||||
if (have_run_init[bus])
|
||||
return;
|
||||
have_run_init[bus] = 1;
|
||||
|
||||
// Configure MISO0, MOSI0, SCK0 pins
|
||||
gpio_peripheral(spi_bus[bus].miso_pin, 2, 0);
|
||||
gpio_peripheral(spi_bus[bus].mosi_pin, 2, 0);
|
||||
gpio_peripheral(spi_bus[bus].sck_pin, 2, 0);
|
||||
|
||||
// Setup clock
|
||||
enable_pclock(spi_bus[bus].pclk);
|
||||
|
||||
// Set initial registers
|
||||
LPC_SSP_TypeDef *spi = spi_bus[bus].spi;
|
||||
spi->CR0 = 0x07;
|
||||
spi->CPSR = 254;
|
||||
spi->CR1 = 1<<1;
|
||||
}
|
||||
|
||||
void
|
||||
spi_set_rate(struct spi_config *config, uint32_t rate)
|
||||
{
|
||||
config->cr0 &= 0xFF;
|
||||
uint32_t pclk = get_pclock_frequency(spi_bus[config->bus].pclk);
|
||||
uint32_t div = DIV_ROUND_UP(pclk/4, rate) << 1;
|
||||
config->cpsr = div < 2 ? 2 : (div > 254 ? 254 : div);
|
||||
|
||||
}
|
||||
|
||||
struct spi_config
|
||||
spi_setup(uint32_t bus, uint8_t mode, uint32_t rate)
|
||||
{
|
||||
// Make sure bus is enabled
|
||||
spi_init(bus);
|
||||
|
||||
// Setup clock rate and mode
|
||||
struct spi_config res = {spi_bus[bus].spi, 0, 0};
|
||||
res.cr0 = 0x07 | ((mode & 2) << 5) | ((mode & 1) << 7) | (1 << 8);
|
||||
res.bus = bus;
|
||||
spi_set_rate(&res, rate);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
void
|
||||
spi_prepare(struct spi_config config)
|
||||
{
|
||||
LPC_SSP_TypeDef *spi = config.spi;
|
||||
spi->CR0 = config.cr0;
|
||||
spi->CPSR = config.cpsr;
|
||||
}
|
||||
|
||||
void
|
||||
spi_transfer(struct spi_config config, uint8_t receive_data
|
||||
, uint16_t len, uint8_t *data)
|
||||
{
|
||||
LPC_SSP_TypeDef *spi = config.spi;
|
||||
if (receive_data) {
|
||||
while (len--) {
|
||||
spi->DR = *data;
|
||||
// wait for read data to be ready
|
||||
while (!(spi->SR & (1<<2)))
|
||||
;
|
||||
// get data
|
||||
*data++ = spi->DR;
|
||||
}
|
||||
} else {
|
||||
while (len--) {
|
||||
spi->DR = *data++;
|
||||
// wait for read data to be ready
|
||||
while (!(spi->SR & (1<<2)))
|
||||
;
|
||||
// read data (to clear receive fifo)
|
||||
spi->DR;
|
||||
}
|
||||
}
|
||||
}
|
||||
-218
@@ -1,218 +0,0 @@
|
||||
// SDCard Upload implementation
|
||||
//
|
||||
// Copyright (C) 2022 Eric Callahan <arksine.code@gmail.com>
|
||||
//
|
||||
// This file may be distributed under the terms of the GNU GPLv3 license.
|
||||
|
||||
#include <string.h> // memmove
|
||||
#include "autoconf.h" // CONFIG_BLOCK_SIZE
|
||||
#include "board/flash.h" // application_read_flash
|
||||
#include "board/misc.h" // application_jump
|
||||
#include "sdcard.h" // sdcard_init
|
||||
#include "ff.h" // f_open
|
||||
#include "diskio.h" // disk_initialize
|
||||
#include "command.h" // command_set_enable
|
||||
#include "flashcmd.h" // set_in_transfer
|
||||
#include "sched.h" // DECL_TASK
|
||||
|
||||
static struct {
|
||||
FATFS fs;
|
||||
FIL file_obj;
|
||||
DSTATUS disk_status;
|
||||
uint8_t file_open;
|
||||
uint8_t flash_state;
|
||||
uint32_t block_address;
|
||||
} ff_data;
|
||||
|
||||
enum {SDS_NEED_UPLOAD = 1, SDS_NEED_VERIFY, SDS_DONE};
|
||||
|
||||
/**********************************************************
|
||||
*
|
||||
* FatFS Callbacks
|
||||
*
|
||||
* ********************************************************/
|
||||
|
||||
|
||||
|
||||
DSTATUS
|
||||
disk_initialize(BYTE pdrv)
|
||||
{
|
||||
if (sdcard_init()) {
|
||||
ff_data.disk_status = 0;
|
||||
return 0;
|
||||
}
|
||||
return STA_NOINIT;
|
||||
}
|
||||
|
||||
DSTATUS
|
||||
disk_status(BYTE pdrv)
|
||||
{
|
||||
return ff_data.disk_status;
|
||||
}
|
||||
|
||||
|
||||
DRESULT
|
||||
disk_read(BYTE pdrv, BYTE *buff, LBA_t sector, UINT count)
|
||||
{
|
||||
uint8_t* read_buf = buff;
|
||||
while (count) {
|
||||
if (!sdcard_read_sector(read_buf, sector++))
|
||||
return RES_ERROR;
|
||||
count--;
|
||||
if (count)
|
||||
read_buf += SD_SECTOR_SIZE;
|
||||
}
|
||||
return RES_OK;
|
||||
}
|
||||
|
||||
|
||||
DRESULT
|
||||
disk_write(BYTE pdrv, const BYTE *buff, LBA_t sector, UINT count)
|
||||
{
|
||||
uint8_t* write_buf = (uint8_t *)buff;
|
||||
while (count) {
|
||||
if (!sdcard_write_sector(write_buf, sector++))
|
||||
return RES_ERROR;
|
||||
count--;
|
||||
if (count)
|
||||
write_buf += SD_SECTOR_SIZE;
|
||||
}
|
||||
return RES_OK;
|
||||
}
|
||||
|
||||
DRESULT
|
||||
disk_ioctl(BYTE pdrv, BYTE cmd, void *buff)
|
||||
{
|
||||
// No need for ioctls in a minimal fatfs implementation
|
||||
return RES_OK;
|
||||
}
|
||||
|
||||
static void
|
||||
sdcard_close(void)
|
||||
{
|
||||
set_in_transfer(0);
|
||||
if (ff_data.file_open)
|
||||
f_close(&(ff_data.file_obj));
|
||||
ff_data.file_open = 0;
|
||||
f_unmount("");
|
||||
memset(&(ff_data.fs), 0, sizeof(ff_data.fs));
|
||||
sdcard_deinit();
|
||||
}
|
||||
|
||||
void
|
||||
sdcard_open(void)
|
||||
{
|
||||
if (!CONFIG_ENABLE_SDCARD)
|
||||
return;
|
||||
ff_data.disk_status = STA_NOINIT;
|
||||
FRESULT res;
|
||||
res = f_mount(&(ff_data.fs), "", 1);
|
||||
if (res != FR_OK)
|
||||
goto fail;
|
||||
// open file in read mode
|
||||
res = f_open(&(ff_data.file_obj), "firmware.bin", 1);
|
||||
if (res != FR_OK)
|
||||
goto fail;
|
||||
ff_data.file_open = 1;
|
||||
ff_data.flash_state = SDS_NEED_UPLOAD;
|
||||
ff_data.block_address = CONFIG_APPLICATION_START;
|
||||
command_set_enable(0);
|
||||
set_in_transfer(1);
|
||||
return;
|
||||
fail:
|
||||
sdcard_close();
|
||||
}
|
||||
DECL_INIT(sdcard_open);
|
||||
|
||||
static void
|
||||
rename_firmware(const char* new_fname)
|
||||
{
|
||||
if (ff_data.file_open) {
|
||||
f_close(&ff_data.file_obj);
|
||||
ff_data.file_open = 0;
|
||||
}
|
||||
f_unlink(new_fname);
|
||||
f_rename("firmware.bin", new_fname);
|
||||
}
|
||||
|
||||
static void
|
||||
sdcard_flash_error(void)
|
||||
{
|
||||
ff_data.flash_state = 0;
|
||||
rename_firmware("firmware.err");
|
||||
sdcard_close();
|
||||
set_complete();
|
||||
}
|
||||
|
||||
static void
|
||||
sdcard_upload_block(void)
|
||||
{
|
||||
uint32_t sd_buf[CONFIG_BLOCK_SIZE / 4];
|
||||
memset(sd_buf, 0xFF, CONFIG_BLOCK_SIZE);
|
||||
UINT bcount;
|
||||
if (f_read(&(ff_data.file_obj), sd_buf, CONFIG_BLOCK_SIZE,
|
||||
&bcount) != FR_OK) {
|
||||
// Transfer Error
|
||||
sdcard_flash_error();
|
||||
return;
|
||||
}
|
||||
|
||||
if (flash_write_block(ff_data.block_address, sd_buf) < 0) {
|
||||
sdcard_flash_error();
|
||||
return;
|
||||
}
|
||||
ff_data.block_address += CONFIG_BLOCK_SIZE;
|
||||
if (bcount < CONFIG_BLOCK_SIZE) {
|
||||
if (flash_complete() < 0)
|
||||
sdcard_flash_error();
|
||||
else {
|
||||
f_rewind(&ff_data.file_obj);
|
||||
ff_data.block_address = CONFIG_APPLICATION_START;
|
||||
ff_data.flash_state = SDS_NEED_VERIFY;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
sdcard_verify_block(void)
|
||||
{
|
||||
uint32_t sd_buf[CONFIG_BLOCK_SIZE / 4];
|
||||
uint32_t flash_buf[CONFIG_BLOCK_SIZE];
|
||||
memset(sd_buf, 0xFF, CONFIG_BLOCK_SIZE);
|
||||
UINT bcount;
|
||||
if (f_read(&(ff_data.file_obj), sd_buf, CONFIG_BLOCK_SIZE,
|
||||
&bcount) != FR_OK) {
|
||||
// Transfer Error
|
||||
sdcard_flash_error();
|
||||
return;
|
||||
}
|
||||
application_read_flash(ff_data.block_address, flash_buf);
|
||||
if (memcmp(flash_buf, sd_buf, CONFIG_BLOCK_SIZE) != 0) {
|
||||
sdcard_flash_error();
|
||||
return;
|
||||
}
|
||||
ff_data.block_address += CONFIG_BLOCK_SIZE;
|
||||
if (bcount < CONFIG_BLOCK_SIZE)
|
||||
ff_data.flash_state = SDS_DONE;
|
||||
}
|
||||
|
||||
void
|
||||
sdcard_upload_task(void)
|
||||
{
|
||||
if (!CONFIG_ENABLE_SDCARD)
|
||||
return;
|
||||
switch(ff_data.flash_state) {
|
||||
case SDS_NEED_UPLOAD:
|
||||
sdcard_upload_block();
|
||||
break;
|
||||
case SDS_NEED_VERIFY:
|
||||
sdcard_verify_block();
|
||||
break;
|
||||
case SDS_DONE:
|
||||
ff_data.flash_state = 0;
|
||||
rename_firmware("firmware.cur");
|
||||
sdcard_close();
|
||||
set_complete();
|
||||
}
|
||||
}
|
||||
DECL_TASK(sdcard_upload_task);
|
||||
@@ -1,29 +0,0 @@
|
||||
#ifndef SDCARD_H
|
||||
#define SDCARD_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define SDCMD_GO_IDLE_STATE 0
|
||||
#define SDCMD_ALL_SEND_CID 2
|
||||
#define SDCMD_SEND_REL_ADDR 3
|
||||
#define SDCMD_IO_SEND_OP_COND 5
|
||||
#define SDCMD_SEL_DESEL_CARD 7
|
||||
#define SDCMD_SEND_IF_COND 8
|
||||
#define SDCMD_SEND_CSD 9
|
||||
#define SDCMD_SEND_OP_COND 41
|
||||
#define SDCMD_SET_CLR_CD_DETECT 42
|
||||
#define SDCMD_SEND_STATUS 13
|
||||
#define SDCMD_SET_BLOCKLEN 16
|
||||
#define SDCMD_READ_SINGLE_BLOCK 17
|
||||
#define SDCMD_WRITE_BLOCK 24
|
||||
#define SDCMD_APP_CMD 55
|
||||
#define SDCMD_READ_OCR 58
|
||||
#define SDCMD_CRC_ON_OFF 59
|
||||
#define SD_SECTOR_SIZE 512
|
||||
|
||||
uint8_t sdcard_write_sector(uint8_t* buf, uint32_t sector);
|
||||
uint8_t sdcard_read_sector(uint8_t *buf, uint32_t sector);
|
||||
uint8_t sdcard_init(void);
|
||||
void sdcard_deinit(void);
|
||||
|
||||
#endif
|
||||
@@ -1,445 +0,0 @@
|
||||
// SPI SDCard Commands
|
||||
//
|
||||
// Copyright (C) 2022 Eric Callahan <arksine.code@gmail.com>
|
||||
//
|
||||
// This file may be distributed under the terms of the GNU GPLv3 license.
|
||||
|
||||
#include <string.h> // memmove
|
||||
#include "autoconf.h" // CONFIG_SD_SPI_CS
|
||||
#include "sdcard.h" // sdcard_init
|
||||
#include "board/gpio.h" // gpio_out_setup
|
||||
#include "board/misc.h" // timer_read_time
|
||||
#include "board/io.h" // readb
|
||||
#include "sched.h" // udelay
|
||||
#include "spi_software.h" // spi_software_setup
|
||||
|
||||
DECL_CTR("DECL_SD_SPI_CS_PIN " __stringify(CONFIG_SD_SPI_CS_PIN));
|
||||
extern uint32_t sdcard_cs_gpio; // Generated by buildcommands.py
|
||||
|
||||
#define CRC7_POLY 0x12
|
||||
#define CRC16_POLY 0x1021
|
||||
#define SPI_INIT_RATE 400000
|
||||
#define SPI_XFER_RATE 4000000
|
||||
|
||||
static struct {
|
||||
struct spi_config config;
|
||||
struct gpio_out cs_pin;
|
||||
uint8_t flags;
|
||||
uint8_t err;
|
||||
} sd_spi;
|
||||
|
||||
enum {SDF_INITIALIZED = 1, SDF_HIGH_CAPACITY = 2, SDF_WRITE_PROTECTED = 4,
|
||||
SDF_DEINIT = 8};
|
||||
enum {SDE_NO_IDLE = 1, SDE_IF_COND_ERR = 2, SDE_CRC_ERR = 4,
|
||||
SDE_OP_COND_ERR = 8, SDE_OCR_ERR = 16, SDE_READ_ERR = 32,
|
||||
SDE_WRITE_ERR = 64, SDE_OTHER_ERR = 128};
|
||||
// COMMAND FLAGS
|
||||
enum {CF_APP_CMD = 1, CF_FULL_RESP = 2, CF_NOT_EXPECT = 4};
|
||||
|
||||
/**********************************************************
|
||||
*
|
||||
* CRC Helper Methods
|
||||
*
|
||||
* ********************************************************/
|
||||
|
||||
// Standard CRC
|
||||
static uint8_t
|
||||
calc_crc7(uint8_t *data, uint16_t length)
|
||||
{
|
||||
uint8_t crc = 0;
|
||||
while (length--)
|
||||
{
|
||||
crc ^= *data++;
|
||||
for (uint8_t i = 0; i < 8; i++) {
|
||||
if (crc & 0x80)
|
||||
crc = (crc << 1) ^ CRC7_POLY;
|
||||
else
|
||||
crc = crc << 1;
|
||||
}
|
||||
}
|
||||
return crc | 1;
|
||||
}
|
||||
|
||||
static uint16_t
|
||||
calc_crc16(uint8_t *data, uint16_t length)
|
||||
{
|
||||
uint16_t crc = 0;
|
||||
while (length--)
|
||||
{
|
||||
crc ^= (*data++ << 8);
|
||||
for (uint8_t i = 0; i < 8; i++) {
|
||||
if (crc & 0x8000)
|
||||
crc = (crc << 1) ^ CRC16_POLY;
|
||||
else
|
||||
crc = crc << 1;
|
||||
}
|
||||
}
|
||||
return crc;
|
||||
}
|
||||
|
||||
/**********************************************************
|
||||
*
|
||||
* SPI Device Methods
|
||||
*
|
||||
* ********************************************************/
|
||||
|
||||
static void
|
||||
spidev_setup(void)
|
||||
{
|
||||
if (CONFIG_SDCARD_SOFTWARE_SPI)
|
||||
spi_software_setup(0);
|
||||
else
|
||||
sd_spi.config = spi_setup(CONFIG_SD_SPI_BUS, 0, SPI_INIT_RATE);
|
||||
}
|
||||
|
||||
static void
|
||||
spidev_set_rate(uint32_t rate)
|
||||
{
|
||||
if (CONFIG_SDCARD_SOFTWARE_SPI)
|
||||
return;
|
||||
spi_set_rate(&(sd_spi.config), rate);
|
||||
}
|
||||
|
||||
static void
|
||||
spidev_prepare(void)
|
||||
{
|
||||
if (CONFIG_SDCARD_SOFTWARE_SPI)
|
||||
spi_software_prepare();
|
||||
else
|
||||
spi_prepare(sd_spi.config);
|
||||
}
|
||||
|
||||
static void
|
||||
spidev_transfer(uint8_t receive_data, uint16_t len, uint8_t *data)
|
||||
{
|
||||
if (CONFIG_SDCARD_SOFTWARE_SPI)
|
||||
spi_software_transfer(receive_data, len, data);
|
||||
else
|
||||
spi_transfer(sd_spi.config, receive_data, len, data);
|
||||
}
|
||||
|
||||
/**********************************************************
|
||||
*
|
||||
* SD Card Commands
|
||||
*
|
||||
* ********************************************************/
|
||||
static void
|
||||
populate_buffer(uint8_t command, uint32_t arg, uint8_t* buf)
|
||||
{
|
||||
buf[0] = command | 0x40;
|
||||
buf[1] = (arg >> 24) & 0xFF;
|
||||
buf[2] = (arg >> 16) & 0xFF;
|
||||
buf[3] = (arg >> 8) & 0xFF;
|
||||
buf[4] = (arg & 0xFF);
|
||||
uint8_t crc = calc_crc7(buf, 5);
|
||||
buf[5] = crc;
|
||||
}
|
||||
|
||||
static uint8_t
|
||||
send_command(uint8_t command, uint32_t arg, uint8_t* buf, uint8_t cmd_flags)
|
||||
{
|
||||
uint8_t ret;
|
||||
spidev_prepare();
|
||||
gpio_out_write(sd_spi.cs_pin, 0);
|
||||
memset(buf, 0, 8);
|
||||
if (cmd_flags & CF_APP_CMD) {
|
||||
buf[0] = SDCMD_APP_CMD | 0x40;
|
||||
uint8_t crc = calc_crc7(buf, 5);
|
||||
buf[5] = crc;
|
||||
spidev_transfer(0, 6, buf);
|
||||
}
|
||||
populate_buffer(command, arg, buf);
|
||||
spidev_transfer(0, 6, buf);
|
||||
memset(buf, 0xFF, 8);
|
||||
spidev_transfer(1, 8, buf);
|
||||
for (uint8_t i = 0; i < 8; i++) {
|
||||
ret = readb(&buf[i]);
|
||||
if (ret != 0xFF) {
|
||||
if (i) {
|
||||
uint8_t recd = 8 - i;
|
||||
memmove(buf, &buf[i], recd);
|
||||
if (cmd_flags & CF_FULL_RESP) {
|
||||
// need to read out a complete response
|
||||
uint8_t rem = 8 - recd;
|
||||
memset(&buf[recd], 0xFF, rem);
|
||||
spidev_transfer(1, rem, &buf[recd]);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
gpio_out_write(sd_spi.cs_pin, 1);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static uint8_t
|
||||
check_command(uint8_t cmd, uint32_t arg, uint8_t* buf, uint8_t flags,
|
||||
uint8_t expect, uint8_t attempts)
|
||||
{
|
||||
while (attempts) {
|
||||
uint8_t ret = send_command(cmd, arg, buf, flags);
|
||||
if (flags & CF_NOT_EXPECT) {
|
||||
if (ret != expect)
|
||||
return 1;
|
||||
}
|
||||
else if (ret == expect)
|
||||
return 1;
|
||||
attempts--;
|
||||
if (attempts)
|
||||
udelay((cmd == SDCMD_SEND_OP_COND) ? 100000 : 1000);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static uint8_t
|
||||
find_token(uint8_t token, uint32_t timeout_us)
|
||||
{
|
||||
uint8_t buf[1];
|
||||
uint32_t endtime = timer_read_time() + timer_from_us(timeout_us);
|
||||
while(timer_is_before(timer_read_time(), endtime))
|
||||
{
|
||||
writeb(buf, 0xFF);
|
||||
spidev_transfer(1, 1, buf);
|
||||
if (readb(buf) == token) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t
|
||||
sdcard_write_sector(uint8_t *buf, uint32_t sector)
|
||||
{
|
||||
if (!(sd_spi.flags & SDF_INITIALIZED))
|
||||
return 0;
|
||||
uint32_t offset = sector;
|
||||
if (!(sd_spi.flags & SDF_HIGH_CAPACITY))
|
||||
offset = sector * SD_SECTOR_SIZE;
|
||||
uint16_t crc = calc_crc16(buf, SD_SECTOR_SIZE);
|
||||
uint8_t cmd_buf[8];
|
||||
uint8_t ret = 1;
|
||||
populate_buffer(SDCMD_WRITE_BLOCK, offset, cmd_buf);
|
||||
spidev_prepare();
|
||||
gpio_out_write(sd_spi.cs_pin, 0);
|
||||
spidev_transfer(0, 6, cmd_buf);
|
||||
memset(cmd_buf, 0xFF, 8);
|
||||
spidev_transfer(1, 8, cmd_buf);
|
||||
for (uint8_t i = 0; i < 8; i++) {
|
||||
ret = readb(&cmd_buf[i]);
|
||||
if (ret != 0xFF)
|
||||
break;
|
||||
}
|
||||
if (ret == 0xFF) {
|
||||
gpio_out_write(sd_spi.cs_pin, 1);
|
||||
sd_spi.err |= SDE_WRITE_ERR;
|
||||
return 0;
|
||||
}
|
||||
// Send Header
|
||||
cmd_buf[0] = 0xFE;
|
||||
spidev_transfer(0, 1, cmd_buf);
|
||||
// Send Data
|
||||
spidev_transfer(0, SD_SECTOR_SIZE, buf);
|
||||
// Send CRC
|
||||
cmd_buf[0] = (crc >> 8) & 0xFF;
|
||||
cmd_buf[1] = crc & 0xFF;
|
||||
spidev_transfer(0, 2, cmd_buf);
|
||||
// Check for successful response
|
||||
memset(cmd_buf, 0xFF, 8);
|
||||
spidev_transfer(1, 8, cmd_buf);
|
||||
// Check the response
|
||||
for (uint8_t i = 0; i < 8; i ++)
|
||||
{
|
||||
ret = readb(&cmd_buf[i]);
|
||||
if (ret != 0xFF)
|
||||
break;
|
||||
}
|
||||
if ((ret & 0x1F) == 5)
|
||||
ret = 0;
|
||||
else
|
||||
ret = 2;
|
||||
// Wait for card to come out of busy state, 50ms timeout
|
||||
if (!find_token(0xFF, 50000))
|
||||
ret += 1;
|
||||
gpio_out_write(sd_spi.cs_pin, 1);
|
||||
if (ret > 0) {
|
||||
sd_spi.err |= SDE_WRITE_ERR;
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
static uint8_t
|
||||
read_data_block(uint8_t cmd, uint32_t arg, uint8_t* buf, uint32_t length)
|
||||
{
|
||||
uint8_t ret = 0;
|
||||
uint8_t cmd_buf[8];
|
||||
populate_buffer(cmd, arg, cmd_buf);
|
||||
spidev_prepare();
|
||||
gpio_out_write(sd_spi.cs_pin, 0);
|
||||
spidev_transfer(0, 6, cmd_buf);
|
||||
// find the first non-zero response, maximum of 16 tries
|
||||
for (uint8_t i = 0; i < 16; i++) {
|
||||
writeb(cmd_buf, 0xFF);
|
||||
spidev_transfer(1, 1, cmd_buf);
|
||||
ret = readb(cmd_buf);
|
||||
if (ret != 0xFF)
|
||||
break;
|
||||
}
|
||||
if (ret != 0)
|
||||
// Invalid Response
|
||||
ret = 1;
|
||||
|
||||
// Find Transfer Start token, 50ms timeout
|
||||
if (!find_token(0xFE, 50000))
|
||||
ret = 2;
|
||||
|
||||
// Read out the response. This is done regardless
|
||||
// of success to make sure the sdcard response is
|
||||
// flushed
|
||||
memset(buf, 0xFF, length);
|
||||
spidev_transfer(1, length, buf);
|
||||
// Read and check crc
|
||||
memset(cmd_buf, 0xFF, 8);
|
||||
spidev_transfer(1, 2, cmd_buf);
|
||||
// Exit busy state
|
||||
find_token(0xFF, 50000);
|
||||
gpio_out_write(sd_spi.cs_pin, 1);
|
||||
uint16_t recd_crc = (cmd_buf[0] << 8) | cmd_buf[1];
|
||||
uint16_t crc = calc_crc16(buf, length);
|
||||
if (recd_crc != crc) {
|
||||
sd_spi.err |= SDE_CRC_ERR;
|
||||
ret = 3;
|
||||
}
|
||||
if (ret > 0) {
|
||||
sd_spi.err |= SDE_READ_ERR;
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
uint8_t
|
||||
sdcard_read_sector(uint8_t *buf, uint32_t sector)
|
||||
{
|
||||
if (!(sd_spi.flags & SDF_INITIALIZED))
|
||||
return 0;
|
||||
uint32_t offset = sector;
|
||||
if (!(sd_spi.flags & SDF_HIGH_CAPACITY))
|
||||
offset = sector * SD_SECTOR_SIZE;
|
||||
return read_data_block(
|
||||
SDCMD_READ_SINGLE_BLOCK, offset, buf, SD_SECTOR_SIZE
|
||||
);
|
||||
}
|
||||
|
||||
static uint8_t
|
||||
sdcard_check_write_protect(void)
|
||||
{
|
||||
// Read the CSD register to check the write protect
|
||||
// bit. A name change can't be performed on a
|
||||
// write protected card.
|
||||
uint8_t csd_buf[16];
|
||||
if (!read_data_block(SDCMD_SEND_CSD, 0, csd_buf, 16))
|
||||
return 0;
|
||||
if ((csd_buf[14] & 0x30) != 0)
|
||||
// card is write protected
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
uint8_t
|
||||
sdcard_init(void)
|
||||
{
|
||||
sd_spi.cs_pin = gpio_out_setup(sdcard_cs_gpio, 1);
|
||||
spidev_setup();
|
||||
// per the spec, delay for 1ms and apply a minimum of 74 clocks
|
||||
// with CS high
|
||||
udelay(1000);
|
||||
spidev_prepare();
|
||||
uint8_t buf[8];
|
||||
memset(buf, 0xFF, 8);
|
||||
for (uint8_t i = 0; i < 10; i++) {
|
||||
spidev_transfer(0, 8, buf);
|
||||
}
|
||||
// attempt to go idle
|
||||
if (!check_command(SDCMD_GO_IDLE_STATE, 0, buf, 0, 1, 50)) {
|
||||
sd_spi.err |= SDE_NO_IDLE;
|
||||
return 0;
|
||||
}
|
||||
// Check SD Card Version
|
||||
uint8_t sd_ver = 0;
|
||||
if (!check_command(SDCMD_SEND_IF_COND, 0x10A, buf,
|
||||
CF_FULL_RESP | CF_NOT_EXPECT, 0xFF, 3))
|
||||
{
|
||||
sd_spi.err |= SDE_IF_COND_ERR;
|
||||
return 0;
|
||||
}
|
||||
if (buf[0] & 4)
|
||||
sd_ver = 1;
|
||||
else if (buf[0] == 1 && buf[3] == 1 && buf[4] == 10)
|
||||
sd_ver = 2;
|
||||
else {
|
||||
sd_spi.err |= SDE_IF_COND_ERR;
|
||||
return 0;
|
||||
}
|
||||
// Enable CRC Checks
|
||||
if (!check_command(SDCMD_CRC_ON_OFF, 1, buf, 0, 1, 3)) {
|
||||
sd_spi.err |= SDE_CRC_ERR;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Read OCR Register to determine if voltage is acceptable
|
||||
if (!check_command(SDCMD_READ_OCR, 0, buf, CF_FULL_RESP, 1, 20)) {
|
||||
sd_spi.err |= SDE_OCR_ERR;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if ((buf[2] & 0x30) != 0x30) {
|
||||
// Voltage between 3.2-3.4v not supported by this
|
||||
// card
|
||||
sd_spi.err |= SDE_OCR_ERR;
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
// Finsh init and come out of idle. This can take some time,
|
||||
// give up to 20 attempts (100 ms delay between attempts)
|
||||
if (!check_command(SDCMD_SEND_OP_COND, (sd_ver == 1) ? 0 : (1 << 30),
|
||||
buf, CF_APP_CMD, 0, 20))
|
||||
{
|
||||
sd_spi.err |= SDE_OP_COND_ERR;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (sd_ver == 2) {
|
||||
// read OCR again to determine capacity
|
||||
if (!check_command(SDCMD_READ_OCR, 0, buf, CF_FULL_RESP, 0, 5)) {
|
||||
sd_spi.err |= SDE_OCR_ERR;
|
||||
return 0;
|
||||
}
|
||||
if (buf[1] & 0x40)
|
||||
sd_spi.flags |= SDF_HIGH_CAPACITY;
|
||||
}
|
||||
|
||||
if (!check_command(SDCMD_SET_BLOCKLEN, SD_SECTOR_SIZE, buf, 0, 0, 3)) {
|
||||
sd_spi.err |= SDE_OTHER_ERR;
|
||||
return 0;
|
||||
}
|
||||
if (!sdcard_check_write_protect()) {
|
||||
sd_spi.flags |= SDF_WRITE_PROTECTED;
|
||||
return 0;
|
||||
}
|
||||
sd_spi.flags |= SDF_INITIALIZED;
|
||||
spidev_set_rate(SPI_XFER_RATE);
|
||||
return 1;
|
||||
}
|
||||
|
||||
void
|
||||
sdcard_deinit(void)
|
||||
{
|
||||
if (sd_spi.flags & SDF_DEINIT)
|
||||
return;
|
||||
sd_spi.flags |= SDF_DEINIT;
|
||||
uint8_t buf[8];
|
||||
// return to idle and disable crc checks
|
||||
send_command(SDCMD_GO_IDLE_STATE, 0, buf, 0);
|
||||
send_command(SDCMD_CRC_ON_OFF, 0, buf, 0);
|
||||
}
|
||||
@@ -1,69 +0,0 @@
|
||||
// Software SPI emulation
|
||||
//
|
||||
// Copyright (C) 2019 Kevin O'Connor <kevin@koconnor.net>
|
||||
//
|
||||
// This file may be distributed under the terms of the GNU GPLv3 license.
|
||||
|
||||
#include "autoconf.h"
|
||||
#include "board/gpio.h" // gpio_out_setup
|
||||
#include "ctr.h" // DECL_CTR
|
||||
|
||||
DECL_CTR("DECL_SWSPI_MISO_PIN " __stringify(CONFIG_SWSPI_MISO_PIN));
|
||||
DECL_CTR("DECL_SWSPI_MOSI_PIN " __stringify(CONFIG_SWSPI_MOSI_PIN));
|
||||
DECL_CTR("DECL_SWSPI_SCLK_PIN " __stringify(CONFIG_SWSPI_SCLK_PIN));
|
||||
extern uint32_t swspi_miso_gpio; // Generated by buildcommands.py
|
||||
extern uint32_t swspi_mosi_gpio; // Generated by buildcommands.py
|
||||
extern uint32_t swspi_sclk_gpio; // Generated by buildcommands.py
|
||||
|
||||
static struct spi_software {
|
||||
struct gpio_in miso;
|
||||
struct gpio_out mosi, sclk;
|
||||
uint8_t mode;
|
||||
} ss;
|
||||
|
||||
void
|
||||
spi_software_setup(uint8_t mode)
|
||||
{
|
||||
ss.miso = gpio_in_setup(swspi_miso_gpio, 1);
|
||||
ss.mosi = gpio_out_setup(swspi_mosi_gpio, 0);
|
||||
ss.sclk = gpio_out_setup(swspi_sclk_gpio, 0);
|
||||
ss.mode = mode;
|
||||
}
|
||||
|
||||
void
|
||||
spi_software_prepare(void)
|
||||
{
|
||||
gpio_out_write(ss.sclk, ss.mode & 0x02);
|
||||
}
|
||||
|
||||
void
|
||||
spi_software_transfer(uint8_t receive_data, uint16_t len, uint8_t *data)
|
||||
{
|
||||
while (len--) {
|
||||
uint8_t outbuf = *data;
|
||||
uint8_t inbuf = 0;
|
||||
for (uint_fast8_t i = 0; i < 8; i++) {
|
||||
if (ss.mode & 0x01) {
|
||||
// MODE 1 & 3
|
||||
gpio_out_toggle(ss.sclk);
|
||||
gpio_out_write(ss.mosi, outbuf & 0x80);
|
||||
outbuf <<= 1;
|
||||
gpio_out_toggle(ss.sclk);
|
||||
inbuf <<= 1;
|
||||
inbuf |= gpio_in_read(ss.miso);
|
||||
} else {
|
||||
// MODE 0 & 2
|
||||
gpio_out_write(ss.mosi, outbuf & 0x80);
|
||||
outbuf <<= 1;
|
||||
gpio_out_toggle(ss.sclk);
|
||||
inbuf <<= 1;
|
||||
inbuf |= gpio_in_read(ss.miso);
|
||||
gpio_out_toggle(ss.sclk);
|
||||
}
|
||||
}
|
||||
|
||||
if (receive_data)
|
||||
*data = inbuf;
|
||||
data++;
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
#ifndef __SPI_SOFTWARE_H
|
||||
#define __SPI_SOFTWARE_H
|
||||
|
||||
#include <stdint.h> // uint8_t
|
||||
|
||||
void spi_software_setup(uint8_t mode);
|
||||
void spi_software_prepare(void);
|
||||
void spi_software_transfer(uint8_t receive_data, uint16_t len, uint8_t *data);
|
||||
|
||||
#endif // spi_software.h
|
||||
+18
-90
@@ -75,6 +75,10 @@ choice
|
||||
select MACH_STM32H7
|
||||
endchoice
|
||||
|
||||
config MACH_STM32F103x6
|
||||
depends on LOW_LEVEL_OPTIONS && MACH_STM32F103
|
||||
bool "Only 10KiB of RAM (for rare stm32f103x6 variant)"
|
||||
|
||||
config MACH_STM32F0
|
||||
bool
|
||||
config MACH_STM32F1
|
||||
@@ -93,13 +97,17 @@ config MACH_STM32F4x5 # F405, F407, F429 series
|
||||
bool
|
||||
config HAVE_STM32_USBFS
|
||||
bool
|
||||
default y if MACH_STM32F103 || MACH_STM32F0x2 || MACH_STM32F070 || MACH_STM32G0
|
||||
default y if MACH_STM32F0x2 || MACH_STM32G0
|
||||
default y if (MACH_STM32F103 || MACH_STM32F070) && !STM32_CLOCK_REF_INTERNAL
|
||||
config HAVE_STM32_USBOTG
|
||||
bool
|
||||
default y if MACH_STM32F2 || MACH_STM32F4 || MACH_STM32H7
|
||||
config HAVE_STM32_CANBUS
|
||||
bool
|
||||
default y if MACH_STM32F1 || MACH_STM32F2 || MACH_STM32F4x5 || MACH_STM32F446 || MACH_STM32F0x2
|
||||
config HAVE_STM32_FDCANBUS
|
||||
bool
|
||||
default y if MACH_STM32G0
|
||||
|
||||
config MCU
|
||||
string
|
||||
@@ -152,7 +160,8 @@ config RAM_SIZE
|
||||
default 0x1000 if MACH_STM32F031
|
||||
default 0x1800 if MACH_STM32F042
|
||||
default 0x4000 if MACH_STM32F070 || MACH_STM32F072
|
||||
default 0x5000 if MACH_STM32F103 # Ram size of stm32f103x8 (20KiB)
|
||||
default 0x2800 if MACH_STM32F103x6
|
||||
default 0x5000 if MACH_STM32F103 && !MACH_STM32F103x6 # Ram size of stm32f103x8
|
||||
default 0x20000 if MACH_STM32F207
|
||||
default 0x10000 if MACH_STM32F401
|
||||
default 0x20000 if MACH_STM32F4x5 || MACH_STM32F446
|
||||
@@ -174,80 +183,6 @@ config STM32F103GD_DISABLE_SWD
|
||||
and PA14 pins from being available. Selecting this option
|
||||
disables SWD at startup and thus makes these pins available.
|
||||
|
||||
config SDCARD_SPI
|
||||
bool
|
||||
|
||||
config SDCARD_SOFTWARE_SPI
|
||||
bool
|
||||
|
||||
config SDCARD_SDIO
|
||||
bool
|
||||
|
||||
menu "SD Card Configuration"
|
||||
depends on HAVE_GPIO_SPI
|
||||
config ENABLE_SDCARD
|
||||
bool "Enable SD Card"
|
||||
default n
|
||||
choice
|
||||
depends on ENABLE_SDCARD
|
||||
prompt "Interface"
|
||||
config STM32_SD_SPI_1
|
||||
bool "SPI1 (on PA6/PA7/PA5)"
|
||||
select SDCARD_SPI
|
||||
config STM32_SD_SPI_1A
|
||||
bool "SPI1A (on PB4/PB5/PB3)"
|
||||
select SDCARD_SPI
|
||||
config STM32_SD_SPI_2
|
||||
bool "SPI2 (on PB14/PB15/PB13)"
|
||||
select SDCARD_SPI
|
||||
config STM32_SD_SPI_2A
|
||||
bool "SPI2A (on PC2/PC3/PB10)" if !MACH_STM32F1
|
||||
select SDCARD_SPI
|
||||
config STM32_SD_SPI_2B
|
||||
bool "SPI2B (on PI2/PI3/PI1)" if MACH_STM32F405 || MACH_STM32F407
|
||||
select SDCARD_SPI
|
||||
config STM32_SD_SPI_3
|
||||
bool "SPI3 (on PB4/PB5/PB3)" if !MACH_STM32F0
|
||||
select SDCARD_SPI
|
||||
config STM32_SD_SPI_3A
|
||||
bool "SPI3A (on PC11/PC12/PC10)" if MACH_STM32F4
|
||||
select SDCARD_SPI
|
||||
config STM32_SD_SPI_4
|
||||
bool "SPI4 (on PE13,PE14,PE12)" if MACH_STM32F401 || MACH_STM32F446
|
||||
select SDCARD_SPI
|
||||
config STM32_SOFTWARE_SPI
|
||||
bool "Software SPI"
|
||||
select SDCARD_SPI
|
||||
select SDCARD_SOFTWARE_SPI
|
||||
config STM32_SDIO
|
||||
bool "SDIO (on PD2,PC8,PC9,PC10,PC11,PC12)" if MACH_STM32F103 || MACH_STM32F4
|
||||
select SDCARD_SDIO
|
||||
endchoice
|
||||
config SWSPI_MISO_PIN
|
||||
depends on SDCARD_SOFTWARE_SPI
|
||||
string "Software SPI MISO Pin"
|
||||
config SWSPI_MOSI_PIN
|
||||
depends on SDCARD_SOFTWARE_SPI
|
||||
string "Sofware SPI MOSI Pin"
|
||||
config SWSPI_SCLK_PIN
|
||||
depends on SDCARD_SOFTWARE_SPI
|
||||
string "Software SPI SCLK Pin"
|
||||
config SD_SPI_CS_PIN
|
||||
depends on SDCARD_SPI
|
||||
string "SPI CS Pin"
|
||||
endmenu
|
||||
|
||||
config SD_SPI_BUS
|
||||
int
|
||||
default 0 if STM32_SD_SPI_2
|
||||
default 1 if STM32_SD_SPI_1
|
||||
default 2 if STM32_SD_SPI_1A
|
||||
default 3 if STM32_SD_SPI_2A
|
||||
default 4 if STM32_SD_SPI_3
|
||||
default 5 if STM32_SD_SPI_3A
|
||||
default 6 if STM32_SD_SPI_4 || STM32_SD_SPI_2B
|
||||
default 0
|
||||
|
||||
|
||||
######################################################################
|
||||
# Bootloader
|
||||
@@ -331,12 +266,6 @@ config STM32F0_TRIM
|
||||
# Communication inteface
|
||||
######################################################################
|
||||
|
||||
config USBSERIAL
|
||||
bool
|
||||
config SERIAL
|
||||
bool
|
||||
config CANSERIAL
|
||||
bool
|
||||
choice
|
||||
prompt "Communication interface"
|
||||
config STM32_USB_PA11_PA12
|
||||
@@ -378,7 +307,7 @@ choice
|
||||
select SERIAL
|
||||
config STM32_CANBUS_PA11_PA12
|
||||
bool "CAN bus (on PA11/PA12)"
|
||||
depends on HAVE_STM32_CANBUS
|
||||
depends on HAVE_STM32_CANBUS || HAVE_STM32_FDCANBUS
|
||||
select CANSERIAL
|
||||
config STM32_CANBUS_PA11_PA12_REMAP
|
||||
bool "CAN bus (on PA9/PA10)" if LOW_LEVEL_OPTIONS
|
||||
@@ -386,7 +315,7 @@ choice
|
||||
select CANSERIAL
|
||||
config STM32_CANBUS_PB8_PB9
|
||||
bool "CAN bus (on PB8/PB9)" if LOW_LEVEL_OPTIONS
|
||||
depends on HAVE_STM32_CANBUS
|
||||
depends on HAVE_STM32_CANBUS || HAVE_STM32_FDCANBUS
|
||||
select CANSERIAL
|
||||
config STM32_CANBUS_PI9_PH13
|
||||
bool "CAN bus (on PI9/PH13)" if LOW_LEVEL_OPTIONS
|
||||
@@ -404,13 +333,12 @@ choice
|
||||
bool "CAN bus (on PD0/PD1)" if LOW_LEVEL_OPTIONS
|
||||
depends on HAVE_STM32_CANBUS
|
||||
select CANSERIAL
|
||||
config STM32_CANBUS_PB0_PB1
|
||||
bool "CAN bus (on PB0/PB1)"
|
||||
depends on HAVE_STM32_FDCANBUS
|
||||
select CANSERIAL
|
||||
endchoice
|
||||
|
||||
config CANBUS_FREQUENCY
|
||||
int "CAN bus speed" if LOW_LEVEL_OPTIONS && CANSERIAL
|
||||
default 500000
|
||||
|
||||
|
||||
######################################################################
|
||||
# Flash settings
|
||||
######################################################################
|
||||
@@ -420,7 +348,7 @@ choice
|
||||
config STM32_APP_START_8000
|
||||
bool "32KiB offset" if MACH_STM32F2 || MACH_STM32F4
|
||||
config STM32_APP_START_4000
|
||||
bool "16KiB offset"
|
||||
bool "16KiB offset" if MACH_STM32F2 || MACH_STM32F4
|
||||
config STM32_APP_START_2000
|
||||
bool "8KiB offset" if MACH_STM32F0 || MACH_STM32F1 || MACH_STM32G0
|
||||
config STM32_APP_START_1000
|
||||
|
||||
+5
-7
@@ -3,7 +3,7 @@
|
||||
# Setup the toolchain
|
||||
CROSS_PREFIX=arm-none-eabi-
|
||||
|
||||
dirs-y += src/stm32 src/generic
|
||||
dirs-y += src/stm32 src/generic lib/fast-hash
|
||||
dirs-$(CONFIG_MACH_STM32F0) += lib/stm32f0
|
||||
dirs-$(CONFIG_MACH_STM32F1) += lib/stm32f1
|
||||
dirs-$(CONFIG_MACH_STM32F2) += lib/stm32f2
|
||||
@@ -53,12 +53,10 @@ serial-src-$(CONFIG_MACH_STM32F0) := stm32/stm32f0_serial.c
|
||||
serial-src-$(CONFIG_MACH_STM32G0) := stm32/stm32f0_serial.c
|
||||
serial-src-$(CONFIG_MACH_STM32H7) := stm32/stm32h7_serial.c
|
||||
src-$(CONFIG_SERIAL) += $(serial-src-y) generic/serial_irq.c
|
||||
src-$(CONFIG_CANSERIAL) += stm32/can.c ../lib/fast-hash/fasthash.c
|
||||
src-$(CONFIG_CANSERIAL) += generic/canbus.c
|
||||
src-$(CONFIG_SDCARD_SPI) += stm32/spi.c
|
||||
src-$(CONFIG_SDCARD_SDIO) += stm32/sdio.c
|
||||
|
||||
dirs-$(CONFIG_CANSERIAL) += lib/fast-hash
|
||||
canbus-src-y := generic/canserial.c ../lib/fast-hash/fasthash.c
|
||||
canbus-src-$(CONFIG_HAVE_STM32_CANBUS) += stm32/can.c
|
||||
canbus-src-$(CONFIG_HAVE_STM32_FDCANBUS) += stm32/fdcan.c
|
||||
src-$(CONFIG_CANSERIAL) += $(canbus-src-y) generic/canbus.c stm32/chipid.c
|
||||
|
||||
# Binary output file rules
|
||||
target-y += $(OUT)canboot.bin
|
||||
|
||||
+42
-80
@@ -10,9 +10,9 @@
|
||||
#include "autoconf.h" // CONFIG_MACH_STM32F1
|
||||
#include "board/irq.h" // irq_disable
|
||||
#include "command.h" // DECL_CONSTANT_STR
|
||||
#include "fasthash.h" // fasthash64
|
||||
#include "generic/armcm_boot.h" // armcm_enable_irq
|
||||
#include "generic/canbus.h" // canbus_notify_tx
|
||||
#include "generic/canserial.h" // CANBUS_ID_ADMIN
|
||||
#include "internal.h" // enable_pclock
|
||||
#include "sched.h" // DECL_INIT
|
||||
|
||||
@@ -66,7 +66,6 @@
|
||||
#endif
|
||||
|
||||
#if CONFIG_MACH_STM32F4
|
||||
#warning CAN on STM32F4 is untested
|
||||
#if (CONFIG_STM32_CANBUS_PA11_PA12 || CONFIG_STM32_CANBUS_PB8_PB9 \
|
||||
|| CONFIG_STM32_CANBUS_PD0_PD1 || CONFIG_STM32_CANBUS_PI9_PH13)
|
||||
#define SOC_CAN CAN1
|
||||
@@ -91,41 +90,9 @@
|
||||
#error No known CAN device for configured MCU
|
||||
#endif
|
||||
|
||||
// Read the next CAN packet
|
||||
int
|
||||
canbus_read(uint32_t *id, uint8_t *data)
|
||||
{
|
||||
if (!(SOC_CAN->RF0R & CAN_RF0R_FMP0)) {
|
||||
// All rx mboxes empty, enable wake on rx IRQ
|
||||
irq_disable();
|
||||
SOC_CAN->IER |= CAN_IER_FMPIE0;
|
||||
irq_enable();
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Read and ack packet
|
||||
CAN_FIFOMailBox_TypeDef *mb = &SOC_CAN->sFIFOMailBox[0];
|
||||
uint32_t rir_id = (mb->RIR >> CAN_RI0R_STID_Pos) & 0x7FF;
|
||||
uint32_t dlc = mb->RDTR & CAN_RDT0R_DLC;
|
||||
uint32_t rdlr = mb->RDLR, rdhr = mb->RDHR;
|
||||
SOC_CAN->RF0R = CAN_RF0R_RFOM0;
|
||||
|
||||
// Return packet
|
||||
*id = rir_id;
|
||||
data[0] = (rdlr >> 0) & 0xff;
|
||||
data[1] = (rdlr >> 8) & 0xff;
|
||||
data[2] = (rdlr >> 16) & 0xff;
|
||||
data[3] = (rdlr >> 24) & 0xff;
|
||||
data[4] = (rdhr >> 0) & 0xff;
|
||||
data[5] = (rdhr >> 8) & 0xff;
|
||||
data[6] = (rdhr >> 16) & 0xff;
|
||||
data[7] = (rdhr >> 24) & 0xff;
|
||||
return dlc;
|
||||
}
|
||||
|
||||
// Transmit a packet
|
||||
int
|
||||
canbus_send(uint32_t id, uint32_t len, uint8_t *data)
|
||||
canbus_send(struct canbus_msg *msg)
|
||||
{
|
||||
uint32_t tsr = SOC_CAN->TSR;
|
||||
if (!(tsr & (CAN_TSR_TME0|CAN_TSR_TME1|CAN_TSR_TME2))) {
|
||||
@@ -143,23 +110,21 @@ canbus_send(uint32_t id, uint32_t len, uint8_t *data)
|
||||
CAN_TxMailBox_TypeDef *mb = &SOC_CAN->sTxMailBox[mbox];
|
||||
|
||||
/* Set up the DLC */
|
||||
mb->TDTR = (mb->TDTR & 0xFFFFFFF0) | (len & 0x0F);
|
||||
mb->TDTR = (mb->TDTR & 0xFFFFFFF0) | (msg->dlc & 0x0F);
|
||||
|
||||
/* Set up the data field */
|
||||
if (len) {
|
||||
mb->TDLR = (((uint32_t)data[3] << 24)
|
||||
| ((uint32_t)data[2] << 16)
|
||||
| ((uint32_t)data[1] << 8)
|
||||
| ((uint32_t)data[0] << 0));
|
||||
mb->TDHR = (((uint32_t)data[7] << 24)
|
||||
| ((uint32_t)data[6] << 16)
|
||||
| ((uint32_t)data[5] << 8)
|
||||
| ((uint32_t)data[4] << 0));
|
||||
}
|
||||
mb->TDLR = msg->data32[0];
|
||||
mb->TDHR = msg->data32[1];
|
||||
|
||||
/* Request transmission */
|
||||
mb->TIR = (id << CAN_TI0R_STID_Pos) | CAN_TI0R_TXRQ;
|
||||
return len;
|
||||
uint32_t tir;
|
||||
if (msg->id & CANMSG_ID_EFF)
|
||||
tir = ((msg->id & 0x1fffffff) << CAN_TI0R_EXID_Pos) | CAN_TI0R_IDE;
|
||||
else
|
||||
tir = (msg->id & 0x7ff) << CAN_TI0R_STID_Pos;
|
||||
tir |= msg->id & CANMSG_ID_RTR ? CAN_TI0R_RTR : 0;
|
||||
mb->TIR = (msg->id << CAN_TI0R_STID_Pos) | CAN_TI0R_TXRQ;
|
||||
return CANMSG_DATA_LEN(msg);
|
||||
}
|
||||
|
||||
// Setup the receive packet filter
|
||||
@@ -171,20 +136,23 @@ canbus_set_filter(uint32_t id)
|
||||
/* Initialisation mode for the filter */
|
||||
SOC_CAN->FA1R = 0;
|
||||
|
||||
uint32_t mask = CAN_RI0R_STID | CAN_TI0R_IDE | CAN_TI0R_RTR;
|
||||
SOC_CAN->sFilterRegister[0].FR1 = CANBUS_ID_ADMIN << CAN_RI0R_STID_Pos;
|
||||
SOC_CAN->sFilterRegister[0].FR2 = mask;
|
||||
SOC_CAN->sFilterRegister[1].FR1 = (id + 1) << CAN_RI0R_STID_Pos;
|
||||
SOC_CAN->sFilterRegister[1].FR2 = mask;
|
||||
SOC_CAN->sFilterRegister[2].FR1 = id << CAN_RI0R_STID_Pos;
|
||||
SOC_CAN->sFilterRegister[2].FR2 = mask;
|
||||
if (CONFIG_CANBUS_FILTER) {
|
||||
uint32_t mask = CAN_TI0R_STID | CAN_TI0R_IDE | CAN_TI0R_RTR;
|
||||
SOC_CAN->sFilterRegister[0].FR1 = CANBUS_ID_ADMIN << CAN_RI0R_STID_Pos;
|
||||
SOC_CAN->sFilterRegister[0].FR2 = mask;
|
||||
SOC_CAN->sFilterRegister[1].FR1 = (id + 1) << CAN_RI0R_STID_Pos;
|
||||
SOC_CAN->sFilterRegister[1].FR2 = mask;
|
||||
SOC_CAN->sFilterRegister[2].FR1 = id << CAN_RI0R_STID_Pos;
|
||||
SOC_CAN->sFilterRegister[2].FR2 = mask;
|
||||
} else {
|
||||
SOC_CAN->sFilterRegister[0].FR1 = 0;
|
||||
SOC_CAN->sFilterRegister[0].FR2 = 0;
|
||||
id = 0;
|
||||
}
|
||||
|
||||
/* 32-bit scale for the filter */
|
||||
SOC_CAN->FS1R = (1<<0) | (1<<1) | (1<<2);
|
||||
|
||||
/* FIFO 1 assigned to 'id' */
|
||||
SOC_CAN->FFA1R = (1<<2);
|
||||
|
||||
/* Filter activation */
|
||||
SOC_CAN->FA1R = (1<<0) | (id ? (1<<1) | (1<<2) : 0);
|
||||
/* Leave the initialisation mode for the filter */
|
||||
@@ -195,27 +163,25 @@ canbus_set_filter(uint32_t id)
|
||||
void
|
||||
CAN_IRQHandler(void)
|
||||
{
|
||||
if (SOC_CAN->RF1R & CAN_RF1R_FMP1) {
|
||||
if (SOC_CAN->RF0R & CAN_RF0R_FMP0) {
|
||||
// Read and ack data packet
|
||||
CAN_FIFOMailBox_TypeDef *mb = &SOC_CAN->sFIFOMailBox[1];
|
||||
uint32_t rir_id = (mb->RIR >> CAN_RI0R_STID_Pos) & 0x7FF;
|
||||
uint32_t dlc = mb->RDTR & CAN_RDT0R_DLC;
|
||||
uint32_t rdlr = mb->RDLR, rdhr = mb->RDHR;
|
||||
SOC_CAN->RF1R = CAN_RF1R_RFOM1;
|
||||
CAN_FIFOMailBox_TypeDef *mb = &SOC_CAN->sFIFOMailBox[0];
|
||||
uint32_t rir = mb->RIR;
|
||||
struct canbus_msg msg;
|
||||
if (rir & CAN_RI0R_IDE)
|
||||
msg.id = ((rir >> CAN_RI0R_EXID_Pos) & 0x1fffffff) | CANMSG_ID_EFF;
|
||||
else
|
||||
msg.id = (rir >> CAN_RI0R_STID_Pos) & 0x7ff;
|
||||
msg.id |= rir & CAN_RI0R_RTR ? CANMSG_ID_RTR : 0;
|
||||
msg.dlc = mb->RDTR & CAN_RDT0R_DLC;
|
||||
msg.data32[0] = mb->RDLR;
|
||||
msg.data32[1] = mb->RDHR;
|
||||
SOC_CAN->RF0R = CAN_RF0R_RFOM0;
|
||||
|
||||
// Process packet
|
||||
union {
|
||||
struct { uint32_t rdlr, rdhr; };
|
||||
uint8_t data[8];
|
||||
} rdata = { .rdlr = rdlr, .rdhr = rdhr };
|
||||
canbus_process_data(rir_id, dlc, rdata.data);
|
||||
canbus_process_data(&msg);
|
||||
}
|
||||
uint32_t ier = SOC_CAN->IER;
|
||||
if (ier & CAN_IER_FMPIE0 && SOC_CAN->RF0R & CAN_RF0R_FMP0) {
|
||||
// Admin Rx
|
||||
SOC_CAN->IER = ier = ier & ~CAN_IER_FMPIE0;
|
||||
canbus_notify_rx();
|
||||
}
|
||||
if (ier & CAN_IER_TMEIE
|
||||
&& SOC_CAN->TSR & (CAN_TSR_RQCP0|CAN_TSR_RQCP1|CAN_TSR_RQCP2)) {
|
||||
// Tx
|
||||
@@ -256,7 +222,7 @@ compute_btr(uint32_t pclock, uint32_t bitrate)
|
||||
|
||||
uint32_t bit_clocks = pclock / bitrate; // clock ticks per bit
|
||||
|
||||
uint32_t sjw = 2;
|
||||
uint32_t sjw = 2;
|
||||
uint32_t qs;
|
||||
// Find number of time quantas that gives us the exact wanted bit time
|
||||
for (qs = 18; qs > 9; qs--) {
|
||||
@@ -310,10 +276,6 @@ can_init(void)
|
||||
armcm_enable_irq(CAN_IRQHandler, CAN_RX1_IRQn, 0);
|
||||
if (CAN_RX0_IRQn != CAN_TX_IRQn)
|
||||
armcm_enable_irq(CAN_IRQHandler, CAN_TX_IRQn, 0);
|
||||
SOC_CAN->IER = CAN_IER_FMPIE1;
|
||||
|
||||
// Convert unique 96-bit chip id into 48 bit representation
|
||||
uint64_t hash = fasthash64((uint8_t*)UID_BASE, 12, 0xA16231A7);
|
||||
canbus_set_uuid(&hash);
|
||||
SOC_CAN->IER = CAN_IER_FMPIE0;
|
||||
}
|
||||
DECL_INIT(can_init);
|
||||
+6
-4
@@ -4,6 +4,7 @@
|
||||
//
|
||||
// This file may be distributed under the terms of the GNU GPLv3 license.
|
||||
|
||||
#include "generic/canserial.h" // canserial_set_uuid
|
||||
#include "generic/usb_cdc.h" // usb_fill_serial
|
||||
#include "generic/usbstd.h" // usb_string_descriptor
|
||||
#include "internal.h" // UID_BASE
|
||||
@@ -25,9 +26,10 @@ usbserial_get_serialid(void)
|
||||
void
|
||||
chipid_init(void)
|
||||
{
|
||||
if (!CONFIG_USB_SERIAL_NUMBER_CHIPID)
|
||||
return;
|
||||
usb_fill_serial(&cdc_chipid.desc, ARRAY_SIZE(cdc_chipid.data)
|
||||
, (void*)UID_BASE);
|
||||
if (CONFIG_USB_SERIAL_NUMBER_CHIPID)
|
||||
usb_fill_serial(&cdc_chipid.desc, ARRAY_SIZE(cdc_chipid.data)
|
||||
, (void*)UID_BASE);
|
||||
if (CONFIG_CANBUS)
|
||||
canserial_set_uuid((void*)UID_BASE, CHIP_UID_LEN);
|
||||
}
|
||||
DECL_INIT(chipid_init);
|
||||
@@ -0,0 +1,266 @@
|
||||
// Serial over CAN emulation for STM32 boards.
|
||||
//
|
||||
// Copyright (C) 2019 Eug Krashtan <eug.krashtan@gmail.com>
|
||||
// Copyright (C) 2020 Pontus Borg <glpontus@gmail.com>
|
||||
// Copyright (C) 2021 Kevin O'Connor <kevin@koconnor.net>
|
||||
//
|
||||
// This file may be distributed under the terms of the GNU GPLv3 license.
|
||||
|
||||
#include <string.h> // memcpy
|
||||
#include "autoconf.h" // CONFIG_MACH_STM32F1
|
||||
#include "board/irq.h" // irq_disable
|
||||
#include "command.h" // DECL_CONSTANT_STR
|
||||
#include "generic/armcm_boot.h" // armcm_enable_irq
|
||||
#include "generic/canbus.h" // canbus_notify_tx
|
||||
#include "generic/canserial.h" // CANBUS_ID_ADMIN
|
||||
#include "generic/serial_irq.h" // serial_rx_byte
|
||||
#include "internal.h" // enable_pclock
|
||||
#include "sched.h" // DECL_INIT
|
||||
|
||||
typedef struct
|
||||
{
|
||||
__IO uint32_t id_section;
|
||||
__IO uint32_t dlc_section;
|
||||
__IO uint32_t data[64 / 4];
|
||||
}FDCAN_FIFO_TypeDef;
|
||||
|
||||
#define FDCAN_XTD (1<<30)
|
||||
#define FDCAN_RTR (1<<29)
|
||||
|
||||
typedef struct
|
||||
{
|
||||
__IO uint32_t FLS[28]; // Filter list standard
|
||||
__IO uint32_t FLE[16]; // Filter list extended
|
||||
FDCAN_FIFO_TypeDef RXF0[3];
|
||||
FDCAN_FIFO_TypeDef RXF1[3];
|
||||
__IO uint32_t TEF[6]; // Tx event FIFO
|
||||
FDCAN_FIFO_TypeDef TXFIFO[3];
|
||||
}FDCAN_MSG_RAM_TypeDef;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
FDCAN_MSG_RAM_TypeDef fdcan1;
|
||||
FDCAN_MSG_RAM_TypeDef fdcan2;
|
||||
}FDCAN_RAM_TypeDef;
|
||||
|
||||
FDCAN_RAM_TypeDef *fdcan_ram = (FDCAN_RAM_TypeDef *)(SRAMCAN_BASE);
|
||||
|
||||
#define FDCAN_IE_TC (FDCAN_IE_TCE | FDCAN_IE_TCFE | FDCAN_IE_TFEE)
|
||||
|
||||
#if CONFIG_STM32_CANBUS_PA11_PA12
|
||||
DECL_CONSTANT_STR("RESERVE_PINS_CAN", "PA11,PA12");
|
||||
#define GPIO_Rx GPIO('A', 11)
|
||||
#define GPIO_Tx GPIO('A', 12)
|
||||
#elif CONFIG_STM32_CANBUS_PB8_PB9
|
||||
DECL_CONSTANT_STR("RESERVE_PINS_CAN", "PB8,PB9");
|
||||
#define GPIO_Rx GPIO('B', 8)
|
||||
#define GPIO_Tx GPIO('B', 9)
|
||||
#elif CONFIG_STM32_CANBUS_PB0_PB1
|
||||
DECL_CONSTANT_STR("RESERVE_PINS_CAN", "PB0,PB1");
|
||||
#define GPIO_Rx GPIO('B', 0)
|
||||
#define GPIO_Tx GPIO('B', 1)
|
||||
#endif
|
||||
|
||||
#if !CONFIG_STM32_CANBUS_PB0_PB1
|
||||
#define SOC_CAN FDCAN1
|
||||
#define MSG_RAM fdcan_ram->fdcan1
|
||||
#else
|
||||
#define SOC_CAN FDCAN2
|
||||
#define MSG_RAM fdcan_ram->fdcan2
|
||||
#endif
|
||||
|
||||
#define CAN_IT0_IRQn TIM16_FDCAN_IT0_IRQn
|
||||
#define CAN_FUNCTION GPIO_FUNCTION(3) // Alternative function mapping number
|
||||
|
||||
#ifndef SOC_CAN
|
||||
#error No known CAN device for configured MCU
|
||||
#endif
|
||||
|
||||
// Transmit a packet
|
||||
int
|
||||
canbus_send(struct canbus_msg *msg)
|
||||
{
|
||||
uint32_t txfqs = SOC_CAN->TXFQS;
|
||||
if (txfqs & FDCAN_TXFQS_TFQF)
|
||||
// No space in transmit fifo - wait for irq
|
||||
return -1;
|
||||
|
||||
uint32_t w_index = ((txfqs & FDCAN_TXFQS_TFQPI) >> FDCAN_TXFQS_TFQPI_Pos);
|
||||
FDCAN_FIFO_TypeDef *txfifo = &MSG_RAM.TXFIFO[w_index];
|
||||
uint32_t ids;
|
||||
if (msg->id & CANMSG_ID_EFF)
|
||||
ids = (msg->id & 0x1fffffff) | FDCAN_XTD;
|
||||
else
|
||||
ids = (msg->id & 0x7ff) << 18;
|
||||
ids |= msg->id & CANMSG_ID_RTR ? FDCAN_RTR : 0;
|
||||
txfifo->id_section = ids;
|
||||
txfifo->dlc_section = (msg->dlc & 0x0f) << 16;
|
||||
txfifo->data[0] = msg->data32[0];
|
||||
txfifo->data[1] = msg->data32[1];
|
||||
SOC_CAN->TXBAR = ((uint32_t)1 << w_index);
|
||||
return CANMSG_DATA_LEN(msg);
|
||||
}
|
||||
|
||||
static void
|
||||
can_filter(uint32_t index, uint32_t id)
|
||||
{
|
||||
MSG_RAM.FLS[index] = ((0x2 << 30) // Classic filter
|
||||
| (0x1 << 27) // Store in Rx FIFO 0 if filter matches
|
||||
| (id << 16)
|
||||
| 0x7FF); // mask all enabled
|
||||
}
|
||||
|
||||
// Setup the receive packet filter
|
||||
void
|
||||
canbus_set_filter(uint32_t id)
|
||||
{
|
||||
if (!CONFIG_CANBUS_FILTER)
|
||||
return;
|
||||
/* Request initialisation */
|
||||
SOC_CAN->CCCR |= FDCAN_CCCR_INIT;
|
||||
/* Wait the acknowledge */
|
||||
while (!(SOC_CAN->CCCR & FDCAN_CCCR_INIT))
|
||||
;
|
||||
/* Enable configuration change */
|
||||
SOC_CAN->CCCR |= FDCAN_CCCR_CCE;
|
||||
|
||||
// Load filter
|
||||
can_filter(0, CANBUS_ID_ADMIN);
|
||||
can_filter(1, id);
|
||||
can_filter(2, id + 1);
|
||||
SOC_CAN->RXGFC = ((id ? 3 : 1) << FDCAN_RXGFC_LSS_Pos
|
||||
| 0x02 << FDCAN_RXGFC_ANFS_Pos);
|
||||
|
||||
/* Leave the initialisation mode for the filter */
|
||||
SOC_CAN->CCCR &= ~FDCAN_CCCR_CCE;
|
||||
SOC_CAN->CCCR &= ~FDCAN_CCCR_INIT;
|
||||
}
|
||||
|
||||
// This function handles CAN global interrupts
|
||||
void
|
||||
CAN_IRQHandler(void)
|
||||
{
|
||||
uint32_t ir = SOC_CAN->IR;
|
||||
|
||||
if (ir & FDCAN_IE_RF0NE) {
|
||||
SOC_CAN->IR = FDCAN_IE_RF0NE;
|
||||
|
||||
uint32_t rxf0s = SOC_CAN->RXF0S;
|
||||
if (rxf0s & FDCAN_RXF0S_F0FL) {
|
||||
// Read and ack data packet
|
||||
uint32_t idx = (rxf0s & FDCAN_RXF0S_F0GI) >> FDCAN_RXF0S_F0GI_Pos;
|
||||
FDCAN_FIFO_TypeDef *rxf0 = &MSG_RAM.RXF0[idx];
|
||||
uint32_t ids = rxf0->id_section;
|
||||
struct canbus_msg msg;
|
||||
if (ids & FDCAN_XTD)
|
||||
msg.id = (ids & 0x1fffffff) | CANMSG_ID_EFF;
|
||||
else
|
||||
msg.id = (ids >> 18) & 0x7ff;
|
||||
msg.id |= ids & FDCAN_RTR ? CANMSG_ID_RTR : 0;
|
||||
msg.dlc = (rxf0->dlc_section >> 16) & 0x0f;
|
||||
msg.data32[0] = rxf0->data[0];
|
||||
msg.data32[1] = rxf0->data[1];
|
||||
SOC_CAN->RXF0A = idx;
|
||||
|
||||
// Process packet
|
||||
canbus_process_data(&msg);
|
||||
}
|
||||
}
|
||||
if (ir & FDCAN_IE_TC) {
|
||||
// Tx
|
||||
SOC_CAN->IR = FDCAN_IE_TC;
|
||||
canbus_notify_tx();
|
||||
}
|
||||
}
|
||||
|
||||
static inline const uint32_t
|
||||
make_btr(uint32_t sjw, // Sync jump width, ... hmm
|
||||
uint32_t time_seg1, // time segment before sample point, 1 .. 16
|
||||
uint32_t time_seg2, // time segment after sample point, 1 .. 8
|
||||
uint32_t brp) // Baud rate prescaler, 1 .. 1024
|
||||
{
|
||||
return (((uint32_t)(sjw-1)) << FDCAN_NBTP_NSJW_Pos
|
||||
| ((uint32_t)(time_seg1-1)) << FDCAN_NBTP_NTSEG1_Pos
|
||||
| ((uint32_t)(time_seg2-1)) << FDCAN_NBTP_NTSEG2_Pos
|
||||
| ((uint32_t)(brp - 1)) << FDCAN_NBTP_NBRP_Pos);
|
||||
}
|
||||
|
||||
static inline const uint32_t
|
||||
compute_btr(uint32_t pclock, uint32_t bitrate)
|
||||
{
|
||||
/*
|
||||
Some equations:
|
||||
Tpclock = 1 / pclock
|
||||
Tq = brp * Tpclock
|
||||
Tbs1 = Tq * TS1
|
||||
Tbs2 = Tq * TS2
|
||||
NominalBitTime = Tq + Tbs1 + Tbs2
|
||||
BaudRate = 1/NominalBitTime
|
||||
Bit value sample point is after Tq+Tbs1. Ideal sample point
|
||||
is at 87.5% of NominalBitTime
|
||||
Use the lowest brp where ts1 and ts2 are in valid range
|
||||
*/
|
||||
|
||||
uint32_t bit_clocks = pclock / bitrate; // clock ticks per bit
|
||||
|
||||
uint32_t sjw = 2;
|
||||
uint32_t qs;
|
||||
// Find number of time quantas that gives us the exact wanted bit time
|
||||
for (qs = 18; qs > 9; qs--) {
|
||||
// check that bit_clocks / quantas is an integer
|
||||
uint32_t brp_rem = bit_clocks % qs;
|
||||
if (brp_rem == 0)
|
||||
break;
|
||||
}
|
||||
uint32_t brp = bit_clocks / qs;
|
||||
uint32_t time_seg2 = qs / 8; // sample at ~87.5%
|
||||
uint32_t time_seg1 = qs - (1 + time_seg2);
|
||||
|
||||
return make_btr(sjw, time_seg1, time_seg2, brp);
|
||||
}
|
||||
|
||||
void
|
||||
can_init(void)
|
||||
{
|
||||
enable_pclock((uint32_t)SOC_CAN);
|
||||
|
||||
gpio_peripheral(GPIO_Rx, CAN_FUNCTION, 1);
|
||||
gpio_peripheral(GPIO_Tx, CAN_FUNCTION, 0);
|
||||
|
||||
uint32_t pclock = get_pclock_frequency((uint32_t)SOC_CAN);
|
||||
|
||||
uint32_t btr = compute_btr(pclock, CONFIG_CANBUS_FREQUENCY);
|
||||
|
||||
/*##-1- Configure the CAN #######################################*/
|
||||
|
||||
/* Exit from sleep mode */
|
||||
SOC_CAN->CCCR &= ~FDCAN_CCCR_CSR;
|
||||
/* Wait the acknowledge */
|
||||
while (SOC_CAN->CCCR & FDCAN_CCCR_CSA)
|
||||
;
|
||||
/* Request initialisation */
|
||||
SOC_CAN->CCCR |= FDCAN_CCCR_INIT;
|
||||
/* Wait the acknowledge */
|
||||
while (!(SOC_CAN->CCCR & FDCAN_CCCR_INIT))
|
||||
;
|
||||
/* Enable configuration change */
|
||||
SOC_CAN->CCCR |= FDCAN_CCCR_CCE;
|
||||
|
||||
/* Disable protocol exception handling */
|
||||
SOC_CAN->CCCR |= FDCAN_CCCR_PXHD;
|
||||
|
||||
SOC_CAN->NBTP = btr;
|
||||
|
||||
/* Leave the initialisation mode */
|
||||
SOC_CAN->CCCR &= ~FDCAN_CCCR_CCE;
|
||||
SOC_CAN->CCCR &= ~FDCAN_CCCR_INIT;
|
||||
|
||||
/*##-2- Configure the CAN Filter #######################################*/
|
||||
canbus_set_filter(0);
|
||||
|
||||
/*##-3- Configure Interrupts #################################*/
|
||||
armcm_enable_irq(CAN_IRQHandler, CAN_IT0_IRQn, 0);
|
||||
SOC_CAN->ILE = FDCAN_ILE_EINT0;
|
||||
SOC_CAN->IE = FDCAN_IE_RF0NE | FDCAN_IE_TC;
|
||||
}
|
||||
DECL_INIT(can_init);
|
||||
@@ -23,14 +23,4 @@ struct gpio_in gpio_in_setup(uint32_t pin, int32_t pull_up);
|
||||
void gpio_in_reset(struct gpio_in g, int32_t pull_up);
|
||||
uint8_t gpio_in_read(struct gpio_in g);
|
||||
|
||||
struct spi_config {
|
||||
void *spi;
|
||||
uint32_t spi_cr1;
|
||||
};
|
||||
struct spi_config spi_setup(uint32_t bus, uint8_t mode, uint32_t rate);
|
||||
void spi_prepare(struct spi_config config);
|
||||
void spi_transfer(struct spi_config config, uint8_t receive_data
|
||||
, uint16_t len, uint8_t *data);
|
||||
void spi_set_rate(struct spi_config *config, uint32_t rate);
|
||||
|
||||
#endif // gpio.h
|
||||
@@ -1,481 +0,0 @@
|
||||
// SDIO Support for STM32 microcontrollers
|
||||
//
|
||||
// Copyright (C) 2022 Eric Callahan <arksine.code@gmail.com>
|
||||
//
|
||||
// This file may be distributed under the terms of the GNU GPLv3 license.
|
||||
|
||||
#include <string.h> // memcpy
|
||||
#include "autoconf.h" // CONFIG_MACH_STM32F1
|
||||
#include "command.h" // DECL_CONSTANT_STR
|
||||
#include "sdcard.h" // sdcard_init
|
||||
#include "internal.h" // enable_pclock
|
||||
#include "sched.h" // udelay
|
||||
|
||||
DECL_CONSTANT_STR("RESERVE_PINS_SDIO", "PD2,PC8,PC9,PC10,PC11,PC12");
|
||||
#define GPIO_SDIO_CMD GPIO('D', 2)
|
||||
#define GPIO_SDIO_D0 GPIO('C', 8)
|
||||
#define GPIO_SDIO_D1 GPIO('C', 9)
|
||||
#define GPIO_SDIO_D2 GPIO('C', 10)
|
||||
#define GPIO_SDIO_D3 GPIO('C', 11)
|
||||
#define GPIO_SDIO_CLK GPIO('C', 12)
|
||||
|
||||
#define SDIO_RESPONSE_SUCCESS 0
|
||||
#define SDIO_RESPONSE_TIMEOUT -1
|
||||
#define SDIO_RESPONSE_CRCERR -2
|
||||
#define SDIO_RESPONSE_STA_ERR -3
|
||||
|
||||
#define SDIO_SHORT_RESPONSE (1 << 6)
|
||||
#define SDIO_LONG_RESPONSE (3 << 6)
|
||||
#define SDIO_READ_TIMEOUT_MS 100
|
||||
#define SDIO_WRITE_TIMEOUT_MS 250
|
||||
#define SDIO_CLOCK_USB 48000000
|
||||
#define SDIO_CLOCK_NO_USB 50000000
|
||||
|
||||
// Rx Error Mask:
|
||||
// SDIO_STA_RXUNDERR | SDIO_STA_DCRCFAIL | SDIO_STA_DTIMEOUT | SDIO_STA_STBITERR
|
||||
#define SDIO_RX_ERR_MASK 0x22A
|
||||
// Tx Error Mask:
|
||||
// SDIO_STA_TXUNDERR | SDIO_STA_DCRCFAIL | SDIO_STA_DTIMEOUT | SDIO_STA_STBITERR
|
||||
#define SDIO_TX_ERR_MASK 0x21A
|
||||
#define SDIO_STATUS_ERR_MASK 0xFDF98008
|
||||
#define SDIO_ICR_CLEAR_FLAGS 0x7FF
|
||||
|
||||
static struct {
|
||||
uint32_t read_timeout, write_timeout, rca;
|
||||
uint8_t flags, error;
|
||||
} sdio_config;
|
||||
enum {SDF_INITIALIZED = 1, SDF_XFER_MODE = 2, SDF_HIGH_CAPACITY = 4,
|
||||
SDF_WRITE_PROTECTED = 8, SDF_CD_OFF = 16, SDF_DEINIT = 32};
|
||||
enum {
|
||||
SDIO_ERROR_NO_IDLE = 1,
|
||||
SDIO_ERROR_SEND_IF_COND,
|
||||
SDIO_ERROR_SEND_OP_COND,
|
||||
SDIO_ERROR_BAD_OCR,
|
||||
SDIO_ERROR_READ_BLOCK,
|
||||
SDIO_ERROR_WRITE_BLOCK,
|
||||
SDIO_ERROR_ALL_SEND_CID,
|
||||
SDIO_ERROR_SEND_REL_ADDR,
|
||||
SDIO_ERROR_SEND_CSD,
|
||||
SDIO_ERROR_SEL_DESEL_CARD,
|
||||
SDIO_ERROR_SET_CARD_DETECT,
|
||||
SDIO_ERROR_SET_BLOCKLEN
|
||||
};
|
||||
|
||||
static uint8_t
|
||||
get_response_type(uint8_t command)
|
||||
{
|
||||
switch (command) {
|
||||
case 0:
|
||||
case 4:
|
||||
case 15:
|
||||
// No response
|
||||
return 0;
|
||||
case 3:
|
||||
return 6;
|
||||
case 5:
|
||||
return 4;
|
||||
case 2:
|
||||
case 9:
|
||||
case 10:
|
||||
return 2;
|
||||
case 41:
|
||||
return 3;
|
||||
case 8:
|
||||
return 7;
|
||||
default:
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
static uint16_t
|
||||
get_response_length(uint8_t command)
|
||||
{
|
||||
if (get_response_type(command) == 2)
|
||||
return SDIO_LONG_RESPONSE;
|
||||
return SDIO_SHORT_RESPONSE;
|
||||
}
|
||||
|
||||
static uint8_t
|
||||
response_has_crc(uint8_t command)
|
||||
{
|
||||
// Commands that return an R3 and R4b response do not
|
||||
// include a CRC
|
||||
if (command == 1 || command == 5 || command == 41)
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static void
|
||||
sdio_set_rate(uint32_t rate)
|
||||
{
|
||||
uint32_t sdioclk;
|
||||
if (CONFIG_MACH_STM32F103)
|
||||
// SDIO on the STM32F103 uses the HCLK frequency
|
||||
sdioclk = CONFIG_CLOCK_FREQ;
|
||||
else if (CONFIG_USBSERIAL)
|
||||
sdioclk = SDIO_CLOCK_USB;
|
||||
else
|
||||
sdioclk = SDIO_CLOCK_NO_USB;
|
||||
uint32_t div = DIV_ROUND_UP(sdioclk, rate);
|
||||
div = (div > 256) ? 255 : ((div >= 2) ? (div - 2) : 0);
|
||||
SDIO->CLKCR = (1 << 8) | (div & 0xFF);
|
||||
uint32_t rate_actual = sdioclk / (div + 2);
|
||||
sdio_config.read_timeout = (rate_actual / 1000) * SDIO_READ_TIMEOUT_MS;
|
||||
sdio_config.write_timeout = (rate_actual / 1000) * SDIO_WRITE_TIMEOUT_MS;
|
||||
}
|
||||
|
||||
static void
|
||||
sdio_init(void)
|
||||
{
|
||||
enable_pclock((uint32_t)SDIO);
|
||||
|
||||
// Init SDIO GPIOs
|
||||
gpio_peripheral(GPIO_SDIO_CMD, GPIO_FUNCTION(12), 0);
|
||||
gpio_peripheral(GPIO_SDIO_D0, GPIO_FUNCTION(12), 0);
|
||||
gpio_peripheral(GPIO_SDIO_D1, GPIO_FUNCTION(12), 0);
|
||||
gpio_peripheral(GPIO_SDIO_D2, GPIO_FUNCTION(12), 0);
|
||||
gpio_peripheral(GPIO_SDIO_D3, GPIO_FUNCTION(12), 0);
|
||||
gpio_peripheral(GPIO_SDIO_CLK, GPIO_FUNCTION(12), 0);
|
||||
|
||||
// Initialize rate to 400K
|
||||
sdio_set_rate(400000);
|
||||
// Power on SDIO
|
||||
SDIO->POWER = 0x3;
|
||||
}
|
||||
|
||||
static void
|
||||
sdio_power_off(void)
|
||||
{
|
||||
SDIO->POWER = 0;
|
||||
}
|
||||
|
||||
static int
|
||||
sdio_send_command(uint8_t command, uint32_t arg)
|
||||
{
|
||||
// clear status flags
|
||||
SDIO->ICR = SDIO_ICR_CLEAR_FLAGS;
|
||||
// set the command argument
|
||||
SDIO->ARG = arg;
|
||||
uint8_t resp_type = get_response_type(command);
|
||||
uint8_t resp_len = 0;
|
||||
uint32_t sta_mask;
|
||||
if (resp_type) {
|
||||
resp_type = get_response_length(command);
|
||||
sta_mask = SDIO_STA_CTIMEOUT | SDIO_STA_CMDREND | SDIO_STA_CCRCFAIL;
|
||||
}
|
||||
else
|
||||
sta_mask = SDIO_STA_CTIMEOUT | SDIO_STA_CMDSENT;
|
||||
SDIO->CMD = (1 << 10) | resp_type | (command & 0x3f);
|
||||
while(!(SDIO->STA & sta_mask));
|
||||
uint32_t done_sta = SDIO->STA;
|
||||
if (done_sta & SDIO_STA_CTIMEOUT)
|
||||
return SDIO_RESPONSE_TIMEOUT;
|
||||
if (resp_len) {
|
||||
if (response_has_crc(command) && (done_sta & SDIO_STA_CCRCFAIL))
|
||||
return SDIO_RESPONSE_CRCERR;
|
||||
|
||||
if (resp_type == 1 && (SDIO->RESP1 && SDIO_STATUS_ERR_MASK))
|
||||
{
|
||||
return SDIO_RESPONSE_STA_ERR;
|
||||
}
|
||||
}
|
||||
return SDIO_RESPONSE_SUCCESS;
|
||||
}
|
||||
|
||||
static uint8_t
|
||||
wait_programming_done(void)
|
||||
{
|
||||
int ret = 0;
|
||||
uint8_t state;
|
||||
uint8_t tries = 100;
|
||||
uint32_t response;
|
||||
while (tries) {
|
||||
udelay(1000);
|
||||
ret = sdio_send_command(SDCMD_SEND_STATUS, sdio_config.rca);
|
||||
response = SDIO->RESP1;
|
||||
state = ((response >> 9) & 0xF);
|
||||
if (ret != SDIO_RESPONSE_SUCCESS || (response & SDIO_STATUS_ERR_MASK))
|
||||
tries--;
|
||||
else if (state != 6 && state != 7)
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
sdio_write_block(uint32_t* write_buf, uint32_t address)
|
||||
{
|
||||
SDIO->DCTRL = 0;
|
||||
// write a single block
|
||||
int ret = sdio_send_command(SDCMD_WRITE_BLOCK, address);
|
||||
if (ret != SDIO_RESPONSE_SUCCESS) {
|
||||
sdio_config.error = SDIO_ERROR_WRITE_BLOCK;
|
||||
return ret;
|
||||
}
|
||||
SDIO->ICR = SDIO_ICR_CLEAR_FLAGS;
|
||||
SDIO->DTIMER = sdio_config.write_timeout;
|
||||
SDIO->DLEN = SD_SECTOR_SIZE;
|
||||
SDIO->DCTRL = (9 << 4) | SDIO_DCTRL_DTEN;
|
||||
uint32_t word_count = 0;
|
||||
uint32_t status;
|
||||
while (1) {
|
||||
status = SDIO->STA;
|
||||
if (status & SDIO_STA_TXFIFOHE &&
|
||||
(word_count < (SD_SECTOR_SIZE / 4)))
|
||||
{
|
||||
SDIO->FIFO = write_buf[word_count++];
|
||||
SDIO->FIFO = write_buf[word_count++];
|
||||
SDIO->FIFO = write_buf[word_count++];
|
||||
SDIO->FIFO = write_buf[word_count++];
|
||||
SDIO->FIFO = write_buf[word_count++];
|
||||
SDIO->FIFO = write_buf[word_count++];
|
||||
SDIO->FIFO = write_buf[word_count++];
|
||||
SDIO->FIFO = write_buf[word_count++];
|
||||
}
|
||||
if (status & SDIO_TX_ERR_MASK) {
|
||||
ret = -4;
|
||||
sdio_config.error = SDIO_ERROR_WRITE_BLOCK;
|
||||
break;
|
||||
} else if(status & SDIO_STA_DBCKEND)
|
||||
break;
|
||||
}
|
||||
// Wait for card to finish transmitting/programming
|
||||
if (!wait_programming_done()) {
|
||||
ret = -5;
|
||||
sdio_config.error = SDIO_ERROR_WRITE_BLOCK;
|
||||
}
|
||||
// clear transfer state
|
||||
SDIO->DCTRL = 0;
|
||||
SDIO->ICR = SDIO_ICR_CLEAR_FLAGS;
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
static int
|
||||
sdio_read_block(uint32_t* read_buf, uint32_t address)
|
||||
{
|
||||
// read a single block
|
||||
int ret = sdio_send_command(SDCMD_READ_SINGLE_BLOCK, address);
|
||||
if (ret != SDIO_RESPONSE_SUCCESS) {
|
||||
sdio_config.error = SDIO_ERROR_READ_BLOCK;
|
||||
return ret;
|
||||
}
|
||||
SDIO->DTIMER = sdio_config.read_timeout;
|
||||
SDIO->DLEN = SD_SECTOR_SIZE;
|
||||
SDIO->DCTRL = (9 << 4) | SDIO_DCTRL_DTDIR | SDIO_DCTRL_DTEN;
|
||||
uint32_t word_count = 0;
|
||||
uint32_t status;
|
||||
while (1) {
|
||||
status = SDIO->STA;
|
||||
// Read out 8 words when FIFO is half full to maximize
|
||||
// performance and prevent buffer overrun
|
||||
if (status & SDIO_STA_RXFIFOHF) {
|
||||
read_buf[word_count++] = SDIO->FIFO;
|
||||
read_buf[word_count++] = SDIO->FIFO;
|
||||
read_buf[word_count++] = SDIO->FIFO;
|
||||
read_buf[word_count++] = SDIO->FIFO;
|
||||
read_buf[word_count++] = SDIO->FIFO;
|
||||
read_buf[word_count++] = SDIO->FIFO;
|
||||
read_buf[word_count++] = SDIO->FIFO;
|
||||
read_buf[word_count++] = SDIO->FIFO;
|
||||
}
|
||||
if (status & SDIO_RX_ERR_MASK) {
|
||||
ret = -4;
|
||||
sdio_config.error = SDIO_ERROR_READ_BLOCK;
|
||||
break;
|
||||
}
|
||||
else if (status & SDIO_STA_DBCKEND)
|
||||
break;
|
||||
}
|
||||
// Clear the buffer remaining buffer
|
||||
while (SDIO->STA & SDIO_STA_RXDAVL)
|
||||
read_buf[word_count++] = SDIO->FIFO;
|
||||
|
||||
// Verify we received the full sector
|
||||
if (ret == SDIO_RESPONSE_SUCCESS && (word_count != (SD_SECTOR_SIZE / 4))) {
|
||||
ret = -5;
|
||||
sdio_config.error = SDIO_ERROR_READ_BLOCK;
|
||||
}
|
||||
// clear transfer state
|
||||
SDIO->DCTRL = 0;
|
||||
SDIO->ICR = SDIO_ICR_CLEAR_FLAGS;
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
static uint8_t
|
||||
check_command(uint8_t cmd, uint32_t arg, uint8_t is_acmd,
|
||||
uint32_t expect, uint32_t mask, uint8_t attempts)
|
||||
{
|
||||
int ret;
|
||||
while (attempts--) {
|
||||
if (is_acmd) {
|
||||
ret = sdio_send_command(SDCMD_APP_CMD, sdio_config.rca);
|
||||
if (ret != SDIO_RESPONSE_SUCCESS) {
|
||||
if (attempts) udelay(1000);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
ret = sdio_send_command(cmd, arg);
|
||||
if (ret == SDIO_RESPONSE_SUCCESS)
|
||||
{
|
||||
if ((get_response_type(cmd) == 0) ||
|
||||
((SDIO->RESP1 & mask) == expect))
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
if (attempts)
|
||||
udelay((cmd == SDCMD_SEND_OP_COND) ? 100000 : 1000);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static uint8_t
|
||||
check_interface_condition(void)
|
||||
{
|
||||
int ret;
|
||||
uint8_t attempts = 3;
|
||||
uint32_t response;
|
||||
while(attempts--)
|
||||
{
|
||||
// CMD8, Request 2.7V-3.6V range, check pattern 0x0A
|
||||
ret = sdio_send_command(SDCMD_SEND_IF_COND, 0x10A);
|
||||
response = SDIO->RESP1;
|
||||
if (ret == SDIO_RESPONSE_TIMEOUT || (response & (1 << 22)))
|
||||
// illegal command, version 1 card
|
||||
return 1;
|
||||
else if (ret == SDIO_RESPONSE_SUCCESS && (response & 0xFFF) == 0x10A)
|
||||
return 2;
|
||||
if (attempts) udelay(1000);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**********************************************************
|
||||
*
|
||||
* SDCard Methods
|
||||
*
|
||||
* ********************************************************/
|
||||
|
||||
uint8_t
|
||||
sdcard_write_sector(uint8_t *buf, uint32_t sector)
|
||||
{
|
||||
if (!(sdio_config.flags & SDF_INITIALIZED))
|
||||
return 0;
|
||||
uint32_t addr = sector;
|
||||
if (!(sdio_config.flags & SDF_HIGH_CAPACITY))
|
||||
addr = sector * SD_SECTOR_SIZE;
|
||||
int ret = sdio_write_block((uint32_t *)buf, addr);
|
||||
if (ret < 0)
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
uint8_t
|
||||
sdcard_read_sector(uint8_t *buf, uint32_t sector)
|
||||
{
|
||||
if (!(sdio_config.flags & SDF_INITIALIZED))
|
||||
return 0;
|
||||
uint32_t addr = sector;
|
||||
if (!(sdio_config.flags & SDF_HIGH_CAPACITY))
|
||||
addr = sector * SD_SECTOR_SIZE;
|
||||
int ret = sdio_read_block((uint32_t *)buf, addr);
|
||||
if (ret < 0)
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
uint8_t
|
||||
sdcard_init(void)
|
||||
{
|
||||
uint8_t sd_ver;
|
||||
sdio_init();
|
||||
// Attempt software reset
|
||||
|
||||
// Software Reset (CMD0)
|
||||
sdio_send_command(SDCMD_GO_IDLE_STATE, 0);
|
||||
// Check interface condition to determine card version
|
||||
sd_ver = check_interface_condition();
|
||||
if (!sd_ver) {
|
||||
sdio_config.error = SDIO_ERROR_SEND_IF_COND;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// CMD41 - Go Operational, voltage window 3.2-3.4v
|
||||
uint8_t arg = ((sd_ver == 1) ? 0 : (1 << 30)) | (3 << 20);
|
||||
uint32_t ocr_expect = (1 << 31) | (3 << 20);
|
||||
if (!check_command(SDCMD_SEND_OP_COND, arg, 1, ocr_expect, ocr_expect, 20))
|
||||
{
|
||||
sdio_config.error = SDIO_ERROR_SEND_OP_COND;
|
||||
return 0;
|
||||
}
|
||||
if (SDIO->RESP1 & (1 << 30))
|
||||
sdio_config.flags |= SDF_HIGH_CAPACITY;
|
||||
|
||||
// request CID
|
||||
if (sdio_send_command(SDCMD_ALL_SEND_CID, 0) < 0) {
|
||||
sdio_config.error = SDIO_ERROR_ALL_SEND_CID;
|
||||
return 0;
|
||||
}
|
||||
// Set Relative Address, don't allow the card to select "0"
|
||||
while (!sdio_config.rca) {
|
||||
if (sdio_send_command(SDCMD_SEND_REL_ADDR, 0) < 0) {
|
||||
sdio_config.error = SDIO_ERROR_SEND_REL_ADDR;
|
||||
return 0;
|
||||
}
|
||||
sdio_config.rca = SDIO->RESP1 & 0xFFFF0000;
|
||||
}
|
||||
// Request CSD
|
||||
if (sdio_send_command(SDCMD_SEND_CSD, sdio_config.rca) < 0) {
|
||||
sdio_config.error = SDIO_ERROR_SEND_CSD;
|
||||
return 0;
|
||||
}
|
||||
if (SDIO->RESP4 & (3 << 12)) {
|
||||
// card is write protected, can't rename the
|
||||
// firmware file after upload.
|
||||
sdio_config.flags |= SDF_WRITE_PROTECTED;
|
||||
sdio_config.error = SDIO_ERROR_WRITE_BLOCK;
|
||||
return 0;
|
||||
}
|
||||
// card is out of identification mode, increase clock
|
||||
sdio_set_rate(4000000);
|
||||
// put card in transfer mode
|
||||
if (sdio_send_command(SDCMD_SEL_DESEL_CARD, sdio_config.rca) < 0) {
|
||||
sdio_config.error = SDIO_ERROR_SEL_DESEL_CARD;
|
||||
return 0;
|
||||
}
|
||||
sdio_config.flags |= SDF_XFER_MODE;
|
||||
if (check_command(SDCMD_SET_CLR_CD_DETECT, 0, 1, 0, 0, 1))
|
||||
sdio_config.flags |= SDF_CD_OFF;
|
||||
else {
|
||||
sdio_config.error = SDIO_ERROR_SET_CARD_DETECT;
|
||||
return 0;
|
||||
}
|
||||
// Set block length to 512
|
||||
if (sdio_send_command(SDCMD_SET_BLOCKLEN, SD_SECTOR_SIZE) < 0) {
|
||||
sdio_config.error = SDIO_ERROR_SET_BLOCKLEN;
|
||||
return 0;
|
||||
}
|
||||
sdio_config.flags |= SDF_INITIALIZED;
|
||||
return 1;
|
||||
}
|
||||
|
||||
void
|
||||
sdcard_deinit(void)
|
||||
{
|
||||
if (sdio_config.flags & SDF_DEINIT)
|
||||
return;
|
||||
sdio_config.flags |= SDF_DEINIT;
|
||||
if (sdio_config.flags & SDF_CD_OFF) {
|
||||
if (check_command(SDCMD_SET_CLR_CD_DETECT, 1, 1, 0, 0, 1))
|
||||
sdio_config.flags &= ~SDF_CD_OFF;
|
||||
}
|
||||
if (sdio_config.flags |= SDF_XFER_MODE) {
|
||||
sdio_send_command(SDCMD_SEL_DESEL_CARD, 0);
|
||||
sdio_config.flags &= ~SDF_XFER_MODE;
|
||||
}
|
||||
sdio_send_command(SDCMD_GO_IDLE_STATE, 0);
|
||||
sdio_power_off();
|
||||
sdio_config.rca = 0;
|
||||
}
|
||||
-138
@@ -1,138 +0,0 @@
|
||||
// SPI functions on STM32
|
||||
//
|
||||
// Copyright (C) 2019 Kevin O'Connor <kevin@koconnor.net>
|
||||
//
|
||||
// This file may be distributed under the terms of the GNU GPLv3 license.
|
||||
|
||||
#include "board/io.h" // readb, writeb
|
||||
#include "command.h" // DECL_ENUMERATION
|
||||
#include "gpio.h" // spi_setup
|
||||
#include "internal.h" // gpio_peripheral
|
||||
#include "sched.h" // sched_shutdown
|
||||
|
||||
struct spi_info {
|
||||
SPI_TypeDef *spi;
|
||||
uint8_t miso_pin, mosi_pin, sck_pin, function;
|
||||
};
|
||||
|
||||
DECL_ENUMERATION("spi_bus", "spi2", 0);
|
||||
DECL_CONSTANT_STR("BUS_PINS_spi2", "PB14,PB15,PB13");
|
||||
DECL_ENUMERATION("spi_bus", "spi1", 1);
|
||||
DECL_CONSTANT_STR("BUS_PINS_spi1", "PA6,PA7,PA5");
|
||||
DECL_ENUMERATION("spi_bus", "spi1a", 2);
|
||||
DECL_CONSTANT_STR("BUS_PINS_spi1a", "PB4,PB5,PB3");
|
||||
#if !CONFIG_MACH_STM32F1
|
||||
DECL_ENUMERATION("spi_bus", "spi2a", 3);
|
||||
DECL_CONSTANT_STR("BUS_PINS_spi2a", "PC2,PC3,PB10");
|
||||
#endif
|
||||
#ifdef SPI3
|
||||
DECL_ENUMERATION("spi_bus", "spi3", 4);
|
||||
DECL_CONSTANT_STR("BUS_PINS_spi3", "PB4,PB5,PB3");
|
||||
#if CONFIG_MACH_STM32F4
|
||||
DECL_ENUMERATION("spi_bus", "spi3a", 5);
|
||||
DECL_CONSTANT_STR("BUS_PINS_spi3a", "PC11,PC12,PC10");
|
||||
#ifdef SPI4
|
||||
DECL_ENUMERATION("spi_bus", "spi4", 6);
|
||||
DECL_CONSTANT_STR("BUS_PINS_spi4", "PE13,PE14,PE12");
|
||||
#elif defined(GPIOI)
|
||||
DECL_ENUMERATION("spi_bus", "spi2b", 6);
|
||||
DECL_CONSTANT_STR("BUS_PINS_spi2b", "PI2,PI3,PI1");
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if CONFIG_MACH_STM32F0 || CONFIG_MACH_STM32G0
|
||||
#define SPI_FUNCTION GPIO_FUNCTION(0)
|
||||
#else
|
||||
#define SPI_FUNCTION GPIO_FUNCTION(5)
|
||||
#endif
|
||||
|
||||
static const struct spi_info spi_bus[] = {
|
||||
{ SPI2, GPIO('B', 14), GPIO('B', 15), GPIO('B', 13), SPI_FUNCTION },
|
||||
{ SPI1, GPIO('A', 6), GPIO('A', 7), GPIO('A', 5), SPI_FUNCTION },
|
||||
{ SPI1, GPIO('B', 4), GPIO('B', 5), GPIO('B', 3), SPI_FUNCTION },
|
||||
{ SPI2, GPIO('C', 2), GPIO('C', 3), GPIO('B', 10), SPI_FUNCTION },
|
||||
#ifdef SPI3
|
||||
{ SPI3, GPIO('B', 4), GPIO('B', 5), GPIO('B', 3), GPIO_FUNCTION(6) },
|
||||
#if CONFIG_MACH_STM32F4
|
||||
{ SPI3, GPIO('C', 11), GPIO('C', 12), GPIO('C', 10), GPIO_FUNCTION(6) },
|
||||
#ifdef SPI4
|
||||
{ SPI4, GPIO('E', 13), GPIO('E', 14), GPIO('E', 12), GPIO_FUNCTION(5) },
|
||||
#elif defined(GPIOI)
|
||||
{ SPI2, GPIO('I', 2), GPIO('I', 3), GPIO('I', 1), GPIO_FUNCTION(5) },
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
};
|
||||
|
||||
struct spi_config
|
||||
spi_setup(uint32_t bus, uint8_t mode, uint32_t rate)
|
||||
{
|
||||
// Enable SPI
|
||||
SPI_TypeDef *spi = spi_bus[bus].spi;
|
||||
if (!is_enabled_pclock((uint32_t)spi)) {
|
||||
enable_pclock((uint32_t)spi);
|
||||
gpio_peripheral(spi_bus[bus].miso_pin, spi_bus[bus].function, 1);
|
||||
gpio_peripheral(spi_bus[bus].mosi_pin, spi_bus[bus].function, 0);
|
||||
gpio_peripheral(spi_bus[bus].sck_pin, spi_bus[bus].function, 0);
|
||||
|
||||
// Configure CR2 on stm32f0
|
||||
#if CONFIG_MACH_STM32F0 || CONFIG_MACH_STM32G0
|
||||
spi->CR2 = SPI_CR2_FRXTH | (7 << SPI_CR2_DS_Pos);
|
||||
#endif
|
||||
}
|
||||
|
||||
// Calculate CR1 register
|
||||
uint32_t pclk = get_pclock_frequency((uint32_t)spi);
|
||||
uint32_t div = 0;
|
||||
while ((pclk >> (div + 1)) > rate && div < 7)
|
||||
div++;
|
||||
uint32_t cr1 = ((mode << SPI_CR1_CPHA_Pos) | (div << SPI_CR1_BR_Pos)
|
||||
| SPI_CR1_SPE | SPI_CR1_MSTR | SPI_CR1_SSM | SPI_CR1_SSI);
|
||||
|
||||
return (struct spi_config){ .spi = spi, .spi_cr1 = cr1 };
|
||||
}
|
||||
|
||||
void
|
||||
spi_set_rate(struct spi_config *config, uint32_t rate)
|
||||
{
|
||||
SPI_TypeDef *spi = config->spi;
|
||||
uint32_t pclk = get_pclock_frequency((uint32_t)spi);
|
||||
uint32_t div = 0;
|
||||
while ((pclk >> (div + 1)) > rate && div < 7)
|
||||
div++;
|
||||
config->spi_cr1 &= ~SPI_CR1_BR;
|
||||
config->spi_cr1 |= div << SPI_CR1_BR_Pos;
|
||||
}
|
||||
|
||||
void
|
||||
spi_prepare(struct spi_config config)
|
||||
{
|
||||
SPI_TypeDef *spi = config.spi;
|
||||
uint32_t cr1 = spi->CR1;
|
||||
if (cr1 == config.spi_cr1)
|
||||
return;
|
||||
// The SPE bit must be disabled before changing CPOL/CPHA bits
|
||||
spi->CR1 = cr1 & ~SPI_CR1_SPE;
|
||||
spi->CR1; // Force flush of previous write
|
||||
spi->CR1 = config.spi_cr1;
|
||||
}
|
||||
|
||||
void
|
||||
spi_transfer(struct spi_config config, uint8_t receive_data,
|
||||
uint16_t len, uint8_t *data)
|
||||
{
|
||||
SPI_TypeDef *spi = config.spi;
|
||||
while (len--) {
|
||||
writeb((void*)&spi->DR, *data);
|
||||
while (!(spi->SR & SPI_SR_RXNE))
|
||||
;
|
||||
uint8_t rdata = readb((void*)&spi->DR);
|
||||
if (receive_data)
|
||||
*data = rdata;
|
||||
data++;
|
||||
}
|
||||
// Wait for any remaining SCLK updates before returning
|
||||
while ((spi->SR & (SPI_SR_TXE|SPI_SR_BSY)) != SPI_SR_TXE)
|
||||
;
|
||||
}
|
||||
@@ -30,6 +30,8 @@ lookup_clock_line(uint32_t periph_base)
|
||||
uint32_t bit = 1 << ((periph_base - AHBPERIPH_BASE) / 0x400);
|
||||
return (struct cline){.en=&RCC->AHBENR, .rst=&RCC->AHBRSTR, .bit=bit};
|
||||
}
|
||||
if ((periph_base == FDCAN1_BASE) || (periph_base == FDCAN2_BASE))
|
||||
return (struct cline){.en=&RCC->APBENR1,.rst=&RCC->APBRSTR1,.bit=1<<12};
|
||||
if (periph_base == USB_BASE)
|
||||
return (struct cline){.en=&RCC->APBENR1,.rst=&RCC->APBRSTR1,.bit=1<<13};
|
||||
if (periph_base == CRS_BASE)
|
||||
|
||||
Reference in new issue
Block a user