Skip to content

Commit 3143541

Browse files
committed
PmodALS driver changes
Added checking inputs of its functions. Variable used to read from SPI changed from 1 byte to 4. SPI is in 2 bytes modes and produced stack overwrite.
1 parent b3d4555 commit 3143541

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

code/CM7/Drivers/PmodALS/Src/pmodals.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
pmodals_device_t pmodals_create_handle( const void* const phy_handle , float Vcc , float Rload )
88
{
9+
if( phy_handle == NULL )
10+
return (pmodals_device_t ){0};
11+
912
return (pmodals_device_t ){
1013
.phy_handle = phy_handle ,
1114
.Vcc = Vcc ,
@@ -18,18 +21,21 @@ pmodals_device_t pmodals_create_handle( const void* const phy_handle , float Vcc
1821

1922
HAL_StatusTypeDef pmodals_get_lux( const pmodals_device_t* const device , float* const lux )
2023
{
21-
uint8_t data;
24+
if( device == NULL || lux == NULL )
25+
return HAL_ERROR;
26+
27+
uint32_t data = 0;
2228
#if PMODALS_OS == NO_OS
2329
HAL_StatusTypeDef rv = HAL_SPI_Receive( (SPI_HandleTypeDef*) device->phy_handle , (uint8_t *)&data , 1 , HAL_MAX_DELAY );
2430
#else
25-
HAL_StatusTypeDef rv = HAL_SPI_Receive_IT( (SPI_HandleTypeDef*) device->phy_handle , (uint8_t *)&data , 1 );
31+
HAL_StatusTypeDef rv = HAL_SPI_Receive_IT( (SPI_HandleTypeDef*) device->phy_handle , (uint8_t*)&data , 1 );
2632
#endif
2733

2834
if( rv != HAL_OK )
2935
return rv;
3036

3137
#if PMODALS_OS == FREE_RTOS
32-
osThreadFlagsWait( SPI_MEM_IT_FLAG , osFlagsWaitAny , osWaitForever );
38+
osThreadFlagsWait( SPI_MEM_IT_FLAG | SPI_ERR_IT_FLAG , osFlagsWaitAny , osWaitForever );
3339
#endif
3440

3541
*lux = ( (float)data * device->Vcc / device->adc_max_value / device->Rload ) * device->TEMT6000_transfer_function;

0 commit comments

Comments
 (0)