mirror of
https://github.com/supleed2/EIE4-FYP.git
synced 2024-12-22 06:05:49 +00:00
Add can_init()
and can_isr()
This commit is contained in:
parent
be6ab940ff
commit
9da49fa5e3
4
demo/can
4
demo/can
|
@ -21,3 +21,7 @@ uint32_t can_mask_read(void);
|
|||
void can_mask_write(uint32_t value);
|
||||
|
||||
can_frame can_read(void);
|
||||
|
||||
void can_isr(void);
|
||||
|
||||
void can_init(void);
|
||||
|
|
22
demo/can.cpp
22
demo/can.cpp
|
@ -1,6 +1,10 @@
|
|||
#include "can"
|
||||
#include <generated/csr.h>
|
||||
#include <irq.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
char *readstr(bool);
|
||||
|
||||
#ifdef CSR_CAN_BASE
|
||||
uint32_t can_id_read(void) {
|
||||
|
@ -32,4 +36,22 @@ can_frame can_read(void) {
|
|||
frame.data[7] = can_rcv_data7_read();
|
||||
return frame;
|
||||
}
|
||||
|
||||
void can_isr(void) {
|
||||
can_ev_pending_frame_write(1); // Should use `can_ev_pending_read()` and check which interrupt, but there is only 1
|
||||
leds_out_write(leds_out_read() ^ 0xFF0000); // Toggle Red LED
|
||||
can_frame frame = can_read();
|
||||
printf("\033[F\033[F\33[2K\nCAN frame received, ID: 0x%03X, data: 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X\n",
|
||||
frame.id, frame.data[0], frame.data[1], frame.data[2], frame.data[3], frame.data[4], frame.data[5],
|
||||
frame.data[6], frame.data[7]); // Print CAN frame to UART
|
||||
can_ev_enable_frame_write(1); // Re-enable event handler, same as in `can_init()`
|
||||
printf("\e[92;1mStackSynth\e[0m> "); // Print prompt to UART
|
||||
readstr(true); // Print current user input (if any)
|
||||
}
|
||||
|
||||
void can_init(void) {
|
||||
irq_setmask(irq_getmask() | (1 << CAN_INTERRUPT)); // Enable CAN interrupt
|
||||
can_ev_enable_frame_write(1); // Should be `can_ev_enable_frame_write(1)` but it is equivalent
|
||||
printf("CAN INIT\n"); // Print debug message to UART
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -17,12 +17,14 @@
|
|||
/* Uart */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
|
||||
static char *readstr(void) {
|
||||
char *readstr(bool print) {
|
||||
char c[2];
|
||||
static char s[64];
|
||||
static unsigned int ptr = 0;
|
||||
|
||||
if (readchar_nonblock()) {
|
||||
if (print) {
|
||||
fputs(s, stdout);
|
||||
} else if (readchar_nonblock()) {
|
||||
c[0] = getchar();
|
||||
c[1] = 0;
|
||||
switch (c[0]) {
|
||||
|
@ -54,6 +56,10 @@ static char *readstr(void) {
|
|||
return NULL;
|
||||
}
|
||||
|
||||
static char *readstr(void) {
|
||||
return readstr(false);
|
||||
}
|
||||
|
||||
static char *get_token(char **str) {
|
||||
char *c, *d;
|
||||
|
||||
|
@ -439,17 +445,21 @@ static void console_service(void) {
|
|||
prompt();
|
||||
}
|
||||
|
||||
void isr(void);
|
||||
|
||||
int main(void) {
|
||||
#ifdef CONFIG_CPU_HAS_INTERRUPT
|
||||
irq_setmask(0);
|
||||
irq_setie(1);
|
||||
#endif
|
||||
uart_init();
|
||||
can_init();
|
||||
|
||||
help();
|
||||
prompt();
|
||||
|
||||
while (1) {
|
||||
isr();
|
||||
console_service();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue