blob: d2dc01e251d497c05059d697bd98c8b16b8a4566 [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"
Brian Silverman95244d82013-12-14 12:15:46 -080014#include "cape/analog.h"
Brian Silvermanc58872f2013-12-15 16:27:53 -080015#include "cape/robot.h"
Brian Silverman25a06d92013-12-15 16:28:52 -080016#include "cape/digital.h"
Brian Silverman176c6762013-12-19 16:28:09 -080017#include "cape/led.h"
Brian Silverman1b6fbd02013-12-12 18:08:47 -080018
Brian Silvermaned030062013-12-20 21:03:47 -080019#include "cape/uart_byte.h"
20
Brian Silverman1b6fbd02013-12-12 18:08:47 -080021#define TIMESTAMP_TIM TIM6
22#define RCC_APB1ENR_TIMESTAMP_TIMEN RCC_APB1ENR_TIM6EN
Brian Silverman2df84412013-12-10 14:00:40 -080023
24static uint8_t buffer1[DATA_STRUCT_SEND_SIZE] __attribute__((aligned(4)));
25static uint8_t buffer2[DATA_STRUCT_SEND_SIZE] __attribute__((aligned(4)));
26
Brian Silverman1b6fbd02013-12-12 18:08:47 -080027static uint32_t flash_checksum;
28// These aren't really integers; they're (4-byte) variables whose addresses mark
29// various locations.
30extern uint8_t __etext, __data_start__, __data_end__;
31
32static inline void do_fill_packet(struct DataStruct *packet) {
33 static uint64_t timestamp = 0;
34 counter_update_u64_u16(&timestamp, TIMESTAMP_TIM->CNT);
35 packet->timestamp = timestamp;
36
37 packet->flash_checksum = flash_checksum;
38
Brian Silverman18b01642013-12-13 21:12:25 -080039 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 Silvermanc58872f2013-12-15 16:27:53 -080047 robot_fill_packet(packet);
Brian Silvermanffeef3f2013-12-22 14:06:23 -080048 //counter_update_u64_u16(&timestamp, TIMESTAMP_TIM->CNT);
49 //packet->main.encoders[0] = timestamp;
Brian Silverman1b6fbd02013-12-12 18:08:47 -080050}
51
Brian Silverman2df84412013-12-10 14:00:40 -080052// Fills the new packet with data.
53void uart_dma_callback(uint8_t *buffer) {
54 struct {
55 struct DataStruct packet;
56 uint8_t padding[DATA_STRUCT_SEND_SIZE - sizeof(struct DataStruct) - 12];
57 uint32_t checksum;
58 } data __attribute__((aligned(4)));
59 STATIC_ASSERT(sizeof(data) == DATA_STRUCT_SEND_SIZE - 8,
60 The_size_of_the_data_is_wrong);
61 struct DataStruct *packet = &data.packet;
62
Brian Silverman3e0a05b2013-12-22 11:33:42 -080063 do_fill_packet(packet);
Brian Silverman1b6fbd02013-12-12 18:08:47 -080064
65 uint32_t *p;
66 memcpy(&p, &packet, sizeof(void *));
Brian Silverman53f29182013-12-21 15:16:27 -080067 data.checksum = crc_calculate(p, (sizeof(data) - 4) / 4);
Brian Silverman2df84412013-12-10 14:00:40 -080068
Brian Silvermanc58872f2013-12-15 16:27:53 -080069 ((uint32_t *)buffer)[0] = 0;
Brian Silverman2df84412013-12-10 14:00:40 -080070 cows_stuff(&data, sizeof(data), buffer + 4);
71}
72
73void fill_packet_start(void) {
Brian Silverman1b6fbd02013-12-12 18:08:47 -080074 RCC->APB1ENR |= RCC_APB1ENR_TIMESTAMP_TIMEN;
Brian Silvermanffeef3f2013-12-22 14:06:23 -080075 TIMESTAMP_TIM->CR1 = 0;
76 TIMESTAMP_TIM->PSC = 600 - 1;
Brian Silverman1b6fbd02013-12-12 18:08:47 -080077 TIMESTAMP_TIM->EGR = TIM_EGR_UG;
78 TIMESTAMP_TIM->CR1 |= TIM_CR1_CEN;
79
80 crc_init();
Brian Silverman95244d82013-12-14 12:15:46 -080081 analog_init();
Brian Silverman1b6fbd02013-12-12 18:08:47 -080082 encoder_init();
Brian Silverman25a06d92013-12-15 16:28:52 -080083 digital_init();
Brian Silverman1b6fbd02013-12-12 18:08:47 -080084
85 uint8_t *flash_end = &__etext + (&__data_start__ - &__data_end__) + 8;
86 flash_checksum = crc_calculate((void *)MAIN_FLASH_START,
87 (size_t)(flash_end - MAIN_FLASH_START) / 4);
Brian Silverman2df84412013-12-10 14:00:40 -080088
Brian Silverman176c6762013-12-19 16:28:09 -080089 led_set(LED_ERR, 0);
Brian Silverman391beca2013-12-28 22:32:48 -080090 gyro_init();
Brian Silverman176c6762013-12-19 16:28:09 -080091
Brian Silvermanac5dd402013-12-23 21:50:06 -080092 uart_common_configure(1500000);
Brian Silvermandf49fe32013-12-11 14:21:37 -080093 uart_dma_configure(DATA_STRUCT_SEND_SIZE, buffer1, buffer2);
Brian Silverman2df84412013-12-10 14:00:40 -080094}