ATxmega Core
Loading...
Searching...
No Matches
cpu.h
Go to the documentation of this file.
1// Author: Filipe Carvalho
2#ifndef _CPU_1V1_H_
3#define _CPU_1V1_H_
4
5/************************************************************************/
6/* Include external libraries */
7/************************************************************************/
8#include <avr/io.h>
9#include <avr/interrupt.h>
10
11/************************************************************************/
12/* Define if not defined */
13/************************************************************************/
14#ifndef bool
15 #define bool uint8_t
16#endif
17#ifndef true
18 #define true 1
19#endif
20#ifndef false
21 #define false 0
22#endif
23
24/************************************************************************/
25/* Common defines */
26/************************************************************************/
27#define INT_LEVEL_OFF 0
28#define INT_LEVEL_LOW 1
29#define INT_LEVEL_MED 2
30#define INT_LEVEL_HIGH 3
31
32/************************************************************************/
33/* CPU */
34/************************************************************************/
35#define PRESCALER_CPU_1 CLK_PSADIV_1_gc
36#define PRESCALER_CPU_2 CLK_PSADIV_2_gc
37#define PRESCALER_CPU_4 CLK_PSADIV_4_gc
38#define PRESCALER_CPU_8 CLK_PSADIV_8_gc
39#define PRESCALER_CPU_16 CLK_PSADIV_16_gc
40#define PRESCALER_CPU_32 CLK_PSADIV_32_gc
41#define PRESCALER_CPU_64 CLK_PSADIV_64_gc
42#define PRESCALER_CPU_128 CLK_PSADIV_128_gc
43#define PRESCALER_CPU_256 CLK_PSADIV_256_gc
44#define PRESCALER_CPU_512 CLK_PSADIV_512_gc
45
46#define OUT_PORT_CLK_PORTC 0
47#define OUT_PORT_CLK_PORTD 1
48
49bool cpu_config_clock(uint32_t cpu_freq, bool lock_clock, bool external_clock);
50void cpu_config_clock_32Khz(uint8_t prescaler, bool lock_clock);
51bool cpu_change_clock(uint32_t cpu_freq, bool lock_clock);
52
53void cpu_clk_output(bool stay_on_loop, bool pin4_instead_of_7, uint8_t out_port);
54
55void cpu_enable_int_level(uint8_t int_level);
56void cpu_disable_int_level(uint8_t int_level);
57
58/************************************************************************/
59/* Watchdog */
60/************************************************************************/
61#define PER_WDT_8ms WDT_PER_8CLK_gc
62#define PER_WDT_16ms WDT_PER_16CLK_gc
63#define PER_WDT_32ms WDT_PER_32CLK_gc
64#define PER_WDT_64ms WDT_PER_64CLK_gc
65#define PER_WDT_125ms WDT_PER_125CLK_gc
66#define PER_WDT_250ms WDT_PER_250CLK_gc
67#define PER_WDT_500ms WDT_PER_500CLK_gc
68#define PER_WDT_1s WDT_PER_1KCLK_gc
69#define PER_WDT_2s WDT_PER_2KCLK_gc
70#define PER_WDT_4s WDT_PER_4KCLK_gc
71#define PER_WDT_8s WDT_PER_8KCLK_gc
72
73void wdt_enable(uint8_t per);
74void wdt_disable(void);
75void wdt_reset_device(void);
76
77/************************************************************************/
78/* IO */
79/************************************************************************/
80#define PULL_IO_TRISTATE (0x0 << 3)
81#define PULL_IO_BUSHOLDER (0x1 << 3)
82#define PULL_IO_DOWN (0x2 << 3)
83#define PULL_IO_UP (0x3 << 3)
84
85#define SENSE_IO_EDGES_BOTH 0
86#define SENSE_IO_EDGE_RISING 1
87#define SENSE_IO_EDGE_FALLING 2
88#define SENSE_IO_LOW_LEVEL 3
89#define SENSE_IO_NO_INT_USED 0
90
91#define OUT_IO_DIGITAL (0x0 << 3)
92#define OUT_IO_WIREDOR (0x4 << 3)
93#define OUT_IO_WIREDAND (0x5 << 3)
94#define OUT_IO_WIREDORPULL (0x6 << 3)
95#define OUT_IO_WIREDANDPULL (0x7 << 3)
96
97#define IN_EN_IO_EN 1
98#define IN_EN_IO_DIS 0
99
100#define set_io(port, pin) port.OUTSET = 1 << pin
101#define clear_io(port, pin) port.OUTCLR = 1 << pin
102#define toggle_io(port, pin) port.OUTTGL = 1 << pin
103#define read_io(port, pin) (*(&port.IN) & (1 << pin))
104#define set_io_mask(port, mask) port.OUTSET = mask
105#define clear_io_mask(port, mask) port.OUTCLR = mask
106#define toggle_io_mask(port, mask) port.OUTTGL = mask
107
108void io_pin2in(PORT_t* port, uint8_t pin, uint8_t pull, uint8_t sense);
109void io_pin2out(PORT_t* port, uint8_t pin, uint8_t out, bool input_en);
110void io_pin2out_with_interrupt(PORT_t* port, uint8_t pin, uint8_t out, uint8_t sense);
111void io_set_int(PORT_t* port, uint8_t int_level, uint8_t int_n, uint8_t mask, bool reset_mask);
112
113/************************************************************************/
114/* Timer */
115/************************************************************************/
116#define TIMER_PRESCALER_DIV1 1
117#define TIMER_PRESCALER_DIV2 2
118#define TIMER_PRESCALER_DIV4 3
119#define TIMER_PRESCALER_DIV8 4
120#define TIMER_PRESCALER_DIV64 5
121#define TIMER_PRESCALER_DIV256 6
122#define TIMER_PRESCALER_DIV1024 7
123
124void timer_type0_enable(TC0_t* timer, uint8_t prescaler, uint16_t target_count, uint8_t int_level);
125void timer_type0_pwm(TC0_t* timer, uint8_t prescaler, uint16_t target_count, uint16_t duty_cycle_count, uint8_t int_level_ovf, uint8_t int_level_cca);
126void timer_type0_set_target(TC0_t* timer, uint16_t target_count);
127bool timer_type0_get_flag(TC0_t* timer);
128void timer_type0_reset_flag(TC0_t* timer);
129void timer_type0_stop(TC0_t* timer);
130void timer_type0_set_counter(TC0_t* timer, uint16_t counter);
131void timer_type0_wait(TC0_t* timer, uint8_t prescaler, uint16_t target_count);
132bool calculate_timer_16bits(uint32_t f_cpu, float freq, uint8_t * timer_prescaler, uint16_t * timer_target_count);
133
134void timer_type1_enable(TC1_t* timer, uint8_t prescaler, uint16_t target_count, uint8_t int_level);
135void timer_type1_set_target(TC1_t* timer, uint16_t target_count);
136bool timer_type1_get_flag(TC1_t* timer);
137void timer_type1_reset_flag(TC1_t* timer);
138void timer_type1_stop(TC1_t* timer);
139void timer_type1_set_counter(TC1_t* timer, uint16_t counter);
140void timer_type1_wait(TC1_t* timer, uint8_t prescaler, uint16_t target_count);
141
142/************************************************************************/
143/* EEPROM */
144/************************************************************************/
145bool eeprom_is_busy(void);
146
147uint8_t eeprom_rd_byte(uint16_t addr);
148void eeprom_wr_byte(uint16_t addr, uint8_t byte);
149
150void eeprom_wr_i16(uint16_t addr, int16_t _16b);
151void eeprom_wr_i32(uint16_t addr, int32_t _32b);
152int16_t eeprom_rd_i16(uint16_t addr);
153int32_t eeprom_rd_i32(uint16_t addr);
154
155/************************************************************************/
156/* ADC */
157/************************************************************************/
158#define RES_ADC_8BITS ADC_RESOLUTION_8BIT_gc
159#define RES_ADC_12BITS ADC_RESOLUTION_12BIT_gc
160
161#define REF_ADC_1V ADC_REFSEL_INT1V_gc | ADC_BANDGAP_bm // 10/11
162#define REF_ADC_VCC ADC_REFSEL_INTVCC_gc // VCC/1.6
163#define REF_ADC_PORTA ADC_REFSEL_AREFA_gc //
164#define REF_ADC_PORTB ADC_REFSEL_AREFB_gc //
165#define REF_ADC_VCCDIV2 ADC_REFSEL_INTVCC2_gc // VCC/2
166
167#define PRESCALER_ADC_DIV4 ADC_PRESCALER_DIV4_gc
168#define PRESCALER_ADC_DIV8 ADC_PRESCALER_DIV8_gc
169#define PRESCALER_ADC_DIV16 ADC_PRESCALER_DIV16_gc
170#define PRESCALER_ADC_DIV32 ADC_PRESCALER_DIV32_gc
171#define PRESCALER_ADC_DIV64 ADC_PRESCALER_DIV64_gc
172#define PRESCALER_ADC_DIV128 ADC_PRESCALER_DIV128_gc
173#define PRESCALER_ADC_DIV256 ADC_PRESCALER_DIV256_gc
174#define PRESCALER_ADC_DIV512 ADC_PRESCALER_DIV512_gc
175
176void adc_A_initialize_single_ended(uint8_t analog_reference);
177int16_t adc_A_read_channel(uint8_t index);
178void adc_A_calibrate_offset(uint8_t index);
179
180uint16_t adcA_unsigned_single_ended(ADC_t* adc, uint8_t res, uint8_t ref , uint8_t prescaler, uint8_t adc_pin, TC0_t* timer);
181
182#endif /* _CPU_1V1_H_ */
void cpu_enable_int_level(uint8_t int_level)
Definition cpu.c:183
void timer_type0_set_counter(TC0_t *timer, uint16_t counter)
Definition cpu.c:309
void timer_type1_stop(TC1_t *timer)
Definition cpu.c:383
void cpu_clk_output(bool stay_on_loop, bool pin4_instead_of_7, uint8_t out_port)
Definition cpu.c:134
bool timer_type0_get_flag(TC0_t *timer)
Definition cpu.c:293
void io_pin2out(PORT_t *port, uint8_t pin, uint8_t out, bool input_en)
Definition cpu.c:226
uint8_t eeprom_rd_byte(uint16_t addr)
Definition cpu.c:413
void timer_type0_wait(TC0_t *timer, uint8_t prescaler, uint16_t target_count)
Definition cpu.c:316
void eeprom_wr_i32(uint16_t addr, int32_t _32b)
Definition cpu.c:459
void eeprom_wr_i16(uint16_t addr, int16_t _16b)
Definition cpu.c:453
void timer_type0_enable(TC0_t *timer, uint8_t prescaler, uint16_t target_count, uint8_t int_level)
Definition cpu.c:269
void timer_type0_stop(TC0_t *timer)
Definition cpu.c:303
void timer_type1_set_target(TC1_t *timer, uint16_t target_count)
Definition cpu.c:369
int16_t adc_A_read_channel(uint8_t index)
Definition cpu.c:509
void timer_type0_reset_flag(TC0_t *timer)
Definition cpu.c:298
void adc_A_initialize_single_ended(uint8_t analog_reference)
Definition cpu.c:492
bool calculate_timer_16bits(uint32_t f_cpu, float freq, uint8_t *timer_prescaler, uint16_t *timer_target_count)
Definition cpu.c:326
void cpu_config_clock_32Khz(uint8_t prescaler, bool lock_clock)
Definition cpu.c:69
uint16_t adcA_unsigned_single_ended(ADC_t *adc, uint8_t res, uint8_t ref, uint8_t prescaler, uint8_t adc_pin, TC0_t *timer)
Definition cpu.c:533
bool cpu_change_clock(uint32_t cpu_freq, bool lock_clock)
Definition cpu.c:96
void timer_type0_set_target(TC0_t *timer, uint16_t target_count)
Definition cpu.c:289
bool timer_type1_get_flag(TC1_t *timer)
Definition cpu.c:373
void eeprom_wr_byte(uint16_t addr, uint8_t byte)
Definition cpu.c:429
bool eeprom_is_busy(void)
void timer_type1_enable(TC1_t *timer, uint8_t prescaler, uint16_t target_count, uint8_t int_level)
Definition cpu.c:361
bool cpu_config_clock(uint32_t cpu_freq, bool lock_clock, bool external_clock)
Definition cpu.c:11
void io_set_int(PORT_t *port, uint8_t int_level, uint8_t int_n, uint8_t mask, bool reset_mask)
Definition cpu.c:240
void wdt_reset_device(void)
Definition cpu.c:211
int16_t eeprom_rd_i16(uint16_t addr)
Definition cpu.c:467
void timer_type1_wait(TC1_t *timer, uint8_t prescaler, uint16_t target_count)
Definition cpu.c:396
void adc_A_calibrate_offset(uint8_t index)
Definition cpu.c:520
void io_pin2in(PORT_t *port, uint8_t pin, uint8_t pull, uint8_t sense)
Definition cpu.c:220
void cpu_disable_int_level(uint8_t int_level)
Definition cpu.c:189
void timer_type1_set_counter(TC1_t *timer, uint16_t counter)
Definition cpu.c:389
int32_t eeprom_rd_i32(uint16_t addr)
Definition cpu.c:475
void wdt_enable(uint8_t per)
Definition cpu.c:199
void io_pin2out_with_interrupt(PORT_t *port, uint8_t pin, uint8_t out, uint8_t sense)
Definition cpu.c:234
void wdt_disable(void)
Definition cpu.c:205
void timer_type1_reset_flag(TC1_t *timer)
Definition cpu.c:378
void timer_type0_pwm(TC0_t *timer, uint8_t prescaler, uint16_t target_count, uint16_t duty_cycle_count, uint8_t int_level_ovf, uint8_t int_level_cca)
Definition cpu.c:277