Skip to content

Commit

Permalink
Rework protocol handling (#176)
Browse files Browse the repository at this point in the history
* Rework protocol handling

* Repair the emulator

* Update dpsboot protocol handling

* Correct dpsboot's rx buffer size
  • Loading branch information
Xenoamor authored and kanflo committed Oct 2, 2019
1 parent c1a8ab6 commit 74cdb90
Show file tree
Hide file tree
Showing 9 changed files with 567 additions and 433 deletions.
56 changes: 28 additions & 28 deletions dpsboot/dpsboot.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,25 +79,26 @@ static uint16_t fw_crc16;

static upgrade_reason_t reason = reason_unknown;

static void handle_frame(uint8_t *frame, uint32_t length);
static void send_frame(uint8_t *frame, uint32_t length);
static void handle_frame(uint8_t *payload, uint32_t length);
static void send_frame(const frame_t *frame);

/**
* @brief Send ack to upgrade start and do some book keeping
* @retval none
*/
static void send_start_response(void)
{
DECLARE_FRAME(MAX_FRAME_LENGTH);
PACK8(cmd_response | cmd_upgrade_start);
PACK8(upgrade_continue);
PACK16(chunk_size);
PACK8(reason);
FINISH_FRAME();
frame_t frame;
set_frame_header(&frame);
pack8(&frame, cmd_response | cmd_upgrade_start);
pack8(&frame, upgrade_continue);
pack16(&frame, chunk_size);
pack8(&frame, reason);
end_frame(&frame);
uint32_t setting = 1;
(void) past_write_unit(&past, past_upgrade_started, (void*) &setting, sizeof(setting));
cur_flash_address = (uint32_t) &_app_start;
send_frame(_buffer, _length);
send_frame(&frame);
}

/**
Expand Down Expand Up @@ -173,12 +174,10 @@ static inline bool flash_write32(uint32_t address, uint32_t data)
* @param length length of frame
* @retval None
*/
static void send_frame(uint8_t *frame, uint32_t length)
static void send_frame(const frame_t *frame)
{
do {
usart_send_blocking(USART1, *frame);
frame++;
} while(--length);
for (uint32_t i = 0; i < frame->length; ++i)
usart_send_blocking(USART1, frame->buffer[i]);
}

/**
Expand All @@ -187,24 +186,24 @@ static void send_frame(uint8_t *frame, uint32_t length)
* @param length length of frame
* @retval None
*/
static void handle_frame(uint8_t *frame, uint32_t length)
static void handle_frame(uint8_t *payload, uint32_t length)
{
command_t cmd = cmd_response;
uint8_t *payload;
upgrade_status_t status;
int32_t payload_len = uframe_extract_payload(frame, length);
payload = frame; // Why? Well, frame now points to the payload
frame_t frame;
int32_t payload_len = uframe_extract_payload(&frame, payload, length);

if (payload_len > 0) {
cmd = frame[0];
cmd = frame.buffer[0];
switch(cmd) {
case cmd_upgrade_start:
{
{
DECLARE_UNPACK(payload, length);
UNPACK8(cmd);
UNPACK16(chunk_size);
start_frame_unpacking(&frame);
unpack8(&frame, &cmd);
unpack16(&frame, &chunk_size);
chunk_size = MIN(MAX_CHUNK_SIZE, chunk_size);
UNPACK16(fw_crc16);
unpack16(&frame, &fw_crc16);
}
send_start_response();
break;
Expand Down Expand Up @@ -244,11 +243,12 @@ static void handle_frame(uint8_t *frame, uint32_t length)
}
}
{
DECLARE_FRAME(MAX_FRAME_LENGTH);
PACK8(cmd_response | cmd_upgrade_data);
PACK8(status);
FINISH_FRAME();
send_frame(_buffer, _length);
frame_t frame_resp;
set_frame_header(&frame_resp);
pack8(&frame_resp, cmd_response | cmd_upgrade_data);
pack8(&frame_resp, status);
end_frame(&frame_resp);
send_frame(&frame_resp);
if (status == upgrade_success) {
usart_wait_send_ready(USART1); /** make sure FIFO is empty */
(void) past_erase_unit(&past, past_upgrade_started);
Expand Down
5 changes: 3 additions & 2 deletions emu/dpsemul.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "event.h"
#include "tft.h"
#include "dbg_printf.h"
#include "uframe.h"

#define UDP_RX_BUF_LEN (512)
#define DPS_PORT (5005)
Expand All @@ -58,10 +59,10 @@ struct sockaddr_in comm_client_sock;
* @param frame The frame
* @param[in] length The length
*/
void dps_emul_send_frame(uint8_t *frame, uint32_t length)
void dps_emul_send_frame(frame_t *frame)
{
int slen = sizeof(comm_client_sock);
if (sendto(comm_sock, frame, length, 0, (struct sockaddr*) &comm_client_sock, slen) == -1) {
if (sendto(comm_sock, frame->buffer, frame->length, 0, (struct sockaddr*) &comm_client_sock, slen) == -1) {
printf("Error: sendto()\n");
}
}
Expand Down
38 changes: 38 additions & 0 deletions emu/hw.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,41 @@ bool hw_sel_button_pressed(void)
{
return false;
}

/**
* @brief Set TFT backlight value
* @retval None
*/
void hw_set_backlight(uint8_t brightness)
{
(void) brightness;
}

/**
* @brief Get TFT backlight value
* @retval Brightness percentage
*/
uint8_t hw_get_backlight(void)
{
return 0;
}

/**
* @brief Set the output voltage DAC value
* @param v_dac the value to set to
* @retval none
*/
void hw_set_voltage_dac(uint16_t v_dac)
{
(void) v_dac;
}

/**
* @brief Set the output current DAC value
* @param i_dac the value to set to
* @retval none
*/
void hw_set_current_dac(uint16_t i_dac)
{
(void) i_dac;
}
47 changes: 45 additions & 2 deletions emu/tft.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ void tft_blit(uint16_t *bits, uint32_t width, uint32_t height, uint32_t x, uint3
* @param highlight if true, the character will be inverted
* @retval none
*/
void tft_putch(uint8_t size, char ch, uint32_t x, uint32_t y, uint32_t w, uint32_t h, bool highlight)
uint8_t tft_putch(tft_font_size_t size, char ch, uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint16_t color, bool invert)
{
if (x >= TFT_WIDTH || y >= TFT_HEIGHT) {
printf("Error: character '%c' put outside of screen (%d, %d)\n", ch, x, y);
Expand All @@ -116,7 +116,32 @@ void tft_putch(uint8_t size, char ch, uint32_t x, uint32_t y, uint32_t w, uint32
(void) size;
(void) w;
(void) h;
(void) highlight;
(void) color;
(void) invert;
}

/**
* @brief Blit string on TFT, anchored to bottom-left
* @param size size of character
* @param str the string (must be a supported character)
* @param x x position (left-side of string)
* @param y y position (bottom of string)
* @param w width of bounding box
* @param h height of bounding box
* @param color color of the string
* @param invert if true, the string will be inverted
* @retval the width of the string drawn
*/
uint16_t tft_puts(tft_font_size_t size, const char *str, uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint16_t color, bool invert)
{
(void) size;
(void) str;
(void) x;
(void) y;
(void) w;
(void) h;
(void) color;
(void) invert;
}

/**
Expand All @@ -137,6 +162,24 @@ void tft_fill_pattern(uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2, uint8_
(void) fill_size;
}

/**
* @brief Draw a one pixel rectangle
* @param xpos x position
* @param ypos y position
* @param width width of frame
* @param height height of frame
* @param color color in bgr565 format
* @retval none
*/
void tft_rect(uint32_t xpos, uint32_t ypos, uint32_t width, uint32_t height, uint16_t color)
{
(void) xpos;
(void) ypos;
(void) width;
(void) height;
(void) color;
}

/**
* @brief Fill area with specified color
* @param x y top left corner
Expand Down
Loading

0 comments on commit 74cdb90

Please sign in to comment.