-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatch.c
181 lines (157 loc) · 4.13 KB
/
patch.c
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "inc/stm32f407xx.h"
#define _PIN_HI( PORT, PIN ) GPIO##PORT->BSRR = (1u<<PIN)
#define _PIN_LOW( PORT, PIN ) GPIO##PORT->BSRR = (0x10000u<<PIN)
#define _PIN_TOGGLE( PORT, PIN )do{\
uint32_t odr = GPIO##PORT->ODR;\
GPIO##PORT->BSRR = ((odr & (1u<<PIN)) << 16u) | (~odr & (1u<<PIN));\
}while(0)
#define PIN_HI( CONFIG ) _PIN_HI( CONFIG )
#define PIN_LOW( CONFIG ) _PIN_LOW( CONFIG )
#define PIN_TOGGLE( CONFIG ) _PIN_TOGGLE( CONFIG )
#define TIM_DEV TIM9
#define TIM_BUS RCC->APB2ENR
#define TIM_CLOCK RCC_APB2ENR_TIM9EN
#define TIM_IRQN TIM1_BRK_TIM9_IRQn
#define TIM_PERIOD 0xFFFFu
#if ( GUIDER == 1 ) || ( FINDER == 1 )
#define FAN_1 F, 4 /* guider or finder */
#else
#define FAN_1 F, 5
#define FAN_2 E, 1 /* printer case rear fan */
#endif
void __attribute__((section (".patch_location"))) TIM1_BRK_TIM9_IRQHandler( void )
{
uint32_t sr = TIM_DEV->SR;
if( sr & TIM_SR_CC1IF )
{
PIN_LOW( FAN_1 );
TIM_DEV->SR &= ~TIM_SR_CC1IF;
}
if( sr & TIM_SR_UIF )
{
PIN_HI( FAN_1 );
TIM_DEV->SR &= ~TIM_SR_UIF;
}
}
void __attribute__((section (".patch_location"))) hw_config_timer( void )
{
/* TIM9 */
TIM_BUS |= TIM_CLOCK;
(void)TIM_BUS;
TIM_DEV->PSC = (25-1); /* 168000000/25/65536 = ~102Hz */
TIM_DEV->CR1 &= (uint16_t)(~TIM_CR1_CKD);
/* no clock div */
TIM_DEV->CR1 &= ~TIM_CR1_CKD;
TIM_DEV->EGR |= TIM_EGR_UG;
/* set low priority */
NVIC_SetPriority( TIM_IRQN, (1UL << __NVIC_PRIO_BITS) - 1UL );
NVIC_EnableIRQ( TIM_IRQN );
}
#define FAN_PWM_TO_PERC( _pwm ) ((_pwm*1000)/TIM_PERIOD)
#define FAN_MAX (900) /* 90% */
#define FAN_MIN (100) /* 10% */
void __attribute__((section (".patch_location"))) timer_pwm_set( uint32_t value )
{
if( !( TIM_BUS & TIM_CLOCK ) )
{
hw_config_timer();
}
if( ( FAN_PWM_TO_PERC( value ) >= FAN_MAX ) || ( FAN_PWM_TO_PERC( value ) <= FAN_MIN ) )
{
TIM_DEV->DIER &= ~( TIM_DIER_UIE | TIM_DIER_CC1IE );
TIM_DEV->CR1 &= ~TIM_CR1_CEN;
if( FAN_PWM_TO_PERC( value ) <= FAN_MIN )
PIN_LOW( FAN_1 );
else
PIN_HI( FAN_1 );
}
else
{
TIM_DEV->CCR1 = value;
if( !(TIM_DEV->CR1 & TIM_CR1_CEN ) )
{
TIM_DEV->DIER |= ( TIM_DIER_UIE | TIM_DIER_CC1IE );
TIM_DEV->CR1 |= TIM_CR1_CEN;
}
}
}
int __attribute__((section (".patch_location"))) __atoi( char *s )
{
int r = 0;
while( (*s >= '0') && (*s <= '9') )
{
r = r*10 + (*s++ - '0');
}
return r;
}
int __attribute__((section (".patch_location"))) gcode_int_param( char t )
{
/* firmware specific offsets */
uint16_t *p_index_value = (void*)PRINTER_DOFFSET;
char *p_data_buffer = (void*)PRINTER_DBUFFER; /* command buffer */
/* get Gcode buffer pointer */
char *s = &p_data_buffer[0x60*(*p_index_value)];
while( *s )
{
if( *s++ == t )
return __atoi( s );
}
return -1;
}
/* set fan speed */
void __attribute__((section (".patch_location"))) hook_m106( void *port, uint32_t pin )
{
int speed = gcode_int_param( 'S' );
int index = gcode_int_param( 'P' );
/* if no param was found or param too hight */
if( ( speed < 0 ) || ( speed > 255 ) )
speed = 255;
if( index <= 0 ) /* print cooling fan */
{
speed *= 257; /* from 0...255 => 0...65535 */
timer_pwm_set( speed );
}
#if !defined( GUIDER ) && !defined( FINDER )
else if( index == 1 ) /* rear fan */
{
/* disable rear fan logic */
#if defined( REAR_CASE_ENABLE )
*(uint8_t*)REAR_CASE_ENABLE = 0;
#endif
if( !speed )
PIN_LOW( FAN_2 ); /* turn it off */
else
PIN_HI( FAN_2 ); /* any positive value => on */
}
#endif
return;
}
/* fan off */
void __attribute__((section (".patch_location"))) hook_m107( void *port, uint32_t pin )
{
int index = gcode_int_param( 'P' );
if( index <= 0 ) /* print cooling fan */
{
timer_pwm_set( 0 );
}
#if !defined( GUIDER ) && !defined( FINDER )
else if( index == 1 ) /* rear fan */
{
/* disable rear fan logic */
#if defined( REAR_CASE_ENABLE )
*(uint8_t*)REAR_CASE_ENABLE = 0;
#endif
PIN_LOW( FAN_2 );
}
#endif
return;
}
/* ............. for test purpose only ............. */
void SystemInit( void )
{
SCB->VTOR = 0x8010000;//FLASH_BASE;
}
int main( void )
{
for(;;);
}