Skip to content

Commit

Permalink
Added support of GD32F405 and many fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
rombrew committed May 9, 2024
1 parent c1a390a commit e4e8cfc
Show file tree
Hide file tree
Showing 37 changed files with 607 additions and 377 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ PMC is an open project that aims to build the quality permanent magnet
synchronous machine (PMSM) controller for use in a variety of scopes like RC or
electric transport.

## Brief
## Brief description

PMC is ready to use in most intended applications. You can flash any supported
third-party hardware to work with PMC or use our original hardware.
Expand Down Expand Up @@ -111,7 +111,7 @@ There are a few videos about PMC on [youtube](https://www.youtube.com/@romblv).
- Battery energy (Wh) and charge (Ah) consumed.
- Fuel gauge percentage.

## Hardware specification (`REV5A`)
## Hardware specification (`REV5A`, `REV5B`)

- Dimension: 82mm x 55mm x 35mm.
- Weight: 40g (PCB) or about 400g (with wires and heatsink).
Expand Down
2 changes: 1 addition & 1 deletion doc/InputStepDirection.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The step and direction signals are fed to STEP and DIR pins.

```
+---------------< STEP (or CW)
| +----------< DIR (or CCW)
| +----------< DIR (or CCW)
| | +-----> GND
| | |
+----------+----+----+-------------------------+
Expand Down
100 changes: 79 additions & 21 deletions phobia/link.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
#define LINK_SPACE " \t"
#define LINK_EXTRA "])"

#define LINK_ALLOC_MAX 92160U
#define LINK_CACHE_MAX 4096U

enum {
LINK_MODE_IDLE = 0,
LINK_MODE_HWINFO,
Expand All @@ -40,8 +43,10 @@ struct link_priv {
char hw_build[LINK_NAME_MAX];
char hw_crc32[LINK_NAME_MAX];

char mb[81920];
char mb[LINK_ALLOC_MAX];
char *mbflow;

int cache[LINK_CACHE_MAX];
};

const char *lk_stoi(int *x, const char *s)
Expand Down Expand Up @@ -183,13 +188,32 @@ lk_token(char **sp)
return r;
}

static unsigned int
lk_hash(const char *sym)
{
unsigned long hash = 0U;

while (*sym != 0) {

hash = (hash + *sym) * 1149773U;
hash ^= (hash << 1) + (hash >> 4);

++sym;
}

hash ^= (hash << 15);
hash = (hash >> 16) & (LINK_CACHE_MAX - 1U);

return hash;
}

static char *
link_mballoc(struct link_pmc *lp, int len)
{
struct link_priv *priv = lp->priv;
char *mb = NULL;

if ((int) (priv->mbflow - priv->mb) < sizeof(priv->mb) - len) {
if ((int) (priv->mbflow - priv->mb) < LINK_ALLOC_MAX - len) {

mb = priv->mbflow;
priv->mbflow += len;
Expand All @@ -203,7 +227,7 @@ link_fetch_network(struct link_pmc *lp)
{
struct link_priv *priv = lp->priv;
char *lbuf = priv->lbuf;
int n, rc = 0;
int net_ID, rc = 0;

if (strstr(lbuf, "(pmc)") == lbuf) {

Expand All @@ -214,9 +238,9 @@ link_fetch_network(struct link_pmc *lp)

if (strstr(lbuf, "(net/") == lbuf) {

if (lk_stoi(&n, lbuf + 5) != NULL) {
if (lk_stoi(&net_ID, lbuf + 5) != NULL) {

sprintf(lp->network, "REMOTE/%i", n);
sprintf(lp->network, "REMOTE/%i", net_ID);
}
else {
lp->network[0] = 0;
Expand Down Expand Up @@ -894,17 +918,32 @@ int link_command(struct link_pmc *lp, const char *command)

struct link_reg *link_reg_lookup(struct link_pmc *lp, const char *sym)
{
struct link_reg *reg = NULL;
int reg_ID;
struct link_priv *priv = lp->priv;
struct link_reg *reg = NULL;
int hash, reg_ID;

for (reg_ID = 0; reg_ID < lp->reg_MAX_N; ++reg_ID) {
if (lp->linked == 0)
return NULL;

if (lp->reg[reg_ID].sym[0] != 0) {
hash = lk_hash(sym);
reg_ID = priv->cache[hash];

if (strcmp(lp->reg[reg_ID].sym, sym) == 0) {
if (strcmp(lp->reg[reg_ID].sym, sym) == 0) {

reg = &lp->reg[reg_ID];
break;
reg = &lp->reg[reg_ID];
}
else {
for (reg_ID = 0; reg_ID < lp->reg_MAX_N; ++reg_ID) {

if (lp->reg[reg_ID].sym[0] != 0) {

if (strcmp(lp->reg[reg_ID].sym, sym) == 0) {

priv->cache[hash] = reg_ID;
reg = &lp->reg[reg_ID];

break;
}
}
}
}
Expand All @@ -914,31 +953,50 @@ struct link_reg *link_reg_lookup(struct link_pmc *lp, const char *sym)

int link_reg_lookup_range(struct link_pmc *lp, const char *sym, int *min, int *max)
{
int n, rc, reg_ID;
struct link_priv *priv = lp->priv;
int len, hash, reg_ID, found = 0;

n = strlen(sym);
rc = 0;
if (lp->linked == 0)
return 0;

for (reg_ID = 0; reg_ID < lp->reg_MAX_N; ++reg_ID) {
len = strlen(sym);

hash = lk_hash(sym);
reg_ID = priv->cache[hash];

if (strncmp(lp->reg[reg_ID].sym, sym, len) == 0) {

*min = reg_ID;
*max = reg_ID++;

found = 1;
}
else {
reg_ID = 0;
}

for (; reg_ID < lp->reg_MAX_N; ++reg_ID) {

if (lp->reg[reg_ID].sym[0] != 0) {

if (strncmp(lp->reg[reg_ID].sym, sym, n) == 0) {
if (strncmp(lp->reg[reg_ID].sym, sym, len) == 0) {

if (rc == 0) {
if (found == 0) {

priv->cache[hash] = reg_ID;

*min = reg_ID;
rc = 1;
found = 1;
}

*max = reg_ID;
}
else if (rc != 0)
else if (found != 0)
break;
}
}

return rc;
return found;
}

void link_reg_fetch_all_shown(struct link_pmc *lp)
Expand Down
7 changes: 6 additions & 1 deletion phobia/phobia.c
Original file line number Diff line number Diff line change
Expand Up @@ -3099,6 +3099,11 @@ page_hal(struct public *pub)
nk_layout_row_dynamic(ctx, 0, 1);
nk_spacer(ctx);

reg_enum_errno(pub, "hal.MCU_ID", "MCU ID", 0);

nk_layout_row_dynamic(ctx, 0, 1);
nk_spacer(ctx);

reg_float(pub, "hal.USART_baudrate", "USART baudrate");
reg_enum_combo(pub, "hal.USART_parity", "USART parity", 0);

Expand Down Expand Up @@ -5291,7 +5296,7 @@ page_flash(struct public *pub)
nk_spacer(ctx);
pub_drawing_flash_colored(nk, 'a');
nk_spacer(ctx);
nk_label(ctx, "Data block with correct CRC", NK_TEXT_LEFT);
nk_label(ctx, "Data block (with correct CRC)", NK_TEXT_LEFT);

nk_spacer(ctx);
pub_drawing_flash_colored(nk, 'x');
Expand Down
2 changes: 1 addition & 1 deletion src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ $(TARGET): $(BUILD_OBJS)

$(IMAGE): $(TARGET)
@ echo " OC " $(notdir $@)
@ $(OC) -j .text -j .data -O binary -S $< $@
@ $(OC) -O binary -S $< $@

gdb: $(TARGET)
@ echo " GDB " $(notdir $<)
Expand Down
2 changes: 1 addition & 1 deletion src/app/as5047.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ LD_TASK void app_AS5047(void *pData)
{
volatile int *knob = (volatile int *) pData;

SPI_startup(HW_SPI_EXT_ID, AS5047_FREQUENCY, SPI_LOW_FALLING | SPI_DMA);
SPI_startup(HW_SPI_EXT_ID, AS5047_FREQUENCY, SPI_LOW_FALLING | SPI_DMA | SPI_NSS_ON);

hal_memory_fence();

Expand Down
9 changes: 3 additions & 6 deletions src/cherry/usbd_cdc.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
#include "usbd_core.h"
#include "usbd_cdc.h"

const char *stop_name[] = { "1", "1.5", "2" };
const char *parity_name[] = { "N", "O", "E", "M", "S" };

static int cdc_acm_class_interface_request_handler(struct usb_setup_packet *setup, uint8_t **data, uint32_t *len)
{
USB_LOG_DBG("CDC Class request: "
Expand Down Expand Up @@ -40,12 +37,12 @@ static int cdc_acm_class_interface_request_handler(struct usb_setup_packet *setu
/* 6 | bDataBits | 1 | Number Data bits (5, 6, 7, 8 or 16). */
/*******************************************************************************/
memcpy(&line_coding, *data, setup->wLength);
USB_LOG_DBG("Set intf:%d linecoding <%d %d %s %s>\r\n",
USB_LOG_DBG("Set intf:%d linecoding <%d %d %d %d>\r\n",
intf_num,
line_coding.dwDTERate,
line_coding.bDataBits,
parity_name[line_coding.bParityType],
stop_name[line_coding.bCharFormat]);
line_coding.bParityType,
line_coding.bCharFormat);

usbd_cdc_acm_set_line_coding(intf_num, &line_coding);
break;
Expand Down
4 changes: 2 additions & 2 deletions src/epcan.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ enum {
EPCAN_NODE_TX, /* send to remote node */
EPCAN_NODE_GET, /* request to GET register data */
EPCAN_NODE_SET, /* request to SET register data */
EPCAN_NODE_DATA /* reply to GET request */
EPCAN_NODE_DATA /* reply to the GET request */
};

enum {
Expand Down Expand Up @@ -70,7 +70,7 @@ typedef struct {
int ID; /* CAN ID of the endpoint (EP) */
int clock_ID; /* CAN ID used as clock */

float reg_DATA; /* current DATA */
float reg_DATA; /* actual DATA */
int reg_ID; /* linked register ID */

int PAYLOAD; /* packet payload type */
Expand Down
Loading

0 comments on commit e4e8cfc

Please sign in to comment.