Compare commits
No commits in common. "c3ffd3bbf82142a2e45341f5e630b0595cd0031f" and "b4086ddea039ff1d87a1f95421e9e58e007ef00b" have entirely different histories.
c3ffd3bbf8
...
b4086ddea0
@ -15,12 +15,9 @@ U8G2_SRC:=$(U8G2_DIR)/u8x8_d_$(DISPLAY).c $(filter-out $(U8G2_DIR)/u8x8_d_%.c, \
|
|||||||
FRMATH_DIR:=fr_math
|
FRMATH_DIR:=fr_math
|
||||||
FRMATH_SRC:=$(wildcard $(FRMATH_DIR)/*.c)
|
FRMATH_SRC:=$(wildcard $(FRMATH_DIR)/*.c)
|
||||||
|
|
||||||
# include protothreads
|
EXTRA_CFLAGS += -I$(U8G2_DIR) -I$(FRMATH_DIR)
|
||||||
PT_DIR:=pt
|
|
||||||
|
|
||||||
EXTRA_CFLAGS += -I$(U8G2_DIR) -I$(FRMATH_DIR) -I$(PT_DIR)
|
|
||||||
ADDITIONAL_C_FILES += lib_i2c.c display.c sc7a20.c pd.c
|
ADDITIONAL_C_FILES += lib_i2c.c display.c sc7a20.c pd.c
|
||||||
HEADER_FILES := $(wildcard *.h) $(wildcard $(PT_DIR)/*.h)
|
HEADER_FILES := $(wildcard *.h)
|
||||||
|
|
||||||
include ch32fun/ch32fun/ch32fun.mk
|
include ch32fun/ch32fun/ch32fun.mk
|
||||||
|
|
||||||
|
|||||||
9
fw/README
Normal file
9
fw/README
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
SHRIM
|
||||||
|
__-------._
|
||||||
|
_` __===O~O_\.
|
||||||
|
/ \ _ __w_=
|
||||||
|
/___ /~^
|
||||||
|
| |~^
|
||||||
|
\ __\~^ \ V/
|
||||||
|
\_ _\ _ / /
|
||||||
|
`- __|_-'
|
||||||
26
fw/README.md
26
fw/README.md
@ -1,26 +0,0 @@
|
|||||||
# USB-C Soldering Iron Firmware
|
|
||||||
|
|
||||||
```
|
|
||||||
SHRIM
|
|
||||||
__-------._
|
|
||||||
_` __===O~O_\.
|
|
||||||
/ \ _ __w_=
|
|
||||||
/___ /~^
|
|
||||||
| |~^
|
|
||||||
\ __\~^ \ V/
|
|
||||||
\_ _\ __ / /
|
|
||||||
`- __|_-'
|
|
||||||
```
|
|
||||||
|
|
||||||
## Special thanks to:
|
|
||||||
|
|
||||||
* cnlohr and all the contributors of [ch32fun][ch32fun]
|
|
||||||
* deftio for [FR_math][frmath] which I briefly used
|
|
||||||
* olkiarus for [u8g2][u8g2]
|
|
||||||
* The Swedish Institute of Computer Science for [protothreads][pt]
|
|
||||||
|
|
||||||
|
|
||||||
[ch32fun]: https://github.com/cnlohr/ch32fun
|
|
||||||
[frmath]: https://github.com/deftio/fr_math
|
|
||||||
[u8g2]: https://github.com/olikraus/u8g2
|
|
||||||
[pt]: https://dunkels.com/adam/pt/index.html
|
|
||||||
5
fw/programmer/.gitignore
vendored
Normal file
5
fw/programmer/.gitignore
vendored
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
.pio
|
||||||
|
.zed
|
||||||
|
.vscode
|
||||||
|
.cache
|
||||||
|
compile_commands.json
|
||||||
14
fw/programmer/platformio.ini
Normal file
14
fw/programmer/platformio.ini
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
; PlatformIO Project Configuration File
|
||||||
|
;
|
||||||
|
; Build options: build flags, source filter
|
||||||
|
; Upload options: custom upload port, speed and extra flags
|
||||||
|
; Library options: dependencies, extra library storages
|
||||||
|
; Advanced options: extra scripting
|
||||||
|
;
|
||||||
|
; Please visit documentation for the other options and examples
|
||||||
|
; https://docs.platformio.org/page/projectconf.html
|
||||||
|
|
||||||
|
[env:default]
|
||||||
|
platform = https://github.com/maxgerhardt/platform-raspberrypi
|
||||||
|
framework = arduino
|
||||||
|
board = rpipico
|
||||||
1096
fw/programmer/src/bitbang_rvswdio.h
Normal file
1096
fw/programmer/src/bitbang_rvswdio.h
Normal file
File diff suppressed because it is too large
Load Diff
197
fw/programmer/src/bitbang_rvswdio_pico.h
Normal file
197
fw/programmer/src/bitbang_rvswdio_pico.h
Normal file
@ -0,0 +1,197 @@
|
|||||||
|
#include <hardware/gpio.h>
|
||||||
|
#include <pico/time.h>
|
||||||
|
|
||||||
|
#include "bitbang_rvswdio.h"
|
||||||
|
#include "hardware/timer.h"
|
||||||
|
#include "lwipopts.h"
|
||||||
|
#include "pico.h"
|
||||||
|
|
||||||
|
|
||||||
|
#define PIN_SWDIO 10
|
||||||
|
#define PIN_SWCLK 11
|
||||||
|
|
||||||
|
|
||||||
|
// wait time between line transitions
|
||||||
|
#define SWD_DELAY() busy_wait_us(2);
|
||||||
|
|
||||||
|
// microseconds between each register read/write
|
||||||
|
#define STOP_WAIT 8
|
||||||
|
|
||||||
|
// Single wire debug (SWDIO and SWCLK)
|
||||||
|
static inline void ConfigureIOForRVSWD(void)
|
||||||
|
{
|
||||||
|
// SWCLK forced, starts at 1
|
||||||
|
gpio_init(PIN_SWCLK);
|
||||||
|
gpio_set_pulls(PIN_SWCLK, false, false);
|
||||||
|
gpio_put(PIN_SWCLK, 1);
|
||||||
|
gpio_set_dir(PIN_SWCLK, GPIO_OUT);
|
||||||
|
|
||||||
|
// SWDIO, open drain (emulated) with pull-up
|
||||||
|
gpio_init(PIN_SWDIO);
|
||||||
|
gpio_set_pulls(PIN_SWDIO, true, false);
|
||||||
|
gpio_put(PIN_SWDIO, 1);
|
||||||
|
gpio_set_dir(PIN_SWDIO, GPIO_IN);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Single wire input-output SDI (just SWDIO)
|
||||||
|
static inline void ConfigureIOForRVSWIO(void)
|
||||||
|
{
|
||||||
|
//BB_PRINTF_DEBUG( "TODO: add support for SWIO\n" );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void rvswd_write_bit(bool value)
|
||||||
|
{
|
||||||
|
gpio_put(PIN_SWCLK, 0);
|
||||||
|
gpio_put(PIN_SWDIO, value);
|
||||||
|
gpio_set_dir(PIN_SWDIO, GPIO_OUT);
|
||||||
|
gpio_set_dir(PIN_SWDIO, GPIO_IN);
|
||||||
|
SWD_DELAY();
|
||||||
|
gpio_put(PIN_SWCLK, 1); // Data is sampled on rising edge of clock
|
||||||
|
SWD_DELAY();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool rvswd_read_bit(void)
|
||||||
|
{
|
||||||
|
gpio_put(PIN_SWDIO, 1);
|
||||||
|
gpio_set_dir(PIN_SWDIO, GPIO_IN);
|
||||||
|
gpio_put(PIN_SWCLK, 0);
|
||||||
|
SWD_DELAY();
|
||||||
|
bool bit = gpio_get(PIN_SWDIO);
|
||||||
|
gpio_put(PIN_SWCLK, 1); // Data is output on rising edge of clock
|
||||||
|
SWD_DELAY();
|
||||||
|
return bit;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static inline void rvswd_stop(void)
|
||||||
|
{
|
||||||
|
gpio_put(PIN_SWCLK, 0);
|
||||||
|
SWD_DELAY();
|
||||||
|
gpio_put(PIN_SWDIO, 0);
|
||||||
|
gpio_set_dir(PIN_SWDIO, GPIO_OUT);
|
||||||
|
SWD_DELAY();
|
||||||
|
gpio_put(PIN_SWCLK, 1);
|
||||||
|
SWD_DELAY();
|
||||||
|
gpio_put(PIN_SWDIO, 1);
|
||||||
|
gpio_set_dir(PIN_SWDIO, GPIO_IN);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void MCFWriteReg32( struct SWIOState * state, uint8_t command, uint32_t value )
|
||||||
|
{
|
||||||
|
// only supported mode is SWD
|
||||||
|
if (state->opmode != 2) {
|
||||||
|
//BB_PRINTF_DEBUG( "TODO: add support for SWIO\n" );
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
noInterrupts();
|
||||||
|
|
||||||
|
// start transaction
|
||||||
|
gpio_put(PIN_SWDIO, 0);
|
||||||
|
gpio_set_dir(PIN_SWDIO, GPIO_OUT);
|
||||||
|
SWD_DELAY();
|
||||||
|
|
||||||
|
// ADDR HOST
|
||||||
|
bool parity = true;
|
||||||
|
for (uint32_t mask = 1<<6; mask; mask >>= 1) {
|
||||||
|
bool bit = !!(command & mask);
|
||||||
|
parity ^= bit;
|
||||||
|
rvswd_write_bit(bit);
|
||||||
|
}
|
||||||
|
|
||||||
|
rvswd_write_bit(1); // Operation: write
|
||||||
|
rvswd_write_bit(parity);
|
||||||
|
rvswd_read_bit(); // ???
|
||||||
|
rvswd_read_bit(); // Seems only need to be set for first transaction (We are ignoring that though)
|
||||||
|
rvswd_read_bit(); // ???
|
||||||
|
rvswd_write_bit(0); // 0 for register, 1 for value.
|
||||||
|
rvswd_write_bit(0); // ??? Seems to have something to do with halting.
|
||||||
|
|
||||||
|
// DATA
|
||||||
|
parity = false; // This time it's even parity?
|
||||||
|
for (uint32_t mask = 1<<31; mask; mask >>= 1) {
|
||||||
|
bool bit = !!(value & mask);
|
||||||
|
parity ^= bit;
|
||||||
|
rvswd_write_bit(bit);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parity bit
|
||||||
|
rvswd_write_bit(parity);
|
||||||
|
|
||||||
|
rvswd_read_bit(); // ???
|
||||||
|
rvswd_read_bit(); // ???
|
||||||
|
rvswd_read_bit(); // ???
|
||||||
|
rvswd_write_bit(1); // 0 for register, 1 for value
|
||||||
|
rvswd_write_bit(0); // ??? Seems to have something to do with halting?
|
||||||
|
|
||||||
|
rvswd_stop();
|
||||||
|
interrupts();
|
||||||
|
sleep_us(STOP_WAIT);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int MCFReadReg32( struct SWIOState * state, uint8_t command, uint32_t * value )
|
||||||
|
{
|
||||||
|
// only supported mode is SWD
|
||||||
|
if (state->opmode != 2) {
|
||||||
|
//BB_PRINTF_DEBUG( "TODO: add support for SWIO\n" );
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
noInterrupts();
|
||||||
|
|
||||||
|
// start transaction
|
||||||
|
gpio_put(PIN_SWDIO, 0);
|
||||||
|
gpio_set_dir(PIN_SWDIO, GPIO_OUT);
|
||||||
|
SWD_DELAY();
|
||||||
|
|
||||||
|
// ADDR HOST
|
||||||
|
bool parity = false;
|
||||||
|
for (uint8_t mask = 1<<6; mask; mask >>= 1) {
|
||||||
|
bool bit = !!(command & mask);
|
||||||
|
parity ^= bit;
|
||||||
|
rvswd_write_bit(bit);
|
||||||
|
}
|
||||||
|
|
||||||
|
rvswd_write_bit(0); // Operation: read
|
||||||
|
rvswd_write_bit(parity);
|
||||||
|
rvswd_read_bit(); // ??
|
||||||
|
rvswd_read_bit(); // ??
|
||||||
|
rvswd_read_bit(); // ??
|
||||||
|
rvswd_write_bit(0); // 0 for register, 1 for value
|
||||||
|
rvswd_write_bit(0); // ??? Seems to have something to do with halting?
|
||||||
|
|
||||||
|
// DATA
|
||||||
|
*value = 0;
|
||||||
|
parity = false;
|
||||||
|
uint32_t rval = 0;
|
||||||
|
for (uint8_t position = 0; position < 32; position++) {
|
||||||
|
bool bit = rvswd_read_bit();
|
||||||
|
rval <<= 1;
|
||||||
|
rval |= bit;
|
||||||
|
parity ^= bit;
|
||||||
|
}
|
||||||
|
*value = rval;
|
||||||
|
|
||||||
|
// Parity bit
|
||||||
|
bool parity_read = rvswd_read_bit();
|
||||||
|
if (parity_read != parity) goto read_end;
|
||||||
|
|
||||||
|
rvswd_read_bit(); // ??
|
||||||
|
rvswd_read_bit(); // ??
|
||||||
|
rvswd_read_bit(); // ??
|
||||||
|
rvswd_write_bit(1); // 0 for register, 1 for value
|
||||||
|
rvswd_write_bit(0); // ??? Seems to have something to do with halting?
|
||||||
|
|
||||||
|
rvswd_stop();
|
||||||
|
|
||||||
|
read_end:
|
||||||
|
interrupts();
|
||||||
|
sleep_us(STOP_WAIT);
|
||||||
|
BB_PRINTF_DEBUG("wrong parity: %d\n", parity_read);
|
||||||
|
return (parity == parity_read) ? 0 : -1;
|
||||||
|
}
|
||||||
59
fw/programmer/src/main.cpp
Normal file
59
fw/programmer/src/main.cpp
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
#include "pico/time.h"
|
||||||
|
#include <Arduino.h>
|
||||||
|
#include <SerialUSB.h>
|
||||||
|
|
||||||
|
#define BB_PRINTF_DEBUG(...) Serial.printf(__VA_ARGS__)
|
||||||
|
//#define BB_PRINTF_DEBUG(...)
|
||||||
|
#include "bitbang_rvswdio.h"
|
||||||
|
#include "bitbang_rvswdio_pico.h"
|
||||||
|
|
||||||
|
SWIOState state = {.opmode = 2};
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
Serial.begin(115200);
|
||||||
|
Serial.setTimeout(100);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void setup1()
|
||||||
|
{
|
||||||
|
pinMode(LED_BUILTIN, OUTPUT);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
if (Serial.available() > 0) {
|
||||||
|
char cmd = Serial.read();
|
||||||
|
int ret = 0;
|
||||||
|
uint8_t reg;
|
||||||
|
|
||||||
|
switch (cmd) {
|
||||||
|
case '!':
|
||||||
|
state = {};
|
||||||
|
ret = InitializeSWDSWIO(&state);
|
||||||
|
if (ret != 0) {
|
||||||
|
Serial.printf("error initializing SWD: %d\n", ret);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
ret = DetermineChipTypeAndSectorInfo(&state, NULL);
|
||||||
|
if (ret != 0) {
|
||||||
|
Serial.printf("failed to determine chip type: %d\n", ret);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
Serial.printf("chip type is %x\n", state.target_chip_type);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
Serial.printf("unknown command '%c'\n", cmd);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void loop1()
|
||||||
|
{
|
||||||
|
digitalWrite(LED_BUILTIN, 1);
|
||||||
|
delay(200);
|
||||||
|
digitalWrite(LED_BUILTIN, 0);
|
||||||
|
delay(200);
|
||||||
|
}
|
||||||
199
fw/programmer/test/rvswd.c
Normal file
199
fw/programmer/test/rvswd.c
Normal file
@ -0,0 +1,199 @@
|
|||||||
|
#include "rvswd.h"
|
||||||
|
#include <inttypes.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <hardware/gpio.h>
|
||||||
|
#include <pico/time.h>
|
||||||
|
|
||||||
|
|
||||||
|
// adapted from https://github.com/Nicolai-Electronics/esp32-component-rvswd
|
||||||
|
|
||||||
|
rvswd_result_t rvswd_init(rvswd_handle_t* handle) {
|
||||||
|
//gpio_config_t swio_cfg = {
|
||||||
|
// .pin_bit_mask = BIT64(handle->swdio),
|
||||||
|
// .mode = GPIO_MODE_INPUT_OUTPUT_OD,
|
||||||
|
// .pull_up_en = true,
|
||||||
|
// .pull_down_en = false,
|
||||||
|
// .intr_type = GPIO_INTR_DISABLE,
|
||||||
|
//};
|
||||||
|
gpio_init(handle->swdio);
|
||||||
|
gpio_set_pulls(handle->swdio, true, false);
|
||||||
|
gpio_put(handle->swdio, 0);
|
||||||
|
gpio_set_dir(handle->swdio, GPIO_IN);
|
||||||
|
|
||||||
|
//gpio_config_t swck_cfg = {
|
||||||
|
// .pin_bit_mask = BIT64(handle->swclk),
|
||||||
|
// .mode = GPIO_MODE_OUTPUT,
|
||||||
|
// .pull_up_en = false,
|
||||||
|
// .pull_down_en = false,
|
||||||
|
// .intr_type = GPIO_INTR_DISABLE,
|
||||||
|
//};
|
||||||
|
gpio_init(handle->swclk);
|
||||||
|
gpio_set_pulls(handle->swclk, false, false);
|
||||||
|
gpio_put(handle->swclk, 0);
|
||||||
|
gpio_set_dir(handle->swclk, GPIO_OUT);
|
||||||
|
|
||||||
|
return RVSWD_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
rvswd_result_t rvswd_start(rvswd_handle_t* handle) {
|
||||||
|
// Start with both lines high
|
||||||
|
// open drain emulation, input for high (pulled up) and out for low (forced)
|
||||||
|
gpio_set_dir(handle->swdio, GPIO_IN); // high
|
||||||
|
gpio_put(handle->swclk, 1);
|
||||||
|
sleep_us(2);
|
||||||
|
|
||||||
|
// Pull data low
|
||||||
|
gpio_set_dir(handle->swdio, GPIO_OUT); // low
|
||||||
|
gpio_put(handle->swclk, 1);
|
||||||
|
sleep_us(1);
|
||||||
|
|
||||||
|
// Pull clock low
|
||||||
|
gpio_set_dir(handle->swdio, GPIO_OUT); // low
|
||||||
|
gpio_put(handle->swclk, 0);
|
||||||
|
sleep_us(1);
|
||||||
|
|
||||||
|
return RVSWD_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
rvswd_result_t rvswd_stop(rvswd_handle_t* handle) {
|
||||||
|
// Pull data low
|
||||||
|
gpio_set_dir(handle->swdio, GPIO_OUT);
|
||||||
|
sleep_us(1);
|
||||||
|
gpio_put(handle->swclk, 1);
|
||||||
|
sleep_us(2);
|
||||||
|
|
||||||
|
// Let data float high
|
||||||
|
gpio_set_dir(handle->swdio, GPIO_IN);
|
||||||
|
sleep_us(1);
|
||||||
|
return RVSWD_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
rvswd_result_t rvswd_reset(rvswd_handle_t* handle) {
|
||||||
|
// set data floating high
|
||||||
|
gpio_set_dir(handle->swdio, GPIO_IN);
|
||||||
|
sleep_us(1);
|
||||||
|
// let clock go brrr
|
||||||
|
for (uint8_t i = 0; i < 100; i++) {
|
||||||
|
gpio_put(handle->swclk, 0);
|
||||||
|
sleep_us(1);
|
||||||
|
gpio_put(handle->swclk, 1);
|
||||||
|
sleep_us(1);
|
||||||
|
}
|
||||||
|
return rvswd_stop(handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
#define OD_PULL(pin, value) gpio_set_dir((pin), (value) ? GPIO_IN : GPIO_OUT)
|
||||||
|
|
||||||
|
void rvswd_write_bit(rvswd_handle_t* handle, bool value) {
|
||||||
|
OD_PULL(handle->swdio, value);
|
||||||
|
gpio_put(handle->swclk, 0);
|
||||||
|
// FIXME: does this need a delay?
|
||||||
|
gpio_put(handle->swclk, 1); // Data is sampled on rising edge of clock
|
||||||
|
}
|
||||||
|
|
||||||
|
bool rvswd_read_bit(rvswd_handle_t* handle) {
|
||||||
|
// let data float high
|
||||||
|
gpio_set_dir(handle->swdio, GPIO_IN);
|
||||||
|
// pulse clock
|
||||||
|
gpio_put(handle->swclk, 0);
|
||||||
|
gpio_put(handle->swclk, 1); // Data is output on rising edge of clock
|
||||||
|
// FIXME: does this need a delay?
|
||||||
|
return gpio_get(handle->swdio);
|
||||||
|
}
|
||||||
|
|
||||||
|
rvswd_result_t rvswd_write(rvswd_handle_t* handle, uint8_t reg, uint32_t value) {
|
||||||
|
rvswd_start(handle);
|
||||||
|
|
||||||
|
// ADDR HOST
|
||||||
|
bool parity = false; // This time it's odd parity?
|
||||||
|
for (uint8_t position = 0; position < 7; position++) {
|
||||||
|
bool bit = (reg >> (6 - position)) & 1;
|
||||||
|
rvswd_write_bit(handle, bit);
|
||||||
|
if (bit) parity = !parity;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Operation: write
|
||||||
|
rvswd_write_bit(handle, true);
|
||||||
|
parity = !parity;
|
||||||
|
|
||||||
|
// Parity bit (even)
|
||||||
|
rvswd_write_bit(handle, parity);
|
||||||
|
|
||||||
|
rvswd_write_bit(handle, 1);
|
||||||
|
rvswd_write_bit(handle, 0);
|
||||||
|
rvswd_write_bit(handle, 1);
|
||||||
|
rvswd_write_bit(handle, 0);
|
||||||
|
rvswd_write_bit(handle, 1);
|
||||||
|
|
||||||
|
// Data
|
||||||
|
parity = false; // This time it's even parity?
|
||||||
|
for (uint8_t position = 0; position < 32; position++) {
|
||||||
|
bool bit = (value >> (31 - position)) & 1;
|
||||||
|
rvswd_write_bit(handle, bit);
|
||||||
|
if (bit) parity = !parity;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parity bit
|
||||||
|
rvswd_write_bit(handle, parity);
|
||||||
|
|
||||||
|
rvswd_write_bit(handle, 1);
|
||||||
|
rvswd_write_bit(handle, 0);
|
||||||
|
rvswd_write_bit(handle, 1);
|
||||||
|
rvswd_write_bit(handle, 1);
|
||||||
|
rvswd_write_bit(handle, 1);
|
||||||
|
|
||||||
|
rvswd_stop(handle);
|
||||||
|
|
||||||
|
return RVSWD_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
rvswd_result_t rvswd_read(rvswd_handle_t* handle, uint8_t reg, uint32_t* value) {
|
||||||
|
bool parity;
|
||||||
|
|
||||||
|
rvswd_start(handle);
|
||||||
|
|
||||||
|
// ADDR HOST
|
||||||
|
parity = false;
|
||||||
|
for (uint8_t position = 0; position < 7; position++) {
|
||||||
|
bool bit = (reg >> (6 - position)) & 1;
|
||||||
|
rvswd_write_bit(handle, bit);
|
||||||
|
if (bit) parity = !parity;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Operation: read
|
||||||
|
rvswd_write_bit(handle, false);
|
||||||
|
|
||||||
|
// Parity bit (even)
|
||||||
|
rvswd_write_bit(handle, parity);
|
||||||
|
|
||||||
|
rvswd_write_bit(handle, 1);
|
||||||
|
rvswd_write_bit(handle, 0);
|
||||||
|
rvswd_write_bit(handle, 1);
|
||||||
|
rvswd_write_bit(handle, 0);
|
||||||
|
rvswd_write_bit(handle, 1);
|
||||||
|
|
||||||
|
*value = 0;
|
||||||
|
|
||||||
|
// Data
|
||||||
|
parity = false;
|
||||||
|
for (uint8_t position = 0; position < 32; position++) {
|
||||||
|
bool bit = rvswd_read_bit(handle);
|
||||||
|
if (bit) {
|
||||||
|
*value |= 1 << (31 - position);
|
||||||
|
}
|
||||||
|
if (bit) parity = !parity;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parity bit
|
||||||
|
bool parity_read = rvswd_read_bit(handle);
|
||||||
|
|
||||||
|
rvswd_write_bit(handle, 1);
|
||||||
|
rvswd_write_bit(handle, 0);
|
||||||
|
rvswd_write_bit(handle, 1);
|
||||||
|
rvswd_write_bit(handle, 1);
|
||||||
|
rvswd_write_bit(handle, 1);
|
||||||
|
|
||||||
|
rvswd_stop(handle);
|
||||||
|
|
||||||
|
return (parity == parity_read) ? RVSWD_OK : RVSWD_FAIL;
|
||||||
|
}
|
||||||
20
fw/programmer/test/rvswd.h
Normal file
20
fw/programmer/test/rvswd.h
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
typedef struct rvswd_handle {
|
||||||
|
int swdio;
|
||||||
|
int swclk;
|
||||||
|
} rvswd_handle_t;
|
||||||
|
|
||||||
|
typedef enum rvswd_result {
|
||||||
|
RVSWD_OK = 0,
|
||||||
|
RVSWD_FAIL = 1,
|
||||||
|
RVSWD_INVALID_ARGS = 2,
|
||||||
|
RVSWD_PARITY_ERROR = 3,
|
||||||
|
} rvswd_result_t;
|
||||||
|
|
||||||
|
rvswd_result_t rvswd_init(rvswd_handle_t* handle);
|
||||||
|
rvswd_result_t rvswd_reset(rvswd_handle_t* handle);
|
||||||
|
rvswd_result_t rvswd_write(rvswd_handle_t* handle, uint8_t reg, uint32_t value);
|
||||||
|
rvswd_result_t rvswd_read(rvswd_handle_t* handle, uint8_t reg, uint32_t* value);
|
||||||
@ -1,85 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2004-2005, Swedish Institute of Computer Science.
|
|
||||||
* All rights reserved.
|
|
||||||
*
|
|
||||||
* Redistribution and use in source and binary forms, with or without
|
|
||||||
* modification, are permitted provided that the following conditions
|
|
||||||
* are met:
|
|
||||||
* 1. Redistributions of source code must retain the above copyright
|
|
||||||
* notice, this list of conditions and the following disclaimer.
|
|
||||||
* 2. Redistributions in binary form must reproduce the above copyright
|
|
||||||
* notice, this list of conditions and the following disclaimer in the
|
|
||||||
* documentation and/or other materials provided with the distribution.
|
|
||||||
* 3. Neither the name of the Institute nor the names of its contributors
|
|
||||||
* may be used to endorse or promote products derived from this software
|
|
||||||
* without specific prior written permission.
|
|
||||||
*
|
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
||||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
||||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
||||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
||||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
||||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
||||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
||||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
||||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
||||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
||||||
* SUCH DAMAGE.
|
|
||||||
*
|
|
||||||
* This file is part of the Contiki operating system.
|
|
||||||
*
|
|
||||||
* Author: Adam Dunkels <adam@sics.se>
|
|
||||||
*
|
|
||||||
* $Id: lc-addrlabels.h,v 1.4 2006/06/03 11:29:43 adam Exp $
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \addtogroup lc
|
|
||||||
* @{
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \file
|
|
||||||
* Implementation of local continuations based on the "Labels as
|
|
||||||
* values" feature of gcc
|
|
||||||
* \author
|
|
||||||
* Adam Dunkels <adam@sics.se>
|
|
||||||
*
|
|
||||||
* This implementation of local continuations is based on a special
|
|
||||||
* feature of the GCC C compiler called "labels as values". This
|
|
||||||
* feature allows assigning pointers with the address of the code
|
|
||||||
* corresponding to a particular C label.
|
|
||||||
*
|
|
||||||
* For more information, see the GCC documentation:
|
|
||||||
* http://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef __LC_ADDRLABELS_H__
|
|
||||||
#define __LC_ADDRLABELS_H__
|
|
||||||
|
|
||||||
/** \hideinitializer */
|
|
||||||
typedef void * lc_t;
|
|
||||||
|
|
||||||
#define LC_INIT(s) s = NULL
|
|
||||||
|
|
||||||
#define LC_RESUME(s) \
|
|
||||||
do { \
|
|
||||||
if(s != NULL) { \
|
|
||||||
goto *s; \
|
|
||||||
} \
|
|
||||||
} while(0)
|
|
||||||
|
|
||||||
#define LC_CONCAT2(s1, s2) s1##s2
|
|
||||||
#define LC_CONCAT(s1, s2) LC_CONCAT2(s1, s2)
|
|
||||||
|
|
||||||
#define LC_SET(s) \
|
|
||||||
do { \
|
|
||||||
LC_CONCAT(LC_LABEL, __LINE__): \
|
|
||||||
(s) = &&LC_CONCAT(LC_LABEL, __LINE__); \
|
|
||||||
} while(0)
|
|
||||||
|
|
||||||
#define LC_END(s)
|
|
||||||
|
|
||||||
#endif /* __LC_ADDRLABELS_H__ */
|
|
||||||
/** @} */
|
|
||||||
@ -1,76 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2004-2005, Swedish Institute of Computer Science.
|
|
||||||
* All rights reserved.
|
|
||||||
*
|
|
||||||
* Redistribution and use in source and binary forms, with or without
|
|
||||||
* modification, are permitted provided that the following conditions
|
|
||||||
* are met:
|
|
||||||
* 1. Redistributions of source code must retain the above copyright
|
|
||||||
* notice, this list of conditions and the following disclaimer.
|
|
||||||
* 2. Redistributions in binary form must reproduce the above copyright
|
|
||||||
* notice, this list of conditions and the following disclaimer in the
|
|
||||||
* documentation and/or other materials provided with the distribution.
|
|
||||||
* 3. Neither the name of the Institute nor the names of its contributors
|
|
||||||
* may be used to endorse or promote products derived from this software
|
|
||||||
* without specific prior written permission.
|
|
||||||
*
|
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
||||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
||||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
||||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
||||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
||||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
||||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
||||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
||||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
||||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
||||||
* SUCH DAMAGE.
|
|
||||||
*
|
|
||||||
* This file is part of the Contiki operating system.
|
|
||||||
*
|
|
||||||
* Author: Adam Dunkels <adam@sics.se>
|
|
||||||
*
|
|
||||||
* $Id: lc-switch.h,v 1.4 2006/06/03 11:29:43 adam Exp $
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \addtogroup lc
|
|
||||||
* @{
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \file
|
|
||||||
* Implementation of local continuations based on switch() statment
|
|
||||||
* \author Adam Dunkels <adam@sics.se>
|
|
||||||
*
|
|
||||||
* This implementation of local continuations uses the C switch()
|
|
||||||
* statement to resume execution of a function somewhere inside the
|
|
||||||
* function's body. The implementation is based on the fact that
|
|
||||||
* switch() statements are able to jump directly into the bodies of
|
|
||||||
* control structures such as if() or while() statmenets.
|
|
||||||
*
|
|
||||||
* This implementation borrows heavily from Simon Tatham's coroutines
|
|
||||||
* implementation in C:
|
|
||||||
* http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef __LC_SWITCH_H__
|
|
||||||
#define __LC_SWITCH_H__
|
|
||||||
|
|
||||||
/* WARNING! lc implementation using switch() does not work if an
|
|
||||||
LC_SET() is done within another switch() statement! */
|
|
||||||
|
|
||||||
/** \hideinitializer */
|
|
||||||
typedef unsigned short lc_t;
|
|
||||||
|
|
||||||
#define LC_INIT(s) s = 0;
|
|
||||||
|
|
||||||
#define LC_RESUME(s) switch(s) { case 0:
|
|
||||||
|
|
||||||
#define LC_SET(s) s = __LINE__; case __LINE__:
|
|
||||||
|
|
||||||
#define LC_END(s) }
|
|
||||||
|
|
||||||
#endif /* __LC_SWITCH_H__ */
|
|
||||||
|
|
||||||
/** @} */
|
|
||||||
132
fw/pt/lc.h
132
fw/pt/lc.h
@ -1,132 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2004-2005, Swedish Institute of Computer Science.
|
|
||||||
* All rights reserved.
|
|
||||||
*
|
|
||||||
* Redistribution and use in source and binary forms, with or without
|
|
||||||
* modification, are permitted provided that the following conditions
|
|
||||||
* are met:
|
|
||||||
* 1. Redistributions of source code must retain the above copyright
|
|
||||||
* notice, this list of conditions and the following disclaimer.
|
|
||||||
* 2. Redistributions in binary form must reproduce the above copyright
|
|
||||||
* notice, this list of conditions and the following disclaimer in the
|
|
||||||
* documentation and/or other materials provided with the distribution.
|
|
||||||
* 3. Neither the name of the Institute nor the names of its contributors
|
|
||||||
* may be used to endorse or promote products derived from this software
|
|
||||||
* without specific prior written permission.
|
|
||||||
*
|
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
||||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
||||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
||||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
||||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
||||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
||||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
||||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
||||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
||||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
||||||
* SUCH DAMAGE.
|
|
||||||
*
|
|
||||||
* This file is part of the protothreads library.
|
|
||||||
*
|
|
||||||
* Author: Adam Dunkels <adam@sics.se>
|
|
||||||
*
|
|
||||||
* $Id: lc.h,v 1.2 2005/02/24 10:36:59 adam Exp $
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \addtogroup pt
|
|
||||||
* @{
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \defgroup lc Local continuations
|
|
||||||
* @{
|
|
||||||
*
|
|
||||||
* Local continuations form the basis for implementing protothreads. A
|
|
||||||
* local continuation can be <i>set</i> in a specific function to
|
|
||||||
* capture the state of the function. After a local continuation has
|
|
||||||
* been set can be <i>resumed</i> in order to restore the state of the
|
|
||||||
* function at the point where the local continuation was set.
|
|
||||||
*
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \file lc.h
|
|
||||||
* Local continuations
|
|
||||||
* \author
|
|
||||||
* Adam Dunkels <adam@sics.se>
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifdef DOXYGEN
|
|
||||||
/**
|
|
||||||
* Initialize a local continuation.
|
|
||||||
*
|
|
||||||
* This operation initializes the local continuation, thereby
|
|
||||||
* unsetting any previously set continuation state.
|
|
||||||
*
|
|
||||||
* \hideinitializer
|
|
||||||
*/
|
|
||||||
#define LC_INIT(lc)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set a local continuation.
|
|
||||||
*
|
|
||||||
* The set operation saves the state of the function at the point
|
|
||||||
* where the operation is executed. As far as the set operation is
|
|
||||||
* concerned, the state of the function does <b>not</b> include the
|
|
||||||
* call-stack or local (automatic) variables, but only the program
|
|
||||||
* counter and such CPU registers that needs to be saved.
|
|
||||||
*
|
|
||||||
* \hideinitializer
|
|
||||||
*/
|
|
||||||
#define LC_SET(lc)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Resume a local continuation.
|
|
||||||
*
|
|
||||||
* The resume operation resumes a previously set local continuation, thus
|
|
||||||
* restoring the state in which the function was when the local
|
|
||||||
* continuation was set. If the local continuation has not been
|
|
||||||
* previously set, the resume operation does nothing.
|
|
||||||
*
|
|
||||||
* \hideinitializer
|
|
||||||
*/
|
|
||||||
#define LC_RESUME(lc)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Mark the end of local continuation usage.
|
|
||||||
*
|
|
||||||
* The end operation signifies that local continuations should not be
|
|
||||||
* used any more in the function. This operation is not needed for
|
|
||||||
* most implementations of local continuation, but is required by a
|
|
||||||
* few implementations.
|
|
||||||
*
|
|
||||||
* \hideinitializer
|
|
||||||
*/
|
|
||||||
#define LC_END(lc)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \var typedef lc_t;
|
|
||||||
*
|
|
||||||
* The local continuation type.
|
|
||||||
*
|
|
||||||
* \hideinitializer
|
|
||||||
*/
|
|
||||||
#endif /* DOXYGEN */
|
|
||||||
|
|
||||||
#ifndef __LC_H__
|
|
||||||
#define __LC_H__
|
|
||||||
|
|
||||||
|
|
||||||
#ifdef LC_INCLUDE
|
|
||||||
#include LC_INCLUDE
|
|
||||||
#else
|
|
||||||
#include "lc-switch.h"
|
|
||||||
#endif /* LC_INCLUDE */
|
|
||||||
|
|
||||||
#endif /* __LC_H__ */
|
|
||||||
|
|
||||||
/** @} */
|
|
||||||
/** @} */
|
|
||||||
228
fw/pt/pt-sem.h
228
fw/pt/pt-sem.h
@ -1,228 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2004, Swedish Institute of Computer Science.
|
|
||||||
* All rights reserved.
|
|
||||||
*
|
|
||||||
* Redistribution and use in source and binary forms, with or without
|
|
||||||
* modification, are permitted provided that the following conditions
|
|
||||||
* are met:
|
|
||||||
* 1. Redistributions of source code must retain the above copyright
|
|
||||||
* notice, this list of conditions and the following disclaimer.
|
|
||||||
* 2. Redistributions in binary form must reproduce the above copyright
|
|
||||||
* notice, this list of conditions and the following disclaimer in the
|
|
||||||
* documentation and/or other materials provided with the distribution.
|
|
||||||
* 3. Neither the name of the Institute nor the names of its contributors
|
|
||||||
* may be used to endorse or promote products derived from this software
|
|
||||||
* without specific prior written permission.
|
|
||||||
*
|
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
||||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
||||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
||||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
||||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
||||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
||||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
||||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
||||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
||||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
||||||
* SUCH DAMAGE.
|
|
||||||
*
|
|
||||||
* This file is part of the protothreads library.
|
|
||||||
*
|
|
||||||
* Author: Adam Dunkels <adam@sics.se>
|
|
||||||
*
|
|
||||||
* $Id: pt-sem.h,v 1.2 2005/02/24 10:36:59 adam Exp $
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \addtogroup pt
|
|
||||||
* @{
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \defgroup ptsem Protothread semaphores
|
|
||||||
* @{
|
|
||||||
*
|
|
||||||
* This module implements counting semaphores on top of
|
|
||||||
* protothreads. Semaphores are a synchronization primitive that
|
|
||||||
* provide two operations: "wait" and "signal". The "wait" operation
|
|
||||||
* checks the semaphore counter and blocks the thread if the counter
|
|
||||||
* is zero. The "signal" operation increases the semaphore counter but
|
|
||||||
* does not block. If another thread has blocked waiting for the
|
|
||||||
* semaphore that is signalled, the blocked thread will become
|
|
||||||
* runnable again.
|
|
||||||
*
|
|
||||||
* Semaphores can be used to implement other, more structured,
|
|
||||||
* synchronization primitives such as monitors and message
|
|
||||||
* queues/bounded buffers (see below).
|
|
||||||
*
|
|
||||||
* The following example shows how the producer-consumer problem, also
|
|
||||||
* known as the bounded buffer problem, can be solved using
|
|
||||||
* protothreads and semaphores. Notes on the program follow after the
|
|
||||||
* example.
|
|
||||||
*
|
|
||||||
\code
|
|
||||||
#include "pt-sem.h"
|
|
||||||
|
|
||||||
#define NUM_ITEMS 32
|
|
||||||
#define BUFSIZE 8
|
|
||||||
|
|
||||||
static struct pt_sem mutex, full, empty;
|
|
||||||
|
|
||||||
PT_THREAD(producer(struct pt *pt))
|
|
||||||
{
|
|
||||||
static int produced;
|
|
||||||
|
|
||||||
PT_BEGIN(pt);
|
|
||||||
|
|
||||||
for(produced = 0; produced < NUM_ITEMS; ++produced) {
|
|
||||||
|
|
||||||
PT_SEM_WAIT(pt, &full);
|
|
||||||
|
|
||||||
PT_SEM_WAIT(pt, &mutex);
|
|
||||||
add_to_buffer(produce_item());
|
|
||||||
PT_SEM_SIGNAL(pt, &mutex);
|
|
||||||
|
|
||||||
PT_SEM_SIGNAL(pt, &empty);
|
|
||||||
}
|
|
||||||
|
|
||||||
PT_END(pt);
|
|
||||||
}
|
|
||||||
|
|
||||||
PT_THREAD(consumer(struct pt *pt))
|
|
||||||
{
|
|
||||||
static int consumed;
|
|
||||||
|
|
||||||
PT_BEGIN(pt);
|
|
||||||
|
|
||||||
for(consumed = 0; consumed < NUM_ITEMS; ++consumed) {
|
|
||||||
|
|
||||||
PT_SEM_WAIT(pt, &empty);
|
|
||||||
|
|
||||||
PT_SEM_WAIT(pt, &mutex);
|
|
||||||
consume_item(get_from_buffer());
|
|
||||||
PT_SEM_SIGNAL(pt, &mutex);
|
|
||||||
|
|
||||||
PT_SEM_SIGNAL(pt, &full);
|
|
||||||
}
|
|
||||||
|
|
||||||
PT_END(pt);
|
|
||||||
}
|
|
||||||
|
|
||||||
PT_THREAD(driver_thread(struct pt *pt))
|
|
||||||
{
|
|
||||||
static struct pt pt_producer, pt_consumer;
|
|
||||||
|
|
||||||
PT_BEGIN(pt);
|
|
||||||
|
|
||||||
PT_SEM_INIT(&empty, 0);
|
|
||||||
PT_SEM_INIT(&full, BUFSIZE);
|
|
||||||
PT_SEM_INIT(&mutex, 1);
|
|
||||||
|
|
||||||
PT_INIT(&pt_producer);
|
|
||||||
PT_INIT(&pt_consumer);
|
|
||||||
|
|
||||||
PT_WAIT_THREAD(pt, producer(&pt_producer) &
|
|
||||||
consumer(&pt_consumer));
|
|
||||||
|
|
||||||
PT_END(pt);
|
|
||||||
}
|
|
||||||
\endcode
|
|
||||||
*
|
|
||||||
* The program uses three protothreads: one protothread that
|
|
||||||
* implements the consumer, one thread that implements the producer,
|
|
||||||
* and one protothread that drives the two other protothreads. The
|
|
||||||
* program uses three semaphores: "full", "empty" and "mutex". The
|
|
||||||
* "mutex" semaphore is used to provide mutual exclusion for the
|
|
||||||
* buffer, the "empty" semaphore is used to block the consumer is the
|
|
||||||
* buffer is empty, and the "full" semaphore is used to block the
|
|
||||||
* producer is the buffer is full.
|
|
||||||
*
|
|
||||||
* The "driver_thread" holds two protothread state variables,
|
|
||||||
* "pt_producer" and "pt_consumer". It is important to note that both
|
|
||||||
* these variables are declared as <i>static</i>. If the static
|
|
||||||
* keyword is not used, both variables are stored on the stack. Since
|
|
||||||
* protothreads do not store the stack, these variables may be
|
|
||||||
* overwritten during a protothread wait operation. Similarly, both
|
|
||||||
* the "consumer" and "producer" protothreads declare their local
|
|
||||||
* variables as static, to avoid them being stored on the stack.
|
|
||||||
*
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \file
|
|
||||||
* Couting semaphores implemented on protothreads
|
|
||||||
* \author
|
|
||||||
* Adam Dunkels <adam@sics.se>
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef __PT_SEM_H__
|
|
||||||
#define __PT_SEM_H__
|
|
||||||
|
|
||||||
#include "pt.h"
|
|
||||||
|
|
||||||
struct pt_sem {
|
|
||||||
unsigned int count;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Initialize a semaphore
|
|
||||||
*
|
|
||||||
* This macro initializes a semaphore with a value for the
|
|
||||||
* counter. Internally, the semaphores use an "unsigned int" to
|
|
||||||
* represent the counter, and therefore the "count" argument should be
|
|
||||||
* within range of an unsigned int.
|
|
||||||
*
|
|
||||||
* \param s (struct pt_sem *) A pointer to the pt_sem struct
|
|
||||||
* representing the semaphore
|
|
||||||
*
|
|
||||||
* \param c (unsigned int) The initial count of the semaphore.
|
|
||||||
* \hideinitializer
|
|
||||||
*/
|
|
||||||
#define PT_SEM_INIT(s, c) (s)->count = c
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Wait for a semaphore
|
|
||||||
*
|
|
||||||
* This macro carries out the "wait" operation on the semaphore. The
|
|
||||||
* wait operation causes the protothread to block while the counter is
|
|
||||||
* zero. When the counter reaches a value larger than zero, the
|
|
||||||
* protothread will continue.
|
|
||||||
*
|
|
||||||
* \param pt (struct pt *) A pointer to the protothread (struct pt) in
|
|
||||||
* which the operation is executed.
|
|
||||||
*
|
|
||||||
* \param s (struct pt_sem *) A pointer to the pt_sem struct
|
|
||||||
* representing the semaphore
|
|
||||||
*
|
|
||||||
* \hideinitializer
|
|
||||||
*/
|
|
||||||
#define PT_SEM_WAIT(pt, s) \
|
|
||||||
do { \
|
|
||||||
PT_WAIT_UNTIL(pt, (s)->count > 0); \
|
|
||||||
--(s)->count; \
|
|
||||||
} while(0)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Signal a semaphore
|
|
||||||
*
|
|
||||||
* This macro carries out the "signal" operation on the semaphore. The
|
|
||||||
* signal operation increments the counter inside the semaphore, which
|
|
||||||
* eventually will cause waiting protothreads to continue executing.
|
|
||||||
*
|
|
||||||
* \param pt (struct pt *) A pointer to the protothread (struct pt) in
|
|
||||||
* which the operation is executed.
|
|
||||||
*
|
|
||||||
* \param s (struct pt_sem *) A pointer to the pt_sem struct
|
|
||||||
* representing the semaphore
|
|
||||||
*
|
|
||||||
* \hideinitializer
|
|
||||||
*/
|
|
||||||
#define PT_SEM_SIGNAL(pt, s) ++(s)->count
|
|
||||||
|
|
||||||
#endif /* __PT_SEM_H__ */
|
|
||||||
|
|
||||||
/** @} */
|
|
||||||
/** @} */
|
|
||||||
|
|
||||||
323
fw/pt/pt.h
323
fw/pt/pt.h
@ -1,323 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (c) 2004-2005, Swedish Institute of Computer Science.
|
|
||||||
* All rights reserved.
|
|
||||||
*
|
|
||||||
* Redistribution and use in source and binary forms, with or without
|
|
||||||
* modification, are permitted provided that the following conditions
|
|
||||||
* are met:
|
|
||||||
* 1. Redistributions of source code must retain the above copyright
|
|
||||||
* notice, this list of conditions and the following disclaimer.
|
|
||||||
* 2. Redistributions in binary form must reproduce the above copyright
|
|
||||||
* notice, this list of conditions and the following disclaimer in the
|
|
||||||
* documentation and/or other materials provided with the distribution.
|
|
||||||
* 3. Neither the name of the Institute nor the names of its contributors
|
|
||||||
* may be used to endorse or promote products derived from this software
|
|
||||||
* without specific prior written permission.
|
|
||||||
*
|
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
||||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
||||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
||||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
||||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
||||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
||||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
||||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
||||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
||||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
||||||
* SUCH DAMAGE.
|
|
||||||
*
|
|
||||||
* This file is part of the Contiki operating system.
|
|
||||||
*
|
|
||||||
* Author: Adam Dunkels <adam@sics.se>
|
|
||||||
*
|
|
||||||
* $Id: pt.h,v 1.7 2006/10/02 07:52:56 adam Exp $
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \addtogroup pt
|
|
||||||
* @{
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \file
|
|
||||||
* Protothreads implementation.
|
|
||||||
* \author
|
|
||||||
* Adam Dunkels <adam@sics.se>
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef __PT_H__
|
|
||||||
#define __PT_H__
|
|
||||||
|
|
||||||
#include "lc.h"
|
|
||||||
|
|
||||||
struct pt {
|
|
||||||
lc_t lc;
|
|
||||||
};
|
|
||||||
|
|
||||||
#define PT_WAITING 0
|
|
||||||
#define PT_YIELDED 1
|
|
||||||
#define PT_EXITED 2
|
|
||||||
#define PT_ENDED 3
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \name Initialization
|
|
||||||
* @{
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Initialize a protothread.
|
|
||||||
*
|
|
||||||
* Initializes a protothread. Initialization must be done prior to
|
|
||||||
* starting to execute the protothread.
|
|
||||||
*
|
|
||||||
* \param pt A pointer to the protothread control structure.
|
|
||||||
*
|
|
||||||
* \sa PT_SPAWN()
|
|
||||||
*
|
|
||||||
* \hideinitializer
|
|
||||||
*/
|
|
||||||
#define PT_INIT(pt) LC_INIT((pt)->lc)
|
|
||||||
|
|
||||||
/** @} */
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \name Declaration and definition
|
|
||||||
* @{
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Declaration of a protothread.
|
|
||||||
*
|
|
||||||
* This macro is used to declare a protothread. All protothreads must
|
|
||||||
* be declared with this macro.
|
|
||||||
*
|
|
||||||
* \param name_args The name and arguments of the C function
|
|
||||||
* implementing the protothread.
|
|
||||||
*
|
|
||||||
* \hideinitializer
|
|
||||||
*/
|
|
||||||
#define PT_THREAD(name_args) char name_args
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Declare the start of a protothread inside the C function
|
|
||||||
* implementing the protothread.
|
|
||||||
*
|
|
||||||
* This macro is used to declare the starting point of a
|
|
||||||
* protothread. It should be placed at the start of the function in
|
|
||||||
* which the protothread runs. All C statements above the PT_BEGIN()
|
|
||||||
* invokation will be executed each time the protothread is scheduled.
|
|
||||||
*
|
|
||||||
* \param pt A pointer to the protothread control structure.
|
|
||||||
*
|
|
||||||
* \hideinitializer
|
|
||||||
*/
|
|
||||||
#define PT_BEGIN(pt) { char PT_YIELD_FLAG = 1; LC_RESUME((pt)->lc)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Declare the end of a protothread.
|
|
||||||
*
|
|
||||||
* This macro is used for declaring that a protothread ends. It must
|
|
||||||
* always be used together with a matching PT_BEGIN() macro.
|
|
||||||
*
|
|
||||||
* \param pt A pointer to the protothread control structure.
|
|
||||||
*
|
|
||||||
* \hideinitializer
|
|
||||||
*/
|
|
||||||
#define PT_END(pt) LC_END((pt)->lc); PT_YIELD_FLAG = 0; \
|
|
||||||
PT_INIT(pt); return PT_ENDED; }
|
|
||||||
|
|
||||||
/** @} */
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \name Blocked wait
|
|
||||||
* @{
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Block and wait until condition is true.
|
|
||||||
*
|
|
||||||
* This macro blocks the protothread until the specified condition is
|
|
||||||
* true.
|
|
||||||
*
|
|
||||||
* \param pt A pointer to the protothread control structure.
|
|
||||||
* \param condition The condition.
|
|
||||||
*
|
|
||||||
* \hideinitializer
|
|
||||||
*/
|
|
||||||
#define PT_WAIT_UNTIL(pt, condition) \
|
|
||||||
do { \
|
|
||||||
LC_SET((pt)->lc); \
|
|
||||||
if(!(condition)) { \
|
|
||||||
return PT_WAITING; \
|
|
||||||
} \
|
|
||||||
} while(0)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Block and wait while condition is true.
|
|
||||||
*
|
|
||||||
* This function blocks and waits while condition is true. See
|
|
||||||
* PT_WAIT_UNTIL().
|
|
||||||
*
|
|
||||||
* \param pt A pointer to the protothread control structure.
|
|
||||||
* \param cond The condition.
|
|
||||||
*
|
|
||||||
* \hideinitializer
|
|
||||||
*/
|
|
||||||
#define PT_WAIT_WHILE(pt, cond) PT_WAIT_UNTIL((pt), !(cond))
|
|
||||||
|
|
||||||
/** @} */
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \name Hierarchical protothreads
|
|
||||||
* @{
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Block and wait until a child protothread completes.
|
|
||||||
*
|
|
||||||
* This macro schedules a child protothread. The current protothread
|
|
||||||
* will block until the child protothread completes.
|
|
||||||
*
|
|
||||||
* \note The child protothread must be manually initialized with the
|
|
||||||
* PT_INIT() function before this function is used.
|
|
||||||
*
|
|
||||||
* \param pt A pointer to the protothread control structure.
|
|
||||||
* \param thread The child protothread with arguments
|
|
||||||
*
|
|
||||||
* \sa PT_SPAWN()
|
|
||||||
*
|
|
||||||
* \hideinitializer
|
|
||||||
*/
|
|
||||||
#define PT_WAIT_THREAD(pt, thread) PT_WAIT_WHILE((pt), PT_SCHEDULE(thread))
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Spawn a child protothread and wait until it exits.
|
|
||||||
*
|
|
||||||
* This macro spawns a child protothread and waits until it exits. The
|
|
||||||
* macro can only be used within a protothread.
|
|
||||||
*
|
|
||||||
* \param pt A pointer to the protothread control structure.
|
|
||||||
* \param child A pointer to the child protothread's control structure.
|
|
||||||
* \param thread The child protothread with arguments
|
|
||||||
*
|
|
||||||
* \hideinitializer
|
|
||||||
*/
|
|
||||||
#define PT_SPAWN(pt, child, thread) \
|
|
||||||
do { \
|
|
||||||
PT_INIT((child)); \
|
|
||||||
PT_WAIT_THREAD((pt), (thread)); \
|
|
||||||
} while(0)
|
|
||||||
|
|
||||||
/** @} */
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \name Exiting and restarting
|
|
||||||
* @{
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Restart the protothread.
|
|
||||||
*
|
|
||||||
* This macro will block and cause the running protothread to restart
|
|
||||||
* its execution at the place of the PT_BEGIN() call.
|
|
||||||
*
|
|
||||||
* \param pt A pointer to the protothread control structure.
|
|
||||||
*
|
|
||||||
* \hideinitializer
|
|
||||||
*/
|
|
||||||
#define PT_RESTART(pt) \
|
|
||||||
do { \
|
|
||||||
PT_INIT(pt); \
|
|
||||||
return PT_WAITING; \
|
|
||||||
} while(0)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Exit the protothread.
|
|
||||||
*
|
|
||||||
* This macro causes the protothread to exit. If the protothread was
|
|
||||||
* spawned by another protothread, the parent protothread will become
|
|
||||||
* unblocked and can continue to run.
|
|
||||||
*
|
|
||||||
* \param pt A pointer to the protothread control structure.
|
|
||||||
*
|
|
||||||
* \hideinitializer
|
|
||||||
*/
|
|
||||||
#define PT_EXIT(pt) \
|
|
||||||
do { \
|
|
||||||
PT_INIT(pt); \
|
|
||||||
return PT_EXITED; \
|
|
||||||
} while(0)
|
|
||||||
|
|
||||||
/** @} */
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \name Calling a protothread
|
|
||||||
* @{
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Schedule a protothread.
|
|
||||||
*
|
|
||||||
* This function shedules a protothread. The return value of the
|
|
||||||
* function is non-zero if the protothread is running or zero if the
|
|
||||||
* protothread has exited.
|
|
||||||
*
|
|
||||||
* \param f The call to the C function implementing the protothread to
|
|
||||||
* be scheduled
|
|
||||||
*
|
|
||||||
* \hideinitializer
|
|
||||||
*/
|
|
||||||
#define PT_SCHEDULE(f) ((f) < PT_EXITED)
|
|
||||||
|
|
||||||
/** @} */
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \name Yielding from a protothread
|
|
||||||
* @{
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Yield from the current protothread.
|
|
||||||
*
|
|
||||||
* This function will yield the protothread, thereby allowing other
|
|
||||||
* processing to take place in the system.
|
|
||||||
*
|
|
||||||
* \param pt A pointer to the protothread control structure.
|
|
||||||
*
|
|
||||||
* \hideinitializer
|
|
||||||
*/
|
|
||||||
#define PT_YIELD(pt) \
|
|
||||||
do { \
|
|
||||||
PT_YIELD_FLAG = 0; \
|
|
||||||
LC_SET((pt)->lc); \
|
|
||||||
if(PT_YIELD_FLAG == 0) { \
|
|
||||||
return PT_YIELDED; \
|
|
||||||
} \
|
|
||||||
} while(0)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief Yield from the protothread until a condition occurs.
|
|
||||||
* \param pt A pointer to the protothread control structure.
|
|
||||||
* \param cond The condition.
|
|
||||||
*
|
|
||||||
* This function will yield the protothread, until the
|
|
||||||
* specified condition evaluates to true.
|
|
||||||
*
|
|
||||||
*
|
|
||||||
* \hideinitializer
|
|
||||||
*/
|
|
||||||
#define PT_YIELD_UNTIL(pt, cond) \
|
|
||||||
do { \
|
|
||||||
PT_YIELD_FLAG = 0; \
|
|
||||||
LC_SET((pt)->lc); \
|
|
||||||
if((PT_YIELD_FLAG == 0) || !(cond)) { \
|
|
||||||
return PT_YIELDED; \
|
|
||||||
} \
|
|
||||||
} while(0)
|
|
||||||
|
|
||||||
/** @} */
|
|
||||||
|
|
||||||
#endif /* __PT_H__ */
|
|
||||||
|
|
||||||
/** @} */
|
|
||||||
Loading…
x
Reference in New Issue
Block a user