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" |
Brian Silverman | 18b0164 | 2013-12-13 21:12:25 -0800 | [diff] [blame] | 13 | #include "cape/gyro.h" |
Brian Silverman | 95244d8 | 2013-12-14 12:15:46 -0800 | [diff] [blame] | 14 | #include "cape/analog.h" |
Brian Silverman | c58872f | 2013-12-15 16:27:53 -0800 | [diff] [blame] | 15 | #include "cape/robot.h" |
Brian Silverman | 25a06d9 | 2013-12-15 16:28:52 -0800 | [diff] [blame] | 16 | #include "cape/digital.h" |
Brian Silverman | 176c676 | 2013-12-19 16:28:09 -0800 | [diff] [blame] | 17 | #include "cape/led.h" |
Brian Silverman | 1b6fbd0 | 2013-12-12 18:08:47 -0800 | [diff] [blame] | 18 | |
Brian Silverman | ed03006 | 2013-12-20 21:03:47 -0800 | [diff] [blame^] | 19 | #include "cape/uart_byte.h" |
| 20 | |
Brian Silverman | 1b6fbd0 | 2013-12-12 18:08:47 -0800 | [diff] [blame] | 21 | #define TIMESTAMP_TIM TIM6 |
| 22 | #define RCC_APB1ENR_TIMESTAMP_TIMEN RCC_APB1ENR_TIM6EN |
Brian Silverman | 2df8441 | 2013-12-10 14:00:40 -0800 | [diff] [blame] | 23 | |
| 24 | static uint8_t buffer1[DATA_STRUCT_SEND_SIZE] __attribute__((aligned(4))); |
| 25 | static uint8_t buffer2[DATA_STRUCT_SEND_SIZE] __attribute__((aligned(4))); |
| 26 | |
Brian Silverman | 1b6fbd0 | 2013-12-12 18:08:47 -0800 | [diff] [blame] | 27 | static uint32_t flash_checksum; |
| 28 | // These aren't really integers; they're (4-byte) variables whose addresses mark |
| 29 | // various locations. |
| 30 | extern uint8_t __etext, __data_start__, __data_end__; |
| 31 | |
| 32 | static inline void do_fill_packet(struct DataStruct *packet) { |
| 33 | static uint64_t timestamp = 0; |
| 34 | counter_update_u64_u16(×tamp, TIMESTAMP_TIM->CNT); |
| 35 | packet->timestamp = timestamp; |
| 36 | |
| 37 | packet->flash_checksum = flash_checksum; |
| 38 | |
Brian Silverman | 18b0164 | 2013-12-13 21:12:25 -0800 | [diff] [blame] | 39 | struct GyroOutput gyro_output; |
| 40 | gyro_get_output(&gyro_output); |
| 41 | packet->gyro_angle = gyro_output.angle; |
| 42 | packet->old_gyro_reading = gyro_output.last_reading_bad; |
| 43 | packet->uninitialized_gyro = !gyro_output.initialized; |
| 44 | packet->zeroing_gyro = !gyro_output.zeroed; |
| 45 | packet->bad_gyro = gyro_output.gyro_bad; |
| 46 | |
Brian Silverman | c58872f | 2013-12-15 16:27:53 -0800 | [diff] [blame] | 47 | robot_fill_packet(packet); |
Brian Silverman | 1b6fbd0 | 2013-12-12 18:08:47 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Brian Silverman | 2df8441 | 2013-12-10 14:00:40 -0800 | [diff] [blame] | 50 | // Fills the new packet with data. |
| 51 | void uart_dma_callback(uint8_t *buffer) { |
| 52 | struct { |
| 53 | struct DataStruct packet; |
| 54 | uint8_t padding[DATA_STRUCT_SEND_SIZE - sizeof(struct DataStruct) - 12]; |
| 55 | uint32_t checksum; |
| 56 | } data __attribute__((aligned(4))); |
| 57 | STATIC_ASSERT(sizeof(data) == DATA_STRUCT_SEND_SIZE - 8, |
| 58 | The_size_of_the_data_is_wrong); |
| 59 | struct DataStruct *packet = &data.packet; |
| 60 | |
Brian Silverman | 1b6fbd0 | 2013-12-12 18:08:47 -0800 | [diff] [blame] | 61 | do_fill_packet(packet); |
| 62 | |
| 63 | uint32_t *p; |
| 64 | memcpy(&p, &packet, sizeof(void *)); |
| 65 | data.checksum = crc_calculate(p, sizeof(*packet) / 4); |
Brian Silverman | 2df8441 | 2013-12-10 14:00:40 -0800 | [diff] [blame] | 66 | |
Brian Silverman | c58872f | 2013-12-15 16:27:53 -0800 | [diff] [blame] | 67 | ((uint32_t *)buffer)[0] = 0; |
Brian Silverman | 2df8441 | 2013-12-10 14:00:40 -0800 | [diff] [blame] | 68 | cows_stuff(&data, sizeof(data), buffer + 4); |
| 69 | } |
| 70 | |
| 71 | void fill_packet_start(void) { |
Brian Silverman | 1b6fbd0 | 2013-12-12 18:08:47 -0800 | [diff] [blame] | 72 | RCC->APB1ENR |= RCC_APB1ENR_TIMESTAMP_TIMEN; |
| 73 | TIMESTAMP_TIM->CR1 = TIM_CR1_UDIS; |
| 74 | TIMESTAMP_TIM->EGR = TIM_EGR_UG; |
| 75 | TIMESTAMP_TIM->CR1 |= TIM_CR1_CEN; |
| 76 | |
| 77 | crc_init(); |
Brian Silverman | 95244d8 | 2013-12-14 12:15:46 -0800 | [diff] [blame] | 78 | analog_init(); |
Brian Silverman | 1b6fbd0 | 2013-12-12 18:08:47 -0800 | [diff] [blame] | 79 | encoder_init(); |
Brian Silverman | 25a06d9 | 2013-12-15 16:28:52 -0800 | [diff] [blame] | 80 | digital_init(); |
Brian Silverman | 1b6fbd0 | 2013-12-12 18:08:47 -0800 | [diff] [blame] | 81 | |
| 82 | uint8_t *flash_end = &__etext + (&__data_start__ - &__data_end__) + 8; |
| 83 | flash_checksum = crc_calculate((void *)MAIN_FLASH_START, |
| 84 | (size_t)(flash_end - MAIN_FLASH_START) / 4); |
Brian Silverman | 2df8441 | 2013-12-10 14:00:40 -0800 | [diff] [blame] | 85 | |
Brian Silverman | 176c676 | 2013-12-19 16:28:09 -0800 | [diff] [blame] | 86 | led_set(LED_ERR, 0); |
| 87 | //gyro_init(); |
| 88 | |
Brian Silverman | ed03006 | 2013-12-20 21:03:47 -0800 | [diff] [blame^] | 89 | //uart_common_configure(3000000); |
| 90 | uart_common_configure(300000); |
| 91 | //uart_common_configure(19200); |
| 92 | #if 0 |
| 93 | //for (int i = 0; i < 5; ++i) uart_byte_send(255); |
| 94 | for (int i = 0; i < 10; ++i) uart_byte_send(i + 20); |
| 95 | //uart_byte_send('a'); |
| 96 | //uart_byte_send('b'); |
| 97 | //uart_byte_send('c'); |
| 98 | //uart_byte_send('d'); |
| 99 | led_set(LED_DB, 1); |
| 100 | (void)buffer1; |
| 101 | (void)buffer2; |
| 102 | #else |
Brian Silverman | df49fe3 | 2013-12-11 14:21:37 -0800 | [diff] [blame] | 103 | uart_dma_configure(DATA_STRUCT_SEND_SIZE, buffer1, buffer2); |
Brian Silverman | ed03006 | 2013-12-20 21:03:47 -0800 | [diff] [blame^] | 104 | #endif |
Brian Silverman | 2df8441 | 2013-12-10 14:00:40 -0800 | [diff] [blame] | 105 | } |