Commit 26336f58 authored by Pedro Henrique Kopper's avatar Pedro Henrique Kopper

Add ADC subsystem

parent 6c9b7025
......@@ -5,6 +5,8 @@
.project/
build/*
.ycm_extra_conf.py
# Prerequisites
*.d
......
def Settings( **kwargs ):
return {
'flags': [ '-x', 'c', '-Wall', '-Wextra', '-Werror', '-I./ChibiOS', '-I./lib', '-I./threads', '-I./board' ],
}
......@@ -5,7 +5,7 @@
# Compiler options here.
ifeq ($(USE_OPT),)
USE_OPT = -O2 -ggdb -fomit-frame-pointer -falign-functions=16
USE_OPT = -Og -ggdb -fomit-frame-pointer -falign-functions=16
endif
# C specific options here (added to USE_OPT).
......@@ -111,9 +111,10 @@ include $(CHIBIOS)/os/common/ports/ARMCMx/compilers/GCC/mk/port_v7m.mk
# Auto-build files in ./source recursively.
include $(CHIBIOS)/tools/mk/autobuild.mk
# Other files (optional).
include $(CHIBIOS)/test/lib/test.mk
include $(CHIBIOS)/test/rt/rt_test.mk
include $(CHIBIOS)/test/oslib/oslib_test.mk
#include $(CHIBIOS)/test/lib/test.mk
#include $(CHIBIOS)/test/rt/rt_test.mk
#include $(CHIBIOS)/test/oslib/oslib_test.mk
include $(CHIBIOS)/os/hal/lib/streams/streams.mk
# Define linker script file here
LDSCRIPT= $(STARTUPLD)/STM32F303xC.ld
......@@ -124,8 +125,10 @@ CSRC = $(ALLCSRC) \
$(TESTSRC) \
$(CHIBIOS)/os/various/syscalls.c \
./lib/ssd1306.c \
./threads/comm.c \
./threads/status.c \
./threads/display.c \
./threads/sensors.c \
./usb/usbcfg.c \
main.c
......
......@@ -361,8 +361,8 @@
PIN_OSPEED_HIGH(GPIOA_SWDIO) | \
PIN_OSPEED_HIGH(GPIOA_SWCLK) | \
PIN_OSPEED_HIGH(GPIOA_PIN15))
#define VAL_GPIOA_PUPDR (PIN_PUPDR_PULLUP(GPIOA_ARD_A0) | \
PIN_PUPDR_PULLUP(GPIOA_ARD_A1) | \
#define VAL_GPIOA_PUPDR (PIN_PUPDR_FLOATING(GPIOA_ARD_A0) | \
PIN_PUPDR_FLOATING(GPIOA_ARD_A1) | \
PIN_PUPDR_FLOATING(GPIOA_ARD_D1) | \
PIN_PUPDR_FLOATING(GPIOA_ARD_D0) | \
PIN_PUPDR_PULLUP(GPIOA_ARD_A2) | \
......
......@@ -44,7 +44,7 @@
* @brief Enables the ADC subsystem.
*/
#if !defined(HAL_USE_ADC) || defined(__DOXYGEN__)
#define HAL_USE_ADC FALSE
#define HAL_USE_ADC TRUE
#endif
/**
......@@ -427,7 +427,7 @@
* buffers.
*/
#if !defined(SERIAL_USB_BUFFERS_SIZE) || defined(__DOXYGEN__)
#define SERIAL_USB_BUFFERS_SIZE 256
#define SERIAL_USB_BUFFERS_SIZE 2048
#endif
/**
......
......@@ -95,9 +95,9 @@
*/
#define STM32_ADC_DUAL_MODE FALSE
#define STM32_ADC_COMPACT_SAMPLES FALSE
#define STM32_ADC_USE_ADC1 FALSE
#define STM32_ADC_USE_ADC1 TRUE
#define STM32_ADC_USE_ADC2 FALSE
#define STM32_ADC_USE_ADC3 FALSE
#define STM32_ADC_USE_ADC3 TRUE
#define STM32_ADC_USE_ADC4 FALSE
#define STM32_ADC_ADC1_DMA_STREAM STM32_DMA_STREAM_ID(1, 1)
#define STM32_ADC_ADC2_DMA_STREAM STM32_DMA_STREAM_ID(2, 1)
......
#include "ch.h"
#include "hal.h"
#include "board.h"
#include "chprintf.h"
#include "./threads/status.h"
#include "./threads/sensors.h"
#include "./threads/display.h"
#include "./threads/comm.h"
#include "./usb/usbcfg.h"
// Callback placeholders
......@@ -25,12 +28,25 @@ int main(void) {
halInit();
chSysInit();
palSetPadMode(GPIOA, 11, PAL_MODE_ALTERNATE(14));
palSetPadMode(GPIOA, 12, PAL_MODE_ALTERNATE(14));
/*
* Configures GPIO
*/
palSetLineMode(LINE_LED0, PAL_MODE_OUTPUT_PUSHPULL); // LED0 output
palSetLineMode(LINE_LED1, PAL_MODE_OUTPUT_PUSHPULL); // LED1 output
palSetLineMode(LINE_LED2, PAL_MODE_OUTPUT_PUSHPULL); // LED2 output
palSetPadMode(GPIOB, 6, PAL_MODE_ALTERNATE(4)); // I2C1 SCL
palSetPadMode(GPIOB, 7, PAL_MODE_ALTERNATE(4)); // I2C1 SDA
palSetPadMode(GPIOB, 10, PAL_MODE_ALTERNATE(7)); // UART3
palSetPadMode(GPIOB, 11, PAL_MODE_ALTERNATE(7)); // UART3
palSetPadMode(GPIOA, 11, PAL_MODE_ALTERNATE(14)); // USB
palSetPadMode(GPIOA, 12, PAL_MODE_ALTERNATE(14)); // USB
palSetPadMode(GPIOA, 1, PAL_MODE_INPUT_ANALOG); // ADC1_IN2
palSetPadMode(GPIOB, 13, PAL_MODE_INPUT_ANALOG); // ADC3_IN5
/*
* Initializes USB serial interfaces
*/
chThdSleepMilliseconds(500);
sduObjectInit(&SDU1);
sduStart(&SDU1, &serusbcfg1);
sduObjectInit(&SDU2);
......@@ -45,23 +61,20 @@ int main(void) {
sdStart(&SD3, NULL);
/*
* Configures GPIO
* Activates the ADC 1 and 3 drivers
*/
palSetLineMode(LINE_LED0, PAL_MODE_OUTPUT_PUSHPULL); // LED0 output
palSetLineMode(LINE_LED1, PAL_MODE_OUTPUT_PUSHPULL); // LED1 output
palSetLineMode(LINE_LED2, PAL_MODE_OUTPUT_PUSHPULL); // LED2 output
palSetPadMode(GPIOB, 6, PAL_MODE_ALTERNATE(4)); // I2C1 SCL
palSetPadMode(GPIOB, 7, PAL_MODE_ALTERNATE(4)); // I2C1 SDA
palSetPadMode(GPIOB, 10, PAL_MODE_ALTERNATE(7)); // UART3
palSetPadMode(GPIOB, 11, PAL_MODE_ALTERNATE(7)); // UART3
adcStart(&ADCD1, NULL);
adcStart(&ADCD3, NULL);
/*
* Starts running the threads
*/
//chThdSleepMilliseconds(500);
chThdCreateStatic(waStatus, sizeof(waStatus), NORMALPRIO, Status, NULL);
chThdCreateStatic(waDisplay, sizeof(waDisplay), NORMALPRIO, Display, NULL);
chThdCreateStatic(waSensors, sizeof(waSensors), NORMALPRIO, Sensors, NULL);
chThdCreateStatic(waComm, sizeof(waComm), NORMALPRIO, Comm, NULL);
/*
* Normal main() thread activity.
* For now it just sleeps in a loop.
......
#include "ch.h"
#include "hal.h"
#include "chprintf.h"
#include "board.h"
#include "../usb/usbcfg.h"
#include <stdio.h>
#include "comm.h"
#include "display.h"
/* Comm THREAD
*
* This thread communication with the host PC
*/
#define COMM &SDU1 // USB Serial 1
THD_WORKING_AREA(waComm, 1024);
THD_FUNCTION(Comm, arg) {
(void)arg;
chRegSetThreadName("comm");
// Simple command-response interface
while (true) {
char buf = chnGetTimeout((BaseChannel *)COMM, TIME_INFINITE);
//chprintf((BaseSequentialStream *)COMM, "You pressed %c!\r\n", buf);
/*char buf2[16] = {0};
sprintf(buf2, "Tecla: %c", buf);
(void)chMsgSend(chRegFindThreadByName("display"), (msg_t)buf2);*/
adcsample_t *reading = (adcsample_t *) chMsgSend(chRegFindThreadByName("sensors"), (msg_t)0);
chprintf((BaseSequentialStream *)COMM, "%d\r\n", reading[0]);
}
}
#ifndef COMM_H
#define COMM_H
extern THD_WORKING_AREA(waComm, 1024);
THD_FUNCTION(Comm, arg);
#endif /* COMM_H */
......@@ -25,24 +25,36 @@ static const SSD1306Config ssd1306cfg = {
static SSD1306Driver SSD1306D1;
THD_WORKING_AREA(waDisplay, 512);
THD_WORKING_AREA(waDisplay, 1024);
THD_FUNCTION(Display, arg) {
(void)arg;
chRegSetThreadName("OledDisplay");
chRegSetThreadName("display");
ssd1306ObjectInit(&SSD1306D1);
ssd1306Start(&SSD1306D1, &ssd1306cfg);
ssd1306ToggleInvert(&SSD1306D1);
ssd1306Start(&SSD1306D1, &ssd1306cfg);
ssd1306FillScreen(&SSD1306D1, 0x00);
ssd1306UpdateScreen(&SSD1306D1);
while (TRUE) {
ssd1306FillScreen(&SSD1306D1, 0x01);
palClearLine(LINE_LED2);
thread_t *tp = chMsgWait();
char *msg = (char *)chMsgGet(tp);
palSetLine(LINE_LED2);
ssd1306FillScreen(&SSD1306D1, 0x00);
ssd1306GotoXy(&SSD1306D1, 0, 1);
ssd1306Puts(&SSD1306D1, "THREAD", &ssd1306_font_11x18, SSD1306_COLOR_WHITE);
ssd1306Puts(&SSD1306D1, msg, &ssd1306_font_11x18, SSD1306_COLOR_WHITE);
char buf[16] = {0};
ssd1306GotoXy(&SSD1306D1, 0, 32);
ssd1306Puts(&SSD1306D1, "ABC", &ssd1306_font_11x18, SSD1306_COLOR_WHITE);
sprintf(buf, "%010lu", TIME_I2MS(chVTGetSystemTime()));
ssd1306Puts(&SSD1306D1, buf, &ssd1306_font_11x18, SSD1306_COLOR_WHITE);
ssd1306UpdateScreen(&SSD1306D1);
chThdSleepMilliseconds(1000);
/* Sending back an acknowledge.*/
chMsgRelease(tp, MSG_OK);
}
ssd1306Stop(&SSD1306D1);
......
#ifndef DISPLAY_H
#define DISPLAY_H
extern THD_WORKING_AREA(waDisplay, 512);
extern THD_WORKING_AREA(waDisplay, 1024);
THD_FUNCTION(Display, arg);
#endif /* DISPLAY_H */
#include "ch.h"
#include "hal.h"
#include "board.h"
#include "sensors.h"
#include <stdio.h>
#include "display.h"
#define ADC_GRP1_NUM_CHANNELS 1
/*
* ADC conversion group.
* Mode: Linear buffer, 8 samples of 2 channels, SW triggered.
* Channels: IN7, IN8.
*/
static const ADCConversionGroup adcgrpcfg1 = {
FALSE,
ADC_GRP1_NUM_CHANNELS,
NULL,
NULL,
0, /* CFGR */
ADC_TR(0, 4095), /* TR1 */
{ /* SMPR[2] */
ADC_SMPR1_SMP_AN2(ADC_SMPR_SMP_601P5),
0
},
{ /* SQR[4] */
ADC_SQR1_SQ1_N(ADC_CHANNEL_IN2),
0,
0,
0
}
};
THD_WORKING_AREA(waSensors, 512);
THD_FUNCTION(Sensors, arg) {
(void)arg;
chRegSetThreadName("sensors");
while (true) {
thread_t *tp = chMsgWait();
adcsample_t sample;
adcConvert(&ADCD1, &adcgrpcfg1, &sample, 1);
float volts = (sample / 4095.0f) * 18.0f;
char buf[16] = {0};
sprintf(buf, "Volts: %d.%d", (int)volts, (int) ((volts - ((int) volts)) * 100));
(void)chMsgSend(chRegFindThreadByName("display"), (msg_t)buf);
chMsgRelease(tp, (msg_t) &sample);
}
}
#ifndef SENSORS_H
#define SENSORS_H
extern THD_WORKING_AREA(waSensors, 512);
THD_FUNCTION(Sensors, arg);
#endif /* SENSORS_H */
\ No newline at end of file
......@@ -22,14 +22,6 @@ THD_FUNCTION(Status, arg) {
chThdSleepMilliseconds(100);
palSetLine(LINE_LED0);
chThdSleepMilliseconds(100);
palClearLine(LINE_LED1);
chThdSleepMilliseconds(100);
palSetLine(LINE_LED1);
chThdSleepMilliseconds(100);
palClearLine(LINE_LED2);
chThdSleepMilliseconds(100);
palSetLine(LINE_LED2);
chThdSleepMilliseconds(100);
sdWrite(DEBUG, (uint8_t *)"Teste\r\n", sizeof("Teste\r\n"));
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment