Advertisement
Guest User

K

a guest
Feb 20th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 KB | None | 0 0
  1. /*
  2. * The Clear BSD License
  3. * Copyright (c) 2015, Freescale Semiconductor, Inc.
  4. * Copyright 2016-2017 NXP
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without modification,
  8. * are permitted (subject to the limitations in the disclaimer below) provided
  9. * that the following conditions are met:
  10. *
  11. * o Redistributions of source code must retain the above copyright notice, this list
  12. * of conditions and the following disclaimer.
  13. *
  14. * o Redistributions in binary form must reproduce the above copyright notice, this
  15. * list of conditions and the following disclaimer in the documentation and/or
  16. * other materials provided with the distribution.
  17. *
  18. * o Neither the name of the copyright holder nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  25. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  27. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  28. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  30. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34.  
  35. #include "fsl_debug_console.h"
  36. #include "fsl_port.h"
  37. #include "fsl_gpio.h"
  38. #include "fsl_common.h"
  39. #include "board.h"
  40. #include "pin_mux.h"
  41. #include "clock_config.h"
  42. /*******************************************************************************
  43. * Definitions
  44. ******************************************************************************/
  45. #define BOARD_LED_GPIO BOARD_LED_RED_GPIO
  46. #define BOARD_LED_GPIO_PIN BOARD_LED_RED_GPIO_PIN
  47.  
  48. #define BOARD_SW_GPIO BOARD_SW3_GPIO
  49. #define BOARD_SW_PORT BOARD_SW3_PORT
  50. #define BOARD_SW_GPIO_PIN BOARD_SW3_GPIO_PIN
  51. #define BOARD_SW_IRQ BOARD_SW3_IRQ
  52. #define BOARD_SW_IRQ_HANDLER BOARD_SW3_IRQ_HANDLER
  53. #define BOARD_SW_NAME BOARD_SW3_NAME
  54.  
  55. /*******************************************************************************
  56. * Prototypes
  57. ******************************************************************************/
  58.  
  59. /*******************************************************************************
  60. * Variables
  61. ******************************************************************************/
  62. /* Whether the SW button is pressed */
  63. volatile bool g_ButtonPress = false;
  64.  
  65. /*******************************************************************************
  66. * Code
  67. ******************************************************************************/
  68. /*!
  69. * @brief Interrupt service fuction of switch.
  70. *
  71. * This function toggles the LED
  72. */
  73. void BOARD_SW_IRQ_HANDLER(void)
  74. {
  75. /* Clear external interrupt flag. */
  76. GPIO_PortClearInterruptFlags(BOARD_SW_GPIO, 1U << BOARD_SW_GPIO_PIN);
  77. /* Change state of button. */
  78. g_ButtonPress = true;
  79. /* Add for ARM errata 838869, affects Cortex-M4, Cortex-M4F Store immediate overlapping
  80. exception return operation might vector to incorrect interrupt */
  81. #if defined __CORTEX_M && (__CORTEX_M == 4U)
  82. __DSB();
  83. #endif
  84. }
  85.  
  86. /*!
  87. * @brief Main function
  88. */
  89. int main(void)
  90. {
  91. /* Define the init structure for the input switch pin */
  92. gpio_pin_config_t sw_config = {
  93. kGPIO_DigitalInput, 0,
  94. };
  95.  
  96. /* Define the init structure for the output LED pin */
  97. gpio_pin_config_t led_config = {
  98. kGPIO_DigitalOutput, 0,
  99. };
  100.  
  101. BOARD_InitPins();
  102. BOARD_BootClockRUN();
  103. BOARD_InitDebugConsole();
  104.  
  105. /* Print a note to terminal. */
  106. PRINTF("\r\n GPIO Driver example\r\n");
  107. PRINTF("\r\n Press %s to turn on/off a LED \r\n", BOARD_SW_NAME);
  108.  
  109. /* Init input switch GPIO. */
  110. PORT_SetPinInterruptConfig(BOARD_SW_PORT, BOARD_SW_GPIO_PIN, kPORT_InterruptFallingEdge);
  111. EnableIRQ(BOARD_SW_IRQ);
  112. GPIO_PinInit(BOARD_SW_GPIO, BOARD_SW_GPIO_PIN, &sw_config);
  113.  
  114. /* Init output LED GPIO. */
  115. GPIO_PinInit(BOARD_LED_GPIO, BOARD_LED_GPIO_PIN, &led_config);
  116.  
  117. while (1)
  118. {
  119. if (g_ButtonPress)
  120. {
  121. PRINTF(" %s is pressed \r\n", BOARD_SW_NAME);
  122. /* Toggle LED. */
  123. GPIO_PortToggle(BOARD_LED_GPIO, 1U << BOARD_LED_GPIO_PIN);
  124. /* Reset state of button. */
  125. g_ButtonPress = false;
  126. }
  127. }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement