diff options
| author | Shlomo Hecht <shlomo@twine-s.com> | 2018-03-19 12:08:17 +0200 |
|---|---|---|
| committer | Shlomo Hecht <shlomo@twine-s.com> | 2018-03-19 12:08:17 +0200 |
| commit | cc4129c557405accb3ced68f7b6cb19dbdb87a24 (patch) | |
| tree | f86827ef77a6ef6733805f8190bd961934543e5c /Software/Embedded_SW | |
| parent | 9d04bbb2b4a2eb2eb14cc813103c856b7bde975a (diff) | |
| download | Tango-cc4129c557405accb3ced68f7b6cb19dbdb87a24.tar.gz Tango-cc4129c557405accb3ced68f7b6cb19dbdb87a24.zip | |
Driver structures and prototypes
Diffstat (limited to 'Software/Embedded_SW')
28 files changed, 2963 insertions, 73 deletions
diff --git a/Software/Embedded_SW/Embedded/Common/Utilities/Utils.c b/Software/Embedded_SW/Embedded/Common/Utilities/Utils.c index e732d2d16..0a586d9cb 100644 --- a/Software/Embedded_SW/Embedded/Common/Utilities/Utils.c +++ b/Software/Embedded_SW/Embedded/Common/Utilities/Utils.c @@ -216,7 +216,8 @@ void cb_free(circular_buffer *cb) void cb_push_back(/*circular_buffer *cb,*/ const void *item, int size) { return; - if(cb.count == cb.capacity){ + /* + if(cb.count == cb.capacity){ return; // handle error } @@ -231,6 +232,8 @@ void cb_push_back(/*circular_buffer *cb,*/ const void *item, int size) cb.count++; // if (sendDataToHost) // SendMessageToHost(DEBUG_LOG,item); + + */ } void cb_pop_front(/*circular_buffer *cb,*/ void *item) diff --git a/Software/Embedded_SW/Embedded/Common/report/reportInit.c b/Software/Embedded_SW/Embedded/Common/report/reportInit.c index de1eaa06e..36fb94788 100644 --- a/Software/Embedded_SW/Embedded/Common/report/reportInit.c +++ b/Software/Embedded_SW/Embedded/Common/report/reportInit.c @@ -34,15 +34,10 @@ #include "PMR/common/MessageContainer.pb-c.h" #include "PMR/debugging/DebugLogRequest.pb-c.h" #include "PMR/debugging/DebugLogResponse.pb-c.h" -#include "Drivers/USB_Communication/USBCDCD.h" - +#include "drivers/twine_usblib/usb_serial_adapter.h" //#include "graphics_adapter.h" #include "Modules/Stubs_Handler/Container.h" -#include "Modules/thread/thread.h" - -MotorConfigStruc MotorsCfg[MAX_THREAD_MOTORS_NUM]; -InternalWinderConfigStruc InternalWinderCfg; char protobufToken[36] = {0}; /*void SendStatusResponse (uint32_t Code, char *Token) { diff --git a/Software/Embedded_SW/Embedded/Drivers/Heater/Heater.c b/Software/Embedded_SW/Embedded/Drivers/Heater/Heater.c index 0b85053c0..7295180ec 100644 --- a/Software/Embedded_SW/Embedded/Drivers/Heater/Heater.c +++ b/Software/Embedded_SW/Embedded/Drivers/Heater/Heater.c @@ -8,6 +8,10 @@ #include <stdint.h> #include <string.h> +#include "include.h" +#include "heater.h" +#include "TemperatureSensor.h" + #include "inc/hw_memmap.h" #include "driverlib/rom.h" diff --git a/Software/Embedded_SW/Embedded/Drivers/Heater/Heater.h b/Software/Embedded_SW/Embedded/Drivers/Heater/Heater.h index 9292f10b7..1581a3f0f 100644 --- a/Software/Embedded_SW/Embedded/Drivers/Heater/Heater.h +++ b/Software/Embedded_SW/Embedded/Drivers/Heater/Heater.h @@ -1,7 +1,10 @@ #define HEATER_ON true #define HEATER_OFF false +#include "include.h" + uint32_t ActivateHeater (int HeaterId); uint32_t DeActivateHeater (int HeaterId); bool GetHeaterState (int HeaterId); +uint32_t HeaterSetTemperatureWithCallback (int HeaterId, uint32_t RequestedTemperature, callback_fptr callback); diff --git a/Software/Embedded_SW/Embedded/Drivers/Heater/TemperatureSensor.c b/Software/Embedded_SW/Embedded/Drivers/Heater/TemperatureSensor.c new file mode 100644 index 000000000..bcef41029 --- /dev/null +++ b/Software/Embedded_SW/Embedded/Drivers/Heater/TemperatureSensor.c @@ -0,0 +1,33 @@ +/* + * TemperatureSensor.c + * + * Created on: 15 במרץ 2018 + * Author: shlomo + */ + +#include "include.h" +#include "heater.h" +#include "TemperatureSensor.h" + +uint32_t TemperatureSensorsInit(void) +{ + return OK; +} +uint32_t TemperatureSensorConfig(TEMPERATURE_SENSOR_ID_ENUM SensorId, void *TemperatureSensorConfig) +{ + return OK; +} + +uint32_t TemperatureSensorRead(TEMPERATURE_SENSOR_ID_ENUM SensorId) +{ + return OK; +} + +uint32_t TemperatureSensorReadFromFPGA(TEMPERATURE_SENSOR_ID_ENUM SensorId) +{ + return OK; +} + + + + diff --git a/Software/Embedded_SW/Embedded/Drivers/Heater/TemperatureSensor.h b/Software/Embedded_SW/Embedded/Drivers/Heater/TemperatureSensor.h new file mode 100644 index 000000000..10cfa4bcf --- /dev/null +++ b/Software/Embedded_SW/Embedded/Drivers/Heater/TemperatureSensor.h @@ -0,0 +1,42 @@ +/* + * TemperatureSensor.h + * + * Created on: 15 במרץ 2018 + * Author: shlomo + */ + +#ifndef DRIVERS_HEATER_TEMPERATURESENSOR_H_ +#define DRIVERS_HEATER_TEMPERATURESENSOR_H_ + +#include "include.h" +#include "heater.h" + +typedef enum { +TEMP_SENSE_ANALOG_MIXCHIP_TEMP, +TEMP_SENSE_ANALOG_DYEINGH_TEMP1, +TEMP_SENSE_ANALOG_DYEINGH_TEMP2, +TEMP_SENSE_ANALOG_DYEINGH_TEMP3, +TEMP_SENSE_ANALOG_DYEINGH_TEMP4, +TEMP_SENSE_ANALOG_DYEINGH_TEMP5, +TEMP_SENSE_ANALOG_DRYER_TEMP1, +TEMP_SENSE_ANALOG_DRYER_TEMP2, +TEMP_SENSE_ANALOG_DRYER_TEMP3, +TEMP_SENSE_AN_ENCLOSURETEMP1, +TEMP_SENSE_AN_ENCLOSURETEMP2, +TEMP_SENSE_AN_ENCLOSURETEMP3, +MAX_TEMPERATURE_SENSOR_ID +} TEMPERATURE_SENSOR_ID_ENUM; + +uint32_t TemperatureSensorsInit(void); +//set the motor configuration +uint32_t TemperatureSensorConfig(TEMPERATURE_SENSOR_ID_ENUM SensorId, void *TemperatureSensorConfig); + +uint32_t TemperatureSensorRead(TEMPERATURE_SENSOR_ID_ENUM SensorId); + +uint32_t TemperatureSensorReadFromFPGA(TEMPERATURE_SENSOR_ID_ENUM SensorId); + + + + + +#endif /* DRIVERS_HEATER_TEMPERATURESENSOR_H_ */ diff --git a/Software/Embedded_SW/Embedded/Drivers/Motors/Motor.c b/Software/Embedded_SW/Embedded/Drivers/Motors/Motor.c new file mode 100644 index 000000000..af03ef4ef --- /dev/null +++ b/Software/Embedded_SW/Embedded/Drivers/Motors/Motor.c @@ -0,0 +1,113 @@ +/* + * Motor.c + * + * Created on: 15 במרץ 2018 + * Author: shlomo + */ +/************************************************************************************************ + * TimerMotor.h + * this module is used to operate and control over dispensers and winding, using clock object + * the motors which are not connected to the PWM pins and needs microstepping + * + *************************************************************************************************/ + +#include "include.h" +#include "motor.h" +#include "Modules/thread/thread.h" + +#include <stdint.h> +#include <stdbool.h> + +MotorDriverConfigStruc MotorDriverCfg[NUM_OF_MOTORS]; +TimerMotors_t FastMotorToMotorId[4] = {MOTOR_RDRIVING,MOTOR_DRYER_DRIVING,MOTOR_LDRIVING,MOTOR_WINDER}; + +//initialize general motors driver database +uint32_t MotorsInit(void) +{ + return OK; +} +//set the motor configuration +uint32_t MotorConfig(TimerMotors_t _motorId, HardwareMotor *MotorConfig) +{ + return OK; +} + + +TimerMotors_t getMotorId(int FastMotorId) +{ + return FastMotorToMotorId[FastMotorId]; +} + +uint32_t MotorGetDirection(TimerMotors_t _motorId) +{ + return OK; +} + +uint32_t MotorSetDirection(TimerMotors_t _motorId,bool _direction) +{ + return OK; +} + +uint32_t MotorSetSpeed(TimerMotors_t _motorId, uint32_t _freq, int MicroSteps) +{ + return OK; +} + +uint32_t MotorGetSpeed(TimerMotors_t _motorId) +{ + return OK; +} + +uint32_t MotorGetSpeedFromFPGA(TimerMotors_t _motorId) +{ + return OK; +} + +uint32_t MotorGetStatusFromFPGA(TimerMotors_t _motorId) +{ + return OK; +} + +uint32_t MotorGetMicroSteps(TimerMotors_t _motorId) +{ + return OK; +} + +uint32_t MotorGetMicroStepsFromFPGA(TimerMotors_t _motorId) +{ + return OK; +} +uint32_t MotorGetnBusyFromFPGA(void) // get all motors nBusy bit status from the FPGAs +{ + return OK; +} +bool MotorGetnBusyState(TimerMotors_t _motorId) +{ + return true; +} + + +/* + * Stop Command + * 1 - Hard_Stop/ 2 - Soft_Stop/ 3 - Hard_Hiz/ 4 - Soft_Hiz + */ +uint32_t StopMotor(TimerMotors_t _motorId, int StopType) +{ + return OK; +} + +uint32_t MotorMove(TimerMotors_t _motorId,bool direction, uint32_t Steps) +{ + return OK; +} + +uint32_t MotorMoveWithCallback (TimerMotors_t _motorId,bool direction, uint32_t Steps, callback_fptr callback) +{ + return OK; +} + +uint32_t MotorSetSpeedWithCallback (TimerMotors_t _motorId, uint32_t _freq, int MicroSteps, callback_fptr callback) +{ + return OK; +} + diff --git a/Software/Embedded_SW/Embedded/Drivers/Motors/Motor.h b/Software/Embedded_SW/Embedded/Drivers/Motors/Motor.h new file mode 100644 index 000000000..0098a5036 --- /dev/null +++ b/Software/Embedded_SW/Embedded/Drivers/Motors/Motor.h @@ -0,0 +1,135 @@ +/************************************************************************************************ + * TimerMotor.h + * this module is used to operate and control over dispensers and winding, using clock object + * the motors which are not connected to the PWM pins and needs microstepping + * + *************************************************************************************************/ + +#ifndef DRIVERS_MOTOR_H_ +#define DRIVERS_MOTOR_H_ +#include "include.h" +#include "Modules/thread/thread.h" + +#include <stdint.h> +#include <stdbool.h> +/* +MOT01,DH,MOTO_DH_CLEANHEAD +MOT02,DH,MOTO_DH_CLEANMECH +MOT03,DH,MOTO_DH_LID +MOT04,DRYER,MOTO_DRYER_DRIVING +MOT05,DRYER,MOTO_DRYER_LID +MOT06,DRYER,MOTO_DRYER_LOADARM +MOT07,IDS,MOTO_DISPENSER_1 +MOT08,IDS,MOTO_DISPENSER_2 +MOT09,IDS,MOTO_DISPENSER_3 +MOT10,IDS,MOTO_DISPENSER_4 +MOT11,IDS,MOTO_DISPENSER_5 +MOT12,IDS,MOTO_DISPENSER_6 +MOT13,IDS,MOTO_DISPENSER_7 +MOT14,IDS,MOTO_DISPENSER_8 +MOT15,INT. WINDER,MOTO_SCREW +MOT16,INT. WINDER,MOTO_WINDER +MOT17,L-TFU,MOTO_LDANCER1 +MOT18,L-TFU,MOTO_LDANCER2 +MOT19,L-TFU,MOTO_LDRIVING +MOT20,L-TFU,MOTO_LLOADING +MOT21,L-TFU,MOTO_LPIVOT1 +MOT22,R-TFU,MOTO_RDANCER +MOT23,R-TFU,MOTO_RDRIVING +MOT24,R-TFU,MOTO_RLOADARM +MOT25,R-TFU,MOTO_RLOADING +MOT26,SPARE,MOTO_SPARE1_1 +MOT27,SPARE,MOTO_SPARE1_2 +MOT28,SPARE,MOTO_SPARE2_1 +MOT29,SPARE,MOTO_SPARE2_2 + + * */ +typedef enum +{ + MOTOR_DH_CLEANHEAD, // cleaning head motor + MOTOR_DH_CLEANMECH, // cleaning mechanism + MOTOR_DH_LID, // dyeing head lid + MOTOR_DRYER_DRIVING, // dryer main motor + MOTOR_DRYER_LID, // drier lid + MOTOR_DRYER_LOADARM, // drier thread loading arm + MOTOR_DISPENSER_1, // Dispenser + MOTOR_DISPENSER_2, // Dispenser + MOTOR_DISPENSER_3, // Dispenser + MOTOR_DISPENSER_4, // Dispenser + MOTOR_DISPENSER_5, // Dispenser + MOTOR_DISPENSER_6, // Dispenser + MOTOR_DISPENSER_7, // Dispenser + MOTOR_DISPENSER_8, // Dispenser + MOTOR_SCREW, // winder screw + MOTOR_WINDER, // Winder + MOTOR_LDANCER1, // pooler Dancer adjustment + MOTOR_LDANCER2, // winder Dancer adjustment #1 + MOTOR_LDRIVING, // pooler + MOTOR_LLOADING, // pooler loading + MOTOR_LPIVOT1, // pooler loading pivot arm + MOTOR_RDANCER, // feeder dancer adjustment + MOTOR_RDRIVING, // Feeder + MOTOR_RLOADARM, // Right loading arm + MOTOR_RLOADING, // Right Loading + MOTOR_SPARE1_1, // Spare + MOTOR_SPARE1_2, // Spare + MOTOR_SPARE2_1, // Spare + MOTOR_SPARE2_2, // Spare + NUM_OF_MOTORS +} TimerMotors_t; + +typedef struct +{ + TimerMotors_t id; + uint32_t minfreq; + uint32_t maxfreq; + uint32_t minmicrostep; + uint32_t maxmicrostep; + double changeslope; + double hightimeoutusec; +}MotorDriverConfigStruc; + + + +extern MotorDriverConfigStruc MotorDriverCfg[NUM_OF_MOTORS]; + +//initialize general motors driver database +uint32_t MotorsInit(void); +//set the motor configuration +uint32_t MotorConfig(TimerMotors_t _motorId, HardwareMotor *MotorConfig); + +TimerMotors_t getMotorId(int FastMotorId); + +uint32_t MotorGetDirection(TimerMotors_t _motorId); + +uint32_t MotorSetDirection(TimerMotors_t _motorId,bool _direction); + +uint32_t MotorSetSpeed(TimerMotors_t _motorId, uint32_t _freq, int MicroSteps); + +uint32_t MotorGetSpeed(TimerMotors_t _motorId); + +uint32_t MotorGetSpeedFromFPGA(TimerMotors_t _motorId); + +uint32_t MotorGetStatusFromFPGA(TimerMotors_t _motorId); + +uint32_t MotorGetMicroSteps(TimerMotors_t _motorId); + +uint32_t MotorGetMicroStepsFromFPGA(TimerMotors_t _motorId); + +uint32_t MotorGetnBusyFromFPGA(void); // get all motors nBusy bit status from the FPGAs + +bool MotorGetnBusyState(TimerMotors_t _motorId); + +/* + * Stop Command + * 1 - Hard_Stop/ 2 - Soft_Stop/ 3 - Hard_Hiz/ 4 - Soft_Hiz + */ +uint32_t StopMotor(TimerMotors_t _motorId, int StopType); + +uint32_t MotorMove(TimerMotors_t _motorId,bool direction, uint32_t Steps); + +uint32_t MotorMoveWithCallback (TimerMotors_t _motorId,bool direction, uint32_t Steps, callback_fptr callback); + +uint32_t MotorSetSpeedWithCallback (TimerMotors_t _motorId, uint32_t _freq, int MicroSteps, callback_fptr callback); + +#endif /* DRIVERS_MOTOR_H_ */ diff --git a/Software/Embedded_SW/Embedded/Drivers/Peripheral_GPIO/GPIO.h b/Software/Embedded_SW/Embedded/Drivers/Peripheral_GPIO/GPIO.h index 8a0c54df4..5c6ba7d36 100644 --- a/Software/Embedded_SW/Embedded/Drivers/Peripheral_GPIO/GPIO.h +++ b/Software/Embedded_SW/Embedded/Drivers/Peripheral_GPIO/GPIO.h @@ -8,19 +8,158 @@ typedef enum { - DISPENSER_1_LMS, - DISPENSER_2_LMS, - DISPENSER_3_LMS, - DISPENSER_4_LMS, - DISPENSER_5_LMS, - DISPENSER_6_LMS, - SCREW_LMS, - START_MANUAL_BTN, - END_MANUAL_BTN, - MANUAL_1_BTN, - MANUAL_2_BTN, - MANUAL_3_BTN, - MANUAL_4_BTN, + GPI001_LS_DH_CLEAN_DOWN, + GPI002_LS_DH_CLEAN_LEFT, + GPI003_LS_DH_CLEAN_RIGHT, + GPI004_LS_DH_CLEAN_UP, + GPI005_LS_DH_LID_CLEANING, + GPI006_LS_DH_LID_CLOSED, + GPI007_LS_DH_LID_OPEN, + GPI008_LS_DH_SPARE1, + GPI009_LS_DRYER_LID_CLOSED, + GPI010_LS_DRYER_LID_OPEN, + GPI011_LS_DRYER_SPARE1, + GPI012_LS_DRYER_SPARE2, + GPI013_LS_DRYER_SPARE3, + GPI014_LS_DRYER_SPARE4, + GPI015_LS_LOADARM_LEFT, + GPI016_LS_LOADARM_RIGHT, + GPI017_GPI_PS1_DC_OK, + GPI018_GPI_PANSW1, + GPI019_GPI_PANSW2, + GPI020_GPI_PANSW3, + GPI021_GPI_PANSW4, + GPI022_GPI_PANSW5, + GPI023_GPI_PANSW6, + GPI024_GPI_PWRBUTTON, + GPI025_GPI_TACTSW1, + GPI026_GPI_TACTSW2, + GPI027_GPI_TACTSW3, + GPI028_GPI_TACTSW4, + GPI029_GPI_EXTWINDER_1, + GPI030_GPI_EXTWINDER_2, + GPI031_GPI_EXTWINDER_3, + GPI032_LS_DISPENSER_25_1, + GPI033_LS_DISPENSER_25_2, + GPI034_LS_DISPENSER_25_3, + GPI035_LS_DISPENSER_25_4, + GPI036_LS_DISPENSER_25_5, + GPI037_LS_DISPENSER_25_6, + GPI038_LS_DISPENSER_25_7, + GPI039_LS_DISPENSER_25_8, + GPI040_LS_DISPENSER_50_1, + GPI041_LS_DISPENSER_50_2, + GPI042_LS_DISPENSER_50_3, + GPI043_LS_DISPENSER_50_4, + GPI044_LS_DISPENSER_50_5, + GPI045_LS_DISPENSER_50_6, + GPI046_LS_DISPENSER_50_7, + GPI047_LS_DISPENSER_50_8, + GPI048_LS_DISPENSER_75_1, + GPI049_LS_DISPENSER_75_2, + GPI050_LS_DISPENSER_75_3, + GPI051_LS_DISPENSER_75_4, + GPI052_LS_DISPENSER_75_5, + GPI053_LS_DISPENSER_75_6, + GPI054_LS_DISPENSER_75_7, + GPI055_LS_DISPENSER_75_8, + GPI056_LS_DISPENSER_DOWN_1, + GPI057_LS_DISPENSER_DOWN_2, + GPI058_LS_DISPENSER_DOWN_3, + GPI059_LS_DISPENSER_DOWN_4, + GPI060_LS_DISPENSER_DOWN_5, + GPI061_LS_DISPENSER_DOWN_6, + GPI062_LS_DISPENSER_DOWN_7, + GPI063_LS_DISPENSER_DOWN_8, + GPI064_LS_DISPENSER_SPARE_1, + GPI065_LS_DISPENSER_SPARE_2, + GPI066_LS_DISPENSER_SPARE_3, + GPI067_LS_DISPENSER_SPARE_4, + GPI068_LS_DISPENSER_SPARE_5, + GPI069_LS_DISPENSER_SPARE_6, + GPI070_LS_DISPENSER_SPARE_7, + GPI071_LS_DISPENSER_SPARE_8, + GPI072_LS_DISPENSER_UP_1, + GPI073_LS_DISPENSER_UP_2, + GPI074_LS_DISPENSER_UP_3, + GPI075_LS_DISPENSER_UP_4, + GPI076_LS_DISPENSER_UP_5, + GPI077_LS_DISPENSER_UP_6, + GPI078_LS_DISPENSER_UP_7, + GPI079_LS_DISPENSER_UP_8, + GPI080_MIDTANK1_LVL1_FLOAT, + GPI081_MIDTANK1_LVL2_FLOAT, + GPI082_MIDTANK1_LVL3_FLOAT, + GPI083_MIDTANK1_LVL4_FLOAT, + GPI084_MIDTANK2_LVL1_FLOAT, + GPI085_MIDTANK2_LVL2_FLOAT, + GPI086_MIDTANK2_LVL3_FLOAT, + GPI087_MIDTANK2_LVL4_FLOAT, + GPI088_MIDTANK3_LVL1_FLOAT, + GPI089_MIDTANK3_LVL2_FLOAT, + GPI090_MIDTANK3_LVL3_FLOAT, + GPI091_MIDTANK3_LVL4_FLOAT, + GPI092_MIDTANK4_LVL1_FLOAT, + GPI093_MIDTANK4_LVL2_FLOAT, + GPI094_MIDTANK4_LVL3_FLOAT, + GPI095_MIDTANK4_LVL4_FLOAT, + GPI096_MIDTANK5_LVL1_FLOAT, + GPI097_MIDTANK5_LVL2_FLOAT, + GPI098_MIDTANK5_LVL3_FLOAT, + GPI099_MIDTANK5_LVL4_FLOAT, + GPI100_MIDTANK6_LVL1_FLOAT, + GPI101_MIDTANK6_LVL2_FLOAT, + GPI102_MIDTANK6_LVL3_FLOAT, + GPI103_MIDTANK6_LVL4_FLOAT, + GPI104_MIDTANK7_LVL1_FLOAT, + GPI105_MIDTANK7_LVL2_FLOAT, + GPI106_MIDTANK7_LVL3_FLOAT, + GPI107_MIDTANK7_LVL4_FLOAT, + GPI108_MIDTANK8_LVL1_FLOAT, + GPI109_MIDTANK8_LVL2_FLOAT, + GPI110_MIDTANK8_LVL3_FLOAT, + GPI111_MIDTANK8_LVL4_FLOAT, + GPI112_LS_SCREW_LEFT, + GPI113_LS_SCREW_RIGHT, + GPI114_SW_SPARE, + GPI115_SW_SPOOL_EXISTS, + GPI116_LS_LDANCER1_DOWN, + GPI117_LS_LDANCER1_UP, + GPI118_LS_LDANCER2_DOWN, + GPI119_LS_LDANCER2_UP, + GPI120_LS_LLOADMOTOR_DOWN, + GPI121_LS_LLOADMOTOR_UP, + GPI122_LS_LPIVOT_DOWN, + GPI123_LS_LPIVOT_UP, + GPI124_LS_LSPARE1, + GPI125_LS_LSPARE2, + GPI126_LS_PIVOT_SPARE1, + GPI127_LS_PIVOT_SPARE2, + GPI128_LS_RLOADMOTOR_UP, + GPI129_GPI_TFEED_BREAK_1, + GPI130_GPI_TFEED_BREAK_2, + GPI131_LS_RDANCER_DOWN, + GPI132_LS_RDANCER_UP, + GPI133_LS_RLOADMOTOR_DOWN, + GPI134_LS_RLOADRAM_DOWN, + GPI135_LS_RLOADRAM_UP, + GPI136_LS_RSPARE1, + GPI137_LS_RSPARE2, + GPI138_LS_SPARE1_1, + GPI139_LS_SPARE1_2, + GPI140_LS_SPARE1_3, + GPI141_LS_SPARE1_4, + GPI142_LS_SPARE2_1, + GPI143_LS_SPARE2_2, + GPI144_LS_SPARE2_3, + GPI145_LS_SPARE2_4, + GPI146_GPI_AIRFLOW_FLAP, + GPI147_GPI_BLOWER_TACH, + GPI148_GPI_CHILLER_FAULT, + GPI149_GPI_CHILLER_STAT1, + GPI150_GPI_SW_FILTER_PRES, + GPI151_GPI_WCONTAINER_FULL, + GPI152_GPI_WCONTAINER_WARN, NUM_OF_GPIO_INT }GPIOInt_t; diff --git a/Software/Embedded_SW/Embedded/Drivers/USB_Communication/USBCDCD - Copy.new b/Software/Embedded_SW/Embedded/Drivers/USB_Communication/USBCDCD - Copy.new new file mode 100644 index 000000000..f8b333c8a --- /dev/null +++ b/Software/Embedded_SW/Embedded/Drivers/USB_Communication/USBCDCD - Copy.new @@ -0,0 +1,1063 @@ + +/************************************************************************************ + * this module is represents the access layer to USB STACK DRIVER + * used to communication over USB protocol + * the USB protocol implementation is inside the module no need for another includes + * ======== USBCDCD.c ======== + ************************************************************************************/ + +/* XDCtools Header files */ +#include <xdc/std.h> +#include <xdc/runtime/Error.h> +#include <xdc/runtime/System.h> + +/* BIOS Header files */ +#include <ti/sysbios/BIOS.h> +#include <ti/sysbios/gates/GateMutex.h> +#include <ti/sysbios/hal/Hwi.h> +#include <ti/sysbios/knl/Semaphore.h> +#include <ti/sysbios/knl/Task.h> + +#include <stdbool.h> +#include <stdint.h> + +/* driverlib Header files */ +#include <inc/hw_ints.h> +#include <inc/hw_types.h> + +#include <driverlib/rom.h> +#include <driverlib/sysctl.h> +#include <driverlib/usb.h> +#include <inc/hw_memmap.h> + +/* usblib Header files */ +#include <usblib/usb-ids.h> +#include <usblib/usblib.h> +#include <usblib/usbcdc.h> +#include <usblib/device/usbdevice.h> +#include <usblib/device/usbdcdc.h> +#include <usblib/device/usbddfu-rt.h> +#include <usblib/device/usbdcomp.h> +#include "Drivers/USB_Communication/USBCDCD.h" + +//#include "Common/Sys_Configuration/Configuration.h" +//#include "Communication/Communication.h" + +#include <utils/ustdlib.h> +#include "Common/Utilities/Utils.h" +//#include "Communication/HostMessaging.h" + +#if defined(TIVAWARE) +typedef uint32_t USBCDCDEventType; +#else +#define eUSBModeForceDevice USB_MODE_FORCE_DEVICE +typedef unsigned long USBCDCDEventType; +#endif + +/* Defines */ +#define USBBUFFERSIZE 256 +//**************************************************************************** +// +// A buffer into which the composite device can write the combined config +// descriptor. +// +//**************************************************************************** +#define DESCRIPTOR_BUFFER_SIZE (COMPOSITE_DDFU_SIZE + COMPOSITE_DCDC_SIZE) + +extern Semaphore_Handle updateSem; +extern Semaphore_Handle ReconnectSem; + +uint8_t g_pui8DescriptorBuffer[DESCRIPTOR_BUFFER_SIZE]; + +/* Typedefs */ +typedef volatile enum { + USBCDCD_STATE_IDLE = 0, + USBCDCD_STATE_INIT, + USBCDCD_STATE_UNCONFIGURED +} USBCDCD_USBState; + +/* Static variables and handles */ +static volatile USBCDCD_USBState state; +static unsigned char receiveBuffer[USBBUFFERSIZE]; +static unsigned char transmitBuffer[USBBUFFERSIZE]; + +static GateMutex_Handle gateTxSerial; +static GateMutex_Handle gateRxSerial; +static GateMutex_Handle gateUSBWait; +static Semaphore_Handle semTxSerial; +static Semaphore_Handle semRxSerial; +static Semaphore_Handle semUSBConnected; +extern Semaphore_Handle initConnectionSem; + +/* Function prototypes */ +static USBCDCDEventType cbRxHandler(void *cbData, USBCDCDEventType event, + USBCDCDEventType _eventMsgData, + void *eventMsgPtr); +static USBCDCDEventType cbSerialHandler(void *cbData, USBCDCDEventType event, + USBCDCDEventType _eventMsgData, + void *eventMsgPtr); +static USBCDCDEventType cbTxHandler(void *cbData, USBCDCDEventType event, + USBCDCDEventType _eventMsgData, + void *eventMsgPtr); +void USBCDCD_hwiHandler(UArg arg0); +static unsigned int rxData(unsigned char *_pBuff, + unsigned int _length, + unsigned int _timeout); +static unsigned int txData(const unsigned char *_pBuff, + int _length, unsigned int _timeout); +void USBCDCD_init(void); + +unsigned int USBCDCD_receiveData(unsigned char *_pBuff, + unsigned int _length, + unsigned int _timeout); +unsigned int USBCDCD_sendData(const unsigned char *_pBuff, + unsigned int _length, + unsigned int _timeout); +bool USBCDCD_waitForConnect(unsigned int _timeout); + +uint32_t DFUDetachCallback(void *pvCBData, uint32_t ui32Event, + uint32_t ui32MsgData, void *pvMsgData); + +/* The languages supported by this device. */ +const unsigned char langDescriptor[] = { + 4, + USB_DTYPE_STRING, + USBShort(USB_LANG_EN_US) +}; + +/* The manufacturer string. */ +const unsigned char manufacturerString[] = { + (17 + 1) * 2, + USB_DTYPE_STRING, + 'T', 0, 'e', 0, 'x', 0, 'a', 0, 's', 0, ' ', 0, 'I', 0, 'n', 0, 's', 0, + 't', 0, 'r', 0, 'u', 0, 'm', 0, 'e', 0, 'n', 0, 't', 0, 's', 0, +}; + +/* The product string. */ +const unsigned char productString[] = { + 2 + (13 * 2), + USB_DTYPE_STRING, + 'T', 0, 'w', 0, 'i', 0, 'n', 0, 'e', 0, ' ', 0, 'J', 0, 'i', 0, + 'g', 0, ' ', 0, 'U', 0, 'S', 0, 'B', 0, +}; + +/* The serial number string. */ +const unsigned char serialNumberString[] = { + (8 + 1) * 2, + USB_DTYPE_STRING, + '1', 0, '2', 0, '3', 0, '4', 0, '5', 0, '6', 0, '7', 0, '8', 0 +}; + +/* The interface description string. */ +const unsigned char controlInterfaceString[] = { + 2 + (21 * 2), + USB_DTYPE_STRING, + 'J', 0, 'i', 0, 'g', 0, ' ', 0, 'C', 0, 'o', 0, 'n', 0, 't', 0, + 'r', 0, 'o', 0, 'l', 0, ' ', 0, 'I', 0, 'n', 0, 't', 0, 'e', 0, + 'r', 0, 'f', 0, 'a', 0, 'c', 0, 'e', 0 +}; + +/* The configuration description string. */ +const unsigned char configString[] = { + 2 + (26 * 2), + USB_DTYPE_STRING, + 'S', 0, 'e', 0, 'l', 0, 'f', 0, ' ', 0, 'P', 0, 'o', 0, 'w', 0, + 'e', 0, 'r', 0, 'e', 0, 'd', 0, ' ', 0, 'C', 0, 'o', 0, 'n', 0, + 'f', 0, 'i', 0, 'g', 0, 'u', 0, 'r', 0, 'a', 0, 't', 0, 'i', 0, + 'o', 0, 'n', 0 +}; + +//***************************************************************************** +// +// The descriptor string table. +// +//***************************************************************************** +const uint8_t * const g_ppui8StringDescriptors[] = +{ + langDescriptor, + manufacturerString, + productString, + serialNumberString, + controlInterfaceString, + configString +}; + +#define NUM_STRING_DESCRIPTORS (sizeof(g_ppui8StringDescriptors) / \ + sizeof(g_ppui8StringDescriptors[0])) + +/* The descriptor string table. */ +const unsigned char * const stringDescriptors[] = { + langDescriptor, + manufacturerString, + productString, + serialNumberString, + controlInterfaceString, + configString +}; + +#define STRINGDESCRIPTORSCOUNT (sizeof(stringDescriptors) / \ + sizeof(unsigned char *)) + +#if defined(TIVAWARE) +tUSBBuffer txBuffer; +tUSBBuffer rxBuffer; +static tUSBDCDCDevice g_sCDCDevice; + +tUSBBuffer rxBuffer = +{ + false, /* This is a receive buffer. */ + cbRxHandler, /* pfnCallback */ + (void *)&g_sCDCDevice, /* Callback data is our device pointer. */ + USBDCDCPacketRead, /* pfnTransfer */ + USBDCDCRxPacketAvailable, /* pfnAvailable */ + (void *)&g_sCDCDevice, /* pvHandle */ + receiveBuffer, /* pcBuffer */ + USBBUFFERSIZE, /* ulBufferSize */ + {{0, 0, 0, 0}, 0, 0} /* private data workspace */ +}; + +tUSBBuffer txBuffer = +{ + true, /* This is a transmit buffer. */ + cbTxHandler, /* pfnCallback */ + (void *)&g_sCDCDevice, /* Callback data is our device pointer. */ + USBDCDCPacketWrite, /* pfnTransfer */ + USBDCDCTxPacketAvailable, /* pfnAvailable */ + (void *)&g_sCDCDevice, /* pvHandle */ + transmitBuffer, /* pcBuffer */ + USBBUFFERSIZE, /* ulBufferSize */ + {{0, 0, 0, 0}, 0, 0} /* private data workspace */ +}; + +static tUSBDCDCDevice g_sCDCDevice = +{ + USB_VID_TI_1CBE, + USB_PID_SERIAL, + 0, + USB_CONF_ATTR_SELF_PWR, + + cbSerialHandler, + (void *)&g_sCDCDevice, + + USBBufferEventCallback, + (void *)&rxBuffer, + + USBBufferEventCallback, + (void *)&txBuffer, + + stringDescriptors, + STRINGDESCRIPTORSCOUNT +}; + +//***************************************************************************** +// +// The DFU runtime interface initialization and customization structures +// +//***************************************************************************** +tUSBDDFUDevice g_sDFUDevice = +{ + DFUDetachCallback, + (void *)&g_sDFUDevice +}; + +//**************************************************************************** +// +// The number of device class instances that this composite device will +// use. +// +//**************************************************************************** +#define NUM_DEVICES 2 + +//**************************************************************************** +// +// The array of devices supported by this composite device. +// +//**************************************************************************** +tCompositeEntry g_psCompDevices[NUM_DEVICES]; + +//**************************************************************************** +// +// Additional workspace required by the composite driver to hold a lookup +// table allowing mapping of composite interface and endpoint numbers to +// individual device class instances. +// +//**************************************************************************** +uint32_t g_pui32CompWorkspace[NUM_DEVICES]; + +//**************************************************************************** +// +// The instance data for this composite device. +// +//**************************************************************************** +tCompositeInstance g_sCompInstance; + +//**************************************************************************** +// +// Allocate the Device Data for the top level composite device class. +// +//**************************************************************************** +tUSBDCompositeDevice g_sCompDevice = +{ + // + // Stellaris VID. + // + USB_VID_TI_1CBE, + + // + // Stellaris PID for composite SERIAL/DFU device. + // + USB_PID_COMP_SERIAL, + + // + // This is in milliamps. + // + 250, + + // + // Bus powered device. + // + USB_CONF_ATTR_BUS_PWR, + + // + // Device event handler function pointer (receives connect, disconnect + // and other device-level notifications). + // + cbSerialHandler, + + // + // The string table. + // + g_ppui8StringDescriptors, + NUM_STRING_DESCRIPTORS, + + // + // The Composite device array. + // + NUM_DEVICES, + g_psCompDevices, +}; + +#else +#define USB_BUFFER_WORKSPACE_SIZE 1200 +const tUSBBuffer rxBuffer; +const tUSBBuffer txBuffer; +static unsigned char receiveBufferWorkspace[USB_BUFFER_WORKSPACE_SIZE]; +static unsigned char transmitBufferWorkspace[USB_BUFFER_WORKSPACE_SIZE]; + +static tCDCSerInstance serialInstance; +const tUSBDCDCDevice g_sCDCDevice; + +const tUSBBuffer rxBuffer = { + false, /* This is a receive buffer. */ + cbRxHandler, /* pfnCallback */ + (void *)&g_sCDCDevice, /* Callback data is our device pointer. */ + USBDCDCPacketRead, /* pfnTransfer */ + USBDCDCRxPacketAvailable, /* pfnAvailable */ + (void *)&g_sCDCDevice, /* pvHandle */ + receiveBuffer, /* pcBuffer */ + USBBUFFERSIZE, /* ulBufferSize */ + receiveBufferWorkspace /* pvWorkspace */ +}; + +const tUSBBuffer txBuffer = { + true, /* This is a transmit buffer. */ + cbTxHandler, /* pfnCallback */ + (void *)&g_sCDCDevice, /* Callback data is our device pointer. */ + USBDCDCPacketWrite, /* pfnTransfer */ + USBDCDCTxPacketAvailable, /* pfnAvailable */ + (void *)&g_sCDCDevice, /* pvHandle */ + transmitBuffer, /* pcBuffer */ + USBBUFFERSIZE, /* ulBufferSize */ + transmitBufferWorkspace /* pvWorkspace */ +}; + +const tUSBDCDCDevice g_sCDCDevice = { + USB_VID_TI_1CBE, + USB_PID_SERIAL, + 0, + USB_CONF_ATTR_SELF_PWR, + + cbSerialHandler, + NULL, + + USBBufferEventCallback, + (void *)&rxBuffer, + + USBBufferEventCallback, + (void *)&txBuffer, + + stringDescriptors, + STRINGDESCRIPTORSCOUNT, + + &serialInstance /* Old usblib stores a pointer */ +}; +#endif + +static tLineCoding g_sLineCoding = { + 115200, /* 115200 baud rate. */ + 1, /* 1 Stop Bit. */ + 0, /* No Parity. */ + 8 /* 8 Bits of data. */ +}; + +int cpt[20] = {0}; +//***************************************************************************** +// +//! Waits for a character from the USB port. +//! +//! This function gets a character from the USB receive buffer. +//! If there are no characters available, this function waits until a +//! character is received before returning. +//! +//! \return Returns the character read from the USB port. +// +//***************************************************************************** +char USBGetChar(void) +{ + uint8_t ucChar; + int len = 0; + //int leng; +// char str[60]; + + len = USBCDCD_receiveData(&ucChar, 1, BIOS_WAIT_FOREVER); + if (len !=1) + { +// leng = usnprintf(str, 60, "\r\n USBGetChar len error %d",len ); +// cb_push_back (str, leng); + Task_sleep(1); + len = USBCDCD_receiveData(&ucChar, 1, BIOS_WAIT_FOREVER); +// leng = usnprintf(str, 60, "\r\n USBGetChar len second %d",len ); +// cb_push_back (str, leng); + } + // + // Now return the char. + // + return (ucChar); +} + +unsigned int USBSendData(const unsigned char *_pBuff,unsigned int _length) +{ + //StoreUSBByte ((uint8_t *)_pBuff,_length,false); + return USBCDCD_sendData(_pBuff,_length,10/*BIOS_WAIT_FOREVER*/); +} + +void USBFlush(void) +{ + USBBufferFlush(&rxBuffer); +} + +/****************************************************************************** + * ======== cbRxHandler ======== + * Callback handler for the USB stack. + * + * Callback handler call by the USB stack to notify us on what has happened in + * regards to the keyboard. + * + * @param(_pcbData) A callback pointer provided by the client. + * + * @param(_event) Identifies the event that occurred in regards to + * this device. + * + * @param(_eventMsgData) A data value associated with a particular event. + * + * @param(_pEventMsgPtr) A data pointer associated with a particular event. + * + ******************************************************************************/ +static USBCDCDEventType cbRxHandler(void *_pcbData, USBCDCDEventType _event, + USBCDCDEventType _eventMsgData, + void *_pEventMsgPtr) +{ + cpt[0]++; + switch (_event) + { + case USB_EVENT_RX_AVAILABLE: + { + cpt[1]++; + Semaphore_post(semRxSerial); + break; + } + case USB_EVENT_DATA_REMAINING: + break; + + case USB_EVENT_REQUEST_BUFFER: + break; + + default: + break; + } + + return (0); +} +//***************************************************************************** +// +// This is the callback from the USB DFU runtime interface driver. +// +// \param pvCBData is ignored by this function. +// \param ui32Event is one of the valid events for a DFU device. +// \param ui32MsgParam is defined by the event that occurs. +// \param pvMsgData is a pointer to data that is defined by the event that +// occurs. +// +// This function will be called to inform the application when a change occurs +// during operation as a DFU device. Currently, the only event passed to this +// callback is USBD_DFU_EVENT_DETACH which tells the recipient that they should +// pass control to the boot loader at the earliest, non-interrupt context +// point. +// +// \return This function will return 0. +// +//***************************************************************************** +uint32_t +DFUDetachCallback(void *pvCBData, uint32_t ui32Event, uint32_t ui32MsgData, + void *pvMsgData) +{ + if(ui32Event == USBD_DFU_EVENT_DETACH) + { + // + // Set the flag that the main loop uses to determine when it is time + // to transfer control back to the boot loader. Note that we + // absolutely DO NOT call USBDDFUUpdateBegin() here since we are + // currently in interrupt context and this would cause bad things to + // happen (and the boot loader to not work). + // + // + // Release updateSem + // + Semaphore_post(updateSem); + } + + return (0); +} + +/****************************************************************************** + * ======== cbSerialHandler ======== + * Callback handler for the USB stack. + * + * Callback handler call by the USB stack to notify us on what has happened in + * regards to the keyboard. + * + * @param(_pcbData) A callback pointer provided by the client. + * + * @param(_event) Identifies the event that occurred in regards to + * this device. + * + * @param(_eventMsgData) A data value associated with a particular event. + * + * @param(_pEventMsgPtr) A data pointer associated with a particular event. + * + *****************************************************************************/ +static USBCDCDEventType cbSerialHandler(void *_pcbData, USBCDCDEventType _event, + USBCDCDEventType _eventMsgData, + void *_pEventMsgPtr) +{ + tLineCoding *psLineCoding; + static bool initiated = false; + cpt[2]++; + + /* Determine what event has happened */ + switch (_event) + { + case USB_EVENT_CONNECTED: + { + cpt[3]++; + if(state == USBCDCD_STATE_UNCONFIGURED) + { + state = USBCDCD_STATE_INIT; + //SetCommunication(USBGetChar, USBSendData,USBFlush); + Semaphore_post(semUSBConnected); + Semaphore_post(initConnectionSem); + Semaphore_post(semTxSerial); + initiated = true; + } + break; + } + case USB_EVENT_DISCONNECTED: + { + state = USBCDCD_STATE_UNCONFIGURED; + break; + } + case USBD_CDC_EVENT_GET_LINE_CODING: + { + cpt[7]++; + /* Create a pointer to the line coding information. */ + psLineCoding = (tLineCoding *)_pEventMsgPtr; + + /* Copy the current line coding information into the structure. */ + *(psLineCoding) = g_sLineCoding; + break; + } + case USBD_CDC_EVENT_SET_LINE_CODING: + { + cpt[6]++; + /* Create a pointer to the line coding information. */ + psLineCoding = (tLineCoding *)_pEventMsgPtr; + + /* + * Copy the line coding information into the current line coding + * structure. + */ + g_sLineCoding = *(psLineCoding); + break; + } + case USBD_CDC_EVENT_SET_CONTROL_LINE_STATE: + break; + + case USBD_CDC_EVENT_SEND_BREAK: + break; + + case USBD_CDC_EVENT_CLEAR_BREAK: + break; + + case USB_EVENT_SUSPEND: + cpt[4]++; + if (state != USBCDCD_STATE_UNCONFIGURED) + { + state = USBCDCD_STATE_UNCONFIGURED; + if (initiated == true) + { + Semaphore_post(ReconnectSem); + //Semaphore_post(semTxSerial); + } + } + break; + + case USB_EVENT_RESUME: + cpt[5]++; + break; + + default: + break; + } + + return (0); +} + +/******************************************************************************** + * ======== cbTxHandler ======== + * Callback handler for the USB stack. + * + * Callback handler call by the USB stack to notify us on what has happened in + * regards to the keyboard. + * + * @param(_pcbData) A callback pointer provided by the client. + * + * @param(_event) Identifies the event that occurred in regards to + * this device. + * + * @param(_eventMsgData) A data value associated with a particular event. + * + * @param(_pEventMsgPtr) A data pointer associated with a particular event. + * + ********************************************************************************/ +static USBCDCDEventType cbTxHandler(void *_pcbData, USBCDCDEventType _event, + USBCDCDEventType _eventMsgData, + void *_pEventMsgPtr) +{ + cpt[8]++; + switch (_event) + { + case USB_EVENT_TX_COMPLETE: + { + cpt[9]++; + /* + * Data was sent, so there should be some space available on the + * buffer + */ + Semaphore_post(semTxSerial); + break; + } + default: + break; + } + + return (0); +} + +/******************************************************************** + * ======== USBCDCD_hwiHandler ======== + * This function calls the USB library's device interrupt handler. + ********************************************************************/ +void USBCDCD_hwiHandler(UArg arg0) +{ + USB0DeviceIntHandler(); +} + +/******************************************************************** + * ======== rxData ======== + *******************************************************************/ +static unsigned int rxData(unsigned char *_pBuff, + unsigned int _length, + unsigned int _timeout) +{ + unsigned int read = 0; + cpt[10]++; + + if (USBBufferDataAvailable(&rxBuffer) || Semaphore_pend(semRxSerial, _timeout)) + { + read = USBBufferRead(&rxBuffer, _pBuff, _length); + } + + return (read); +} + + +/******************************************************************** + * ======== txData ======== + ********************************************************************/ +static unsigned int txData(const unsigned char *_pBuff, + int _length, unsigned int _timeout) +{ + unsigned int bufferedCount = 0; + unsigned int sendCount = 0; + cpt[11]++; + + while (bufferedCount != _length) + { + /* Determine the buffer _size available */ + unsigned int buffAvailSize = USBBufferSpaceAvailable(&txBuffer); + + /* Determine how much needs to be sent */ + if ((_length - bufferedCount) > buffAvailSize) + { + sendCount = buffAvailSize; + } + else + { + sendCount = _length - bufferedCount; + } + + unsigned char *sendPtr; + /* Adjust the pointer to the data */ + sendPtr = (unsigned char *)_pBuff + bufferedCount; + + /* Place the contents into the USB BUffer */ + bufferedCount += USBBufferWrite(&txBuffer, sendPtr, sendCount); + + /* Pend until some data was sent through the USB*/ + if (!Semaphore_pend(semTxSerial, _timeout)) + { + break; + } + } + + return (bufferedCount); +} +//***************************************************************************** +// +// Interrupt handler for the UART which we are redirecting via USB. +// +//***************************************************************************** +void USB0Handler(void) +{ + USBCDCD_init(); +} + +void * USBDComposite = NULL; +/******************************************************************* + * ======== USBCDCD_init ======== + *******************************************************************/ +void USBCDCD_init(void) +{ + Semaphore_Params semParams; + Error_Block eb; + + Error_init(&eb); + /* Hwi_Handle hwi; + + // Install interrupt handler + hwi = Hwi_create(INT_USB0, USBCDCD_hwiHandler, NULL, &eb); + if (hwi == NULL) + { + System_abort("Can't create USB Hwi"); + } +*/ + /* RTOS primitives */ + Semaphore_Params_init(&semParams); + semParams.mode = Semaphore_Mode_BINARY; + semTxSerial = Semaphore_create(0, &semParams, &eb); + if (semTxSerial == NULL) + { + System_abort("Can't create TX semaphore"); + } + + semRxSerial = Semaphore_create(0, &semParams, &eb); + if (semRxSerial == NULL) + { + System_abort("Can't create RX semaphore"); + } + + semUSBConnected = Semaphore_create(0, &semParams, &eb); + if (semUSBConnected == NULL) + { + System_abort("Can't create USB semaphore"); + } + + gateTxSerial = GateMutex_create(NULL, &eb); + if (gateTxSerial == NULL) + { + System_abort("Can't create gate"); + } + + gateRxSerial = GateMutex_create(NULL, &eb); + if (gateRxSerial == NULL) + { + System_abort("Can't create gate"); + } + + gateUSBWait = GateMutex_create(NULL, &eb); + if (gateUSBWait == NULL) + { + System_abort("Could not create USB Wait gate"); + } + + /* State specific variables */ + state = USBCDCD_STATE_UNCONFIGURED; + + /* Set the USB stack mode to Device mode with VBUS monitoring */ + USBStackModeSet(0, eUSBModeForceDevice, 0); + + //should be done here only once for supporting firmware upgrade as composite devise + //if done several times in different places doesn't work in the upgrade. + USBBufferInit(&txBuffer); + USBBufferInit(&rxBuffer); + + //if (!USBDCDCCompositeInit(0, &g_sCDCDevice, &(g_sCompDevice.psDevices[0]))) + if (!USBDCDCInit(0, &g_sCDCDevice)) + { + System_abort("Error initializing the serial device"); + } + + /*if (!USBDDFUCompositeInit(0, &g_sDFUDevice, &(g_sCompDevice.psDevices[1]))) + { + System_abort("Error initializing the DFU device"); + } + // + // Pass the USB library our device information, initialize the USB + // controller and connect the device to the bus. + // + g_sCompDevice.sPrivateData.sDeviceDescriptor.bcdUSB = 0X200; + USBDComposite = USBDCompositeInit(0, &g_sCompDevice, DESCRIPTOR_BUFFER_SIZE, g_pui8DescriptorBuffer); + if (!USBDComposite) + { + System_abort("Error initializing the composite device"); + }*/ +} + +//----------------------------------------------------------- +void USBCDCD_Reinit(void) +{ + Error_Block eb; + Semaphore_Params semParams; + + Error_init(&eb); + + /* RTOS primitives */ + Semaphore_Params_init(&semParams); + semParams.mode = Semaphore_Mode_BINARY; + semTxSerial = Semaphore_create(0, &semParams, &eb); + if (semTxSerial == NULL) + { + System_abort("Can't create TX semaphore"); + } + + /* State specific variables */ + state = USBCDCD_STATE_UNCONFIGURED; + + /* Set the USB stack mode to Device mode with VBUS monitoring */ + USBStackModeSet(0, eUSBModeForceDevice, 0); + + // if (!USBDCDCInit(0, &g_sCDCDevice, &(g_sCompDevice.psDevices[0]))) + if (!USBDCDCInit(0, &g_sCDCDevice)) + { + System_abort("Error initializing the serial device"); + } +/* if (!USBDDFUCompositeInit(0, &g_sDFUDevice, &(g_sCompDevice.psDevices[1]))) + { + System_abort("Error initializing the DFU device"); + } + + // Pass the USB library our device information, initialize the USB + // controller and connect the device to the bus. + // + g_sCompDevice.sPrivateData.sDeviceDescriptor.bcdUSB = 0X200; + USBDComposite = USBDCompositeInit(0, &g_sCompDevice, DESCRIPTOR_BUFFER_SIZE, g_pui8DescriptorBuffer); + if (!USBDComposite) + { + System_abort("Error initializing the composite device"); + } +*/ +} + +//----------------------------------------------------------- +void USBCDC_close(void) +{ + uint32_t tick = 0; + char str[60]; + uint8_t len = 0; + tick = UsersysTickGet(); + + USBDCompositeTerm(USBDComposite); + + //Semaphore_post(semTxSerial); + //Semaphore_delete(&semTxSerial); + + //messageFlush(); + USBCDCD_Reinit(); + + len = usnprintf(str, 60, "\r\n USBCDCD_Reinit tick %d state %d",tick, state ); + cb_push_back (str, len); +} + +/************************************************************** + * ======== USBCDCD_receiveData ======== +************************************************************* */ +unsigned int USBCDCD_receiveData(unsigned char *_pBuff, + unsigned int _length, + unsigned int _timeout) +{ + unsigned int retValue = 0; + unsigned int key; + + switch (state) + { + case USBCDCD_STATE_UNCONFIGURED: + { + USBCDCD_waitForConnect(_timeout); + break; + } + case USBCDCD_STATE_INIT: + { + /* Acquire lock */ + key = GateMutex_enter(gateRxSerial); + + state = USBCDCD_STATE_IDLE; + + retValue = rxData(_pBuff, _length, _timeout); + + /* Release lock */ + GateMutex_leave(gateRxSerial, key); + break; + } + case USBCDCD_STATE_IDLE: + { + /* Acquire lock */ + key = GateMutex_enter(gateRxSerial); + + retValue = rxData(_pBuff, _length, _timeout); + + /* Release lock */ + GateMutex_leave(gateRxSerial, key); + break; + } + default: + break; + } + + return (retValue); +} + +/******************************************************* + * ======== USBCDCD_sendData ======== + *******************************************************/ +unsigned int USBCDCD_sendData(const unsigned char *_pBuff, + unsigned int _length, + unsigned int _timeout) +{ + unsigned int retValue = 0; + unsigned int key; + cpt[13]++; + + switch (state) + { + case USBCDCD_STATE_UNCONFIGURED: + { + USBCDCD_waitForConnect(_timeout); + break; + } + case USBCDCD_STATE_INIT: + { + /* Acquire lock */ + key = GateMutex_enter(gateTxSerial); + + state = USBCDCD_STATE_IDLE; + + retValue = txData(_pBuff, _length, _timeout); + + /* Release lock */ + GateMutex_leave(gateTxSerial, key); + break; + } + case USBCDCD_STATE_IDLE: + { + /* Acquire lock */ + key = GateMutex_enter(gateTxSerial); + + retValue = txData(_pBuff, _length, _timeout); + + /* Release lock */ + GateMutex_leave(gateTxSerial, key); + break; + } + default: + break; + } + + return (retValue); +} + +/************************************************ + * ======== USBCDCD_waitForConnect ======== + *************************************************/ +bool USBCDCD_waitForConnect(unsigned int _timeout) +{ + bool ret = true; + unsigned int key; + cpt[12]++; + + // Need exclusive access to prevent a race condition + key = GateMutex_enter(gateUSBWait); + + if (state == USBCDCD_STATE_UNCONFIGURED) + { + if (!Semaphore_pend(semUSBConnected, _timeout)) + { + ret = false; + } + } + + GateMutex_leave(gateUSBWait, key); + + return (ret); +} + +uint32_t SendChars(char* buffer,size_t length) +{ + USBSendData(buffer, length); + return 0; +} + +/****************************************************************************** + * ======== communicationTask ======== + * Task for this function is created statically. See the project's .cfg file. + * this communication task is created statically in system initialization, in blocking mode + * over one of the chosen ommunication methods (USB or Blutooth). + ******************************************************************************/ +void communicationTask(UArg arg0, UArg arg1) +{ + +// USBCDCD_init(); + cpt[14]++; + //UartBTInit(); + /* Block while the device is NOT connected to the USB */ + Semaphore_pend(initConnectionSem, BIOS_WAIT_FOREVER); + cpt[15]++; + + // Loop forever receiving commands + while(true) + { + cpt[16]++; +// receive_callback + //handle incoming data + } +} + diff --git a/Software/Embedded_SW/Embedded/Drivers/USB_Communication/USBCDCD.bak b/Software/Embedded_SW/Embedded/Drivers/USB_Communication/USBCDCD.bak new file mode 100644 index 000000000..89f467e97 --- /dev/null +++ b/Software/Embedded_SW/Embedded/Drivers/USB_Communication/USBCDCD.bak @@ -0,0 +1,1053 @@ + +/************************************************************************************ + * this module is represents the access layer to USB STACK DRIVER + * used to communication over USB protocol + * the USB protocol implementation is inside the module no need for another includes + * ======== USBCDCD.c ======== + ************************************************************************************/ + +/* XDCtools Header files */ +#include <xdc/std.h> +#include <xdc/runtime/Error.h> +#include <xdc/runtime/System.h> + +/* BIOS Header files */ +#include <ti/sysbios/BIOS.h> +#include <ti/sysbios/gates/GateMutex.h> +#include <ti/sysbios/hal/Hwi.h> +#include <ti/sysbios/knl/Semaphore.h> +#include <ti/sysbios/knl/Task.h> + +#include <stdbool.h> +#include <stdint.h> + +/* driverlib Header files */ +#include <inc/hw_ints.h> +#include <inc/hw_types.h> +#include <inc/hw_uart.h> + +#include <driverlib/rom.h> +#include <driverlib/sysctl.h> +#include <driverlib/usb.h> +#include <inc/hw_memmap.h> + +/* usblib Header files */ +#include <usblib/usb-ids.h> +#include <usblib/usblib.h> +#include <usblib/usbcdc.h> +#include <usblib/device/usbdevice.h> +#include <usblib/device/usbdcdc.h> +#include <usblib/device/usbddfu-rt.h> +#include <usblib/device/usbdcomp.h> +#include "Drivers/USB_Communication/USBCDCD.h" + + +#include <utils/ustdlib.h> +#include "Common/Utilities/Utils.h" +//#include "usb_serial_structs.h" +#include "usb_serial_buffer.h" + +#if defined(TIVAWARE) +typedef uint32_t USBCDCDEventType; +#else +#define eUSBModeForceDevice USB_MODE_FORCE_DEVICE +typedef unsigned long USBCDCDEventType; +#endif + +/* Defines */ +#define USBBUFFERSIZE 256 +//**************************************************************************** +// +// A buffer into which the composite device can write the combined config +// descriptor. +// +//**************************************************************************** +#define DESCRIPTOR_BUFFER_SIZE (COMPOSITE_DDFU_SIZE + COMPOSITE_DCDC_SIZE) + +extern Semaphore_Handle updateSem; +extern Semaphore_Handle ReconnectSem; + +uint8_t g_pui8DescriptorBuffer[DESCRIPTOR_BUFFER_SIZE]; + +/* Typedefs */ +typedef volatile enum { + USBCDCD_STATE_IDLE = 0, + USBCDCD_STATE_INIT, + USBCDCD_STATE_UNCONFIGURED +} USBCDCD_USBState; + +/* Static variables and handles */ +static volatile USBCDCD_USBState state; +static unsigned char receiveBuffer[USBBUFFERSIZE]; +static unsigned char transmitBuffer[USBBUFFERSIZE]; +SerialBuffer inBuffer; +int expected_message_size; +int current_message_size; +static volatile uint32_t g_RxCount; +static GateMutex_Handle gateTxSerial; +static GateMutex_Handle gateRxSerial; +static GateMutex_Handle gateUSBWait; +static Semaphore_Handle semTxSerial; +static Semaphore_Handle semRxSerial; +static Semaphore_Handle semUSBConnected; +extern Semaphore_Handle initConnectionSem; + +#define FLAG_STATUS_UPDATE 0 +#define FLAG_USB_CONFIGURED 1 +#define FLAG_SENDING_BREAK 2 +static volatile uint32_t g_ui32Flags; + +/* Function prototypes */ +/* +static USBCDCDEventType cbRxHandler(void *cbData, USBCDCDEventType event, + USBCDCDEventType _eventMsgData, + void *eventMsgPtr); +static USBCDCDEventType cbSerialHandler(void *cbData, USBCDCDEventType event, + USBCDCDEventType _eventMsgData, + void *eventMsgPtr); +static USBCDCDEventType cbTxHandler(void *cbData, USBCDCDEventType event, + USBCDCDEventType _eventMsgData, + void *eventMsgPtr); + */ +uint32_t checkpoints [18] = {0}; +uint32_t TxHandler(void *pvCBData, uint32_t ui32Event, uint32_t ui32MsgValue, void *pvMsgData); +uint32_t RxHandler(void *pvCBData, uint32_t ui32Event, uint32_t ui32MsgValue,void *pvMsgData); +uint32_t ControlHandler(void *pvCBData, uint32_t ui32Event, uint32_t ui32MsgValue, void *pvMsgData); +static Void USBCDCD_hwiHandler(UArg arg0); +void USBCDCD_init(void); + +unsigned int USBCDCD_receiveData(unsigned char *_pBuff, + unsigned int _length, + unsigned int _timeout); +bool USBCDCD_waitForConnect(unsigned int _timeout); + +static void (*callback)(char* buffer, size_t length); + +/* The languages supported by this device. */ +const unsigned char langDescriptor[] = { + 4, + USB_DTYPE_STRING, + USBShort(USB_LANG_EN_US) +}; + +/* The manufacturer string. */ +const unsigned char manufacturerString[] = { + (17 + 1) * 2, + USB_DTYPE_STRING, + 'T', 0, 'e', 0, 'x', 0, 'a', 0, 's', 0, ' ', 0, 'I', 0, 'n', 0, 's', 0, + 't', 0, 'r', 0, 'u', 0, 'm', 0, 'e', 0, 'n', 0, 't', 0, 's', 0, +}; + +/* The product string. */ +const unsigned char productString[] = { + 2 + (13 * 2), + USB_DTYPE_STRING, + 'T', 0, 'w', 0, 'i', 0, 'n', 0, 'e', 0, ' ', 0, 'J', 0, 'i', 0, + 'g', 0, ' ', 0, 'U', 0, 'S', 0, 'B', 0, +}; + +/* The serial number string. */ +const unsigned char serialNumberString[] = { + (8 + 1) * 2, + USB_DTYPE_STRING, + '1', 0, '2', 0, '3', 0, '4', 0, '5', 0, '6', 0, '7', 0, '8', 0 +}; + +/* The interface description string. */ +const unsigned char controlInterfaceString[] = { + 2 + (21 * 2), + USB_DTYPE_STRING, + 'J', 0, 'i', 0, 'g', 0, ' ', 0, 'C', 0, 'o', 0, 'n', 0, 't', 0, + 'r', 0, 'o', 0, 'l', 0, ' ', 0, 'I', 0, 'n', 0, 't', 0, 'e', 0, + 'r', 0, 'f', 0, 'a', 0, 'c', 0, 'e', 0 +}; + +/* The configuration description string. */ +const unsigned char configString[] = { + 2 + (26 * 2), + USB_DTYPE_STRING, + 'S', 0, 'e', 0, 'l', 0, 'f', 0, ' ', 0, 'P', 0, 'o', 0, 'w', 0, + 'e', 0, 'r', 0, 'e', 0, 'd', 0, ' ', 0, 'C', 0, 'o', 0, 'n', 0, + 'f', 0, 'i', 0, 'g', 0, 'u', 0, 'r', 0, 'a', 0, 't', 0, 'i', 0, + 'o', 0, 'n', 0 +}; + +//***************************************************************************** +// +// The descriptor string table. +// +//***************************************************************************** +const uint8_t * const g_ppui8StringDescriptors[] = +{ + langDescriptor, + manufacturerString, + productString, + serialNumberString, + controlInterfaceString, + configString +}; + +#define NUM_STRING_DESCRIPTORS (sizeof(g_ppui8StringDescriptors) / \ + sizeof(g_ppui8StringDescriptors[0])) + +/* The descriptor string table. */ +const unsigned char * const stringDescriptors[] = { + langDescriptor, + manufacturerString, + productString, + serialNumberString, + controlInterfaceString, + configString +}; + +#define STRINGDESCRIPTORSCOUNT (sizeof(stringDescriptors) / \ + sizeof(unsigned char *)) + +tUSBBuffer txBuffer; +tUSBBuffer rxBuffer; +static tUSBDCDCDevice g_sCDCDevice; + +tUSBBuffer rxBuffer = +{ + false, /* This is a receive buffer. */ + RxHandler, /* pfnCallback */ + (void *)&g_sCDCDevice, /* Callback data is our device pointer. */ + USBDCDCPacketRead, /* pfnTransfer */ + USBDCDCRxPacketAvailable, /* pfnAvailable */ + (void *)&g_sCDCDevice, /* pvHandle */ + receiveBuffer, /* pcBuffer */ + USBBUFFERSIZE, /* ulBufferSize */ + {{0, 0, 0, 0}, 0, 0} /* private data workspace */ +}; + +tUSBBuffer txBuffer = +{ + true, /* This is a transmit buffer. */ + TxHandler, /* pfnCallback */ + (void *)&g_sCDCDevice, /* Callback data is our device pointer. */ + USBDCDCPacketWrite, /* pfnTransfer */ + USBDCDCTxPacketAvailable, /* pfnAvailable */ + (void *)&g_sCDCDevice, /* pvHandle */ + transmitBuffer, /* pcBuffer */ + USBBUFFERSIZE, /* ulBufferSize */ + {{0, 0, 0, 0}, 0, 0} /* private data workspace */ +}; + +static tUSBDCDCDevice g_sCDCDevice = +{ + USB_VID_TI_1CBE, + USB_PID_SERIAL, + 0, + USB_CONF_ATTR_SELF_PWR, + + ControlHandler, + (void *)&g_sCDCDevice, + + USBBufferEventCallback, + (void *)&rxBuffer, + + USBBufferEventCallback, + (void *)&txBuffer, + + stringDescriptors, + STRINGDESCRIPTORSCOUNT +}; + + + +static tLineCoding g_sLineCoding = { + 115200, /* 115200 baud rate. */ + 1, /* 1 Stop Bit. */ + 0, /* No Parity. */ + 8 /* 8 Bits of data. */ +}; + + +uint32_t SendChars(char* buffer,size_t length) +{ + USBCDCD_sendData(buffer, length,10); + checkpoints[4]++; + +} +//***************************************************************************** +// +//! Waits for a character from the USB port. +//! +//! This function gets a character from the USB receive buffer. +//! If there are no characters available, this function waits until a +//! character is received before returning. +//! +//! \return Returns the character read from the USB port. +// +//***************************************************************************** +char USBGetChar(void) +{ + uint8_t ucChar; + int len = 0; + //int leng; +// char str[60]; + checkpoints[5]++; + + len = USBCDCD_receiveData(&ucChar, 1, BIOS_WAIT_FOREVER); + if (len !=1) + { +// leng = usnprintf(str, 60, "\r\n USBGetChar len error %d",len ); +// cb_push_back (str, leng); + Task_sleep(1); + len = USBCDCD_receiveData(&ucChar, 1, BIOS_WAIT_FOREVER); +// leng = usnprintf(str, 60, "\r\n USBGetChar len second %d",len ); +// cb_push_back (str, leng); + } + // + // Now return the char. + // + return (ucChar); +} + +/*unsigned int USBSendData(const unsigned char *_pBuff,unsigned int _length) +{ + //StoreUSBByte ((uint8_t *)_pBuff,_length,false); + return USBCDCD_sendData(_pBuff,_length,10); +} +*/ +void USBFlush(void) +{ + USBBufferFlush(&rxBuffer); +} + + +//***************************************************************************** +// +// Set the state of the RS232 RTS and DTR signals. +// +//***************************************************************************** +void SetControlLineState(uint16_t ui16State) +{ + // + // TODO: If configured with GPIOs controlling the handshake lines, + // set them appropriately depending upon the flags passed in the wValue + // field of the request structure passed. + // +} + +//***************************************************************************** +// +// Get the communication parameters in use on the UART. +// +//***************************************************************************** +void GetLineCoding(tLineCoding *psLineCoding) +{ + psLineCoding->ui32Rate = 9600; + psLineCoding->ui8Databits = 8; + psLineCoding->ui8Parity = USB_CDC_PARITY_NONE; + psLineCoding->ui8Stop = USB_CDC_STOP_BITS_1; +} +/******************************************************************** + * ======== USBCDCD_hwiHandler ======== + * This function calls the USB library's device interrupt handler. + ********************************************************************/ +static Void USBCDCD_hwiHandler(UArg arg0) +{ + USB0DeviceIntHandler(); + checkpoints[6]++; + +} +//***************************************************************************** +// +// This function is called whenever serial data is received from the UART. +// It is passed the accumulated error flags from each character received in +// this interrupt and determines from them whether or not an interrupt +// notification to the host is required. +// +// If a notification is required and the control interrupt endpoint is idle, +// we send the notification immediately. If the endpoint is not idle, we +// accumulate the errors in a global variable which will be checked on +// completion of the previous notification and used to send a second one +// if necessary. +// +//***************************************************************************** +void CheckForSerialStateChange(const tUSBDCDCDevice *psDevice, uint32_t ui32Errors) +{ + uint16_t ui16SerialState; + + // + // Clear our USB serial state. Since we are faking the handshakes, always + // set the TXCARRIER (DSR) and RXCARRIER (DCD) bits. + // + ui16SerialState = USB_CDC_SERIAL_STATE_TXCARRIER | + USB_CDC_SERIAL_STATE_RXCARRIER; + + // + // Are any error bits set? + // + if(ui32Errors) + { + // + // At least one error is being notified so translate from our hardware + // error bits into the correct state markers for the USB notification. + // + if(ui32Errors & UART_DR_OE) + { + ui16SerialState |= USB_CDC_SERIAL_STATE_OVERRUN; + } + + if(ui32Errors & UART_DR_PE) + { + ui16SerialState |= USB_CDC_SERIAL_STATE_PARITY; + } + + if(ui32Errors & UART_DR_FE) + { + ui16SerialState |= USB_CDC_SERIAL_STATE_FRAMING; + } + + if(ui32Errors & UART_DR_BE) + { + ui16SerialState |= USB_CDC_SERIAL_STATE_BREAK; + } + + // + // Call the CDC driver to notify the state change. + // + USBDCDCSerialStateChange((void *)psDevice, ui16SerialState); + } +} + +//***************************************************************************** +// +// Handles CDC driver notifications related to control and setup of the device. +// +// \param pvCBData is the client-supplied callback pointer for this channel. +// \param ulEvent identifies the event we are being notified about. +// \param ulMsgValue is an event-specific value. +// \param pvMsgData is an event-specific pointer. +// +// This function is called by the CDC driver to perform control-related +// operations on behalf of the USB host. These functions include setting +// and querying the serial communication parameters, setting handshake line +// states and sending break conditions. +// +// \return The return value is event-specific. +// +//***************************************************************************** +uint32_t ControlHandler(void *pvCBData, uint32_t ui32Event, uint32_t ui32MsgValue, void *pvMsgData) +{ + checkpoints[3]++; + + // + // Which event are we being asked to process? + // + switch(ui32Event) + { + // + // We are connected to a host and communication is now possible. + // + case USB_EVENT_CONNECTED: + { + checkpoints[11]++; + // + // Now connected and ready for normal operation. + // + HWREGBITW(&g_ui32Flags, FLAG_USB_CONFIGURED) = 1; + + // + // Flush our buffers. + // + USBBufferFlush(&txBuffer); + USBBufferFlush(&rxBuffer); + + //TODO: Notify connection! + + // + // Set the command status update flag. + // + HWREGBITW(&g_ui32Flags, FLAG_STATUS_UPDATE) = 1; + + break; + } + + // + // The host has disconnected. + // + case USB_EVENT_DISCONNECTED: + { + //checkpoints[12]++; + // + // No longer connected. + // + HWREGBITW(&g_ui32Flags, FLAG_USB_CONFIGURED) = 0; + + //TODO: Notify disconnection! + + // + // Set the command status update flag. + // + HWREGBITW(&g_ui32Flags, FLAG_STATUS_UPDATE) = 1; + + break; + } + + // + // Return the current serial communication parameters. + // + case USBD_CDC_EVENT_GET_LINE_CODING: + { + //checkpoints[13]++; + GetLineCoding(pvMsgData); + break; + } + + // + // Set the current serial communication parameters. + // + case USBD_CDC_EVENT_SET_LINE_CODING: + { + //checkpoints[14]++; + GetLineCoding(pvMsgData); + break; + } + + // + // Set the current serial communication parameters. + // + case USBD_CDC_EVENT_SET_CONTROL_LINE_STATE: + { + checkpoints[15]++; + SetControlLineState((uint16_t)ui32MsgValue); + break; + } + + // + // Send a break condition on the serial line. + // + case USBD_CDC_EVENT_SEND_BREAK: + { + checkpoints[12]++; + break; + } + + // + // Clear the break condition on the serial line. + // + case USBD_CDC_EVENT_CLEAR_BREAK: + { + checkpoints[13]++; + break; + } + + // + // Ignore SUSPEND and RESUME for now. + // + case USB_EVENT_SUSPEND: + case USB_EVENT_RESUME: + { + checkpoints[14]++; + break; + } + + // + // We don't expect to receive any other events. Ignore any that show + // up in a release build or hang in a debug build. + // + default: + { +#ifdef DEBUG + while(1); +#else + break; +#endif + } + + } + + return(0); +} +void handleRx(void) +{ + uint32_t ui32Read; + uint8_t ui8Char; + uint8_t size[4]; + int size_bar = 0; + checkpoints[2]++; + + if (expected_message_size == 0) + { + do + { + ui32Read = USBBufferRead((tUSBBuffer *)&rxBuffer, &ui8Char, 1); + + if(ui32Read) + { + size[size_bar++] = ui8Char; + } + + } while(size_bar < 4); + + expected_message_size = *(int *)size; + } + + do + { + ui32Read = USBBufferRead((tUSBBuffer *)&rxBuffer, &ui8Char, 1); + + // Did we get a character? + if(ui32Read) + { + insertArray(&inBuffer, ui8Char); + current_message_size++; + } + + if (current_message_size == expected_message_size) + { + g_RxCount += current_message_size; + expected_message_size = 0; + current_message_size = 0; + break; + } + + } while(ui32Read); +} + +//***************************************************************************** +// +// Handles CDC driver notifications related to the transmit channel (data to +// the USB host). +// +// \param pvCBData is the client-supplied callback pointer for this channel. +// \param ui32Event identifies the event we are being notified about. +// \param ui32MsgValue is an event-specific value. +// \param pvMsgData is an event-specific pointer. +// +// This function is called by the CDC driver to notify us of any events +// related to operation of the transmit data channel (the IN channel carrying +// data to the USB host). +// +// \return The return value is event-specific. +// +//***************************************************************************** +uint32_t TxHandler(void *pvCBData, uint32_t ui32Event, uint32_t ui32MsgValue, void *pvMsgData) +{ + // + // Which event have we been sent? + // + checkpoints[0]++; + switch(ui32Event) + { + case USB_EVENT_TX_COMPLETE: + { + // + // Since we are using the USBBuffer, we don't need to do anything + // here. + // + break; + } + + // + // We don't expect to receive any other events. Ignore any that show + // up in a release build or hang in a debug build. + // + default: + { +#ifdef DEBUG + while(1); +#else + break; +#endif + } + } + return(0); +} + +//***************************************************************************** +// +// Handles CDC driver notifications related to the receive channel (data from +// the USB host). +// +// \param pvCBData is the client-supplied callback data value for this channel. +// \param ui32Event identifies the event we are being notified about. +// \param ui32MsgValue is an event-specific value. +// \param pvMsgData is an event-specific pointer. +// +// This function is called by the CDC driver to notify us of any events +// related to operation of the receive data channel (the OUT channel carrying +// data from the USB host). +// +// \return The return value is event-specific. +// +//***************************************************************************** +uint32_t RxHandler(void *pvCBData, uint32_t ui32Event, uint32_t ui32MsgValue,void *pvMsgData) +{ + // + // Which event are we being sent? + // + checkpoints[1]++; + + switch(ui32Event) + { + // + // A new packet has been received. + // + case USB_EVENT_RX_AVAILABLE: + { + // + // Feed some characters into the UART TX FIFO and enable the + // interrupt so we are told when there is more space. + // + handleRx(); + break; + } + + // + // We are being asked how much unprocessed data we have still to + // process. We return 0 if the UART is currently idle or 1 if it is + // in the process of transmitting something. The actual number of + // bytes in the UART FIFO is not important here, merely whether or + // not everything previously sent to us has been transmitted. + // + case USB_EVENT_DATA_REMAINING: + { + // + // Get the number of bytes in the buffer and add 1 if some data + // still has to clear the transmitter. + return(0); + } + + // + // We are being asked to provide a buffer into which the next packet + // can be read. We do not support this mode of receiving data so let + // the driver know by returning 0. The CDC driver should not be sending + // this message but this is included just for illustration and + // completeness. + // + case USB_EVENT_REQUEST_BUFFER: + { + return(0); + } + + // + // We don't expect to receive any other events. Ignore any that show + // up in a release build or hang in a debug build. + // + default: +#ifdef DEBUG + while(1); +#else + break; +#endif + } + + return(0); +} + + +//Execute this function on UART0 Interrupt; +void InitUSB(void) +{ + checkpoints[9]++; + CheckForSerialStateChange(&g_sCDCDevice, 0); +} +//***************************************************************************** +// +// Interrupt handler for the UART which we are redirecting via USB. +// +//***************************************************************************** +void USB0Handler(void) +{ + InitUSB(); + checkpoints[7]++; + +} + + + + +void * USBDComposite = NULL; +/******************************************************************* + * ======== USBCDCD_init ======== + *******************************************************************/ +void USBCDCD_init(void) +{ + Hwi_Handle hwi; + Error_Block eb; + Semaphore_Params semParams; + + Error_init(&eb); + + /* Install interrupt handler */ + hwi = Hwi_create(INT_USB0, USBCDCD_hwiHandler, NULL, &eb); + if (hwi == NULL) + { + System_abort("Can't create USB Hwi"); + } + + /* RTOS primitives */ + Semaphore_Params_init(&semParams); + semParams.mode = Semaphore_Mode_BINARY; + semTxSerial = Semaphore_create(0, &semParams, &eb); + if (semTxSerial == NULL) + { + System_abort("Can't create TX semaphore"); + } + + semRxSerial = Semaphore_create(0, &semParams, &eb); + if (semRxSerial == NULL) + { + System_abort("Can't create RX semaphore"); + } + + semUSBConnected = Semaphore_create(0, &semParams, &eb); + if (semUSBConnected == NULL) + { + System_abort("Can't create USB semaphore"); + } + + gateTxSerial = GateMutex_create(NULL, &eb); + if (gateTxSerial == NULL) + { + System_abort("Can't create gate"); + } + + gateRxSerial = GateMutex_create(NULL, &eb); + if (gateRxSerial == NULL) + { + System_abort("Can't create gate"); + } + + gateUSBWait = GateMutex_create(NULL, &eb); + if (gateUSBWait == NULL) + { + System_abort("Could not create USB Wait gate"); + } + + /* State specific variables */ + state = USBCDCD_STATE_UNCONFIGURED; + + /* Set the USB stack mode to Device mode with VBUS monitoring */ + USBStackModeSet(0, eUSBModeForceDevice, 0); + + //should be done here only once for supporting firmware upgrade as composite devise + //if done several times in different places doesn't work in the upgrade. + USBBufferInit(&txBuffer); + USBBufferInit(&rxBuffer); +/* + if (!USBDCDCCompositeInit(0, &g_sCDCDevice, &(g_sCompDevice.psDevices[0]))) + { + System_abort("Error initializing the serial device"); + } + + if (!USBDDFUCompositeInit(0, &g_sDFUDevice, &(g_sCompDevice.psDevices[1]))) + { + System_abort("Error initializing the DFU device"); + } + + // + // Pass the USB library our device information, initialize the USB + // controller and connect the device to the bus. + // + g_sCompDevice.sPrivateData.sDeviceDescriptor.bcdUSB = 0X200; + USBDComposite = USBDCompositeInit(0, &g_sCompDevice, DESCRIPTOR_BUFFER_SIZE, g_pui8DescriptorBuffer); + if (!USBDComposite) + { + System_abort("Error initializing the composite device"); + } + */ + if (!USBDCDCInit(0, &g_sCDCDevice)) + { + System_abort("Error initializing the serial device"); + } + +} + +//----------------------------------------------------------- +void USBCDCD_Reinit(void) +{ + Error_Block eb; + Semaphore_Params semParams; + + Error_init(&eb); + + /* RTOS primitives */ + Semaphore_Params_init(&semParams); + semParams.mode = Semaphore_Mode_BINARY; + semTxSerial = Semaphore_create(0, &semParams, &eb); + if (semTxSerial == NULL) + { + System_abort("Can't create TX semaphore"); + } + + /* State specific variables */ + state = USBCDCD_STATE_UNCONFIGURED; + + /* Set the USB stack mode to Device mode with VBUS monitoring */ + USBStackModeSet(0, eUSBModeForceDevice, 0); + + if (!USBDCDCInit(0, &g_sCDCDevice)) + { + System_abort("Error initializing the serial device"); + } + +} + +//----------------------------------------------------------- +void USBCDC_close(void) +{ + uint32_t tick = 0; + char str[60]; + uint8_t len = 0; + tick = UsersysTickGet(); + + USBDCompositeTerm(USBDComposite); + + Semaphore_post(semTxSerial); + Semaphore_delete(&semTxSerial); + + USBCDCD_Reinit(); + + len = usnprintf(str, 60, "\r\n USBCDCD_Reinit tick %d state %d",tick, state ); + cb_push_back (str, len); +} + +/************************************************************** + * ======== USBCDCD_receiveData ======== +************************************************************* */ +unsigned int USBCDCD_receiveData(unsigned char *_pBuff, + unsigned int _length, + unsigned int _timeout) +{ + unsigned int retValue = 0; + unsigned int key; + checkpoints[9]++; + + switch (state) + { + case USBCDCD_STATE_UNCONFIGURED: + { + USBCDCD_waitForConnect(_timeout); + break; + } + case USBCDCD_STATE_INIT: + { + /* Acquire lock */ + key = GateMutex_enter(gateRxSerial); + + state = USBCDCD_STATE_IDLE; + + handleRx(); + //retValue = rxData(_pBuff, _length, _timeout); + + /* Release lock */ + GateMutex_leave(gateRxSerial, key); + break; + } + case USBCDCD_STATE_IDLE: + { + /* Acquire lock */ + key = GateMutex_enter(gateRxSerial); + handleRx(); + //retValue = rxData(_pBuff, _length, _timeout); + + /* Release lock */ + GateMutex_leave(gateRxSerial, key); + break; + } + default: + break; + } + + return (retValue); +} + +/******************************************************* + * ======== USBCDCD_sendData ======== + *******************************************************/ +unsigned int USBCDCD_sendData(char *_pBuff, + unsigned int _length, + unsigned int _timeout) +{ + uint8_t size[4]; + size[3] = (_length>>24) & 0xFF; + size[2] = (_length>>16) & 0xFF; + size[1] = (_length>>8) & 0xFF; + size[0] = _length & 0xFF; + checkpoints[10]++; + + USBBufferWrite((tUSBBuffer *)&txBuffer, size, 4); + return USBBufferWrite((tUSBBuffer *)&txBuffer, (uint8_t*)_pBuff, _length); +} + +/************************************************ + * ======== USBCDCD_waitForConnect ======== + *************************************************/ +bool USBCDCD_waitForConnect(unsigned int _timeout) +{ + bool ret = true; + unsigned int key; + + // Need exclusive access to prevent a race condition + key = GateMutex_enter(gateUSBWait); + checkpoints[16]++; + + if (state == USBCDCD_STATE_UNCONFIGURED) + { + if (!Semaphore_pend(semUSBConnected, _timeout)) + { + ret = false; + } + } + + GateMutex_leave(gateUSBWait, key); + + return (ret); +} +void RegisterReceiveCallback(void (*callback_ptr)(char* buffer, size_t length)) +{ + callback = callback_ptr; +} + +extern Semaphore_Handle initConnectionSem; + +/****************************************************************************** + * ======== communicationTask ======== + * Task for this function is created statically. See the project's .cfg file. + * this communication task is created statically in system initialization, in blocking mode + * over one of the chosen ommunication methods (USB or Blutooth). + ******************************************************************************/ +void communicationTask(UArg arg0, UArg arg1) +{ + + uint32_t ui32RxCount; + + ui32RxCount = 0; + g_RxCount = 0; + + USBCDCD_init(); + //UartBTInit(); + //Clock_Params_init(&clkParams); + + //create clock timeout counter + //clkParams.period = 0; + //clkParams.startFlag = FALSE; + //communicationTimeoutClock = Clock_create(CommunicationLoseClockHandle, 0, &clkParams, NULL); + /* Block while the device is NOT connected to the USB */ + Semaphore_pend(initConnectionSem, BIOS_WAIT_FOREVER); + + // Loop forever receiving commands + while(true) + { + if(ui32RxCount != g_RxCount) + { + ui32RxCount = g_RxCount; + checkpoints[17]++; + + if (callback != NULL) + { + callback(inBuffer.buffer,inBuffer.used); + } + + freeArray(&inBuffer); + initArray(&inBuffer, 1); + } + + } +} + diff --git a/Software/Embedded_SW/Embedded/Drivers/USB_Communication/USBCDCD.c b/Software/Embedded_SW/Embedded/Drivers/USB_Communication/USBCDCD.c index a24e760bd..60ef7feb3 100644 --- a/Software/Embedded_SW/Embedded/Drivers/USB_Communication/USBCDCD.c +++ b/Software/Embedded_SW/Embedded/Drivers/USB_Communication/USBCDCD.c @@ -266,9 +266,7 @@ static tLineCoding g_sLineCoding = { uint32_t SendChars(char* buffer,size_t length) { - USBCDCD_sendData(buffer, length,10); - checkpoints[4]++; - + return USBCDCD_sendData(buffer, length,10); } //***************************************************************************** // diff --git a/Software/Embedded_SW/Embedded/Include.h b/Software/Embedded_SW/Embedded/Include.h index 598f22667..699f20a98 100644 --- a/Software/Embedded_SW/Embedded/Include.h +++ b/Software/Embedded_SW/Embedded/Include.h @@ -1,3 +1,6 @@ +#ifndef INCLUDE_H_ +#define INCLUDE_H_ + //standard lib includes #include <stdbool.h> #include <stdio.h> @@ -50,5 +53,9 @@ #include "Common/report/report.h" #define SYS_CLK_FREQ 120000000 + +typedef uint32_t (* callback_fptr)(uint32_t deviceID, uint32_t ReadValue); + extern void SysTickHandler(void); +#endif // INCLUDE_H_ diff --git a/Software/Embedded_SW/Embedded/Main.c b/Software/Embedded_SW/Embedded/Main.c index ef111330a..dc0755406 100644 --- a/Software/Embedded_SW/Embedded/Main.c +++ b/Software/Embedded_SW/Embedded/Main.c @@ -30,6 +30,7 @@ #include "Drivers/USBStick_FileSys/USBMSCHFatFs.h" */ #include "Modules/Control/control.h" +#include "Modules/Heaters/Heaters_ex.h" #include "StateMachines/Printing/PrintingSTM.h" extern Semaphore_Handle sdCardSem; //#define WATCHDOG diff --git a/Software/Embedded_SW/Embedded/Modules/Control/DriverWithCallbackExample.c b/Software/Embedded_SW/Embedded/Modules/Control/DriverWithCallbackExample.c index 216779f8b..1de8ac0de 100644 --- a/Software/Embedded_SW/Embedded/Modules/Control/DriverWithCallbackExample.c +++ b/Software/Embedded_SW/Embedded/Modules/Control/DriverWithCallbackExample.c @@ -1,7 +1,53 @@ /* * DriverWithCallbackExample.c * - * Created on: 11 במרץ 2018 + * Created on: 11 march 2018 * Author: shlomo */ -uint32_t +#include "include.h" +#include "control.h" + + +uint32_t KeepParameter = 0; +callback_fptr ModuleCallback = 0; +bool isValid (uint32_t deviceID); +uint32_t ControlCallBackFunction(uint32_t deviceID, uint32_t ReadValue); + + + +uint32_t DriverActionWithCallback (uint32_t deviceId, uint32_t parameter, callback_fptr callback) +{ + assert (callback); + assert (isValid(deviceId)); + + //call driver action to device id with the parameter + //SetMotorSpeed (deviceId, parameter); + KeepParameter = parameter; + ModuleCallback = callback; + //start control: + + uint32_t ControlId = AddControlCallback( callback, eOneMillisecond, NULL, deviceId, parameter ); + return ControlId; + +} + +uint32_t ControlCallBackFunction(uint32_t deviceId, uint32_t ReadValue) +{ + if (ReadValue == KeepParameter) + { + //stop this control loop + RemoveControlCallback(deviceId, ControlCallBackFunction ); + //possibly: start regular control (speed etc) + //uint32_t ControlId = AddControlCallback(ControlCBFunction Callback, eOneMillisecond, NULL, deviceId, Parameter ); + + //call the module callback + ModuleCallback(deviceId,ReadValue); + + } + return OK; +} + +bool isValid (uint32_t deviceID) +{ + return true; +} diff --git a/Software/Embedded_SW/Embedded/Modules/Control/MillisecTask.c b/Software/Embedded_SW/Embedded/Modules/Control/MillisecTask.c index 6f3a59852..ded4c9f9e 100644 --- a/Software/Embedded_SW/Embedded/Modules/Control/MillisecTask.c +++ b/Software/Embedded_SW/Embedded/Modules/Control/MillisecTask.c @@ -22,6 +22,10 @@ #include "drivers/adc_sampling/adc.h" #include "control.h" + +#include "drivers/Motors/Motor.h" +#include "drivers/Heater/TemperatureSensor.h" + /******************** Definitions ********************************************/ #define INVALID_MSG_ID 0xFFFF #define MAX_TANGO_CONTROL_DEVICES 200 @@ -30,7 +34,7 @@ typedef enum { OneMillisec, -}nillisecMessages; +}MillisecMessages; typedef struct MillisecMessage{ uint16_t messageId; @@ -38,6 +42,13 @@ typedef struct MillisecMessage{ uint32_t tick; uint8_t messageData[20]; }MillisecMessageStruc; + +uint32_t ADC_Data[MAX_ADC_DEVICES] = {0}; +uint32_t TemperatureSensor_Data[MAX_TEMPERATURE_SENSOR_ID] = {0}; +uint32_t MotorSpeed_Data[MOTOR_SPARE1_1] = {0}; +uint32_t MotorStatus_Data[MOTOR_SPARE1_1] = {0}; +bool MotorBusy_Data[MOTOR_SPARE1_1] = {true}; + /******************** GLOBAL PARAMETERS ********************************************/ Mailbox_Handle MillisecMsgQ = NULL; bool MillisecRestart; @@ -105,9 +116,6 @@ void OneMilliSecondMillisecInterrupt(UArg arg0) else ROM_TimerDisable(Millisec_timerBase,TIMER_A); - //trigger the ADC collection - check and set priorities to make sure handling timing is correct. - //we might want to call it from the task, afetr execution of other taks!!! - ADC_TriggerCollection(); //send message to the Millisec task Message.messageId = OneMillisec; Message.tick = UsersysTickGet(); @@ -133,7 +141,46 @@ uint32_t MillisecLoop(uint32_t tick) Hundred_msTick = (tick%eHunderdMillisecond == 0) ?true:false; Onesecond_Tick = (tick%eOneSecond == 0) ?true:false; + //gather Motor data from FPGA + MotorSpeed_Data[MOTOR_DRYER_DRIVING] = MotorGetSpeedFromFPGA(MOTOR_DRYER_DRIVING); + MotorStatus_Data[MOTOR_DRYER_DRIVING] = MotorGetStatusFromFPGA(MOTOR_DRYER_DRIVING); + MotorSpeed_Data[MOTOR_SCREW] = MotorGetSpeedFromFPGA(MOTOR_SCREW); + MotorStatus_Data[MOTOR_SCREW] = MotorGetStatusFromFPGA(MOTOR_SCREW); + MotorSpeed_Data[MOTOR_WINDER] = MotorGetSpeedFromFPGA(MOTOR_WINDER); + MotorStatus_Data[MOTOR_WINDER] = MotorGetStatusFromFPGA(MOTOR_WINDER); + MotorSpeed_Data[MOTOR_LDRIVING] = MotorGetSpeedFromFPGA(MOTOR_LDRIVING); + MotorStatus_Data[MOTOR_LDRIVING] = MotorGetStatusFromFPGA(MOTOR_LDRIVING); + MotorSpeed_Data[MOTOR_RDRIVING] = MotorGetSpeedFromFPGA(MOTOR_RDRIVING); + MotorStatus_Data[MOTOR_RDRIVING] = MotorGetStatusFromFPGA(MOTOR_RDRIVING); + //gather Dancer data from FPGA //gather data from FPGA + + if (Hundred_msTick) + { + int adc_i; + for (adc_i = 0; adc_i < MAX_ADC_DEVICES ; adc_i++) + ADC_Data[adc_i] = ADC_GetReading(adc_i); + //trigger the ADC collection - check and set priorities to make sure handling timing is correct. + //we might want to call it from the task, after execution of other tasks!!! + ADC_TriggerCollection(); + + TEMPERATURE_SENSOR_ID_ENUM pt100_i; + for (pt100_i = 0; pt100_i < (int)MAX_TEMPERATURE_SENSOR_ID ; pt100_i++) + TemperatureSensor_Data[pt100_i] = TemperatureSensorReadFromFPGA(pt100_i); + MotorGetnBusyFromFPGA(); // get all motors nBusy bit status from the FPGAs + TimerMotors_t motor_i; + for (motor_i = 0; motor_i < MOTOR_SPARE1_1 ; motor_i++) + { + MotorBusy_Data[motor_i] = MotorGetnBusyState(motor_i); + if (MotorBusy_Data[motor_i] == false) //can get data + { + MotorSpeed_Data[motor_i] = MotorGetSpeedFromFPGA(motor_i); + MotorStatus_Data[motor_i] = MotorGetStatusFromFPGA(motor_i); + } + } + } + //gather data from FPGA + return OK; } /****************************************************************************** @@ -163,3 +210,28 @@ void MillisecTask(UArg arg0, UArg arg1) } } } + + +uint32_t getMotorStatusData(int MotorId) +{ + assert (MotorId < MOTOR_SPARE1_1); + return MotorStatus_Data[MotorId]; +} +uint32_t getMotorSpeedData(int MotorId) +{ + assert (MotorId < MOTOR_SPARE1_1); + return MotorSpeed_Data[MotorId]; +} + +uint32_t getTemperatureSensorData(int SensorId) +{ + assert (SensorId < MAX_TEMPERATURE_SENSOR_ID); + return TemperatureSensor_Data[SensorId]; +} + +uint32_t getADCData(int DeviceId) +{ + assert (DeviceId < MAX_ADC_DEVICES); + return ADC_Data[DeviceId]; +} + diff --git a/Software/Embedded_SW/Embedded/Modules/Control/MillisecTask.h b/Software/Embedded_SW/Embedded/Modules/Control/MillisecTask.h new file mode 100644 index 000000000..0c9d66201 --- /dev/null +++ b/Software/Embedded_SW/Embedded/Modules/Control/MillisecTask.h @@ -0,0 +1,25 @@ +/* + * MillisecTask.h + * + * Created on: 15 במרץ 2018 + * Author: shlomo + */ + +#ifndef MODULES_CONTROL_MILLISECTASK_H_ +#define MODULES_CONTROL_MILLISECTASK_H_ + +#include "drivers/adc_sampling/adc.h" +#include "control.h" +#include "drivers/Motors/Motor.h" +#include "drivers/Heater/TemperatureSensor.h" + + +uint32_t getMotorStatusData(int MotorId); +uint32_t getMotorSpeedData(int MotorId); +uint32_t getTemperatureSensorData(int SensorId); +uint32_t getADCData(int DeviceId); + + + + +#endif /* MODULES_CONTROL_MILLISECTASK_H_ */ diff --git a/Software/Embedded_SW/Embedded/Modules/Control/control.c b/Software/Embedded_SW/Embedded/Modules/Control/control.c index abc61972d..1d9686124 100644 --- a/Software/Embedded_SW/Embedded/Modules/Control/control.c +++ b/Software/Embedded_SW/Embedded/Modules/Control/control.c @@ -62,7 +62,8 @@ typedef struct { uint32_t PartId; // the identity of the inspected/controlled part in the Devices enum. bool ControlActive; - uint32_t Parameter; + uint32_t Parameter1; + uint32_t Parameter2; DataReadCBFunction ControlDataReadPtr; ControlCBFunction ControlCallbackPtr; CTRL_TIMING_ENUM ControlTiming; @@ -97,7 +98,7 @@ void OneMilliSecondFunction(UArg arg0); //********************************************************************** /******************** CODE ********************************************/ //********************************************************************** -uint32_t TemplateDataReadCBFunction (uint32_t deviceID, uint32_t Parameter) +uint32_t TemplateDataReadCBFunction (uint32_t deviceID, uint32_t Parameter1, uint32_t Parameter2) { return 0; } @@ -160,23 +161,34 @@ void ControlStart(void) * both these callbacks can be removed. if a new call is arriving, it invalidates the previous one (no dual control or data) * ***************************************************************************************************************************************************/ -int AddControlCallback(uint32_t deviceId, ControlCBFunction Callback, CTRL_TIMING_ENUM CtrlFrequency, DataReadCBFunction DriverfPtr, uint32_t Parameter ) +uint32_t AddControlCallback( ControlCBFunction Callback, CTRL_TIMING_ENUM CtrlFrequency, DataReadCBFunction DriverfPtr, uint32_t Parameter1, uint32_t Parameter2 ) { - assert(deviceId < MAX_TANGO_CONTROL_DEVICES); assert(Callback); assert(DriverfPtr); unsigned int key; - + uint32_t device_i; + uint32_t deviceId = 0xFFFFFFFF; + for(device_i = 0;device_i < MAX_TANGO_CONTROL_DEVICES;device_i++) + { + if (ControlArray[device_i].ControlActive == false) + { + deviceId = device_i; + break; + } + } + if (deviceId == 0xFFFFFFFF) + return 0xFFFFFFFF; key = GateMutex_enter(gateControlDB); ControlArray[deviceId].ControlTiming = CtrlFrequency; ControlArray[deviceId].ControlCallbackPtr = Callback; ControlArray[deviceId].ControlActive = true; ControlArray[deviceId].ControlDataReadPtr = DriverfPtr; - ControlArray[deviceId].Parameter = Parameter; + ControlArray[deviceId].Parameter1 = Parameter1; + ControlArray[deviceId].Parameter2 = Parameter2; GateMutex_leave(gateControlDB, key); - return OK; + return deviceId; } int RemoveControlCallback(uint32_t deviceId , ControlCBFunction Callback) @@ -212,9 +224,6 @@ void OneMilliSecondControlInterrupt(UArg arg0) else ROM_TimerDisable(Control_timerBase, TIMER_A); - //trigger the ADC collection - check and set priorities to make sure handling timing is correct. - //we might want to call it from the task, afetr execution of other taks!!! - ADC_TriggerCollection(); //send message to the control task Message.messageId = OneMillisec; Message.tick = UsersysTickGet(); @@ -243,33 +252,41 @@ uint32_t ControlLoop(uint32_t tick) for (Device_i = 0; Device_i < MAX_TANGO_CONTROL_DEVICES;Device_i++) { - if (ControlArray[Device_i].ControlDataReadPtr) - ControlDatalog[Device_i] = ControlArray[Device_i].ControlDataReadPtr(Device_i, ControlArray[Device_i].Parameter); if (ControlArray[Device_i].ControlActive) { switch (ControlArray[Device_i].ControlTiming) { case eOneMillisecond: + ControlDatalog[Device_i] = ControlArray[Device_i].ControlDataReadPtr(Device_i, ControlArray[Device_i].Parameter1,ControlArray[Device_i].Parameter2); ControlArray[Device_i].ControlCallbackPtr(Device_i, ControlDatalog[Device_i]); break; case eTenMilliSecond: if (Ten_msTick) + { + ControlDatalog[Device_i] = ControlArray[Device_i].ControlDataReadPtr(Device_i, ControlArray[Device_i].Parameter1,ControlArray[Device_i].Parameter2); ControlArray[Device_i].ControlCallbackPtr(Device_i, ControlDatalog[Device_i]); + } break; case eHunderdMillisecond: if (Hundred_msTick) + { + ControlDatalog[Device_i] = ControlArray[Device_i].ControlDataReadPtr(Device_i, ControlArray[Device_i].Parameter1,ControlArray[Device_i].Parameter2); ControlArray[Device_i].ControlCallbackPtr(Device_i, ControlDatalog[Device_i]); + } break; case eOneSecond: if (Onesecond_Tick) + { + ControlDatalog[Device_i] = ControlArray[Device_i].ControlDataReadPtr(Device_i, ControlArray[Device_i].Parameter1,ControlArray[Device_i].Parameter2); ControlArray[Device_i].ControlCallbackPtr(Device_i, ControlDatalog[Device_i]); + } break; default: LOG_ERROR (ControlArray[Device_i].ControlTiming, "Invalid control timing value"); break; - } - } - } + } //switch + } //if control active + } //for return OK; } diff --git a/Software/Embedded_SW/Embedded/Modules/Control/control.h b/Software/Embedded_SW/Embedded/Modules/Control/control.h index 292bbeb82..f443a10d7 100644 --- a/Software/Embedded_SW/Embedded/Modules/Control/control.h +++ b/Software/Embedded_SW/Embedded/Modules/Control/control.h @@ -10,7 +10,7 @@ //typedef uint32_t (* DeviceDataFunction)(uint32_t deviceID, uint32_t *Value); typedef uint32_t (* ControlCBFunction)(uint32_t deviceID, uint32_t ReadValue); -typedef uint32_t (* DataReadCBFunction)(uint32_t deviceID, uint32_t Parameter); +typedef uint32_t (* DataReadCBFunction)(uint32_t deviceID, uint32_t Parameter1, uint32_t Parameter2); typedef enum { eNoControl = 0, eOneMillisecond = 1, @@ -20,10 +20,11 @@ typedef enum { }CTRL_TIMING_ENUM; -void ControlInit(void); -void ControlStop(void); -void ControlStart(void); -int AddControlCallback(uint32_t deviceId, ControlCBFunction Callback, CTRL_TIMING_ENUM CtrlFrequency, DataReadCBFunction DriverfPtr, uint32_t Parameter ); -int RemoveControlCallback(uint32_t deviceId, ControlCBFunction Callback ); -uint32_t TemplateDataReadCBFunction (uint32_t deviceId, uint32_t Parameter); +void ControlInit(void); +void ControlStop(void); +void ControlStart(void); +uint32_t AddControlCallback( ControlCBFunction Callback, CTRL_TIMING_ENUM CtrlFrequency, DataReadCBFunction DriverfPtr, uint32_t Parameter1, uint32_t Parameter2 ); +int RemoveControlCallback(uint32_t deviceId, ControlCBFunction Callback ); +uint32_t TemplateDataReadCBFunction (uint32_t deviceId, uint32_t Parameter1, uint32_t Parameter2); + diff --git a/Software/Embedded_SW/Embedded/Modules/Heaters/Heaters_init.c b/Software/Embedded_SW/Embedded/Modules/Heaters/Heaters_init.c index 1162ff11e..b835cf8f1 100644 --- a/Software/Embedded_SW/Embedded/Modules/Heaters/Heaters_init.c +++ b/Software/Embedded_SW/Embedded/Modules/Heaters/Heaters_init.c @@ -23,6 +23,7 @@ #include "include.h" #include <driverlib/timer.h> +#include "heaters_ex.h" #include "heaters.h" #include "Drivers/Heater/Heater.h" @@ -50,6 +51,7 @@ HeaterControlConfig_t HeaterPIDConfig[MAX_HEATERS_NUM] = {0}; uint32_t DeviceId2Heater[MAX_HEATERS_NUM] = {0}; bool AcHeaterConfigured[MAX_AC_HEATERS] = {0}; +bool FastHeating = 1; /******************** CODE ********************************************/ /* * HeatersInit @@ -107,6 +109,10 @@ uint32_t HeaterConfigRequestMessage(HardwarePidControl* request) LOG_ERROR (ValidationError, "Validation Error in Heaters Control"); return ERROR; } + else + { + HeaterConfigSetSharedHeatersParams(1000,120000*12); // initial setting 50Mhz + } } return OK; } diff --git a/Software/Embedded_SW/Embedded/Modules/Heaters/Heaters_print.c b/Software/Embedded_SW/Embedded/Modules/Heaters/Heaters_print.c index f1920f68f..398f9463b 100644 --- a/Software/Embedded_SW/Embedded/Modules/Heaters/Heaters_print.c +++ b/Software/Embedded_SW/Embedded/Modules/Heaters/Heaters_print.c @@ -23,8 +23,22 @@ #include "heaters.h" #include "Drivers/Heater/Heater.h" +#include "Drivers/Heater/TemperatureSensor.h" +/******************** Data Structures ********************************************/ +typedef enum +{ + HeatersControl, +}HeatersControlMessages; + +typedef struct HeatersControlMessage{ + uint16_t messageId; + uint16_t msglen; + uint32_t tick; + uint8_t messageData[20]; +}HeatersControlMessageStruc; + /******************** GLOBAL PARAMETERS ********************************************/ HeaterCommand HeaterCmd[MAX_HEATERS_NUM] = {}; int NumberOFSlicesInUse = 0; @@ -35,16 +49,64 @@ uint32_t Heater_timerBase = TIMER2_BASE; //Timer handle uint32_t OutputProportionalCycleTime = 0; //A/C Heaters Cycle time in milliseconds - one for all heaters char TimeSliceAllocation[MAX_TIMESLICES] = {0xFF}; -bool InitialHeatingState = false; +bool InitialHeatingState = true; +bool TimerActivated = false; + +Mailbox_Handle HeatersControlMsgQ = NULL; + /******************** FUNCTIONS ********************************************/ uint32_t HeaterControlCBFunction(uint32_t deviceID, uint32_t ReadValue); uint32_t PrepareHeater(int HeaterId, uint32_t SetTemperatue); +void HeatersStartControlTimer (void); -/* +//********************************************************************** +//******************** CODE *******************************************/ +//********************************************************************** + +void HeatersControlInit(void) +{ + + HeatersControlMsgQ = Mailbox_create(sizeof(HeatersControlMessageStruc), 20, NULL,NULL); + + ROM_TimerConfigure(Heater_timerBase, TIMER_CFG_PERIODIC); // 32 bits Timer + ROM_IntEnable(INT_TIMER2A); + ROM_TimerIntEnable(Heater_timerBase, TIMER_TIMA_TIMEOUT); + + HeatersRestart = false; + + return; +} +void HeatersControlStop(void) +{ + Report("HeatersControlStop ", __FILE__,__LINE__,0, RpMessage, 0, 0); + TimerActivated = false; + HeatersRestart = false; +} + +void HeatersControlStart(void) +{ + Report("HeatersControlStart ", __FILE__,__LINE__,0, RpMessage, 0, 0); + HeatersRestart = true; + HeatersStartControlTimer(); +} + +void HeatersStartControlTimer (void) +{ + if (TimerActivated == true) + return; + TimerActivated = true; + ROM_TimerConfigure(Heater_timerBase, TIMER_CFG_PERIODIC); // 32 bits Timer + ROM_IntEnable(INT_TIMER2A); + ROM_TimerIntEnable(Heater_timerBase, TIMER_TIMA_TIMEOUT); + Report("HeatersStartControlTimer ", __FILE__,__LINE__,0, RpMessage, 0, 0); + return OK; +} + +/************************************************************************************** * HeaterCommandRequestMessage * called by: Communication from host * initialized all global data - */ + *************************************************************************************/ uint32_t HeaterCommandRequestMessage(int HeaterId, bool OnOff, int Temperature) { //uint32_t status = NOT_SUPPORTED; @@ -69,9 +131,6 @@ uint32_t HeaterCommandRequestMessage(int HeaterId, bool OnOff, int Temperature) //start the control PrepareHeater(HeaterId,Temperature); //prepare the heaters control info //set the power balance handler (if not set yet) - ROM_TimerConfigure(Heater_timerBase, TIMER_CFG_PERIODIC); // 32 bits Timer - ROM_IntEnable(INT_TIMER2A); - ROM_TimerIntEnable(Heater_timerBase, TIMER_TIMA_TIMEOUT); // if the heater is off (?) start it. ActivateHeater(HeaterId); //set the heater operation mode to fast heating - depended on the current temperature @@ -123,7 +182,7 @@ uint32_t PrepareHeater(int HeaterId, uint32_t SetTemperatue) HeaterPIDConfig[HeaterId].m_mesuredParam = 0; HeaterPIDConfig[HeaterId].m_preError = 0; HeaterPIDConfig[HeaterId].m_SetParam = SetTemperatue;//need to update SetParams on presegment stage - AddControlCallback(DeviceId2Heater[HeaterId], HeaterControlCBFunction, eHunderdMillisecond,TemplateDataReadCBFunction,HeaterId); + AddControlCallback( HeaterControlCBFunction, eHunderdMillisecond,TemperatureSensorRead,HeaterId,0); return OK; } @@ -196,6 +255,37 @@ uint32_t HeaterControlCBFunction(uint32_t deviceID, uint32_t ReadValue) int SliceCounter = 0; void EightMilliSecondHeatersInterrupt(UArg arg0) { + + ROM_TimerIntClear(Heater_timerBase, TIMER_TIMA_TIMEOUT); // Clear the timer interrupt + + ROM_IntMasterDisable(); + HeatersControlMessageStruc Message; + bool retcode = false; + + //send message to the HeatersControl task + Message.messageId = HeatersControl; + Message.tick = UsersysTickGet(); + Message.msglen = sizeof(HeatersControlMessageStruc); + if (HeatersControlMsgQ != NULL) + retcode = Mailbox_post(HeatersControlMsgQ , &Message, BIOS_NO_WAIT); + + if (HeatersRestart == true) + { + ROM_TimerLoadSet(Heater_timerBase, TIMER_A,OutputProportionalSingleStep/*twelve millisecond???*/); + } + else + TimerDisable(Heater_timerBase, TIMER_A); + + // + // Enable all interrupts. + // + ROM_IntMasterEnable(); + return ; +} + + +uint32_t HeatersControlLoop(uint32_t tick) +{ char str[100]; uint8_t len = 0; @@ -203,7 +293,6 @@ void EightMilliSecondHeatersInterrupt(UArg arg0) len = usnprintf(str, 100, "\r\n EightMilliSecondHeatersInterrupt SliceCounter %d Owner %d H1000 %d H2000 %d" ,SliceCounter,TimeSliceAllocation[SliceCounter],HeatersRestart,NumberOFSlicesInUse); Report(str, __FILE__,__LINE__,0, RpMessage, SliceCounter, TimeSliceAllocation[SliceCounter]); - ROM_IntMasterDisable(); if (TimeSliceAllocation[SliceCounter] == HARDWARE_PID_CONTROL_TYPE__DryerHeater1000w) { @@ -236,16 +325,32 @@ void EightMilliSecondHeatersInterrupt(UArg arg0) if (SliceCounter >= NumberOFSlicesInUse) SliceCounter = 0; - if (HeatersRestart == true) + return OK; +} +/****************************************************************************** + * ======== messageTsk ======== + * Task for this function is created statically. See the project's .cfg file. + * this message task is created statically in system initialization, + ******************************************************************************/ +void HeatersControlTask(UArg arg0, UArg arg1) +{ + HeatersControlMessageStruc Message; + //char str[60]; + //uint16_t length; + //Clock_setTimeout(HostKAClock, 1000); + //Clock_start(HostKAClock); + HeatersControlInit(); + + while(1) { - ROM_TimerLoadSet(Heater_timerBase, TIMER_A,OutputProportionalSingleStep/*twelve millisecond???*/); + Mailbox_pend(HeatersControlMsgQ , &Message, BIOS_WAIT_FOREVER); + switch (Message.messageId) + { + case HeatersControl: + HeatersControlLoop(Message.tick); + break; + default: + break; + } } - else - TimerDisable(Heater_timerBase, TIMER_A); - - // - // Enable all interrupts. - // - ROM_IntMasterEnable(); - return ; } diff --git a/Software/Embedded_SW/Embedded/Modules/IDS/IDS.h b/Software/Embedded_SW/Embedded/Modules/IDS/IDS.h index 1c9fee547..074923d79 100644 --- a/Software/Embedded_SW/Embedded/Modules/IDS/IDS.h +++ b/Software/Embedded_SW/Embedded/Modules/IDS/IDS.h @@ -1,6 +1,8 @@ +#ifndef MODULES_IDS_IDS_H_ +#define MODULES_IDS_IDS_H_ #include "PMR/common/MessageContainer.pb-c.h" #include "PMR/Hardware/HardwareDispenser.pb-c.h" - +#include "drivers/motors/motor.h" #define MAX_SYSTEM_DISPENSERS 8 typedef struct { @@ -22,4 +24,9 @@ typedef struct int32_t id; }DispenserConfigStruc; + +extern uint32_t DispenserIdToMotorId[MAX_SYSTEM_DISPENSERS]; uint32_t DispenserConfigMessage(HardwareDispenser * request); + + +#endif //MODULES_IDS_IDS_H_ diff --git a/Software/Embedded_SW/Embedded/Modules/IDS/IDS_init.c b/Software/Embedded_SW/Embedded/Modules/IDS/IDS_init.c index 0294615b6..d575b0b6e 100644 --- a/Software/Embedded_SW/Embedded/Modules/IDS/IDS_init.c +++ b/Software/Embedded_SW/Embedded/Modules/IDS/IDS_init.c @@ -12,6 +12,8 @@ DispenserConfigStruc DispensersCfg[ MAX_SYSTEM_DISPENSERS]; +uint32_t DispenserIdToMotorId[MAX_SYSTEM_DISPENSERS] = {MOTOR_DISPENSER_1,MOTOR_DISPENSER_2,MOTOR_DISPENSER_3,MOTOR_DISPENSER_4,MOTOR_DISPENSER_5,MOTOR_DISPENSER_6,MOTOR_DISPENSER_7,MOTOR_DISPENSER_8,}; + uint32_t DispenserConfigMessage(HardwareDispenser * request) diff --git a/Software/Embedded_SW/Embedded/Modules/IDS/IDS_print.c b/Software/Embedded_SW/Embedded/Modules/IDS/IDS_print.c index 32b147569..13ea3d77e 100644 --- a/Software/Embedded_SW/Embedded/Modules/IDS/IDS_print.c +++ b/Software/Embedded_SW/Embedded/Modules/IDS/IDS_print.c @@ -11,6 +11,7 @@ #include "PMR/Hardware/Hardwaremotor.pb-c.h" #include "PMR/Hardware/HardwareDispenser.pb-c.h" #include "StateMachines/Printing/printingSTM.h" +#include "drivers/motors/motor.h" diff --git a/Software/Embedded_SW/Embedded/Modules/Thread/Thread.h b/Software/Embedded_SW/Embedded/Modules/Thread/Thread.h index 475f63795..f0dd35da9 100644 --- a/Software/Embedded_SW/Embedded/Modules/Thread/Thread.h +++ b/Software/Embedded_SW/Embedded/Modules/Thread/Thread.h @@ -1,3 +1,7 @@ + +#ifndef MODULES_THREAD_THREAD_H_ +#define MODULES_THREAD_THREAD_H_ + #include "PMR/common/MessageContainer.pb-c.h" #include "PMR/Hardware/HardwareMotor.pb-c.h" #include "PMR/Hardware/HardwareMotorType.pb-c.h" @@ -53,3 +57,7 @@ uint32_t MotorsConfigMessage(HardwareMotor * request); uint32_t InternalWindingConfigMessage(JobSpool* request); uint32_t DancerConfigMessage(HardwareDancer * request); + + +#endif //MODULES_THREAD_THREAD_H_ + diff --git a/Software/Embedded_SW/Embedded/Modules/Thread/Thread_print.c b/Software/Embedded_SW/Embedded/Modules/Thread/Thread_print.c index 13c81fb39..44e5c8936 100644 --- a/Software/Embedded_SW/Embedded/Modules/Thread/Thread_print.c +++ b/Software/Embedded_SW/Embedded/Modules/Thread/Thread_print.c @@ -10,6 +10,9 @@ #include "../control/pidalgo.h" #include "PMR/Hardware/HardwareMotor.pb-c.h" #include "PMR/Hardware/HardwareMotorType.pb-c.h" +#include "drivers/Motors/Motor.h" +#include "drivers/Heater/TemperatureSensor.h" +#include "drivers/Heater/Heater.h" ////////////////////////////////State machine operation//////////////////////////////////// //the state machine operation is used to operate in runtime correct profile flow execution //by recieved esign flow of the user from the UI @@ -186,8 +189,9 @@ static ReturnCode PrepareState(void *JobDetails) MotorControlConfig[Motor_i].m_mesuredParam = 0; MotorControlConfig[Motor_i].m_preError = 0; MotorControlConfig[Motor_i].m_SetParam = 0;//need to update SetParams on presegment stage - AddControlCallback(DeviceId2Motor[Motor_i], ThreadControlCBFunction, eOneMillisecond,TemplateDataReadCBFunction,Motor_i); + AddControlCallback(ThreadControlCBFunction, eOneMillisecond,TemplateDataReadCBFunction,Motor_i,0); } + //set 3 dancers to the profile positions return NextState; } @@ -196,10 +200,10 @@ static ReturnCode PrepareState(void *JobDetails) static ReturnCode PreSegmentState(void *JobDetails) { - int Motor_i; + TimerMotors_t Motor_i; for (Motor_i = 0;Motor_i < MAX_THREAD_MOTORS_NUM;Motor_i++) { - MotorControlConfig[Motor_i].m_SetParam = getMotorFreq(Motor_i);//need to update SetParams on presegment stage + MotorControlConfig[Motor_i].m_SetParam = MotorGetSpeed(getMotorId(Motor_i));//need to update SetParams on presegment stage } // set the new speed in the dryer motor to the speed of the new segment // activate control fr all motors diff --git a/Software/Embedded_SW/Embedded/StateMachines/Printing/JobSTM.c b/Software/Embedded_SW/Embedded/StateMachines/Printing/JobSTM.c index 0247be584..154278ab9 100644 --- a/Software/Embedded_SW/Embedded/StateMachines/Printing/JobSTM.c +++ b/Software/Embedded_SW/Embedded/StateMachines/Printing/JobSTM.c @@ -155,6 +155,12 @@ static ReturnCode PrepareState(void *JobDetails) { ReturnCode retcode; retcode = JobSuccess; + //start (fast??) heating + //wait for fast heating to end + //start other peripheral systems: chiller, waist handling + //check thread type + //prepare winder + return retcode; } diff --git a/Software/Embedded_SW/Embedded/StateMachines/Printing/PrintingSTM.c b/Software/Embedded_SW/Embedded/StateMachines/Printing/PrintingSTM.c index 76b7b2a69..a3e810127 100644 --- a/Software/Embedded_SW/Embedded/StateMachines/Printing/PrintingSTM.c +++ b/Software/Embedded_SW/Embedded/StateMachines/Printing/PrintingSTM.c @@ -116,6 +116,7 @@ static ReturnCode EntryState(void *JobDetails) //******************************************************************************************************************** static ReturnCode PrepareState(void *JobDetails) { + return NextState; } |
