Advertisement
Guest User

GPIO over uart

a guest
Feb 19th, 2013
533
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 14.81 KB | None | 0 0
  1. /*
  2.  ## Cypress USB 3.0 Platform source file (cyfxgpioapp.c)
  3.  ## ===========================
  4.  ##
  5.  ##  Copyright Cypress Semiconductor Corporation, 2010-2011,
  6.  ##  All Rights Reserved
  7.  ##  UNPUBLISHED, LICENSED SOFTWARE.
  8.  ##
  9.  ##  CONFIDENTIAL AND PROPRIETARY INFORMATION
  10.  ##  WHICH IS THE PROPERTY OF CYPRESS.
  11.  ##
  12.  ##  Use of this file is governed
  13.  ##  by the license agreement included in the file
  14.  ##
  15.  ##     <install>/license/license.txt
  16.  ##
  17.  ##  where <install> is the Cypress software
  18.  ##  installation root directory path.
  19.  ##
  20.  ## ===========================
  21. */
  22.  
  23. /* This file implements a simple GPIO application example. */
  24.  
  25. /* This example illustrates the use of the FX3 firmware APIs to implement
  26.  * a simple GPIO application example.
  27.  *
  28.  * The example illustrates the usage of simple GPIO API to set and get
  29.  * the status of the pin and the usage of GPIO interrupts.
  30.  *
  31.  * The example uses GPIO 21 as output. It toggles this pin ON and OFF
  32.  * at an interval of 2s.
  33.  *
  34.  * GPIO 45 is used as the input GPIO. Interrupts are enabled and a
  35.  * callback is registered for the GPIO edge interrupts both positive
  36.  * and negative edges.
  37.  *
  38.  * GPIO 21 is located at pin 2 of the jumper J100 on the DVK board.
  39.  * GPIO 45 is located on the DVK board at the test point TP13.
  40.  */
  41.  
  42. #include <cyu3system.h>
  43. #include <cyu3os.h>
  44. #include <cyu3error.h>
  45. #include <cyu3gpio.h>
  46. //#include <cyu3uart.h>
  47.  
  48. #define CY_FX_GPIOAPP_THREAD_STACK       (0x0400)   /* GPIO application thread stack size */
  49. #define CY_FX_GPIOAPP_THREAD_PRIORITY    (8)        /* GPIO application thread priority */
  50.  
  51. #define CY_FX_GPIOAPP_GPIO_HIGH_EVENT    (1 << 0)   /* GPIO high event */
  52. #define CY_FX_GPIOAPP_GPIO_LOW_EVENT     (1 << 1)   /* GPIO low event */
  53.  
  54. CyU3PThread gpioOutputThread;   /* GPIO thread structure */
  55. CyU3PThread gpioInputThread;    /* GPIO thread structure */
  56. CyU3PEvent glFxGpioAppEvent;    /* GPIO input event group. */
  57.  
  58. /* Application error handler. */
  59. void
  60. CyFxAppErrorHandler (
  61.         CyU3PReturnStatus_t apiRetStatus    /* API return status */
  62.         )
  63. {
  64.     /* Application failed with the error code apiRetStatus */
  65.  
  66.     /* Add custom debug or recovery actions here */
  67.  
  68.     /* Loop indefinitely */
  69.     for (;;)
  70.     {
  71.         /* Thread sleep : 100 ms */
  72.         CyU3PThreadSleep (100);
  73.     }
  74. }
  75.  
  76. // Initialize the debug module with UART.
  77. /*
  78. CyU3PReturnStatus_t
  79. CyFxDebugInit (
  80.         void)
  81. {
  82.     CyU3PUartConfig_t uartConfig;
  83.     CyU3PReturnStatus_t status = CY_U3P_SUCCESS;
  84.  
  85.      //Initialize and configure the UART for logging.
  86.     status = CyU3PUartInit ();
  87.     if (status != CY_U3P_SUCCESS)
  88.     {
  89.         return status;
  90.     }
  91.  
  92.     CyU3PMemSet ((uint8_t *)&uartConfig, 0, sizeof (uartConfig));
  93.     uartConfig.baudRate = CY_U3P_UART_BAUDRATE_115200;
  94.     uartConfig.stopBit  = CY_U3P_UART_ONE_STOP_BIT;
  95.     uartConfig.parity   = CY_U3P_UART_NO_PARITY;
  96.     uartConfig.txEnable = CyFalse;
  97.     uartConfig.rxEnable = CyFalse;
  98.     uartConfig.flowCtrl = CyFalse;
  99.     uartConfig.isDma    = CyFalse;
  100.     status = CyU3PUartSetConfig (&uartConfig, NULL);
  101.     if (status != CY_U3P_SUCCESS)
  102.     {
  103.         return status;
  104.     }
  105.  
  106.     // Set the dma for an inifinity transfer
  107.     status = CyU3PUartTxSetBlockXfer (0xFFFFFFFF);
  108.     if (status != CY_U3P_SUCCESS)
  109.     {
  110.         return status;
  111.     }
  112.  
  113.     // Start the debug module for printing log messages.
  114.     status = CyU3PDebugInit (CY_U3P_LPP_SOCKET_UART_CONS, 8);
  115.  
  116.     return status;
  117. }
  118. */
  119.  
  120. /* GPIO interrupt callback handler. This is received from
  121.  * the interrupt context. So DebugPrint API is not available
  122.  * from here. Set an event in the event group so that the
  123.  * GPIO thread can print the event information. */
  124. void CyFxGpioIntrCb (
  125.         uint8_t gpioId /* Indicates the pin that triggered the interrupt */
  126.         )
  127. {
  128.     CyBool_t gpioValue = CyFalse;
  129.     CyU3PReturnStatus_t apiRetStatus = CY_U3P_SUCCESS;
  130.  
  131.     /* Get the status of the pin */
  132.     apiRetStatus = CyU3PGpioGetValue (gpioId, &gpioValue);
  133.     if (apiRetStatus == CY_U3P_SUCCESS)
  134.     {
  135.         /* Check status of the pin */
  136.         if (gpioValue == CyTrue)
  137.         {
  138.             /* Set GPIO high event */
  139.             CyU3PEventSet(&glFxGpioAppEvent, CY_FX_GPIOAPP_GPIO_HIGH_EVENT,
  140.                     CYU3P_EVENT_OR);
  141.         }
  142.         else
  143.         {
  144.             /* Set GPIO low Event */
  145.             CyU3PEventSet(&glFxGpioAppEvent, CY_FX_GPIOAPP_GPIO_LOW_EVENT,
  146.                     CYU3P_EVENT_OR);
  147.         }
  148.     }
  149. }
  150.  
  151. void
  152. CyFxGpioInit (void)
  153. {
  154.     CyU3PGpioClock_t gpioClock;
  155.     CyU3PGpioSimpleConfig_t gpioConfig;
  156.     CyU3PReturnStatus_t apiRetStatus = CY_U3P_SUCCESS;
  157.  
  158.     /* Init the GPIO module */
  159.     gpioClock.fastClkDiv = 2;
  160.     gpioClock.slowClkDiv = 0;
  161.     gpioClock.simpleDiv = CY_U3P_GPIO_SIMPLE_DIV_BY_2;
  162.     gpioClock.clkSrc = CY_U3P_SYS_CLK;
  163.     gpioClock.halfDiv = 0;
  164.  
  165.     apiRetStatus = CyU3PGpioInit(&gpioClock, CyFxGpioIntrCb);
  166.     if (apiRetStatus != 0)
  167.     {
  168.         /* Error Handling */
  169.         /*CyU3PDebugPrint (4, "CyU3PGpioInit failed, error code = %d\n", apiRetStatus);*/
  170.         CyFxAppErrorHandler(apiRetStatus);
  171.     }
  172.  
  173.     /* Configure GPIO 45 as input with interrupt enabled for both edges */
  174.     gpioConfig.outValue = CyTrue;
  175.     gpioConfig.inputEn = CyTrue;
  176.     gpioConfig.driveLowEn = CyFalse;
  177.     gpioConfig.driveHighEn = CyFalse;
  178.     gpioConfig.intrMode = CY_U3P_GPIO_INTR_BOTH_EDGE;
  179.     apiRetStatus = CyU3PGpioSetSimpleConfig(45, &gpioConfig);
  180.     if (apiRetStatus != CY_U3P_SUCCESS)
  181.     {
  182.         /* Error handling */
  183.        /* CyU3PDebugPrint (4, "CyU3PGpioSetSimpleConfig failed, error code = %d\n",
  184.                 apiRetStatus);*/
  185.         CyFxAppErrorHandler(apiRetStatus);
  186.     }
  187.  
  188.     /* Override GPIO 21 as this pin is associated with GPIF Control signal.
  189.        * The IO cannot be selected as GPIO by CyU3PDeviceConfigureIOMatrix call
  190.        * as it is part of the GPIF IOs. Override API call must be made with
  191.        * caution as this will change the functionality of the pin. If the IO
  192.        * line is used as part of GPIF and is connected to some external device,
  193.        * then the line will no longer behave as a GPIF IO.. Here CTL4 line is
  194.        * not used and so it is safe to override.  */
  195.    /*   apiRetStatus = CyU3PDeviceGpioOverride (53, CyTrue);
  196.       if (apiRetStatus != 0)
  197.       {
  198.            //Error Handling
  199.           CyU3PDebugPrint (4, "CyU3PDeviceGpioOverride failed, error code = %d\n",
  200.                   apiRetStatus);
  201.           CyFxAppErrorHandler(apiRetStatus);
  202.       }*/
  203.     //CyU3PDeviceGpioRestore(53);
  204.     /* Configure GPIO 21 as output */
  205.     int i;
  206.     for(i = 51; i <=57; i ++)
  207.     {
  208.         gpioConfig.outValue = CyFalse;
  209.         gpioConfig.driveLowEn = CyTrue;
  210.         gpioConfig.driveHighEn = CyTrue;
  211.         gpioConfig.inputEn = CyFalse;
  212.         gpioConfig.intrMode = CY_U3P_GPIO_NO_INTR;
  213.         apiRetStatus = CyU3PGpioSetSimpleConfig(i, &gpioConfig);
  214.         if (apiRetStatus != CY_U3P_SUCCESS)
  215.         {
  216.             /* Error handling */
  217.             /*CyU3PDebugPrint (4, "CyU3PGpioSetSimpleConfig failed, error code = %d\n",
  218.                     apiRetStatus);*/
  219.             CyFxAppErrorHandler(apiRetStatus);
  220.         }
  221.     }
  222. }
  223.  
  224. /* Entry function for the gpioOutputThread */
  225. void
  226. GpioOutputThread_Entry (
  227.         uint32_t input)
  228. {
  229.     CyU3PReturnStatus_t apiRetStatus = CY_U3P_SUCCESS;
  230.  
  231.     /* Initialize Debug module */
  232.    //apiRetStatus = CyFxDebugInit();
  233.     if (apiRetStatus != CY_U3P_SUCCESS)
  234.     {
  235.         /* Error handling */
  236.        /* CyU3PDebugPrint (4, "Debug module initialization failed, error code = %d\n",
  237.                 apiRetStatus);*/
  238.         CyFxAppErrorHandler(apiRetStatus);
  239.     }
  240.  
  241.     /* Initialize GPIO module. */
  242.     CyFxGpioInit ();
  243.  
  244.     for (;;)
  245.     {
  246.         /* Set the GPIO 21 to high */
  247.         int i;
  248.         for(i = 51; i <=57; i ++)
  249.         {
  250.             apiRetStatus = CyU3PGpioSetValue (i, CyTrue);
  251.             if (apiRetStatus != CY_U3P_SUCCESS)
  252.             {
  253.                 /* Error handling */
  254.                /* CyU3PDebugPrint (4, "CyU3PGpioSetValue failed, error code = %d\n",
  255.                         apiRetStatus);*/
  256.                 CyFxAppErrorHandler(apiRetStatus);
  257.             }
  258.  
  259.             /* Wait for two seconds */
  260.             CyU3PThreadSleep(200);
  261.  
  262.             /* Set the GPIO 21 to low */
  263.             apiRetStatus = CyU3PGpioSetValue (i, CyFalse);
  264.             if (apiRetStatus != CY_U3P_SUCCESS)
  265.             {
  266.                 /* Error handling */
  267.                 /*CyU3PDebugPrint (4, "CyU3PGpioSetValue failed, error code = %d\n",
  268.                         apiRetStatus);*/
  269.                 CyFxAppErrorHandler(apiRetStatus);
  270.             }
  271.  
  272.             /* Wait for two seconds */
  273.             CyU3PThreadSleep(200);
  274.         }
  275.     }
  276. }
  277.  
  278. /* Entry function for the gpioInputThread */
  279. void
  280. GpioInputThread_Entry (
  281.         uint32_t input)
  282. {
  283.     uint32_t eventFlag;
  284.     CyU3PReturnStatus_t txApiRetStatus = CY_U3P_SUCCESS;
  285.  
  286.     for (;;)
  287.     {
  288.         /* Wait for a GPIO event */
  289.         txApiRetStatus = CyU3PEventGet (&glFxGpioAppEvent,
  290.                 (CY_FX_GPIOAPP_GPIO_HIGH_EVENT | CY_FX_GPIOAPP_GPIO_LOW_EVENT),
  291.                 CYU3P_EVENT_OR_CLEAR, &eventFlag, CYU3P_WAIT_FOREVER);
  292.         if (txApiRetStatus == CY_U3P_SUCCESS)
  293.         {
  294.             if (eventFlag & CY_FX_GPIOAPP_GPIO_HIGH_EVENT)
  295.             {
  296.                 /* Print the status of the pin */
  297.                 /*CyU3PDebugPrint (4, "GPIO 45 is set to high\n");*/
  298.             }
  299.             else
  300.             {
  301.                 /* Print the status of the pin */
  302.                 /*CyU3PDebugPrint (4, "GPIO 45 is set to low\n");*/
  303.             }
  304.         }
  305.     }
  306. }
  307.  
  308. /* Application define function which creates the threads. */
  309. void
  310. CyFxApplicationDefine (
  311.         void)
  312. {
  313.     void *ptr = NULL;
  314.     uint32_t retThrdCreate = CY_U3P_SUCCESS;
  315.  
  316.     /* Allocate the memory for the threads */
  317.     ptr = CyU3PMemAlloc (CY_FX_GPIOAPP_THREAD_STACK);
  318.  
  319.     /* Create the thread for the application */
  320.     retThrdCreate = CyU3PThreadCreate (&gpioOutputThread,        /* GPIO Example App Thread structure */
  321.                           "21:GPIO_simple_output",               /* Thread ID and Thread name */
  322.                           GpioOutputThread_Entry,                /* GPIO Example App Thread Entry function */
  323.                           0,                                     /* No input parameter to thread */
  324.                           ptr,                                   /* Pointer to the allocated thread stack */
  325.                           CY_FX_GPIOAPP_THREAD_STACK,            /* Thread stack size */
  326.                           CY_FX_GPIOAPP_THREAD_PRIORITY,         /* Thread priority */
  327.                           CY_FX_GPIOAPP_THREAD_PRIORITY,         /* Pre-emption threshold for the thread. */
  328.                           CYU3P_NO_TIME_SLICE,                   /* No time slice for the application thread */
  329.                           CYU3P_AUTO_START                       /* Start the Thread immediately */
  330.                           );
  331.  
  332.     /* Check the return code */
  333.     if (retThrdCreate != 0)
  334.     {
  335.         /* Thread creation failed with the error code retThrdCreate */
  336.  
  337.         /* Add custom recovery or debug actions here */
  338.  
  339.         /* Application cannot continue */
  340.         /* Loop indefinitely */
  341.         while(1);
  342.     }
  343.  
  344.     /* Allocate the memory for the threads */
  345.     ptr = CyU3PMemAlloc (CY_FX_GPIOAPP_THREAD_STACK);
  346.  
  347.     /* Create the thread for the application */
  348.     retThrdCreate = CyU3PThreadCreate (&gpioInputThread,          /* GPIO Example App Thread structure */
  349.                           "22:GPIO_simple_input",                 /* Thread ID and Thread name */
  350.                           GpioInputThread_Entry,                  /* GPIO Example App Thread entry function */
  351.                           0,                                      /* No input parameter to thread */
  352.                           ptr,                                    /* Pointer to the allocated thread stack */
  353.                           CY_FX_GPIOAPP_THREAD_STACK,             /* Thread stack size */
  354.                           CY_FX_GPIOAPP_THREAD_PRIORITY,          /* Thread priority */
  355.                           CY_FX_GPIOAPP_THREAD_PRIORITY,          /* Pre-emption threshold for the thread */
  356.                           CYU3P_NO_TIME_SLICE,                    /* No time slice for the application thread */
  357.                           CYU3P_AUTO_START                        /* Start the Thread immediately */
  358.                           );
  359.  
  360.     /* Check the return code */
  361.     if (retThrdCreate != 0)
  362.     {
  363.         /* Thread creation failed with the error code retThrdCreate */
  364.  
  365.         /* Add custom recovery or debug actions here */
  366.  
  367.         /* Application cannot continue */
  368.         /* Loop indefinitely */
  369.         while(1);
  370.     }
  371.  
  372.     /* Create GPIO application event group */
  373.     retThrdCreate = CyU3PEventCreate(&glFxGpioAppEvent);
  374.     if (retThrdCreate != 0)
  375.     {
  376.         /* Event group creation failed with the error code retThrdCreate */
  377.  
  378.         /* Add custom recovery or debug actions here */
  379.  
  380.         /* Application cannot continue */
  381.         /* Loop indefinitely */
  382.         while(1);
  383.     }
  384. }
  385.  
  386. /*
  387.  * Main function
  388.  */
  389. int
  390. main (void)
  391. {
  392.     CyU3PIoMatrixConfig_t io_cfg;
  393.     CyU3PReturnStatus_t status = CY_U3P_SUCCESS;
  394.  
  395.     /* Initialize the device */
  396.     status = CyU3PDeviceInit (0);
  397.     if (status != CY_U3P_SUCCESS)
  398.     {
  399.         goto handle_fatal_error;
  400.     }
  401.  
  402.     /* Initialize the caches. Enable both Instruction and Data Caches. */
  403.     status = CyU3PDeviceCacheControl (CyTrue, CyTrue, CyTrue);
  404.     if (status != CY_U3P_SUCCESS)
  405.     {
  406.         goto handle_fatal_error;
  407.     }
  408.  
  409.     /* Configure the IO matrix for the device. On the FX3 DVK board,
  410.      * the COM port is connected to the IO(53:56). This means that
  411.      * either DQ32 mode should be selected or lppMode should be set
  412.      * to UART_ONLY. Here we are choosing UART_ONLY configuration. */
  413.     CyU3PMemSet ((uint8_t *)&io_cfg, 0, sizeof(io_cfg));
  414.     io_cfg.isDQ32Bit = CyFalse;
  415.     io_cfg.useUart   = CyFalse;
  416.     io_cfg.useI2C    = CyFalse;
  417.     io_cfg.useI2S    = CyFalse;
  418.     io_cfg.useSpi    = CyFalse;
  419.     io_cfg.lppMode   = CY_U3P_IO_MATRIX_LPP_DEFAULT;
  420.     /* GPIO 45 is used as input pin. GPIO 21 is also used but cannot
  421.      * be selected here as it is part of the GPIF IOs (CTL4). Since
  422.      * this IO is not used, it can be overridden to become a GPIO by
  423.      * invoking the CyU3PDeviceGpioOverride call. */
  424.     io_cfg.gpioSimpleEn[0]  = 0;
  425.     io_cfg.gpioSimpleEn[1]  = (1<<13) | (1<<19) | (1<<20)| (1<<21)| (1<<22)| (1<<23)| (1<<24)| (1<<25); //| (1<<22) | (1<<23) | (1<<24)| (1<<25); /* GPIO 45 */
  426.     io_cfg.gpioComplexEn[0] = 0;
  427.     io_cfg.gpioComplexEn[1] = 0;
  428.     status = CyU3PDeviceConfigureIOMatrix (&io_cfg);
  429.     if (status != CY_U3P_SUCCESS)
  430.     {
  431.         goto handle_fatal_error;
  432.     }
  433.  
  434.     /* This is a non returnable call for initializing the RTOS kernel */
  435.     CyU3PKernelEntry ();
  436.  
  437.     /* Dummy return to make the compiler happy */
  438.     return 0;
  439.  
  440. handle_fatal_error:
  441.     /* Cannot recover from this error. */
  442.     while (1);
  443. }
  444.  
  445. /* [ ] */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement