blob: b525c2f010a10efa77f1dea47f6e052565c80f52 [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"
Brian Silverman1b6fbd02013-12-12 18:08:47 -080010#include "cape/encoder.h"
11#include "cape/crc.h"
12#include "cape/bootloader_handoff.h"
Brian Silverman18b01642013-12-13 21:12:25 -080013#include "cape/gyro.h"
14#include "cape/led.h"
Brian Silverman95244d82013-12-14 12:15:46 -080015#include "cape/analog.h"
Brian Silvermanc58872f2013-12-15 16:27:53 -080016#include "cape/robot.h"
Brian Silverman1b6fbd02013-12-12 18:08:47 -080017
18#define TIMESTAMP_TIM TIM6
19#define RCC_APB1ENR_TIMESTAMP_TIMEN RCC_APB1ENR_TIM6EN
Brian Silverman2df84412013-12-10 14:00:40 -080020
21static uint8_t buffer1[DATA_STRUCT_SEND_SIZE] __attribute__((aligned(4)));
22static uint8_t buffer2[DATA_STRUCT_SEND_SIZE] __attribute__((aligned(4)));
23
Brian Silverman1b6fbd02013-12-12 18:08:47 -080024static uint32_t flash_checksum;
25// These aren't really integers; they're (4-byte) variables whose addresses mark
26// various locations.
27extern uint8_t __etext, __data_start__, __data_end__;
28
29static inline void do_fill_packet(struct DataStruct *packet) {
30 static uint64_t timestamp = 0;
31 counter_update_u64_u16(&timestamp, TIMESTAMP_TIM->CNT);
32 packet->timestamp = timestamp;
33
34 packet->flash_checksum = flash_checksum;
35
Brian Silverman18b01642013-12-13 21:12:25 -080036 struct GyroOutput gyro_output;
37 gyro_get_output(&gyro_output);
38 packet->gyro_angle = gyro_output.angle;
39 packet->old_gyro_reading = gyro_output.last_reading_bad;
40 packet->uninitialized_gyro = !gyro_output.initialized;
41 packet->zeroing_gyro = !gyro_output.zeroed;
42 packet->bad_gyro = gyro_output.gyro_bad;
43
Brian Silvermanc58872f2013-12-15 16:27:53 -080044 robot_fill_packet(packet);
Brian Silverman1b6fbd02013-12-12 18:08:47 -080045}
46
Brian Silverman2df84412013-12-10 14:00:40 -080047// Fills the new packet with data.
48void uart_dma_callback(uint8_t *buffer) {
49 struct {
50 struct DataStruct packet;
51 uint8_t padding[DATA_STRUCT_SEND_SIZE - sizeof(struct DataStruct) - 12];
52 uint32_t checksum;
53 } data __attribute__((aligned(4)));
54 STATIC_ASSERT(sizeof(data) == DATA_STRUCT_SEND_SIZE - 8,
55 The_size_of_the_data_is_wrong);
56 struct DataStruct *packet = &data.packet;
57
Brian Silverman1b6fbd02013-12-12 18:08:47 -080058 do_fill_packet(packet);
59
60 uint32_t *p;
61 memcpy(&p, &packet, sizeof(void *));
62 data.checksum = crc_calculate(p, sizeof(*packet) / 4);
Brian Silverman2df84412013-12-10 14:00:40 -080063
Brian Silvermanc58872f2013-12-15 16:27:53 -080064 ((uint32_t *)buffer)[0] = 0;
Brian Silverman2df84412013-12-10 14:00:40 -080065 cows_stuff(&data, sizeof(data), buffer + 4);
66}
67
68void fill_packet_start(void) {
Brian Silverman1b6fbd02013-12-12 18:08:47 -080069 RCC->APB1ENR |= RCC_APB1ENR_TIMESTAMP_TIMEN;
70 TIMESTAMP_TIM->CR1 = TIM_CR1_UDIS;
71 TIMESTAMP_TIM->EGR = TIM_EGR_UG;
72 TIMESTAMP_TIM->CR1 |= TIM_CR1_CEN;
73
74 crc_init();
Brian Silverman18b01642013-12-13 21:12:25 -080075 led_init();
Brian Silverman95244d82013-12-14 12:15:46 -080076 analog_init();
Brian Silverman1b6fbd02013-12-12 18:08:47 -080077 encoder_init();
Brian Silverman18b01642013-12-13 21:12:25 -080078 gyro_init();
Brian Silverman1b6fbd02013-12-12 18:08:47 -080079
80 uint8_t *flash_end = &__etext + (&__data_start__ - &__data_end__) + 8;
81 flash_checksum = crc_calculate((void *)MAIN_FLASH_START,
82 (size_t)(flash_end - MAIN_FLASH_START) / 4);
Brian Silverman2df84412013-12-10 14:00:40 -080083
Brian Silvermandf49fe32013-12-11 14:21:37 -080084 uart_common_configure(3000000);
85 uart_dma_configure(DATA_STRUCT_SEND_SIZE, buffer1, buffer2);
Brian Silverman2df84412013-12-10 14:00:40 -080086}