Skip to content

Commit

Permalink
soc: sdk: update ahb_mailbox & ahb_iopmp modules
Browse files Browse the repository at this point in the history
  • Loading branch information
redchenjs committed May 23, 2023
1 parent 45dfc24 commit 84359fd
Show file tree
Hide file tree
Showing 13 changed files with 180 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@
</BuildConfig>
</BuildConfigs>
<DebugSessions>
<watchExpressions>mail_acked:1;mail_pending:1;MBOX_L_CTRL_REG:1;MBOX_R_CTRL_REG:1</watchExpressions>
<watchExpressions/>
<memoryExpressions>0x10000000;;;</memoryExpressions>
<statistics>;;32;;MHZ;;up</statistics>
<peripheralTabs/>
Expand Down
44 changes: 38 additions & 6 deletions sdk/projects/apps/wujian100_open-app0/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

#include "pmu.h"
#include "pmp.h"
Expand All @@ -19,8 +21,15 @@ extern void mdelay(uint32_t ms);

int main(void)
{
bool send_msg = false;
bool send_ack = false;

uint8_t send_msg_id = 0;
uint8_t send_ack_id = 0;

uint8_t loop = 100;
uint8_t mail_id = 0;
int16_t mail_len = 0;
uint8_t mail_buff[1024] = {0};

pmu_set_reset(0);
Expand All @@ -39,6 +48,9 @@ int main(void)

if (mbox_send_message(loop, mail_buff, strlen((char *)mail_buff)) >= 0) {
printf("app0: mail %u is sent to remote core\n", loop);
} else {
send_msg_id = loop;
send_msg = true;
}

do {
Expand All @@ -47,20 +59,40 @@ int main(void)
pwm_test();

if (mbox_check_acked()) {
snprintf((char *)mail_buff, sizeof(mail_buff), "Hello from core 0! The message id is %u", loop);
mbox_read_ack(&mail_id);

printf("app0: mail %u is acked by remote core\n", mail_id);

send_msg_id = loop;
send_msg = true;
}

if (send_msg) {
snprintf((char *)mail_buff, sizeof(mail_buff), "Hello from core 0! The message id is %u", send_msg_id);

if (mbox_send_message(loop, mail_buff, strlen((char *)mail_buff)) >= 0) {
printf("app0: mail %u is acked by remote core\n", mbox_read_ack());
printf("app0: mail %u is sent to remote core\n", loop);
printf("app0: mail %u is sent to remote core\n", send_msg_id);

send_msg = false;
}
}

if (mbox_check_pending()) {
if (mbox_send_ack(mail_id) >= 0) {
mbox_read_message(&mail_id, mail_buff, sizeof(mail_buff));
if ((mail_len = mbox_read_message(&mail_id, mail_buff, sizeof(mail_buff))) >= 0) {
mail_buff[mail_len] = 0x00;

printf("app0: mail %u is received: %s\n", mail_id, (char *)mail_buff);
printf("app0: mail %u is acked\n", mail_id);

send_ack_id = mail_id;
send_ack = true;
}
}

if (send_ack) {
if (mbox_send_ack(send_ack_id) >= 0) {
printf("app0: mail %u is acked\n", send_ack_id);

send_ack = false;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@
</BuildConfig>
</BuildConfigs>
<DebugSessions>
<watchExpressions>mail_acked:1;mail_pending:1;MBOX_L_CTRL_REG:1;MBOX_R_CTRL_REG:1</watchExpressions>
<watchExpressions/>
<memoryExpressions>0x20000000;;;</memoryExpressions>
<statistics>;;32;;MHZ;;up</statistics>
<peripheralTabs/>
Expand Down
2 changes: 1 addition & 1 deletion sdk/projects/apps/wujian100_open-app1/board/include/pin.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
extern "C" {
#endif

#define CLOCK_GETTIME_USE_TIMER_ID 0
#define CLOCK_GETTIME_USE_TIMER_ID 2
#define UART_TXD0 1
#define UART_RXD0 2

Expand Down
44 changes: 38 additions & 6 deletions sdk/projects/apps/wujian100_open-app1/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

#include "gpio.h"
#include "mbox.h"
Expand All @@ -17,8 +19,15 @@ extern void mdelay(uint32_t ms);

int main(void)
{
bool send_msg = false;
bool send_ack = false;

uint8_t send_msg_id = 0;
uint8_t send_ack_id = 0;

uint8_t loop = 200;
uint8_t mail_id = 0;
int16_t mail_len = 0;
uint8_t mail_buff[1024] = {0};

gpio_init();
Expand All @@ -30,6 +39,9 @@ int main(void)

if (mbox_send_message(loop, mail_buff, strlen((char *)mail_buff)) >= 0) {
printf("app1: mail %u is sent to remote core\n", loop);
} else {
send_msg_id = loop;
send_msg = true;
}

do {
Expand All @@ -38,20 +50,40 @@ int main(void)
gpio_toggle();

if (mbox_check_acked()) {
snprintf((char *)mail_buff, sizeof(mail_buff), "Hello from core 1! The message id is %u", loop);
mbox_read_ack(&mail_id);

printf("app1: mail %u is acked by remote core\n", mail_id);

send_msg_id = loop;
send_msg = true;
}

if (send_msg) {
snprintf((char *)mail_buff, sizeof(mail_buff), "Hello from core 1! The message id is %u", send_msg_id);

if (mbox_send_message(loop, mail_buff, strlen((char *)mail_buff)) >= 0) {
printf("app1: mail %u is acked by remote core\n", mbox_read_ack());
printf("app1: mail %u is sent to remote core\n", loop);
printf("app1: mail %u is sent to remote core\n", send_msg_id);

send_msg = false;
}
}

if (mbox_check_pending()) {
if (mbox_send_ack(mail_id) >= 0) {
mbox_read_message(&mail_id, mail_buff, sizeof(mail_buff));
if ((mail_len = mbox_read_message(&mail_id, mail_buff, sizeof(mail_buff))) >= 0) {
mail_buff[mail_len] = 0x00;

printf("app1: mail %u is received: %s\n", mail_id, (char *)mail_buff);
printf("app1: mail %u is acked\n", mail_id);

send_ack_id = mail_id;
send_ack = true;
}
}

if (send_ack) {
if (mbox_send_ack(send_ack_id) >= 0) {
printf("app1: mail %u is acked\n", send_ack_id);

send_ack = false;
}
}

Expand Down
2 changes: 1 addition & 1 deletion sdk/projects/apps/wujian100_open-driver/include/mbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ extern void mbox_init(uint8_t core_id);
extern bool mbox_check_acked(void);
extern bool mbox_check_pending(void);

extern int mbox_read_ack(void);
extern int mbox_read_ack(uint8_t *id);
extern int mbox_read_message(uint8_t *id, void *buff, uint32_t buff_size);

extern int mbox_send_ack(uint8_t id);
Expand Down
102 changes: 58 additions & 44 deletions sdk/projects/apps/wujian100_open-driver/mbox.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,32 @@
#include "drv_irq.h"

// Mailbox Functions
#define MAILBOX_IRQ_NUM (42)
#define MAILBOX_RAM_MAX (32 * 1024)
#define MBOX_IRQ_NUM (42)
#define MBOX_RAM_SIZE (32 * 1024)

#define MBOX_0_CTRL_REG_BASE (0x40010000)
#define MBOX_0_DATA_RAM_BASE (0x40018000)
#define MBOX_0_CTRL_0_REG_BASE (0x40010000)
#define MBOX_0_CTRL_1_REG_BASE (0x40010004)
#define MBOX_0_DATA_0_RAM_BASE (0x40018000)

#define MBOX_1_CTRL_REG_BASE (0x40020000)
#define MBOX_1_DATA_RAM_BASE (0x40028000)
#define MBOX_1_CTRL_0_REG_BASE (0x40020000)
#define MBOX_1_CTRL_1_REG_BASE (0x40020004)
#define MBOX_1_DATA_0_RAM_BASE (0x40028000)

#define MBOX_INTR_BIT (0x80000000)
#define MBOX_RESP_BIT (0x40000000)
#define MBOX_FULL_BIT (0x20000000)
#define MBOX_INTR_BIT (0x80000000)
#define MBOX_FULL_BIT (0x40000000)

volatile uint32_t *MBOX_L_CTRL_REG_BASE = NULL;
volatile uint32_t *MBOX_L_DATA_RAM_BASE = NULL;
volatile uint32_t *MBOX_L_CTRL_0_REG_BASE = NULL;
volatile uint32_t *MBOX_L_CTRL_1_REG_BASE = NULL;
volatile uint32_t *MBOX_L_DATA_0_RAM_BASE = NULL;

volatile uint32_t *MBOX_R_CTRL_REG_BASE = NULL;
volatile uint32_t *MBOX_R_DATA_RAM_BASE = NULL;
volatile uint32_t *MBOX_R_CTRL_0_REG_BASE = NULL;
volatile uint32_t *MBOX_R_CTRL_1_REG_BASE = NULL;
volatile uint32_t *MBOX_R_DATA_0_RAM_BASE = NULL;

#define MBOX_L_CTRL_REG (*(volatile uint32_t *)MBOX_L_CTRL_REG_BASE)
#define MBOX_R_CTRL_REG (*(volatile uint32_t *)MBOX_R_CTRL_REG_BASE)
#define MBOX_L_CTRL_0_REG (*(volatile uint32_t *)MBOX_L_CTRL_0_REG_BASE)
#define MBOX_L_CTRL_1_REG (*(volatile uint32_t *)MBOX_L_CTRL_1_REG_BASE)
#define MBOX_R_CTRL_0_REG (*(volatile uint32_t *)MBOX_R_CTRL_0_REG_BASE)
#define MBOX_R_CTRL_1_REG (*(volatile uint32_t *)MBOX_R_CTRL_1_REG_BASE)

uint8_t mail_id = 0;

Expand All @@ -41,35 +46,44 @@ bool mail_pending = false;

void mbox_irq_handler(void)
{
if (MBOX_L_CTRL_REG & MBOX_RESP_BIT) {
if (MBOX_L_CTRL_1_REG & MBOX_INTR_BIT) {
MBOX_L_CTRL_1_REG &=~MBOX_INTR_BIT;

mail_acked = true;
} else {
mail_pending = true;
}

MBOX_L_CTRL_REG &=~MBOX_INTR_BIT;
if (MBOX_L_CTRL_0_REG & MBOX_INTR_BIT) {
MBOX_L_CTRL_0_REG &=~MBOX_INTR_BIT;

mail_pending = true;
}
}

void mbox_init(uint8_t core_id)
{
if (core_id == 0) {
MBOX_L_CTRL_REG_BASE = (volatile uint32_t *)MBOX_0_CTRL_REG_BASE;
MBOX_L_DATA_RAM_BASE = (volatile uint32_t *)MBOX_0_DATA_RAM_BASE;
MBOX_L_CTRL_0_REG_BASE = (volatile uint32_t *)MBOX_0_CTRL_0_REG_BASE;
MBOX_L_CTRL_1_REG_BASE = (volatile uint32_t *)MBOX_0_CTRL_1_REG_BASE;
MBOX_L_DATA_0_RAM_BASE = (volatile uint32_t *)MBOX_0_DATA_0_RAM_BASE;

MBOX_R_CTRL_REG_BASE = (volatile uint32_t *)MBOX_1_CTRL_REG_BASE;
MBOX_R_DATA_RAM_BASE = (volatile uint32_t *)MBOX_1_DATA_RAM_BASE;
MBOX_R_CTRL_0_REG_BASE = (volatile uint32_t *)MBOX_1_CTRL_0_REG_BASE;
MBOX_R_CTRL_1_REG_BASE = (volatile uint32_t *)MBOX_1_CTRL_1_REG_BASE;
MBOX_R_DATA_0_RAM_BASE = (volatile uint32_t *)MBOX_1_DATA_0_RAM_BASE;
} else {
MBOX_L_CTRL_REG_BASE = (volatile uint32_t *)MBOX_1_CTRL_REG_BASE;
MBOX_L_DATA_RAM_BASE = (volatile uint32_t *)MBOX_1_DATA_RAM_BASE;
MBOX_L_CTRL_0_REG_BASE = (volatile uint32_t *)MBOX_1_CTRL_0_REG_BASE;
MBOX_L_CTRL_1_REG_BASE = (volatile uint32_t *)MBOX_1_CTRL_1_REG_BASE;
MBOX_L_DATA_0_RAM_BASE = (volatile uint32_t *)MBOX_1_DATA_0_RAM_BASE;

MBOX_R_CTRL_REG_BASE = (volatile uint32_t *)MBOX_0_CTRL_REG_BASE;
MBOX_R_DATA_RAM_BASE = (volatile uint32_t *)MBOX_0_DATA_RAM_BASE;
MBOX_R_CTRL_0_REG_BASE = (volatile uint32_t *)MBOX_0_CTRL_0_REG_BASE;
MBOX_R_CTRL_1_REG_BASE = (volatile uint32_t *)MBOX_0_CTRL_1_REG_BASE;
MBOX_R_DATA_0_RAM_BASE = (volatile uint32_t *)MBOX_0_DATA_0_RAM_BASE;
}

MBOX_L_CTRL_REG = 0x00000000;
MBOX_L_CTRL_0_REG = 0x00000000;
MBOX_L_CTRL_1_REG = 0x00000000;

drv_irq_register(MAILBOX_IRQ_NUM, mbox_irq_handler);
drv_irq_enable(MAILBOX_IRQ_NUM);
drv_irq_register(MBOX_IRQ_NUM, mbox_irq_handler);
drv_irq_enable(MBOX_IRQ_NUM);
}

bool mbox_check_acked(void)
Expand All @@ -82,60 +96,60 @@ bool mbox_check_pending(void)
return mail_pending;
}

int mbox_read_ack(void)
int mbox_read_ack(uint8_t *id)
{
uint8_t id = MBOX_L_CTRL_REG & 0xff;
*id = MBOX_L_CTRL_1_REG & 0xff;

mail_acked = false;

MBOX_L_CTRL_REG &=~MBOX_FULL_BIT;
MBOX_L_CTRL_1_REG &=~MBOX_FULL_BIT;

return id;
return 0;
}

int mbox_read_message(uint8_t *id, void *buff, uint32_t buff_size)
{
uint16_t size = (MBOX_L_CTRL_REG & 0x7fff00) >> 8;
uint16_t size = (MBOX_L_CTRL_0_REG & 0x7fff00) >> 8;

if (size > buff_size) {
return -1;
}

*id = MBOX_L_CTRL_REG & 0xff;
*id = MBOX_L_CTRL_0_REG & 0xff;

memcpy(buff, (void *)MBOX_L_DATA_RAM_BASE, size);
memcpy(buff, (void *)MBOX_L_DATA_0_RAM_BASE, size);

mail_pending = false;

MBOX_L_CTRL_REG &=~MBOX_FULL_BIT;
MBOX_L_CTRL_0_REG &=~MBOX_FULL_BIT;

return size;
}

int mbox_send_ack(uint8_t id)
{
if (MBOX_R_CTRL_REG & MBOX_FULL_BIT) {
if (MBOX_R_CTRL_1_REG & MBOX_FULL_BIT) {
return -1;
}

MBOX_R_CTRL_REG = id | MBOX_FULL_BIT | MBOX_RESP_BIT | MBOX_INTR_BIT;
MBOX_R_CTRL_1_REG = id | MBOX_FULL_BIT | MBOX_INTR_BIT;

return 0;
}

int mbox_send_message(uint8_t id, const void *buff, uint32_t len)
{
if (MBOX_R_CTRL_REG & MBOX_FULL_BIT) {
if (MBOX_R_CTRL_0_REG & MBOX_FULL_BIT) {
return -1;
}

if (len > MAILBOX_RAM_MAX) {
if (len > MBOX_RAM_SIZE) {
return -2;
}

memcpy((void *)MBOX_R_DATA_RAM_BASE, buff, len);
memcpy((void *)MBOX_R_DATA_0_RAM_BASE, buff, len);

MBOX_R_CTRL_REG = id | (len << 8) | MBOX_FULL_BIT | MBOX_INTR_BIT;
MBOX_R_CTRL_0_REG = id | (len << 8) | MBOX_FULL_BIT | MBOX_INTR_BIT;

return len;
}
Loading

0 comments on commit 84359fd

Please sign in to comment.