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" |
Brian Silverman | df49fe3 | 2013-12-11 14:21:37 -0800 | [diff] [blame] | 8 | #include "cape/uart_common.h" |
Brian Silverman | 2df8441 | 2013-12-10 14:00:40 -0800 | [diff] [blame] | 9 | #include "cape/cows.h" |
Brian Silverman | 1b6fbd0 | 2013-12-12 18:08:47 -0800 | [diff] [blame^] | 10 | #include "cape/encoder.h" |
| 11 | #include "cape/crc.h" |
| 12 | #include "cape/bootloader_handoff.h" |
| 13 | |
| 14 | #define TIMESTAMP_TIM TIM6 |
| 15 | #define RCC_APB1ENR_TIMESTAMP_TIMEN RCC_APB1ENR_TIM6EN |
Brian Silverman | 2df8441 | 2013-12-10 14:00:40 -0800 | [diff] [blame] | 16 | |
| 17 | static uint8_t buffer1[DATA_STRUCT_SEND_SIZE] __attribute__((aligned(4))); |
| 18 | static uint8_t buffer2[DATA_STRUCT_SEND_SIZE] __attribute__((aligned(4))); |
| 19 | |
Brian Silverman | 1b6fbd0 | 2013-12-12 18:08:47 -0800 | [diff] [blame^] | 20 | static uint32_t flash_checksum; |
| 21 | // These aren't really integers; they're (4-byte) variables whose addresses mark |
| 22 | // various locations. |
| 23 | extern uint8_t __etext, __data_start__, __data_end__; |
| 24 | |
| 25 | static inline void do_fill_packet(struct DataStruct *packet) { |
| 26 | static uint64_t timestamp = 0; |
| 27 | counter_update_u64_u16(×tamp, TIMESTAMP_TIM->CNT); |
| 28 | packet->timestamp = timestamp; |
| 29 | |
| 30 | packet->flash_checksum = flash_checksum; |
| 31 | |
| 32 | packet->main.encoders[0] = encoder_read(0); |
| 33 | packet->main.encoders[1] = encoder_read(1); |
| 34 | packet->main.encoders[2] = encoder_read(2); |
| 35 | packet->main.encoders[3] = encoder_read(3); |
| 36 | packet->main.encoders[4] = encoder_read(4); |
| 37 | packet->main.encoders[5] = encoder_read(5); |
| 38 | packet->main.encoders[6] = encoder_read(6); |
| 39 | packet->main.encoders[7] = encoder_read(7); |
| 40 | } |
| 41 | |
Brian Silverman | 2df8441 | 2013-12-10 14:00:40 -0800 | [diff] [blame] | 42 | // Fills the new packet with data. |
| 43 | void uart_dma_callback(uint8_t *buffer) { |
| 44 | struct { |
| 45 | struct DataStruct packet; |
| 46 | uint8_t padding[DATA_STRUCT_SEND_SIZE - sizeof(struct DataStruct) - 12]; |
| 47 | uint32_t checksum; |
| 48 | } data __attribute__((aligned(4))); |
| 49 | STATIC_ASSERT(sizeof(data) == DATA_STRUCT_SEND_SIZE - 8, |
| 50 | The_size_of_the_data_is_wrong); |
| 51 | struct DataStruct *packet = &data.packet; |
| 52 | |
Brian Silverman | 1b6fbd0 | 2013-12-12 18:08:47 -0800 | [diff] [blame^] | 53 | do_fill_packet(packet); |
| 54 | |
| 55 | uint32_t *p; |
| 56 | memcpy(&p, &packet, sizeof(void *)); |
| 57 | data.checksum = crc_calculate(p, sizeof(*packet) / 4); |
Brian Silverman | 2df8441 | 2013-12-10 14:00:40 -0800 | [diff] [blame] | 58 | |
| 59 | memset(buffer, 0, 4); |
| 60 | cows_stuff(&data, sizeof(data), buffer + 4); |
| 61 | } |
| 62 | |
| 63 | void fill_packet_start(void) { |
Brian Silverman | 1b6fbd0 | 2013-12-12 18:08:47 -0800 | [diff] [blame^] | 64 | RCC->APB1ENR |= RCC_APB1ENR_TIMESTAMP_TIMEN; |
| 65 | TIMESTAMP_TIM->CR1 = TIM_CR1_UDIS; |
| 66 | TIMESTAMP_TIM->EGR = TIM_EGR_UG; |
| 67 | TIMESTAMP_TIM->CR1 |= TIM_CR1_CEN; |
| 68 | |
| 69 | crc_init(); |
| 70 | encoder_init(); |
| 71 | |
| 72 | uint8_t *flash_end = &__etext + (&__data_start__ - &__data_end__) + 8; |
| 73 | flash_checksum = crc_calculate((void *)MAIN_FLASH_START, |
| 74 | (size_t)(flash_end - MAIN_FLASH_START) / 4); |
Brian Silverman | 2df8441 | 2013-12-10 14:00:40 -0800 | [diff] [blame] | 75 | |
Brian Silverman | df49fe3 | 2013-12-11 14:21:37 -0800 | [diff] [blame] | 76 | uart_common_configure(3000000); |
| 77 | uart_dma_configure(DATA_STRUCT_SEND_SIZE, buffer1, buffer2); |
Brian Silverman | 2df8441 | 2013-12-10 14:00:40 -0800 | [diff] [blame] | 78 | } |