clks.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. uint32_t CLK_get_ms(void);
  65. uint64_t CLK_get_us(void);
  66. void CLK_delay_us(uint16_t usec);
  67. void CLK_delay_ms(uint64_t msec);
  68. uint32_t CLK_set_spi_freq(uint8_t sercomn, uint32_t freq);
  69. uint32_t CLK_set_i2c0_freq(uint8_t sercomn, uint32_t freq);
  70. uint32_t CLK_set_i2c1_freq(uint8_t sercomn, uint32_t freq);
  71. void CLK_init(void);
  72. #endif // _CLKS_H_