Permalink
Browse files

Initial draft boxcar code.

debug led works.
adc works
adc only updating if the phono jack detect pin is set works. (needed input pull
down)

doesn't work with memset!
  • Loading branch information...
1 parent 5ef1e22 commit 97e4714decc72df5d90a5da3435bcf52997a9500 @karlp committed May 22, 2012
Showing with 256 additions and 0 deletions.
  1. +74 −0 example/boxcar/Makefile
  2. +4 −0 example/boxcar/README
  3. +178 −0 example/boxcar/main.c
View
@@ -0,0 +1,74 @@
+TARGET=boxcar_f100
+PLATFORM=stm32f10x
+LINKER_BASENAME=stm32f100xb
+# TODO - unify stmlib and platform....
+VECTORS=stm32f100x
+STMLIB=stm32_stdperiph_f10x
+# all below here should not need to be touched...(much)
+
+
+EXECUTABLE=$(TARGET).elf
+BIN_IMAGE=$(TARGET).bin
+
+CC=arm-none-eabi-gcc
+OBJCOPY=arm-none-eabi-objcopy
+SIZE=arm-none-eabi-size
+
+DEBUG?=1
+STM32_BASE=../..
+STLINK_HOME=/home/karlp/src/stlink
+
+
+CFLAGS=-Os -mlittle-endian -mthumb
+CFLAGS+=-mcpu=cortex-m3
+# we use newlib
+CFLAGS+=-nostdlib
+# for better garbage collection
+CFLAGS+=-ffunction-sections
+# Warnings are awesome!
+CFLAGS+=-Wall -Wextra
+ifeq ($(DEBUG),1)
+ CFLAGS+=-g
+endif
+
+BOOT_FLASH?=1
+ifeq ($(BOOT_FLASH),1)
+ CFLAGS+=-Wl,-T,$(LINKER_BASENAME)_flash.ld
+else
+ CFLAGS+=-Wl,-T,$(LINKER_BASENAME)_sram.ld
+ CFLAGS+=-DCONFIG_BOOT_SRAM=1
+ # This keeps the standard system_xxx files from doing any damage...
+ CFLAGS+=-DVECT_TAB_SRAM
+endif
+
+LIBS_STM_PATH=$(STM32_BASE)/libs_stm
+
+# stm32l_discovery lib
+#CFLAGS+=-I$(LIBS_STM_PATH)/inc/base
+CFLAGS+=-I$(LIBS_STM_PATH)/inc/core_support
+CFLAGS+=-I$(LIBS_STM_PATH)/inc/device_support
+CFLAGS+=-I$(LIBS_STM_PATH)/inc/$(PLATFORM)
+
+LDFLAGS+=-L$(LIBS_STM_PATH)/build -l$(STMLIB)
+LDFLAGS+=-lc
+LDFLAGS+=-L$(STM32_BASE)/ld_scripts
+LDFLAGS+=-Wl,-Map=$(TARGET).map
+
+all: $(BIN_IMAGE)
+
+$(BIN_IMAGE): $(EXECUTABLE)
+ $(OBJCOPY) -O binary $^ $@
+ $(SIZE) $(EXECUTABLE)
+
+$(EXECUTABLE): main.c $(STM32_BASE)/startup/cm3_genstartup.c $(STM32_BASE)/startup/$(VECTORS)_vectors.c
+ $(CC) $(CFLAGS) $^ -o $@ $(LDFLAGS)
+
+clean:
+ rm -rf $(EXECUTABLE)
+ rm -rf $(BIN_IMAGE)
+ rm -rf *.map
+
+write: all
+ $(STLINK_HOME)/flash/st-flash write blah $(BIN_IMAGE) 0x08000000
+
+.PHONY: all clean write
View
@@ -0,0 +1,4 @@
+Personal project:
+See https://github.com/karlp/karlnet/tree/master/nodes/boxcar
+
+Eventually, when I can package libstm and so on better, it won't live here
View
@@ -0,0 +1,178 @@
+/*
+ * Karl Palsson, 2012
+ * boxcar project code.
+ * 2x analog probes, or digital outs
+ * 1x 802.15.4 module (MRF24J40xx)
+ * 1x DHT03 temp/humi sensor
+ * 1x debug led
+ * battery powered, so designed for lower power. (but not using the actual
+ * low power stm32L)
+ * board files at: https://github.com/karlp/karlnet/tree/master/nodes/boxcar
+ */
+
+#include <stdint.h>
+#include <string.h>
+
+/* libstm32vl_discovery headers */
+#include "stm32f10x_gpio.h"
+#include "stm32f10x_adc.h"
+#include "stm32f10x_rcc.h"
+#include "stm32f10x_exti.h"
+#include "stm32f10x_usart.h"
+
+
+#define GPIO_HIGH(a,b) a->BSRR = b
+#define GPIO_LOW(a,b) a->BRR = b
+#define GPIO_TOGGLE(a,b) a->ODR ^= b
+
+/* hardware configuration */
+
+#define LED_PORT GPIOA
+#define LED_DEBUG GPIO_Pin_2
+#define ADC_PORT GPIOA
+
+struct phono_adc_connector {
+ uint16_t last_reading;
+ uint8_t adc_channel;
+ uint16_t adc_detect_pin;
+ uint16_t adc_input_pin;
+};
+
+struct bstate {
+ bool led_on;
+ int blink_speed_ms;
+ uint64_t last_blink_time;
+ struct phono_adc_connector analog_channel1;
+ struct phono_adc_connector analog_channel2;
+};
+
+static inline void switch_leds_on(void)
+{
+ GPIO_HIGH(LED_PORT, LED_DEBUG);
+}
+
+static inline void switch_leds_off(void)
+{
+ GPIO_LOW(LED_PORT, LED_DEBUG);
+}
+
+/**
+ * Check what requirements F100 has for adc usage, and double check default
+ * startup clocks... (Compare with 32L code)
+ */
+void SystemInit(void)
+{
+}
+
+volatile uint64_t ksystick;
+
+uint64_t millis(void)
+{
+ return ksystick;
+}
+
+void SysTick_Handler(void)
+{
+ ksystick++;
+}
+
+void setup_gpios(struct bstate *st) {
+ /* Enable GPIOs clock */
+ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
+ /* Configure the GPIO_LED pins */
+ static GPIO_InitTypeDef gpio;
+ gpio.GPIO_Pin = LED_DEBUG;
+ gpio.GPIO_Mode = GPIO_Mode_Out_PP;
+ gpio.GPIO_Speed = GPIO_Speed_2MHz;
+ GPIO_Init(LED_PORT, &gpio);
+ GPIO_LOW(LED_PORT, LED_DEBUG);
+
+ // setup detection pins for phono jacks
+ gpio.GPIO_Mode = GPIO_Mode_IPD;
+ gpio.GPIO_Pin = st->analog_channel1.adc_detect_pin | st->analog_channel2.adc_detect_pin;
+ GPIO_Init(ADC_PORT, &gpio);
+}
+
+/**
+ * sets up gpios to be adc in, and the adc itself
+ */
+void setup_adc(struct bstate *st)
+{
+ static GPIO_InitTypeDef gpio;
+ gpio.GPIO_Pin = st->analog_channel1.adc_input_pin | st->analog_channel2.adc_input_pin;
+ gpio.GPIO_Mode = GPIO_Mode_AIN;
+ GPIO_Init(ADC_PORT, &gpio);
+
+ RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
+ RCC_ADCCLKConfig (RCC_PCLK2_Div6); // FIXME
+ //ADC_DeInit(ADC1);
+ // all defaults...
+ ADC_InitTypeDef adcinit;
+ ADC_StructInit(&adcinit);
+ ADC_Init(ADC1, &adcinit);
+ ADC_Cmd(ADC1, ENABLE);
+}
+
+/**
+ * Task to update the blinking heartbeat led in debug setups
+ * @param st
+ */
+void update_leds(struct bstate *st) {
+ if (millis() - st->blink_speed_ms > st->last_blink_time) {
+ if (st->led_on & 1) {
+ switch_leds_on();
+ } else {
+ switch_leds_off();
+ }
+ st->led_on ^= 1;
+ st->last_blink_time = millis();
+ }
+}
+
+/**
+ * Basic blocking read.
+ * Only reads if the io pin indicates a plug is connected
+ * @param adc
+ */
+void task_get_channel(struct phono_adc_connector *adc) {
+ if (GPIO_ReadInputDataBit(ADC_PORT, adc->adc_detect_pin)) {
+ // start and wait for adc to convert...
+ ADC_RegularChannelConfig(ADC1, adc->adc_channel, 1, ADC_SampleTime_239Cycles5);
+ ADC_Cmd(ADC1, ENABLE);
+ ADC_SoftwareStartConvCmd(ADC1, ENABLE);
+ while (ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET)
+ ;
+ adc->last_reading = ADC_GetConversionValue(ADC1);
+ }
+}
+
+
+int main(void)
+{
+ RCC_ClocksTypeDef clockinfo;
+ RCC_GetClocksFreq(&clockinfo);
+ // regardless of clock speed this gives us 1000 ticks per second
+ SysTick_Config(clockinfo.SYSCLK_Frequency / 1000);
+ struct bstate state;
+ // FIXME - karl, you need to sort this shit out once and for all...
+ //memset(&state, 0, sizeof(state));
+ // These are from my schematic....
+ state.analog_channel1.adc_channel = ADC_Channel_5;
+ state.analog_channel1.adc_detect_pin = GPIO_Pin_6;
+ state.analog_channel1.adc_input_pin = GPIO_Pin_5;
+ state.analog_channel2.adc_channel = ADC_Channel_3;
+ state.analog_channel2.adc_detect_pin = GPIO_Pin_4;
+ state.analog_channel2.adc_input_pin = GPIO_Pin_3;
+
+ state.blink_speed_ms = 400;
+ state.last_blink_time = millis();
+
+ setup_gpios(&state);
+ setup_adc(&state);
+
+ while (1) {
+ update_leds(&state);
+ task_get_channel(&state.analog_channel1);
+ task_get_channel(&state.analog_channel2);
+ }
+}

0 comments on commit 97e4714

Please sign in to comment.