periph_cpu_common.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2015 HAW Hamburg
3  * SPDX-FileCopyrightText: 2016 Freie Universität Berlin
4  * SPDX-FileCopyrightText: 2016 INRIA
5  * SPDX-FileCopyrightText: 2023 Hugues Larrive
6  * SPDX-License-Identifier: LGPL-2.1-only
7  */
8 
9 #pragma once
10 
24 #include "cpu.h"
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
34 #define CPUID_LEN (4U)
37 #ifndef DOXYGEN
42 #define HAVE_GPIO_T
43 typedef uint8_t gpio_t;
45 #endif
46 
50 #define GPIO_UNDEF (0xff)
51 
55 #define GPIO_PIN(x, y) ((x << 4) | y)
56 
62 #if (defined(OCF1A) && defined(OCF1B) && (OCF1A > OCF1B)) \
63  || (defined(PUD) && (PUD != 4)) || (defined(INT0) && (INT0 == 6))
64  /* match with 65 devices against 61 for (PORTB == _SFR_IO8(0x18)) which
65  * did not work here anyway */
66 #define GPIO_PORT_DESCENDENT
67 #endif
68 
69 #ifdef GPIO_PORT_DESCENDENT
70 #ifdef _AVR_ATTINY1634_H_INCLUDED
71 /* the only one that requires particular treatment! */
72 #define ATMEGA_GPIO_BASE_A 0x2F
73 #else
74 /* all other port descendent, including :
75  - _AVR_IO8534_ (only have port A but with 0x1B address) ;
76  - _AVR_IOAT94K_H_ (only have ports D and E) ;
77  - _AVR_IOTN28_H_ (only have ports A and D). */
78 #define ATMEGA_GPIO_BASE_A 0x39
79 #endif /* _AVR_ATTINY1634_H_INCLUDED */
80 #else /* !GPIO_PORT_DESCENDENT */
81 #define ATMEGA_GPIO_BASE_A 0x20
82 #endif /* GPIO_PORT_DESCENDENT */
88 #define ATMEGA_GPIO_BASE_G (ATMEGA_GPIO_BASE_A + ATMEGA_GPIO_SIZE * ('G' - 'A'))
94 #define ATMEGA_GPIO_BASE_H (0x100)
98 #define ATMEGA_GPIO_SIZE (0x03)
99 
100 #if defined(DOXYGEN)
104 #define GPIO_EXT_INT_NUMOF <CPU_SPECIFIC>
105 #elif defined(INT7_vect)
106 #define GPIO_EXT_INT_NUMOF (8U)
107 #elif defined(INT6_vect)
108 #define GPIO_EXT_INT_NUMOF (7U)
109 #elif defined(INT5_vect)
110 #define GPIO_EXT_INT_NUMOF (6U)
111 #elif defined(INT4_vect)
112 #define GPIO_EXT_INT_NUMOF (5U)
113 #elif defined(INT3_vect)
114 #define GPIO_EXT_INT_NUMOF (4U)
115 #elif defined(INT2_vect)
116 #define GPIO_EXT_INT_NUMOF (3U)
117 #else
118 #define GPIO_EXT_INT_NUMOF (2U)
119 #endif
120 
125 typedef struct {
134  volatile uint8_t pin;
139  volatile uint8_t ddr;
147  volatile uint8_t port;
149 
155 static inline atmega_gpio_port_t *atmega_gpio_port(uint8_t port_num)
156 {
157  static const uintptr_t base_addr = (uintptr_t)ATMEGA_GPIO_BASE_A;
158 #ifdef GPIO_PORT_DESCENDENT
159  uintptr_t res = base_addr - port_num * sizeof(atmega_gpio_port_t);
160 #else
161  uintptr_t res = base_addr + port_num * sizeof(atmega_gpio_port_t);
162 #endif
163  /* GPIO ports up to (including) G are mapped in the I/O address space,
164  * port H and higher (if present) are mapped in a different contiguous
165  * region afterwards (e.g. 0x100 for ATmega2560). */
166 #ifdef PORTH
167  if (port_num > 'G'-'A') {
168  static const uintptr_t offset = ATMEGA_GPIO_BASE_H - ATMEGA_GPIO_BASE_G;
169  res += offset;
170  }
171 #endif
172 
173  return (atmega_gpio_port_t *)res;
174 }
175 
176 #ifndef DOXYGEN
186 #define HAVE_GPIO_FLANK_T
187 typedef enum {
188  GPIO_LOW,
189  GPIO_BOTH,
190  GPIO_FALLING,
191  GPIO_RISING,
192 } gpio_flank_t;
194 #endif /* ndef DOXYGEN */
195 
196 #ifndef DOXYGEN /* BEGIN: GPIO LL overwrites */
197 #define HAVE_GPIO_SLEW_T
198 typedef enum {
199  GPIO_SLEW_SLOWEST = 0,
200  GPIO_SLEW_SLOW = 0,
201  GPIO_SLEW_FAST = 0,
202  GPIO_SLEW_FASTEST = 0,
203 } gpio_slew_t;
204 
205 #define HAVE_GPIO_PULL_STRENGTH_T
206 typedef enum {
207  GPIO_PULL_WEAKEST = 0,
208  GPIO_PULL_WEAK = 0,
209  GPIO_PULL_STRONG = 0,
212 
213 #define HAVE_GPIO_DRIVE_STRENGTH_T
214 typedef enum {
215  GPIO_DRIVE_WEAKEST = 0,
216  GPIO_DRIVE_WEAK = 0,
217  GPIO_DRIVE_STRONG = 0,
220 
221 #define HAVE_GPIO_IRQ_TRIG_T
222 typedef enum {
223  GPIO_TRIGGER_LEVEL_LOW = 0x00,
224  GPIO_TRIGGER_EDGE_BOTH = 0x01,
227  GPIO_TRIGGER_LEVEL_HIGH = 0xff,
229 
230 #define HAVE_GPIO_STATE_T
231 typedef enum {
232  GPIO_INPUT,
238 } gpio_state_t;
239 
240 #define HAVE_GPIO_LL_PREPARE_WRITE_ALL_PINS
241 #define HAVE_GPIO_LL_PREPARE_WRITE
242 
243 #endif /* END: GPIO LL overwrites */
244 
249 #define PERIPH_SPI_NEEDS_INIT_CS
250 #define PERIPH_SPI_NEEDS_TRANSFER_BYTE
251 #define PERIPH_SPI_NEEDS_TRANSFER_REG
252 #define PERIPH_SPI_NEEDS_TRANSFER_REGS
255 #ifndef DOXYGEN
262 #define SPI_MODE_SEL(pol, pha) ((pol << 3) | (pha << 2))
263 
271 #define HAVE_SPI_MODE_T
272 typedef enum {
273  SPI_MODE_0 = SPI_MODE_SEL(0, 0),
274  SPI_MODE_1 = SPI_MODE_SEL(0, 1),
275  SPI_MODE_2 = SPI_MODE_SEL(1, 0),
276  SPI_MODE_3 = SPI_MODE_SEL(1, 1)
277 } spi_mode_t;
286 #define SPI_CLK_SEL(s2x, pr1, pr0) ((s2x << 2) | (pr1 << 1) | pr0)
287 
294 #define HAVE_SPI_CLK_T
295 typedef enum {
296  SPI_CLK_100KHZ = SPI_CLK_SEL(0, 1, 1),
297  SPI_CLK_400KHZ = SPI_CLK_SEL(1, 1, 0),
298  SPI_CLK_1MHZ = SPI_CLK_SEL(0, 0, 1),
299  SPI_CLK_5MHZ = SPI_CLK_SEL(0, 0, 0),
300  SPI_CLK_10MHZ = SPI_CLK_SEL(1, 0, 0)
301 } spi_clk_t;
303 #endif /* ifndef DOXYGEN */
304 
309 typedef enum {
312 } timer_div_t;
319 typedef struct {
321  gpio_t pin_ch[2];
323 } pwm_conf_t;
329 #define PERIPH_TIMER_PROVIDES_SET
330 
334 #define EEPROM_CLEAR_BYTE (0xff)
335 
340 #define NWDT_TIME_LOWER_LIMIT (1)
341 #define NWDT_TIME_UPPER_LIMIT (8192U)
347 #define WDT_HAS_STOP (1)
348 
353 #if defined(SCCR0) && !defined(RTT_BACKEND_SC)
354 #define RTT_BACKEND_SC (1)
355 #endif
356 
357 #if RTT_BACKEND_SC
358 /* For MCU with MAC symbol counter */
359 #ifndef RTT_MAX_VALUE
360 #define RTT_MAX_VALUE (0xFFFFFFFFUL) /* 32-bit timer */
361 #endif
362 
363 #ifndef RTT_FREQUENCY
364 #define RTT_FREQUENCY (62500UL) /* in Hz. */
365 #endif
366 
367 #else
368 /* For MCU without MAC symbol counter */
369 #ifndef RTT_MAX_VALUE
370 #define RTT_MAX_VALUE (0x00FFFFFF) /* 24-bit timer */
371 #endif
372 /* possible values: 32, 128, 256, 512, 1024, 4096, 32768 */
373 #ifndef RTT_FREQUENCY
374 #define RTT_FREQUENCY (1024U) /* in Hz. */
375 #endif
376 #endif
379 #ifdef __cplusplus
380 }
381 #endif
382 
#define ATMEGA_GPIO_BASE_H
Base of the GPIO registers of the second memory region (port >= H)
#define ATMEGA_GPIO_BASE_A
Base of the GPIO registers as memory address.
timer_div_t
@ TIMER_DIV1_8_32_64_128_256_1024
1/{1,8,32,64,128,256,1024}
@ TIMER_DIV1_8_64_128_1024
1/{1,8,64,128,1024}
#define ATMEGA_GPIO_BASE_G
Base of the GPIO port G register as memory address.
static atmega_gpio_port_t * atmega_gpio_port(uint8_t port_num)
Get the GPIO PORT registers of the given GPIO PORT.
spi_clk_t
Definition: periph_cpu.h:348
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_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
@ 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
@ 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:353
@ SPI_CLK_5MHZ
drive the SPI bus with 5MHz
Definition: periph_cpu.h:352
@ SPI_CLK_400KHZ
drive the SPI bus with 400KHz
Definition: periph_cpu.h:350
@ SPI_CLK_1MHZ
drive the SPI bus with 1MHz
Definition: periph_cpu.h:351
@ SPI_CLK_100KHZ
drive the SPI bus with 100KHz
Definition: periph_cpu.h:349
spi_mode_t
Support SPI modes.
Definition: periph_cpu.h:42
gpio_flank_t
Enumeration of supported GPIO flanks.
Structure describing the memory layout of the registers of a GPIO port on ATmega MCUs.
volatile uint8_t port
Read/write the state of GPIO pins using the Port Data Register.
volatile uint8_t pin
Toggle bits in the port register.
volatile uint8_t ddr
Configure pins as output (1) or input (0) using the Data Direction Register.
8-bit timer register map
PWM device configuration.
timer_div_t div
Timer divider mask.
mini_timer_t * dev
Timer used.