All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
periph_cpu.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2021 Gerson Fernando Budke <nandojve@gmail.com>
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 
19 #ifndef PERIPH_CPU_H
20 #define PERIPH_CPU_H
21 
22 #include <avr/io.h>
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
32 #define CPUID_LEN (11U)
39 typedef enum {
50 enum {
67  /* ... */
68  PORT_MAX,
69 };
70 
74 typedef uint16_t pwr_reduction_t;
75 
79 #define PWR_RED_REG(reg, dev) ((reg << 8) | dev)
80 
84 enum {
85  PWR_GENERAL_POWER,
86  PWR_PORT_A,
87  PWR_PORT_B,
88  PWR_PORT_C,
89  PWR_PORT_D,
90  PWR_PORT_E,
91  PWR_PORT_F,
92 };
93 
98 #define PM_NUM_MODES (5)
99 #define AVR8_PM_SLEEP_MODE_0 SLEEP_MODE_PWR_DOWN
100 #define AVR8_PM_SLEEP_MODE_1 SLEEP_MODE_PWR_SAVE
101 #define AVR8_PM_SLEEP_MODE_2 SLEEP_MODE_STANDBY
102 #define AVR8_PM_SLEEP_MODE_3 SLEEP_MODE_EXT_STANDBY
109 #define GPIO_EXT_INT_NUMOF (2 * PORT_MAX)
116 #define HAVE_GPIO_T
117 typedef uint16_t gpio_t;
123 #define GPIO_UNDEF (0xffff)
124 
135 #define ATXMEGA_GPIO_PIN(x, y) (((x & 0x0f) << 8) | (y & 0xff))
136 #define GPIO_PIN(x, y) ATXMEGA_GPIO_PIN(x, (1U << (y & 0x07)))
148 #define HAVE_GPIO_MODE_T
149 typedef enum GPIO_MODE {
150  GPIO_SLEW_RATE = (1 << 7),
151  GPIO_INVERTED = (1 << 6),
153  GPIO_OPC_TOTEN = (0 << 3),
154  GPIO_OPC_BSKPR = (1 << 3),
155  GPIO_OPC_PD = (2 << 3),
156  GPIO_OPC_PU = (3 << 3),
157  GPIO_OPC_WRD_OR = (4 << 3),
158  GPIO_OPC_WRD_AND = (5 << 3),
159  GPIO_OPC_WRD_OR_PULL = (6 << 3),
162  GPIO_ANALOG = (1 << 1),
164  GPIO_IN = (0 << 0),
165  GPIO_OUT = (1 << 0),
167  /* Compatibility Mode */
172 } gpio_mode_t;
179 #define HAVE_GPIO_FLANK_T
180 typedef enum {
181  GPIO_ISC_BOTH = (0 << 4),
182  GPIO_ISC_RISING = (1 << 4),
183  GPIO_ISC_FALLING = (2 << 4),
184  GPIO_ISC_LOW_LEVEL = (3 << 4),
188  GPIO_INT0_VCT = (0 << 2),
189  GPIO_INT1_VCT = (1 << 2),
191  GPIO_LVL_OFF = (0 << 0),
192  GPIO_LVL_LOW = (1 << 0),
193  GPIO_LVL_MID = (2 << 0),
194  GPIO_LVL_HIGH = (3 << 0),
196  /* Compatibility Mode */
200 } gpio_flank_t;
206 #define UART_MAX_NUMOF (7)
207 
211 #ifndef UART_TXBUF_SIZE
212 #define UART_TXBUF_SIZE (64)
213 #endif
214 
218 typedef struct {
219  USART_t *dev;
221  gpio_t rx_pin;
222  gpio_t tx_pin;
223 #ifdef MODULE_PERIPH_UART_HW_FC
224  gpio_t rts_pin;
225  gpio_t cts_pin;
226 #endif
230 } uart_conf_t;
231 
235 #define TIMER_CH_MAX_NUMOF (4)
236 
240 #define PERIPH_TIMER_PROVIDES_SET
241 
250 typedef enum {
251  TC_TYPE_0 = 0,
252  TC_TYPE_1 = 1,
253  TC_TYPE_2 = 2,
254  TC_TYPE_4 = 4,
255  TC_TYPE_5 = 5,
256 } timer_type_t;
257 
264 typedef struct {
265  TC0_t *dev;
269 } timer_conf_t;
270 
275 #define HAVE_I2C_SPEED_T
276 typedef enum {
277  I2C_SPEED_LOW = 10000ul,
278  I2C_SPEED_NORMAL = 100000ul,
279  I2C_SPEED_FAST = 400000ul,
280  I2C_SPEED_FAST_PLUS = 1000000ul,
281  /* High speed is not supported without external hardware hacks */
282  I2C_SPEED_HIGH = 3400000ul,
283 } i2c_speed_t;
290 #define PERIPH_I2C_NEED_READ_REG
291 #define PERIPH_I2C_NEED_READ_REGS
292 #define PERIPH_I2C_NEED_WRITE_REG
293 #define PERIPH_I2C_NEED_WRITE_REGS
299 typedef struct {
300  TWI_t *dev;
302  gpio_t sda_pin;
303  gpio_t scl_pin;
306 } i2c_conf_t;
307 
312 #define PERIPH_SPI_NEEDS_INIT_CS
313 #define PERIPH_SPI_NEEDS_TRANSFER_BYTE
314 #define PERIPH_SPI_NEEDS_TRANSFER_REG
315 #define PERIPH_SPI_NEEDS_TRANSFER_REGS
322 #define SPI_UNDEF (UCHAR_MAX)
329 #define HAVE_SPI_T
330 typedef uint8_t spi_t;
337 typedef struct {
338  SPI_t *dev;
340  gpio_t sck_pin;
341  gpio_t miso_pin;
342  gpio_t mosi_pin;
343  gpio_t ss_pin;
344 } spi_conf_t;
351 #define HAVE_SPI_CLK_T
352 typedef enum {
353  SPI_CLK_100KHZ = 100000U,
354  SPI_CLK_400KHZ = 400000U,
355  SPI_CLK_1MHZ = 1000000U,
356  SPI_CLK_5MHZ = 5000000U,
357  SPI_CLK_10MHZ = 10000000U,
358 } spi_clk_t;
361 #if defined(__AVR_ATxmega64A1__) || \
362  defined(__AVR_ATxmega128A1__) || \
363  defined(__AVR_ATxmega64A1U__) || \
364  defined(__AVR_ATxmega128A1U__) || \
365  defined(Doxygen)
374 typedef enum {
375  EBI_LPC_MODE_ALE1 = 0x01,
376  EBI_LPC_MODE_ALE12 = 0x03,
377 } ebi_lpc_mode_t;
378 
384 typedef enum {
385  EBI_PORT_3PORT = 0x01,
386  EBI_PORT_SDRAM = 0x02,
387  EBI_PORT_SRAM = 0x04,
388  EBI_PORT_LPC = 0x08,
389  EBI_PORT_CS0 = 0x10,
390  EBI_PORT_CS1 = 0x20,
391  EBI_PORT_CS2 = 0x40,
392  EBI_PORT_CS3 = 0x80,
393  EBI_PORT_CS_ALL = 0xF0,
394 } ebi_port_mask_t;
395 
399 typedef enum {
400  EBI_SDRAM_CAS_LAT_2CLK = 0x00,
401  EBI_SDRAM_CAS_LAT_3CLK = 0x01,
402 } ebi_sdram_cas_latency_t;
403 
407 typedef enum {
408  EBI_SDRAM_ROW_BITS_11 = 0x00,
409  EBI_SDRAM_ROW_BITS_12 = 0x01,
410 } ebi_sdram_row_bits_t;
411 
415 #define PERIPH_EBI_MAX_CS (4)
416 
420 #define PERIPH_EBI_SDRAM_CS (3)
421 
422 /* for compatibility between different versions of AVR libc: Legacy versions
423  * occasionally use ASPACE instead of ASIZE for some MCUs, while new AVR libc
424  * uses ASIZE as name consistently */
425 #ifndef EBI_CS_ASIZE_gm
426 typedef EBI_CS_ASPACE_t EBI_CS_ASIZE_t;
427 #define EBI_CS_ASIZE_256B_gc EBI_CS_ASPACE_256B_gc
428 #define EBI_CS_ASIZE_512B_gc EBI_CS_ASPACE_512B_gc
429 #define EBI_CS_ASIZE_1KB_gc EBI_CS_ASPACE_1KB_gc
430 #define EBI_CS_ASIZE_2KB_gc EBI_CS_ASPACE_2KB_gc
431 #define EBI_CS_ASIZE_4KB_gc EBI_CS_ASPACE_4KB_gc
432 #define EBI_CS_ASIZE_8KB_gc EBI_CS_ASPACE_8KB_gc
433 #define EBI_CS_ASIZE_16KB_gc EBI_CS_ASPACE_16KB_gc
434 #define EBI_CS_ASIZE_32KB_gc EBI_CS_ASPACE_32KB_gc
435 #define EBI_CS_ASIZE_64KB_gc EBI_CS_ASPACE_64KB_gc
436 #define EBI_CS_ASIZE_128KB_gc EBI_CS_ASPACE_128KB_gc
437 #define EBI_CS_ASIZE_256KB_gc EBI_CS_ASPACE_256KB_gc
438 #define EBI_CS_ASIZE_512KB_gc EBI_CS_ASPACE_512KB_gc
439 #define EBI_CS_ASIZE_1MB_gc EBI_CS_ASPACE_1MB_gc
440 #define EBI_CS_ASIZE_2MB_gc EBI_CS_ASPACE_2MB_gc
441 #define EBI_CS_ASIZE_4MB_gc EBI_CS_ASPACE_4MB_gc
442 #define EBI_CS_ASIZE_8MB_gc EBI_CS_ASPACE_8MB_gc
443 #define EBI_CS_ASIZE_16MB_gc EBI_CS_ASPACE_16MB_gc
444 #endif
445 
449 typedef struct {
450  EBI_CS_MODE_t mode;
451  EBI_CS_ASIZE_t space;
452  EBI_CS_SRWS_t wait;
453  uint32_t address;
454 } ebi_cs_t;
455 
459 typedef struct {
460  uint8_t refresh;
461  uint16_t refresh_period;
462  uint16_t init_dly;
463  EBI_CS_SDMODE_t mode;
464  ebi_sdram_cas_latency_t cas_latency;
465  ebi_sdram_row_bits_t row_bits;
466  EBI_SDCOL_t column_bits;
467  EBI_MRDLY_t ld_mode_dly;
468  EBI_ROWCYCDLY_t row_cycle_dly;
469  EBI_RPDLY_t row_prechage_dly;
470  EBI_WRDLY_t write_recovery_dly;
471  EBI_ESRDLY_t exit_self_rfsh_dly;
472  EBI_ROWCOLDLY_t row_to_column_dly;
473 } ebi_sd_t;
474 
599 typedef struct {
600  uint8_t addr_bits;
601  ebi_port_mask_t flags;
602  uint8_t sram_ale;
603  uint8_t lpc_ale;
604  ebi_sd_t sdram;
605  ebi_cs_t cs[PERIPH_EBI_MAX_CS];
606 } ebi_conf_t;
609 #endif /* __AVR_ATxmegaxxxA1x__ */
610 
611 #ifdef __cplusplus
612 }
613 #endif
614 
615 #endif /* PERIPH_CPU_H */
@ PORT_B
port B
Definition: periph_cpu.h:48
@ PORT_G
port G
Definition: periph_cpu.h:53
@ PORT_C
port C
Definition: periph_cpu.h:49
@ PORT_F
port F
Definition: periph_cpu.h:52
@ PORT_E
port E
Definition: periph_cpu.h:51
@ PORT_A
port A
Definition: periph_cpu.h:47
@ PORT_D
port D
Definition: periph_cpu.h:50
@ PORT_K
port K
Definition: periph_cpu.h:54
@ PORT_H
port H
Definition: periph_cpu.h:52
@ PORT_J
port J
Definition: periph_cpu.h:53
@ PORT_L
port L
Definition: periph_cpu.h:55
#define TIMER_CH_MAX_NUMOF
Max number of available timer channels.
Definition: periph_cpu.h:235
gpio_flank_t
Definition: periph_cpu.h:180
@ PORT_P
port P - 7A0 - D
Definition: periph_cpu.h:64
@ PORT_Q
port Q - 7C0 - E
Definition: periph_cpu.h:65
@ PORT_M
port M - 760 - B
Definition: periph_cpu.h:62
@ PORT_R
port R - 7E0 - F
Definition: periph_cpu.h:66
@ PORT_N
port N - 780 - C
Definition: periph_cpu.h:63
GPIO_MODE
Definition: periph_cpu.h:149
@ GPIO_OUT
select GPIO MASK as output
Definition: periph_cpu.h:165
@ GPIO_IN
select GPIO MASK as input
Definition: periph_cpu.h:164
@ GPIO_OPC_PU
pull-up resistor
Definition: periph_cpu.h:156
@ GPIO_OPC_WRD_AND_PULL
enable wired AND and pull-up resistor
Definition: periph_cpu.h:160
@ GPIO_OPC_TOTEN
select no pull resistor (TOTEM)
Definition: periph_cpu.h:153
@ GPIO_OPC_WRD_AND
enable wired AND
Definition: periph_cpu.h:158
@ GPIO_OPC_WRD_OR_PULL
enable wired OR and pull-down resistor
Definition: periph_cpu.h:159
@ GPIO_OPC_BSKPR
push-pull mode (BUSKEEPER)
Definition: periph_cpu.h:154
@ GPIO_OPC_WRD_OR
enable wired OR
Definition: periph_cpu.h:157
@ GPIO_SLEW_RATE
enable slew rate
Definition: periph_cpu.h:150
@ GPIO_INVERTED
enable inverted signal
Definition: periph_cpu.h:151
@ GPIO_OPC_PD
pull-down resistor
Definition: periph_cpu.h:155
@ GPIO_ANALOG
select GPIO for analog function
Definition: periph_cpu.h:162
timer_type_t
Timer Type.
Definition: periph_cpu.h:250
i2c_speed_t
Definition: periph_cpu.h:276
uint16_t pwr_reduction_t
Power Reduction Peripheral Mask.
Definition: periph_cpu.h:74
cpu_int_lvl_t
Definition: periph_cpu.h:39
@ CPU_INT_LVL_MID
Interrupt Medium Level.
Definition: periph_cpu.h:42
@ CPU_INT_LVL_OFF
Interrupt Disabled
Definition: periph_cpu.h:40
@ CPU_INT_LVL_LOW
Interrupt Low Level
Definition: periph_cpu.h:41
@ CPU_INT_LVL_HIGH
Interrupt High Level
Definition: periph_cpu.h:43
spi_clk_t
Definition: periph_cpu.h:352
gpio_mode_t
Available pin modes.
Definition: periph_cpu.h:91
unsigned int gpio_t
GPIO type identifier.
Definition: gpio.h:92
@ GPIO_FALLING
emit interrupt on falling flank
Definition: periph_cpu.h:109
@ GPIO_ISC_FALLING
emit interrupt on falling flank
Definition: periph_cpu.h:183
@ GPIO_INT0_VCT
enable interrupt on Vector 0 (default)
Definition: periph_cpu.h:188
@ GPIO_INT1_VCT
enable interrupt on Vector 1
Definition: periph_cpu.h:189
@ GPIO_ISC_LOW_LEVEL
emit interrupt on low level
Definition: periph_cpu.h:184
@ GPIO_ISC_BOTH
emit interrupt on both flanks (default)
Definition: periph_cpu.h:181
@ GPIO_RISING
emit interrupt on rising flank
Definition: periph_cpu.h:110
@ GPIO_LVL_LOW
interrupt low level
Definition: periph_cpu.h:192
@ GPIO_BOTH
emit interrupt on both flanks
Definition: periph_cpu.h:111
@ GPIO_INT_DISABLED_ALL
disable all interrupts
Definition: periph_cpu.h:186
@ GPIO_LVL_OFF
interrupt disabled (default)
Definition: periph_cpu.h:191
@ GPIO_LVL_HIGH
interrupt higher
Definition: periph_cpu.h:194
@ GPIO_ISC_RISING
emit interrupt on rising flank
Definition: periph_cpu.h:182
@ GPIO_LVL_MID
interrupt medium level
Definition: periph_cpu.h:193
@ GPIO_OD
configure as output in open-drain mode without pull resistor
Definition: gpio.h:124
@ GPIO_IN_PU
configure as input with pull-up resistor
Definition: gpio.h:122
@ GPIO_OD_PU
configure as output in open-drain mode with pull resistor enabled
Definition: gpio.h:126
@ GPIO_IN_PD
configure as input with pull-down resistor
Definition: gpio.h:121
@ I2C_SPEED_NORMAL
normal mode: ~100 kbit/s
Definition: periph_cpu.h:278
@ I2C_SPEED_FAST_PLUS
fast plus mode: ~1000 kbit/s
Definition: periph_cpu.h:280
@ I2C_SPEED_LOW
low speed mode: ~10 kbit/s
Definition: periph_cpu.h:277
@ I2C_SPEED_HIGH
high speed mode: ~3400 kbit/s
Definition: periph_cpu.h:282
@ I2C_SPEED_FAST
fast mode: ~400 kbit/s
Definition: periph_cpu.h:279
uint_fast8_t spi_t
Default type for SPI devices.
Definition: spi.h:128
@ SPI_CLK_10MHZ
drive the SPI bus with 10MHz
Definition: periph_cpu.h:357
@ SPI_CLK_5MHZ
drive the SPI bus with 5MHz
Definition: periph_cpu.h:356
@ SPI_CLK_400KHZ
drive the SPI bus with 400KHz
Definition: periph_cpu.h:354
@ SPI_CLK_1MHZ
drive the SPI bus with 1MHz
Definition: periph_cpu.h:355
@ SPI_CLK_100KHZ
drive the SPI bus with 100KHz
Definition: periph_cpu.h:353
I2C configuration structure.
Definition: periph_cpu.h:299
pwr_reduction_t pwr
Power Management.
Definition: periph_cpu.h:301
i2c_speed_t speed
Configured bus speed, actual speed may be lower but never higher.
Definition: periph_cpu.h:304
TWI_t * dev
Pointer to hardware module registers.
Definition: periph_cpu.h:300
cpu_int_lvl_t int_lvl
Serial Interrupt Level.
Definition: periph_cpu.h:305
gpio_t sda_pin
SDA GPIO pin.
Definition: periph_cpu.h:302
gpio_t scl_pin
SCL GPIO pin.
Definition: periph_cpu.h:303
SPI device configuration.
Definition: periph_cpu.h:337
gpio_t miso_pin
pin used for MISO
Definition: periph_cpu.h:341
pwr_reduction_t pwr
Power Management.
Definition: periph_cpu.h:339
gpio_t mosi_pin
pin used for MOSI
Definition: periph_cpu.h:342
gpio_t ss_pin
pin used for SS line
Definition: periph_cpu.h:343
SPI_t * dev
pointer to the used SPI device
Definition: periph_cpu.h:338
gpio_t sck_pin
pin used for SCK
Definition: periph_cpu.h:340
Timer device configuration.
Definition: periph_cpu.h:264
pwr_reduction_t pwr
Power Management.
Definition: periph_cpu.h:266
TC0_t * dev
Pointer to the used as Timer device.
Definition: periph_cpu.h:265
timer_type_t type
Timer Type.
Definition: periph_cpu.h:267
UART device configuration.
Definition: periph_cpu.h:218
USART_t * dev
pointer to the used UART device
Definition: periph_cpu.h:219
cpu_int_lvl_t tx_int_lvl
TX Complete Interrupt Level.
Definition: periph_cpu.h:228
pwr_reduction_t pwr
Power Management.
Definition: periph_cpu.h:220
gpio_t tx_pin
pin used for TX
Definition: periph_cpu.h:222
cpu_int_lvl_t dre_int_lvl
Data Registry Empty Interrupt Level.
Definition: periph_cpu.h:229
gpio_t rx_pin
pin used for RX
Definition: periph_cpu.h:221
cpu_int_lvl_t rx_int_lvl
RX Complete Interrupt Level.
Definition: periph_cpu.h:227