diff --git a/car_cfmw/.gitignore b/car_cfmw/.gitignore new file mode 100644 index 00000000000..86c36a2d66f --- /dev/null +++ b/car_cfmw/.gitignore @@ -0,0 +1,2 @@ +tags +bin diff --git a/car_cfmw/Makefile b/car_cfmw/Makefile new file mode 100644 index 00000000000..81b5bbd78bd --- /dev/null +++ b/car_cfmw/Makefile @@ -0,0 +1,70 @@ +SRC_DIR=src +BIN_DIR=bin + +CAR_FMW_OBJ=$(BIN_DIR)/car_fmw.o +CAR_FMW_ELF=$(BIN_DIR)/car_fmw.elf +CAR_FMW_BIN=$(BIN_DIR)/car_fmw.bin + +LIB_CARHW_DIR=../car_hw/ +LIB_CARHW_OBJ=$(LIB_CARHW_DIR)/bin/car_hw.o +LIB_CARHW_LIB_DIR=$(LIB_CARHW_DIR)/src/lib/ + +CC=arm-none-eabi-gcc +AS=arm-none-eabi-as +LD=arm-none-eabi-ld +OBJ_COPY=arm-none-eabi-objcopy + +INCLUDES=-I$(LIB_CARHW_LIB_DIR)/cmsis \ + -I$(LIB_CARHW_LIB_DIR)/stdperiph \ + -I$(LIB_CARHW_LIB_DIR)/stm32f4d \ + -I$(LIB_CARHW_LIB_DIR)/usb_device \ + -I$(LIB_CARHW_LIB_DIR)/usb_otg \ + -I$(LIB_CARHW_LIB_DIR)/usb_vcp \ + -I$(LIB_CARHW_LIB_DIR)/usart \ + -I$(LIB_CARHW_DIR)/src/include \ + -I$(SRC_DIR) + +DEFINES=-DUSE_STM32_DISCOVERY=1 -DUSE_STDPERIPH_DRIVER=1 -DSTM32F4XX=1 -DHSE_VALUE=8000000 +CFLAGS=-g -nostdlib -ffreestanding -O0 \ + -mcpu=cortex-m3 -mfloat-abi=soft -mthumb \ + $(DEFINES) +ASMFLAGS=-g -mthumb + +LD_ALL_DEPS=$(LD) -r $(filter %.o,$^) -o $@ +CC_ALL_DEPS=$(CC) $(CFLAGS) -c $(INCLUDES) $(filter %.c,$^) -o $@ +AS_ALL_DEPS=$(AS) $(ASMFLAGS) $(filter %.s,$^) -o $@ + +dirhs=$(wildcard $(1)/*.h) +dircs=$(wildcard $(1)/*.c) +dircs_to_prefxd_objs=\ + $(patsubst $(1)/%.c,$(BIN_DIR)/$(2)%.o,$(call dircs,$1)) + +CARFMW_OBJ_PREFIX=carfmw_ + +$(CAR_FMW_BIN): $(CAR_FMW_ELF) $(BIN_DIR) + $(OBJ_COPY) -O binary $< $@ + +$(CAR_FMW_ELF): $(CAR_FMW_OBJ) $(LIB_CARHW_OBJ) + $(CC) $(CFLAGS) $^ -T $(SRC_DIR)/stm32_flash.ld -o $@ + +$(CAR_FMW_OBJ): \ + $(call dircs_to_prefxd_objs,$(SRC_DIR),$(CARFMW_OBJ_PREFIX)) + $(LD_ALL_DEPS) + +$(BIN_DIR)/$(CARFMW_OBJ_PREFIX)%.o: $(SRC_DIR)/%.c \ + $(call dirhs,$(SRC_DIR)) + $(CC_ALL_DEPS) + +$(LIB_CARHW_OBJ): + make -C $(LIB_CARHW_DIR) + +$(BIN_DIR): + mkdir -p $(BIN_DIR) + +clean: + rm -rf bin/* + +tags: + ctags -R * $(LIB_CARHW_DIR) + +.PHONY: clean tags diff --git a/car_cfmw/README.md b/car_cfmw/README.md new file mode 100644 index 00000000000..7b7925b0cef --- /dev/null +++ b/car_cfmw/README.md @@ -0,0 +1,2 @@ +Car firmware project for low level experimentation +using C. diff --git a/car_cfmw/src/main.c b/car_cfmw/src/main.c new file mode 100644 index 00000000000..870f2359e94 --- /dev/null +++ b/car_cfmw/src/main.c @@ -0,0 +1,101 @@ +#include +#include +#include +#include +#include +#include +#include + +size_t strlen(const char *str) +{ + size_t len = 0; + + while (str[len]) + ++len; + return len; +} + +char digit_to_char(uint8_t digit) +{ + if (digit <= 9) + return '0' + digit; + else return 'A' + (digit - 10); +} + +// Should allow to store up to "65535\0" +char uint16_str[6] = "65535"; + +char *uint16_to_str(uint16_t val) +{ + int dix = 4; + for (; dix >= 0; --dix) { + uint8_t digit = (uint8_t)(val % 10); + uint16_str[dix] = digit_to_char(digit); + val = val / 10; + if (val == 0) + break; + } + if (dix == -1) + dix = 0; + return &uint16_str[dix]; +} + +void sonar_prog_dist_to_conn(void) +{ + while (1) { + uint16_t dist = 0; + + car_leds_clear_all(); + dist = car_sonar_get_dist(90); + + if (dist == CAR_SONAR_DIST_ERR) + car_led_set(CAR_LED_RED, true); + else { + car_led_set(CAR_LED_GREEN, true); + const char *str = uint16_to_str(dist); + car_conn_snd_buf(strlen(str), (int)str); + car_conn_snd_byte('\n'); + } + car_time_wait(1000); + } +} + +void sonar_prog_rotate(void) +{ + uint8_t degree = 0; + while (1) { + uint16_t dist = 0; + + car_leds_clear_all(); + dist = car_sonar_get_dist(degree); + if (degree == 0 || + degree == 45 || + degree == 90 || + degree == (90 + 45) || + degree == 180) + car_time_wait(500); + + degree += 5; + if (degree > 180) + degree = 0; + if (dist == CAR_SONAR_DIST_ERR) + car_led_set(CAR_LED_RED, true); + else + car_led_set(CAR_LED_GREEN, true); + car_time_wait(300); + } +} + +int main(void) +{ + car_time_init(); + car_leds_init(); + car_engine_init(); + car_user_btn_init(NULL); + car_conn_init(); + car_sonar_init(); + + sonar_prog_rotate(); + //sonar_prog_dist_to_conn(); + // Also we can run usart test from here +} diff --git a/car_cfmw/src/stm32_flash.ld b/car_cfmw/src/stm32_flash.ld new file mode 100644 index 00000000000..9a10d57b3c1 --- /dev/null +++ b/car_cfmw/src/stm32_flash.ld @@ -0,0 +1,177 @@ +/* +***************************************************************************** +** +** File : stm32_flash.ld +** +** Abstract : Linker script for STM32F407VG Device with +** 1024KByte FLASH, 192KByte RAM +** +** Set heap size, stack size and stack location according +** to application requirements. +** +** Set memory bank area and size if external memory is used. +** +** Target : STMicroelectronics STM32 +** +** Environment : Atollic TrueSTUDIO(R) +** +** Distribution: The file is distributed “as is,” without any warranty +** of any kind. +** +** (c)Copyright Atollic AB. +** You may use this file as-is or modify it according to the needs of your +** project. Distribution of this file (unmodified or modified) is not +** permitted. Atollic AB permit registered Atollic TrueSTUDIO(R) users the +** rights to distribute the assembled, compiled & linked contents of this +** file as part of an application binary file, provided that it is built +** using the Atollic TrueSTUDIO(R) toolchain. +** +***************************************************************************** +*/ + +/* Entry Point */ +ENTRY(Reset_Handler) + +/* + * _estack is the highest address of user mode stack + * (sp initial value, stack grows down). + * We have 192K RAM on our MCU and _estack should be 0x20030000. + * But our MCU maps only 128K SRAM at 0x20000000 after boot. + * We need to perform some memory remapping to use more. + * This isn't implemented yet. + */ +_estack = 0x20020000; /* end of 128K SRAM region */ + +/* Generate a link error if heap and stack don't fit into RAM */ +_Min_Heap_Size = 0; /* required amount of heap */ +_Min_Stack_Size = 0x400; /* required amount of stack */ + +/* Specify the memory areas */ +MEMORY +{ + FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 1024K + RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 128K + MEMORY_B1 (rx) : ORIGIN = 0x60000000, LENGTH = 0K +} + +/* Define output sections */ +SECTIONS +{ + /* The startup code goes first into FLASH */ + .isr_vector : + { + . = ALIGN(4); + KEEP(*(.isr_vector)) /* Startup code */ + . = ALIGN(4); + } >FLASH + + /* The program code and other data goes into FLASH */ + .text : + { + . = ALIGN(4); + *(.text) /* .text sections (code) */ + *(.text*) /* .text* sections (code) */ + *(.rodata) /* .rodata sections (constants, strings, etc.) */ + *(.rodata*) /* .rodata* sections (constants, strings, etc.) */ + *(.glue_7) /* glue arm to thumb code */ + *(.glue_7t) /* glue thumb to arm code */ + *(.eh_frame) + + KEEP (*(.init)) + KEEP (*(.fini)) + + . = ALIGN(4); + _etext = .; /* define a global symbols at end of code */ + } >FLASH + + + .ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) } >FLASH + .ARM : { + __exidx_start = .; + *(.ARM.exidx*) + __exidx_end = .; + } >FLASH + + .preinit_array : + { + PROVIDE_HIDDEN (__preinit_array_start = .); + KEEP (*(.preinit_array*)) + PROVIDE_HIDDEN (__preinit_array_end = .); + } >FLASH + .init_array : + { + PROVIDE_HIDDEN (__init_array_start = .); + KEEP (*(SORT(.init_array.*))) + KEEP (*(.init_array*)) + PROVIDE_HIDDEN (__init_array_end = .); + } >FLASH + .fini_array : + { + PROVIDE_HIDDEN (__fini_array_start = .); + KEEP (*(.fini_array*)) + KEEP (*(SORT(.fini_array.*))) + PROVIDE_HIDDEN (__fini_array_end = .); + } >FLASH + + /* used by the startup to initialize data */ + _sidata = .; + + /* Initialized data sections goes into RAM, load LMA copy after code */ + .data : AT ( _sidata ) + { + . = ALIGN(4); + _sdata = .; /* create a global symbol at data start */ + *(.data) /* .data sections */ + *(.data*) /* .data* sections */ + + . = ALIGN(4); + _edata = .; /* define a global symbol at data end */ + } >RAM + + /* Uninitialized data section */ + . = ALIGN(4); + .bss : + { + /* This is used by the startup in order to initialize the .bss secion */ + _sbss = .; /* define a global symbol at bss start */ + __bss_start__ = _sbss; + *(.bss) + *(.bss*) + *(COMMON) + + . = ALIGN(4); + _ebss = .; /* define a global symbol at bss end */ + __bss_end__ = _ebss; + } >RAM + + /* User_heap_stack section, used to check that there is enough RAM left */ + ._user_heap_stack : + { + . = ALIGN(4); + PROVIDE ( end = . ); + PROVIDE ( _end = . ); + . = . + _Min_Heap_Size; + . = . + _Min_Stack_Size; + . = ALIGN(4); + } >RAM + + /* MEMORY_bank1 section, code must be located here explicitly */ + /* Example: extern int foo(void) __attribute__ ((section (".mb1text"))); */ + .memory_b1_text : + { + *(.mb1text) /* .mb1text sections (code) */ + *(.mb1text*) /* .mb1text* sections (code) */ + *(.mb1rodata) /* read-only data (constants) */ + *(.mb1rodata*) + } >MEMORY_B1 + + /* Remove information from the standard libraries */ + /DISCARD/ : + { + libc.a ( * ) + libm.a ( * ) + libgcc.a ( * ) + } + + .ARM.attributes 0 : { *(.ARM.attributes) } +} diff --git a/car_hw/Makefile b/car_hw/Makefile index 3e96486f015..0e7b193bb1b 100644 --- a/car_hw/Makefile +++ b/car_hw/Makefile @@ -9,6 +9,7 @@ LIB_STM32F4D_DIR=$(LIB_DIR)/stm32f4d LIB_USB_DEV_DIR=$(LIB_DIR)/usb_device LIB_USB_OTG_DIR=$(LIB_DIR)/usb_otg LIB_USB_VCP_DIR=$(LIB_DIR)/usb_vcp +LIB_USART_DIR=$(LIB_DIR)/usart LIB_CMSIS_OBJ=$(BIN_DIR)/libcmsis.o LIB_STDPERIPH_OBJ=$(BIN_DIR)/libstdperiph.o @@ -16,6 +17,7 @@ LIB_STM32F4D_OBJ=$(BIN_DIR)/libstm32f4d.o LIB_USB_DEV_OBJ=$(BIN_DIR)/libusbdevice.o LIB_USB_OTG_OBJ=$(BIN_DIR)/libusbotg.o LIB_USB_VCP_OBJ=$(BIN_DIR)/libusbvcp.o +LIB_USART_OBJ=$(BIN_DIR)/libusart.o CARHW_OBJ=$(BIN_DIR)/car_hw.o @@ -32,6 +34,7 @@ INCLUDES=-I$(LIB_CMSIS_DIR) \ -I$(LIB_USB_DEV_DIR) \ -I$(LIB_USB_OTG_DIR) \ -I$(LIB_USB_VCP_DIR) \ + -I$(LIB_USART_DIR) \ -I$(USB_CONF_DIR) \ -I$(SRC_DIR) \ -I$(SRC_DIR)/include @@ -57,7 +60,7 @@ CAR_OBJ_PREFIX=car_ $(CARHW_OBJ): $(CAR_OBJ) $(LIB_STM32F4D_OBJ) \ $(LIB_CMSIS_OBJ) $(LIB_STDPERIPH_OBJ) \ $(LIB_USB_DEV_OBJ) $(LIB_USB_OTG_OBJ) \ - $(LIB_USB_VCP_OBJ) + $(LIB_USB_VCP_OBJ) $(LIB_USART_OBJ) $(LD) -r $^ -o $@ $(CAR_OBJ): \ diff --git a/car_hw/src/car_sonar.c b/car_hw/src/car_sonar.c new file mode 100644 index 00000000000..481bb3060a0 --- /dev/null +++ b/car_hw/src/car_sonar.c @@ -0,0 +1,64 @@ +#include +#include +#include + +/* + * Sonar reference: + * http://www.dfrobot.com/wiki/index.php?title=URM37_V4.0_Ultrasonic_Sensor_(SKU:SEN0001) + */ + +static uint8_t sonar_degree(uint8_t degree) +{ + // urm37 accepts values [0;46] + // so we have 47 steps. + // But we don't use the last 46 value. + // It gives us angle > 180. + if (degree > 180) + degree = 180; + return degree / 4; +} + +#define SONAR_CMD_GET_DIST 0x22 + +static uint8_t sonar_cmd[4]; +static void sonar_snd_dist_req(uint8_t rot_degree) +{ + size_t i = 0; + sonar_cmd[0] = SONAR_CMD_GET_DIST; + sonar_cmd[1] = sonar_degree(rot_degree); + sonar_cmd[2] = sonar_cmd[3] = 0; + + for (; i < 3; ++i) + sonar_cmd[3] += sonar_cmd[i]; + usart_send_data(USART3_ID, sonar_cmd, 4); +} + +static uint8_t sonar_resp[4]; +static uint16_t sonar_wait_dist_resp(void) +{ + size_t i = 0; + uint8_t sum = 0; + + usart_rcv_data(USART3_ID, sonar_resp, 4); + sum = sonar_resp[0] + sonar_resp[1] + sonar_resp[2]; + if ((sonar_resp[0] != SONAR_CMD_GET_DIST) + || (sum != sonar_resp[3])) + return CAR_SONAR_DIST_ERR; + + return ((uint16_t)sonar_resp[1] << 8) + sonar_resp[2]; +} + +uint16_t car_sonar_get_dist(uint8_t rot_degree) +{ + sonar_snd_dist_req(rot_degree); + return sonar_wait_dist_resp(); +} + +void car_sonar_init(void) +{ + usart_init(USART3_ID); + // Sonar boots in ~2.2 secs + car_time_wait(3000); + // Some really needed magical tweak + sonar_snd_dist_req(0); +} diff --git a/car_hw/src/include/car_sonar.h b/car_hw/src/include/car_sonar.h new file mode 100644 index 00000000000..aa192e11d73 --- /dev/null +++ b/car_hw/src/include/car_sonar.h @@ -0,0 +1,24 @@ +#pragma once + +#include + +/* + * Pins commutation: + * MCU | URM37 | Servo | + * ---------------------------- + * PC11 | TXD | - | + * PC10 | RXD | - | + * GND | GND | - | + * NRST | NRST | - | + * 5V | +5V | - | + * - | MOTO | Orange | + * GND | - | Brown | + * 5V | - | Red | + */ + +#define CAR_SONAR_DIST_ERR 0xffff + +void car_sonar_init(void); +// returns CAR_SONAR_DIST_ERR on error +// else distance in centimeters +uint16_t car_sonar_get_dist(uint8_t rot_degree); diff --git a/car_hw/src/include/stm32f4xx_it.h b/car_hw/src/stm32f4xx_it.h similarity index 100% rename from car_hw/src/include/stm32f4xx_it.h rename to car_hw/src/stm32f4xx_it.h