Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
2F62501ED4689FB349E356AB974DBE57=DB4D67F16CA069769A3F87733D593AF2
8DF89ED150041C4CBC7CB9A9CAA90856=DB4D67F16CA069769A3F87733D593AF2
DC22A860405A8BF2F2C095E5B6529F12=D1E96F3EE32E2AB2CF91940507FB3624
eclipse.preferences.version=1
1 change: 1 addition & 0 deletions Mainboard/Firmware/motherboard_v1/Core/Inc/adc.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ void adc_init(void);
// but if it is called in the middle of an conversion,
// one sample old data will be returned.
void adc_latest_bits(uint16_t *output);
void adc_latest_amds(uint16_t *output);

#endif // ADC_H
56 changes: 56 additions & 0 deletions Mainboard/Firmware/motherboard_v1/Core/Inc/drv_uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,54 @@

#include "platform.h"
#include <stdint.h>
#include <stdbool.h>

void drv_uart_init(void);

extern UART_HandleTypeDef huart2;
extern UART_HandleTypeDef huart3;

// 16-byte accumulator
// Layout: [UART4 pkt0-3 | UART5 pkt0-3][UART4 pkt4-7 | UART5 pkt4-7]
extern volatile uint16_t latest_valid_amds_samples[2][8];
extern volatile bool amds_samples_ready[2];
extern volatile uint8_t uart4_amds_sample_count;
extern volatile uint8_t uart5_amds_sample_count;


typedef enum {
STATE_IDLE,
STATE_GOT_HEADER,
STATE_GOT_BYTE1
} rx_state_t;

typedef struct {
rx_state_t state;
uint8_t header;
uint8_t data[2];
uint32_t read_index;
} uart_rx_tracker_t;

extern uart_rx_tracker_t tracker4;
extern uart_rx_tracker_t tracker5;

#define AMDS_RX_BUF_SIZE 256
extern uint8_t UART4_DMA_Pool[AMDS_RX_BUF_SIZE];
extern uint8_t UART5_DMA_Pool[AMDS_RX_BUF_SIZE];

// Define large buffers for outgoing data
#define TX_BUF_SIZE 512
extern uint8_t usart2_tx_ring[TX_BUF_SIZE];
extern uint8_t usart3_tx_ring[TX_BUF_SIZE];

extern uint32_t usart2_tx_write_idx;
extern uint32_t usart3_tx_write_idx;


void process_uart_fifo(uint8_t *pool, uart_rx_tracker_t *track, uint8_t uart_id);

void dma_send(uint8_t uart_id, uint8_t *data, uint8_t len);

static inline void drv_uart_putc_fast(USART_TypeDef *uart, uint8_t data)
{
// Wait until UART is ready to accept next character
Expand All @@ -17,6 +62,17 @@ static inline void drv_uart_putc_fast(USART_TypeDef *uart, uint8_t data)
uart->TDR = data;
}

static inline void drv_uart_putc_dma(USART_TypeDef *uart, uint8_t data)
{
if (uart == USART2) {
usart2_tx_ring[usart2_tx_write_idx] = data;
usart2_tx_write_idx = (usart2_tx_write_idx + 1) % TX_BUF_SIZE;
} else if (uart == USART3) {
usart3_tx_ring[usart3_tx_write_idx] = data;
usart3_tx_write_idx = (usart3_tx_write_idx + 1) % TX_BUF_SIZE;
}
}

static inline void drv_uart_wait_TC(USART_TypeDef *uart)
{
// After done sending characters, must wait for TC flag!!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@
#define USE_RTOS 0U
#define PREFETCH_ENABLE 0U
#define ART_ACCLERATOR_ENABLE 0U /* To enable instruction cache and prefetch */
#define USE_HAL_UART_REGISTER_CALLBACKS 1U /* UART register callback disabled */

/* ########################## Assert Selection ############################## */
/**
Expand Down
45 changes: 44 additions & 1 deletion Mainboard/Firmware/motherboard_v1/Core/Src/adc.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "adc.h"
#include "drv_uart.h"
#include "drv_spi.h"
#include "platform.h"
#include "tx.h"
Expand All @@ -23,6 +24,7 @@ static void setup_pin_CONVST(void);
// clang-format on

#define GPIO_SET_PIN(port, pin, x) port->BSRR = (x) ? pin : (pin << 16)
#define GPIO_TOGGLE_PIN(port, pin) ((port)->BSRR = ((port)->ODR & (pin)) ? ((pin) << 16) : (pin))

#define SET_PIN_CONVST12_HIGH GPIO_SET_PIN(GPIOE, GPIO_PIN_10, 1)
#define SET_PIN_CONVST34_HIGH GPIO_SET_PIN(GPIOE, GPIO_PIN_11, 1)
Expand All @@ -43,6 +45,10 @@ static void setup_pin_CONVST(void);
// Buffer of latest samples
static volatile uint16_t latest_valid_adc_data[8] = { 0 };

volatile uint16_t latest_valid_amds_samples[2][8] = { 0 };

volatile bool amds_samples_ready[2] = { 0 };

void adc_init(void)
{
// Setup output pin which starts ADC conversions
Expand All @@ -68,6 +74,33 @@ void adc_latest_bits(uint16_t *output)
output[7] = data[7];
}

// NOTE: this function is called from the transmit function
void adc_latest_amds(uint16_t *output)
{
volatile uint16_t *data1 = latest_valid_amds_samples[0];
volatile uint16_t *data2 = latest_valid_amds_samples[1];

// Give user their data (unrolled for speed)
output[0] = data1[0];
output[1] = data1[1];
output[2] = data1[2];
output[3] = data1[3];
output[4] = data1[4];
output[5] = data1[5];
output[6] = data1[6];
output[7] = data1[7];

// Give user their data (unrolled for speed)
output[8] = data2[0];
output[9] = data2[1];
output[10] = data2[2];
output[11] = data2[3];
output[12] = data2[4];
output[13] = data2[5];
output[14] = data2[6];
output[15] = data2[7];
}

static void adc_sample_all_daughtercards(uint16_t *sample_data_out)
{
// This function has been optimized for very
Expand Down Expand Up @@ -147,7 +180,8 @@ static void adc_sample_all_daughtercards(uint16_t *sample_data_out)
void EXTI3_IRQHandler(void)
{
// Perform the actual SPI transactions
uint16_t new_data[8] = { 0 };
GPIO_TOGGLE_PIN(GPIOD, GPIO_PIN_1);
uint16_t new_data[8] = { 0 };
adc_sample_all_daughtercards(new_data);

// Copy data into write buffer destination
Expand Down Expand Up @@ -214,9 +248,12 @@ static void setup_pin_SYNC_ADC(void)
GPIO_InitTypeDef GPIO_InitStruct = { 0 };

__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();

// Configure GPIO pin Output Level
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_1, GPIO_PIN_SET);


// Configure GPIO pins
GPIO_InitStruct.Pin = GPIO_PIN_3;
Expand All @@ -225,6 +262,12 @@ static void setup_pin_SYNC_ADC(void)
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

GPIO_InitStruct.Pin = GPIO_PIN_1;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

// EXTI interrupt init
HAL_NVIC_SetPriority(EXTI3_IRQn, 10, 0);
HAL_NVIC_EnableIRQ(EXTI3_IRQn);
Expand Down
6 changes: 3 additions & 3 deletions Mainboard/Firmware/motherboard_v1/Core/Src/drv_gpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ static void MX_GPIO_Init(void)

// Configure GPIO pin Output Level
HAL_GPIO_WritePin(GPIOD,
GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2
GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11
| GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7,
GPIO_PIN_RESET);

Expand Down Expand Up @@ -78,9 +78,9 @@ static void MX_GPIO_Init(void)
HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);

// Configure GPIO pins : PD8 PD9 PD10 PD11
// PD0 PD1 PD2 PD3
// PD3
// PD4 PD5 PD6 PD7
GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2
GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10 | GPIO_PIN_11
| GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
Expand Down
Loading