From a2b2926d4fce19b52f1d2539b33dc6b545bbd88a Mon Sep 17 00:00:00 2001 From: deadprogram Date: Sun, 16 Feb 2025 13:22:28 +0100 Subject: [PATCH] targets: WIP on Arduino Uno R4 Signed-off-by: deadprogram --- src/machine/board_arduino_unor4.go | 95 ++++++++++++ src/machine/machine_ra4m1.go | 236 +++++++++++++++++++++++++++++ src/runtime/runtime_ra4m1.go | 2 +- targets/arduino-unor4.json | 7 + targets/ra4m1.json | 8 +- targets/ra4m1.ld | 2 +- 6 files changed, 343 insertions(+), 7 deletions(-) create mode 100644 src/machine/board_arduino_unor4.go create mode 100644 src/machine/machine_ra4m1.go create mode 100644 targets/arduino-unor4.json diff --git a/src/machine/board_arduino_unor4.go b/src/machine/board_arduino_unor4.go new file mode 100644 index 000000000..33ea96a50 --- /dev/null +++ b/src/machine/board_arduino_unor4.go @@ -0,0 +1,95 @@ +//go:build arduino_unor4 + +// This contains the pin mappings for the Arduino Uno R4 board. +// +// For more information, see: +package machine + +// GPIO Pins +const ( + D0 Pin = P3_01 + D1 Pin = P3_02 + D2 Pin = P1_04 + D3 Pin = P1_05 + D4 Pin = P1_06 + D5 Pin = P1_07 + D6 Pin = P1_11 + D7 Pin = P1_12 + D8 Pin = P3_04 + D9 Pin = P3_03 + D10 Pin = P1_03 + D11 Pin = P4_11 + D12 Pin = P4_10 + D13 Pin = P1_02 +) + +// Analog pins +const ( + A0 Pin = P0_14 + A1 Pin = P0_00 + A2 Pin = P0_01 + A3 Pin = P0_02 + A4 Pin = P1_01 + A5 Pin = P1_00 +) + +const ( + LED = D13 +) + + +// UART1 pins +const ( + UART_TX_PIN Pin = P1_09 + UART_RX_PIN Pin = P1_10 +) + +// I2C pins +const ( + SDA_PIN Pin = A4 + SCL_PIN Pin = A5 +) + +// { BSP_IO_PORT_03_PIN_01, P301 }, /* (0) D0 ------------------------- DIGITAL */ +// { BSP_IO_PORT_03_PIN_02, P302 }, /* (1) D1 */ +// { BSP_IO_PORT_01_PIN_04, P104 }, /* (2) D2 */ +// { BSP_IO_PORT_01_PIN_05, P105 }, /* (3) D3~ */ +// { BSP_IO_PORT_01_PIN_06, P106 }, /* (4) D4 */ +// { BSP_IO_PORT_01_PIN_07, P107 }, /* (5) D5~ */ +// { BSP_IO_PORT_01_PIN_11, P111 }, /* (6) D6~ */ +// { BSP_IO_PORT_01_PIN_12, P112 }, /* (7) D7 */ +// { BSP_IO_PORT_03_PIN_04, P304 }, /* (8) D8 */ +// { BSP_IO_PORT_03_PIN_03, P303 }, /* (9) D9~ */ +// { BSP_IO_PORT_01_PIN_03, P103 }, /* (10) D10~ */ +// { BSP_IO_PORT_04_PIN_11, P411 }, /* (11) D11~ */ +// { BSP_IO_PORT_04_PIN_10, P410 }, /* (12) D12 */ +// { BSP_IO_PORT_01_PIN_02, P102 }, /* (13) D13 */ +// { BSP_IO_PORT_00_PIN_14, P014 }, /* (14) A0 -------------------------- ANALOG */ +// { BSP_IO_PORT_00_PIN_00, P000 }, /* (15) A1 */ +// { BSP_IO_PORT_00_PIN_01, P001 }, /* (16) A2 */ +// { BSP_IO_PORT_00_PIN_02, P002 }, /* (17) A3 */ +// { BSP_IO_PORT_01_PIN_01, P101 }, /* (18) A4/SDA */ +// { BSP_IO_PORT_01_PIN_00, P100 }, /* (19) A5/SCL */ + +// { BSP_IO_PORT_05_PIN_00, P500 }, /* (20) Analog voltage measure pin */ +// { BSP_IO_PORT_04_PIN_08, P408 }, /* (21) USB switch, drive high for RA4 */ + +// { BSP_IO_PORT_01_PIN_09, P109 }, /* (22) D22 ------------------------ TX */ +// { BSP_IO_PORT_01_PIN_10, P110 }, /* (23) D23 ------------------------ RX */ +// { BSP_IO_PORT_05_PIN_01, P501 }, /* (24) D24 ------------------------- TX WIFI */ +// { BSP_IO_PORT_05_PIN_02, P502 }, /* (25) D25 ------------------------- RX WIFI */ + +// { BSP_IO_PORT_04_PIN_00, P400 }, /* (26) D26 QWIC SCL */ +// { BSP_IO_PORT_04_PIN_01, P401 }, /* (27) D27 QWIC SDA */ + +// { BSP_IO_PORT_00_PIN_03, P003 }, /* (28) D28 */ +// { BSP_IO_PORT_00_PIN_04, P004 }, /* (29) D29 */ +// { BSP_IO_PORT_00_PIN_11, P011 }, /* (30) D30 */ +// { BSP_IO_PORT_00_PIN_12, P012 }, /* (31) D31 */ +// { BSP_IO_PORT_00_PIN_13, P013 }, /* (32) D32 */ +// { BSP_IO_PORT_00_PIN_15, P015 }, /* (33) D33 */ +// { BSP_IO_PORT_02_PIN_04, P204 }, /* (34) D34 */ +// { BSP_IO_PORT_02_PIN_05, P205 }, /* (35) D35 */ +// { BSP_IO_PORT_02_PIN_06, P206 }, /* (36) D36 */ +// { BSP_IO_PORT_02_PIN_12, P212 }, /* (37) D37 */ +// { BSP_IO_PORT_02_PIN_13, P213 }, /* (38) D38 */ \ No newline at end of file diff --git a/src/machine/machine_ra4m1.go b/src/machine/machine_ra4m1.go new file mode 100644 index 000000000..148a8c4a0 --- /dev/null +++ b/src/machine/machine_ra4m1.go @@ -0,0 +1,236 @@ +//go:build ra4m1 + +package machine + +import ( + "device/renesas" + "runtime/volatile" + "unsafe" +) + +const deviceName = renesas.Device + +const ( + P0_00 Pin = 0 + P0_01 Pin = 1 + P0_02 Pin = 2 + P0_03 Pin = 3 + P0_04 Pin = 4 + P0_05 Pin = 5 + P0_06 Pin = 6 + P0_07 Pin = 7 + P0_08 Pin = 8 + P0_09 Pin = 9 + P0_10 Pin = 10 + P0_11 Pin = 11 + P0_12 Pin = 12 + P0_13 Pin = 13 + P0_14 Pin = 14 + P0_15 Pin = 15 + P1_00 Pin = 16 + P1_01 Pin = 17 + P1_02 Pin = 18 + P1_03 Pin = 19 + P1_04 Pin = 20 + P1_05 Pin = 21 + P1_06 Pin = 22 + P1_07 Pin = 23 + P1_08 Pin = 24 + P1_09 Pin = 25 + P1_10 Pin = 26 + P1_11 Pin = 27 + P1_12 Pin = 28 + P1_13 Pin = 29 + P1_14 Pin = 30 + P1_15 Pin = 31 + P2_00 Pin = 32 + P2_01 Pin = 33 + P2_02 Pin = 34 + P2_03 Pin = 35 + P2_04 Pin = 36 + P2_05 Pin = 37 + P2_06 Pin = 38 + P2_07 Pin = 39 + P2_08 Pin = 40 + P2_09 Pin = 41 + P2_10 Pin = 42 + P2_11 Pin = 43 + P2_12 Pin = 44 + P2_13 Pin = 45 + P2_14 Pin = 46 + P2_15 Pin = 47 + P3_00 Pin = 48 + P3_01 Pin = 49 + P3_02 Pin = 50 + P3_03 Pin = 51 + P3_04 Pin = 52 + P3_05 Pin = 53 + P3_06 Pin = 54 + P3_07 Pin = 55 + P3_08 Pin = 56 + P3_09 Pin = 57 + P3_10 Pin = 58 + P3_11 Pin = 59 + P3_12 Pin = 60 + P3_13 Pin = 61 + P3_14 Pin = 62 + P3_15 Pin = 63 + P4_00 Pin = 64 + P4_01 Pin = 65 + P4_02 Pin = 66 + P4_03 Pin = 67 + P4_04 Pin = 68 + P4_05 Pin = 69 + P4_06 Pin = 70 + P4_07 Pin = 71 + P4_08 Pin = 72 + P4_09 Pin = 73 + P4_10 Pin = 74 + P4_11 Pin = 75 + P4_12 Pin = 76 + P4_13 Pin = 77 + P4_14 Pin = 78 + P4_15 Pin = 79 + P5_00 Pin = 80 + P5_01 Pin = 81 + P5_02 Pin = 82 + P5_03 Pin = 83 + P5_04 Pin = 84 + P5_05 Pin = 85 + P5_06 Pin = 86 + P5_07 Pin = 87 + P5_08 Pin = 88 + P5_09 Pin = 89 + P5_10 Pin = 90 + P5_11 Pin = 91 + P5_12 Pin = 92 + P5_13 Pin = 93 + P5_14 Pin = 94 + P5_15 Pin = 95 +) + +const ( + PinOutput PinMode = iota + PinInput + PinInputPullUp + PinInputPullDown +) + +// Configure configures the gpio pin as per mode. +func (p Pin) Configure(config PinConfig) { + if p == NoPin { + return + } + + enableWritingPmnPFS() + + switch config.Mode { + case PinOutput: + setPDR(p, renesas.PFS_P000PFS_PDR_1) + default: + setPDR(p, renesas.PFS_P000PFS_PDR_0) + } + + disableWritingPmnPFS() +} + +// Set drives the pin high if value is true else drives it low. +func (p Pin) Set(value bool) { + if p == NoPin { + return + } + + port := getPort(p) + if port == nil { + return + } + + if value == true { + port.set(p) + } else { + port.clr(p) + } +} + +// Get reads the pin value. +func (p Pin) Get() bool { + return false +} + +var ( + gpioPort0 = gpioPortType0{renesas.PORT0} + gpioPort1 = gpioPortType1{renesas.PORT1} + gpioPort2 = gpioPortType1{renesas.PORT2} + gpioPort3 = gpioPortType1{renesas.PORT3} + gpioPort4 = gpioPortType1{renesas.PORT4} + gpioPort5 = gpioPortType0{renesas.PORT5} +) + +func getPort(p Pin) gpioPort { + switch getPortNumber(p) { + case 0: + return gpioPort0 + case 1: + return gpioPort1 + case 2: + return gpioPort2 + case 3: + return gpioPort3 + case 4: + return gpioPort4 + case 5: + return gpioPort5 + default: + return nil + } +} + +func getPortNumber(p Pin) int { + return int(p) >> 8 +} + +type gpioPort interface { + set(p Pin) + clr(p Pin) +} + +type gpioPortType0 struct { + port *renesas.PORT0_Type +} + +func (p gpioPortType0) set(pin Pin) { + p.port.SetPCNTR3_PORR(1 << pin) +} + +func (p gpioPortType0) clr(pin Pin) { + p.port.SetPCNTR3_POSR(1 << pin) +} + +type gpioPortType1 struct { + port *renesas.PORT1_Type +} + +func (p gpioPortType1) set(pin Pin) { + p.port.SetPCNTR3_PORR(1 << pin) +} + +func (p gpioPortType1) clr(pin Pin) { + p.port.SetPCNTR3_POSR(1 << pin) +} + +func enableWritingPmnPFS() { + renesas.PMISC.PWPR.Set(0x0) + renesas.PMISC.SetPWPR_PFSWE(0x1) +} + +func disableWritingPmnPFS() { + renesas.PMISC.PWPR.Set(0x0) + renesas.PMISC.SetPWPR_B0WI(0x1) +} + +func setPDR(p Pin, value uint32) { + port := getPortNumber(p) + bit := int(p) & 0xff + reg := (*volatile.Register32)(unsafe.Add(unsafe.Pointer(uintptr(0x40040800)), 0x40*port+4*bit)) + reg.SetBits(value << renesas.PFS_P000PFS_PDR_Pos) +} diff --git a/src/runtime/runtime_ra4m1.go b/src/runtime/runtime_ra4m1.go index 895874227..cdc0bb8d2 100644 --- a/src/runtime/runtime_ra4m1.go +++ b/src/runtime/runtime_ra4m1.go @@ -1,4 +1,4 @@ -//go:build renasas +//go:build ra4m1 package runtime diff --git a/targets/arduino-unor4.json b/targets/arduino-unor4.json new file mode 100644 index 000000000..7758e002b --- /dev/null +++ b/targets/arduino-unor4.json @@ -0,0 +1,7 @@ +{ + "inherits": ["ra4m1"], + "build-tags": ["arduino_unor4"], + "flash-command": "bossac -i -e -w -v -R -U --port={port} --offset=0x4000 {bin}", + "serial-port": ["2341:8057", "2341:0057"], + "flash-1200-bps-reset": "true" +} diff --git a/targets/ra4m1.json b/targets/ra4m1.json index 82d90f6c2..2269c844b 100644 --- a/targets/ra4m1.json +++ b/targets/ra4m1.json @@ -1,10 +1,8 @@ { "inherits": ["cortex-m4"], - "build-tags": ["ra4m1", "renesas"], + "build-tags": ["ra4m1", "r7fa4m1ab", "renesas"], "linkerscript": "targets/ra4m1.ld", "extra-files": [ - "src/device/renesas/r7fa4m2ad.s" - ], - "openocd-transport": "swd", - "openocd-target": "atsame5x" + "src/device/renesas/r7fa4m1ab.s" + ] } diff --git a/targets/ra4m1.ld b/targets/ra4m1.ld index 908ce6a84..d95308913 100644 --- a/targets/ra4m1.ld +++ b/targets/ra4m1.ld @@ -2,7 +2,7 @@ MEMORY { FLASH_TEXT (rw) : ORIGIN = 0x00000000+0x4000, LENGTH = 0x00080000-0x4000 /* First 16KB used by bootloader */ - RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 0x00030000 + RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 0x00040000 } _stack_size = 4K;