⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bfspiboot.c

📁 在linux平台上通过spi引导bf533 dsp的应用程序代码
💻 C
字号:
/**********************************************************************/
/* FILE     : bfspiboot.c                                             */
/* FUNCTION : use spi interface send BF53x boot code to bf53x DSP     */
/* AUTOR    : zhang yi feng                                           */
/* COM      : VCOM                                                    */
/* DATE     : 20060517                                                */
/**********************************************************************/
/*
Ver0.01     : 20060605    add dsp busy pin test function
Ver0.02     : 20060612    通过SCP口发送BF533引导程序工作正常
*/

#include<unistd.h>
#include <stdio.h> 
#include <sys/types.h> 
#include <sys/stat.h> 
#include <fcntl.h> 
#include <sys/mman.h>
#include <time.h>

#include "bfspiboot.h"
#include "scp_inf.h"

#define  DEBUG

#ifdef DEBUG
    #define DBG(fmt, args...) printf(fmt,## args)
#else
    #define DBG(fmt, args...)
#endif

SCP_RW_STRUCT                 scp_rw;
u64                           cdm;
GPIO_REGISTER  *gpio_base;
int fd;
struct tm *tnow;
time_t now;
u32 ErroRebootCount;
#define SCP_WORK_MODE_CODE 0x55
#define SCP_WORK_MODE_DATA 0xAA


int main(int argc, char **argv){

    u32  dev; 
    u8*  buf;
    u8*  rxbuf;
    u32  i,flag;
    u32  Length,Count,ptr;
    u8   ch;
    if(argc <= 1) {
        printf("\r\nplease input boot file name.\r\n");
        exit(0);
    }

    dev = open(argv[1],O_RDONLY);
    if(dev == -1){ 
         printf("Cann't open file \n"); 
         exit(0); 
    }
    
    lseek(dev,0,SEEK_SET);
    Length = lseek(dev,0,SEEK_END);
    lseek(dev,0,SEEK_SET);
    buf = (u8*)malloc(Length);
    rxbuf = (u8*)malloc(256);
    memset(buf,0,Length);
    memset(rxbuf,0,256);
    if(buf == NULL){
        DBG("can't malloc memory.\r\n");
        exit(0);
    }
    printf("\r\nAll file  is %dbyte,%dkb\r\n",Length,Length/1024);
    Length = read(dev,buf,Length); 
    close(dev);

    //-----------------------------------------------------------------------
    //rw the scp use buf data
    ErroRebootCount = 0;
    repeat:
    dev = open(scp_dev_name,O_RDWR);
    if(dev == -1){
         printf("Cann't get scp handler.\n");
         exit(0);
    }else{
    }
    
    cdm = 0;
    ioctl(dev,IOCTL_SCP_SET_CLK_INV,&cdm);
    cdm = 0;
    ioctl(dev,IOCTL_SCP_SET_REV_DATA,&cdm);
    cdm = 0;
    if(ioctl(dev,IOCTL_SCP_SET_CDM,&cdm)<0){
         printf("set cdm is error\n");
         close(dev);
         return 0;
    } 
    cdm = SCP_WORK_MODE_CODE;
    ioctl(dev,IOCTL_SET_WORK_MODE,&cdm);
    //cdm = 0;
    //ioctl(dev,IOCTL_SET_CODE_COUNT,&cdm);
    
    OpenDspBusyIo();
    SetDspnRst(0);
    usleep(1000);
    SetDspnRst(1);
    usleep(1000);
    
    printf("start boot\r\n");
    ptr = 0;
    while(1){
     
         if(ptr >= Length) {
             break;
         }
         if((Length-ptr)>=2048)  
              Count = 2048;
         else
              Count = Length-ptr;
         
         scp_rw.write_ptr = buf + ptr;
         scp_rw.count     = Count;       
         scp_rw.read_ptr  = rxbuf;     
         if(ioctl(dev,IOCTL_SCP_RW,&scp_rw) != 0){
              printf("write data to scp is error\n");
              /*ErroRebootCount += 1;
              if(ErroRebootCount<=3){
                   close(dev);
                   goto repeat;
              }*/
              break;
              ;	
         }
         ptr += Count;
         
    }
    //cdm = SCP_WORK_MODE_DATA;
    //ioctl(dev,IOCTL_SET_WORK_MODE,&cdm);
    
    CloseDspBusyIo();
    close(dev); 
    //-----------------------------------------------------------------
    free(buf);
    free(rxbuf);
    return 0; 
} 

/**********************************************************************/
/* NAME     : OpenDspBusyIo                                           */
/* FUNCTION : open dsp busy pin in user level  and initialize GPIO31  */
/* PARAMETER: no                                                      */                                      
/**********************************************************************/
void OpenDspBusyIo(void){
    char dev_name[]="/dev/mem";
    //printf("OpenDspBusyIo  ");   
    fd = open(dev_name,O_RDWR);
    if(fd<0){
         printf("open %s is error\n",dev_name);
         return;
    }
    //printf("...success\r\n");    
gpio_base = (GPIO_REGISTER *)mmap( 0, 0x32, PROT_READ | PROT_WRITE, MAP_SHARED,fd, 0x40060000 );
    if(gpio_base == NULL){
         printf("gpio base mmap is error\n");
         close(fd);
         return;
    }

    /*
    gpio_base->tc  = (gpio_base->tc  & 0xfffffffe);             //set GPIO31 as input
    gpio_base->osl = (gpio_base->osl & 0xfffffffc);             //set GPIO31 was control by GPIO
    gpio_base->tsl = (gpio_base->tsl & 0xfffffffc);             //set GPTC as GPIO out enable
    */

    gpio_base->or  = (gpio_base->or  |  (0x80000000 >> 18)); //set GPIO18 as output
    gpio_base->tc  = (gpio_base->tc  |  (0x80000000 >> 18));  //set GPIO18 as output
    gpio_base->osl = gpio_base->osl & 0xf3ffffff;            //set GPIO18 was control by GPIO
    gpio_base->tsl = gpio_base->tsl & 0xf3ffffff;            //set GPTC18 as GPIO out enable
    gpio_base->od  = (gpio_base->od  |  (0x80000000 >> 18)); //GPIO Open Drain Register (GPOD)

    return;
}

/**********************************************************************/
/* NAME     : CloseDspBusyIo                                          */
/* FUNCTION : release resource                                        */
/* PARAMETER: no                                                      */
/**********************************************************************/
void CloseDspBusyIo(void){
    munmap(gpio_base,0x32);
    close(fd);
}

/**********************************************************************/
/* NAME     : GetDspBusyStatus                                        */
/* FUNCTION : get dsp busy pin current status(low or height)          */
/* PARAMETER:                                                         */
/*            out: if dsp busy pin height return 1 else return 0      */
/**********************************************************************/
u32 GetDspBusyStatus(void){
    u32 gpi;
    gpi = gpio_base->ir;
    //printf("GPIO = %08x\r\n",gpi);
    return (gpi&0x01);
}
/**********************************************************************/
/* NAME     : SetDspnRst                                              */
/* FUNCTION : use ppc405's gpio18  set dsp nRST                       */
/* PARAMETER:                                                         */
/*            in: cs will be set level                                */
/**********************************************************************/
void SetDspnRst(u32 nRST){
    if(nRST){
        gpio_base->or = gpio_base->or | (0x80000000 >> 18);
    }
    else{
        gpio_base->or = gpio_base->or & (~(0x80000000 >> 18));
    }
}
/**********************************************************************/
/* NAME     : SetDspnRst                                              */
/* FUNCTION : use ppc405's gpio18  set dsp nRST                       */
/* PARAMETER:                                                         */
/*            in: cs will be set level                                */
/**********************************************************************/
void SetDspSpics(u32 cs){
    if(cs){
        gpio_base->or=(gpio_base->or | 0x00400000);    //out height
    }
    else{
        gpio_base->or=(gpio_base->or & 0xffbfffff);    //out low
    }
}

/**********************************************************************/
/* NAME     : DisHexData                                              */
/* FUNCTION : format out the hex data  as debug out mode              */
/* PARAMETER: *buf :will be display data buf pointer                  */
/*            count:will be display data length                       */
/**********************************************************************/
void DisHexData(u8 * buf ,u32 Count){
    u32 i;
    printf("       ");
    for(i=0;i<16;i++){
        if(i==8) printf(" ");
        printf("%02X ",i);
    }
    printf("\r\n       ");
    for(i=0;i<16;i++){
        printf("...");
    }
    for(i=0;i<Count;i++){
        if(((i%512)==0)&(i!=0)) printf("\r\n");
        if((i%8)==0) printf(" ");
        if((i%16)==0){
             printf("\r\n");
             printf("%04X . ",i);
        }
        printf("%02X ",buf[i]);
 
    }
    //-----------------------------------
    printf("\r\n");
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -