Skip to content

Nrf52840 usbboot #1004

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 60 commits into from
Jul 12, 2018
Merged

Nrf52840 usbboot #1004

merged 60 commits into from
Jul 12, 2018

Conversation

hathach
Copy link
Member

@hathach hathach commented Jul 9, 2018

this PR add uf2 bootloader, also up-gradable via CDC with nrfutil and OTA. It also add the MSC with internal flash + CDC as serial for circuitpython. To go into uf2 mode, press button0 (on pca10056) while reset (double reset won't work for nrf series).

There is still (at least) some known bugs.

  1. On Linux the very first time we connect to cp with minicom, there is ~10 seconds delay. Afterwards (welcome prompt is printed) the terminal can disconnect/reconnect and work normally. On windows, it seems to be OK. I am suspecting some background task is not run properly at first. Spend a few hours to debug, but I want to make the initial PR for testing first ( come back to this later)
  2. The flash filesystem only work when SD is disabled. When SD is enabled, application basically only queue the flash operation (erase + write), SD will report complete asynchronously via callback (write buffer must remain valid until then). Some sync method is needed, we come back to this later.

@arturo182 to continue to use hwuart (jlink) for serial instead of usb cdc, you can change this macro from 0 to 1. The name of macro is not good at all, I will try to
find a better name for it later or if you could, please suggest one. https://github.com/adafruit/circuitpython/blob/nrf52840_usbboot/ports/nrf/boards/pca10056/mpconfigboard.h#L40

PS: First time I pushed an PR to circuitpy, let's me know if anything I should changes to get merged.

hathach and others added 30 commits April 24, 2018 00:19
define CIRCUITPY_BOOT_OUTPUT_FILE will cause mp_hal_stdout_tx_strn() to
invoke before serial_init() is called. Solution is skipped output to
serial if it is not inited.
add crc bypass magic to bootloader settting
[WIP] nRF52840 USB Bootloader
@dhalbert
Copy link
Collaborator

Let's get this merged into master, and then you can work in master instead of a branch, and submit PR's more often. Then we can keep up with each other's work. We try to have each PR add a particular feature or solve a particular bug, as opposed to adding multiple things that have been in the works for weeks.. It's fine to have multiple PR's and it makes it easier to review each set of changes.

@dhalbert
Copy link
Collaborator

In other words: fork your own hathach/circuitpython. Keep it up to date with adafruit/circuitpython. Work in a feature or bugfix branch. When you're ready to merge, submit a PR for that branch, after bringing the branch up to date if necessary with master. After the PR is accepted, you update your fork from master, then delete the branch, and start a new branch for the next thing.

This workflow is covered in detail in @kattni's new Guide: https://learn.adafruit.com/contribute-to-circuitpython-with-git-and-github.

@hathach
Copy link
Member Author

hathach commented Jul 10, 2018

Ok, so after merged, I will fork the cp. Then work on my fork and submit PR from there, right? I kind of new with the PR things on cp repo

@hathach
Copy link
Member Author

hathach commented Jul 10, 2018

Ah. Super thanks, I am totally new. Never read that doc before :)

@dhalbert
Copy link
Collaborator

Yes, exactly. It's somewhat more complicated but it's fairly standard, and it keeps adafruit/circuitpython clean from work branches. You have the freedom to push to your fork whenever you want for backup purposes.

@hathach
Copy link
Member Author

hathach commented Jul 10, 2018

Thanks for explanation @dhalbert :D

@arturo182
Copy link
Collaborator

Are we sure we want to merge 50+ commits without any squashes?

@dhalbert
Copy link
Collaborator

@hathach Feel free to contact any of us: @dhalbert, @tannewt, @kattni, @arturo182 for explanations, either in GitHub, HipChat, or discord. We're all happy to get you going on this -- it's not intuitive and takes some rote learning to get it right.

@dhalbert
Copy link
Collaborator

@arturo182 Yes, we're fine with many commits. It represents a lot of work and the individual commits can be helpful when bisecting or just looking back. We used to try to keep a cleaner commit chain, doing rebases more often, etc., and it just caused more issues and was harder to explain. Now we're fine with merge commits, etc. The history is already huge and a few more commits doesn't matter.

@arturo182
Copy link
Collaborator

Ok them :) But they're still the issue that this PR is in conflict with #1011 so maybe that one can be merged and then this one could be fixed?

@dhalbert
Copy link
Collaborator

Right. #1011 first and then fix this one to remove the merge conflicts. I can work on that if doing the updating to master is difficult on this one. But I will wait until the code changes requested by you and @tannewt are done.

tannewt
tannewt previously approved these changes Jul 10, 2018
Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. @arturo182 and @dhalbert should approve as well. Thanks!

@hathach
Copy link
Member Author

hathach commented Jul 11, 2018

resolve conflict with #1011

# If the build directory is not given, make it reflect the board name
BUILD ?= $(if $(SD),build-$(BOARD)-$(SD_LOWER),build-$(BOARD))
# Build directory with SD
BUILD = $(if $(SD),build-$(BOARD)-$(SD_LOWER),build-$(BOARD))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unsure why we would want to remove the capability to override the build directory? I think the ?= should stay.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah right, sorry, I tried to get SD default by board (e.g pca10056 default is s140, feather52832 is S132) and thought it would be fixed. Just change include order a bit to get that.

#include "tusb.h"

void run_background_tasks(void) {
#ifdef NRF52840_XXAA
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if there's a better way to do this, could we only include this file if we're building for 840, then we wouldn't need the macro check

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There could be other background service running besides usb, e.g ble task handling.

boot-flash:
@:$(call check_defined, SERIAL, example: SERIAL=/dev/ttyUSB0)
$(NRFUTIL) dfu serial --package $(BOOTLOADER_PKG) -p $(SERIAL) -b 115200
NRF_DEFINES += -DADAFRUIT_FEATHER52
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this define is used anywhere, I have removed it before in another PR.

boot-flash:
nrfjprog --program $(BOOTLOADER_FILENAME).hex -f nrf52 --chiperase --reset
NRF_DEFINES += -DNRF52840_XXAA
NRF_DEFINES += -DADAFRUIT_FEATHER52840
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same with this define


#define MICROPY_HW_UART_RX NRF_GPIO_PIN_MAP(0, 8)
#define MICROPY_HW_UART_TX NRF_GPIO_PIN_MAP(0, 6)
#define MICROPY_HW_UART_HWFC (0)

#define PORT_HEAP_SIZE (128 * 1024)
#define CIRCUITPY_AUTORELOAD_DELAY_MS 500

// Temp (could be removed) 0: usb cdc (default), 1 : hwuart (jlink)
#define CFG_HWUART_FOR_SERIAL 0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should probably have a better name, fitting the define convention with reversed logic like MICROPY_HW_USB_CDC ?

NRF_DEFINES += -DNRF52840_XXAA
NRF_DEFINES += -DADAFRUIT_FEATHER52840
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can remove

/* Internal Flash API
*------------------------------------------------------------------*/
static inline uint32_t lba2addr(uint32_t block) {
return ((uint32_t)__fatfs_flash_start_addr) + block * FILESYSTEM_BLOCK_SIZE;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong indent level

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you got an eagle eye :D

uint32_t internal_flash_get_block_size(void);
uint32_t internal_flash_get_block_count(void);
void internal_flash_irq_handler(void);
void internal_flash_flush(void);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to align functions

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find it easier to read :D


#if (MICROPY_PY_BLE_NUS == 0)

#if !defined( NRF52840_XXAA) || ( defined(CFG_HWUART_FOR_SERIAL) && CFG_HWUART_FOR_SERIAL == 1 )
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you define a default value in mpconfigport.h then no need to check for defined

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is temporarily, it is only defined in pca10056, feather52840 won't have it since it does not have jlink to begin with. I am still not sure if we should keep it hmmm

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it might be ok to remove the define and just have it always on, as you say, the feather doesn't have jlink, the dongle doesn't have jlink and the PCA10056 has a separate USB connector for the nRF USB, so everyone can use the CDC.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, but let's remove it later when CDC is tested reliably enough, we still need a easy switch to jlink for comparison for now.

*/
/**************************************************************************/

#ifdef NRF52840_XXAA
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for the macro, we only include this file in the Makefile if we are building for nrf52840

Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The build is failing because the ports/atmel-samd/peripherals submodule is deleted. Try git submodule update --init --recursive from the top level.

@hathach
Copy link
Member Author

hathach commented Jul 11, 2018

@tannewt superb !!! Thanks a lot, that fixes the annoying travis build failed. Somehow it is deleted by accident .

@arturo182
Copy link
Collaborator

Hey @hathach looks like the docs fail to build, make sure to exclude the tinyusb directory from the doc build like I did here: 0686857

@hathach
Copy link
Member Author

hathach commented Jul 12, 2018

@arturo182 thanks, finally the travis build is passed now :)

@arturo182
Copy link
Collaborator

Ok then, I think we can merge this and make a new PR fixing rest of my comments :)

@tannewt
Copy link
Member

tannewt commented Jul 12, 2018

Thank you all!

@tannewt tannewt merged commit 75f48a5 into master Jul 12, 2018
@dhalbert dhalbert deleted the nrf52840_usbboot branch August 1, 2018 03:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants