diff --git a/misc/stm32f1_test_project/Makefile b/misc/stm32f1_test_project/Makefile index fc9cfdd7b4..ef1a156f71 100644 --- a/misc/stm32f1_test_project/Makefile +++ b/misc/stm32f1_test_project/Makefile @@ -127,7 +127,9 @@ CSRC = $(ALLCSRC) # C++ sources that can be compiled in ARM or THUMB mode depending on the global # setting. CPPSRC = $(ALLCPPSRC) \ - main.cpp \ + main.cpp \ + spi.cpp \ + can.cpp \ uart.cpp # List ASM source files here. diff --git a/misc/stm32f1_test_project/can.cpp b/misc/stm32f1_test_project/can.cpp new file mode 100644 index 0000000000..fe545f703f --- /dev/null +++ b/misc/stm32f1_test_project/can.cpp @@ -0,0 +1,79 @@ +#include "can.h" +#include "hal.h" + +#include +#include + +const CANConfig canConfig500 = +{ + CAN_MCR_ABOM | CAN_MCR_AWUM | CAN_MCR_TXFP, + /* + For 24MHz http://www.bittiming.can-wiki.info/ gives us Pre-scaler=3, Seq 1=13 and Seq 2=2. Subtract '1' for register values + */ + CAN_BTR_SJW(0) | CAN_BTR_BRP(2) | CAN_BTR_TS1(12) | CAN_BTR_TS2(1), +}; + + +#define SWAP_UINT16(x) (((x) << 8) | ((x) >> 8)) + +void SendSomething() +{ + auto baseAddress = 0x156; + + { + CANTxFrame m_frame; + + m_frame.IDE = CAN_IDE_STD; + m_frame.EID = baseAddress; + m_frame.RTR = CAN_RTR_DATA; + m_frame.DLC = 8; + memset(m_frame.data8, 0, sizeof(m_frame.data8)); + + canTransmitTimeout(&CAND1, CAN_ANY_MAILBOX, &m_frame, TIME_IMMEDIATE); + } + +} + +static THD_WORKING_AREA(waCanTxThread, 256); +void CanTxThread(void*) +{ + while(1) { + SendSomething(); + + chThdSleepMilliseconds(10); + } +} + +static THD_WORKING_AREA(waCanRxThread, 256); +void CanRxThread(void*) +{ + while(1) + { + CANRxFrame frame; + msg_t msg = canReceiveTimeout(&CAND1, CAN_ANY_MAILBOX, &frame, TIME_INFINITE); + + // Ignore non-ok results... + if (msg != MSG_OK) + { + continue; + } + + // Ignore std frames, only listen to ext + if (frame.IDE != CAN_IDE_EXT) + { + continue; + } + + } +} + +void InitCan() { + canStart(&CAND1, &canConfig500); + // CAN TX + palSetPadMode(GPIOA,12, PAL_MODE_STM32_ALTERNATE_PUSHPULL ); + // CAN RX + palSetPadMode(GPIOA,11, PAL_MODE_INPUT_PULLUP ); + + chThdCreateStatic(waCanTxThread, sizeof(waCanTxThread), NORMALPRIO, CanTxThread, nullptr); + chThdCreateStatic(waCanRxThread, sizeof(waCanRxThread), NORMALPRIO - 4, CanRxThread, nullptr); +} diff --git a/misc/stm32f1_test_project/can.h b/misc/stm32f1_test_project/can.h new file mode 100644 index 0000000000..d195c7d441 --- /dev/null +++ b/misc/stm32f1_test_project/can.h @@ -0,0 +1,3 @@ +#pragma once + +void InitCan(); diff --git a/misc/stm32f1_test_project/main.cpp b/misc/stm32f1_test_project/main.cpp index ac25da1db5..aaf759f042 100644 --- a/misc/stm32f1_test_project/main.cpp +++ b/misc/stm32f1_test_project/main.cpp @@ -17,6 +17,8 @@ #include "ch.h" #include "hal.h" #include "uart.h" +#include "can.h" +#include "spi.h" #define BL_PORT GPIOC #define BL_PIN 13 @@ -61,6 +63,8 @@ int main(void) { chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL); InitUart(); + InitCan(); + InitSpi(); /* * Normal main() thread activity, in this demo it does nothing except diff --git a/misc/stm32f1_test_project/spi.cpp b/misc/stm32f1_test_project/spi.cpp new file mode 100644 index 0000000000..491d85872f --- /dev/null +++ b/misc/stm32f1_test_project/spi.cpp @@ -0,0 +1,30 @@ + +#include "can.h" +#include "hal.h" +#include "spi.h" +#include + +#define SPI_CS_PORT GPIOA +#define SPI_CS_PIN 4 + +/* Low speed SPI configuration (281.250kHz, CPHA=0, CPOL=0, MSb first).*/ +static SPIConfig ls_spicfg = {false, NULL, SPI_CS_PORT, SPI_CS_PIN, + SPI_CR1_BR_2 | SPI_CR1_BR_1, + 0}; + +/* + * SPI TX and RX buffers. + */ +static uint8_t txbuf[512]; +static uint8_t rxbuf[512]; + +void InitSpi() { + /* + * SPI1 I/O pins setup. + */ + palSetPadMode(GPIOA, 5, PAL_MODE_STM32_ALTERNATE_PUSHPULL); /* SCK. */ + palSetPadMode(GPIOA, 6, PAL_MODE_STM32_ALTERNATE_PUSHPULL); /* MISO.*/ + palSetPadMode(GPIOA, 7, PAL_MODE_STM32_ALTERNATE_PUSHPULL); /* MOSI.*/ + palSetPadMode(SPI_CS_PORT, SPI_CS_PIN, PAL_MODE_OUTPUT_PUSHPULL); + palSetPad(SPI_CS_PORT, SPI_CS_PIN); +} \ No newline at end of file diff --git a/misc/stm32f1_test_project/spi.h b/misc/stm32f1_test_project/spi.h new file mode 100644 index 0000000000..18fbbaedc5 --- /dev/null +++ b/misc/stm32f1_test_project/spi.h @@ -0,0 +1,2 @@ + +void InitSpi(); diff --git a/misc/stm32f1_test_project/uart.cpp b/misc/stm32f1_test_project/uart.cpp index fb8dcc4ddd..f9884a81a4 100644 --- a/misc/stm32f1_test_project/uart.cpp +++ b/misc/stm32f1_test_project/uart.cpp @@ -21,6 +21,7 @@ static const UARTConfig uartCfg = .cr1 = 0, .cr2 = 0, .cr3 = 0, + .rxhalf_cb = nullptr, }; static char printBuffer[200]; @@ -30,9 +31,6 @@ static void UartThread(void*) { while(true) { - float lambda = 0; - int lambdaIntPart = 1; - int lambdaThousandths = 2; size_t writeCount = chsnprintf(printBuffer, 200, "%d.%03d\t%d\t%d\r\n", 0, 0, 0, 100); uartStartSend(&UARTD1, writeCount, printBuffer);