blob: e8516a9eb8e4544a45f90a26a985df699f6d37a5 [file] [log] [blame]
Brian Silverman2df84412013-12-10 14:00:40 -08001#include "cape/fill_packet.h"
2
3#include <string.h>
4
5#include <STM32F2XX.h>
6
7#include "cape/uart_dma.h"
Brian Silvermandf49fe32013-12-11 14:21:37 -08008#include "cape/uart_common.h"
Brian Silverman2df84412013-12-10 14:00:40 -08009#include "cape/cows.h"
10
11static uint8_t buffer1[DATA_STRUCT_SEND_SIZE] __attribute__((aligned(4)));
12static uint8_t buffer2[DATA_STRUCT_SEND_SIZE] __attribute__((aligned(4)));
13
14// Fills the new packet with data.
15void uart_dma_callback(uint8_t *buffer) {
16 struct {
17 struct DataStruct packet;
18 uint8_t padding[DATA_STRUCT_SEND_SIZE - sizeof(struct DataStruct) - 12];
19 uint32_t checksum;
20 } data __attribute__((aligned(4)));
21 STATIC_ASSERT(sizeof(data) == DATA_STRUCT_SEND_SIZE - 8,
22 The_size_of_the_data_is_wrong);
23 struct DataStruct *packet = &data.packet;
24
25 CRC->CR = 1; // reset it
26 uint32_t *p1;
27 memcpy(&p1, &packet, sizeof(void *));
28 {
29 uint32_t *restrict p = p1;
30 for (; p < (uint32_t *)(packet + 1); ++p) {
31 CRC->DR = *p;
32 }
33 }
34 data.checksum = CRC->DR;
35
36 memset(buffer, 0, 4);
37 cows_stuff(&data, sizeof(data), buffer + 4);
38}
39
40void fill_packet_start(void) {
41 RCC->AHB1ENR |= RCC_AHB1ENR_CRCEN;
42
Brian Silvermandf49fe32013-12-11 14:21:37 -080043 uart_common_configure(3000000);
44 uart_dma_configure(DATA_STRUCT_SEND_SIZE, buffer1, buffer2);
Brian Silverman2df84412013-12-10 14:00:40 -080045}