wip
This commit is contained in:
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
17097
workspace/.metadata/.ide.log-2025-12-07.log
Normal file
17097
workspace/.metadata/.ide.log-2025-12-07.log
Normal file
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -9,3 +9,4 @@
|
||||
*** SESSION dic 06, 2025 16:57:32.486 ------------------------------------------
|
||||
*** SESSION dic 06, 2025 22:49:40.40 -------------------------------------------
|
||||
*** SESSION dic 07, 2025 19:18:34.790 ------------------------------------------
|
||||
*** SESSION dic 08, 2025 11:52:34.203 ------------------------------------------
|
||||
|
||||
Binary file not shown.
@@ -1,10 +1,10 @@
|
||||
21:37:00 **** Incremental Build of configuration Debug for project AUTOM10 ****
|
||||
13:11:54 **** Incremental Build of configuration Debug for project AUTOM10 ****
|
||||
make -j12 all
|
||||
arm-none-eabi-size AUTOM10.elf
|
||||
text data bss dec hex filename
|
||||
45384 464 7184 53032 cf28 AUTOM10.elf
|
||||
45800 464 7184 53448 d0c8 AUTOM10.elf
|
||||
Finished building: default.size.stdout
|
||||
|
||||
|
||||
21:37:00 Build Finished. 0 errors, 0 warnings. (took 185ms)
|
||||
13:11:54 Build Finished. 0 errors, 0 warnings. (took 188ms)
|
||||
|
||||
|
||||
@@ -1,21 +1,21 @@
|
||||
21:36:56 **** Incremental Build of configuration Debug for project AUTOM10 ****
|
||||
13:11:50 **** Incremental Build of configuration Debug for project AUTOM10 ****
|
||||
make -j12 all
|
||||
arm-none-eabi-gcc "../Core/Src/cdc_int.c" -mcpu=cortex-m3 -std=gnu11 -g3 -DDEBUG -DUSE_HAL_DRIVER -DSTM32F103xB -c -I../Core/Inc -I../Drivers/STM32F1xx_HAL_Driver/Inc/Legacy -I../Drivers/STM32F1xx_HAL_Driver/Inc -I../Drivers/CMSIS/Device/ST/STM32F1xx/Include -I../Drivers/CMSIS/Include -I../USB_DEVICE/App -I../USB_DEVICE/Target -I../Middlewares/ST/STM32_USB_Device_Library/Core/Inc -I../Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Inc -O0 -ffunction-sections -fdata-sections -Wall -fstack-usage -fcyclomatic-complexity -MMD -MP -MF"Core/Src/cdc_int.d" -MT"Core/Src/cdc_int.o" --specs=nano.specs -mfloat-abi=soft -mthumb -o "Core/Src/cdc_int.o"
|
||||
arm-none-eabi-gcc "../Core/Src/eeprom.c" -mcpu=cortex-m3 -std=gnu11 -g3 -DDEBUG -DUSE_HAL_DRIVER -DSTM32F103xB -c -I../Core/Inc -I../Drivers/STM32F1xx_HAL_Driver/Inc/Legacy -I../Drivers/STM32F1xx_HAL_Driver/Inc -I../Drivers/CMSIS/Device/ST/STM32F1xx/Include -I../Drivers/CMSIS/Include -I../USB_DEVICE/App -I../USB_DEVICE/Target -I../Middlewares/ST/STM32_USB_Device_Library/Core/Inc -I../Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Inc -O0 -ffunction-sections -fdata-sections -Wall -fstack-usage -fcyclomatic-complexity -MMD -MP -MF"Core/Src/eeprom.d" -MT"Core/Src/eeprom.o" --specs=nano.specs -mfloat-abi=soft -mthumb -o "Core/Src/eeprom.o"
|
||||
arm-none-eabi-gcc -o "AUTOM10.elf" @"objects.list" -mcpu=cortex-m3 -T"C:\progetti\AUTOM\workspace\AUTOM10\STM32F103C8TX_FLASH.ld" --specs=nosys.specs -Wl,-Map="AUTOM10.map" -Wl,--gc-sections -static --specs=nano.specs -mfloat-abi=soft -mthumb -Wl,--start-group -lc -lm -Wl,--end-group
|
||||
Finished building target: AUTOM10.elf
|
||||
|
||||
arm-none-eabi-size AUTOM10.elf
|
||||
arm-none-eabi-objdump -h -S AUTOM10.elf > "AUTOM10.list"
|
||||
text data bss dec hex filename
|
||||
45384 464 7184 53032 cf28 AUTOM10.elf
|
||||
45800 464 7184 53448 d0c8 AUTOM10.elf
|
||||
Finished building: default.size.stdout
|
||||
|
||||
Finished building: AUTOM10.list
|
||||
|
||||
21:37:00 **** Incremental Build of configuration Debug for project AUTOM10 ****
|
||||
13:11:54 **** Incremental Build of configuration Debug for project AUTOM10 ****
|
||||
make -j12 all
|
||||
arm-none-eabi-size AUTOM10.elf
|
||||
text data bss dec hex filename
|
||||
45384 464 7184 53032 cf28 AUTOM10.elf
|
||||
45800 464 7184 53448 d0c8 AUTOM10.elf
|
||||
Finished building: default.size.stdout
|
||||
|
||||
|
||||
@@ -0,0 +1,197 @@
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include "usb_device.h"
|
||||
|
||||
#include "usbd_cdc_if.h"
|
||||
#include "cdc_int.h"
|
||||
#include "stm32f1xx_hal.h"
|
||||
#include "eeprom.h"
|
||||
#include "pwm.h"
|
||||
|
||||
extern TIM_HandleTypeDef htim1;
|
||||
extern TIM_HandleTypeDef htim2;
|
||||
extern TIM_HandleTypeDef htim3;
|
||||
extern TIM_HandleTypeDef htim4;
|
||||
extern const uint16_t EE_VirtAddrs[];
|
||||
|
||||
bool toHex(char c1,char c2,char c3,char c4,uint16_t* retval){
|
||||
if((c1>='0')&&(c1<='9')){
|
||||
c1-='0';
|
||||
}else if((c1>='a')&&(c1<='f')){
|
||||
c1=(c1-'a')+10;
|
||||
}else return false;
|
||||
|
||||
if((c2>='0')&&(c2<='9')){
|
||||
c2-='0';
|
||||
}else if((c2>='a')&&(c2<='f')){
|
||||
c2=(c2-'a')+10;
|
||||
}else return false;
|
||||
|
||||
if((c3>='0')&&(c3<='9')){
|
||||
c3-='0';
|
||||
}else if((c3>='a')&&(c3<='f')){
|
||||
c3=(c3-'a')+10;
|
||||
}else return false;
|
||||
|
||||
if((c4>='0')&&(c4<='9')){
|
||||
c4-='0';
|
||||
}else if((c4>='a')&&(c4<='f')){
|
||||
c4=(c4-'a')+10;
|
||||
}else return false;
|
||||
|
||||
*retval=c1;
|
||||
*retval*=16;
|
||||
*retval+=c2;
|
||||
*retval*=16;
|
||||
*retval+=c3;
|
||||
*retval*=16;
|
||||
*retval+=c4;
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
void manageCDC(void){
|
||||
static uint8_t rxbuf[100];
|
||||
static uint8_t rxidx=0;
|
||||
unsigned char s[100];
|
||||
uint8_t mot,dir,dm,m,c,d,u;
|
||||
uint16_t val;
|
||||
uint16_t st;
|
||||
uint16_t eeadd;
|
||||
uint8_t i;
|
||||
|
||||
if (CDC_Available()) {
|
||||
int rx = CDC_ReadByte();
|
||||
if (rx < 0) return;
|
||||
//uint8_t out = (uint8_t)c;
|
||||
//if (out >= 'a' && out <= 'z') out -= 32; // to upper
|
||||
rxbuf[rxidx]=rx;
|
||||
while (CDC_Transmit_FS(&rxbuf[rxidx], 1) == USBD_BUSY);//echo
|
||||
if((rx==0x0d)||(rx==0x0a)){
|
||||
if(rxidx){
|
||||
switch(rxbuf[0]){
|
||||
case 'i':
|
||||
sprintf((char*)s,"\ninfo");
|
||||
while (CDC_Transmit_FS(s, 5) == USBD_BUSY);
|
||||
sprintf((char*)s,"\npwm %d%d%d%d%d%d%d%d",IsPwmRunning(timMot1,FWMot1),IsPwmRunning(timMot1,BWMot1),IsPwmRunning(timMot2,FWMot2),IsPwmRunning(timMot2,BWMot2),IsPwmRunning(timMot3,FWMot3),IsPwmRunning(timMot3,BWMot3),IsPwmRunning(timMot4,FWMot4),IsPwmRunning(timMot4,BWMot4));
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
__HAL_TIM_GET_COMPARE(&htim3, TIM_CHANNEL_1);
|
||||
sprintf((char*)s,"\n0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx",(uint32_t)__HAL_TIM_GET_COMPARE(timMot1,FWMot1),(uint32_t)__HAL_TIM_GET_COMPARE(timMot1,BWMot1),(uint32_t)__HAL_TIM_GET_COMPARE(timMot2,FWMot2),(uint32_t)__HAL_TIM_GET_COMPARE(timMot2,BWMot2),(uint32_t)__HAL_TIM_GET_COMPARE(timMot3,FWMot3),(uint32_t)__HAL_TIM_GET_COMPARE(timMot3,BWMot3),(uint32_t)__HAL_TIM_GET_COMPARE(timMot4,FWMot4),(uint32_t)__HAL_TIM_GET_COMPARE(timMot4,BWMot4));
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
break;
|
||||
case 'm':
|
||||
if(rxidx==8){
|
||||
if((rxbuf[1]>='1')&&(rxbuf[1]<='4')){
|
||||
mot=rxbuf[1]-='0';
|
||||
if(rxbuf[2]=='f'){
|
||||
dir=FW;
|
||||
}else if(rxbuf[2]=='b'){
|
||||
dir=BW;
|
||||
}else{
|
||||
sprintf((char*)s,"\n?mnsvvvvv s=f|b");//m nmotore senso valore
|
||||
while (CDC_Transmit_FS(s, 16) == USBD_BUSY);
|
||||
break;
|
||||
}
|
||||
if(((rxbuf[3]>='0')&&(rxbuf[3]<='9'))&&((rxbuf[4]>='0')&&(rxbuf[4]<='9'))&&((rxbuf[5]>='0')&&(rxbuf[5]<='9'))&&((rxbuf[6]>='0')&&(rxbuf[6]<='9'))&&((rxbuf[7]>='0')&&(rxbuf[7]<='9'))){
|
||||
dm=rxbuf[3]-='0';
|
||||
m=rxbuf[4]-='0';
|
||||
c=rxbuf[5]-='0';
|
||||
d=rxbuf[6]-='0';
|
||||
u=rxbuf[7]-='0';
|
||||
val=dm;
|
||||
val*=10;
|
||||
val+=m;
|
||||
val*=10;
|
||||
val+=c;
|
||||
val*=10;
|
||||
val+=d;
|
||||
val*=10;
|
||||
val+=u;
|
||||
SetMot(mot,dir,val);
|
||||
}else{
|
||||
sprintf((char*)s,"\n?mnsvvvvv 00000>=vvvvv<=99999");//m nmotore senso valore
|
||||
while (CDC_Transmit_FS(s, 19) == USBD_BUSY);
|
||||
}
|
||||
|
||||
}else{
|
||||
sprintf((char*)s,"\n?mnsvvvvv 1>=m<=4");//m nmotore senso valore
|
||||
while (CDC_Transmit_FS(s, 19) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?mnsvvvvv");//m nmotore senso valore
|
||||
while (CDC_Transmit_FS(s, 10) == USBD_BUSY);
|
||||
}
|
||||
break;
|
||||
case 'e':
|
||||
if(rxidx>1){
|
||||
if(rxbuf[1]=='r'){
|
||||
if(rxidx==4){
|
||||
if(toHex('0','0',rxbuf[2],rxbuf[3],&eeadd)){
|
||||
st=EEW_Read(EE_VirtAddrs[eeadd], &val);
|
||||
if (st == EE_OK){
|
||||
sprintf((char*)s,"\nee 0x%02x=0x%04x",eeadd,val);
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}else{
|
||||
sprintf((char*)s,"\nee read error %d",st);
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?eraa hex values");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?eraa");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else if(rxbuf[1]=='w'){
|
||||
if(rxidx==8){
|
||||
if(toHex('0','0',rxbuf[2],rxbuf[3],&eeadd)){
|
||||
if(toHex(rxbuf[4],rxbuf[5],rxbuf[6],rxbuf[7],&val)){
|
||||
st=EEW_Write(EE_VirtAddrs[eeadd], val);
|
||||
if (st == EE_OK)sprintf((char*)s,"\ndone 0x%02x=0x%04x",eeadd,val);
|
||||
else sprintf((char*)s,"\nee write error %d", st);
|
||||
}else sprintf((char*)s,"\n?ewaavvvv hex values");
|
||||
}else sprintf((char*)s,"\n?ewaa hex values");
|
||||
}else sprintf((char*)s,"\n?ewaavvvv");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}else if(rxbuf[1]=='d'){
|
||||
if(rxidx==2){
|
||||
for(i=0;i<32;i++){
|
||||
if((i%4)==0){
|
||||
sprintf((char*)s,"\n");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
st=EEW_Read(EE_VirtAddrs[i], &val);
|
||||
if (st == EE_OK){
|
||||
sprintf((char*)s,"0x%02x=0x%04x ",i,val);
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}else{
|
||||
sprintf((char*)s,"rderr %d ",st);
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?ed");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?er|w|d");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?eraa | ewaavvvv");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
sprintf((char*)s,"\n?");
|
||||
while (CDC_Transmit_FS(s, 2) == USBD_BUSY);
|
||||
break;
|
||||
}
|
||||
}
|
||||
rxidx=0;
|
||||
}else rxidx++;
|
||||
// tiny spin or yield
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
|
||||
#include "usb_device.h"
|
||||
|
||||
#include "usbd_cdc_if.h"
|
||||
#include "cdc_int.h"
|
||||
|
||||
void manageCDC(void){
|
||||
static uint8_t rxbuf[100];
|
||||
static uint8_t rxidx=0;
|
||||
unsigned char s[100];
|
||||
if (CDC_Available()) {
|
||||
int c = CDC_ReadByte();
|
||||
if (c < 0) return;
|
||||
//uint8_t out = (uint8_t)c;
|
||||
//if (out >= 'a' && out <= 'z') out -= 32; // to upper
|
||||
rxbuf[rxidx]=c;
|
||||
rxidx++;
|
||||
if((c==0x0d)||(c==x0a)){
|
||||
switch(rxbuf[0]){
|
||||
// case 'i':
|
||||
// sprintf((char*)s,"\n?",c);
|
||||
// while (CDC_Transmit_FS(s, 6) == USBD_BUSY) {
|
||||
// break;
|
||||
default:
|
||||
sprintf((char*)s,"\n?",c);
|
||||
while (CDC_Transmit_FS(s, 6) == USBD_BUSY) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
// tiny spin or yield
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,204 @@
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include "usb_device.h"
|
||||
|
||||
#include "usbd_cdc_if.h"
|
||||
#include "cdc_int.h"
|
||||
#include "stm32f1xx_hal.h"
|
||||
#include "eeprom.h"
|
||||
#include "pwm.h"
|
||||
|
||||
extern TIM_HandleTypeDef htim1;
|
||||
extern TIM_HandleTypeDef htim2;
|
||||
extern TIM_HandleTypeDef htim3;
|
||||
extern TIM_HandleTypeDef htim4;
|
||||
|
||||
bool toHex(char c1,char c2,char c3,char c4,uint16_t* retval){
|
||||
if((c1>='0')&&(c1<='9')){
|
||||
c1-='0';
|
||||
}else if((c1>='a')&&(c1<='f')){
|
||||
c1=(c1-'a')+10;
|
||||
}else return false;
|
||||
|
||||
if((c2>='0')&&(c2<='9')){
|
||||
c2-='0';
|
||||
}else if((c2>='a')&&(c2<='f')){
|
||||
c2=(c2-'a')+10;
|
||||
}else return false;
|
||||
|
||||
if((c3>='0')&&(c3<='9')){
|
||||
c3-='0';
|
||||
}else if((c3>='a')&&(c3<='f')){
|
||||
c3=(c3-'a')+10;
|
||||
}else return false;
|
||||
|
||||
if((c4>='0')&&(c4<='9')){
|
||||
c4-='0';
|
||||
}else if((c4>='a')&&(c4<='f')){
|
||||
c4=(c4-'a')+10;
|
||||
}else return false;
|
||||
|
||||
*retval=c1;
|
||||
*retval*=16;
|
||||
*retval+=c2;
|
||||
*retval*=16;
|
||||
*retval+=c3;
|
||||
*retval*=16;
|
||||
*retval+=c4;
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
void manageCDC(void){
|
||||
static uint8_t rxbuf[100];
|
||||
static uint8_t rxidx=0;
|
||||
unsigned char s[100];
|
||||
uint8_t mot,dir,dm,m,c,d,u;
|
||||
uint16_t val;
|
||||
uint16_t st;
|
||||
uint16_t eeadd;
|
||||
uint8_t i;
|
||||
|
||||
if (CDC_Available()) {
|
||||
int rx = CDC_ReadByte();
|
||||
if (rx < 0) return;
|
||||
//uint8_t out = (uint8_t)c;
|
||||
//if (out >= 'a' && out <= 'z') out -= 32; // to upper
|
||||
rxbuf[rxidx]=rx;
|
||||
while (CDC_Transmit_FS(&rxbuf[rxidx], 1) == USBD_BUSY);//echo
|
||||
if((rx==0x0d)||(rx==0x0a)){
|
||||
if(rxidx){
|
||||
switch(rxbuf[0]){
|
||||
case 'i':
|
||||
sprintf((char*)s,"\ninfo");
|
||||
while (CDC_Transmit_FS(s, 5) == USBD_BUSY);
|
||||
sprintf((char*)s,"\npwm %d%d%d%d%d%d%d%d",IsPwmRunning(timMot1,FWMot1),IsPwmRunning(timMot1,BWMot1),IsPwmRunning(timMot2,FWMot2),IsPwmRunning(timMot2,BWMot2),IsPwmRunning(timMot3,FWMot3),IsPwmRunning(timMot3,BWMot3),IsPwmRunning(timMot4,FWMot4),IsPwmRunning(timMot4,BWMot4));
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
__HAL_TIM_GET_COMPARE(&htim3, TIM_CHANNEL_1);
|
||||
sprintf((char*)s,"\n0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx",(uint32_t)__HAL_TIM_GET_COMPARE(timMot1,FWMot1),(uint32_t)__HAL_TIM_GET_COMPARE(timMot1,BWMot1),(uint32_t)__HAL_TIM_GET_COMPARE(timMot2,FWMot2),(uint32_t)__HAL_TIM_GET_COMPARE(timMot2,BWMot2),(uint32_t)__HAL_TIM_GET_COMPARE(timMot3,FWMot3),(uint32_t)__HAL_TIM_GET_COMPARE(timMot3,BWMot3),(uint32_t)__HAL_TIM_GET_COMPARE(timMot4,FWMot4),(uint32_t)__HAL_TIM_GET_COMPARE(timMot4,BWMot4));
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
break;
|
||||
case 'm':
|
||||
if(rxidx==8){
|
||||
if((rxbuf[1]>='1')&&(rxbuf[1]<='4')){
|
||||
mot=rxbuf[1]-='0';
|
||||
if(rxbuf[2]=='f'){
|
||||
dir=FW;
|
||||
}else if(rxbuf[2]=='b'){
|
||||
dir=BW;
|
||||
}else{
|
||||
sprintf((char*)s,"\n?mnsvvvvv s=f|b");//m nmotore senso valore
|
||||
while (CDC_Transmit_FS(s, 16) == USBD_BUSY);
|
||||
break;
|
||||
}
|
||||
if(((rxbuf[3]>='0')&&(rxbuf[3]<='9'))&&((rxbuf[4]>='0')&&(rxbuf[4]<='9'))&&((rxbuf[5]>='0')&&(rxbuf[5]<='9'))&&((rxbuf[6]>='0')&&(rxbuf[6]<='9'))&&((rxbuf[7]>='0')&&(rxbuf[7]<='9'))){
|
||||
dm=rxbuf[3]-='0';
|
||||
m=rxbuf[4]-='0';
|
||||
c=rxbuf[5]-='0';
|
||||
d=rxbuf[6]-='0';
|
||||
u=rxbuf[7]-='0';
|
||||
val=dm;
|
||||
val*=10;
|
||||
val+=m;
|
||||
val*=10;
|
||||
val+=c;
|
||||
val*=10;
|
||||
val+=d;
|
||||
val*=10;
|
||||
val+=u;
|
||||
SetMot(mot,dir,val);
|
||||
}else{
|
||||
sprintf((char*)s,"\n?mnsvvvvv 00000>=vvvvv<=99999");//m nmotore senso valore
|
||||
while (CDC_Transmit_FS(s, 19) == USBD_BUSY);
|
||||
}
|
||||
|
||||
}else{
|
||||
sprintf((char*)s,"\n?mnsvvvvv 1>=m<=4");//m nmotore senso valore
|
||||
while (CDC_Transmit_FS(s, 19) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?mnsvvvvv");//m nmotore senso valore
|
||||
while (CDC_Transmit_FS(s, 10) == USBD_BUSY);
|
||||
}
|
||||
break;
|
||||
case 'e':
|
||||
if(rxidx>1){
|
||||
if(rxbuf[1]=='r'){
|
||||
if(rxidx==4){
|
||||
if(toHex('0','0',rxbuf[2],rxbuf[3],&eeadd)){
|
||||
st=EEW_Read(eeadd, &val);
|
||||
if (st == EE_OK){
|
||||
sprintf((char*)s,"\nee 0x%02x=0x%04x",eeadd,val);
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}else{
|
||||
sprintf((char*)s,"\nee read error %d",st);
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?eraa hex values");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?eraa");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else if(rxbuf[1]=='w'){
|
||||
if(rxidx==8){
|
||||
if(toHex('0','0',rxbuf[2],rxbuf[3],&eeadd)){
|
||||
if(toHex(rxbuf[4],rxbuf[5],rxbuf[6],rxbuf[7],&val)){
|
||||
EEW_Write(eeadd, val);
|
||||
sprintf((char*)s,"\ndone 0x%02x=0x%04x",eeadd,val);
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}else{
|
||||
sprintf((char*)s,"\n?ewaavvvv hex values");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?ewaa hex values");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?ewaavvvv");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else if(rxbuf[1]=='d'){
|
||||
if(rxidx==2){
|
||||
for(i=0;i<32;i++){
|
||||
if((i%4)==0){
|
||||
sprintf((char*)s,"\n");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
st=EEW_Read(i, &val);
|
||||
if (st == EE_OK){
|
||||
sprintf((char*)s,"0x%02x=0x%04x ",i,val);
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}else{
|
||||
sprintf((char*)s,"rderr %d ",st);
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?ed");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?er|w|d");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?eraa | ewaavvvv");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
sprintf((char*)s,"\n?");
|
||||
while (CDC_Transmit_FS(s, 2) == USBD_BUSY);
|
||||
break;
|
||||
}
|
||||
}
|
||||
rxidx=0;
|
||||
}else rxidx++;
|
||||
// tiny spin or yield
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include "usb_device.h"
|
||||
|
||||
#include "usbd_cdc_if.h"
|
||||
#include "cdc_int.h"
|
||||
#include "stm32f1xx_hal.h"
|
||||
#include "eeprom.h"
|
||||
#include "pwm.h"
|
||||
|
||||
extern TIM_HandleTypeDef htim1;
|
||||
extern TIM_HandleTypeDef htim2;
|
||||
extern TIM_HandleTypeDef htim3;
|
||||
extern TIM_HandleTypeDef htim4;
|
||||
|
||||
bool toHex(char c1,char c2,char c3,char c4,uint16_t* retval){
|
||||
if((c1>='0')&&(c1<='9')){
|
||||
c1-='0';
|
||||
}else if((c1>='a')&&(c1<='f')){
|
||||
c1=(c1-'a')+10;
|
||||
}else return false;
|
||||
|
||||
if((c2>='0')&&(c2<='9')){
|
||||
c2-='0';
|
||||
}else if((c2>='a')&&(c2<='f')){
|
||||
c2=(c2-'a')+10;
|
||||
}else return false;
|
||||
|
||||
if((c3>='0')&&(c3<='9')){
|
||||
c3-='0';
|
||||
}else if((c3>='a')&&(c3<='f')){
|
||||
c3=(c3-'a')+10;
|
||||
}else return false;
|
||||
|
||||
if((c4>='0')&&(c4<='9')){
|
||||
c4-='0';
|
||||
}else if((c4>='a')&&(c4<='f')){
|
||||
c4=(c4-'a')+10;
|
||||
}else return false;
|
||||
|
||||
*retval=c1;
|
||||
*retval*=10;
|
||||
*retval+=c2;
|
||||
*retval*=10;
|
||||
*retval+=c3;
|
||||
*retval*=10;
|
||||
*retval+=c4;
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
void manageCDC(void){
|
||||
static uint8_t rxbuf[100];
|
||||
static uint8_t rxidx=0;
|
||||
unsigned char s[100];
|
||||
uint8_t mot,dir,dm,m,c,d,u;
|
||||
uint16_t val;
|
||||
uint16_t eeadd;
|
||||
|
||||
if (CDC_Available()) {
|
||||
int rx = CDC_ReadByte();
|
||||
if (rx < 0) return;
|
||||
//uint8_t out = (uint8_t)c;
|
||||
//if (out >= 'a' && out <= 'z') out -= 32; // to upper
|
||||
rxbuf[rxidx]=rx;
|
||||
while (CDC_Transmit_FS(&rxbuf[rxidx], 1) == USBD_BUSY);//echo
|
||||
if((rx==0x0d)||(rx==0x0a)){
|
||||
if(rxidx){
|
||||
switch(rxbuf[0]){
|
||||
case 'i':
|
||||
sprintf((char*)s,"\ninfo");
|
||||
while (CDC_Transmit_FS(s, 5) == USBD_BUSY);
|
||||
sprintf((char*)s,"\npwm %d%d%d%d%d%d%d%d",IsPwmRunning(timMot1,FWMot1),IsPwmRunning(timMot1,BWMot1),IsPwmRunning(timMot2,FWMot2),IsPwmRunning(timMot2,BWMot2),IsPwmRunning(timMot3,FWMot3),IsPwmRunning(timMot3,BWMot3),IsPwmRunning(timMot4,FWMot4),IsPwmRunning(timMot4,BWMot4));
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
__HAL_TIM_GET_COMPARE(&htim3, TIM_CHANNEL_1);
|
||||
sprintf((char*)s,"\n0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx",(uint32_t)__HAL_TIM_GET_COMPARE(timMot1,FWMot1),(uint32_t)__HAL_TIM_GET_COMPARE(timMot1,BWMot1),(uint32_t)__HAL_TIM_GET_COMPARE(timMot2,FWMot2),(uint32_t)__HAL_TIM_GET_COMPARE(timMot2,BWMot2),(uint32_t)__HAL_TIM_GET_COMPARE(timMot3,FWMot3),(uint32_t)__HAL_TIM_GET_COMPARE(timMot3,BWMot3),(uint32_t)__HAL_TIM_GET_COMPARE(timMot4,FWMot4),(uint32_t)__HAL_TIM_GET_COMPARE(timMot4,BWMot4));
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
break;
|
||||
case 'm':
|
||||
if(rxidx==8){
|
||||
if((rxbuf[1]>='1')&&(rxbuf[1]<='4')){
|
||||
mot=rxbuf[1]-='0';
|
||||
if(rxbuf[2]=='f'){
|
||||
dir=FW;
|
||||
}else if(rxbuf[2]=='b'){
|
||||
dir=BW;
|
||||
}else{
|
||||
sprintf((char*)s,"\n?mnsvvvvv s=f|b");//m nmotore senso valore
|
||||
while (CDC_Transmit_FS(s, 16) == USBD_BUSY);
|
||||
break;
|
||||
}
|
||||
if(((rxbuf[3]>='0')&&(rxbuf[3]<='9'))&&((rxbuf[4]>='0')&&(rxbuf[4]<='9'))&&((rxbuf[5]>='0')&&(rxbuf[5]<='9'))&&((rxbuf[6]>='0')&&(rxbuf[6]<='9'))&&((rxbuf[7]>='0')&&(rxbuf[7]<='9'))){
|
||||
dm=rxbuf[3]-='0';
|
||||
m=rxbuf[4]-='0';
|
||||
c=rxbuf[5]-='0';
|
||||
d=rxbuf[6]-='0';
|
||||
u=rxbuf[7]-='0';
|
||||
val=dm;
|
||||
val*=10;
|
||||
val+=m;
|
||||
val*=10;
|
||||
val+=c;
|
||||
val*=10;
|
||||
val+=d;
|
||||
val*=10;
|
||||
val+=u;
|
||||
SetMot(mot,dir,val);
|
||||
}else{
|
||||
sprintf((char*)s,"\n?mnsvvvvv 00000>=vvvvv<=99999");//m nmotore senso valore
|
||||
while (CDC_Transmit_FS(s, 19) == USBD_BUSY);
|
||||
}
|
||||
|
||||
}else{
|
||||
sprintf((char*)s,"\n?mnsvvvvv 1>=m<=4");//m nmotore senso valore
|
||||
while (CDC_Transmit_FS(s, 19) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?mnsvvvvv");//m nmotore senso valore
|
||||
while (CDC_Transmit_FS(s, 10) == USBD_BUSY);
|
||||
}
|
||||
break;
|
||||
case 'e':
|
||||
if(rxidx>1){
|
||||
if(rxbuf[1]=='r'){
|
||||
if(rxidx==4){
|
||||
if(toHex('0','0',rxbuf[2],rxbuf[3],&eeadd)){
|
||||
EEW_Read(eeadd, &val);
|
||||
sprintf((char*)s,"\nee 0x%02x=%04x",eeadd,val);
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}else{
|
||||
sprintf((char*)s,"\n?eraa hex values");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?eraa");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else if(rxbuf[1]=='w'){
|
||||
if(rxidx==8){
|
||||
if(toHex('0','0',rxbuf[2],rxbuf[3],&eeadd)){
|
||||
if(toHex(rxbuf[4],rxbuf[5],rxbuf[6],rxbuf[7],&val)){
|
||||
EEW_Write(eeadd, val);
|
||||
sprintf((char*)s,"\ndone");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}else{
|
||||
sprintf((char*)s,"\n?ewaavvvv hex values");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?ewaa hex values");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?ewaavvvv");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?er|w");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?eraa | ewaavvvv");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
sprintf((char*)s,"\n?");
|
||||
while (CDC_Transmit_FS(s, 2) == USBD_BUSY);
|
||||
break;
|
||||
}
|
||||
}
|
||||
rxidx=0;
|
||||
}else rxidx++;
|
||||
// tiny spin or yield
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,287 @@
|
||||
#include "eeprom.h"
|
||||
|
||||
/*
|
||||
* Record format (4 bytes):
|
||||
* [0] VirtAddress (uint16_t)
|
||||
* [2] Data (uint16_t)
|
||||
*
|
||||
* Page layout:
|
||||
* [0] PageStatus (uint16_t)
|
||||
* [2..] Records...
|
||||
*/
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint16_t VirtAddress;
|
||||
uint16_t Data;
|
||||
} EE_Record_t;
|
||||
|
||||
/* Active page base address (runtime selected in EE_Init) */
|
||||
static uint32_t EE_ActivePageBase = EE_PAGE0_BASE;
|
||||
|
||||
/* 32 sequential virtual addresses */
|
||||
const uint16_t EE_VirtAddrs[EE_NUM_VIRTUAL_ADDR] =
|
||||
{
|
||||
0x0001, 0x0002, 0x0003, 0x0004,
|
||||
0x0005, 0x0006, 0x0007, 0x0008,
|
||||
0x0009, 0x000A, 0x000B, 0x000C,
|
||||
0x000D, 0x000E, 0x000F, 0x0010,
|
||||
0x0011, 0x0012, 0x0013, 0x0014,
|
||||
0x0015, 0x0016, 0x0017, 0x0018,
|
||||
0x0019, 0x001A, 0x001B, 0x001C,
|
||||
0x001D, 0x001E, 0x001F, 0x0020
|
||||
};
|
||||
|
||||
/* ========================================================================= */
|
||||
|
||||
/* --- Internal helpers ---------------------------------------------------- */
|
||||
|
||||
static uint16_t EE_GetPageStatus(uint32_t pageBase)
|
||||
{
|
||||
return *(__IO uint16_t *)pageBase;
|
||||
}
|
||||
|
||||
static HAL_StatusTypeDef EE_FlashProgramHalfWord(uint32_t Address, uint16_t Data)
|
||||
{
|
||||
HAL_StatusTypeDef status;
|
||||
|
||||
HAL_FLASH_Unlock();
|
||||
status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, Address, Data);
|
||||
HAL_FLASH_Lock();
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
static HAL_StatusTypeDef EE_FlashErasePage(uint32_t PageAddress)
|
||||
{
|
||||
HAL_StatusTypeDef status;
|
||||
FLASH_EraseInitTypeDef EraseInit;
|
||||
uint32_t PageError = 0;
|
||||
|
||||
HAL_FLASH_Unlock();
|
||||
|
||||
EraseInit.TypeErase = FLASH_TYPEERASE_PAGES;
|
||||
EraseInit.PageAddress = PageAddress;
|
||||
EraseInit.NbPages = 1;
|
||||
|
||||
status = HAL_FLASHEx_Erase(&EraseInit, &PageError);
|
||||
|
||||
HAL_FLASH_Lock();
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Find first free record address in given page (returns 0 if full) */
|
||||
static uint32_t EE_FindFreeAddress(uint32_t pageBase)
|
||||
{
|
||||
uint32_t addr = pageBase + 2U; /* Skip status word */
|
||||
uint32_t pageEnd = pageBase + EE_PAGE_SIZE;
|
||||
|
||||
while (addr < (pageEnd - sizeof(EE_Record_t) + 1U))
|
||||
{
|
||||
uint16_t vaddr = *(__IO uint16_t *)addr;
|
||||
|
||||
if (vaddr == 0xFFFFU)
|
||||
{
|
||||
/* Empty slot */
|
||||
return addr;
|
||||
}
|
||||
addr += sizeof(EE_Record_t);
|
||||
}
|
||||
|
||||
return 0U; /* No space */
|
||||
}
|
||||
|
||||
/* Find latest value of VirtAddress in a specific page */
|
||||
static EE_Status EE_FindInPage(uint32_t pageBase, uint16_t VirtAddress, uint16_t *Data)
|
||||
{
|
||||
uint32_t pageEnd = pageBase + EE_PAGE_SIZE;
|
||||
uint32_t addr;
|
||||
|
||||
if (Data == NULL)
|
||||
return EE_ERROR;
|
||||
|
||||
/* Start from last possible record and go backwards */
|
||||
for (addr = pageEnd - sizeof(EE_Record_t); addr > pageBase + 1U; addr -= sizeof(EE_Record_t))
|
||||
{
|
||||
uint16_t vaddr = *(__IO uint16_t *)addr;
|
||||
uint16_t value = *(__IO uint16_t *)(addr + 2U);
|
||||
|
||||
if (vaddr == 0xFFFFU)
|
||||
{
|
||||
/* Empty slot, skip */
|
||||
continue;
|
||||
}
|
||||
|
||||
if (vaddr == VirtAddress)
|
||||
{
|
||||
*Data = value;
|
||||
return EE_OK;
|
||||
}
|
||||
}
|
||||
|
||||
return EE_NOT_FOUND;
|
||||
}
|
||||
|
||||
/* Format both pages: erase and set PAGE0 as VALID */
|
||||
static EE_Status EE_Format(void)
|
||||
{
|
||||
if (EE_FlashErasePage(EE_PAGE0_BASE) != HAL_OK)
|
||||
return EE_ERROR;
|
||||
|
||||
if (EE_FlashErasePage(EE_PAGE1_BASE) != HAL_OK)
|
||||
return EE_ERROR;
|
||||
|
||||
if (EE_FlashProgramHalfWord(EE_PAGE0_BASE, EE_PAGE_STATUS_VALID) != HAL_OK)
|
||||
return EE_ERROR;
|
||||
|
||||
/* PAGE1 will remain erased (status = 0xFFFF) */
|
||||
EE_ActivePageBase = EE_PAGE0_BASE;
|
||||
|
||||
return EE_OK;
|
||||
}
|
||||
|
||||
/* Get the base of the other page */
|
||||
static uint32_t EE_GetOtherPageBase(uint32_t pageBase)
|
||||
{
|
||||
return (pageBase == EE_PAGE0_BASE) ? EE_PAGE1_BASE : EE_PAGE0_BASE;
|
||||
}
|
||||
|
||||
/* Page transfer (garbage collection + new write) */
|
||||
static EE_Status EE_PageTransfer(uint16_t VirtAddress, uint16_t Data)
|
||||
{
|
||||
uint32_t oldBase = EE_ActivePageBase;
|
||||
uint32_t newBase = EE_GetOtherPageBase(oldBase);
|
||||
uint32_t addr;
|
||||
uint16_t value;
|
||||
EE_Status st;
|
||||
|
||||
/* Erase new page */
|
||||
if (EE_FlashErasePage(newBase) != HAL_OK)
|
||||
return EE_ERROR;
|
||||
|
||||
/* Mark new page as RECEIVE */
|
||||
if (EE_FlashProgramHalfWord(newBase, EE_PAGE_STATUS_RECEIVE) != HAL_OK)
|
||||
return EE_ERROR;
|
||||
|
||||
/* Start writing records just after status */
|
||||
addr = newBase + 2U;
|
||||
|
||||
/* For each known virtual variable */
|
||||
for (uint16_t i = 0; i < EE_NUM_VIRTUAL_ADDR; i++)
|
||||
{
|
||||
uint16_t vaddr = EE_VirtAddrs[i];
|
||||
|
||||
if (vaddr == VirtAddress)
|
||||
{
|
||||
/* Use the new data passed into PageTransfer */
|
||||
value = Data;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Read latest value from old active page */
|
||||
st = EE_FindInPage(oldBase, vaddr, &value);
|
||||
if (st != EE_OK)
|
||||
{
|
||||
/* Variable never written -> skip */
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
/* Write record to new page */
|
||||
if (EE_FlashProgramHalfWord(addr, vaddr) != HAL_OK)
|
||||
return EE_ERROR;
|
||||
if (EE_FlashProgramHalfWord(addr + 2U, value) != HAL_OK)
|
||||
return EE_ERROR;
|
||||
|
||||
addr += sizeof(EE_Record_t);
|
||||
if (addr >= (newBase + EE_PAGE_SIZE))
|
||||
return EE_NO_SPACE;
|
||||
}
|
||||
|
||||
/* Erase old page */
|
||||
if (EE_FlashErasePage(oldBase) != HAL_OK)
|
||||
return EE_ERROR;
|
||||
|
||||
/* Mark new page as VALID */
|
||||
if (EE_FlashProgramHalfWord(newBase, EE_PAGE_STATUS_VALID) != HAL_OK)
|
||||
return EE_ERROR;
|
||||
|
||||
/* Update active page */
|
||||
EE_ActivePageBase = newBase;
|
||||
|
||||
return EE_OK;
|
||||
}
|
||||
|
||||
/* --- Public API ----------------------------------------------------------- */
|
||||
|
||||
/*
|
||||
* Initialize the EEPROM emulation.
|
||||
* - Checks page statuses and chooses the active page.
|
||||
* - If inconsistent or blank, formats pages.
|
||||
*/
|
||||
EE_Status EE_Init(void)
|
||||
{
|
||||
uint16_t status0 = EE_GetPageStatus(EE_PAGE0_BASE);
|
||||
uint16_t status1 = EE_GetPageStatus(EE_PAGE1_BASE);
|
||||
|
||||
if ((status0 == EE_PAGE_STATUS_ERASED) && (status1 == EE_PAGE_STATUS_ERASED))
|
||||
{
|
||||
/* Fresh device -> format */
|
||||
return EE_Format();
|
||||
}
|
||||
else if ((status0 == EE_PAGE_STATUS_VALID) && (status1 == EE_PAGE_STATUS_ERASED))
|
||||
{
|
||||
EE_ActivePageBase = EE_PAGE0_BASE;
|
||||
return EE_OK;
|
||||
}
|
||||
else if ((status1 == EE_PAGE_STATUS_VALID) && (status0 == EE_PAGE_STATUS_ERASED))
|
||||
{
|
||||
EE_ActivePageBase = EE_PAGE1_BASE;
|
||||
return EE_OK;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Any weird or inconsistent state -> reformat */
|
||||
return EE_Format();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Read a 16-bit variable by its virtual address.
|
||||
*/
|
||||
EE_Status EE_ReadVariable(uint16_t VirtAddress, uint16_t *Data)
|
||||
{
|
||||
if (Data == NULL)
|
||||
return EE_ERROR;
|
||||
|
||||
return EE_FindInPage(EE_ActivePageBase, VirtAddress, Data);
|
||||
}
|
||||
|
||||
/*
|
||||
* Write (append) a 16-bit variable.
|
||||
* - Writes a new record in the active page.
|
||||
* - If the page is full, triggers a page transfer (GC).
|
||||
*/
|
||||
EE_Status EE_WriteVariable(uint16_t VirtAddress, uint16_t Data)
|
||||
{
|
||||
uint32_t freeAddr;
|
||||
EE_Status st;
|
||||
|
||||
/* Find free space in active page */
|
||||
freeAddr = EE_FindFreeAddress(EE_ActivePageBase);
|
||||
if (freeAddr != 0U)
|
||||
{
|
||||
/* Write new record */
|
||||
if (EE_FlashProgramHalfWord(freeAddr, VirtAddress) != HAL_OK)
|
||||
return EE_ERROR;
|
||||
if (EE_FlashProgramHalfWord(freeAddr + 2U, Data) != HAL_OK)
|
||||
return EE_ERROR;
|
||||
|
||||
return EE_OK;
|
||||
}
|
||||
|
||||
/* No space -> page transfer */
|
||||
st = EE_PageTransfer(VirtAddress, Data);
|
||||
return st;
|
||||
}
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
|
||||
#include "usb_device.h"
|
||||
|
||||
#include "usbd_cdc_if.h"
|
||||
#include "cdc_int.h"
|
||||
|
||||
void manageCDC(void){
|
||||
if (CDC_Available()) {
|
||||
int c = CDC_ReadByte();
|
||||
//if (c < 0) break;
|
||||
|
||||
// uint8_t out = (uint8_t)c;
|
||||
// if (out >= 'a' && out <= 'z') out -= 32; // to upper
|
||||
|
||||
unsigned char s[100];
|
||||
sprintf((char*)s,"\nc=%03d",c);
|
||||
while (CDC_Transmit_FS(s, 6) == USBD_BUSY) {
|
||||
// tiny spin or yield
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
|
||||
#include "usb_device.h"
|
||||
|
||||
#include "usbd_cdc_if.h"
|
||||
#include "cdc_int.h"
|
||||
|
||||
void manageCDC(void){
|
||||
static uint8_t rxbuf[100];
|
||||
static uint8_t rxidx=0;
|
||||
unsigned char s[100];
|
||||
if (CDC_Available()) {
|
||||
int c = CDC_ReadByte();
|
||||
if (c < 0) return;
|
||||
//uint8_t out = (uint8_t)c;
|
||||
//if (out >= 'a' && out <= 'z') out -= 32; // to upper
|
||||
rxbuf[rxidx]=c;
|
||||
rxidx++;
|
||||
if((c==0x0d)||(c==0x0a)){
|
||||
switch(rxbuf[0]){
|
||||
// case 'i':
|
||||
// sprintf((char*)s,"\n?",c);
|
||||
// while (CDC_Transmit_FS(s, 6) == USBD_BUSY) {
|
||||
// break;
|
||||
default:
|
||||
sprintf((char*)s,"\n?",c);
|
||||
while (CDC_Transmit_FS(s, 6) == USBD_BUSY) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
// tiny spin or yield
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* eeprom.h
|
||||
*
|
||||
* Created on: Dec 7, 2025
|
||||
* Author: user
|
||||
*/
|
||||
|
||||
#ifndef __EEPROM_H
|
||||
#define __EEPROM_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "stm32f1xx_hal.h"
|
||||
|
||||
/*
|
||||
* Simple EEPROM emulation for STM32F103C8T6 (medium density).
|
||||
* - Uses 2 Flash pages (1 kB each) at the end of Flash.
|
||||
* - Stores variables as 16-bit values identified by 16-bit "virtual addresses".
|
||||
*
|
||||
* You must define the list of virtual addresses in eeprom.c: EE_VirtAddrs[].
|
||||
*/
|
||||
|
||||
typedef enum
|
||||
{
|
||||
EE_OK = 0,
|
||||
EE_ERROR,
|
||||
EE_NOT_FOUND,
|
||||
EE_NO_SPACE
|
||||
} EE_Status;
|
||||
|
||||
/* Flash parameters for STM32F103C8T6 */
|
||||
#define EE_FLASH_BASE_ADDR 0x08000000U
|
||||
#define EE_PAGE_SIZE 0x400U /* 1 kB pages */
|
||||
|
||||
/*
|
||||
* Here we assume a 64 kB Flash device (STM32F103C8T6):
|
||||
* Flash range: 0x0800 0000 - 0x0800 FFFF
|
||||
* Pages: 0..63 (64 pages)
|
||||
* We use the last 2 pages for EEPROM:
|
||||
* - Page 62: 0x0800 F800
|
||||
* - Page 63: 0x0800 FC00
|
||||
*/
|
||||
#define EE_PAGE0_BASE (EE_FLASH_BASE_ADDR + (62U * EE_PAGE_SIZE))
|
||||
#define EE_PAGE1_BASE (EE_FLASH_BASE_ADDR + (63U * EE_PAGE_SIZE))
|
||||
|
||||
/* Page status markers (stored in the first halfword of each page) */
|
||||
#define EE_PAGE_STATUS_ERASED 0xFFFFU
|
||||
#define EE_PAGE_STATUS_VALID 0xAAAAU
|
||||
#define EE_PAGE_STATUS_RECEIVE 0x5555U
|
||||
|
||||
/*
|
||||
* Configure how many virtual variables you have.
|
||||
* Example: bytes, words, and array elements mapped to 16-bit variables.
|
||||
* Set EE_NUM_VIRTUAL_ADDR and define EE_VirtAddrs[] in eeprom.c.
|
||||
*/
|
||||
/* 32 virtual variables, sequential addresses */
|
||||
#define EE_NUM_VIRTUAL_ADDR 32U
|
||||
#define EEW_ADDR(i) (uint16_t)(0x0001 + (i)) // i = 0..31
|
||||
|
||||
|
||||
|
||||
|
||||
/* Virtual address table (defined in eeprom.c, can be customized) */
|
||||
extern const uint16_t EE_VirtAddrs[EE_NUM_VIRTUAL_ADDR];
|
||||
|
||||
/* Public API */
|
||||
EE_Status EE_Init(void);
|
||||
EE_Status EE_ReadVariable(uint16_t VirtAddress, uint16_t *Data);
|
||||
EE_Status EE_WriteVariable(uint16_t VirtAddress, uint16_t Data);
|
||||
|
||||
/* Pseudo-array accessor */
|
||||
static inline EE_Status EEW_Read(uint8_t idx, uint16_t *value)
|
||||
{
|
||||
return EE_ReadVariable(EEW_ADDR(idx), value);
|
||||
}
|
||||
|
||||
static inline EE_Status EEW_Write(uint8_t idx, uint16_t value)
|
||||
{
|
||||
return EE_WriteVariable(EEW_ADDR(idx), value);
|
||||
}
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __EEPROM_H */
|
||||
|
||||
@@ -0,0 +1,295 @@
|
||||
#include "eeprom.h"
|
||||
|
||||
/*
|
||||
* Record format (4 bytes):
|
||||
* [0] VirtAddress (uint16_t)
|
||||
* [2] Data (uint16_t)
|
||||
*
|
||||
* Page layout:
|
||||
* [0] PageStatus (uint16_t)
|
||||
* [2..] Records...
|
||||
*/
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint16_t VirtAddress;
|
||||
uint16_t Data;
|
||||
} EE_Record_t;
|
||||
|
||||
/* Active page base address (runtime selected in EE_Init) */
|
||||
static uint32_t EE_ActivePageBase = EE_PAGE0_BASE;
|
||||
|
||||
/* 32 sequential virtual addresses */
|
||||
const uint16_t EE_VirtAddrs[EE_NUM_VIRTUAL_ADDR] =
|
||||
{
|
||||
0x0001, 0x0002, 0x0003, 0x0004,
|
||||
0x0005, 0x0006, 0x0007, 0x0008,
|
||||
0x0009, 0x000A, 0x000B, 0x000C,
|
||||
0x000D, 0x000E, 0x000F, 0x0010,
|
||||
0x0011, 0x0012, 0x0013, 0x0014,
|
||||
0x0015, 0x0016, 0x0017, 0x0018,
|
||||
0x0019, 0x001A, 0x001B, 0x001C,
|
||||
0x001D, 0x001E, 0x001F, 0x0020
|
||||
};
|
||||
/* ========================================================================= */
|
||||
|
||||
/* --- Internal helpers ---------------------------------------------------- */
|
||||
|
||||
static uint16_t EE_GetPageStatus(uint32_t pageBase)
|
||||
{
|
||||
return *(__IO uint16_t *)pageBase;
|
||||
}
|
||||
|
||||
static HAL_StatusTypeDef EE_FlashProgramHalfWord(uint32_t Address, uint16_t Data)
|
||||
{
|
||||
HAL_StatusTypeDef status;
|
||||
|
||||
HAL_FLASH_Unlock();
|
||||
status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, Address, Data);
|
||||
HAL_FLASH_Lock();
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
static HAL_StatusTypeDef EE_FlashErasePage(uint32_t PageAddress)
|
||||
{
|
||||
HAL_StatusTypeDef status;
|
||||
FLASH_EraseInitTypeDef EraseInit;
|
||||
uint32_t PageError = 0;
|
||||
|
||||
HAL_FLASH_Unlock();
|
||||
|
||||
EraseInit.TypeErase = FLASH_TYPEERASE_PAGES;
|
||||
EraseInit.PageAddress = PageAddress;
|
||||
EraseInit.NbPages = 1;
|
||||
|
||||
status = HAL_FLASHEx_Erase(&EraseInit, &PageError);
|
||||
|
||||
HAL_FLASH_Lock();
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Find first free record address in given page (returns 0 if full) */
|
||||
static uint32_t EE_FindFreeAddress(uint32_t pageBase)
|
||||
{
|
||||
uint32_t addr = pageBase + 2U; /* Skip status word */
|
||||
uint32_t pageEnd = pageBase + EE_PAGE_SIZE;
|
||||
|
||||
while (addr < (pageEnd - sizeof(EE_Record_t) + 1U))
|
||||
{
|
||||
uint16_t vaddr = *(__IO uint16_t *)addr;
|
||||
|
||||
if (vaddr == 0xFFFFU)
|
||||
{
|
||||
/* Empty slot */
|
||||
return addr;
|
||||
}
|
||||
addr += sizeof(EE_Record_t);
|
||||
}
|
||||
|
||||
return 0U; /* No space */
|
||||
}
|
||||
|
||||
/* Find latest value of VirtAddress in a specific page (internal, uses EE_Status) */
|
||||
static EE_Status EE_FindInPage(uint32_t pageBase, uint16_t VirtAddress, uint16_t *Data)
|
||||
{
|
||||
uint32_t pageEnd = pageBase + EE_PAGE_SIZE;
|
||||
uint32_t addr;
|
||||
|
||||
if (Data == NULL)
|
||||
return EE_ERROR;
|
||||
|
||||
/* Start from last possible record and go backwards */
|
||||
for (addr = pageEnd - sizeof(EE_Record_t);
|
||||
addr >= pageBase + 2U; // <-- FIX HERE
|
||||
addr -= sizeof(EE_Record_t))
|
||||
{
|
||||
uint16_t vaddr = *(__IO uint16_t *)addr;
|
||||
uint16_t value = *(__IO uint16_t *)(addr + 2U);
|
||||
|
||||
if (vaddr == 0xFFFFU)
|
||||
{
|
||||
/* Empty slot, skip */
|
||||
continue;
|
||||
}
|
||||
|
||||
if (vaddr == VirtAddress)
|
||||
{
|
||||
*Data = value;
|
||||
return EE_OK;
|
||||
}
|
||||
}
|
||||
|
||||
return EE_STATUS_NOT_FOUND; // or EE_NOT_FOUND depending on your enum
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* Format both pages: erase and set PAGE0 as VALID */
|
||||
static EE_Status EE_Format(void)
|
||||
{
|
||||
if (EE_FlashErasePage(EE_PAGE0_BASE) != HAL_OK)
|
||||
return EE_STATUS_ERROR;
|
||||
|
||||
if (EE_FlashErasePage(EE_PAGE1_BASE) != HAL_OK)
|
||||
return EE_STATUS_ERROR;
|
||||
|
||||
if (EE_FlashProgramHalfWord(EE_PAGE0_BASE, EE_PAGE_STATUS_VALID) != HAL_OK)
|
||||
return EE_STATUS_ERROR;
|
||||
|
||||
/* PAGE1 will remain erased (status = 0xFFFF) */
|
||||
EE_ActivePageBase = EE_PAGE0_BASE;
|
||||
|
||||
return EE_STATUS_OK;
|
||||
}
|
||||
|
||||
/* Get the base of the other page */
|
||||
static uint32_t EE_GetOtherPageBase(uint32_t pageBase)
|
||||
{
|
||||
return (pageBase == EE_PAGE0_BASE) ? EE_PAGE1_BASE : EE_PAGE0_BASE;
|
||||
}
|
||||
|
||||
/* Page transfer (garbage collection + new write) */
|
||||
static EE_Status EE_PageTransfer(uint16_t VirtAddress, uint16_t Data)
|
||||
{
|
||||
uint32_t oldBase = EE_ActivePageBase;
|
||||
uint32_t newBase = EE_GetOtherPageBase(oldBase);
|
||||
uint32_t addr;
|
||||
uint16_t value;
|
||||
EE_Status st;
|
||||
|
||||
/* Erase new page */
|
||||
if (EE_FlashErasePage(newBase) != HAL_OK)
|
||||
return EE_STATUS_ERROR;
|
||||
|
||||
/* Mark new page as RECEIVE */
|
||||
if (EE_FlashProgramHalfWord(newBase, EE_PAGE_STATUS_RECEIVE) != HAL_OK)
|
||||
return EE_STATUS_ERROR;
|
||||
|
||||
/* Start writing records just after status */
|
||||
addr = newBase + 2U;
|
||||
|
||||
/* For each known virtual variable */
|
||||
for (uint16_t i = 0; i < EE_NUM_VIRTUAL_ADDR; i++)
|
||||
{
|
||||
uint16_t vaddr = EE_VirtAddrs[i];
|
||||
|
||||
if (vaddr == VirtAddress)
|
||||
{
|
||||
/* Use the new data passed into PageTransfer */
|
||||
value = Data;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Read latest value from old active page */
|
||||
st = EE_FindInPage(oldBase, vaddr, &value);
|
||||
if (st != EE_STATUS_OK)
|
||||
{
|
||||
/* Variable never written -> skip */
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
/* Write record to new page */
|
||||
if (EE_FlashProgramHalfWord(addr, vaddr) != HAL_OK)
|
||||
return EE_STATUS_ERROR;
|
||||
if (EE_FlashProgramHalfWord(addr + 2U, value) != HAL_OK)
|
||||
return EE_STATUS_ERROR;
|
||||
|
||||
addr += sizeof(EE_Record_t);
|
||||
if (addr >= (newBase + EE_PAGE_SIZE))
|
||||
return EE_STATUS_NO_SPACE;
|
||||
}
|
||||
|
||||
/* Erase old page */
|
||||
if (EE_FlashErasePage(oldBase) != HAL_OK)
|
||||
return EE_STATUS_ERROR;
|
||||
|
||||
/* Mark new page as VALID */
|
||||
if (EE_FlashProgramHalfWord(newBase, EE_PAGE_STATUS_VALID) != HAL_OK)
|
||||
return EE_STATUS_ERROR;
|
||||
|
||||
/* Update active page */
|
||||
EE_ActivePageBase = newBase;
|
||||
|
||||
return EE_STATUS_OK;
|
||||
}
|
||||
|
||||
/* --- Public API ----------------------------------------------------------- */
|
||||
|
||||
/*
|
||||
* Initialize the EEPROM emulation.
|
||||
* - Checks page statuses and chooses the active page.
|
||||
* - If inconsistent or blank, formats pages.
|
||||
* PUBLIC RETURN TYPE: uint16_t (EE_OK / EE_ERROR / ...)
|
||||
*/
|
||||
uint16_t EE_Init(void)
|
||||
{
|
||||
uint16_t status0 = EE_GetPageStatus(EE_PAGE0_BASE);
|
||||
uint16_t status1 = EE_GetPageStatus(EE_PAGE1_BASE);
|
||||
|
||||
if ((status0 == EE_PAGE_STATUS_ERASED) && (status1 == EE_PAGE_STATUS_ERASED))
|
||||
{
|
||||
/* Fresh device -> format */
|
||||
return (uint16_t)EE_Format();
|
||||
}
|
||||
else if ((status0 == EE_PAGE_STATUS_VALID) && (status1 == EE_PAGE_STATUS_ERASED))
|
||||
{
|
||||
EE_ActivePageBase = EE_PAGE0_BASE;
|
||||
return EE_OK;
|
||||
}
|
||||
else if ((status1 == EE_PAGE_STATUS_VALID) && (status0 == EE_PAGE_STATUS_ERASED))
|
||||
{
|
||||
EE_ActivePageBase = EE_PAGE1_BASE;
|
||||
return EE_OK;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Any weird or inconsistent state -> reformat */
|
||||
return (uint16_t)EE_Format();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Read a 16-bit variable by its virtual address.
|
||||
* PUBLIC RETURN: EE_OK / EE_NOT_FOUND / EE_ERROR (as uint16_t)
|
||||
*/
|
||||
uint16_t EE_ReadVariable(uint16_t VirtAddress, uint16_t *Data)
|
||||
{
|
||||
EE_Status st;
|
||||
|
||||
if (Data == NULL)
|
||||
return EE_ERROR;
|
||||
|
||||
st = EE_FindInPage(EE_ActivePageBase, VirtAddress, Data);
|
||||
return (uint16_t)st;
|
||||
}
|
||||
|
||||
/*
|
||||
* Write (append) a 16-bit variable.
|
||||
* - Writes a new record in the active page.
|
||||
* - If the page is full, triggers a page transfer (GC).
|
||||
* PUBLIC RETURN: EE_OK / EE_ERROR / EE_NO_SPACE (as uint16_t)
|
||||
*/
|
||||
uint16_t EE_WriteVariable(uint16_t VirtAddress, uint16_t Data)
|
||||
{
|
||||
uint32_t freeAddr;
|
||||
EE_Status st;
|
||||
|
||||
/* Find free space in active page */
|
||||
freeAddr = EE_FindFreeAddress(EE_ActivePageBase);
|
||||
if (freeAddr != 0U)
|
||||
{
|
||||
/* Write new record */
|
||||
if (EE_FlashProgramHalfWord(freeAddr, VirtAddress) != HAL_OK)
|
||||
return EE_ERROR;
|
||||
if (EE_FlashProgramHalfWord(freeAddr + 2U, Data) != HAL_OK)
|
||||
return EE_ERROR;
|
||||
|
||||
return EE_OK;
|
||||
}
|
||||
|
||||
/* No space -> page transfer */
|
||||
st = EE_PageTransfer(VirtAddress, Data);
|
||||
return (uint16_t)st;
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
|
||||
#include "usb_device.h"
|
||||
|
||||
#include "usbd_cdc_if.h"
|
||||
#include "cdc_int.h"
|
||||
@@ -0,0 +1,193 @@
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include "usb_device.h"
|
||||
|
||||
#include "usbd_cdc_if.h"
|
||||
#include "cdc_int.h"
|
||||
#include "stm32f1xx_hal.h"
|
||||
#include "eeprom.h"
|
||||
#include "pwm.h"
|
||||
|
||||
extern TIM_HandleTypeDef htim1;
|
||||
extern TIM_HandleTypeDef htim2;
|
||||
extern TIM_HandleTypeDef htim3;
|
||||
extern TIM_HandleTypeDef htim4;
|
||||
|
||||
bool toHex(char c1,char c2,char c3,char c4,uint16_t* retval){
|
||||
if((c1>='0')&&(c1<='9')){
|
||||
c1-='0';
|
||||
}else if((c1>='a')&&(c1<='f')){
|
||||
c1=(c1-'a')+10;
|
||||
}else return false;
|
||||
|
||||
if((c2>='0')&&(c2<='9')){
|
||||
c2-='0';
|
||||
}else if((c2>='a')&&(c2<='f')){
|
||||
c2=(c2-'a')+10;
|
||||
}else return false;
|
||||
|
||||
if((c3>='0')&&(c3<='9')){
|
||||
c3-='0';
|
||||
}else if((c3>='a')&&(c3<='f')){
|
||||
c3=(c3-'a')+10;
|
||||
}else return false;
|
||||
|
||||
if((c4>='0')&&(c4<='9')){
|
||||
c4-='0';
|
||||
}else if((c4>='a')&&(c4<='f')){
|
||||
c4=(c4-'a')+10;
|
||||
}else return false;
|
||||
|
||||
*retval=c1;
|
||||
*retval*=16;
|
||||
*retval+=c2;
|
||||
*retval*=16;
|
||||
*retval+=c3;
|
||||
*retval*=16;
|
||||
*retval+=c4;
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
void manageCDC(void){
|
||||
static uint8_t rxbuf[100];
|
||||
static uint8_t rxidx=0;
|
||||
unsigned char s[100];
|
||||
uint8_t mot,dir,dm,m,c,d,u;
|
||||
uint16_t val;
|
||||
uint16_t eeadd;
|
||||
uint8_t i;
|
||||
|
||||
if (CDC_Available()) {
|
||||
int rx = CDC_ReadByte();
|
||||
if (rx < 0) return;
|
||||
//uint8_t out = (uint8_t)c;
|
||||
//if (out >= 'a' && out <= 'z') out -= 32; // to upper
|
||||
rxbuf[rxidx]=rx;
|
||||
while (CDC_Transmit_FS(&rxbuf[rxidx], 1) == USBD_BUSY);//echo
|
||||
if((rx==0x0d)||(rx==0x0a)){
|
||||
if(rxidx){
|
||||
switch(rxbuf[0]){
|
||||
case 'i':
|
||||
sprintf((char*)s,"\ninfo");
|
||||
while (CDC_Transmit_FS(s, 5) == USBD_BUSY);
|
||||
sprintf((char*)s,"\npwm %d%d%d%d%d%d%d%d",IsPwmRunning(timMot1,FWMot1),IsPwmRunning(timMot1,BWMot1),IsPwmRunning(timMot2,FWMot2),IsPwmRunning(timMot2,BWMot2),IsPwmRunning(timMot3,FWMot3),IsPwmRunning(timMot3,BWMot3),IsPwmRunning(timMot4,FWMot4),IsPwmRunning(timMot4,BWMot4));
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
__HAL_TIM_GET_COMPARE(&htim3, TIM_CHANNEL_1);
|
||||
sprintf((char*)s,"\n0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx",(uint32_t)__HAL_TIM_GET_COMPARE(timMot1,FWMot1),(uint32_t)__HAL_TIM_GET_COMPARE(timMot1,BWMot1),(uint32_t)__HAL_TIM_GET_COMPARE(timMot2,FWMot2),(uint32_t)__HAL_TIM_GET_COMPARE(timMot2,BWMot2),(uint32_t)__HAL_TIM_GET_COMPARE(timMot3,FWMot3),(uint32_t)__HAL_TIM_GET_COMPARE(timMot3,BWMot3),(uint32_t)__HAL_TIM_GET_COMPARE(timMot4,FWMot4),(uint32_t)__HAL_TIM_GET_COMPARE(timMot4,BWMot4));
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
break;
|
||||
case 'm':
|
||||
if(rxidx==8){
|
||||
if((rxbuf[1]>='1')&&(rxbuf[1]<='4')){
|
||||
mot=rxbuf[1]-='0';
|
||||
if(rxbuf[2]=='f'){
|
||||
dir=FW;
|
||||
}else if(rxbuf[2]=='b'){
|
||||
dir=BW;
|
||||
}else{
|
||||
sprintf((char*)s,"\n?mnsvvvvv s=f|b");//m nmotore senso valore
|
||||
while (CDC_Transmit_FS(s, 16) == USBD_BUSY);
|
||||
break;
|
||||
}
|
||||
if(((rxbuf[3]>='0')&&(rxbuf[3]<='9'))&&((rxbuf[4]>='0')&&(rxbuf[4]<='9'))&&((rxbuf[5]>='0')&&(rxbuf[5]<='9'))&&((rxbuf[6]>='0')&&(rxbuf[6]<='9'))&&((rxbuf[7]>='0')&&(rxbuf[7]<='9'))){
|
||||
dm=rxbuf[3]-='0';
|
||||
m=rxbuf[4]-='0';
|
||||
c=rxbuf[5]-='0';
|
||||
d=rxbuf[6]-='0';
|
||||
u=rxbuf[7]-='0';
|
||||
val=dm;
|
||||
val*=10;
|
||||
val+=m;
|
||||
val*=10;
|
||||
val+=c;
|
||||
val*=10;
|
||||
val+=d;
|
||||
val*=10;
|
||||
val+=u;
|
||||
SetMot(mot,dir,val);
|
||||
}else{
|
||||
sprintf((char*)s,"\n?mnsvvvvv 00000>=vvvvv<=99999");//m nmotore senso valore
|
||||
while (CDC_Transmit_FS(s, 19) == USBD_BUSY);
|
||||
}
|
||||
|
||||
}else{
|
||||
sprintf((char*)s,"\n?mnsvvvvv 1>=m<=4");//m nmotore senso valore
|
||||
while (CDC_Transmit_FS(s, 19) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?mnsvvvvv");//m nmotore senso valore
|
||||
while (CDC_Transmit_FS(s, 10) == USBD_BUSY);
|
||||
}
|
||||
break;
|
||||
case 'e':
|
||||
if(rxidx>1){
|
||||
if(rxbuf[1]=='r'){
|
||||
if(rxidx==4){
|
||||
if(toHex('0','0',rxbuf[2],rxbuf[3],&eeadd)){
|
||||
EEW_Read(eeadd, &val);
|
||||
sprintf((char*)s,"\nee 0x%02x=0x%04x",eeadd,val);
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}else{
|
||||
sprintf((char*)s,"\n?eraa hex values");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?eraa");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else if(rxbuf[1]=='w'){
|
||||
if(rxidx==8){
|
||||
if(toHex('0','0',rxbuf[2],rxbuf[3],&eeadd)){
|
||||
if(toHex(rxbuf[4],rxbuf[5],rxbuf[6],rxbuf[7],&val)){
|
||||
EEW_Write(eeadd, val);
|
||||
sprintf((char*)s,"\ndone");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}else{
|
||||
sprintf((char*)s,"\n?ewaavvvv hex values");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?ewaa hex values");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?ewaavvvv");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else if(rxbuf[1]=='d'){
|
||||
if(rxidx==2){
|
||||
for(i=0;i<32;i++){
|
||||
if((i%4)==0){
|
||||
sprintf((char*)s,"\n");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
EEW_Read(i, &val);
|
||||
sprintf((char*)s,"0x%02x=0x%04x ",i,val);
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?ed");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?er|w|d");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?eraa | ewaavvvv");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
sprintf((char*)s,"\n?");
|
||||
while (CDC_Transmit_FS(s, 2) == USBD_BUSY);
|
||||
break;
|
||||
}
|
||||
}
|
||||
rxidx=0;
|
||||
}else rxidx++;
|
||||
// tiny spin or yield
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
|
||||
#include "usb_device.h"
|
||||
|
||||
#include "usbd_cdc_if.h"
|
||||
#include "cdc_int.h"
|
||||
|
||||
void manageCDC(void){
|
||||
if (CDC_Available()) {
|
||||
int c = CDC_ReadByte();
|
||||
//if (c < 0) break;
|
||||
|
||||
// uint8_t out = (uint8_t)c;
|
||||
// if (out >= 'a' && out <= 'z') out -= 32; // to upper
|
||||
|
||||
unsigned char s[100];
|
||||
sprintf((char*)s,"\nc=%03d",c);
|
||||
while (CDC_Transmit_FS(s, 6) == USBD_BUSY) {
|
||||
// tiny spin or yield
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,197 @@
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include "usb_device.h"
|
||||
|
||||
#include "usbd_cdc_if.h"
|
||||
#include "cdc_int.h"
|
||||
#include "stm32f1xx_hal.h"
|
||||
#include "eeprom.h"
|
||||
#include "pwm.h"
|
||||
|
||||
extern TIM_HandleTypeDef htim1;
|
||||
extern TIM_HandleTypeDef htim2;
|
||||
extern TIM_HandleTypeDef htim3;
|
||||
extern TIM_HandleTypeDef htim4;
|
||||
extern const uint16_t EE_VirtAddrs[];
|
||||
|
||||
bool toHex(char c1,char c2,char c3,char c4,uint16_t* retval){
|
||||
if((c1>='0')&&(c1<='9')){
|
||||
c1-='0';
|
||||
}else if((c1>='a')&&(c1<='f')){
|
||||
c1=(c1-'a')+10;
|
||||
}else return false;
|
||||
|
||||
if((c2>='0')&&(c2<='9')){
|
||||
c2-='0';
|
||||
}else if((c2>='a')&&(c2<='f')){
|
||||
c2=(c2-'a')+10;
|
||||
}else return false;
|
||||
|
||||
if((c3>='0')&&(c3<='9')){
|
||||
c3-='0';
|
||||
}else if((c3>='a')&&(c3<='f')){
|
||||
c3=(c3-'a')+10;
|
||||
}else return false;
|
||||
|
||||
if((c4>='0')&&(c4<='9')){
|
||||
c4-='0';
|
||||
}else if((c4>='a')&&(c4<='f')){
|
||||
c4=(c4-'a')+10;
|
||||
}else return false;
|
||||
|
||||
*retval=c1;
|
||||
*retval*=16;
|
||||
*retval+=c2;
|
||||
*retval*=16;
|
||||
*retval+=c3;
|
||||
*retval*=16;
|
||||
*retval+=c4;
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
void manageCDC(void){
|
||||
static uint8_t rxbuf[100];
|
||||
static uint8_t rxidx=0;
|
||||
unsigned char s[100];
|
||||
uint8_t mot,dir,dm,m,c,d,u;
|
||||
uint16_t val;
|
||||
uint16_t st;
|
||||
uint16_t eeadd;
|
||||
uint8_t i;
|
||||
|
||||
if (CDC_Available()) {
|
||||
int rx = CDC_ReadByte();
|
||||
if (rx < 0) return;
|
||||
//uint8_t out = (uint8_t)c;
|
||||
//if (out >= 'a' && out <= 'z') out -= 32; // to upper
|
||||
rxbuf[rxidx]=rx;
|
||||
while (CDC_Transmit_FS(&rxbuf[rxidx], 1) == USBD_BUSY);//echo
|
||||
if((rx==0x0d)||(rx==0x0a)){
|
||||
if(rxidx){
|
||||
switch(rxbuf[0]){
|
||||
case 'i':
|
||||
sprintf((char*)s,"\ninfo");
|
||||
while (CDC_Transmit_FS(s, 5) == USBD_BUSY);
|
||||
sprintf((char*)s,"\npwm %d%d%d%d%d%d%d%d",IsPwmRunning(timMot1,FWMot1),IsPwmRunning(timMot1,BWMot1),IsPwmRunning(timMot2,FWMot2),IsPwmRunning(timMot2,BWMot2),IsPwmRunning(timMot3,FWMot3),IsPwmRunning(timMot3,BWMot3),IsPwmRunning(timMot4,FWMot4),IsPwmRunning(timMot4,BWMot4));
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
__HAL_TIM_GET_COMPARE(&htim3, TIM_CHANNEL_1);
|
||||
sprintf((char*)s,"\n0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx",(uint32_t)__HAL_TIM_GET_COMPARE(timMot1,FWMot1),(uint32_t)__HAL_TIM_GET_COMPARE(timMot1,BWMot1),(uint32_t)__HAL_TIM_GET_COMPARE(timMot2,FWMot2),(uint32_t)__HAL_TIM_GET_COMPARE(timMot2,BWMot2),(uint32_t)__HAL_TIM_GET_COMPARE(timMot3,FWMot3),(uint32_t)__HAL_TIM_GET_COMPARE(timMot3,BWMot3),(uint32_t)__HAL_TIM_GET_COMPARE(timMot4,FWMot4),(uint32_t)__HAL_TIM_GET_COMPARE(timMot4,BWMot4));
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
break;
|
||||
case 'm':
|
||||
if(rxidx==8){
|
||||
if((rxbuf[1]>='1')&&(rxbuf[1]<='4')){
|
||||
mot=rxbuf[1]-='0';
|
||||
if(rxbuf[2]=='f'){
|
||||
dir=FW;
|
||||
}else if(rxbuf[2]=='b'){
|
||||
dir=BW;
|
||||
}else{
|
||||
sprintf((char*)s,"\n?mnsvvvvv s=f|b");//m nmotore senso valore
|
||||
while (CDC_Transmit_FS(s, 16) == USBD_BUSY);
|
||||
break;
|
||||
}
|
||||
if(((rxbuf[3]>='0')&&(rxbuf[3]<='9'))&&((rxbuf[4]>='0')&&(rxbuf[4]<='9'))&&((rxbuf[5]>='0')&&(rxbuf[5]<='9'))&&((rxbuf[6]>='0')&&(rxbuf[6]<='9'))&&((rxbuf[7]>='0')&&(rxbuf[7]<='9'))){
|
||||
dm=rxbuf[3]-='0';
|
||||
m=rxbuf[4]-='0';
|
||||
c=rxbuf[5]-='0';
|
||||
d=rxbuf[6]-='0';
|
||||
u=rxbuf[7]-='0';
|
||||
val=dm;
|
||||
val*=10;
|
||||
val+=m;
|
||||
val*=10;
|
||||
val+=c;
|
||||
val*=10;
|
||||
val+=d;
|
||||
val*=10;
|
||||
val+=u;
|
||||
SetMot(mot,dir,val);
|
||||
}else{
|
||||
sprintf((char*)s,"\n?mnsvvvvv 00000>=vvvvv<=99999");//m nmotore senso valore
|
||||
while (CDC_Transmit_FS(s, 19) == USBD_BUSY);
|
||||
}
|
||||
|
||||
}else{
|
||||
sprintf((char*)s,"\n?mnsvvvvv 1>=m<=4");//m nmotore senso valore
|
||||
while (CDC_Transmit_FS(s, 19) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?mnsvvvvv");//m nmotore senso valore
|
||||
while (CDC_Transmit_FS(s, 10) == USBD_BUSY);
|
||||
}
|
||||
break;
|
||||
case 'e':
|
||||
if(rxidx>1){
|
||||
if(rxbuf[1]=='r'){
|
||||
if(rxidx==4){
|
||||
if(toHex('0','0',rxbuf[2],rxbuf[3],&eeadd)){
|
||||
st=EEW_Read(EE_VirtAddrse[eeadd], &val);
|
||||
if (st == EE_OK){
|
||||
sprintf((char*)s,"\nee 0x%02x=0x%04x",eeadd,val);
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}else{
|
||||
sprintf((char*)s,"\nee read error %d",st);
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?eraa hex values");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?eraa");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else if(rxbuf[1]=='w'){
|
||||
if(rxidx==8){
|
||||
if(toHex('0','0',rxbuf[2],rxbuf[3],&eeadd)){
|
||||
if(toHex(rxbuf[4],rxbuf[5],rxbuf[6],rxbuf[7],&val)){
|
||||
st=EEW_Write(EE_VirtAddrse[eeadd], val);
|
||||
if (st == EE_OK)sprintf((char*)s,"\ndone 0x%02x=0x%04x",eeadd,val);
|
||||
else sprintf((char*)s,"\nee write error %d", st);
|
||||
}else sprintf((char*)s,"\n?ewaavvvv hex values");
|
||||
}else sprintf((char*)s,"\n?ewaa hex values");
|
||||
}else sprintf((char*)s,"\n?ewaavvvv");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}else if(rxbuf[1]=='d'){
|
||||
if(rxidx==2){
|
||||
for(i=0;i<32;i++){
|
||||
if((i%4)==0){
|
||||
sprintf((char*)s,"\n");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
st=EEW_Read(EE_VirtAddrse[i], &val);
|
||||
if (st == EE_OK){
|
||||
sprintf((char*)s,"0x%02x=0x%04x ",i,val);
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}else{
|
||||
sprintf((char*)s,"rderr %d ",st);
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?ed");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?er|w|d");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?eraa | ewaavvvv");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
sprintf((char*)s,"\n?");
|
||||
while (CDC_Transmit_FS(s, 2) == USBD_BUSY);
|
||||
break;
|
||||
}
|
||||
}
|
||||
rxidx=0;
|
||||
}else rxidx++;
|
||||
// tiny spin or yield
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,193 @@
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include "usb_device.h"
|
||||
|
||||
#include "usbd_cdc_if.h"
|
||||
#include "cdc_int.h"
|
||||
#include "stm32f1xx_hal.h"
|
||||
#include "eeprom.h"
|
||||
#include "pwm.h"
|
||||
|
||||
extern TIM_HandleTypeDef htim1;
|
||||
extern TIM_HandleTypeDef htim2;
|
||||
extern TIM_HandleTypeDef htim3;
|
||||
extern TIM_HandleTypeDef htim4;
|
||||
|
||||
bool toHex(char c1,char c2,char c3,char c4,uint16_t* retval){
|
||||
if((c1>='0')&&(c1<='9')){
|
||||
c1-='0';
|
||||
}else if((c1>='a')&&(c1<='f')){
|
||||
c1=(c1-'a')+10;
|
||||
}else return false;
|
||||
|
||||
if((c2>='0')&&(c2<='9')){
|
||||
c2-='0';
|
||||
}else if((c2>='a')&&(c2<='f')){
|
||||
c2=(c2-'a')+10;
|
||||
}else return false;
|
||||
|
||||
if((c3>='0')&&(c3<='9')){
|
||||
c3-='0';
|
||||
}else if((c3>='a')&&(c3<='f')){
|
||||
c3=(c3-'a')+10;
|
||||
}else return false;
|
||||
|
||||
if((c4>='0')&&(c4<='9')){
|
||||
c4-='0';
|
||||
}else if((c4>='a')&&(c4<='f')){
|
||||
c4=(c4-'a')+10;
|
||||
}else return false;
|
||||
|
||||
*retval=c1;
|
||||
*retval*=16;
|
||||
*retval+=c2;
|
||||
*retval*=16;
|
||||
*retval+=c3;
|
||||
*retval*=16;
|
||||
*retval+=c4;
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
void manageCDC(void){
|
||||
static uint8_t rxbuf[100];
|
||||
static uint8_t rxidx=0;
|
||||
unsigned char s[100];
|
||||
uint8_t mot,dir,dm,m,c,d,u;
|
||||
uint16_t val;
|
||||
uint16_t eeadd;
|
||||
uint8_t i;
|
||||
|
||||
if (CDC_Available()) {
|
||||
int rx = CDC_ReadByte();
|
||||
if (rx < 0) return;
|
||||
//uint8_t out = (uint8_t)c;
|
||||
//if (out >= 'a' && out <= 'z') out -= 32; // to upper
|
||||
rxbuf[rxidx]=rx;
|
||||
while (CDC_Transmit_FS(&rxbuf[rxidx], 1) == USBD_BUSY);//echo
|
||||
if((rx==0x0d)||(rx==0x0a)){
|
||||
if(rxidx){
|
||||
switch(rxbuf[0]){
|
||||
case 'i':
|
||||
sprintf((char*)s,"\ninfo");
|
||||
while (CDC_Transmit_FS(s, 5) == USBD_BUSY);
|
||||
sprintf((char*)s,"\npwm %d%d%d%d%d%d%d%d",IsPwmRunning(timMot1,FWMot1),IsPwmRunning(timMot1,BWMot1),IsPwmRunning(timMot2,FWMot2),IsPwmRunning(timMot2,BWMot2),IsPwmRunning(timMot3,FWMot3),IsPwmRunning(timMot3,BWMot3),IsPwmRunning(timMot4,FWMot4),IsPwmRunning(timMot4,BWMot4));
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
__HAL_TIM_GET_COMPARE(&htim3, TIM_CHANNEL_1);
|
||||
sprintf((char*)s,"\n0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx",(uint32_t)__HAL_TIM_GET_COMPARE(timMot1,FWMot1),(uint32_t)__HAL_TIM_GET_COMPARE(timMot1,BWMot1),(uint32_t)__HAL_TIM_GET_COMPARE(timMot2,FWMot2),(uint32_t)__HAL_TIM_GET_COMPARE(timMot2,BWMot2),(uint32_t)__HAL_TIM_GET_COMPARE(timMot3,FWMot3),(uint32_t)__HAL_TIM_GET_COMPARE(timMot3,BWMot3),(uint32_t)__HAL_TIM_GET_COMPARE(timMot4,FWMot4),(uint32_t)__HAL_TIM_GET_COMPARE(timMot4,BWMot4));
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
break;
|
||||
case 'm':
|
||||
if(rxidx==8){
|
||||
if((rxbuf[1]>='1')&&(rxbuf[1]<='4')){
|
||||
mot=rxbuf[1]-='0';
|
||||
if(rxbuf[2]=='f'){
|
||||
dir=FW;
|
||||
}else if(rxbuf[2]=='b'){
|
||||
dir=BW;
|
||||
}else{
|
||||
sprintf((char*)s,"\n?mnsvvvvv s=f|b");//m nmotore senso valore
|
||||
while (CDC_Transmit_FS(s, 16) == USBD_BUSY);
|
||||
break;
|
||||
}
|
||||
if(((rxbuf[3]>='0')&&(rxbuf[3]<='9'))&&((rxbuf[4]>='0')&&(rxbuf[4]<='9'))&&((rxbuf[5]>='0')&&(rxbuf[5]<='9'))&&((rxbuf[6]>='0')&&(rxbuf[6]<='9'))&&((rxbuf[7]>='0')&&(rxbuf[7]<='9'))){
|
||||
dm=rxbuf[3]-='0';
|
||||
m=rxbuf[4]-='0';
|
||||
c=rxbuf[5]-='0';
|
||||
d=rxbuf[6]-='0';
|
||||
u=rxbuf[7]-='0';
|
||||
val=dm;
|
||||
val*=10;
|
||||
val+=m;
|
||||
val*=10;
|
||||
val+=c;
|
||||
val*=10;
|
||||
val+=d;
|
||||
val*=10;
|
||||
val+=u;
|
||||
SetMot(mot,dir,val);
|
||||
}else{
|
||||
sprintf((char*)s,"\n?mnsvvvvv 00000>=vvvvv<=99999");//m nmotore senso valore
|
||||
while (CDC_Transmit_FS(s, 19) == USBD_BUSY);
|
||||
}
|
||||
|
||||
}else{
|
||||
sprintf((char*)s,"\n?mnsvvvvv 1>=m<=4");//m nmotore senso valore
|
||||
while (CDC_Transmit_FS(s, 19) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?mnsvvvvv");//m nmotore senso valore
|
||||
while (CDC_Transmit_FS(s, 10) == USBD_BUSY);
|
||||
}
|
||||
break;
|
||||
case 'e':
|
||||
if(rxidx>1){
|
||||
if(rxbuf[1]=='r'){
|
||||
if(rxidx==4){
|
||||
if(toHex('0','0',rxbuf[2],rxbuf[3],&eeadd)){
|
||||
EEW_Read(eeadd, &val);
|
||||
sprintf((char*)s,"\nee 0x%02x=0x%04x",eeadd,val);
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}else{
|
||||
sprintf((char*)s,"\n?eraa hex values");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?eraa");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else if(rxbuf[1]=='w'){
|
||||
if(rxidx==8){
|
||||
if(toHex('0','0',rxbuf[2],rxbuf[3],&eeadd)){
|
||||
if(toHex(rxbuf[4],rxbuf[5],rxbuf[6],rxbuf[7],&val)){
|
||||
EEW_Write(eeadd, val);
|
||||
sprintf((char*)s,"\ndone 0x%02x=0x%04x",eeadd,val);
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}else{
|
||||
sprintf((char*)s,"\n?ewaavvvv hex values");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?ewaa hex values");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?ewaavvvv");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else if(rxbuf[1]=='d'){
|
||||
if(rxidx==2){
|
||||
for(i=0;i<32;i++){
|
||||
if((i%4)==0){
|
||||
sprintf((char*)s,"\n");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
EEW_Read(i, &val);
|
||||
sprintf((char*)s,"0x%02x=0x%04x ",i,val);
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?ed");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?er|w|d");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?eraa | ewaavvvv");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
sprintf((char*)s,"\n?");
|
||||
while (CDC_Transmit_FS(s, 2) == USBD_BUSY);
|
||||
break;
|
||||
}
|
||||
}
|
||||
rxidx=0;
|
||||
}else rxidx++;
|
||||
// tiny spin or yield
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,180 @@
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include "usb_device.h"
|
||||
|
||||
#include "usbd_cdc_if.h"
|
||||
#include "cdc_int.h"
|
||||
#include "stm32f1xx_hal.h"
|
||||
#include "eeprom.h"
|
||||
#include "pwm.h"
|
||||
|
||||
extern TIM_HandleTypeDef htim1;
|
||||
extern TIM_HandleTypeDef htim2;
|
||||
extern TIM_HandleTypeDef htim3;
|
||||
extern TIM_HandleTypeDef htim4;
|
||||
|
||||
bool toHex(char c1,char c2,char c3,char c4,uint16_t* retval){
|
||||
if((c1>='0')&&(c1<='9')){
|
||||
c1-='0';
|
||||
}else if((c1>='a')&&(c1<='f')){
|
||||
c1=(c1-'a')+10;
|
||||
}else return false;
|
||||
|
||||
if((c2>='0')&&(c2<='9')){
|
||||
c2-='0';
|
||||
}else if((c2>='a')&&(c2<='f')){
|
||||
c2=(c2-'a')+10;
|
||||
}else return false;
|
||||
|
||||
if((c3>='0')&&(c3<='9')){
|
||||
c3-='0';
|
||||
}else if((c3>='a')&&(c3<='f')){
|
||||
c3=(c3-'a')+10;
|
||||
}else return false;
|
||||
|
||||
if((c4>='0')&&(c4<='9')){
|
||||
c4-='0';
|
||||
}else if((c4>='a')&&(c4<='f')){
|
||||
c4=(c4-'a')+10;
|
||||
}else return false;
|
||||
|
||||
*retval=c1;
|
||||
*retval*=16;
|
||||
*retval+=c2;
|
||||
*retval*=16;
|
||||
*retval+=c3;
|
||||
*retval*=16;
|
||||
*retval+=c4;
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
void manageCDC(void){
|
||||
static uint8_t rxbuf[100];
|
||||
static uint8_t rxidx=0;
|
||||
unsigned char s[100];
|
||||
uint8_t mot,dir,dm,m,c,d,u;
|
||||
uint16_t val;
|
||||
uint16_t eeadd;
|
||||
|
||||
if (CDC_Available()) {
|
||||
int rx = CDC_ReadByte();
|
||||
if (rx < 0) return;
|
||||
//uint8_t out = (uint8_t)c;
|
||||
//if (out >= 'a' && out <= 'z') out -= 32; // to upper
|
||||
rxbuf[rxidx]=rx;
|
||||
while (CDC_Transmit_FS(&rxbuf[rxidx], 1) == USBD_BUSY);//echo
|
||||
if((rx==0x0d)||(rx==0x0a)){
|
||||
if(rxidx){
|
||||
switch(rxbuf[0]){
|
||||
case 'i':
|
||||
sprintf((char*)s,"\ninfo");
|
||||
while (CDC_Transmit_FS(s, 5) == USBD_BUSY);
|
||||
sprintf((char*)s,"\npwm %d%d%d%d%d%d%d%d",IsPwmRunning(timMot1,FWMot1),IsPwmRunning(timMot1,BWMot1),IsPwmRunning(timMot2,FWMot2),IsPwmRunning(timMot2,BWMot2),IsPwmRunning(timMot3,FWMot3),IsPwmRunning(timMot3,BWMot3),IsPwmRunning(timMot4,FWMot4),IsPwmRunning(timMot4,BWMot4));
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
__HAL_TIM_GET_COMPARE(&htim3, TIM_CHANNEL_1);
|
||||
sprintf((char*)s,"\n0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx",(uint32_t)__HAL_TIM_GET_COMPARE(timMot1,FWMot1),(uint32_t)__HAL_TIM_GET_COMPARE(timMot1,BWMot1),(uint32_t)__HAL_TIM_GET_COMPARE(timMot2,FWMot2),(uint32_t)__HAL_TIM_GET_COMPARE(timMot2,BWMot2),(uint32_t)__HAL_TIM_GET_COMPARE(timMot3,FWMot3),(uint32_t)__HAL_TIM_GET_COMPARE(timMot3,BWMot3),(uint32_t)__HAL_TIM_GET_COMPARE(timMot4,FWMot4),(uint32_t)__HAL_TIM_GET_COMPARE(timMot4,BWMot4));
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
break;
|
||||
case 'm':
|
||||
if(rxidx==8){
|
||||
if((rxbuf[1]>='1')&&(rxbuf[1]<='4')){
|
||||
mot=rxbuf[1]-='0';
|
||||
if(rxbuf[2]=='f'){
|
||||
dir=FW;
|
||||
}else if(rxbuf[2]=='b'){
|
||||
dir=BW;
|
||||
}else{
|
||||
sprintf((char*)s,"\n?mnsvvvvv s=f|b");//m nmotore senso valore
|
||||
while (CDC_Transmit_FS(s, 16) == USBD_BUSY);
|
||||
break;
|
||||
}
|
||||
if(((rxbuf[3]>='0')&&(rxbuf[3]<='9'))&&((rxbuf[4]>='0')&&(rxbuf[4]<='9'))&&((rxbuf[5]>='0')&&(rxbuf[5]<='9'))&&((rxbuf[6]>='0')&&(rxbuf[6]<='9'))&&((rxbuf[7]>='0')&&(rxbuf[7]<='9'))){
|
||||
dm=rxbuf[3]-='0';
|
||||
m=rxbuf[4]-='0';
|
||||
c=rxbuf[5]-='0';
|
||||
d=rxbuf[6]-='0';
|
||||
u=rxbuf[7]-='0';
|
||||
val=dm;
|
||||
val*=10;
|
||||
val+=m;
|
||||
val*=10;
|
||||
val+=c;
|
||||
val*=10;
|
||||
val+=d;
|
||||
val*=10;
|
||||
val+=u;
|
||||
SetMot(mot,dir,val);
|
||||
}else{
|
||||
sprintf((char*)s,"\n?mnsvvvvv 00000>=vvvvv<=99999");//m nmotore senso valore
|
||||
while (CDC_Transmit_FS(s, 19) == USBD_BUSY);
|
||||
}
|
||||
|
||||
}else{
|
||||
sprintf((char*)s,"\n?mnsvvvvv 1>=m<=4");//m nmotore senso valore
|
||||
while (CDC_Transmit_FS(s, 19) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?mnsvvvvv");//m nmotore senso valore
|
||||
while (CDC_Transmit_FS(s, 10) == USBD_BUSY);
|
||||
}
|
||||
break;
|
||||
case 'e':
|
||||
if(rxidx>1){
|
||||
if(rxbuf[1]=='r'){
|
||||
if(rxidx==4){
|
||||
if(toHex('0','0',rxbuf[2],rxbuf[3],&eeadd)){
|
||||
EEW_Read(eeadd, &val);
|
||||
sprintf((char*)s,"\nee 0x%02x=%04x",eeadd,val);
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}else{
|
||||
sprintf((char*)s,"\n?eraa hex values");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?eraa");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else if(rxbuf[1]=='w'){
|
||||
if(rxidx==8){
|
||||
if(toHex('0','0',rxbuf[2],rxbuf[3],&eeadd)){
|
||||
if(toHex(rxbuf[4],rxbuf[5],rxbuf[6],rxbuf[7],&val)){
|
||||
EEW_Write(eeadd, val);
|
||||
sprintf((char*)s,"\ndone");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}else{
|
||||
sprintf((char*)s,"\n?ewaavvvv hex values");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?ewaa hex values");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?ewaavvvv");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else if(rxbuf[1]=='w'){
|
||||
if(rxidx==8){
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?er|w|d");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?eraa | ewaavvvv");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
sprintf((char*)s,"\n?");
|
||||
while (CDC_Transmit_FS(s, 2) == USBD_BUSY);
|
||||
break;
|
||||
}
|
||||
}
|
||||
rxidx=0;
|
||||
}else rxidx++;
|
||||
// tiny spin or yield
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,296 @@
|
||||
#include "eeprom.h"
|
||||
|
||||
/*
|
||||
* Record format (4 bytes):
|
||||
* [0] VirtAddress (uint16_t)
|
||||
* [2] Data (uint16_t)
|
||||
*
|
||||
* Page layout:
|
||||
* [0] PageStatus (uint16_t)
|
||||
* [2..] Records...
|
||||
*/
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint16_t VirtAddress;
|
||||
uint16_t Data;
|
||||
} EE_Record_t;
|
||||
|
||||
/* Active page base address (runtime selected in EE_Init) */
|
||||
static uint32_t EE_ActivePageBase = EE_PAGE0_BASE;
|
||||
|
||||
/* 32 sequential virtual addresses */
|
||||
const uint16_t EE_VirtAddrs[EE_NUM_VIRTUAL_ADDR] =
|
||||
{
|
||||
0x0001, 0x0002, 0x0003, 0x0004,
|
||||
0x0005, 0x0006, 0x0007, 0x0008,
|
||||
0x0009, 0x000A, 0x000B, 0x000C,
|
||||
0x000D, 0x000E, 0x000F, 0x0010,
|
||||
0x0011, 0x0012, 0x0013, 0x0014,
|
||||
0x0015, 0x0016, 0x0017, 0x0018,
|
||||
0x0019, 0x001A, 0x001B, 0x001C,
|
||||
0x001D, 0x001E, 0x001F, 0x0020
|
||||
};
|
||||
/* ========================================================================= */
|
||||
|
||||
/* --- Internal helpers ---------------------------------------------------- */
|
||||
|
||||
static uint16_t EE_GetPageStatus(uint32_t pageBase)
|
||||
{
|
||||
return *(__IO uint16_t *)pageBase;
|
||||
}
|
||||
|
||||
static HAL_StatusTypeDef EE_FlashProgramHalfWord(uint32_t Address, uint16_t Data)
|
||||
{
|
||||
HAL_StatusTypeDef status;
|
||||
|
||||
HAL_FLASH_Unlock();
|
||||
status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, Address, Data);
|
||||
HAL_FLASH_Lock();
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
static HAL_StatusTypeDef EE_FlashErasePage(uint32_t PageAddress)
|
||||
{
|
||||
HAL_StatusTypeDef status;
|
||||
FLASH_EraseInitTypeDef EraseInit;
|
||||
uint32_t PageError = 0;
|
||||
|
||||
HAL_FLASH_Unlock();
|
||||
|
||||
EraseInit.TypeErase = FLASH_TYPEERASE_PAGES;
|
||||
EraseInit.PageAddress = PageAddress;
|
||||
EraseInit.NbPages = 1;
|
||||
|
||||
status = HAL_FLASHEx_Erase(&EraseInit, &PageError);
|
||||
|
||||
HAL_FLASH_Lock();
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Find first free record address in given page (returns 0 if full) */
|
||||
static uint32_t EE_FindFreeAddress(uint32_t pageBase)
|
||||
{
|
||||
uint32_t addr = pageBase + 2U; /* Skip status word */
|
||||
uint32_t pageEnd = pageBase + EE_PAGE_SIZE;
|
||||
|
||||
while (addr < (pageEnd - sizeof(EE_Record_t) + 1U))
|
||||
{
|
||||
uint16_t vaddr = *(__IO uint16_t *)addr;
|
||||
|
||||
if (vaddr == 0xFFFFU)
|
||||
{
|
||||
/* Empty slot */
|
||||
return addr;
|
||||
}
|
||||
addr += sizeof(EE_Record_t);
|
||||
}
|
||||
|
||||
return 0U; /* No space */
|
||||
}
|
||||
|
||||
/* Find latest value of VirtAddress in a specific page (internal, uses EE_Status) */
|
||||
static EE_Status EE_FindInPage(uint32_t pageBase, uint16_t VirtAddress, uint16_t *Data)
|
||||
{
|
||||
uint32_t pageEnd = pageBase + EE_PAGE_SIZE;
|
||||
uint32_t addr;
|
||||
|
||||
if (Data == NULL)
|
||||
return EE_ERROR;
|
||||
|
||||
/* Start from last possible record and go backwards */
|
||||
/* Start from last possible record and go backwards */
|
||||
for (addr = pageEnd - sizeof(EE_Record_t);
|
||||
addr >= pageBase + 2U; // *** FIX HERE ***
|
||||
addr -= sizeof(EE_Record_t))
|
||||
{
|
||||
uint16_t vaddr = *(__IO uint16_t *)addr;
|
||||
uint16_t value = *(__IO uint16_t *)(addr + 2U);
|
||||
|
||||
if (vaddr == 0xFFFFU)
|
||||
{
|
||||
/* Empty slot, skip */
|
||||
continue;
|
||||
}
|
||||
|
||||
if (vaddr == VirtAddress)
|
||||
{
|
||||
*Data = value;
|
||||
return EE_OK;
|
||||
}
|
||||
}
|
||||
|
||||
return EE_NOT_FOUND;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* Format both pages: erase and set PAGE0 as VALID */
|
||||
static EE_Status EE_Format(void)
|
||||
{
|
||||
if (EE_FlashErasePage(EE_PAGE0_BASE) != HAL_OK)
|
||||
return EE_STATUS_ERROR;
|
||||
|
||||
if (EE_FlashErasePage(EE_PAGE1_BASE) != HAL_OK)
|
||||
return EE_STATUS_ERROR;
|
||||
|
||||
if (EE_FlashProgramHalfWord(EE_PAGE0_BASE, EE_PAGE_STATUS_VALID) != HAL_OK)
|
||||
return EE_STATUS_ERROR;
|
||||
|
||||
/* PAGE1 will remain erased (status = 0xFFFF) */
|
||||
EE_ActivePageBase = EE_PAGE0_BASE;
|
||||
|
||||
return EE_STATUS_OK;
|
||||
}
|
||||
|
||||
/* Get the base of the other page */
|
||||
static uint32_t EE_GetOtherPageBase(uint32_t pageBase)
|
||||
{
|
||||
return (pageBase == EE_PAGE0_BASE) ? EE_PAGE1_BASE : EE_PAGE0_BASE;
|
||||
}
|
||||
|
||||
/* Page transfer (garbage collection + new write) */
|
||||
static EE_Status EE_PageTransfer(uint16_t VirtAddress, uint16_t Data)
|
||||
{
|
||||
uint32_t oldBase = EE_ActivePageBase;
|
||||
uint32_t newBase = EE_GetOtherPageBase(oldBase);
|
||||
uint32_t addr;
|
||||
uint16_t value;
|
||||
EE_Status st;
|
||||
|
||||
/* Erase new page */
|
||||
if (EE_FlashErasePage(newBase) != HAL_OK)
|
||||
return EE_STATUS_ERROR;
|
||||
|
||||
/* Mark new page as RECEIVE */
|
||||
if (EE_FlashProgramHalfWord(newBase, EE_PAGE_STATUS_RECEIVE) != HAL_OK)
|
||||
return EE_STATUS_ERROR;
|
||||
|
||||
/* Start writing records just after status */
|
||||
addr = newBase + 2U;
|
||||
|
||||
/* For each known virtual variable */
|
||||
for (uint16_t i = 0; i < EE_NUM_VIRTUAL_ADDR; i++)
|
||||
{
|
||||
uint16_t vaddr = EE_VirtAddrs[i];
|
||||
|
||||
if (vaddr == VirtAddress)
|
||||
{
|
||||
/* Use the new data passed into PageTransfer */
|
||||
value = Data;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Read latest value from old active page */
|
||||
st = EE_FindInPage(oldBase, vaddr, &value);
|
||||
if (st != EE_STATUS_OK)
|
||||
{
|
||||
/* Variable never written -> skip */
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
/* Write record to new page */
|
||||
if (EE_FlashProgramHalfWord(addr, vaddr) != HAL_OK)
|
||||
return EE_STATUS_ERROR;
|
||||
if (EE_FlashProgramHalfWord(addr + 2U, value) != HAL_OK)
|
||||
return EE_STATUS_ERROR;
|
||||
|
||||
addr += sizeof(EE_Record_t);
|
||||
if (addr >= (newBase + EE_PAGE_SIZE))
|
||||
return EE_STATUS_NO_SPACE;
|
||||
}
|
||||
|
||||
/* Erase old page */
|
||||
if (EE_FlashErasePage(oldBase) != HAL_OK)
|
||||
return EE_STATUS_ERROR;
|
||||
|
||||
/* Mark new page as VALID */
|
||||
if (EE_FlashProgramHalfWord(newBase, EE_PAGE_STATUS_VALID) != HAL_OK)
|
||||
return EE_STATUS_ERROR;
|
||||
|
||||
/* Update active page */
|
||||
EE_ActivePageBase = newBase;
|
||||
|
||||
return EE_STATUS_OK;
|
||||
}
|
||||
|
||||
/* --- Public API ----------------------------------------------------------- */
|
||||
|
||||
/*
|
||||
* Initialize the EEPROM emulation.
|
||||
* - Checks page statuses and chooses the active page.
|
||||
* - If inconsistent or blank, formats pages.
|
||||
* PUBLIC RETURN TYPE: uint16_t (EE_OK / EE_ERROR / ...)
|
||||
*/
|
||||
uint16_t EE_Init(void)
|
||||
{
|
||||
uint16_t status0 = EE_GetPageStatus(EE_PAGE0_BASE);
|
||||
uint16_t status1 = EE_GetPageStatus(EE_PAGE1_BASE);
|
||||
|
||||
if ((status0 == EE_PAGE_STATUS_ERASED) && (status1 == EE_PAGE_STATUS_ERASED))
|
||||
{
|
||||
/* Fresh device -> format */
|
||||
return (uint16_t)EE_Format();
|
||||
}
|
||||
else if ((status0 == EE_PAGE_STATUS_VALID) && (status1 == EE_PAGE_STATUS_ERASED))
|
||||
{
|
||||
EE_ActivePageBase = EE_PAGE0_BASE;
|
||||
return EE_OK;
|
||||
}
|
||||
else if ((status1 == EE_PAGE_STATUS_VALID) && (status0 == EE_PAGE_STATUS_ERASED))
|
||||
{
|
||||
EE_ActivePageBase = EE_PAGE1_BASE;
|
||||
return EE_OK;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Any weird or inconsistent state -> reformat */
|
||||
return (uint16_t)EE_Format();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Read a 16-bit variable by its virtual address.
|
||||
* PUBLIC RETURN: EE_OK / EE_NOT_FOUND / EE_ERROR (as uint16_t)
|
||||
*/
|
||||
uint16_t EE_ReadVariable(uint16_t VirtAddress, uint16_t *Data)
|
||||
{
|
||||
EE_Status st;
|
||||
|
||||
if (Data == NULL)
|
||||
return EE_ERROR;
|
||||
|
||||
st = EE_FindInPage(EE_ActivePageBase, VirtAddress, Data);
|
||||
return (uint16_t)st;
|
||||
}
|
||||
|
||||
/*
|
||||
* Write (append) a 16-bit variable.
|
||||
* - Writes a new record in the active page.
|
||||
* - If the page is full, triggers a page transfer (GC).
|
||||
* PUBLIC RETURN: EE_OK / EE_ERROR / EE_NO_SPACE (as uint16_t)
|
||||
*/
|
||||
uint16_t EE_WriteVariable(uint16_t VirtAddress, uint16_t Data)
|
||||
{
|
||||
uint32_t freeAddr;
|
||||
EE_Status st;
|
||||
|
||||
/* Find free space in active page */
|
||||
freeAddr = EE_FindFreeAddress(EE_ActivePageBase);
|
||||
if (freeAddr != 0U)
|
||||
{
|
||||
/* Write new record */
|
||||
if (EE_FlashProgramHalfWord(freeAddr, VirtAddress) != HAL_OK)
|
||||
return EE_ERROR;
|
||||
if (EE_FlashProgramHalfWord(freeAddr + 2U, Data) != HAL_OK)
|
||||
return EE_ERROR;
|
||||
|
||||
return EE_OK;
|
||||
}
|
||||
|
||||
/* No space -> page transfer */
|
||||
st = EE_PageTransfer(VirtAddress, Data);
|
||||
return (uint16_t)st;
|
||||
}
|
||||
@@ -1,2 +0,0 @@
|
||||
|
||||
#include "cdc_int.h"
|
||||
@@ -0,0 +1,197 @@
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include "usb_device.h"
|
||||
|
||||
#include "usbd_cdc_if.h"
|
||||
#include "cdc_int.h"
|
||||
#include "stm32f1xx_hal.h"
|
||||
#include "eeprom.h"
|
||||
#include "pwm.h"
|
||||
|
||||
extern TIM_HandleTypeDef htim1;
|
||||
extern TIM_HandleTypeDef htim2;
|
||||
extern TIM_HandleTypeDef htim3;
|
||||
extern TIM_HandleTypeDef htim4;
|
||||
|
||||
bool toHex(char c1,char c2,char c3,char c4,uint16_t* retval){
|
||||
if((c1>='0')&&(c1<='9')){
|
||||
c1-='0';
|
||||
}else if((c1>='a')&&(c1<='f')){
|
||||
c1=(c1-'a')+10;
|
||||
}else return false;
|
||||
|
||||
if((c2>='0')&&(c2<='9')){
|
||||
c2-='0';
|
||||
}else if((c2>='a')&&(c2<='f')){
|
||||
c2=(c2-'a')+10;
|
||||
}else return false;
|
||||
|
||||
if((c3>='0')&&(c3<='9')){
|
||||
c3-='0';
|
||||
}else if((c3>='a')&&(c3<='f')){
|
||||
c3=(c3-'a')+10;
|
||||
}else return false;
|
||||
|
||||
if((c4>='0')&&(c4<='9')){
|
||||
c4-='0';
|
||||
}else if((c4>='a')&&(c4<='f')){
|
||||
c4=(c4-'a')+10;
|
||||
}else return false;
|
||||
|
||||
*retval=c1;
|
||||
*retval*=16;
|
||||
*retval+=c2;
|
||||
*retval*=16;
|
||||
*retval+=c3;
|
||||
*retval*=16;
|
||||
*retval+=c4;
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
void manageCDC(void){
|
||||
static uint8_t rxbuf[100];
|
||||
static uint8_t rxidx=0;
|
||||
unsigned char s[100];
|
||||
uint8_t mot,dir,dm,m,c,d,u;
|
||||
uint16_t val;
|
||||
uint16_t st;
|
||||
uint16_t eeadd;
|
||||
uint8_t i;
|
||||
|
||||
if (CDC_Available()) {
|
||||
int rx = CDC_ReadByte();
|
||||
if (rx < 0) return;
|
||||
//uint8_t out = (uint8_t)c;
|
||||
//if (out >= 'a' && out <= 'z') out -= 32; // to upper
|
||||
rxbuf[rxidx]=rx;
|
||||
while (CDC_Transmit_FS(&rxbuf[rxidx], 1) == USBD_BUSY);//echo
|
||||
if((rx==0x0d)||(rx==0x0a)){
|
||||
if(rxidx){
|
||||
switch(rxbuf[0]){
|
||||
case 'i':
|
||||
sprintf((char*)s,"\ninfo");
|
||||
while (CDC_Transmit_FS(s, 5) == USBD_BUSY);
|
||||
sprintf((char*)s,"\npwm %d%d%d%d%d%d%d%d",IsPwmRunning(timMot1,FWMot1),IsPwmRunning(timMot1,BWMot1),IsPwmRunning(timMot2,FWMot2),IsPwmRunning(timMot2,BWMot2),IsPwmRunning(timMot3,FWMot3),IsPwmRunning(timMot3,BWMot3),IsPwmRunning(timMot4,FWMot4),IsPwmRunning(timMot4,BWMot4));
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
__HAL_TIM_GET_COMPARE(&htim3, TIM_CHANNEL_1);
|
||||
sprintf((char*)s,"\n0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx",(uint32_t)__HAL_TIM_GET_COMPARE(timMot1,FWMot1),(uint32_t)__HAL_TIM_GET_COMPARE(timMot1,BWMot1),(uint32_t)__HAL_TIM_GET_COMPARE(timMot2,FWMot2),(uint32_t)__HAL_TIM_GET_COMPARE(timMot2,BWMot2),(uint32_t)__HAL_TIM_GET_COMPARE(timMot3,FWMot3),(uint32_t)__HAL_TIM_GET_COMPARE(timMot3,BWMot3),(uint32_t)__HAL_TIM_GET_COMPARE(timMot4,FWMot4),(uint32_t)__HAL_TIM_GET_COMPARE(timMot4,BWMot4));
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
break;
|
||||
case 'm':
|
||||
if(rxidx==8){
|
||||
if((rxbuf[1]>='1')&&(rxbuf[1]<='4')){
|
||||
mot=rxbuf[1]-='0';
|
||||
if(rxbuf[2]=='f'){
|
||||
dir=FW;
|
||||
}else if(rxbuf[2]=='b'){
|
||||
dir=BW;
|
||||
}else{
|
||||
sprintf((char*)s,"\n?mnsvvvvv s=f|b");//m nmotore senso valore
|
||||
while (CDC_Transmit_FS(s, 16) == USBD_BUSY);
|
||||
break;
|
||||
}
|
||||
if(((rxbuf[3]>='0')&&(rxbuf[3]<='9'))&&((rxbuf[4]>='0')&&(rxbuf[4]<='9'))&&((rxbuf[5]>='0')&&(rxbuf[5]<='9'))&&((rxbuf[6]>='0')&&(rxbuf[6]<='9'))&&((rxbuf[7]>='0')&&(rxbuf[7]<='9'))){
|
||||
dm=rxbuf[3]-='0';
|
||||
m=rxbuf[4]-='0';
|
||||
c=rxbuf[5]-='0';
|
||||
d=rxbuf[6]-='0';
|
||||
u=rxbuf[7]-='0';
|
||||
val=dm;
|
||||
val*=10;
|
||||
val+=m;
|
||||
val*=10;
|
||||
val+=c;
|
||||
val*=10;
|
||||
val+=d;
|
||||
val*=10;
|
||||
val+=u;
|
||||
SetMot(mot,dir,val);
|
||||
}else{
|
||||
sprintf((char*)s,"\n?mnsvvvvv 00000>=vvvvv<=99999");//m nmotore senso valore
|
||||
while (CDC_Transmit_FS(s, 19) == USBD_BUSY);
|
||||
}
|
||||
|
||||
}else{
|
||||
sprintf((char*)s,"\n?mnsvvvvv 1>=m<=4");//m nmotore senso valore
|
||||
while (CDC_Transmit_FS(s, 19) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?mnsvvvvv");//m nmotore senso valore
|
||||
while (CDC_Transmit_FS(s, 10) == USBD_BUSY);
|
||||
}
|
||||
break;
|
||||
case 'e':
|
||||
if(rxidx>1){
|
||||
if(rxbuf[1]=='r'){
|
||||
if(rxidx==4){
|
||||
if(toHex('0','0',rxbuf[2],rxbuf[3],&eeadd)){
|
||||
st=EEW_Read(EE_VirtAddrse[eadd], &val);
|
||||
if (st == EE_OK){
|
||||
sprintf((char*)s,"\nee 0x%02x=0x%04x",eeadd,val);
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}else{
|
||||
sprintf((char*)s,"\nee read error %d",st);
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?eraa hex values");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?eraa");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else if(rxbuf[1]=='w'){
|
||||
if(rxidx==8){
|
||||
if(toHex('0','0',rxbuf[2],rxbuf[3],&eeadd)){
|
||||
if(toHex(rxbuf[4],rxbuf[5],rxbuf[6],rxbuf[7],&val)){
|
||||
st=EEW_Write(EE_VirtAddrse[eadd], val);
|
||||
if (st == EE_OK)sprintf((char*)s,"\ndone 0x%02x=0x%04x",eeadd,val);
|
||||
else sprintf((char*)s,"\nee write error %d", st);
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}else sprintf((char*)s,"\n?ewaavvvv hex values");
|
||||
}else sprintf((char*)s,"\n?ewaa hex values");
|
||||
}else sprintf((char*)s,"\n?ewaavvvv");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}else if(rxbuf[1]=='d'){
|
||||
if(rxidx==2){
|
||||
for(i=0;i<32;i++){
|
||||
if((i%4)==0){
|
||||
sprintf((char*)s,"\n");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
st=EEW_Read(EE_VirtAddrse[i], &val);
|
||||
if (st == EE_OK){
|
||||
sprintf((char*)s,"0x%02x=0x%04x ",i,val);
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}else{
|
||||
sprintf((char*)s,"rderr %d ",st);
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?ed");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?er|w|d");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?eraa | ewaavvvv");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
sprintf((char*)s,"\n?");
|
||||
while (CDC_Transmit_FS(s, 2) == USBD_BUSY);
|
||||
break;
|
||||
}
|
||||
}
|
||||
rxidx=0;
|
||||
}else rxidx++;
|
||||
// tiny spin or yield
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,291 @@
|
||||
#include "eeprom.h"
|
||||
|
||||
/*
|
||||
* Record format (4 bytes):
|
||||
* [0] VirtAddress (uint16_t)
|
||||
* [2] Data (uint16_t)
|
||||
*
|
||||
* Page layout:
|
||||
* [0] PageStatus (uint16_t)
|
||||
* [2..] Records...
|
||||
*/
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint16_t VirtAddress;
|
||||
uint16_t Data;
|
||||
} EE_Record_t;
|
||||
|
||||
/* Active page base address (runtime selected in EE_Init) */
|
||||
static uint32_t EE_ActivePageBase = EE_PAGE0_BASE;
|
||||
|
||||
/* 32 sequential virtual addresses */
|
||||
const uint16_t EE_VirtAddrs[EE_NUM_VIRTUAL_ADDR] =
|
||||
{
|
||||
0x0001, 0x0002, 0x0003, 0x0004,
|
||||
0x0005, 0x0006, 0x0007, 0x0008,
|
||||
0x0009, 0x000A, 0x000B, 0x000C,
|
||||
0x000D, 0x000E, 0x000F, 0x0010,
|
||||
0x0011, 0x0012, 0x0013, 0x0014,
|
||||
0x0015, 0x0016, 0x0017, 0x0018,
|
||||
0x0019, 0x001A, 0x001B, 0x001C,
|
||||
0x001D, 0x001E, 0x001F, 0x0020
|
||||
};
|
||||
/* ========================================================================= */
|
||||
|
||||
/* --- Internal helpers ---------------------------------------------------- */
|
||||
|
||||
static uint16_t EE_GetPageStatus(uint32_t pageBase)
|
||||
{
|
||||
return *(__IO uint16_t *)pageBase;
|
||||
}
|
||||
|
||||
static HAL_StatusTypeDef EE_FlashProgramHalfWord(uint32_t Address, uint16_t Data)
|
||||
{
|
||||
HAL_StatusTypeDef status;
|
||||
|
||||
HAL_FLASH_Unlock();
|
||||
status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, Address, Data);
|
||||
HAL_FLASH_Lock();
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
static HAL_StatusTypeDef EE_FlashErasePage(uint32_t PageAddress)
|
||||
{
|
||||
HAL_StatusTypeDef status;
|
||||
FLASH_EraseInitTypeDef EraseInit;
|
||||
uint32_t PageError = 0;
|
||||
|
||||
HAL_FLASH_Unlock();
|
||||
|
||||
EraseInit.TypeErase = FLASH_TYPEERASE_PAGES;
|
||||
EraseInit.PageAddress = PageAddress;
|
||||
EraseInit.NbPages = 1;
|
||||
|
||||
status = HAL_FLASHEx_Erase(&EraseInit, &PageError);
|
||||
|
||||
HAL_FLASH_Lock();
|
||||
return status;
|
||||
}
|
||||
|
||||
/* Find first free record address in given page (returns 0 if full) */
|
||||
static uint32_t EE_FindFreeAddress(uint32_t pageBase)
|
||||
{
|
||||
uint32_t addr = pageBase + 2U; /* Skip status word */
|
||||
uint32_t pageEnd = pageBase + EE_PAGE_SIZE;
|
||||
|
||||
while (addr < (pageEnd - sizeof(EE_Record_t) + 1U))
|
||||
{
|
||||
uint16_t vaddr = *(__IO uint16_t *)addr;
|
||||
|
||||
if (vaddr == 0xFFFFU)
|
||||
{
|
||||
/* Empty slot */
|
||||
return addr;
|
||||
}
|
||||
addr += sizeof(EE_Record_t);
|
||||
}
|
||||
|
||||
return 0U; /* No space */
|
||||
}
|
||||
|
||||
/* Find latest value of VirtAddress in a specific page (internal, uses EE_Status) */
|
||||
static EE_Status EE_FindInPage(uint32_t pageBase, uint16_t VirtAddress, uint16_t *Data)
|
||||
{
|
||||
uint32_t pageEnd = pageBase + EE_PAGE_SIZE;
|
||||
uint32_t addr;
|
||||
|
||||
if (Data == NULL)
|
||||
return EE_STATUS_ERROR;
|
||||
|
||||
/* Start from last possible record and go backwards */
|
||||
for (addr = pageEnd - sizeof(EE_Record_t); addr > pageBase + 1U; addr -= sizeof(EE_Record_t))
|
||||
{
|
||||
uint16_t vaddr = *(__IO uint16_t *)addr;
|
||||
uint16_t value = *(__IO uint16_t *)(addr + 2U);
|
||||
|
||||
if (vaddr == 0xFFFFU)
|
||||
{
|
||||
/* Empty slot, skip */
|
||||
continue;
|
||||
}
|
||||
|
||||
if (vaddr == VirtAddress)
|
||||
{
|
||||
*Data = value;
|
||||
return EE_STATUS_OK;
|
||||
}
|
||||
}
|
||||
|
||||
return EE_STATUS_NOT_FOUND;
|
||||
}
|
||||
|
||||
/* Format both pages: erase and set PAGE0 as VALID */
|
||||
static EE_Status EE_Format(void)
|
||||
{
|
||||
if (EE_FlashErasePage(EE_PAGE0_BASE) != HAL_OK)
|
||||
return EE_STATUS_ERROR;
|
||||
|
||||
if (EE_FlashErasePage(EE_PAGE1_BASE) != HAL_OK)
|
||||
return EE_STATUS_ERROR;
|
||||
|
||||
if (EE_FlashProgramHalfWord(EE_PAGE0_BASE, EE_PAGE_STATUS_VALID) != HAL_OK)
|
||||
return EE_STATUS_ERROR;
|
||||
|
||||
/* PAGE1 will remain erased (status = 0xFFFF) */
|
||||
EE_ActivePageBase = EE_PAGE0_BASE;
|
||||
|
||||
return EE_STATUS_OK;
|
||||
}
|
||||
|
||||
/* Get the base of the other page */
|
||||
static uint32_t EE_GetOtherPageBase(uint32_t pageBase)
|
||||
{
|
||||
return (pageBase == EE_PAGE0_BASE) ? EE_PAGE1_BASE : EE_PAGE0_BASE;
|
||||
}
|
||||
|
||||
/* Page transfer (garbage collection + new write) */
|
||||
static EE_Status EE_PageTransfer(uint16_t VirtAddress, uint16_t Data)
|
||||
{
|
||||
uint32_t oldBase = EE_ActivePageBase;
|
||||
uint32_t newBase = EE_GetOtherPageBase(oldBase);
|
||||
uint32_t addr;
|
||||
uint16_t value;
|
||||
EE_Status st;
|
||||
|
||||
/* Erase new page */
|
||||
if (EE_FlashErasePage(newBase) != HAL_OK)
|
||||
return EE_STATUS_ERROR;
|
||||
|
||||
/* Mark new page as RECEIVE */
|
||||
if (EE_FlashProgramHalfWord(newBase, EE_PAGE_STATUS_RECEIVE) != HAL_OK)
|
||||
return EE_STATUS_ERROR;
|
||||
|
||||
/* Start writing records just after status */
|
||||
addr = newBase + 2U;
|
||||
|
||||
/* For each known virtual variable */
|
||||
for (uint16_t i = 0; i < EE_NUM_VIRTUAL_ADDR; i++)
|
||||
{
|
||||
uint16_t vaddr = EE_VirtAddrs[i];
|
||||
|
||||
if (vaddr == VirtAddress)
|
||||
{
|
||||
/* Use the new data passed into PageTransfer */
|
||||
value = Data;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Read latest value from old active page */
|
||||
st = EE_FindInPage(oldBase, vaddr, &value);
|
||||
if (st != EE_STATUS_OK)
|
||||
{
|
||||
/* Variable never written -> skip */
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
/* Write record to new page */
|
||||
if (EE_FlashProgramHalfWord(addr, vaddr) != HAL_OK)
|
||||
return EE_STATUS_ERROR;
|
||||
if (EE_FlashProgramHalfWord(addr + 2U, value) != HAL_OK)
|
||||
return EE_STATUS_ERROR;
|
||||
|
||||
addr += sizeof(EE_Record_t);
|
||||
if (addr >= (newBase + EE_PAGE_SIZE))
|
||||
return EE_STATUS_NO_SPACE;
|
||||
}
|
||||
|
||||
/* Erase old page */
|
||||
if (EE_FlashErasePage(oldBase) != HAL_OK)
|
||||
return EE_STATUS_ERROR;
|
||||
|
||||
/* Mark new page as VALID */
|
||||
if (EE_FlashProgramHalfWord(newBase, EE_PAGE_STATUS_VALID) != HAL_OK)
|
||||
return EE_STATUS_ERROR;
|
||||
|
||||
/* Update active page */
|
||||
EE_ActivePageBase = newBase;
|
||||
|
||||
return EE_STATUS_OK;
|
||||
}
|
||||
|
||||
/* --- Public API ----------------------------------------------------------- */
|
||||
|
||||
/*
|
||||
* Initialize the EEPROM emulation.
|
||||
* - Checks page statuses and chooses the active page.
|
||||
* - If inconsistent or blank, formats pages.
|
||||
* PUBLIC RETURN TYPE: uint16_t (EE_OK / EE_ERROR / ...)
|
||||
*/
|
||||
uint16_t EE_Init(void)
|
||||
{
|
||||
uint16_t status0 = EE_GetPageStatus(EE_PAGE0_BASE);
|
||||
uint16_t status1 = EE_GetPageStatus(EE_PAGE1_BASE);
|
||||
|
||||
if ((status0 == EE_PAGE_STATUS_ERASED) && (status1 == EE_PAGE_STATUS_ERASED))
|
||||
{
|
||||
/* Fresh device -> format */
|
||||
return (uint16_t)EE_Format();
|
||||
}
|
||||
else if ((status0 == EE_PAGE_STATUS_VALID) && (status1 == EE_PAGE_STATUS_ERASED))
|
||||
{
|
||||
EE_ActivePageBase = EE_PAGE0_BASE;
|
||||
return EE_OK;
|
||||
}
|
||||
else if ((status1 == EE_PAGE_STATUS_VALID) && (status0 == EE_PAGE_STATUS_ERASED))
|
||||
{
|
||||
EE_ActivePageBase = EE_PAGE1_BASE;
|
||||
return EE_OK;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Any weird or inconsistent state -> reformat */
|
||||
return (uint16_t)EE_Format();
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Read a 16-bit variable by its virtual address.
|
||||
* PUBLIC RETURN: EE_OK / EE_NOT_FOUND / EE_ERROR (as uint16_t)
|
||||
*/
|
||||
uint16_t EE_ReadVariable(uint16_t VirtAddress, uint16_t *Data)
|
||||
{
|
||||
EE_Status st;
|
||||
|
||||
if (Data == NULL)
|
||||
return EE_ERROR;
|
||||
|
||||
st = EE_FindInPage(EE_ActivePageBase, VirtAddress, Data);
|
||||
return (uint16_t)st;
|
||||
}
|
||||
|
||||
/*
|
||||
* Write (append) a 16-bit variable.
|
||||
* - Writes a new record in the active page.
|
||||
* - If the page is full, triggers a page transfer (GC).
|
||||
* PUBLIC RETURN: EE_OK / EE_ERROR / EE_NO_SPACE (as uint16_t)
|
||||
*/
|
||||
uint16_t EE_WriteVariable(uint16_t VirtAddress, uint16_t Data)
|
||||
{
|
||||
uint32_t freeAddr;
|
||||
EE_Status st;
|
||||
|
||||
/* Find free space in active page */
|
||||
freeAddr = EE_FindFreeAddress(EE_ActivePageBase);
|
||||
if (freeAddr != 0U)
|
||||
{
|
||||
/* Write new record */
|
||||
if (EE_FlashProgramHalfWord(freeAddr, VirtAddress) != HAL_OK)
|
||||
return EE_ERROR;
|
||||
if (EE_FlashProgramHalfWord(freeAddr + 2U, Data) != HAL_OK)
|
||||
return EE_ERROR;
|
||||
|
||||
return EE_OK;
|
||||
}
|
||||
|
||||
/* No space -> page transfer */
|
||||
st = EE_PageTransfer(VirtAddress, Data);
|
||||
return (uint16_t)st;
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include "usb_device.h"
|
||||
|
||||
#include "usbd_cdc_if.h"
|
||||
#include "cdc_int.h"
|
||||
#include "stm32f1xx_hal.h"
|
||||
#include "eeprom.h"
|
||||
#include "pwm.h"
|
||||
|
||||
extern TIM_HandleTypeDef htim1;
|
||||
extern TIM_HandleTypeDef htim2;
|
||||
extern TIM_HandleTypeDef htim3;
|
||||
extern TIM_HandleTypeDef htim4;
|
||||
|
||||
bool toHex(char c1,char c2,char c3,char c4,uint16_t* retval){
|
||||
if((c1>='0')&&(c1<='9')){
|
||||
c1-='0';
|
||||
}else if((c1>='a')&&(c1<='f')){
|
||||
c1=(c1-'a')+10;
|
||||
}else return false;
|
||||
|
||||
if((c2>='0')&&(c2<='9')){
|
||||
c2-='0';
|
||||
}else if((c2>='a')&&(c2<='f')){
|
||||
c2=(c2-'a')+10;
|
||||
}else return false;
|
||||
|
||||
if((c3>='0')&&(c3<='9')){
|
||||
c3-='0';
|
||||
}else if((c3>='a')&&(c3<='f')){
|
||||
c3=(c3-'a')+10;
|
||||
}else return false;
|
||||
|
||||
if((c4>='0')&&(c4<='9')){
|
||||
c4-='0';
|
||||
}else if((c4>='a')&&(c4<='f')){
|
||||
c4=(c4-'a')+10;
|
||||
}else return false;
|
||||
|
||||
*retval=c1;
|
||||
*retval*=16;
|
||||
*retval+=c2;
|
||||
*retval*=16;
|
||||
*retval+=c3;
|
||||
*retval*=16;
|
||||
*retval+=c4;
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
void manageCDC(void){
|
||||
static uint8_t rxbuf[100];
|
||||
static uint8_t rxidx=0;
|
||||
unsigned char s[100];
|
||||
uint8_t mot,dir,dm,m,c,d,u;
|
||||
uint16_t val;
|
||||
uint16_t eeadd;
|
||||
|
||||
if (CDC_Available()) {
|
||||
int rx = CDC_ReadByte();
|
||||
if (rx < 0) return;
|
||||
//uint8_t out = (uint8_t)c;
|
||||
//if (out >= 'a' && out <= 'z') out -= 32; // to upper
|
||||
rxbuf[rxidx]=rx;
|
||||
while (CDC_Transmit_FS(&rxbuf[rxidx], 1) == USBD_BUSY);//echo
|
||||
if((rx==0x0d)||(rx==0x0a)){
|
||||
if(rxidx){
|
||||
switch(rxbuf[0]){
|
||||
case 'i':
|
||||
sprintf((char*)s,"\ninfo");
|
||||
while (CDC_Transmit_FS(s, 5) == USBD_BUSY);
|
||||
sprintf((char*)s,"\npwm %d%d%d%d%d%d%d%d",IsPwmRunning(timMot1,FWMot1),IsPwmRunning(timMot1,BWMot1),IsPwmRunning(timMot2,FWMot2),IsPwmRunning(timMot2,BWMot2),IsPwmRunning(timMot3,FWMot3),IsPwmRunning(timMot3,BWMot3),IsPwmRunning(timMot4,FWMot4),IsPwmRunning(timMot4,BWMot4));
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
__HAL_TIM_GET_COMPARE(&htim3, TIM_CHANNEL_1);
|
||||
sprintf((char*)s,"\n0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx",(uint32_t)__HAL_TIM_GET_COMPARE(timMot1,FWMot1),(uint32_t)__HAL_TIM_GET_COMPARE(timMot1,BWMot1),(uint32_t)__HAL_TIM_GET_COMPARE(timMot2,FWMot2),(uint32_t)__HAL_TIM_GET_COMPARE(timMot2,BWMot2),(uint32_t)__HAL_TIM_GET_COMPARE(timMot3,FWMot3),(uint32_t)__HAL_TIM_GET_COMPARE(timMot3,BWMot3),(uint32_t)__HAL_TIM_GET_COMPARE(timMot4,FWMot4),(uint32_t)__HAL_TIM_GET_COMPARE(timMot4,BWMot4));
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
break;
|
||||
case 'm':
|
||||
if(rxidx==8){
|
||||
if((rxbuf[1]>='1')&&(rxbuf[1]<='4')){
|
||||
mot=rxbuf[1]-='0';
|
||||
if(rxbuf[2]=='f'){
|
||||
dir=FW;
|
||||
}else if(rxbuf[2]=='b'){
|
||||
dir=BW;
|
||||
}else{
|
||||
sprintf((char*)s,"\n?mnsvvvvv s=f|b");//m nmotore senso valore
|
||||
while (CDC_Transmit_FS(s, 16) == USBD_BUSY);
|
||||
break;
|
||||
}
|
||||
if(((rxbuf[3]>='0')&&(rxbuf[3]<='9'))&&((rxbuf[4]>='0')&&(rxbuf[4]<='9'))&&((rxbuf[5]>='0')&&(rxbuf[5]<='9'))&&((rxbuf[6]>='0')&&(rxbuf[6]<='9'))&&((rxbuf[7]>='0')&&(rxbuf[7]<='9'))){
|
||||
dm=rxbuf[3]-='0';
|
||||
m=rxbuf[4]-='0';
|
||||
c=rxbuf[5]-='0';
|
||||
d=rxbuf[6]-='0';
|
||||
u=rxbuf[7]-='0';
|
||||
val=dm;
|
||||
val*=10;
|
||||
val+=m;
|
||||
val*=10;
|
||||
val+=c;
|
||||
val*=10;
|
||||
val+=d;
|
||||
val*=10;
|
||||
val+=u;
|
||||
SetMot(mot,dir,val);
|
||||
}else{
|
||||
sprintf((char*)s,"\n?mnsvvvvv 00000>=vvvvv<=99999");//m nmotore senso valore
|
||||
while (CDC_Transmit_FS(s, 19) == USBD_BUSY);
|
||||
}
|
||||
|
||||
}else{
|
||||
sprintf((char*)s,"\n?mnsvvvvv 1>=m<=4");//m nmotore senso valore
|
||||
while (CDC_Transmit_FS(s, 19) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?mnsvvvvv");//m nmotore senso valore
|
||||
while (CDC_Transmit_FS(s, 10) == USBD_BUSY);
|
||||
}
|
||||
break;
|
||||
case 'e':
|
||||
if(rxidx>1){
|
||||
if(rxbuf[1]=='r'){
|
||||
if(rxidx==4){
|
||||
if(toHex('0','0',rxbuf[2],rxbuf[3],&eeadd)){
|
||||
EEW_Read(eeadd, &val);
|
||||
sprintf((char*)s,"\nee 0x%02x=%04x",eeadd,val);
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}else{
|
||||
sprintf((char*)s,"\n?eraa hex values");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?eraa");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else if(rxbuf[1]=='w'){
|
||||
if(rxidx==8){
|
||||
if(toHex('0','0',rxbuf[2],rxbuf[3],&eeadd)){
|
||||
if(toHex(rxbuf[4],rxbuf[5],rxbuf[6],rxbuf[7],&val)){
|
||||
EEW_Write(eeadd, val);
|
||||
sprintf((char*)s,"\ndone");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}else{
|
||||
sprintf((char*)s,"\n?ewaavvvv hex values");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?ewaa hex values");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?ewaavvvv");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?er|w");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?eraa | ewaavvvv");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
sprintf((char*)s,"\n?");
|
||||
while (CDC_Transmit_FS(s, 2) == USBD_BUSY);
|
||||
break;
|
||||
}
|
||||
}
|
||||
rxidx=0;
|
||||
}else rxidx++;
|
||||
// tiny spin or yield
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,201 @@
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include "usb_device.h"
|
||||
|
||||
#include "usbd_cdc_if.h"
|
||||
#include "cdc_int.h"
|
||||
#include "stm32f1xx_hal.h"
|
||||
#include "eeprom.h"
|
||||
#include "pwm.h"
|
||||
|
||||
extern TIM_HandleTypeDef htim1;
|
||||
extern TIM_HandleTypeDef htim2;
|
||||
extern TIM_HandleTypeDef htim3;
|
||||
extern TIM_HandleTypeDef htim4;
|
||||
|
||||
bool toHex(char c1,char c2,char c3,char c4,uint16_t* retval){
|
||||
if((c1>='0')&&(c1<='9')){
|
||||
c1-='0';
|
||||
}else if((c1>='a')&&(c1<='f')){
|
||||
c1=(c1-'a')+10;
|
||||
}else return false;
|
||||
|
||||
if((c2>='0')&&(c2<='9')){
|
||||
c2-='0';
|
||||
}else if((c2>='a')&&(c2<='f')){
|
||||
c2=(c2-'a')+10;
|
||||
}else return false;
|
||||
|
||||
if((c3>='0')&&(c3<='9')){
|
||||
c3-='0';
|
||||
}else if((c3>='a')&&(c3<='f')){
|
||||
c3=(c3-'a')+10;
|
||||
}else return false;
|
||||
|
||||
if((c4>='0')&&(c4<='9')){
|
||||
c4-='0';
|
||||
}else if((c4>='a')&&(c4<='f')){
|
||||
c4=(c4-'a')+10;
|
||||
}else return false;
|
||||
|
||||
*retval=c1;
|
||||
*retval*=16;
|
||||
*retval+=c2;
|
||||
*retval*=16;
|
||||
*retval+=c3;
|
||||
*retval*=16;
|
||||
*retval+=c4;
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
void manageCDC(void){
|
||||
static uint8_t rxbuf[100];
|
||||
static uint8_t rxidx=0;
|
||||
unsigned char s[100];
|
||||
uint8_t mot,dir,dm,m,c,d,u;
|
||||
uint16_t val;
|
||||
uint16_t eeadd;
|
||||
uint8_t i;
|
||||
|
||||
if (CDC_Available()) {
|
||||
int rx = CDC_ReadByte();
|
||||
if (rx < 0) return;
|
||||
//uint8_t out = (uint8_t)c;
|
||||
//if (out >= 'a' && out <= 'z') out -= 32; // to upper
|
||||
rxbuf[rxidx]=rx;
|
||||
while (CDC_Transmit_FS(&rxbuf[rxidx], 1) == USBD_BUSY);//echo
|
||||
if((rx==0x0d)||(rx==0x0a)){
|
||||
if(rxidx){
|
||||
switch(rxbuf[0]){
|
||||
case 'i':
|
||||
sprintf((char*)s,"\ninfo");
|
||||
while (CDC_Transmit_FS(s, 5) == USBD_BUSY);
|
||||
sprintf((char*)s,"\npwm %d%d%d%d%d%d%d%d",IsPwmRunning(timMot1,FWMot1),IsPwmRunning(timMot1,BWMot1),IsPwmRunning(timMot2,FWMot2),IsPwmRunning(timMot2,BWMot2),IsPwmRunning(timMot3,FWMot3),IsPwmRunning(timMot3,BWMot3),IsPwmRunning(timMot4,FWMot4),IsPwmRunning(timMot4,BWMot4));
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
__HAL_TIM_GET_COMPARE(&htim3, TIM_CHANNEL_1);
|
||||
sprintf((char*)s,"\n0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx",(uint32_t)__HAL_TIM_GET_COMPARE(timMot1,FWMot1),(uint32_t)__HAL_TIM_GET_COMPARE(timMot1,BWMot1),(uint32_t)__HAL_TIM_GET_COMPARE(timMot2,FWMot2),(uint32_t)__HAL_TIM_GET_COMPARE(timMot2,BWMot2),(uint32_t)__HAL_TIM_GET_COMPARE(timMot3,FWMot3),(uint32_t)__HAL_TIM_GET_COMPARE(timMot3,BWMot3),(uint32_t)__HAL_TIM_GET_COMPARE(timMot4,FWMot4),(uint32_t)__HAL_TIM_GET_COMPARE(timMot4,BWMot4));
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
break;
|
||||
case 'm':
|
||||
if(rxidx==8){
|
||||
if((rxbuf[1]>='1')&&(rxbuf[1]<='4')){
|
||||
mot=rxbuf[1]-='0';
|
||||
if(rxbuf[2]=='f'){
|
||||
dir=FW;
|
||||
}else if(rxbuf[2]=='b'){
|
||||
dir=BW;
|
||||
}else{
|
||||
sprintf((char*)s,"\n?mnsvvvvv s=f|b");//m nmotore senso valore
|
||||
while (CDC_Transmit_FS(s, 16) == USBD_BUSY);
|
||||
break;
|
||||
}
|
||||
if(((rxbuf[3]>='0')&&(rxbuf[3]<='9'))&&((rxbuf[4]>='0')&&(rxbuf[4]<='9'))&&((rxbuf[5]>='0')&&(rxbuf[5]<='9'))&&((rxbuf[6]>='0')&&(rxbuf[6]<='9'))&&((rxbuf[7]>='0')&&(rxbuf[7]<='9'))){
|
||||
dm=rxbuf[3]-='0';
|
||||
m=rxbuf[4]-='0';
|
||||
c=rxbuf[5]-='0';
|
||||
d=rxbuf[6]-='0';
|
||||
u=rxbuf[7]-='0';
|
||||
val=dm;
|
||||
val*=10;
|
||||
val+=m;
|
||||
val*=10;
|
||||
val+=c;
|
||||
val*=10;
|
||||
val+=d;
|
||||
val*=10;
|
||||
val+=u;
|
||||
SetMot(mot,dir,val);
|
||||
}else{
|
||||
sprintf((char*)s,"\n?mnsvvvvv 00000>=vvvvv<=99999");//m nmotore senso valore
|
||||
while (CDC_Transmit_FS(s, 19) == USBD_BUSY);
|
||||
}
|
||||
|
||||
}else{
|
||||
sprintf((char*)s,"\n?mnsvvvvv 1>=m<=4");//m nmotore senso valore
|
||||
while (CDC_Transmit_FS(s, 19) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?mnsvvvvv");//m nmotore senso valore
|
||||
while (CDC_Transmit_FS(s, 10) == USBD_BUSY);
|
||||
}
|
||||
break;
|
||||
case 'e':
|
||||
if(rxidx>1){
|
||||
if(rxbuf[1]=='r'){
|
||||
if(rxidx==4){
|
||||
if(toHex('0','0',rxbuf[2],rxbuf[3],&eeadd)){
|
||||
if (EEW_Read(eeadd, &val) == EE_OK){
|
||||
sprintf((char*)s,"\nee 0x%02x=0x%04x",eeadd,val);
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}else{
|
||||
sprintf((char*)s,"\nee read error");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?eraa hex values");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?eraa");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else if(rxbuf[1]=='w'){
|
||||
if(rxidx==8){
|
||||
if(toHex('0','0',rxbuf[2],rxbuf[3],&eeadd)){
|
||||
if(toHex(rxbuf[4],rxbuf[5],rxbuf[6],rxbuf[7],&val)){
|
||||
EEW_Write(eeadd, val);
|
||||
sprintf((char*)s,"\ndone 0x%02x=0x%04x",eeadd,val);
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}else{
|
||||
sprintf((char*)s,"\n?ewaavvvv hex values");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?ewaa hex values");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?ewaavvvv");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else if(rxbuf[1]=='d'){
|
||||
if(rxidx==2){
|
||||
for(i=0;i<32;i++){
|
||||
if((i%4)==0){
|
||||
sprintf((char*)s,"\n");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
if (EEW_Read(i, &val) == EE_OK){
|
||||
sprintf((char*)s,"0x%02x=0x%04x ",i,val);
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}else{
|
||||
sprintf((char*)s,"rderr ");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?ed");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?er|w|d");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?eraa | ewaavvvv");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
sprintf((char*)s,"\n?");
|
||||
while (CDC_Transmit_FS(s, 2) == USBD_BUSY);
|
||||
break;
|
||||
}
|
||||
}
|
||||
rxidx=0;
|
||||
}else rxidx++;
|
||||
// tiny spin or yield
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,196 @@
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include "usb_device.h"
|
||||
|
||||
#include "usbd_cdc_if.h"
|
||||
#include "cdc_int.h"
|
||||
#include "stm32f1xx_hal.h"
|
||||
#include "eeprom.h"
|
||||
#include "pwm.h"
|
||||
|
||||
extern TIM_HandleTypeDef htim1;
|
||||
extern TIM_HandleTypeDef htim2;
|
||||
extern TIM_HandleTypeDef htim3;
|
||||
extern TIM_HandleTypeDef htim4;
|
||||
|
||||
bool toHex(char c1,char c2,char c3,char c4,uint16_t* retval){
|
||||
if((c1>='0')&&(c1<='9')){
|
||||
c1-='0';
|
||||
}else if((c1>='a')&&(c1<='f')){
|
||||
c1=(c1-'a')+10;
|
||||
}else return false;
|
||||
|
||||
if((c2>='0')&&(c2<='9')){
|
||||
c2-='0';
|
||||
}else if((c2>='a')&&(c2<='f')){
|
||||
c2=(c2-'a')+10;
|
||||
}else return false;
|
||||
|
||||
if((c3>='0')&&(c3<='9')){
|
||||
c3-='0';
|
||||
}else if((c3>='a')&&(c3<='f')){
|
||||
c3=(c3-'a')+10;
|
||||
}else return false;
|
||||
|
||||
if((c4>='0')&&(c4<='9')){
|
||||
c4-='0';
|
||||
}else if((c4>='a')&&(c4<='f')){
|
||||
c4=(c4-'a')+10;
|
||||
}else return false;
|
||||
|
||||
*retval=c1;
|
||||
*retval*=16;
|
||||
*retval+=c2;
|
||||
*retval*=16;
|
||||
*retval+=c3;
|
||||
*retval*=16;
|
||||
*retval+=c4;
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
void manageCDC(void){
|
||||
static uint8_t rxbuf[100];
|
||||
static uint8_t rxidx=0;
|
||||
unsigned char s[100];
|
||||
uint8_t mot,dir,dm,m,c,d,u;
|
||||
uint16_t val;
|
||||
uint16_t st;
|
||||
uint16_t eeadd;
|
||||
uint8_t i;
|
||||
|
||||
if (CDC_Available()) {
|
||||
int rx = CDC_ReadByte();
|
||||
if (rx < 0) return;
|
||||
//uint8_t out = (uint8_t)c;
|
||||
//if (out >= 'a' && out <= 'z') out -= 32; // to upper
|
||||
rxbuf[rxidx]=rx;
|
||||
while (CDC_Transmit_FS(&rxbuf[rxidx], 1) == USBD_BUSY);//echo
|
||||
if((rx==0x0d)||(rx==0x0a)){
|
||||
if(rxidx){
|
||||
switch(rxbuf[0]){
|
||||
case 'i':
|
||||
sprintf((char*)s,"\ninfo");
|
||||
while (CDC_Transmit_FS(s, 5) == USBD_BUSY);
|
||||
sprintf((char*)s,"\npwm %d%d%d%d%d%d%d%d",IsPwmRunning(timMot1,FWMot1),IsPwmRunning(timMot1,BWMot1),IsPwmRunning(timMot2,FWMot2),IsPwmRunning(timMot2,BWMot2),IsPwmRunning(timMot3,FWMot3),IsPwmRunning(timMot3,BWMot3),IsPwmRunning(timMot4,FWMot4),IsPwmRunning(timMot4,BWMot4));
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
__HAL_TIM_GET_COMPARE(&htim3, TIM_CHANNEL_1);
|
||||
sprintf((char*)s,"\n0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx",(uint32_t)__HAL_TIM_GET_COMPARE(timMot1,FWMot1),(uint32_t)__HAL_TIM_GET_COMPARE(timMot1,BWMot1),(uint32_t)__HAL_TIM_GET_COMPARE(timMot2,FWMot2),(uint32_t)__HAL_TIM_GET_COMPARE(timMot2,BWMot2),(uint32_t)__HAL_TIM_GET_COMPARE(timMot3,FWMot3),(uint32_t)__HAL_TIM_GET_COMPARE(timMot3,BWMot3),(uint32_t)__HAL_TIM_GET_COMPARE(timMot4,FWMot4),(uint32_t)__HAL_TIM_GET_COMPARE(timMot4,BWMot4));
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
break;
|
||||
case 'm':
|
||||
if(rxidx==8){
|
||||
if((rxbuf[1]>='1')&&(rxbuf[1]<='4')){
|
||||
mot=rxbuf[1]-='0';
|
||||
if(rxbuf[2]=='f'){
|
||||
dir=FW;
|
||||
}else if(rxbuf[2]=='b'){
|
||||
dir=BW;
|
||||
}else{
|
||||
sprintf((char*)s,"\n?mnsvvvvv s=f|b");//m nmotore senso valore
|
||||
while (CDC_Transmit_FS(s, 16) == USBD_BUSY);
|
||||
break;
|
||||
}
|
||||
if(((rxbuf[3]>='0')&&(rxbuf[3]<='9'))&&((rxbuf[4]>='0')&&(rxbuf[4]<='9'))&&((rxbuf[5]>='0')&&(rxbuf[5]<='9'))&&((rxbuf[6]>='0')&&(rxbuf[6]<='9'))&&((rxbuf[7]>='0')&&(rxbuf[7]<='9'))){
|
||||
dm=rxbuf[3]-='0';
|
||||
m=rxbuf[4]-='0';
|
||||
c=rxbuf[5]-='0';
|
||||
d=rxbuf[6]-='0';
|
||||
u=rxbuf[7]-='0';
|
||||
val=dm;
|
||||
val*=10;
|
||||
val+=m;
|
||||
val*=10;
|
||||
val+=c;
|
||||
val*=10;
|
||||
val+=d;
|
||||
val*=10;
|
||||
val+=u;
|
||||
SetMot(mot,dir,val);
|
||||
}else{
|
||||
sprintf((char*)s,"\n?mnsvvvvv 00000>=vvvvv<=99999");//m nmotore senso valore
|
||||
while (CDC_Transmit_FS(s, 19) == USBD_BUSY);
|
||||
}
|
||||
|
||||
}else{
|
||||
sprintf((char*)s,"\n?mnsvvvvv 1>=m<=4");//m nmotore senso valore
|
||||
while (CDC_Transmit_FS(s, 19) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?mnsvvvvv");//m nmotore senso valore
|
||||
while (CDC_Transmit_FS(s, 10) == USBD_BUSY);
|
||||
}
|
||||
break;
|
||||
case 'e':
|
||||
if(rxidx>1){
|
||||
if(rxbuf[1]=='r'){
|
||||
if(rxidx==4){
|
||||
if(toHex('0','0',rxbuf[2],rxbuf[3],&eeadd)){
|
||||
st=EEW_Read(EE_VirtAddrse[eadd], &val);
|
||||
if (st == EE_OK){
|
||||
sprintf((char*)s,"\nee 0x%02x=0x%04x",eeadd,val);
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}else{
|
||||
sprintf((char*)s,"\nee read error %d",st);
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?eraa hex values");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?eraa");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else if(rxbuf[1]=='w'){
|
||||
if(rxidx==8){
|
||||
if(toHex('0','0',rxbuf[2],rxbuf[3],&eeadd)){
|
||||
if(toHex(rxbuf[4],rxbuf[5],rxbuf[6],rxbuf[7],&val)){
|
||||
st=EEW_Write(EE_VirtAddrse[eadd], val);
|
||||
if (st == EE_OK)sprintf((char*)s,"\ndone 0x%02x=0x%04x",eeadd,val);
|
||||
else sprintf((char*)s,"\nee write error %d", st);
|
||||
}else sprintf((char*)s,"\n?ewaavvvv hex values");
|
||||
}else sprintf((char*)s,"\n?ewaa hex values");
|
||||
}else sprintf((char*)s,"\n?ewaavvvv");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}else if(rxbuf[1]=='d'){
|
||||
if(rxidx==2){
|
||||
for(i=0;i<32;i++){
|
||||
if((i%4)==0){
|
||||
sprintf((char*)s,"\n");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
st=EEW_Read(EE_VirtAddrse[i], &val);
|
||||
if (st == EE_OK){
|
||||
sprintf((char*)s,"0x%02x=0x%04x ",i,val);
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}else{
|
||||
sprintf((char*)s,"rderr %d ",st);
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?ed");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?er|w|d");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?eraa | ewaavvvv");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
sprintf((char*)s,"\n?");
|
||||
while (CDC_Transmit_FS(s, 2) == USBD_BUSY);
|
||||
break;
|
||||
}
|
||||
}
|
||||
rxidx=0;
|
||||
}else rxidx++;
|
||||
// tiny spin or yield
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
|
||||
#include "usb_device.h"
|
||||
|
||||
#include "usbd_cdc_if.h"
|
||||
#include "cdc_int.h"
|
||||
|
||||
void manageCDC(void){
|
||||
static uint8_t rxbuf[100];
|
||||
static uint8_t rxidx=0;
|
||||
unsigned char s[100];
|
||||
if (CDC_Available()) {
|
||||
int c = CDC_ReadByte();
|
||||
if (c < 0) return;
|
||||
//uint8_t out = (uint8_t)c;
|
||||
//if (out >= 'a' && out <= 'z') out -= 32; // to upper
|
||||
rxbuf[rxidx]=c;
|
||||
rxidx++;
|
||||
if((c==0x0d)||(c==x0a)){
|
||||
switch(rxbuf[0]){
|
||||
case 'i':
|
||||
sprintf((char*)s,"\n?",c);
|
||||
while (CDC_Transmit_FS(s, 6) == USBD_BUSY) {
|
||||
break;
|
||||
default:
|
||||
sprintf((char*)s,"\n?",c);
|
||||
while (CDC_Transmit_FS(s, 6) == USBD_BUSY) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
// tiny spin or yield
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
|
||||
#include "usb_device.h"
|
||||
|
||||
#include "usbd_cdc_if.h"
|
||||
#include "cdc_int.h"
|
||||
|
||||
void manageCDC(void){
|
||||
static uint8_t rxbuf[100];
|
||||
static uint8_t rxidx=0;
|
||||
unsigned char s[100];
|
||||
if (CDC_Available()) {
|
||||
int c = CDC_ReadByte();
|
||||
if (c < 0) return;
|
||||
//uint8_t out = (uint8_t)c;
|
||||
//if (out >= 'a' && out <= 'z') out -= 32; // to upper
|
||||
rxbuf[rxidx]=c;
|
||||
rxidx++;
|
||||
if((c==0x0d)||(c==0x0a)){
|
||||
switch(rxbuf[0]){
|
||||
// case 'i':
|
||||
// sprintf((char*)s,"\n?",c);
|
||||
// while (CDC_Transmit_FS(s, 6) == USBD_BUSY) {
|
||||
// break;
|
||||
default:
|
||||
sprintf((char*)s,"\n?");
|
||||
while (CDC_Transmit_FS(s, 6) == USBD_BUSY) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
// tiny spin or yield
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,201 @@
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include "usb_device.h"
|
||||
|
||||
#include "usbd_cdc_if.h"
|
||||
#include "cdc_int.h"
|
||||
#include "stm32f1xx_hal.h"
|
||||
#include "eeprom.h"
|
||||
#include "pwm.h"
|
||||
|
||||
extern TIM_HandleTypeDef htim1;
|
||||
extern TIM_HandleTypeDef htim2;
|
||||
extern TIM_HandleTypeDef htim3;
|
||||
extern TIM_HandleTypeDef htim4;
|
||||
|
||||
bool toHex(char c1,char c2,char c3,char c4,uint16_t* retval){
|
||||
if((c1>='0')&&(c1<='9')){
|
||||
c1-='0';
|
||||
}else if((c1>='a')&&(c1<='f')){
|
||||
c1=(c1-'a')+10;
|
||||
}else return false;
|
||||
|
||||
if((c2>='0')&&(c2<='9')){
|
||||
c2-='0';
|
||||
}else if((c2>='a')&&(c2<='f')){
|
||||
c2=(c2-'a')+10;
|
||||
}else return false;
|
||||
|
||||
if((c3>='0')&&(c3<='9')){
|
||||
c3-='0';
|
||||
}else if((c3>='a')&&(c3<='f')){
|
||||
c3=(c3-'a')+10;
|
||||
}else return false;
|
||||
|
||||
if((c4>='0')&&(c4<='9')){
|
||||
c4-='0';
|
||||
}else if((c4>='a')&&(c4<='f')){
|
||||
c4=(c4-'a')+10;
|
||||
}else return false;
|
||||
|
||||
*retval=c1;
|
||||
*retval*=16;
|
||||
*retval+=c2;
|
||||
*retval*=16;
|
||||
*retval+=c3;
|
||||
*retval*=16;
|
||||
*retval+=c4;
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
void manageCDC(void){
|
||||
static uint8_t rxbuf[100];
|
||||
static uint8_t rxidx=0;
|
||||
unsigned char s[100];
|
||||
uint8_t mot,dir,dm,m,c,d,u;
|
||||
uint16_t val;
|
||||
uint16_t eeadd;
|
||||
uint8_t i;
|
||||
|
||||
if (CDC_Available()) {
|
||||
int rx = CDC_ReadByte();
|
||||
if (rx < 0) return;
|
||||
//uint8_t out = (uint8_t)c;
|
||||
//if (out >= 'a' && out <= 'z') out -= 32; // to upper
|
||||
rxbuf[rxidx]=rx;
|
||||
while (CDC_Transmit_FS(&rxbuf[rxidx], 1) == USBD_BUSY);//echo
|
||||
if((rx==0x0d)||(rx==0x0a)){
|
||||
if(rxidx){
|
||||
switch(rxbuf[0]){
|
||||
case 'i':
|
||||
sprintf((char*)s,"\ninfo");
|
||||
while (CDC_Transmit_FS(s, 5) == USBD_BUSY);
|
||||
sprintf((char*)s,"\npwm %d%d%d%d%d%d%d%d",IsPwmRunning(timMot1,FWMot1),IsPwmRunning(timMot1,BWMot1),IsPwmRunning(timMot2,FWMot2),IsPwmRunning(timMot2,BWMot2),IsPwmRunning(timMot3,FWMot3),IsPwmRunning(timMot3,BWMot3),IsPwmRunning(timMot4,FWMot4),IsPwmRunning(timMot4,BWMot4));
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
__HAL_TIM_GET_COMPARE(&htim3, TIM_CHANNEL_1);
|
||||
sprintf((char*)s,"\n0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx",(uint32_t)__HAL_TIM_GET_COMPARE(timMot1,FWMot1),(uint32_t)__HAL_TIM_GET_COMPARE(timMot1,BWMot1),(uint32_t)__HAL_TIM_GET_COMPARE(timMot2,FWMot2),(uint32_t)__HAL_TIM_GET_COMPARE(timMot2,BWMot2),(uint32_t)__HAL_TIM_GET_COMPARE(timMot3,FWMot3),(uint32_t)__HAL_TIM_GET_COMPARE(timMot3,BWMot3),(uint32_t)__HAL_TIM_GET_COMPARE(timMot4,FWMot4),(uint32_t)__HAL_TIM_GET_COMPARE(timMot4,BWMot4));
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
break;
|
||||
case 'm':
|
||||
if(rxidx==8){
|
||||
if((rxbuf[1]>='1')&&(rxbuf[1]<='4')){
|
||||
mot=rxbuf[1]-='0';
|
||||
if(rxbuf[2]=='f'){
|
||||
dir=FW;
|
||||
}else if(rxbuf[2]=='b'){
|
||||
dir=BW;
|
||||
}else{
|
||||
sprintf((char*)s,"\n?mnsvvvvv s=f|b");//m nmotore senso valore
|
||||
while (CDC_Transmit_FS(s, 16) == USBD_BUSY);
|
||||
break;
|
||||
}
|
||||
if(((rxbuf[3]>='0')&&(rxbuf[3]<='9'))&&((rxbuf[4]>='0')&&(rxbuf[4]<='9'))&&((rxbuf[5]>='0')&&(rxbuf[5]<='9'))&&((rxbuf[6]>='0')&&(rxbuf[6]<='9'))&&((rxbuf[7]>='0')&&(rxbuf[7]<='9'))){
|
||||
dm=rxbuf[3]-='0';
|
||||
m=rxbuf[4]-='0';
|
||||
c=rxbuf[5]-='0';
|
||||
d=rxbuf[6]-='0';
|
||||
u=rxbuf[7]-='0';
|
||||
val=dm;
|
||||
val*=10;
|
||||
val+=m;
|
||||
val*=10;
|
||||
val+=c;
|
||||
val*=10;
|
||||
val+=d;
|
||||
val*=10;
|
||||
val+=u;
|
||||
SetMot(mot,dir,val);
|
||||
}else{
|
||||
sprintf((char*)s,"\n?mnsvvvvv 00000>=vvvvv<=99999");//m nmotore senso valore
|
||||
while (CDC_Transmit_FS(s, 19) == USBD_BUSY);
|
||||
}
|
||||
|
||||
}else{
|
||||
sprintf((char*)s,"\n?mnsvvvvv 1>=m<=4");//m nmotore senso valore
|
||||
while (CDC_Transmit_FS(s, 19) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?mnsvvvvv");//m nmotore senso valore
|
||||
while (CDC_Transmit_FS(s, 10) == USBD_BUSY);
|
||||
}
|
||||
break;
|
||||
case 'e':
|
||||
if(rxidx>1){
|
||||
if(rxbuf[1]=='r'){
|
||||
if(rxidx==4){
|
||||
if(toHex('0','0',rxbuf[2],rxbuf[3],&eeadd)){
|
||||
if (EEW_Read(eeadd, &val) == EE_OK){
|
||||
sprintf((char*)s,"\nee 0x%02x=0x%04x",eeadd,val);
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}else{
|
||||
sprintf((char*)s,"\nee read error",eeadd,val);
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?eraa hex values");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?eraa");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else if(rxbuf[1]=='w'){
|
||||
if(rxidx==8){
|
||||
if(toHex('0','0',rxbuf[2],rxbuf[3],&eeadd)){
|
||||
if(toHex(rxbuf[4],rxbuf[5],rxbuf[6],rxbuf[7],&val)){
|
||||
EEW_Write(eeadd, val);
|
||||
sprintf((char*)s,"\ndone 0x%02x=0x%04x",eeadd,val);
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}else{
|
||||
sprintf((char*)s,"\n?ewaavvvv hex values");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?ewaa hex values");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?ewaavvvv");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else if(rxbuf[1]=='d'){
|
||||
if(rxidx==2){
|
||||
for(i=0;i<32;i++){
|
||||
if((i%4)==0){
|
||||
sprintf((char*)s,"\n");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
if (EEW_Read(i, &val) == EE_OK){
|
||||
sprintf((char*)s,"0x%02x=0x%04x ",i,val);
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}else{
|
||||
sprintf((char*)s,"rderr ",i,val);
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?ed");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?er|w|d");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?eraa | ewaavvvv");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
sprintf((char*)s,"\n?");
|
||||
while (CDC_Transmit_FS(s, 2) == USBD_BUSY);
|
||||
break;
|
||||
}
|
||||
}
|
||||
rxidx=0;
|
||||
}else rxidx++;
|
||||
// tiny spin or yield
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,197 @@
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
#include "usb_device.h"
|
||||
|
||||
#include "usbd_cdc_if.h"
|
||||
#include "cdc_int.h"
|
||||
#include "stm32f1xx_hal.h"
|
||||
#include "eeprom.h"
|
||||
#include "pwm.h"
|
||||
|
||||
extern TIM_HandleTypeDef htim1;
|
||||
extern TIM_HandleTypeDef htim2;
|
||||
extern TIM_HandleTypeDef htim3;
|
||||
extern TIM_HandleTypeDef htim4;
|
||||
extern const uint16_t EE_VirtAddrs[];
|
||||
|
||||
bool toHex(char c1,char c2,char c3,char c4,uint16_t* retval){
|
||||
if((c1>='0')&&(c1<='9')){
|
||||
c1-='0';
|
||||
}else if((c1>='a')&&(c1<='f')){
|
||||
c1=(c1-'a')+10;
|
||||
}else return false;
|
||||
|
||||
if((c2>='0')&&(c2<='9')){
|
||||
c2-='0';
|
||||
}else if((c2>='a')&&(c2<='f')){
|
||||
c2=(c2-'a')+10;
|
||||
}else return false;
|
||||
|
||||
if((c3>='0')&&(c3<='9')){
|
||||
c3-='0';
|
||||
}else if((c3>='a')&&(c3<='f')){
|
||||
c3=(c3-'a')+10;
|
||||
}else return false;
|
||||
|
||||
if((c4>='0')&&(c4<='9')){
|
||||
c4-='0';
|
||||
}else if((c4>='a')&&(c4<='f')){
|
||||
c4=(c4-'a')+10;
|
||||
}else return false;
|
||||
|
||||
*retval=c1;
|
||||
*retval*=16;
|
||||
*retval+=c2;
|
||||
*retval*=16;
|
||||
*retval+=c3;
|
||||
*retval*=16;
|
||||
*retval+=c4;
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
void manageCDC(void){
|
||||
static uint8_t rxbuf[100];
|
||||
static uint8_t rxidx=0;
|
||||
unsigned char s[100];
|
||||
uint8_t mot,dir,dm,m,c,d,u;
|
||||
uint16_t val;
|
||||
uint16_t st;
|
||||
uint16_t eeadd;
|
||||
uint8_t i;
|
||||
|
||||
if (CDC_Available()) {
|
||||
int rx = CDC_ReadByte();
|
||||
if (rx < 0) return;
|
||||
//uint8_t out = (uint8_t)c;
|
||||
//if (out >= 'a' && out <= 'z') out -= 32; // to upper
|
||||
rxbuf[rxidx]=rx;
|
||||
while (CDC_Transmit_FS(&rxbuf[rxidx], 1) == USBD_BUSY);//echo
|
||||
if((rx==0x0d)||(rx==0x0a)){
|
||||
if(rxidx){
|
||||
switch(rxbuf[0]){
|
||||
case 'i':
|
||||
sprintf((char*)s,"\ninfo");
|
||||
while (CDC_Transmit_FS(s, 5) == USBD_BUSY);
|
||||
sprintf((char*)s,"\npwm %d%d%d%d%d%d%d%d",IsPwmRunning(timMot1,FWMot1),IsPwmRunning(timMot1,BWMot1),IsPwmRunning(timMot2,FWMot2),IsPwmRunning(timMot2,BWMot2),IsPwmRunning(timMot3,FWMot3),IsPwmRunning(timMot3,BWMot3),IsPwmRunning(timMot4,FWMot4),IsPwmRunning(timMot4,BWMot4));
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
__HAL_TIM_GET_COMPARE(&htim3, TIM_CHANNEL_1);
|
||||
sprintf((char*)s,"\n0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx 0x%lx",(uint32_t)__HAL_TIM_GET_COMPARE(timMot1,FWMot1),(uint32_t)__HAL_TIM_GET_COMPARE(timMot1,BWMot1),(uint32_t)__HAL_TIM_GET_COMPARE(timMot2,FWMot2),(uint32_t)__HAL_TIM_GET_COMPARE(timMot2,BWMot2),(uint32_t)__HAL_TIM_GET_COMPARE(timMot3,FWMot3),(uint32_t)__HAL_TIM_GET_COMPARE(timMot3,BWMot3),(uint32_t)__HAL_TIM_GET_COMPARE(timMot4,FWMot4),(uint32_t)__HAL_TIM_GET_COMPARE(timMot4,BWMot4));
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
break;
|
||||
case 'm':
|
||||
if(rxidx==8){
|
||||
if((rxbuf[1]>='1')&&(rxbuf[1]<='4')){
|
||||
mot=rxbuf[1]-='0';
|
||||
if(rxbuf[2]=='f'){
|
||||
dir=FW;
|
||||
}else if(rxbuf[2]=='b'){
|
||||
dir=BW;
|
||||
}else{
|
||||
sprintf((char*)s,"\n?mnsvvvvv s=f|b");//m nmotore senso valore
|
||||
while (CDC_Transmit_FS(s, 16) == USBD_BUSY);
|
||||
break;
|
||||
}
|
||||
if(((rxbuf[3]>='0')&&(rxbuf[3]<='9'))&&((rxbuf[4]>='0')&&(rxbuf[4]<='9'))&&((rxbuf[5]>='0')&&(rxbuf[5]<='9'))&&((rxbuf[6]>='0')&&(rxbuf[6]<='9'))&&((rxbuf[7]>='0')&&(rxbuf[7]<='9'))){
|
||||
dm=rxbuf[3]-='0';
|
||||
m=rxbuf[4]-='0';
|
||||
c=rxbuf[5]-='0';
|
||||
d=rxbuf[6]-='0';
|
||||
u=rxbuf[7]-='0';
|
||||
val=dm;
|
||||
val*=10;
|
||||
val+=m;
|
||||
val*=10;
|
||||
val+=c;
|
||||
val*=10;
|
||||
val+=d;
|
||||
val*=10;
|
||||
val+=u;
|
||||
SetMot(mot,dir,val);
|
||||
}else{
|
||||
sprintf((char*)s,"\n?mnsvvvvv 00000>=vvvvv<=99999");//m nmotore senso valore
|
||||
while (CDC_Transmit_FS(s, 19) == USBD_BUSY);
|
||||
}
|
||||
|
||||
}else{
|
||||
sprintf((char*)s,"\n?mnsvvvvv 1>=m<=4");//m nmotore senso valore
|
||||
while (CDC_Transmit_FS(s, 19) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?mnsvvvvv");//m nmotore senso valore
|
||||
while (CDC_Transmit_FS(s, 10) == USBD_BUSY);
|
||||
}
|
||||
break;
|
||||
case 'e':
|
||||
if(rxidx>1){
|
||||
if(rxbuf[1]=='r'){
|
||||
if(rxidx==4){
|
||||
if(toHex('0','0',rxbuf[2],rxbuf[3],&eeadd)){
|
||||
st=EEW_Read(EE_VirtAddrse[eeadd], &val);
|
||||
if (st == EE_OK){
|
||||
sprintf((char*)s,"\nee 0x%02x=0x%04x",eeadd,val);
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}else{
|
||||
sprintf((char*)s,"\nee read error %d",st);
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?eraa hex values");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?eraa");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else if(rxbuf[1]=='w'){
|
||||
if(rxidx==8){
|
||||
if(toHex('0','0',rxbuf[2],rxbuf[3],&eeadd)){
|
||||
if(toHex(rxbuf[4],rxbuf[5],rxbuf[6],rxbuf[7],&val)){
|
||||
st=EEW_Write(EE_VirtAddrse[eadd], val);
|
||||
if (st == EE_OK)sprintf((char*)s,"\ndone 0x%02x=0x%04x",eeadd,val);
|
||||
else sprintf((char*)s,"\nee write error %d", st);
|
||||
}else sprintf((char*)s,"\n?ewaavvvv hex values");
|
||||
}else sprintf((char*)s,"\n?ewaa hex values");
|
||||
}else sprintf((char*)s,"\n?ewaavvvv");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}else if(rxbuf[1]=='d'){
|
||||
if(rxidx==2){
|
||||
for(i=0;i<32;i++){
|
||||
if((i%4)==0){
|
||||
sprintf((char*)s,"\n");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
st=EEW_Read(EE_VirtAddrse[i], &val);
|
||||
if (st == EE_OK){
|
||||
sprintf((char*)s,"0x%02x=0x%04x ",i,val);
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}else{
|
||||
sprintf((char*)s,"rderr %d ",st);
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?ed");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?er|w|d");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
}else{
|
||||
sprintf((char*)s,"\n?eraa | ewaavvvv");
|
||||
while (CDC_Transmit_FS(s, strlen((char*)s)) == USBD_BUSY);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
sprintf((char*)s,"\n?");
|
||||
while (CDC_Transmit_FS(s, 2) == USBD_BUSY);
|
||||
break;
|
||||
}
|
||||
}
|
||||
rxidx=0;
|
||||
}else rxidx++;
|
||||
// tiny spin or yield
|
||||
}
|
||||
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
File diff suppressed because one or more lines are too long
@@ -38,7 +38,7 @@
|
||||
<item key="ignoreCase" value="true"/>
|
||||
<item key="isRegExSearch" value="false"/>
|
||||
<item key="isWholeWord" value="false"/>
|
||||
<item key="textPattern" value="EE_ReadVariable"/>
|
||||
<item key="textPattern" value="EE_VirtAddrse"/>
|
||||
<item key="scope" value="0"/>
|
||||
<list key="fileNamePatterns">
|
||||
<item value="*"/>
|
||||
@@ -53,7 +53,7 @@
|
||||
<item key="ignoreCase" value="true"/>
|
||||
<item key="isRegExSearch" value="false"/>
|
||||
<item key="isWholeWord" value="false"/>
|
||||
<item key="textPattern" value="inh2"/>
|
||||
<item key="textPattern" value="ee_init"/>
|
||||
<item key="scope" value="0"/>
|
||||
<list key="fileNamePatterns">
|
||||
<item value="*"/>
|
||||
@@ -65,7 +65,7 @@
|
||||
<item key="ignoreCase" value="true"/>
|
||||
<item key="isRegExSearch" value="false"/>
|
||||
<item key="isWholeWord" value="false"/>
|
||||
<item key="textPattern" value="INH1_GPIO_Port"/>
|
||||
<item key="textPattern" value="tohex"/>
|
||||
<item key="scope" value="0"/>
|
||||
<list key="fileNamePatterns">
|
||||
<item value="*"/>
|
||||
@@ -77,7 +77,7 @@
|
||||
<item key="ignoreCase" value="true"/>
|
||||
<item key="isRegExSearch" value="false"/>
|
||||
<item key="isWholeWord" value="false"/>
|
||||
<item key="textPattern" value="INH1"/>
|
||||
<item key="textPattern" value="EE_ReadVariable"/>
|
||||
<item key="scope" value="0"/>
|
||||
<list key="fileNamePatterns">
|
||||
<item value="*"/>
|
||||
@@ -89,7 +89,7 @@
|
||||
<item key="ignoreCase" value="true"/>
|
||||
<item key="isRegExSearch" value="false"/>
|
||||
<item key="isWholeWord" value="false"/>
|
||||
<item key="textPattern" value="INH1_Pin"/>
|
||||
<item key="textPattern" value="inh2"/>
|
||||
<item key="scope" value="0"/>
|
||||
<list key="fileNamePatterns">
|
||||
<item value="*"/>
|
||||
@@ -101,7 +101,7 @@
|
||||
<item key="ignoreCase" value="true"/>
|
||||
<item key="isRegExSearch" value="false"/>
|
||||
<item key="isWholeWord" value="false"/>
|
||||
<item key="textPattern" value="Led2"/>
|
||||
<item key="textPattern" value="INH1_GPIO_Port"/>
|
||||
<item key="scope" value="0"/>
|
||||
<list key="fileNamePatterns">
|
||||
<item value="*"/>
|
||||
@@ -113,7 +113,7 @@
|
||||
<item key="ignoreCase" value="true"/>
|
||||
<item key="isRegExSearch" value="false"/>
|
||||
<item key="isWholeWord" value="false"/>
|
||||
<item key="textPattern" value="htim3"/>
|
||||
<item key="textPattern" value="INH1"/>
|
||||
<item key="scope" value="0"/>
|
||||
<list key="fileNamePatterns">
|
||||
<item value="*"/>
|
||||
@@ -125,7 +125,7 @@
|
||||
<item key="ignoreCase" value="true"/>
|
||||
<item key="isRegExSearch" value="false"/>
|
||||
<item key="isWholeWord" value="false"/>
|
||||
<item key="textPattern" value="led2"/>
|
||||
<item key="textPattern" value="INH1_Pin"/>
|
||||
<item key="scope" value="0"/>
|
||||
<list key="fileNamePatterns">
|
||||
<item value="*"/>
|
||||
@@ -137,7 +137,7 @@
|
||||
<item key="ignoreCase" value="true"/>
|
||||
<item key="isRegExSearch" value="false"/>
|
||||
<item key="isWholeWord" value="false"/>
|
||||
<item key="textPattern" value="HAL_GPIO_TogglePin"/>
|
||||
<item key="textPattern" value="Led2"/>
|
||||
<item key="scope" value="0"/>
|
||||
<list key="fileNamePatterns">
|
||||
<item value="*"/>
|
||||
@@ -149,7 +149,7 @@
|
||||
<item key="ignoreCase" value="true"/>
|
||||
<item key="isRegExSearch" value="false"/>
|
||||
<item key="isWholeWord" value="false"/>
|
||||
<item key="textPattern" value="pwm.h"/>
|
||||
<item key="textPattern" value="htim3"/>
|
||||
<item key="scope" value="0"/>
|
||||
<list key="fileNamePatterns">
|
||||
<item value="*"/>
|
||||
@@ -161,7 +161,7 @@
|
||||
<item key="ignoreCase" value="true"/>
|
||||
<item key="isRegExSearch" value="false"/>
|
||||
<item key="isWholeWord" value="false"/>
|
||||
<item key="textPattern" value="HAL_TIM_PWM_Start"/>
|
||||
<item key="textPattern" value="led2"/>
|
||||
<item key="scope" value="0"/>
|
||||
<list key="fileNamePatterns">
|
||||
<item value="*"/>
|
||||
@@ -173,7 +173,7 @@
|
||||
<item key="ignoreCase" value="true"/>
|
||||
<item key="isRegExSearch" value="false"/>
|
||||
<item key="isWholeWord" value="false"/>
|
||||
<item key="textPattern" value="GPIO_PIN_13"/>
|
||||
<item key="textPattern" value="HAL_GPIO_TogglePin"/>
|
||||
<item key="scope" value="0"/>
|
||||
<list key="fileNamePatterns">
|
||||
<item value="*"/>
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
<item key="hasShownOverlayPopupBefore" value="true"/>
|
||||
<item key="replaceBarOpen" value="false"/>
|
||||
<list key="searchhistory">
|
||||
<item value="eefindinpage"/>
|
||||
<item value="eefindstring"/>
|
||||
<item value="eeadd"/>
|
||||
<item value="hin"/>
|
||||
</list>
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
#Sun Dec 07 19:18:32 CET 2025
|
||||
#Mon Dec 08 11:52:31 CET 2025
|
||||
org.eclipse.core.runtime=2
|
||||
org.eclipse.platform=4.33.0.v20240903-0240
|
||||
|
||||
Reference in New Issue
Block a user