You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

186 line
5.0 KiB

  1. /**
  2. *
  3. * \file
  4. *
  5. * \brief This module contains SAMD21 BSP APIs implementation.
  6. *
  7. * Copyright (c) 2017-2018 Microchip Technology Inc. and its subsidiaries.
  8. *
  9. * \asf_license_start
  10. *
  11. * \page License
  12. *
  13. * Subject to your compliance with these terms, you may use Microchip
  14. * software and any derivatives exclusively with Microchip products.
  15. * It is your responsibility to comply with third party license terms applicable
  16. * to your use of third party software (including open source software) that
  17. * may accompany Microchip software.
  18. *
  19. * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES,
  20. * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE,
  21. * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY,
  22. * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE
  23. * LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL
  24. * LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE
  25. * SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE
  26. * POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT
  27. * ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY
  28. * RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
  29. * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
  30. *
  31. * \asf_license_stop
  32. *
  33. */
  34. #include "bsp/include/nm_bsp.h"
  35. #include "bsp/include/nm_bsp_internal.h"
  36. #include "common/include/nm_common.h"
  37. #include "extint.h"
  38. #include "port.h"
  39. #ifdef TICK_IF
  40. #include "tick_if.h"
  41. #else
  42. #include "delay.h"
  43. #endif
  44. #include "system_interrupt.h"
  45. #include "conf_winc.h"
  46. static tpfNmBspIsr gpfIsr;
  47. static void chip_isr(void)
  48. {
  49. if (gpfIsr) {
  50. gpfIsr();
  51. }
  52. }
  53. /*
  54. * @fn init_chip_pins
  55. * @brief Initialize reset, chip enable and wake pin
  56. */
  57. static void init_chip_pins(void)
  58. {
  59. struct port_config pin_conf;
  60. port_get_config_defaults(&pin_conf);
  61. /* Configure control pins as output. */
  62. pin_conf.direction = PORT_PIN_DIR_OUTPUT;
  63. port_pin_set_config(CONF_WINC_PIN_RESET, &pin_conf);
  64. port_pin_set_config(CONF_WINC_PIN_CHIP_ENABLE, &pin_conf);
  65. port_pin_set_config(CONF_WINC_PIN_WAKE, &pin_conf);
  66. port_pin_set_output_level(CONF_WINC_PIN_CHIP_ENABLE, false);
  67. port_pin_set_output_level(CONF_WINC_PIN_RESET, false);
  68. }
  69. /*
  70. * @fn nm_bsp_init
  71. * @brief Initialize BSP
  72. * @return 0 in case of success and -1 in case of failure
  73. */
  74. sint8 nm_bsp_init(void)
  75. {
  76. gpfIsr = NULL;
  77. /* Initialize chip IOs. */
  78. init_chip_pins();
  79. #ifndef CONF_WINC_USE_SPI
  80. nm_bsp_reset();
  81. #endif
  82. /* Make sure a 1ms Systick is configured. */
  83. if (!(SysTick->CTRL & SysTick_CTRL_ENABLE_Msk && SysTick->CTRL & SysTick_CTRL_TICKINT_Msk)) {
  84. delay_init();
  85. }
  86. system_interrupt_enable_global();
  87. return M2M_SUCCESS;
  88. }
  89. /**
  90. * @fn nm_bsp_deinit
  91. * @brief De-iInitialize BSP
  92. * @return 0 in case of success and -1 in case of failure
  93. */
  94. sint8 nm_bsp_deinit(void)
  95. {
  96. struct port_config pin_conf;
  97. port_get_config_defaults(&pin_conf);
  98. /* Configure control pins as input no pull up. */
  99. pin_conf.direction = PORT_PIN_DIR_INPUT;
  100. pin_conf.input_pull = PORT_PIN_PULL_NONE;
  101. port_pin_set_output_level(CONF_WINC_PIN_CHIP_ENABLE, false);
  102. port_pin_set_output_level(CONF_WINC_PIN_RESET, false);
  103. port_pin_set_config(CONF_WINC_SPI_INT_PIN, &pin_conf);
  104. return M2M_SUCCESS;
  105. }
  106. /**
  107. * @fn nm_bsp_reset
  108. * @brief Reset NMC1500 SoC by setting CHIP_EN and RESET_N signals low,
  109. * CHIP_EN high then RESET_N high.
  110. * CHIP_EN and RESET_N are actually already set low by nm_bsp_init or nm_bsp_deinit().
  111. * Here we just need to set them high.
  112. */
  113. void nm_bsp_reset(void)
  114. {
  115. port_pin_set_output_level(CONF_WINC_PIN_CHIP_ENABLE, true);
  116. nm_bsp_sleep(5);
  117. port_pin_set_output_level(CONF_WINC_PIN_RESET, true);
  118. }
  119. /*
  120. * @fn nm_bsp_sleep
  121. * @brief Sleep in units of mSec
  122. * @param[IN] u32TimeMsec
  123. * Time in milliseconds
  124. */
  125. void nm_bsp_sleep(uint32 u32TimeMsec)
  126. {
  127. while (u32TimeMsec--) {
  128. delay_ms(1);
  129. }
  130. }
  131. /*
  132. * @fn nm_bsp_register_isr
  133. * @brief Register interrupt service routine
  134. * @param[IN] pfIsr
  135. * Pointer to ISR handler
  136. */
  137. void nm_bsp_register_isr(tpfNmBspIsr pfIsr)
  138. {
  139. struct extint_chan_conf config_extint_chan;
  140. gpfIsr = pfIsr;
  141. extint_chan_get_config_defaults(&config_extint_chan);
  142. config_extint_chan.gpio_pin = CONF_WINC_SPI_INT_PIN;
  143. config_extint_chan.gpio_pin_mux = CONF_WINC_SPI_INT_MUX;
  144. config_extint_chan.gpio_pin_pull = EXTINT_PULL_UP;
  145. config_extint_chan.detection_criteria = EXTINT_DETECT_FALLING;
  146. extint_chan_set_config(CONF_WINC_SPI_INT_EIC, &config_extint_chan);
  147. extint_register_callback(chip_isr, CONF_WINC_SPI_INT_EIC,
  148. EXTINT_CALLBACK_TYPE_DETECT);
  149. extint_chan_enable_callback(CONF_WINC_SPI_INT_EIC,
  150. EXTINT_CALLBACK_TYPE_DETECT);
  151. }
  152. /*
  153. * @fn nm_bsp_interrupt_ctrl
  154. * @brief Enable/Disable interrupts
  155. * @param[IN] u8Enable
  156. * '0' disable interrupts. '1' enable interrupts
  157. */
  158. void nm_bsp_interrupt_ctrl(uint8 u8Enable)
  159. {
  160. if (u8Enable) {
  161. extint_chan_enable_callback(CONF_WINC_SPI_INT_EIC,
  162. EXTINT_CALLBACK_TYPE_DETECT);
  163. } else {
  164. extint_chan_disable_callback(CONF_WINC_SPI_INT_EIC,
  165. EXTINT_CALLBACK_TYPE_DETECT);
  166. }
  167. }