Jeroen Lodder / Mbed 2 deprecated SteppermotorBoard

Dependencies:   USBDevice mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001  /**
00002  * @file    main.c
00003  * @brief   Main thread controlling everything
00004  * @details This main thread received USB commands and executes them to other modules                   
00005  *
00006  * @Author  Jeroen Lodder
00007  * @Date        October 2013
00008  *
00009  * @{
00010  */
00011  
00012 #include "mbed.h"
00013 #include "smc.h"
00014 #include "watchdog.h"
00015 #include "USBHID.h"
00016 #include "ByteOperations.h"
00017 #include "USBHIDProtocol.h"
00018 #include "CT16B1_PWM.h"
00019 
00020 /* Main routine handles USB and SMC api */
00021 
00022 #define VERSION 0x02
00023 
00024 /**
00025  * @brief   USB HID Declarations
00026  * @{
00027  */
00028 USBHID *hid;
00029 HID_REPORT send_report __attribute__((aligned (4))); // Aligned for fast access
00030 HID_REPORT recv_report __attribute__((aligned (4))); // Aligned for fast access
00031 /** 
00032  * @} 
00033  */
00034 
00035 /* Input Output */
00036 DigitalOut led(P0_21);  /**< @brief User led */
00037 DigitalIn button(P0_1); /**< @brief User button input (bootloader pin) */
00038 DigitalOut aux(P0_22);  /**< @brief Auxilary mosfet channel */
00039 
00040 /**
00041  * @brief       Debounced button reader
00042  */
00043 Ticker Ticker_button;
00044 volatile uint32_t button_history = 0xFF;
00045 void Ticker_button_handler(void){
00046     button_history <<= 1;
00047     button_history |= button;
00048 }
00049 
00050 /**
00051  * @brief   LED Blink Handler
00052  */
00053 Ticker Ticker_led;
00054 void Ticker_led_handler(void){
00055     led = !led;
00056 }
00057 
00058 /**
00059  * @brief   Clear buffer contents
00060  * @note    Requires 4 byte alignement of data
00061  * @param[in] c   Pointer to @p HID_REPORT.
00062  */
00063 void empty_report(HID_REPORT *data){
00064     register uint32_t *p = (uint32_t *)data->data;
00065     for( register int i=0; i<((sizeof(HID_REPORT)-1)/4); i++ ){
00066         *p = 0xFFFFFFFF;
00067         p++;
00068     }
00069 }
00070  
00071 /* Program entry */
00072 int main(void){
00073 /* Init */
00074     // reset flags
00075     LPC_SYSCON->SYSRSTSTAT = 0x1F;
00076     // Motor Control IO config
00077     SMC_init();
00078     // aux init
00079     aux = 0;
00080     // LED
00081     led = 0;
00082     // USB Initialize   
00083     static USBHID hid_object(64, 64);
00084     hid = &hid_object;
00085     send_report.length = 64;
00086     // button config
00087     Ticker_button.attach_us(&Ticker_button_handler, 20000);
00088         
00089     // Ready
00090     while(1){
00091         // insert idle time, since we cant be running USB stuff all the time
00092         wait_us(5000);      
00093         //try to read a msg
00094         if(hid->readNB(&recv_report)){
00095             // Data packet received, start parsing
00096             int irx=0;
00097             int itx=0;
00098             send_report.data[itx++] = recv_report.data[0];
00099             switch( recv_report.data[irx++] ){
00100                 case    CMD_SYS_CHECK       :
00101                     send_report.data[itx++] =  VERSION;
00102                 break;
00103                 case    CMD_SYS_RESET       : 
00104                       // Soft reset
00105                         SMC_deinit();
00106                         SMC_init();
00107                         CT16B1_deinit(0);
00108                         led = 1;
00109                         Ticker_led.detach();
00110                         aux = 0;
00111                         empty_report(&recv_report);
00112                 break;
00113                 case    CMD_LED_OFF         :
00114                     led = 1;    // inverted led
00115                     Ticker_led.detach();
00116                 break;
00117                 case    CMD_LED_ON          :
00118                     led = 0;    // inverted led
00119                     Ticker_led.detach();
00120                 break;
00121                 case    CMD_LED_BLINK       :
00122                     Ticker_led.attach_us(&Ticker_led_handler, 100000);
00123                 break;
00124                 case    CMD_LED_BREATH  :
00125                     // Not implemented
00126                 break;
00127                 case    CMD_GET_BUTTON  :
00128                     write_32_to_8(&itx, send_report.data, button_history);                  
00129                 break;
00130                 case    CMD_SMC_STEP        :   {       
00131                     uint32_t arg_step = read_8_to_32(&irx, recv_report.data);               
00132                     uint8_t  arg_dir    =   recv_report.data[irx++];
00133                     uint32_t arg_time = read_8_to_32(&irx, recv_report.data);               
00134                     uint8_t  arg_free   =   recv_report.data[irx++];
00135                     SMC_step(arg_step, arg_dir, arg_time, arg_free);
00136                 }
00137                 break;
00138                 case    CMD_SMC_STATE       :
00139                     write_32_to_8(&itx, send_report.data, SMC_getState());  
00140                 break;
00141                 case    CMD_SMC_STOP        :
00142                     SMC_deinit();
00143                 break;
00144                 case    CMD_SMC_PAUSE       :
00145                     SMC_pause();
00146                 break;
00147                 case    CMD_SMC_CONTINUE:
00148                     SMC_continue();
00149                 break;
00150                 case    CMD_SMC_FREE        :
00151                     SMC_free();
00152                 break;
00153                 case    CMD_SMC_BRAKE       :
00154                     SMC_brake();
00155                 break;
00156                 case    CMD_AUX_OFF         :
00157                     CT16B1_deinit(0);
00158                     aux = 0;    
00159                     //if(CT16B1_isStarted())
00160                     
00161                 break;
00162                 case    CMD_AUX_ON          :
00163                     CT16B1_deinit(1);
00164                     aux = 1;
00165                     //if(CT16B1_isStarted())
00166                 break;
00167                 case    CMD_AUX_PWM         : {
00168                     uint16_t dutycycle, period, prescaler;
00169                     dutycycle = read_8_to_16(&irx, recv_report.data);
00170                     period      = read_8_to_16(&irx, recv_report.data);
00171                     prescaler = read_8_to_16(&irx, recv_report.data);
00172                     /* Invert PWM, since timer is inverted */
00173                     dutycycle = period - dutycycle;
00174                     if(CT16B1_isStarted()){
00175                       CT16B1_wait_refresh();
00176                         CT16B1_set(1, dutycycle);
00177                     }else{
00178                         CT16B1_initpwm(period,dutycycle,prescaler);
00179                         CT16B1_start();
00180                     }
00181                 }                   
00182                 break;
00183                 case 0xEE   : {
00184                     hid->sendNB(&send_report);  
00185                     SMC_egg();
00186                     WatchDog_us bello(100);
00187                     }
00188                 break;
00189                 default:
00190                     send_report.data[0] =   0xFF;   //Failure
00191                 break;
00192             } // Switch 
00193             // Return command + optional new args
00194             hid->send(&send_report);
00195             // 0xFF unused bytes
00196             empty_report(&recv_report);         
00197             empty_report(&send_report);
00198         }       // if packet
00199     } // While
00200 } // Main
00201 /** @} */
00202