From 1aed2eb5c7d477a2f3d4fada22980041aba97cb8 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Fri, 23 Aug 2013 11:22:10 -0700 Subject: altos/lpc: Stop using burst mode for LPC ADC Burst mode doesn't stop after one round of conversions, so we end up getting incorrect values in whatever the last conversion register is. Just use single conversions and take an interrupt per channel. Also, slow down the ADC so that our values are more stable -- just need to make sure we get the whole conversion sequence done 100 times a second. Signed-off-by: Keith Packard --- src/lpc/ao_adc_lpc.c | 119 ++++++++++++++++++++------------------------------- 1 file changed, 46 insertions(+), 73 deletions(-) (limited to 'src') diff --git a/src/lpc/ao_adc_lpc.c b/src/lpc/ao_adc_lpc.c index 874d2d29..7005f86e 100644 --- a/src/lpc/ao_adc_lpc.c +++ b/src/lpc/ao_adc_lpc.c @@ -50,89 +50,65 @@ #define AO_ADC_7 0 #endif -#if AO_ADC_7 -# define AO_ADC_LAST 7 -#else -# if AO_ADC_6 -# define AO_ADC_LAST 6 -# else -# if AO_ADC_5 -# define AO_ADC_LAST 5 -# else -# if AO_ADC_4 -# define AO_ADC_LAST 4 -# else -# if AO_ADC_3 -# define AO_ADC_LAST 3 -# else -# if AO_ADC_2 -# define AO_ADC_LAST 2 -# else -# if AO_ADC_1 -# define AO_ADC_LAST 1 -# else -# if AO_ADC_0 -# define AO_ADC_LAST 0 -# endif -# endif -# endif -# endif -# endif -# endif -# endif -#endif - -#define AO_ADC_MASK ((AO_ADC_0 << 0) | \ - (AO_ADC_1 << 1) | \ - (AO_ADC_2 << 2) | \ - (AO_ADC_3 << 3) | \ - (AO_ADC_4 << 4) | \ - (AO_ADC_5 << 5) | \ - (AO_ADC_6 << 6) | \ - (AO_ADC_7 << 7)) - -#define AO_ADC_CLKDIV (AO_LPC_SYSCLK / 4500000) - -static uint8_t ao_adc_ready; +#define AO_ADC_NUM (AO_ADC_0 + AO_ADC_1 + AO_ADC_2 + AO_ADC_3 + \ + AO_ADC_4 + AO_ADC_5 + AO_ADC_6 + AO_ADC_7) -#define sample(id) (*out++ = lpc_adc.dr[id] >> 1) - -void lpc_adc_isr(void) -{ - uint32_t stat = lpc_adc.stat; - uint16_t *out; - - /* Turn off burst mode */ - lpc_adc.cr = 0; - lpc_adc.stat = 0; +/* ADC clock is divided by this value + 1, which ensures that + * the ADC clock will be strictly less than 4.5MHz as required + */ +#define AO_ADC_CLKDIV (AO_LPC_SYSCLK / 450000) - /* Store converted values in packet */ +static uint8_t ao_adc_ready; +static uint8_t ao_adc_sequence; - out = (uint16_t *) &ao_data_ring[ao_data_head].adc; +static const uint8_t ao_adc_mask_seq[AO_ADC_NUM] = { #if AO_ADC_0 - sample(0); + 1 << 0, #endif #if AO_ADC_1 - sample(1); + 1 << 1, #endif #if AO_ADC_2 - sample(2); + 1 << 2, #endif #if AO_ADC_3 - sample(3); + 1 << 3, #endif #if AO_ADC_4 - sample(4); + 1 << 4, #endif #if AO_ADC_5 - sample(5); + 1 << 6, #endif #if AO_ADC_6 - sample(6); + 1 << 6, #endif #if AO_ADC_7 - sample(7); + 1 << 7, #endif +}; + +#define sample(id) (*out++ = (uint16_t) lpc_adc.dr[id] >> 1) + +static inline void lpc_adc_start(void) { + lpc_adc.cr = ((ao_adc_mask_seq[ao_adc_sequence] << LPC_ADC_CR_SEL) | + (AO_ADC_CLKDIV << LPC_ADC_CR_CLKDIV) | + (0 << LPC_ADC_CR_BURST) | + (LPC_ADC_CR_CLKS_11 << LPC_ADC_CR_CLKS) | + (LPC_ADC_CR_START_NOW << LPC_ADC_CR_START)); +} + +void lpc_adc_isr(void) +{ + uint16_t *out; + + /* Store converted value in packet */ + out = (uint16_t *) &ao_data_ring[ao_data_head].adc; + out[ao_adc_sequence] = (uint16_t) lpc_adc.gdr >> 1; + if (++ao_adc_sequence < AO_ADC_NUM) { + lpc_adc_start(); + return; + } AO_DATA_PRESENT(AO_DATA_ADC); if (ao_data_present == AO_DATA_ALL) { @@ -157,7 +133,7 @@ void lpc_adc_isr(void) /* - * Start the ADC sequence using the DMA engine + * Start the ADC sequence using burst mode */ void ao_adc_poll(void) @@ -165,11 +141,8 @@ ao_adc_poll(void) if (!ao_adc_ready) return; ao_adc_ready = 0; - - lpc_adc.cr = ((AO_ADC_MASK << LPC_ADC_CR_SEL) | - (AO_ADC_CLKDIV << LPC_ADC_CR_CLKDIV) | - (1 << LPC_ADC_CR_BURST) | - (LPC_ADC_CR_CLKS_11 << LPC_ADC_CR_CLKS)); + ao_adc_sequence = 0; + lpc_adc_start(); } static void @@ -202,8 +175,8 @@ ao_adc_init(void) lpc_scb.sysahbclkctrl |= (1 << LPC_SCB_SYSAHBCLKCTRL_ADC); lpc_scb.pdruncfg &= ~(1 << LPC_SCB_PDRUNCFG_ADC_PD); - /* Enable interrupt when last channel is complete */ - lpc_adc.inten = (1 << AO_ADC_LAST); + /* Enable interrupt when channel is complete */ + lpc_adc.inten = (1 << LPC_ADC_INTEN_ADGINTEN); lpc_nvic_set_enable(LPC_ISR_ADC_POS); lpc_nvic_set_priority(LPC_ISR_ADC_POS, AO_LPC_NVIC_CLOCK_PRIORITY); @@ -232,7 +205,7 @@ ao_adc_init(void) ao_enable_analog(0, 23, 7); #endif - lpc_adc.cr = ((AO_ADC_MASK << LPC_ADC_CR_SEL) | + lpc_adc.cr = ((0 << LPC_ADC_CR_SEL) | (AO_ADC_CLKDIV << LPC_ADC_CR_CLKDIV) | (0 << LPC_ADC_CR_BURST) | (LPC_ADC_CR_CLKS_11 << LPC_ADC_CR_CLKS)); -- cgit v1.2.3 From e908eb090fc2aaa03b35dc37c3e008b05ad44d80 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Fri, 23 Aug 2013 11:24:18 -0700 Subject: altos: Use installed arm compiler for LPC Signed-off-by: Keith Packard --- src/lpc/Makefile.defs | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src') diff --git a/src/lpc/Makefile.defs b/src/lpc/Makefile.defs index b63bdd12..9e87cee1 100644 --- a/src/lpc/Makefile.defs +++ b/src/lpc/Makefile.defs @@ -12,6 +12,8 @@ SAT=/opt/cortex SAT_CLIB=$(SAT)/lib/pdclib-cortex-m0.a SAT_CFLAGS=-I$(SAT)/include +#CC=/opt/arm-gcc-bits/bin/arm-none-eabi-gcc + ifndef VERSION include ../Version endif -- cgit v1.2.3 From 10f88c46df9a266f62452dc25275c79a3bb0653d Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Sat, 17 Aug 2013 17:43:18 +0200 Subject: altos: Set default LPC stack to 512 bytes, Em to 384 bytes The default for lpc has been raised to 512 bytes, but Em doesn't have enough RAM for that. Signed-off-by: Keith Packard --- src/easymini-v0.1/ao_pins.h | 2 ++ src/lpc/ao_arch.h | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/easymini-v0.1/ao_pins.h b/src/easymini-v0.1/ao_pins.h index 6f102dbe..c09fb4c2 100644 --- a/src/easymini-v0.1/ao_pins.h +++ b/src/easymini-v0.1/ao_pins.h @@ -18,6 +18,8 @@ #define HAS_BEEP 1 #define HAS_LED 1 +#define AO_STACK_SIZE 384 + #define IS_FLASH_LOADER 0 /* Crystal on the board */ diff --git a/src/lpc/ao_arch.h b/src/lpc/ao_arch.h index a8d3cfc4..d04bf2c8 100644 --- a/src/lpc/ao_arch.h +++ b/src/lpc/ao_arch.h @@ -24,7 +24,9 @@ * LPC11U14 definitions and code fragments for AltOS */ -#define AO_STACK_SIZE 320 +#ifndef AO_STACK_SIZE +#define AO_STACK_SIZE 512 +#endif #define AO_LED_TYPE uint16_t -- cgit v1.2.3 From aa2948803d33dbee6f1eab30370178252df2b56d Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Sat, 17 Aug 2013 17:45:06 +0200 Subject: altos: Wake up on LPC usart ISR only once Instead of waking up after every character, wait until the FIFO is empty to reduce overhead Signed-off-by: Keith Packard --- src/lpc/ao_serial_lpc.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/lpc/ao_serial_lpc.c b/src/lpc/ao_serial_lpc.c index c0424699..b34de704 100644 --- a/src/lpc/ao_serial_lpc.c +++ b/src/lpc/ao_serial_lpc.c @@ -49,21 +49,25 @@ _ao_serial_tx_start(void) void lpc_usart_isr(void) { + uint8_t wake_input = 0; (void) lpc_usart.iir_fcr; while (lpc_usart.lsr & (1 << LPC_USART_LSR_RDR)) { char c = lpc_usart.rbr_thr; if (!ao_fifo_full(ao_usart_rx_fifo)) ao_fifo_insert(ao_usart_rx_fifo, c); - ao_wakeup(&ao_usart_rx_fifo); - if (stdin) - ao_wakeup(&ao_stdin_ready); + wake_input = 1; } if (lpc_usart.lsr & (1 << LPC_USART_LSR_THRE)) { ao_usart_tx_avail = LPC_USART_TX_FIFO_SIZE; _ao_serial_tx_start(); ao_wakeup(&ao_usart_tx_fifo); } + if (wake_input) { + ao_wakeup(&ao_usart_rx_fifo); + if (stdin) + ao_wakeup(&ao_stdin_ready); + } } int -- cgit v1.2.3 From b363a628fc6137c3395a48ef13de7a799ec3e2c3 Mon Sep 17 00:00:00 2001 From: Keith Packard Date: Wed, 22 May 2013 19:31:15 -0600 Subject: altos: MS5607 pressure computation for low temperatures was wrong MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Second correction only applies to temps < -15°C, not 15°C. Signed-off-by: Keith Packard --- src/drivers/ao_ms5607_convert.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/drivers/ao_ms5607_convert.c b/src/drivers/ao_ms5607_convert.c index e61d19ed..bfb952a4 100644 --- a/src/drivers/ao_ms5607_convert.c +++ b/src/drivers/ao_ms5607_convert.c @@ -42,11 +42,14 @@ ao_ms5607_convert(struct ao_ms5607_sample *sample, struct ao_ms5607_value *value int32_t TEMPM = TEMP - 2000; int64_t OFF2 = (61 * (int64_t) TEMPM * (int64_t) TEMPM) >> 4; int64_t SENS2 = 2 * (int64_t) TEMPM * (int64_t) TEMPM; - if (TEMP < 1500) { + if (TEMP < -1500) { int32_t TEMPP = TEMP + 1500; - int64_t TEMPP2 = TEMPP * TEMPP; - OFF2 = OFF2 + 15 * TEMPP2; - SENS2 = SENS2 + 8 * TEMPP2; + /* You'd think this would need a 64-bit int, but + * that would imply a temperature below -327.67°C... + */ + int32_t TEMPP2 = TEMPP * TEMPP; + OFF2 = OFF2 + (int64_t) 15 * TEMPP2; + SENS2 = SENS2 + (int64_t) 8 * TEMPP2; } TEMP -= T2; OFF -= OFF2; -- cgit v1.2.3