Change USART1 to LEDs for T0/T1 presence indication (@ Tcalc_screen_ver2)

This commit is contained in:
eddyem 2019-09-18 16:41:35 +03:00
parent 4bd57d704a
commit 8491f4bf90
13 changed files with 42 additions and 309 deletions

View File

@ -1,6 +1,6 @@
BINARY = tcalc_screen
BOOTPORT ?= /dev/ttyUSB0
BOOTSPEED ?= 9600
BOOTSPEED ?= 115200
# MCU FAMILY
FAMILY = F0
# MCU code

View File

@ -26,9 +26,11 @@
static I2C_SPEED curI2Cspeed = LOW_SPEED;
void gpio_setup(void){
// PA9, PA10 - LEDs T0/T1
RCC->AHBENR |= RCC_AHBENR_GPIOCEN | RCC_AHBENR_GPIOBEN | RCC_AHBENR_GPIOAEN;
// Set LEDS (PA14/15)
GPIOA->MODER = GPIO_MODER_MODER14_O | GPIO_MODER_MODER15_O;
GPIOA->MODER = GPIO_MODER_MODER9_O | GPIO_MODER_MODER10_O |
GPIO_MODER_MODER14_O | GPIO_MODER_MODER15_O;
// PC13 - digital output - poweron
GPIOC->OTYPER = 1 << 13; // opendrain
GPIOC->MODER = GPIO_MODER_MODER13_O;

View File

@ -36,6 +36,12 @@
#define LED1_port GPIOA
#define LED1_pin (1<<15)
// LEDs for T0/T1 (PA9/PA10)
#define LEDT0_port GPIOA
#define LEDT0_pin (1<<9)
#define LEDT1_port GPIOA
#define LEDT1_pin (1<<10)
#define CONCAT(a,b) a ## b
#define STR_HELPER(s) #s
#define STR(s) STR_HELPER(s)
@ -46,6 +52,9 @@
#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)
#define LEDT_on(x) pin_set(LEDT ## x ## _port, LEDT ## x ## _pin)
#define LEDT_off(x) pin_clear(LEDT ## x ## _port, LEDT ## x ## _pin)
#define LEDT_blink(x) pin_toggle(LEDT ## x ## _port, LEDT ## x ## _pin)
// PA1 - USB On (in)
#define USBisOn() (GPIOA->IDR & 1<<1)

View File

@ -23,7 +23,6 @@
#include "i2c.h"
#include "proto.h"
#include "ssd1306.h"
#include "usart.h"
#include "usb.h"
static uint16_t coefficients[2][5]; // Coefficients for given sensors
@ -60,15 +59,8 @@ uint8_t sprintu(char *buf, uint8_t buflen, uint32_t val){
}
return l;
}
/*
// print 32bit unsigned int
void printu(uint32_t val){
char buf[11];
uint8_t l = sprintu(buf, 11, val);
while(LINE_BUSY == usart_send_blocking(buf, l));
}*/
void getcoeffs(uint8_t addr){ // show norm coefficiens
void getcoeffs(uint8_t addr){ // get norm coefficiens
int i;
const uint8_t regs[5] = {0xAA, 0xA8, 0xA6, 0xA4, 0xA2}; // commands for coefficients
uint32_t K;
@ -98,7 +90,6 @@ uint8_t calc_t(uint32_t t, int i){
getcoeffs(Taddr[i]);
}
if(coefficients[i][0] == 0){
USEND("no sensor\n");
return 0;
}
if (t < 6500000 || t > 13000000) return 0; // wrong value - too small or too large
@ -148,7 +139,8 @@ int main(void){
SysTick_Config(6000, 1);
gpio_setup();
LED_on(LED0);
usart_setup();
LEDT_off(0);
LEDT_off(1);
USB_setup();
i2c_setup(LOW_SPEED);
spi_setup();
@ -179,7 +171,7 @@ int main(void){
LED_off(LED1);
if(++uptime > UPTIME - 3){
ssd1306_Fill(0);
ssd1306_WriteString("Power off!", Font_16x26, 1);
ssd1306_WriteString("Power\n off!", Font_16x26, 1);
ssd1306_UpdateScreen();
}
if(uptime > UPTIME) POWEROFF();
@ -194,18 +186,15 @@ int main(void){
uint32_t t;
if(read_i2c(Taddr[i], &t, 3) && t){
if(!calc_t(t, i)){
//USEND("!calc ");
write_i2c(Taddr[i], TSYS01_RESET);
}else{
err = 0;
tgot[i] = Tms;
if(i) LEDT_on(1);
else LEDT_on(0);
}
started[i] = 0;
}else{
//USEND("can't read ");
}
}else{
//USEND("can't write ");
}
}else{
err = 0;
@ -216,7 +205,6 @@ int main(void){
started[i] = Tms ? Tms : 1;
err = 0;
}else{
//USEND("can't start conv\n");
started[i] = 0;
}
}
@ -231,6 +219,8 @@ int main(void){
Tbuf[i][1] = 0;
coefficients[i][0] = 0;
refreshdisplay = 1;
if(i) LEDT_off(1);
else LEDT_off(0);
}
}
}else errcnt[i] = 0;
@ -240,13 +230,7 @@ int main(void){
char inbuf[256];
if((r = USB_receive(inbuf, 255))){
inbuf[r] = 0;
cmd_parser(inbuf, 1);
}
if(usartrx()){ // usart1 received data, store in in buffer
char *txt = NULL;
r = usart_getline(&txt);
txt[r] = 0;
cmd_parser(txt, 0);
cmd_parser(inbuf);
}
}
return 0;

View File

@ -25,14 +25,12 @@
#include "i2c.h"
#include "proto.h"
#include "ssd1306.h"
#include "usart.h"
//#include "usart.h"
#include "usb.h"
#include <string.h> // strlen, strcpy(
extern volatile uint8_t canerror;
static char buff[UARTBUFSZ+1], *bptr = buff;
static uint8_t blen = 0, USBcmd = 0;
static uint8_t blen = 0;
// LEDs are OFF by default
uint8_t noLED = 1;
@ -40,8 +38,7 @@ void sendbuf(){
IWDG->KR = IWDG_REFRESH;
if(blen == 0) return;
*bptr = 0;
if(USBcmd) USB_send(buff);
else while(LINE_BUSY == usart_send(buff, blen)){IWDG->KR = IWDG_REFRESH;}
USB_send(buff);
bptr = buff;
blen = 0;
}
@ -51,8 +48,7 @@ void addtobuf(const char *txt){
int l = strlen(txt);
if(l > UARTBUFSZ){
sendbuf();
if(USBcmd) USB_send(txt);
else while(LINE_BUSY == usart_send_blocking(txt, l)){IWDG->KR = IWDG_REFRESH;}
USB_send(txt);
}else{
if(blen+l > UARTBUFSZ){
sendbuf();
@ -75,13 +71,17 @@ void bufputchar(char ch){
/**
* @brief cmd_parser - command parsing
* @param txt - buffer with commands & data
* @param isUSB - == 1 if data got from USB
*/
void cmd_parser(char *txt, uint8_t isUSB){
USBcmd = isUSB;
void cmd_parser(char *txt){
char _1st = txt[0];
sendbuf();
switch(_1st){
case '0':
LEDT_blink(0);
break;
case '1':
LEDT_blink(1);
break;
case 'b':
ssd1306_Fill(0);
ssd1306_UpdateScreen();
@ -119,7 +119,8 @@ void cmd_parser(char *txt, uint8_t isUSB){
break;
default: // help
SEND(
"ALL little letters - without CAN messaging\n"
"Commands:\n"
"0/1 - invert LEDT0/1\n"
"b - clear screen\n"
"c - fill screen white\n"
"D - discovery sensors\n"

View File

@ -26,6 +26,8 @@
#include "stm32f0.h"
#define UARTBUFSZ (64)
// macro for static strings
#define SEND(str) do{addtobuf(str);}while(0)
@ -38,7 +40,7 @@
#define newline() do{bufputchar('\n');}while(0)
extern uint8_t noLED;
void cmd_parser(char *buf, uint8_t isUSB);
void cmd_parser(char *buf);
void addtobuf(const char *txt);
void bufputchar(char ch);
void printu(uint32_t val);

View File

@ -22,7 +22,7 @@
*/
#include "spi.h"
#include "usart.h"
//#include "usart.h"
extern volatile uint32_t Tms;
static uint32_t tstart;

View File

@ -22,7 +22,8 @@
*/
#include "spi.h"
#include "ssd1306.h"
#include "usart.h"
//#include "usart.h"
#include "proto.h"
#include <string.h> // memset
#define RST_PAUSE (10)

View File

@ -1,213 +0,0 @@
/*
* 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)){IWDG->KR = IWDG_REFRESH;};
}
return ALL_OK;
}
void usart_send_blck(const char *str){
while(!txrdy){IWDG->KR = IWDG_REFRESH;}
bufovr = 0;
while(*str){
USARTX -> TDR = *str++;
while(!(USARTX->ISR & USART_ISR_TXE)){IWDG->KR = IWDG_REFRESH;};
}
}
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
}
}
}
#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

View File

@ -1,52 +0,0 @@
/*
* 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
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 usart_send_blck(const char *str);
#define USEND(str) do{}while(LINE_BUSY == usart_send(str, sizeof(str)-1))
#define UNEWLINE() do{}while(LINE_BUSY == usart_send("\n", 1))
#endif // __USART_H__

View File

@ -23,7 +23,6 @@
#include "usb.h"
#include "usb_lib.h"
#include "usart.h"
#include <string.h> // memcpy, memmove
// incoming buffer size
@ -112,6 +111,7 @@ void usb_proc(){
}
void USB_send(const char *buf){
if(!USB_configured()) return;
uint16_t l = 0, ctr = 0;
const char *p = buf;
while(*p++) ++l;

View File

@ -24,7 +24,6 @@
#include <stdint.h>
#include "usb_lib.h"
#include <string.h> // memcpy
#include "usart.h"
ep_t endpoints[ENDPOINTS_NUM];