Skip to content
This repository has been archived by the owner on Jan 8, 2024. It is now read-only.

Commit

Permalink
Initial implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dual Tachyon authored and Dual Tachyon committed Sep 9, 2023
1 parent da9ae75 commit a2a21dd
Show file tree
Hide file tree
Showing 10 changed files with 562 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
25 changes: 25 additions & 0 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
on:
push:

jobs:
build:
runs-on: ubuntu-22.04

steps:
- name: arm-none-eabi-gcc
uses: carlosperate/arm-none-eabi-gcc-action@v1
with:
release: '10.3-2021.10'

- name: Checkout
uses: actions/checkout@v3

- name: Make
run: make

- name: 'Upload Artifact'
uses: actions/upload-artifact@v3
with:
name: 'Flash Loader'
path: dp32-fl.elf

32 changes: 32 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
on:
push:
tags:
- '*'

jobs:
build:
runs-on: ubuntu-22.04

steps:
- name: arm-none-eabi-gcc
uses: carlosperate/arm-none-eabi-gcc-action@v1
with:
release: '10.3-2021.10'

- name: Checkout
uses: actions/checkout@v3

- name: Make
run: make

- name: 'Upload Artifact'
uses: actions/upload-artifact@v3
with:
name: 'Flash Loader'
path: dp32-fl.elf

- name: 'Create Release'
uses: ncipollo/release-action@v1
with:
artifacts: "dp32-fl.elf,Devices.xml"

3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.d
*.o
*.elf
6 changes: 6 additions & 0 deletions Devices.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<Database>
<Device>
<ChipInfo Vendor="DualTachyon" Name="DP32G030" WorkRAMAddr="0x20000000" WorkRAMSize="0x4000" Core="JLINK_CORE_CORTEX_M0" />
<FlashBankInfo Name="Internal Code Flash" BaseAddr="0x00000000" MaxSize="0x0000F000" Loader="dp32-fl.elf" LoaderType="FLASH_ALGO_TYPE_OPEN" AlwaysPresent="1" />
</Device>
</Database>
44 changes: 44 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
TARGET = dp32-fl

OBJS = $(TARGET).o

ifeq ($(OS),Windows_NT)
TOP := $(dir $(realpath $(lastword $(MAKEFILE_LIST))))
else
TOP := $(shell pwd)
endif

AS = arm-none-eabi-as
CC = arm-none-eabi-gcc
LD = arm-none-eabi-gcc
OBJCOPY = arm-none-eabi-objcopy
SIZE = arm-none-eabi-size

ASFLAGS = -mcpu=cortex-m0
CFLAGS = -Os -Wall -Werror -mcpu=cortex-m0 -fno-builtin -fshort-enums -fno-delete-null-pointer-checks -std=c11 -MMD -fno-toplevel-reorder
CFLAGS += -DPRINTF_INCLUDE_CONFIG_H
CFLAGS += -DGIT_HASH=\"$(GIT_HASH)\"
LDFLAGS = -mcpu=cortex-m0 -nostartfiles -Wl,-T,dp32-fl.ld -Wl,-x

INC =
LIBS =

DEPS = $(OBJS:.o=.d)

all: $(TARGET).elf
$(SIZE) $<

$(TARGET).elf: $(OBJS)
$(LD) $(LDFLAGS) $^ -o $@ $(LIBS)

%.o: %.c | $(BSP_HEADERS)
$(CC) $(CFLAGS) $(INC) -c $< -o $@

%.o: %.S
$(AS) $(ASFLAGS) $< -o $@

-include $(DEPS)

clean:
rm -f $(TARGET).elf $(OBJS) $(DEPS)

42 changes: 41 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,41 @@
# dp32g030-flash-loader
# Flash Loader for Segger JLink software to support the DP32G030

This flash loader supports flashing to the DP32G030 as used in the [UV-K5](https://github.com/DualTachyon/uv-k5-firmware) class of radio transceivers.

# Build

```
make
```

# Usage on Windows

Create a folder:

```
md "%USERPROFILE%\AppData\Roaming\SEGGER\JLinkDevices"
md "%USERPROFILE%\AppData\Roaming\SEGGER\JLinkDevices\DualTachyon"
md "%USERPROFILE%\AppData\Roaming\SEGGER\JLinkDevices\DualTachyon\DP32G030"
copy Devices.xml "%USERPROFILE%\AppData\Roaming\SEGGER\JLinkDevices\DualTachyon\DP32G030\"
copy dp32-fl.elf "%USERPROFILE%\AppData\Roaming\SEGGER\JLinkDevices\DualTachyon\DP32G030\"
```

For Linux, you'll have to figure out the paths, but should be a similar approach as Windows.

# License

Copyright 2023 Dual Tachyon
https://github.com/DualTachyon

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

194 changes: 194 additions & 0 deletions dp32-fl.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
#include "flash.h"

#define BLOCK_SIZE 512

typedef struct {
uint32_t Size;
uint32_t Address;
} SectorInfo_t;

typedef struct {
uint16_t AlgoVer;
uint8_t Name[128];
uint16_t Type;
uint32_t BaseAddr;
uint32_t TotalSize;
uint32_t PageSize;
uint32_t Reserved;
uint8_t ErasedVal;
uint8_t Padding[3];
uint32_t TimeoutProg;
uint32_t TimeoutErase;
SectorInfo_t SectorInfo[2];
} FlashDevice_t;

extern const FlashDevice_t FlashDevice __attribute__((section(".constdata")));

const FlashDevice_t FlashDevice = {
0x0101,
"DP32G030 Internal Flash",
1,
0x00000000,
0x0000F000,
0x00000200,
0x00000000,
0xFF,
{ 0x00, 0x00, 0x00},
500,
500,
{
{ 0x00000200, 0x00000000 },
{ 0xFFFFFFFF, 0xFFFFFFFF },
}
};

static void WaitNotBusy(void)
{
while (1) {
if ((FLASH_ST & FLASH_ST_BUSY_MASK) == FLASH_ST_BUSY_BITS_READY) {
return;
}
}
}

static void WaitForEmpty(void)
{
while (1) {
if ((FLASH_ST & FLASH_ST_PROG_BUF_EMPTY_MASK) == FLASH_ST_PROG_BUF_EMPTY_BITS_EMPTY) {
return;
}
}
}

static void FlashLock(void)
{
FLASH_LOCK = FLASH_LOCK_LOCK_BITS_LOCK;
}

static void FlashUnlock(void)
{
FLASH_UNLOCK = FLASH_UNLOCK_UNLOCK_BITS_UNLOCK;
}

static void FlashStart(void)
{
FlashUnlock();

FLASH_START = FLASH_START_START_BITS_START;
}

static void FlashStop(void)
{
FLASH_CFG = (FLASH_CFG & ~FLASH_CFG_MODE_MASK) | FLASH_CFG_MODE_VALUE_READ_AHB;
}

static void FlashHalfSector(uint32_t Address, const uint8_t *pBuffer, uint32_t WordCount)
{
const uint32_t *pWord = (const uint32_t *)pBuffer;
uint32_t i;

WaitNotBusy();

FLASH_CFG = (FLASH_CFG & ~FLASH_CFG_MODE_MASK) | FLASH_CFG_MODE_BITS_PROGRAM;
FLASH_ADDR = Address >> 2;
FLASH_WDATA = *pWord++;

FlashStart();

for (i = 1; i < WordCount; i++) {
WaitForEmpty();
FLASH_WDATA = *pWord++;
}

WaitNotBusy();

FlashStop();
FlashLock();
}

// --------

int Init(uint32_t Addr, uint32_t Freq, uint32_t Func) __attribute__((section("PrgCode")));
int UnInit(uint32_t Func) __attribute__((section("PrgCode")));
int EraseChip(void) __attribute__((section("PrgCode")));
int EraseSector(uint32_t SectorAddr) __attribute__((section("PrgCode")));
int ProgramPage(uint32_t DestAddr, uint32_t NumBytes, const uint8_t *pSrcBuff) __attribute__((section("PrgCode")));
int CheckBlank(uint32_t Addr, uint32_t NumBytes, uint8_t BlankValue) __attribute__((section("PrgCode")));

int Init(uint32_t Addr, uint32_t Freq, uint32_t Func)
{
if (FLASH_MASK != 6) {
FLASH_MASK = 0;
WaitNotBusy();
FLASH_MASK = 6;
WaitNotBusy();
if (FLASH_MASK != 6) {
return 1;
}
}

return 0;
}

int UnInit(uint32_t Func)
{
// restore the MCU

return 0;
}

int EraseChip(void)
{
uint16_t i;

for (i = 0; i < 0xF000; i += 512) {
int ret = EraseSector(i);

if (ret) {
return ret;
}
}

return 0;
}

int EraseSector(uint32_t SectorAddr)
{

if (SectorAddr % 512 || SectorAddr >= 0xF000) {
return 1;
}

WaitNotBusy();

FLASH_CFG = (FLASH_CFG & ~FLASH_CFG_MODE_MASK) | FLASH_CFG_MODE_BITS_ERASE;

FLASH_ADDR = SectorAddr >> 2;

FlashStart();
WaitNotBusy();
FlashStop();
FlashLock();

return 0;
}


int ProgramPage(uint32_t DestAddr, uint32_t NumBytes, const uint8_t *pBuffer)
{
if (DestAddr % BLOCK_SIZE || DestAddr >= 0xF000 || NumBytes % BLOCK_SIZE || NumBytes > 0xF000 || (DestAddr + NumBytes) > 0xF000) {
return 1;
}

while (NumBytes >= BLOCK_SIZE) {
FlashHalfSector(DestAddr + 0x0000, pBuffer + 0x0000, BLOCK_SIZE / 4);
FlashHalfSector(DestAddr + 0x0100, pBuffer + 0x0100, BLOCK_SIZE / 4);
NumBytes -= BLOCK_SIZE;
pBuffer += BLOCK_SIZE;
}

FlashStop();
FlashLock();

return 0;
}
Loading

0 comments on commit a2a21dd

Please sign in to comment.