-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BSP ADC #70
base: main
Are you sure you want to change the base?
BSP ADC #70
Conversation
Can you write the header before workday so that I can review it? |
Oh also it's not STMF24. We use two different processors on the PCBs:
|
can you check to see if the queue is full (for adding) or empty (for popping) before you do any queue accesses. |
put it in |
…emClock_Config and ErrorHandler
Can you merge main into this branch before I review? seems there are some conflicts. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll review the test files next time since compilation is currently failing & there's a good amount of structural changes I had questions about. Let me know if there's any questions about things, and please respond here to the questions/suggestions I've posed so that we keep stuff on the PR. This is a good start, keep up the good work!
* Vcc double | ||
* rxQueue QueueHandle_t pointer to user-provided Queue handle | ||
*/ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
*/ | ||
|
||
|
||
HAL_StatusTypeDef BSP_ADC_Init(ADC_InitTypeDef init, uint8_t bitNum, double Vcc, QueueHandle_t *rxQueue); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we make the function names consistent with #76 (adc_init, adc_oneshotread, etc.). Same for file name (i know this conflicts with what I said earlier but just want to keep things consistent)
@@ -0,0 +1,28 @@ | |||
#include "stm32f4xx_hal.h" | |||
#include "queue.h" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe compilation is getting mad because of this being before FreeRTOS.h:
"error: #error "include FreeRTOS.h" must appear in source files before "include queue.h""
|
||
ADC_HandleTypeDef hadc; | ||
|
||
double ADC_RAW_TO_VOLTAGE; // conversion factor for readings |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This conversion can be made a bit more accurate by utilizing fixed point. Instead of storing a double here to represent the decimal, you can store both the VCC and bitNum and do the multiplication and division in the interrupt itself, to preserve some accuracy especially at higher values.
Here's a few resources to make this change/understand why this change needs to be made:
https://stackoverflow.com/a/67234824
https://www.tonmeister.ca/wordpress/2021/07/19/fixed-point-vs-floating-point/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Additionally, some macro magic can probably be used to make defining the bitNum and VCC for each ADC channel a compile time step rather than a runtime step. To sort of understand how this would work, take a look at Nathaniel's PR (https://github.com/lhr-solar/Embedded-Sharepoint/pull/76/files), specifically at how CAN.c and can1_recv_entries.h are used in conjunction. The macro stuff for this wouldn't need to be as complex, but understanding how that works wil give you a good starting point for understanding how to implement something that would work in this context.
This stuff can be a separate PR if you wish (make an issue ticket for it if this is the case). For right now, I'd say changing this to fixed point could be enough.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Finally, this can be static as well. static just means it's a variable that's file-local, not that it necessarily is a constant (const is the keyword used for constants)
|
||
static QueueHandle_t* adcReadings; | ||
|
||
HAL_StatusTypeDef BSP_ADC_Init(ADC_InitTypeDef init, uint8_t bitNum, double Vcc, QueueHandle_t *rxQueue) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible for the user to initialize multiple ADC channels/peripherals with different conversion factors (vcc, bitnum) for each? Can this change be made?
portENTER_Critical(); | ||
|
||
// push value to q | ||
xQueueSendFromISR(adcReadings, &realVal, &higherPriorityTaskWoken); | ||
|
||
// give semphr | ||
xSemaphoreGive(adc_send_semaphore); | ||
|
||
portEXIT_Critical(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't seem like it's a critical section. The QueueSendFromISR already internally enters & exits a critical section, which means these portENTER_Critical/portEXIT_Critical calls aren't required (xQueueSendFromISR is already threadsafe). Furthermore, as explained elsewhere, I don't think this semaphore around the queue send is required either
/** | ||
* GPIO Inits for ADC Reading [using ADC1] | ||
* PA_3 ADC1_3 | ||
* PC_0 ADC1_10 | ||
* PC_3 ADC1_13 | ||
* PA_7 ADC1_7 | ||
* PA_4 ADC1_4 | ||
* PB_1 ADC1_9 | ||
* PC_2 ADC1_12 | ||
* PA_0 ADC1_0 | ||
* PB_0 ADC1_8 | ||
* PA_5 ADC1_5 | ||
* PA_6 ADC1_6 | ||
* PA_7 ADC1_7 | ||
*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this always true? How would we make these pin definitions hardware agnostic in the future? (hint hint, I suggest some sort of macro thing). This doesn't need to be handled in this PR per-say but I would like you to start thinking about it for the future PRs.
} | ||
|
||
|
||
void HAL_ADC_MspDeInit(ADC_HandleTypeDef *hadc) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could/should we add an adc_deinit?
void Error_Handler(void) | ||
{ | ||
__disable_irq(); | ||
while (1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps this should be kept static and file-local, and a completely different error_handler could be used for ADC (could call it adc_error_handler and keep it file-local to ADC as well). See my comments on the need for the ADC error-handler as well
#include "queue.h" | ||
#include "FreeRTOS.h" | ||
#include "stm32xx_hal.h" | ||
#include "stm32f4xx_hal_init.c" // this can be put in "stm32xx_hal.h" later |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should not be including .c files
Above is my proposed implementation of ADC Recieve in the BSP. Design decisions were made with the STMF24 in mind.
The PR has an empty BSP_ADC.h file for the actual implementation.