From 118e4acde5fd6a5d62925cfbb12b86f5ba734868 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Sun, 14 Jul 2019 20:06:44 +0200 Subject: [PATCH] Aircon: More peripherals... --- aircon/Makefile | 4 ++-- aircon/README | 4 ++-- aircon/config.h | 12 ++++++++++ aircon/main.c | 62 +++++++++++++++++++++++-------------------------- 4 files changed, 45 insertions(+), 37 deletions(-) diff --git a/aircon/Makefile b/aircon/Makefile index e7d6e38..15d7d8e 100644 --- a/aircon/Makefile +++ b/aircon/Makefile @@ -1,6 +1,6 @@ ROOT=.. BINARY=main -OBJS=main.o # ds18b20.o -LIB_OBJS=util-debug.o +OBJS=main.o +LIB_OBJS=util-debug.o ds18b20.o include $(ROOT)/mk/bluepill.mk diff --git a/aircon/README b/aircon/README index 80f672b..fab35a7 100644 --- a/aircon/README +++ b/aircon/README @@ -19,8 +19,8 @@ BluePill LED | PC13 GND | | PC14 5V | bypass opto-coupler | PC15 PB9 | | PA0 PB8 | - | PA1 PB7 | TIM3 CH2 - IR LED* - | PA2 PB6 | TIM3 CH1 - fan control opto-coupler + | PA1 PB7 | TIM4 CH2 - IR LED* + | PA2 PB6 | TIM4 CH1 - fan control opto-coupler | PA3 PB5 | | PA4 PB4 | | PA5 PB3 | diff --git a/aircon/config.h b/aircon/config.h index 5e7ab4b..77a5f33 100644 --- a/aircon/config.h +++ b/aircon/config.h @@ -28,3 +28,15 @@ #define MODBUS_OUR_ADDRESS 42 #define MODBUS_BAUD_RATE 19200 + +// DS18B20 library parameters + +#define DS_TIMER TIM3 +#define DS_GPIO GPIOA +#define DS_PIN GPIO7 +#define DS_DMA DMA1 +#define DS_DMA_CH 6 +#define DS_NUM_SENSORS 8 + +#define DS_DEBUG +#define DS_DEBUG2 diff --git a/aircon/main.c b/aircon/main.c index 75afedf..09d563a 100644 --- a/aircon/main.c +++ b/aircon/main.c @@ -42,6 +42,9 @@ static void clock_setup(void) static void gpio_setup(void) { + // Switch JTAG off to free up pins + gpio_primary_remap(AFIO_MAPR_SWJ_CFG_JTAG_OFF_SW_ON, 0); + // PA6 = RS485 TX enable gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO6); gpio_clear(GPIOA, GPIO6); @@ -55,18 +58,18 @@ static void gpio_setup(void) gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO10); // PB0 = bypass LED* - gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO0); + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO0); gpio_set(GPIOB, GPIO0); // PB1 = IR RC active LED* - gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO1); + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO1); gpio_set(GPIOB, GPIO1); - // PB6 = TIM3_CH1 fan control opto-coupler - gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO7); + // PB6 = TIM4_CH1 fan control opto-coupler + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO6); - // PB7 = TIM3_CH2 IR LED - gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO7); + // PB7 = TIM4_CH2 IR LED + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN, GPIO7); // PB10 = TXD3 for RS485 gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO10); @@ -116,11 +119,10 @@ static void usart_setup(void) usart_enable(USART1); } -#if 0 - -static bool bypass_active; -static byte pwm; +// TIM4 will run on CPU clock, it will overflow with frequency 38 kHz (IR modulation frequency) +#define T4_CYCLE ((CPU_CLOCK_MHZ * 1000000 + 37999) / 38000) +#if 0 static void show_temperature(void) { debug_putc('#'); @@ -136,49 +138,43 @@ static void show_temperature(void) debug_printf(" %d", pwm); debug_puts("\r\n"); } +#endif static void pwm_init(void) { - timer_set_prescaler(TIM4, 3); // clock = 72 MHz / 2 = 36 MHz + timer_set_prescaler(TIM4, 0); timer_set_mode(TIM4, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP); timer_disable_preload(TIM4); - timer_set_period(TIM4, 255); // PWM frequency = 18 MHz / 256 = 70.3125 kHz FIXME + timer_set_period(TIM4, T4_CYCLE - 1); + + // 50% PWM for the IR LED + //timer_set_oc_mode(TIM4, TIM_OC2, TIM_OCM_FORCE_HIGH); // will be TIM_OCM_PWM1 when transmitting + timer_set_oc_mode(TIM4, TIM_OC2, TIM_OCM_PWM1); // FIXME + timer_set_oc_value(TIM4, TIM_OC2, T4_CYCLE / 2); + timer_set_oc_polarity_high(TIM4, TIM_OC2); + timer_enable_oc_output(TIM4, TIM_OC2); + + // PWM for controlling fan timer_set_oc_mode(TIM4, TIM_OC1, TIM_OCM_PWM1); - timer_set_oc_value(TIM4, TIM_OC1, 1); - pwm = 1; - /* - * 1 0.03 - * 4 0.48 - * 8 1.54 - * 12 2.71 - * 16 3.99 - * 24 6.34 - * 32 7.85 - * 64 7.95 - * 128 8.02 - * 255 - */ + timer_set_oc_value(TIM4, TIM_OC1, T4_CYCLE * 20 / 256); timer_set_oc_polarity_high(TIM4, TIM_OC1); - timer_enable_counter(TIM4); timer_enable_oc_output(TIM4, TIM_OC1); - gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO6); + timer_enable_counter(TIM4); } -#endif - int main(void) { clock_setup(); gpio_setup(); tick_setup(); usart_setup(); - // pwm_init(); - - // ds_init(); + pwm_init(); debug_puts("Hello, world!\n"); + ds_init(); + for (;;) { gpio_toggle(GPIOC, GPIO13); -- 2.39.2