第一次提交
This commit is contained in:
84
Middlewares/mx_spi.c
Normal file
84
Middlewares/mx_spi.c
Normal file
@@ -0,0 +1,84 @@
|
||||
#include "mx_spi.h"
|
||||
#include "bsp_spi.h"
|
||||
#include "string.h"
|
||||
mx_spi_frame_struct mx_spi_frame = {0};
|
||||
|
||||
|
||||
static uint16_t CalChecksum(uint8_t * data, uint16_t len)
|
||||
{
|
||||
uint16_t sum = 0;
|
||||
for(int i = 0; i < len; ++i)
|
||||
{
|
||||
sum += data[i];
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
uint8_t creat_mx_spi_frame(uint8_t device_id, uint8_t message_type, uint8_t error_flag, uint8_t *data, uint16_t data_len, mx_spi_frame_struct * mx_spi_out_frame)
|
||||
{
|
||||
if(data_len != sizeof(mx_spi_out_frame->data))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
mx_spi_out_frame->device_id = device_id;
|
||||
mx_spi_out_frame->message_type = message_type;
|
||||
mx_spi_out_frame->error_flag = error_flag;
|
||||
memcpy(mx_spi_out_frame->data,data,data_len);
|
||||
mx_spi_out_frame->check_sum = CalChecksum((uint8_t *)mx_spi_out_frame,sizeof(mx_spi_frame_struct)-1);
|
||||
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
void mx_spi_init(void)
|
||||
{
|
||||
spi1_init((uint8_t *)&mx_spi_frame, sizeof(mx_spi_frame));
|
||||
|
||||
}
|
||||
|
||||
void update_spi_data(uint8_t *sensor_data,uint16_t sensor_data_len)
|
||||
{
|
||||
static mx_spi_frame_struct mx_spi_frame_temp = {0};
|
||||
|
||||
creat_mx_spi_frame(0x01,0x01,0x00,sensor_data,sensor_data_len,&mx_spi_frame_temp);
|
||||
if(spi_i2s_flag_get(SPI1,SPI_I2S_BF_FLAG) == RESET)
|
||||
{
|
||||
if(SPI_SLAVE_CS_READ() == SET)
|
||||
{
|
||||
|
||||
memcpy((uint8_t *)&mx_spi_frame,(uint8_t *)&mx_spi_frame_temp,sizeof(mx_spi_frame_struct));
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
//ˢ<>±<EFBFBD>־
|
||||
void updata_spi_dma(void)
|
||||
{
|
||||
static uint8_t busy = 0;
|
||||
|
||||
if(spi_i2s_flag_get(SPI1,SPI_I2S_BF_FLAG) == RESET && SPI_SLAVE_CS_READ() == SET)
|
||||
{
|
||||
if(busy)
|
||||
{
|
||||
//mx_spi_init();
|
||||
busy = 0;
|
||||
dma_channel_enable(DMA2_CHANNEL1, FALSE);
|
||||
spi_enable(SPI1, FALSE);
|
||||
spi_enable(SPI1, TRUE);
|
||||
dma_data_number_set(DMA2_CHANNEL1, sizeof(mx_spi_frame));
|
||||
dma_channel_enable(DMA2_CHANNEL1, TRUE);
|
||||
}
|
||||
}
|
||||
|
||||
if(spi_i2s_flag_get(SPI1,SPI_I2S_BF_FLAG) == SET && SPI_SLAVE_CS_READ() == RESET)
|
||||
{
|
||||
busy = 1;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user