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

Changing some stuff to make the ADC driver build #129

Merged
merged 1 commit into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion general/include/ADS131M04.h → general/include/ads131m04.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ typedef struct {
void ads131m04_init(ads131_t *adc, SPI_HandleTypeDef *hspi, GPIO_TypeDef *hgpio, uint8_t cs_pin);

/* Method to read values out of the ADC */
HAL_StatusTypeDef ads131m04_read_adc(ads131_t *adc);
HAL_StatusTypeDef ads131m04_read_adc(ads131_t *adc, uint32_t *adc_values);

#endif /* ADS131M04_H */
14 changes: 6 additions & 8 deletions general/src/ADS131M04.c → general/src/ads131m04.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "ADS131M04.h"
#include "ads131m04.h"
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
Expand Down Expand Up @@ -29,8 +29,6 @@ void ads131m04_init(ads131_t *adc, SPI_HandleTypeDef *hspi, GPIO_TypeDef *hgpio,

// Channel 0 has Gain default set to level 1, which is what we want for this application
// We can try out different configurations later if we need to, but it seems like the defaults work

return adc;
}

/* Method to abstract writing to a register, will use SPI commands under the hood, value is MSB aligned */
Expand All @@ -50,8 +48,8 @@ static void ads131m04_write_reg(ads131_t *adc, uint8_t reg, uint16_t value)

// Send command to write to the specified register immediately followed by sending the value we want to write to that register
HAL_GPIO_WritePin(adc->gpio, adc->cs_pin, GPIO_PIN_RESET);
HAL_SPI_Transmit(adc->spi, spi_word, 1, HAL_MAX_DELAY);
HAL_SPI_Transmit(adc->spi, value, 1, HAL_MAX_DELAY);
HAL_SPI_Transmit(adc->spi, &spi_word, 1, HAL_MAX_DELAY);
HAL_SPI_Transmit(adc->spi, &value, 1, HAL_MAX_DELAY);
HAL_GPIO_WritePin(adc->gpio, adc->cs_pin, GPIO_PIN_SET);
}

Expand All @@ -70,12 +68,12 @@ static uint16_t ads131m04_read_reg(ads131_t *adc, uint8_t reg)
spi_word |= (num_registers - 1); // Number of registers (bits 0-6)

HAL_GPIO_WritePin(adc->gpio, adc->cs_pin, GPIO_PIN_RESET);
HAL_SPI_Transmit(adc->spi, spi_word, 1, HAL_MAX_DELAY);
HAL_SPI_Transmit(adc->spi, &spi_word, 1, HAL_MAX_DELAY);
HAL_GPIO_WritePin(adc->gpio, adc->cs_pin, GPIO_PIN_SET);

uint16_t res = 0;

if (HAL_SPI_Receive(adc->hspi, (uint16_t *)&res, 1, 10) != HAL_OK)
if (HAL_SPI_Receive(adc->spi, (uint16_t *)&res, 1, 10) != HAL_OK)
return 1;

return res;
Expand All @@ -89,7 +87,7 @@ HAL_StatusTypeDef ads131m04_read_adc(ads131_t *adc, uint32_t *adc_values)

// Read SPI data
HAL_GPIO_WritePin(adc->gpio, adc->cs_pin, GPIO_PIN_RESET);
ret = HAL_SPI_Receive(adc->hspi, data, 6 * 3, HAL_MAX_DELAY);
ret = HAL_SPI_Receive(adc->spi, data, 6 * 3, HAL_MAX_DELAY);
HAL_GPIO_WritePin(adc->gpio, adc->cs_pin, GPIO_PIN_SET);

// Process received data into ADC values
Expand Down
Loading