board_spresense.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * The MIT License (MIT)
  3. *
  4. * Copyright 2019 Sony Semiconductor Solutions Corporation
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. *
  24. * This file is part of the TinyUSB stack.
  25. */
  26. #include <sys/boardctl.h>
  27. #include <nuttx/arch.h>
  28. #include <arch/board/board.h>
  29. #include <arch/chip/pin.h>
  30. #include "bsp/board.h"
  31. /*------------------------------------------------------------------*/
  32. /* MACRO TYPEDEF CONSTANT ENUM
  33. *------------------------------------------------------------------*/
  34. #define LED_PIN PIN_I2S1_BCK
  35. #define BUTTON_PIN PIN_HIF_IRQ_OUT
  36. // Initialize on-board peripherals : led, button, uart and USB
  37. void board_init(void)
  38. {
  39. boardctl(BOARDIOC_INIT, 0);
  40. board_gpio_write(PIN_I2S1_BCK, -1);
  41. board_gpio_config(PIN_I2S1_BCK, 0, false, true, PIN_FLOAT);
  42. board_gpio_write(PIN_HIF_IRQ_OUT, -1);
  43. board_gpio_config(PIN_HIF_IRQ_OUT, 0, true, true, PIN_FLOAT);
  44. };
  45. //--------------------------------------------------------------------+
  46. // Board porting API
  47. //--------------------------------------------------------------------+
  48. // Turn LED on or off
  49. void board_led_write(bool state)
  50. {
  51. board_gpio_write(LED_PIN, state);
  52. }
  53. // Get the current state of button
  54. // a '1' means active (pressed), a '0' means inactive.
  55. uint32_t board_button_read(void)
  56. {
  57. if (board_gpio_read(BUTTON_PIN))
  58. {
  59. return 0;
  60. }
  61. return 1;
  62. }
  63. // Get characters from UART
  64. int board_uart_read(uint8_t *buf, int len)
  65. {
  66. int r = read(0, buf, len);
  67. return r;
  68. }
  69. // Send characters to UART
  70. int board_uart_write(void const *buf, int len)
  71. {
  72. int r = write(1, buf, len);
  73. return r;
  74. }
  75. // Get current milliseconds
  76. uint32_t board_millis(void)
  77. {
  78. struct timespec tp;
  79. /* Wait until RTC is available */
  80. while (g_rtc_enabled == false);
  81. if (clock_gettime(CLOCK_MONOTONIC, &tp))
  82. {
  83. return 0;
  84. }
  85. return (((uint64_t)tp.tv_sec) * 1000 + tp.tv_nsec / 1000000);
  86. }