periph_cpu_common.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016 Freie Universität Berlin
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
9 #pragma once
10 
23 #include "cpu.h"
24 #include "exti_config.h"
25 #include "timer_config.h"
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
34 #define CPUID_LEN (16U)
35 
40 #define PERIPH_SPI_NEEDS_INIT_CS
41 #define PERIPH_SPI_NEEDS_TRANSFER_BYTE
42 #ifndef MODULE_PERIPH_DMA
43 # define PERIPH_SPI_NEEDS_TRANSFER_REG
44 # define PERIPH_SPI_NEEDS_TRANSFER_REGS
45 #endif
52 #define PERIPH_I2C_NEED_READ_REG
53 #define PERIPH_I2C_NEED_READ_REGS
54 #define PERIPH_I2C_NEED_WRITE_REG
55 #define PERIPH_I2C_NEED_WRITE_REGS
61 #define PERIPH_I2C_MAX_BYTES_PER_FRAME 256
62 
67 #define HAVE_GPIO_T
68 typedef uint32_t gpio_t;
74 #define GPIO_UNDEF (0xffffffff)
75 
80 #ifdef MODULE_PERIPH_GPIO_FAST_READ
81 # ifdef PORT_IOBUS_SEC
82 # define GPIO_PIN(x, y) (((gpio_t)(&PORT_IOBUS_SEC->Group[x])) | y)
83 # else /* Use IOBUS access when available */
84 # define GPIO_PIN(x, y) (((gpio_t)(&PORT_IOBUS->Group[x])) | y)
85 # endif /* PORT_IOBUS_SEC */
86 #else
87 # ifdef PORT_SEC
88 # define GPIO_PIN(x, y) (((gpio_t)(&PORT_SEC->Group[x])) | y)
89 # else
90 # define GPIO_PIN(x, y) (((gpio_t)(&PORT->Group[x])) | y)
91 # endif /* PORT_IOBUS_SEC */
92 #endif
93 
97 enum {
98  PA = 0,
99  PB = 1,
100  PC = 2,
101  PD = 3,
102 };
103 
112 #define GPIO_MODE(pr, ie, pe) (pr | (ie << 1) | (pe << 2))
113 
114 #ifndef DOXYGEN
118 # define HAVE_GPIO_MODE_T
119 typedef enum {
120  GPIO_IN = GPIO_MODE(0, 1, 0),
121  GPIO_IN_PD = GPIO_MODE(0, 1, 1),
122  GPIO_IN_PU = GPIO_MODE(1, 1, 1),
123  GPIO_OUT = GPIO_MODE(0, 0, 0),
124  GPIO_OD = 0xfe,
125  GPIO_OD_PU = 0xff
126 } gpio_mode_t;
127 
128 # define HAVE_GPIO_SLEW_T
129 typedef enum {
130  GPIO_SLEW_SLOWEST = 0,
131  GPIO_SLEW_SLOW = 0,
132  GPIO_SLEW_FAST = 0,
133  GPIO_SLEW_FASTEST = 0,
134 } gpio_slew_t;
135 
136 # define HAVE_GPIO_PULL_STRENGTH_T
137 typedef enum {
138  GPIO_PULL_WEAKEST = 0,
139  GPIO_PULL_WEAK = 0,
140  GPIO_PULL_STRONG = 0,
143 
144 # define HAVE_GPIO_DRIVE_STRENGTH_T
145 typedef enum {
146  GPIO_DRIVE_WEAKEST = 0,
147  GPIO_DRIVE_WEAK = 0,
148  GPIO_DRIVE_STRONG = 1,
151 
152 # define HAVE_GPIO_PULL_T
153 typedef enum {
155  GPIO_PULL_UP,
158 } gpio_pull_t;
159 
160 # define HAVE_GPIO_STATE_T
161 typedef enum {
165  GPIO_INPUT,
168 } gpio_state_t;
169 
170 # define HAVE_GPIO_IRQ_TRIG_T
171 typedef enum {
172  GPIO_TRIGGER_EDGE_RISING = EIC_CONFIG_SENSE0_RISE_Val,
173  GPIO_TRIGGER_EDGE_FALLING = EIC_CONFIG_SENSE0_FALL_Val,
174  GPIO_TRIGGER_EDGE_BOTH = EIC_CONFIG_SENSE0_BOTH_Val,
175  GPIO_TRIGGER_LEVEL_HIGH = EIC_CONFIG_SENSE0_HIGH_Val,
176  GPIO_TRIGGER_LEVEL_LOW = EIC_CONFIG_SENSE0_LOW_Val,
178 
179 # define HAVE_GPIO_CONF_T
180 typedef union gpio_conf_sam0 gpio_conf_t;
181 
186 # define HAVE_GPIO_FLANK_T
187 typedef enum {
188  GPIO_FALLING = 2,
189  GPIO_RISING = 1,
190  GPIO_BOTH = 3
191 } gpio_flank_t;
193 #endif /* ndef DOXYGEN */
194 
200  uint8_t bits;
201  struct {
233  bool initial_value : 1;
234  uint8_t : 1;
235  };
236 };
237 
241 #ifndef SAM_MUX_T
242 typedef enum {
243  GPIO_MUX_A = 0x0,
244  GPIO_MUX_B = 0x1,
245  GPIO_MUX_C = 0x2,
246  GPIO_MUX_D = 0x3,
247  GPIO_MUX_E = 0x4,
248  GPIO_MUX_F = 0x5,
249  GPIO_MUX_G = 0x6,
250  GPIO_MUX_H = 0x7,
251  GPIO_MUX_I = 0x8,
252  GPIO_MUX_J = 0x9,
253  GPIO_MUX_K = 0xa,
254  GPIO_MUX_L = 0xb,
255  GPIO_MUX_M = 0xc,
256  GPIO_MUX_N = 0xd,
258 } gpio_mux_t;
259 #endif
260 
264 typedef enum {
269 } uart_rxpad_t;
270 
274 typedef enum {
279 } uart_txpad_t;
280 
284 typedef enum {
289 } uart_flag_t;
290 
291 #ifndef DOXYGEN
299 # define HAVE_UART_DATA_BITS_T
300 typedef enum {
301  UART_DATA_BITS_5 = 0x5,
302  UART_DATA_BITS_6 = 0x6,
303  UART_DATA_BITS_7 = 0x7,
304  UART_DATA_BITS_8 = 0x0,
312 # define uart_pin_rx(dev) uart_config[dev].rx_pin
313 # define uart_pin_tx(dev) uart_config[dev].tx_pin
316 #endif /* ndef DOXYGEN */
317 
321 #ifndef UART_TXBUF_SIZE
322 # define UART_TXBUF_SIZE (64)
323 #endif
324 
338 typedef struct {
339  SercomUsart *dev;
340  gpio_t rx_pin;
341  gpio_t tx_pin;
342 #ifdef MODULE_PERIPH_UART_HW_FC
343  gpio_t rts_pin;
344  gpio_t cts_pin;
345 #endif
350  uint8_t gclk_src;
351 } uart_conf_t;
352 
353 enum {
356 };
357 
361 typedef struct {
362  union {
363 #ifdef REV_TC
364  Tc *tc;
365 #endif
366 #ifdef REV_TCC
367  Tcc *tcc;
368 #endif
369  } dev;
370 #ifdef MCLK
371  volatile uint32_t *mclk;
372  uint32_t mclk_mask;
373 #else
374  uint32_t pm_mask;
375 #endif
376  uint16_t gclk_id;
377  uint8_t type;
378 } tc_tcc_cfg_t;
379 
383 #ifdef MCLK
384 # define TC_CONFIG(tim) { \
385  .dev = {.tc = tim}, \
386  .mclk = MCLK_ ## tim, \
387  .mclk_mask = MCLK_ ## tim ## _MASK, \
388  .gclk_id = tim ## _GCLK_ID, \
389  .type = TIMER_TYPE_TC, }
390 #else
391 # define TC_CONFIG(tim) { \
392  .dev = {.tc = tim}, \
393  .pm_mask = PM_APBCMASK_ ## tim, \
394  .gclk_id = tim ## _GCLK_ID, \
395  .type = TIMER_TYPE_TC, }
396 #endif
397 
401 #ifdef MCLK
402 # define TCC_CONFIG(tim) { \
403  .dev = {.tcc = tim}, \
404  .mclk = MCLK_ ## tim, \
405  .mclk_mask = MCLK_ ## tim ## _MASK, \
406  .gclk_id = tim ## _GCLK_ID, \
407  .type = TIMER_TYPE_TCC, }
408 #else
409 # define TCC_CONFIG(tim) { \
410  .dev = {.tcc = tim}, \
411  .pm_mask = PM_APBCMASK_ ## tim, \
412  .gclk_id = tim ## _GCLK_ID, \
413  .type = TIMER_TYPE_TCC, }
414 #endif
415 
419 typedef struct {
420  gpio_t pin;
422  uint8_t chan;
424 
428 typedef struct {
431  uint8_t chan_numof;
432  uint8_t gclk_src;
433 } pwm_conf_t;
434 
438 typedef enum {
443 } spi_misopad_t;
444 
448 typedef enum {
453 } spi_mosipad_t;
454 
455 #ifndef DOXYGEN
460 # define HAVE_SPI_MODE_T
461 typedef enum {
462  SPI_MODE_0 = 0x0,
463  SPI_MODE_1 = 0x1,
464  SPI_MODE_2 = 0x2,
465  SPI_MODE_3 = 0x3
466 } spi_mode_t;
473 # define HAVE_SPI_CLK_T
474 typedef enum {
475  SPI_CLK_100KHZ = 100000U,
476  SPI_CLK_400KHZ = 400000U,
477  SPI_CLK_1MHZ = 1000000U,
478  SPI_CLK_5MHZ = 5000000U,
479  SPI_CLK_10MHZ = 10000000U
480 } spi_clk_t;
487 # define spi_pin_mosi(dev) spi_config[dev].mosi_pin
488 # define spi_pin_miso(dev) spi_config[dev].miso_pin
489 # define spi_pin_clk(dev) spi_config[dev].clk_pin
492 #endif /* ndef DOXYGEN */
493 
497 typedef struct {
498  void *dev;
499  gpio_t miso_pin;
500  gpio_t mosi_pin;
501  gpio_t clk_pin;
507  uint8_t gclk_src;
508 #ifdef MODULE_PERIPH_DMA
509  uint8_t tx_trigger;
510  uint8_t rx_trigger;
511 #endif
512 } spi_conf_t;
518 typedef enum {
521 } i2c_flag_t;
522 
523 #ifndef DOXYGEN
528 # define HAVE_I2C_SPEED_T
529 typedef enum {
530  I2C_SPEED_LOW = 10000U,
531  I2C_SPEED_NORMAL = 100000U,
532  I2C_SPEED_FAST = 400000U,
533  I2C_SPEED_FAST_PLUS = 1000000U,
534  I2C_SPEED_HIGH = 3400000U,
535 } i2c_speed_t;
542 # define i2c_pin_sda(dev) i2c_config[dev].sda_pin
543 # define i2c_pin_scl(dev) i2c_config[dev].scl_pin
546 #endif /* ndef DOXYGEN */
547 
560 typedef struct {
561  SercomI2cm *dev;
562  i2c_speed_t speed;
563  gpio_t scl_pin;
564  gpio_t sda_pin;
566  uint8_t gclk_src;
567  uint8_t flags;
568 } i2c_conf_t;
569 
573 typedef struct {
574  Tc *dev;
576 #ifdef MCLK
577  volatile uint32_t *mclk;
578  uint32_t mclk_mask;
579  uint16_t gclk_id;
580 #else
581  uint32_t pm_mask;
582  uint16_t gclk_ctrl;
583 #endif
584  uint8_t gclk_src;
585  uint16_t flags;
586 } tc32_conf_t;
587 
591 #define TIMER_CHANNEL_NUMOF (2)
592 
599 void gpio_init_mux(gpio_t pin, gpio_mux_t mux);
600 
601 #ifdef PM_SLEEPCFG_SLEEPMODE_OFF
602 
606 # define PROVIDES_PM_OFF
607 
611 # define PROVIDES_PM_LAYERED_OFF
612 
613 #endif /* PM_SLEEPCFG_SLEEPMODE_OFF */
614 
620 void gpio_pm_cb_enter(int deep);
621 
627 void gpio_pm_cb_leave(int deep);
628 
634 void cpu_pm_cb_enter(int deep);
635 
641 void cpu_pm_cb_leave(int deep);
642 
648 static inline void sam0_cortexm_sleep(int deep)
649 {
650 #ifdef MODULE_PERIPH_GPIO
651  gpio_pm_cb_enter(deep);
652 #endif
653 
654  cpu_pm_cb_enter(deep);
655 
656  cortexm_sleep(deep);
657 
658  cpu_pm_cb_leave(deep);
659 
660 #ifdef MODULE_PERIPH_GPIO
661  gpio_pm_cb_leave(deep);
662 #endif
663 }
664 
670 void gpio_disable_mux(gpio_t pin);
671 
675 typedef enum {
680 
696 {
697 #ifdef REG_SUPC_VREG
698  if (src == SAM0_VREG_BUCK) {
699  SUPC->VREG.reg |= (1 << SUPC_VREG_SEL_Pos);
700  }
701  else {
702  SUPC->VREG.reg &= ~(1 << SUPC_VREG_SEL_Pos);
703  }
704  while (!(SUPC->STATUS.reg & SUPC_STATUS_VREGRDY)) {}
705 #else
706  (void) src;
707  assert(0);
708 #endif
709 }
710 
718 uint32_t sam0_gclk_freq(uint8_t id);
719 
725 void sam0_gclk_enable(uint8_t id);
726 
734 static inline uint8_t sercom_id(const void *sercom)
735 {
736 #ifdef SERCOM0
737  if (sercom == SERCOM0) {
738  return 0;
739  }
740 #endif
741 #ifdef SERCOM1
742  if (sercom == SERCOM1) {
743  return 1;
744  }
745 #endif
746 #ifdef SERCOM2
747  if (sercom == SERCOM2) {
748  return 2;
749  }
750 #endif
751 #ifdef SERCOM3
752  if (sercom == SERCOM3) {
753  return 3;
754  }
755 #endif
756 #ifdef SERCOM4
757  if (sercom == SERCOM4) {
758  return 4;
759  }
760 #endif
761 #ifdef SERCOM5
762  if (sercom == SERCOM5) {
763  return 5;
764  }
765 #endif
766 #ifdef SERCOM6
767  if (sercom == SERCOM6) {
768  return 6;
769  }
770 #endif
771 #ifdef SERCOM7
772  if (sercom == SERCOM7) {
773  return 7;
774  }
775 #endif
776 
777  /* should not be reached, so fail with assert */
778  assert(false);
779 
780  return SERCOM_INST_NUM;
781 }
782 
788 static inline void sercom_clk_en(void *sercom)
789 {
790  const uint8_t id = sercom_id(sercom);
791 #if defined(CPU_COMMON_SAMD21)
792  PM->APBCMASK.reg |= (PM_APBCMASK_SERCOM0 << id);
793 #elif defined (CPU_COMMON_SAMD5X)
794  if (id < 2) {
795  MCLK->APBAMASK.reg |= (1 << (id + 12));
796  } else if (id < 4) {
797  MCLK->APBBMASK.reg |= (1 << (id + 7));
798  } else {
799  MCLK->APBDMASK.reg |= (1 << (id - 4));
800  }
801 #else
802  if (id < 5) {
803  MCLK->APBCMASK.reg |= (MCLK_APBCMASK_SERCOM0 << id);
804  }
805 # if defined(CPU_COMMON_SAML21)
806  else {
807  MCLK->APBDMASK.reg |= (MCLK_APBDMASK_SERCOM5);
808  }
809 # endif /* CPU_COMMON_SAML21 */
810 #endif
811 }
812 
818 static inline void sercom_clk_dis(void *sercom)
819 {
820  const uint8_t id = sercom_id(sercom);
821 #if defined(CPU_COMMON_SAMD21)
822  PM->APBCMASK.reg &= ~(PM_APBCMASK_SERCOM0 << id);
823 #elif defined (CPU_COMMON_SAMD5X)
824  if (id < 2) {
825  MCLK->APBAMASK.reg &= ~(1 << (id + 12));
826  } else if (id < 4) {
827  MCLK->APBBMASK.reg &= ~(1 << (id + 7));
828  } else {
829  MCLK->APBDMASK.reg &= ~(1 << (id - 4));
830  }
831 #else
832  if (id < 5) {
833  MCLK->APBCMASK.reg &= ~(MCLK_APBCMASK_SERCOM0 << id);
834  }
835 # if defined (CPU_COMMON_SAML21)
836  else {
837  MCLK->APBDMASK.reg &= ~(MCLK_APBDMASK_SERCOM5);
838  }
839 # endif /* CPU_COMMON_SAML21 */
840 #endif
841 }
842 
843 #ifdef CPU_COMMON_SAMD5X
844 static inline uint8_t _sercom_gclk_id_core(uint8_t sercom_id) {
845  if (sercom_id < 2) {
846  return sercom_id + 7;
847  }
848  else if (sercom_id < 4) {
849  return sercom_id + 21;
850  }
851  else {
852  return sercom_id + 30;
853  }
854 }
855 #endif
856 
863 static inline void sercom_set_gen(void *sercom, uint8_t gclk)
864 {
865  const uint8_t id = sercom_id(sercom);
866  sam0_gclk_enable(gclk);
867 #if defined(CPU_COMMON_SAMD21)
868  GCLK->CLKCTRL.reg = (GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN(gclk) |
869  (SERCOM0_GCLK_ID_CORE + id));
870  while (GCLK->STATUS.reg & GCLK_STATUS_SYNCBUSY) {}
871 #elif defined(CPU_COMMON_SAMD5X)
872  GCLK->PCHCTRL[_sercom_gclk_id_core(id)].reg = (GCLK_PCHCTRL_CHEN | GCLK_PCHCTRL_GEN(gclk));
873 #else
874  if (id < 5) {
875  GCLK->PCHCTRL[SERCOM0_GCLK_ID_CORE + id].reg = (GCLK_PCHCTRL_CHEN | GCLK_PCHCTRL_GEN(gclk));
876  }
877 # if defined(CPU_COMMON_SAML21)
878  else {
879  GCLK->PCHCTRL[SERCOM5_GCLK_ID_CORE].reg = (GCLK_PCHCTRL_CHEN | GCLK_PCHCTRL_GEN(gclk));
880  }
881 # endif /* CPU_COMMON_SAML21 */
882 #endif
883 }
884 
888 static inline bool cpu_woke_from_backup(void)
889 {
890 #ifdef RSTC_RCAUSE_BACKUP
891  return RSTC->RCAUSE.reg & RSTC_RCAUSE_BACKUP;
892 #else
893  return false;
894 #endif
895 }
896 
900 typedef struct {
901  uint32_t inputctrl;
902 #ifdef ADC0
903  Adc *dev;
904 #endif
906 
911 #ifndef ADC_INPUTCTRL_DIFFMODE
912 # define ADC_INPUTCTRL_DIFFMODE (1 << 7)
913 #endif
914 
918 #define ADC_REFSEL_AREFA_PIN GPIO_PIN(PA, 3)
919 
923 #define ADC_REFSEL_AREFB_PIN GPIO_PIN(PA, 4)
924 
925 #if defined(ADC_REFCTRL_REFSEL_AREFC) || DOXYGEN
929 # define ADC_REFSEL_AREFC_PIN GPIO_PIN(PA, 6)
930 #endif
931 
932 #ifndef DOXYGEN
933 # define HAVE_ADC_RES_T
934 typedef enum {
935  ADC_RES_6BIT = 0xff,
936 # if defined(ADC_CTRLB_RESSEL)
937  ADC_RES_8BIT = ADC_CTRLB_RESSEL_8BIT_Val,
938  ADC_RES_10BIT = ADC_CTRLB_RESSEL_10BIT_Val,
939  ADC_RES_12BIT = ADC_CTRLB_RESSEL_12BIT_Val,
940 # elif defined(ADC_CTRLC_RESSEL)
941  ADC_RES_8BIT = ADC_CTRLC_RESSEL_8BIT_Val,
942  ADC_RES_10BIT = ADC_CTRLC_RESSEL_10BIT_Val,
943  ADC_RES_12BIT = ADC_CTRLC_RESSEL_12BIT_Val,
944 # endif
945  ADC_RES_16BIT_2SAMPL = ( 0x1 << 2) | 0x1,
946  ADC_RES_16BIT_4SAMPL = ( 0x2 << 2) | 0x1,
947  ADC_RES_16BIT_8SAMPL = ( 0x3 << 2) | 0x1,
948  ADC_RES_16BIT_16SAMPL = ( 0x4 << 2) | 0x1,
949  ADC_RES_16BIT_32SAMPL = ( 0x5 << 2) | 0x1,
950  ADC_RES_16BIT_64SAMPL = ( 0x6 << 2) | 0x1,
951  ADC_RES_16BIT_128SAMPL = ( 0x7 << 2) | 0x1,
952  ADC_RES_16BIT_256SAMPL = ( 0x8 << 2) | 0x1,
953  ADC_RES_16BIT_512SAMPL = ( 0x9 << 2) | 0x1,
954  ADC_RES_16BIT_1024SAMPL = ( 0xA << 2) | 0x1,
955  ADC_RES_14BIT = 0xfe,
956 } adc_res_t;
957 
958 # define ADC_RES_16BIT ADC_RES_16BIT_16SAMPL
959 #endif /* DOXYGEN */
960 
965 #ifndef ETH_RX_BUFFER_COUNT
966 # define ETH_RX_BUFFER_COUNT (4)
967 #endif
968 
969 #ifndef ETH_TX_BUFFER_COUNT
970 # define ETH_TX_BUFFER_COUNT (2)
971 #endif
972 
973 #ifndef ETH_RX_BUFFER_SIZE
974 # define ETH_RX_BUFFER_SIZE (1536)
975 #endif
976 
977 #ifndef ETH_TX_BUFFER_SIZE
978 # define ETH_TX_BUFFER_SIZE (1536)
979 #endif
985 #if defined(GMAC_INST_NUM) || defined(DOXYGEN)
986 typedef struct {
987  Gmac *dev;
988  gpio_t refclk;
989  gpio_t txen;
990  gpio_t txd0;
991  gpio_t txd1;
992  gpio_t crsdv;
993  gpio_t rxd0;
994  gpio_t rxd1;
995  gpio_t rxer;
996  gpio_t mdc;
997  gpio_t mdio;
998  gpio_t rst_pin;
999  gpio_t int_pin;
1001 #endif
1002 
1006 #define USBDEV_CPU_DMA_ALIGNMENT (4)
1007 
1011 #define USBDEV_CPU_DMA_REQUIREMENTS __attribute__((aligned(USBDEV_CPU_DMA_ALIGNMENT)))
1012 
1016 #if defined(USB_INST_NUM) || defined(DOXYGEN)
1017 typedef struct {
1018  gpio_t dm;
1019  gpio_t dp;
1021  UsbDevice *device;
1022  uint8_t gclk_src;
1024 #endif /* USB_INST_NUM */
1025 
1029 #define SDMMC_CPU_DMA_ALIGNMENT 4
1030 
1034 #define SDMMC_CPU_DMA_REQUIREMENTS __attribute__((aligned(SDMMC_CPU_DMA_ALIGNMENT)))
1035 
1039 typedef struct {
1040  void *sdhc;
1041  gpio_t cd;
1042  gpio_t wp;
1043 } sdhc_conf_t;
1044 
1049 /* Limits are in clock cycles according to data sheet.
1050  As the WDT is clocked by a 1024 Hz clock, 1 cycle ≈ 1 ms */
1051 #define NWDT_TIME_LOWER_LIMIT (8U)
1052 #define NWDT_TIME_UPPER_LIMIT (16384U)
1058 #define WDT_HAS_STOP (1)
1062 #define WDT_HAS_INIT (1)
1063 
1067 typedef struct {
1068  gpio_t pin;
1069  uint8_t gclk_src;
1070 } freqm_config_t;
1071 
1072 #if defined(REV_DMAC) || DOXYGEN
1122 # define DMA_TRIGGER_DISABLED 0
1123 
1127 # if defined(CPU_COMMON_SAML21) || defined(DOXYGEN)
1128 # define DMA_DESCRIPTOR_IN_LPSRAM
1129 # endif
1130 
1134 # ifdef DMA_DESCRIPTOR_IN_LPSRAM
1135 # define DMA_DESCRIPTOR_ATTRS __attribute__((section(".backup.bss")))
1136 # else
1137 # define DMA_DESCRIPTOR_ATTRS
1138 # endif
1139 
1143 typedef unsigned dma_t;
1144 
1148 typedef enum {
1153 } dma_incr_t;
1154 
1158 void dma_init(void);
1159 
1171 
1178 
1187 void dma_setup(dma_t dma, unsigned trigger, uint8_t prio, bool irq);
1188 
1202 void dma_prepare(dma_t dma, uint8_t width, const void *src, void *dst,
1203  size_t num, dma_incr_t incr);
1204 
1225 void dma_prepare_src(dma_t dma, const void *src, size_t num, bool incr);
1226 
1247 void dma_prepare_dst(dma_t dma, void *dst, size_t num, bool incr);
1248 
1268 void dma_append(dma_t dma, DmacDescriptor *descriptor, uint8_t width,
1269  const void *src, void *dst, size_t num, dma_incr_t incr);
1270 
1289 void dma_append_src(dma_t dma, DmacDescriptor *next, const void *src,
1290  size_t num, bool incr);
1291 
1310 void dma_append_dst(dma_t dma, DmacDescriptor *next, void *dst, size_t num,
1311  bool incr);
1312 
1318 void dma_start(dma_t dma);
1319 
1329 void dma_wait(dma_t dma);
1330 
1339 void dma_cancel(dma_t dma);
1341 #endif /* REV_DMAC || DOXYGEN */
1342 
1351 void rtc_tamper_init(void);
1352 
1361 int rtc_tamper_register(gpio_t pin, gpio_flank_t flank);
1362 
1367 
1375 uint8_t rtc_get_tamper_event(void);
1376 
1387 uint8_t rtc_tamper_pin_mask(gpio_t pin);
1404 typedef struct sam0_aux_cfg_mapping nvm_user_page_t;
1405 
1409 #ifdef FLASH_USER_PAGE_SIZE
1410 # define FLASH_USER_PAGE_AUX_SIZE (FLASH_USER_PAGE_SIZE - sizeof(nvm_user_page_t))
1411 #else
1412 # define FLASH_USER_PAGE_AUX_SIZE (AUX_PAGE_SIZE * AUX_NB_OF_PAGES - sizeof(nvm_user_page_t))
1413 #endif
1414 
1424 
1440 void sam0_flashpage_aux_write(uint32_t offset, const void *data, size_t len);
1441 
1449 #define sam0_flashpage_aux_get(offset) \
1450  (const void*)((uint8_t*)NVMCTRL_USER + sizeof(nvm_user_page_t) + (offset))
1451 
1457 #define sam0_flashpage_aux_cfg() \
1458  ((const nvm_user_page_t*)NVMCTRL_USER)
1459 
1462 #ifdef __cplusplus
1463 }
1464 #endif
1465 
#define assert(cond)
abort the program if assertion is false
Definition: assert.h:135
gpio_flank_t
Definition: periph_cpu.h:179
@ GPIO_OUT
select GPIO MASK as output
Definition: periph_cpu.h:164
@ GPIO_IN
select GPIO MASK as input
Definition: periph_cpu.h:163
i2c_speed_t
Definition: periph_cpu.h:275
spi_clk_t
Definition: periph_cpu.h:351
enum IRQn IRQn_Type
Interrupt Number Definition.
static void cortexm_sleep(int deep)
Put the CPU into (deep) sleep mode, using the WFI instruction.
Definition: cpu.h:154
gpio_mode_t
Available pin modes.
Definition: periph_cpu.h:91
adc_res_t
Possible ADC resolution settings.
Definition: adc.h:92
@ ADC_RES_8BIT
ADC resolution: 8 bit.
Definition: adc.h:94
@ ADC_RES_14BIT
ADC resolution: 14 bit.
Definition: adc.h:97
@ ADC_RES_6BIT
ADC resolution: 6 bit.
Definition: adc.h:93
@ ADC_RES_10BIT
ADC resolution: 10 bit.
Definition: adc.h:95
@ ADC_RES_12BIT
ADC resolution: 12 bit.
Definition: adc.h:96
gpio_irq_trig_t
Definition of possible IRQ triggers.
Definition: gpio_ll_irq.h:71
@ GPIO_TRIGGER_EDGE_FALLING
edge triggered IRQ on falling flanks only
Definition: gpio_ll_irq.h:72
@ GPIO_TRIGGER_LEVEL_HIGH
level triggered IRQ on high input
Definition: gpio_ll_irq.h:77
@ GPIO_TRIGGER_EDGE_RISING
edge triggered IRQ on rising flanks only
Definition: gpio_ll_irq.h:74
@ GPIO_TRIGGER_EDGE_BOTH
edge triggered IRQ on falling AND rising flanks
Definition: gpio_ll_irq.h:75
@ GPIO_TRIGGER_LEVEL_LOW
level triggered IRQ on low input
Definition: gpio_ll_irq.h:78
gpio_pull_t
Enumeration of pull resistor configurations.
Definition: gpio_ll.h:257
gpio_pull_strength_t
Enumeration of pull resistor values.
Definition: gpio_ll.h:275
gpio_state_t
Enumeration of GPIO states (direction)
Definition: gpio_ll.h:165
gpio_slew_t
Enumeration of slew rate settings.
Definition: gpio_ll.h:339
gpio_drive_strength_t
Enumeration of drive strength options.
Definition: gpio_ll.h:306
typedef gpio_conf_t
GPIO pin configuration.
Definition: gpio_ll.h:423
@ GPIO_FLOATING
No pull ups nor pull downs enabled.
Definition: gpio_ll.h:258
@ GPIO_PULL_KEEP
Keep the signal at current logic level with pull up/down resistors.
Definition: gpio_ll.h:261
@ GPIO_PULL_DOWN
Pull down resistor enabled.
Definition: gpio_ll.h:260
@ GPIO_PULL_UP
Pull up resistor enabled.
Definition: gpio_ll.h:259
@ GPIO_PULL_WEAKEST
Use the weakest (highest Ohm value) resistor.
Definition: gpio_ll.h:276
@ GPIO_PULL_WEAK
Use a weak pull resistor.
Definition: gpio_ll.h:277
@ GPIO_PULL_STRONG
Use a strong pull resistor.
Definition: gpio_ll.h:278
@ GPIO_PULL_STRONGEST
Use the strongest pull resistor.
Definition: gpio_ll.h:279
@ GPIO_OUTPUT_OPEN_SOURCE
Use pin as output in open emitter configuration.
Definition: gpio_ll.h:202
@ GPIO_USED_BY_PERIPHERAL
The GPIO pin is used by a peripheral.
Definition: gpio_ll.h:221
@ GPIO_OUTPUT_OPEN_DRAIN
Use pin as output in open collector configuration.
Definition: gpio_ll.h:189
@ GPIO_OUTPUT_PUSH_PULL
Use pin as output in push-pull configuration.
Definition: gpio_ll.h:176
@ GPIO_DISCONNECT
Disconnect pin from all peripherals.
Definition: gpio_ll.h:249
@ GPIO_INPUT
Use pin as input.
Definition: gpio_ll.h:208
@ GPIO_SLEW_SLOWEST
let the output voltage level rise/fall as slow as possible
Definition: gpio_ll.h:340
@ GPIO_SLEW_FAST
let the output voltage level rise/fall fast
Definition: gpio_ll.h:343
@ GPIO_SLEW_SLOW
let the output voltage level rise/fall slowly
Definition: gpio_ll.h:342
@ GPIO_SLEW_FASTEST
let the output voltage level rise/fall as fast as possible
Definition: gpio_ll.h:344
@ GPIO_DRIVE_STRONG
Use a strong drive strength.
Definition: gpio_ll.h:309
@ GPIO_DRIVE_WEAK
Use a weak drive strength.
Definition: gpio_ll.h:308
@ GPIO_DRIVE_STRONGEST
Use the strongest drive strength.
Definition: gpio_ll.h:310
@ GPIO_DRIVE_WEAKEST
Use the weakest drive strength.
Definition: gpio_ll.h:307
unsigned int gpio_t
GPIO type identifier.
Definition: gpio.h:91
@ GPIO_FALLING
emit interrupt on falling flank
@ GPIO_RISING
emit interrupt on rising flank
@ GPIO_BOTH
not supported -> random value
@ GPIO_OD
configure as output in open-drain mode without pull resistor
Definition: gpio.h:123
@ GPIO_IN_PU
configure as input with pull-up resistor
Definition: gpio.h:121
@ GPIO_OD_PU
configure as output in open-drain mode with pull resistor enabled
Definition: gpio.h:125
@ GPIO_IN_PD
configure as input with pull-down resistor
Definition: gpio.h:120
@ I2C_SPEED_NORMAL
normal mode: ~100 kbit/s
Definition: periph_cpu.h:277
@ I2C_SPEED_FAST_PLUS
fast plus mode: ~1000 kbit/s
Definition: periph_cpu.h:279
@ I2C_SPEED_LOW
low speed mode: ~10 kbit/s
Definition: periph_cpu.h:276
@ I2C_SPEED_HIGH
high speed mode: ~3400 kbit/s
Definition: periph_cpu.h:281
@ I2C_SPEED_FAST
fast mode: ~400 kbit/s
Definition: periph_cpu.h:278
@ SPI_MODE_0
CPOL=0, CPHA=0.
Definition: periph_cpu.h:43
@ SPI_MODE_2
CPOL=1, CPHA=0.
Definition: periph_cpu.h:45
@ SPI_MODE_1
CPOL=0, CPHA=1.
Definition: periph_cpu.h:44
@ SPI_MODE_3
CPOL=1, CPHA=1.
Definition: periph_cpu.h:46
@ SPI_CLK_10MHZ
drive the SPI bus with 10MHz
Definition: periph_cpu.h:356
@ SPI_CLK_5MHZ
drive the SPI bus with 5MHz
Definition: periph_cpu.h:355
@ SPI_CLK_400KHZ
drive the SPI bus with 400KHz
Definition: periph_cpu.h:353
@ SPI_CLK_1MHZ
drive the SPI bus with 1MHz
Definition: periph_cpu.h:354
@ SPI_CLK_100KHZ
drive the SPI bus with 100KHz
Definition: periph_cpu.h:352
@ UART_DATA_BITS_6
6 data bits
Definition: periph_cpu.h:519
@ UART_DATA_BITS_5
5 data bits
Definition: periph_cpu.h:518
@ UART_DATA_BITS_7
7 data bits
Definition: periph_cpu.h:520
@ UART_DATA_BITS_8
8 data bits
Definition: periph_cpu.h:521
spi_mode_t
Support SPI modes.
Definition: periph_cpu.h:42
gpio_flank_t
Enumeration of supported GPIO flanks.
uart_data_bits_t
Definition of possible data bits lengths in a UART frame.
Definition: periph_cpu.h:517
uint8_t rtc_get_tamper_event(void)
Get and clear the RTC tamper event that has woken the CPU from Deep Sleep.
uart_rxpad_t
Available values for SERCOM UART RX pad selection.
@ UART_PAD_RX_1
select pad 1
@ UART_PAD_RX_0
use pad 0 for RX line
@ UART_PAD_RX_3
select pad 3
@ UART_PAD_RX_2
select pad 2
void gpio_disable_mux(gpio_t pin)
Disable alternate function (PMUX setting) for a PORT pin.
@ PB
port B
@ PC
port C
@ PA
port A
@ PD
port D
void dma_prepare(dma_t dma, uint8_t width, const void *src, void *dst, size_t num, dma_incr_t incr)
Prepare the DMA channel for an individual transfer.
void dma_init(void)
Initialize DMA.
@ TIMER_TYPE_TC
Timer is a TC timer
@ TIMER_TYPE_TCC
Timer is a TCC timer.
int rtc_tamper_register(gpio_t pin, gpio_flank_t flank)
Enable Tamper Detection IRQs.
#define GPIO_MODE(pr, ie, pe)
Generate GPIO mode bitfields.
static uint8_t sercom_id(const void *sercom)
Return the numeric id of a SERCOM device derived from its address.
void dma_wait(dma_t dma)
Wait for a DMA channel to finish the transfer.
void dma_cancel(dma_t dma)
Cancel an active DMA transfer.
i2c_flag_t
Available SERCOM I2C flag selections.
@ I2C_FLAG_NONE
No flags set.
@ I2C_FLAG_RUN_STANDBY
run SERCOM in standby mode
void dma_prepare_dst(dma_t dma, void *dst, size_t num, bool incr)
Prepare a transfer without modifying the source address settings.
void cpu_pm_cb_leave(int deep)
Called after the power management left a power mode.
void gpio_init_mux(gpio_t pin, gpio_mux_t mux)
Set up alternate function (PMUX setting) for a PORT pin.
void dma_setup(dma_t dma, unsigned trigger, uint8_t prio, bool irq)
Initialize a previously allocated DMA channel with one-time settings.
uint32_t sam0_gclk_freq(uint8_t id)
Returns the frequency of a GCLK provider.
dma_t dma_acquire_channel(void)
Acquire a DMA channel.
spi_misopad_t
Available values for SERCOM SPI MISO pad selection.
@ SPI_PAD_MISO_1
use pad 1 for MISO line
@ SPI_PAD_MISO_2
use pad 2 for MISO line
@ SPI_PAD_MISO_0
use pad 0 for MISO line
@ SPI_PAD_MISO_3
use pad 3 for MISO line
void dma_release_channel(dma_t dma)
Release a previously acquired DMA channel.
void gpio_pm_cb_leave(int deep)
Called after the power management left a power mode.
static void sercom_set_gen(void *sercom, uint8_t gclk)
Configure generator clock for given SERCOM device.
uint8_t rtc_tamper_pin_mask(gpio_t pin)
Get the tamper event mask for a certain pin.
uart_flag_t
Available SERCOM UART flag selections.
@ UART_FLAG_TX_ONDEMAND
Only enable TX pin on demand.
@ UART_FLAG_NONE
No flags set.
@ UART_FLAG_RUN_STANDBY
run SERCOM in standby mode
@ UART_FLAG_WAKEUP
wake from sleep on receive
void sam0_flashpage_aux_reset(const nvm_user_page_t *cfg)
Reset the configuration area, apply a new configuration.
static void sam0_cortexm_sleep(int deep)
Wrapper for cortexm_sleep calling power management callbacks.
static void sercom_clk_dis(void *sercom)
Disable peripheral clock for given SERCOM device.
static bool cpu_woke_from_backup(void)
Returns true if the CPU woke deep sleep (backup/standby)
void cpu_pm_cb_enter(int deep)
Called before the power management enters a power mode.
void dma_append_dst(dma_t dma, DmacDescriptor *next, void *dst, size_t num, bool incr)
Append a second transfer descriptor after the default channel descriptor, copying source and block si...
void sam0_flashpage_aux_write(uint32_t offset, const void *data, size_t len)
Write data to the user configuration area.
uart_txpad_t
Available values for SERCOM UART TX pad selection.
@ UART_PAD_TX_0_RTS_2_CTS_3
TX is pad 0, on top RTS on pad 2 and CTS on pad 3.
@ UART_PAD_TX_0
select pad 0
@ UART_PAD_TX_2
select pad 2
dma_incr_t
Available DMA address increment modes.
@ DMA_INCR_NONE
Don't increment any addresses after a beat.
@ DMA_INCR_DEST
Increment destination address after a beat.
@ DMA_INCR_BOTH
Increment both addresses after a beat.
@ DMA_INCR_SRC
Increment the source address after a beat.
void dma_append(dma_t dma, DmacDescriptor *descriptor, uint8_t width, const void *src, void *dst, size_t num, dma_incr_t incr)
Append a second transfer descriptor after the default channel descriptor.
void rtc_tamper_enable(void)
Enable Tamper Detection IRQs.
void rtc_tamper_init(void)
Power on the RTC (if the RTC/RTT is not otherwise used)
unsigned dma_t
DMA channel type.
void sam0_gclk_enable(uint8_t id)
Enables an on-demand GCLK that has been configured in cpu.c.
void dma_append_src(dma_t dma, DmacDescriptor *next, const void *src, size_t num, bool incr)
Append a second transfer descriptor after the default channel descriptor, copying destination and blo...
gpio_mux_t
Available MUX values for configuring a pin's alternate function.
@ GPIO_MUX_E
select peripheral function E
@ GPIO_MUX_J
select peripheral function J
@ GPIO_MUX_K
select peripheral function K
@ GPIO_MUX_M
select peripheral function M
@ GPIO_MUX_H
select peripheral function H
@ GPIO_MUX_I
select peripheral function I
@ GPIO_MUX_D
select peripheral function D
@ GPIO_MUX_G
select peripheral function G
@ GPIO_MUX_C
select peripheral function C
@ GPIO_MUX_N
select peripheral function N
@ GPIO_MUX_A
select peripheral function A
@ GPIO_MUX_L
select peripheral function L
@ GPIO_MUX_B
select peripheral function B
@ GPIO_MUX_DISABLED
Disable
@ GPIO_MUX_F
select peripheral function F
static void sam0_set_voltage_regulator(sam0_supc_t src)
Switch the internal voltage regulator used for generating the internal MCU voltages.
spi_mosipad_t
Available values for SERCOM SPI MOSI and SCK pad selection.
@ SPI_PAD_MOSI_2_SCK_3
use pad 2 for MOSI, pad 3 for SCK
@ SPI_PAD_MOSI_0_SCK_3
use pad 0 for MOSI, pad 3 for SCK
@ SPI_PAD_MOSI_3_SCK_1
use pad 3 for MOSI, pad 1 for SCK
@ SPI_PAD_MOSI_0_SCK_1
use pad 0 for MOSI, pad 1 for SCK
static void sercom_clk_en(void *sercom)
Enable peripheral clock for given SERCOM device.
sam0_supc_t
Available voltage regulators on the supply controller.
@ SAM0_VREG_BUCK
Buck converter, efficient but may clash with internal fast clock generators (see errata sheets)
@ SAM0_VREG_LDO
LDO, always available but not very power efficient.
void dma_prepare_src(dma_t dma, const void *src, size_t num, bool incr)
Prepare a transfer without modifying the destination address settings.
void dma_start(dma_t dma)
Start a DMA transfer.
void gpio_pm_cb_enter(int deep)
Called before the power management enters a power mode.
ADC Channel Configuration.
uint32_t inputctrl
ADC channel pin multiplexer value
Frequency meter configuration.
uint8_t gclk_src
GCLK source select for reference.
gpio_t pin
GPIO at which the frequency is to be measured.
I2C configuration structure.
Definition: periph_cpu.h:298
uint8_t gclk_src
GCLK source which supplys SERCOM.
gpio_mux_t mux
alternate function (mux)
uint8_t flags
allow SERCOM to run in standby mode
SercomI2cm * dev
pointer to the used I2C device
PWM channel configuration data structure.
gpio_t pin
GPIO pin.
uint8_t chan
TCC channel to use.
gpio_mux_t mux
pin function multiplex value
PWM device configuration.
tc_tcc_cfg_t tim
timer configuration
uint8_t chan_numof
number of channels
const pwm_conf_chan_t * chan
channel configuration
uint8_t gclk_src
GCLK source which clocks TIMER.
NVM User Row Mapping - Dedicated Entries Config values will be applied at power-on.
Definition: periph_cpu.h:176
Ethernet parameters struct.
Gmac * dev
ptr to the device registers
gpio_t mdc
MII interface, clock gpio.
gpio_t mdio
MII interface, data gpio.
gpio_t rst_pin
PHY reset gpio.
gpio_t int_pin
PHY interrupt gpio.
USB peripheral parameters.
gpio_mux_t d_mux
alternate function (mux) for data pins
uint8_t gclk_src
GCLK source which supplys 48 MHz
UsbDevice * device
ptr to the device registers
SDHC peripheral configuration.
gpio_t wp
Write Protect pin (must be GPIO_UNDEF if not connected)
void * sdhc
SDHC peripheral.
gpio_t cd
Card Detect pin (must be GPIO_UNDEF if not connected)
SPI device configuration.
Definition: periph_cpu.h:336
gpio_mux_t miso_mux
alternate function for MISO pin (mux)
spi_misopad_t miso_pad
pad to use for MISO line
gpio_mux_t clk_mux
alternate function for CLK pin (mux)
spi_mosipad_t mosi_pad
pad to use for MOSI and CLK line
gpio_mux_t mosi_mux
alternate function for MOSI pin (mux)
uint8_t gclk_src
GCLK source which supplys SERCOM.
void * dev
pointer to the used SPI device
Timer device configuration.
uint16_t flags
flags for CTRA, e.g.
uint32_t pm_mask
PM_APBCMASK bits to enable Timer.
uint8_t gclk_src
GCLK source which supplys Timer.
IRQn_Type irq
IRQ# of Timer Interrupt.
uint16_t gclk_ctrl
GCLK_CLKCTRL_ID for the Timer.
Tc * dev
pointer to the used Timer device
Common configuration for timer devices.
uint16_t gclk_id
TCn_GCLK_ID.
uint8_t type
Timer type (TC/TCC)
uint32_t pm_mask
PM_APBCMASK bits to enable Timer.
UART device configuration.
Definition: periph_cpu.h:217
gpio_mux_t mux
alternative function for pins
uint8_t gclk_src
GCLK source which supplys SERCOM.
uart_txpad_t tx_pad
pad selection for TX line
SercomUsart * dev
pointer to the used UART device
uart_rxpad_t rx_pad
pad selection for RX line
uart_flag_t flags
set optional SERCOM flags
GPIO pin configuration for SAM0 MCUs.
bool initial_value
Initial value of the output.
uint8_t bits
the raw bits
gpio_pull_t pull
Pull resistor configuration.
gpio_state_t state
State of the pin.
gpio_drive_strength_t drive_strength
Drive strength of the GPIO.