mirror of
https://github.com/eddyem/stm32samples.git
synced 2026-02-28 20:04:30 +03:00
hide deprecated code; make USB snippet common for F0/F1/F3
This commit is contained in:
32
F0:F030,F042,F072/deprecated/canbus/src/CANbus.geany
Normal file
32
F0:F030,F042,F072/deprecated/canbus/src/CANbus.geany
Normal file
@@ -0,0 +1,32 @@
|
||||
[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=CANbus
|
||||
base_path=/home/eddy/Docs/SAO/ELECTRONICS/STM32/F0-srcs/canbus/src
|
||||
|
||||
[long line marker]
|
||||
long_line_behaviour=1
|
||||
long_line_column=100
|
||||
|
||||
[files]
|
||||
current_page=-1
|
||||
|
||||
[VTE]
|
||||
last_dir=/home/eddy
|
||||
145
F0:F030,F042,F072/deprecated/canbus/src/Makefile
Normal file
145
F0:F030,F042,F072/deprecated/canbus/src/Makefile
Normal file
@@ -0,0 +1,145 @@
|
||||
BINARY = canbus
|
||||
BOOTPORT ?= /dev/ttyUSB0
|
||||
BOOTSPEED ?= 57600
|
||||
# MCU FAMILY
|
||||
FAMILY = F0
|
||||
# MCU code
|
||||
MCU = F042x6
|
||||
# hardware definitions
|
||||
DEFS += -DUSARTNUM=1
|
||||
#DEFS += -DCHECK_TMOUT
|
||||
DEFS += -DEBUG
|
||||
# change this linking script depending on particular MCU model,
|
||||
# for example, if you have STM32F103VBT6, you should write:
|
||||
LDSCRIPT = 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 -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 $<
|
||||
|
||||
#$(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 *.d
|
||||
@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
|
||||
347
F0:F030,F042,F072/deprecated/canbus/src/can.c
Normal file
347
F0:F030,F042,F072/deprecated/canbus/src/can.c
Normal file
@@ -0,0 +1,347 @@
|
||||
/*
|
||||
* geany_encoding=koi8-r
|
||||
* can.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 <string.h> // memcpy
|
||||
#include "can.h"
|
||||
#include "hardware.h"
|
||||
#include "usart.h"
|
||||
|
||||
#define CMD_TOGGLE (0xDA)
|
||||
#define CAN_ID_MASK (0x7F8)
|
||||
#define CAN_ID_PREFIX (0xAAA)
|
||||
#define TARG_ID (CAN_ID_PREFIX & CAN_ID_MASK)
|
||||
|
||||
#define CAN_FLAG_GOTDUMMY (1)
|
||||
// incoming message buffer size
|
||||
#define CAN_INMESSAGE_SIZE (6)
|
||||
|
||||
extern volatile uint32_t Tms;
|
||||
|
||||
// circular buffer for received messages
|
||||
static CAN_message messages[CAN_INMESSAGE_SIZE];
|
||||
static uint8_t first_free_idx = 0; // index of first empty cell
|
||||
static int8_t first_nonfree_idx = -1; // index of first data cell
|
||||
|
||||
static uint16_t CANID = 0xFFFF;
|
||||
static uint8_t CAN_flags = 0;
|
||||
static uint32_t last_err_code = 0;
|
||||
static CAN_status can_status = CAN_STOP;
|
||||
|
||||
static void can_process_fifo(uint8_t fifo_num);
|
||||
|
||||
CAN_status CAN_get_status(){
|
||||
CAN_status st = can_status;
|
||||
// give overrun message only once
|
||||
if(st == CAN_FIFO_OVERRUN) can_status = CAN_READY;
|
||||
return st;
|
||||
}
|
||||
|
||||
// push next message into buffer; return 1 if buffer overfull
|
||||
static int CAN_messagebuf_push(CAN_message *msg){
|
||||
MSG("Try to push\n");
|
||||
if(first_free_idx == first_nonfree_idx) return 1; // no free space
|
||||
memcpy(&messages[first_free_idx++], msg, sizeof(CAN_message));
|
||||
// need to roll?
|
||||
if(first_free_idx == CAN_INMESSAGE_SIZE) first_free_idx = 0;
|
||||
#ifdef EBUG
|
||||
MSG("1st free: "); usart_putchar('0' + first_free_idx); newline();
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
// pop message from buffer
|
||||
CAN_message *CAN_messagebuf_pop(){
|
||||
if(first_nonfree_idx < 0) return NULL;
|
||||
#ifdef EBUG
|
||||
MSG("read from idx "); usart_putchar('0' + first_nonfree_idx); newline();
|
||||
#endif
|
||||
CAN_message *msg = &messages[first_nonfree_idx++];
|
||||
if(first_nonfree_idx == CAN_INMESSAGE_SIZE) first_nonfree_idx = 0;
|
||||
if(first_nonfree_idx == first_free_idx){ // buffer is empty - refresh it
|
||||
first_nonfree_idx = -1;
|
||||
first_free_idx = 0;
|
||||
MSG("refresh buffer\n");
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
|
||||
// get CAN address data from GPIO pins
|
||||
void readCANID(){
|
||||
uint8_t CAN_addr = READ_CAN_INV_ADDR();
|
||||
CAN_addr = ~CAN_addr & 0x7;
|
||||
CANID = (CAN_ID_PREFIX & CAN_ID_MASK) | CAN_addr;
|
||||
}
|
||||
|
||||
uint16_t getCANID(){
|
||||
return CANID;
|
||||
}
|
||||
|
||||
void CAN_reinit(){
|
||||
readCANID();
|
||||
CAN->TSR |= CAN_TSR_ABRQ0 | CAN_TSR_ABRQ1 | CAN_TSR_ABRQ2;
|
||||
RCC->APB1RSTR |= RCC_APB1RSTR_CANRST;
|
||||
RCC->APB1RSTR &= ~RCC_APB1RSTR_CANRST;
|
||||
CAN_setup();
|
||||
}
|
||||
|
||||
void CAN_setup(){
|
||||
if(CANID == 0xFFFF) readCANID();
|
||||
// Configure GPIO: PB8 - CAN_Rx, PB9 - CAN_Tx
|
||||
/* (1) Select AF mode (10) on PB8 and PB9 */
|
||||
/* (2) AF4 for CAN signals */
|
||||
GPIOB->MODER = (GPIOB->MODER & ~(GPIO_MODER_MODER8 | GPIO_MODER_MODER9))
|
||||
| (GPIO_MODER_MODER8_AF | GPIO_MODER_MODER9_AF); /* (1) */
|
||||
GPIOB->AFR[1] = (GPIOB->AFR[1] &~ (GPIO_AFRH_AFRH0 | GPIO_AFRH_AFRH1))\
|
||||
| (4 << (0 * 4)) | (4 << (1 * 4)); /* (2) */
|
||||
/* Enable the peripheral clock CAN */
|
||||
RCC->APB1ENR |= RCC_APB1ENR_CANEN;
|
||||
/* Configure CAN */
|
||||
/* (1) Enter CAN init mode to write the configuration */
|
||||
/* (2) Wait the init mode entering */
|
||||
/* (3) Exit sleep mode */
|
||||
/* (4) Loopback mode, set timing to 100kb/s: BS1 = 4, BS2 = 3, prescaler = 60 */
|
||||
/* (5) Leave init mode */
|
||||
/* (6) Wait the init mode leaving */
|
||||
/* (7) Enter filter init mode, (16-bit + mask, filter 0 for FIFO 0) */
|
||||
/* (8) Acivate filter 0 */
|
||||
/* (9) Identifier list mode */
|
||||
/* (10) Set the Id list */
|
||||
/* (12) Leave filter init */
|
||||
/* (13) Set error interrupts enable */
|
||||
CAN->MCR |= CAN_MCR_INRQ; /* (1) */
|
||||
while((CAN->MSR & CAN_MSR_INAK)!=CAN_MSR_INAK) /* (2) */
|
||||
{
|
||||
/* add time out here for a robust application */
|
||||
}
|
||||
CAN->MCR &=~ CAN_MCR_SLEEP; /* (3) */
|
||||
CAN->MCR |= CAN_MCR_ABOM;
|
||||
|
||||
CAN->BTR = 2 << 20 | 3 << 16 | 59 << 0; /* (4) */
|
||||
CAN->MCR &=~ CAN_MCR_INRQ; /* (5) */
|
||||
while((CAN->MSR & CAN_MSR_INAK)==CAN_MSR_INAK) /* (6) */
|
||||
{
|
||||
/* add time out here for a robust application */
|
||||
}
|
||||
CAN->FMR = CAN_FMR_FINIT; /* (7) */
|
||||
CAN->FA1R = CAN_FA1R_FACT0; /* (8) */
|
||||
CAN->FM1R = CAN_FM1R_FBM0; /* (9) */
|
||||
CAN->sFilterRegister[0].FR1 = CANID << 5; /* (10) */
|
||||
|
||||
CAN->FMR &=~ CAN_FMR_FINIT; /* (12) */
|
||||
CAN->IER |= CAN_IER_ERRIE | CAN_IER_FOVIE0 | CAN_IER_FOVIE1; /* (13) */
|
||||
|
||||
/* Configure IT */
|
||||
/* (14) Set priority for CAN_IRQn */
|
||||
/* (15) Enable CAN_IRQn */
|
||||
NVIC_SetPriority(CEC_CAN_IRQn, 0); /* (14) */
|
||||
NVIC_EnableIRQ(CEC_CAN_IRQn); /* (15) */
|
||||
can_status = CAN_READY;
|
||||
}
|
||||
|
||||
void can_proc(){
|
||||
if(last_err_code){
|
||||
#ifdef EBUG
|
||||
MSG("Error, ESR=");
|
||||
printu(last_err_code);
|
||||
newline();
|
||||
#endif
|
||||
last_err_code = 0;
|
||||
}
|
||||
// check for messages in FIFO0 & FIFO1
|
||||
if(CAN->RF0R & CAN_RF0R_FMP0){
|
||||
can_process_fifo(0);
|
||||
}
|
||||
if(CAN->RF1R & CAN_RF1R_FMP1){
|
||||
can_process_fifo(1);
|
||||
}
|
||||
if(CAN->ESR & (CAN_ESR_BOFF | CAN_ESR_EPVF | CAN_ESR_EWGF)){ // much errors - restart CAN BUS
|
||||
MSG("bus-off, restarting\n");
|
||||
// request abort for all mailboxes
|
||||
CAN->TSR |= CAN_TSR_ABRQ0 | CAN_TSR_ABRQ1 | CAN_TSR_ABRQ2;
|
||||
// reset CAN bus
|
||||
RCC->APB1RSTR |= RCC_APB1RSTR_CANRST;
|
||||
RCC->APB1RSTR &= ~RCC_APB1RSTR_CANRST;
|
||||
CAN_setup();
|
||||
}
|
||||
if(CAN_flags){
|
||||
if(CAN_flags & CAN_FLAG_GOTDUMMY){
|
||||
SEND("Got dummy message\n");
|
||||
}
|
||||
CAN_flags = 0;
|
||||
}
|
||||
LED_off(LED1);
|
||||
#ifdef EBUG
|
||||
static uint32_t esr, msr, tsr;
|
||||
uint32_t msr_now = CAN->MSR & 0xf;
|
||||
if(esr != CAN->ESR || msr != msr_now || tsr != CAN->TSR){
|
||||
MSG("Timestamp: ");
|
||||
printu(Tms);
|
||||
newline();
|
||||
}
|
||||
if((CAN->ESR) != esr){
|
||||
usart_putchar(((CAN->ESR & CAN_ESR_BOFF) != 0) + '0');
|
||||
esr = CAN->ESR;
|
||||
MSG("CAN->ESR: ");
|
||||
printuhex(esr); newline();
|
||||
}
|
||||
if(msr_now != msr){
|
||||
msr = msr_now;
|
||||
MSG("CAN->MSR & 0xf: ");
|
||||
printuhex(msr); newline();
|
||||
}
|
||||
if(CAN->TSR != tsr){
|
||||
tsr = CAN->TSR;
|
||||
MSG("CAN->TSR: ");
|
||||
printuhex(tsr); newline();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
CAN_status can_send(uint8_t *msg, uint8_t len, uint16_t target_id){
|
||||
uint8_t mailbox = 0;
|
||||
// check first free mailbox
|
||||
if(CAN->TSR & (CAN_TSR_TME)){
|
||||
mailbox = (CAN->TSR & CAN_TSR_CODE) >> 24;
|
||||
#ifdef EBUG
|
||||
MSG("select "); usart_putchar('0'+mailbox); SEND(" mailbox\n");
|
||||
#endif
|
||||
}else{ // no free mailboxes
|
||||
return CAN_BUSY;
|
||||
}
|
||||
CAN_TxMailBox_TypeDef *box = &CAN->sTxMailBox[mailbox];
|
||||
uint32_t lb = 0, hb = 0;
|
||||
switch(len){
|
||||
case 8:
|
||||
hb |= (uint32_t)msg[7] << 24;
|
||||
__attribute__((fallthrough));
|
||||
case 7:
|
||||
hb |= (uint32_t)msg[6] << 16;
|
||||
__attribute__((fallthrough));
|
||||
case 6:
|
||||
hb |= (uint32_t)msg[5] << 8;
|
||||
__attribute__((fallthrough));
|
||||
case 5:
|
||||
hb |= (uint32_t)msg[4];
|
||||
__attribute__((fallthrough));
|
||||
case 4:
|
||||
lb |= (uint32_t)msg[3] << 24;
|
||||
__attribute__((fallthrough));
|
||||
case 3:
|
||||
lb |= (uint32_t)msg[2] << 16;
|
||||
__attribute__((fallthrough));
|
||||
case 2:
|
||||
lb |= (uint32_t)msg[1] << 8;
|
||||
__attribute__((fallthrough));
|
||||
default:
|
||||
lb |= (uint32_t)msg[0];
|
||||
}
|
||||
box->TDLR = lb;
|
||||
box->TDHR = hb;
|
||||
box->TDTR = len;
|
||||
box->TIR = (target_id & 0x7FF) << 21 | CAN_TI0R_TXRQ;
|
||||
return CAN_OK;
|
||||
}
|
||||
|
||||
void can_send_dummy(){
|
||||
uint8_t msg = CMD_TOGGLE;
|
||||
if(CAN_OK != can_send(&msg, 1, TARG_ID)) SEND("Bus busy!\n");
|
||||
MSG("CAN->MSR: ");
|
||||
printuhex(CAN->MSR); newline();
|
||||
MSG("CAN->TSR: ");
|
||||
printuhex(CAN->TSR); newline();
|
||||
MSG("CAN->ESR: ");
|
||||
printuhex(CAN->ESR); newline();
|
||||
}
|
||||
|
||||
static void can_process_fifo(uint8_t fifo_num){
|
||||
if(fifo_num > 1) return;
|
||||
LED_on(LED1); // Toggle LED1
|
||||
CAN_FIFOMailBox_TypeDef *box = &CAN->sFIFOMailBox[fifo_num];
|
||||
volatile uint32_t *RFxR = (fifo_num) ? &CAN->RF1R : &CAN->RF0R;
|
||||
MSG("Receive, RDTR=");
|
||||
#ifdef EBUG
|
||||
printuhex(box->RDTR);
|
||||
newline();
|
||||
#endif
|
||||
// read all
|
||||
while(*RFxR & CAN_RF0R_FMP0){ // amount of messages pending
|
||||
// CAN_RDTxR: (16-31) - timestamp, (8-15) - filter match index, (0-3) - data length
|
||||
/* TODO: check filter match index if more than one ID can receive */
|
||||
CAN_message msg;
|
||||
uint8_t *dat = msg.data;
|
||||
uint8_t len = box->RDTR & 0x7;
|
||||
msg.length = len;
|
||||
if(len){ // message can be without data
|
||||
uint32_t hb = box->RDHR, lb = box->RDLR;
|
||||
switch(len){
|
||||
case 8:
|
||||
dat[7] = hb>>24;
|
||||
__attribute__((fallthrough));
|
||||
case 7:
|
||||
dat[6] = (hb>>16) & 0xff;
|
||||
__attribute__((fallthrough));
|
||||
case 6:
|
||||
dat[5] = (hb>>8) & 0xff;
|
||||
__attribute__((fallthrough));
|
||||
case 5:
|
||||
dat[4] = hb & 0xff;
|
||||
__attribute__((fallthrough));
|
||||
case 4:
|
||||
dat[3] = lb>>24;
|
||||
__attribute__((fallthrough));
|
||||
case 3:
|
||||
dat[2] = (lb>>16) & 0xff;
|
||||
__attribute__((fallthrough));
|
||||
case 2:
|
||||
dat[1] = (lb>>8) & 0xff;
|
||||
__attribute__((fallthrough));
|
||||
case 1:
|
||||
dat[0] = lb & 0xff;
|
||||
}
|
||||
}
|
||||
if(CAN_messagebuf_push(&msg)) return; // error: buffer is full, try later
|
||||
*RFxR |= CAN_RF0R_RFOM0; // release fifo for access to next message
|
||||
}
|
||||
if(*RFxR & CAN_RF0R_FULL0) *RFxR &= ~CAN_RF0R_FULL0;
|
||||
}
|
||||
|
||||
void cec_can_isr(){
|
||||
if(CAN->RF0R & CAN_RF0R_FOVR0){ // FIFO overrun
|
||||
CAN->RF0R &= ~CAN_RF0R_FOVR0;
|
||||
can_status = CAN_FIFO_OVERRUN;
|
||||
}
|
||||
if(CAN->RF1R & CAN_RF1R_FOVR1){
|
||||
CAN->RF1R &= ~CAN_RF1R_FOVR1;
|
||||
can_status = CAN_FIFO_OVERRUN;
|
||||
}
|
||||
#ifdef EBUG
|
||||
if(can_status == CAN_FIFO_OVERRUN) MSG("fifo 0 overrun\n");
|
||||
#endif
|
||||
if(CAN->MSR & CAN_MSR_ERRI){ // Error
|
||||
CAN->MSR &= ~CAN_MSR_ERRI;
|
||||
// request abort for problem mailbox
|
||||
if(CAN->TSR & CAN_TSR_TERR0) CAN->TSR |= CAN_TSR_ABRQ0;
|
||||
if(CAN->TSR & CAN_TSR_TERR1) CAN->TSR |= CAN_TSR_ABRQ1;
|
||||
if(CAN->TSR & CAN_TSR_TERR2) CAN->TSR |= CAN_TSR_ABRQ2;
|
||||
last_err_code = CAN->ESR;
|
||||
}
|
||||
}
|
||||
55
F0:F030,F042,F072/deprecated/canbus/src/can.h
Normal file
55
F0:F030,F042,F072/deprecated/canbus/src/can.h
Normal file
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* geany_encoding=koi8-r
|
||||
* can.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 __CAN_H__
|
||||
#define __CAN_H__
|
||||
|
||||
#include "hardware.h"
|
||||
|
||||
typedef struct{
|
||||
uint8_t data[8];
|
||||
uint8_t length;
|
||||
} CAN_message;
|
||||
|
||||
typedef enum{
|
||||
CAN_STOP,
|
||||
CAN_READY,
|
||||
CAN_BUSY,
|
||||
CAN_OK,
|
||||
CAN_FIFO_OVERRUN
|
||||
} CAN_status;
|
||||
|
||||
CAN_status CAN_get_status();
|
||||
|
||||
void readCANID();
|
||||
uint16_t getCANID();
|
||||
|
||||
void CAN_reinit();
|
||||
void CAN_setup();
|
||||
|
||||
void can_send_dummy();
|
||||
void can_proc();
|
||||
|
||||
CAN_message *CAN_messagebuf_pop();
|
||||
|
||||
#endif // __CAN_H__
|
||||
BIN
F0:F030,F042,F072/deprecated/canbus/src/canbus.bin
Executable file
BIN
F0:F030,F042,F072/deprecated/canbus/src/canbus.bin
Executable file
Binary file not shown.
4387
F0:F030,F042,F072/deprecated/canbus/src/canbus.c.tags
Normal file
4387
F0:F030,F042,F072/deprecated/canbus/src/canbus.c.tags
Normal file
File diff suppressed because it is too large
Load Diff
43
F0:F030,F042,F072/deprecated/canbus/src/hardware.c
Normal file
43
F0:F030,F042,F072/deprecated/canbus/src/hardware.c
Normal file
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* 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"
|
||||
#include "usart.h"
|
||||
|
||||
void gpio_setup(void){
|
||||
// here we turn on clocking for all periph.
|
||||
RCC->AHBENR |= RCC_AHBENR_GPIOAEN | RCC_AHBENR_GPIOBEN | RCC_AHBENR_GPIOCEN | RCC_AHBENR_DMAEN;
|
||||
// Set LEDS (PC13/14) as output
|
||||
GPIOC->MODER = (GPIOC->MODER & ~(GPIO_MODER_MODER13 | GPIO_MODER_MODER14)
|
||||
) |
|
||||
GPIO_MODER_MODER13_O | GPIO_MODER_MODER14_O;
|
||||
// PB14(0), PB15(1), PA8(2) - CAN address, pullup inputs
|
||||
GPIOA->PUPDR = (GPIOA->PUPDR & ~(GPIO_PUPDR_PUPDR8)
|
||||
) |
|
||||
GPIO_PUPDR_PUPDR8_0;
|
||||
GPIOB->PUPDR = (GPIOB->PUPDR & ~(GPIO_PUPDR_PUPDR14 | GPIO_PUPDR_PUPDR15)
|
||||
) |
|
||||
GPIO_PUPDR_PUPDR14_0 | GPIO_PUPDR_PUPDR15_0;
|
||||
pin_set(LED0_port, LED0_pin); // clear LEDs
|
||||
pin_set(LED1_port, LED1_pin);
|
||||
}
|
||||
54
F0:F030,F042,F072/deprecated/canbus/src/hardware.h
Normal file
54
F0:F030,F042,F072/deprecated/canbus/src/hardware.h
Normal file
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* 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"
|
||||
|
||||
#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)
|
||||
|
||||
// LEDS: 0 - PC13, 1 - PC14
|
||||
// LED0
|
||||
#define LED0_port GPIOC
|
||||
#define LED0_pin (1<<13)
|
||||
// LED1
|
||||
#define LED1_port GPIOC
|
||||
#define LED1_pin (1<<14)
|
||||
|
||||
#define LED_blink(x) pin_toggle(x ## _port, x ## _pin)
|
||||
#define LED_on(x) pin_clear(x ## _port, x ## _pin)
|
||||
#define LED_off(x) pin_set(x ## _port, x ## _pin)
|
||||
|
||||
|
||||
// CAN address - PB14(0), PB15(1), PA8(2)
|
||||
#define READ_CAN_INV_ADDR() (((GPIOA->IDR & (1<<8))>>6)|((GPIOB->IDR & (3<<14))>>14))
|
||||
|
||||
void gpio_setup(void);
|
||||
|
||||
#endif // __HARDWARE_H__
|
||||
148
F0:F030,F042,F072/deprecated/canbus/src/main.c
Normal file
148
F0:F030,F042,F072/deprecated/canbus/src/main.c
Normal file
@@ -0,0 +1,148 @@
|
||||
/*
|
||||
* 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 "can.h"
|
||||
|
||||
volatile uint32_t Tms = 0;
|
||||
|
||||
/* Called when systick fires */
|
||||
void sys_tick_handler(void){
|
||||
++Tms;
|
||||
}
|
||||
|
||||
void iwdg_setup(){
|
||||
/* Enable the peripheral clock RTC */
|
||||
/* (1) Enable the LSI (40kHz) */
|
||||
/* (2) Wait while it is not ready */
|
||||
RCC->CSR |= RCC_CSR_LSION; /* (1) */
|
||||
while((RCC->CSR & RCC_CSR_LSIRDY) != RCC_CSR_LSIRDY); /* (2) */
|
||||
/* Configure IWDG */
|
||||
/* (1) Activate IWDG (not needed if done in option bytes) */
|
||||
/* (2) Enable write access to IWDG registers */
|
||||
/* (3) Set prescaler by 64 (1.6ms for each tick) */
|
||||
/* (4) Set reload value to have a rollover each 2s */
|
||||
/* (5) Check if flags are reset */
|
||||
/* (6) Refresh counter */
|
||||
IWDG->KR = IWDG_START; /* (1) */
|
||||
IWDG->KR = IWDG_WRITE_ACCESS; /* (2) */
|
||||
IWDG->PR = IWDG_PR_PR_1; /* (3) */
|
||||
IWDG->RLR = 1250; /* (4) */
|
||||
while(IWDG->SR); /* (5) */
|
||||
IWDG->KR = IWDG_REFRESH; /* (6) */
|
||||
}
|
||||
|
||||
int main(void){
|
||||
uint32_t lastT = 0;
|
||||
uint8_t ctr, len;
|
||||
CAN_message *can_mesg;
|
||||
int L;
|
||||
char *txt;
|
||||
sysreset();
|
||||
SysTick_Config(6000, 1);
|
||||
gpio_setup();
|
||||
usart_setup();
|
||||
iwdg_setup();
|
||||
readCANID();
|
||||
CAN_setup();
|
||||
|
||||
SEND("Greetings! My address is ");
|
||||
printuhex(getCANID());
|
||||
newline();
|
||||
|
||||
if(RCC->CSR & RCC_CSR_IWDGRSTF){ // watchdog reset occured
|
||||
SEND("WDGRESET=1\n");
|
||||
}
|
||||
if(RCC->CSR & RCC_CSR_SFTRSTF){ // software reset occured
|
||||
SEND("SOFTRESET=1\n");
|
||||
}
|
||||
RCC->CSR |= RCC_CSR_RMVF; // remove reset flags
|
||||
|
||||
while (1){
|
||||
IWDG->KR = IWDG_REFRESH; // refresh watchdog
|
||||
if(lastT > Tms || Tms - lastT > 499){
|
||||
LED_blink(LED0);
|
||||
lastT = Tms;
|
||||
}
|
||||
can_proc();
|
||||
if(CAN_get_status() == CAN_FIFO_OVERRUN){
|
||||
SEND("CAN bus fifo overrun occured!\n");
|
||||
}
|
||||
can_mesg = CAN_messagebuf_pop();
|
||||
if(can_mesg){ // new data in buff
|
||||
len = can_mesg->length;
|
||||
SEND("got message, len: "); usart_putchar('0' + len);
|
||||
SEND(", data: ");
|
||||
for(ctr = 0; ctr < len; ++ctr){
|
||||
printuhex(can_mesg->data[ctr]);
|
||||
usart_putchar(' ');
|
||||
}
|
||||
newline();
|
||||
}
|
||||
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':
|
||||
can_send_dummy();
|
||||
break;
|
||||
case 'G':
|
||||
SEND("Can address: ");
|
||||
printuhex(getCANID());
|
||||
newline();
|
||||
break;
|
||||
case 'R':
|
||||
SEND("Soft reset\n");
|
||||
NVIC_SystemReset();
|
||||
break;
|
||||
case 'S':
|
||||
CAN_reinit();
|
||||
SEND("Can address: ");
|
||||
printuhex(getCANID());
|
||||
newline();
|
||||
break;
|
||||
case 'W':
|
||||
SEND("Wait for reboot\n");
|
||||
while(1){nop();};
|
||||
break;
|
||||
default: // help
|
||||
SEND(
|
||||
"'C' - send dummy byte over CAN\n"
|
||||
"'G' - get CAN address\n"
|
||||
"'R' - software reset\n"
|
||||
"'S' - reinit CAN (with new address)\n"
|
||||
"'W' - test watchdog\n"
|
||||
);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(L){ // text waits for sending
|
||||
while(LINE_BUSY == usart_send(txt, L));
|
||||
L = 0;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
252
F0:F030,F042,F072/deprecated/canbus/src/usart.c
Normal file
252
F0:F030,F042,F072/deprecated/canbus/src/usart.c
Normal file
@@ -0,0 +1,252 @@
|
||||
/*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')
|
||||
|
||||
volatile 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));
|
||||
}
|
||||
|
||||
// print 32bit unsigned int as hex
|
||||
void printuhex(uint32_t val){
|
||||
SEND("0x");
|
||||
uint8_t *ptr = (uint8_t*)&val + 3;
|
||||
int i, j;
|
||||
for(i = 0; i < 4; ++i, --ptr){
|
||||
for(j = 1; j > -1; --j){
|
||||
uint8_t half = (*ptr >> (4*j)) & 0x0f;
|
||||
if(half < 10) usart_putchar(half + '0');
|
||||
else usart_putchar(half - 10 + 'a');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#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
|
||||
61
F0:F030,F042,F072/deprecated/canbus/src/usart.h
Normal file
61
F0:F030,F042,F072/deprecated/canbus/src/usart.h
Normal file
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* 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))
|
||||
|
||||
#ifdef EBUG
|
||||
#define MSG(str) do{SEND(__FILE__ " (L" STR(__LINE__) "): " str);}while(0)
|
||||
#else
|
||||
#define MSG(str)
|
||||
#endif
|
||||
|
||||
typedef enum{
|
||||
ALL_OK,
|
||||
LINE_BUSY,
|
||||
STR_TOO_LONG
|
||||
} TXstatus;
|
||||
|
||||
#define usartrx() (linerdy)
|
||||
#define usartovr() (bufovr)
|
||||
|
||||
extern volatile 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);
|
||||
void printuhex(uint32_t val);
|
||||
|
||||
#endif // __USART_H__
|
||||
Reference in New Issue
Block a user