diff --git a/F1:F103/FX3U/Readme.md b/F1:F103/FX3U/Readme.md index e69de29..54e3260 100644 --- a/F1:F103/FX3U/Readme.md +++ b/F1:F103/FX3U/Readme.md @@ -0,0 +1,75 @@ +A usefull thing made of chineese FX3U clone +=========================================== + +Works over RS-232 (default: 115200, 8N1) or CAN (default 250000 baud). + +You can see pinout table in file `hardware.c`. + +## Serial protocol (each string ends with '\n'). + +``` +commands format: parameter[number][=setter] +parameter [CAN idx] - help +-------------------------- + + CAN bus commands: +canbuserr - print all CAN bus errors (a lot of if not connected) +cansniff - switch CAN sniffer mode + + Configuration: +bounce [14] - set/get anti-bounce timeout (ms, max: 1000) +canid [6] - set both (in/out) CAN ID / get in CAN ID +canidin [7] - get/set input CAN ID +canidout [8] - get/set output CAN ID +canspeed [5] - get/set CAN speed (bps) +dumpconf - dump current configuration +eraseflash [10] - erase all flash storage +saveconf [9] - save configuration +usartspeed [15] - get/set USART1 speed + + IN/OUT: +adc [4] - get raw ADC values for given channel +esw [12] - anti-bounce read inputs +eswnow [13] - read current inputs' state +led [16] - work with onboard LED +relay [11] - get/set relay state (0 - off, 1 - on) + + Other commands: +mcutemp [3] - get MCU temperature (*10degrC) +reset [1] - reset MCU +s - send CAN message: ID 0..8 data bytes +time [2] - get/set time (1ms, 32bit) +wdtest - test watchdog +error=badcmd +``` + +Value in square brackets is CAN bus command code. + +## CAN bus protocol + +All data in little-endian format! + +BIT - MEANING + +0, 1 - (uint16_t) - command code (value in square brackets upper); + +2 - (uint8_t) - parameter number (e.g. ADC channel or X/Y channel number), 0..127 [ORed with 0x80 for setter]; + +3 - (uint8_t) - error code (only when device answers for requests); + +4..7 - (int32_t) - data. + +### CAN bus error codes + +0 - `ERR_OK` - all OK, + +1 - `ERR_BADPAR` - parameter is wrong, + +2 - `ERR_BADVAL` - value is wrong (e.g. out of range), + +3 - `ERR_WRONGLEN` - wrong message length (for setter or for obligatory parameter number), + +4 - `ERR_BADCMD` - unknown command code, + +5 - `ERR_CANTRUN` - can't run given command due to bad parameters or other reason. + diff --git a/F1:F103/FX3U/adc.c b/F1:F103/FX3U/adc.c index fc3b3fb..e8a7ddf 100644 --- a/F1:F103/FX3U/adc.c +++ b/F1:F103/FX3U/adc.c @@ -38,7 +38,7 @@ void adc_setup(){ ADC1->SMPR1 = ADC_SMPR1_SMP16 | ADC_SMPR1_SMP17; // sequence order: 1[0]->3[1]->14[2]->15[3]->10[4]->11[5] -> 16[tsen] -> 17[vdd] ADC1->SQR3 = (1 << 0) | (3<<5) | (14 << 10) | (15 << 15) | (10 << 20) | (11 < 25); - ADC1->SQR2 = (16 << 0) | (17 << 5); + ADC1->SQR2 = (12 << 0) | (13 << 5) | (16 << 10) | (17 << 15); ADC1->SQR1 = (ADC_CHANNELS - 1) << 20; // amount of conversions ADC1->CR1 = ADC_CR1_SCAN; // scan mode // DMA, continuous mode; enable vref & Tsens; enable SWSTART as trigger diff --git a/F1:F103/FX3U/adc.h b/F1:F103/FX3U/adc.h index 063bb90..d594f4d 100644 --- a/F1:F103/FX3U/adc.h +++ b/F1:F103/FX3U/adc.h @@ -28,6 +28,8 @@ enum{ ADC_CH_3, ADC_CH_4, ADC_CH_5, + ADC_POT0, // on-board pots (PC2 - in12, PC3 - in13) + ADC_POT1, ADC_CH_TSEN, // T sensor ADC_CH_VDD, // Vdd sensor ADC_CHANNELS diff --git a/F1:F103/FX3U/canproto.c b/F1:F103/FX3U/canproto.c index cd43cad..6304cd1 100644 --- a/F1:F103/FX3U/canproto.c +++ b/F1:F103/FX3U/canproto.c @@ -27,8 +27,11 @@ #define FIXDL(m) do{m->length = 8;}while(0) /*********** START of all common functions list (for `funclist`) ***********/ +static errcodes ping(CAN_message *m){ + m->ID = the_conf.CANIDout; // change ID + return ERR_OK; // send same message +} // reset MCU - static errcodes reset(CAN_message _U_ *msg){ usart_send("Soft reset\n"); usart_transmit(); @@ -127,6 +130,13 @@ static errcodes eswg(CAN_message *msg){ FIXDL(msg); return ERR_OK; } +// onboard LED +static errcodes led(CAN_message *m){ + if(m->length > 4 && ISSETTER(m->data)) LED(m->data[4]); + m->data[4] = LED(-1); + FIXDL(m); + return ERR_OK; +} // common uint32_t setter/getter static errcodes u32setget(CAN_message *msg){ @@ -176,6 +186,7 @@ typedef struct{ // list of common (CAN/RS-232) functions // !!!!!!!!! Getters should set message length to 8 !!!!!!!!!!! static const commonfunction funclist[CMD_AMOUNT] = { + [CMD_PING] = {ping, 0, 0, 0}, [CMD_RESET] = {reset, 0, 0, 0}, [CMD_TIME] = {time_getset, 0, 0, 0}, [CMD_MCUTEMP] = {mcut, 0, 0, 0}, @@ -191,6 +202,7 @@ static const commonfunction funclist[CMD_AMOUNT] = { [CMD_GETESWNOW] = {esw, 0, INT32_MAX, 0}, [CMD_BOUNCE] = {u32setget, 0, 1000, 0}, [CMD_USARTSPEED] = {u32setget, 1200, 3000000, 0}, + [CMD_LED] = {led, 0, 0, 0}, }; diff --git a/F1:F103/FX3U/canproto.h b/F1:F103/FX3U/canproto.h index 557cdb2..dbaf54a 100644 --- a/F1:F103/FX3U/canproto.h +++ b/F1:F103/FX3U/canproto.h @@ -35,8 +35,8 @@ // error codes for answer message typedef enum{ ERR_OK, // 0 - all OK - ERR_BADPAR, // 1 - parameter's value is wrong - ERR_BADVAL, // 2 - wrong parameter's value + ERR_BADPAR, // 1 - parameter is wrong + ERR_BADVAL, // 2 - wrong value ERR_WRONGLEN, // 3 - wrong message length ERR_BADCMD, // 4 - unknown command ERR_CANTRUN, // 5 - can't run given command due to bad parameters or other @@ -45,6 +45,7 @@ typedef enum{ // CAN commands indexes enum{ + CMD_PING, // just ping CMD_RESET, // reset MCU CMD_TIME, // get/set Tms CMD_MCUTEMP, // get MCU temperature (*10) @@ -60,6 +61,7 @@ enum{ CMD_GETESWNOW, // current ESW state, absolute CMD_BOUNCE, // get/set bounce constant (ms) CMD_USARTSPEED, // get/set USART1 speed (if encoder on RS-422) + CMD_LED, // onboard LED // should be the last: CMD_AMOUNT // amount of CAN commands }; diff --git a/F1:F103/FX3U/fx3u.bin b/F1:F103/FX3U/fx3u.bin index 1d624e1..d00a77f 100755 Binary files a/F1:F103/FX3U/fx3u.bin and b/F1:F103/FX3U/fx3u.bin differ diff --git a/F1:F103/FX3U/fx3u.creator.user b/F1:F103/FX3U/fx3u.creator.user index 2796bd3..b9fc616 100644 --- a/F1:F103/FX3U/fx3u.creator.user +++ b/F1:F103/FX3U/fx3u.creator.user @@ -1,6 +1,6 @@ - + EnvironmentId diff --git a/F1:F103/FX3U/hardware.c b/F1:F103/FX3U/hardware.c index 1d1e6cd..d81ebce 100644 --- a/F1:F103/FX3U/hardware.c +++ b/F1:F103/FX3U/hardware.c @@ -22,23 +22,28 @@ #include "hardware.h" #ifdef EBUG #include "strfunc.h" +#include "uchar.h" #endif /* pinout: +Xn - inputs, Yn - outputs, ADCn - ADC inputs + | **Pin #** | **Pin name ** | **function** | **settings** | **comment ** | | --------- | ------------- | ------------ | ---------------------- | --------------------------------------- | -| 15 | PC0/adcin10 | ADC4 | ADC in | | -| 16 | PC1/adcin11 | ADC5 | ADC in | | +| 15 | PC0/adcin10 | ADC4 | ADC in | current in (0..20mA) | +| 16 | PC1/adcin11 | ADC5 | ADC in | current in | +| 17 | PC2/adcin12 | ADC6 | ADC in | right potentiometer | +| 18 | PC3/adcin13 | ADC7 | ADC in | left pot | | 23 | PA0 | Y3 | PPOUT | | -| 24 | PA1/adcin1 | ADC0 | ADC in | | +| 24 | PA1/adcin1 | ADC0 | ADC in | voltage in (up to 11V) | | 25 | PA2 | Y11 | PPOUT | | -| 26 | PA3/adcin3 | ADC1 | ADC in | | +| 26 | PA3/adcin3 | ADC1 | ADC in | voltage in (up to 11V) | | 31 | PA6 | Y10 | PPOUT | | | 32 | PA7 | Y7 | PPOUT | | -| 33 | PC4/adcin14 | ADC2 | ADC in | | -| 34 | PC5/adcin15 | ADC3 | ADC in | | -| 37 | PB2/boot1 | PROG SW | PUIN | | +| 33 | PC4/adcin14 | ADC2 | ADC in | voltage in | +| 34 | PC5/adcin15 | ADC3 | ADC in | current in | +| 37 | PB2/boot1 | PROG SW | PUIN | onboard switch "Prog" (X8!!!) | | 38 | PE7 | X14 | PUIN | | | 39 | PE8 | X15 | PUIN | | | 40 | PE9 | X12 | PUIN | | @@ -54,13 +59,15 @@ | 52 | PB13 | X0 | PUIN | | | 53 | PB14 | X1 | PUIN | | | 54 | PB15 | Y6 | PPOUT | | +| 57 | PD10 | LED | PPOUT | onboard LED "RUN" | | 59 | PD12 | Y5 | PPOUT | | +| 64 | PC7 | | (FLIN) | (Not now) extern 24V power detect | | 65 | PC8 | Y1 | PPOUT | | | 66 | PC9 | Y0 | PPOUT | | | 67 | PA8 | Y2 | PPOUT | | | 68 | PA9 | RS TX | AFPP | | | 69 | PA10 | RS RX | FLIN | | -| 76 | PA14/SWCLK | 485 DE * | (default) | (Not now) | +| 76 | PA14/SWCLK | 485 DE * | (default) | (Not now) RS-485 Data Enable | | 81 | PD0 | CAN RX | FLIN | | | 82 | PD1 | CAN TX | AFPP | | | 89 | PB3/JTDO | Y4 | PPOUT | | @@ -77,14 +84,14 @@ void gpio_setup(void){ AFIO->MAPR = AFIO_MAPR_SWJ_CFG_JTAGDISABLE; GPIOA->CRL = CRL(0, CNF_PPOUTPUT|MODE_NORMAL) | CRL(1, CNF_ANALOG) | CRL(2, CNF_PPOUTPUT|MODE_NORMAL) | CRL(3, CNF_ANALOG) | CRL(6, CNF_PPOUTPUT|MODE_NORMAL) | CRL(7, CNF_PPOUTPUT|MODE_NORMAL); - GPIOA->CRH = 0; - GPIOB->CRL = CRL(2, CNF_PUDINPUT); + GPIOA->CRH = CRH(8, CNF_PPOUTPUT|MODE_NORMAL); + GPIOB->CRL = CRL(2, CNF_PUDINPUT) | CRL(3, CNF_PPOUTPUT|MODE_NORMAL); GPIOB->CRH = CRH(10, CNF_PUDINPUT) | CRH(11, CNF_PUDINPUT) | CRH(12, CNF_PUDINPUT) | CRH(13, CNF_PUDINPUT) | CRH(14, CNF_PUDINPUT) | CRH(15, CNF_PPOUTPUT|MODE_NORMAL); GPIOC-> CRL = CRL(0, CNF_ANALOG) | CRL(1, CNF_ANALOG) | CRL(4, CNF_ANALOG) | CRL(5, CNF_ANALOG); GPIOC->CRH = CRH(8, CNF_PPOUTPUT|MODE_NORMAL) | CRH(9, CNF_PPOUTPUT|MODE_NORMAL); GPIOD->CRL = 0; - GPIOD->CRH = CRH(12, CNF_PPOUTPUT|MODE_NORMAL); + GPIOD->CRH = CRH(10, CNF_PPOUTPUT|MODE_NORMAL) | CRH(12, CNF_PPOUTPUT|MODE_NORMAL); GPIOE->CRL = CRL(7, CNF_PUDINPUT); GPIOE->CRH = CRH(8, CNF_PUDINPUT) | CRH(9, CNF_PUDINPUT) | CRH(10, CNF_PUDINPUT) | CRH(11, CNF_PUDINPUT) | CRH(12, CNF_PUDINPUT) | CRH(13, CNF_PUDINPUT) | CRH(14, CNF_PUDINPUT) | CRH(15, CNF_PUDINPUT); @@ -92,7 +99,7 @@ void gpio_setup(void){ GPIOA->ODR = 0; GPIOB->ODR = (1<<2) | (1<<10) | (1<<11) | (1<<12) | (1<<13) | (1<<14); GPIOC->ODR = 0; - GPIOD->ODR = 0; + GPIOD->ODR = (1<<10); // turn off LED GPIOE->ODR = (1<<7) | (1<<8) | (1<<9) | (1<<10) | (1<<11) | (1<<12) | (1<<13) | (1<<14) | (1<<15); } @@ -125,9 +132,9 @@ static const pin_t IN[INMAX+1] = { // youbannye uskoglazye pidarasy! Ready to fu {GPIOB, 1<<10}, // X5 - PB10 {GPIOE, 1<<13}, // X6 - PE13 {GPIOE, 1<<14}, // X7 - PE14 - {NULL, 0}, // X8 - absent + {GPIOB, 1<<2}, // X8 - onboard switch {NULL, 0}, // X9 - absent - {GPIOE, 1<11}, // X10 - PE11 + {GPIOE, 1<<11}, // X10 - PE11 {GPIOE, 1<<12}, // X11 - PE12 {GPIOE, 1<<9}, // X12 - PE9 {GPIOE, 1<<10}, // X13 - PE10 @@ -160,6 +167,15 @@ uint32_t outchannels(){ return 0b110011111111; } +// turn on/off onboard LED; if onoff < 0 - return current state +uint8_t LED(int onoff){ + if(onoff > -1){ + if(onoff) pin_clear(LEDPORT, LEDPIN); + else pin_set(LEDPORT, LEDPIN); + } + return ((LEDPORT->IDR & LEDPIN) ? 0 : 1); // inverse! +} + /** * @brief set_relay - turn on/off relay `Nch` (if Nch > OUTMAX - all) * @param Nch - single relay channel No or >Ymax to set/reset all relays @@ -195,9 +211,12 @@ static int readpins(uint8_t Nch, const pin_t *pins, uint8_t max){ for(int i = max; i > -1; --i){ val <<= 1; int p = gpin(i); - if(p > -1) val |= gpin(i); + if(p == 1){ + //usart_send("pin"); usart_send(u2str(i)); usart_send("=1\n"); + val |= p; + } } -usart_send("readpins, val="); usart_send(i2str(val)); newline(); + //usart_send("readpins, val="); usart_send(i2str(val)); newline(); return val; } return gpin(Nch); @@ -227,7 +246,7 @@ static uint32_t lastET[INMAX+1] = {0}; // last changing time // anti-bouce process esw void proc_esw(){ uint32_t mask = 1, oldesw = ESW_ab_values; - for(uint8_t i = 0; i <= OUTMAX; ++i, mask <<= 1){ + for(uint8_t i = 0; i <= INMAX; ++i, mask <<= 1){ if(Tms - lastET[i] < the_conf.bouncetime) continue; if(NULL == IN[i].port) continue; uint32_t now = pin_read(IN[i].port, IN[i].pin); @@ -236,7 +255,7 @@ void proc_esw(){ lastET[i] = Tms; } if(oldesw != ESW_ab_values){ - usart_send("esw="); usart_send(u2str(ESW_ab_values)); newline(); + //usart_send("esw="); usart_send(u2str(ESW_ab_values)); newline(); CAN_message msg = {.ID = the_conf.CANIDout, .length = 8}; msg.data[0] = CMD_GETESW; *((uint32_t*)(&msg.data[4])) = ESW_ab_values; diff --git a/F1:F103/FX3U/hardware.h b/F1:F103/FX3U/hardware.h index 2024f45..8248f82 100644 --- a/F1:F103/FX3U/hardware.h +++ b/F1:F103/FX3U/hardware.h @@ -34,6 +34,10 @@ #define INMAX (15) #define OUTMAX (11) +// onboard LED - PD10 +#define LEDPORT GPIOD +#define LEDPIN (1<<10) + extern volatile uint32_t Tms; void gpio_setup(void); @@ -44,6 +48,7 @@ int get_esw(uint8_t Nch); void proc_esw(); uint32_t get_ab_esw(); +uint8_t LED(int onoff); uint32_t inchannels(); uint32_t outchannels(); int set_relay(uint8_t Nch, uint32_t val); diff --git a/F1:F103/FX3U/proto.c b/F1:F103/FX3U/proto.c index 14d12a9..cfc53d7 100644 --- a/F1:F103/FX3U/proto.c +++ b/F1:F103/FX3U/proto.c @@ -67,8 +67,10 @@ static const funcdescr funclist[] = { {"saveconf", CMD_SAVECONF, "save configuration"}, {"usartspeed", CMD_USARTSPEED, "get/set USART1 speed"}, {NULL, 0, "IN/OUT"}, + {"adc", CMD_ADCRAW, "get raw ADC values for given channel"}, {"esw", CMD_GETESW, "anti-bounce read inputs"}, {"eswnow", CMD_GETESWNOW, "read current inputs' state"}, + {"led", CMD_LED, "work with onboard LED"}, {"relay", CMD_RELAY, "get/set relay state (0 - off, 1 - on)"}, {NULL, 0, "Other commands"}, {"mcutemp", CMD_MCUTEMP, "get MCU temperature (*10degrC)"}, diff --git a/F1:F103/FX3U/version.inc b/F1:F103/FX3U/version.inc index 144ca8b..627e72b 100644 --- a/F1:F103/FX3U/version.inc +++ b/F1:F103/FX3U/version.inc @@ -1,2 +1,2 @@ -#define BUILD_NUMBER "50" -#define BUILD_DATE "2024-06-03" +#define BUILD_NUMBER "56" +#define BUILD_DATE "2024-06-04"