1045 lines
42 KiB
C
1045 lines
42 KiB
C
/** SWJ (SWD + JTAG) finder
|
|
* @file
|
|
* @author King Kévin <kingkevin@cuvoodoo.info>
|
|
* @copyright SPDX-License-Identifier: GPL-3.0-or-later
|
|
* @date 2016-2021
|
|
*/
|
|
|
|
/* standard libraries */
|
|
#include <stdint.h> // standard integer types
|
|
#include <stdlib.h> // standard utilities
|
|
#include <string.h> // string utilities
|
|
#include <time.h> // date/time utilities
|
|
#include <ctype.h> // utilities to check chars
|
|
#include <math.h> // rounding utilities
|
|
|
|
/* STM32 (including CM3) libraries */
|
|
#include <libopencmsis/core_cm3.h> // Cortex M3 utilities
|
|
#include <libopencm3/cm3/scb.h> // vector table definition
|
|
#include <libopencm3/cm3/nvic.h> // interrupt utilities
|
|
#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/desig.h> // design utilities
|
|
#include <libopencm3/stm32/flash.h> // flash utilities
|
|
#include <libopencm3/stm32/adc.h> // ADC utilities
|
|
|
|
/* own libraries */
|
|
#include "global.h" // board definitions
|
|
#include "print.h" // printing utilities
|
|
#include "usb_cdcacm.h" // USB CDC ACM utilities
|
|
#include "terminal.h" // handle the terminal interface
|
|
#include "menu.h" // menu utilities
|
|
#include "swd.h" // SWD utilities
|
|
|
|
/** watchdog period in ms */
|
|
#define WATCHDOG_PERIOD 10000
|
|
|
|
/** wakeup frequency (i.e. least number of times per second to perform the main loop) */
|
|
#define WAKEUP_FREQ 16
|
|
|
|
/** @defgroup main_flags flag set in interrupts to be processed in main task
|
|
* @{
|
|
*/
|
|
static volatile bool wakeup_flag = false; /**< flag set when wakeup timer triggered */
|
|
static volatile bool second_flag = false; /**< flag set when a second passed */
|
|
/** @} */
|
|
|
|
/** number of seconds since boot */
|
|
static uint32_t boot_time = 0;
|
|
|
|
#define TARGET_CHANNEL 1 /**< PA1/ADC1_IN1 used to measure target voltage */
|
|
const uint8_t adc_channels[] = {ADC_CHANNEL17, ADC_CHANNEL(TARGET_CHANNEL)}; /**< voltages to convert (channel 17 = internal voltage reference) */
|
|
#define TARGET_EN PA5 /**< pin to provide target voltage to LV side of voltage shifter (pulling them high through 10 kO) */
|
|
#define TARGET_RST PA6 /**< pin to reset target */
|
|
#define TARGET_5V PA7 /**< pin to provide 5 V on target voltage (controlling gate of pMOS, externally pulled up) */
|
|
#define TARGET_3V PB0 /**< pin to provide 3.3 V on target voltage (controlling gate of pMOS, externally pulled up) */
|
|
|
|
#define CHANNEL_NUMBERS 16 /**< number of target signals */
|
|
static const uint32_t channel_ports[] = {GPIO_PORT(PB12), GPIO_PORT(PB13), GPIO_PORT(PB14), GPIO_PORT(PB15), GPIO_PORT(PA8), GPIO_PORT(PA9), GPIO_PORT(PA10), GPIO_PORT(PA15), GPIO_PORT(PB3), GPIO_PORT(PB4), GPIO_PORT(PB5), GPIO_PORT(PB6), GPIO_PORT(PB7), GPIO_PORT(PB8), GPIO_PORT(PB9), GPIO_PORT(PB10)}; /**< GPIO ports for signal pin */
|
|
static const uint32_t channel_pins[] = {GPIO_PIN(PB12), GPIO_PIN(PB13), GPIO_PIN(PB14), GPIO_PIN(PB15), GPIO_PIN(PA8), GPIO_PIN(PA9), GPIO_PIN(PA10), GPIO_PIN(PA15), GPIO_PIN(PB3), GPIO_PIN(PB4), GPIO_PIN(PB5), GPIO_PIN(PB6), GPIO_PIN(PB7), GPIO_PIN(PB8), GPIO_PIN(PB9), GPIO_PIN(PB10)}; /**< GPIO pins for signal pin */
|
|
static uint8_t channel_start = 0; /**< first signal of range to probe */
|
|
static uint8_t channel_stop = CHANNEL_NUMBERS - 1; /**< last signal of range to probe */
|
|
|
|
size_t putc(char c)
|
|
{
|
|
size_t length = 0; // number of characters printed
|
|
static char last_c = 0; // to remember on which character we last sent
|
|
if ('\n' == c) { // send carriage return (CR) + line feed (LF) newline for each LF
|
|
if ('\r' != last_c) { // CR has not already been sent
|
|
usb_cdcacm_putchar('\r'); // send CR over USB
|
|
length++; // remember we printed 1 character
|
|
}
|
|
}
|
|
usb_cdcacm_putchar(c); // send byte over USB
|
|
length++; // remember we printed 1 character
|
|
last_c = c; // remember last character
|
|
return length; // return number of characters printed
|
|
}
|
|
|
|
/** print float with fixed precision
|
|
* @param[in] fpu float to print
|
|
* @param[in] precision number of digits after comma to print
|
|
* @note %f is used to force scientific notation
|
|
*/
|
|
static void print_fpu(double fpu, uint8_t precision)
|
|
{
|
|
uint32_t multiplier = 1;
|
|
for (uint8_t i = 0; i < precision; i++) {
|
|
multiplier *= 10;
|
|
}
|
|
double to_print = round(fpu * multiplier);
|
|
printf("%d.", (int32_t)to_print / multiplier);
|
|
char decimal[32];
|
|
snprintf(decimal, LENGTH(decimal), "%u", abs(to_print) % multiplier);
|
|
if (strlen(decimal) > precision) {
|
|
decimal[precision] = 0;
|
|
}
|
|
for (uint8_t i = strlen(decimal); i < precision; i++) {
|
|
putc('0');
|
|
}
|
|
puts(decimal);
|
|
}
|
|
|
|
/** get RCC from corresponding port
|
|
* @param[in] port port address
|
|
* @return RCC address corresponding to port
|
|
*/
|
|
static uint32_t port2rcc(uint32_t port)
|
|
{
|
|
uint32_t rcc = 0;
|
|
switch (port) {
|
|
case GPIOA:
|
|
rcc = RCC_GPIOA;
|
|
break;
|
|
case GPIOB:
|
|
rcc = RCC_GPIOB;
|
|
break;
|
|
case GPIOC:
|
|
rcc = RCC_GPIOC;
|
|
break;
|
|
case GPIOD:
|
|
rcc = RCC_GPIOD;
|
|
break;
|
|
case GPIOE:
|
|
rcc = RCC_GPIOE;
|
|
break;
|
|
case GPIOF:
|
|
rcc = RCC_GPIOF;
|
|
break;
|
|
case GPIOG:
|
|
rcc = RCC_GPIOG;
|
|
break;
|
|
default: // unknown port
|
|
while (true); // halt firmware
|
|
break;
|
|
}
|
|
return rcc;
|
|
}
|
|
|
|
/** measure target and signal voltages
|
|
* @return voltages of channels
|
|
*/
|
|
static float* measure_voltages(void)
|
|
{
|
|
static float voltages[LENGTH(adc_channels)]; // to store and return the voltages
|
|
|
|
// read lid temperature using ADC
|
|
ADC_SR(ADC1) = 0; // reset flags
|
|
uint16_t adc_values[LENGTH(adc_channels)];
|
|
for (uint8_t i = 0; i < LENGTH(adc_channels); i++) {
|
|
adc_start_conversion_regular(ADC1); // start conversion (using trigger)
|
|
while (!adc_eoc(ADC1)); // wait until conversion finished
|
|
adc_values[i] = adc_read_regular(ADC1); // read voltage value (clears flag)
|
|
voltages[i] = adc_values[i] * 1.21 / adc_values[0]; // use 1.21 V internal voltage reference to get ADC voltage
|
|
}
|
|
voltages[1] *= 2.0; // the is a /2 voltage divider for target voltage
|
|
return voltages;
|
|
}
|
|
|
|
/** print decoded IDCODE
|
|
* @param[in] idcode IDCODE to decode
|
|
*/
|
|
static void print_idcode(uint32_t idcode)
|
|
{
|
|
printf("designer: %03x/%s, part number: 0x%04x/%s, revision %u",
|
|
(idcode >> 1) & 0x3ff,
|
|
swd_jep106_manufacturer((idcode >> 8) & 0x0f, (idcode >> 1) & 0x7f),
|
|
(idcode >> 12) & 0xffff,
|
|
swd_dpidr_partno((idcode >> 1) & 0x3ff, (idcode >> 12) & 0xffff),
|
|
(idcode >> 28) & 0x0f);
|
|
}
|
|
|
|
// menu commands
|
|
|
|
/** measure and print target voltage
|
|
* @param[in] argument 0 to no provide power, 3 to provide 3.3V, 5 to provide 5V
|
|
*/
|
|
static void command_target_voltage(void* argument)
|
|
{
|
|
(void)argument; // we won't use the argument
|
|
|
|
gpio_set(GPIO_PORT(TARGET_EN), GPIO_PIN(TARGET_EN)); // ensure the level shifters pulling up the signals are not enabled
|
|
|
|
// set voltage
|
|
if (argument) { // if argument is provided
|
|
const uint8_t voltage = *(uint32_t*)argument; // get target voltage
|
|
switch (voltage) {
|
|
case 0:
|
|
gpio_set(GPIO_PORT(TARGET_5V), GPIO_PIN(TARGET_5V)); // disable 5V output
|
|
gpio_set(GPIO_PORT(TARGET_3V), GPIO_PIN(TARGET_3V)); // disable 3V output
|
|
break;
|
|
case 3:
|
|
gpio_set(GPIO_PORT(TARGET_5V), GPIO_PIN(TARGET_5V)); // disable 5V output
|
|
gpio_clear(GPIO_PORT(TARGET_3V), GPIO_PIN(TARGET_3V)); // enable 3V output
|
|
break;
|
|
case 5:
|
|
gpio_clear(GPIO_PORT(TARGET_5V), GPIO_PIN(TARGET_5V)); // enable 5V output
|
|
gpio_set(GPIO_PORT(TARGET_3V), GPIO_PIN(TARGET_3V)); // disable 3V output
|
|
break;
|
|
default:
|
|
puts("unknown voltage to set\n");
|
|
break;
|
|
}
|
|
sleep_us(100); // wait a bit for voltage to settle
|
|
}
|
|
|
|
// show voltage output
|
|
if (!gpio_get(GPIO_PORT(TARGET_5V), GPIO_PIN(TARGET_5V))) {
|
|
puts("target voltage set to 5 V\n");
|
|
} else if (!gpio_get(GPIO_PORT(TARGET_3V), GPIO_PIN(TARGET_3V))) {
|
|
puts("target voltage set to 3.3 V\n");
|
|
} else {
|
|
puts("target voltage externally provided\n");
|
|
}
|
|
|
|
float* voltages = measure_voltages(); // measure voltages
|
|
puts("target voltage: ");
|
|
print_fpu(voltages[1], 2);
|
|
puts(" V");
|
|
if (voltages[1] < 1.0) {
|
|
puts(" (warning: target voltage seems not connected)");
|
|
}
|
|
putc('\n');
|
|
}
|
|
|
|
/** configure or reset target
|
|
* @param[in] argument 1 to assert reset, 0 to release reset, ODL to set reset pin to open-drain active low, ODH to set reset pin to open-drain active high, PPL to set reset pin to push-pull active low, PPH to set reset pin to push-pull active high
|
|
*/
|
|
static void command_target_reset(void* argument)
|
|
{
|
|
(void)argument; // we won't use the argument
|
|
static bool active_low = true; // if the reset is active low or high
|
|
|
|
// set reset mode
|
|
if (argument) { // if argument is provided
|
|
if (0 == strcmp("0", argument)) { // release reset
|
|
if (active_low) {
|
|
gpio_set(GPIO_PORT(TARGET_RST), GPIO_PIN(TARGET_RST));
|
|
} else {
|
|
gpio_clear(GPIO_PORT(TARGET_RST), GPIO_PIN(TARGET_RST));
|
|
}
|
|
} else if (0 == strcmp("1", argument)) { // assert reset
|
|
if (active_low) {
|
|
gpio_clear(GPIO_PORT(TARGET_RST), GPIO_PIN(TARGET_RST));
|
|
} else {
|
|
gpio_set(GPIO_PORT(TARGET_RST), GPIO_PIN(TARGET_RST));
|
|
}
|
|
} else if (0 == strcmp("ODL", argument)) { // set reset to open-drain active low
|
|
active_low = true; // remember we are active low
|
|
gpio_set_output_options(GPIO_PORT(TARGET_RST), GPIO_OTYPE_OD, GPIO_OSPEED_2MHZ, GPIO_PIN(TARGET_RST)); // set output as open-drain
|
|
} else if (0 == strcmp("ODH", argument)) { // set reset to open-drain active hig
|
|
active_low = false; // remember we are active high
|
|
gpio_set_output_options(GPIO_PORT(TARGET_RST), GPIO_OTYPE_OD, GPIO_OSPEED_2MHZ, GPIO_PIN(TARGET_RST)); // set output as open-drain
|
|
} else if (0 == strcmp("PPL", argument)) { // set reset to push-pull active low
|
|
active_low = true; // remember we are active low
|
|
gpio_set_output_options(GPIO_PORT(TARGET_RST), GPIO_OTYPE_PP, GPIO_OSPEED_2MHZ, GPIO_PIN(TARGET_RST)); // set output as push-pull
|
|
} else if (0 == strcmp("PPH", argument)) { // set reset to push-pull active high
|
|
active_low = false; // remember we are active high
|
|
gpio_set_output_options(GPIO_PORT(TARGET_RST), GPIO_OTYPE_PP, GPIO_OSPEED_2MHZ, GPIO_PIN(TARGET_RST)); // set output as push-pull
|
|
}
|
|
}
|
|
|
|
const bool open_drain = (GPIO_OTYPER(GPIO_PORT(TARGET_RST)) & GPIO_PIN(TARGET_RST)); // if the output is configured as open drain (else it's push-pull)
|
|
printf("reset pin set to %s active %s\n", open_drain ? "open-drain" : "push-pull", active_low ? "low" : "high");
|
|
if (gpio_get(GPIO_PORT(TARGET_RST), GPIO_PIN(TARGET_RST))) {
|
|
if (active_low) {
|
|
puts("reset released\n");
|
|
} else {
|
|
puts("reset asserted\n");
|
|
}
|
|
} else {
|
|
if (active_low) {
|
|
puts("reset asserted\n");
|
|
} else {
|
|
puts("reset released\n");
|
|
}
|
|
}
|
|
}
|
|
|
|
/** scan for SWD interface on channels
|
|
* @param[in] argument not used
|
|
*/
|
|
static void command_swd_scan(void* argument)
|
|
{
|
|
(void)argument; // we won't use the argument
|
|
|
|
float* voltages = measure_voltages(); // measure voltages
|
|
if (voltages[1] < 0.5) { // check target voltage connection
|
|
puts("connect target voltage to test channel type\n");
|
|
return;
|
|
}
|
|
|
|
gpio_clear(GPIO_PORT(TARGET_EN), GPIO_PIN(TARGET_EN)); // power level shifter
|
|
sleep_us(100); // wait a tiny bit for the pull-up to be active
|
|
|
|
printf("searching SWD on channels (%u combinations): ", (channel_stop - channel_start + 1) * (channel_stop - channel_start));
|
|
|
|
uint8_t found = 0;
|
|
for (uint8_t swclk = channel_start; swclk <= channel_stop; swclk++) {
|
|
for (uint8_t swdio = channel_start; swdio <= channel_stop; swdio++) {
|
|
// skip when SWCLK and SWDIO share a same pin
|
|
if (swdio == swclk) {
|
|
continue;
|
|
}
|
|
// set SWCLK/SWDIO combination
|
|
if (!swd_set_pins(channel_ports[swclk], channel_pins[swclk], channel_ports[swdio], channel_pins[swdio])) {
|
|
putc('!');
|
|
continue;
|
|
}
|
|
// switch from JTAG to SWD (see ARM IHI 0074A B5.2.2)
|
|
swd_line_reset(); // put target in reset state
|
|
swd_jtag_to_swd(); // put target SWJ in SWD mode
|
|
// read DPIDR (see ARM IHI 0074A)
|
|
// connection, e.g. reading the DPIDR, is the only allowed action after line reset
|
|
uint8_t retry = 2; // number of times to retry reading DPIDR
|
|
while (retry) {
|
|
swd_line_reset(); // put target in reset state
|
|
swd_idle_cycles(2); // idle before packet request
|
|
swd_packet_request(false, SWD_A_DP_DPIDR, true); // request DPIDR
|
|
swd_turnaround(1); // switch from writing to reading
|
|
const enum swd_ack_e ack = swd_acknowledge_response(); // get ack
|
|
uint32_t data; // data to read/write over SWD
|
|
const bool parity = swd_read(&data); // read data no matter what
|
|
swd_turnaround(1); // switch from reading to writing
|
|
switch (ack) {
|
|
case SWD_ACK_OK: // expected answer
|
|
if (parity) { // parity is ok
|
|
printf("\nSWD found: SWCLK=CH%02u SWDIO=CH%02u, DPIDR=0x%08x", swclk, swdio, data);
|
|
found++; // remember we found an SWD combination
|
|
if (data & 0x1) {
|
|
puts(" (");
|
|
print_idcode(data); // decode DPIDR
|
|
puts(")");
|
|
} else {
|
|
puts(" (invalid LSb)");
|
|
}
|
|
} else {
|
|
printf("(invalid: RAO != 1)");
|
|
}
|
|
break;
|
|
case SWD_ACK_NOREPLY: // the is no SWD here
|
|
puts("no reply");
|
|
retry = 0; // no need to retry
|
|
break;
|
|
case SWD_ACK_WAIT: // not allowed for DPIDR
|
|
case SWD_ACK_FAULT: // not allowed for DPIDR
|
|
default: // invalid ACK
|
|
if (!swd_read(&data)) { // read the data
|
|
puts("parity error ");
|
|
}
|
|
printf("garbage data %+08x ", data);
|
|
swd_turnaround(1); // switch from reading to writing
|
|
break;
|
|
}
|
|
if (retry) {
|
|
retry--; // decrement retry count
|
|
}
|
|
}
|
|
puts("\n");
|
|
swd_release_pins(); // release pins
|
|
} // end SWDIO
|
|
} // end SWCLK
|
|
|
|
gpio_set(GPIO_PORT(TARGET_EN), GPIO_PIN(TARGET_EN)); // disable level shifters
|
|
printf("\n%u SWD interface(s) found\n", found);
|
|
}
|
|
|
|
#define JTAG_SPEED 50 /**< time in us between clock edges (i.e. setting the clock speed) */
|
|
#define JTAG_PATTERN 0x0ff06699 /**< pattern to fin TDI pin */
|
|
static int8_t jtag_tms_ch = -1; /**< channel used for JTAG TCK output (-1 = not configured) */
|
|
static int8_t jtag_tck_ch = -1; /**< channel used for JTAG TMS output (-1 = not configured) */
|
|
static int8_t jtag_tdi_ch = -1; /**< channel used for JTAG TMS output (-1 = not configured) */
|
|
static uint32_t jtag_tdo[CHANNEL_NUMBERS]; /**< possible TDO pin data bits, for each channel (1 for TCK/TMS/TDO pins) */
|
|
|
|
/** send data to JTAG pins
|
|
* @param[in] tms TMS bits to send (LSb first)
|
|
* @param[in] tdi TDI bits to send (LSb first)
|
|
* @param[in] nb number of bits to send
|
|
* @note TDO data will be stored in jtag_tdo (only between channel start and stop, and TDI/TMS/TCK set to 1)
|
|
*/
|
|
static void jtag_transaction(uint32_t tms, uint32_t tdi, uint8_t nb)
|
|
{
|
|
if (jtag_tck_ch < 0 || jtag_tck_ch >= CHANNEL_NUMBERS || jtag_tms_ch < 0 || jtag_tms_ch >= CHANNEL_NUMBERS) { // ensure at least TCK and TMS are configured
|
|
return;
|
|
}
|
|
|
|
// reset TDO values
|
|
for (uint8_t tdo = 0; tdo < LENGTH(jtag_tdo); tdo++) {
|
|
jtag_tdo[tdo] = UINT32_MAX;
|
|
}
|
|
|
|
gpio_set(channel_ports[jtag_tck_ch], channel_pins[jtag_tck_ch]); // ensure we start with TCK high
|
|
for (uint8_t bit = 0; bit < nb && bit < 32; bit++) { // go through all bits
|
|
sleep_us(JTAG_SPEED); // wait for clock falling edge
|
|
// output change is on TCK falling edge
|
|
if (tms & 0x1) {
|
|
gpio_set(channel_ports[jtag_tms_ch], channel_pins[jtag_tms_ch]); // set TMS high
|
|
} else {
|
|
gpio_clear(channel_ports[jtag_tms_ch], channel_pins[jtag_tms_ch]); // set TMS low
|
|
}
|
|
tms >>= 1; // go to next bit
|
|
if (jtag_tdi_ch >= channel_start && jtag_tdi_ch <= channel_stop) { // TDI is configured
|
|
if (tdi & 0x1) {
|
|
gpio_set(channel_ports[jtag_tdi_ch], channel_pins[jtag_tdi_ch]); // set TDI high
|
|
} else {
|
|
gpio_clear(channel_ports[jtag_tdi_ch], channel_pins[jtag_tdi_ch]); // set TDI low
|
|
}
|
|
tdi >>= 1; // go to next bit
|
|
}
|
|
gpio_clear(channel_ports[jtag_tck_ch], channel_pins[jtag_tck_ch]); // clock falling edge
|
|
sleep_us(JTAG_SPEED); // wait for clock rising edge
|
|
gpio_set(channel_ports[jtag_tck_ch], channel_pins[jtag_tck_ch]); // clock rising edge
|
|
for (uint8_t tdo = channel_start; tdo < channel_stop && tdo < CHANNEL_NUMBERS; tdo++) { // read TDO
|
|
if (tdo == jtag_tms_ch || tdo == jtag_tck_ch || tdo == jtag_tdi_ch) { // channel is already used
|
|
continue; // ignore output signal
|
|
} else if (0 == gpio_get(channel_ports[tdo], channel_pins[tdo])) { // signal is low
|
|
jtag_tdo[tdo] &= ~(1U << bit); // clear bit
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/** scan for JTAG interface on channels
|
|
* @param[in] argument not used
|
|
*/
|
|
static void command_jtag_scan(void* argument)
|
|
{
|
|
(void)argument; // we won't use the argument
|
|
|
|
float* voltages = measure_voltages(); // measure voltages
|
|
if (voltages[1] < 0.5) { // check target voltage connection
|
|
puts("connect target voltage to test channel type\n");
|
|
return;
|
|
}
|
|
|
|
gpio_clear(GPIO_PORT(TARGET_EN), GPIO_PIN(TARGET_EN)); // power level shifter
|
|
sleep_us(100); // wait a tiny bit for the pull-up to be active
|
|
|
|
printf("searching JTAG on channels CH%02u-CH%02u\n", channel_start, channel_stop);
|
|
|
|
printf("searching for TDO using IDCODE scan on TCK/TMS (%u combinations): ", (channel_stop - channel_start + 1) * (channel_stop - channel_start));
|
|
uint8_t idcodes[CHANNEL_NUMBERS]; // how many IDCODEs have been found on channel
|
|
for (uint8_t i = 0; i < LENGTH(idcodes); i++) {
|
|
idcodes[i] = 0;
|
|
}
|
|
bool tck_ok[CHANNEL_NUMBERS]; // if channel is a possible TCK
|
|
for (uint8_t i = 0; i < LENGTH(tck_ok); i++) {
|
|
tck_ok[i] = false;
|
|
}
|
|
bool tms_ok[CHANNEL_NUMBERS]; // if channel is a possible TMS
|
|
for (uint8_t i = 0; i < LENGTH(tms_ok); i++) {
|
|
tms_ok[i] = false;
|
|
}
|
|
jtag_tdi_ch = -1; // we don't use TDI for now
|
|
for (uint8_t tck = channel_start; tck <= channel_stop; tck++) { // use channel as TCK output
|
|
for (uint8_t tms = channel_start; tms <= channel_stop; tms++) { // use channel as TMS output
|
|
if (tck == tms) { // don't use the same channel for TCK and TMS
|
|
continue;
|
|
}
|
|
gpio_set(channel_ports[tck], channel_pins[tck]); // clock is idle high
|
|
gpio_mode_setup(channel_ports[tck], GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, channel_pins[tck]); // set channel for TCK as output
|
|
jtag_tck_ch = tck; // remember which channel we use for TCK for the transaction
|
|
gpio_set(channel_ports[tms], channel_pins[tms]); // start high (to go to reset state)
|
|
gpio_mode_setup(channel_ports[tms], GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, channel_pins[tms]); // set channel for TMS as output
|
|
jtag_tms_ch = tms; // remember which channel we use for TMS for the transaction
|
|
jtag_transaction(0xffffffff, 0, 26); // ensure we are is reset state, even on SWD devices (needs 50 TMS hig);
|
|
jtag_transaction(0xffffffff, 0, 26); // continuation
|
|
jtag_transaction(0xE73C, 0, 16); // send sequence to switch any SWD device back to JTAG (this constant magic value)
|
|
// all other channel should already be inputs
|
|
jtag_transaction(0x3f | (0 << 6) | (1 << 7) | (0 << 8) | (0 << 9), 0, 6 + 1 + 1 + 1 + 1); // go back to JTAG TEST-LOGIC_RESET (5 bits should be enough to go from any state to RESET, but we a one just to be sure) -> RUN-TEST/IDLE -> SELECT-DR-SCAN -> CAPTURE-DR -> SHIFT-DR states
|
|
bool idcode[CHANNEL_NUMBERS]; // when a new IDCODE has been found
|
|
for (uint8_t i = 0; i < LENGTH(idcode); i++) {
|
|
idcode[i] = true;
|
|
}
|
|
bool idcode_scan = true; // if we need to check for an IDCODE
|
|
bool idcode_found = false; // if we found an IDCODE (in this combination)
|
|
uint8_t idcode_max = 20; // maximum number of chained IDCODEs
|
|
while (idcode_scan && idcode_max) {
|
|
idcode_scan = false; // stop scanning (unless we find an IDCODE)
|
|
jtag_transaction(0, 0, 32); // read 32-bit IDCODE
|
|
for (uint8_t tdo = channel_start; tdo < channel_stop; tdo++) {
|
|
if (tdo == tms || tdo == tck) { // channel already used for other signal
|
|
continue;
|
|
} else if (!idcode[tdo]) { // IDCODE already exhausted
|
|
continue; // any other data on the line should be noise
|
|
} else if (0 == jtag_tdo[tdo] || UINT32_MAX == jtag_tdo[tdo]) { // no IDCODE received (line constant low or pulled up
|
|
idcode[tdo] = false;
|
|
} else { // IDCODE received
|
|
printf("\nIDCODE found: TCK=CH%02u TMS=CH%02u TDO=CH%02u CHAIN=%u IDCODE=%+08x (", tck, tms, tdo, idcodes[tdo] + 1, jtag_tdo[tdo]); // show finding
|
|
if (jtag_tdo[tdo] & 0x1) { // RAO bit is wrong
|
|
print_idcode(jtag_tdo[tdo]);
|
|
} else { // RAO bit 0 is wrong
|
|
puts("invalid");
|
|
}
|
|
puts(")");
|
|
idcodes[tdo]++; // count the number of IDCODEs found
|
|
tck_ok[tck] = true; // remember we found TCK on this channel
|
|
tms_ok[tms] = true; // remember we found TMS on this channel
|
|
idcode_found = true; // remember we found an IDCODE
|
|
idcode_scan = true; // continue scanning for the next code
|
|
}
|
|
}
|
|
idcode_max--; // to not be stuck in a loop
|
|
}
|
|
if (idcode_found) {
|
|
putc('\n'); // continue dot pattern on new line
|
|
}
|
|
gpio_mode_setup(channel_ports[tck], GPIO_MODE_INPUT, GPIO_PUPD_NONE, channel_pins[tck]); // set channel for TCK back to input
|
|
jtag_tck_ch = -1; // clear channel configuration
|
|
gpio_mode_setup(channel_ports[tms], GPIO_MODE_INPUT, GPIO_PUPD_NONE, channel_pins[tms]); // set channel for TMS back to input
|
|
jtag_tms_ch = -1; // clear channel configuration
|
|
putc('.'); // one combination completed
|
|
}
|
|
}
|
|
putc('\n'); // all combinations completed
|
|
|
|
// get max length of scan chain
|
|
uint8_t chain = 0;
|
|
for (uint8_t tdo = channel_start; tdo <= channel_stop; tdo++) {
|
|
if (idcodes[tdo] > chain) {
|
|
chain = idcodes[tdo];
|
|
}
|
|
}
|
|
if (0 == chain) {
|
|
puts("no IDCODE found\n");
|
|
return;
|
|
}
|
|
|
|
printf("searching for TDI using IDCODE feeding on TCK/TMS/TDO: ");
|
|
jtag_tdi_ch = -1; // we don't use TDI for now
|
|
for (uint8_t tck = channel_start; tck <= channel_stop; tck++) { // test channel as TCK output
|
|
if (!tck_ok[tck]) { // this is not one of the possibles TCK
|
|
continue;
|
|
}
|
|
for (uint8_t tms = channel_start; tms <= channel_stop; tms++) { // test channel as TMS output
|
|
if (tck == tms) { // don't use the same channel for TCK and TMS
|
|
continue;
|
|
}
|
|
if (!tms_ok[tms]) { // this is not one of the possible TMS
|
|
continue;
|
|
}
|
|
for (uint8_t tdi = channel_start; tdi <= channel_stop; tdi++) { // test channel as TDI
|
|
if (tck == tdi) { // don't use the same channel for TCK and TDI
|
|
continue;
|
|
}
|
|
if (tms == tdi) { // don't use the same channel for TMS and TDI
|
|
continue;
|
|
}
|
|
bool tdi_found = false; // if we found a TDI pin in this combination
|
|
gpio_set(channel_ports[tck], channel_pins[tck]); // clock is idle high
|
|
gpio_mode_setup(channel_ports[tck], GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, channel_pins[tck]); // set channel for TCK as output
|
|
jtag_tck_ch = tck; // remember which channel we use for TCK for the transaction
|
|
gpio_set(channel_ports[tms], channel_pins[tms]); // start high (to go to reset state)
|
|
gpio_mode_setup(channel_ports[tms], GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, channel_pins[tms]); // set channel for TMS as output
|
|
jtag_tms_ch = tms; // remember which channel we use for TMS for the transaction
|
|
gpio_set(channel_ports[tdi], channel_pins[tdi]); // start high (idle state)
|
|
gpio_mode_setup(channel_ports[tdi], GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, channel_pins[tdi]); // set channel for TMS back to input
|
|
jtag_tdi_ch = tdi; // remember which channel we use for TDI for the transaction
|
|
// all other channels are already inputs (to check TDO)
|
|
// switching from SWD to JTAG has already been done
|
|
jtag_transaction(0x3f | (0 << 6) | (1 << 7) | (0 << 8) | (0 << 9), 0, 6 + 1 + 1 + 1 + 1); // go to IDCODE state: back to JTAG TEST-LOGIC_RESET (5 bits should be enough to go from any state to RESET, but we a one just to be sure) -> RUN-TEST/IDLE -> SELECT-DR-SCAN -> CAPTURE-DR -> SHIFT-DR
|
|
for (uint8_t sequence = 0; sequence <= chain; sequence++) { // go through longest chain
|
|
jtag_transaction(0, JTAG_PATTERN, 32); // send pattern into chain
|
|
for (uint8_t tdo = channel_start; tdo <= channel_stop; tdo++) { // test channel as TDO
|
|
if (tck == tdo) { // don't use the same channel for TCK and TDO
|
|
continue;
|
|
}
|
|
if (tms == tdo) { // don't use the same channel for TMS and TDO
|
|
continue;
|
|
}
|
|
if (0 == idcodes[tdo]) { // we did not seen any IDCODE on this pin
|
|
continue;
|
|
}
|
|
if (sequence < idcodes[tdo]) { // we did not got through the chain yet, thus we don't expect the pattern
|
|
continue;
|
|
}
|
|
if (0 == jtag_tdo[tdo] || 0xffffffff == jtag_tdo[tdo]) { // we received nothing
|
|
continue;
|
|
}
|
|
if (JTAG_PATTERN == jtag_tdo[tdo]) { // we found out pattern
|
|
printf("\nJTAG found: TCK=CH%02u TMS=CH%02u TDO=CH%02u TDI=CH%02u CHAIN=%u", tck, tms, tdo, tdi, sequence);
|
|
tdi_found = true; // remember we found one and printed
|
|
}
|
|
}
|
|
}
|
|
if (tdi_found) {
|
|
putc('\n'); // continue dot pattern on new line
|
|
} else {
|
|
putc('.');
|
|
}
|
|
gpio_mode_setup(channel_ports[tck], GPIO_MODE_INPUT, GPIO_PUPD_NONE, channel_pins[tck]); // set channel for TCK back to input
|
|
jtag_tck_ch = -1; // clear channel configuration
|
|
gpio_mode_setup(channel_ports[tms], GPIO_MODE_INPUT, GPIO_PUPD_NONE, channel_pins[tms]); // set channel for TMS back to input
|
|
jtag_tms_ch = -1; // clear channel configuration
|
|
gpio_mode_setup(channel_ports[tdi], GPIO_MODE_INPUT, GPIO_PUPD_NONE, channel_pins[tdi]); // set channel for TDI back to input
|
|
jtag_tdi_ch = -1; // clear channel configuration
|
|
} // end test channel as TDI
|
|
} // end test channel as TMS
|
|
} // end test channel as TCK
|
|
putc('\n'); // all combinations completed
|
|
}
|
|
|
|
/** set first channel of range to scan
|
|
* @param[in] argument optional pointer to first channel number
|
|
*/
|
|
static void command_channel_start(void* argument)
|
|
{
|
|
if (argument) {
|
|
const uint32_t channel = *(uint32_t*)argument;
|
|
if (channel < CHANNEL_NUMBERS && channel < channel_stop) {
|
|
channel_start = channel;
|
|
}
|
|
}
|
|
printf("channels to probe: %u-%u\n", channel_start, channel_stop);
|
|
}
|
|
|
|
/** set last channel of range to scan
|
|
* @param[in] argument optional pointer to last channel number
|
|
*/
|
|
static void command_channel_stop(void* argument)
|
|
{
|
|
if (argument) {
|
|
const uint32_t channel = *(uint32_t*)argument;
|
|
if (channel < CHANNEL_NUMBERS && channel > channel_start) {
|
|
channel_stop = channel;
|
|
}
|
|
}
|
|
printf("channels to probe: %u-%u\n", channel_start, channel_stop);
|
|
}
|
|
|
|
/** display available commands
|
|
* @param[in] argument no argument required
|
|
*/
|
|
static void command_help(void* argument);
|
|
|
|
/** show software and hardware version
|
|
* @param[in] argument no argument required
|
|
*/
|
|
static void command_version(void* argument)
|
|
{
|
|
(void)argument; // we won't use the argument
|
|
printf("firmware date: %04u-%02u-%02u\n", BUILD_YEAR, BUILD_MONTH, BUILD_DAY); // show firmware build date
|
|
printf("device serial: %08x%08x%08x\n", DESIG_UNIQUE_ID2, DESIG_UNIQUE_ID1, DESIG_UNIQUE_ID0); // show complete serial (different than the one used for USB)
|
|
}
|
|
|
|
/** convert RTC date/time to number of seconds
|
|
* @return number of seconds since 2000-01-01 00:00:00
|
|
* @warning for simplicity I consider every month to have 31 days
|
|
*/
|
|
static uint32_t rtc_to_seconds(void)
|
|
{
|
|
rtc_wait_for_synchro(); // wait until date/time is synchronised
|
|
const uint8_t year = ((RTC_DR >> RTC_DR_YT_SHIFT) & RTC_DR_YT_MASK) * 10 + ((RTC_DR >> RTC_DR_YU_SHIFT) & RTC_DR_YU_MASK); // get year
|
|
uint8_t month = ((RTC_DR >> RTC_DR_MT_SHIFT) & RTC_DR_MT_MASK) * 10 + ((RTC_DR >> RTC_DR_MU_SHIFT) & RTC_DR_MU_MASK); // get month
|
|
if (month > 0) { // month has been initialized, but starts with 1
|
|
month--; // fix for calculation
|
|
}
|
|
uint8_t day = ((RTC_DR >> RTC_DR_DT_SHIFT) & RTC_DR_DT_MASK) * 10 + ((RTC_DR >> RTC_DR_DU_SHIFT) & RTC_DR_DU_MASK); // get day
|
|
if (day > 0) { // day has been initialized, but starts with 1
|
|
day--; // fix for calculation
|
|
}
|
|
const uint8_t hour = ((RTC_TR >> RTC_TR_HT_SHIFT) & RTC_TR_HT_MASK) * 10 + ((RTC_TR >> RTC_TR_HU_SHIFT) & RTC_TR_HU_MASK); // get hours
|
|
const uint8_t minute = ((RTC_TR >> RTC_TR_MNT_SHIFT) & RTC_TR_MNT_MASK) * 10 + ((RTC_TR >> RTC_TR_MNU_SHIFT) & RTC_TR_MNU_MASK); // get minutes
|
|
const uint8_t second = ((RTC_TR >> RTC_TR_ST_SHIFT) & RTC_TR_ST_MASK) * 10 + ((RTC_TR >> RTC_TR_SU_SHIFT) & RTC_TR_SU_MASK); // get seconds
|
|
const uint32_t seconds = ((((((((year * 12) + month) * 31) + day) * 24) + hour) * 60) + minute) * 60 + second; // convert to number of seconds
|
|
return seconds;
|
|
}
|
|
|
|
/** show uptime
|
|
* @param[in] argument no argument required
|
|
*/
|
|
static void command_uptime(void* argument)
|
|
{
|
|
(void)argument; // we won't use the argument
|
|
const uint32_t uptime = rtc_to_seconds() - boot_time; // get time from internal RTC
|
|
printf("uptime: %u.%02u:%02u:%02u\n", uptime / (24 * 60 * 60), (uptime / (60 * 60)) % 24, (uptime / 60) % 60, uptime % 60);
|
|
}
|
|
|
|
/** reset board
|
|
* @param[in] argument no argument required
|
|
*/
|
|
static void command_reset(void* argument)
|
|
{
|
|
(void)argument; // we won't use the argument
|
|
scb_reset_system(); // reset device
|
|
while (true); // wait for the reset to happen
|
|
}
|
|
|
|
/** switch to system memory (e.g. embedded bootloader)
|
|
* @param[in] argument no argument required
|
|
*/
|
|
static void command_system(void* argument)
|
|
{
|
|
(void)argument; // we won't use the argument
|
|
system_memory(); // jump to system memory
|
|
}
|
|
|
|
/** switch to DFU bootloader
|
|
* @param[in] argument no argument required
|
|
*/
|
|
static void command_bootloader(void* argument)
|
|
{
|
|
(void)argument; // we won't use the argument
|
|
dfu_bootloader(); // start DFU bootloader
|
|
}
|
|
|
|
/** list of all supported commands */
|
|
static const struct menu_command_t menu_commands[] = {
|
|
{
|
|
.shortcut = 'h',
|
|
.name = "help",
|
|
.command_description = "display help",
|
|
.argument = MENU_ARGUMENT_NONE,
|
|
.argument_description = NULL,
|
|
.command_handler = &command_help,
|
|
},
|
|
{
|
|
.shortcut = 'V',
|
|
.name = "version",
|
|
.command_description = "show software and hardware version",
|
|
.argument = MENU_ARGUMENT_NONE,
|
|
.argument_description = NULL,
|
|
.command_handler = &command_version,
|
|
},
|
|
{
|
|
.shortcut = 'u',
|
|
.name = "uptime",
|
|
.command_description = "show uptime",
|
|
.argument = MENU_ARGUMENT_NONE,
|
|
.argument_description = NULL,
|
|
.command_handler = &command_uptime,
|
|
},
|
|
{
|
|
.shortcut = 'R',
|
|
.name = "board_reset",
|
|
.command_description = "reset board",
|
|
.argument = MENU_ARGUMENT_NONE,
|
|
.argument_description = NULL,
|
|
.command_handler = &command_reset,
|
|
},
|
|
{
|
|
.shortcut = 'S',
|
|
.name = "system",
|
|
.command_description = "reboot into system memory",
|
|
.argument = MENU_ARGUMENT_NONE,
|
|
.argument_description = NULL,
|
|
.command_handler = &command_system,
|
|
},
|
|
{
|
|
.shortcut = 'b',
|
|
.name = "bootloader",
|
|
.command_description = "reboot into DFU bootloader",
|
|
.argument = MENU_ARGUMENT_NONE,
|
|
.argument_description = NULL,
|
|
.command_handler = &command_bootloader,
|
|
},
|
|
{
|
|
.shortcut = 's',
|
|
.name = "swd",
|
|
.command_description = "scan for SWD interfaces",
|
|
.argument = MENU_ARGUMENT_NONE,
|
|
.argument_description = NULL,
|
|
.command_handler = &command_swd_scan,
|
|
},
|
|
{
|
|
.shortcut = 'j',
|
|
.name = "jtag",
|
|
.command_description = "scan for JTAG interfaces",
|
|
.argument = MENU_ARGUMENT_NONE,
|
|
.argument_description = NULL,
|
|
.command_handler = &command_jtag_scan,
|
|
},
|
|
{
|
|
.shortcut = 'v',
|
|
.name = "voltage",
|
|
.command_description = "set/measure target voltage",
|
|
.argument = MENU_ARGUMENT_UNSIGNED,
|
|
.argument_description = "[0|3|5]",
|
|
.command_handler = &command_target_voltage,
|
|
},
|
|
{
|
|
.shortcut = 'r',
|
|
.name = "reset",
|
|
.command_description = "configure/reset target board",
|
|
.argument = MENU_ARGUMENT_STRING,
|
|
.argument_description = "[0|1|ODL|ODH|PPL|PPH]",
|
|
.command_handler = &command_target_reset,
|
|
},
|
|
{
|
|
.shortcut = 'c',
|
|
.name = "start",
|
|
.command_description = "first channel of range to probe",
|
|
.argument = MENU_ARGUMENT_UNSIGNED,
|
|
.argument_description = "[ch]",
|
|
.command_handler = &command_channel_start,
|
|
},
|
|
{
|
|
.shortcut = 'C',
|
|
.name = "stop",
|
|
.command_description = "last channel of range to probe",
|
|
.argument = MENU_ARGUMENT_UNSIGNED,
|
|
.argument_description = "[ch]",
|
|
.command_handler = &command_channel_stop,
|
|
},
|
|
};
|
|
|
|
static void command_help(void* argument)
|
|
{
|
|
(void)argument; // we won't use the argument
|
|
printf("available commands:\n");
|
|
menu_print_commands(menu_commands, LENGTH(menu_commands)); // print global commands
|
|
}
|
|
|
|
/** process user command
|
|
* @param[in] str user command string (\0 ended)
|
|
*/
|
|
static void process_command(char* str)
|
|
{
|
|
// ensure actions are available
|
|
if (NULL == menu_commands || 0 == LENGTH(menu_commands)) {
|
|
return;
|
|
}
|
|
// don't handle empty lines
|
|
if (!str || 0 == strlen(str)) {
|
|
return;
|
|
}
|
|
bool command_handled = false;
|
|
if (!command_handled) {
|
|
command_handled = menu_handle_command(str, menu_commands, LENGTH(menu_commands)); // try if this is not a global command
|
|
}
|
|
if (!command_handled) {
|
|
printf("command not recognized. enter help to list commands\n");
|
|
}
|
|
}
|
|
|
|
/** program entry point
|
|
* this is the firmware function started by the micro-controller
|
|
*/
|
|
void main(void);
|
|
void main(void)
|
|
{
|
|
|
|
#if DEBUG
|
|
// enable functionalities for easier debug
|
|
DBGMCU_CR |= DBGMCU_CR_IWDG_STOP; // stop independent watchdog counter when code is halted
|
|
DBGMCU_CR |= DBGMCU_CR_WWDG_STOP; // stop window watchdog counter when code is halted
|
|
DBGMCU_CR |= DBGMCU_CR_STANDBY; // allow debug also in standby mode (keep digital part and clock powered)
|
|
DBGMCU_CR |= DBGMCU_CR_STOP; // allow debug also in stop mode (keep clock powered)
|
|
DBGMCU_CR |= DBGMCU_CR_SLEEP; // allow debug also in sleep mode (keep clock powered)
|
|
#else
|
|
// setup watchdog to reset in case we get stuck (i.e. when an error occurred)
|
|
iwdg_set_period_ms(WATCHDOG_PERIOD); // set independent watchdog period
|
|
iwdg_start(); // start independent watchdog
|
|
#endif
|
|
|
|
board_setup(); // setup board
|
|
usb_cdcacm_setup(); // setup USB CDC ACM (for printing)
|
|
puts("\nwelcome to the CuVoodoo SWJ finder\n"); // print welcome message
|
|
|
|
#if DEBUG
|
|
// show reset cause
|
|
if (RCC_CSR & (RCC_CSR_LPWRRSTF | RCC_CSR_WWDGRSTF | RCC_CSR_IWDGRSTF | RCC_CSR_SFTRSTF | RCC_CSR_PORRSTF | RCC_CSR_PINRSTF)) {
|
|
puts("reset cause(s):");
|
|
if (RCC_CSR & RCC_CSR_LPWRRSTF) {
|
|
puts(" low-power");
|
|
}
|
|
if (RCC_CSR & RCC_CSR_WWDGRSTF) {
|
|
puts(" window-watchdog");
|
|
}
|
|
if (RCC_CSR & RCC_CSR_IWDGRSTF) {
|
|
puts(" independent-watchdog");
|
|
}
|
|
if (RCC_CSR & RCC_CSR_SFTRSTF) {
|
|
puts(" software");
|
|
}
|
|
if (RCC_CSR & RCC_CSR_PORRSTF) {
|
|
puts(" POR/PDR");
|
|
}
|
|
if (RCC_CSR & RCC_CSR_PINRSTF) {
|
|
puts(" pin");
|
|
}
|
|
putc('\n');
|
|
RCC_CSR |= RCC_CSR_RMVF; // clear reset flags
|
|
}
|
|
#endif
|
|
#if !(DEBUG)
|
|
// show watchdog information
|
|
printf("setup watchdog: %.2fs", WATCHDOG_PERIOD / 1000.0);
|
|
if (FLASH_OBR & FLASH_OBR_OPTERR) {
|
|
puts(" (option bytes not set in flash: software watchdog used, not automatically started at reset)\n");
|
|
} else if (FLASH_OBR & FLASH_OBR_WDG_SW) {
|
|
puts(" (software watchdog used, not automatically started at reset)\n");
|
|
} else {
|
|
puts(" (hardware watchdog used, automatically started at reset)\n");
|
|
}
|
|
#endif
|
|
|
|
// setup RTC
|
|
puts("setup RTC: ");
|
|
rcc_periph_clock_enable(RCC_RTC); // enable clock for RTC peripheral
|
|
if (!(RCC_BDCR && RCC_BDCR_RTCEN)) { // the RTC has not been configured yet
|
|
pwr_disable_backup_domain_write_protect(); // disable backup protection so we can set the RTC clock source
|
|
rtc_unlock(); // enable writing RTC registers
|
|
#if defined(MINIF401)
|
|
rcc_osc_on(RCC_LSE); // enable LSE clock
|
|
while (!rcc_is_osc_ready(RCC_LSE)); // wait until clock is ready
|
|
rtc_set_prescaler(256, 128); // set clock prescaler to 32768
|
|
RCC_BDCR = (RCC_BDCR & ~(RCC_BDCR_RTCSEL_MASK << RCC_BDCR_RTCSEL_SHIFT)) | (RCC_BDCR_RTCSEL_LSE << RCC_BDCR_RTCSEL_SHIFT); // select LSE as RTC clock source
|
|
#else
|
|
rcc_osc_on(RCC_LSI); // enable LSI clock
|
|
while (!rcc_is_osc_ready(RCC_LSI)); // wait until clock is ready
|
|
rtc_set_prescaler(250, 128); // set clock prescaler to 32000
|
|
RCC_BDCR = (RCC_BDCR & ~(RCC_BDCR_RTCSEL_MASK << RCC_BDCR_RTCSEL_SHIFT)) | (RCC_BDCR_RTCSEL_LSI << RCC_BDCR_RTCSEL_SHIFT); // select LSI as RTC clock source
|
|
#endif
|
|
RCC_BDCR |= RCC_BDCR_RTCEN; // enable RTC
|
|
rtc_lock(); // protect RTC register against writing
|
|
pwr_enable_backup_domain_write_protect(); // re-enable protection now that we configured the RTC clock
|
|
}
|
|
boot_time = rtc_to_seconds(); // remember the start time
|
|
puts("OK\n");
|
|
|
|
// setup wakeup timer for periodic checks
|
|
puts("setup wakeup: ");
|
|
// RTC needs to be configured beforehand
|
|
pwr_disable_backup_domain_write_protect(); // disable backup protection so we can write to the RTC registers
|
|
rtc_unlock(); // enable writing RTC registers
|
|
rtc_clear_wakeup_flag(); // clear flag for fresh start
|
|
#if defined(MINIF401)
|
|
rtc_set_wakeup_time((32768 / 2) / WAKEUP_FREQ - 1, RTC_CR_WUCLKSEL_RTC_DIV2); // set wakeup time based on LSE (keep highest precision, also enables the wakeup timer)
|
|
#else
|
|
rtc_set_wakeup_time((32000 / 2) / WAKEUP_FREQ - 1, RTC_CR_WUCLKSEL_RTC_DIV2); // set wakeup time based on LSI (keep highest precision, also enables the wakeup timer)
|
|
#endif
|
|
rtc_enable_wakeup_timer_interrupt(); // enable interrupt
|
|
rtc_lock(); // disable writing RTC registers
|
|
// important: do not re-enable backup_domain_write_protect, since this will prevent clearing flags (but RTC registers do not need to be unlocked)
|
|
puts("OK\n");
|
|
|
|
puts("setup voltage control: ");
|
|
rcc_periph_clock_enable(GPIO_RCC(TARGET_EN)); // enable clock for port domain
|
|
gpio_set(GPIO_PORT(TARGET_EN), GPIO_PIN(TARGET_EN)); // ensure we do not enable pMOS to power level shifters
|
|
gpio_set_output_options(GPIO_PORT(TARGET_EN), GPIO_OTYPE_OD, GPIO_OSPEED_2MHZ, GPIO_PIN(TARGET_EN)); // set output as open-drain
|
|
gpio_mode_setup(GPIO_PORT(TARGET_EN), GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN(TARGET_EN)); // configure pin as output
|
|
rcc_periph_clock_enable(GPIO_RCC(TARGET_3V)); // enable clock for port domain
|
|
gpio_set(GPIO_PORT(TARGET_3V), GPIO_PIN(TARGET_3V)); // ensure we do not enable pMOS to provide voltage
|
|
gpio_set_output_options(GPIO_PORT(TARGET_3V), GPIO_OTYPE_OD, GPIO_OSPEED_2MHZ, GPIO_PIN(TARGET_3V)); // set output as open-drain
|
|
gpio_mode_setup(GPIO_PORT(TARGET_3V), GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN(TARGET_3V)); // configure pin as output
|
|
rcc_periph_clock_enable(GPIO_RCC(TARGET_5V)); // enable clock for port domain
|
|
gpio_set(GPIO_PORT(TARGET_5V), GPIO_PIN(TARGET_5V)); // ensure we do not enable pMOS to provide voltage
|
|
gpio_set_output_options(GPIO_PORT(TARGET_5V), GPIO_OTYPE_OD, GPIO_OSPEED_2MHZ, GPIO_PIN(TARGET_5V)); // set output as open-drain
|
|
gpio_mode_setup(GPIO_PORT(TARGET_5V), GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN(TARGET_5V)); // configure pin as output
|
|
rcc_periph_clock_enable(GPIO_RCC(TARGET_RST)); // enable clock for port domain
|
|
gpio_set(GPIO_PORT(TARGET_RST), GPIO_PIN(TARGET_RST)); // ensure reset is not pulled low
|
|
gpio_set_output_options(GPIO_PORT(TARGET_RST), GPIO_OTYPE_OD, GPIO_OSPEED_2MHZ, GPIO_PIN(TARGET_RST)); // set output as open-drain
|
|
gpio_mode_setup(GPIO_PORT(TARGET_RST), GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN(TARGET_RST)); // configure pin as output
|
|
puts("OK\n");
|
|
|
|
puts("setup signal pins: ");
|
|
for (uint8_t i = 0; i < CHANNEL_NUMBERS; i++) {
|
|
rcc_periph_clock_enable(port2rcc(channel_ports[i])); // enable clock for port domain
|
|
gpio_mode_setup(channel_ports[i], GPIO_MODE_INPUT, GPIO_PUPD_NONE, channel_pins[i]); // ensure pin is floating input
|
|
}
|
|
puts("OK\n");
|
|
|
|
puts("setup ADC to measure voltages: ");
|
|
rcc_periph_clock_enable(RCC_ADC1); // enable clock for ADC domain
|
|
adc_power_off(ADC1); // switch off ADC while configuring it
|
|
adc_set_right_aligned(ADC1); // ensure it is right aligned to get the actual value in the 16-bit register
|
|
adc_enable_scan_mode(ADC1); // use scan mode do be able to go to next discontinuous subgroup of the regular sequence
|
|
adc_enable_discontinuous_mode_regular(ADC1, 1); // use discontinuous mode (to go through all channels of the group, one after another)
|
|
adc_set_single_conversion_mode(ADC1); // ensure continuous mode is not used (that's not the same as discontinuous)
|
|
adc_eoc_after_each(ADC1); // set EOC after each conversion instead of each group
|
|
adc_set_sample_time_on_all_channels(ADC1, ADC_SMPR_SMP_28CYC); // use at least 15 cycles to be able to sample at 12-bit resolution
|
|
adc_set_regular_sequence(ADC1, LENGTH(adc_channels), (uint8_t*)adc_channels); // set channel to convert
|
|
adc_enable_temperature_sensor(); // enable internal voltage reference
|
|
adc_power_on(ADC1); // switch on ADC
|
|
sleep_us(3); // wait t_stab for the ADC to stabilize
|
|
rcc_periph_clock_enable(RCC_ADC1_IN(TARGET_CHANNEL)); // enable clock for GPIO domain for target voltage channel
|
|
gpio_mode_setup(ADC1_IN_PORT(TARGET_CHANNEL), GPIO_MODE_ANALOG, GPIO_PUPD_NONE, ADC1_IN_PIN(TARGET_CHANNEL)); // set target voltage channel as analog input for the ADC
|
|
measure_voltages(); // try to measure voltages
|
|
puts("OK\n");
|
|
|
|
puts("setup SWD: ");
|
|
if (!swd_set_pins(GPIO_PORT(PB10), GPIO_PIN(PB10), GPIO_PORT(PB2), GPIO_PIN(PB2))) {
|
|
puts("unknown pins\n");
|
|
} else {
|
|
swd_setup(50000); // setup SWD clock to 50 KHz, slow enough for any target and loose connection
|
|
puts("OK\n");
|
|
}
|
|
|
|
// setup terminal
|
|
terminal_prefix = ""; // set default prefix
|
|
terminal_process = &process_command; // set central function to process commands
|
|
terminal_setup(); // start terminal
|
|
|
|
// start main loop
|
|
bool action = false; // if an action has been performed don't go to sleep
|
|
button_flag = false; // reset button flag
|
|
led_on(); // switch LED to indicate booting completed
|
|
while (true) { // infinite loop
|
|
iwdg_reset(); // kick the dog
|
|
if (user_input_available) { // user input is available
|
|
action = true; // action has been performed
|
|
led_toggle(); // toggle LED
|
|
char c = user_input_get(); // store receive character
|
|
terminal_send(c); // send received character to terminal
|
|
}
|
|
if (button_flag) { // user pressed button
|
|
action = true; // action has been performed
|
|
puts("button pressed\n");
|
|
led_toggle(); // toggle LED
|
|
sleep_ms(100); // wait a bit to remove noise and double trigger
|
|
button_flag = false; // reset flag
|
|
}
|
|
if (wakeup_flag) { // time to do periodic checks
|
|
wakeup_flag = false; // clear flag
|
|
}
|
|
if (second_flag) { // one second passed
|
|
second_flag = false; // clear flag
|
|
led_toggle(); // toggle LED to indicate if main function is stuck
|
|
}
|
|
if (action) { // go to sleep if nothing had to be done, else recheck for activity
|
|
action = false;
|
|
} else {
|
|
__WFI(); // go to sleep
|
|
}
|
|
} // main loop
|
|
}
|
|
|
|
/** interrupt service routine when the wakeup timer triggered */
|
|
void rtc_wkup_isr(void)
|
|
{
|
|
static uint16_t tick = WAKEUP_FREQ; // how many wakeup have occurred
|
|
exti_reset_request(EXTI22); // clear EXTI flag used by wakeup
|
|
rtc_clear_wakeup_flag(); // clear flag
|
|
wakeup_flag = true; // notify main loop
|
|
tick--; // count the number of ticks down (do it in the ISR to no miss any tick)
|
|
if (0 == tick) { // count down completed
|
|
second_flag = true; // notify main loop a second has passed
|
|
tick = WAKEUP_FREQ; // restart count down
|
|
}
|
|
}
|