F1 test project
This commit is contained in:
parent
f5d4690fe1
commit
6aac4b2f6d
|
@ -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.
|
||||
|
|
|
@ -0,0 +1,79 @@
|
|||
#include "can.h"
|
||||
#include "hal.h"
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
|
||||
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);
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
#pragma once
|
||||
|
||||
void InitCan();
|
|
@ -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
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
|
||||
#include "can.h"
|
||||
#include "hal.h"
|
||||
#include "spi.h"
|
||||
#include <cstdint>
|
||||
|
||||
#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);
|
||||
}
|
|
@ -0,0 +1,2 @@
|
|||
|
||||
void InitSpi();
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue