From b6ba25c4e7fd2296c34dc1f2b94a959a2ff02985 Mon Sep 17 00:00:00 2001 From: Oskar Weigl Date: Fri, 3 Jun 2016 22:21:10 +0200 Subject: [PATCH] drv driver port compiles --- Drivers/DRV8301/drv8301.c | 136 +++++++++++++++++--------------------- Drivers/DRV8301/drv8301.h | 18 +++-- Inc/tim.h | 2 + Makefile | 4 +- Src/test.c | 25 ++++--- 5 files changed, 94 insertions(+), 91 deletions(-) diff --git a/Drivers/DRV8301/drv8301.c b/Drivers/DRV8301/drv8301.c index 20c02564..5b6fb1d3 100644 --- a/Drivers/DRV8301/drv8301.c +++ b/Drivers/DRV8301/drv8301.c @@ -38,20 +38,14 @@ // ************************************************************************** // the includes +#include "assert.h" #include - +#include "cmsis_os.h" // drivers #include "drv8301.h" -// modules - - -// platforms - - - // ************************************************************************** // the defines @@ -65,27 +59,18 @@ void DRV8301_enable(DRV8301_Handle handle) { - DRV8301_Obj *obj = (DRV8301_Obj *)handle; - static volatile uint16_t enableWaitTimeOut; - uint16_t n = 0; - // Enable the drv8301 - GPIO_setHigh(obj->gpioHandle,obj->gpioNumber); + //Enable driver + HAL_GPIO_WritePin(handle->EngpioHandle, handle->EngpioNumber, GPIO_PIN_SET); - enableWaitTimeOut = 0; + //Wait for driver to come online + osDelay(20); // Make sure the Fault bit is not set during startup - while(((DRV8301_readSpi(handle,DRV8301_RegName_Status_1) & DRV8301_STATUS1_FAULT_BITS) != 0) && (enableWaitTimeOut < 1000)) - { - if(++enableWaitTimeOut > 999) - { - obj->enableTimeOut = true; - } - } + while((DRV8301_readSpi(handle,DRV8301_RegName_Status_1) & DRV8301_STATUS1_FAULT_BITS) != 0); // Wait for the DRV8301 registers to update - for(n=0;n<0xffff;n++) - asm(" NOP"); + osDelay(1); return; } @@ -281,23 +266,45 @@ DRV8301_Handle DRV8301_init(void *pMemory,const size_t numBytes) } // end of DRV8301_init() function -void DRV8301_setGpioHandle(DRV8301_Handle handle,GPIO_Handle gpioHandle) +void DRV8301_setEnGpioHandle(DRV8301_Handle handle,GPIO_Handle gpioHandle) { DRV8301_Obj *obj = (DRV8301_Obj *)handle; // initialize the gpio interface object - obj->gpioHandle = gpioHandle; + obj->EngpioHandle = gpioHandle; return; } // end of DRV8301_setGpioHandle() function -void DRV8301_setGpioNumber(DRV8301_Handle handle,GPIO_Number_e gpioNumber) +void DRV8301_setEnGpioNumber(DRV8301_Handle handle,GPIO_Number_e gpioNumber) { DRV8301_Obj *obj = (DRV8301_Obj *)handle; // initialize the gpio interface object - obj->gpioNumber = gpioNumber; + obj->EngpioNumber = gpioNumber; + + return; +} // end of DRV8301_setGpioNumber() function + + +void DRV8301_setnCSGpioHandle(DRV8301_Handle handle,GPIO_Handle gpioHandle) +{ + DRV8301_Obj *obj = (DRV8301_Obj *)handle; + + // initialize the gpio interface object + obj->nCSgpioHandle = gpioHandle; + + return; +} // end of DRV8301_setGpioHandle() function + + +void DRV8301_setnCSGpioNumber(DRV8301_Handle handle,GPIO_Number_e gpioNumber) +{ + DRV8301_Obj *obj = (DRV8301_Obj *)handle; + + // initialize the gpio interface object + obj->nCSgpioNumber = gpioNumber; return; } // end of DRV8301_setGpioNumber() function @@ -350,45 +357,28 @@ bool DRV8301_isReset(DRV8301_Handle handle) } // end of DRV8301_isReset() function -uint16_t DRV8301_readSpi(DRV8301_Handle handle,const DRV8301_RegName_e regName) +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; + // Actuate chipselect + HAL_GPIO_WritePin(handle->nCSgpioHandle, handle->nCSgpioNumber, GPIO_PIN_RESET); + osDelay(1); - // build the control word - ctrlWord = (uint16_t)DRV8301_buildCtrlWord(DRV8301_CtrlMode_Read,regName,data); + // 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); + HAL_SPI_TransmitReceive(handle->spiHandle, (uint8_t*)(&zerobuff), (uint8_t*)(&recbuff), 1, 1000); + osDelay(1); - // reset the Rx fifo pointer to zero - SPI_resetRxFifo(obj->spiHandle); - SPI_enableRxFifo(obj->spiHandle); + // Actuate chipselect + HAL_GPIO_WritePin(handle->nCSgpioHandle, handle->nCSgpioNumber, GPIO_PIN_SET); + osDelay(1); + assert(recbuff != 0xbeef); - // 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); + return(recbuff & DRV8301_DATA_MASK); } // end of DRV8301_readSpi() function @@ -588,20 +578,18 @@ void DRV8301_setShuntAmpGain(DRV8301_Handle handle,const DRV8301_ShuntAmpGain_e void DRV8301_writeSpi(DRV8301_Handle handle, const DRV8301_RegName_e regName,const uint16_t data) { - DRV8301_Obj *obj = (DRV8301_Obj *)handle; - uint16_t ctrlWord; + // 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, 0); + HAL_SPI_Transmit(handle->spiHandle, (uint8_t*)(&controlword), 1, 1000); + osDelay(1); - // 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); - + // Actuate chipselect + HAL_GPIO_WritePin(handle->nCSgpioHandle, handle->nCSgpioNumber, GPIO_PIN_SET); + osDelay(1); return; } // end of DRV8301_writeSpi() function @@ -696,7 +684,6 @@ void DRV8301_setupSpi(DRV8301_Handle handle, DRV_SPI_8301_Vars_t *Spi_8301_Vars) { DRV8301_RegName_e drvRegName; uint16_t drvDataNew; - uint16_t n; // Update Control Register 1 @@ -723,8 +710,7 @@ void DRV8301_setupSpi(DRV8301_Handle handle, DRV_SPI_8301_Vars_t *Spi_8301_Vars) // Wait for the DRV8301 registers to update - for(n=0;n<100;n++) - asm(" NOP"); + osDelay(1); // Update Status Register 1 diff --git a/Drivers/DRV8301/drv8301.h b/Drivers/DRV8301/drv8301.h index d2edbaba..bdf9fa50 100644 --- a/Drivers/DRV8301/drv8301.h +++ b/Drivers/DRV8301/drv8301.h @@ -42,13 +42,17 @@ // ************************************************************************** // the includes +#include "stdbool.h" +#include "stdint.h" + // drivers -#include "spi.h" -#include "gpio.h" -// modules +#include "stm32f4xx_hal.h" -// solutions +// Port +typedef SPI_HandleTypeDef* SPI_Handle; +typedef GPIO_TypeDef* GPIO_Handle; +typedef uint16_t GPIO_Number_e; //! @@ -418,8 +422,10 @@ typedef struct _DRV_SPI_8301_Vars_t_ typedef struct _DRV8301_Obj_ { SPI_Handle spiHandle; //!< the handle for the serial peripheral interface - GPIO_Handle gpioHandle; //!< the gpio handle that is connected to the drv8301 enable pin - GPIO_Number_e gpioNumber; //!< the gpio number that is connected to the drv8301 enable pin + GPIO_Handle EngpioHandle; //!< the gpio handle that is connected to the drv8301 enable pin + GPIO_Number_e EngpioNumber; //!< the gpio number that is connected to the drv8301 enable pin + GPIO_Handle nCSgpioHandle; //!< the gpio handle that is connected to the drv8301 nCS pin + GPIO_Number_e nCSgpioNumber; //!< the gpio number that is connected to the drv8301 nCS pin bool RxTimeOut; //!< the timeout flag for the RX fifo bool enableTimeOut; //!< the timeout flag for drv8301 enable } DRV8301_Obj; diff --git a/Inc/tim.h b/Inc/tim.h index 4da34fae..ae7ff7e7 100644 --- a/Inc/tim.h +++ b/Inc/tim.h @@ -64,6 +64,8 @@ void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim); /* USER CODE BEGIN Prototypes */ +void OC4_PWM_Override(TIM_HandleTypeDef* htim); + /* USER CODE END Prototypes */ #ifdef __cplusplus diff --git a/Makefile b/Makefile index cd85a217..323d521c 100644 --- a/Makefile +++ b/Makefile @@ -47,7 +47,6 @@ C_SOURCES = \ Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma.c \ Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_usb.c \ Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim_ex.c \ - Drivers/DRV8301/drv8301.c \ Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Src/usbd_cdc.c \ Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_ioreq.c \ Middlewares/ST/STM32_USB_Device_Library/Core/Src/usbd_core.c \ @@ -74,7 +73,8 @@ C_SOURCES = \ Src/test.c \ Src/stm32f4xx_it.c \ Src/adc.c \ - Src/usbd_conf.c + Src/usbd_conf.c \ + Drivers/DRV8301/drv8301.c ASM_SOURCES = \ Drivers/CMSIS/Device/ST/STM32F4xx/Source/Templates/gcc/startup_stm32f405xx.s diff --git a/Src/test.c b/Src/test.c index 56c56094..eacf4dc2 100644 --- a/Src/test.c +++ b/Src/test.c @@ -1,18 +1,18 @@ #include "test.h" -#include "cmsis_os.h" +#include "stm32f4xx_hal.h" #include "stm32f405xx.h" -#include "stm32f4xx.h" -#include "stm32f4xx_hal_adc.h" -#include "stm32f4xx_hal_adc_ex.h" #include "assert.h" +#include "cmsis_os.h" #include "adc.h" #include "tim.h" #include "spi.h" +#include "drv8301.h" #include "math.h" +#include "stdint.h" static void test_read_ADC(void); @@ -41,8 +41,13 @@ void start_DRV8301() { HAL_GPIO_WritePin(M0_nCS_GPIO_Port, M0_nCS_Pin, GPIO_PIN_RESET); osDelay(1); - - + uint16_t zerobuff = 0; + uint16_t controlword = (uint16_t)DRV8301_buildCtrlWord(DRV8301_CtrlMode_Read, DRV8301_RegName_Status_2, 0); + uint16_t recbuff = 0xbeef; + HAL_SPI_Transmit(&hspi3, &controlword, 1, 1000); + HAL_SPI_TransmitReceive(&hspi3, &zerobuff, &recbuff, 1, 1000); + osDelay(1); + HAL_GPIO_WritePin(M0_nCS_GPIO_Port, M0_nCS_Pin, GPIO_PIN_SET); osDelay(1); @@ -60,10 +65,14 @@ void test_adc_trigger() { __HAL_ADC_ENABLE_IT(&hadc2, ADC_IT_JEOC); } +void DRV8301_setup() { + +} + void test_main(void) { - start_DRV8301(); - + //start_DRV8301(); + DRV8301_setup(); } // Simple loop to read ADC1