DSP TMS320 F28035的串口驱动程序

2019-07-20 16:05发布

/************************************
    标题:MySci.h
    软件平台:DSP.com/forum.php?mod=forumdisplay&fid=58" target="_blank" class="relatedlink">CCS v5.2
    硬件平台:TMS320F28035coreboard
    主频:60M

    author:小船
    data:2013-01-22
*************************************/
#ifndef MySci_H_
#define MySci_H_

#include "PeripheralHeaderIncludes.h"
#include <stdbool.h>

#define RECEIVER_BUFF_SIZE 50

struct Sci_send_data{
char *AddPtr;
char length;
};

extern char receiver_counter;
extern struct Sci_send_data data;
extern char receiver_data_buff[RECEIVER_BUFF_SIZE];

void MySci_init();
bool MySci_send(char *Ptr,char length);

#endif /* MySci_H_ */

/************************************
    标题:MySci.c
    软件平台:CCS v5.2
    硬件平台:TMS320F28035coreboard
    主频:60M

       描述:串口通信,波特率38400,自动中断发送,不必多余的等待
注意:如果用fifo,就不用判断SciaRegs.SCIRXST.bit.RXRDY,此位不会生效
      基于2803x C/C++ Header Files V1.21

    author:小船
    data:2013-01-22
*************************************/
#include"MySci.h"

struct Sci_send_data data;
char receiver_data_buff[RECEIVER_BUFF_SIZE];
char receiver_counter = 0;

interrupt void rece_isr();
interrupt void tx_isr();

void MySci_init()
{
/*配置GPIO*/
    EALLOW;
    GpioCtrlRegs.GPAPUD.bit.GPIO28 = 0;   
    GpioCtrlRegs.GPAPUD.bit.GPIO29 = 0;   
    GpioCtrlRegs.GPAQSEL2.bit.GPIO28 = 3;  
    GpioCtrlRegs.GPAMUX2.bit.GPIO28 = 1;  
    GpioCtrlRegs.GPAMUX2.bit.GPIO29 = 1;  
    EDIS;

  /*配置fifo*/
    SciaRegs.SCIFFTX.all=0xC060;
    SciaRegs.SCIFFRX.all=0x0021;  //fifo接收到一个字节就中断
    SciaRegs.SCIFFCT.all=0x0;

  /*配置sci*/
    SciaRegs.SCICCR.all = 0x0007;  
                                   
                                    
    SciaRegs.SCICTL1.all = 0x0063;  
                                    
    //SciaRegs.SCICTL2.bit.TXINTENA = 1;
    SciaRegs.SCICTL2.bit.RXBKINTENA = 1;
    SciaRegs.SCIHBAUD    = 0x0000;  // 38400 baud @LSPCLK = 15MHz (60 MHz SYSCLK).
    SciaRegs.SCILBAUD    = 0x0030;

    SciaRegs.SCICTL1.all = 0x0063;  
    SciaRegs.SCIFFTX.bit.TXFIFOXRESET=1;
    SciaRegs.SCIFFRX.bit.RXFIFORESET=1;

    /*配置中断*/
    EALLOW;
    PieVectTable.SCITXINTA = &tx_isr; //注册中断向量表
    PieVectTable.SCIRXINTA = &rece_isr;
    PieCtrlRegs.PIECTRL.bit.ENPIE = 1;   //使能PIE
    PieCtrlRegs.PIEIER9.bit.INTx1=1;     //使能int9.1
    PieCtrlRegs.PIEIER9.bit.INTx2=1;     //使能int9.2
    IER |= (1<<8);//使能int9
    EINT;
    EDIS;

    data.length=0;
    data.AddPtr=0;
}

bool MySci_send(char *Ptr,char length)
{
if(SciaRegs.SCIFFTX.bit.TXFFIENA==1)
  return false;
else
{
  data.AddPtr = Ptr;
  data.length = length;
  SciaRegs.SCIFFTX.bit.TXFFIENA=1;
}
return true;
}

interrupt void tx_isr()
{
if( data.length)
{
  SciaRegs.SCITXBUF = *data.AddPtr++;
  data.length--;
}
else
  SciaRegs.SCIFFTX.bit.TXFFIENA=0;
SciaRegs.SCIFFTX.bit.TXFFINTCLR=1;
PieCtrlRegs.PIEACK.all |= PIEACK_GROUP9;
}

interrupt void rece_isr()
{
if(SciaRegs.SCIRXST.bit.RXERROR)
{
  while(1);
}
if( receiver_counter != RECEIVER_BUFF_SIZE)
  receiver_data_buff[receiver_counter++] = SciaRegs.SCIRXBUF.bit.RXDT;

SciaRegs.SCIFFRX.bit.RXFFOVRCLR=1;   // Clear Overflow flag
SciaRegs.SCIFFRX.bit.RXFFINTCLR=1;   // Clear Interrupt flag
PieCtrlRegs.PIEACK.all |= PIEACK_GROUP9;
}
0条回答

一周热门 更多>