Skip to content

Commit

Permalink
Make changes suggested in #32.
Browse files Browse the repository at this point in the history
1. use writenum() for serial number.
2. Add comment and VID checking for SWD programmer delay for Metro M0.
Also:
3. Make warnings be errors on compile so they don't fly by; fix remaining warnings.
4. `make all-boards` now stops when a board fails to build.
5. Fix .binfo location for Metro M4: M4 code is now >8K. Use <n>K constants instead of hex constants for readability.
  • Loading branch information
dhalbert committed Feb 23, 2018
1 parent 8176ee9 commit a91d7b0
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 23 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ ifeq ($(CHIP_FAMILY), samd51)
COMMON_FLAGS = -mthumb -mcpu=cortex-m4 -O2 -g -DSAMD51
endif
WFLAGS = \
-Wall -Wstrict-prototypes \
-Werror -Wall -Wstrict-prototypes \
-Werror-implicit-function-declaration -Wpointer-arith -std=gnu99 \
-ffunction-sections -fdata-sections -Wchar-subscripts -Wcomment -Wformat=2 \
-Wimplicit-int -Wmain -Wparentheses -Wsequence-point -Wreturn-type -Wswitch \
Expand Down Expand Up @@ -175,6 +175,6 @@ drop-pkg:
rm -rf build/uf2-samd21-$(UF2_VERSION_BASE)

all-boards:
for f in `cd boards; ls` ; do "$(MAKE)" BOARD=$$f drop-board ; done
for f in `cd boards; ls` ; do "$(MAKE)" BOARD=$$f drop-board || break; done

drop: all-boards drop-pkg
8 changes: 4 additions & 4 deletions scripts/samd51j19a.ld
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ SEARCH_DIR(.)
/* Memory Spaces Definitions */
MEMORY
{
rom (rx) : ORIGIN = 0x00000000, LENGTH = 0x00080000
ram (rwx) : ORIGIN = 0x20000000, LENGTH = 0x00030000
rom (rx) : ORIGIN = 0x00000000, LENGTH = 16K
ram (rwx) : ORIGIN = 0x20000000, LENGTH = 192K
}

/* The stack size used by the application. NOTE: you need to adjust according to your application. */
STACK_SIZE = DEFINED(STACK_SIZE) ? STACK_SIZE : DEFINED(__stack_size__) ? __stack_size__ : 0xC000;
STACK_SIZE = DEFINED(STACK_SIZE) ? STACK_SIZE : DEFINED(__stack_size__) ? __stack_size__ : 48K;

/* Section Definitions */
SECTIONS
Expand Down Expand Up @@ -144,7 +144,7 @@ SECTIONS
. = ALIGN(4);
_end = . ;

_binfo_start = 0x00002000 - 4 * 4;
_binfo_start = 16K - 4 * 4;
.binfo _binfo_start : {
KEEP(*(.binfo)) ;
} > rom
Expand Down
9 changes: 1 addition & 8 deletions src/cdc_enumerate.c
Original file line number Diff line number Diff line change
Expand Up @@ -390,15 +390,8 @@ static void load_serial_number(char serial_number[SERIAL_NUMBER_LENGTH]) {
#endif
uint32_t serial_number_idx = 0;
for (int i = 0; i < 4; i++) {
uint32_t word = *(addresses[i]);
for (int j = 0; j < 8; j++) {
// Get top 4 bits.
uint8_t nibble = word >> 28;
word <<= 4;
serial_number[serial_number_idx++] = (nibble >= 10) ? 'A' + (nibble - 10) : '0' + nibble;
}
serial_number_idx += writeNum(&(serial_number[serial_number_idx]), *(addresses[i]), true);
}

serial_number[serial_number_idx] = '\0';
}

Expand Down
14 changes: 8 additions & 6 deletions src/flash_samd51.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ bool row_same[FLASH_SIZE / NVMCTRL_BLOCK_SIZE][NVMCTRL_BLOCK_SIZE / FLASH_ROW_SI
#define QUICK_FLASH 1

void flash_write_row(uint32_t *dst, uint32_t *src) {
const uint32_t FLASH_ROW_SIZE_WORDS = FLASH_ROW_SIZE / 4;

// The cache in Rev A isn't reliable when reading and writing to the NVM.
NVMCTRL->CTRLA.bit.CACHEDIS0 = true;
NVMCTRL->CTRLA.bit.CACHEDIS1 = true;
Expand All @@ -85,7 +87,7 @@ void flash_write_row(uint32_t *dst, uint32_t *src) {
uint8_t row = (((uint32_t) dst) % NVMCTRL_BLOCK_SIZE) / FLASH_ROW_SIZE;
#if QUICK_FLASH
bool src_different = false;
for (uint32_t i = 0; i < FLASH_ROW_SIZE / 4; ++i) {
for (uint32_t i = 0; i < FLASH_ROW_SIZE_WORDS; ++i) {
if (src[i] != dst[i]) {
src_different = true;
break;
Expand All @@ -102,17 +104,17 @@ void flash_write_row(uint32_t *dst, uint32_t *src) {

if (!block_erased[block]) {
uint8_t rows_per_block = NVMCTRL_BLOCK_SIZE / FLASH_ROW_SIZE;
uint8_t* block_address = block * NVMCTRL_BLOCK_SIZE;
uint32_t* block_address = (uint32_t *) (block * NVMCTRL_BLOCK_SIZE);

bool some_rows_same = false;
for (uint8_t i = 0; i < rows_per_block; i++) {
some_rows_same = some_rows_same || row_same[block][i];
}
uint32_t row_cache[rows_per_block][FLASH_ROW_SIZE / 4];
uint32_t row_cache[rows_per_block][FLASH_ROW_SIZE_WORDS];
if (some_rows_same) {
for (uint8_t i = 0; i < rows_per_block; i++) {
if(row_same[block][i]) {
memcpy(row_cache[i], block_address + i * FLASH_ROW_SIZE, FLASH_ROW_SIZE);
memcpy(row_cache[i], block_address + i * FLASH_ROW_SIZE_WORDS, FLASH_ROW_SIZE);
}
}
}
Expand All @@ -123,13 +125,13 @@ void flash_write_row(uint32_t *dst, uint32_t *src) {
if(row_same[block][i]) {
// dst is a uint32_t pointer so we add the number of words,
// not bytes.
flash_write_words(block_address + i * FLASH_ROW_SIZE, row_cache[i], FLASH_ROW_SIZE / 4);
flash_write_words(block_address + i * FLASH_ROW_SIZE_WORDS, row_cache[i], FLASH_ROW_SIZE_WORDS);
}
}
}
}

flash_write_words(dst, src, FLASH_ROW_SIZE / 4);
flash_write_words(dst, src, FLASH_ROW_SIZE_WORDS);

// Don't return until we're done writing in case something after us causes
// a reset.
Expand Down
3 changes: 2 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ int main(void) {
while (1) {
}

#if USB_PID == 0x0013 // Metro m0
#if (USB_VID == 0x239a) && (USB_PID == 0x0013) // Adafruit Metro M0
// Delay a bit so SWD programmer can have time to attach.
delay(100);
#endif
led_init();
Expand Down
3 changes: 1 addition & 2 deletions src/usart_sam_ba.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ uint8_t mode_of_transfer;
* \brief Open the given USART
*/
void usart_open() {
uint32_t inst;
uint32_t port;
uint8_t pin;

Expand Down Expand Up @@ -103,7 +102,7 @@ void usart_open() {
}

#ifdef SAMD21
inst = uart_get_sercom_index(BOOT_USART_MODULE);
uint32_t inst = uart_get_sercom_index(BOOT_USART_MODULE);
/* Enable clock for BOOT_USART_MODULE */
PM->APBCMASK.reg |= (1u << (inst + PM_APBCMASK_SERCOM0_Pos));

Expand Down

0 comments on commit a91d7b0

Please sign in to comment.