Bootcamp: Daniel Kolev#308
Conversation
EemanAleem
left a comment
There was a problem hiding this comment.
Heyu! Did great on your bootcamp, with only very minor nits to tweak a little and then you're good to go!
| { | ||
|
|
||
| HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_RESET); //Set low | ||
| uint8_t tx_data[3] = {0x01, (0x80 | (ADC_CHANNEL << 4)), 0x00}; |
There was a problem hiding this comment.
No magic numbers, such as for 0x01 and 0x08! For code to be understandable as developers iterate upon it in the future, you need to ensure values like this that seem obvious now, but not-so-obvious in a few months or even years should be prescribed a variable first! Otherwise correct values :)
|
|
||
| HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_RESET); //Set low | ||
| uint8_t tx_data[3] = {0x01, (0x80 | (ADC_CHANNEL << 4)), 0x00}; | ||
| uint8_t rx_data[3] = {0, 0, 0}; // buffer |
There was a problem hiding this comment.
Additionally, since tx_data and rx_data do not change in the duration of the while loop, they should be initialized outside of the while loop to prevent constant reinitialization every time the while loop runs.
| HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_RESET); //Set low | ||
| uint8_t tx_data[3] = {0x01, (0x80 | (ADC_CHANNEL << 4)), 0x00}; | ||
| uint8_t rx_data[3] = {0, 0, 0}; // buffer | ||
| HAL_SPI_TransmitReceive(&hspi1, tx_data, rx_data, 3, HAL_MAX_DELAY); |
There was a problem hiding this comment.
See first comment about magic numbers for 3.
|
|
||
| uint16_t adc_value = ((rx_data[1] & 0x03) << 8) | rx_data[2]; //parse 10-bit | ||
|
|
||
| uint32_t pulse = ON_TIME_MIN + (adc_value * (ON_TIME_MAX - ON_TIME_MIN)) / 1023; //map to 50-100 counts = 1-2ms |
There was a problem hiding this comment.
This calculation is correct! One minor nit is to make sure that even though most compilers follow BIDMAS (or the equivalent acronym for determing the order of operations for mathematical calculations), make sure your brackets make it unquestionable so it prevents errors as you write the code yourself.
Bootcamp Pull Request for EFS: Daniel Kolev