这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 嵌入式开发 » MCU » bf533入门代码

共1条 1/1 1 跳转至

bf533入门代码

工程师
2015-01-03 20:23:29     打赏
*

 

板子做好了,能够工作吗,是很多工程师要问的问题,本代码段是一个完整的测试程序,其中包含了

printk的移植代码,可供dsp入门工程师用。

张驿风

*/

/*****************************************************/
/*project :   led                                    */
/*date    :   2007-2-2 15:12                         */
/*author  :   zhangyifeng                            */
/*function:   blink led in dsp of bf533              */
/*dsp study                                          */
/*****************************************************/

/*
Ver1.01   20070406     能够正确设置BF533的PLL
Ver1.02   20070419     serial工作正常
Ver1.03   20070419     printk移植成功
编译设置方法:
boot mode   = spi slave 
boot format = binary
programable = 4 (PF4 --> 连接powerpc405 GPIO31)
*/

#include <sys\exception.h>
#include <cdefBF533.h>
#include "ccblkfn.h"
#include "sysreg.h"
#include "serial.h"
#include "printk.h"

void Init_Flags(void){
    *pFIO_INEN  = 0x0000;                      //input buffer disable
 *pFIO_DIR  = (1<<11) | (1<<10);           //set PF11 ,PF10 as output other as input ;
}


void inti_pll(void){
 int new_PLL_CTL;
 *pPLL_DIV = 6;                                 //cpu 核1分频 =594Mhz   外围6分频  594/6 = 99Mhz
 asm("ssync;");
 new_PLL_CTL = 23 << 9;                     //23倍   22*27 = 594Mhz
 // enable Wake-up from PLL event
 *pSIC_IWR |= 0xffffffff;

 if (new_PLL_CTL != *pPLL_CTL){ 
  *pPLL_CTL = new_PLL_CTL;
  asm("ssync;");
  asm("idle;");
 }
}

void delms(unsigned long value){
    unsigned long i,j;
    for(i=0;i<value;i++){
        for(j=0;j<10000;j++){
            asm ("nop;");
        }
    }
}

void main(void){
    
  sysreg_write(reg_SYSCFG, 0x32); //Initialize System Configuration Register
  Init_Flags();
  inti_pll();
  initserial();
  printf("Uart test\r\n");
  printf(__DATE__ "\r\n");
  printf(__TIME__ "\r\n");
  
  while(1){
      int ch;
      ch = getchar();
      putchar(ch);
      
      putchar('\n');
      putchar('\r');
      switch(ch){
          case 's':{
              *pFIO_FLAG_S = (1<<11) ;
              *pFIO_FLAG_C = (1<<10) ;
                 break;
          }
          case 'c':{
                 *pFIO_FLAG_C = (1<<11) ;
              *pFIO_FLAG_S = (1<<10) ;
                 break;
          }
          case 'p':{
              printk("printk test:%d,0x%x\r\n",356,356);
               break;
          }
          default:{
              printf("please s,c to control led.\r\n");
          }
      }
  }
}

 

//-----------------------------------------------------------------------------------

/**************************************************************************/
/* 2007-04-19  移植到blackfin成功                                         */
/* 张驿风                                                                 */
/**************************************************************************/

#include <sys\exception.h>
#include <cdefBF533.h>
#include "ccblkfn.h"
#include "sysreg.h"
#include "serial.h"

 

#define ACCESS_LATCH *pUART_LCR |= UART_LCR_DLAB;
#define ACCESS_PORT_IER *pUART_LCR &= (~UART_LCR_DLAB);

void initserial(void){
  *pUART_GCTL |= UART_GCTL_UCEN;  // Enable UART 
  asm("ssync;");


  ACCESS_LATCH;                   // Set DLAB in LCR to Access DLL and DLH 
  asm("ssync;");
 
  *pUART_DLL = 0x284 & 0xFF;
  asm("ssync;");
   *pUART_DLH = (0x284 >> 8) & 0xFF;
   asm("ssync;");
  
  *pUART_LCR = UART_LCR_WLS8;     //Set LCR to Word Lengh 8-bit word select    
  asm("ssync;");    
    
    
}
void putchar(char data){
     while(!((*pUART_LSR) & UART_LSR_TEMT));
     *pUART_THR = data;
}
char kbhit(void){
     if((*pUART_LSR) & UART_LSR_DR)   return 0x55;
     else                             return 0x00;
}
char getchar(void){
     while(!((*pUART_LSR) & UART_LSR_DR));
     return *pUART_RBR;  
}
void printf(char *p){
  while(*p != 0x00){
      putchar(*(p++));
  }
  
}

/*============================================================================*/                            
/*名称: SendHex (int hex)                                                     */
/*功能: 从串口设备发送Hex                                                     */
/*入口参数: 无                                                                */
/*出口参数: 读出的数据                                                        */ 
/*============================================================================*/   
void sendhex (int hex) {                  
    if (hex > 9) putchar('A' + (hex - 10));
    else         putchar('0' +  hex);
}

/***********************************************************************/
/*名称:sendhexstr                                                      */
/*功能:发送hex字符串                                                   */
/*入口参数:no                                                          */
/*出口参数:1 正确提供,2 ID不正确 0失败                               */
/***********************************************************************/
#if SEND_HEX_STR16
void sendhexstr16(unsigned int  Temp){
    putchar('0');
 putchar('x');
 sendhex((Temp>>12)&0x0f);
 sendhex((Temp>>8)&0x0f);
 sendhex((Temp>>4)&0x0f);
 sendhex(Temp&0x0f);
}
#endif
/***********************************************************************/
/*名称:sendhexstr                                                      */
/*功能:发送hex字符串                                                   */
/*入口参数:no                                                          */
/*出口参数:1 正确提供,2 ID不正确 0失败                               */
/***********************************************************************/
#define SEND_HEX_STR8  0
#if SEND_HEX_STR8
void sendhexstr8(unsigned char Temp){
    //putchar('0');
 //putchar('x');
 sendhex((Temp>>4)&0x0f);
 sendhex((Temp)&0x0f);
}
#endif

#define  SEND_DEC_STR4  0
#if SEND_DEC_STR4
void IntToStr(u16 Value,u8 *Buf){
    Buf[0] = (Value / 1000) + '0';
 Buf[1] = (Value % 1000)/100 + '0';
 Buf[2] = ((Value % 1000)%100)/10 + '0';
 Buf[3] = ((Value % 1000)%100)%10 + '0';
}
/***********************************************************************/
/*名称:sendhexstr                                                      */
/*功能:发送hex字符串                                                   */
/*入口参数:no                                                          */
/*出口参数:1 正确提供,2 ID不正确 0失败                               */
/***********************************************************************/
void senddecstr4(u16 uValue){
     unsigned char uStr[4]; 
     IntToStr(uValue,uStr);
     putchar(uStr[0]);
     putchar(uStr[1]);
     putchar(uStr[2]);
     putchar(uStr[3]);
}

#endif

//-------------------------------------------------------------------------------------------------

 

#ifndef __SERIAL__H
#define __SERIAL__H

  #define UART_THR_LO  HALFWORD_REF(UART_THR)
  #define UART_RBR_LO  HALFWORD_REF(UART_RBR)
  #define UART_DLL_LO  HALFWORD_REF(UART_DLL)
  #define UART_IER_LO  HALFWORD_REF(UART_IER)
  #define UART_IER_ERBFI  0x01
  #define UART_IER_ETBEI  0x02
  #define UART_IER_ELSI  0x04
  #define UART_IER_EDDSI  0x08
  
  #define UART_DLH_LO  HALFWORD_REF(UART_DLH)
  #define UART_IIR_LO  HALFWORD_REF(UART_IIR)
  #define UART_IIR_NOINT  0x01
  #define UART_IIR_STATUS  0x06
  #define UART_IIR_LSR  0x06
  #define UART_IIR_RBR  0x04
  #define UART_IIR_THR  0x02
  #define UART_IIR_MSR  0x00
  
  #define UART_LCR_LO  HALFWORD_REF(UART_LCR)
  #define UART_LCR_WLS5  0
  #define UART_LCR_WLS6  0x01
  #define UART_LCR_WLS7  0x02
  #define UART_LCR_WLS8  0x03
  #define UART_LCR_STB  0x04
  #define UART_LCR_PEN  0x08
  #define UART_LCR_EPS  0x10
  #define UART_LCR_SP  0x20
  #define UART_LCR_SB  0x40
  #define UART_LCR_DLAB  0x80
  
  #define UART_MCR_LO  HALFWORD_REF(UART_MCR)
  
  #define UART_LSR_LO  HALFWORD_REF(UART_LSR)
  #define UART_LSR_DR  0x01
  #define UART_LSR_OE  0x02
  #define UART_LSR_PE  0x04
  #define UART_LSR_FE  0x08
  #define UART_LSR_BI  0x10
  #define UART_LSR_THRE  0x20
  #define UART_LSR_TEMT  0x40
  
  #define UART_MSR_LO  HALFWORD_REF(UART_MSR)
  #define UART_SCR_LO  HALFWORD_REF(UART_SCR)
  #define UART_GCTL_LO  HALFWORD_REF(UART_GCTL)
  #define UART_GCTL_UCEN  0x01


 void putchar(char);
 char getchar(void);
 void printf(char *p);
 char kbhit(void);
 void SendToDis(unsigned char bits,unsigned char Value);
 void sendhexstr8(unsigned char Temp);
 void senddecstr4(unsigned int uValue);
 void initserial(void);
 
#endif


//-----------------------------------------------------------------------------

 

 

 

 

 

 

 

 

 

 

 

 

 

/*-------------------------------------------------------------------------+
| printk.c v1.1 - PC386 BSP - 1997/08/07
+--------------------------------------------------------------------------+
| (C) Copyright 1997 -
| - NavIST Group - Real-Time Distributed Systems and Industrial Automation
|
| http://pandora.ist.utl.pt
|
| Instituto Superior Tecnico * Lisboa * PORTUGAL
+--------------------------------------------------------------------------+
| Disclaimer:
|
| This file is provided "AS IS" without warranty of any kind, either
| expressed or implied.
+--------------------------------------------------------------------------+
| This code is based on code by: Jose Rufino - IST
|
|  $Id: printk.c,v 1.3 2000/06/12 16:44:11 joel dead $
+--------------------------------------------------------------------------*/


#include <stdarg.h>
#include <printk.h>

/*-------------------------------------------------------------------------+
|         Function: printNum
|      Description: print number in a given base.
| Global Variables: None.
|        Arguments: num - number to print, base - base used to print the number.
|          Returns: Nothing. 
+--------------------------------------------------------------------------*/


static void printNum(long unsigned int num, int base, int sign)
{
  long unsigned int n;
  int count;
  char toPrint[20];

  if ( (sign == 1) && ((long)num <  0) ) {
    BSP_output_char('-');
    num = -num;
  }
  
  count = 0;
  while ((n = num / base) > 0) {
    toPrint[count++] = (num - (n*base));
    num = n ;
  }
  toPrint[count++] = num;

  for (n = 0; n < count; n++){
     BSP_output_char("0123456789ABCDEF"[(int)(toPrint[count-(n+1)])]);
  }
} /* printNum */


/*-------------------------------------------------------------------------+
|         Function: printk
|      Description: a simplified version of printf intended for use when the
                    console is not yet initialized or in ISR's.
| Global Variables: None.
|        Arguments: as in printf: fmt - format string, ... - unnamed arguments.
|          Returns: Nothing. 
+--------------------------------------------------------------------------*/
void printk(char *fmt, ...)
{
  va_list  ap;      /* points to each unnamed argument in turn */
  char     c, *str;
  int      lflag, base, sign;
  unsigned int level;

  _CPU_ISR_Disable(level);
 
  va_start(ap, fmt); /* make ap point to 1st unnamed arg */
  for (; *fmt != '\0'; fmt++)
  {
    lflag = 0;
    base  = 0;
    sign = 0;
    if (*fmt == '%')
    {
      if ((c = *++fmt) == 'l')
      {
        lflag = 1;
        c = *++fmt;
      }
      switch (c)
      {
        case 'o': case 'O': base = 8; sign = 0; break;
        case 'd': case 'D': base = 10; sign = 1; break;
        case 'u': case 'U': base = 10; sign = 0; break;
        case 'x': case 'X': base = 16; sign = 0; break;
        case 's':
          for (str = va_arg(ap, char *); *str; str++) 
            BSP_output_char(*str);
          break;
        case 'c':
          BSP_output_char(va_arg(ap, char));
          break;
        default:
          BSP_output_char(c);
          break;
      } /* switch*/

      if (base)
        printNum(lflag ? va_arg(ap, long int) : (long int)va_arg(ap, int),
                 base, sign);
    }
    else
    {
      BSP_output_char(*fmt);
    }
  }
  va_end(ap); /* clean up when done */
  _CPU_ISR_Enable(level);

} /* printk */

 


共1条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]