altos: Work on MAX6691 driver
authorKeith Packard <keithp@keithp.com>
Fri, 12 Apr 2019 06:54:37 +0000 (23:54 -0700)
committerKeith Packard <keithp@keithp.com>
Sun, 21 Apr 2019 23:54:54 +0000 (16:54 -0700)
src/drivers/ao_max6691.c [new file with mode: 0644]
src/drivers/ao_max6691.h [new file with mode: 0644]
src/stm/stm32l.h
src/telefireone-v2.0/Makefile
src/telefireone-v2.0/ao_pins.h
src/telefireone-v2.0/ao_telefireone.c

diff --git a/src/drivers/ao_max6691.c b/src/drivers/ao_max6691.c
new file mode 100644 (file)
index 0000000..89e8618
--- /dev/null
@@ -0,0 +1,199 @@
+/*
+ * Copyright © 2019 Keith Packard <keithp@keithp.com>
+ *
+ * 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.
+ */
+
+#include "ao.h"
+#include "ao_max6691.h"
+
+#define cat(a,b)       a ## b
+
+#define AO_MAX6691_CCR         (AO_MAX6691_TIMER->cat(ccr, AO_MAX6691_CH))
+
+#if 0
+static uint16_t        ao_max6691_data[8];
+
+void
+ao_max6691_sample(void)
+{
+       /* Get the DMA engine ready */
+       ao_dma_set_transfer(AO_MAX6691_DMA,
+                           &AO_MAX6691_CCR,
+                           &ao_max6691_data,
+                           8,
+                           (0 << STM_DMA_CCR_MEM2MEM) |
+                           (STM_DMA_CCR_PL_MEDIUM << STM_DMA_CCR_PL) |
+                           (STM_DMA_CCR_MSIZE_16 << STM_DMA_CCR_MSIZE) |
+                           (STM_DMA_CCR_PSIZE_16 << STM_DMA_CCR_PSIZE) |
+                           (1 << STM_DMA_CCR_MINC) |
+                           (0 << STM_DMA_CCR_PINC) |
+                           (0 << STM_DMA_CCR_CIRC) |
+                           (STM_DMA_CCR_DIR_PER_TO_MEM << STM_DMA_CCR_DIR));
+       ao_dma_start(AO_MAX6691_DMA);
+
+       /* Prod the max6691 */
+       ao_gpio_set(AO_MAX6691_PORT, AO_MAX6691_PIN, 0);
+       ao_arch_nop();
+       ao_gpio_set(AO_MAX6691_PORT, AO_MAX6691_PIN, 1);
+
+       /* Switch the pin to timer input mode */
+       stm_afr_set(AO_MAX6691_GPIO, AO_MAX6691_PIN, STM_AFR_AF1);
+
+       ao_arch_block_interrupts();
+       while (!ao_dma_done(AO_MAX6691_DMA))
+               ao_sleep(&ao_dma_done(AO_MAX6691_DMA));
+       ao_arch_release_interrupts();
+
+       /* Switch pin back to output mode */
+       stm_moder_set(AO_MAX6691_GPIO, AO_MAX6691_PIN, STM_MODER_OUTPUT);
+
+       /* Mark DMA done */
+       ao_dma_done_transfer(AO_MAX6691_DMA);
+}
+#endif
+
+static void
+ao_max6691_test(void)
+{
+       int i;
+
+       printf("Testing MAX6691\n");
+       /* Prod the max6691 */
+       ao_set_output(AO_MAX6691_GPIO, AO_MAX6691_PIN, 0);
+       for (i = 0; i < 1000; i++) {
+               ao_led_on(AO_LED_ARMED);
+               ao_gpio_set(AO_MAX6691_GPIO, AO_MAX6691_PIN, 0);
+               ao_delay(AO_MS_TO_TICKS(250));
+               ao_led_off(AO_LED_ARMED);
+               ao_gpio_set(AO_MAX6691_GPIO, AO_MAX6691_PIN, 1);
+               ao_delay(AO_MS_TO_TICKS(250));
+       }
+#if 0
+       uint16_t        tick;
+       uint16_t        i, j;
+       uint8_t         p, v;
+
+       for (i = 0; i < 100; i++)
+               ao_arch_nop();
+       ao_gpio_set(AO_MAX6691_GPIO, AO_MAX6691_PIN, 1);
+       ao_arch_nop();
+       ao_set_input(AO_MAX6691_GPIO, AO_MAX6691_PIN);
+       i = 0;
+       p = 1;
+       for (tick = 0; i < 8 && tick < 10000; tick++)
+       {
+               v = ao_gpio_get(AO_MAX6691_GPIO, AO_MAX6691_PIN);
+               if (v != p) {
+                       ao_max6691_data[i++] = tick;
+                       p = v;
+               }
+       }
+       for (j = 0; j < i; i++)
+               printf("%d: %5u\n", j, ao_max6691_data[j]);
+#endif
+       printf("Done\n");
+}
+
+static const struct ao_cmds ao_max6691_cmds[] = {
+       { ao_max6691_test,      "q\0Thermistor test" },
+       { 0, NULL },
+};
+
+
+void
+ao_max6691_init(void)
+{
+       ao_cmd_register(&ao_max6691_cmds[0]);
+
+#if 0
+       struct stm_tim234       *tim = &AO_MAX6691_TIMER;
+
+       stm_rcc.apb1enr |= (1 << AO_MAX6691_TIMER_ENABLE);
+
+       tim->cr1 = 0;
+       tim->psc = 0;
+       tim->cnt = 0;
+
+       tim->ccmr1 = ((0 << STM_TIM234_CCMR1_IC2F) |
+                     (
+
+
+       tim->ccmr2 = ((0 << STM_TIM234_CCMR2_OC4CE) |
+                     (STM_TIM234_CCMR2_OC4M_PWM_MODE_1 << STM_TIM234_CCMR2_OC4M) |
+                     (0 << STM_TIM234_CCMR2_OC4PE) |
+                     (0 << STM_TIM234_CCMR2_OC4FE) |
+                     (STM_TIM234_CCMR2_CC4S_OUTPUT << STM_TIM234_CCMR2_CC4S) |
+
+                     (0 << STM_TIM234_CCMR2_OC3CE) |
+                     (STM_TIM234_CCMR2_OC3M_PWM_MODE_1 << STM_TIM234_CCMR2_OC3M) |
+                     (0 << STM_TIM234_CCMR2_OC3PE) |
+                     (0 << STM_TIM234_CCMR2_OC3FE) |
+                     (STM_TIM234_CCMR2_CC3S_OUTPUT << STM_TIM234_CCMR2_CC3S));
+       tim->ccer = ((1 << STM_TIM234_CCER_CC1E) |
+                    (0 << STM_TIM234_CCER_CC1P) |
+                    (1 << STM_TIM234_CCER_CC2E) |
+                    (0 << STM_TIM234_CCER_CC2P) |
+                    (1 << STM_TIM234_CCER_CC3E) |
+                    (0 << STM_TIM234_CCER_CC3P) |
+                    (1 << STM_TIM234_CCER_CC4E) |
+                    (0 << STM_TIM234_CCER_CC4P));
+
+       tim->egr = 0;
+
+       tim->sr = 0;
+       tim->dier = 0;
+       tim->smcr = 0;
+       tim->cr2 = ((0 << STM_TIM234_CR2_TI1S) |
+                   (STM_TIM234_CR2_MMS_RESET<< STM_TIM234_CR2_MMS) |
+                   (0 << STM_TIM234_CR2_CCDS));
+
+       tim->cr1 = ((STM_TIM234_CR1_CKD_1 << STM_TIM234_CR1_CKD) |
+                   (0 << STM_TIM234_CR1_ARPE) |
+                   (STM_TIM234_CR1_CMS_EDGE << STM_TIM234_CR1_CMS) |
+                   (STM_TIM234_CR1_DIR_UP << STM_TIM234_CR1_DIR) |
+                   (0 << STM_TIM234_CR1_OPM) |
+                   (0 << STM_TIM234_CR1_URS) |
+                   (0 << STM_TIM234_CR1_UDIS) |
+                   (1 << STM_TIM234_CR1_CEN));
+
+       tim->cr2 = ((0 << STM_TIM234_CR2_TI1S) |
+                   (STM_TIM234_CR2_MMS_RESET << STM_TIM234_CR2_MMS) |
+                   (0 << STM_TIM234_CR2_CCDS));
+
+       tim->dier = ((0 << STM_TIM234_DIER_TDE) |
+                    (0 << STM_TIM234_DIER_CC4DE) |
+                    (0 << STM_TIM234_DIER_CC3DE) |
+                    (1 << STM_TIM234_DIER_CC2DE) |
+                    (0 << STM_TIM234_DIER_CC1DE) |
+                    (0 << STM_TIM234_DIER_TIE) |
+                    (0 << STM_TIM234_DIER_CC4IE) |
+                    (0 << STM_TIM234_DIER_CC3IE) |
+                    (0 << STM_TIM234_DIER_CC2IE) |
+                    (0 << STM_TIM234_DIER_CC1IE) |
+                    (0 << STM_TIM234_DIER_UIE));
+
+       tim->egr = ((0 << STM_TIM234_EGR_TG) |
+                   (0 << STM_TIM234_EGR_CC4G) |
+                   (0 << STM_TIM234_EGR_CC3G) |
+                   (1 << STM_TIM234_EGR_CC2G) |
+                   (0 << STM_TIM234_EGR_CC1G) |
+                   (0 << STM_TIM234_EGR_UG));
+
+       tim->ccmr1 = ((0 << STM_TIM234_CCMR_IC2F) |
+                     (0 << STM_TIM234_CCMR_IC2PSC) |
+                     (2 << STM_TIM234_CCMR_CC2S) |
+                     (
+
+       stm_ospeedr_set(AO_MAX6691_GPIO, AO_MAX6691_PIN, STM_OSPEEDR_40MHz);
+#endif
+       ao_enable_output(AO_MAX6691_GPIO, AO_MAX6691_PIN, 1);
+}
diff --git a/src/drivers/ao_max6691.h b/src/drivers/ao_max6691.h
new file mode 100644 (file)
index 0000000..0c422d9
--- /dev/null
@@ -0,0 +1,21 @@
+/*
+ * Copyright © 2019 Keith Packard <keithp@keithp.com>
+ *
+ * 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.
+ */
+
+#ifndef _AO_MAX6691_H_
+#define _AO_MAX6691_H_
+
+void
+ao_max6691_init(void);
+
+#endif /* _AO_MAX6691_H_ */
index 71955988cc5ccffa1cebaf400f74e364f804f0a9..5741a846c573a3b65f41599e90bb2d2df8481473 100644 (file)
@@ -1799,6 +1799,14 @@ extern struct stm_tim234 stm_tim2, stm_tim3, stm_tim4;
 #define  STM_TIM234_SMCR_SMS_EXTERNAL_CLOCK    7
 #define  STM_TIM234_SMCR_SMS_MASK              7
 
 #define  STM_TIM234_SMCR_SMS_EXTERNAL_CLOCK    7
 #define  STM_TIM234_SMCR_SMS_MASK              7
 
+#define STM_TIM234_DIER_TDE            14
+#define STM_TIM234_DIER_CC4DE          12
+#define STM_TIM234_DIER_CC3DE          11
+#define STM_TIM234_DIER_CC2DE          10
+#define STM_TIM234_DIER_CC1DE          9
+#define STM_TIM234_DIER_UDE            8
+
+#define STM_TIM234_DIER_TIE            6
 #define STM_TIM234_DIER_CC4IE          4
 #define STM_TIM234_DIER_CC3IE          3
 #define STM_TIM234_DIER_CC2IE          2
 #define STM_TIM234_DIER_CC4IE          4
 #define STM_TIM234_DIER_CC3IE          3
 #define STM_TIM234_DIER_CC2IE          2
index 0b93c1cbc94f147abf24e5529c783ee8080cc0b3..85f06de46a92ca8ad638b91ad8701b54f67e6b78 100644 (file)
@@ -55,7 +55,8 @@ ALTOS_SRC = \
        ao_radio_cmac_cmd.c \
        ao_log.c \
        ao_log_fireone.c \
        ao_radio_cmac_cmd.c \
        ao_log.c \
        ao_log_fireone.c \
-       ao_ads124s0x.c
+       ao_ads124s0x.c \
+       ao_max6691.c
 
 PRODUCT_SRC = \
        ao_telefireone.c
 
 PRODUCT_SRC = \
        ao_telefireone.c
index a4b506176229680c82626ed94afb0fa2f7c422bd..6b153f923a0fb3ff2b799f1e644bb3b005428d45 100644 (file)
@@ -260,4 +260,12 @@ struct ao_adc {
                         (1 << AO_PAD_ADC_PYRO) | \
                         (1 << AO_PAD_ADC_BATT))
 
                         (1 << AO_PAD_ADC_PYRO) | \
                         (1 << AO_PAD_ADC_BATT))
 
+/* MAX6691 thermistor chip */
+#define AO_MAX6691_GPIO                (&stm_gpiob)
+#define AO_MAX6691_PIN         3
+#define AO_MAX6691_TIMER       (&stm_tim2)
+#define AO_MAX6691_CH          2
+#define AO_MAX6691_DMA         7
+
+
 #endif /* _AO_PINS_H_ */
 #endif /* _AO_PINS_H_ */
index a4883234bcd037d9c48758e1ce8feb42a048a000..ff2f11d8b36ea4f34efe97e6ba53c14cc4a15c67 100644 (file)
@@ -23,6 +23,7 @@
 #include <ao_radio_cmac_cmd.h>
 #include <ao_eeprom.h>
 #include <ao_ads124s0x.h>
 #include <ao_radio_cmac_cmd.h>
 #include <ao_eeprom.h>
 #include <ao_ads124s0x.h>
+#include <ao_max6691.h>
 
 static void
 set_logging(void)
 
 static void
 set_logging(void)
@@ -55,6 +56,8 @@ main(void)
 
        ao_adc_init();
 
 
        ao_adc_init();
 
+       ao_max6691_init();
+
        ao_eeprom_init();
        ao_storage_init();
        ao_log_init();
        ao_eeprom_init();
        ao_storage_init();
        ao_log_init();