fix some compiler errors. delays are still TODO

This commit is contained in:
Triss 2021-11-07 21:07:37 +01:00
parent 1b1ed215eb
commit e77699c9cc
4 changed files with 37 additions and 27 deletions

View File

@ -43,6 +43,7 @@ if(FAMILY STREQUAL "rp2040")
${CMAKE_CURRENT_SOURCE_DIR}/src/tool78/tool78_hw_78k0r_uart1.c
${CMAKE_CURRENT_SOURCE_DIR}/src/tool78/tool78_hw_rl78_uart1.c
${CMAKE_CURRENT_SOURCE_DIR}/src/tool78/tool78_hw_rl78_uart2.c
${CMAKE_CURRENT_SOURCE_DIR}/src/tool78/tool78_cmds.c
${CMAKE_CURRENT_SOURCE_DIR}/src/test/piotest.c
)

View File

@ -1,4 +1,6 @@
#include <string.h>
#include <pico/time.h>
#include <pico/timeout_helper.h>
@ -41,7 +43,7 @@ static uint8_t databuf[255];
static enum tool78_stat tool78_data_send(struct tool78_hw* hw,
size_t len, const uint8_t* data, bool final_block) {
int len, const uint8_t* data, bool final_block) {
if (len == 0) return tool78_stat_ack;
if (len > 0x100) return tool78_stat_internal_error;
len &= 0xff;
@ -73,7 +75,7 @@ static enum tool78_stat tool78_data_send(struct tool78_hw* hw,
}
static enum tool78_stat tool78_cmd_send(struct tool78_hw* hw, uint8_t cmd,
uint8_t len, const uint8_t* data) {
int len, const uint8_t* data) {
if (len == 0) return tool78_stat_ack;
if (len > 0x100) return tool78_stat_internal_error;
len &= 0xff;
@ -121,7 +123,7 @@ static enum tool78_stat tool78_data_recv(struct tool78_hw* hw, uint8_t* buf,
rr = hw->recv(1, &pre[1], 100);
if (rr != 1) return tool78_stat_internal_error;
uint8_t len = pre[1];
int len = pre[1];
if (!len) len = 256;
if (hw->target == tool78k0_spi && pre[1] == tool78_stat_busy)
@ -137,7 +139,7 @@ static enum tool78_stat tool78_data_recv(struct tool78_hw* hw, uint8_t* buf,
if (post[1] != tool78_frame_etx && post[1] != tool78_frame_etb)
return tool78_stat_protocol_error;
uint8_t ck = tool78_calc_checksum(len, buf);
uint8_t ck = tool78_calc_checksum8(len, buf);
if (ck != post[0])
return tool78_stat_protocol_error;
@ -150,6 +152,7 @@ static enum tool78_stat tool78_wait_status(struct tool78_hw* hw, int l, int time
timeout_us *= 2;
}
enum tool78_stat st;
bool blockinf = timeout_us < 0;
absolute_time_t at = make_timeout_time_us(timeout_us);
timeout_state_t ts = {0};
@ -166,11 +169,11 @@ static enum tool78_stat tool78_wait_status(struct tool78_hw* hw, int l, int time
}
st = tool78_data_recv(hw, databuf, l, timeout_us);
if (st == tool78_status_busy) goto cont;
if (st == tool78_stat_busy) goto cont;
if (st != tool78_stat_ack) return st;
st = databuf[0];
if (st == tool78_status_busy) goto cont;
if (st == tool78_stat_busy) goto cont;
return st;
@ -195,7 +198,6 @@ enum tool78_stat tool78_do_reset(struct tool78_hw* hw) {
enum tool78_stat tool78_do_baud_rate_set(struct tool78_hw* hw) {
uint8_t d0x[5];
int len = 0;
enum tool78_stat st;
switch (hw->target & tool78_mcu_mask) {
@ -224,7 +226,7 @@ enum tool78_stat tool78_do_baud_rate_set(struct tool78_hw* hw) {
d0x[0] = 0x00; // 0=115.2k 1=250k 2=500k 3=1M
d0x[1] = 33; // 3.3V, will switch to full-speed mode
st = tool78_cmd_send(hw, tool78_cmd_baud_rate_set, 5, d0x);
st = tool78_cmd_send(hw, tool78_cmd_baud_rate_set, 2, d0x);
if (st != tool78_stat_ack) return st;
st = tool78_wait_status(hw, 3, tWT10); // TODO // aka tCS6
@ -450,17 +452,17 @@ enum tool78_stat tool78_do_silicon_signature(struct tool78_hw* hw,
case tool78_mcu_78k0r: nbyte = 27; break;
case tool78_mcu_rl78: nbyte = 22; break;
default: // oops
return tool78_stat_bad_cmu_for_cmd;
return tool78_stat_bad_mcu_for_cmd;
}
memset(sig_Dest, 0, 27);
memset(sig_dest, 0, 27);
enum tool78_stat st = tool78_cmd_send(hw, tool78_cmd_silicon_signature, 0, NULL);
if (st != tool78_stat_ack) return st;
st = tool78_wait_status(hw, 1, tWT11); // TODO // aka tCS11
if (st != tool78_stat_ack) return st;
st = tool78_data_recv(hw, sig_dest, nbyte, tFD2); // TODO // aka tSD11
st = tool78_data_recv(hw, (uint8_t*)sig_dest, nbyte, tFD2); // TODO // aka tSD11
if (st != tool78_stat_ack) return st;
// TODO: parity check of data
@ -479,7 +481,7 @@ enum tool78_stat tool78_do_version_get(struct tool78_hw* hw, tool78_version_t* v
if (st != tool78_stat_ack) return st;
memset(vout, 0, 6);
st = tool78_data_recv(hw, vout, 6, tFD2); // TODO
st = tool78_data_recv(hw, (uint8_t*)vout, 6, tFD2); // TODO
if (st != tool78_stat_ack) return st;
return st;
@ -513,7 +515,7 @@ enum tool78_stat tool78_do_checksum(struct tool78_hw* hw, uint32_t start,
if (st != tool78_stat_ack) return st;
*ckout = 0;
st = tool78_data_recv(hw, ckout, 2, tFD1); // TODO // aka tSD10
st = tool78_data_recv(hw, (uint8_t*)ckout, 2, tFD1); // TODO // aka tSD10
if (st != tool78_stat_ack) return st;
return st;
@ -566,7 +568,7 @@ enum tool78_stat tool78_do_security_set(struct tool78_hw* hw,
st = tool78_wait_status(hw, 1, tWT14); // TODO // aka tDS7
if (st != tool78_stat_ack) return st;
if (!srl78) { // yeah...
if (!isrl78) { // yeah...
st = tool78_wait_status(hw, 1, tWT15); // TODO
if (st != tool78_stat_ack) return st;
}
@ -595,7 +597,7 @@ enum tool78_stat tool78_do_security_get(struct tool78_hw* hw,
sec->fsws = data[2] | ((uint16_t)data[3]<<8);
sec->fswe = data[4] | ((uint16_t)data[5]<<8);
return st
return st;
}
enum tool78_stat tool78_do_security_release(struct tool78_hw* hw) {

View File

@ -112,7 +112,7 @@ enum tool78_stat {
// 78k0/Kx2 only. security related?
tool78_stat_read_error = 0x20,
// 78k0/Kx2 SPI only
tool78_stat_busy = 0xff
tool78_stat_busy = 0xff,
tool78_stat_protocol_error = 0xc1, // low-level wire protocol violation
tool78_stat_bad_mcu_for_cmd = 0xc2,
@ -154,19 +154,26 @@ enum tool78_frame {
tool78_frame_eot = 0x17,
};
static inline uint8_t tool78_digest_checksum8(uint8_t r, const uint8_t* data) {
static inline uint8_t tool78_digest_checksum8(uint8_t r, size_t len, const uint8_t* data) {
for (size_t i = 0; i < len; ++i) r = r - data[i];
return r;
}
static inline uint8_t tool78_calc_checksum8(size_t len, const uint8_t* data) {
return tool78_digest_checksum8(r, len, data);
return tool78_digest_checksum8(0, len, data);
}
static inline uint8_t tool78_calc_ocd_checksum8(size_t len, const uint8_t* data) {
return ~tool78_calc_checksum8(len, data);
}
// TODO: 16bit checksum (cf. command B0 Checksum)
__attribute__((__packed__, __align__(1))) struct tool78_silicon_sig_78k0 {
static inline uint16_t tool78_digest_checksum16(uint16_t r, size_t len, const uint8_t* data) {
for (size_t i = 0; i < len; ++i) r = r - data[i]; // yes it's done byte by byte
return r;
}
static inline uint8_t tool78_calc_checksum16(size_t len, const uint8_t* data) {
return tool78_digest_checksum16(0, len, data);
}
struct __attribute__((__packed__)) tool78_silicon_sig_78k0 {
uint8_t ven; // vendor code. 0x10 == NEC
uint8_t met; // macro extension (0x7f)
uint8_t msc; // macro function code (0x04)
@ -178,7 +185,7 @@ __attribute__((__packed__, __align__(1))) struct tool78_silicon_sig_78k0 {
// "for above fields except BOT, the lower 7 bits are used as data entity,
// and the highest bit is used as an odd parity"
};
__attribute__((__packed__, __align__(1))) struct tool78_silicon_sig_78k0r {
struct __attribute__((__packed__)) tool78_silicon_sig_78k0r {
uint8_t ven; // vendor code. 0x10 == NEC
uint8_t met; // macro extension (0x6f or 0x7f)
uint8_t msc; // macro function code (0x04)
@ -195,7 +202,7 @@ __attribute__((__packed__, __align__(1))) struct tool78_silicon_sig_78k0r {
// "for VEN, MET, MSC and DEC, the lower 7 bits are used as data entity,
// and the highest bit is used as an odd parity."
};
__attribute__((__packed__, __align__(1))) struct tool78_silicon_sig_rl78 {
struct __attribute__((__packed__)) tool78_silicon_sig_rl78 {
uint8_t dec[3]; // device code
uint8_t dev[10]; // device name
uint8_t cen[3]; // last address of code flash rom, little-endian
@ -203,14 +210,14 @@ __attribute__((__packed__, __align__(1))) struct tool78_silicon_sig_rl78 {
uint8_t ver[3]; // firmware version number
// no parity bits
};
__attribute__((__packed__, __align__(1))) typedef union tool78_silicon_sig {
typedef union __attribute__((__packed__)) tool78_silicon_sig {
struct tool78_silicon_sig_78k0 k780 ;
struct tool78_silicon_sig_78k0r k780r;
struct tool78_silicon_sig_rl78 rl78 ;
} tool78_silicon_sig_t;
// same structure for 78k0 and 78k0r, not used in rl78
__attribute__((__packed__, __align__(1))) typedef struct tool78_version {
typedef struct __attribute__((__packed__)) tool78_version {
uint8_t dv[3]; // all zero
uint8_t fw[3];
} tool78_version_t;

View File

@ -40,10 +40,10 @@ static bool t78k0r_uart1_init(void) {
busy_wait_us_32(120 + 20); // t01 or tCOM
// send two 0x00 "LOW" bytes
uint8_t byte = 0x00;
t78k0_uart2_send(1, &byte, -1);
byte = 0x00;
t78k0r_uart1_send(1, &byte, -1);
busy_wait_us_32(120); // t12
t78k0_uart2_send(1, &byte, -1);
t78k0r_uart1_send(1, &byte, -1);
busy_wait_us_32(610); // t2C
// now a reset command needs to be sent, but we leave that to the upper