AnsweredAssumed Answered

using HAL SPI drivers to read and write to SPI

Question asked by ben_ishai.ephraim on Nov 22, 2015
hi there!
writing fine to SPI slave, however, when trying to read, writing stops working:
     for(;;){
          for (i=0; i<=128; i++){
               Pot0Write(i);
               Pot1Write(i);
          //  res0=Pot0Read();
          //  res1=Pot1Read();
            printf("%5d:  POT0 %d\t POT1 %d\n\r",i,res0,res1);
               osDelay(10);
          }

void Pot0Write(int data){     
          HAL_GPIO_WritePin(GPIOD, GPIO_PIN_1, GPIO_PIN_RESET);  //set CS line active low
          HAL_SPI_Transmit(&hspi2,(uint8_t *)&pot0Write,1, 0);
          HAL_SPI_Transmit(&hspi2,(uint8_t *)&data,1, 0);
//          HAL_SPI_TransmitReceive(&hspi2,(uint8_t *)&pot0Write , &RxData, 1,0);
//          HAL_SPI_TransmitReceive(&hspi2,(uint8_t *)&data , &RxData, 1,0);
       HAL_GPIO_WritePin(GPIOD, GPIO_PIN_1, GPIO_PIN_SET);  //reset CS line active high
}
/**                            
  * @brief  writes to MCP4231 first sends a address/command, then data
  * @param  None
  * @retval None
  */
void Pot1Write(int data){     
          HAL_GPIO_WritePin(GPIOD, GPIO_PIN_1, GPIO_PIN_RESET);  //set CS line active low
       HAL_SPI_Transmit(&hspi2,(uint8_t *)&pot1Write,1, 0);
          HAL_SPI_Transmit(&hspi2,(uint8_t *)&data,1, 0);
     
     //     HAL_SPI_TransmitReceive(&hspi2,(uint8_t *)&pot0Write , &RxData, 1,0);
     //     HAL_SPI_TransmitReceive(&hspi2,(uint8_t *)&data , &RxData, 1,0);
       HAL_GPIO_WritePin(GPIOD, GPIO_PIN_1, GPIO_PIN_SET);  //reset CS line active high
}
/**                            
  * @brief  writes to MCP4231 first sends a address/command, then reads the data
  * @param  None
  * @retval None
  */
uint8_t  Pot0Read(void){     
          uint8_t RxData;
          HAL_GPIO_WritePin(GPIOD, GPIO_PIN_1, GPIO_PIN_RESET);  //set CS line active low
          HAL_SPI_Transmit(&hspi2,(uint8_t *)&pot0Read,1, 0);
       HAL_SPI_Receive(&hspi2,&RxData, 1, 5000);
       HAL_GPIO_WritePin(GPIOD, GPIO_PIN_1, GPIO_PIN_SET);  //reset CS line active high
          return RxData;
}
/**                            
  * @brief  writes to MCP4231 first sends a address/command, then reads the data
  * @param  None
  * @retval None
  */
uint8_t  Pot1Read(void){     
          uint8_t RxData;
          HAL_GPIO_WritePin(GPIOD, GPIO_PIN_1, GPIO_PIN_RESET);  //set CS line active low
          HAL_SPI_Transmit(&hspi2,(uint8_t *)&pot1Read,1, 0);
       HAL_SPI_Receive(&hspi2,&RxData, 1, 5000);
       HAL_GPIO_WritePin(GPIOD, GPIO_PIN_1, GPIO_PIN_SET);  //reset CS line active high
          return RxData;
}

Outcomes