Skip to content
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

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
Open

BSP ADC #70

wants to merge 13 commits into from

Conversation

aaravm4
Copy link
Contributor

@aaravm4 aaravm4 commented Oct 5, 2024

image

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.

@IshDeshpa IshDeshpa marked this pull request as draft October 5, 2024 23:30
@IshDeshpa IshDeshpa changed the title BSP ADC Stuff --- Aarav BSP ADC Oct 7, 2024
@IshDeshpa IshDeshpa requested a review from connorl309 October 7, 2024 14:07
@IshDeshpa IshDeshpa linked an issue Oct 7, 2024 that may be closed by this pull request
@IshDeshpa
Copy link
Contributor

Can you write the header before workday so that I can review it?

@IshDeshpa
Copy link
Contributor

IshDeshpa commented Oct 7, 2024

Oh also it's not STMF24. We use two different processors on the PCBs:

  • STM32F413RHT
  • STM32L431CBT
    but then there's different ones on our Nucleo development boards as well that you'll probably use for testing. Your code will generically support most STM32s through the interface of the HAL.

@Lakshay983
Copy link
Contributor

can you check to see if the queue is full (for adding) or empty (for popping) before you do any queue accesses.

@IshDeshpa
Copy link
Contributor

put it in Embedded-Sharepoint/bsp/Inc/BSP_ADC.h. The .c file will go in Embedded-Sharepoint/bsp/Src/BSP_ADC.c

@IshDeshpa IshDeshpa marked this pull request as ready for review January 3, 2025 23:12
@IshDeshpa
Copy link
Contributor

Can you merge main into this branch before I review? seems there are some conflicts.
https://www.youtube.com/watch?v=YINamUvOYhA

Copy link
Contributor

@IshDeshpa IshDeshpa left a 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
*/

Copy link
Contributor

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);
Copy link
Contributor

@IshDeshpa IshDeshpa Jan 8, 2025

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"
Copy link
Contributor

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
Copy link
Contributor

@IshDeshpa IshDeshpa Jan 9, 2025

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/

Copy link
Contributor

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.

Copy link
Contributor

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) {
Copy link
Contributor

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?

Comment on lines +69 to +77
portENTER_Critical();

// push value to q
xQueueSendFromISR(adcReadings, &realVal, &higherPriorityTaskWoken);

// give semphr
xSemaphoreGive(adc_send_semaphore);

portEXIT_Critical();
Copy link
Contributor

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

Comment on lines +87 to +101
/**
* 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
*/
Copy link
Contributor

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) {
Copy link
Contributor

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?

Comment on lines +7 to 10
void Error_Handler(void)
{
__disable_irq();
while (1)
Copy link
Contributor

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
Copy link
Contributor

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

@IshDeshpa IshDeshpa requested review from Shaun345 and removed request for connorl309 January 9, 2025 00:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BSP_ADC
6 participants