diff --git a/src/hydrabus/hydrabus_mode_spi.c b/src/hydrabus/hydrabus_mode_spi.c index a76be6eb..bd7022a1 100644 --- a/src/hydrabus/hydrabus_mode_spi.c +++ b/src/hydrabus/hydrabus_mode_spi.c @@ -38,8 +38,7 @@ static const char* str_bsp_init_err= { "bsp_spi_init() error %d\r\n" }; #define MODE_DEV_NB_ARGC ((int)ARRAY_SIZE(mode_dev_arg)) /* Number of arguments/parameters for this mode */ -#define SPEED_NB (8) -static uint32_t speeds[2][SPEED_NB] = { +const uint32_t spi_speeds[2][SPI_SPEED_NB] = { /* SPI1 */ { 320000, @@ -91,14 +90,14 @@ static void show_params(t_hydra_console *con) "floating", proto->config.spi.dev_mode == DEV_MASTER ? "master" : "slave"); - print_freq(con, speeds[proto->dev_num][proto->config.spi.dev_speed]); + print_freq(con, spi_speeds[proto->dev_num][proto->config.spi.dev_speed]); cprintf(con, " ("); - for (i = 0, cnt = 0; i < SPEED_NB; i++) { + for (i = 0, cnt = 0; i < SPI_SPEED_NB; i++) { if (proto->config.spi.dev_speed == i) continue; if (cnt++) cprintf(con, ", "); - print_freq(con, speeds[proto->dev_num][i]); + print_freq(con, spi_speeds[proto->dev_num][i]); } cprintf(con, ")\r\n"); cprintf(con, "Polarity: %d\r\nPhase: %d\r\nBit order: %s first\r\n", @@ -187,8 +186,8 @@ static int exec(t_hydra_console *con, t_tokenline_parsed *p, int token_pos) case T_FREQUENCY: t += 2; memcpy(&arg_float, p->buf + p->tokens[t], sizeof(float)); - for (i = 0; i < SPEED_NB; i++) { - if (arg_float == speeds[proto->dev_num][i]) { + for (i = 0; i < SPI_SPEED_NB; i++) { + if (arg_float == spi_speeds[proto->dev_num][i]) { proto->config.spi.dev_speed = i; break; } diff --git a/src/hydrabus/hydrabus_mode_spi.h b/src/hydrabus/hydrabus_mode_spi.h index aa2b4a55..3da0cbf0 100644 --- a/src/hydrabus/hydrabus_mode_spi.h +++ b/src/hydrabus/hydrabus_mode_spi.h @@ -26,5 +26,7 @@ enum { SPI_LSB_FIRST, }; -#endif /* _HYDRABUS_MODE_SPI_H_ */ +#define SPI_SPEED_NB (8) +extern const uint32_t spi_speeds[2][SPI_SPEED_NB]; +#endif /* _HYDRABUS_MODE_SPI_H_ */ diff --git a/src/hydrabus/hydrabus_serprog.c b/src/hydrabus/hydrabus_serprog.c index f6e29cd8..1f0e01e7 100644 --- a/src/hydrabus/hydrabus_serprog.c +++ b/src/hydrabus/hydrabus_serprog.c @@ -22,6 +22,7 @@ #include "hydrabus_bbio.h" #include "hydrabus_serprog.h" +#include "hydrabus_mode_spi.h" #include "bsp_spi.h" void bbio_serprog_init_proto_default(t_hydra_console *con) @@ -148,8 +149,35 @@ void bbio_mode_serprog(t_hydra_console *con) break; case S_CMD_S_SPI_FREQ: chnRead(con->sdu, rx_data, 4); - //TODO: define variable speed. + /* requested speed */ + to_rx = ((uint32_t)rx_data[3] << 24) | \ + ((uint32_t)rx_data[2] << 16) | \ + ((uint32_t)rx_data[1] << 8) | rx_data[0]; + /* value 0 is reserved and should not be requested as speed */ + if (to_rx == 0) { + cprint(con, S_NAK, 1); + break; + } + /* find available SPI speed <= request speed */ + i = 0; + while (i < SPI_SPEED_NB && spi_speeds[proto->dev_num][i] <= to_rx) { + i++; + } + i = i == 0 ? 0 : i - 1; + /* configure selected speed */ + proto->config.spi.dev_speed = i; + if (bsp_spi_init(proto->dev_num, proto) != BSP_OK) { + cprint(con, S_NAK, 1); + break; + } + /* send back selected speed */ + to_tx = spi_speeds[proto->dev_num][i]; + tx_data[0] = (uint8_t)((to_tx >> 0 ) & 0xff); + tx_data[1] = (uint8_t)((to_tx >> 8 ) & 0xff); + tx_data[2] = (uint8_t)((to_tx >> 16) & 0xff); + tx_data[3] = (uint8_t)((to_tx >> 24) & 0xff); cprint(con, S_ACK, 1); + cprint(con, (char *)tx_data, 4); break; case S_CMD_S_PIN_STATE: chnRead(con->sdu, rx_data, 1);