Skip to content

Commit

Permalink
Merge pull request #19 from kendryte/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
zzxcanaan authored Nov 6, 2018
2 parents c68cb82 + f52b208 commit 6d05eba
Show file tree
Hide file tree
Showing 17 changed files with 195 additions and 127 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ CMakeSettings.json
.idea
*.tar
*.tar.*
cmake-build-*
cmake-build-*
src
~src/hello_world
*.kdev4
5 changes: 4 additions & 1 deletion cmake/executable.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ set_target_properties(${PROJECT_NAME} PROPERTIES LINKER_LANGUAGE C)

target_link_libraries(${PROJECT_NAME}
-Wl,--start-group
gcc m c kendryte
gcc m c
-Wl,--whole-archive
kendryte
-Wl,--no-whole-archive
-Wl,--end-group
)

Expand Down
3 changes: 2 additions & 1 deletion lds/kendryte.ld
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ PROVIDE( _ram_start = ORIGIN(ram) );
PROVIDE( _ram_end = ORIGIN(ram) + LENGTH(ram) );
PROVIDE( _io_start = 0x40000000 );
PROVIDE( _io_end = _io_start + LENGTH(ram) );
PROVIDE( _stack_size = 1 << 17 );
PROVIDE( _stack_size = 1 << 16 );


/*
Expand Down Expand Up @@ -244,6 +244,7 @@ SECTIONS
PROVIDE( _tp1 = _tp0 + _stack_size );
PROVIDE( _sp0 = _tp0 + _stack_size );
PROVIDE( _sp1 = _tp1 + _stack_size );

/* Heap end is at the end of memory, the memory size can set in kconfig */
PROVIDE( _heap_end = _ram_end );
}
Expand Down
16 changes: 2 additions & 14 deletions lib/bsp/crt.S
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
# define LFREG fld
# define SFREG fsd
# define REGBYTES 8
# define STKSHIFT 17
# define STKSHIFT 16


.section .text.start, "ax", @progbits
Expand Down Expand Up @@ -72,14 +72,6 @@ _start:
li x30,0
li x31,0

csrr t0, misa
bltz t0, 1f
li a0, 1234
j sys_exit
1:

andi t0, t0, 1 << ('f' - 'a')
beqz t0, 1f
li t0, MSTATUS_FS
csrs mstatus, t0

Expand Down Expand Up @@ -117,17 +109,13 @@ _start:
fmv.d.x f30,x0
fmv.d.x f31,x0

1:
.option push
.option norelax
la gp, __global_pointer$
la gp, __global_pointer$
.option pop
la tp, _end + 63
and tp, tp, -64
csrr a0, mhartid
li a1, 2

1:bgeu a0, a1, 1b

sll a2, a0, STKSHIFT
add tp, tp, a2
Expand Down
7 changes: 5 additions & 2 deletions lib/bsp/entry_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ void _init_bsp(int core_id, int number_of_cores)
extern int main(int argc, char* argv[]);
extern void __libc_init_array(void);
extern void __libc_fini_array(void);
/* Initialize thread local data */
init_tls();

if (core_id == 0)
{
Expand Down Expand Up @@ -101,3 +99,8 @@ void _init_bsp(int core_id, int number_of_cores)
exit(ret);
}

int pthread_setcancelstate(int __state, int *__oldstate)
{
return 0;
}

2 changes: 1 addition & 1 deletion lib/bsp/interrupt.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ handle_irq_m_soft(uintptr_t cause, uintptr_t epc, uintptr_t regs[32], uintptr_t
uintptr_t __attribute__((weak, alias("handle_irq_dummy")))
handle_irq_m_timer(uintptr_t cause, uintptr_t epc, uintptr_t regs[32], uintptr_t fregs[32]);

uintptr_t __attribute__((weak, alias("handle_irq_dummy")))
extern uintptr_t
handle_irq_m_ext(uintptr_t cause, uintptr_t epc, uintptr_t regs[32], uintptr_t fregs[32]);

uintptr_t __attribute__((weak))
Expand Down
2 changes: 1 addition & 1 deletion lib/bsp/syscalls.c
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ handle_ecall(uintptr_t cause, uintptr_t epc, uintptr_t regs[32], uintptr_t fregs
regs[17] /* n */
);

return epc + ((*(unsigned short *)epc & 3) == 3 ? 4 : 2);
return epc + 4;
}

uintptr_t __attribute__((weak, alias("handle_ecall")))
Expand Down
44 changes: 0 additions & 44 deletions lib/drivers/common.c

This file was deleted.

60 changes: 60 additions & 0 deletions lib/drivers/i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@
#include "string.h"
#include "sysctl.h"

typedef struct _i2c_slave_context
{
uint32_t i2c_num;
const i2c_slave_handler_t *slave_handler;
} i2c_slave_context_t;

i2c_slave_context_t slave_context;

volatile i2c_t* const i2c[3] =
{
(volatile i2c_t*)I2C0_BASE_ADDR,
Expand Down Expand Up @@ -65,6 +73,58 @@ void i2c_init(i2c_device_number_t i2c_num, uint32_t slave_address, uint32_t addr
i2c_adapter->enable = I2C_ENABLE_ENABLE;
}

static int i2c_slave_irq(void *userdata)
{
i2c_slave_context_t *context = (i2c_slave_context_t *)userdata;
volatile i2c_t *i2c_adapter = i2c[context->i2c_num];
uint32_t status = i2c_adapter->intr_stat;
if (status & I2C_INTR_STAT_START_DET)
{
context->slave_handler->on_event(I2C_EV_START);
readl(&i2c_adapter->clr_start_det);
}
if (status & I2C_INTR_STAT_STOP_DET)
{
context->slave_handler->on_event(I2C_EV_STOP);
readl(&i2c_adapter->clr_stop_det);
}
if (status & I2C_INTR_STAT_RX_FULL)
{
context->slave_handler->on_receive(i2c_adapter->data_cmd);
}
if (status & I2C_INTR_STAT_RD_REQ)
{
i2c_adapter->data_cmd = context->slave_handler->on_transmit();
readl(&i2c_adapter->clr_rd_req);
}
return 0;
}

void i2c_init_as_slave(i2c_device_number_t i2c_num, uint32_t slave_address, uint32_t address_width,
const i2c_slave_handler_t *handler)
{
configASSERT(address_width == 7 || address_width == 10);
volatile i2c_t *i2c_adapter = i2c[i2c_num];
slave_context.i2c_num = i2c_num;
slave_context.slave_handler = handler;

i2c_clk_init(i2c_num);
i2c_adapter->enable = 0;
i2c_adapter->con = (address_width == 10 ? I2C_CON_10BITADDR_SLAVE : 0) | I2C_CON_SPEED(1) | I2C_CON_STOP_DET_IFADDRESSED;
i2c_adapter->ss_scl_hcnt = I2C_SS_SCL_HCNT_COUNT(37);
i2c_adapter->ss_scl_lcnt = I2C_SS_SCL_LCNT_COUNT(40);
i2c_adapter->sar = I2C_SAR_ADDRESS(slave_address);
i2c_adapter->rx_tl = I2C_RX_TL_VALUE(0);
i2c_adapter->tx_tl = I2C_TX_TL_VALUE(0);
i2c_adapter->intr_mask = I2C_INTR_MASK_RX_FULL | I2C_INTR_MASK_START_DET | I2C_INTR_MASK_STOP_DET | I2C_INTR_MASK_RD_REQ;

plic_set_priority(IRQN_I2C0_INTERRUPT + i2c_num, 1);
plic_irq_register(IRQN_I2C0_INTERRUPT + i2c_num, i2c_slave_irq, &slave_context);
plic_irq_enable(IRQN_I2C0_INTERRUPT + i2c_num);

i2c_adapter->enable = I2C_ENABLE_ENABLE;
}

int i2c_send_data(i2c_device_number_t i2c_num, const uint8_t *send_buf, size_t send_buf_len)
{
configASSERT(i2c_num < I2C_MAX_NUM);
Expand Down
25 changes: 25 additions & 0 deletions lib/drivers/include/i2c.h
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,20 @@ typedef enum _i2c_bus_speed_mode
I2C_BS_HIGHSPEED
} i2c_bus_speed_mode_t;

typedef enum _i2c_event
{
I2C_EV_START,
I2C_EV_RESTART,
I2C_EV_STOP
} i2c_event_t;

typedef struct _i2c_slave_handler
{
void(*on_receive)(uint32_t data);
uint32_t(*on_transmit)();
void(*on_event)(i2c_event_t event);
} i2c_slave_handler_t;

/**
* @brief Set i2c params
*
Expand All @@ -364,6 +378,17 @@ void i2c_init(i2c_device_number_t i2c_num, uint32_t slave_address, uint32_t addr
*/
int i2c_send_data(i2c_device_number_t i2c_num, const uint8_t *send_buf, size_t send_buf_len);

/**
* @brief Init i2c as slave mode.
*
* @param[in] i2c_num i2c number
* @param[in] slave_address i2c slave device address
* @param[in] address_width address width 7bit or 10bit
* @param[in] handler Handle of i2c slave interrupt function.
*/
void i2c_init_as_slave(i2c_device_number_t i2c_num, uint32_t slave_address, uint32_t address_width,
const i2c_slave_handler_t *handler);

/**
* @brief I2c send data by dma
*
Expand Down
7 changes: 4 additions & 3 deletions lib/drivers/kpu.h → lib/drivers/include/kpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -312,11 +312,12 @@ extern volatile kpu_config_t *const kpu;
typedef struct
{
kpu_layer_argument_t* layers;
uint32_t length;
int dma_ch;
uint64_t* dst;
uint32_t dst_length;
plic_irq_callback_t cb;
uint32_t length;
uint32_t dst_length;
uint32_t dma_ch;
uint32_t eight_bit_mode;
} kpu_task_t;

/**
Expand Down
7 changes: 4 additions & 3 deletions lib/drivers/kpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "printf.h"
#include "dmac.h"
#include <string.h>
#include "bsp.h"

volatile kpu_config_t *const kpu = (volatile kpu_config_t *)AI_BASE_ADDR;

Expand All @@ -19,7 +20,7 @@ volatile kpu_context_t g_kpu_context;

static int kpu_run_all_done(void* _task)
{
g_kpu_context.kpu_status = 0;
atomic_swap(&g_kpu_context.kpu_status, 0);
kpu_task_t* task = (kpu_task_t*)_task;
task->cb(task);
return 0;
Expand Down Expand Up @@ -102,7 +103,7 @@ static int kpu_run_dma_input_done_push_layers(void* _task)
};
kpu->eight_bit_mode.data = (kpu_config_eight_bit_mode_t)
{
.eight_bit_mode=0
.eight_bit_mode=task->eight_bit_mode
};

kpu_layer_argument_t* last_layer = &task->layers[task->length-1];
Expand Down Expand Up @@ -132,7 +133,7 @@ static void kpu_run_dma_input(uint32_t dma_ch, const void* src, plic_irq_callbac

int kpu_run(kpu_task_t* v_task, dmac_channel_number_t dma_ch, const void *src, void* dest, plic_irq_callback_t callback)
{
if(g_kpu_context.kpu_status)
if(atomic_cas(&g_kpu_context.kpu_status, 0, 1))
return -1;

memcpy((void *)&g_kpu_context.kpu_task, v_task, sizeof(kpu_task_t));
Expand Down
3 changes: 2 additions & 1 deletion lib/drivers/plic.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ void plic_irq_deregister(plic_irq_t irq)
}

/*Entry Point for PLIC Interrupt Handler*/
uintptr_t handle_irq_m_ext(uintptr_t cause, uintptr_t epc)
uintptr_t __attribute__((weak))
handle_irq_m_ext(uintptr_t cause, uintptr_t epc)
{
/*
* After the highest-priority pending interrupt is claimed by a target
Expand Down
Loading

0 comments on commit 6d05eba

Please sign in to comment.