uint16_t DRV8301_readSpi(DRV8301_Handle handle, const DRV8301_RegName_e regName) { // Actuate chipselect HAL_GPIO_WritePin(handle->nCSgpioHandle, handle->nCSgpioNumber, GPIO_PIN_RESET); osDelay(1); // Do blocking read uint16_t zerobuff = 0; uint16_t controlword = (uint16_t)DRV8301_buildCtrlWord(DRV8301_CtrlMode_Read, regName, 0); uint16_t recbuff = 0xbeef; HAL_SPI_Transmit(handle->spiHandle, (uint8_t*)(&controlword), 1, 1000); // Datasheet says you don't have to pulse the nCS between transfers, (16 clocks should commit the transfer) // but for some reason you actually need to pulse it. // Actuate chipselect HAL_GPIO_WritePin(handle->nCSgpioHandle, handle->nCSgpioNumber, GPIO_PIN_SET); osDelay(1); // Actuate chipselect HAL_GPIO_WritePin(handle->nCSgpioHandle, handle->nCSgpioNumber, GPIO_PIN_RESET); osDelay(1); HAL_SPI_TransmitReceive(handle->spiHandle, (uint8_t*)(&zerobuff), (uint8_t*)(&recbuff), 1, 1000); osDelay(1); // Actuate chipselect HAL_GPIO_WritePin(handle->nCSgpioHandle, handle->nCSgpioNumber, GPIO_PIN_SET); osDelay(1); assert(recbuff != 0xbeef); return(recbuff & DRV8301_DATA_MASK); } // end of DRV8301_readSpi() function
void DRV8301_writeSpi(DRV8301_Handle handle, const DRV8301_RegName_e regName,const uint16_t data) { // Actuate chipselect HAL_GPIO_WritePin(handle->nCSgpioHandle, handle->nCSgpioNumber, GPIO_PIN_RESET); osDelay(1); // Do blocking write uint16_t controlword = (uint16_t)DRV8301_buildCtrlWord(DRV8301_CtrlMode_Write, regName, data); HAL_SPI_Transmit(handle->spiHandle, (uint8_t*)(&controlword), 1, 1000); osDelay(1); // Actuate chipselect HAL_GPIO_WritePin(handle->nCSgpioHandle, handle->nCSgpioNumber, GPIO_PIN_SET); osDelay(1); return; } // end of DRV8301_writeSpi() function
void DRV8301_writeSpi(DRV8301_Handle handle, const DRV8301_RegName_e regName,const uint16_t data) { DRV8301_Obj *obj = (DRV8301_Obj *)handle; uint16_t ctrlWord; // build the command ctrlWord = (uint16_t)DRV8301_buildCtrlWord(DRV8301_CtrlMode_Write,regName,data); // reset the Rx fifo pointer to zero SPI_resetRxFifo(obj->spiHandle); SPI_enableRxFifo(obj->spiHandle); // write the command (time N) SPI_write(obj->spiHandle,ctrlWord); return; } // end of DRV8301_writeSpi() function
uint16_t DRV8301_readSpi(DRV8301_Handle handle,const DRV8301_RegName_e regName) { DRV8301_Obj *obj = (DRV8301_Obj *)handle; uint16_t ctrlWord; const uint16_t data = 0; volatile uint16_t readWord; static volatile uint16_t WaitTimeOut = 0; volatile SPI_FifoStatus_e RxFifoCnt = SPI_FifoStatus_Empty; // build the control word ctrlWord = (uint16_t)DRV8301_buildCtrlWord(DRV8301_CtrlMode_Read,regName,data); // reset the Rx fifo pointer to zero SPI_resetRxFifo(obj->spiHandle); SPI_enableRxFifo(obj->spiHandle); // write the command SPI_write(obj->spiHandle,ctrlWord); // dummy write to return the reply from the 8301 SPI_write(obj->spiHandle,0x0000); // wait for two words to populate the RX fifo, or a wait timeout will occur while((RxFifoCnt < SPI_FifoStatus_2_Words) && (WaitTimeOut < 0xffff)) { RxFifoCnt = SPI_getRxFifoStatus(obj->spiHandle); if(++WaitTimeOut > 0xfffe) { obj->RxTimeOut = true; } } // Read two words, the dummy word and the data readWord = SPI_readEmu(obj->spiHandle); readWord = SPI_readEmu(obj->spiHandle); return(readWord & DRV8301_DATA_MASK); } // end of DRV8301_readSpi() function