summaryrefslogtreecommitdiffhomepage
path: root/analog/motor-power-avr/src/mp_pwm_LR_.h
blob: 2fe44d654ba295fb2fd2c1faadaabba568ea4377 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#ifndef mp_pwm_LR__h
#define mp_pwm_LR__h
/* mp_pwm_LR_.h */
/*  {{{
 *
 * Copyright (C) 2008 Pierre Prot
 *
 * APBTeam:
 *        Web: http://apbteam.org/
 *      Email: team AT apbteam DOT org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * }}} */

#include "common.h"
#include "io.h"

#include "modules/utils/utils.h"

// Control outputs of H-bridges
// _L_/_R_ : Left or Right bridge
// A/B : bridge leg
// H/L : High or Low side

// _L_AH : PORTC6
#define _L_AH_1 PORTC |= 0x40
#define _L_AH_0 PORTC &= ~0x40
// _L_AL : PORTC7
#define _L_AL_1 PORTC |= 0x80
#define _L_AL_0 PORTC &= ~0x80
// _L_BH : PORTC4
#define _L_BH_1 PORTC |= 0x10
#define _L_BH_0 PORTC &= ~0x10
// _L_BL : PORTC5
#define _L_BL_1 PORTC |= 0x20
#define _L_BL_0 PORTC &= ~0x20


// _R_AH : PORTD6
#define _R_AH_1 PORTD |= 0x40
#define _R_AH_0 PORTD &= ~0x40
// _R_AL : PORTD7
#define _R_AL_1 PORTD |= 0x80
#define _R_AL_0 PORTD &= ~0x80
// _R_BH : PORTC2
#define _R_BH_1 PORTC |= 0x04
#define _R_BH_0 PORTC &= ~0x04
// _R_BL : PORTC3
#define _R_BL_1 PORTC |= 0x08
#define _R_BL_0 PORTC &= ~0x08

// Timer for _L_ and _R_ control
#define TCNT_L_	TCNT0
#define TCNT_R_	TCNT2
#define OCR_L_	OCR0
#define OCR_R_	OCR2
#define TCCR_L_	TCCR0
#define TCCR_R_	TCCR2

// timer configuration
// for 57.21kHz : prescaler = 0 : CSx2:0 = 0x01
// for 7.68kHz  : prescaler = 8 : CSx2:0 = 0x02
#define TCCR_LR_CFG (regv (FOC0, WGM00, COM01, COM00, WGM01, CS02, CS01, CS00, \
			      0,     0,     0,     0,     0,    0,    1,    0))

// timer interrupts configuration
#define TIMSK_LR_CFG (regv (OCIE2, TOIE2, TICIE1, OCIE1A, OCIE1B, TOIE1, OCIE0, TOIE0, \
			        1,     1,      0,      0,      0,      0,     1,     1))

// PWM max, min, and offset values
#define PWM_MIN_LR_ 0x10
#define PWM_MAX_LR_ 0xF0
#define PWM_OFFSET_LR_ 0x00

// Current limitation
#define CURLIM_MAX 0x80
#define OCR_CurLim_L_ OCR1A
#define OCR_CurLim_R_ OCR1B

// Vectors
#define ILIM_R_vect INT0_vect
#define ILIM_L_vect INT1_vect
#define L_OVF_vect  TIMER0_OVF_vect
#define R_OVF_vect  TIMER2_OVF_vect
#define L_COMP_vect TIMER0_COMP_vect
#define R_COMP_vect TIMER2_COMP_vect

// Command states
#define CMD_STATE_DIR_0   0x00
#define CMD_STATE_DIR_1   0x01
#define CMD_STATE_BRAKE   0x02
#define CMD_STATE_HIGH_Z  0x03

// functions
void init_timer_LR_(void);
void init_curLim (void);
uint8_t get_curLim_temp (uint8_t temperature);
uint8_t get_curLim_bat (uint8_t battery);
void update_curLim(void);
void launch_envTest(void);
void setCurLim_soft(uint8_t curLim);


#endif /* mp_pwm_LR__h */