summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Lamparter <equinox@diac24.net>2014-12-07 16:47:06 +0100
committerDavid Lamparter <equinox@diac24.net>2014-12-07 16:47:06 +0100
commit2861294d9b9f159b63696bfbe5a0e2cbb8d56124 (patch)
tree5eec53f8240065c8ef485af58c9a4fe7eb71542b
parentc0d62add7e20301c80e829924dd523f6390c1f32 (diff)
can: some improvements
- make debug conditional - read status from MCP2515 - put RX packets in struct
-rw-r--r--can.c49
1 files changed, 44 insertions, 5 deletions
diff --git a/can.c b/can.c
index 739ae68..143d311 100644
--- a/can.c
+++ b/can.c
@@ -73,6 +73,7 @@ static void spi_performpgm(const uint8_t * PROGMEM cmds, uint8_t len)
#define MCP2515_WRTXB_TXB0 0x00
#define MCP2515_WRTXB_TXB1 0x02
#define MCP2515_WRTXB_TXB2 0x04
+#define MCP2515_RDSTAT 0xa0
#define A_CNF3 0x28
#define A_CANINTF 0x2c
@@ -150,12 +151,32 @@ static void can_send(uint32_t daddr, uint8_t len, const uint8_t *data)
can_irq_sei();
}
+static bool can_tx_busy(void)
+{
+ uint8_t status;
+
+ can_irq_cli();
+ spi_ss(0);
+ spi_wrrd(MCP2515_RDSTAT);
+ status = spi_wrrd(0xff);
+ spi_ss(1);
+ can_irq_sei();
+
+ return status & 0x04;
+}
+
#ifndef R0KET
-union {
- uint8_t b[4];
- uint32_t u;
-} can_rx_addr;
-uint8_t can_rx_dlc, can_rx_data[8];
+struct can {
+ union can_rxa {
+ uint8_t b[4];
+ uint32_t u;
+ } rx_addr;
+ uint8_t rx_dlc;
+ uint8_t rx_data[8];
+} can;
+#define can_rx_addr can.rx_addr
+#define can_rx_dlc can.rx_dlc
+#define can_rx_data can.rx_data
#define can_rx_avail() (can_rx_addr.u)
#define can_rx_pop() do { can_rx_addr.u = 0; } while (0)
@@ -164,10 +185,12 @@ static void can_rxh(uint8_t buffer)
{
uint8_t c, byte;
+#ifdef CAN_DBG_IN_IRQ
if (buffer)
uart_puts("can: RX1IF\n");
else
uart_puts("can: RX0IF\n");
+#endif
spi_ss(0);
spi_wrrd(0x90 + 0x04 * buffer);
@@ -179,12 +202,17 @@ static void can_rxh(uint8_t buffer)
};
c = 0;
+#ifdef CAN_DBG_IN_IRQ
#define rdaddr() byte = spi_wrrd(0xff); uart_puthex(byte); can_rx_addr.b[c++] = byte
+#else
+#define rdaddr() byte = spi_wrrd(0xff); can_rx_addr.b[c++] = byte
+#endif
rdaddr();
rdaddr();
rdaddr();
rdaddr();
can_rx_dlc = spi_wrrd(0xff);
+#ifdef CAN_DBG_IN_IRQ
uart_puthex(can_rx_dlc);
uart_puts("\n");
for (c = 0; c < (can_rx_dlc & 0x0f); c++) {
@@ -193,18 +221,27 @@ static void can_rxh(uint8_t buffer)
uart_puthex(byte);
}
uart_puts("\n");
+#else
+ for (c = 0; c < (can_rx_dlc & 0x0f); c++)
+ can_rx_data[c] = spi_wrrd(0xff);
+#endif
spi_ss(1);
+#ifdef CAN_USER_IRQH
+ can_user_irqh();
+#endif
}
ISR(INT0_vect)
{
uint8_t canintf, eflg, canstat;
+#ifdef CAN_DBG_IN_IRQ
#ifdef HAVE_TICK
uart_puttick();
#endif
// dbg_dump_ret();
uart_puts("can: irqh<");
+#endif
spi_ss(0);
spi_wrrd(MCP2515_READ);
@@ -214,10 +251,12 @@ ISR(INT0_vect)
canstat = spi_wrrd(0xff);
spi_ss(1);
+#ifdef CAN_DBG_IN_IRQ
uart_puthex(canintf);
uart_puthex(eflg);
uart_puthex(canstat);
uart_puts(">\n");
+#endif
if (canintf & 0x80 || canintf & 0x04) {
uint8_t txb0ctrl;