application: add BusVoodoo boards tests

This commit is contained in:
King Kévin 2017-12-12 13:56:28 +01:00
parent 57556ba19c
commit 89b79db511
1 changed files with 667 additions and 73 deletions

View File

@ -12,7 +12,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/** STM32F1 application example
/** STM32F1 BusVoodoo application
* @file application.c
* @author King Kévin <kingkevin@cuvoodoo.info>
* @date 2016-2017
@ -22,7 +22,7 @@
#include <stdint.h> // standard integer types
#include <stdlib.h> // standard utilities
#include <string.h> // string utilities
#include <time.h> // date/time utilities
#include <math.h> // math utilities
/* STM32 (including CM3) libraries */
#include <libopencmsis/core_cm3.h> // Cortex M3 utilities
@ -31,10 +31,12 @@
#include <libopencm3/stm32/gpio.h> // general purpose input output library
#include <libopencm3/stm32/rcc.h> // real-time control clock library
#include <libopencm3/stm32/exti.h> // external interrupt utilities
#include <libopencm3/stm32/rtc.h> // real time clock utilities
#include <libopencm3/stm32/iwdg.h> // independent watchdog utilities
#include <libopencm3/stm32/dbgmcu.h> // debug utilities
#include <libopencm3/stm32/flash.h> // flash utilities
#include <libopencm3/stm32/desig.h> // design utilities
#include <libopencm3/stm32/adc.h> // ADC utilities
#include <libopencm3/stm32/dac.h> // DAC utilities
/* own libraries */
#include "global.h" // board definitions
@ -42,16 +44,60 @@
#include "usart.h" // USART utilities
#include "usb_cdcacm.h" // USB CDC ACM utilities
//#include "rs.h" // RS-232/485 utilities
#include "i2c_master.h"
#define WATCHDOG_PERIOD 10000 /**< watchdog period in ms */
/** @defgroup main_flags flag set in interrupts to be processed in main task
/** @defgroup busvoodoo_peripherals peripheral pin definitions
* @{
*/
volatile bool rtc_internal_tick_flag = false; /**< flag set when internal RTC ticked */
#define BUSVOODOO_5VPULLUP_PORT A /**< 5V pull-up enable pin (active low) */
#define BUSVOODOO_5VPULLUP_PIN 15 /**< 5V pull-up enable pin (active low) */
#define BUSVOODOO_XVPULLUP_PORT B /**< xV pull-up enable pin (active low) */
#define BUSVOODOO_XVPULLUP_PIN 3 /**< xV pull-up enable pin (active low) */
#define BUSVOODOO_XVEN_PORT A /**< xV enable pin (active high) */
#define BUSVOODOO_XVEN_PIN 6 /**< xV enable pin (active high) */
#define BUSVOODOO_12VEN_PORT A /**< 12V enable pin (active high) */
#define BUSVOODOO_12VEN_PIN 7 /**< 12V enable pin (active high) */
#define BUSVOODOO_RS232EN_PORT C /**< RS-232 enable pin (active low) */
#define BUSVOODOO_RS232EN_PIN 13 /**< RS-232 enable pin (active low) */
#define BUSVOODOO_VOUTEN_PORT B /**< voltage output (5V and 3.3V) enable pin (active low) */
#define BUSVOODOO_VOUTEN_PIN 4 /**< voltage output (5V and 3.3V) enable pin (active low) */
/** @} */
time_t time_rtc = 0; /**< time (seconds since Unix Epoch) */
struct tm* time_tm; /**< time in tm format (time zones are not handled for non-POSIX environments) */
/** @defgroup busvoodoo_adc inputs to measure voltages
* @{
*/
#define BUSVOODOO_3V3_CHANNEL 12 /**< ADC channel to measure 5V rail */
#define BUSVOODOO_5V_CHANNEL 13 /**< ADC channel to measure 3.3V rail */
#define BUSVOODOO_XV_CHANNEL 11 /**< ADC channel to measure xV rail */
#define BUSVOODOO_12V_CHANNEL 10 /**< ADC channel to measure 12V rail */
/** @} */
#define BUSVOOFOO_XVCTL_CHANNEL CHANNEL_1 /**< DAC channel to control xV output voltage */
#define BUSVOODOO_XV_DEFAULT (0.8*(1+33.0/10.0)) /**< default (when not driven) xV voltage regulator output voltage based on R1 and R2 */
#define BUSVOODOO_XV_TEST 2.5 /**< target xV output voltage to test if we can set control the xV voltage regulator */
#define BUSVOODOO_XV_SET(x) ((0.8*(1+33.0/10.0)-x)*(10.0/33.0)+0.8) /**< voltage to output for the DAC to set the desired xV output voltage (based on resistor values on the xV adjust pins and xV voltage reference) */
#define BUSVOOFOO_12VCTL_CHANNEL CHANNEL_2 /**< DAC channel to control 12V output voltage */
#define BUSVOODOO_12V_DEFAULT (1.25*(1+100.0/10.0)) /**< default (when not driven) 12V voltage regulator output voltage based on R1 and R2 */
#define BUSVOODOO_12V_TEST 12.0 /**< target 12V output voltage to test if we can set control the 12V voltage regulator */
#define BUSVOODOO_12V_SET(x) ((1.25*(1+100.0/10.0)-x)*(10.0/100.0)+1.25) /**< voltage to output for the DAC to set the desired
12V output voltage (based on resistor values on the 12V adjust pins and 12V voltage reference) */
/** @defgroup busvoodoo_io I/O pin definitions
* @{
*/
static const char* busvoodoo_io_names[13] = {"I2C_SMBA/SPI_NSS/I2S_WS", "SDIO_CMD", "USART_CTS/SPI_SCK/I2S_CK", "SDIO_D3/UART_RX", "I2C_SDA/USART_RX", "SDIO_D0", "SPI_MOSI/I2S_SD", "SDIO_CK/USART_CK", "I2C_SCL/USART_TX", "SDIO_D1", "I2S_MCK", "USART_RTS/SPI_MISO", "SDIO_D2/UART_TX"}; /**< I/O individual signal names */
static const uint32_t busvoodoo_io_ports[13] = {GPIOB, GPIOD, GPIOB, GPIOC, GPIOB, GPIOC, GPIOB, GPIOC, GPIOB, GPIOC, GPIOC, GPIOB, GPIOC}; /**< port of individual signals */
static const uint32_t busvoodoo_io_pins[13] = {GPIO12, GPIO2, GPIO13, GPIO11, GPIO11, GPIO8, GPIO15, GPIO12, GPIO10, GPIO9, GPIO6, GPIO14, GPIO10}; /**< pin of individual signals */
// ideal pinout
//static const uint8_t busvoodoo_io_groups[13] = {1, 1, 3, 3, 5, 5, 2, 2, 4, 4, 6, 6, 6}; /**< which I/O (group) does the signal belong to */
// schematic/layout v0.001 pinout (SDIO_D2/UART_TX landed in group 6 instead of 3)
static const uint8_t busvoodoo_io_groups[13] = {1, 1, 3, 3, 5, 5, 2, 2, 4, 4, 6, 6, 3}; /**< which I/O (group) does the signal belong to */
/** @} */
size_t putc(char c)
{
@ -79,6 +125,535 @@ size_t putc(char c)
return length; // return number of characters printed
}
static bool wait_space(void)
{
// disable watchdog when waiting for user input
printf("press space to continue, or any other key to abort\n");
while (!usart_received && !usb_cdcacm_received) { // wait for user input
__WFI(); // go to sleep
}
char c = 0;
if (usart_received) {
c = usart_getchar(); // read user input from UART
} else if (usb_cdcacm_received) {
c = usb_cdcacm_getchar(); // read user input from USB
} else {
return false; // this should not happen
}
if (' '==c) { // space entered
return true;
} else { // something else entered
return false;
}
}
/** set safe state by disabling all outputs */
static void safe_state(void)
{
// disable voltage outputs
gpio_set(GPIO(BUSVOODOO_VOUTEN_PORT), GPIO(BUSVOODOO_VOUTEN_PIN)); // disable 5V and 3.3V output on connector
gpio_set_mode(GPIO(BUSVOODOO_VOUTEN_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO(BUSVOODOO_VOUTEN_PIN)); // set pin as output (open-drain pulled high to disable the pMOS)
gpio_clear(GPIO(BUSVOODOO_XVEN_PORT), GPIO(BUSVOODOO_XVEN_PIN)); // disable xV voltage regulator
gpio_set_mode(GPIO(BUSVOODOO_XVEN_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(BUSVOODOO_XVEN_PIN)); // set pin as output (push-pull, pulled low for safety)
gpio_clear(GPIO(BUSVOODOO_12VEN_PORT), GPIO(BUSVOODOO_12VEN_PIN)); // disable 12V voltage regulator
gpio_set_mode(GPIO(BUSVOODOO_12VEN_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(BUSVOODOO_12VEN_PIN)); // set pin as output (push-pull, pulled low for safety)
// disable embedded pull-ups
gpio_primary_remap(AFIO_MAPR_SWJ_CFG_JTAG_OFF_SW_ON, 0); // disable JTAG (but keep SWD) so to use the underlying GPIOs (PA15, PB3, PB4)
gpio_set(GPIO(BUSVOODOO_5VPULLUP_PORT), GPIO(BUSVOODOO_5VPULLUP_PIN)); // set pin high to disable 5V embedded pull-up
gpio_set_mode(GPIO(BUSVOODOO_5VPULLUP_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO(BUSVOODOO_5VPULLUP_PIN)); // set pin as output (open-drain pulled high to disable the pMOS)
gpio_set(GPIO(BUSVOODOO_XVPULLUP_PORT), GPIO(BUSVOODOO_XVPULLUP_PIN)); // set pin high to disable xV embedded pull-up
gpio_set_mode(GPIO(BUSVOODOO_XVPULLUP_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO(BUSVOODOO_XVPULLUP_PIN)); // set pin as output (open-drain pulled high to disable the pMOS)
// disable all signal I/O outputs
for (uint8_t pin=0; pin<LENGTH(busvoodoo_io_ports) && pin<LENGTH(busvoodoo_io_pins); pin++) {
gpio_set_mode(busvoodoo_io_ports[pin], GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, busvoodoo_io_pins[pin]); // set pin back to input (floating)
}
}
/** read power rail voltage
* @param[in] channel which ADC channel to read voltage from
* @return voltage of NaN if channel is invalid
*/
static float rail_voltage(uint8_t channel)
{
if (channel!=BUSVOODOO_5V_CHANNEL && channel!=BUSVOODOO_3V3_CHANNEL && channel!=BUSVOODOO_XV_CHANNEL && channel!=BUSVOODOO_12V_CHANNEL) { // check channel
return NAN;
}
uint16_t channels[5] = {0}; // to start converted values: internal reference 1.2V, 5V rail, 3.3V rail, xV rail, 12V rail
adc_start_conversion_regular(ADC1); // start conversion to get first voltage
for (uint8_t channel_i=0; channel_i<LENGTH(channels); channel_i++) { // get all conversions
while (!adc_eoc(ADC1)); // wait until conversion finished
channels[channel_i] = adc_read_regular(ADC1); // read voltage value (clears flag)
}
float to_return = NAN; // voltage to return
switch (channel) { // get converter value and calculate according to the voltage divider on this channel
case BUSVOODOO_5V_CHANNEL:
to_return = channels[1]/(10.0/(10.0+10.0));
break;
case BUSVOODOO_3V3_CHANNEL:
to_return = channels[2]/(10.0/(10.0+10.0));
break;
case BUSVOODOO_XV_CHANNEL:
to_return = channels[3]/(10.0/(10.0+10.0));
break;
case BUSVOODOO_12V_CHANNEL:
to_return = channels[4]/(1.5/(10.0+1.5));
break;
default: // unknown channel
to_return = NAN;
break;
}
if (!isnan(to_return)) {
to_return *= 1.2/channels[0]; // calculate voltage from converted values using internal 1.2V voltage reference
}
return to_return;
}
/** perform self tests
* @return if self tests passed
*/
static bool test_self(void)
{
bool to_return = false; // success of the self-test
safe_state(); // start from a safe state
// get device information
// get device identifier (DEV_ID)
// 0x412: low-density, 16-32 kB flash
// 0x410: medium-density, 64-128 kB flash
// 0x414: high-density, 256-512 kB flash
// 0x430: XL-density, 768-1024 kB flash
// 0x418: connectivity
if (0x414!=(DBGMCU_IDCODE&DBGMCU_IDCODE_DEV_ID_MASK)) {
printf("this is not a high-density device: a wrong micro-controller might have been used\n");
#if DEBUG
#else
goto error;
#endif
}
// ensure flash size is ok
if (0xffff==DESIG_FLASH_SIZE) {
printf("unknown flash size: this is probably a defective micro-controller\n");
#if DEBUG
#else
goto error;
#endif
}
// check 5V power rail
float voltage = rail_voltage(BUSVOODOO_5V_CHANNEL); // get 5V power rail voltage
if (voltage<4.0) {
printf("5V power rail voltage is too low: %d.%02uV, check USB port\n", (int32_t)voltage, (uint32_t)((voltage-(int32_t)voltage)*100));
#if DEBUG
while (true);
#else
goto error;
#endif
} else if (voltage>5.5) {
printf("5V power rail voltage is too high: %d.%02uV, check USB port\n", (int32_t)voltage, (uint32_t)((voltage-(int32_t)voltage)*100));
#if DEBUG
while (true);
#else
goto error;
#endif
}
// check 3.3V power rail
voltage = rail_voltage(BUSVOODOO_3V3_CHANNEL); // get 3.3V power rail voltage
if (voltage<3.0) {
printf("3.3V power rail voltage is too low: %d.%02uV, check OLED connector and voltage regulator\n", (int32_t)voltage, (uint32_t)((voltage-(int32_t)voltage)*100));
#if DEBUG
while (true);
#else
goto error;
#endif
} else if (voltage>3.6) {
printf("3.3V power rail voltage is too high: %d.%02uV, check OLED connector and voltage regulator\n", (int32_t)voltage, (uint32_t)((voltage-(int32_t)voltage)*100));
#if DEBUG
while (true);
#else
goto error;
#endif
}
// check xV voltage regulator
gpio_set(GPIO(BUSVOODOO_XVEN_PORT), GPIO(BUSVOODOO_XVEN_PIN)); // enable xV voltage regulator
sleep_ms(1); // let the voltage regulator start and voltage settle
voltage = rail_voltage(BUSVOODOO_XV_CHANNEL); // get xV voltage
// without being driven it should be around the default voltage
if (voltage<BUSVOODOO_XV_DEFAULT-0.2) {
printf("xV voltage is lower than expected: %d.%02uV, check xV voltage regulator\n", (int32_t)voltage, (uint32_t)((voltage-(int32_t)voltage)*100));
#if DEBUG
while (true);
#else
goto error;
#endif
} else if (voltage>BUSVOODOO_XV_DEFAULT+0.2) {
printf("xV voltage is too high: %d.%02uV, check xV voltage regulator\n", (int32_t)voltage, (uint32_t)((voltage-(int32_t)voltage)*100));
#if DEBUG
while (true);
#else
goto error;
#endif
}
// check if we can control xV
voltage = rail_voltage(BUSVOODOO_3V3_CHANNEL); // get reference voltage
if (isnan(voltage)) {
printf("can get 3V3 rail voltage");
goto error;
}
uint16_t dac_set = BUSVOODOO_XV_SET(BUSVOODOO_XV_TEST)/voltage*4095; // DAC value corresponding to the voltage
dac_load_data_buffer_single(dac_set, RIGHT12, BUSVOOFOO_XVCTL_CHANNEL); // set output so the voltage regulator is set to 2.5V
dac_software_trigger(BUSVOOFOO_XVCTL_CHANNEL); // transfer the value to the DAC
dac_enable(BUSVOOFOO_XVCTL_CHANNEL); // enable DAC
sleep_ms(5); // let voltage settle
voltage = rail_voltage(BUSVOODOO_XV_CHANNEL); // get xV voltage
// check if it matched desired voltage
if (voltage<-BUSVOODOO_XV_TEST-0.2) {
printf("xV voltage is too low: %d.%02uV, check xV voltage regulator\n", (int32_t)voltage, (uint32_t)((voltage-(int32_t)voltage)*100));
#if DEBUG
while (true);
#else
goto error;
#endif
} else if (voltage>BUSVOODOO_XV_TEST+0.2) {
printf("xV voltage is too high: %d.%02uV, check xV voltage regulator\n", (int32_t)voltage, (uint32_t)((voltage-(int32_t)voltage)*100));
#if DEBUG
while (true);
#else
goto error;
#endif
}
dac_disable(BUSVOOFOO_XVCTL_CHANNEL); // disable xV control
gpio_clear(GPIO(BUSVOODOO_XVEN_PORT), GPIO(BUSVOODOO_XVEN_PIN)); // disable xV voltage regulator
sleep_ms(1); // let voltage settle
// check 12V voltage regulator
gpio_set(GPIO(BUSVOODOO_12VEN_PORT), GPIO(BUSVOODOO_12VEN_PIN)); // enable 12V voltage regulator
sleep_ms(1); // let the voltage regulator start and voltage settle
voltage = rail_voltage(BUSVOODOO_12V_CHANNEL); // get 12V voltage
// without being driven it should be around the default voltage
if (voltage<BUSVOODOO_12V_DEFAULT-0.3) {
printf("12V voltage is lower than expected: %d.%02uV, check 12V voltage regulator\n", (int32_t)voltage, (uint32_t)((voltage-(int32_t)voltage)*100));
#if DEBUG
while (true);
#else
goto error;
#endif
} else if (voltage>BUSVOODOO_12V_DEFAULT+0.3) {
printf("12V voltage is too high: %d.%02uV, check 12V voltage regulator\n", (int32_t)voltage, (uint32_t)((voltage-(int32_t)voltage)*100));
#if DEBUG
while (true);
#else
goto error;
#endif
}
// check if we can control 12V voltage regulator
voltage = rail_voltage(BUSVOODOO_3V3_CHANNEL); // get reference voltage
if (isnan(voltage)) {
printf("can get 3V3 rail voltage");
goto error;
}
dac_set = BUSVOODOO_12V_SET(BUSVOODOO_12V_TEST)/voltage*4095; // DAC value corresponding to the voltage
dac_load_data_buffer_single(dac_set, RIGHT12, BUSVOOFOO_12VCTL_CHANNEL); // set output so the voltage regulator is set to desired output voltage
dac_software_trigger(BUSVOOFOO_12VCTL_CHANNEL); // transfer the value to the DAC
dac_enable(BUSVOOFOO_12VCTL_CHANNEL); // enable DAC
sleep_ms(5); // let voltage settle
voltage = rail_voltage(BUSVOODOO_12V_CHANNEL); // get 12V voltage
if (voltage<-BUSVOODOO_12V_TEST-0.3) {
printf("12V voltage is too low: %d.%02uV, check 12V voltage regulator\n", (int32_t)voltage, (uint32_t)((voltage-(int32_t)voltage)*100));
#if DEBUG
while (true);
#else
goto error;
#endif
} else if (voltage>BUSVOODOO_12V_TEST+0.3) {
printf("12V voltage is too high: %d.%02uV, check 12V voltage regulator\n", (int32_t)voltage, (uint32_t)((voltage-(int32_t)voltage)*100));
#if DEBUG
while (true);
#else
goto error;
#endif
}
dac_disable(BUSVOOFOO_12VCTL_CHANNEL); // disable 12V control
gpio_clear(GPIO(BUSVOODOO_12VEN_PORT), GPIO(BUSVOODOO_12VEN_PIN)); // disable 12V voltage regulator
sleep_ms(1); // let voltage settle
// pull all pins down and ensure they are low
for (uint8_t pin=0; pin<LENGTH(busvoodoo_io_ports) && pin<LENGTH(busvoodoo_io_pins); pin++) {
gpio_set_mode(busvoodoo_io_ports[pin], GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, busvoodoo_io_pins[pin]); // set pin to input
gpio_clear(busvoodoo_io_ports[pin], busvoodoo_io_pins[pin]); // pull down so it's not floating
}
for (uint8_t pin=0; pin<LENGTH(busvoodoo_io_ports) && pin<LENGTH(busvoodoo_io_pins) && pin<LENGTH(busvoodoo_io_names); pin++) {
if (gpio_get(busvoodoo_io_ports[pin], busvoodoo_io_pins[pin])) { // ensure it really is low
printf("signal %s is high although it is pulled low (internal)\n", busvoodoo_io_names[pin]); // warn user about the error
#if DEBUG
while (true);
#else
goto error;
#endif
}
}
// pull all pins up and ensure they are high
for (uint8_t pin=0; pin<LENGTH(busvoodoo_io_ports) && pin<LENGTH(busvoodoo_io_pins); pin++) {
gpio_set_mode(busvoodoo_io_ports[pin], GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, busvoodoo_io_pins[pin]); // set pin to input
gpio_set(busvoodoo_io_ports[pin], busvoodoo_io_pins[pin]); // pull up using internal pull-up
}
for (uint8_t pin=0; pin<LENGTH(busvoodoo_io_ports) && pin<LENGTH(busvoodoo_io_pins) && pin<LENGTH(busvoodoo_io_names); pin++) {
if (!gpio_get(busvoodoo_io_ports[pin], busvoodoo_io_pins[pin])) { // ensure it really is high
// this does not apply to v0.001 because of a design error
printf("signal %s is low although it is pulled up\n", busvoodoo_io_names[pin]); // warn user about the error
#if DEBUG
// while (true);
#else
// goto error;
#endif
}
}
// set individual pin high and ensure only pins in the same group are at the same level
for (uint8_t pin=0; pin<LENGTH(busvoodoo_io_ports) && pin<LENGTH(busvoodoo_io_pins); pin++) {
gpio_set_mode(busvoodoo_io_ports[pin], GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, busvoodoo_io_pins[pin]); // set pin to input
gpio_clear(busvoodoo_io_ports[pin], busvoodoo_io_pins[pin]); // pull down to ensure it is not high by accident
}
for (uint8_t pin1=0; pin1<LENGTH(busvoodoo_io_ports) && pin1<LENGTH(busvoodoo_io_pins) && pin1<LENGTH(busvoodoo_io_groups) && pin1<LENGTH(busvoodoo_io_names); pin1++) {
gpio_set_mode(busvoodoo_io_ports[pin1], GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, busvoodoo_io_pins[pin1]); // set button pin to output
gpio_set(busvoodoo_io_ports[pin1], busvoodoo_io_pins[pin1]); // set pin high
for (uint8_t pin2=0; pin2<LENGTH(busvoodoo_io_ports) && pin2<LENGTH(busvoodoo_io_pins) && pin2<LENGTH(busvoodoo_io_groups) && pin2<LENGTH(busvoodoo_io_names); pin2++) {
if (busvoodoo_io_groups[pin1]==busvoodoo_io_groups[pin2] && !gpio_get(busvoodoo_io_ports[pin2], busvoodoo_io_pins[pin2])) {
printf("signal %s of I/O-%u is low while it should be set high by signal %s of I/O-%u\n", busvoodoo_io_names[pin2], busvoodoo_io_groups[pin2], busvoodoo_io_names[pin1], busvoodoo_io_groups[pin1]); // warn user about the error
goto error;
} else if (busvoodoo_io_groups[pin1]!=busvoodoo_io_groups[pin2] && gpio_get(busvoodoo_io_ports[pin2], busvoodoo_io_pins[pin2])) {
// this does not apply to v0.001 because of a design error
// printf("signal %s of I/O-%u is high while it should not be set high by signal %s of I/O-%u\n", busvoodoo_io_names[pin2], busvoodoo_io_groups[pin2], busvoodoo_io_names[pin1], busvoodoo_io_groups[pin1]); // warn user about the error
#if DEBUG
// while (true);
#else
// goto error;
#endif
}
}
gpio_set_mode(busvoodoo_io_ports[pin1], GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, busvoodoo_io_pins[pin1]); // set pin back to input
gpio_clear(busvoodoo_io_ports[pin1], busvoodoo_io_pins[pin1]); // pull pin back down
}
// set individual pin low and ensure only pins in the same group are at the same level
for (uint8_t pin=0; pin<LENGTH(busvoodoo_io_ports) && pin<LENGTH(busvoodoo_io_pins); pin++) {
gpio_set_mode(busvoodoo_io_ports[pin], GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, busvoodoo_io_pins[pin]); // set pin to input
gpio_set(busvoodoo_io_ports[pin], busvoodoo_io_pins[pin]); // pull up to ensure it is not low by accident
}
for (uint8_t pin1=0; pin1<LENGTH(busvoodoo_io_ports) && pin1<LENGTH(busvoodoo_io_pins) && pin1<LENGTH(busvoodoo_io_groups) && pin1<LENGTH(busvoodoo_io_names); pin1++) {
gpio_set_mode(busvoodoo_io_ports[pin1], GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, busvoodoo_io_pins[pin1]); // set button pin to output
gpio_clear(busvoodoo_io_ports[pin1], busvoodoo_io_pins[pin1]); // set pin low
for (uint8_t pin2=0; pin2<LENGTH(busvoodoo_io_ports) && pin2<LENGTH(busvoodoo_io_pins) && pin2<LENGTH(busvoodoo_io_groups) && pin2<LENGTH(busvoodoo_io_names); pin2++) {
if (busvoodoo_io_groups[pin1]==busvoodoo_io_groups[pin2] && gpio_get(busvoodoo_io_ports[pin2], busvoodoo_io_pins[pin2])) {
printf("signal %s of I/O-%u is high while it should be set low by signal %s of I/O-%u\n", busvoodoo_io_names[pin2], busvoodoo_io_groups[pin2], busvoodoo_io_names[pin1], busvoodoo_io_groups[pin1]); // warn user about the error
#if DEBUG
while (true);
#else
goto error;
#endif
} else if (busvoodoo_io_groups[pin1]!=busvoodoo_io_groups[pin2] && !gpio_get(busvoodoo_io_ports[pin2], busvoodoo_io_pins[pin2])) {
// this does not apply to v0.001 because of a design error
// printf("signal %s of I/O-%u is low while it should not be set low by signal %s of I/O-%u\n", busvoodoo_io_names[pin2], busvoodoo_io_groups[pin2], busvoodoo_io_names[pin1], busvoodoo_io_groups[pin1]); // warn user about the error
#if DEBUG
// while (true);
#else
// goto error;
#endif
}
}
gpio_set_mode(busvoodoo_io_ports[pin1], GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, busvoodoo_io_pins[pin1]); // set pin back to input
gpio_clear(busvoodoo_io_ports[pin1], busvoodoo_io_pins[pin1]); // pull pin back down
}
// test 5V pull-up
for (uint8_t pin=0; pin<LENGTH(busvoodoo_io_ports) && pin<LENGTH(busvoodoo_io_pins); pin++) {
gpio_set_mode(busvoodoo_io_ports[pin], GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, busvoodoo_io_pins[pin]); // set pin to input
gpio_clear(busvoodoo_io_ports[pin], busvoodoo_io_pins[pin]); // pull down to ensure it is not high by accident
}
gpio_clear(GPIO(BUSVOODOO_5VPULLUP_PORT), GPIO(BUSVOODOO_5VPULLUP_PIN)); // enable 5V pull-up
sleep_ms(1); // wait a bit for voltage to settle
voltage = rail_voltage(BUSVOODOO_5V_CHANNEL); // get 5V power rail voltage
if (voltage<4.0) {
printf("5V power rail voltage is too low: %d.%02uV, check for shorts on I/O connector\n", (int32_t)voltage, (uint32_t)((voltage-(int32_t)voltage)*100));
#if DEBUG
while (true);
#else
goto error;
#endif
} else if (voltage>5.5) {
printf("5V power rail voltage is too high: %d.%02uV, check USB port\n", (int32_t)voltage, (uint32_t)((voltage-(int32_t)voltage)*100));
#if DEBUG
while (true);
#else
goto error;
#endif
}
for (uint8_t pin=0; pin<LENGTH(busvoodoo_io_ports) && pin<LENGTH(busvoodoo_io_pins) && pin<LENGTH(busvoodoo_io_names); pin++) {
if (!gpio_get(busvoodoo_io_ports[pin], busvoodoo_io_pins[pin])) { // ensure it really is high
printf("signal %s is low although it is pulled up by 5V\n", busvoodoo_io_names[pin]); // warn user about the error
#if DEBUG
while (true);
#else
goto error;
#endif
}
}
gpio_set(GPIO(BUSVOODOO_5VPULLUP_PORT), GPIO(BUSVOODOO_5VPULLUP_PIN)); // disable 5V pull-up
// test xV pull-up set to 3.3V
for (uint8_t pin=0; pin<LENGTH(busvoodoo_io_ports) && pin<LENGTH(busvoodoo_io_pins); pin++) {
gpio_set_mode(busvoodoo_io_ports[pin], GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, busvoodoo_io_pins[pin]); // set pin to input
gpio_clear(busvoodoo_io_ports[pin], busvoodoo_io_pins[pin]); // pull down to ensure it is not high by accident
}
gpio_set(GPIO(BUSVOODOO_XVEN_PORT), GPIO(BUSVOODOO_XVEN_PIN)); // enable xV voltage regulator
gpio_clear(GPIO(BUSVOODOO_XVPULLUP_PORT), GPIO(BUSVOODOO_XVPULLUP_PIN)); // enable xV pull-up
sleep_ms(1); // let the voltage regulator start and voltage settle
voltage = rail_voltage(BUSVOODOO_XV_CHANNEL); // get xV voltage (without being driven it should be around 3.2V)
if (voltage<BUSVOODOO_XV_DEFAULT-0.2) {
printf("xV voltage is lower than expected: %d.%02uV, check xV voltage regulator\n", (int32_t)voltage, (uint32_t)((voltage-(int32_t)voltage)*100));
#if DEBUG
while (true);
#else
goto error;
#endif
} else if (voltage>BUSVOODOO_XV_DEFAULT+0.2) {
printf("xV voltage is too high: %d.%02uV, check xV voltage regulator\n", (int32_t)voltage, (uint32_t)((voltage-(int32_t)voltage)*100));
#if DEBUG
while (true);
#else
goto error;
#endif
}
for (uint8_t pin=0; pin<LENGTH(busvoodoo_io_ports) && pin<LENGTH(busvoodoo_io_pins) && pin<LENGTH(busvoodoo_io_names); pin++) {
if (!gpio_get(busvoodoo_io_ports[pin], busvoodoo_io_pins[pin])) { // ensure it really is high
printf("signal %s is low although it is pulled up by xV\n", busvoodoo_io_names[pin]); // warn user about the error
#if DEBUG
while (true);
#else
goto error;
#endif
}
}
gpio_clear(GPIO(BUSVOODOO_XVPULLUP_PORT), GPIO(BUSVOODOO_XVPULLUP_PIN)); // disable xV pull-up
gpio_clear(GPIO(BUSVOODOO_XVEN_PORT), GPIO(BUSVOODOO_XVEN_PIN)); // disable xV voltage regulator
// test 5V and 3.3V outputs
gpio_clear(GPIO(BUSVOODOO_VOUTEN_PORT), GPIO(BUSVOODOO_VOUTEN_PIN)); // enable Vout
sleep_ms(1); // wait a bit for voltage to settle
voltage = rail_voltage(BUSVOODOO_5V_CHANNEL); // get 5V power rail voltage
if (voltage<4.0) {
printf("5V power rail voltage is too low: %d.%02uV, check pin 2 on I/O connector\n", (int32_t)voltage, (uint32_t)((voltage-(int32_t)voltage)*100));
#if DEBUG
// while (true);
#else
goto error;
#endif
} else if (voltage>5.5) {
printf("5V power rail voltage is too high: %d.%02uV, check pin 2 on I/O connector\n", (int32_t)voltage, (uint32_t)((voltage-(int32_t)voltage)*100));
#if DEBUG
while (true);
#else
goto error;
#endif
}
voltage = rail_voltage(BUSVOODOO_3V3_CHANNEL); // get 3.3V power rail voltage
if (voltage<3.0) {
printf("3.3V power rail voltage is too low: %d.%02uV, check pin 3 on I/O connector\n", (int32_t)voltage, (uint32_t)((voltage-(int32_t)voltage)*100));
#if DEBUG
while (true);
#else
goto error;
#endif
} else if (voltage>3.6) {
printf("3.3V power rail voltage is too high: %d.%02uV, check pin 3 on I/O connector\n", (int32_t)voltage, (uint32_t)((voltage-(int32_t)voltage)*100));
#if DEBUG
while (true);
#else
goto error;
#endif
}
gpio_set(GPIO(BUSVOODOO_VOUTEN_PORT), GPIO(BUSVOODOO_VOUTEN_PIN)); // disable Vout
to_return = true; // all tests are successful
error:
safe_state(); // set back to safe state
if (!to_return) {
printf("the test procedure has been interrupted for safety reasons\n");
}
return to_return;
}
/** perform tests using external user */
static void test_external(void)
{
safe_state(); // start from safe state with all outputs switched off
// test 5V output on pin 2
printf("check pin 2 on I/O connector to verify 5V output, it should be switched off\n");
if (!wait_space()) {
goto end;
}
gpio_clear(GPIO(BUSVOODOO_VOUTEN_PORT), GPIO(BUSVOODOO_VOUTEN_PIN)); // enable Vout
printf("check pin 2 on I/O connector to verify 5V output, it should be switched on\n");
if (!wait_space()) {
goto end;
}
gpio_set(GPIO(BUSVOODOO_VOUTEN_PORT), GPIO(BUSVOODOO_VOUTEN_PIN)); // disable Vout
// test 3.3V output on pin 3
printf("check pin 3 on I/O connector to verify 3.3V output, it should be switched off\n");
if (!wait_space()) {
goto end;
}
gpio_clear(GPIO(BUSVOODOO_VOUTEN_PORT), GPIO(BUSVOODOO_VOUTEN_PIN)); // enable Vout
printf("check pin 3 on I/O connector to verify 3.3V output, it should be switched on\n");
if (!wait_space()) {
goto end;
}
gpio_set(GPIO(BUSVOODOO_VOUTEN_PORT), GPIO(BUSVOODOO_VOUTEN_PIN)); // disable Vout
// test xV output on pin 4
printf("check pin 4 on I/O connector to verify xV output, it should be switched off\n");
if (!wait_space()) {
goto end;
}
gpio_set(GPIO(BUSVOODOO_XVEN_PORT), GPIO(BUSVOODOO_XVEN_PIN)); // enable xV voltage regulator
printf("check pin 4 on I/O connector to verify xV output, it should be switched at 3.4V\n");
if (!wait_space()) {
goto end;
}
gpio_clear(GPIO(BUSVOODOO_XVEN_PORT), GPIO(BUSVOODOO_XVEN_PIN)); // disable xV voltage regulator
// test I/O pins
for (uint8_t io=1; io<=6; io++) { // test each I/O pin
for (uint8_t pin=0; pin<LENGTH(busvoodoo_io_ports) && pin<LENGTH(busvoodoo_io_pins) && pin<LENGTH(busvoodoo_io_groups); pin++) { // look for a pin mapped on this I/O
if (busvoodoo_io_groups[pin]==io) {
gpio_set_mode(busvoodoo_io_ports[pin], GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, busvoodoo_io_pins[pin]); // set pin to output
gpio_clear(busvoodoo_io_ports[pin], busvoodoo_io_pins[pin]); // set pin low
printf("check pin %u on I/O connector to verify output, it should be low\n", 4+io);
if (!wait_space()) {
goto end;
}
gpio_set(busvoodoo_io_ports[pin], busvoodoo_io_pins[pin]); // set pin high
printf("check pin %u on I/O connector to verify output, it should be high\n", 4+io);
if (!wait_space()) {
goto end;
}
gpio_set_mode(busvoodoo_io_ports[pin], GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, busvoodoo_io_pins[pin]); // set pin back to input
break; // stop looking for pin
}
}
}
end:
safe_state(); // go back to safe state
}
/** user input command */
static char command[32] = {0};
/** user input command index */
@ -97,6 +672,7 @@ static void process_command(char* str)
}
// parse command
if (0==strcmp(word,"h") || 0==strcmp(word,"help") || 0==strcmp(word,"?")) {
printf("unit ID: 0x%08x%08x%08x\n", DESIG_UNIQUE_ID0, DESIG_UNIQUE_ID1, DESIG_UNIQUE_ID2);
printf("available commands:\n");
printf("led [on|off|toggle]\n");
} else if (0==strcmp(word,"l") || 0==strcmp(word,"led")) {
@ -120,42 +696,6 @@ static void process_command(char* str)
} else {
goto error;
}
} else if (0==strcmp(word,"time")) {
word = strtok(NULL,delimiter);
if (!word) {
time_rtc = rtc_get_counter_val(); // get time from internal RTC
time_tm = localtime(&time_rtc); // convert time
printf("time: %02d:%02d:%02d\n", time_tm->tm_hour, time_tm->tm_min, time_tm->tm_sec);
} else if (strlen(word)!=8 || word[0]<'0' || word[0]>'2' || word[1]<'0' || word[1]>'9' || word[3]<'0' || word[3]>'5' || word[4]<'0' || word[4]>'9' || word[6]<'0' || word[6]>'5' || word[7]<'0' || word[7]>'9') { // time format is incorrect
goto error;
} else {
time_rtc = rtc_get_counter_val(); // get time from internal RTC
time_tm = localtime(&time_rtc); // convert time
time_tm->tm_hour = (word[0]-'0')*10+(word[1]-'0')*1; // set hours
time_tm->tm_min = (word[3]-'0')*10+(word[4]-'0')*1; // set minutes
time_tm->tm_sec = (word[6]-'0')*10+(word[7]-'0')*1; // set seconds
time_rtc = mktime(time_tm); // get back seconds
rtc_set_counter_val(time_rtc); // save time to internal RTC
printf("time set\n");
}
} else if (0==strcmp(word,"date")) {
word = strtok(NULL,delimiter);
if (!word) {
time_rtc = rtc_get_counter_val(); // get time from internal RTC
time_tm = localtime(&time_rtc); // convert time
printf("date: %d-%02d-%02d\n", 1900+time_tm->tm_year, time_tm->tm_mon+1, time_tm->tm_mday);
} else if (strlen(word)!=10 || word[0]!='2' || word[1]!='0' || word[2]<'0' || word[2]>'9' || word[3]<'0' || word[3]>'9' || word[5]<'0' || word[5]>'1' || word[6]<'0' || word[6]>'9' || word[8]<'0' || word[8]>'3' || word[9]<'0' || word[9]>'9') {
goto error;
} else {
time_rtc = rtc_get_counter_val(); // get time from internal RTC
time_tm = localtime(&time_rtc); // convert time
time_tm->tm_year = ((word[0]-'0')*1000+(word[1]-'0')*100+(word[2]-'0')*10+(word[3]-'0')*1)-1900; // set year
time_tm->tm_mon = (word[5]-'0')*10+(word[6]-'0')*1-1; // set month
time_tm->tm_mday = (word[8]-'0')*10+(word[9]-'0')*1; // set day
time_rtc = mktime(time_tm); // get back seconds
rtc_set_counter_val(time_rtc); // save time to internal RTC
printf("date set\n");
}
} else {
goto error;
}
@ -174,7 +714,6 @@ void main(void)
{
rcc_clock_setup_in_hse_8mhz_out_72mhz(); // use 8 MHz high speed external clock to generate 72 MHz internal clock
#if DEBUG
// enable functionalities for easier debug
DBGMCU_CR |= DBGMCU_CR_IWDG_STOP; // stop independent watchdog counter when code is halted
@ -191,7 +730,8 @@ void main(void)
board_setup(); // setup board
usart_setup(); // setup USART (for printing)
usb_cdcacm_setup(); // setup USB CDC ACM (for printing)
printf("welcome to the CuVoodoo STM32F1 example application\n"); // print welcome message
led_blink(0, 1); // switch blue LED on to show firmware is working
printf("\nwelcome to BusVoodoo\n"); // print welcome message
#if !(DEBUG)
// show watchdog information
@ -205,16 +745,89 @@ void main(void)
}
#endif
// setup RTC
printf("setup internal RTC: ");
rtc_auto_awake(RCC_LSE, 32768-1); // ensure internal RTC is on, uses the 32.678 kHz LSE, and the prescale is set to our tick speed, else update backup registers accordingly (power off the micro-controller for the change to take effect)
rtc_interrupt_enable(RTC_SEC); // enable RTC interrupt on "seconds"
nvic_enable_irq(NVIC_RTC_IRQ); // allow the RTC to interrupt
printf("OK\n");
// enable all GPIO domains since we use pins on all ports
rcc_periph_clock_enable(RCC_GPIOA); // enable clock for all GPIO domains
rcc_periph_clock_enable(RCC_GPIOB); // enable clock for all GPIO domains
rcc_periph_clock_enable(RCC_GPIOC); // enable clock for all GPIO domains
rcc_periph_clock_enable(RCC_GPIOD); // enable clock for all GPIO domains
safe_state(); // switch off all outputs
// setup ADC to measure the 5V, 3.3V, xV, and 12V power rails voltages
rcc_periph_clock_enable(RCC_ADC12_IN(BUSVOODOO_5V_CHANNEL)); // enable clock for GPIO domain for 5V channel
gpio_set_mode(ADC12_IN_PORT(BUSVOODOO_5V_CHANNEL), GPIO_MODE_INPUT, GPIO_CNF_INPUT_ANALOG, ADC12_IN_PIN(BUSVOODOO_5V_CHANNEL)); // set 5V channel as analogue input for the ADC
rcc_periph_clock_enable(RCC_ADC12_IN(BUSVOODOO_3V3_CHANNEL)); // enable clock for GPIO domain for 3.3V channel
gpio_set_mode(ADC12_IN_PORT(BUSVOODOO_3V3_CHANNEL), GPIO_MODE_INPUT, GPIO_CNF_INPUT_ANALOG, ADC12_IN_PIN(BUSVOODOO_3V3_CHANNEL)); // set 3.3V channel as analogue input for the ADC
rcc_periph_clock_enable(RCC_ADC12_IN(BUSVOODOO_XV_CHANNEL)); // enable clock for GPIO domain for xV channel
gpio_set_mode(ADC12_IN_PORT(BUSVOODOO_XV_CHANNEL), GPIO_MODE_INPUT, GPIO_CNF_INPUT_ANALOG, ADC12_IN_PIN(BUSVOODOO_XV_CHANNEL)); // set xV channel as analogue input for the ADC
rcc_periph_clock_enable(RCC_ADC12_IN(BUSVOODOO_12V_CHANNEL)); // enable clock for GPIO domain for 12V channel
gpio_set_mode(ADC12_IN_PORT(BUSVOODOO_12V_CHANNEL), GPIO_MODE_INPUT, GPIO_CNF_INPUT_ANALOG, ADC12_IN_PIN(BUSVOODOO_12V_CHANNEL)); // set 12V channel as analogue input for the ADC
rcc_periph_clock_enable(RCC_ADC1); // enable clock for ADC domain
adc_off(ADC1); // switch off ADC while configuring it
adc_set_sample_time_on_all_channels(ADC1, ADC_SMPR_SMP_28DOT5CYC); // use 28.5 cycles to sample (long enough to be stable)
adc_enable_temperature_sensor(ADC1); // enable internal voltage reference
adc_enable_external_trigger_regular(ADC1, ADC_CR2_EXTSEL_SWSTART); // use software trigger to start conversion
uint8_t channels[] = {ADC_CHANNEL17, ADC_CHANNEL(BUSVOODOO_5V_CHANNEL), ADC_CHANNEL(BUSVOODOO_3V3_CHANNEL), ADC_CHANNEL(BUSVOODOO_XV_CHANNEL), ADC_CHANNEL(BUSVOODOO_12V_CHANNEL)}; // voltages to convert: internal, 5V, 3.3V, xV, 12V
adc_set_regular_sequence(ADC1, LENGTH(channels), channels); // set channels to convert
adc_enable_discontinuous_mode_regular(ADC1, LENGTH(channels)); // convert all channels
adc_power_on(ADC1); // switch on ADC
sleep_us(1); // wait t_stab for the ADC to stabilize
adc_reset_calibration(ADC1); // remove previous non-calibration
adc_calibration(ADC1); // calibrate ADC for less accuracy errors
time_rtc= rtc_get_counter_val(); // get time from internal RTC
time_tm = localtime(&time_rtc); // convert time
printf("date: %d-%02d-%02d %02d:%02d:%02d\n", 1900+time_tm->tm_year, time_tm->tm_mon+1, time_tm->tm_mday, time_tm->tm_hour, time_tm->tm_min, time_tm->tm_sec);
// setup DAC to control xV and 12V voltage outputs
gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_ANALOG, GPIO4); // set both DAC channels as analog
gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_ANALOG, GPIO5); // set both DAC channels as analog
rcc_periph_clock_enable(RCC_DAC); // enable clock for DAC domain
dac_disable(BUSVOOFOO_XVCTL_CHANNEL); // disable output to configure it properly
dac_disable(BUSVOOFOO_12VCTL_CHANNEL); // disable output to configure it properly
dac_buffer_enable(BUSVOOFOO_XVCTL_CHANNEL); // enable output buffer to be able to drive larger loads (should be per default)
dac_buffer_enable(BUSVOOFOO_12VCTL_CHANNEL); // enable output buffer to be able to drive larger loads (should be per default)
dac_set_trigger_source(DAC_CR_TSEL1_SW); // use software to trigger the voltage change
dac_set_trigger_source(DAC_CR_TSEL2_SW); // use software to trigger the voltage change
// perform tests
printf("performing self-test, please remove all cables from connector\n");
if (!test_self()) { // perform self-test
printf("self-test failed\n"); // notify user
led_blink(0.5, 0.5); // show error on LEDs
} else {
printf("self-test succeeded\n"); // notify user
}
// printf("testing RS port\n");
// rs_setup();
printf("testing OLED screen\n");
i2c_master_setup(false);
const uint8_t oled_set_mux_ratio[] = {0x00, 0xa8, 0x3f};
i2c_master_write(0x3c, oled_set_mux_ratio, LENGTH(oled_set_mux_ratio), NULL, 0);
const uint8_t oled_set_display_offset[] = {0x00, 0xd3, 0x00};
i2c_master_write(0x3c, oled_set_display_offset, LENGTH(oled_set_display_offset), NULL, 0);
const uint8_t oled_set_start_line[] = {0x80, 0x40};
i2c_master_write(0x3c, oled_set_start_line, LENGTH(oled_set_start_line), NULL, 0);
const uint8_t oled_set_segment_remap[] = {0x80, 0xa0};
i2c_master_write(0x3c, oled_set_segment_remap, LENGTH(oled_set_segment_remap), NULL, 0);
const uint8_t oled_set_com_output_scan_direction[] = {0x80, 0xc0};
i2c_master_write(0x3c, oled_set_com_output_scan_direction, LENGTH(oled_set_com_output_scan_direction), NULL, 0);
const uint8_t oled_set_com_pins_hardware_configuration[] = {0x00, 0xda, 0x02};
i2c_master_write(0x3c, oled_set_com_pins_hardware_configuration, LENGTH(oled_set_com_pins_hardware_configuration), NULL, 0);
const uint8_t oled_set_contrast_control[] = {0x00, 0x81, 0x7f};
i2c_master_write(0x3c, oled_set_contrast_control, LENGTH(oled_set_contrast_control), NULL, 0);
const uint8_t oled_entire_display_on[] = {0x80, 0xa5};
i2c_master_write(0x3c, oled_entire_display_on, LENGTH(oled_entire_display_on), NULL, 0);
const uint8_t oled_normal_display[] = {0x80, 0xa6};
i2c_master_write(0x3c, oled_normal_display, LENGTH(oled_normal_display), NULL, 0);
const uint8_t oled_set_osc_frequency[] = {0x00, 0xd5, 0x80};
i2c_master_write(0x3c, oled_set_osc_frequency, LENGTH(oled_set_osc_frequency), NULL, 0);
const uint8_t oled_enable_charge_pump_regulator[] = {0x00, 0x8d, 0x14};
i2c_master_write(0x3c, oled_enable_charge_pump_regulator, LENGTH(oled_enable_charge_pump_regulator), NULL, 0);
const uint8_t oled_display_on[] = {0x80, 0xaf};
i2c_master_write(0x3c, oled_display_on, LENGTH(oled_display_on), NULL, 0);
while (true) {
}
printf("performing external test, please follow instructions\n");
test_external(); // perform external test
// main loop
printf("command input: ready\n");
@ -262,18 +875,6 @@ void main(void)
}
button_flag = false; // reset flag
}
while (rtc_internal_tick_flag) { // the internal RTC ticked
rtc_internal_tick_flag = false; // reset flag
action = true; // action has been performed
#if !defined(BLUE_PILL) // on the blue pill the LED is close to the 32.768 kHz oscillator and heavily influences it
led_toggle(); // toggle LED (good to indicate if main function is stuck)
#endif
time_rtc = rtc_get_counter_val(); // get time from internal RTC (seconds since Unix Epoch)
time_tm = localtime(&time_rtc); // get time in tm format from Epoch (time zones are not handled for non-POSIX environments)
if (0==time_tm->tm_sec) { // new minute
printf("time: %02d:%02d:%02d\n", time_tm->tm_hour, time_tm->tm_min, time_tm->tm_sec);
}
}
if (action) { // go to sleep if nothing had to be done, else recheck for activity
action = false;
} else {
@ -281,10 +882,3 @@ void main(void)
}
} // main loop
}
/** @brief interrupt service routine called when tick passed on RTC */
void rtc_isr(void)
{
rtc_clear_flag(RTC_SEC); // clear flag
rtc_internal_tick_flag = true; // notify to show new time
}