Yuliya Smirnova / ADE7912

Dependents:   h7adc

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers communication.cpp Source File

communication.cpp

00001 #include "communication.h"
00002 #include "cmsis_os.h"
00003 #include <cstring>
00004 
00005 //extern UART_HandleTypeDef huart3;
00006 enum _BOOL_ uart3PkgTransmited = _FALSE_;
00007 enum _BOOL_ uart3PkgReceived = _FALSE_;
00008 
00009 extern SPI_HandleTypeDef hspi1;
00010 ALIGN_32BYTES(volatile uint8_t spi1Buf[ALIGN32_SIZE(256)] __attribute__((section(".SRAM"))));
00011 ALIGN_32BYTES(volatile uint8_t spi1AddrBuf __attribute__((section(".SRAM"))));
00012 enum _BOOL_ spi1PkgTransmited = _FALSE_; 
00013 enum _BOOL_ spi1PkgReceived = _FALSE_;
00014 enum OP_TYPE spi1OpType = NONE;
00015 //uint8_t *spi1Buf;
00016 uint8_t spi1Len;
00017 
00018 //void ReceiveUARTPackage(UART_HandleTypeDef *huart, uint8_t *buf, uint8_t len)
00019 //{
00020 //  TickType_t timeBegin = xTaskGetTickCount();
00021 //  //SCB_InvalidateDCache();
00022 //  HAL_StatusTypeDef err = HAL_UART_Receive_DMA(huart, buf, len);
00023 //  while (!uart3PkgReceived  && xTaskGetTickCount() - timeBegin < COMMUNICATION_WAITTING) osDelay(COMMUNICATION_DELAY);
00024 //  
00025 //  uart3PkgReceived = _FALSE_;
00026 //}
00027 
00028 
00029 
00030 //void TransmitUARTPackage(UART_HandleTypeDef *huart, uint8_t *buf, uint8_t len)
00031 //{
00032 //  TickType_t timeBegin = xTaskGetTickCount();
00033 //  //SCB_CleanDCache();
00034 //  HAL_StatusTypeDef err = HAL_UART_Transmit_DMA(huart, buf, len);
00035 //  while (!uart3PkgTransmited  && xTaskGetTickCount() - timeBegin < COMMUNICATION_WAITTING) osDelay(COMMUNICATION_DELAY);
00036 //  
00037 //  uart3PkgTransmited = _FALSE_;
00038 //}
00039 //
00040 //
00041 //
00042 //void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
00043 //{
00044 //  if(huart == &huart3) {
00045 //      uart3PkgTransmited = _TRUE_;
00046 //  }
00047 //}
00048 //
00049 //
00050 //
00051 //void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
00052 //{
00053 //  if(huart == &huart3){
00054 //    uart3PkgReceived = _TRUE_;
00055 //  }
00056 //}
00057 
00058 
00059 
00060 void WriteToRegisterBySPI(SPI_HandleTypeDef *hspi, uint8_t addr, uint8_t *buf, uint8_t len)
00061 {
00062 //  TickType_t timeBegin = xTaskGetTickCount();
00063 
00064     enum _BOOL_ *cpltCheck;
00065     if(hspi == &hspi1) {
00066         //spi1Buf = buf;
00067         spi1Len = len;
00068         spi1OpType = WRITE;
00069         cpltCheck = &spi1PkgTransmited;
00070   } 
00071     memcpy((void*)&spi1AddrBuf, (void*)&addr, 1);
00072     SCB_CleanDCache_by_Addr((uint32_t*)&spi1AddrBuf, ALIGN32_SIZE(1));
00073     HAL_SPI_Transmit_DMA(hspi, (uint8_t*)&spi1AddrBuf, 1);
00074     memcpy((void*)&spi1Buf, (void*)buf, 1);
00075 //  while (!*cpltCheck  && xTaskGetTickCount() - timeBegin < COMMUNICATION_WAITTING) osDelay(COMMUNICATION_DELAY);
00076     *cpltCheck = _FALSE_;
00077 }
00078 
00079 
00080 
00081 void ReadFromRegisterBySPI(SPI_HandleTypeDef *hspi, uint8_t addr, uint8_t *buf, uint8_t len)
00082 {
00083 //  TickType_t timeBegin = xTaskGetTickCount();
00084     
00085     enum _BOOL_ *cpltCheck;
00086     if(hspi == &hspi1) {
00087         //spi1Buf = buf;
00088         spi1Len = len;
00089         spi1OpType = READ;
00090         cpltCheck = &spi1PkgReceived;
00091   } 
00092     
00093     memcpy((void*)&spi1AddrBuf, (void*)&addr, 1);
00094     SCB_CleanDCache_by_Addr((uint32_t*)&spi1AddrBuf, ALIGN32_SIZE(1));
00095     HAL_SPI_Transmit_DMA(hspi, (uint8_t*)&spi1AddrBuf, 1);
00096 //  while (!*cpltCheck  && xTaskGetTickCount() - timeBegin < COMMUNICATION_WAITTING) osDelay(COMMUNICATION_DELAY);
00097     *cpltCheck = _FALSE_;
00098     memcpy((void*)buf, (void*)&spi1Buf, spi1Len);
00099 }
00100 
00101 
00102 
00103 void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi)
00104 {
00105     if(hspi == &hspi1) {
00106         switch (spi1OpType)
00107         {
00108             case WRITE:
00109                 spi1OpType = CPLT;
00110                 SCB_CleanDCache_by_Addr((uint32_t*)&spi1Buf, ALIGN32_SIZE(spi1Len));
00111                 HAL_SPI_Transmit_DMA(hspi, (uint8_t*)spi1Buf, spi1Len);
00112             break;
00113             case READ:
00114                 spi1OpType = CPLT;
00115                 HAL_SPI_Receive_DMA(hspi, (uint8_t*)spi1Buf, spi1Len);
00116             break;
00117             case CPLT:
00118                 spi1PkgTransmited = _TRUE_;
00119                 spi1OpType = NONE;
00120             break;
00121             case NONE:
00122             break;
00123         }
00124   } 
00125 }
00126 
00127 
00128 
00129 void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi)
00130 {
00131     if(hspi == &hspi1) {
00132         SCB_InvalidateDCache_by_Addr((uint32_t*)&spi1Buf, ALIGN32_SIZE(spi1Len));
00133         spi1PkgReceived = _TRUE_;
00134         spi1OpType = NONE;
00135   } 
00136 }
00137