mirror of
https://github.com/rene-dev/stmbl.git
synced 2026-09-26 10:13:59 +08:00
endat test
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
#include "commands.h"
|
||||
#include "hal.h"
|
||||
#include "math.h"
|
||||
#include "defines.h"
|
||||
#include "angle.h"
|
||||
#include "stm32f4xx_conf.h"
|
||||
#include "hw/hw.h"
|
||||
#include <string.h>
|
||||
|
||||
HAL_COMP(endat);
|
||||
|
||||
HAL_PINA(byte, 6);
|
||||
|
||||
static void nrt_init(volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) {
|
||||
// struct endat_ctx_t *ctx = (struct endat_ctx_t *)ctx_ptr;
|
||||
struct endat_pin_ctx_t *pins = (struct endat_pin_ctx_t *)pin_ptr;
|
||||
GPIO_InitTypeDef GPIO_InitStruct;
|
||||
|
||||
//TX enable for mosi
|
||||
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_15;
|
||||
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
|
||||
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
|
||||
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
|
||||
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
||||
GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
|
||||
//tx enable for clock
|
||||
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;
|
||||
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
|
||||
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
|
||||
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
|
||||
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
||||
GPIO_Init(GPIOD, &GPIO_InitStruct);
|
||||
|
||||
//SPI3
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE);
|
||||
|
||||
SPI3->CR1 |= SPI_CR1_MSTR | SPI_CR1_CPOL | SPI_CR1_CPHA | SPI_CR1_SSI | SPI_CR1_SSM | SPI_CR1_BIDIMODE | SPI_BaudRatePrescaler_32;
|
||||
|
||||
//PC12 spi3 mosi
|
||||
//PC10 spi3 clock
|
||||
GPIO_PinAFConfig(GPIOC, GPIO_PinSource12, GPIO_AF_SPI3);
|
||||
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12;
|
||||
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
|
||||
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
|
||||
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
|
||||
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
|
||||
GPIO_Init(GPIOC, &GPIO_InitStruct);
|
||||
|
||||
GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_SPI3);
|
||||
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;
|
||||
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
|
||||
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
|
||||
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
|
||||
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
|
||||
GPIO_Init(GPIOC, &GPIO_InitStruct);
|
||||
|
||||
GPIO_SetBits(GPIOD, GPIO_Pin_10); //clock tx enable
|
||||
|
||||
}
|
||||
|
||||
static void rt_func(float period, volatile void *ctx_ptr, volatile hal_pin_inst_t *pin_ptr) {
|
||||
// struct endat_ctx_t *ctx = (struct endat_ctx_t *)ctx_ptr;
|
||||
struct endat_pin_ctx_t *pins = (struct endat_pin_ctx_t *)pin_ptr;
|
||||
|
||||
GPIO_SetBits(GPIOD, GPIO_Pin_15);//tx enable
|
||||
SPI3->CR1 |= SPI_CR1_BIDIOE;//enable output
|
||||
SPI3->CR1 |= SPI_CR1_SPE;//enable spi
|
||||
|
||||
SPI3->DR = 0x07;
|
||||
while(!(SPI3->SR & SPI_SR_TXE));
|
||||
while(SPI3->SR & SPI_SR_BSY);
|
||||
SPI3->CR1 &= ~SPI_CR1_BIDIOE;//disable output, this activates the clock
|
||||
GPIO_ResetBits(GPIOD, GPIO_Pin_15);//tx disable
|
||||
|
||||
volatile uint32_t foo = SPI3->DR;//dummy read to clear flags
|
||||
for(int i = 0;i<6;i++){
|
||||
while(!(SPI3->SR & SPI_SR_RXNE));
|
||||
PINA(byte,i) = SPI3->DR;
|
||||
}
|
||||
//TODO: wait for busy flag?
|
||||
SPI3->CR1 &= ~SPI_CR1_SPE;//disable spi
|
||||
}
|
||||
|
||||
hal_comp_t endat_comp_struct = {
|
||||
.name = "endat",
|
||||
.nrt = 0,
|
||||
.rt = rt_func,
|
||||
.frt = 0,
|
||||
.nrt_init = nrt_init,
|
||||
.rt_start = 0,
|
||||
.frt_start = 0,
|
||||
.rt_stop = 0,
|
||||
.frt_stop = 0,
|
||||
.ctx_size = 0,
|
||||
.pin_count = sizeof(struct endat_pin_ctx_t) / sizeof(struct hal_pin_inst_t),
|
||||
};
|
||||
Reference in New Issue
Block a user