cancel
Showing results for 
Search instead for 
Did you mean: 

using HAL SPI drivers to read and write to SPI

barry2
Associate
Posted on November 22, 2015 at 17:33

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;

}
0 REPLIES 0