diff --git a/STM32/TSYS_controller/Readme.md b/STM32/TSYS_controller/Readme.md index 1718406..4d8b3d7 100644 --- a/STM32/TSYS_controller/Readme.md +++ b/STM32/TSYS_controller/Readme.md @@ -4,7 +4,7 @@ Make regular scan of 8 sensors' pairs. USART speed 115200. Code for ../../kicad/stm32 ### Serial interface commands (ends with '\n'), small letter for only local processing: -- **0...9** - wait measurements of T from Nth controller (0==current controller) +- **1...7** - send message to Nth controller, not broadcast (after number should be CAN command) - **B** send dummy CAN messages to broadcast address - **c** show coefficients for all thermosensors - **D** send dummy CAN messages to master (0) address diff --git a/STM32/TSYS_controller/can_process.c b/STM32/TSYS_controller/can_process.c index 3b19e1a..1ac2ebd 100644 --- a/STM32/TSYS_controller/can_process.c +++ b/STM32/TSYS_controller/can_process.c @@ -113,7 +113,7 @@ void can_messages_proc(){ #pragma message("TODO: process received T over USB!") break; default: - SEND("Unknown data received"); + SEND("UNKNOWN_DATA"); } newline(); } @@ -125,7 +125,7 @@ static CAN_status try2send(uint8_t *buf, uint8_t len, uint16_t id){ while(Tms - Tstart < SEND_TIMEOUT_MS){ if(CAN_OK == can_send(buf, len, id)) return CAN_OK; } - SEND("Bus busy!\n"); + SEND("CAN_BUSY\n"); return CAN_BUSY; } diff --git a/STM32/TSYS_controller/main.c b/STM32/TSYS_controller/main.c index 50f49cb..981e8a8 100644 --- a/STM32/TSYS_controller/main.c +++ b/STM32/TSYS_controller/main.c @@ -25,6 +25,7 @@ #include "sensors_manage.h" #include "can.h" #include "can_process.h" +#include "proto.h" #pragma message("USARTNUM=" STR(USARTNUM)) #pragma message("I2CPINS=" STR(I2CPINS)) @@ -35,13 +36,14 @@ #endif volatile uint32_t Tms = 0; +volatile uint8_t canerror = 0; /* Called when systick fires */ void sys_tick_handler(void){ ++Tms; } -void iwdg_setup(){ +static void iwdg_setup(){ /* Enable the peripheral clock RTC */ /* (1) Enable the LSI (40kHz) */ /* (2) Wait while it is not ready */ @@ -62,18 +64,9 @@ void iwdg_setup(){ IWDG->KR = IWDG_REFRESH; /* (6) */ } -void CANsend(uint16_t targetID, uint8_t cmd, char echo){ - if(CAN_OK == can_send_cmd(targetID, cmd)){ - usart_putchar(echo); - newline(); - } -} - int main(void){ uint32_t lastT = 0, lastS = 0; - int16_t L = 0, ID; - uint8_t gotmeasurement = 0, canerror = 0; - char *txt; + uint8_t gotmeasurement = 0; sysreset(); SysTick_Config(6000, 1); gpio_setup(); @@ -125,128 +118,7 @@ int main(void){ gotmeasurement = 0; } if(usartrx()){ // usart1 received data, store in in buffer - L = usart_getline(&txt); - char _1st = txt[0]; - if(L == 2 && txt[1] == '\n'){ - L = 0; - if(_1st > '0' && _1st < '8'){ - ID = (CAN_ID_PREFIX & CAN_ID_MASK) | (_1st - '0'); - CANsend(ID, CMD_START_MEASUREMENT, _1st); - }else switch(_1st){ - case 'B': - CANsend(BCAST_ID, CMD_DUMMY0, _1st); - break; - case 'c': - showcoeffs(); - break; - case 'D': - CANsend(MASTER_ID, CMD_DUMMY1, _1st); - break; - case 'E': - CANsend(BCAST_ID, CMD_STOP_SCAN, _1st); - case 'e': - SEND("End scan mode\n"); - sensors_scan_mode = 0; - break; - case 'F': - CANsend(BCAST_ID, CMD_SENSORS_OFF, _1st); - case 'f': - SEND("Turn off sensors\n"); - sensors_off(); - break; - case 'g': - SEND("Can address: "); - printuhex(getCANID()); - newline(); - break; - case 'H': - CANsend(BCAST_ID, CMD_HIGH_SPEED, _1st); - case 'h': - i2c_setup(HIGH_SPEED); - SEND("High speed\n"); - break; - case 'i': - CAN_reinit(); - SEND("Can address: "); - printuhex(getCANID()); - newline(); - break; - case 'L': - CANsend(BCAST_ID, CMD_LOW_SPEED, _1st); - case 'l': - i2c_setup(LOW_SPEED); - SEND("Low speed\n"); - break; - /*case 'o': - sensors_on(); - break;*/ - case 'P': - CANsend(BCAST_ID, CMD_PING, _1st); - break; - case 'R': - CANsend(BCAST_ID, CMD_REINIT_I2C, _1st); - case 'r': - i2c_setup(CURRENT_SPEED); - SEND("Reinit I2C\n"); - break; - case 'S': - CANsend(BCAST_ID, CMD_START_SCAN, _1st); - case 's': - SEND("Start scan mode\n"); - sensors_scan_mode = 1; - break; - case 'T': - CANsend(BCAST_ID, CMD_START_MEASUREMENT, _1st); - case '0': - case 't': - if(!sensors_scan_mode) sensors_start(); - break; - case 'u': - SEND("CANERROR="); - if(canerror){ - canerror = 0; - usart_putchar('1'); - }else usart_putchar('0'); - newline(); - break; - case 'V': - CANsend(BCAST_ID, CMD_LOWEST_SPEED, _1st); - case 'v': - i2c_setup(VERYLOW_SPEED); - SEND("Very low speed\n"); - break; - case 'Z': - CANsend(BCAST_ID, CMD_SENSORS_STATE, _1st); - break; - default: // help - SEND( - "ALL little letters - without CAN messaging\n" - "0..7 - start measurement on given controller (0 - this)\n" - "B - send broadcast CAN dummy message\n" - "c - show coefficients (current)\n" - "D - send CAN dummy message to master\n" - "Ee- end themperature scan\n" - "Ff- turn oFf sensors\n" - "g - get last CAN address\n" - "Hh- high I2C speed\n" - "i - reinit CAN (with new address)\n" - "Ll- low I2C speed\n" - // "o - turn On sensors\n" - "P - ping everyone over CAN\n" - "Rr- reinit I2C\n" - "Ss- Start themperature scan\n" - "Tt- start temperature measurement\n" - "u - check CAN status for errors\n" - "Vv- very low I2C speed\n" - "Z - get sensors state over CAN\n" - ); - break; - } - } - } - if(L){ // text waits for sending - while(LINE_BUSY == usart_send(txt, L)); - L = 0; + cmd_parser(); } } return 0; diff --git a/STM32/TSYS_controller/proto.c b/STM32/TSYS_controller/proto.c new file mode 100644 index 0000000..6f9574e --- /dev/null +++ b/STM32/TSYS_controller/proto.c @@ -0,0 +1,169 @@ +/* + * geany_encoding=koi8-r + * proto.c + * + * Copyright 2018 Edward V. Emelianov + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + * + */ +#include "hardware.h" +#include "usart.h" +#include "can.h" +#include "can_process.h" +#include "sensors_manage.h" + +extern volatile uint8_t canerror; + +static void CANsend(uint16_t targetID, uint8_t cmd, char echo){ + if(CAN_OK == can_send_cmd(targetID, cmd)){ + usart_putchar(echo); + newline(); + } +} + +void cmd_parser(){ + char *txt = NULL; + int16_t L = 0, ID = BCAST_ID; + L = usart_getline(&txt); + char _1st = txt[0]; + if(_1st >= '0' && _1st < '8'){ // send command to Nth controller, not broadcast + if(L == 3){ // with '\n' at end! + if(_1st == '0'){ + usart_putchar(txt[1]); + _1st = txt[1] + 'a' - 'A'; // change network command to local + newline(); + }else{ + ID = (CAN_ID_PREFIX & CAN_ID_MASK) | (_1st - '0'); + _1st = txt[1]; + } + }else{ + _1st = '?'; // show help + } + }else if(L != 2) _1st = '?'; + switch(_1st){ + case 'B': + CANsend(ID, CMD_DUMMY0, _1st); + break; + case 'c': + showcoeffs(); + break; + case 'D': + CANsend(MASTER_ID, CMD_DUMMY1, _1st); + break; + case 'E': + CANsend(ID, CMD_STOP_SCAN, _1st); + break; + case 'e': + sensors_scan_mode = 0; + break; + case 'F': + CANsend(ID, CMD_SENSORS_OFF, _1st); + break; + case 'f': + sensors_off(); + break; + case 'g': + SEND("Can address: "); + printuhex(getCANID()); + newline(); + break; + case 'H': + CANsend(ID, CMD_HIGH_SPEED, _1st); + break; + case 'h': + i2c_setup(HIGH_SPEED); + break; + case 'i': + CAN_reinit(); + SEND("Can address: "); + printuhex(getCANID()); + newline(); + break; + case 'L': + CANsend(ID, CMD_LOW_SPEED, _1st); + break; + case 'l': + i2c_setup(LOW_SPEED); + break; + case 'P': + CANsend(ID, CMD_PING, _1st); + break; + case 'R': + CANsend(ID, CMD_REINIT_I2C, _1st); + break; + case 'r': + i2c_setup(CURRENT_SPEED); + break; + case 'S': + CANsend(ID, CMD_START_SCAN, _1st); + break; + case 's': + sensors_scan_mode = 1; + break; + case 'T': + CANsend(ID, CMD_START_MEASUREMENT, _1st); + break; + case 't': + if(!sensors_scan_mode) sensors_start(); + break; + case 'u': + SEND("CANERROR="); + if(canerror){ + canerror = 0; + usart_putchar('1'); + }else usart_putchar('0'); + newline(); + break; + case 'V': + CANsend(ID, CMD_LOWEST_SPEED, _1st); + break; + case 'v': + i2c_setup(VERYLOW_SPEED); + break; + case 'Z': + CANsend(ID, CMD_SENSORS_STATE, _1st); + break; + case 'z': + SEND("SSTATE0="); + usart_putchar(sensors_get_state()); + newline(); + break; + default: // help + SEND( + "ALL little letters - without CAN messaging\n" + "0..7 - send command to given controller (0 - this) instead of broadcast\n" + "B - send broadcast CAN dummy message\n" + "c - show coefficients (current)\n" + "D - send CAN dummy message to master\n" + "Ee- end themperature scan\n" + "Ff- turn oFf sensors\n" + "g - get last CAN address\n" + "Hh- high I2C speed\n" + "i - reinit CAN (with new address)\n" + "Ll- low I2C speed\n" + // "o - turn On sensors\n" + "P - ping everyone over CAN\n" + "Rr- reinit I2C\n" + "Ss- Start themperature scan\n" + "Tt- start temperature measurement\n" + "u - check CAN status for errors\n" + "Vv- very low I2C speed\n" + "Z - get sensors state over CAN\n" + ); + break; + } +} diff --git a/STM32/TSYS_controller/proto.h b/STM32/TSYS_controller/proto.h new file mode 100644 index 0000000..b796d13 --- /dev/null +++ b/STM32/TSYS_controller/proto.h @@ -0,0 +1,29 @@ +/* + * geany_encoding=koi8-r + * proto.h + * + * Copyright 2018 Edward V. Emelianov + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + * + */ +#pragma once +#ifndef __PROTO_H__ +#define __PROTO_H__ + +void cmd_parser(); + +#endif // __PROTO_H__ diff --git a/STM32/TSYS_controller/sensors_manage.c b/STM32/TSYS_controller/sensors_manage.c index 71a29d7..fab0f0a 100644 --- a/STM32/TSYS_controller/sensors_manage.c +++ b/STM32/TSYS_controller/sensors_manage.c @@ -133,19 +133,19 @@ static void count_sensors(){ // procedure call each time @ resetting static uint8_t resetproc(){ uint8_t i, ctr = 0; - SEND("pair "); printu(curr_mul_addr); - SEND(" : "); + //SEND("pair "); printu(curr_mul_addr); + //SEND(" : "); for(i = 0; i < 2; ++i){ if(write_i2c(Taddr[i], TSYS01_RESET)){ - usart_putchar('0' + i); + //usart_putchar('0' + i); ++ctr; sens_present[i] |= 1<