- Main Page
- Data Structures
- Files

- File List
- Globals

# main.c

Go to the documentation of this file.

    00001 //******************************************************************************
    00002 //THIS PROGRAM IS PROVIDED "AS IS". TI MAKES NO WARRANTIES OR
    00003 //REPRESENTATIONS, EITHER EXPRESS, IMPLIED OR STATUTORY,
    00004 //INCLUDING ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
    00005 //FOR A PARTICULAR PURPOSE, LACK OF VIRUSES, ACCURACY OR
    00006 //COMPLETENESS OF RESPONSES, RESULTS AND LACK OF NEGLIGENCE.
    00007 //TI DISCLAIMS ANY WARRANTY OF TITLE, QUIET ENJOYMENT, QUIET
    00008 //POSSESSION, AND NON-INFRINGEMENT OF ANY THIRD PARTY
    00009 //INTELLECTUAL PROPERTY RIGHTS WITH REGARD TO THE PROGRAM OR
    00010 //YOUR USE OF THE PROGRAM.
    00011 //
    00012 //IN NO EVENT SHALL TI BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
    00013 //CONSEQUENTIAL OR INDIRECT DAMAGES, HOWEVER CAUSED, ON ANY
    00014 //THEORY OF LIABILITY AND WHETHER OR NOT TI HAS BEEN ADVISED
    00015 //OF THE POSSIBILITY OF SUCH DAMAGES, ARISING IN ANY WAY OUT
    00016 //OF THIS AGREEMENT, THE PROGRAM, OR YOUR USE OF THE PROGRAM.
    00017 //EXCLUDED DAMAGES INCLUDE, BUT ARE NOT LIMITED TO, COST OF
    00018 //REMOVAL OR REINSTALLATION, COMPUTER TIME, LABOR COSTS, LOSS
    00019 //OF GOODWILL, LOSS OF PROFITS, LOSS OF SAVINGS, OR LOSS OF
    00020 //USE OR INTERRUPTION OF BUSINESS. IN NO EVENT WILL TI'S
    00021 //AGGREGATE LIABILITY UNDER THIS AGREEMENT OR ARISING OUT OF
    00022 //YOUR USE OF THE PROGRAM EXCEED FIVE HUNDRED DOLLARS
    00023 //(U.S.$500).
    00024 //
    00025 //Unless otherwise stated, the Program written and copyrighted
    00026 //by Texas Instruments is distributed as "freeware".  You may,
    00027 //only under TI's copyright in the Program, use and modify the
    00028 //Program without any charge or restriction.  You may
    00029 //distribute to third parties, provided that you transfer a
    00030 //copy of this license to the third party and the third party
    00031 //agrees to these terms by its first use of the Program. You
    00032 //must reproduce the copyright notice and any other legend of
    00033 //ownership on each copy or partial copy, of the Program.
    00034 //
    00035 //You acknowledge and agree that the Program contains
    00036 //copyrighted material, trade secrets and other TI proprietary
    00037 //information and is protected by copyright laws,
    00038 //international copyright treaties, and trade secret laws, as
    00039 //well as other intellectual property laws.  To protect TI's
    00040 //rights in the Program, you agree not to decompile, reverse
    00041 //engineer, disassemble or otherwise translate any object code
    00042 //versions of the Program to a human-readable form.  You agree
    00043 //that in no event will you alter, remove or destroy any
    00044 //copyright notice included in the Program.  TI reserves all
    00045 //rights not specifically granted under this license. Except
    00046 //as specifically provided herein, nothing in this agreement
    00047 //shall be construed as conferring by implication, estoppel,
    00048 //or otherwise, upon you, any license or other right under any
    00049 //TI patents, copyrights or trade secrets.
    00050 //
    00051 //You may not use the Program in non-TI devices.
    00052 //
    00053 //This software has been submitted to export control regulations
    00054 //The ECCN is EAR99
    00055 //*****************************************************************************
    00069 #include <intrinsics.h>
    00070 #include <cstring>
    00071 #include <cstdio>
    00072
    00073 #include "MSP430.h"
    00074 #include "main.h"
    00075 #include "spi_if.h"
    00076 #include "data_flash.h"
    00077 #include "bq_pack.h"
    00078 #include "USBCDC_constructs.h"
    00079
    00080 #include "Common\types.h"
    00081 #include "Common\hal_macros.h"
    00082 #include "Common\hal_UCS.h"
    00083 #include "Common\hal_pmm.h"
    00084
    00085
    00086
    00092 #ifdef USB_COMM
    00093 char OutputString[MAX_STR_LENGTH] = "";   // Holds the outgoing string
    00094 #endif
    00095
    00096 #ifdef UART_COMM
    00097 char OutputString[MAX_STR_LENGTH] = "";   // Holds the outgoing string
    00098 #endif
    00099
    00100
    00101
    00106 //State Machine Tasks Pointer
    00107 void (*StateMachineTaskPointer)(void);    // Base States pointer
    00108
    00109 #ifdef UART_COMM
    00110 //UART
    00111 char UARTRxBuffer[MAX_STR_LENGTH]= "";
    00112 char * UARTRxBufferIndex = UARTRxBuffer;
    00113 volatile BYTE bUARTDataReceived = FALSE; // Indicates data has been received
    00114 #endif
    00115
    00116 //USB
    00117 // Indicates data has been received without an open rcv operation
    00118 volatile BYTE bDataReceived_event = FALSE;
    00119 // data receive completed event
    00120 volatile BYTE bDataReceiveCompleted_event = FALSE;
    00121 volatile BYTE bDataSendCompleted_event = FALSE;   // data send completed event
    00122 #ifdef USB_COMM
    00123 // The entire input string from the last 'return'
    00124   char wholeString[MAX_STR_LENGTH] = "";
    00125 #endif
    00126
    00127 unsigned char TEST=0x0;
    00128
    00129
    00130
    00139 void main(void)
    00140 {
    00141
    00142   InitMCU();
    00143
    00144   //Enable interrupts
    00145   __bis_SR_register(GIE);
    00146
    00147   while(bq_pack_address_discovery() != NUMBER_OF_BQ_DEVICES){
    00148
    00149
    00150     //BQ devices not discovered -> broken communication
    00151     //Disable 1sec Timer
    00152     Disable1SecTimer();
    00153
    00154     //Turn On LED to indicate failure
    00155     BQ76PL536_LED_PxOUT |= OUT_BQ_LED;
    00156
    00157     //Retry to SPI comm with the BQ76PL536 devices after 1000 MCLCK cycles
    00158     __delay_cycles(1000);
    00159
    00160   }
    00161
    00162   //Initialize detected BQ76PL536 devices
    00163   bq_pack_init();
    00164
    00165   //Initial State Machine status
    00166   StateMachineTaskPointer = &GoToLowPowerMode;
    00167
    00168   //Main system loop
    00169   for(;;)
    00170   {
    00171
    00172     // State machine entry & exit point
    00173           (*StateMachineTaskPointer)(); // jump to a task state
    00174
    00175
    00176   }  //end of for(;;)
    00177 } //end of main()
    00178
    00188 void GoToLowPowerMode(void){
    00189
    00190   //* Enter LPM awaiting for an alarm/fault event or timer*/
    00191   __bis_SR_register(LOW_POWER_MODE);
    00192
    00193 }
    00194
    00195
    00204 void BQFaultSignalTask(void){
    00205
    00206   BYTE i;
    00207
    00208   //*Read BQ device for detailed status*/
    00209   //*Read each device errors*/
    00210   for(i=0;i<NUMBER_OF_BQ_DEVICES;i++){
    00211   //read device errors
    00212   bq_dev_read_errors(&bq_pack.bq_devs[i]);
    00213
    00214   /*Clear faults */
    00215   bq_dev_clear_faults(&bq_pack.bq_devs[i]);
    00216
    00217   /*Read each device temperature*/
    00218   bq_dev_read_temps(&bq_pack.bq_devs[i]);
    00219   }
    00220
    00221   //check the POT, CUV and COV conditions
    00222   CheckFaultConditions();
    00223
    00224   //Send Fault message to the HOST
    00225   SendFaultMessageToHost();
    00226
    00227   //go to Low Power Mode
    00228   StateMachineTaskPointer = &GoToLowPowerMode;
    00229
    00230 }
    00231
    00240 void BQAlertSignalTask(void){
    00241
    00242    BYTE i;
    00243
    00244    //*Read BQ device for detailed status*/
    00245    //*Read each device errors*/
    00246    for(i=0;i<NUMBER_OF_BQ_DEVICES;i++){
    00247
    00248     bq_dev_read_errors(&bq_pack.bq_devs[i]);
    00249
    00250     //*Clear alerts */
    00251     bq_dev_clear_alerts(&bq_pack.bq_devs[i]);
    00252
    00253     //*Read each device temperature*/
    00254     bq_dev_read_temps(&bq_pack.bq_devs[i]);
    00255
    00256    }
    00257
    00258   //*Signal error to the HOST*/
    00259   //check the POT, CUV and COV conditions
    00260   CheckFaultConditions();
    00261
    00262   //Send Fault message to the HOST
    00263   SendAlertMessageToHost();
    00264
    00265   //go to Low Power Mode
    00266   StateMachineTaskPointer = &GoToLowPowerMode;
    00267
    00268 }
    00269
    00277 void Timer1SecExpiredTask(void){
    00278
    00279   //Increment BQ Pack timer
    00280   update_bq_pack_timer();
    00281
    00282   //Start Data processing
    00283   bq_pack_start_conv();
    00284
    00285 }
    00286
    00293 void DRDYSignalTask(void){
    00294
    00296   //Read data from BQ pack for overall system verification
    00297   update_bq_pack_data();
    00298
    00299   //Analyze bq_pack data and perform required action (if necessary)
    00300   BatteryPackManager();
    00301   //Send data to HOST
    00302   HostCommunicationTask();
    00303
    00304   //go to Low Power Mode
    00305   StateMachineTaskPointer = &GoToLowPowerMode;
    00306 }
    00307
    00315 void HostCommunicationTask(void){
    00316
    00317 #ifdef UART_COMM
    00318   if(bUARTDataReceived)// Some data is in the buffer; begin receiving a command
    00319   {
    00320     //Go to the UART comm routine
    00321     UARTCommunicationTask();
    00322   }
    00323 #endif
    00324
    00325 #ifdef USB_COMM
    00326   if(USB_connectionState() == ST_ENUM_ACTIVE){
    00327     //Go to the USB comm routine
    00328     USBCommunicationTask();
    00329   }
    00330 #endif
    00331
    00332
    00333
    00334 }
    00335
    00343 void SendFaultMessageToHost(void){
    00344
    00345   BYTE i;
    00346   BYTE DeviceID = 0;
    00347
    00348   //identify the device & cell with the fault condition
    00349   for(i=0;i<NUMBER_OF_BQ_DEVICES;i++){
    00350     if(bq_pack.bq_devs[i].device_status & BIT6){//check if fault bit is set
    00351       DeviceID = i;
    00352     }
    00353   }
    00354
    00355 #ifdef USB_COMM
    00356   // Prepare the outgoing string
    00357   strcpy(OutputString,"\r\nThe battery pack is in FAULT MODE\r\n");
    00358   // Send the response over USB
    00359   sendData_waitTilDone((BYTE*)OutputString,strlen(OutputString),1,10);
    00360   //print the variable
    00361   sprintf(OutputString,"Fault detected on device %u\r\n",DeviceID);
    00362   // Send the response over USB
    00363   sendData_waitTilDone((BYTE*)OutputString,strlen(OutputString),1,10);
    00364   //print the variable
    00365   sprintf(OutputString,"Device address %u\r\n",
    00366   bq_pack.bq_devs[DeviceID].device_address);
    00367   // Send the response over USB
    00368   sendData_waitTilDone((BYTE*)OutputString,strlen(OutputString),1,10);
    00369   //print the variable
    00370   sprintf(OutputString,"Device %u status 0x%X\r\n",DeviceID,
    00371   bq_pack.bq_devs[DeviceID].device_status);
    00372   // Send the response over USB
    00373   sendData_waitTilDone((BYTE*)OutputString,strlen(OutputString),1,10);
    00374
    00375   for(i=0;i<MAX_CELLS_NUMBER_IN_BQ;i++){
    00376     //print the variable
    00377     sprintf(OutputString,"Cell %u voltage %u\r\n",i,
    00378     bq_pack.bq_devs[DeviceID].cell_voltage[i]);
    00379     // Send the response over USB
    00380     sendData_waitTilDone((BYTE*)OutputString,strlen(OutputString),1,10);
    00381   }
    00382   //print the variable
    00383   sprintf(OutputString,"Device %u alert status 0x%X\r\n",DeviceID,
    00384   bq_pack.bq_devs[DeviceID].alert_status);
    00385   // Send the response over USB
    00386   sendData_waitTilDone((BYTE*)OutputString,strlen(OutputString),1,10);
    00387   //print the variable
    00388   sprintf(OutputString,"Device %u fault status 0x%X\r\n",DeviceID,
    00389   bq_pack.bq_devs[DeviceID].fault_status);
    00390   // Send the response over USB
    00391   if(sendData_waitTilDone((BYTE*)OutputString,strlen(OutputString),1,1)){return;}
    00392   //print the variable
    00393   sprintf(OutputString,"Device %u cov fault on cells 0x%X\r\n",DeviceID,
    00394   bq_pack.bq_devs[DeviceID].cov_fault);
    00395   // Send the response over USB
    00396   if(sendData_waitTilDone((BYTE*)OutputString,strlen(OutputString),1,1)){return;}
    00397   //print the variable
    00398   sprintf(OutputString,"Device %u cuv fault on cells 0x%X\r\n",DeviceID,
    00399   bq_pack.bq_devs[DeviceID].cuv_fault);
    00400   // Send the response over USB
    00401   sendData_waitTilDone((BYTE*)OutputString,strlen(OutputString),1,10);
    00402 #endif
    00403
    00404 #ifdef UART_COMM
    00405   // Prepare the outgoing string
    00406   strcpy(OutputString,"\r\nThe battery pack is in FAULT MODE\r\n");
    00407   // Send the response over UART
    00408   putsUART((BYTE*)OutputString,strlen(OutputString));
    00409   //print the variable
    00410   sprintf(OutputString,"Fault detected on device %u\r\n",DeviceID);
    00411   // Send the response over UART
    00412   putsUART((BYTE*)OutputString,strlen(OutputString));
    00413   //print the variable
    00414   sprintf(OutputString,"Device address %u\r\n",
    00415   bq_pack.bq_devs[DeviceID].device_address);
    00416   // Send the response over UART
    00417   putsUART((BYTE*)OutputString,strlen(OutputString));
    00418   //print the variable
    00419   sprintf(OutputString,"Device %u status 0x%X\r\n",DeviceID,
    00420   bq_pack.bq_devs[DeviceID].device_status);
    00421   // Send the response over UART
    00422   putsUART((BYTE*)OutputString,strlen(OutputString));
    00423
    00424   for(i=0;i<MAX_CELLS_NUMBER_IN_BQ;i++){
    00425     //print the variable
    00426     sprintf(OutputString,"Cell %u voltage %u\r\n",i,
    00427     bq_pack.bq_devs[DeviceID].cell_voltage[i]);
    00428     // Send the response over UART
    00429     putsUART((BYTE*)OutputString,strlen(OutputString));
    00430   }
    00431
    00432   sprintf(OutputString,"Device %u alert status 0x%X\r\n",DeviceID,
    00433   bq_pack.bq_devs[DeviceID].alert_status);           //print the variable
    00434   // Send the response over UART
    00435   putsUART((BYTE*)OutputString,strlen(OutputString));
    00436   sprintf(OutputString,"Device %u fault status 0x%X\r\n",DeviceID,
    00437   bq_pack.bq_devs[DeviceID].fault_status);           //print the variable
    00438   // Send the response over UART
    00439   putsUART((BYTE*)OutputString,strlen(OutputString));
    00440   sprintf(OutputString,"Device %u cov fault on cell 0x%X\r\n",
    00441   DeviceID,bq_pack.bq_devs[DeviceID].cov_fault);     //print the variable
    00442   // Send the response over UART
    00443   putsUART((BYTE*)OutputString,strlen(OutputString));
    00444   sprintf(OutputString,"Device %u cuv fault on cell 0x%X\r\n",DeviceID,
    00445   bq_pack.bq_devs[DeviceID].cuv_fault);           //print the variable
    00446   // Send the response over UART
    00447   putsUART((BYTE*)OutputString,strlen(OutputString));
    00448
    00449 #endif
    00450 }
    00451
    00452
    00459 void SendAlertMessageToHost(void){
    00460
    00461   BYTE i;
    00462   unsigned char DeviceID = 0;
    00463
    00464   //identify the device with the alert condition
    00465   for(i=0;i<NUMBER_OF_BQ_DEVICES;i++){
    00466     if(bq_pack.bq_devs[i].device_status & BIT5){//check if alert bit is set
    00467       DeviceID = i;
    00468     }
    00469   }
    00470
    00471 #ifdef USB_COMM // Prepare the outgoing string & Send data
    00472   strcpy(OutputString,"\r\nThe battery pack is in ALERT Mode\r\n");
    00473   sendData_waitTilDone((BYTE*)OutputString,strlen(OutputString),1,10);
    00474   sprintf(OutputString,"alert detected on device %u\r\n",DeviceID);
    00475   sendData_waitTilDone((BYTE*)OutputString,strlen(OutputString),1,10);
    00476   sprintf(OutputString,"Device address %u\r\n",
    00477           bq_pack.bq_devs[DeviceID].device_address);
    00478   sendData_waitTilDone((BYTE*)OutputString,strlen(OutputString),1,10);
    00479   sprintf(OutputString,"Device %u status 0x%X\r\n",
    00480           DeviceID,bq_pack.bq_devs[DeviceID].device_status);
    00481   sendData_waitTilDone((BYTE*)OutputString,strlen(OutputString),1,10);
    00482
    00483         for(i=0;i<MAX_CELLS_NUMBER_IN_BQ;i++){
    00484           sprintf(OutputString,"Cell %u voltage %u\r\n",i,
    00485                  bq_pack.bq_devs[DeviceID].cell_voltage[i]);
    00486           sendData_waitTilDone((BYTE*)OutputString,strlen(OutputString),1,10);
    00487         }
    00488         sprintf(OutputString,"Device %u alert status 0x%X\r\n",DeviceID,
    00489                 bq_pack.bq_devs[DeviceID].alert_status);
    00490         sendData_waitTilDone((BYTE*)OutputString,strlen(OutputString),1,10);
    00491         sprintf(OutputString,"Device %u alert status 0x%X\r\n",DeviceID,
    00492                 bq_pack.bq_devs[DeviceID].fault_status);
    00493         sendData_waitTilDone((BYTE*)OutputString,strlen(OutputString),1,10);
    00494         sprintf(OutputString,"Device %u cov alert on cells 0x%X\r\n",
    00495                 DeviceID,bq_pack.bq_devs[DeviceID].cov_fault);
    00496         sendData_waitTilDone((BYTE*)OutputString,strlen(OutputString),1,10);
    00497         sprintf(OutputString,"Device %u cuv alert on cells 0x%X\r\n",DeviceID,
    00498                 bq_pack.bq_devs[DeviceID].cuv_fault);
    00499         sendData_waitTilDone((BYTE*)OutputString,strlen(OutputString),1,10);
    00500 #endif
    00501
    00502 #ifdef UART_COMM  // Prepare the outgoing string & Send data
    00503   strcpy(OutputString,"\r\nThe battery pack is in alert MODE\r\n");
    00504   putsUART((BYTE*)OutputString,strlen(OutputString));
    00505
    00506   sprintf(OutputString,"alert detected on device %u\r\n",DeviceID);
    00507   putsUART((BYTE*)OutputString,strlen(OutputString));
    00508   sprintf(OutputString,"Device address %u\r\n",
    00509           bq_pack.bq_devs[DeviceID].device_address);
    00510   putsUART((BYTE*)OutputString,strlen(OutputString));
    00511
    00512   sprintf(OutputString,"Device %u status 0x%X\r\n",DeviceID,
    00513           bq_pack.bq_devs[DeviceID].device_status);
    00514   putsUART((BYTE*)OutputString,strlen(OutputString));
    00515
    00516   for(i=0;i<MAX_CELLS_NUMBER_IN_BQ;i++){
    00517           sprintf(OutputString,"Cell %u voltage %u\r\n",i,
    00518                   bq_pack.bq_devs[DeviceID].cell_voltage[i]);
    00519           putsUART((BYTE*)OutputString,strlen(OutputString));
    00520   }
    00521
    00522   sprintf(OutputString,"Device %u alert status 0x%X\r\n",DeviceID,
    00523           bq_pack.bq_devs[DeviceID].alert_status);
    00524   putsUART((BYTE*)OutputString,strlen(OutputString));
    00525   sprintf(OutputString,"Device %u alert status 0x%X\r\n",DeviceID,
    00526           bq_pack.bq_devs[DeviceID].fault_status);
    00527   putsUART((BYTE*)OutputString,strlen(OutputString));
    00528   sprintf(OutputString,"Device %u cov alert on cells 0x%X\r\n",
    00529           DeviceID,bq_pack.bq_devs[DeviceID].cov_fault);
    00530   putsUART((BYTE*)OutputString,strlen(OutputString));
    00531   sprintf(OutputString,"Device %u cuv alert on cells 0x%X\r\n",
    00532           DeviceID,bq_pack.bq_devs[DeviceID].cuv_fault);
    00533   putsUART((BYTE*)OutputString,strlen(OutputString));
    00534
    00535 #endif
    00536 }
    00537
    00538 //**
    00539 //* @brief Function Name: BQCommunicationProblemTask
    00540 //* @brief Description  : Identify if there is a communication wit
    00541 //* @param parameters   :
    00542 //* @return Value       :
    00543 //*/
    00544 void BQCommunicationProblemTask(void){
    00545 // Prepare the outgoing string & Send data
    00546 strcpy(OutputString,"\r\nThere is a communication problem with the BQ devices");
    00547
    00548 #ifdef USB_COMM
    00549   sendData_waitTilDone((BYTE*)OutputString,strlen(OutputString),1,10);
    00550 #endif
    00551
    00552 #ifdef UART_COMM // Send the response over UART
    00553   putsUART((BYTE*)OutputString,strlen(OutputString));
    00554 #endif
    00555
    00556     //Go To Low Power Mode
    00557   StateMachineTaskPointer = &GoToLowPowerMode;
    00558 }
    00559
    00566 #ifdef UART_COMM
    00567 void UARTCommunicationTask(void){
    00568
    00569     BYTE i;
    00570     bUARTDataReceived = FALSE;
    00571
    00572     if(strchr(UARTRxBuffer,0x0D) != NULL)
    00573     {
    00574
    00575       if(!(strcmp(UARTRxBuffer,"ch\r"))) // Compare to string #1, and respond
    00576       {
    00577         HOST_CONTROL_IN |= BIT0; //Set Charge mode active
    00578         strcpy(OutputString,"\r\nThe battery pack is charging\r\n");
    00579         putsUART((BYTE*)OutputString,strlen(OutputString));
    00580       }// Compare to string #2, and respond
    00581       else if(!(strcmp(UARTRxBuffer, "di\r")))
    00582       {
    00583         HOST_CONTROL_IN &= ~BIT0; //Set discharge mode active
    00584         strcpy(OutputString,"\r\nThe battery pack is discharging\r\n");
    00585         putsUART((BYTE*)OutputString,strlen(OutputString));
    00586       }
    00587       // Compare to string #3, and respond
    00588       else if(!(strcmp(UARTRxBuffer, "p\r")))
    00589       {
    00590         strcpy(OutputString,"\r\nBattery Pack Data\r\n");
    00591         putsUART((BYTE*)OutputString,strlen(OutputString));
    00592
    00593         sprintf(OutputString,"Battery Pack Mode:%u\r\n",bq_pack.op_mode);
    00594         putsUART((BYTE*)OutputString,strlen(OutputString));
    00595         sprintf(OutputString,"Battery Pack Voltage:%u\r\n",bq_pack.voltage);
    00596         putsUART((BYTE*)OutputString,strlen(OutputString));
    00597         sprintf(OutputString,"Battery Pack Lowest Cell Voltage:%u\r\n",
    00598                 bq_pack.lowest_cell_volts);
    00599         putsUART((BYTE*)OutputString,strlen(OutputString));
    00600         sprintf(OutputString,"Battery Pack Highest Voltage:%u\r\n",
    00601                 bq_pack.highest_cell_volts);
    00602         putsUART((BYTE*)OutputString,strlen(OutputString));
    00603         sprintf(OutputString,"Battery Pack Balancing Timer:%u\r\n",
    00604                 bq_pack.balancing_timer);
    00605         putsUART((BYTE*)OutputString,strlen(OutputString));
    00606         sprintf(OutputString,"Battery Pack Charge/Discharge Timer:%u\r\n",
    00607                 bq_pack.chg_dschg_op_timer);
    00608         putsUART((BYTE*)OutputString,strlen(OutputString));
    00609         sprintf(OutputString,"Battery Pack Max Balancing Timer:%u\r\n",
    00610                 bq_pack.max_balance_timer);
    00611         putsUART((BYTE*)OutputString,strlen(OutputString));
    00612         sprintf(OutputString,"Battery Pack Max Imbalancing Fail Timer:%u\r\n",
    00613                 bq_pack.cell_imbalance_fail_timer);
    00614         putsUART((BYTE*)OutputString,strlen(OutputString));
    00615         sprintf(OutputString,"Battery Pack Temperature 1:%u\r\n",
    00616                 bq_pack.bq_devs[0].temperature1);
    00617         putsUART((BYTE*)OutputString,strlen(OutputString));
    00618         sprintf(OutputString,"Battery Pack Temperature 2:%u\r\n",
    00619                 bq_pack.bq_devs[0].temperature2);
    00620         putsUART((BYTE*)OutputString,strlen(OutputString));
    00621       // Compare to string #3, and respond
    00622       }else if(!(strcmp(UARTRxBuffer, "c\r")))
    00623       {
    00624         static unsigned short DeviceID;
    00625
    00626         strcpy(OutputString,"\r\nBQ76PL536 Data\r\n");
    00627         putsUART((BYTE*)OutputString,strlen(OutputString));
    00628         sprintf(OutputString,"Device %u cell count %u\r\n",DeviceID,
    00629                 bq_pack.bq_devs[DeviceID].cell_count);
    00630         putsUART((BYTE*)OutputString,strlen(OutputString));
    00631         sprintf(OutputString,"Device %u address %u\r\n",DeviceID,
    00632                 bq_pack.bq_devs[DeviceID].device_address);
    00633         putsUART((BYTE*)OutputString,strlen(OutputString));
    00634         sprintf(OutputString,"Device %u status 0x%X\r\n",DeviceID,
    00635                 bq_pack.bq_devs[DeviceID].device_status);
    00636         putsUART((BYTE*)OutputString,strlen(OutputString));
    00637
    00638         for(i=0;i<MAX_CELLS_NUMBER_IN_BQ;i++){
    00639           sprintf(OutputString,"Cell %u voltage %u\r\n",i,
    00640                   bq_pack.bq_devs[DeviceID].cell_voltage[i]);
    00641           putsUART((BYTE*)OutputString,strlen(OutputString));
    00642         }
    00643
    00644         sprintf(OutputString,"Device %u alert status 0x%X\r\n",DeviceID,
    00645                 bq_pack.bq_devs[DeviceID].alert_status);
    00646         putsUART((BYTE*)OutputString,strlen(OutputString));
    00647         sprintf(OutputString,"Device %u fault status 0x%X\r\n",DeviceID,
    00648                 bq_pack.bq_devs[DeviceID].fault_status);
    00649         putsUART((BYTE*)OutputString,strlen(OutputString));
    00650         sprintf(OutputString,"Device %u cov fault on cells 0x%X\r\n",
    00651                 DeviceID,bq_pack.bq_devs[DeviceID].cov_fault);
    00652         putsUART((BYTE*)OutputString,strlen(OutputString));
    00653         sprintf(OutputString,"Device %u cuv fault on cells 0x%X\r\n",
    00654                 DeviceID,bq_pack.bq_devs[DeviceID].cuv_fault);
    00655         putsUART((BYTE*)OutputString,strlen(OutputString));
    00656         sprintf(OutputString,"Device %u Imbalanced Cells 0x%X\r\n",
    00657                 DeviceID,(0x003F & find_imbalanced_cell(DeviceID)));
    00658         putsUART((BYTE*)OutputString,strlen(OutputString));
    00659
    00660
    00661
    00662         DeviceID++;
    00663         if(DeviceID > (NUMBER_OF_BQ_DEVICES-1)){
    00664            DeviceID = 0;
    00665         }
    00666       }
    00667       else// Handle other
    00668       {
    00669         strcpy(OutputString,"Invalid Command!\r\n");
    00670         putsUART((BYTE*)OutputString,strlen(OutputString));
    00671         strcpy(OutputString,
    00672       "\r\nMenu:\r\nCharge:ch\r\nDischarge:di\r\nPackInfo:p\r\nCellInfo:c\r\n");
    00673         putsUART((BYTE*)OutputString,strlen(OutputString));
    00674
    00675       }
    00676        // Clear the string in preparation for the next one
    00677       for(i=0;i<MAX_STR_LENGTH;i++){
    00678         UARTRxBuffer[i] = NULL;
    00679         OutputString[i] = NULL;
    00680       }
    00681
    00682       UARTRxBufferIndex = &UARTRxBuffer[0]; //reset index
    00683     }
    00684
    00685   //go to Low Power Mode
    00686 //  StateMachineTaskPointer = &GoToLowPowerMode;
    00687 }
    00688 #endif
    00689
    00690
    00697 #ifdef USB_COMM
    00698 // Holds the new addition to the string
    00699 static char pieceOfString[MAX_STR_LENGTH] = "";
    00700 static unsigned short DeviceID;
    00701 void USBCommunicationTask(void)
    00702 {
    00703   BYTE i;
    00704   // Check the USB state and directly main loop accordingly
    00705   switch(USB_connectionState())
    00706   {
    00707   case ST_USB_DISCONNECTED:
    00708        break;
    00709
    00710   case ST_USB_CONNECTED_NO_ENUM:
    00711        break;
    00712
    00713   case ST_ENUM_ACTIVE:
    00714     // Some data is in the buffer; begin receiving a command
    00715     if(bDataReceived_event)
    00716     {
    00717       bDataReceived_event = FALSE;
    00718       // Add bytes in USB buffer to theCommand
    00719       receiveDataInBuffer((BYTE*)pieceOfString,MAX_STR_LENGTH,1);
    00720       strcat(wholeString,pieceOfString);
    00721       // Has the user pressed return yet?
    00722       if(strchr(wholeString,0x0D) != NULL)
    00723       {
    00724         // Compare to string #1, and respond
    00725         if(!(strcmp(wholeString, "ch\r")))
    00726         {
    00727           HOST_CONTROL_IN |= BIT0; //Set Charge mode active
    00728           strcpy(OutputString,"\r\nThe battery pack is charging\r\n");
    00729           sendData_waitTilDone((BYTE*)OutputString,strlen(OutputString),1,10);
    00730         } // Compare to string #2, and respond
    00731         else if(!(strcmp(wholeString, "di\r")))
    00732         {
    00733           HOST_CONTROL_IN &= ~BIT0; //Set discharge mode active
    00734           strcpy(OutputString,"\r\nThe battery pack is discharging\r\n");
    00735           sendData_waitTilDone((BYTE*)OutputString,strlen(OutputString),1,10);
    00736         }
    00737         // Compare to string #3, and respond
    00738         else if(!(strcmp(wholeString, "p\r")))
    00739         {
    00740           strcpy(OutputString,"\r\nBattery Pack Data\r\n");
    00741           sendData_waitTilDone((BYTE*)OutputString,strlen(OutputString),1,10);
    00742           sprintf(OutputString,"Battery Pack Mode:%u\r\n",bq_pack.op_mode);
    00743           sendData_waitTilDone((BYTE*)OutputString,strlen(OutputString),1,10);
    00744           sprintf(OutputString,"Battery Pack Voltage:%u\r\n",bq_pack.voltage);
    00745           sendData_waitTilDone((BYTE*)OutputString,strlen(OutputString),1,10);
    00746           sprintf(OutputString,"Battery Pack Lowest Cell Voltage:%u\r\n",
    00747                   bq_pack.lowest_cell_volts);
    00748           sendData_waitTilDone((BYTE*)OutputString,strlen(OutputString),1,10);
    00749           sprintf(OutputString,"Battery Pack Highest Voltage:%u\r\n",
    00750                   bq_pack.highest_cell_volts);
    00751           sendData_waitTilDone((BYTE*)OutputString,strlen(OutputString),1,10);
    00752           sprintf(OutputString,"Battery Pack Balancing Timer:%u\r\n",
    00753                   bq_pack.balancing_timer);
    00754           sendData_waitTilDone((BYTE*)OutputString,strlen(OutputString),1,10);
    00755           sprintf(OutputString,"Battery Pack Charge/Discharge Timer:%u\r\n",
    00756                   bq_pack.chg_dschg_op_timer);
    00757           sendData_waitTilDone((BYTE*)OutputString,strlen(OutputString),1,10);
    00758           sprintf(OutputString,"Battery Pack Max Balancing Timer:%u\r\n",
    00759                   bq_pack.max_balance_timer);
    00760           sendData_waitTilDone((BYTE*)OutputString,strlen(OutputString),1,10);
    00761           sprintf(OutputString,"Battery Pack Max Imbalancing Fail Timer:%u\r\n",
    00762                   bq_pack.cell_imbalance_fail_timer);
    00763           sendData_waitTilDone((BYTE*)OutputString,strlen(OutputString),1,10);
    00764           sprintf(OutputString,"Battery Pack Temperature 1:%u\r\n",
    00765                   bq_pack.bq_devs[0].temperature1);
    00766           sendData_waitTilDone((BYTE*)OutputString,strlen(OutputString),1,10);
    00767           sprintf(OutputString,"Battery Pack Temperature 2:%u\r\n",
    00768                   bq_pack.bq_devs[0].temperature2);
    00769           sendData_waitTilDone((BYTE*)OutputString,strlen(OutputString),1,10);
    00770
    00771         }// Compare to string #3, and respond
    00772         else if(!(strcmp(wholeString, "c\r")))
    00773         {
    00774
    00775           strcpy(OutputString,"\r\nBQ76PL536 Data\r\n");
    00776           sendData_waitTilDone((BYTE*)OutputString,strlen(OutputString),1,10);
    00777           sprintf(OutputString,"Device %u cell count %u\r\n",DeviceID,
    00778                   bq_pack.bq_devs[DeviceID].cell_count);
    00779           sendData_waitTilDone((BYTE*)OutputString,strlen(OutputString),1,10);
    00780           sprintf(OutputString,"Device %u address %u\r\n",DeviceID,
    00781                   bq_pack.bq_devs[DeviceID].device_address);
    00782           sendData_waitTilDone((BYTE*)OutputString,strlen(OutputString),1,10);
    00783           sprintf(OutputString,"Device %u status 0x%X\r\n",DeviceID,
    00784                   bq_pack.bq_devs[DeviceID].device_status);
    00785           sendData_waitTilDone((BYTE*)OutputString,strlen(OutputString),1,10);
    00786           for(i=0;i<MAX_CELLS_NUMBER_IN_BQ;i++){
    00787             sprintf(OutputString,"Cell %u voltage %u\r\n",i,
    00788                     bq_pack.bq_devs[DeviceID].cell_voltage[i]);
    00789             sendData_waitTilDone((BYTE*)OutputString,strlen(OutputString),1,10);
    00790           }
    00791           sprintf(OutputString,"Device %u alert status 0x%X\r\n",
    00792                   DeviceID,bq_pack.bq_devs[DeviceID].alert_status);
    00793           sendData_waitTilDone((BYTE*)OutputString,strlen(OutputString),1,10);
    00794           sprintf(OutputString,"Device %u fault status 0x%X\r\n",DeviceID,
    00795                   bq_pack.bq_devs[DeviceID].fault_status);
    00796           sendData_waitTilDone((BYTE*)OutputString,strlen(OutputString),1,10);
    00797           sprintf(OutputString,"Device %u cov fault on cells 0x%X\r\n",
    00798                   DeviceID,bq_pack.bq_devs[DeviceID].cov_fault);
    00799           sendData_waitTilDone((BYTE*)OutputString,strlen(OutputString),1,10);
    00800           sprintf(OutputString,"Device %u cuv fault on cells 0x%X\r\n",
    00801                   DeviceID,bq_pack.bq_devs[DeviceID].cuv_fault);
    00802           sendData_waitTilDone((BYTE*)OutputString,strlen(OutputString),1,10);
    00803           sprintf(OutputString,"Device %u Imbalanced Cells 0x%X\r\n",
    00804                   DeviceID,(0x003F & find_imbalanced_cell(DeviceID)));
    00805           sendData_waitTilDone((BYTE*)OutputString,strlen(OutputString),1,10);
    00806           DeviceID++;
    00807           if(DeviceID > (NUMBER_OF_BQ_DEVICES-1)){
    00808              DeviceID = 0;
    00809           }
    00810         }
    00811         else// Handle other
    00812         {
    00813           strcpy(OutputString,"\r\nInvalid Command!\r\n");
    00814           sendData_waitTilDone((BYTE*)OutputString,strlen(OutputString),1,10);
    00815           strcpy(OutputString,
    00816           "Menu:\r\nCharge:ch\r\nDischarge:di\r\nPack Info:p\r\nCellInfo:c\r\n");
    00817           sendData_waitTilDone((BYTE*)OutputString,strlen(OutputString),1,10);
    00818         }// Clear the string in preparation for the next one
    00819         for(i=0;i<MAX_STR_LENGTH;i++){
    00820           wholeString[i] = NULL;
    00821           pieceOfString[i] = NULL;
    00822           }//end of for
    00823         }//end of if car return received conditional
    00824       }//end of data received conditional
    00825           break;
    00826
    00827    case ST_ENUM_SUSPENDED:
    00828         break;
    00829
    00830    case ST_ENUM_IN_PROGRESS:
    00831         break;
    00832
    00833    case ST_NOENUM_SUSPENDED:
    00834         break;
    00835
    00836    case ST_ERROR:
    00837         break;
    00838
    00839    default:
    00840      break;
    00841   }//end of switch-case sentence
    00842
    00843   //go to Low Power Mode
    00844   StateMachineTaskPointer = &GoToLowPowerMode;
    00845 }
    00846 #endif
    00847
    00848
    00852 //ISR for the DRDY,ALERT,FAULT signals
    00853 static void NotUsedPortISR(void);
    00854 static void BQ76PL536_FAULT_ISR(void);
    00855 static void BQ76PL536_DRDY_ISR(void);
    00856 static void BQ76PL536_ALERT_ISR(void);
    00857
    00858
    00859 //ISR definition for each port, hast to be updated if the ports are reassigned
    00860 //to other pins
    00861 typedef void(*pt2Func)(void);
    00862
    00863 pt2Func pBQ76PL536PortsISR[]={//the order is defined by the port number
    00864
    00865   NotUsedPortISR,         // Handler for PORT Px.x Interrupt
    00866   NotUsedPortISR,         // Handler for PORT Px.0 Interrupt
    00867   BQ76PL536_FAULT_ISR,    // Handler for PORT Px.1 Interrupt
    00868   BQ76PL536_ALERT_ISR,    // Handler for PORT Px.2 Interrupt
    00869   BQ76PL536_DRDY_ISR,     // Handler for PORT Px.3 Interrupt
    00870   NotUsedPortISR,         // Handler for PORT Px.4 Interrupt
    00871   NotUsedPortISR,         // Handler for PORT Px.5 Interrupt
    00872   NotUsedPortISR,         // Handler for PORT Px.6 Interrupt
    00873   NotUsedPortISR,         // Handler for PORT Px.7 Interrupt
    00874
    00875 };
    00876
    00877
    00884 static void NotUsedPortISR(void){
    00885
    00886   BQ76PL536_FAULT_PxIFG = 0x00;         //clear the port interrupt flags
    00887   BQ76PL536_DRDY_PxIFG = 0x00;          //clear the port interrupt flags
    00888   BQ76PL536_ALERT_PxIFG = 0x00;         //clear the port interrupt flags
    00889
    00890 }
    00891
    00898 static void BQ76PL536_FAULT_ISR(void){
    00899   //go to the Fault task
    00900   StateMachineTaskPointer = &BQFaultSignalTask;
    00901   //Clear interrupt flags
    00902   BQ76PL536_FAULT_PxIFG &= ~IN_BQ_FAULT;
    00903 }
    00904
    00911 static void BQ76PL536_ALERT_ISR(void){
    00912   //go to the Alert Task
    00913   StateMachineTaskPointer = &BQAlertSignalTask;
    00914   //Clear interrupt flags
    00915   BQ76PL536_ALERT_PxIFG &= ~IN_BQ_ALERT;
    00916
    00917 }
    00918
    00925 static void BQ76PL536_DRDY_ISR(void){
    00926   //BQ76PL536 ADC conversion ready
    00927   StateMachineTaskPointer = &DRDYSignalTask;
    00928   //Clear interrupt flags
    00929   BQ76PL536_DRDY_PxIFG  &= ~IN_BQ_DRDY;
    00930
    00931 }
    00932
    00942 #ifdef UART_COMM
    00943 #pragma vector=UCAxRX_VECTOR
    00944 __interrupt void USCIxRX_ISR(void){
    00945
    00946   switch(UCAxIV)
    00947   {
    00948
    00949   case 0x02:
    00950     //fill the RX buffer
    00951     *UARTRxBufferIndex++ = UCAxRXBUF;
    00952     //round buffer
    00953     if(UARTRxBufferIndex >= &UARTRxBuffer[MAX_STR_LENGTH])
    00954       UARTRxBufferIndex = &UARTRxBuffer[0]; //reset index
    00955     bUARTDataReceived = TRUE;
    00956     UCAxIFG &= ~UCAxRXIFG;           // Clear RX IFG
    00957     break;
    00958
    00959   case 0x04://TX interrupt - disabled
    00960     break;
    00961   default:
    00962     break;
    00963
    00964   }
    00965     // Clear LPM bits from 0(SR)
    00966   __bic_SR_register_on_exit(LOW_POWER_MODE);
    00967 }
    00968 #endif
    00969
    00976 #pragma vector=Timer1Sec_TIMER_VECTOR
    00977 __interrupt void Timer1SecISR (void)
    00978 {
    00979
    00980   //Go to Timer 1Sec ExpiredT ask
    00981   StateMachineTaskPointer = &Timer1SecExpiredTask;
    00982
    00983   //Toggle LED pin
    00984   BQ76PL536_LED_PxOUT ^= OUT_BQ_LED;
    00985
    00986
    00987    // Clear TACCRx interrupt flag
    00988   Timer1Sec_TAxCCTLx &= ~CCIFG;
    00989
    00990   // Clear LPM bits from 0(SR)
    00991   __bic_SR_register_on_exit(LOW_POWER_MODE);
    00992 }
    00993
    01000 #pragma vector=BQ76PL536_OUTPUTS_VECTOR
    01001 __interrupt void PORT_ISR(void)
    01002 {
    01003   /*Read port inputs*/
    01004
    01005   // Jump to the corresponding ISR based on the Vector register
    01006   pBQ76PL536PortsISR[(BQ76PL536_OUTPUTS_PxIV>>1)]();
    01007
    01008
    01009   // Clear LPM bits from 0(SR)
    01010   __bic_SR_register_on_exit(LOW_POWER_MODE);
    01011
    01012 }
    01013
    01020 #ifdef USB_COMM
    01021 #pragma vector = UNMI_VECTOR
    01022 __interrupt VOID UNMI_ISR(VOID)
    01023 {
    01024     switch (__even_in_range(SYSUNIV, SYSUNIV_BUSIFG))
    01025     {
    01026     case SYSUNIV_NONE:
    01027       __no_operation();
    01028       break;
    01029     case SYSUNIV_NMIIFG:
    01030       __no_operation();
    01031       break;
    01032     case SYSUNIV_OFIFG:
    01033     // Clear OSC flaut Flags fault flags
    01034       UCSCTL7 &= ~(DCOFFG+XT1LFOFFG+XT1HFOFFG+XT2OFFG);
    01035       SFRIFG1 &= ~OFIFG;   // Clear OFIFG fault flag
    01036       break;
    01037     case SYSUNIV_ACCVIFG:
    01038       __no_operation();
    01039       break;
    01040     case SYSUNIV_BUSIFG:
    01041
    01042       // If bus error occured - the cleaning of flag and re-initializing of
    01043       // USB is required.
    01044       SYSBERRIV = 0;            // clear bus error flag
    01045       USB_disable();            // Disable
    01046       break;
    01047     }
    01048 }
    01049 #endif
    01050
    01061 void InitMCU(void){
    01062
    01063   __disable_interrupt(); // Disable global interrupts
    01064   //Stop watchdog timer to prevent time out reset
    01065   WDTCTL = WDTPW + WDTHOLD;
    01066   Init_Ports(); // Init ports (do first ports because clocks do change ports)
    01067   SetVCore(PMMCOREV_3); // DVCC > 2.4V
    01068   Init_Clock(); //init clocl system
    01069 #ifdef UART_COMM
    01070   InitUART();   //init UART module
    01071 #endif
    01072
    01073 #ifdef USB_COMM
    01074   USB_init();   //init USB module
    01075 #endif
    01076   //Init SPI module
    01077   init_spi();
    01078   //*Initialize IO ports for BQ Pack input/output signals*/
    01079   InitBQ76PL536ports();
    01080   //*Initilize 1s system periodic interrupt*/
    01081   Init1SecTimer();
    01082
    01083 #ifdef USB_COMM
    01084   // Enable various USB event handling routines
    01085   USB_setEnabledEvents(kUSB_VbusOnEvent
    01086                        +kUSB_VbusOffEvent
    01087                        +kUSB_receiveCompletedEvent
    01088                        +kUSB_dataReceivedEvent+kUSB_UsbSuspendEvent
    01089                        +kUSB_UsbResumeEvent+kUSB_UsbResetEvent);
    01090
    01091 // See if we're already attached physically to USB, and if so, connect to it
    01092 // Normally applications don't invoke the event handlers,
    01093 // but this is an exception.
    01094   if (USB_connectionInfo() & kUSB_vbusPresent)
    01095     USB_handleVbusOnEvent();
    01096 #endif
    01097   __enable_interrupt(); // enable global interrupts
    01098
    01099 }
    01100
    01101
    01108 VOID Init_Clock(VOID)
    01109 {
    01110     // Initialization of clock module
    01111     // XT2 used as USB PLL reference => USB_PLL_XT = 2
    01112     P5SEL |= 0x0C;                            // enable XT2 pins
    01113
    01114     SELECT_FLLREF(SELREF__REFOCLK);           // Set DCO FLL reference = REFO
    01115     SELECT_ACLK(SELA__REFOCLK);               // Set ACLK = REFO
    01116     Init_FLL_Settle(MCLK_FREQ/1000, MCLK_FREQ/32768); // set FLL (DCOCLK)
    01117     XT2_Start(0); //Lowest Current Consumption;
    01118
    01119 }
    01120
    01128 void InitBQ76PL536ports(void)
    01129 {
    01130
    01131   //Inputs
    01132   // I/O port control pad
    01133   BQ76PL536_FAULT_PxSEL &= ~IN_BQ_FAULT;
    01134   BQ76PL536_ALERT_PxSEL &= ~IN_BQ_ALERT;
    01135   BQ76PL536_DRDY_PxSEL  &= ~IN_BQ_DRDY;
    01136
    01137   // Set pull up/down resistor configuration
    01138   BQ76PL536_FAULT_PxREN &= ~IN_BQ_FAULT;
    01139   BQ76PL536_ALERT_PxREN &= ~IN_BQ_ALERT;
    01140   BQ76PL536_DRDY_PxREN  &= ~IN_BQ_DRDY;
    01141
    01142   // Set port direction as input
    01143   BQ76PL536_FAULT_PxDIR &= ~IN_BQ_FAULT;
    01144   BQ76PL536_ALERT_PxDIR &= ~IN_BQ_ALERT;
    01145   BQ76PL536_DRDY_PxDIR  &= ~IN_BQ_DRDY;
    01146
    01147   // Clear interrupt flags
    01148   BQ76PL536_FAULT_PxIFG &= ~IN_BQ_FAULT;
    01149   BQ76PL536_ALERT_PxIFG &= ~IN_BQ_ALERT;
    01150   BQ76PL536_DRDY_PxIFG  &= ~IN_BQ_DRDY;
    01151
    01152   // interrupts enabled
    01153   BQ76PL536_FAULT_PxIE |= IN_BQ_FAULT;
    01154   BQ76PL536_ALERT_PxIE |= IN_BQ_ALERT;
    01155   BQ76PL536_DRDY_PxIE  |= IN_BQ_DRDY;
    01156
    01157   //Output
    01158   //I/O port control pad
    01159   BQ76PL536_CONV_PxSEL &= ~OUT_BQ_CONV;
    01160
    01161   //Set pin to Logic Level 0
    01162   BQ76PL536_CONV_PxOUT &= ~OUT_BQ_CONV;
    01163
    01164   //Set pull up/down resistor configuration
    01165   BQ76PL536_CONV_PxREN &= ~OUT_BQ_CONV;
    01166
    01167   //Set port direction as output
    01168   BQ76PL536_CONV_PxDIR |= OUT_BQ_CONV;
    01169
    01170   //LED  pin
    01171   // I/O port control pad
    01172   BQ76PL536_CONV_PxSEL &= ~OUT_BQ_LED;
    01173
    01174   //Set pin to Logic Level 1
    01175   BQ76PL536_CONV_PxOUT |= OUT_BQ_LED;
    01176
    01177   // Set pull up/down resistor configuration
    01178   BQ76PL536_CONV_PxREN &= ~OUT_BQ_LED;
    01179
    01180   // Set port direction as output
    01181   BQ76PL536_CONV_PxDIR |= OUT_BQ_LED;
    01182
    01183   BQ76PL536_CONV_PxSEL &= ~(OUT_BQ_CONV|OUT_BQ_LED);
    01184
    01185 }
    01186
    01187
    01195 void Init1SecTimer(void)
    01196 {
    01197   /*Initialize TimerAx in continous mode with 1us tick*/
    01198   Timer1Sec_TAxCTL = MC_0;                             //Stop TA0
    01199   Timer1Sec_TAxCTL |= TACLR;                           //Clear Timer register
    01200   Timer1Sec_TAxCCTLx = 0;                              //Stop TAx capture
    01201
    01202   Timer1Sec_TAxCCRx = 4096;                   //30.51/8us ticks
    01203   Timer1Sec_TAxCTL = TASSEL_1 | MC_1 | ID_3;  //ACLK = 32.768kHz
    01204                                               //=> TCLK = 244.14us
    01205   Timer1Sec_TAxCCTLx &= ~CCIFG;               // Clear TACCRx interrupt flag
    01206   Timer1Sec_TAxCCTLx |= CCIE;                 // TACCRx interrupt enabled
    01207
    01208 }
    01209
    01216 void Disable1SecTimer(void)
    01217 {
    01218   Timer1Sec_TAxCTL &= MC_0;                             // Stop TAx
    01219   Timer1Sec_TAxCTL |= TACLR;                           // Clear TAx
    01220
    01221 }
    01222
    01229 VOID Init_Ports(VOID)
    01230 {
    01231     // Initialization of ports all unused pins as outputs with low-level
    01232
    01233     // set all ports  to low on all pins
    01234     PAOUT   =   0x0000;
    01235     PASEL   =   0x0000;
    01236     PADIR   =   0xFFFF;
    01237
    01238     PBOUT   =   0x0000;
    01239     PBSEL   =   0x0000;
    01240     PBDIR   =   0xFFFF;
    01241
    01242     PCOUT   =   0x0000;
    01243     PCSEL   =   0x0000;
    01244     PCDIR   =   0xFFFF;
    01245
    01246     PJDIR   =   0xFFFF;
    01247     PJOUT   =   0x0000;
    01248 }
    01249
    01250 //EOF
    01251
    01252