clks.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. Copyright 2018 Massdrop Inc.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #ifndef _CLKS_H_
  15. #define _CLKS_H_
  16. #ifndef MD_BOOTLOADER
  17. // From keyboard
  18. # include "config_led.h"
  19. # include "config.h"
  20. #endif // MD_BOOTLOADER
  21. #define PLL_RATIO 47 // mcu frequency ((X+1)MHz)
  22. #define FREQ_DFLL_DEFAULT 48000000 // DFLL frequency / usb clock
  23. #define FREQ_SPI_DEFAULT 1000000 // spi to 595 shift regs
  24. #define FREQ_I2C0_DEFAULT 100000 // i2c to hub
  25. #define FREQ_I2C1_DEFAULT I2C_HZ // i2c to LED drivers
  26. #define FREQ_TC45_DEFAULT 1000000 // 1 usec resolution
  27. // I2C1 Set ~Result PWM Time (2x Drivers)
  28. // 1000000 1090000
  29. // 900000 1000000 3.82ms
  30. // 800000 860000
  31. // 700000 750000
  32. // 600000 630000
  33. // 580000 615000 6.08ms
  34. // 500000 522000
  35. #define FREQ_XOSC0 16000000
  36. #define CHAN_SERCOM_SPI 2 // shift regs
  37. #define CHAN_SERCOM_I2C0 0 // hub
  38. #define CHAN_SERCOM_I2C1 1 // led drivers
  39. #define CHAN_SERCOM_UART 3 // debug util
  40. // Generator clock channels
  41. #define GEN_DPLL0 0
  42. #define GEN_OSC0 1
  43. #define GEN_TC45 2
  44. #define SERCOM_COUNT 5
  45. #define GCLK_COUNT 12
  46. typedef struct clk_s {
  47. uint32_t freq_dfll;
  48. uint32_t freq_dpll[2];
  49. uint32_t freq_sercom[SERCOM_COUNT];
  50. uint32_t freq_gclk[GCLK_COUNT];
  51. uint32_t freq_xosc0;
  52. uint32_t freq_spi;
  53. uint32_t freq_i2c0;
  54. uint32_t freq_i2c1;
  55. uint32_t freq_uart;
  56. uint32_t freq_adc0;
  57. } clk_t;
  58. extern volatile clk_t system_clks;
  59. extern volatile uint64_t ms_clk;
  60. void CLK_oscctrl_init(void);
  61. void CLK_reset_time(void);
  62. uint32_t CLK_set_gclk_freq(uint8_t gclkn, uint32_t freq);
  63. uint32_t CLK_enable_timebase(void);
  64. uint64_t timer_read64(void);
  65. void CLK_delay_us(uint32_t usec);
  66. void CLK_delay_ms(uint64_t msec);
  67. uint32_t CLK_set_spi_freq(uint8_t sercomn, uint32_t freq);
  68. uint32_t CLK_set_i2c0_freq(uint8_t sercomn, uint32_t freq);
  69. uint32_t CLK_set_i2c1_freq(uint8_t sercomn, uint32_t freq);
  70. void CLK_init(void);
  71. #endif // _CLKS_H_