Shivam Garg / Mbed OS EVAL-ADBMS2950_copy

Dependencies:   ADBMS2950

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers pal.cpp Source File

pal.cpp

00001 /**
00002 ********************************************************************************
00003 *
00004 * @file:    pal.c
00005 *
00006 * @brief:   This file contains the pal function implementation.
00007 *
00008 * @details:
00009 *
00010 *******************************************************************************
00011 Copyright(c) 2020 Analog Devices, Inc. All Rights Reserved. This software is
00012 proprietary & confidential to Analog Devices, Inc. and its licensors. By using
00013 this software you agree to the terms of the associated Analog Devices License
00014 Agreement.
00015 *******************************************************************************
00016 */
00017 
00018 /*! \addtogroup Platform_Abstracion_Layer
00019 *  @{
00020 */
00021 #include "pal.h"
00022 #define WAKEUP_DELAY 1                          /* BMS ic wakeup delay ms*/
00023 
00024 #ifdef MBED
00025 extern SPI spi;
00026 extern Timer timer;
00027 extern DigitalOut chip_select;
00028 
00029 /**
00030  *******************************************************************************
00031  * Function: Delay_ms
00032  * @brief Delay mili second
00033  *
00034  * @details This function insert delay in ms.
00035  *
00036  * Parameters:
00037  * @param [in]  delay   Delay_ms
00038  *
00039  * @return None
00040  *
00041  *******************************************************************************
00042 */
00043 void Delay_ms(uint32_t delay)
00044 {
00045   wait_ms((int)delay);
00046 }
00047 
00048 /**
00049  *******************************************************************************
00050  * Function: adBmsCsLow
00051  * @brief Select chip select low
00052  *
00053  * @details This function does spi chip select low.
00054  *
00055  * @return None
00056  *
00057  *******************************************************************************
00058 */
00059 void adBmsCsLow()
00060 {
00061   spi.lock();
00062   chip_select = 0;
00063 }
00064 
00065 /**
00066  *******************************************************************************
00067  * Function: adBmsCsHigh
00068  * @brief Select chip select High
00069  *
00070  * @details This function does spi chip select high.
00071  *
00072  * @return None
00073  *
00074  *******************************************************************************
00075 */
00076 void adBmsCsHigh()
00077 {
00078   chip_select = 1;
00079   spi.unlock();
00080 }
00081 
00082 /**
00083  *******************************************************************************
00084  * Function: spiWriteBytes
00085  * @brief Writes an array of bytes out of the SPI port.
00086  *
00087  * @details This function wakeup bms ic in IsoSpi mode send dumy byte data in spi line..
00088  *
00089  * @param [in]  *tx_Data    Tx data pointer
00090  *
00091  * @param [in]   size       Numberof bytes to be send on the SPI line
00092  *
00093  * @return None
00094  *
00095  *******************************************************************************
00096 */
00097 void spiWriteBytes(uint8_t *tx_data, uint16_t size)
00098 {
00099   uint8_t rx_data[size];
00100   spi.write((char *)tx_data, size ,(char *)rx_data, size);
00101 }
00102 
00103 /**
00104  *******************************************************************************
00105  * Function: spiWriteReadBytes
00106  * @brief Writes and read a set number of bytes using the SPI port.
00107  *
00108  * @details This function writes and read a set number of bytes using the SPI port.
00109  *
00110  * @param [in]  *tx_data    Tx data pointer
00111  *
00112  * @param [in]  *rx_data    Rx data pointer
00113  *
00114  * @param [in]  size            Data size
00115  *
00116  * @return None
00117  *
00118  *******************************************************************************
00119 */
00120 void spiWriteReadBytes
00121 (
00122 uint8_t *tx_data,                   /*array of data to be written on SPI port*/
00123 uint8_t *rx_data,                   /*Input: array that will store the data read by the SPI port*/
00124 uint16_t size                       /*Option: number of bytes*/
00125 )
00126 {
00127   uint16_t data_size = (4 + size);
00128   uint8_t cmd[data_size];
00129   memcpy(&cmd[0], &tx_data[0], 4); /* dst, src, size */
00130   spi.write((char *)cmd, data_size ,(char *)cmd, data_size);
00131   memcpy(&rx_data[0], &cmd[4], size); /* dst, src, size */
00132 }
00133 
00134 /**
00135  *******************************************************************************
00136  * Function: spiReadBytes
00137  * @brief Read number of bytes using the SPI port.
00138  *
00139  * @details This function Read a set number of bytes using the SPI port.
00140  *
00141  * @param [in]  *rx_data    Rx data pointer
00142  *
00143  * @param [in]  size        Data size
00144  *
00145  * @return None
00146  *
00147  *******************************************************************************
00148 */
00149 void spiReadBytes(uint8_t *rx_data, uint16_t size)
00150 {
00151   uint8_t tx_data[size];
00152   for(uint8_t i=0; i < size; i++)
00153   {
00154     tx_data[i] = 0xFF;
00155   }
00156   spi.write((char *)tx_data, size ,(char *)rx_data, size);
00157 }
00158 
00159 /**
00160  *******************************************************************************
00161  * Function: startTimer()
00162  * @brief Start timer
00163  *
00164  * @details This function start the timer.
00165  *
00166  * @return None
00167  *
00168  *******************************************************************************
00169 */
00170 void startTimer()
00171 {
00172   timer.start();
00173 }
00174 
00175 /**
00176  *******************************************************************************
00177  * Function: stopTimer()
00178  * @brief Stop timer
00179  *
00180  * @details This function stop the timer.
00181  *
00182  * @return None
00183  *
00184  *******************************************************************************
00185 */
00186 void stopTimer()
00187 {
00188   timer.stop();
00189 }
00190 
00191 /**
00192  *******************************************************************************
00193  * Function: getTimCount()
00194  * @brief Get Timer Count Value
00195  *
00196  * @details This function return the timer count value.
00197  *
00198  * @return tim_count
00199  *
00200  *******************************************************************************
00201 */
00202 uint32_t getTimCount()
00203 {
00204   uint32_t count = 0;
00205   count = timer.read_us();
00206   timer.reset();
00207   return(count);
00208 }
00209 #else
00210 
00211 #include "main.h"
00212 #include "stm32f4xx_hal.h"
00213 #include "stm32f4xx_hal_tim.h"
00214 #include "stm32f4xx_it.h"
00215 
00216 extern SPI_HandleTypeDef hspi1;                     /* Mcu dependent SPI1 handler */
00217 extern SPI_HandleTypeDef hspi5;                     /* Mcu dependent SPI5 handler */
00218 extern TIM_HandleTypeDef htim2;                     /* Mcu dependent TIM2 handler */
00219 extern TIM_HandleTypeDef htim5;                     /* Mcu dependent TIM5 handler */
00220 
00221 #define SPI_TIME_OUT HAL_MAX_DELAY                  /* SPI Time out delay                   */
00222 #define BMS_CS_PIN ARDUINO_GPIO10_Pin               /* Mcu dependent BMS chip select        */
00223 #define BMS_GPIO_PORT ARDUINO_GPIO10_GPIO_Port      /* Mcu dependent BMS chip select port   */
00224 
00225 SPI_HandleTypeDef       *spi1   = &hspi1;           /* MUC SPI1 Handler  */
00226 SPI_HandleTypeDef       *spi5   = &hspi5;           /* MUC SPI5 Handler  */
00227 TIM_HandleTypeDef       *tim2   = &htim2;           /* MUC TIM2 Handler  */
00228 TIM_HandleTypeDef       *tim4   = &htim5;           /* MUC TIM5 Handler  */
00229 
00230 /**
00231  *******************************************************************************
00232  * Function: Delay_ms
00233  * @brief Delay mili second
00234  *
00235  * @details This function insert delay in ms.
00236  *
00237  * Parameters:
00238  * @param [in]  delay   Delay_ms
00239  *
00240  * @return None
00241  *
00242  *******************************************************************************
00243 */
00244 void Delay_ms(uint32_t delay)
00245 {
00246   HAL_Delay(delay);
00247 }
00248 
00249 /**
00250  *******************************************************************************
00251  * Function: adBmsCsLow
00252  * @brief Select chip select low
00253  *
00254  * @details This function does spi chip select low.
00255  *
00256  * @return None
00257  *
00258  *******************************************************************************
00259 */
00260 void adBmsCsLow()
00261 {
00262   HAL_GPIO_WritePin(BMS_GPIO_PORT, BMS_CS_PIN, GPIO_PIN_RESET);
00263 }
00264 
00265 /**
00266  *******************************************************************************
00267  * Function: adBmsCsHigh
00268  * @brief Select chip select High
00269  *
00270  * @details This function does spi chip select high.
00271  *
00272  * @return None
00273  *
00274  *******************************************************************************
00275 */
00276 void adBmsCsHigh()
00277 {
00278   HAL_GPIO_WritePin(BMS_GPIO_PORT, BMS_CS_PIN, GPIO_PIN_SET);
00279 }
00280 
00281 /**
00282  *******************************************************************************
00283  * Function: spiWriteBytes
00284  * @brief Writes an array of bytes out of the SPI port.
00285  *
00286  * @details This function sends the byte data into spi mosi line.
00287  *
00288  * @param [in]  *tx_Data    Tx data pointer
00289  *
00290  * @param [in]  size            Numberof bytes to be send on the SPI line
00291  *
00292  * @return None
00293  *
00294  *******************************************************************************
00295 */
00296 void spiWriteBytes(uint8_t *tx_data, uint16_t size)
00297 {
00298   HAL_SPI_Transmit(spi1, tx_data, size, SPI_TIME_OUT); /* SPI1 , data, size, timeout */
00299 }
00300 
00301 /**
00302  *******************************************************************************
00303  * Function: spiWriteReadBytes
00304  * @brief Writes and read a set number of bytes using the SPI port.
00305  *
00306  * @details This function writes and read a set number of bytes into spi port.
00307  *
00308  * @param [in]  *tx_data    Tx data pointer
00309  *
00310  * @param [in]  *rx_data    Rx data pointer
00311  *
00312  * @param [in]  size            Data size
00313  *
00314  * @return None
00315  *
00316  *******************************************************************************
00317 */
00318 void spiWriteReadBytes(uint8_t *tx_data, uint8_t *rx_data, uint16_t size)
00319 {
00320   HAL_SPI_Transmit(spi1, tx_data, 4, SPI_TIME_OUT);
00321   HAL_SPI_Receive(spi1, rx_data, size, SPI_TIME_OUT);
00322 }
00323 
00324 /**
00325  *******************************************************************************
00326  * Function: spiReadBytes
00327  * @brief Read number of bytes using the SPI port.
00328  *
00329  * @details This function Read a set number of bytes spi miso line.
00330  *
00331  * @param [in]  *rx_data    Rx data pointer
00332  *
00333  * @param [in]  size            Data size
00334  *
00335  * @return None
00336  *
00337  *******************************************************************************
00338 */
00339 void spiReadBytes(uint8_t *rx_data, uint16_t size)
00340 {
00341   HAL_SPI_Receive(spi1, rx_data, size, SPI_TIME_OUT);
00342 }
00343 
00344 /**
00345  *******************************************************************************
00346  * Function: startTimer()
00347  * @brief Start timer
00348  *
00349  * @details This function start the timer.
00350  *
00351  * @return None
00352  *
00353  *******************************************************************************
00354 */
00355 void startTimer()
00356 {
00357   HAL_TIM_Base_Start(tim2);
00358 }
00359 
00360 /**
00361  *******************************************************************************
00362  * Function: stopTimer()
00363  * @brief Stop timer
00364  *
00365  * @details This function stop the timer.
00366  *
00367  * @return None
00368  *
00369  *******************************************************************************
00370 */
00371 void stopTimer()
00372 {
00373   HAL_TIM_Base_Stop(tim2);
00374 }
00375 
00376 /**
00377  *******************************************************************************
00378  * Function: getTimCount()
00379  * @brief Get Timer Count Value
00380  *
00381  * @details This function return the timer count value.
00382  *
00383  * @return tim_count
00384  *
00385  *******************************************************************************
00386 */
00387 uint32_t getTimCount()
00388 {
00389   uint32_t count = 0;
00390   count = __HAL_TIM_GetCounter(tim2);
00391   __HAL_TIM_SetCounter(tim2, 0);
00392   return(count);
00393 }
00394 #endif /* MBED */
00395 
00396 /**
00397  *******************************************************************************
00398  * Function: adBmsWakeupIc
00399  * @brief Wakeup bms ic using chip select
00400  *
00401  * @details This function wakeup thr bms ic using chip select.
00402  *
00403  * @param [in]  total_ic    Total_ic
00404  *
00405  * @return None
00406  *
00407  *******************************************************************************
00408 */
00409 void adBmsWakeupIc(uint8_t total_ic)
00410 {
00411   for (uint8_t ic = 0; ic < total_ic; ic++)
00412   {
00413     adBmsCsLow();
00414     Delay_ms(WAKEUP_DELAY);
00415     adBmsCsHigh();
00416     Delay_ms(WAKEUP_DELAY);
00417   }
00418 }
00419 
00420 /** @}*/