mirror of
https://github.com/eddyem/tsys01.git
synced 2026-02-01 21:05:01 +03:00
add starting code for controller board
This commit is contained in:
parent
5741d2d2a7
commit
cba53e39a5
5
STM32/TSYS_controller/2DO
Normal file
5
STM32/TSYS_controller/2DO
Normal file
@ -0,0 +1,5 @@
|
||||
- CAN bus: PB8 (Rx), PB9 (Tx)
|
||||
- USB bus: PA11 (DM), PA12 (DP)
|
||||
- ADC inputs: PA0 (V12/4.93), PA1 (V5/2), PA3 (I12 - 1V/A), PA6 (V3.3/2)
|
||||
- controller CAN address: PA13..PA15 (0..2 bits); 0 - master, other address - slave
|
||||
|
||||
145
STM32/TSYS_controller/Makefile
Normal file
145
STM32/TSYS_controller/Makefile
Normal file
@ -0,0 +1,145 @@
|
||||
BINARY = tsys01
|
||||
BOOTPORT ?= /dev/ttyUSB0
|
||||
BOOTSPEED ?= 9600
|
||||
# MCU FAMILY
|
||||
FAMILY = F0
|
||||
# MCU code
|
||||
MCU = F042x6
|
||||
# hardware definitions
|
||||
DEFS := -DUSARTNUM=1 -DI2CPINS=B6B7
|
||||
#DEFS += -DCHECK_TMOUT
|
||||
#DEFS += -DEBUG
|
||||
# change this linking script depending on particular MCU model,
|
||||
# for example, if you have STM32F103VBT6, you should write:
|
||||
LDSCRIPT = ld/stm32f042k.ld
|
||||
|
||||
INDEPENDENT_HEADERS=
|
||||
|
||||
FP_FLAGS ?= -msoft-float
|
||||
ASM_FLAGS = -mthumb -mcpu=cortex-m0 -march=armv6-m -mtune=cortex-m0
|
||||
ARCH_FLAGS = $(ASM_FLAGS) $(FP_FLAGS)
|
||||
|
||||
###############################################################################
|
||||
# Executables
|
||||
OPREFIX ?= /opt/bin/arm-none-eabi
|
||||
#PREFIX ?= /usr/x86_64-pc-linux-gnu/arm-none-eabi/gcc-bin/7.3.0/arm-none-eabi
|
||||
PREFIX ?= $(OPREFIX)
|
||||
|
||||
RM := rm -f
|
||||
RMDIR := rmdir
|
||||
CC := $(PREFIX)-gcc
|
||||
LD := $(PREFIX)-gcc
|
||||
AR := $(PREFIX)-ar
|
||||
AS := $(PREFIX)-as
|
||||
OBJCOPY := $(OPREFIX)-objcopy
|
||||
OBJDUMP := $(OPREFIX)-objdump
|
||||
GDB := $(OPREFIX)-gdb
|
||||
STFLASH := $(shell which st-flash)
|
||||
STBOOT := $(shell which stm32flash)
|
||||
|
||||
###############################################################################
|
||||
# Source files
|
||||
OBJDIR = mk
|
||||
LDSCRIPT ?= $(BINARY).ld
|
||||
SRC := $(wildcard *.c)
|
||||
OBJS := $(addprefix $(OBJDIR)/, $(SRC:%.c=%.o))
|
||||
STARTUP = $(OBJDIR)/startup.o
|
||||
OBJS += $(STARTUP)
|
||||
DEPS := $(OBJS:.o=.d)
|
||||
|
||||
INC_DIR ?= ../inc
|
||||
|
||||
INCLUDE := -I$(INC_DIR)/F0 -I$(INC_DIR)/cm
|
||||
LIB_DIR := $(INC_DIR)/ld
|
||||
|
||||
###############################################################################
|
||||
# C flags
|
||||
CFLAGS += -O2 -g -MD -D__thumb2__=1
|
||||
CFLAGS += -Wall -Wextra -Wshadow -Wimplicit-function-declaration
|
||||
CFLAGS += -Wredundant-decls $(INCLUDE)
|
||||
# -Wmissing-prototypes -Wstrict-prototypes
|
||||
CFLAGS += -fno-common -ffunction-sections -fdata-sections
|
||||
|
||||
###############################################################################
|
||||
# Linker flags
|
||||
LDFLAGS += --static -nostartfiles
|
||||
#--specs=nano.specs
|
||||
LDFLAGS += -L$(LIB_DIR)
|
||||
LDFLAGS += -T$(LDSCRIPT)
|
||||
LDFLAGS += -Wl,-Map=$(OBJDIR)/$(BINARY).map
|
||||
LDFLAGS += -Wl,--gc-sections
|
||||
|
||||
###############################################################################
|
||||
# Used libraries
|
||||
LDLIBS += -Wl,--start-group -lc -lgcc -Wl,--end-group
|
||||
LDLIBS += $(shell $(CC) $(CFLAGS) -print-libgcc-file-name)
|
||||
|
||||
DEFS += -DSTM32$(FAMILY) -DSTM32$(MCU)
|
||||
|
||||
#.SUFFIXES: .elf .bin .hex .srec .list .map .images
|
||||
#.SECONDEXPANSION:
|
||||
#.SECONDARY:
|
||||
|
||||
ELF := $(OBJDIR)/$(BINARY).elf
|
||||
LIST := $(OBJDIR)/$(BINARY).list
|
||||
BIN := $(BINARY).bin
|
||||
HEX := $(BINARY).hex
|
||||
|
||||
all: bin list
|
||||
|
||||
elf: $(ELF)
|
||||
bin: $(BIN)
|
||||
hex: $(HEX)
|
||||
list: $(LIST)
|
||||
|
||||
ifneq ($(MAKECMDGOALS),clean)
|
||||
-include $(DEPS)
|
||||
endif
|
||||
|
||||
$(OBJDIR):
|
||||
mkdir $(OBJDIR)
|
||||
|
||||
$(STARTUP): $(INC_DIR)/startup/vector.c
|
||||
$(CC) $(CFLAGS) $(DEFS) $(INCLUDE) $(ARCH_FLAGS) -o $@ -c $<
|
||||
|
||||
$(OBJDIR)/%.o: %.c
|
||||
@echo " CC $<"
|
||||
$(CC) $(CFLAGS) $(DEFS) $(INCLUDE) $(ARCH_FLAGS) -o $@ -c $<
|
||||
|
||||
#$(OBJDIR)/%.d: %.c $(OBJDIR)
|
||||
# $(CC) -MM -MG $< | sed -e 's,^\([^:]*\)\.o[ ]*:,$(@D)/\1.o $(@D)/\1.d:,' >$@
|
||||
|
||||
$(BIN): $(ELF)
|
||||
@echo " OBJCOPY $(BIN)"
|
||||
$(OBJCOPY) -Obinary $(ELF) $(BIN)
|
||||
|
||||
$(HEX): $(ELF)
|
||||
@echo " OBJCOPY $(HEX)"
|
||||
$(OBJCOPY) -Oihex $(ELF) $(HEX)
|
||||
|
||||
$(LIST): $(ELF)
|
||||
@echo " OBJDUMP $(LIST)"
|
||||
$(OBJDUMP) -S $(ELF) > $(LIST)
|
||||
|
||||
$(ELF): $(OBJDIR) $(OBJS)
|
||||
@echo " LD $(ELF)"
|
||||
$(LD) $(LDFLAGS) $(ARCH_FLAGS) $(OBJS) $(LDLIBS) -o $(ELF)
|
||||
|
||||
clean:
|
||||
@echo " CLEAN"
|
||||
$(RM) $(OBJS) $(DEPS) $(ELF) $(HEX) $(LIST) $(OBJDIR)/*.map
|
||||
@rmdir $(OBJDIR) 2>/dev/null || true
|
||||
|
||||
|
||||
flash: $(BIN)
|
||||
@echo " FLASH $(BIN)"
|
||||
$(STFLASH) write $(BIN) 0x8000000
|
||||
|
||||
boot: $(BIN)
|
||||
@echo " LOAD $(BIN) through bootloader"
|
||||
$(STBOOT) -b$(BOOTSPEED) $(BOOTPORT) -w $(BIN)
|
||||
|
||||
gentags:
|
||||
CFLAGS="$(CFLAGS) $(DEFS)" geany -g $(BINARY).c.tags *[hc] 2>/dev/null
|
||||
|
||||
.PHONY: clean flash boot gentags
|
||||
24
STM32/TSYS_controller/Readme.md
Normal file
24
STM32/TSYS_controller/Readme.md
Normal file
@ -0,0 +1,24 @@
|
||||
# Firmware for controllers of thermal sensors
|
||||
|
||||
Make regular scan of 8 sensors' pairs.
|
||||
USART speed 115200. Code for ../../kicad/stm32
|
||||
|
||||
### Serial interface commands (ends with '\n'):
|
||||
- **C** show coefficients for all thermosensors
|
||||
- **D** detect seosors (reseting them)
|
||||
- **H** switch I2C to high speed (100kHz)
|
||||
- **L** switch I2C to low speed (default, 10kHz)
|
||||
- **R** reset both sensors
|
||||
- **T** get temperature in degrC
|
||||
|
||||
### PINOUT
|
||||
- I2C: PB6 (SCL) & PB7 (SDA)
|
||||
- USART1: PA9 (Tx) & PA10 (Rx)
|
||||
- CAN bus: PB8 (Rx), PB9 (Tx)
|
||||
- USB bus: PA11 (DM), PA12 (DP)
|
||||
- I2C multiplexer: PB0..PB2 (0..2 address bits), PB12 (~EN)
|
||||
- sensors' power: PB3 (in, overcurrent), PA8 (out, enable power)
|
||||
- signal LEDs: PB10 (LED0), PB11 (LED1)
|
||||
- ADC inputs: PA0 (V12/4.93), PA1 (V5/2), PA3 (I12 - 1V/A), PA6 (V3.3/2)
|
||||
- controller CAN address: PA13..PA15 (0..2 bits); 0 - master, other address - slave
|
||||
|
||||
89
STM32/TSYS_controller/hardware.c
Normal file
89
STM32/TSYS_controller/hardware.c
Normal file
@ -0,0 +1,89 @@
|
||||
/*
|
||||
* geany_encoding=koi8-r
|
||||
* hardware.c - hardware-dependent macros & functions
|
||||
*
|
||||
* Copyright 2018 Edward V. Emelianov <eddy@sao.ru, edward.emelianoff@gmail.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.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "hardware.h"
|
||||
|
||||
I2C_SPEED curI2Cspeed = LOW_SPEED;
|
||||
|
||||
|
||||
void gpio_setup(void){
|
||||
// here we turn on clocking for all periph.
|
||||
RCC->AHBENR |= RCC_AHBENR_GPIOBEN | RCC_AHBENR_GPIOAEN | RCC_AHBENR_DMAEN;
|
||||
// Set LEDS (PB10/11) & multiplexer as output (PB0..2,12)
|
||||
GPIOB->MODER = (GPIOB->MODER & ~(GPIO_MODER_MODER10 | GPIO_MODER_MODER11 |
|
||||
GPIO_MODER_MODER0 | GPIO_MODER_MODER1 |
|
||||
GPIO_MODER_MODER2 | GPIO_MODER_MODER12 )
|
||||
) |
|
||||
GPIO_MODER_MODER10_O | GPIO_MODER_MODER11_O |
|
||||
GPIO_MODER_MODER0_O | GPIO_MODER_MODER1_O |
|
||||
GPIO_MODER_MODER2_O | GPIO_MODER_MODER12_O;
|
||||
// multiplexer outputs are push-pull:
|
||||
GPIOB->OTYPER |= GPIO_OTYPER_OT_0 | GPIO_OTYPER_OT_1 | GPIO_OTYPER_OT_2 |
|
||||
GPIO_OTYPER_OT_12;
|
||||
MUL_OFF();
|
||||
// PA8 - power enable
|
||||
GPIOA->MODER = (GPIOA->MODER & ~(GPIO_MODER_MODER8)) |
|
||||
GPIO_MODER_MODER8_O;
|
||||
}
|
||||
|
||||
void i2c_setup(I2C_SPEED speed){
|
||||
if(speed == CURRENT_SPEED){
|
||||
speed = curI2Cspeed;
|
||||
}else{
|
||||
curI2Cspeed = speed;
|
||||
}
|
||||
I2C1->CR1 = 0;
|
||||
#if I2CPINS == A9A10
|
||||
/*
|
||||
* GPIO Resources: I2C1_SCL - PA9, I2C1_SDA - PA10
|
||||
* GPIOA->AFR[1]
|
||||
*/
|
||||
GPIOA->AFR[1] &= ~0xff0; // alternate function F4 for PA9/PA10
|
||||
GPIOA->AFR[1] |= 0x440;
|
||||
GPIOA->MODER &= ~(GPIO_MODER_MODER9 | GPIO_MODER_MODER10);
|
||||
GPIOA->MODER |= GPIO_MODER_MODER9_AF | GPIO_MODER_MODER10_AF; // alternate function
|
||||
GPIOA->OTYPER |= GPIO_OTYPER_OT_9 | GPIO_OTYPER_OT_10; // opendrain
|
||||
//GPIOA->OTYPER |= GPIO_OTYPER_OT_10; // opendrain
|
||||
#elif I2CPINS == B6B7
|
||||
/*
|
||||
* GPIO Resources: I2C1_SCL - PB6, I2C1_SDA - PB7 (AF1)
|
||||
* GPIOB->AFR[0] -> 1<<6*4 | 1<<7*4 = 0x11000000
|
||||
*/
|
||||
GPIOB->AFR[0] = (GPIOB->AFR[0] & ~0xff000000) | 0x11000000;
|
||||
GPIOB->MODER = (GPIOB->MODER & ~(GPIO_MODER_MODER6 | GPIO_MODER_MODER7)) |
|
||||
GPIO_MODER_MODER6_AF | GPIO_MODER_MODER7_AF;
|
||||
GPIOB->OTYPER |= GPIO_OTYPER_OT_6 | GPIO_OTYPER_OT_7;
|
||||
#else // undefined
|
||||
#error "Not implemented"
|
||||
#endif
|
||||
// I2C
|
||||
RCC->APB1ENR |= RCC_APB1ENR_I2C1EN; // timing
|
||||
RCC->CFGR3 |= RCC_CFGR3_I2C1SW; // use sysclock for timing
|
||||
if(speed == LOW_SPEED){ // 10kHz
|
||||
// PRESC=B, SCLDEL=4, SDADEL=2, SCLH=0xC3, SCLL=0xB0
|
||||
I2C1->TIMINGR = (0xB<<28) | (4<<20) | (2<<16) | (0xC3<<8) | (0xB0);
|
||||
}else{ // 100kHz
|
||||
I2C1->TIMINGR = (0xB<<28) | (4<<20) | (2<<16) | (0x12<<8) | (0x11);
|
||||
}
|
||||
I2C1->CR1 = I2C_CR1_PE;// | I2C_CR1_RXIE; // Enable I2C & (interrupt on receive - not supported yet)
|
||||
}
|
||||
86
STM32/TSYS_controller/hardware.h
Normal file
86
STM32/TSYS_controller/hardware.h
Normal file
@ -0,0 +1,86 @@
|
||||
/*
|
||||
* geany_encoding=koi8-r
|
||||
* hardware.h
|
||||
*
|
||||
* Copyright 2018 Edward V. Emelianov <eddy@sao.ru, edward.emelianoff@gmail.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.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*
|
||||
*/
|
||||
#pragma once
|
||||
#ifndef __HARDWARE_H__
|
||||
#define __HARDWARE_H__
|
||||
|
||||
#include "stm32f0.h"
|
||||
|
||||
// LED0
|
||||
#define LED0_port GPIOB
|
||||
#define LED0_pin (1<<10)
|
||||
// LED1
|
||||
#define LED1_port GPIOB
|
||||
#define LED1_pin (1<<11)
|
||||
|
||||
#ifndef USARTNUM
|
||||
#define USARTNUM 2
|
||||
#endif
|
||||
|
||||
#define CONCAT(a,b) a ## b
|
||||
#define STR_HELPER(s) #s
|
||||
#define STR(s) STR_HELPER(s)
|
||||
|
||||
#define FORMUSART(X) CONCAT(USART, X)
|
||||
#define USARTX FORMUSART(USARTNUM)
|
||||
|
||||
#ifndef I2CPINS
|
||||
#define I2CPINS A9A10
|
||||
#endif
|
||||
|
||||
#ifndef LED1_port
|
||||
#define LED1_port LED0_port
|
||||
#endif
|
||||
#ifndef LED1_pin
|
||||
#define LED1_pin LED0_pin
|
||||
#endif
|
||||
#define LED_blink(x) pin_toggle(x ## _port, x ## _pin)
|
||||
|
||||
// set active channel number
|
||||
#define MUL_ADDRESS(x) do{GPIOB->BSRR = (0x7 << 16) | (x);}while(0)
|
||||
// address from 0 to 7
|
||||
// WARNING!!! In current case all variables for sensors counting are uint8_t, so if
|
||||
// MUL_MAX_ADDRESS would be greater than 7 you need to edit sensors_manage.c
|
||||
#define MUL_MAX_ADDRESS (7)
|
||||
// turn multiplexer on/off (PB12 -> 1/0)
|
||||
#define MUL_ON() pin_clear(GPIOB, (1<<12))
|
||||
#define MUL_OFF() pin_set(GPIOB, (1<<12))
|
||||
|
||||
// turn on/off power of sensors (PA8-> 1/0)
|
||||
#define SENSORS_ON() pin_set(GPIOA, (1<<8))
|
||||
#define SENSORS_OFF() pin_clear(GPIOA, (1<<8))
|
||||
// check overcurrent (PB3 == 0)
|
||||
#define SENSORS_OVERCURNT() ((1<<3) != (GPIOB->IDR & (1<<3)))
|
||||
|
||||
typedef enum{
|
||||
LOW_SPEED,
|
||||
HIGH_SPEED,
|
||||
CURRENT_SPEED
|
||||
} I2C_SPEED;
|
||||
|
||||
extern I2C_SPEED curI2Cspeed;
|
||||
|
||||
void gpio_setup(void);
|
||||
void i2c_setup(I2C_SPEED speed);
|
||||
|
||||
#endif // __HARDWARE_H__
|
||||
99
STM32/TSYS_controller/i2c.c
Normal file
99
STM32/TSYS_controller/i2c.c
Normal file
@ -0,0 +1,99 @@
|
||||
/*
|
||||
* geany_encoding=koi8-r
|
||||
* i2c.c
|
||||
*
|
||||
* Copyright 2017 Edward V. Emelianov <eddy@sao.ru, edward.emelianoff@gmail.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.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*
|
||||
*/
|
||||
#include "stm32f0.h"
|
||||
#include "hardware.h"
|
||||
#include "i2c.h"
|
||||
|
||||
/**
|
||||
* I2C for TSYS01
|
||||
* Speed <= 400kHz (200)
|
||||
* t_SCLH > 21ns
|
||||
* t_SCLL > 21ns
|
||||
* while reading, sends NACK
|
||||
* after reading get 24bits of T value, we need upper 2 bytes: ADC16 = ADC>>8
|
||||
* T = (-2) * k4 * 10^{-21} * ADC16^4
|
||||
* + 4 * k3 * 10^{-16} * ADC16^3
|
||||
* + (-2) * k2 * 10^{-11} * ADC16^2
|
||||
* + 1 * k1 * 10^{-6} * ADC16
|
||||
* +(-1.5)* k0 * 10^{-2}
|
||||
* All coefficiens are in registers:
|
||||
* k4 - 0xA2, k3 - 0xA4, k2 - 0xA6, k1 - 0xA8, k0 - 0xAA
|
||||
*/
|
||||
|
||||
extern volatile uint32_t Tms;
|
||||
static uint32_t cntr;
|
||||
|
||||
/**
|
||||
* write command byte to I2C
|
||||
* @param addr - device address (TSYS01_ADDR0 or TSYS01_ADDR1)
|
||||
* @param data - byte to write
|
||||
* @return 0 if error
|
||||
*/
|
||||
uint8_t write_i2c(uint8_t addr, uint8_t data){
|
||||
cntr = Tms;
|
||||
while(I2C1->ISR & I2C_ISR_BUSY) if(Tms - cntr > I2C_TIMEOUT) return 0; // check busy
|
||||
cntr = Tms;
|
||||
while(I2C1->CR2 & I2C_CR2_START) if(Tms - cntr > I2C_TIMEOUT) return 0; // check start
|
||||
I2C1->CR2 = 1<<16 | addr | I2C_CR2_AUTOEND; // 1 byte, autoend
|
||||
// now start transfer
|
||||
I2C1->CR2 |= I2C_CR2_START;
|
||||
cntr = Tms;
|
||||
while(!(I2C1->ISR & I2C_ISR_TXIS)){ // ready to transmit
|
||||
if(I2C1->ISR & I2C_ISR_NACKF){
|
||||
I2C1->ICR |= I2C_ICR_NACKCF;
|
||||
return 0;
|
||||
}
|
||||
if(Tms - cntr > I2C_TIMEOUT) return 0;
|
||||
}
|
||||
I2C1->TXDR = data; // send data
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* read nbytes (2 or 3) of data from I2C line
|
||||
* @return 1 if all OK, 0 if NACK or no device found
|
||||
*/
|
||||
uint8_t read_i2c(uint8_t addr, uint32_t *data, uint8_t nbytes){
|
||||
uint32_t result = 0;
|
||||
cntr = Tms;
|
||||
while(I2C1->ISR & I2C_ISR_BUSY) if(Tms - cntr > 5) return 0; // check busy
|
||||
cntr = Tms;
|
||||
while(I2C1->CR2 & I2C_CR2_START) if(Tms - cntr > 5) return 0; // check start
|
||||
// read N bytes
|
||||
I2C1->CR2 = (nbytes<<16) | addr | 1 | I2C_CR2_AUTOEND | I2C_CR2_RD_WRN;
|
||||
I2C1->CR2 |= I2C_CR2_START;
|
||||
uint8_t i;
|
||||
cntr = Tms;
|
||||
for(i = 0; i < nbytes; ++i){
|
||||
while(!(I2C1->ISR & I2C_ISR_RXNE)){ // wait for data
|
||||
if(I2C1->ISR & I2C_ISR_NACKF){
|
||||
I2C1->ICR |= I2C_ICR_NACKCF;
|
||||
return 0;
|
||||
}
|
||||
if(Tms - cntr > 5) return 0;
|
||||
}
|
||||
result = (result << 8) | I2C1->RXDR;
|
||||
}
|
||||
*data = result;
|
||||
return 1;
|
||||
}
|
||||
39
STM32/TSYS_controller/i2c.h
Normal file
39
STM32/TSYS_controller/i2c.h
Normal file
@ -0,0 +1,39 @@
|
||||
/*
|
||||
* geany_encoding=koi8-r
|
||||
* i2c.h
|
||||
*
|
||||
* Copyright 2017 Edward V. Emelianov <eddy@sao.ru, edward.emelianoff@gmail.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.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*
|
||||
*/
|
||||
|
||||
// timeout in ms
|
||||
#define I2C_TIMEOUT (15)
|
||||
// CSB=1, address 1110110
|
||||
#define TSYS01_ADDR0 (0x76 << 1)
|
||||
// CSB=0, address 1110111
|
||||
#define TSYS01_ADDR1 (0x77 << 1)
|
||||
// registers: reset, read ADC value, start converstion, sart of PROM
|
||||
#define TSYS01_RESET (0x1E)
|
||||
#define TSYS01_ADC_READ (0x00)
|
||||
#define TSYS01_START_CONV (0x48)
|
||||
#define TSYS01_PROM_ADDR0 (0xA0)
|
||||
// conversion time = 10ms
|
||||
#define CONV_TIME (10)
|
||||
|
||||
uint8_t read_i2c(uint8_t addr, uint32_t *data, uint8_t nbytes);
|
||||
uint8_t write_i2c(uint8_t addr, uint8_t data);
|
||||
12
STM32/TSYS_controller/ld/stm32f042k.ld
Normal file
12
STM32/TSYS_controller/ld/stm32f042k.ld
Normal file
@ -0,0 +1,12 @@
|
||||
/* Linker script for STM32F042x6, 32K flash, 6K RAM. */
|
||||
|
||||
/* Define memory regions. */
|
||||
MEMORY
|
||||
{
|
||||
rom (rx) : ORIGIN = 0x08000000, LENGTH = 32K
|
||||
ram (rwx) : ORIGIN = 0x20000000, LENGTH = 6K
|
||||
}
|
||||
|
||||
/* Include the common ld script. */
|
||||
INCLUDE stm32f0.ld
|
||||
|
||||
98
STM32/TSYS_controller/main.c
Normal file
98
STM32/TSYS_controller/main.c
Normal file
@ -0,0 +1,98 @@
|
||||
/*
|
||||
* main.c
|
||||
*
|
||||
* Copyright 2017 Edward V. Emelianoff <eddy@sao.ru, edward.emelianoff@gmail.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.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#include "hardware.h"
|
||||
#include "usart.h"
|
||||
#include "i2c.h"
|
||||
#include "sensors_manage.h"
|
||||
|
||||
volatile uint32_t Tms = 0;
|
||||
|
||||
/* Called when systick fires */
|
||||
void sys_tick_handler(void){
|
||||
++Tms;
|
||||
}
|
||||
|
||||
int main(void){
|
||||
uint32_t lastT = 0;
|
||||
int16_t L = 0;
|
||||
char *txt;
|
||||
sysreset();
|
||||
SysTick_Config(6000, 1);
|
||||
gpio_setup();
|
||||
usart_setup();
|
||||
i2c_setup(LOW_SPEED);
|
||||
// reset on start
|
||||
write_i2c(TSYS01_ADDR0, TSYS01_RESET);
|
||||
write_i2c(TSYS01_ADDR1, TSYS01_RESET);
|
||||
|
||||
while (1){
|
||||
if(lastT > Tms || Tms - lastT > 499){
|
||||
LED_blink(LED0);
|
||||
lastT = Tms;
|
||||
}
|
||||
sensors_process();
|
||||
if(usartrx()){ // usart1 received data, store in in buffer
|
||||
L = usart_getline(&txt);
|
||||
char _1st = txt[0];
|
||||
if(L == 2 && txt[1] == '\n'){
|
||||
L = 0;
|
||||
switch(_1st){
|
||||
case 'C': // 'C' - show coefficients
|
||||
showcoeffs();
|
||||
break;
|
||||
case 'D':
|
||||
sensors_on();
|
||||
break;
|
||||
case 'T': // 'T' - get temperature
|
||||
showtemperature();
|
||||
break;
|
||||
case 'R':
|
||||
i2c_setup(CURRENT_SPEED);
|
||||
SEND("Reinit I2C\n");
|
||||
break;
|
||||
case 'L':
|
||||
i2c_setup(LOW_SPEED);
|
||||
SEND("Low speed\n");
|
||||
break;
|
||||
case 'H':
|
||||
i2c_setup(HIGH_SPEED);
|
||||
SEND("High speed\n");
|
||||
break;
|
||||
default: // help
|
||||
SEND("'C' - show coefficients\n"
|
||||
"'D' - slave discovery\n"
|
||||
"'T' - get raw temperature\n"
|
||||
"'R' - reinit I2C\n"
|
||||
"'L' - low speed\n"
|
||||
"'H' - high speed\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(L){ // text waits for sending
|
||||
while(LINE_BUSY == usart_send(txt, L));
|
||||
L = 0;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
323
STM32/TSYS_controller/sensors_manage.c
Normal file
323
STM32/TSYS_controller/sensors_manage.c
Normal file
@ -0,0 +1,323 @@
|
||||
/*
|
||||
* geany_encoding=koi8-r
|
||||
* sensors_manage.c
|
||||
*
|
||||
* Copyright 2018 Edward V. Emelianov <eddy@sao.ru, edward.emelianoff@gmail.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.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*
|
||||
*/
|
||||
#include "sensors_manage.h"
|
||||
#include "i2c.h"
|
||||
#include "usart.h"
|
||||
|
||||
extern volatile uint32_t Tms;
|
||||
static uint32_t lastSensT = 0;
|
||||
static SensorsState Sstate = SENS_OFF; // turn on sensors only by request
|
||||
static uint8_t curr_mul_addr = 0; // current sensors pair address @ multiplexer
|
||||
static uint8_t overcurnt_ctr = 0; // if this counter > 32 go to OFF state
|
||||
static uint8_t sens_present[2] = {0,0}; // bit flag: Nth bit == 1 if sensor[s] on given channel found
|
||||
static uint8_t Nsens_present = 0; // total amount of sensors found
|
||||
static uint8_t Ntemp_measured = 0; // total amount of themperatures measured
|
||||
|
||||
// 8 - amount of pairs, 2 - amount in pair, 5 - amount of Coef.
|
||||
static uint16_t coefficients[MUL_MAX_ADDRESS+1][2][5]; // Coefficients for given sensors
|
||||
// measured temperatures * 100
|
||||
static int16_t Temperatures[MUL_MAX_ADDRESS+1][2];
|
||||
|
||||
// pair addresses
|
||||
static const uint8_t Taddr[2] = {TSYS01_ADDR0, TSYS01_ADDR1};
|
||||
|
||||
SensorsState sensors_get_state(){return Sstate;}
|
||||
|
||||
/**
|
||||
* Get temperature & calculate it by polinome
|
||||
* T = (-2) * k4 * 10^{-21} * ADC16^4
|
||||
* + 4 * k3 * 10^{-16} * ADC16^3
|
||||
* + (-2) * k2 * 10^{-11} * ADC16^2
|
||||
* + 1 * k1 * 10^{-6} * ADC16
|
||||
* +(-1.5)* k0 * 10^{-2}
|
||||
* k0*(-1.5e-2) + 1e-6*val*(k1 + 1e-5*val*(-2*k2 + 1e-5*val*(4*k3 + -2e-5*k4*val)))
|
||||
*
|
||||
* @param t - value from sensor
|
||||
* @param i - number of sensor in pair
|
||||
* @return -30000 if something wrong or T*100 if all OK
|
||||
*/
|
||||
static uint16_t calc_t(uint32_t t, int i){
|
||||
uint16_t *coeff = coefficients[curr_mul_addr][i];
|
||||
if(coeff[0] == 0) return BAD_TEMPERATURE; // what is with coeffs?
|
||||
if(t < 6500000 || t > 13000000) return BAD_TEMPERATURE; // wrong value - too small or too large
|
||||
int j;
|
||||
double d = (double)t/256., tmp = 0.;
|
||||
// k0*(-1.5e-2) + 0.1*1e-5*val*(1*k1 + 1e-5*val*(-2.*k2 + 1e-5*val*(4*k3 + 1e-5*val*(-2*k4))))
|
||||
const double mul[5] = {-1.5e-2, 1., -2., 4., -2.};
|
||||
for(j = 4; j > 0; --j){
|
||||
tmp += mul[j] * (double)coeff[j];
|
||||
tmp *= 1e-5*d;
|
||||
}
|
||||
tmp = tmp * 10. + 100. * mul[0] * coeff[0];
|
||||
return (uint16_t)tmp;
|
||||
}
|
||||
|
||||
// turn off sensors' power
|
||||
void sensors_off(){
|
||||
MUL_OFF(); // turn off multiplexers
|
||||
SENSORS_OFF(); // turn off sensors' power
|
||||
Sstate = SENS_OFF;
|
||||
}
|
||||
|
||||
/**
|
||||
* if all OK with current, turn ON sensors' power and change state to "initing"
|
||||
*/
|
||||
void sensors_on(){
|
||||
sens_present[0] = sens_present[1] = 0;
|
||||
curr_mul_addr = 0;
|
||||
Nsens_present = 0;
|
||||
MUL_OFF();
|
||||
if(SENSORS_OVERCURNT()){
|
||||
SENSORS_OFF();
|
||||
Sstate = (++overcurnt_ctr > 32) ? SENS_OVERCURNT_OFF : SENS_OVERCURNT;
|
||||
}else{
|
||||
SENSORS_ON();
|
||||
Sstate = SENS_INITING;
|
||||
}
|
||||
}
|
||||
|
||||
/* / count bits in byte
|
||||
static uint8_t bitCount(uint8_t B){
|
||||
uint8_t ctr = 0;
|
||||
while(B){
|
||||
++ctr;
|
||||
B &= (B - 1);
|
||||
}
|
||||
return ctr;
|
||||
}*/
|
||||
|
||||
// count 1 bits in sens_present & set `Nsens_present` to this value
|
||||
static void count_sensors(){
|
||||
Nsens_present = 0;
|
||||
uint16_t B = sens_present[0]<<8 | sens_present[1];
|
||||
while(B){
|
||||
++Nsens_present;
|
||||
B &= (B - 1);
|
||||
}
|
||||
/* / reset temperature values
|
||||
uint16_t *t = Temperatures;
|
||||
for(B = 0; B < 2*(MUL_MAX_ADDRESS+1); ++B)
|
||||
*t++ = BAD_TEMPERATURE; */
|
||||
}
|
||||
|
||||
/**
|
||||
* All procedures return 1 if they change current state due to error & 0 if all OK
|
||||
*/
|
||||
// procedure call each time @ resetting
|
||||
static uint8_t resetproc(){
|
||||
uint8_t i, ctr = 0;
|
||||
SEND("pair "); printu(curr_mul_addr);
|
||||
SEND(" : ");
|
||||
for(i = 0; i < 2; ++i){
|
||||
if(write_i2c(Taddr[i], TSYS01_RESET)){
|
||||
usart_putchar('0' + i);
|
||||
++ctr;
|
||||
sens_present[i] |= 1<<curr_mul_addr; // set bit - found
|
||||
}else{ // not found
|
||||
sens_present[i] &= ~(1<<curr_mul_addr); // reset bit - not found
|
||||
}
|
||||
}
|
||||
if(!ctr) SEND("not found");
|
||||
newline();
|
||||
return 0;
|
||||
}
|
||||
|
||||
// procedure call each time @ getting coefficients
|
||||
static uint8_t getcoefsproc(){
|
||||
uint8_t i, j;
|
||||
const uint8_t regs[5] = {0xAA, 0xA8, 0xA6, 0xA4, 0xA2}; // commands for coefficients
|
||||
for(i = 0; i < 2; ++i){
|
||||
if(!(sens_present[i] & (1<<curr_mul_addr))) continue; // no sensors @ given line
|
||||
uint8_t err = 5;
|
||||
uint16_t *coef = coefficients[curr_mul_addr][i];
|
||||
for(j = 0; j < 5; ++j){
|
||||
uint32_t K;
|
||||
if(write_i2c(Taddr[i], regs[j])){
|
||||
if(read_i2c(Taddr[i], &K, 2)){
|
||||
coef[i] = K;
|
||||
--err;
|
||||
}else break;
|
||||
}else break;
|
||||
}
|
||||
if(err){ // restart all procedures if we can't get coeffs of present sensor
|
||||
sensors_on();
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// procedure call each time @ start measurement
|
||||
static uint8_t msrtempproc(){
|
||||
uint8_t i, j;
|
||||
for(i = 0; i < 2; ++i){
|
||||
if(!(sens_present[i] & (1<<curr_mul_addr))) continue; // no sensors @ given line
|
||||
for(j = 0; j < 5; ++j){
|
||||
if(write_i2c(Taddr[i], TSYS01_START_CONV)) break;
|
||||
if(!write_i2c(Taddr[i], TSYS01_RESET)) i2c_setup(CURRENT_SPEED); // maybe I2C restart will solve the problem?
|
||||
}
|
||||
if(j == 5){
|
||||
sensors_on(); // all very bad
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// procedure call each time @ read temp
|
||||
static uint8_t gettempproc(){
|
||||
uint8_t i;
|
||||
for(i = 0; i < 2; ++i){
|
||||
if(!(sens_present[i] & (1<<curr_mul_addr))) continue; // no sensors @ given line
|
||||
uint8_t err = 1;
|
||||
if(write_i2c(Taddr[i], TSYS01_ADC_READ)){
|
||||
uint32_t t;
|
||||
if(read_i2c(Taddr[i], &t, 3) && t){
|
||||
if(BAD_TEMPERATURE != (Temperatures[curr_mul_addr][i] = calc_t(t, i))){
|
||||
err = 0;
|
||||
++Ntemp_measured;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(err) write_i2c(Taddr[i], TSYS01_RESET);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 2 phase for each call: 1) set address & turn on mul.; 2) call function & turn off mul.
|
||||
* Change address @ call function `procfn`
|
||||
* @return 1 if scan is over
|
||||
*/
|
||||
static uint8_t sensors_scan(uint8_t (* procfn)()){
|
||||
static uint8_t callctr = 0;
|
||||
if(callctr == 0){ // set address & turn on multiplexer
|
||||
MUL_ADDRESS(curr_mul_addr);
|
||||
MUL_ON();
|
||||
callctr = 1;
|
||||
}else{ // call procfn & turn off multiplexer
|
||||
callctr = 0;
|
||||
uint8_t s = procfn();
|
||||
MUL_OFF();
|
||||
if(s) return 0;
|
||||
if(++curr_mul_addr > MUL_MAX_ADDRESS){ // scan is over
|
||||
curr_mul_addr = 0;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// print coefficients @debug console
|
||||
void showcoeffs(){
|
||||
int a, p, k;
|
||||
for(a = 0; a <= MUL_MAX_ADDRESS; ++a){
|
||||
for(p = 0; p < 2; ++p){
|
||||
if(!(sens_present[p] & (1<<a))) continue; // no sensor
|
||||
for(k = 0; k < 5; ++k){
|
||||
char b[] = {'K', a+'0', p+'0', '_', k+'0', '='};
|
||||
while(ALL_OK != usart_send_blocking(b, 6));
|
||||
printu(coefficients[a][p][k]);
|
||||
newline();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// print temperature @debug console
|
||||
void showtemperature(){
|
||||
int a, p;
|
||||
for(a = 0; a <= MUL_MAX_ADDRESS; ++a){
|
||||
for(p = 0; p < 2; ++p){
|
||||
if(!(sens_present[p] & (1<<a))) continue; // no sensor
|
||||
char b[] = {'T', a+'0', p+'0', '=', '+'};
|
||||
int16_t t = Temperatures[a][p];
|
||||
if(t < 0){
|
||||
b[4] = '-';
|
||||
t = -t;
|
||||
}
|
||||
while(ALL_OK != usart_send_blocking(b, 5));
|
||||
printu(t);
|
||||
newline();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// finite state machine for sensors switching & checking
|
||||
void sensors_process(){
|
||||
if(SENSORS_OVERCURNT()){
|
||||
MUL_OFF();
|
||||
SENSORS_OFF();
|
||||
Sstate = (++overcurnt_ctr > 32) ? SENS_OVERCURNT_OFF : SENS_OVERCURNT;
|
||||
return;
|
||||
}
|
||||
switch (Sstate){
|
||||
case SENS_INITING: // initialisation (restart I2C)
|
||||
i2c_setup(CURRENT_SPEED);
|
||||
Sstate = SENS_RESETING;
|
||||
break;
|
||||
case SENS_RESETING: // reset & discovery procedure
|
||||
overcurnt_ctr = 0;
|
||||
if(sensors_scan(resetproc)) Sstate = SENS_GET_COEFFS;
|
||||
break;
|
||||
case SENS_GET_COEFFS: // get coefficients
|
||||
if(sensors_scan(getcoefsproc)){
|
||||
count_sensors(); // get total amount of sensors
|
||||
Sstate = SENS_START_MSRMNT;
|
||||
}
|
||||
break;
|
||||
case SENS_START_MSRMNT: // send all sensors command to start measurements
|
||||
if(sensors_scan(msrtempproc)){
|
||||
lastSensT = Tms;
|
||||
Sstate = SENS_WAITING;
|
||||
Ntemp_measured = 0; // reset value of good measurements
|
||||
}
|
||||
break;
|
||||
case SENS_WAITING: // wait for end of conversion
|
||||
if(Tms - lastSensT > CONV_TIME){
|
||||
Sstate = SENS_GATHERING;
|
||||
}
|
||||
break;
|
||||
case SENS_GATHERING: // scan all sensors, get thermal data & calculate temperature
|
||||
if(sensors_scan(gettempproc)){
|
||||
lastSensT = Tms;
|
||||
if(Nsens_present == Ntemp_measured) // All OK, amount of T == amount of sensors
|
||||
Sstate = SENS_SLEEPING;
|
||||
else{ // reinit I2C & try to start measurements again
|
||||
i2c_setup(CURRENT_SPEED);
|
||||
Sstate = SENS_START_MSRMNT;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case SENS_SLEEPING: // wait for `SLEEP_TIME` till next measurements
|
||||
if(Tms - lastSensT > SLEEP_TIME){
|
||||
Sstate = SENS_START_MSRMNT;
|
||||
}
|
||||
break;
|
||||
case SENS_OVERCURNT: // try to reinit all after overcurrent
|
||||
sensors_on();
|
||||
break;
|
||||
default: // do nothing
|
||||
break;
|
||||
}
|
||||
}
|
||||
56
STM32/TSYS_controller/sensors_manage.h
Normal file
56
STM32/TSYS_controller/sensors_manage.h
Normal file
@ -0,0 +1,56 @@
|
||||
/*
|
||||
* geany_encoding=koi8-r
|
||||
* sensors_manage.h
|
||||
*
|
||||
* Copyright 2018 Edward V. Emelianov <eddy@sao.ru, edward.emelianoff@gmail.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.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*
|
||||
*/
|
||||
#pragma once
|
||||
#ifndef __SENSORS_MANAGE_H__
|
||||
#define __SENSORS_MANAGE_H__
|
||||
|
||||
#include "hardware.h"
|
||||
|
||||
// time between two readings (3sec)
|
||||
#define SLEEP_TIME (3000)
|
||||
// error in measurement == -300degrC
|
||||
#define BAD_TEMPERATURE (-30000)
|
||||
|
||||
typedef enum{
|
||||
SENS_INITING // power on
|
||||
,SENS_RESETING // discovery sensors resetting them
|
||||
,SENS_GET_COEFFS // get coefficients from all sensors
|
||||
,SENS_SLEEPING // wait for a time to process measurements
|
||||
,SENS_START_MSRMNT // send command 2 start measurement
|
||||
,SENS_WAITING // wait for measurements end
|
||||
,SENS_GATHERING // collect information
|
||||
,SENS_OFF // sensors' power is off by external command
|
||||
,SENS_OVERCURNT // overcurrent detected @ any stage
|
||||
,SENS_OVERCURNT_OFF // sensors' power is off due to continuous overcurrent
|
||||
} SensorsState;
|
||||
|
||||
SensorsState sensors_get_state();
|
||||
void sensors_process();
|
||||
|
||||
void sensors_off();
|
||||
void sensors_on();
|
||||
|
||||
void showcoeffs();
|
||||
void showtemperature();
|
||||
|
||||
#endif // __SENSORS_MANAGE_H__
|
||||
17
STM32/TSYS_controller/tmp_UPGWJZ.d
Normal file
17
STM32/TSYS_controller/tmp_UPGWJZ.d
Normal file
@ -0,0 +1,17 @@
|
||||
tmp_UPGWJZ.o: /tmp/tmp_UPGWJZ.cpp /usr/include/stdc-predef.h i2c.h \
|
||||
usart.c ../inc/F0/stm32f0.h ../inc/F0/stm32f0xx.h \
|
||||
../inc/F0/stm32f042x6.h ../inc/cm/core_cm0.h \
|
||||
/usr/lib/gcc/x86_64-pc-linux-gnu/6.4.0/include/stdint.h \
|
||||
/usr/include/stdint.h /usr/include/bits/libc-header-start.h \
|
||||
/usr/include/features.h /usr/include/sys/cdefs.h \
|
||||
/usr/include/bits/wordsize.h /usr/include/bits/long-double.h \
|
||||
/usr/include/gnu/stubs.h /usr/include/gnu/stubs-64.h \
|
||||
/usr/include/bits/types.h /usr/include/bits/typesizes.h \
|
||||
/usr/include/bits/wchar.h /usr/include/bits/stdint-intn.h \
|
||||
/usr/include/bits/stdint-uintn.h ../inc/cm/core_cmInstr.h \
|
||||
../inc/cm/core_cmFunc.h hardware.h usart.h /usr/include/string.h \
|
||||
/usr/lib/gcc/x86_64-pc-linux-gnu/6.4.0/include/stddef.h \
|
||||
/usr/include/bits/types/locale_t.h /usr/include/bits/types/__locale_t.h \
|
||||
/usr/include/strings.h /usr/include/bits/strings_fortified.h \
|
||||
/usr/include/bits/string_fortified.h main.c i2c.h i2c.c hardware.h \
|
||||
hardware.c
|
||||
BIN
STM32/TSYS_controller/tsys01.bin
Executable file
BIN
STM32/TSYS_controller/tsys01.bin
Executable file
Binary file not shown.
4408
STM32/TSYS_controller/tsys01.c.tags
Normal file
4408
STM32/TSYS_controller/tsys01.c.tags
Normal file
File diff suppressed because it is too large
Load Diff
33
STM32/TSYS_controller/tsys01.geany
Normal file
33
STM32/TSYS_controller/tsys01.geany
Normal file
@ -0,0 +1,33 @@
|
||||
[editor]
|
||||
line_wrapping=false
|
||||
line_break_column=100
|
||||
auto_continue_multiline=true
|
||||
|
||||
[file_prefs]
|
||||
final_new_line=true
|
||||
ensure_convert_new_lines=true
|
||||
strip_trailing_spaces=true
|
||||
replace_tabs=true
|
||||
|
||||
[indentation]
|
||||
indent_width=4
|
||||
indent_type=0
|
||||
indent_hard_tab_width=4
|
||||
detect_indent=false
|
||||
detect_indent_width=false
|
||||
indent_mode=3
|
||||
|
||||
[project]
|
||||
name=tsys01
|
||||
base_path=/home/eddy/Docs/SAO/BTA/Зеркало_контроль/Project/STM32src/TSYS_controller
|
||||
description=
|
||||
|
||||
[long line marker]
|
||||
long_line_behaviour=1
|
||||
long_line_column=100
|
||||
|
||||
[files]
|
||||
current_page=-1
|
||||
|
||||
[VTE]
|
||||
last_dir=/home/eddy
|
||||
238
STM32/TSYS_controller/usart.c
Normal file
238
STM32/TSYS_controller/usart.c
Normal file
@ -0,0 +1,238 @@
|
||||
/*us
|
||||
* usart.c
|
||||
*
|
||||
* Copyright 2017 Edward V. Emelianoff <eddy@sao.ru, edward.emelianoff@gmail.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.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*/
|
||||
#include "stm32f0.h"
|
||||
#include "hardware.h"
|
||||
#include "usart.h"
|
||||
#include <string.h>
|
||||
|
||||
extern volatile uint32_t Tms;
|
||||
static int datalen[2] = {0,0}; // received data line length (including '\n')
|
||||
|
||||
int linerdy = 0, // received data ready
|
||||
dlen = 0, // length of data (including '\n') in current buffer
|
||||
bufovr = 0, // input buffer overfull
|
||||
txrdy = 1 // transmission done
|
||||
;
|
||||
|
||||
|
||||
int rbufno = 0; // current rbuf number
|
||||
static char rbuf[UARTBUFSZ][2], tbuf[UARTBUFSZ]; // receive & transmit buffers
|
||||
static char *recvdata = NULL;
|
||||
|
||||
/**
|
||||
* return length of received data (without trailing zero
|
||||
*/
|
||||
int usart_getline(char **line){
|
||||
if(bufovr){
|
||||
bufovr = 0;
|
||||
linerdy = 0;
|
||||
return 0;
|
||||
}
|
||||
*line = recvdata;
|
||||
linerdy = 0;
|
||||
return dlen;
|
||||
}
|
||||
|
||||
TXstatus usart_send(const char *str, int len){
|
||||
if(!txrdy) return LINE_BUSY;
|
||||
if(len > UARTBUFSZ) return STR_TOO_LONG;
|
||||
txrdy = 0;
|
||||
memcpy(tbuf, str, len);
|
||||
#if USARTNUM == 2
|
||||
DMA1_Channel4->CCR &= ~DMA_CCR_EN;
|
||||
DMA1_Channel4->CNDTR = len;
|
||||
DMA1_Channel4->CCR |= DMA_CCR_EN; // start transmission
|
||||
#elif USARTNUM == 1
|
||||
DMA1_Channel2->CCR &= ~DMA_CCR_EN;
|
||||
DMA1_Channel2->CNDTR = len;
|
||||
DMA1_Channel2->CCR |= DMA_CCR_EN;
|
||||
#else
|
||||
#error "Not implemented"
|
||||
#endif
|
||||
return ALL_OK;
|
||||
}
|
||||
|
||||
TXstatus usart_send_blocking(const char *str, int len){
|
||||
if(!txrdy) return LINE_BUSY;
|
||||
int i;
|
||||
bufovr = 0;
|
||||
for(i = 0; i < len; ++i){
|
||||
USARTX -> TDR = *str++;
|
||||
while(!(USARTX->ISR & USART_ISR_TXE));
|
||||
}
|
||||
return ALL_OK;
|
||||
}
|
||||
|
||||
void usart_putchar(const char ch){
|
||||
while(!txrdy);
|
||||
USARTX -> TDR = ch;
|
||||
while(!(USARTX->ISR & USART_ISR_TXE));
|
||||
}
|
||||
|
||||
void newline(){
|
||||
while(!txrdy);
|
||||
USARTX -> TDR = '\n';
|
||||
while(!(USARTX->ISR & USART_ISR_TXE));
|
||||
}
|
||||
|
||||
|
||||
void usart_setup(){
|
||||
// Nucleo's USART2 connected to VCP proxy of st-link
|
||||
#if USARTNUM == 2
|
||||
// setup pins: PA2 (Tx - AF1), PA15 (Rx - AF1)
|
||||
// AF mode (AF1)
|
||||
GPIOA->MODER = (GPIOA->MODER & ~(GPIO_MODER_MODER2|GPIO_MODER_MODER15))\
|
||||
| (GPIO_MODER_MODER2_AF | GPIO_MODER_MODER15_AF);
|
||||
GPIOA->AFR[0] = (GPIOA->AFR[0] &~GPIO_AFRH_AFRH2) | 1 << (2 * 4); // PA2
|
||||
GPIOA->AFR[1] = (GPIOA->AFR[1] &~GPIO_AFRH_AFRH7) | 1 << (7 * 4); // PA15
|
||||
// DMA: Tx - Ch4
|
||||
DMA1_Channel4->CPAR = (uint32_t) &USART2->TDR; // periph
|
||||
DMA1_Channel4->CMAR = (uint32_t) tbuf; // mem
|
||||
DMA1_Channel4->CCR |= DMA_CCR_MINC | DMA_CCR_DIR | DMA_CCR_TCIE; // 8bit, mem++, mem->per, transcompl irq
|
||||
// Tx CNDTR set @ each transmission due to data size
|
||||
NVIC_SetPriority(DMA1_Channel4_5_IRQn, 3);
|
||||
NVIC_EnableIRQ(DMA1_Channel4_5_IRQn);
|
||||
NVIC_SetPriority(USART2_IRQn, 0);
|
||||
// setup usart2
|
||||
RCC->APB1ENR |= RCC_APB1ENR_USART2EN; // clock
|
||||
// oversampling by16, 115200bps (fck=48mHz)
|
||||
//USART2_BRR = 0x1a1; // 48000000 / 115200
|
||||
USART2->BRR = 480000 / 1152;
|
||||
USART2->CR3 = USART_CR3_DMAT; // enable DMA Tx
|
||||
USART2->CR1 = USART_CR1_TE | USART_CR1_RE | USART_CR1_UE; // 1start,8data,nstop; enable Rx,Tx,USART
|
||||
while(!(USART2->ISR & USART_ISR_TC)); // polling idle frame Transmission
|
||||
USART2->ICR |= USART_ICR_TCCF; // clear TC flag
|
||||
USART2->CR1 |= USART_CR1_RXNEIE;
|
||||
NVIC_EnableIRQ(USART2_IRQn);
|
||||
// USART1 of main board
|
||||
#elif USARTNUM == 1
|
||||
// PA9 - Tx, PA10 - Rx (AF1)
|
||||
GPIOA->MODER = (GPIOA->MODER & ~(GPIO_MODER_MODER9 | GPIO_MODER_MODER10))\
|
||||
| (GPIO_MODER_MODER9_AF | GPIO_MODER_MODER10_AF);
|
||||
GPIOA->AFR[1] = (GPIOA->AFR[1] & ~(GPIO_AFRH_AFRH1 | GPIO_AFRH_AFRH2)) |
|
||||
1 << (1 * 4) | 1 << (2 * 4); // PA9, PA10
|
||||
// USART1 Tx DMA - Channel2 (default value in SYSCFG_CFGR1)
|
||||
DMA1_Channel2->CPAR = (uint32_t) &USART1->TDR; // periph
|
||||
DMA1_Channel2->CMAR = (uint32_t) tbuf; // mem
|
||||
DMA1_Channel2->CCR |= DMA_CCR_MINC | DMA_CCR_DIR | DMA_CCR_TCIE; // 8bit, mem++, mem->per, transcompl irq
|
||||
// Tx CNDTR set @ each transmission due to data size
|
||||
NVIC_SetPriority(DMA1_Channel2_3_IRQn, 3);
|
||||
NVIC_EnableIRQ(DMA1_Channel2_3_IRQn);
|
||||
NVIC_SetPriority(USART1_IRQn, 0);
|
||||
// setup usart1
|
||||
RCC->APB2ENR |= RCC_APB2ENR_USART1EN;
|
||||
USART1->BRR = 480000 / 1152;
|
||||
USART1->CR3 = USART_CR3_DMAT; // enable DMA Tx
|
||||
USART1->CR1 = USART_CR1_TE | USART_CR1_RE | USART_CR1_UE; // 1start,8data,nstop; enable Rx,Tx,USART
|
||||
while(!(USART1->ISR & USART_ISR_TC)); // polling idle frame Transmission
|
||||
USART1->ICR |= USART_ICR_TCCF; // clear TC flag
|
||||
USART1->CR1 |= USART_CR1_RXNEIE;
|
||||
NVIC_EnableIRQ(USART1_IRQn);
|
||||
#else
|
||||
#error "Not implemented"
|
||||
#endif
|
||||
}
|
||||
|
||||
#if USARTNUM == 2
|
||||
void usart2_isr(){
|
||||
// USART1
|
||||
#elif USARTNUM == 1
|
||||
void usart1_isr(){
|
||||
#else
|
||||
#error "Not implemented"
|
||||
#endif
|
||||
#ifdef CHECK_TMOUT
|
||||
static uint32_t tmout = 0;
|
||||
#endif
|
||||
if(USARTX->ISR & USART_ISR_RXNE){ // RX not emty - receive next char
|
||||
#ifdef CHECK_TMOUT
|
||||
if(tmout && Tms >= tmout){ // set overflow flag
|
||||
bufovr = 1;
|
||||
datalen[rbufno] = 0;
|
||||
}
|
||||
tmout = Tms + TIMEOUT_MS;
|
||||
if(!tmout) tmout = 1; // prevent 0
|
||||
#endif
|
||||
// read RDR clears flag
|
||||
uint8_t rb = USARTX->RDR;
|
||||
if(datalen[rbufno] < UARTBUFSZ){ // put next char into buf
|
||||
rbuf[rbufno][datalen[rbufno]++] = rb;
|
||||
if(rb == '\n'){ // got newline - line ready
|
||||
linerdy = 1;
|
||||
dlen = datalen[rbufno];
|
||||
recvdata = rbuf[rbufno];
|
||||
// prepare other buffer
|
||||
rbufno = !rbufno;
|
||||
datalen[rbufno] = 0;
|
||||
#ifdef CHECK_TMOUT
|
||||
// clear timeout at line end
|
||||
tmout = 0;
|
||||
#endif
|
||||
}
|
||||
}else{ // buffer overrun
|
||||
bufovr = 1;
|
||||
datalen[rbufno] = 0;
|
||||
#ifdef CHECK_TMOUT
|
||||
tmout = 0;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// print 32bit unsigned int
|
||||
void printu(uint32_t val){
|
||||
char bufa[11], bufb[10];
|
||||
int l = 0, bpos = 0;
|
||||
if(!val){
|
||||
bufa[0] = '0';
|
||||
l = 1;
|
||||
}else{
|
||||
while(val){
|
||||
bufb[l++] = val % 10 + '0';
|
||||
val /= 10;
|
||||
}
|
||||
int i;
|
||||
bpos += l;
|
||||
for(i = 0; i < l; ++i){
|
||||
bufa[--bpos] = bufb[i];
|
||||
}
|
||||
}
|
||||
while(LINE_BUSY == usart_send_blocking(bufa, l+bpos));
|
||||
}
|
||||
|
||||
#if USARTNUM == 2
|
||||
void dma1_channel4_5_isr(){
|
||||
if(DMA1->ISR & DMA_ISR_TCIF4){ // Tx
|
||||
DMA1->IFCR |= DMA_IFCR_CTCIF4; // clear TC flag
|
||||
txrdy = 1;
|
||||
}
|
||||
}
|
||||
// USART1
|
||||
#elif USARTNUM == 1
|
||||
void dma1_channel2_3_isr(){
|
||||
if(DMA1->ISR & DMA_ISR_TCIF2){ // Tx
|
||||
DMA1->IFCR |= DMA_IFCR_CTCIF2; // clear TC flag
|
||||
txrdy = 1;
|
||||
}
|
||||
}
|
||||
#else
|
||||
#error "Not implemented"
|
||||
#endif
|
||||
55
STM32/TSYS_controller/usart.h
Normal file
55
STM32/TSYS_controller/usart.h
Normal file
@ -0,0 +1,55 @@
|
||||
/*
|
||||
* usart.h
|
||||
*
|
||||
* Copyright 2017 Edward V. Emelianoff <eddy@sao.ru, edward.emelianoff@gmail.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.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301, USA.
|
||||
*/
|
||||
#pragma once
|
||||
#ifndef __USART_H__
|
||||
#define __USART_H__
|
||||
|
||||
// input and output buffers size
|
||||
#define UARTBUFSZ (64)
|
||||
// timeout between data bytes
|
||||
#ifndef TIMEOUT_MS
|
||||
#define TIMEOUT_MS (1500)
|
||||
#endif
|
||||
|
||||
// macro for static strings
|
||||
#define SEND(str) do{}while(LINE_BUSY == usart_send_blocking(str, sizeof(str)-1))
|
||||
#define NEWLINE() do{}while(LINE_BUSY == usart_send_blocking('\n', 1))
|
||||
|
||||
typedef enum{
|
||||
ALL_OK,
|
||||
LINE_BUSY,
|
||||
STR_TOO_LONG
|
||||
} TXstatus;
|
||||
|
||||
#define usartrx() (linerdy)
|
||||
#define usartovr() (bufovr)
|
||||
|
||||
extern int linerdy, bufovr, txrdy;
|
||||
|
||||
void usart_setup();
|
||||
int usart_getline(char **line);
|
||||
TXstatus usart_send(const char *str, int len);
|
||||
TXstatus usart_send_blocking(const char *str, int len);
|
||||
void newline();
|
||||
void usart_putchar(const char ch);
|
||||
void printu(uint32_t val);
|
||||
|
||||
#endif // __USART_H__
|
||||
Loading…
x
Reference in New Issue
Block a user