Advertisement
Guest User

Untitled

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