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