From 17206167de83673f1927a15b94ec4131adcf195e Mon Sep 17 00:00:00 2001 From: blah Date: Sun, 24 Apr 2016 14:40:20 -0700 Subject: [PATCH 1/9] Added magnetometer ADC read --- firmware/src/magnet/magnet.cpp | 6 +++--- firmware/src/main.cpp | 2 ++ firmware/src/sys/board.cpp | 18 ++++++++++++++++++ firmware/src/sys/board.hpp | 2 ++ 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/firmware/src/magnet/magnet.cpp b/firmware/src/magnet/magnet.cpp index ce3b5b3..04fa2ca 100644 --- a/firmware/src/magnet/magnet.cpp +++ b/firmware/src/magnet/magnet.cpp @@ -116,7 +116,7 @@ void pollOn() { if (!chrg.isConstructed()) { - board::syslog("Mag ON chrg started\r\n"); +// board::syslog("Mag ON chrg started\r\n"); chrg.construct(475); } @@ -152,7 +152,7 @@ void pollOff() if (!chrg.isConstructed()) { - board::syslog("Mag OFF chrg started cyc ", cycle_index, "\r\n"); +// board::syslog("Mag OFF chrg started cyc ", cycle_index, "\r\n"); chrg.construct(cycle_array_item[0]); } @@ -206,7 +206,7 @@ void turnOn(unsigned num_cycles) num_cycles = std::min(MaxCycles, num_cycles); remaining_cycles = int(num_cycles); - board::syslog("Mag on ", remaining_cycles, "\r\n"); +// board::syslog("Mag on ", remaining_cycles, "\r\n"); } } diff --git a/firmware/src/main.cpp b/firmware/src/main.cpp index 4fb92f8..ed2c63a 100644 --- a/firmware/src/main.cpp +++ b/firmware/src/main.cpp @@ -115,7 +115,9 @@ void callPollAndResetWatchdog() /* Debug * board::syslog("Vin = ", board::getSupplyVoltageInMillivolts(), " mV\r\n"); * board::syslog("Vout = ", board::getOutVoltageInVolts(), " V\r\n"); + * */ + board::syslog("Mag = ", board::getMagInMilliTeslas(), " V\r\n"); } /* diff --git a/firmware/src/sys/board.cpp b/firmware/src/sys/board.cpp index 7a8ed92..0a7eb7e 100644 --- a/firmware/src/sys/board.cpp +++ b/firmware/src/sys/board.cpp @@ -112,6 +112,8 @@ constexpr PinMuxGroup pinmux[] = // PIO1 { IOCON_PIO1_10, IOCON_FUNC1 | IOCON_MODE_INACT | IOCON_ADMODE_EN |IOCON_OPENDRAIN_EN }, // Vin_ADC + { IOCON_PIO1_11, IOCON_FUNC1 | IOCON_MODE_INACT | IOCON_ADMODE_EN |IOCON_OPENDRAIN_EN }, // Mag_ADC + { IOCON_PIO1_7, IOCON_FUNC1 | IOCON_HYS_EN | IOCON_MODE_PULLUP }, // UART_TXD #if BOARD_OLIMEX_LPC_P11C24 { IOCON_PIO1_11, IOCON_FUNC0 | IOCON_HYS_EN | IOCON_DIGMODE_EN }, // CAN LED @@ -264,6 +266,7 @@ void initAdc() Chip_ADC_SetSampleRate(LPC_ADC, &clock, SamplesPerSecond); Chip_ADC_EnableChannel(LPC_ADC, ADC_CH6, ENABLE); // Vin + Chip_ADC_EnableChannel(LPC_ADC, ADC_CH7, ENABLE); // Magnetometer Chip_ADC_EnableChannel(LPC_ADC, ADC_CH0, ENABLE); // Vout Chip_ADC_SetBurstCmd(LPC_ADC, ENABLE); } @@ -466,6 +469,21 @@ bool hadButtonPressEvent() } } + +unsigned getMagInMilliTeslas() //error under 2% +{ + + std::uint16_t new_value = 0; + (void)Chip_ADC_ReadValue(LPC_ADC, ADC_CH7, &new_value); + + // Division and multiplication by 2 are reduced + unsigned x = static_cast((static_cast(new_value) * AdcReferenceMillivolts) >> + AdcResolutionBits); + + + return x; +} + unsigned getSupplyVoltageInMillivolts() //error under 2% { static std::uint16_t old_value = 0; diff --git a/firmware/src/sys/board.hpp b/firmware/src/sys/board.hpp index 9efbb9c..cdccacc 100644 --- a/firmware/src/sys/board.hpp +++ b/firmware/src/sys/board.hpp @@ -85,6 +85,8 @@ unsigned getSupplyVoltageInMillivolts(); unsigned getOutVoltageInVolts(); +unsigned getMagInMilliTeslas(); + /** * Status of the PWM input. */ From 75b97967a2d4929ba893d933463258aeae259064 Mon Sep 17 00:00:00 2001 From: blah Date: Mon, 25 Apr 2016 18:52:15 -0700 Subject: [PATCH 2/9] call mag calibration function when button is pressed on boot --- firmware/src/main.cpp | 19 +++++++++++++------ firmware/src/sys/board.cpp | 10 ++++++++++ firmware/src/sys/board.hpp | 4 ++++ 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/firmware/src/main.cpp b/firmware/src/main.cpp index ed2c63a..e237efc 100644 --- a/firmware/src/main.cpp +++ b/firmware/src/main.cpp @@ -75,7 +75,7 @@ void callPollAndResetWatchdog() static board::MonotonicTime led_update_deadline = ts; static bool led_status = false; - static bool boot = true; + static bool boot = 1; static bool first_time_led_update = true; /* @@ -84,12 +84,19 @@ void callPollAndResetWatchdog() if (boot) { boot = !boot; - if(board::readDipSwitch() == 0) + if (board::isButtonPressed()) { - board::setStatusLed(true); - board::setCanLed(true); - led_update_deadline += board::MonotonicDuration::fromMSec(500); - } + board::calibrateMagnetometer(); + }else{ + + if(board::readDipSwitch() == 0) + { + board::setStatusLed(true); + board::setCanLed(true); + led_update_deadline += board::MonotonicDuration::fromMSec(500); + } + } + } if (ts >= led_update_deadline) diff --git a/firmware/src/sys/board.cpp b/firmware/src/sys/board.cpp index 0a7eb7e..4d4f47a 100644 --- a/firmware/src/sys/board.cpp +++ b/firmware/src/sys/board.cpp @@ -468,7 +468,17 @@ bool hadButtonPressEvent() return had_press; } } +bool isButtonPressed(){ + return gpio::get(StatusLedPortNum, StatusLedPinMask); +} + +unsigned calibrateMagnetometer() +{ + board::syslog("Calibrating \r\n"); + board::syslog("Mag = ", board::getMagInMilliTeslas(), " V\r\n"); + return true; +} unsigned getMagInMilliTeslas() //error under 2% { diff --git a/firmware/src/sys/board.hpp b/firmware/src/sys/board.hpp index cdccacc..9d1b38b 100644 --- a/firmware/src/sys/board.hpp +++ b/firmware/src/sys/board.hpp @@ -81,12 +81,16 @@ std::uint8_t readDipSwitch(); */ bool hadButtonPressEvent(); +bool isButtonPressed(); + unsigned getSupplyVoltageInMillivolts(); unsigned getOutVoltageInVolts(); unsigned getMagInMilliTeslas(); +unsigned calibrateMagnetometer(); + /** * Status of the PWM input. */ From 9feac2214397f6d63d18e0d9f13591eb83aa39f9 Mon Sep 17 00:00:00 2001 From: blah Date: Tue, 26 Apr 2016 13:06:35 -0700 Subject: [PATCH 3/9] added magnetometer detection function --- firmware/src/main.cpp | 2 +- firmware/src/sys/board.cpp | 13 ++++++++++++- firmware/src/sys/board.hpp | 2 ++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/firmware/src/main.cpp b/firmware/src/main.cpp index e237efc..0bec74c 100644 --- a/firmware/src/main.cpp +++ b/firmware/src/main.cpp @@ -84,7 +84,7 @@ void callPollAndResetWatchdog() if (boot) { boot = !boot; - if (board::isButtonPressed()) + if (board::isButtonPressed() && board::isMagPresent()) { board::calibrateMagnetometer(); }else{ diff --git a/firmware/src/sys/board.cpp b/firmware/src/sys/board.cpp index 4d4f47a..f431a38 100644 --- a/firmware/src/sys/board.cpp +++ b/firmware/src/sys/board.cpp @@ -112,7 +112,7 @@ constexpr PinMuxGroup pinmux[] = // PIO1 { IOCON_PIO1_10, IOCON_FUNC1 | IOCON_MODE_INACT | IOCON_ADMODE_EN |IOCON_OPENDRAIN_EN }, // Vin_ADC - { IOCON_PIO1_11, IOCON_FUNC1 | IOCON_MODE_INACT | IOCON_ADMODE_EN |IOCON_OPENDRAIN_EN }, // Mag_ADC + { IOCON_PIO1_11, IOCON_FUNC1 | IOCON_MODE_PULLDOWN | IOCON_ADMODE_EN |IOCON_OPENDRAIN_EN }, // Mag_ADC { IOCON_PIO1_7, IOCON_FUNC1 | IOCON_HYS_EN | IOCON_MODE_PULLUP }, // UART_TXD #if BOARD_OLIMEX_LPC_P11C24 @@ -494,6 +494,17 @@ unsigned getMagInMilliTeslas() //error under 2% return x; } +bool isMagPresent() +{ + std::uint16_t new_value = 0; + (void)Chip_ADC_ReadValue(LPC_ADC, ADC_CH7, &new_value); + unsigned x = static_cast((static_cast(new_value) * AdcReferenceMillivolts) >> + AdcResolutionBits); + if(x < 100) + return false; + return true; +} + unsigned getSupplyVoltageInMillivolts() //error under 2% { static std::uint16_t old_value = 0; diff --git a/firmware/src/sys/board.hpp b/firmware/src/sys/board.hpp index 9d1b38b..6323f00 100644 --- a/firmware/src/sys/board.hpp +++ b/firmware/src/sys/board.hpp @@ -89,6 +89,8 @@ unsigned getOutVoltageInVolts(); unsigned getMagInMilliTeslas(); +bool isMagPresent(); + unsigned calibrateMagnetometer(); /** From 2238a41dd706b4fce4a841ba2e5a9acca542f6dc Mon Sep 17 00:00:00 2001 From: Pavel Kirienko Date: Wed, 4 May 2016 09:55:14 +0300 Subject: [PATCH 4/9] Styling fixes --- firmware/src/magnet/magnet.cpp | 6 +++--- firmware/src/main.cpp | 15 +++++++-------- firmware/src/sys/board.cpp | 26 +++++++++++++------------- firmware/src/sys/board.hpp | 2 +- 4 files changed, 24 insertions(+), 25 deletions(-) diff --git a/firmware/src/magnet/magnet.cpp b/firmware/src/magnet/magnet.cpp index 04fa2ca..ce3b5b3 100644 --- a/firmware/src/magnet/magnet.cpp +++ b/firmware/src/magnet/magnet.cpp @@ -116,7 +116,7 @@ void pollOn() { if (!chrg.isConstructed()) { -// board::syslog("Mag ON chrg started\r\n"); + board::syslog("Mag ON chrg started\r\n"); chrg.construct(475); } @@ -152,7 +152,7 @@ void pollOff() if (!chrg.isConstructed()) { -// board::syslog("Mag OFF chrg started cyc ", cycle_index, "\r\n"); + board::syslog("Mag OFF chrg started cyc ", cycle_index, "\r\n"); chrg.construct(cycle_array_item[0]); } @@ -206,7 +206,7 @@ void turnOn(unsigned num_cycles) num_cycles = std::min(MaxCycles, num_cycles); remaining_cycles = int(num_cycles); -// board::syslog("Mag on ", remaining_cycles, "\r\n"); + board::syslog("Mag on ", remaining_cycles, "\r\n"); } } diff --git a/firmware/src/main.cpp b/firmware/src/main.cpp index 0bec74c..8c8a22f 100644 --- a/firmware/src/main.cpp +++ b/firmware/src/main.cpp @@ -75,7 +75,7 @@ void callPollAndResetWatchdog() static board::MonotonicTime led_update_deadline = ts; static bool led_status = false; - static bool boot = 1; + static bool boot = true; static bool first_time_led_update = true; /* @@ -84,19 +84,19 @@ void callPollAndResetWatchdog() if (boot) { boot = !boot; - if (board::isButtonPressed() && board::isMagPresent()) + if (board::isButtonCurrentlyPressed() && board::isMagPresent()) { board::calibrateMagnetometer(); - }else{ - - if(board::readDipSwitch() == 0) + } + else + { + if (board::readDipSwitch() == 0) { board::setStatusLed(true); board::setCanLed(true); led_update_deadline += board::MonotonicDuration::fromMSec(500); } - } - + } } if (ts >= led_update_deadline) @@ -122,7 +122,6 @@ void callPollAndResetWatchdog() /* Debug * board::syslog("Vin = ", board::getSupplyVoltageInMillivolts(), " mV\r\n"); * board::syslog("Vout = ", board::getOutVoltageInVolts(), " V\r\n"); - * */ board::syslog("Mag = ", board::getMagInMilliTeslas(), " V\r\n"); } diff --git a/firmware/src/sys/board.cpp b/firmware/src/sys/board.cpp index f431a38..9380c7b 100644 --- a/firmware/src/sys/board.cpp +++ b/firmware/src/sys/board.cpp @@ -112,7 +112,7 @@ constexpr PinMuxGroup pinmux[] = // PIO1 { IOCON_PIO1_10, IOCON_FUNC1 | IOCON_MODE_INACT | IOCON_ADMODE_EN |IOCON_OPENDRAIN_EN }, // Vin_ADC - { IOCON_PIO1_11, IOCON_FUNC1 | IOCON_MODE_PULLDOWN | IOCON_ADMODE_EN |IOCON_OPENDRAIN_EN }, // Mag_ADC + { IOCON_PIO1_11, IOCON_FUNC1 | IOCON_MODE_PULLDOWN | IOCON_ADMODE_EN |IOCON_OPENDRAIN_EN }, // Mag_ADC { IOCON_PIO1_7, IOCON_FUNC1 | IOCON_HYS_EN | IOCON_MODE_PULLUP }, // UART_TXD #if BOARD_OLIMEX_LPC_P11C24 @@ -468,8 +468,10 @@ bool hadButtonPressEvent() return had_press; } } -bool isButtonPressed(){ - return gpio::get(StatusLedPortNum, StatusLedPinMask); + +bool isButtonCurrentlyPressed() +{ + return gpio::get(StatusLedPortNum, StatusLedPinMask) != 0; } unsigned calibrateMagnetometer() @@ -482,27 +484,25 @@ unsigned calibrateMagnetometer() unsigned getMagInMilliTeslas() //error under 2% { - std::uint16_t new_value = 0; (void)Chip_ADC_ReadValue(LPC_ADC, ADC_CH7, &new_value); - // Division and multiplication by 2 are reduced - unsigned x = static_cast((static_cast(new_value) * AdcReferenceMillivolts) >> - AdcResolutionBits); - + unsigned x = + static_cast((static_cast(new_value) * AdcReferenceMillivolts) >> AdcResolutionBits); return x; } bool isMagPresent() { + // TODO: this largely duplicates getMagInMilliTeslas(), optimize? std::uint16_t new_value = 0; (void)Chip_ADC_ReadValue(LPC_ADC, ADC_CH7, &new_value); - unsigned x = static_cast((static_cast(new_value) * AdcReferenceMillivolts) >> - AdcResolutionBits); - if(x < 100) - return false; - return true; + + unsigned x = + static_cast((static_cast(new_value) * AdcReferenceMillivolts) >> AdcResolutionBits); + + return x >= 100; } unsigned getSupplyVoltageInMillivolts() //error under 2% diff --git a/firmware/src/sys/board.hpp b/firmware/src/sys/board.hpp index 6323f00..112d025 100644 --- a/firmware/src/sys/board.hpp +++ b/firmware/src/sys/board.hpp @@ -81,7 +81,7 @@ std::uint8_t readDipSwitch(); */ bool hadButtonPressEvent(); -bool isButtonPressed(); +bool isButtonCurrentlyPressed(); unsigned getSupplyVoltageInMillivolts(); From bf24fe64ce97289cdcaf3b39ed4f07d63a38e37d Mon Sep 17 00:00:00 2001 From: Pavel Kirienko Date: Wed, 4 May 2016 10:07:23 +0300 Subject: [PATCH 5/9] Cleaner naming --- firmware/src/main.cpp | 4 ++-- firmware/src/sys/board.cpp | 6 +++--- firmware/src/sys/board.hpp | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/firmware/src/main.cpp b/firmware/src/main.cpp index 8c8a22f..403963a 100644 --- a/firmware/src/main.cpp +++ b/firmware/src/main.cpp @@ -84,7 +84,7 @@ void callPollAndResetWatchdog() if (boot) { boot = !boot; - if (board::isButtonCurrentlyPressed() && board::isMagPresent()) + if (board::isButtonCurrentlyPressed() && board::isMagnetometerPresent()) { board::calibrateMagnetometer(); } @@ -123,7 +123,7 @@ void callPollAndResetWatchdog() * board::syslog("Vin = ", board::getSupplyVoltageInMillivolts(), " mV\r\n"); * board::syslog("Vout = ", board::getOutVoltageInVolts(), " V\r\n"); */ - board::syslog("Mag = ", board::getMagInMilliTeslas(), " V\r\n"); + board::syslog("Mag = ", board::getMagneticFieldStrengthInMilliTeslas(), " V\r\n"); } /* diff --git a/firmware/src/sys/board.cpp b/firmware/src/sys/board.cpp index 9380c7b..de44933 100644 --- a/firmware/src/sys/board.cpp +++ b/firmware/src/sys/board.cpp @@ -477,12 +477,12 @@ bool isButtonCurrentlyPressed() unsigned calibrateMagnetometer() { board::syslog("Calibrating \r\n"); - board::syslog("Mag = ", board::getMagInMilliTeslas(), " V\r\n"); + board::syslog("Mag = ", board::getMagneticFieldStrengthInMilliTeslas(), " V\r\n"); return true; } -unsigned getMagInMilliTeslas() //error under 2% +unsigned getMagneticFieldStrengthInMilliTeslas() //error under 2% { std::uint16_t new_value = 0; (void)Chip_ADC_ReadValue(LPC_ADC, ADC_CH7, &new_value); @@ -493,7 +493,7 @@ unsigned getMagInMilliTeslas() //error under 2% return x; } -bool isMagPresent() +bool isMagnetometerPresent() { // TODO: this largely duplicates getMagInMilliTeslas(), optimize? std::uint16_t new_value = 0; diff --git a/firmware/src/sys/board.hpp b/firmware/src/sys/board.hpp index 112d025..63406cc 100644 --- a/firmware/src/sys/board.hpp +++ b/firmware/src/sys/board.hpp @@ -87,9 +87,9 @@ unsigned getSupplyVoltageInMillivolts(); unsigned getOutVoltageInVolts(); -unsigned getMagInMilliTeslas(); +unsigned getMagneticFieldStrengthInMilliTeslas(); -bool isMagPresent(); +bool isMagnetometerPresent(); unsigned calibrateMagnetometer(); From ee65f1fe00c3fda4d93c22b1446d4c64aad39212 Mon Sep 17 00:00:00 2001 From: Pavel Kirienko Date: Wed, 4 May 2016 13:59:11 +0300 Subject: [PATCH 6/9] Flash write routine --- firmware/lpc11c24.ld | 3 +- firmware/src/sys/board.cpp | 91 ++++++++++++++++++++++++++++++++++---- 2 files changed, 85 insertions(+), 9 deletions(-) diff --git a/firmware/lpc11c24.ld b/firmware/lpc11c24.ld index f7f292e..96caf77 100644 --- a/firmware/lpc11c24.ld +++ b/firmware/lpc11c24.ld @@ -8,7 +8,8 @@ MEMORY { - FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 32K + /* 28K for firmware, the last 4K sector is reserved for nonvolatile storage */ + FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 28K /* Notice RAM offset - this is needed for on-chip CCAN */ RAM (rwx) : ORIGIN = 0x100000C0, LENGTH = 0x1F40 } diff --git a/firmware/src/sys/board.cpp b/firmware/src/sys/board.cpp index de44933..d5b01f7 100644 --- a/firmware/src/sys/board.cpp +++ b/firmware/src/sys/board.cpp @@ -296,6 +296,79 @@ void init() resetWatchdog(); } +static const unsigned FlashStorageSize = 256; +static const unsigned FlashStorageAddress = 32768 - FlashStorageSize; +static const unsigned FlashStorageSectorNumber = 7; + +static const unsigned FlashStorageOffsetDeviceSignature = FlashStorageSize - std::tuple_size::value; +static const unsigned FlashStorageOffsetMagnetometerCalibration = FlashStorageOffsetDeviceSignature - 8; + +void readFlashStorage(unsigned offset, unsigned length, void* output) +{ + std::memcpy(output, + reinterpret_cast(FlashStorageAddress + offset), + length); +} + +int writeFlashStorage(unsigned offset, unsigned length, const void* data) +{ + // Sanity check + if (length == 0 || (length + offset) > FlashStorageSize) + { + return -1000; + } + + // Reading contents from flash into a temporary RAM buffer (which must be word aligned) + alignas(long long) std::uint8_t buffer[FlashStorageSize]; + std::memcpy(buffer, reinterpret_cast(FlashStorageAddress), FlashStorageSize); + + // Modifying the temporary buffer accordingly + std::memmove(buffer + offset, data, length); + + // IAP commands go here + unsigned iap_command = 0; + unsigned iap_args[5] = {}; + const auto iap_entry_point = reinterpret_cast(0x1FFF1FF1); + + // Erasing the sector + iap_command = 52; + iap_args[0] = FlashStorageSectorNumber; // Start sector + iap_args[1] = FlashStorageSectorNumber; // End sector + iap_args[2] = SystemCoreClock / 1000; // System clock in kHz + iap_entry_point(&iap_command, iap_args); + + if (iap_args[0] != 0) + { + return -int(iap_args[0]); + } + + // Preparing the sector for write + iap_command = 50; + iap_args[0] = FlashStorageSectorNumber; // Start sector + iap_args[1] = FlashStorageSectorNumber; // End sector + iap_entry_point(&iap_command, iap_args); + + if (iap_args[0] != 0) + { + return -int(iap_args[0]); + } + + // Writing + iap_command = 51; + iap_args[0] = FlashStorageAddress; // Destination + iap_args[1] = reinterpret_cast(&buffer); // Source + iap_args[2] = FlashStorageSize; // Size + iap_args[3] = SystemCoreClock / 1000; // System clock in kHz + iap_entry_point(&iap_command, iap_args); + + if (iap_args[0] != 0) + { + return -int(iap_args[0]); + } + + return 0; +} + } // namespace void die() @@ -316,11 +389,9 @@ void readUniqueID(UniqueID& out_uid) bool tryReadDeviceSignature(DeviceSignature& out_signature) { - static const unsigned SignatureAddress = 32768 - std::tuple_size::value; // End of flash - - std::memcpy(out_signature.data(), - reinterpret_cast(SignatureAddress), - std::tuple_size::value); + readFlashStorage(FlashStorageOffsetDeviceSignature, + std::tuple_size::value, + out_signature.data()); for (auto x : out_signature) { @@ -476,10 +547,14 @@ bool isButtonCurrentlyPressed() unsigned calibrateMagnetometer() { - board::syslog("Calibrating \r\n"); - board::syslog("Mag = ", board::getMagneticFieldStrengthInMilliTeslas(), " V\r\n"); + board::syslog("Calibrating\r\n"); + + const unsigned value = board::getMagneticFieldStrengthInMilliTeslas(); + board::syslog("Mag = ", value, " V\r\n"); + + writeFlashStorage(FlashStorageOffsetMagnetometerCalibration, sizeof(value), &value); - return true; + return value; } unsigned getMagneticFieldStrengthInMilliTeslas() //error under 2% From 9162734b346f38672952f55ed67c3a0b9ecb7456 Mon Sep 17 00:00:00 2001 From: Pavel Kirienko Date: Thu, 5 May 2016 04:10:51 +0300 Subject: [PATCH 7/9] Minor correction --- firmware/src/main.cpp | 2 +- firmware/src/sys/board.cpp | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/firmware/src/main.cpp b/firmware/src/main.cpp index 403963a..797200e 100644 --- a/firmware/src/main.cpp +++ b/firmware/src/main.cpp @@ -101,7 +101,7 @@ void callPollAndResetWatchdog() if (ts >= led_update_deadline) { - if(first_time_led_update) // Turn off CAN status + if (first_time_led_update) // Turn off CAN status { board::setCanLed(false); first_time_led_update = !first_time_led_update; diff --git a/firmware/src/sys/board.cpp b/firmware/src/sys/board.cpp index d5b01f7..312f2ef 100644 --- a/firmware/src/sys/board.cpp +++ b/firmware/src/sys/board.cpp @@ -542,6 +542,10 @@ bool hadButtonPressEvent() bool isButtonCurrentlyPressed() { + if (gpio::markOutputs(StatusLedPortNum, StatusLedPinMask) != 0) + { + return false; // LED is on, ignore + } return gpio::get(StatusLedPortNum, StatusLedPinMask) != 0; } From 8b75c189bf4407c485b4ada2755c142a04e3e68b Mon Sep 17 00:00:00 2001 From: Pavel Kirienko Date: Thu, 5 May 2016 05:11:30 +0300 Subject: [PATCH 8/9] Fixed flash write routine --- firmware/blackmagic_flash.sh | 5 ++-- firmware/src/sys/board.cpp | 57 +++++++++++++++++++----------------- 2 files changed, 33 insertions(+), 29 deletions(-) diff --git a/firmware/blackmagic_flash.sh b/firmware/blackmagic_flash.sh index fefd60a..cb57eb6 100755 --- a/firmware/blackmagic_flash.sh +++ b/firmware/blackmagic_flash.sh @@ -5,8 +5,9 @@ # Author: Pavel Kirienko # -PORT=${1:-'/dev/ttyACM0'} -#/dev/serial/by-id/usb-Black_Sphere_Technologies_Black_Magic_Probe_DDE578CC-if00 +BM_DEV=$(readlink -f /dev/serial/by-id/usb-Black_Sphere_Technologies_Black_Magic_Probe_*-if00) +PORT=${1:-$BM_DEV} +echo "Using port $PORT" elf=build/firmware.elf diff --git a/firmware/src/sys/board.cpp b/firmware/src/sys/board.cpp index 312f2ef..7274196 100644 --- a/firmware/src/sys/board.cpp +++ b/firmware/src/sys/board.cpp @@ -326,44 +326,47 @@ int writeFlashStorage(unsigned offset, unsigned length, const void* data) std::memmove(buffer + offset, data, length); // IAP commands go here - unsigned iap_command = 0; - unsigned iap_args[5] = {}; - const auto iap_entry_point = reinterpret_cast(0x1FFF1FF1); + unsigned iap_command[5] = {}; + unsigned iap_result[5] = {}; + const auto iap_entry_point = reinterpret_cast(0x1FFF1FF1); - // Erasing the sector - iap_command = 52; - iap_args[0] = FlashStorageSectorNumber; // Start sector - iap_args[1] = FlashStorageSectorNumber; // End sector - iap_args[2] = SystemCoreClock / 1000; // System clock in kHz - iap_entry_point(&iap_command, iap_args); + // Preparing the sector for write + iap_command[0] = 50; + iap_command[1] = FlashStorageSectorNumber; // Start sector + iap_command[2] = FlashStorageSectorNumber; // End sector + iap_entry_point(iap_command, iap_result); + if (iap_result[0] != 0) + { + return -int(iap_result[0]); + } - if (iap_args[0] != 0) + // Erasing the sector + iap_command[0] = 52; + iap_command[3] = SystemCoreClock / 1000; // System clock in kHz + iap_entry_point(iap_command, iap_result); + if (iap_result[0] != 0) { - return -int(iap_args[0]); + return -int(iap_result[0]); } // Preparing the sector for write - iap_command = 50; - iap_args[0] = FlashStorageSectorNumber; // Start sector - iap_args[1] = FlashStorageSectorNumber; // End sector - iap_entry_point(&iap_command, iap_args); - - if (iap_args[0] != 0) + iap_command[0] = 50; + iap_entry_point(iap_command, iap_result); + if (iap_result[0] != 0) { - return -int(iap_args[0]); + return -int(iap_result[0]); } // Writing - iap_command = 51; - iap_args[0] = FlashStorageAddress; // Destination - iap_args[1] = reinterpret_cast(&buffer); // Source - iap_args[2] = FlashStorageSize; // Size - iap_args[3] = SystemCoreClock / 1000; // System clock in kHz - iap_entry_point(&iap_command, iap_args); - - if (iap_args[0] != 0) + iap_command[0] = 51; + iap_command[1] = FlashStorageAddress; // Destination + iap_command[2] = reinterpret_cast(&buffer); // Source + iap_command[3] = FlashStorageSize; // Size + iap_command[4] = SystemCoreClock / 1000; // System clock in kHz + iap_entry_point(iap_command, iap_result); + if (iap_result[0] != 0) { - return -int(iap_args[0]); + return -int(iap_result[0]); } return 0; From 951665261acd5b983b6647b87fb38a019f816af6 Mon Sep 17 00:00:00 2001 From: Pavel Kirienko Date: Thu, 5 May 2016 05:31:09 +0300 Subject: [PATCH 9/9] Warning fixes (GCC 5.3) --- firmware/libuavcan | 2 +- firmware/src/sys/libstubs.cpp | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/firmware/libuavcan b/firmware/libuavcan index 82d161e..ec84f64 160000 --- a/firmware/libuavcan +++ b/firmware/libuavcan @@ -1 +1 @@ -Subproject commit 82d161ee61876b1f15618e7ac96f9428fdbc5db2 +Subproject commit ec84f64f5f194d9f7bd8b0cd72d12431a17ecb42 diff --git a/firmware/src/sys/libstubs.cpp b/firmware/src/sys/libstubs.cpp index f6133a3..44b4b49 100644 --- a/firmware/src/sys/libstubs.cpp +++ b/firmware/src/sys/libstubs.cpp @@ -47,11 +47,21 @@ void operator delete(void*) std::abort(); } +void operator delete(void*, unsigned) +{ + std::abort(); +} + void operator delete[](void*) { std::abort(); } +void operator delete[](void*, unsigned) +{ + std::abort(); +} + namespace __gnu_cxx {