diff --git a/F0-nolib/TM1637/Makefile b/F0-nolib/TM1637/Makefile new file mode 100644 index 0000000..9cdacee --- /dev/null +++ b/F0-nolib/TM1637/Makefile @@ -0,0 +1,134 @@ +BINARY = tm1637 +BOOTPORT ?= /dev/ttyUSB0 +BOOTSPEED ?= 115200 +# MCU FAMILY +FAMILY = F0 +# MCU code +MCU = F051x8 +DEFS = -DEBUG +# change this linking script depending on particular MCU model, +# for example, if you have STM32F103VBT6, you should write: +LDSCRIPT = stm32f030f.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 +PREFIX ?= /opt/bin/arm-none-eabi + +RM := rm -f +RMDIR := rmdir +CC := $(PREFIX)-gcc +LD := $(PREFIX)-gcc +AR := $(PREFIX)-ar +AS := $(PREFIX)-as +OBJCOPY := $(PREFIX)-objcopy +OBJDUMP := $(PREFIX)-objdump +GDB := $(PREFIX)-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 -Werror -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 $< + +$(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) + +.PHONY: clean flash boot diff --git a/F0-nolib/TM1637/Readme.md b/F0-nolib/TM1637/Readme.md new file mode 100644 index 0000000..4978436 --- /dev/null +++ b/F0-nolib/TM1637/Readme.md @@ -0,0 +1,26 @@ +4-digit "time" LED indicator based on TM1637 +============================= + +FOR STM32F051!!! +WARNING! Only writing data!!! Can't read keys state. + +## GPIO +- I2C: PB6 (SCL) & PB7 (SDA) +- USART1: PA9/PA10 + +## UART +115200N1, not more than 100ms between data bytes in command. +To turn ON human terminal (without timeout) send "####". + +## Protocol +All commands are in brackets: `[ command line ]`. + +'[' clears earlier input; '\n', '\r', ' ', '\t' are ignored. +All messages are asynchronous! + +## Commands +0..9 - send data +A - display 'ABCD' +G - get keyboard status (don't work) +Hhex - display 'hex' as hex number +Nnum - display 'num' as decimal number diff --git a/F0-nolib/TM1637/hardware.c b/F0-nolib/TM1637/hardware.c new file mode 100644 index 0000000..d54fa0d --- /dev/null +++ b/F0-nolib/TM1637/hardware.c @@ -0,0 +1,68 @@ +/* + * This file is part of the TM1637 project. + * Copyright 2019 Edward Emelianov . + * + * 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 3 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, see . + */ +#include "hardware.h" +#include "usart.h" + + +/** + * @brief gpio_setup - setup GPIOs for external IO + * GPIO pinout: + * PC13 - open drain - onboard LED (blinks when board works) + */ +static inline void gpio_setup(){ + // Enable clocks to the GPIO subsystems + RCC->AHBENR |= RCC_AHBENR_GPIOAEN | RCC_AHBENR_GPIOBEN | RCC_AHBENR_GPIOCEN; + GPIOC->MODER = GPIO_MODER_MODER13_O; + GPIOC->OTYPER = GPIO_OTYPER_OT_13; + /* + // PA6/7 - AF; PB1 - AF + GPIOA->MODER = GPIO_MODER_MODER4_O | GPIO_MODER_MODER6_AF | GPIO_MODER_MODER7_AF; + GPIOA->OTYPER = GPIO_OTYPER_OT_4; + // alternate functions: + // PA6 - TIM3_CH1, PA7 - TIM3_CH2 + GPIOA->AFR[0] = (GPIOA->AFR[0] &~ (GPIO_AFRL_AFRL6 | GPIO_AFRL_AFRL7)) \ + | (1 << (6 * 4)) | (1 << (7 * 4)); + */ +} + +static inline void i2c_setup(){ + I2C1->CR1 = 0; +/* + * GPIO Resources: I2C1_SCL - PB6 (AF1), I2C1_SDA - PB7 (AF1) + */ + GPIOB->MODER |= GPIO_MODER_MODER6_AF | GPIO_MODER_MODER7_AF; // alternate function + GPIOB->AFR[0] = (GPIOB->AFR[0] &~ (GPIO_AFRL_AFRL6 | GPIO_AFRL_AFRL7)) \ + | (1 << (6 * 4)) | (1 << (7 * 4)); + // GPIOB->OTYPER |= GPIO_OTYPER_OT_6 | GPIO_OTYPER_OT_7; // opendrain + // I2C + RCC->APB1ENR |= RCC_APB1ENR_I2C1EN; // timing + RCC->CFGR3 |= RCC_CFGR3_I2C1SW; // use sysclock for timing + // 100kHz + // Clock = 6MHz, 0.16(6)us, need 5us (*30) + // PRESC=4 (f/5), SCLDEL=0 (t_SU=5/6us), SDADEL=0 (t_HD=5/6us), SCLL,SCLH=14 (2.(3)us) + I2C1->TIMINGR = (0xB<<28) | (4<<20) | (2<<16) | (0x12<<8) | (0x11); + I2C1->CR1 = I2C_CR1_PE; // Enable I2C +} + +void hw_setup(){ + sysreset(); + gpio_setup(); + i2c_setup(); + USART1_config(); +} + diff --git a/F0-nolib/TM1637/hardware.h b/F0-nolib/TM1637/hardware.h new file mode 100644 index 0000000..e0a7a63 --- /dev/null +++ b/F0-nolib/TM1637/hardware.h @@ -0,0 +1,26 @@ +/* + * This file is part of the TM1637 project. + * Copyright 2019 Edward Emelianov . + * + * 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 3 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, see . + */ +#pragma once +#ifndef HARDWARE_H__ +#define HARDWARE_H__ +#include "stm32f0.h" + +extern volatile uint32_t Tms; +void hw_setup(void); + +#endif // HARDWARE_H__ diff --git a/F0-nolib/TM1637/i2c.c b/F0-nolib/TM1637/i2c.c new file mode 100644 index 0000000..0ed33ae --- /dev/null +++ b/F0-nolib/TM1637/i2c.c @@ -0,0 +1,105 @@ +/* + * geany_encoding=koi8-r + * i2c.c + * + * Copyright 2017 Edward V. Emelianov + * + * 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" +#include "usart.h" + +static uint32_t cntr; + +static uint8_t wri2c(uint8_t byte){ + I2C1->CR2 = byte;// | I2C_CR2_AUTOEND; + if(byte & 1) I2C1->CR2 |= I2C_CR2_RD_WRN; + // now start transfer + I2C1->CR2 |= I2C_CR2_START; + if(I2C1->ISR & I2C_ISR_RXNE) (void) I2C1->RXDR; + cntr = Tms; + while((I2C1->ISR & I2C_ISR_TC) == 0) if(Tms - cntr > I2C_TIMEOUT) return 0; + if(I2C1->ISR & I2C_ISR_RXNE) (void) I2C1->RXDR; + I2C1->ICR = 0xff; // clear all error flags + return 1; +} + +/** + * 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(const uint8_t *commands, uint8_t nbytes){ + uint8_t r = 1; + 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 + for(uint8_t i = 0; i < nbytes; ++i){ + if(!wri2c(commands[i])){ + r = 0; + break; + } + } + I2C1->CR2 |= I2C_CR2_STOP; + return r; +} + +/** + * read nbytes (0..4) of data from I2C line + * @return 1 if all OK, 0 if NACK or no device found + */ +uint8_t read_i2c(uint8_t command, uint8_t *data){ + 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 1 byte + if(command & 1){ + I2C1->CR2 = (1<<16) | command | I2C_CR2_RD_WRN; + }else{ + wri2c(command); + /* + I2C1->CR2 = command; + I2C1->CR2 |= I2C_CR2_START; + cntr = Tms; + while((I2C1->ISR & I2C_ISR_TC) == 0) if(Tms - cntr > I2C_TIMEOUT) return 0; + */ + I2C1->CR2 = (1<<16) |0xff | I2C_CR2_RD_WRN; + } + I2C1->CR2 |= I2C_CR2_START; + I2C1->CR2 |= I2C_CR2_STOP; + cntr = Tms; + while(Tms == cntr); +/* + 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; + } +*/ + if(I2C1->ISR & I2C_ISR_RXNE){ + *data = I2C1->RXDR; + return 1; + } + return 0; + } diff --git a/F0-nolib/TM1637/i2c.h b/F0-nolib/TM1637/i2c.h new file mode 100644 index 0000000..50f2e00 --- /dev/null +++ b/F0-nolib/TM1637/i2c.h @@ -0,0 +1,30 @@ +/* + * This file is part of the TM1637 project. + * Copyright 2019 Edward Emelianov . + * + * 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 3 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, see . + */ + +#ifndef I2C_H__ +#define I2C_H__ + +#include "stm32f0.h" + +// timeout in ms +#define I2C_TIMEOUT (15) + +uint8_t read_i2c(uint8_t command, uint8_t *data); +uint8_t write_i2c(const uint8_t *commands, uint8_t nbytes); + +#endif // I2C_H__ diff --git a/F0-nolib/TM1637/main.c b/F0-nolib/TM1637/main.c new file mode 100644 index 0000000..8e4f1fb --- /dev/null +++ b/F0-nolib/TM1637/main.c @@ -0,0 +1,54 @@ +/* + * This file is part of the TM1637 project. + * Copyright 2019 Edward Emelianov . + * + * 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 3 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, see . + */ +#include "hardware.h" +#include "i2c.h" +#include "protocol.h" +#include "usart.h" +#include + + +volatile uint32_t Tms = 0; + +// Called when systick fires +void sys_tick_handler(void){ + ++Tms; +} + +int main(void){ + uint32_t T = 0, TLED = 0; + char *txt; + hw_setup(); + SysTick_Config(6000, 1); + SEND("LED display controller v0.1\n"); + while (1){ + if(Tms - TLED > 499){ + TLED = Tms; + pin_toggle(GPIOC, 1<<13); + } + if(usart1_getline(&txt)){ // usart1 received command, process it + txt = process_command(txt); + }else txt = NULL; + if(txt){ // text waits for sending + while(ALL_OK != usart1_send(txt, 0)); + } + if(Tms - T > 49){ + T = Tms; + } + usart1_sendbuf(); + } +} diff --git a/F0-nolib/TM1637/protocol.c b/F0-nolib/TM1637/protocol.c new file mode 100644 index 0000000..14502be --- /dev/null +++ b/F0-nolib/TM1637/protocol.c @@ -0,0 +1,189 @@ +/* + * This file is part of the TM1637 project. + * Copyright 2019 Edward Emelianov . + * + * 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 3 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, see . + */ +#include "hardware.h" +#include "i2c.h" +#include "protocol.h" +#include "usart.h" + +#define DOT 1 +#define MIN 0b00000010 +#define DIG0 0b11111100 +#define DIG1 0b01100000 +#define DIG2 0b11011010 +#define DIG3 0b11110010 +#define DIG4 0b01100110 +#define DIG5 0b10110110 +#define DIG6 0b10111110 +#define DIG7 0b11100000 +#define DIG8 0b11111110 +#define DIG9 0b11110110 +#define DIGA 0b11101110 +#define DIGB 0b00111110 +#define DIGC 0b10011100 +#define DIGD 0b01111010 +#define DIGE 0b10011110 +#define DIGF 0b10001110 +#define DIGN 0b00000000 + +static const uint8_t digits[] = {DIG0, DIG1, DIG2, DIG3, DIG4, DIG5, DIG6, DIG7, DIG8, DIG9, + DIGA, DIGB, DIGC, DIGD, DIGE, DIGF}; + +static void putdata(uint8_t var){ + put_string("Got: "); + put_char('0'+var); + put_char('\n'); + // 0x40, 0xC0, ff .. 8f ; (0x44, 0xc0, 0x55, 0x89) + const uint8_t commands[] = {2, 3, DIG0, DIG1, DIG2, DIG3, 0xf1, 0x22, 3, 0xaa, 0x91}; + //const uint8_t commands[] = {2, 3, 2, 4, 8, 0x10, 0xf1, 0x22, 3, 0xaa, 0x91}; + const uint8_t commands1[] = {2, 3, DIG4, DIG5|DOT, DIG6, DIG7, 0xf1, 0x22, 3, 0xee, 0x91}; + //const uint8_t commands1[] = {2, 3, 0x20, 0x40|DOT, 0x80, DIG5, 0xf1, 0x22, 3, 0xee, 0x91}; + switch(var){ + case 0: + write_i2c(commands, 1); + write_i2c(&commands[1], 5); + write_i2c(&commands[6], 1); + break; + case 1: + write_i2c(&commands[7], 1); + write_i2c(&commands[8], 2); + write_i2c(&commands[10], 1); + break; + case 2: + write_i2c(commands1, 1); + write_i2c(&commands1[1], 5); + write_i2c(&commands1[6], 1); + break; + case 3: + write_i2c(&commands1[7], 1); + write_i2c(&commands1[8], 2); + write_i2c(&commands1[10], 1); + break; + } +} + +static void getk(){ + //uint8_t k[] = {0x42, 0xff}; + uint8_t k; + if(read_i2c(0x42, &k)){ // 0x42 + //if(write_i2c(k,2)){ + put_string("Keys: "); + put_uint(k); + put_char('\n'); + }else SEND("ERR\n"); +} + +static uint8_t display_number(int32_t N){ + if(N < -999 || N > 9999) return 1; + uint8_t buf[] = {2, 3, DIGN, DIGN, DIGN, DIGN, 0xf1}; + if(N < 0){ + buf[2] = MIN; + N = -N; + } + if(N == 0){ + buf[5] = DIG0; + }else{ + for(uint8_t idx = 5; idx > 1 && N; --idx){ + buf[idx] = digits[N%10]; + N /= 10; + } + } + write_i2c(buf, 1); + write_i2c(&buf[1], 5); + write_i2c(&buf[6], 1); + return 0; +} + +static uint8_t display_hex(int32_t N){ + if(N < -0xfff || N > 0xffff) return 1; + uint8_t buf[] = {2, 3, DIGN, DIGN, DIGN, DIGN, 0xf1}; + if(N < 0){ + buf[2] = MIN; + N = -N; + } + if(N == 0){ + buf[5] = DIG0; + }else{ + for(uint8_t idx = 5; idx > 1 && N; --idx){ + buf[idx] = digits[N&0xf]; + N >>= 4; + } + } + write_i2c(buf, 1); + write_i2c(&buf[1], 5); + write_i2c(&buf[6], 1); + return 0; +} + +static void dispnum(const char *n, uint8_t hex){ + int32_t N = -1; + uint8_t er = 0; + if(!getnum(n, &N)) er = 1; + else{ + if(hex) er = display_hex(N); + else er = display_number(N); + } + if(er) put_string("Wrong number!\n"); +} + +static void dispabcd(){ + uint8_t buf[] = {2, 3, DIGA, DIGB, DIGC, DIGD, 0xf1}; + write_i2c(buf, 1); + write_i2c(&buf[1], 5); + write_i2c(&buf[6], 1); +} + +/** + * @brief process_command - command parser + * @param command - command text (all inside [] without spaces) + * @return text to send over terminal or NULL + */ +char *process_command(const char *command){ + char *ret = NULL; + usart1_sendbuf(); // send buffer (if it is already filled) + if(*command < '0' || *command > '9'){ + switch(*command){ + case '?': // help + SEND_BLK( + "0..9 - send data\n" + "A - display 'ABCD'\n" + "G - get keqboard status\n" + "Hhex - display 'hex'\n" + "Nnum - display 'num'\n" + "R - reset\n" + ); + break; + case 'A': + dispabcd(); + break; + case 'G': + getk(); + break; + case 'H': + dispnum(++command, 1); + break; + case 'N': + dispnum(++command, 0); + break; + case 'R': // reset MCU + NVIC_SystemReset(); + break; + } + }else putdata(*command - '0'); + usart1_sendbuf(); + return ret; +} diff --git a/F0-nolib/TM1637/protocol.h b/F0-nolib/TM1637/protocol.h new file mode 100644 index 0000000..16df922 --- /dev/null +++ b/F0-nolib/TM1637/protocol.h @@ -0,0 +1,24 @@ +/* + * This file is part of the Chiller project. + * Copyright 2018 Edward V. Emelianov . + * + * 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 3 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, see . + */ +#ifndef PROTOCOL_H__ +#define PROTOCOL_H__ +#include + +char *process_command(const char *command); + +#endif // PROTOCOL_H__ diff --git a/F0-nolib/TM1637/tm1637.bin b/F0-nolib/TM1637/tm1637.bin new file mode 100755 index 0000000..0f7c8f4 Binary files /dev/null and b/F0-nolib/TM1637/tm1637.bin differ diff --git a/F0-nolib/TM1637/usart.c b/F0-nolib/TM1637/usart.c new file mode 100644 index 0000000..07fcec2 --- /dev/null +++ b/F0-nolib/TM1637/usart.c @@ -0,0 +1,298 @@ +/* + * This file is part of the TM1637 project. + * Copyright 2019 Edward Emelianov . + * + * 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 3 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, see . + */ +#include "usart.h" +#include // memcpy + +extern volatile uint32_t Tms; + +static int datalen[2] = {0,0}; // received data line length (including '\n') + +uint8_t bufovr = 0; // input buffer overfull + +static uint8_t linerdy = 0 // received data ready + ,dlen = 0 // length of data in current buffer + ,txrdy = 1 // transmission done +; + +static int rbufno = 0; // current rbuf number +static char rbuf[2][UARTBUFSZ+1], tbuf[UARTBUFSZ]; // receive & transmit buffers +static char *recvdata = NULL; + +static char trbuf[UARTBUFSZ+1]; // auxiliary buffer for data transmission +static int trbufidx = 0; + +int put_char(char c){ + if(trbufidx >= UARTBUFSZ - 1){ + for(int i = 0; i < 72000000 && ALL_OK != usart1_sendbuf(); ++i) + if(i == 72000000) return 1; + } + trbuf[trbufidx++] = c; + return 0; +} +// write zero-terminated string +int put_string(const char *str){ + while(*str){ + if(put_char(*str++)) return 1; //error! shouldn't be!!! + } + return 0; // all OK +} +/** + * Fill trbuf with integer value + * @param N - integer value + * @return 1 if buffer overflow; oterwise return 0 + */ +int put_int(int32_t N){ + if(N < 0){ + if(put_char('-')) return 1; + N = -N; + } + return put_uint((uint32_t) N); +} +int put_uint(uint32_t N){ + char buf[10]; + int L = 0; + if(N){ + while(N){ + buf[L++] = N % 10 + '0'; + N /= 10; + } + while(L--) if(put_char(buf[L])) return 1; + }else if(put_char('0')) return 1; + return 0; +} +/** + * @brief usart1_sendbuf - send temporary transmission buffer (trbuf) over USART + * @return Tx status + */ +TXstatus usart1_sendbuf(){ + int len = trbufidx; + if(len == 0) return ALL_OK; + else if(len > UARTBUFSZ) len = UARTBUFSZ; + TXstatus s = usart1_send(trbuf, len); + if(s == ALL_OK) trbufidx = 0; + return s; +} + +void USART1_config(){ + /* Enable the peripheral clock of GPIOA */ + RCC->AHBENR |= RCC_AHBENR_GPIOAEN; + /* GPIO configuration for USART1 signals */ + /* (1) Select AF mode (10) on PA9 and PA10 */ + /* (2) AF1 for USART1 signals */ + GPIOA->MODER = (GPIOA->MODER & ~(GPIO_MODER_MODER9|GPIO_MODER_MODER10))\ + | (GPIO_MODER_MODER9_1 | GPIO_MODER_MODER10_1); /* (1) */ + GPIOA->AFR[1] = (GPIOA->AFR[1] &~ (GPIO_AFRH_AFRH1 | GPIO_AFRH_AFRH2))\ + | (1 << (1 * 4)) | (1 << (2 * 4)); /* (2) */ + /* Enable the peripheral clock USART1 */ + RCC->APB2ENR |= RCC_APB2ENR_USART1EN; + /* Configure USART1 */ + /* (1) oversampling by 16, 115200 baud */ + /* (2) 8 data bit, 1 start bit, 1 stop bit, no parity */ + USART1->BRR = 480000 / 1152; /* (1) */ + USART1->CR1 = USART_CR1_TE | USART_CR1_RE | USART_CR1_UE; /* (2) */ + /* polling idle frame Transmission */ + while(!(USART1->ISR & USART_ISR_TC)){} + USART1->ICR |= USART_ICR_TCCF; /* clear TC flag */ + USART1->CR1 |= USART_CR1_RXNEIE; /* enable TC, TXE & RXNE interrupt */ + RCC->AHBENR |= RCC_AHBENR_DMA1EN; + 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 + USART1->CR3 = USART_CR3_DMAT; + NVIC_SetPriority(DMA1_Channel2_3_IRQn, 3); + NVIC_EnableIRQ(DMA1_Channel2_3_IRQn); + /* Configure IT */ + /* (3) Set priority for USART1_IRQn */ + /* (4) Enable USART1_IRQn */ + NVIC_SetPriority(USART1_IRQn, 0); /* (3) */ + NVIC_EnableIRQ(USART1_IRQn); /* (4) */ +} + +#ifdef EBUG +#define TMO 0 +#else +#define TMO 1 +#endif + +void usart1_isr(){ + static uint8_t timeout = TMO // == 0 for human interface without timeout + ,nctr = 0 // counter of '#' received + ,incmd = 0 // ==1 - inside command + ; + static uint32_t tmout = 0; + if(USART1->ISR & USART_ISR_RXNE){ // RX not emty - receive next char + // read RDR clears flag + uint8_t rb = USART1->RDR; + if(timeout && rb == '#'){ // chek '#' without timeout + if(++nctr == 4){ // "####" received - turn off timeout + timeout = 0; + nctr = 0; + datalen[rbufno] = 0; // reset all incoming data + incmd = 0; + return; + } + }else nctr = 0; + if(!incmd){ + if(rb == '['){ // open command or reset previoud input + datalen[rbufno] = 0; + incmd = 1; + } + return; + } + if(timeout){ // check timeout only inside commands + if(tmout && Tms >= tmout){ // set overflow flag + bufovr = 1; + datalen[rbufno] = 0; + }else{ + tmout = Tms + TIMEOUT_MS; + if(!tmout) tmout = 1; // prevent 0 + } + } + switch(rb){ + case '[': + datalen[rbufno] = 0; + tmout = 0; + break; + case ']': // close command - line ready! + dlen = datalen[rbufno]; + if(dlen){ + linerdy = 1; + incmd = 0; + recvdata = rbuf[rbufno]; + rbuf[rbufno][dlen] = 0; + rbufno = !rbufno; + datalen[rbufno] = 0; + } + tmout = 0; + break; + case '\r': + case '\n': + case ' ': + case '\t': + return; + break; + default: + break; + } + + if(datalen[rbufno] < UARTBUFSZ){ // put next char into buf + rbuf[rbufno][datalen[rbufno]++] = rb; + }else{ // buffer overrun + bufovr = 1; + datalen[rbufno] = 0; + incmd = 0; + tmout = 0; + } + } +} + +void dma1_channel2_3_isr(){ + if(DMA1->ISR & DMA_ISR_TCIF2){ // Tx + DMA1->IFCR |= DMA_IFCR_CTCIF2; // clear TC flag + txrdy = 1; + } +} + +/** + * return length of received data (without trailing zero) + */ +int usart1_getline(char **line){ + if(!linerdy) return 0; + linerdy = 0; + if(bufovr){ + bufovr = 0; + return 0; + } + *line = recvdata; + return dlen; +} + +/** + * @brief usart1_send send buffer `str` adding trailing '\n' + * @param str - string to send (without '\n' at end) + * @param len - its length or 0 to auto count + * @return + */ +TXstatus usart1_send(const char *str, int len){ + if(!txrdy) return LINE_BUSY; + if(len > UARTBUFSZ - 1) return STR_TOO_LONG; + txrdy = 0; + if(len == 0){ + const char *ptr = str; + while(*ptr++) ++len; + } + if(len == 0) return ALL_OK; + DMA1_Channel2->CCR &= ~DMA_CCR_EN; + memcpy(tbuf, str, len); +// tbuf[len++] = '\n'; + DMA1_Channel2->CNDTR = len; + DMA1_Channel2->CCR |= DMA_CCR_EN; // start transmission + return ALL_OK; +} + +TXstatus usart1_send_blocking(const char *str, int len){ + if(!txrdy) return LINE_BUSY; + if(len == 0){ + const char *ptr = str; + while(*ptr++) ++len; + } + if(len == 0) return ALL_OK; + for(int i = 0; i < len; ++i){ + USART1->TDR = *str++; + while(!(USART1->ISR & USART_ISR_TXE)); + } +// USART1->TDR = '\n'; + while(!(USART1->ISR & USART_ISR_TC)); + txrdy = 1; + return ALL_OK; +} + +// read `buf` and get first integer `N` in it +// @return pointer to first non-number if all OK or NULL if first symbol isn't a space or number +char *getnum(const char *buf, int32_t *N){ + char c; + int positive = -1; + int32_t val = 0; + //usart1_send_blocking(buf, 0); + while((c = *buf++)){ + if(c == '\t' || c == ' '){ + if(positive < 0) continue; // beginning spaces + else break; // spaces after number + } + if(c == '-'){ + if(positive < 0){ + positive = 0; + continue; + }else break; // there already was `-` or number + } + if(c < '0' || c > '9') break; + if(positive < 0) positive = 1; + val = val * 10 + (int32_t)(c - '0'); + } + if(positive != -1){ + if(positive == 0){ + if(val == 0) return NULL; // single '-' + val = -val; + } + *N = val; + }else return NULL; +/* usart1_sendbuf(); + put_uint(val); + put_char('\n');*/ + return (char*)buf-1; +} diff --git a/F0-nolib/TM1637/usart.h b/F0-nolib/TM1637/usart.h new file mode 100644 index 0000000..bba6026 --- /dev/null +++ b/F0-nolib/TM1637/usart.h @@ -0,0 +1,56 @@ +/* + * This file is part of the TM1637 project. + * Copyright 2019 Edward Emelianov . + * + * 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 3 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, see . + */ +#pragma once +#ifndef __USART_H__ +#define __USART_H__ + +#include "stm32f0.h" + +// input and output buffers size +#define UARTBUFSZ (64) +// timeout between data bytes +#define TIMEOUT_MS (100) + +typedef enum{ + ALL_OK, + LINE_BUSY, + STR_TOO_LONG +} TXstatus; + +#define usart1ovr() (bufovr) + +// send constant string +#define SEND_BLK(x) do{while(LINE_BUSY == usart1_send_blocking(x, sizeof(x)-1)) IWDG->KR = IWDG_REFRESH;}while(0) +#define SEND(x) do{while(LINE_BUSY == usart1_send(x, sizeof(x)-1)) IWDG->KR = IWDG_REFRESH;}while(0) + +extern uint8_t bufovr; + +void USART1_config(); +int usart1_getline(char **line); +TXstatus usart1_send(const char *str, int len); +TXstatus usart1_send_blocking(const char *str, int len); +TXstatus usart1_sendbuf(); + +int put_char(char c); +int put_string(const char *str); +int put_int(int32_t N); +int put_uint(uint32_t N); + +char *getnum(const char *buf, int32_t *N); + +#endif // __USART_H__ diff --git a/F0-nolib/USBHID/usbhid.bin b/F0-nolib/USBHID/usbhid.bin index bdb7faa..00cde93 100755 Binary files a/F0-nolib/USBHID/usbhid.bin and b/F0-nolib/USBHID/usbhid.bin differ diff --git a/F0-nolib/inc/F0/stm32f0.h b/F0-nolib/inc/F0/stm32f0.h index 3b99a87..4970cc7 100644 --- a/F0-nolib/inc/F0/stm32f0.h +++ b/F0-nolib/inc/F0/stm32f0.h @@ -119,7 +119,7 @@ TRUE_INLINE void StartHSE(){ while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)RCC_CFGR_SWS_PLL){} } */ -#if !defined (STM32F030x4) && !defined (STM32F030x6) && !defined (STM32F030x8) && !defined (STM32F031x6) && !defined (STM32F038xx) && !defined (STM32F030xC) +#if defined (STM32F042x6) || defined (STM32F072xb) TRUE_INLINE void StartHSI48(){ RCC->APB1ENR |= RCC_APB1ENR_CRSEN | RCC_APB1ENR_USBEN; // enable CRS (hsi48 sync) & USB RCC->CFGR3 &= ~RCC_CFGR3_USBSW; // reset USB diff --git a/F0-nolib/inc/ld/stm32f051x8.ld b/F0-nolib/inc/ld/stm32f051x8.ld new file mode 100644 index 0000000..3dc0803 --- /dev/null +++ b/F0-nolib/inc/ld/stm32f051x8.ld @@ -0,0 +1,12 @@ +/* Linker script for STM32F051x8, 64K flash, 8K RAM. */ + +/* Define memory regions. */ +MEMORY +{ + rom (rx) : ORIGIN = 0x08000000, LENGTH = 64K + ram (rwx) : ORIGIN = 0x20000000, LENGTH = 8K +} + +/* Include the common ld script. */ +INCLUDE stm32f01234.ld +