Skip to content

Commit

Permalink
bricks/ev3: Implement clock, systick, and wait for interrupt sleep.
Browse files Browse the repository at this point in the history
  • Loading branch information
laurensvalk committed Jun 11, 2024
1 parent 43d18de commit 503152a
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 26 deletions.
8 changes: 4 additions & 4 deletions bricks/ev3/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ typedef long mp_off_t;
// value from disable_irq back to enable_irq. If you really need
// to know the machine-specific values, see irq.h.

#include <arm920t.h>

static inline void enable_irq(mp_uint_t state) {
// __set_PRIMASK(state);
arm_intr_enable();
}

static inline mp_uint_t disable_irq(void) {
// mp_uint_t state = __get_PRIMASK();
// __disable_irq();
// return state;
arm_intr_disable();
return 0;
}

Expand Down
25 changes: 11 additions & 14 deletions bricks/ev3/mphalport.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
#include "py/mpconfig.h"
#include "py/stream.h"

#include <systick.h>
#include <arm920t.h>
#include <am18x_aintc.h>

void pb_stack_get_info(char **sstack, char **estack) {

volatile int stack_dummy;
Expand All @@ -31,24 +35,17 @@ void pb_event_poll_hook_leave(void) {
// have a critical section where we disable interrupts and check see if there
// are any last second events. If not, we can call __WFI(), which still wakes
// up the CPU on interrupt even though interrupts are otherwise disabled.


// TODO: TIAM1808 Implement commented out parts

// mp_uint_t state = disable_irq();
arm_intr_disable();
if (!process_nevents()) {
// __WFI();
arm_wfi();
}
// enable_irq(state);
arm_intr_enable();
}

// Core delay function that does an efficient sleep and may switch thread context.
// If IRQs are enabled then we must have the GIL.
void mp_hal_delay_ms(mp_uint_t Delay) {

// TODO: TIAM1808 implement IRQ enabled check

if (/*__get_PRIMASK() == 0*/ 0) {
if (aintc_get_active() != AINTC_INVALID_ACTIVE) {
// IRQs enabled, so can use systick counter to do the delay
uint32_t start = pbdrv_clock_get_ms();
// Wraparound of tick is taken care of by 2's complement arithmetic.
Expand All @@ -60,7 +57,7 @@ void mp_hal_delay_ms(mp_uint_t Delay) {
} while (pbdrv_clock_get_ms() - start < Delay);
} else {
// IRQs disabled, so need to use a busy loop for the delay.
// TODO: TIAM1808
systick_sleep(Delay);
}
}

Expand All @@ -80,7 +77,7 @@ typedef struct {
} pb_hal_uart_t;

// Sensor port 1
static pb_hal_uart_t UART0 = { .thr = (volatile uint8_t *)0x01D0C000, .lsr = (volatile uint8_t *)0x01D0C014 };
static pb_hal_uart_t DBG_UART = { .thr = (volatile uint8_t *)0x01D0C000, .lsr = (volatile uint8_t *)0x01D0C014 };

static void debug(pb_hal_uart_t *uart, const char *s) {
while (*s) {
Expand All @@ -91,7 +88,7 @@ static void debug(pb_hal_uart_t *uart, const char *s) {
}

void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
debug(&UART0, str);
debug(&DBG_UART, str);
// MICROPY_EVENT_POLL_HOOK
}

Expand Down
11 changes: 5 additions & 6 deletions lib/am18x-lib/prj/am1808exp.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "am1808exp.h"
#include "am18x_type.h"

#define SYSTICK_PERIOD 10/* milli seconds */
#define SYSTICK_PERIOD_MS (1)


const ddr_conf_t mt46h64m16_6 = {
Expand Down Expand Up @@ -71,16 +71,15 @@ int low_level_init(void) {

// ddr_initialize(DDR0, &mt46h64m16_6);

if (AM18X_OK != (r = systick_init(SYSTICK_PERIOD))) {
// printk("systick_init() error\n");
if (AM18X_OK != (r = systick_init(SYSTICK_PERIOD_MS))) {
printk("systick_init() error\n");
return r;
}

/* if (AM18X_OK != (r = systick_start())) {
if (AM18X_OK != (r = systick_start())) {
printk("systick_start() error\n");
return r;
}
*/
// invalid operation ?
sata_100m_clk_enable(AM18X_FALSE);

Expand All @@ -90,7 +89,7 @@ int low_level_init(void) {

// tps6507x_conf();

// printk("ARM CLK: %9d Hz\n", dev_get_freq(DCLK_ID_ARM));
printk("ARM CLK: %9d Hz\n", dev_get_freq(DCLK_ID_ARM));

return AM18X_OK;
}
2 changes: 2 additions & 0 deletions lib/am18x-lib/prj/am18x_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ extern const uint32_t f_osc;
#define assert(expr) ((expr) ? (void)0 : assert_failed((uint8_t *)__FILE__, __LINE__))
void assert_failed(uint8_t* file, uint32_t line);
#else
#ifndef assert
#define assert(expr) ((void)0)
#endif
#endif // DEBUG

typedef void (*none_arg_handler_t)(void);
Expand Down
16 changes: 14 additions & 2 deletions lib/pbio/drv/clock/clock_tiam1808.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,23 @@

#include <stdint.h>

#include <contiki.h>

#include <arm920t.h>
#include <systick.h>

/* High priority handler, called 1000 times a second */
static int systick_isr(int ticks) {
etimer_request_poll();
return 0;
}

void pbdrv_clock_init(void) {
// Actual low level clocks should probably be configured at the very
// start of in SystemInit in platform.c instead.
// But optionally, additional things can be initialized here.
systick_set_handler(systick_isr);
arm_intr_enable();
}

uint32_t pbdrv_clock_get_us(void) {
Expand All @@ -19,8 +32,7 @@ uint32_t pbdrv_clock_get_us(void) {
}

uint32_t pbdrv_clock_get_ms(void) {
// TODO: TIAM1808 implementation.
return 0;
return systick_elapsed();
}

uint32_t pbdrv_clock_get_100us(void) {
Expand Down

0 comments on commit 503152a

Please sign in to comment.