This commit is contained in:
huxi
2025-12-03 11:12:34 +08:00
parent c23ae4f24c
commit bc195654bf
8163 changed files with 3799544 additions and 92 deletions
@@ -0,0 +1,383 @@
#ifndef ASM_CPU_H
#define ASM_CPU_H
#include "sfr.h"
#include "csfr.h"
#ifndef __ASSEMBLY__
typedef unsigned char u8, bool, BOOL;
typedef char s8;
typedef unsigned short u16;
typedef signed short s16;
typedef unsigned int u32;
typedef signed int s32;
typedef unsigned long long u64;
typedef u32 FOURCC;
typedef long long s64;
typedef unsigned long long u64;
#endif
#define ___trig __asm__ volatile ("trigger")
#ifndef BIG_ENDIAN
#define BIG_ENDIAN 0x3021
#endif
#ifndef LITTLE_ENDIAN
#define LITTLE_ENDIAN 0x4576
#endif
#define CPU_ENDIAN LITTLE_ENDIAN
#ifdef BT_DUT_INTERFERE
#define CPU_CORE_NUM 1 //dut interfere
#else
#define CPU_CORE_NUM 1 //sdk
#endif
#define OS_CORE_AFFINITY_ENABLE 1
#define CPU_TASK_CLR(a)
#define CPU_TASK_SW(a) \
do { \
q32DSP(a)->ILAT_SET |= BIT(3-a); \
} while (0)
#define CPU_INT_NESTING 2
#ifndef __ASSEMBLY__
#if CPU_CORE_NUM > 1
__attribute__((always_inline))
static int current_cpu_id()
{
unsigned id;
asm volatile("%0 = cnum" : "=r"(id) ::);
return id ;
}
__attribute__((always_inline))
static int core_num(void)
{
u32 num;
asm volatile("%0 = cnum" : "=r"(num) :);
return num;
}
#else
static inline int current_cpu_id()
{
return 0;
}
static inline int core_num(void)
{
return 0;
}
#endif
static inline int cpu_in_irq()
{
int flag;
__asm__ volatile("%0 = icfg" : "=r"(flag));
return flag & 0xff;
}
static inline int cpu_irq_disabled()
{
int flag;
__asm__ volatile("%0 = icfg" : "=r"(flag));
return (flag & 0x300) != 0x300;
}
#if 0
static inline int data_sat_s16(int ind)
{
if (ind > 32767) {
ind = 32767;
} else if (ind < -32768) {
ind = -32768;
}
return ind;
}
#else
static inline int data_sat_s16(int ind)
{
__asm__ volatile(
" %0 = sat16(%0)(s) \t\n"
: "=&r"(ind)
: "0"(ind)
:);
return ind;
}
#endif
static inline u32 reverse_u32(u32 data32)
{
#if 0
u8 *dataptr = (u8 *)(&data32);
data32 = (((u32)dataptr[0] << 24) | ((u32)dataptr[1] << 16) | ((u32)dataptr[2] << 8) | (u32)dataptr[3]);
#else
__asm__ volatile("%0 = rev8(%0) \t\n" : "=&r"(data32) : "0"(data32) :);
#endif
return data32;
}
static inline u32 reverse_u16(u16 data16)
{
u32 retv;
#if 0
u8 *dataptr = (u8 *)(&data16);
retv = (((u32)dataptr[0] << 8) | ((u32)dataptr[1]));
#else
retv = ((u32)data16) << 16;
__asm__ volatile("%0 = rev8(%0) \t\n" : "=&r"(retv) : "0"(retv) :);
#endif
return retv;
}
static inline u32 rand32()
{
return JL_RAND->R64L;
}
#define __asm_sine(s64, precision) \
({ \
u64 ret; \
u8 sel = 0; \
__asm__ volatile ("%0 = copex(%1) (%2)" : "=r"(ret) : "r"(s64), "i"(sel)); \
ret = ret>>32; \
ret;\
})
void p33_soft_reset(void);
static inline void cpu_reset(void)
{
// JL_CLOCK->PWR_CON |= (1 << 4);
p33_soft_reset();
}
#define __asm_csync() \
do { \
asm volatile("csync;"); \
} while (0)
#include "irq.h"
#include "printf.h"
#include "log.h"
#define arch_atomic_read(v) \
({ \
__asm_csync(); \
(*(volatile int *)&(v)->counter); \
})
#if 0
extern volatile int cpu_lock_cnt[];
extern volatile int irq_lock_cnt[];
static inline void local_irq_disable()
{
__builtin_pi32v2_cli();
irq_lock_cnt[current_cpu_id()]++;
}
static inline void local_irq_enable()
{
if (--irq_lock_cnt[current_cpu_id()] == 0) {
__builtin_pi32v2_sti();
}
}
#else
extern void __local_irq_disable() ;
extern void __local_irq_enable() ;
extern void local_irq_disable();
extern void local_irq_enable();
#endif
#if(0 )
#define arch_spin_trylock(lock) \
do { \
__asm_csync(); \
while ((lock)->rwlock); \
(lock)->rwlock = 1; \
}while(0)
#define arch_spin_lock(lock) \
do { \
int ret = false; \
__asm_csync(); \
if (!(lock)->rwlock) { \
ret = true; \
(lock)->rwlock = 1; \
} \
if (ret) \
break; \
}while(1)
#define arch_spin_unlock(lock) \
do { \
(lock)->rwlock = 0; \
}while(0)
#else
static inline void q32DSP_testset(u32 volatile *ptr)
{
asm volatile(
" 1: \n\t "
" testset b[%0] \n\t "
" ifeq goto 1b \n\t "
:
: "p"(ptr)
: "memory"
);
}
static inline void q32DSP_testclr(u32 volatile *ptr)
{
asm volatile(
" b[%0] = %1 \n\t "
:
: "p"(ptr), "r"(0)
: "memory"
);
}
#define arch_spin_trylock(lock) \
do { \
q32DSP_testset(&lock->rwlock);\
}while(0)
#define arch_spin_lock(lock) arch_spin_trylock(lock)
#define arch_spin_unlock(lock) \
do{ \
q32DSP_testclr(&lock->rwlock) ;\
}while(0)
#endif
#if 1 // CPU_CORE_NUM >1
extern volatile int cpu_lock_cnt[];
extern volatile int irq_lock_cnt[];
#if 0
#define CPU_SR_ALLOC() \
// int flags
#define CPU_CRITICAL_ENTER() \
do { extern u8 volatile cpulock;\
local_irq_disable(); \
if(cpu_lock_cnt[current_cpu_id()]++ == 0) \
q32DSP_testset(&cpulock);\
__asm_csync(); \
}while(0)
// asm volatile("lockset;");
#define CPU_CRITICAL_EXIT() \
do {extern u8 volatile cpulock; \
if (--cpu_lock_cnt[current_cpu_id()] == 0) \
q32DSP_testclr(&cpulock);\
local_irq_enable();\
}while(0)
#endif
static inline int get_random()
{
return JL_RAND->R64L;
}
#define OS_SR_ALLOC()
#define OS_ENTER_CRITICAL() \
CPU_CRITICAL_ENTER(); \
#define OS_EXIT_CRITICAL() \
CPU_CRITICAL_EXIT()
#define CPU_SR_ALLOC() \
// int flags
#define CPU_CRITICAL_ENTER() \
do { \
local_irq_disable(); \
}while(0)
#define CPU_CRITICAL_EXIT() \
do { \
local_irq_enable(); \
}while(0)
// asm volatile("lockclr;");
#else
#define CPU_SR_ALLOC() \
// int flags
#define CPU_CRITICAL_ENTER() \
do { \
local_irq_disable(); \
__asm_csync(); \
}while(0)
#define CPU_CRITICAL_EXIT() \
do { \
local_irq_enable(); \
}while(0)
#endif
extern void cpu_assert_debug();
extern const int config_asser;
#define ASSERT(a,...) \
do { \
if(1 /* config_asser */){\
if(!(a)){ \
printf("cpu %d file:%s, line:%d",current_cpu_id(), __FILE__, __LINE__); \
printf("ASSERT-FAILD: "#a" "__VA_ARGS__); \
cpu_assert_debug(); \
} \
}else {\
if(!(a)){ \
cpu_reset(); \
}\
}\
}while(0);
#endif //__ASSEMBLY__
#endif
@@ -0,0 +1,71 @@
#ifndef __DEBUG_H__
#define __DEBUG_H__
#define CDBG_IDx(n, id) ((1<<(n+4)) | (id<<(n*8+8)))
#define CDBG_INV (1<<7)
#define CDBG_PEN (1<<3)
#define CDBG_XEN (1<<2)
#define CDBG_WEN (1<<1)
#define CDBG_REN (1<<0)
void debug_init();
void exception_analyze();
/********************************** DUBUG SFR *****************************************/
u32 get_dev_id(char *name);
/* ---------------------------------------------------------------------------- */
/**
* @brief Memory权限保护设置
*
* @param idx: 保护框索引, 范围: 0 ~ 3, 目前系统默认使用0和3, 用户可用1和2
* @param begin: Memory开始地址
* @param end: Memory结束地址
* @param inv: 0: 保护框内, 1: 保护框外
* @param format: "Cxwr0rw1rw2rw3rw", CPU:外设0:外设1:外设2:外设3,
* @param ...: 外设ID号索引, 如: DBG_EQ, 见debug.h
*/
/* ---------------------------------------------------------------------------- */
void mpu_set(int idx, u32 begin, u32 end, u32 inv, const char *format, ...);
/* ---------------------------------------------------------------------------- */
/**
* @brief 取消指定框的mpu保护
*
* @param idx: 保护框索引号
*/
/* ---------------------------------------------------------------------------- */
void mpu_disable_by_index(u8 idx);
/* ---------------------------------------------------------------------------- */
/**
* @brief :取消所有保护框mpu保护
*/
/* ---------------------------------------------------------------------------- */
void mpu_diasble(void);
/* ---------------------------------------------------------------------------- */
/**
* @brief flash PC范围设置为Flash外区域, 调用该接口后调用flash里的函数将触发异常
*/
/* ---------------------------------------------------------------------------- */
void flash_pc_limit_disable();
/* ---------------------------------------------------------------------------- */
/**
* @brief flash PC范围限制恢复为flash代码区域, 调用该接口后可调用flash里的函数
*/
/* ---------------------------------------------------------------------------- */
void flash_pc_limit_enable();
#endif
@@ -0,0 +1,70 @@
/*-----------------------------------------------------------------------
/ Low level disk interface modlue include file R0.06 (C)ChaN, 2007
/-----------------------------------------------------------------------*/
#ifndef _DISKIO
// #include "logic_include.h"
//#ifdef __ICC8051__
/* Status of Disk Functions */
typedef u8 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;
///#endif
/*---------------------------------------*/
/* Prototypes for disk control functions */
//DSTATUS disk_initialize (BYTE);
//DSTATUS disk_status (BYTE);
//DRESULT disk_read (BYTE, BYTE*, DWORD, BYTE);
//#if _READONLY == 0
//DRESULT disk_write (BYTE, const BYTE*, DWORD, BYTE);
//#endif
//DRESULT disk_ioctl (BYTE, BYTE, void*);
//void disk_timerproc (void);
#define _READONLY 0 /* 1: Read-only mode */
#define _USE_IOCTL 1
/* 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() */
/* Generic command */
#define CTRL_SYNC 0 /* Mandatory for read/write configuration */
#define GET_SECTOR_COUNT 1 /* Mandatory for only f_mkfs() */
#define GET_SECTOR_SIZE 2
#define GET_BLOCK_SIZE 3 /* Mandatory for only f_mkfs() */
#define CTRL_POWER 4
#define CTRL_LOCK 5
#define CTRL_EJECT 6
/* MMC/SDC command */
#define MMC_GET_TYPE 10
#define MMC_GET_CSD 11
#define MMC_GET_CID 12
#define MMC_GET_OCR 13
#define MMC_GET_SDSTAT 14
/* ATA/CF command */
#define ATA_GET_REV 20
#define ATA_GET_MODEL 21
#define ATA_GET_SN 22
#define _DISKIO
#endif
@@ -0,0 +1,448 @@
#ifndef _FATFS_H
#define _FATFS_H
#include "common.h"
#include "diskio.h"
// #include "logic_include.h"
#define MAX_DEEPTH 2 /* 0~9 deepth of system */
#define LFN_MAX_SIZE 512 //不能超过512
/*******************************************************/
/********************驱动调试信息***********************/
/*******************************************************/
//#define FS_DEBUG
#ifdef FS_DEBUG
#define fs_deg printf
#define fs_deg_puts printf
#define fs_deg_buf printf_buf
#define fs_deg_data printf_data
#else
#define fs_deg(...)
#define fs_deg_puts(...)
#define fs_deg_buf(...)
#define fs_deg_data(...)
#endif
#define FS_LD_WORD(p) ld_word_func(p)//(u16)(*(u16*)(u8*)(ptr))
#define FS_LD_DWORD(p) ld_dword_func(p)//(u32)(*(u32*)(u8*)(ptr))
#define FS_ST_WORD(ptr,val) st_word_func((u8 *)(ptr),(u16)(val)) //(u16)(*(u16*)(u8*)(ptr))
#define FS_ST_DWORD(ptr,val) st_dword_func((u8 *)(ptr),(u32)(val)) //(u32)(*(u32*)(u8*)(ptr))
typedef u32 CLUST;
#define FS_WRITE_EN /* 是否允许写文件 */
#ifdef FS_WRITE_EN
#define FAT12_WRITE_EN
//#define EXFAT_WRITE_EN
#endif
#define WIN_DATA_DIRTY 0x08
/* File system API info */
typedef struct _FSAPIMSG {
u16 musicdir_counter; // 包含指定文件的文件夹序号
u16 dir_counter; // 文件夹序号
u16 file_total_indir; // 当前目录的根下有效文件的个数
u16 file_total_outdir; // 当前目录前的文件总数,目录循环模式下,需要用它来计算文件序号
u16 file_number; // 当前文件序号
u16 file_counter; //用于搜索文件计数
u8 deepth; // dir deepth for search
} FSAPIMSG;
typedef struct _SWIN_BUF {
u8 start[512];
u32 sector;
struct _FATFS *fs;
u8 flag;
} SWIN_BUF;
/*
#define FS_WIN_START fs->win->start
#define FS_WIN_SECTOR fs->win->sector
#define FS_WIN_FLAG fs->win->flag
*/
#define FS_WIN_START fs->win.start
#define FS_WIN_SECTOR fs->win.sector
#define FS_WIN_FLAG fs->win.flag
/* File system object structure */
struct _FATFS {
//SWIN_BUF *win; /* Disk access window for Directory/FAT/File */
u32 fatbase; /* FAT start sector */
u32 dirbase; /* Root directory start sector */
u32 database; /* Data start sector */
u32 n_fatent; /* Maximum cluster# + 1 */
#ifdef FS_WRITE_EN
u8 write_en; //文件系统是否允许写操作:只允许FAT16FAT32并且sector size只能是512bytes
u8 fsi_flag; /* fsinfo dirty flag (1:must be written back) */
u32 last_clust; /* Last allocated cluster */
u32 fsi_sector; /* fsinfo sector (FAT32) */
#endif
u32 boot_sect;
u16 n_rootdir; /* Number of root directory entries */
u16 total_file; /* 当前设备的匹配文件总数*/
u8 s_size; /* sector size, 2 power n*/
u8 csize; /* Number of sectors per cluster,2 power n */
u8 fs_type; /* FAT sub type */
u8 secotr_512size; /* size of per sector */
u32(*disk_read)(u8 *buf, u32 lba); /* device read function */
u32(*disk_write)(u8 *buf, u32 lba); /* device write function */
u32 fat_time;
// u8 *lfn; //长文件名buffer
// u16 lfn_cnt;
// char *tpath; //路径名称buffer
void *hdev;
SWIN_BUF win; /* Disk access window for Directory/FAT/File */
u8 drive_cnt;
//FSAPIMSG *fs_msg;
};
typedef struct _FATFS FATFS;
struct _FS_NAME {
char tpath[128]; //路径名称buffer
char *lfn; //长文件名buffer
u16 lfn_cnt;
};
typedef struct _FS_NAME FS_NAME;
typedef u32(*disk_read)(u8 *buf, u32 lba);
typedef u32(*disk_write)(u8 *buf, u32 lba);
/* Directory object structure */
typedef struct _DIR {
u32 clust; /* Current cluster */
u32 csect; /* Current sector */
u32 sclust; /* Start cluster */
u16 cindex; /* Current index */
//u16 lfn_index; /* 长文件名index号 */
} DIR;
/* DIR status structure */
typedef struct _DIRINFO {
DIR dj;
u32 sclust; /* File start cluster */
u32 fsize; /* Size */
u8 fattrib; /* Attribute */
char fname[12]; /* */
} DIRINFO;
/* File object structure */
/*
typedef struct _FIL
{
u32 fptr; // File R/W pointer
//u32 fsize; // File size
u8 flag;
SWIN_BUF *data_win;
u32 csect; // Current sector
u32 clust; // Current cluster
//u32 sclust; // File start cluster
#ifdef FS_WRITE_EN
SWIN_BUF *dir_win;
u8 dir_duty;
#endif
u32 start_clust;
u32 end_clust;
FATFS *fs;
DIRINFO dir_info; //文件的目录项信息
FSAPIMSG fs_msg;
} FIL;
*/
typedef struct _FIL0 {
u32 fptr; // File R/W pointer
//u32 fsize; // File size
u8 flag;
SWIN_BUF data_win;
u32 csect; // Current sector
u32 clust; // Current cluster
//u32 sclust; // File start cluster
#ifdef FS_WRITE_EN
SWIN_BUF dir_win;
u8 dir_duty;
#endif
u32 start_clust;
u32 end_clust;
FATFS *fs;
} FIL0;
typedef struct _FIL {
FIL0 fil;
DIRINFO dir_info; //文件的目录项信息
FSAPIMSG fs_msg;
//u8 filename[512];///<[1][512];
//u8 path_name[128];///<128
DIR f_dj[MAX_DEEPTH];
FS_NAME fs_n;
} FIL;
typedef struct _LDIR_INFO {
u8 ldir_ord;
u8 ldir_name1[10]; //文件的目录项信息
u8 ldir_attr0;
u8 ldir_type0;
u8 ldir_chksum0;
u8 ldir_name2[12];
u16 ldir_fst_clus_lo;
u8 ldir_name3[4];
} LDIR_INFO;
/* File access control and file status flags (FIL.flag) */
#define FA_OPEN_EXISTING 0x00
#ifdef FS_WRITE_EN
#define FA_CREATE_HIDDEN 0x02
#define FA_WRITE 0x04 //是否允许写文件
#define FA_CREATE_NEW 0x08 //文件不存在时创建
#define FA_CREATE_ALWAYS 0x10 //无论文件中否存在,均创建
#endif
#define FA__ERROR 0x80 //文件错误
#define FDISK__ERROR 0x40 //设备错误
#define DDE 0xE5 /* Deleted directory entry mark in DIR_Name[0] */
#define NDDE 0x05 /* Replacement of the character collides with DDE */
#define SZ_DIR 32
//#ifdef __SMART_CPU__
//#endif
/* File function return code (FRESULT) */
#define _DF1S 1
/* Character code support macros */
#define IsUpper(c) (((c)>='A')&&((c)<='Z'))
#define IsLower(c) (((c)>='a')&&((c)<='z'))
#if _DF1S /* DBCS configuration */
#ifdef _DF2S /* Two 1st byte areas */
#define IsDBCS1(c) (((u8)(c) >= _DF1S && (u8)(c) <= _DF1E) || ((u8)(c) >= _DF2S && (u8)(c) <= _DF2E))
#else /* One 1st byte area */
//#define IsDBCS1(c) ((u8)(c) >= _DF1S && (u8)(c) <= _DF1E)
#define IsDBCS1(c) ((u8)(c) < ' ' && (u8)(c) > '~')
#endif
#ifdef _DS3S /* Three 2nd byte areas */
#define IsDBCS2(c) (((u8)(c) >= _DS1S && (u8)(c) <= _DS1E) || ((u8)(c) >= _DS2S && (u8)(c) <= _DS2E) || ((u8)(c) >= _DS3S && (u8)(c) <= _DS3E))
#else /* Two 2nd byte areas */
#define IsDBCS2(c) (((u8)(c) >= _DS1S && (u8)(c) <= _DS1E) || ((u8)(c) >= _DS2S && (u8)(c) <= _DS2E))
#endif
#else /* SBCS configuration */
#define IsDBCS1(c) 0
#define IsDBCS2(c) 0
#endif /* _DF1S */
/* FAT sub-type boundaries */
/* Note that the FAT spec by Microsoft says 4085 but Windows works with 4087! */
#define MIN_FAT16 4086 /* Minimum number of clusters for FAT16 */
#define MIN_FAT32 65526 /* Minimum number of clusters for FAT32 */
#define ROOT_DIR 0
/* FAT sub type */
#define FS_FAT12 1
#define FS_FAT16 2
#define FS_FAT32 3
#define FS_EXFAT 4
/* File attribute bits for directory entry */
#define AM_RDO 0x01 /* Read only */
#define AM_HID 0x02 /* Hidden */
#define AM_SYS 0x04 /* System */
#define AM_VOL 0x08 /* Volume label */
#define AM_LFN 0x0F /* LFN entry */
#define AM_DIR 0x10 /* Directory */
#define AM_ARC 0x20 /* Archive */
#define AM_FCH 0x80 /* exFAT下,文件簇连续标志 */
/* Offset of FAT structure members */
#define BS_jmpBoot 0
#define BS_OEMName 3
#define BPB_BytsPerSec_l 11
#define BPB_BytsPerSec_h 12
#define BPB_SecPerClus 13
#define BPB_RsvdSecCnt 14
#define BPB_NumFATs 16
#define BPB_RootEntCnt 17
#define BPB_TotSec16 19
#define BPB_Media 21
#define BPB_FATSz16 22
#define BPB_SecPerTrk 24
#define BPB_NumHeads 26
#define BPB_HiddSec 28
#define BPB_TotSec32 32
#define BS_55AA 510
#define BS_DrvNum 36
#define BS_BootSig 38
#define BS_VolID 39
#define BS_VolLab 43
#define BS_FilSysType 54
#define BPB_FATSz32 36
#define BPB_ExtFlags 40
#define BPB_FSVer 42
#define BPB_RootClus 44
#define BPB_FSInfo 48
#define BPB_BkBootSec 50
#define BS_DrvNum32 64
#define BS_BootSig32 66
#define BS_VolID32 67
#define BS_VolLab32 71
#define BS_FilSysType32 82
#define BS_FileSysTypeexFAT 5
#define BPB_FatOffset 80
#define BPB_FatLength 84
#define BPB_ClusterHeapOffset 88
#define BPB_ClusterCount 92
#define BPB_FirstClusterOfRootDirectory 96
#define BPB_VolumeFlags 106
#define BPB_BytesPerSectorShift 108
#define BPB_SectorsPerClusterShift 109
#define BPB_NumberOfFats 110
#define MBR_Table 446
#define FSI_LeadSig 0 /* FSI: Leading signature (4) */
#define FSI_StrucSig 484 /* FSI: Structure signature (4) */
#define FSI_Free_Count 488 /* FSI: Number of free clusters (4) */
#define FSI_Nxt_Free 492 /* FSI: Last allocated cluster (4) */
///for FAT12/FAT16/FAT32
#define DIR_Name 0 /* Short file name (11) */
#define DIR_Attr 11 /* Attribute (1) */
#define DIR_NTres 12 /* NT flag (1) */
#define DIR_CrtTimeTenth 13 /* Created time sub-second (1) */
#define DIR_CrtTime 14 /* Created time (2) */
#define DIR_CrtDate 16 /* Created date (2) */
#define DIR_LstAccDate 18 /* Last accessed date (2) */
#define DIR_FstClusHI 20 /* Higher 16-bit of first cluster (2) */
#define DIR_WrtTime 22 /* Modified time (2) */
#define DIR_WrtDate 24 /* Modified date (2) */
#define DIR_FstClusLO 26
#define DIR_FileSize 28
#define LDIR_Attr 11 /* LFN attribute (1) */
#define LDIR_Type 12 /* LFN type (1) */
#define LDIR_Chksum 13 /* Sum of corresponding SFN entry */
////for exFAT
#define DIR_FileChainFlags 1
#define DIR_NameLen 3
#define DIR_AttrexFAT 4
#define DIR_FileSizeexFAT 8
#define DIR_FstClustexFAT 20
enum {
SEEK_SET = 0x01,
SEEK_CUR = 0x02,
SEEK_END = 0x03
};
typedef enum {
FR_OK = 0,
FR_FIND_DIR = 0x80,
FR_FIND_FILE,
FR_DIR_END, //前面几个位置不能变
FR_NO_FILE,
FR_NO_PATH,
FR_EXIST,
FR_INVALID_NAME,
FR_INVALID_DRIVE,
FR_DENIED,
FR_RW_ERROR,
FR_WRITE_PROTECTED,
FR_NO_FILESYSTEM,
FR_DEEP_LIMIT,
FR_END_PATH,
FR_FILE_LIMIT,
FR_END_FILE,
FR_LFN_ERR,
FR_MKFS_ABORTED,
FR_DIR_DELETE,
FR_DISK_ERROR,
FR_FILE_END,
FR_FILE_ERR,
FR_NO_WINBUF,
FR_INT_ERR, /* (2) Assertion failed */
FR_NO_SEL_DRIVE,
} FRESULT;
/*-----------------------------------------------------*/
/* Tiny-FatFs module application interface */
FRESULT f_seek(FIL *fp, u8 type, u32 offsize);
u16 f_read(FIL *fp, u8 *buff, u16 btr);
u32 f_read_bt_updata(FIL *fp, u8 *buff, u32 btr, u8 read_type, u8 type, u32 offsize);
FRESULT f_readnextdir(FATFS *fs, DIR *dj, DIRINFO *dinfo, FS_NAME *fs_n);
void f_opendir(FATFS *fs, DIR *dj) ;
FRESULT f_mount(FATFS *fs, u32 bootsect, char drive_sel) ;
FRESULT f_mount_api(FATFS *fs, u32 bootsect, char drive_sel);
FRESULT f_open(FATFS *fs, FIL *fp, char *path, char *lfn_buf, u8 mode);
u8 f_tell_status(FIL *fp);
u16 f_write(FIL *fp, u8 *buff, u16 btw);
FRESULT f_mkdir(FATFS *fs, char *path, u8 mode);
FRESULT f_sync_fs(FIL *fp);
FRESULT f_sync_file(FIL *fp);
FRESULT f_unlink(FIL *fp);
bool path_mem_cmp(char *src, char *dst, u8 len);
bool my_mem_cmp(char *src, char *dst, u8 len);
u8 get_powerof2(u8 n);
u32 clust2sect(FATFS *fs, u32 clust);
void st_clust(u8 *dir, u32 cl);
FRESULT sync_window(SWIN_BUF *win_buf);
FRESULT move_window(u32 sector, SWIN_BUF *win_buf);
u32 get_fat(FATFS *fs, u32 clust, SWIN_BUF *win_buf);
u32 get_cluster(FIL *fp, u32 clust, SWIN_BUF *win_buf);
FRESULT put_fat(FATFS *fs, u32 clst, u32 val, SWIN_BUF *win_buf);
u32 ld_clust(FATFS *fs, u8 *dir);
bool dir_sdi(FATFS *fs, DIR *dj, u16 idx);
FRESULT dir_alloc(FATFS *fs, DIR *dj, u8 nent);
FRESULT dir_register(FATFS *fs, DIR *dj, char *fn);
FRESULT sync_fs(FATFS *fs);
u8 create_name(char *sfn, const char *path);
FRESULT dir_find(FATFS *fs, DIR *dj, DIRINFO *dir_info, char *sfn, FS_NAME *lfn);
FRESULT follow_path(FATFS *fs, DIR *dj, char *sfn, DIRINFO *dinfo, char *path, FS_NAME *lfn);
FRESULT remove_chain(FATFS *fs, u32 clst);
u32 create_chain(FATFS *fs, u32 clst, SWIN_BUF *win);
FRESULT check_fs(FATFS *fs, u32 sec);
void get_dir_info(const u8 *dir, DIRINFO *dinfo);
bool dir_next(FATFS *fs, DIR *dj, bool stretch);
u16 ld_word_func(u8 *p);
u32 ld_dword_func(u8 *p);
void st_word_func(u8 *ptr, u16 val);
void st_dword_func(u8 *ptr, u32 val);
bool decode_lfn(char *p, char *sou_p, u16 max_copy);
#endif /* _FATFS */
@@ -0,0 +1,53 @@
#ifndef CIRCULAR_BUF_INTERFACE_H
#define CIRCULAR_BUF_INTERFACE_H
#include "typedef.h"
typedef struct _cbuffer {
u8 *begin;
u8 *end;
u8 *read_ptr;
u8 *write_ptr;
u8 *tmp_ptr ;
u32 tmp_len;
u32 data_len;
u32 total_len;
} cbuffer_t;
extern void cbuf_init(cbuffer_t *cbuffer, void *buf, u32 size);
extern u32 cbuf_read(cbuffer_t *cbuffer, void *buf, u32 len);
extern u32 cbuf_write(cbuffer_t *cbuffer, void *buf, u32 len);
extern u32 cbuf_is_write_able(cbuffer_t *cbuffer, u32 len);
extern void *cbuf_write_alloc(cbuffer_t *cbuffer, u32 *len);
extern void cbuf_write_updata(cbuffer_t *cbuffer, u32 len);
void *cbuf_read_alloc(cbuffer_t *cbuffer, u32 *len);
void cbuf_read_updata(cbuffer_t *cbuffer, u32 len);
void cbuf_clear(cbuffer_t *cbuffer);
u32 cbuf_rewrite(cbuffer_t *cbuffer, void *begin, void *buf, u32 len);
void cbuf_discard_prewrite(cbuffer_t *cbuffer);
void cbuf_updata_prewrite(cbuffer_t *cbuffer);
u32 cbuf_prewrite(cbuffer_t *cbuffer, void *buf, u32 len);
u32 cbuf_get_data_size(cbuffer_t *cbuffer);
void cbuf_read_alloc_len_updata(cbuffer_t *cbuffer, u32 len);
u32 cbuf_read_alloc_len(cbuffer_t *cbuffer, void *buf, u32 len);
#define cbuf_get_writeptr(a) (a)->write_ptr
#define cbuf_get_readptr(a ) (a)->read_ptr
#endif
@@ -0,0 +1,42 @@
#ifndef __COMMON_H__
#define __COMMON_H__
#include "asm/cpu.h"
#include "csfr.h"
#include "typedef.h"
#include "device_errno.h"
#include "wdt.h"
//#include "printf.h"
#define SFR(sfr, start, len, dat) (sfr = (sfr & ~((~(0xffffffff << (len))) << (start))) | (((dat) & (~(0xffffffff << (len)))) << (start)))
#ifndef MIN
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#endif
#ifndef MAX
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#endif
#ifdef __DEBUG
#define APP_DEBUG 1
#else
#define APP_DEBUG 0
#endif
// #if APP_DEBUG
// #define log_info printf
// #define log_error(...)
// #define log_info_hexdump printf_buf
// #else
// #define log_info(...)
// #define log_info_hexdump(a,b)
// #endif
// void wdt_clear();
#endif
@@ -0,0 +1,109 @@
#ifndef _DEBUG_H_
#define _DEBUG_H_
#include "printf.h"
#include "asm/cpu.h"
#include "generic/typedef.h"
#define RedBold "\033[31;1m" // 红色加粗
#define RedBoldBlink "\033[31;1;5m" // 红色加粗、闪烁
#define GreenBold "\033[32;1m" // 红色加粗
#define GreenBoldBlink "\033[32;1;5m" // 红色加粗、闪烁
#define YellowBold "\033[33;1m" // 红色加粗
#define YellowBoldBlink "\033[33;1;5m" // 红色加粗、闪烁
#define BlueBold "\033[34;1m" // 蓝色加粗
#define BlueBoldBlink "\033[34;1;5m" // 蓝色加粗、闪烁
#define PurpleBold "\033[35;1m" // 紫色加粗
#define PurpleBoldBlink "\033[35;1;5m" // 紫色加粗、闪烁
#define DGreenBold "\033[36;1m" // 红色加粗
#define DGreenBoldBlink "\033[36;1;5m" // 红色加粗、闪烁
#define WhiteBold "\033[37;1m" // 红色加粗
#define WhiteBoldBlink "\033[37;1;5m" // 红色加粗、闪烁
#define Reset "\033[0;25m" // 颜色复位
#define LOG_VERBOSE v
#define LOG_INFO i
#define LOG_DEBUG d
#define LOG_WARN w
#define LOG_ERROR e
#define LOG_CHAR c
#define _STR(x) #x
#define STR(x) "["_STR(x)"]"
#define _LOG_TAG_CONST_DECLARE(level, name) extern const char log_tag_const_##level##_##name
#define LOG_TAG_CONST_DECLARE(level, name) _LOG_TAG_CONST_DECLARE(level, name)
#define ___LOG_IS_ENABLE(level, name) (log_tag_const_##level##_##name)
#define __LOG_IS_ENABLE(level, name) ___LOG_IS_ENABLE(level, name)
#define _LOG_IS_ENABLE(level) __LOG_IS_ENABLE(level, LOG_TAG_CONST)
#ifdef LOG_TAG_CONST
LOG_TAG_CONST_DECLARE(LOG_VERBOSE, LOG_TAG_CONST);
LOG_TAG_CONST_DECLARE(LOG_INFO, LOG_TAG_CONST);
LOG_TAG_CONST_DECLARE(LOG_DEBUG, LOG_TAG_CONST);
LOG_TAG_CONST_DECLARE(LOG_WARN, LOG_TAG_CONST);
LOG_TAG_CONST_DECLARE(LOG_ERROR, LOG_TAG_CONST);
LOG_TAG_CONST_DECLARE(LOG_CHAR, LOG_TAG_CONST);
#define _LOG_TAG LOG_TAG
#define LOG_IS_ENABLE(level) _LOG_IS_ENABLE(level)
#else
#define _LOG_TAG "[NULL]"
#define LOG_IS_ENABLE(x) 0
#endif
#ifdef __DEBUG
#define log_print printf
#define log_info(format, ...) \
if (LOG_IS_ENABLE(LOG_INFO)) \
log_print("[Info] " _LOG_TAG format "\r\n", ## __VA_ARGS__)
#define log_info_hexdump(x, y) \
if (LOG_IS_ENABLE(LOG_INFO)) \
printf_buf(x, y)
#define log_debug(format, ...) \
if (LOG_IS_ENABLE(LOG_DEBUG)) \
log_print("[Debug] " _LOG_TAG format "\r\n", ## __VA_ARGS__)
#define log_debug_hexdump(x, y) \
if (LOG_IS_ENABLE(LOG_DEBUG)) \
printf_buf(x, y)
#define log_error(format, ...) \
if (LOG_IS_ENABLE(LOG_ERROR)) \
log_print("<Error> " _LOG_TAG format "\r\n", ## __VA_ARGS__)
#define log_error_hexdump(x, y) \
if (LOG_IS_ENABLE(LOG_ERROR)) \
printf_buf(x, y)
#define log_char(x) \
if (LOG_IS_ENABLE(LOG_CHAR)) \
putchar(x)
#else
#define log_info(format, ...)
#define log_info_hexdump(x, y)
#define log_debug(format, ...)
#define log_debug_hexdump(x, y)
#define log_error(format, ...)
#define log_error_hexdump(x, y)
#define log_char(x)
#endif
int printf_lite(const char *format, ...);
void log_putbyte(char c);
void put_buf_lite(void *_buf, u32 len);
#endif//__DEBUG_H_
@@ -0,0 +1,41 @@
#ifndef _ASM_GENERIC_ERRNO_BASE_H
#define _ASM_GENERIC_ERRNO_BASE_H
#define EPERM 1 /* Operation not permitted */
#define ENOENT 2 /* No such file or directory */
#define ESRCH 3 /* No such process */
#define EINTR 4 /* Interrupted system call */
#define EIO 5 /* I/O error */
#define ENXIO 6 /* No such device or address */
#define E2BIG 7 /* Argument list too long */
#define ENOEXEC 8 /* Exec format error */
#define EBADF 9 /* Bad file number */
#define ECHILD 10 /* No child processes */
#define EAGAIN 11 /* Try again */
#define ENOMEM 12 /* Out of memory */
#define EACCES 13 /* Permission denied */
#define EFAULT 14 /* Bad address */
#define ENOTBLK 15 /* Block device required */
#define EBUSY 16 /* Device or resource busy */
#define EEXIST 17 /* File exists */
#define EXDEV 18 /* Cross-device link */
#define ENODEV 19 /* No such device */
#define ENOTDIR 20 /* Not a directory */
#define EISDIR 21 /* Is a directory */
#define EINVAL 22 /* Invalid argument */
#define ENFILE 23 /* File table overflow */
#define EMFILE 24 /* Too many open files */
#define ENOTTY 25 /* Not a typewriter */
#define ETXTBSY 26 /* Text file busy */
#define EFBIG 27 /* File too large */
#define ENOSPC 28 /* No space left on device */
#define ESPIPE 29 /* Illegal seek */
#define EROFS 30 /* Read-only file system */
#define EMLINK 31 /* Too many links */
#define EPIPE 32 /* Broken pipe */
#define EDOM 33 /* Math argument out of domain of func */
#define ERANGE 34 /* Math result not representable */
#define ETIMEDOUT 35 /* Connection timed out */
#endif
@@ -0,0 +1,35 @@
#ifndef JIFFIES_H
#define JIFFIES_H
#define HZ 100L
#define MSEC_PER_SEC 1000L
#define USEC_PER_MSEC 1000L
#define NSEC_PER_USEC 1000L
#define NSEC_PER_MSEC 1000000L
#define USEC_PER_SEC 1000000L
#define NSEC_PER_SEC 1000000000L
#define FSEC_PER_SEC 1000000000000000LL
#ifndef __ASSEMBLY__
extern volatile unsigned long jiffies;
extern unsigned long jiffies_msec();
extern unsigned long jiffies_half_msec();
#endif
#define maskrom_get_jiffies() jiffies
#define maskrom_set_jiffies(n) jiffies = n
#define JIFFIES_CIRCLE 0x7FFFFFF
#define msecs_to_jiffies_10(msec) ((msec)/10)
#define jiffies_to_msecs(jiff) ((jiff)*10)
#define msecs_to_jiffies(msec) ((msec)/10)
#define time_after(a,b) ((long)(b) - (long)(a) <= 0)
#define time_before(a,b) time_after(b, a)
int jiffies_msec2offset(unsigned long begin_msec, unsigned long end_msec);
#endif
@@ -0,0 +1,225 @@
#ifndef LBUF_H
#define LBUF_H
#include "typedef.h"
#include "list.h"
#include "spinlock.h"
#define LBUF_DEBUG 0
struct lbuff_head {
#if LBUF_DEBUG
int magic_a; /*!< 测试验证变量*/
#endif
struct list_head head; /*!< 指向hentry链表*/
struct list_head free; /*!< 指向hfree链表*/
spinlock_t lock; /*!< 混合自旋锁,单核是为开关临界区,多核是自旋锁.*/
u8 align; /*!< 数据包字节对齐*/
u16 priv_len; /*!< 数据包结构体的最小长度*/
u32 total_size; /*!< 总大小*/
u32 last_addr; /*!< 指向free链表中找到的足够长度的hfree结构体地址*/
void *priv;
#if LBUF_DEBUG
int magic_b; /*!< 测试验证变量*/
#endif
};
struct lbuff_state {
u32 avaliable; /*!< 剩余空间的字节长度*/
u32 fragment; /*!< lbuf内存碎片块数量*/
u32 max_continue_len; /*!< 最大的剩余内存块的字节长度*/
int num; /*!< 剩余内存块数量*/
};
/* --------------------------------------------------------------------------*/
/**
* @brief 链表buf初始化
*
* @param [in] buf 需要lbuf进行管理的内存
* @param [in] len 内存长度
* @param [in] align 输入对管理的内存进行对齐的参数,避免后续使用因地址不对齐产生碎片
* @param [in] priv_head_len 要管理的一个数据包结构体的最小的长度
*
* @return lbuf操作句柄
*/
/* --------------------------------------------------------------------------*/
struct lbuff_head *lbuf_init(void *buf, u32 len, int align, int priv_head_len);
/* --------------------------------------------------------------------------*/
/**
* @brief 分配内存空间进行存储数据包
*
* @param [in] head lbuf操作句柄
* @param [in] len 需要存入的数据包的长度
*
* @return 成功则返回进行存储数据包的地址,调用时候需要用户把该块内存的类型初始化为数据包结构体的类型。失败则返回NULL。
*/
/* --------------------------------------------------------------------------*/
void *lbuf_alloc(struct lbuff_head *head, u32 len);
/* --------------------------------------------------------------------------*/
/**
* @brief 重新分配lbuf_alloc()返回用于存储数据包的lbuf空间
*
* @param [in] lbuf lbuf_alloc()返回用于存储数据包的地址
* @param [in] size 需重新分配的空间的字节长度.注:size的大小只能比lbuf_alloc()中的len小,即只能重新分配更小的lbuf空间,不能扩大空间.
*
* @return 重新分配后用于存储数据包的地址。失败则返回空指针。注:重新分配最好使用lbuf_real_size()获取lbuf空间的长度确认是否分配成功
*/
/* --------------------------------------------------------------------------*/
void *lbuf_realloc(void *lbuf, int size);
/* --------------------------------------------------------------------------*/
/**
* @brief 判断lbuf空间内的内容是否为空
*
* @param [in] head lbuf操作句柄
*
* @return 返回1则为空,0则不为空
*/
/* --------------------------------------------------------------------------*/
int lbuf_empty(struct lbuff_head *head);
/* --------------------------------------------------------------------------*/
/**
* @brief 清空lbuf空间内进行已经分配给数据包的空间
*
* @param [in] head lbuf操作句柄
*/
/* --------------------------------------------------------------------------*/
void lbuf_clear(struct lbuff_head *head);
/* --------------------------------------------------------------------------*/
/**
* @brief 把数据包写入分配好的lbuf区域
*
* @param [in] lbuf lbuf_alloc()返回用于存储数据包的地址
* @param [in] channel_map 选择映射到哪个通道,最多8个通道,使用位映射的方式进行通道对应.
*/
/* --------------------------------------------------------------------------*/
void lbuf_push(void *lbuf, u8 channel_map);
/* --------------------------------------------------------------------------*/
/**
* @brief 读取对应的通道映射的lbuf区域存储的内容
*
* @param [in] head lbuf操作句柄
* @param [in] channel 需要读取的通道值,一般使用BIT(n),n为需要读取的通道
*
* @return 成功则返回存储对应的通道映射的数据包的地址
*/
/* --------------------------------------------------------------------------*/
void *lbuf_pop(struct lbuff_head *head, u8 channel);
/* --------------------------------------------------------------------------*/
/**
* @brief 释放存储数据包的lbuf空间
*
* @param [in] lbuf lbuf_alloc()返回用于存储数据包的地址
*
* @return 0则释放失败,存在地址越界操作或者通道还没有被读完,ref-1,读完后才能完全释放。1则释放成功。
*/
/* --------------------------------------------------------------------------*/
int lbuf_free(void *lbuf);
/* --------------------------------------------------------------------------*/
/**
* @brief 用于调试,检查是否可以释放存储数据包的lbuf空间
*
* @param [in] lbuf lbuf_alloc()返回用于存储数据包的地址
* @param [in] rets 调用lbuf_free_check()函数的返回地址rets,取值可参考lbuf_free()
*/
/* --------------------------------------------------------------------------*/
void lbuf_free_check(void *lbuf, u32 rets);
/* --------------------------------------------------------------------------*/
/**
* @brief 返回可分配的用来存储数据包的最大lbuf内存空间
*
* @param [in] head lbuf操作句柄
*
* @return 可分配的最大lbuf内存空间的字节长度
*/
/* --------------------------------------------------------------------------*/
u32 lbuf_free_space(struct lbuff_head *head);
/* --------------------------------------------------------------------------*/
/**
* @brief 获取lbuf空间的状态
*
* @param [in] head lbuf操作句柄
* @param [out] state lbuff_state结构体
*/
/* --------------------------------------------------------------------------*/
void lbuf_state(struct lbuff_head *head, struct lbuff_state *state);
/* --------------------------------------------------------------------------*/
/**
* @brief lbuf信息打印
*
* @param [in] head lbuf操作句柄
*/
/* --------------------------------------------------------------------------*/
void lbuf_dump(struct lbuff_head *head);
/* --------------------------------------------------------------------------*/
/**
* @brief 获取已经存入lbuf空间的数据包的数量
*
* @param [in] head lbuf操作句柄
*
* @return lbuf存储的数据包的数量
*/
/* --------------------------------------------------------------------------*/
int lbuf_traversal(struct lbuff_head *head);
/* --------------------------------------------------------------------------*/
/**
* @brief 返回lbuf空间还可以被写入size大小数据包的数量
*
* @param [in] head lbuf操作句柄
* @param [in] size 欲检测写入数据包的大小
*
* @return 可以写入的数量
*/
/* --------------------------------------------------------------------------*/
int lbuf_avaliable(struct lbuff_head *head, int size);
/* --------------------------------------------------------------------------*/
/**
* @brief 返回给数据包分配的内存空间的大小
*
* @param [in] lbuf lbuf_alloc()返回用于存储数据包的地址
*
* @return 实际占用空间的字节长度
*/
/* --------------------------------------------------------------------------*/
int lbuf_real_size(void *lbuf);
/* --------------------------------------------------------------------------*/
/**
* @brief 计算lbuf空间剩下多少剩余空间
*
* @param [in] head lbuf操作句柄
*
* @return 剩余空间的字节长度
*/
/* --------------------------------------------------------------------------*/
int lbuf_remain_space(struct lbuff_head *head);
/* --------------------------------------------------------------------------*/
/**
* @brief 需要被重复释放的次数+1
*
* @param [in] lbuf lbuf_alloc()返回用于存储数据包的地址
*/
/* --------------------------------------------------------------------------*/
void lbuf_inc_ref(void *lbuf);
#endif
@@ -0,0 +1,284 @@
#ifndef LIST_H
#define LIST_H
/**
* container_of - cast a member of a structure out to the containing structure
* @ptr: the pointer to the member.
* @type: the type of the container struct this is embedded in.
* @member: the name of the member within the struct.
*
*/
#ifdef offsetof
#undef offsetof
#endif
#ifdef container_of
#undef container_of
#endif
#define list_offsetof(type, memb) \
((unsigned long)(&((type *)0)->memb))
#define container_of(ptr, type, memb) \
((type *)((char *)ptr - list_offsetof(type, memb)))
struct list_head {
struct list_head *next, *prev;
};
#define LIST_HEAD_INIT(name) { &(name), &(name) }
#define LIST_HEAD(name) \
struct list_head name = LIST_HEAD_INIT(name)
/**
* list_entry - get the struct for this entry
* @ptr: the &struct list_head pointer.
* @type: the type of the struct this is embedded in.
* @member: the name of the list_struct within the struct.
*/
#define list_entry(ptr, type, member) \
container_of(ptr, type, member)
/**
* list_first_entry - get the first element from a list
* @ptr: the list head to take the element from.
* @type: the type of the struct this is embedded in.
* @member: the name of the list_struct within the struct.
*
* Note, that list is expected to be not empty.
*/
/* REF_LIST: spi.c */
#define list_first_entry(ptr, type, member) \
list_entry((ptr)->next, type, member)
/**
* list_for_each - iterate over a list
* @pos: the &struct list_head to use as a loop cursor.
* @head: the head for your list.
*/
#define list_for_each(pos, head) \
for (pos = (head)->next; pos != (head); \
pos = pos->next)
/**
* list_for_each_safe - iterate over a list safe against removal of list entry
* @pos: the &struct list_head to use as a loop counter.
* @n: another &struct list_head to use as temporary storage
* @head: the head for your list.
*/
#define list_for_each_safe(pos, n, head) \
for (pos = (head)->next, n = pos->next; pos != (head); \
pos = n, n = pos->next)
/**
* list_for_each_entry - iterate over list of given type
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the list_struct within the struct.
*/
#define list_for_each_entry(pos, head, member) \
for (pos = list_entry((head)->next, typeof(*pos), member); \
&pos->member != (head); \
pos = list_entry(pos->member.next, typeof(*pos), member))
/**
* list_for_each_entry_reverse - iterate backwards over list of given type.
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the list_struct within the struct.
*/
#define list_for_each_entry_reverse(pos, head, member) \
for (pos = list_entry((head)->prev, typeof(*pos), member); \
&pos->member != (head); \
pos = list_entry(pos->member.prev, typeof(*pos), member))
/**
* list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
* @pos: the type * to use as a loop cursor.
* @n: another type * to use as temporary storage
* @head: the head for your list.
* @member: the name of the list_struct within the struct.
*/
#define list_for_each_entry_safe(pos, n, head, member) \
for (pos = list_entry((head)->next, typeof(*pos), member), \
n = list_entry(pos->member.next, typeof(*pos), member); \
&pos->member != (head); \
pos = n, n = list_entry(n->member.next, typeof(*n), member))
/**
* list_empty - tests whether a list is empty
* @head: the list to test.
*/
/* REF_LIST: spi.c */
static inline int list_empty(const struct list_head *head)
{
return head->next == head;
}
/*
* Insert a new entry between two known consecutive entries.
*
* This is only for internal list manipulation where we know
* the prev/next entries already!
*/
static inline void __list_add(struct list_head *_new,
struct list_head *prev,
struct list_head *next)
{
next->prev = _new;
_new->next = next;
_new->prev = prev;
prev->next = _new;
}
/**
* list_add_tail - add a new entry
* @new: new entry to be added
* @head: list head to add it before
*
* Insert a new entry before the specified head.
* This is useful for implementing queues.
*/
/* REF_LIST: spi.c */
static inline void list_add_tail(struct list_head *_new, struct list_head *head)
{
__list_add(_new, head->prev, head);
}
static inline void __list_del(struct list_head *prev, struct list_head *next)
{
if (prev == 0 || next == 0) {
return;
}
//ASSERT(prev!=NULL || next!=NULL)
next->prev = prev;
prev->next = next;
}
static inline void __list_del_entry(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
}
/* REF_LIST: spi.c */
static inline void list_del(struct list_head *entry) //修改过的list_del,这里与list_del_init一样
{
__list_del(entry->prev, entry->next);
entry->next = entry;
entry->prev = entry;
}
/*
* Simple doubly linked list implementation.
*
* Some of the internal functions ("__xxx") are useful when
* manipulating whole lists rather than single entries, as
* sometimes we already know the next/prev entries and we can
* generate better code by using them directly rather than
* using the generic single-entry routines.
*/
static inline void INIT_LIST_HEAD(struct list_head *list)
{
list->next = list;
list->prev = list;
}
static inline void list_del_init(struct list_head *entry)
{
__list_del_entry(entry);
INIT_LIST_HEAD(entry);
}
/**
* list_move_tail - delete from one list and add as another's tail
* @list: the entry to move
* @head: the head that will follow our entry
*/
static inline void list_move_tail(struct list_head *list,
struct list_head *head)
{
__list_del(list->prev, list->next);
list_add_tail(list, head);
}
/**
* list_add - add a new entry
* @new: new entry to be added
* @head: list head to add it after
*
* Insert a new entry after the specified head.
* This is good for implementing stacks.
*/
static inline void list_add(struct list_head *new, struct list_head *head)
{
__list_add(new, head, head->next);
}
static inline int list_is_head(struct list_head *head, struct list_head *member)
{
return head->next == member;
}
#if 0
static inline void __list_splice(const struct list_head *list,
struct list_head *prev,
struct list_head *next)
{
struct list_head *first = list->next;
struct list_head *last = list->prev;
first->prev = prev;
prev->next = first;
last->next = next;
next->prev = last;
}
/**
* list_splice_tail_init - join two lists and reinitialise the emptied list
* @list: the new list to add.
* @head: the place to add it in the first list.
*
* Each of the lists is a queue.
* The list at @list is reinitialised
*/
static inline void list_splice_tail_init(struct list_head *list,
struct list_head *head)
{
if (!list_empty(list)) {
__list_splice(list, head->prev, head);
INIT_LIST_HEAD(list);
}
}
/**
* list_is_singular - tests whether a list has just one entry.
* @head: the list to test.
*/
static inline int list_is_singular(const struct list_head *head)
{
return !list_empty(head) && (head->next == head->prev);
}
/**
* list_splice_tail - join two lists, each list being a queue
* @list: the new list to add.
* @head: the place to add it in the first list.
*/
static inline void list_splice_tail(struct list_head *list,
struct list_head *head)
{
if (!list_empty(list)) {
__list_splice(list, head->prev, head);
}
}
#endif
#endif
@@ -0,0 +1,125 @@
#ifndef __LOG_H
#define __LOG_H
#include "printf.h"
#define __LOG_VERB 0
#define __LOG_DEBUG 1
#define __LOG_INFO 2
#define __LOG_WARN 3
#define __LOG_ERROR 4
#define __LOG_CHAR 5
#define __LOG_ENABLE
#ifndef __LOG_LEVEL
#define __LOG_LEVEL 0
#endif
#ifdef __DEBUG
#else
#define CONFIG_RELEASE_ENABLE
#endif
#ifdef CONFIG_RELEASE_ENABLE
#undef __LOG_LEVEL
#define __LOG_LEVEL 0xff
#endif
#if __LOG_LEVEL > __LOG_VERB
#define log_v(...) do {} while (0)
#else
#define log_v(...) printf(__VA_ARGS__)
#endif
#if __LOG_LEVEL > __LOG_DEBUG
#define log_d(...) do {} while (0)
#else
#define log_d(...) printf(__VA_ARGS__)
#endif
#if __LOG_LEVEL > __LOG_INFO
#define log_i(...) do {} while (0)
#else
#define log_i(...) printf(__VA_ARGS__)
#endif
#if __LOG_LEVEL > __LOG_WARN
#define log_w(...) do {} while (0)
#else
#define log_w(...) printf(__VA_ARGS__)
#endif
#if __LOG_LEVEL > __LOG_ERROR
#define log_e(...) do {} while (0)
#else
#define log_e(...) printf(__VA_ARGS__)
#endif
#if __LOG_LEVEL > __LOG_CHAR
#define log_c(x) do {} while (0)
#elif defined __LOG_ENABLE
#define log_c(x) putchar(x)
#else
#define log_c(x)
#endif
#define r_printf(x, ...) log_i("\e[31m\e[1m" x "\e[0m", ## __VA_ARGS__)
#define g_printf(x, ...) log_i("\e[32m\e[1m" x "\e[0m", ## __VA_ARGS__)
#define y_printf(x, ...) log_i("\e[33m\e[1m" x "\e[0m", ## __VA_ARGS__)
#define r_f_printf(x, ...) log_i("\e[31m\e[5m\e[1m" x "\e[0m", ## __VA_ARGS__)
#define g_f_printf(x, ...) log_i("\e[32m\e[5m\e[1m" x "\e[0m", ## __VA_ARGS__)
#define y_f_printf(x, ...) log_i("\e[33m\e[5m\e[1m" x "\e[0m", ## __VA_ARGS__)
#ifndef __LOG_ENABLE
#define log_dump(a, b) do {} while(0)
#define log_putchar() do {} while(0)
#define log_early_init(a) do {} while(0)
#define log_level(a) do {} while(0)
#else
struct logbuf {
u16 len;
u16 buf_len;
char buf[0];
};
int log_output_lock();
void log_output_unlock();
void log_print_time();
void log_early_init(int buf_size);
void log_level(int level);
// void log_print(int level, const char *tag, const char *format, ...);
void log_dump(const u8 *buf, int len);
struct logbuf *log_output_start(int len);
void log_output_end(struct logbuf *);
void log_putchar(struct logbuf *lb, char c);
void log_put_u8hex(struct logbuf *lb, unsigned char dat);
void log_putbyte(char);
void log_set_time_offset(int offset);
int log_get_time_offset();
#endif
void log_flush();
#endif
@@ -0,0 +1,63 @@
#ifndef _MSG_H_
#define _MSG_H_
#include "typedef.h"
#if (defined(CONFIG_CPU_BD47) || defined(CONFIG_CPU_BR29)) && defined(BLE_APP_LOW_RAM_USED) // bd47 内存紧缺
#define MAX_POOL 100
#else
#define MAX_POOL 128
#endif
enum {
MSG_NO_ERROR = 0,
MSG_EVENT_EXIST = -1,
MSG_NOT_EVENT = -2,
MSG_EVENT_PARAM_ERROR = -3,
MSG_BUF_NOT_ENOUGH = -4,
};
#define Q_USER 0x400000
enum {
LMP_EVENT = Q_USER + 1,
LMP_HCI_CMD,
LMP_HCI_CMD_TO_CONN,
HCI_COMMON_CMD,
LL_EVENT,
HCI_CMD_TO_LL,
HCI_CMD_TO_LL_CONN,
TWS_LMP_EVENT,
BTSTACK_HCI_EVENT,
BTSTACK_HCI_ACL,
//MSG_BT_UPDATA_START = 0x80,
//MSG_BT_UPDATE_LOADER_DOWNLOAD_START,
MSG_JL_SPEECH_START_PREPARE = 0xB00,
MSG_JL_SPEECH_START,
MSG_JL_SPEECH_STOP,
MSG_JL_GET_DEV_INFO,
MSG_JL_GET_DEV_UPDATE_FILE_INFO_OFFSET,
MSG_JL_INQUIRE_DEVEICE_IF_CAN_UPDATE,
MSG_JL_ENTER_UPDATE_MODE,
MSG_JL_EXIT_UPDATE_MODE,
MSG_JL_UPDATE_DISCONNECT,
MSG_JL_UPDATE_REVICE_REBOOT,
MSG_JL_SWITCH_CH_SPP,
MSG_JL_SUCCESS_STATUS_HANDLE,
MSG_JL_ERR_STATUS_HANDLE,
MSG_JL_GET_PRIVATE_INFO,
MSG_UBOOT_SOFT_POWEROFF,
};
void task_message_init(void);
int task_post_msg(char *name, int argc, ...);
int task_post_msg_base(const char *name, int argc, int cmd, int *argv);
int task_get_msg(u16 timeout, int len, int *msg);
#endif
@@ -0,0 +1,76 @@
#define SYS_TIMER_H
#include "generic/typedef.h"
#include "list.h"
typedef int sys_timer;
struct sys_timer {
struct list_head entry;
void (*func)(void *priv);
void *priv;
u32 jiffies;
u32 msec: 24;
u32 del: 1;
u32 timeout: 1;
u16 id;
u8 used;
};
void sys_timer_init();
bool __timer_find(struct sys_timer *timer);
u16 sys_timer_add(void *priv, void (*func)(void *priv), u32 msec);
void sys_timer_schedule(void);
void sys_timer_delay_schedule(void);
void sys_timer_set_user(struct sys_timer *timer, u32 user);
u32 sys_timer_get_user(struct sys_timer *timer);
void sys_timer_del_schedule(void);
void loop_timer_schedule(void);
void sys_timer_re_run(u16 id);
void sys_timer_del(u16 t);
int sys_timer_modify(u16 id, u32 msec);
u16 sys_timeout_add(void *priv, void (*func)(void *priv), u32 msec);
void sys_timeout_del(u16 t);
/*
* For Compatible
*/
#define sys_hi_timer_schedule()\
usr_timer_schedule()
#define sys_hi_timer_add(a, b, c)\
sys_timer_add(a, b, c)
#define sys_hi_timeout_add(a, b, c)\
sys_timeout_add(a, b, c)
// usr_timeout_add(a, b, c, 1)
#define sys_hi_timer_modify(a, b)\
sys_timer_modify(a, b)
#define sys_hi_timeout_modify(a, b)\
sys_timer_modify(a, b)
#define sys_hi_timer_del(a)\
sys_timer_del(a)
#define sys_hi_timeout_del(a)\
sys_timer_del(a)
#define sys_s_hi_timer_add(a, b, c)\
sys_timer_add(a, b, c, 0)
#define sys_s_hi_timerout_add(a, b, c)\
sys_timeout_add(a, b, c, 0)
#define sys_s_hi_timer_modify(a, b)\
sys_timer_modify(a, b)
#define sys_s_hi_timeout_modify(a, b)\
sys_timer_modify(a, b)
#define sys_s_hi_timer_del(a)\
sys_timer_del(a)
#define sys_s_hi_timeout_del(a)\
sys_timeout_del(a)
@@ -0,0 +1,219 @@
/*************************************************************
File: typedef.h
Author:Juntham
Discriptor:
数据类型重定义
Version:
Date
*************************************************************/
#ifndef _typedef_h_
#define _typedef_h_
#include "asm/cpu.h"
typedef signed char int8_t;
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
typedef unsigned long long int uint64_t;
typedef unsigned char u8, bool, BOOL;
typedef char s8;
typedef signed short s16;
typedef unsigned short u16;
typedef unsigned int u32;
typedef signed int s32;
typedef unsigned long long u64;
typedef u32 FOURCC;
typedef long long s64;
typedef unsigned long long u64;
#ifdef READ_BT_8
#undef READ_BT_8
#endif
#ifdef READ_BT_16
#undef READ_BT_16
#endif
#ifdef READ_BT_24
#undef READ_BT_24
#endif
#ifdef READ_BT_32
#undef READ_BT_32
#endif
#define READ_BT_8( buffer, pos) ( ((u8) buffer[pos]))
#define READ_BT_16( buffer, pos) ( ((u16) buffer[pos]) | (((u16)buffer[pos+1]) << 8))
#define READ_BT_24( buffer, pos) ( ((u32) buffer[pos]) | (((u32)buffer[pos+1]) << 8) | (((u32)buffer[pos+2]) << 16))
#define READ_BT_32( buffer, pos) ( ((u32) buffer[pos]) | (((u32)buffer[pos+1]) << 8) | (((u32)buffer[pos+2]) << 16) | (((u32) buffer[pos+3])) << 24)
#if defined(__GNUC__)
///<locate code to x segment ever exist
#define SEC_USED(x) __attribute__((section(#x),used))
///<locate code to x segment optimized by dependency
#define SEC(x) __attribute__((section(#x)))
#define sec(x) __attribute__((section(#x),used))
///<locate data to x segment
#define AT(x) __attribute__((section(#x)))
#define SET(x) __attribute__((x))
#define ALIGNED(x) __attribute__((aligned(x)))
#define _GNU_PACKED_ __attribute__((packed))
#define _NOINLINE_ __attribute__((noinline))
#define _INLINE_ __attribute__((always_inline))
#define _WEAK_ __attribute__((weak))
#define _WEAKREF_ __attribute__((weakref))
#define _NORETURN_ __attribute__((noreturn))
#define _NAKED_ __attribute__((naked))
#define SET_INTERRUPT __attribute__((interrupt("")))
#define ___interrupt __attribute__((interrupt("")))
#else
#define SEC_USED(x)
#define SEC(x)
#define AT(x)
#define SET(x)
#define ALIGNED(x)
#define _GNU_PACKED_
#define _NOINLINE_
#define _INLINE_
#define _WEAK_
#define _WEAKREF_
#define _NORETURN_
#define _NAKED_
#endif
#if CPU_ENDIAN == LITTLE_ENDIAN
//#define ntohl(x) (u32)((x>>24)|((x>>8)&0xff00)|(x<<24)|((x&0xff00)<<8))
//#define ntoh(x) (u16)((x>>8&0x00ff)|x<<8&0xff00)
//#define ntohl(x) (u32)((((u32)(x))>>24) | ((((u32)(x))>>8)&0xff00) | (((u32)(x))<<24) | ((((u32)(x))&0xff00)<<8))
//#define ntoh(x) (u16)((((u32)(x))>>8&0x00ff) | (((u32)(x))<<8&0xff00))
//#define NTOH(x) (x) = ntoh(x)
//#define NTOHL(x) (x) = ntohl(x)
#define LD_WORD(ptr) (u16)(*(u16*)(u8*)(ptr))
#define LD_DWORD(ptr) (u32)(*(u32*)(u8*)(ptr))
#define ST_WORD(ptr,val) *(u16*)(u8*)(ptr)=(u16)(val)
#define ST_DWORD(ptr,val) *(u32*)(u8*)(ptr)=(u32)(val)
#else
#define ntohl(x) (x)
#define ntoh(x) (x)
#define NTOH(x) (x) = ntoh(x)
#define NTOHL(x) (x) = ntohl(x)
#endif
#if defined(__UPDATE_NEED_SFC) && defined(__APP_IS_OTA) && !defined(__UPDATE_RUN_RAM)
#define SFC_MODE_EN
#endif
#if defined(SFC_MODE_EN)
#define AT_SPI_CODE AT(.vm_sfc.text.cache)
#else
#define AT_SPI_CODE
#endif
#undef FALSE
#define FALSE 0
#undef TRUE
#define TRUE 1
#define false 0
#define true 1
#ifndef NULL
#define NULL (void *)0
#endif
#define BIT(n) (1UL << (n))
#define BitSET(REG,POS) ((REG) |= (1L << (POS)))
#define BitCLR(REG,POS) ((REG) &= (~(1L<< (POS))))
#define BitXOR(REG,POS) ((REG) ^= (~(1L << (POS))))
#define BitCHK_1(REG,POS) (((REG) & (1L << (POS))) == (1L << (POS)))
#define BitCHK_0(REG,POS) (((REG) & (1L << (POS))) == 0x00)
#define testBit(REG,POS) ((REG) & (1L << (POS)))
#define clrBit(x,y) (x) &= ~(1L << (y))
#define setBit(x,y) (x) |= (1L << (y))
#define readb(addr) *((volatile unsigned char*)(addr))
#define readw(addr) *((volatile unsigned short *)(addr))
#define readl(addr) *((volatile unsigned long*)(addr))
#define writeb(addr, val) *((volatile unsigned char*)(addr)) = (u8)(val)
#define writew(addr, val) *((volatile unsigned short *)(addr)) = (u16)(val)
#define writel(addr, val) *((volatile unsigned long*)(addr)) = (u32)(val)
#define ALIGN_4BYTE(size) ((size+3)&0xfffffffc)
#if CPU_ENDIAN == BIG_ENDIAN
#define __cpu_u16(lo, hi) ((lo)|((hi)<<8))
#elif CPU_ENDIAN == LITTLE_ENDIAN
#define __cpu_u16(lo, hi) ((hi)|((lo)<<8))
#else
#error "undefine cpu eadin"
#endif
#ifndef MIN
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#endif
#ifndef MAX
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#endif
#define ARRAY_SIZE(array) (sizeof(array)/sizeof(array[0]))
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
#define SFR(sfr, start, len, dat) \
(sfr = (sfr & ~((~(0xffffffff << (len))) << (start))) | \
(((dat) & (~(0xffffffff << (len)))) << (start)))
#include "errno-base.h"
#include "string.h"
#include "strings.h"
#include "malloc.h"
#include "jiffies.h"
#ifdef offsetof
#undef offsetof
#endif
#ifdef container_of
#undef container_of
#endif
#define offsetof(type, memb) \
((unsigned long)(&((type *)0)->memb))
#define container_of(ptr, type, memb) \
((type *)((char *)(ptr) - offsetof(type, memb)))
void delay(unsigned int);
void delay_us(unsigned int);
// 只适用32位以内数据比较
#define LOOP_OVERTAKE(a, b, n) \
((((a) - (b)) & ((1ULL << (n)) - 1)) < ((1UL << ((n) - 1))))
#endif
@@ -0,0 +1,30 @@
#ifndef _MEM_HEAP_H_
#define _MEM_HEAP_H_
#ifdef __cplusplus
extern "C" {
#endif
extern void *malloc(unsigned long size);
extern void *zalloc(unsigned long size);
extern void *calloc(unsigned long count, unsigned long size);
extern void *realloc(void *rmem, unsigned long newsize);
extern void free(void *mem);
extern void *kmalloc(unsigned long size, int flags);
extern void *vmalloc(unsigned long size);
extern void vfree(void *addr);
extern void *kzalloc(unsigned int len, int a);
extern void kfree(void *p);
extern void malloc_stats(void);
extern void malloc_dump();
#ifdef __cplusplus
}
#endif
#endif /* _MEM_HEAP_H_ */
@@ -0,0 +1,87 @@
/***********************************Jieli tech************************************************
File : os_cpu.h
By : Juntham
date : 2014-07-03 09:06
********************************************************************************************/
#ifndef _OS_CPU_H
#define _OS_CPU_H
#include "asm/cpu.h"
#include "jiffies.h"
#ifndef __ASSEMBLY__
typedef unsigned short QS;
typedef unsigned int OS_STK; /* Each stack entry is 32-bit wide*/
typedef unsigned int OS_CPU_SR; /* Unsigned 32 bit quantity */
typedef unsigned int OS_CPU_DATA; /* Unsigned 32 bit quantity */
#endif
#define OS_CPU_EXT extern
#define OS_CPU_CORE CPU_CORE_NUM
#define OS_CPU_ID current_cpu_id()
#define OS_STK_GROWTH 1 /* Stack grows from HIGH to LOW memory*/
#define OS_CPU_MMU 0
#define OS_CPU_VIRTUAL_MEM 1 //临时定义:区别于OS_CPU_MMU
#define OS_TASK_CLR(a) CPU_TASK_CLR(a)
#define OS_TASK_SW(a) CPU_TASK_SW(a) /* 任务级任务切换函数*/
#define OS_INT_NESTING CPU_INT_NESTING
#define CPU_SR_ALLOC()
#define OS_SR_ALLOC()
#define OS_ENTER_CRITICAL() \
CPU_CRITICAL_ENTER(); \
#define OS_EXIT_CRITICAL() \
CPU_CRITICAL_EXIT()
#ifndef __ASSEMBLY__
/*#include "system/spinlock.h"
extern spinlock_t os_lock;
#define OS_ENTER_CRITICAL() \
spin_lock(&os_lock)
#define OS_EXIT_CRITICAL() \
spin_unlock(&os_lock)*/
void OSCtxSw(void);
extern void EnableOtherCpu(void) ;
#define os_ctx_sw OSCtxSw
void OSInitTick(u32 hz);
void InstallOSISR(void);
void os_task_dead(const char *task_name);
#endif
/*
*********************************************************************************************************
* DATA TYPES
* (Compiler Specific)
*********************************************************************************************************
*/
#define OS_CRITICAL_METHOD 3
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
//#define CPU_SR_ALLOC() OS_CPU_SR cpu_sr
#endif
#endif /*_OS_CPU_H */
@@ -0,0 +1,76 @@
#ifndef SYS_SPINLOCK_H
#define SYS_SPINLOCK_H
#include "typedef.h"
//#include "cpu.h"
//#include "irq.h"
struct __spinlock {
volatile u32 rwlock;
};
typedef struct __spinlock spinlock_t;
extern void local_irq_disable(void);
extern void local_irq_enable(void);
#define preempt_disable() \
local_irq_disable()
#define preempt_enable() \
local_irq_enable()
#if CPU_CORE_NUM > 1
#define spin_acquire(lock) \
do { \
arch_spin_lock(lock); \
}while(0)
#define spin_release(lock) \
do { \
arch_spin_unlock(lock); \
}while(0)
#else
#define spin_acquire(lock) \
do { \
}while(0)
#define spin_release(lock) \
do { \
}while(0)
#endif
#define DEFINE_SPINLOCK(x) \
spinlock_t x = { .rwlock = 0 }
static inline void spin_lock_init(spinlock_t *lock)
{
lock->rwlock = 0;
}
static inline void spin_lock(spinlock_t *lock)
{
preempt_disable();
spin_acquire(lock);
}
static inline void spin_unlock(spinlock_t *lock)
{
spin_release(lock);
preempt_enable();
}
#endif