main.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (c) 2016, Alex Taradov <alex@taradov.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  20. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  21. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  22. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  23. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  24. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26. * POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. /*- Includes ----------------------------------------------------------------*/
  29. #include <stdlib.h>
  30. #include <stdint.h>
  31. #include <stdbool.h>
  32. #include <stdalign.h>
  33. #include <string.h>
  34. #include "samd21.h"
  35. #include "hal_gpio.h"
  36. #include "nvm_data.h"
  37. #include "usb.h"
  38. #include "dap.h"
  39. #include "dap_config.h"
  40. /*- Definitions -------------------------------------------------------------*/
  41. #define APP_EP_SEND 1
  42. #define APP_EP_RECV 2
  43. /*- Variables ---------------------------------------------------------------*/
  44. alignas(4) uint8_t app_request_buffer[DAP_CONFIG_PACKET_SIZE];
  45. alignas(4) uint8_t app_response_buffer[DAP_CONFIG_PACKET_SIZE];
  46. /*- Implementations ---------------------------------------------------------*/
  47. //-----------------------------------------------------------------------------
  48. static void sys_init(void)
  49. {
  50. uint32_t coarse, fine;
  51. SYSCTRL->OSC8M.bit.PRESC = 0;
  52. SYSCTRL->INTFLAG.reg = SYSCTRL_INTFLAG_BOD33RDY | SYSCTRL_INTFLAG_BOD33DET |
  53. SYSCTRL_INTFLAG_DFLLRDY;
  54. NVMCTRL->CTRLB.bit.RWS = 1;
  55. coarse = NVM_READ_CAL(NVM_DFLL48M_COARSE_CAL);
  56. fine = NVM_READ_CAL(NVM_DFLL48M_FINE_CAL);
  57. SYSCTRL->DFLLCTRL.reg = 0; // See Errata 9905
  58. while (0 == (SYSCTRL->PCLKSR.reg & SYSCTRL_PCLKSR_DFLLRDY));
  59. SYSCTRL->DFLLMUL.reg = SYSCTRL_DFLLMUL_MUL(48000);
  60. SYSCTRL->DFLLVAL.reg = SYSCTRL_DFLLVAL_COARSE(coarse) | SYSCTRL_DFLLVAL_FINE(fine);
  61. SYSCTRL->DFLLCTRL.reg = SYSCTRL_DFLLCTRL_ENABLE | SYSCTRL_DFLLCTRL_USBCRM |
  62. SYSCTRL_DFLLCTRL_MODE | SYSCTRL_DFLLCTRL_BPLCKC | SYSCTRL_DFLLCTRL_CCDIS |
  63. SYSCTRL_DFLLCTRL_STABLE;
  64. while (0 == (SYSCTRL->PCLKSR.reg & SYSCTRL_PCLKSR_DFLLRDY));
  65. GCLK->GENCTRL.reg = GCLK_GENCTRL_ID(0) | GCLK_GENCTRL_SRC(GCLK_SOURCE_DFLL48M) |
  66. GCLK_GENCTRL_RUNSTDBY | GCLK_GENCTRL_GENEN;
  67. while (GCLK->STATUS.reg & GCLK_STATUS_SYNCBUSY);
  68. }
  69. //-----------------------------------------------------------------------------
  70. void usb_send_callback(void)
  71. {
  72. }
  73. //-----------------------------------------------------------------------------
  74. void usb_recv_callback(void)
  75. {
  76. dap_process_request(app_request_buffer, sizeof(app_request_buffer),
  77. app_response_buffer, sizeof(app_response_buffer));
  78. usb_send(APP_EP_SEND, app_response_buffer, sizeof(app_response_buffer), usb_send_callback);
  79. usb_recv(APP_EP_RECV, app_request_buffer, sizeof(app_request_buffer), usb_recv_callback);
  80. }
  81. //-----------------------------------------------------------------------------
  82. void usb_configuration_callback(int config)
  83. {
  84. usb_recv(APP_EP_RECV, app_request_buffer, sizeof(app_request_buffer), usb_recv_callback);
  85. (void)config;
  86. }
  87. //-----------------------------------------------------------------------------
  88. int main(void)
  89. {
  90. sys_init();
  91. dap_init();
  92. usb_init();
  93. while (1);
  94. return 0;
  95. }