Add can_init() and can_isr()

This commit is contained in:
Aadi Desai 2023-06-10 14:07:36 +01:00
parent be6ab940ff
commit 9da49fa5e3
No known key found for this signature in database
3 changed files with 38 additions and 2 deletions

View file

@ -21,3 +21,7 @@ uint32_t can_mask_read(void);
void can_mask_write(uint32_t value); void can_mask_write(uint32_t value);
can_frame can_read(void); can_frame can_read(void);
void can_isr(void);
void can_init(void);

View file

@ -1,6 +1,10 @@
#include "can" #include "can"
#include <generated/csr.h> #include <generated/csr.h>
#include <irq.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h>
char *readstr(bool);
#ifdef CSR_CAN_BASE #ifdef CSR_CAN_BASE
uint32_t can_id_read(void) { uint32_t can_id_read(void) {
@ -32,4 +36,22 @@ can_frame can_read(void) {
frame.data[7] = can_rcv_data7_read(); frame.data[7] = can_rcv_data7_read();
return frame; 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 #endif

View file

@ -17,12 +17,14 @@
/* Uart */ /* Uart */
/*-----------------------------------------------------------------------*/ /*-----------------------------------------------------------------------*/
static char *readstr(void) { char *readstr(bool print) {
char c[2]; char c[2];
static char s[64]; static char s[64];
static unsigned int ptr = 0; static unsigned int ptr = 0;
if (readchar_nonblock()) { if (print) {
fputs(s, stdout);
} else if (readchar_nonblock()) {
c[0] = getchar(); c[0] = getchar();
c[1] = 0; c[1] = 0;
switch (c[0]) { switch (c[0]) {
@ -54,6 +56,10 @@ static char *readstr(void) {
return NULL; return NULL;
} }
static char *readstr(void) {
return readstr(false);
}
static char *get_token(char **str) { static char *get_token(char **str) {
char *c, *d; char *c, *d;
@ -439,17 +445,21 @@ static void console_service(void) {
prompt(); prompt();
} }
void isr(void);
int main(void) { int main(void) {
#ifdef CONFIG_CPU_HAS_INTERRUPT #ifdef CONFIG_CPU_HAS_INTERRUPT
irq_setmask(0); irq_setmask(0);
irq_setie(1); irq_setie(1);
#endif #endif
uart_init(); uart_init();
can_init();
help(); help();
prompt(); prompt();
while (1) { while (1) {
isr();
console_service(); console_service();
} }
} }