car_hw: urm37 and rotating servo C driver
+ new C firmware with two car_sonar module usage examples also used as manual tests.
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
tags
|
||||
bin
|
||||
@@ -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
|
||||
@@ -0,0 +1,2 @@
|
||||
Car firmware project for low level experimentation
|
||||
using C.
|
||||
@@ -0,0 +1,101 @@
|
||||
#include <car_leds.h>
|
||||
#include <car_engine.h>
|
||||
#include <car_time.h>
|
||||
#include <car_user_btn.h>
|
||||
#include <car_conn.h>
|
||||
#include <car_sonar.h>
|
||||
#include <usart_test.h>
|
||||
|
||||
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
|
||||
}
|
||||
@@ -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) }
|
||||
}
|
||||
+4
-1
@@ -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): \
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
#include <car_sonar.h>
|
||||
#include <car_time.h>
|
||||
#include <usart.h>
|
||||
|
||||
/*
|
||||
* 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);
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/*
|
||||
* 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);
|
||||
Reference in New Issue
Block a user