mirror of
https://github.com/eddyem/stm32samples.git
synced 2026-02-28 03:44:30 +03:00
start fixed F1 testboard for new board type
This commit is contained in:
1
F1:F103/F1_testbrd/F1_testbrd.cflags
Normal file
1
F1:F103/F1_testbrd/F1_testbrd.cflags
Normal file
@@ -0,0 +1 @@
|
||||
-std=c17
|
||||
6
F1:F103/F1_testbrd/F1_testbrd.config
Normal file
6
F1:F103/F1_testbrd/F1_testbrd.config
Normal file
@@ -0,0 +1,6 @@
|
||||
#define FAMILY F1
|
||||
#define MCU F103xB
|
||||
#define USARTNUM 1
|
||||
#define STM32F1 1
|
||||
#define STM32F103xB 1
|
||||
#define DENSITY MD
|
||||
1
F1:F103/F1_testbrd/F1_testbrd.creator
Normal file
1
F1:F103/F1_testbrd/F1_testbrd.creator
Normal file
@@ -0,0 +1 @@
|
||||
[General]
|
||||
1
F1:F103/F1_testbrd/F1_testbrd.cxxflags
Normal file
1
F1:F103/F1_testbrd/F1_testbrd.cxxflags
Normal file
@@ -0,0 +1 @@
|
||||
-std=c++17
|
||||
16
F1:F103/F1_testbrd/F1_testbrd.files
Normal file
16
F1:F103/F1_testbrd/F1_testbrd.files
Normal file
@@ -0,0 +1,16 @@
|
||||
Readme.md
|
||||
adc.c
|
||||
adc.h
|
||||
hardware.c
|
||||
hardware.h
|
||||
main.c
|
||||
pl2303.bin
|
||||
proto.c
|
||||
proto.h
|
||||
usart.c
|
||||
usart.h
|
||||
usb.c
|
||||
usb.h
|
||||
usb_defs.h
|
||||
usb_lib.c
|
||||
usb_lib.h
|
||||
3
F1:F103/F1_testbrd/F1_testbrd.includes
Normal file
3
F1:F103/F1_testbrd/F1_testbrd.includes
Normal file
@@ -0,0 +1,3 @@
|
||||
.
|
||||
../inc/cm
|
||||
../inc/Fx
|
||||
@@ -4,11 +4,11 @@ BOOTSPEED ?= 115200
|
||||
# MCU FAMILY
|
||||
FAMILY ?= F1
|
||||
# MCU code
|
||||
MCU ?= F103x8
|
||||
MCU ?= F103x6
|
||||
# density (stm32f10x.h, lines 70-84)
|
||||
DENSITY ?= MD
|
||||
DENSITY ?= LD
|
||||
# change this linking script depending on particular MCU model,
|
||||
LDSCRIPT ?= stm32f103x8.ld
|
||||
LDSCRIPT ?= stm32f103x6.ld
|
||||
# debug
|
||||
#DEFS = -DEBUG
|
||||
|
||||
|
||||
@@ -18,12 +18,6 @@
|
||||
|
||||
#include "adc.h"
|
||||
|
||||
/**
|
||||
* @brief ADC_array - array for ADC channels with median filtering:
|
||||
* 0 - Rvar
|
||||
* 1 - internal Tsens
|
||||
* 2 - Vref
|
||||
*/
|
||||
uint16_t ADC_array[NUMBER_OF_ADC_CHANNELS*9];
|
||||
|
||||
/**
|
||||
@@ -51,11 +45,19 @@ uint16_t getADCval(int nch){
|
||||
#undef PIX_SWAP
|
||||
}
|
||||
|
||||
// get voltage @input nch (1/100V)
|
||||
uint32_t getADCvoltage(int nch){
|
||||
uint32_t v = getADCval(nch);
|
||||
v *= getVdd();
|
||||
v /= 0xfff; // 12bit ADC
|
||||
return v;
|
||||
}
|
||||
|
||||
// return MCU temperature (degrees of celsius * 10)
|
||||
int32_t getMCUtemp(){
|
||||
// Temp = (V25 - Vsense)/Avg_Slope + 25
|
||||
// V_25 = 1.45V, Slope = 4.3e-3
|
||||
int32_t Vsense = getVdd() * getADCval(1);
|
||||
int32_t Vsense = getVdd() * getADCval(CHTSENS);
|
||||
int32_t temperature = 593920 - Vsense; // 593920 == 145*4096
|
||||
temperature /= 172; // == /(4096*10*4.3e-3), 10 - to convert from *100 to *10
|
||||
temperature += 250;
|
||||
@@ -65,6 +67,6 @@ int32_t getMCUtemp(){
|
||||
// return Vdd * 100 (V)
|
||||
uint32_t getVdd(){
|
||||
uint32_t vdd = 120 * 4096; // 1.2V
|
||||
vdd /= getADCval(2);
|
||||
vdd /= getADCval(CHVREF);
|
||||
return vdd;
|
||||
}
|
||||
|
||||
@@ -19,11 +19,25 @@
|
||||
#define ADC_H
|
||||
#include "stm32f1.h"
|
||||
|
||||
#define NUMBER_OF_ADC_CHANNELS (3)
|
||||
#define NUMBER_OF_ADC_CHANNELS (5)
|
||||
|
||||
// channels for Tsens and Vref
|
||||
#define CHTSENS (3)
|
||||
#define CHVREF (4)
|
||||
|
||||
/**
|
||||
* @brief ADC_array - array for ADC channels with median filtering:
|
||||
* 0 - Rvar
|
||||
* 1 - Rvar/2
|
||||
* 2 - AIN5
|
||||
* 3 - internal Tsens
|
||||
* 4 - Vref
|
||||
*/
|
||||
extern uint16_t ADC_array[];
|
||||
|
||||
int32_t getMCUtemp();
|
||||
uint32_t getVdd();
|
||||
uint16_t getADCval(int nch);
|
||||
uint32_t getADCvoltage(int nch);
|
||||
|
||||
#endif // ADC_H
|
||||
|
||||
@@ -28,30 +28,80 @@
|
||||
static inline void gpio_setup(){
|
||||
// Enable clocks to the GPIO subsystems (PB for ADC), turn on AFIO clocking to disable SWD/JTAG
|
||||
RCC->APB2ENR |= RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPBEN | RCC_APB2ENR_AFIOEN;
|
||||
// turn off SWJ/JTAG
|
||||
AFIO->MAPR = AFIO_MAPR_SWJ_CFG_DISABLE;
|
||||
AFIO->MAPR = AFIO_MAPR_SWJ_CFG_JTAGDISABLE; // for PA15
|
||||
// turn off USB pullup
|
||||
GPIOA->ODR = (1<<13)|(1<<14)|(1<<15); // turn off usb pullup & turn on pullups for buttons
|
||||
// Set leds (PA0/PA4) as opendrain output
|
||||
GPIOA->CRL = CRL(0, CNF_ODOUTPUT|MODE_SLOW) | CRL(4, CNF_ODOUTPUT|MODE_SLOW);
|
||||
// Set buttons (PA14/15) as inputs with weak pullups, USB pullup (PA13) - opendrain output
|
||||
GPIOA->CRH = CRH(13, CNF_ODOUTPUT|MODE_SLOW) | CRH(14, CNF_PUDINPUT|MODE_INPUT)
|
||||
| CRH(15, CNF_PUDINPUT|MODE_INPUT);
|
||||
GPIOA->ODR = (1<<15); // turn off usb pullup & turn on pullups for buttons
|
||||
// Set LEDS (PA6-8, PB0/1) as Out & AF (PWM)
|
||||
GPIOA->CRL = CRL(6, CNF_AFOD|MODE_NORMAL) | CRL(7, CNF_AFOD|MODE_NORMAL);
|
||||
// USB pullup (PA15) - pushpull output
|
||||
GPIOA->CRH = CRH(8, CNF_AFOD|MODE_NORMAL) | CRH(15, CNF_PPOUTPUT|MODE_SLOW);
|
||||
GPIOB->CRL = CRL(0, CNF_AFOD|MODE_NORMAL) | CRL(1, CNF_AFOD|MODE_NORMAL);
|
||||
}
|
||||
|
||||
static inline void iwdg_setup(){
|
||||
uint32_t tmout = 16000000;
|
||||
/* 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){if(--tmout == 0) break;} /* (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) */
|
||||
tmout = 16000000;
|
||||
while(IWDG->SR){if(--tmout == 0) break;} /* (5) */
|
||||
IWDG->KR = IWDG_REFRESH; /* (6) */
|
||||
}
|
||||
|
||||
// TIM3 - PWM @ all 4 channels. TIM1 - PWM @ ch1
|
||||
static inline void tim1_setup(){
|
||||
RCC->APB2ENR |= RCC_APB2ENR_TIM1EN; // enable TIM1 clocking
|
||||
RCC->APB1ENR |= RCC_APB1ENR_TIM3EN;
|
||||
TIM1->PSC = 8; // 72/9 = 8MHz
|
||||
TIM3->PSC = 8;
|
||||
// ARR for 8-bit PWM
|
||||
TIM1->ARR = 255; // 100 ticks for 80kHz
|
||||
TIM3->ARR = 255;
|
||||
TIM1->CCR1 = 127; // 50%
|
||||
TIM3->CCR1 = 63; TIM3->CCR2 = 127; TIM3->CCR3 = 191; TIM3->CCR4 = 250;
|
||||
// PWM mode 2
|
||||
TIM1->CCMR1 = TIM_CCMR1_OC1M;
|
||||
TIM3->CCMR1 = TIM_CCMR1_OC1M | TIM_CCMR1_OC2M;
|
||||
TIM3->CCMR2 = TIM_CCMR2_OC3M | TIM_CCMR2_OC4M;
|
||||
// main output
|
||||
TIM1->BDTR = TIM_BDTR_MOE;
|
||||
TIM3->BDTR |= TIM_BDTR_MOE;
|
||||
// main PWM output
|
||||
TIM1->CCER = TIM_CCER_CC1E;
|
||||
TIM3->CCER = TIM_CCER_CC1E | TIM_CCER_CC2E | TIM_CCER_CC3E | TIM_CCER_CC4E;
|
||||
// turn it on
|
||||
TIM1->CR1 = TIM_CR1_CEN;// | TIM_CR1_ARPE;
|
||||
TIM3->CR1 |= TIM_CR1_CEN;
|
||||
TIM1->EGR |= TIM_EGR_UG; // generate update event to refresh all
|
||||
TIM3->EGR |= TIM_EGR_UG;
|
||||
}
|
||||
|
||||
static inline void adc_setup(){
|
||||
GPIOB->CRL |= CRL(0, CNF_ANALOG|MODE_INPUT);
|
||||
GPIOA->CRL |= CRL(0, CNF_ANALOG|MODE_INPUT) | CRL(1, CNF_ANALOG|MODE_INPUT) | CRL(5, CNF_ANALOG|MODE_INPUT);
|
||||
uint32_t ctr = 0;
|
||||
// Enable clocking
|
||||
RCC->APB2ENR |= RCC_APB2ENR_ADC1EN;
|
||||
RCC->CFGR &= ~(RCC_CFGR_ADCPRE);
|
||||
RCC->CFGR |= RCC_CFGR_ADCPRE_DIV8; // ADC clock = RCC / 8
|
||||
// sampling time - 239.5 cycles for channels 8, 16 and 17
|
||||
ADC1->SMPR2 = ADC_SMPR2_SMP8;
|
||||
// sampling time - 239.5 cycles for channels 0, 1, 5, 16 and 17
|
||||
ADC1->SMPR2 = ADC_SMPR2_SMP0 | ADC_SMPR2_SMP1 | ADC_SMPR2_SMP5;
|
||||
ADC1->SMPR1 = ADC_SMPR1_SMP16 | ADC_SMPR1_SMP17;
|
||||
// we have three conversions in group -> ADC1->SQR1[L] = 2, order: 8->16->17
|
||||
ADC1->SQR3 = 8 | (16<<5) | (17<<10);
|
||||
ADC1->SQR1 = ADC_SQR1_L_1;
|
||||
// sequence order: 0 -> 1 -> 5 -> 16 -> 17
|
||||
ADC1->SQR3 = 0 | (1<<5) | (5<<10) | (16<<15) | (17<<20);
|
||||
ADC1->SQR1 = (NUMBER_OF_ADC_CHANNELS - 1) << 20; // amount of conversions
|
||||
ADC1->CR1 |= ADC_CR1_SCAN; // scan mode
|
||||
// DMA configuration
|
||||
RCC->AHBENR |= RCC_AHBENR_DMA1EN;
|
||||
@@ -76,4 +126,6 @@ static inline void adc_setup(){
|
||||
void hw_setup(){
|
||||
gpio_setup();
|
||||
adc_setup();
|
||||
tim1_setup();
|
||||
iwdg_setup();
|
||||
}
|
||||
|
||||
@@ -24,30 +24,32 @@
|
||||
#ifndef __HARDWARE_H__
|
||||
#define __HARDWARE_H__
|
||||
|
||||
#include "stm32f1.h"
|
||||
#include <stm32f1.h>
|
||||
|
||||
// LEDS: 0 - PA0, 1 - PA4
|
||||
// LED0 - blinking each second
|
||||
#define LED0_port GPIOA
|
||||
#define LED0_pin (1<<0)
|
||||
// LED1 - PWM
|
||||
#define LED1_port GPIOA
|
||||
#define LED1_pin (1<<4)
|
||||
#if 0
|
||||
#ifndef CONCAT
|
||||
#define CONCAT(a,b) a ## b
|
||||
#endif
|
||||
#ifndef STR_HELPER
|
||||
#define STR_HELPER(s) #s
|
||||
#endif
|
||||
#ifndef STR
|
||||
#define STR(s) STR_HELPER(s)
|
||||
#endif
|
||||
|
||||
// Buttons' state: PA14 (1)/PA15 (0)
|
||||
#define GET_BTN0() ((GPIOA->IDR & (1<<15)) ? 0 : 1)
|
||||
#define GET_BTN1() ((GPIOA->IDR & (1<<14)) ? 0 : 1)
|
||||
// PWM LEDS
|
||||
#define SET_LED_PWM3(ch, N) do{TIM3->CCR ## ch = (uint32_t)N;}while(0)
|
||||
#define GET_LED_PWM3(ch) (uint8_t)(TIM3->CCR ## ch)
|
||||
#define SET_LED_PWM1(N) do{TIM1->CCR1 = (uint32_t)N;}while(0)
|
||||
#define GET_LED_PWM1() (uint8_t)(TIM1->CCR1)
|
||||
#endif
|
||||
|
||||
// USB pullup (not used in STM32F0x2!) - PA13
|
||||
#define USBPU_port GPIOA
|
||||
#define USBPU_pin (1<<13)
|
||||
#define USBPU_pin (1<<15)
|
||||
#define USBPU_ON() pin_clear(USBPU_port, USBPU_pin)
|
||||
#define USBPU_OFF() pin_set(USBPU_port, USBPU_pin)
|
||||
|
||||
#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)
|
||||
|
||||
void hw_setup();
|
||||
|
||||
#endif // __HARDWARE_H__
|
||||
|
||||
@@ -21,10 +21,14 @@
|
||||
|
||||
#include "adc.h"
|
||||
#include "hardware.h"
|
||||
#include "proto.h"
|
||||
#include "usart.h"
|
||||
#include "usb.h"
|
||||
#include "usb_lib.h"
|
||||
|
||||
// ADC value threshold for meaning it is new
|
||||
#define ADCthreshold (20)
|
||||
|
||||
volatile uint32_t Tms = 0;
|
||||
|
||||
/* Called when systick fires */
|
||||
@@ -32,123 +36,11 @@ void sys_tick_handler(void){
|
||||
++Tms;
|
||||
}
|
||||
|
||||
void iwdg_setup(){
|
||||
uint32_t tmout = 16000000;
|
||||
/* 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){if(--tmout == 0) break;} /* (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) */
|
||||
tmout = 16000000;
|
||||
while(IWDG->SR){if(--tmout == 0) break;} /* (5) */
|
||||
IWDG->KR = IWDG_REFRESH; /* (6) */
|
||||
}
|
||||
volatile uint8_t ADCmon = 0; // ==1 to monitor ADC (change PWM of LEDS & show current value)
|
||||
uint16_t oldADCval = 0;
|
||||
|
||||
#define USND(str) do{USB_send((uint8_t*)str, sizeof(str)-1);}while(0)
|
||||
char *parse_cmd(char *buf){
|
||||
static char btns[] = "BTN0=0, BTN1=0\n";
|
||||
if(buf[1] != '\n') return buf;
|
||||
switch(*buf){
|
||||
case '0':
|
||||
pin_set(GPIOA, 1<<4);
|
||||
break;
|
||||
case '1':
|
||||
pin_clear(GPIOA, 1<<4);
|
||||
break;
|
||||
case 'b':
|
||||
btns[5] = GET_BTN0() + '0';
|
||||
btns[13] = GET_BTN1() + '0';
|
||||
return btns;
|
||||
break;
|
||||
case 'p':
|
||||
pin_toggle(USBPU_port, USBPU_pin);
|
||||
SEND("USB pullup is ");
|
||||
if(pin_read(USBPU_port, USBPU_pin)) SEND("off");
|
||||
else SEND("on");
|
||||
newline();
|
||||
break;
|
||||
case 'A':
|
||||
return u2str(getADCval(0));
|
||||
break;
|
||||
case 'L':
|
||||
USND("Very long test string for USB (it's length is more than 64 bytes).\n"
|
||||
"This is another part of the string! Can you see all of this?\n");
|
||||
return "Long test sent\n";
|
||||
break;
|
||||
case 'R':
|
||||
USND("Soft reset\n");
|
||||
SEND("Soft reset\n");
|
||||
NVIC_SystemReset();
|
||||
break;
|
||||
case 'S':
|
||||
USND("Test string for USB\n");
|
||||
return "Short test sent\n";
|
||||
break;
|
||||
case 'T':
|
||||
return u2str(getMCUtemp());
|
||||
break;
|
||||
case 'V':
|
||||
return u2str(getVdd());
|
||||
break;
|
||||
case 'W':
|
||||
USND("Wait for reboot\n");
|
||||
SEND("Wait for reboot\n");
|
||||
while(1){nop();};
|
||||
break;
|
||||
default: // help
|
||||
return
|
||||
"0/1 - turn on/off LED1\n"
|
||||
"'b' - get buttons's state\n"
|
||||
"'p' - toggle USB pullup\n"
|
||||
"'A' - get ADC8 value\n"
|
||||
"'L' - send long string over USB\n"
|
||||
"'R' - software reset\n"
|
||||
"'S' - send short string over USB\n"
|
||||
"'T' - MCU temperature\n"
|
||||
"'V' - Vdd\n"
|
||||
"'W' - test watchdog\n"
|
||||
;
|
||||
break;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// usb getline
|
||||
char *get_USB(){
|
||||
static char tmpbuf[512], *curptr = tmpbuf;
|
||||
static int rest = 511;
|
||||
int x = USB_receive((uint8_t*)curptr);
|
||||
curptr[x] = 0;
|
||||
if(!x) return NULL;
|
||||
if(curptr[x-1] == '\n'){
|
||||
curptr = tmpbuf;
|
||||
rest = 511;
|
||||
return tmpbuf;
|
||||
}
|
||||
curptr += x; rest -= x;
|
||||
if(rest <= 0){ // buffer overflow
|
||||
SEND("USB buffer overflow!\n");
|
||||
curptr = tmpbuf;
|
||||
rest = 511;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
//int8_t dump = 0;
|
||||
int main(void){
|
||||
uint32_t lastT = 0, lastB = 0, LEDperiod = 499;
|
||||
uint32_t lastT = 0;
|
||||
sysreset();
|
||||
StartHSE();
|
||||
hw_setup();
|
||||
@@ -167,21 +59,45 @@ int main(void){
|
||||
|
||||
USBPU_OFF();
|
||||
USB_setup();
|
||||
iwdg_setup();
|
||||
USBPU_ON();
|
||||
|
||||
while (1){
|
||||
IWDG->KR = IWDG_REFRESH; // refresh watchdog
|
||||
if(lastT > Tms || Tms - lastT > LEDperiod){
|
||||
LED_blink(LED0);
|
||||
if(lastT > Tms || Tms - lastT > 499){
|
||||
if(ADCmon){
|
||||
uint16_t v = getADCval(0);
|
||||
int32_t d = v - oldADCval;
|
||||
if(d < -ADCthreshold || d > ADCthreshold){
|
||||
oldADCval = v;
|
||||
printADCvals();
|
||||
v >>= 2; // 10 bits
|
||||
TIM3->CCR1 = TIM3->CCR2 = TIM3->CCR3 = 0xff; TIM3->CCR4 = 0;
|
||||
if(v >= 0x300) TIM3->CCR4 = v - 0x300;
|
||||
else if(v >= 0x200) TIM3->CCR3 = v - 0x200;
|
||||
else if(v >= 0x100){ TIM3->CCR2 = v - 0x100; TIM3->CCR3 = 0; }
|
||||
else{ TIM3->CCR1 = v; TIM3->CCR2 = TIM3->CCR3 = 0; }
|
||||
}
|
||||
}
|
||||
lastT = Tms;
|
||||
transmit_tbuf(); // non-blocking transmission of data from UART buffer every 0.5s
|
||||
}
|
||||
/*
|
||||
if(I2C_scan_mode){
|
||||
uint8_t addr;
|
||||
int ok = i2c_scan_next_addr(&addr);
|
||||
if(addr == I2C_ADDREND) USND("Scan ends\n");
|
||||
else if(ok){
|
||||
USB_sendstr(uhex2str(addr));
|
||||
USND(" ("); USB_sendstr(u2str(addr));
|
||||
USND(") - found device\n");
|
||||
}
|
||||
}
|
||||
*/
|
||||
usb_proc();
|
||||
int r = 0;
|
||||
char *txt, *ans;
|
||||
if((txt = get_USB())){
|
||||
ans = parse_cmd(txt);
|
||||
ans = (char*)parse_cmd(txt);
|
||||
SEND("Received data over USB:\n");
|
||||
SEND(txt);
|
||||
newline();
|
||||
@@ -189,30 +105,20 @@ int main(void){
|
||||
uint16_t l = 0; char *p = ans;
|
||||
while(*p++) l++;
|
||||
USB_send((uint8_t*)ans, l);
|
||||
if(ans[l-1] != '\n') USND("\n");
|
||||
}
|
||||
}
|
||||
if(usartrx()){ // usart1 received data, store in in buffer
|
||||
r = usart_getline(&txt);
|
||||
if(r){
|
||||
txt[r] = 0;
|
||||
ans = parse_cmd(txt);
|
||||
ans = (char*)parse_cmd(txt);
|
||||
if(ans){
|
||||
usart_send(ans);
|
||||
transmit_tbuf();
|
||||
}
|
||||
}
|
||||
}
|
||||
// check buttons - each 50ms (increase / decrease LED blinking period by 10)
|
||||
if(Tms - lastB > 49){
|
||||
lastB = Tms;
|
||||
uint8_t btn0 = GET_BTN0(), btn1 = GET_BTN1();
|
||||
// both: set to default
|
||||
if(btn0 && btn1){
|
||||
LEDperiod = 499;
|
||||
}else if(btn0){
|
||||
if(LEDperiod < 1989) LEDperiod += 10;
|
||||
}else if(btn1){
|
||||
if(LEDperiod > 29) LEDperiod -= 10;
|
||||
USND("Got string over USART:\n");
|
||||
USB_sendstr(txt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Binary file not shown.
558
F1:F103/F1_testbrd/proto.c
Normal file
558
F1:F103/F1_testbrd/proto.c
Normal file
@@ -0,0 +1,558 @@
|
||||
/*
|
||||
* This file is part of the F0testbrd project.
|
||||
* Copyright 2021 Edward V. Emelianov <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 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "adc.h"
|
||||
//#include "i2c.h"
|
||||
#include "hardware.h"
|
||||
#include "proto.h"
|
||||
//#include "spi.h"
|
||||
#include "usart.h"
|
||||
#include "usb.h"
|
||||
#include "usb_lib.h"
|
||||
|
||||
#define LOCBUFFSZ (32)
|
||||
// local buffer for I2C and SPI data to send
|
||||
static uint8_t locBuffer[LOCBUFFSZ];
|
||||
|
||||
void USB_sendstr(const char *str){
|
||||
uint16_t l = 0;
|
||||
const char *b = str;
|
||||
while(*b++) ++l;
|
||||
USB_send((const uint8_t*)str, l);
|
||||
}
|
||||
|
||||
static inline char *chPWM(volatile uint32_t *reg, char *buf){
|
||||
char *lbuf = buf;
|
||||
lbuf = omit_spaces(lbuf);
|
||||
char cmd = *lbuf;
|
||||
lbuf = omit_spaces(lbuf + 1);
|
||||
uint32_t N;
|
||||
if(getnum(lbuf, &N) == lbuf) N = 1;
|
||||
uint32_t oldval = *reg;
|
||||
if(cmd == '-'){ // decrement
|
||||
if(oldval < N) return "Already at minimum";
|
||||
else *reg -= N;
|
||||
}else if(cmd == '+'){ // increment
|
||||
if(oldval + N > 255) return "Already at maximum";
|
||||
else *reg += N;
|
||||
}else{
|
||||
USND("Wrong command: ");
|
||||
return buf;
|
||||
}
|
||||
return "OK";
|
||||
}
|
||||
|
||||
static inline char *TIM3pwm(char *buf){
|
||||
uint8_t channel = *buf - '1';
|
||||
if(channel > 3) return "Wrong channel number";
|
||||
volatile uint32_t *reg = (uint32_t*)&(TIM3->CCR1);
|
||||
return chPWM(®[channel], buf+1);
|
||||
}
|
||||
|
||||
static inline char *getPWMvals(){
|
||||
USND("TIM1CH1: "); USB_sendstr(u2str(TIM1->CCR1));
|
||||
USND("\nTIM3CH1: "); USB_sendstr(u2str(TIM3->CCR1));
|
||||
USND("\nTIM3CH2: "); USB_sendstr(u2str(TIM3->CCR2));
|
||||
USND("\nTIM3CH3: "); USB_sendstr(u2str(TIM3->CCR3));
|
||||
USND("\nTIM3CH4: "); USB_sendstr(u2str(TIM3->CCR4));
|
||||
USND("\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static inline char *USARTsend(char *buf){
|
||||
// uint32_t N;
|
||||
// if(buf == getnum(buf, &N)) return "Point number of USART";
|
||||
// if(N < 1 || N > USARTNUM) return "Wrong USART number";
|
||||
// buf = omit_spaces(buf + 1);
|
||||
usart_send(buf);
|
||||
transmit_tbuf();
|
||||
return "OK";
|
||||
}
|
||||
|
||||
// read N numbers from buf, @return 0 if wrong or none
|
||||
static uint16_t readNnumbers(char *buf){
|
||||
uint32_t D;
|
||||
char *nxt;
|
||||
uint16_t N = 0;
|
||||
while((nxt = getnum(buf, &D)) && nxt != buf && N < LOCBUFFSZ){
|
||||
buf = nxt;
|
||||
locBuffer[N++] = (uint8_t) D&0xff;
|
||||
USND("add byte: "); USB_sendstr(uhex2str(D&0xff)); USND("\n");
|
||||
}
|
||||
USND("Send "); USB_sendstr(u2str(N)); USND(" bytes\n");
|
||||
return N;
|
||||
}
|
||||
|
||||
// dump memory buffer
|
||||
static void hexdump(uint8_t *arr, uint16_t len){
|
||||
char buf[52], *bptr = buf;
|
||||
for(uint16_t l = 0; l < len; ++l, ++arr){
|
||||
for(int16_t j = 1; j > -1; --j){
|
||||
register uint8_t half = (*arr >> (4*j)) & 0x0f;
|
||||
if(half < 10) *bptr++ = half + '0';
|
||||
else *bptr++ = half - 10 + 'a';
|
||||
}
|
||||
if(l % 16 == 15){
|
||||
*bptr++ = '\n';
|
||||
*bptr = 0;
|
||||
USB_sendstr(buf);
|
||||
bptr = buf;
|
||||
}else *bptr++ = ' ';
|
||||
}
|
||||
if(bptr != buf){
|
||||
*bptr++ = '\n';
|
||||
*bptr = 0;
|
||||
USB_sendstr(buf);
|
||||
}
|
||||
}
|
||||
|
||||
static uint8_t i2cinited = 0;
|
||||
static inline char *setupI2C(char *buf){
|
||||
buf = omit_spaces(buf);
|
||||
if(*buf < '0' || *buf > '2') return "Wrong speed";
|
||||
//i2c_setup(*buf - '0');
|
||||
i2cinited = 1;
|
||||
return "OK";
|
||||
}
|
||||
|
||||
static uint8_t I2Caddress = 0;
|
||||
static inline char *saI2C(char *buf){
|
||||
uint32_t addr;
|
||||
if(!getnum(buf, &addr) || addr > 0x7f) return "Wrong address";
|
||||
I2Caddress = (uint8_t) addr << 1;
|
||||
USND("I2Caddr="); USB_sendstr(uhex2str(addr)); USND("\n");
|
||||
return "OK";
|
||||
}
|
||||
static inline void rdI2C(char *buf){
|
||||
uint32_t N;
|
||||
char *nxt = getnum(buf, &N);
|
||||
if(!nxt || buf == nxt || N > 0xff){
|
||||
USND("Bad register number\n");
|
||||
return;
|
||||
}
|
||||
buf = nxt;
|
||||
uint8_t reg = N;
|
||||
nxt = getnum(buf, &N);
|
||||
if(!nxt || buf == nxt || N > LOCBUFFSZ){
|
||||
USND("Bad length\n");
|
||||
return;
|
||||
}/*
|
||||
if(!read_i2c_reg(I2Caddress, reg, locBuffer, N)){
|
||||
USND("Error reading I2C\n");
|
||||
return;
|
||||
}*/
|
||||
if(N == 0){ USND("OK"); return; }
|
||||
USND("Register "); USB_sendstr(uhex2str(reg)); USND(":\n");
|
||||
hexdump(locBuffer, N);
|
||||
}
|
||||
static inline char *wrI2C(char *buf){
|
||||
uint16_t N = readNnumbers(buf);
|
||||
//if(!write_i2c(I2Caddress, locBuffer, N)) return "Error writing I2C";
|
||||
if(N < 1) return "bad";
|
||||
return "OK";
|
||||
}
|
||||
|
||||
static inline char *DAC_chval(char *buf){
|
||||
uint32_t D;
|
||||
char *nxt = getnum(buf, &D);
|
||||
if(!nxt || nxt == buf || D > 4095) return "Wrong DAC amplitude\n";
|
||||
DAC->DHR12R1 = D;
|
||||
return "OK";
|
||||
}
|
||||
|
||||
// write locBuffer to SPI
|
||||
static inline void wrSPI(int SPIidx, char *buf){
|
||||
if(SPIidx < 0 || SPIidx > 2) return;
|
||||
uint16_t N = readNnumbers(buf);
|
||||
if(N < 1){
|
||||
*(uint8_t *)&(SPI1->DR) = 0xea;
|
||||
USND("Enter at least 1 number (max: ");
|
||||
USB_sendstr(u2str(LOCBUFFSZ)); USND(")\n");
|
||||
return;
|
||||
}
|
||||
//if(SPI_transmit(SPIidx, locBuffer, N)) USND("Error: busy?\n");
|
||||
else USND("done");
|
||||
}
|
||||
static inline void rdSPI(int SPIidx){
|
||||
if(SPIidx < 0 || SPIidx > 2) return;
|
||||
//if(SPI_isoverflow(SPIidx)) USND("SPI buffer overflow\n");
|
||||
uint8_t len = LOCBUFFSZ;
|
||||
/*
|
||||
if(SPI_getdata(SPIidx, locBuffer, &len)){
|
||||
USND("Error getting data: busy?\n");
|
||||
return;
|
||||
}*/
|
||||
if(len == 0){
|
||||
USND("Nothing to read\n");
|
||||
return;
|
||||
}
|
||||
if(len > LOCBUFFSZ) USND("Can't get full message: buffer too small\n");
|
||||
USND("SPI data:\n");
|
||||
hexdump(locBuffer, len);
|
||||
}
|
||||
|
||||
static inline char *procSPI(char *buf){
|
||||
int idx = 0;
|
||||
if(*buf == 'p') idx = 1;
|
||||
buf = omit_spaces(buf + 1);
|
||||
if(*buf == 'w') wrSPI(idx, buf + 1);
|
||||
else if(*buf == 'r') rdSPI(idx);
|
||||
else return "Enter `w` and data to write, `r` - to read";
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const char *helpstring =
|
||||
"+/-[num] - increase/decrease TIM1ch1 PWM by 1 or `num`\n"
|
||||
"0 - USB Pullup change\n"
|
||||
"1..4'+'/'-'[num] - increase/decrease TIM3chN PWM by 1 or `num`\n"
|
||||
"A - get ADC values\n"
|
||||
"dx - change DAC lowcal to x\n"
|
||||
"g - get PWM values\n"
|
||||
"i0..3 - setup I2C with lowest..highest speed (5.8, 10 and 100kHz)\n"
|
||||
"Ia addr - set I2C address\n"
|
||||
"Iw bytes - send bytes (hex/dec/oct/bin) to I2C\n"
|
||||
"Ir reg n - read n bytes from I2C reg\n"
|
||||
"Is - scan I2C bus\n"
|
||||
"L - send long string over USB\n"
|
||||
"m - monitor ADC on/off\n"
|
||||
"Pw bytes - send bytes over SPI1\n"
|
||||
"pw bytes - send bytes over SPI2\n"
|
||||
"Pr - get data from SPI1\n"
|
||||
"pr - get data from SPI2\n"
|
||||
"R - software reset\n"
|
||||
"S - send short string over USB\n"
|
||||
"s - setup SPI (and turn off USARTs)\n"
|
||||
"Ux str - send string to USARTx (1..3)\n"
|
||||
"u - setup USARTs (and turn off SPI)\n"
|
||||
"T - MCU temperature\n"
|
||||
"V - Vdd\n"
|
||||
"W - test watchdog\n"
|
||||
;
|
||||
|
||||
void printADCvals(){
|
||||
USND("AIN0: "); USB_sendstr(u2str(getADCval(0)));
|
||||
USND(" ("); USB_sendstr(u2str(getADCvoltage(0)));
|
||||
USND("/100 V)\nAIN1: "); USB_sendstr(u2str(getADCval(1)));
|
||||
USND(" ("); USB_sendstr(u2str(getADCvoltage(1)));
|
||||
USND("/100 V)\nAIN5: "); USB_sendstr(u2str(getADCval(2)));
|
||||
USND(" ("); USB_sendstr(u2str(getADCvoltage(2)));
|
||||
USND("/100 V)\n");
|
||||
}
|
||||
|
||||
const char *parse_cmd(char *buf){
|
||||
// "long" commands
|
||||
switch(*buf){
|
||||
case '+':
|
||||
case '-':
|
||||
return chPWM((uint32_t*)&TIM1->CCR1, buf);
|
||||
break;
|
||||
case '1':
|
||||
case '2':
|
||||
case '3':
|
||||
case '4':
|
||||
return TIM3pwm(buf);
|
||||
break;
|
||||
case 'd':
|
||||
return DAC_chval(buf + 1);
|
||||
case 'i':
|
||||
return setupI2C(buf + 1);
|
||||
break;
|
||||
case 'I':
|
||||
return "TODO";
|
||||
/*
|
||||
if(!i2cinited) return "Init I2C first";
|
||||
buf = omit_spaces(buf + 1);
|
||||
if(*buf == 'a') return saI2C(buf + 1);
|
||||
else if(*buf == 'r'){ rdI2C(buf + 1); return NULL; }
|
||||
else if(*buf == 'w') return wrI2C(buf + 1);
|
||||
else if(*buf == 's') i2c_init_scan_mode();
|
||||
else return "Command should be 'Ia', 'Iw', 'Ir' or 'Is'\n";
|
||||
*/
|
||||
break;
|
||||
case 'p':
|
||||
case 'P':
|
||||
//return procSPI(buf);
|
||||
return "TODO";
|
||||
break;
|
||||
case 'U':
|
||||
return USARTsend(buf + 1);
|
||||
break;
|
||||
}
|
||||
// "short" commands
|
||||
if(buf[1] != '\n') return buf;
|
||||
switch(*buf){
|
||||
case '0':
|
||||
pin_toggle(USBPU_port, USBPU_pin);
|
||||
if(USBPU_port->ODR & USBPU_pin) return "Pullup clear";
|
||||
else return "Pullup set";
|
||||
break;
|
||||
case 'g':
|
||||
return getPWMvals();
|
||||
break;
|
||||
case 'A':
|
||||
printADCvals();
|
||||
break;
|
||||
case 'L':
|
||||
USND("Very long test string for USB (it's length is more than 64 bytes).\n"
|
||||
"This is another part of the string! Can you see all of this?\n");
|
||||
return "Long test sent";
|
||||
break;
|
||||
case 'm':
|
||||
ADCmon = !ADCmon;
|
||||
USND("Monitoring is ");
|
||||
if(ADCmon) USND("on\n");
|
||||
else USND("off\n");
|
||||
break;
|
||||
case 'R':
|
||||
USND("Soft reset\n");
|
||||
//SEND("Soft reset\n");
|
||||
NVIC_SystemReset();
|
||||
break;
|
||||
case 'S':
|
||||
USND("Test string for USB\n");
|
||||
return "Short test sent";
|
||||
break;
|
||||
case 's':
|
||||
USND("SPI are ON, USART are OFF\n");
|
||||
//usart_stop();
|
||||
//spi_setup();
|
||||
break;
|
||||
case 'T':
|
||||
return u2str(getMCUtemp());
|
||||
break;
|
||||
case 'u':
|
||||
USND("USART are ON, SPI are OFF\n");
|
||||
//spi_stop();
|
||||
//usart_setup();
|
||||
break;
|
||||
case 'V':
|
||||
return u2str(getVdd());
|
||||
break;
|
||||
case 'W':
|
||||
USND("Wait for reboot\n");
|
||||
//SEND("Wait for reboot\n");
|
||||
while(1){nop();};
|
||||
break;
|
||||
default: // help
|
||||
return helpstring;
|
||||
break;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// usb getline
|
||||
char *get_USB(){
|
||||
static char tmpbuf[129], *curptr = tmpbuf;
|
||||
static int rest = 128;
|
||||
int x = USB_receive((uint8_t*)curptr);
|
||||
curptr[x] = 0;
|
||||
if(!x) return NULL;
|
||||
if(curptr[x-1] == '\n'){
|
||||
curptr = tmpbuf;
|
||||
rest = 128;
|
||||
return tmpbuf;
|
||||
}
|
||||
curptr += x; rest -= x;
|
||||
if(rest <= 0){ // buffer overflow
|
||||
curptr = tmpbuf;
|
||||
rest = 128;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
static char *_2str(uint32_t val, uint8_t minus){
|
||||
static char strbuf[12];
|
||||
char *bufptr = &strbuf[11];
|
||||
*bufptr = 0;
|
||||
if(!val){
|
||||
*(--bufptr) = '0';
|
||||
}else{
|
||||
while(val){
|
||||
*(--bufptr) = val % 10 + '0';
|
||||
val /= 10;
|
||||
}
|
||||
}
|
||||
if(minus) *(--bufptr) = '-';
|
||||
return bufptr;
|
||||
}
|
||||
|
||||
// return string with number `val`
|
||||
char *u2str(uint32_t val){
|
||||
return _2str(val, 0);
|
||||
}
|
||||
char *i2str(int32_t i){
|
||||
uint8_t minus = 0;
|
||||
uint32_t val;
|
||||
if(i < 0){
|
||||
minus = 1;
|
||||
val = -i;
|
||||
}else val = i;
|
||||
return _2str(val, minus);
|
||||
}
|
||||
// print 32bit unsigned int as hex
|
||||
char *uhex2str(uint32_t val){
|
||||
static char buf[12] = "0x";
|
||||
int npos = 2;
|
||||
uint8_t *ptr = (uint8_t*)&val + 3;
|
||||
int8_t i, j, z=1;
|
||||
for(i = 0; i < 4; ++i, --ptr){
|
||||
if(*ptr == 0){ // omit leading zeros
|
||||
if(i == 3) z = 0;
|
||||
if(z) continue;
|
||||
}
|
||||
else z = 0;
|
||||
for(j = 1; j > -1; --j){
|
||||
uint8_t half = (*ptr >> (4*j)) & 0x0f;
|
||||
if(half < 10) buf[npos++] = half + '0';
|
||||
else buf[npos++] = half - 10 + 'a';
|
||||
}
|
||||
}
|
||||
buf[npos] = 0;
|
||||
return buf;
|
||||
}
|
||||
|
||||
char *omit_spaces(const char *buf){
|
||||
while(*buf){
|
||||
if(*buf > ' ') break;
|
||||
++buf;
|
||||
}
|
||||
return (char*)buf;
|
||||
}
|
||||
|
||||
// In case of overflow return `buf` and N==0xffffffff
|
||||
// read decimal number & return pointer to next non-number symbol
|
||||
static char *getdec(const char *buf, uint32_t *N){
|
||||
char *start = (char*)buf;
|
||||
uint32_t num = 0;
|
||||
while(*buf){
|
||||
char c = *buf;
|
||||
if(c < '0' || c > '9'){
|
||||
break;
|
||||
}
|
||||
if(num > 429496729 || (num == 429496729 && c > '5')){ // overflow
|
||||
*N = 0xffffff;
|
||||
return start;
|
||||
}
|
||||
num *= 10;
|
||||
num += c - '0';
|
||||
++buf;
|
||||
}
|
||||
*N = num;
|
||||
return (char*)buf;
|
||||
}
|
||||
// read hexadecimal number (without 0x prefix!)
|
||||
static char *gethex(const char *buf, uint32_t *N){
|
||||
char *start = (char*)buf;
|
||||
uint32_t num = 0;
|
||||
while(*buf){
|
||||
char c = *buf;
|
||||
uint8_t M = 0;
|
||||
if(c >= '0' && c <= '9'){
|
||||
M = '0';
|
||||
}else if(c >= 'A' && c <= 'F'){
|
||||
M = 'A' - 10;
|
||||
}else if(c >= 'a' && c <= 'f'){
|
||||
M = 'a' - 10;
|
||||
}
|
||||
if(M){
|
||||
if(num & 0xf0000000){ // overflow
|
||||
*N = 0xffffff;
|
||||
return start;
|
||||
}
|
||||
num <<= 4;
|
||||
num += c - M;
|
||||
}else{
|
||||
break;
|
||||
}
|
||||
++buf;
|
||||
}
|
||||
*N = num;
|
||||
return (char*)buf;
|
||||
}
|
||||
// read octal number (without 0 prefix!)
|
||||
static char *getoct(const char *buf, uint32_t *N){
|
||||
char *start = (char*)buf;
|
||||
uint32_t num = 0;
|
||||
while(*buf){
|
||||
char c = *buf;
|
||||
if(c < '0' || c > '7'){
|
||||
break;
|
||||
}
|
||||
if(num & 0xe0000000){ // overflow
|
||||
*N = 0xffffff;
|
||||
return start;
|
||||
}
|
||||
num <<= 3;
|
||||
num += c - '0';
|
||||
++buf;
|
||||
}
|
||||
*N = num;
|
||||
return (char*)buf;
|
||||
}
|
||||
// read binary number (without b prefix!)
|
||||
static char *getbin(const char *buf, uint32_t *N){
|
||||
char *start = (char*)buf;
|
||||
uint32_t num = 0;
|
||||
while(*buf){
|
||||
char c = *buf;
|
||||
if(c < '0' || c > '1'){
|
||||
break;
|
||||
}
|
||||
if(num & 0x80000000){ // overflow
|
||||
*N = 0xffffff;
|
||||
return start;
|
||||
}
|
||||
num <<= 1;
|
||||
if(c == '1') num |= 1;
|
||||
++buf;
|
||||
}
|
||||
*N = num;
|
||||
return (char*)buf;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief getnum - read uint32_t from string (dec, hex or bin: 127, 0x7f, 0b1111111)
|
||||
* @param buf - buffer with number and so on
|
||||
* @param N - the number read
|
||||
* @return pointer to first non-number symbol in buf
|
||||
* (if it is == buf, there's no number or if *N==0xffffffff there was overflow)
|
||||
*/
|
||||
char *getnum(const char *txt, uint32_t *N){
|
||||
char *nxt = NULL;
|
||||
char *s = omit_spaces(txt);
|
||||
if(*s == '0'){ // hex, oct or 0
|
||||
if(s[1] == 'x' || s[1] == 'X'){ // hex
|
||||
nxt = gethex(s+2, N);
|
||||
if(nxt == s+2) nxt = (char*)txt;
|
||||
}else if(s[1] > '0'-1 && s[1] < '8'){ // oct
|
||||
nxt = getoct(s+1, N);
|
||||
if(nxt == s+1) nxt = (char*)txt;
|
||||
}else{ // 0
|
||||
nxt = s+1;
|
||||
*N = 0;
|
||||
}
|
||||
}else if(*s == 'b' || *s == 'B'){
|
||||
nxt = getbin(s+1, N);
|
||||
if(nxt == s+1) nxt = (char*)txt;
|
||||
}else{
|
||||
nxt = getdec(s, N);
|
||||
if(nxt == s) nxt = (char*)txt;
|
||||
}
|
||||
return nxt;
|
||||
}
|
||||
38
F1:F103/F1_testbrd/proto.h
Normal file
38
F1:F103/F1_testbrd/proto.h
Normal file
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* This file is part of the F0testbrd project.
|
||||
* Copyright 2021 Edward V. Emelianov <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 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#pragma once
|
||||
#ifndef PROTO_H__
|
||||
#define PROTO_H__
|
||||
|
||||
#include <stm32f1.h>
|
||||
|
||||
extern volatile uint8_t ADCmon;
|
||||
|
||||
void USB_sendstr(const char *str);
|
||||
char *get_USB();
|
||||
const char *parse_cmd(char *buf);
|
||||
|
||||
void printADCvals();
|
||||
|
||||
char *u2str(uint32_t val);
|
||||
char *i2str(int32_t i);
|
||||
char *uhex2str(uint32_t val);
|
||||
char *getnum(const char *txt, uint32_t *N);
|
||||
char *omit_spaces(const char *buf);
|
||||
|
||||
#endif // PROTO_H__
|
||||
@@ -20,6 +20,7 @@
|
||||
*/
|
||||
#include "stm32f1.h"
|
||||
#include "usart.h"
|
||||
#include "usb.h"
|
||||
|
||||
extern volatile uint32_t Tms;
|
||||
static volatile int idatalen[2] = {0,0}; // received data line length (including '\n')
|
||||
@@ -52,10 +53,13 @@ int usart_getline(char **line){
|
||||
|
||||
// transmit current tbuf and swap buffers
|
||||
void transmit_tbuf(){
|
||||
uint32_t tmout = 72000;
|
||||
while(!txrdy){if(--tmout == 0) return;}; // wait for previos buffer transmission
|
||||
//uint32_t tmout = 72000000;
|
||||
while(!txrdy);
|
||||
//while(!txrdy){if(--tmout == 0) return;}; // wait for previos buffer transmission
|
||||
register int l = odatalen[tbufno];
|
||||
if(!l) return;
|
||||
//USND("\n\nUTX:\n"); USB_send((uint8_t*)tbuf[tbufno], odatalen[tbufno]);
|
||||
//USND("\n\n\n");
|
||||
txrdy = 0;
|
||||
odatalen[tbufno] = 0;
|
||||
DMA1_Channel4->CCR &= ~DMA_CCR_EN;
|
||||
@@ -66,13 +70,12 @@ void transmit_tbuf(){
|
||||
}
|
||||
|
||||
void usart_putchar(const char ch){
|
||||
for(int i = 0; odatalen[tbufno] == UARTBUFSZO && i < 1024; ++i) transmit_tbuf();
|
||||
if(odatalen[tbufno] == UARTBUFSZO) transmit_tbuf();
|
||||
tbuf[tbufno][odatalen[tbufno]++] = ch;
|
||||
}
|
||||
|
||||
void usart_send(const char *str){
|
||||
uint32_t x = 512;
|
||||
while(*str && --x){
|
||||
while(*str){
|
||||
if(odatalen[tbufno] == UARTBUFSZO){
|
||||
transmit_tbuf();
|
||||
continue;
|
||||
@@ -158,91 +161,6 @@ void usart1_isr(){
|
||||
}
|
||||
}
|
||||
|
||||
// return string buffer with val
|
||||
char *u2str(uint32_t val){
|
||||
static char bufa[11];
|
||||
char 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];
|
||||
}
|
||||
}
|
||||
bufa[l + bpos] = 0;
|
||||
return bufa;
|
||||
}
|
||||
// print 32bit unsigned int
|
||||
void printu(uint32_t val){
|
||||
usart_send(u2str(val));
|
||||
}
|
||||
|
||||
// print 32bit unsigned int as hex
|
||||
void printuhex(uint32_t val){
|
||||
usart_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){
|
||||
register uint8_t half = (*ptr >> (4*j)) & 0x0f;
|
||||
if(half < 10) usart_putchar(half + '0');
|
||||
else usart_putchar(half - 10 + 'a');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// dump memory buffer
|
||||
void hexdump(uint8_t *arr, uint16_t len){
|
||||
for(uint16_t l = 0; l < len; ++l, ++arr){
|
||||
for(int16_t j = 1; j > -1; --j){
|
||||
register uint8_t half = (*arr >> (4*j)) & 0x0f;
|
||||
if(half < 10) usart_putchar(half + '0');
|
||||
else usart_putchar(half - 10 + 'a');
|
||||
}
|
||||
if(l % 16 == 15) usart_putchar('\n');
|
||||
else if((l & 3) == 3) usart_putchar(' ');
|
||||
}
|
||||
}
|
||||
|
||||
// dump USB memory (uint16_t mapped as uint32_t); len - in uint16_t
|
||||
void hexdump16(uint16_t *arr, uint16_t len){
|
||||
for(uint16_t l = 0; l < len; ++l, ++arr){
|
||||
uint16_t x = arr[l];
|
||||
for(int8_t i = 0; i < 2; ++i){
|
||||
for(int16_t j = 1; j > -1; --j){
|
||||
register uint8_t half = (x >> (4*j+8*i)) & 0x0f;
|
||||
if(half < 10) usart_putchar(half + '0');
|
||||
else usart_putchar(half - 10 + 'a');
|
||||
}
|
||||
}
|
||||
if(l % 8 == 7) usart_putchar('\n');
|
||||
else if(l & 1) usart_putchar(' ');
|
||||
}
|
||||
}
|
||||
|
||||
void hexdump32(uint32_t *arr, uint16_t len){
|
||||
for(uint16_t l = 0; l < len; ++l, ++arr){
|
||||
uint16_t x = (uint16_t)arr[l];
|
||||
for(int8_t i = 0; i < 2; ++i){
|
||||
for(int16_t j = 1; j > -1; --j){
|
||||
register uint8_t half = (x >> (4*j+8*i)) & 0x0f;
|
||||
if(half < 10) usart_putchar(half + '0');
|
||||
else usart_putchar(half - 10 + 'a');
|
||||
}
|
||||
}
|
||||
if(l % 8 == 7) usart_putchar('\n');
|
||||
else if(l & 1) usart_putchar(' ');
|
||||
}
|
||||
}
|
||||
|
||||
void dma1_channel4_isr(){
|
||||
if(DMA1->ISR & DMA_ISR_TCIF4){ // Tx
|
||||
DMA1->IFCR = DMA_IFCR_CTCIF4; // clear TC flag
|
||||
|
||||
@@ -57,11 +57,5 @@ int usart_getline(char **line);
|
||||
void usart_send(const char *str);
|
||||
void newline();
|
||||
void usart_putchar(const char ch);
|
||||
char *u2str(uint32_t val);
|
||||
void printu(uint32_t val);
|
||||
void printuhex(uint32_t val);
|
||||
void hexdump(uint8_t *arr, uint16_t len);
|
||||
void hexdump16(uint16_t *arr, uint16_t len);
|
||||
void hexdump32(uint32_t *arr, uint16_t len);
|
||||
|
||||
#endif // __USART_H__
|
||||
|
||||
@@ -28,6 +28,8 @@
|
||||
|
||||
#define BUFFSIZE (64)
|
||||
|
||||
#define USND(str) do{USB_send((uint8_t*)str, sizeof(str)-1);}while(0)
|
||||
|
||||
void USB_setup();
|
||||
void usb_proc();
|
||||
void USB_send(const uint8_t *buf, uint16_t len);
|
||||
|
||||
Reference in New Issue
Block a user