Skip to content

Adc cleanup #5

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ revex/Debug/*
revex/.settings/*
revex/.mxproject
revex/.cproject
Debug.launch
revex/RevEx Debug.launch

2 changes: 2 additions & 0 deletions revex/Core/Inc/adc.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ extern "C" {
extern ADC_HandleTypeDef hadc;

/* USER CODE BEGIN Private defines */
void print_adc(uint8_t* outBuffer);

uint16_t sample_adc();
/* USER CODE END Private defines */

void MX_ADC_Init(void);
Expand Down
2 changes: 1 addition & 1 deletion revex/Core/Inc/imu.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

void IMU_Init(uint8_t loadBias);

void print_imu_raw(uint8_t* outBuffer);
void sample_imu_raw(uint8_t* outBuffer);

void IMU_read_all_raw();

Expand Down
3 changes: 2 additions & 1 deletion revex/Core/Inc/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ void Error_Handler(void);

/* USER CODE BEGIN EFP */
void sample();
uint16_t print_adc();
void battery();
void setup_gpio(GPIO_TypeDef *GPIOx, int num, enum mode m, int pupd, int speed);
int get_gpio(GPIO_TypeDef *GPIOx, int num);
uint8_t * get_buff();
void my_old_friend();
void darkness();
void toggle_on(GPIO_TypeDef *GPIOx, int num);
Expand Down
2 changes: 1 addition & 1 deletion revex/Core/Inc/stm32l0xx_it.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ void PendSV_Handler(void);
void SysTick_Handler(void);
void DMA1_Channel1_IRQHandler(void);
void DMA1_Channel2_3_IRQHandler(void);
void TIM3_IRQHandler(void);
void TIM6_IRQHandler(void);
void TIM7_IRQHandler(void);
void USART1_IRQHandler(void);
/* USER CODE BEGIN EFP */
void TIM6_DAC_IRQHandler(void);
Expand Down
4 changes: 2 additions & 2 deletions revex/Core/Inc/tim.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ extern "C" {

/* USER CODE END Includes */

extern TIM_HandleTypeDef htim3;
extern TIM_HandleTypeDef htim6;
extern TIM_HandleTypeDef htim7;

/* USER CODE BEGIN Private defines */

/* USER CODE END Private defines */

void MX_TIM3_Init(void);
void MX_TIM6_Init(void);
void MX_TIM7_Init(void);

/* USER CODE BEGIN Prototypes */

Expand Down
39 changes: 35 additions & 4 deletions revex/Core/Src/adc.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
#include "adc.h"

/* USER CODE BEGIN 0 */

#include "dma.h"
#include <stdio.h>
uint32_t AD_RES[2];
/* USER CODE END 0 */

ADC_HandleTypeDef hadc;
Expand Down Expand Up @@ -71,6 +73,13 @@ void MX_ADC_Init(void)
{
Error_Handler();
}
/** Configure for the selected ADC regular channel to be converted.
*/
sConfig.Channel = ADC_CHANNEL_8;
if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN ADC_Init 2 */

/* USER CODE END ADC_Init 2 */
Expand All @@ -90,23 +99,30 @@ void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle)
__HAL_RCC_ADC1_CLK_ENABLE();

__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/**ADC GPIO Configuration
PA7 ------> ADC_IN7
PB0 ------> ADC_IN8
*/
GPIO_InitStruct.Pin = GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

/* ADC1 DMA Init */
/* ADC Init */
hdma_adc.Instance = DMA1_Channel1;
hdma_adc.Init.Request = DMA_REQUEST_0;
hdma_adc.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_adc.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_adc.Init.MemInc = DMA_MINC_DISABLE;
hdma_adc.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
hdma_adc.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
hdma_adc.Init.MemInc = DMA_MINC_ENABLE;
hdma_adc.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
hdma_adc.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
hdma_adc.Init.Mode = DMA_CIRCULAR;
hdma_adc.Init.Priority = DMA_PRIORITY_LOW;
if (HAL_DMA_Init(&hdma_adc) != HAL_OK)
Expand Down Expand Up @@ -135,9 +151,12 @@ void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle)

/**ADC GPIO Configuration
PA7 ------> ADC_IN7
PB0 ------> ADC_IN8
*/
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_7);

HAL_GPIO_DeInit(GPIOB, GPIO_PIN_0);

/* ADC1 DMA DeInit */
HAL_DMA_DeInit(adcHandle->DMA_Handle);
/* USER CODE BEGIN ADC1_MspDeInit 1 */
Expand All @@ -147,5 +166,17 @@ void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle)
}

/* USER CODE BEGIN 1 */
void print_adc(uint8_t* outBuffer)
{
uint16_t value1 = AD_RES[0];
uint16_t value2 = AD_RES[1];
sprintf((char*)&(outBuffer[0]), "%04x", value2);
sprintf((char*)&(outBuffer[4]), "%04x", value1);
}

uint16_t sample_adc()
{
HAL_ADC_Start_DMA(&hadc, AD_RES, 2);
return AD_RES[0];
}
/* USER CODE END 1 */
34 changes: 24 additions & 10 deletions revex/Core/Src/ble.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ bleData BLE_data;
uint8_t usart_flag = 0;
uint8_t waiting = 0;
uint8_t commandBuffer[30] = {0};
uint8_t txBuffer[64];
uint8_t txBuffer1[64];
uint8_t txBuffer2[64];
uint8_t command_ind = 0;
uint8_t command_done = 0;

Expand All @@ -30,7 +31,8 @@ char Service[37] = "PS,123456789012345678901234567890FF\r\n";
char Characteristic1[43] = "PC,12345678901234567890123456789011,12,20\r\n";
char Characteristic2[43] = "PC,12345678901234567890123456789022,14,02\r\n";
char Characteristic3[43] = "PC,12345678901234567890123456789033,12,04\r\n";
char cmdData[9] = "SHW,0018,";
char cmdData1[9] = "SHW,0018,";
char cmdData3[9] = "SHW,001E,";
char ret[2] = "\n\r";


Expand Down Expand Up @@ -101,19 +103,20 @@ bleState BLE_Init_IT()
BLE_Info.awaitingState = BLE_FREE;
BLE_Info.init = 0;

memcpy(txBuffer1, cmdData1, 9);
memcpy(txBuffer2, cmdData3, 9);

BLE_data.dataRdy = 0;
BLE_data.length = 0;

memcpy(txBuffer, cmdData, 9);

if (!retry) {
setup_gpio(GPIOA, 6, output, 0, 0);
setup_gpio(GPIOA, 8, output, 0, 0);
}

toggle_off(GPIOA, 6);
toggle_off(GPIOA, 8);
HAL_Delay(4000);
HAL_Delay(200);
BLE_Info.currentState = BLE_FREE;
toggle_on(GPIOA, 6);
BLE_awaitState(BLE_CMD);
Expand Down Expand Up @@ -186,7 +189,7 @@ bleState BLE_Init_IT()

BLE_Info.init = 1;

return;
return BLE_Info.currentState;
}

void BLE_OTA()
Expand Down Expand Up @@ -218,12 +221,23 @@ void BLE_lowPower(void)

void BLE_transmit(uint8_t* data, uint16_t length)
{
memcpy(&(txBuffer[9]), data, length);
if(length == 40)
{
memcpy(&(txBuffer1[9]), data + 4, length);

memcpy(&(txBuffer[49]), ret, 2);
memcpy(&(txBuffer1[49]), ret, 2);
HAL_UART_Transmit(&huart1, txBuffer1, length + 11, 10);
//HAL_UART_Transmit_DMA(&huart1, data, length);
}
else
{
memcpy(&(txBuffer2[9]), data, length);

HAL_UART_Transmit(&huart1, txBuffer, length + 11, 10);
// HAL_UART_Transmit_DMA(&huart1, data, length);
memcpy(&(txBuffer2[13]), ret, 2);

HAL_UART_Transmit(&huart1, txBuffer2, length + 11, 10);
HAL_UART_Transmit_DMA(&huart1, data, length);
}
}

bleState BLE_awaitState(bleState state) {
Expand Down
8 changes: 4 additions & 4 deletions revex/Core/Src/imu.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ void print_imu_raw()
HAL_UART_Transmit(&huart1, (uint8_t*)buffer3, n, 100);
}*/

void print_imu_raw(uint8_t* outBuffer)
void sample_imu_raw(uint8_t* outBuffer)
{
sprintf((char*)&(outBuffer[4]), "%02x%02x%02x%02x%02x%02x", ((gyro.x & 0xff00)>>8), (gyro.x & 0xff), ((gyro.y & 0xff00)>>8), (gyro.y & 0xff), ((gyro.z & 0xff00)>>8), (gyro.z & 0xff));
sprintf((char*)&(outBuffer[16]), "%02x%02x%02x%02x%02x%02x", ((accel.x & 0xff00)>>8), (accel.x & 0xff), ((accel.y & 0xff00)>>8), (accel.y & 0xff), ((accel.z & 0xff00)>>8), (accel.z & 0xff));
sprintf((char*)&(outBuffer[28]), "%02x%02x%02x%02x%02x%02x", ((mag.x & 0xff00)>>8), (mag.x & 0xff), ((mag.y & 0xff00)>>8), (mag.y & 0xff), ((mag.z & 0xff00)>>8), (mag.z & 0xff));
sprintf((char*)&(outBuffer[8]), "%02x%02x%02x%02x%02x%02x", ((gyro.x & 0xff00)>>8), (gyro.x & 0xff), ((gyro.y & 0xff00)>>8), (gyro.y & 0xff), ((gyro.z & 0xff00)>>8), (gyro.z & 0xff));
sprintf((char*)&(outBuffer[20]), "%02x%02x%02x%02x%02x%02x", ((accel.x & 0xff00)>>8), (accel.x & 0xff), ((accel.y & 0xff00)>>8), (accel.y & 0xff), ((accel.z & 0xff00)>>8), (accel.z & 0xff));
sprintf((char*)&(outBuffer[32]), "%02x%02x%02x%02x%02x%02x", ((mag.x & 0xff00)>>8), (mag.x & 0xff), ((mag.y & 0xff00)>>8), (mag.y & 0xff), ((mag.z & 0xff00)>>8), (mag.z & 0xff));
//int l = sprintf(buffer1, "%04x%04x%04x", gyro.x, gyro.y, gyro.z);
//int m = sprintf(buffer2, "%04x%04x%04x", accel.x, accel.y, accel.z);
//int n = sprintf(buffer3, "%04x%04x%04x\r\n", mag.x, mag.y, mag.z);
Expand Down
25 changes: 12 additions & 13 deletions revex/Core/Src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,16 @@

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
uint32_t AD_RES = 0;
uint32_t Period = 0;
uint32_t Duty_cycle = 1000;
uint32_t val = 0;
uint32_t sleep_cnt = 0;
uint16_t old = 0;
int gate = 0;
char temp [50] = "hello\r\n";
char sleep [16] = "Going to Sleep\r\n";
char d1 [6] = "dip1\r\n";
char d2 [6] = "dip2\r\n";
uint8_t outputData[128];
uint8_t readyToSend = 0;
uint8_t readyToSend = 1;
uint8_t stallSleep = 0;
/* USER CODE END PTD */

Expand Down Expand Up @@ -305,7 +302,7 @@ void ADC_config()
setup_gpio(GPIOA, 7, analog, 0, 1);
DMA1_CSELR->CSELR &= (uint32_t)(~DMA_CSELR_C1S);
DMA1_Channel1->CPAR = (uint32_t) (&(ADC1->DR));
DMA1_Channel1->CMAR = (uint32_t) (&AD_RES);
//DMA1_Channel1->CMAR = (uint32_t) (&AD_RES);
DMA1_Channel1->CNDTR = 1;
DMA1_Channel1->CCR |= DMA_CCR_MSIZE_0 | DMA_CCR_PSIZE_0 | DMA_CCR_TEIE | DMA_CCR_TCIE | DMA_CCR_CIRC;
DMA1_Channel1->CCR |= DMA_CCR_EN;
Expand Down Expand Up @@ -333,18 +330,19 @@ void ADC_config()
}
}

uint16_t print_adc(void)
uint8_t * get_buff(void)
{
uint16_t value = AD_RES;// & 0xffe;
sprintf((char*)outputData, "%04x", value);
return outputData;
}

return value;
void battery()
{
BLE_transmit(outputData, 4);
}

void sample()
{
HAL_ADC_Start_DMA(&hadc, &AD_RES, 1);
uint16_t value = print_adc();
uint16_t value = sample_adc();
if(value > old + 6 || value < old - 6)
{
old = value;
Expand All @@ -357,7 +355,7 @@ void sample()

if (!readyToSend) { return; } // Don't send anything yet

print_imu_raw(outputData);
sample_imu_raw(outputData);

BLE_transmit(outputData, 40);
/*ADC1->CR |= ADC_CR_ADSTP;
Expand Down Expand Up @@ -496,13 +494,13 @@ int main(void)

/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_TIM3_Init();
MX_I2C1_Init();
MX_SPI1_Init();
MX_USART1_UART_Init();
MX_DMA_Init();
MX_TIM6_Init();
MX_ADC_Init();
MX_TIM7_Init();
/* USER CODE BEGIN 2 */
dips = go_goDipSwitch();
HAL_PWR_EnableWakeUpPin(PWR_WAKEUP_PIN1);
Expand All @@ -526,6 +524,7 @@ int main(void)
//setup_tim6();

HAL_TIM_Base_Start_IT(&htim6);
//HAL_TIM_Base_Start_IT(&htim7);
/* USER CODE END 2 */

/* Infinite loop */
Expand Down
Loading