summaryrefslogtreecommitdiffstats
path: root/utils/flash_watch_pyruler/flash_watch_pyruler.ino
blob: 2e4d2f088929cbf0c774affd9270f690151eaa94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/** 
 *  Sensor Watch Firmware Flasher
 *  
 *  This sketch is meant to run on an Adafruit PyRuler or Trinket.
 *  Connect the SWD, SWC and RST pads on the watch to pins 0, 1 and 3.
 *  Connect V+ on the watch to 3V, and GND to GND.
 *  This will power the watch from the Trinket so you don't need to plug the watch in to USB.
 *  Hit reset on the PyRuler / Trinket to begin the flashing process.
 *  The Trinket's red LED will blink slowly when the bootloader has been successfully written.
 */

#include "Adafruit_DAP.h"
#include "bootloader.h"

#define SWDIO 0
#define SWCLK 1
#define SWRST 3

#define BUFSIZE 256       //don't change!
//uint8_t buf[BUFSIZE];

//create a DAP for programming Atmel SAM devices
Adafruit_DAP_SAM dap;

// Function called when there's an SWD error
void error(const char *text) {
  Serial.println(text);
  while (1);
}


void setup() {
  pinMode(13, OUTPUT);
  Serial.begin(115200);
//  while(!Serial) {
//    delay(1);         // will pause the chip until it opens serial console
//  }

  dap.begin(SWCLK, SWDIO, SWRST, &error);
  
  Serial.print("Connecting...");  
  if (! dap.dap_disconnect())                      error(dap.error_message);
  
  char debuggername[100];
  if (! dap.dap_get_debugger_info(debuggername))   error(dap.error_message);
  Serial.print(debuggername); Serial.print("\n\r");
  
  if (! dap.dap_connect())                         error(dap.error_message);
  
  if (! dap.dap_transfer_configure(0, 128, 128))   error(dap.error_message);
  if (! dap.dap_swd_configure(0))                  error(dap.error_message);
  if (! dap.dap_reset_link())                      error(dap.error_message);
  if (! dap.dap_swj_clock(50))               error(dap.error_message);
  dap.dap_target_prepare();

  uint32_t dsu_did;
  if (! dap.select(&dsu_did)) {
    Serial.print("Unknown device found 0x"); Serial.print(dsu_did, HEX);
    error("Unknown device found");
  }
  for (device_t *device = dap.devices; device->dsu_did > 0; device++) {
    if (device->dsu_did == dsu_did) {
      Serial.print("Found Target: ");
      Serial.print(device->name);
      Serial.print("\tFlash size: ");
      Serial.print(device->flash_size);
      Serial.print("\tFlash pages: ");
      Serial.println(device->n_pages);
      //Serial.print("Page size: "); Serial.println(device->flash_size / device->n_pages);
    }
  }
        
  Serial.println(" done.");

  Serial.print("Erasing... ");
  dap.erase();
  Serial.println(" done.");
  
  Serial.print("Programming... ");
  unsigned long t = millis();
  uint32_t addr = dap.program_start();

  while(addr < sizeof(binfile)){
    dap.programBlock(addr, binfile + addr);
    addr += BUFSIZE;
  }
  
  Serial.println(millis() - t);
  Serial.println("\nDone!");

  Serial.print("Fuses... ");
  dap.fuseRead(); //MUST READ FUSES BEFORE SETTING OR WRITING ANY
  Serial.println("read.");
  dap._USER_ROW.bit.BOOTPROT = 0x2;
  Serial.println("Setting BOOTPROT to 0x2 for 8192 byte bootloader");
  dap.fuseWrite();
  Serial.println("\nDone!!");

  dap.dap_set_clock(50);

  dap.deselect();
  dap.dap_disconnect();
}

void loop() {
  //blink led on the host to show we're done
  digitalWrite(13, HIGH);
  delay(500); 
  digitalWrite(13, LOW);
  delay(500);  
}