Advertisement
Guest User

Untitled

a guest
Sep 1st, 2015
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.35 KB | None | 0 0
  1. // Graph-Tech AG
  2. // 08.November 2011
  3. //###########################################################################
  4. //
  5. // FILE:    EncSync.c
  6. //
  7. // TITLE:   Test Program.
  8. //
  9. // ASSUMPTIONS:
  10. //
  11. //    This program requires the DSP2834x header files.
  12. //
  13. //    As supplied, this project is configured for "boot to SARAM"
  14. //    operation.  The 2834x Boot Mode table is shown below.
  15. //
  16. //         GPIO87   GPIO86     GPIO85   GPIO84
  17. //          XA15     XA14       XA13     XA12
  18. //           PU       PU         PU       PU
  19. //        ==========================================
  20. //            1        1          0        0    I2C-A boot timing 1
  21. //
  22. // DESCRIPTION:
  23. //
  24. // //
  25.  
  26. #include "DSP28x_Project.h"     // Device Headerfile and Examples Include File
  27.  
  28.  
  29. // Prototype statements for functions found within this file.
  30. void    Gpio_select(void);
  31. void    I2CA_Init(void);
  32. Uint16  I2CA_WriteData(struct I2CMSG *msg);
  33. void    scia_fifo_init(void);
  34.  
  35. // Interrupt prototyps
  36. interrupt void i2c_int1a_isr(void);
  37. interrupt void sciaTxFifoIsr(void);
  38. interrupt void sciaRxFifoIsr(void);
  39.  
  40. // defines
  41. #define I2C_NUMBYTES          8
  42.  
  43. // Global variables
  44.  
  45. Uint16  DldCounter;
  46. long    Download;
  47. char    far *DldReadMsgPtr;
  48. char    far *DldWriteMsgPtr;
  49.  
  50. // Global variables
  51. // Two bytes will be used for the outgoing address,
  52. // thus only setup 14 bytes maximum
  53. struct I2CMSG I2cMsgOut1;
  54. struct I2CMSG *CurrentMsgPtr;               // Used in interrupts
  55.  
  56. void main(void)
  57. {
  58.    Uint16 count    = 0;
  59.    Uint16 iiCCount = 0;
  60.    long wait;
  61.  
  62. // Step 1. Initialize System Control:
  63. // PLL, WatchDog, enable Peripheral Clocks
  64. // This example function is found in the DSP2834x_SysCtrl.c file.
  65.    InitSysCtrl();
  66.  
  67. //  Step 2. Initalize GPIO:
  68. //  This example function is found in the DSP2834x_Gpio.c file and
  69. //  illustrates how to set the GPIO to it's default state.
  70. //  InitGpio();
  71.  
  72. //  For this example use the following configuration:
  73.     Gpio_select();
  74.  
  75. // Step 3. Clear all interrupts and initialize PIE vector table:
  76. // Disable CPU interrupts
  77.    DINT;
  78.  
  79. // Initialize PIE control registers to their default state.
  80. // The default state is all PIE interrupts disabled and flags
  81. // are cleared.
  82. // This function is found in the DSP2834x_PieCtrl.c file.
  83.    InitPieCtrl();
  84.  
  85. // Disable CPU interrupts and clear all CPU interrupt flags:
  86.    IER = 0x0000;
  87.    IFR = 0x0000;
  88.  
  89. // Initialize the PIE vector table with pointers to the shell Interrupt
  90. // Service Routines (ISR).
  91. // This will populate the entire table, even if the interrupt
  92. // is not used in this example.  This is useful for debug purposes.
  93. // The shell ISR routines are found in DSP2834x_DefaultIsr.c.
  94. // This function is found in DSP2834x_PieVect.c.
  95.    InitPieVectTable();
  96.  
  97. // Interrupts that are used in this example are re-mapped to
  98. // ISR functions found within this file.
  99.    EALLOW;  // This is needed to write to EALLOW protected registers
  100.    PieVectTable.I2CINT1A  = &i2c_int1a_isr;
  101.    PieVectTable.SCIRXINTA = &sciaRxFifoIsr;
  102.    EDIS;   // This is needed to disable write to EALLOW protected registers
  103.  
  104. // Step 4. Initialize all the Device Peripherals:
  105. // This function is found in DSP2834x_InitPeripherals.c
  106.  
  107.    I2CA_Init();
  108.    scia_fifo_init();  // Init SCI-A
  109.  
  110. // Step 5. User specific code
  111.  
  112.     DldCounter      = 0;
  113.     Download        = 0;
  114.  
  115. // Enable interrupts required for this example
  116.  
  117.     PieCtrlRegs.PIECTRL.bit.ENPIE   = 1;        // Enable the PIE block
  118.     PieCtrlRegs.PIEIER8.bit.INTx1   = 1;        // PIE Group 8      IIC
  119.     PieCtrlRegs.PIEIER9.bit.INTx1   = 1;        // PIE Group 9
  120.  
  121. // Enable CPU INT8/9 which is connected to PIE group 8/9
  122.  
  123.     IER |= M_INT8;
  124.     IER |= M_INT9;
  125.  
  126.     EINT;
  127.  
  128.     CurrentMsgPtr               = &I2cMsgOut1;
  129.  
  130.     int countUP = 0;
  131.     GpioDataRegs.GPBCLEAR.bit.GPIO46 = 1;
  132.     GpioDataRegs.GPBCLEAR.bit.GPIO45 = 1;
  133.     GpioDataRegs.GPBCLEAR.bit.GPIO44 = 1;
  134.  
  135.     // Application loop
  136.     for(;;)
  137.     {
  138.         if(Download)
  139.         {
  140.             GpioDataRegs.GPBCLEAR.bit.GPIO46 = 1;
  141.             GpioDataRegs.GPBSET.bit.GPIO45 = 1;
  142.             if(--Download == 0)
  143.             {
  144.                 GpioDataRegs.GPBSET.bit.GPIO44 = 1;
  145.                 I2cMsgOut1.MsgStatus = I2C_MSGSTAT_INACTIVE;
  146.  
  147.                 for(count = 0 ; count < DldCounter ; count += I2C_NUMBYTES)
  148.                 {
  149.                     while(I2cMsgOut1.MsgStatus != I2C_MSGSTAT_INACTIVE)
  150.                         ;
  151.  
  152.                     for(wait = 0 ; wait < 0x10000 ; wait++) // todo wait for eeprom write is done
  153.                         ;
  154.  
  155.                     I2cMsgOut1.MsgStatus        = I2C_MSGSTAT_SEND_WITHSTOP;
  156.                     I2cMsgOut1.SlaveAddress     = 0x50;
  157.                     I2cMsgOut1.MemoryHighAddr   = (count >> 8) & 0xff;
  158.                     I2cMsgOut1.MemoryLowAddr    = (count >> 0) & 0xff;
  159.                     I2cMsgOut1.NumOfBytes       = I2C_NUMBYTES;
  160.  
  161.                     DldReadMsgPtr               = (char far *)(0x320000 + count);
  162.  
  163.                     for (iiCCount = 0 ; iiCCount < I2C_NUMBYTES ; iiCCount++)
  164.                         I2cMsgOut1.MsgBuffer[iiCCount]      = *DldReadMsgPtr++;
  165.  
  166.                     while(I2CA_WriteData(&I2cMsgOut1) != I2C_SUCCESS)
  167.                         ;
  168.  
  169.                     I2cMsgOut1.MsgStatus        = I2C_MSGSTAT_WRITE_BUSY;
  170.                 }
  171.  
  172.                 DldCounter  = 0;
  173.             }
  174.         } else {
  175.             if(countUP == 0){
  176.                 GpioDataRegs.GPBTOGGLE.bit.GPIO46 = 1;
  177.             }
  178.         }
  179.  
  180.         countUP++;
  181.  
  182.         // TEST TEST TEST
  183.         if(GpioDataRegs.GPBDAT.bit.GPIO50)                  // A Sig Encoder 1
  184.         {
  185.             GpioDataRegs.GPCSET.bit.GPIO80      = 1;        // Encoder 1
  186.             GpioDataRegs.GPCSET.bit.GPIO78      = 1;        // Encoder 2
  187.             GpioDataRegs.GPCSET.bit.GPIO77      = 1;        // Encoder 3
  188.             GpioDataRegs.GPCSET.bit.GPIO71      = 1;        // Encoder 4
  189.             GpioDataRegs.GPCSET.bit.GPIO74      = 1;        // Encoder 5
  190.             GpioDataRegs.GPCSET.bit.GPIO73      = 1;        // Encoder 6
  191.             GpioDataRegs.GPCSET.bit.GPIO68      = 1;        // Encoder 7
  192.             GpioDataRegs.GPCSET.bit.GPIO67      = 1;        // Encoder 8
  193.             GpioDataRegs.GPCSET.bit.GPIO64      = 1;        // Encoder 9
  194.         }
  195.         else
  196.         {
  197.             GpioDataRegs.GPCCLEAR.bit.GPIO80    = 1;        // Encoder 1
  198.             GpioDataRegs.GPCCLEAR.bit.GPIO78    = 1;        // Encoder 2
  199.             GpioDataRegs.GPCCLEAR.bit.GPIO77    = 1;        // Encoder 3
  200.             GpioDataRegs.GPCCLEAR.bit.GPIO71    = 1;        // Encoder 4
  201.             GpioDataRegs.GPCCLEAR.bit.GPIO74    = 1;        // Encoder 5
  202.             GpioDataRegs.GPCCLEAR.bit.GPIO73    = 1;        // Encoder 6
  203.             GpioDataRegs.GPCCLEAR.bit.GPIO68    = 1;        // Encoder 7
  204.             GpioDataRegs.GPCCLEAR.bit.GPIO67    = 1;        // Encoder 8
  205.             GpioDataRegs.GPCCLEAR.bit.GPIO64    = 1;        // Encoder 9
  206.         }
  207.  
  208.  
  209.         if(GpioDataRegs.GPADAT.bit.GPIO2)                   // Input 1
  210.             GpioDataRegs.GPCSET.bit.GPIO72  = 1;            // PG 1
  211.         else
  212.             GpioDataRegs.GPCCLEAR.bit.GPIO72    = 1;
  213.  
  214.         if(GpioDataRegs.GPADAT.bit.GPIO9)                   // Input 2
  215.             GpioDataRegs.GPCSET.bit.GPIO81  = 1;            // PG 2
  216.         else
  217.             GpioDataRegs.GPCCLEAR.bit.GPIO81    = 1;
  218.  
  219.         if(GpioDataRegs.GPADAT.bit.GPIO6)                   // Input 3
  220.             GpioDataRegs.GPCSET.bit.GPIO76  = 1;            // PG 3
  221.         else
  222.             GpioDataRegs.GPCCLEAR.bit.GPIO76    = 1;
  223.  
  224.         if(GpioDataRegs.GPADAT.bit.GPIO3)                   // Input 4
  225.             GpioDataRegs.GPCSET.bit.GPIO75  = 1;            // PG 4
  226.         else
  227.             GpioDataRegs.GPCCLEAR.bit.GPIO75    = 1;
  228.  
  229.         if(GpioDataRegs.GPADAT.bit.GPIO5)                   // Input 5
  230.             GpioDataRegs.GPCSET.bit.GPIO70  = 1;            // PG 5
  231.         else
  232.             GpioDataRegs.GPCCLEAR.bit.GPIO70    = 1;
  233.  
  234.         if(GpioDataRegs.GPADAT.bit.GPIO10)                  // Input 6
  235.             GpioDataRegs.GPCSET.bit.GPIO79  = 1;            // PG 6
  236.         else
  237.             GpioDataRegs.GPCCLEAR.bit.GPIO79    = 1;
  238.  
  239.         if(GpioDataRegs.GPADAT.bit.GPIO11)                  // Input 7
  240.             GpioDataRegs.GPCSET.bit.GPIO66  = 1;            // PG 7
  241.         else
  242.             GpioDataRegs.GPCCLEAR.bit.GPIO66    = 1;
  243.  
  244.         if(GpioDataRegs.GPADAT.bit.GPIO8)                   // Input 8
  245.             GpioDataRegs.GPCSET.bit.GPIO69  = 1;            // PG 8
  246.         else
  247.             GpioDataRegs.GPCCLEAR.bit.GPIO69    = 1;
  248.  
  249.         if(GpioDataRegs.GPADAT.bit.GPIO7)                   // Input 9
  250.             GpioDataRegs.GPCSET.bit.GPIO65  = 1;            // PG 9
  251.         else
  252.             GpioDataRegs.GPCCLEAR.bit.GPIO65    = 1;
  253.  
  254.         GpioDataRegs.GPBTOGGLE.bit.GPIO42    = 1;
  255.     }
  256. }
  257.  
  258. void Gpio_select(void)
  259. {
  260.     EALLOW;
  261.  
  262.     GpioCtrlRegs.GPAMUX1.all = 0x00000000;  // 00 - 15
  263.     GpioCtrlRegs.GPAMUX2.all = 0x552A0055;  // 16 - 31
  264.     GpioCtrlRegs.GPBMUX1.all = 0x00000005;  // 32 - 47
  265.     GpioCtrlRegs.GPBMUX2.all = 0x000CF450;  // 48 - 63
  266.     GpioCtrlRegs.GPCMUX1.all = 0x00000000;  // 64 - 79
  267.     GpioCtrlRegs.GPCMUX2.all = 0x00000000;  // 80 - 87
  268.  
  269.     GpioCtrlRegs.GPADIR.all = 0xA8FEF000;   // dir
  270.     GpioCtrlRegs.GPBDIR.all = 0xFD13FDFE;   // dir
  271.     GpioCtrlRegs.GPCDIR.all = 0x000FFFFF;   // dir
  272.  
  273.     EDIS;
  274. }
  275.  
  276. void I2CA_Init(void)
  277. {
  278.    // Initialize I2C
  279.    I2caRegs.I2CSAR      = 0x050;        // Slave address - EEPROM control code
  280.    I2caRegs.I2CPSC.all  = 29;           // Prescaler - need 7-12 Mhz on module clk (300/30 = 10MHz)
  281.    I2caRegs.I2CCLKL     = 10;           // NOTE: must be non zero
  282.    I2caRegs.I2CCLKH     = 5;            // NOTE: must be non zero
  283.  
  284.    I2caRegs.I2CIER.all  = 0x24;         // Enable SCD & ARDY interrupts
  285.  
  286.    I2caRegs.I2CMDR.all  = 0x0020;       // Take I2C out of reset
  287.                                         // Stop I2C when suspended
  288.  
  289.    I2caRegs.I2CFFTX.all = 0x6000;       // Enable FIFO mode and TXFIFO
  290.    I2caRegs.I2CFFRX.all = 0x2040;       // Enable RXFIFO, clear RXFFINT,
  291.  
  292.    return;
  293. }
  294.  
  295. Uint16 I2CA_WriteData(struct I2CMSG *msg)
  296. {
  297.    Uint16 i;
  298.  
  299.    // Wait until the STP bit is cleared from any previous master communication.
  300.    // Clearing of this bit by the module is delayed until after the SCD bit is
  301.    // set. If this bit is not checked prior to initiating a new message, the
  302.    // I2C could get confused.
  303.  
  304.    if (I2caRegs.I2CMDR.bit.STP == 1)    return I2C_STP_NOT_READY_ERROR;
  305.  
  306.    // Setup slave address
  307.    I2caRegs.I2CSAR = 0x050;
  308.  
  309.    // Check if bus busy
  310.    if (I2caRegs.I2CSTR.bit.BB == 1)     return I2C_BUS_BUSY_ERROR;
  311.  
  312.    // Setup number of bytes to send
  313.    // MsgBuffer + Address
  314.    I2caRegs.I2CCNT = msg->NumOfBytes+2;
  315.  
  316.    // Setup data to send
  317.    I2caRegs.I2CDXR = msg->MemoryHighAddr;
  318.    I2caRegs.I2CDXR = msg->MemoryLowAddr;
  319.  
  320.    for (i=0; i<msg->NumOfBytes; i++)
  321.      I2caRegs.I2CDXR = *(msg->MsgBuffer+i);
  322.  
  323.    // Send start as master transmitter
  324.    I2caRegs.I2CMDR.all = 0x6E20;
  325.  
  326.    return I2C_SUCCESS;
  327. }
  328.  
  329. interrupt void i2c_int1a_isr(void)      // I2C-A
  330. {
  331.     Uint16 IntSource;
  332.  
  333.     IntSource   = I2caRegs.I2CISRC.all; // Read interrupt source
  334.  
  335.     if(IntSource == I2C_SCD_ISRC)       // Interrupt source = stop condition detected
  336.     {
  337.         // If completed message was writing data, reset msg to inactive state
  338.         if (CurrentMsgPtr->MsgStatus == I2C_MSGSTAT_WRITE_BUSY)
  339.             CurrentMsgPtr->MsgStatus = I2C_MSGSTAT_INACTIVE;
  340.     }
  341.     else
  342.     {
  343.         if(IntSource == I2C_ARDY_ISRC)
  344.         {
  345.             if(I2caRegs.I2CSTR.bit.NACK == 1)
  346.             {
  347.                 I2caRegs.I2CMDR.bit.STP = 1;
  348.                 I2caRegs.I2CSTR.all     = I2C_CLR_NACK_BIT;
  349.             }
  350.         }  // end of register access ready
  351.     }
  352.     // Enable future I2C (PIE Group 8) interrupts
  353.     PieCtrlRegs.PIEACK.all = PIEACK_GROUP8;
  354. }
  355.  
  356. interrupt void sciaTxFifoIsr(void)
  357. {
  358.     SciaRegs.SCIFFTX.bit.TXFFINTCLR  =1;                    // Clear SCI Interrupt flag
  359.     PieCtrlRegs.PIEACK.all          |= PIEACK_GROUP9;       // Issue PIE ACK
  360. }
  361.  
  362. interrupt void sciaRxFifoIsr(void)
  363. {
  364.     char data;
  365.  
  366.     data    = SciaRegs.SCIRXBUF.all;     // Read data
  367.  
  368.     if(DldCounter == 0)
  369.     {
  370.         if(data != 0xAA)    // Download Start
  371.             DldCounter  = 0;
  372.         else
  373.         {
  374.             DldWriteMsgPtr  = (char far *)(0x320000);
  375.  
  376.             *DldWriteMsgPtr++ = data;
  377.             DldCounter++;
  378.         }
  379.     }
  380.     else
  381.     {
  382.         *DldWriteMsgPtr++ = data;
  383.         DldCounter++;
  384.     }
  385.  
  386.     if(DldCounter)
  387.         Download    = 0x10000;
  388.  
  389.     SciaRegs.SCIFFRX.bit.RXFFOVRCLR     = 1;   // Clear Overflow flag
  390.     SciaRegs.SCIFFRX.bit.RXFFINTCLR     = 1;   // Clear Interrupt flag
  391.  
  392.     PieCtrlRegs.PIEACK.all              |= PIEACK_GROUP9;       // Issue PIE ack
  393. }
  394.  
  395. void scia_fifo_init()
  396. {
  397.    SciaRegs.SCICCR.all                  = 0x0007;   // 1 stop bit,  No loopback
  398.                                                     // No parity,8 char bits,
  399.                                                     // async mode, idle-line protocol
  400.    SciaRegs.SCICTL1.all                 = 0x0003;   // enable TX, RX, internal SCICLK,
  401.                                                     // Disable RX ERR, SLEEP, TXWAKE
  402.    SciaRegs.SCICTL2.bit.TXINTENA        = 1;
  403.    SciaRegs.SCICTL2.bit.RXBKINTENA      = 1;
  404.    SciaRegs.SCIHBAUD                    = 0x0000;
  405.    //SciaRegs.SCILBAUD                  = SCI_PRD;
  406.  
  407.    SciaRegs.SCILBAUD                    = 81;       // 115200
  408.  
  409.    SciaRegs.SCICCR.bit.LOOPBKENA        = 0;        // Disable loop back
  410.    SciaRegs.SCIFFTX.all                 = 0xC021;
  411.    SciaRegs.SCIFFRX.all                 = 0x0021;
  412.    SciaRegs.SCIFFCT.all                 = 0x00;
  413.  
  414.    SciaRegs.SCICTL1.all                 = 0x0023;   // Relinquish SCI from Reset
  415.    SciaRegs.SCIFFTX.bit.TXFIFOXRESET    = 1;
  416.    SciaRegs.SCIFFRX.bit.RXFIFORESET     = 1;
  417. }
  418. //===========================================================================
  419. // No more.
  420. //===========================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement