/************************************************************ * Utils.h * general module of system utility functions **************************************************************/ #include #include #include #include #include "Common/Utilities/Utils.h" //#include #include "driverlib/hibernate.h" static const float speedCoef = 5.3051647697298445256294587790838f;//(200 micro steping)/2*Pi*Radius - 200 micro steping //***************************************************************************** bool DanserCheckPosition(uint16_t position , Danser_t danser, bool test) { if (test == false) return true; #ifdef DEBUG_DANCER ProfileType_t profileType = GetCurrentProfileType(); if (position < DANSER_POS_MIN) { if(danser == Winder) { if(profileType == Manual) { JigStopAllWithError("Thread torn in winder danser");//for stoping the system operation } else if (profileType == User) { //notify the UI about an error and pause the system SendMessageToHost(ERROR_IN_PROFILE_EXECUTION,"Thread torn in winder danser"); JigPauseAll(); } } else { if(profileType == Manual) { JigStopAllWithError("Thread torn in pooler danser");//for stoping the system operation } else if (profileType == User) { //notify the UI about an error and pause the system SendMessageToHost(ERROR_IN_PROFILE_EXECUTION,"Thread torn in pooler danser"); JigPauseAll(); } } return false; } if (position > DANSER_POS_MAX) { if(danser == Pooler) { if(profileType == Manual) { JigStopAllWithError("Thread stocked in pooler danser"); } else if (profileType == User) { SendMessageToHost(ERROR_IN_PROFILE_EXECUTION,"Thread stuck in pooler danser"); JigPauseAll(); } } else { if(profileType == Manual) { JigStopAllWithError("Thread stocked in winder danser"); } else if (profileType ==User) { SendMessageToHost(ERROR_IN_PROFILE_EXECUTION,"Thread stuck in winder danser"); JigPauseAll(); } } return false; } #endif return true; } //***************************************************************************** //convert thread speed recieved in milimeters per second into motor steps of pps //***************************************************************************** uint16_t ConvertSpeed2Pps(uint16_t milimeterPerSecond) { float res = speedCoef*(float)milimeterPerSecond; return (uint16_t)res; } //***************************************************************************** uint16_t ConvertPpsToSpeed(uint16_t Pps) { float res = (float)Pps/speedCoef; return (uint16_t)res; } //***************************************************************************** // // Init WD timer // //***************************************************************************** void InitWatchdog(uint32_t clock) { // // Enable the watchdog interrupt. // ROM_IntEnable(INT_WATCHDOG); // // Set the period of the watchdog timer. // ROM_WatchdogReloadSet(WATCHDOG0_BASE, clock); // // Enable reset generation from the watchdog timer. // ROM_WatchdogResetEnable(WATCHDOG0_BASE); // // Enable the watchdog timer. // ROM_WatchdogEnable(WATCHDOG0_BASE); } void utilsInit(uint32_t ui32SysClock) { cb_init(); // SysTickPeriodSet(120000); // SysTickEnable(); // Configure Hibernate module clock. // HibernateEnableExpClk(ui32SysClock); // Enable RTC mode. // HibernateRTCEnable(); // // Configure the hibernate module counter to 24-hour calendar mode. // // HibernateCounterMode(HIBERNATE_COUNTER_24HR); // Configure the hibernate module counter to RTC counter mode. HibernateCounterMode(HIBERNATE_COUNTER_RTC); } uint32_t UsersysTickGet (void) { uint32_t tick = 0; // tick = SysTickValueGet(); tick = HibernateRTCSSGet()*10000/0x8000; return tick; } //***************************************************************************** // // The interrupt handler for the watchdog. This feeds the dog (so that the // processor does not get reset) and winks the LED connected to GPIO B3. // //***************************************************************************** void WatchdogIntHandler(void) { // // Clear the watchdog interrupt. // ROM_WatchdogIntClear(WATCHDOG0_BASE); } bool sendDataToHost = false; void UtilsSetCommunicationOk(void) { sendDataToHost = true; } typedef struct circular_buffer { void *buffer; // data buffer void *buffer_end; // end of data buffer uint16_t capacity; // maximum number of items in the buffer uint16_t count; // number of items in the buffer uint16_t sz; // size of each item in the buffer void *head; // pointer to head void *tail; // pointer to tail } circular_buffer; uint8_t LogBuffer[8002]; circular_buffer cb; void cb_init() { //cb.buffer = malloc(capacity * sz); //if(cb.buffer == NULL) // handle error cb.buffer = LogBuffer; cb.buffer_end = &LogBuffer[3950]; cb.capacity = 4000; cb.count = 0; cb.sz = 60; cb.head = cb.buffer; cb.tail = cb.buffer; } void cb_free(circular_buffer *cb) { //free(cb.buffer); // clear out other fields too, just to be safe } void cb_push_back(/*circular_buffer *cb,*/ const void *item, int size) { if(cb.count == cb.capacity){ return; // handle error } if ((int)(cb.head) + size > (int)(cb.buffer_end)){ size = (int)(cb.buffer_end) - (int)(cb.head); } memcpy(cb.head, item, size); cb.head = (char*)cb.head + size; if(cb.head >= cb.buffer_end) cb.head = cb.buffer; cb.count++; // if (sendDataToHost) // SendMessageToHost(DEBUG_LOG,item); } void cb_pop_front(/*circular_buffer *cb,*/ void *item) { if(cb.count == 0){ // handle error } memcpy(item, cb.tail, cb.sz); cb.tail = (char*)cb.tail + cb.sz; if(cb.tail == cb.buffer_end) cb.tail = cb.buffer; cb.count--; }