Compare commits

14 Commits

Author SHA1 Message Date
NIIBE Yutaka
aeea3c31f8 Version 1.14. 2019-03-02 08:48:11 +09:00
NIIBE Yutaka
91dbfaf6b7 usart: SCEN should be configured after CR1 enabled. 2019-03-01 22:22:25 +09:00
NIIBE Yutaka
304441d393 usart: usart_config should be called by application. 2019-03-01 20:02:15 +09:00
NIIBE Yutaka
4780da5b5b Parity error enabled when parity is in use. 2019-03-01 20:01:43 +09:00
NIIBE Yutaka
a4aa99f772 Add smartcard interface support for ST Nucleo Board. 2019-02-28 22:35:55 +09:00
NIIBE Yutaka
3315435579 Fix unsigned-ness of MASK_MODE. 2019-02-25 17:00:42 +09:00
NIIBE Yutaka
bd330d81c3 Add comment for eventflag. 2019-02-22 14:00:44 +09:00
NIIBE Yutaka
078c8db5d7 Minor code-cleanup for priority inheritance. 2019-02-22 09:52:28 +09:00
NIIBE Yutaka
53e4d1a371 Add usart modes. 2019-02-21 16:44:01 +09:00
NIIBE Yutaka
2b18685cbf Use "System Control Block" instead of "System Control Registers". 2018-12-29 09:33:19 +09:00
NIIBE Yutaka
b6c90e3df4 Version 1.13. 2018-12-19 12:52:26 +09:00
NIIBE Yutaka
4d4f82fd06 Update copyright notice. 2018-12-19 12:47:20 +09:00
NIIBE Yutaka
1ef6256784 Fix calculation of ticks remained. 2018-12-08 10:06:33 +09:00
NIIBE Yutaka
1400e677e3 Change behavior of chopstx_poll to correctly update *USEC_P. 2018-12-07 18:01:39 +09:00
20 changed files with 195 additions and 55 deletions

View File

@@ -1,3 +1,55 @@
2019-03-02 NIIBE Yutaka <gniibe@fsij.org>
* VERSION: 1.14.
* doc/chopstx.texi (VERSION): 1.14.
2019-03-01 NIIBE Yutaka <gniibe@fsij.org>
* contrib/usart-stm32f103.c (usart_config): Fix SCEN setting
procedure.
2019-03-01 NIIBE Yutaka <gniibe@fsij.org>
* contrib/usart-stm32f103.c (usart_config): Support parity error
interrupt setting.
(usart_init): Don't call usart_config here.
* example-usb-serial/sample.c: It's application calling
usart_config.
2019-02-28 NIIBE Yutaka <gniibe@fsij.org>
* contrib/usart.h (BSCARD): New.
* contrib/usart-stm32f103.c (BSCARD): Baudrate for smartcard.
(usart_config_clken): New.
(usart_config): Fix for MODE_SMARTCARD.
* board/board-st-nucleo-f103.h: Define pins for smartcard
interface.
2019-02-21 NIIBE Yutaka <gniibe@fsij.org>
* contrib/usart.h (MODE_SMARTCARD, MODE_IRDA, MODE_IRDA_LP)
(MASK_MODE): New.
* contrib/usart-stm32f103.c (usart_config): Add support for
those modes.
2018-12-19 NIIBE Yutaka <gniibe@fsij.org>
* VERSION: 1.13.
* doc/chopstx.texi (VERSION): 1.13.
2018-12-08 NIIBE Yutaka <gniibe@fsij.org>
* chopstx.c (chx_timer_dequeue): Fix calculation of return value.
2018-12-07 NIIBE Yutaka <gniibe@fsij.org>
* chopstx.c (chx_timer_dequeue): Return ticks remained.
(chx_wakeup, chopstx_mutex_lock): Use return value of
chx_timer_dequeue.
(chx_snooze): Update *USEC_P, accordingly.
2018-11-12 NIIBE Yutaka <gniibe@fsij.org>
* VERSION: 1.12.

17
NEWS
View File

@@ -1,6 +1,23 @@
NEWS - Noteworthy changes
* Major changes in Chopstx 1.14
Released 2019-03-02
** Enhancement of driver: USART for STM32
Now, it supports smartcard communication.
* Major changes in Chopstx 1.13
Released 2018-12-19
** API fix (redefinition): chopstx_poll
In old implementations, when chopstx_poll returns by non-timeout
event, *USEC_P is not updated. Now, it is updated.
* Major changes in Chopstx 1.12
Released 2018-11-12

4
README
View File

@@ -1,6 +1,6 @@
Chopstx - Threads and only Threads
Version 1.12
2018-11-12
Version 1.14
2018-03-02
Niibe Yutaka
Flying Stone Technology

View File

@@ -1 +1 @@
release/1.12
release/1.14

View File

@@ -49,17 +49,26 @@
* Port B setup.
* PB0 - input with pull-up: AN8 for NeuG
* PB1 - input with pull-up: AN9 for NeuG
* PB10 - Alternate function push pull output 2MHz USART3-TX
* PB11 - Input with pull-up USART3-RX
* PB12 - Alternate function push pull output 2MHz USART3-CK
* PB13 - Input with pull-up USART3-CTS
* PB14 - Alternate function push pull output 2MHz USART3-RTS
* ---
* ---
* PB4 - Input with pull-up: Card insertion detect: 0 when detected
* ---
* PB6 - Output push pull 2MHz: Vcc for card: default 0
* ---
* PB8 - Output push pull 2MHz: Vpp for card: default 0
* PB9 - Output push pull 2MHz: RST for card: default 0
* PB10 - Alternate function open-drain output 50MHz USART3-TX
* PB11 - Input with pull-up USART3-RX
* PB12 - Alternate function push pull output 50MHz USART3-CK
* PB13 - Input with pull-up USART3-CTS
* PB14 - Alternate function push pull output 50MHz USART3-RTS
* ---
* ------------------------ Default
* PBx - input with pull-up.
*/
#define VAL_GPIO_OTHER_ODR 0xFFFFFFFF
#define VAL_GPIO_OTHER_CRL 0x88888888 /* PB7...PB0 */
#define VAL_GPIO_OTHER_CRH 0x8A8A8A88 /* PB15...PB8 */
#define VAL_GPIO_OTHER_ODR 0xFFFFFCBF
#define VAL_GPIO_OTHER_CRL 0x82888888 /* PB7...PB0 */
#define VAL_GPIO_OTHER_CRH 0x8B8B8F22 /* PB15...PB8 */
#define RCC_ENR_IOP_EN (RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPBEN)
#define RCC_RSTR_IOP_RST (RCC_APB2RSTR_IOPARST | RCC_APB2RSTR_IOPBRST)

View File

@@ -293,8 +293,8 @@ chx_request_preemption (uint16_t prio)
* AAPCS: ARM Architecture Procedure Call Standard
*
* Returns:
* 1 on wakeup by others.
* 0 on normal wakeup (timer expiration, lock aquirement).
* >= 1 on wakeup by others, value means ticks remained for sleep.
* 0 on normal wakeup (timer expiration, lock acquirement).
* -1 on cancellation.
*/
static uintptr_t __attribute__ ((naked, noinline))

View File

@@ -1,7 +1,7 @@
/*
* chopstx.c - Threads and only threads.
*
* Copyright (C) 2013, 2014, 2015, 2016, 2017, 2018
* Copyright (C) 2013, 2014, 2015, 2016, 2017, 2018, 2019
* Flying Stone Technology
* Author: NIIBE Yutaka <gniibe@fsij.org>
*
@@ -107,7 +107,7 @@ static struct chx_queue q_intr;
static void chx_request_preemption (uint16_t prio);
static int chx_wakeup (struct chx_pq *p);
static struct chx_thread * chx_timer_insert (struct chx_thread *tp, uint32_t usec);
static void chx_timer_dequeue (struct chx_thread *tp);
static uint32_t chx_timer_dequeue (struct chx_thread *tp);
@@ -349,29 +349,35 @@ chx_timer_insert (struct chx_thread *tp, uint32_t usec)
}
static void
static uint32_t
chx_timer_dequeue (struct chx_thread *tp)
{
struct chx_thread *tp_prev;
uint32_t ticks_remained;
chx_spin_lock (&q_timer.lock);
ticks_remained = chx_systick_get ();
tp_prev = (struct chx_thread *)tp->prev;
if (tp_prev == (struct chx_thread *)&q_timer.q)
{
if (tp->next == (struct chx_pq *)&q_timer.q)
chx_set_timer (tp_prev, 0); /* Cancel timer*/
chx_systick_reload (0); /* Cancel timer. */
else
{ /* Update timer. */
uint32_t next_ticks = chx_systick_get () + tp->v;
chx_set_timer (tp_prev, next_ticks);
}
chx_systick_reload (ticks_remained + tp->v); /* Update timer. */
}
else
tp_prev->v += tp->v;
{
struct chx_pq *p;
for (p = q_timer.q.next; p != (struct chx_pq *)tp; p = p->next)
ticks_remained += p->v;
tp_prev->v += tp->v;
}
ll_dequeue ((struct chx_pq *)tp);
tp->v = 0;
chx_spin_unlock (&q_timer.lock);
return ticks_remained;
}
@@ -502,9 +508,11 @@ chx_wakeup (struct chx_pq *pq)
tp = px->master;
if (tp->state == THREAD_WAIT_POLL)
{
tp->v = (uintptr_t)1;
if (tp->parent == &q_timer.q)
chx_timer_dequeue (tp);
tp->v = (uintptr_t)chx_timer_dequeue (tp);
else
tp->v = (uintptr_t)1;
chx_ready_enqueue (tp);
if (!running || tp->prio > running->prio)
yield = 1;
@@ -559,19 +567,21 @@ chx_exit (void *retval)
/*
* Lower layer mutex unlocking. Called with schedule lock held.
* Return PRIO of the thread which is waken up.
*/
static chopstx_prio_t
chx_mutex_unlock (chopstx_mutex_t *mutex)
{
struct chx_thread *tp;
chopstx_prio_t prio = 0;
mutex->owner = NULL;
running->mutex_list = mutex->list;
mutex->list = NULL;
tp = (struct chx_thread *)ll_pop (&mutex->q);
if (tp)
if (!tp)
return 0;
else
{
uint16_t newprio = running->prio_orig;
chopstx_mutex_t *m;
@@ -587,11 +597,8 @@ chx_mutex_unlock (chopstx_mutex_t *mutex)
/* Then, assign it. */
running->prio = newprio;
if (prio < tp->prio)
prio = tp->prio;
return tp->prio;
}
return prio;
}
#define CHOPSTX_PRIO_MASK ((1 << CHOPSTX_PRIO_BITS) - 1)
@@ -679,6 +686,11 @@ chx_snooze (uint32_t state, uint32_t *usec_p)
r = chx_sched (CHX_SLEEP);
if (r == 0)
*usec_p -= usec0;
else if (r > 0)
{
*usec_p -= (usec0 - r / MHZ);
r = 1;
}
return r;
}
@@ -803,9 +815,10 @@ chopstx_mutex_lock (chopstx_mutex_t *mutex)
if (tp0->state == THREAD_WAIT_TIME
|| tp0->state == THREAD_WAIT_POLL)
{
tp0->v = (uintptr_t)1;
if (tp0->parent == &q_timer.q)
chx_timer_dequeue (tp0);
tp0->v = (uintptr_t)chx_timer_dequeue (tp0);
else
tp0->v = (uintptr_t)1;
chx_ready_enqueue (tp0);
tp0 = NULL;
@@ -1347,7 +1360,8 @@ chx_proxy_init (struct chx_px *px, uint32_t *cp)
/**
* chopstx_poll - wait for condition variable, thread's exit, or IRQ
* @usec_p: Pointer to usec for timeout. Forever if NULL.
* @usec_p: Pointer to usec for timeout. Forever if NULL. It is
* updated on return
* @n: Number of poll descriptors
* @pd_array: Pointer to an array of poll descriptor pointer which
* should be one of:

View File

@@ -1,7 +1,7 @@
/*
* chopstx.h - Threads and only threads.
*
* Copyright (C) 2013, 2016, 2017 Flying Stone Technology
* Copyright (C) 2013, 2016, 2017, 2018 Flying Stone Technology
* Author: NIIBE Yutaka <gniibe@fsij.org>
*
* This file is a part of Chopstx, a thread library for embedded.

View File

@@ -1,7 +1,7 @@
/*
* usart-stm32.c - USART driver for STM32F103 (USART2 and USART3)
*
* Copyright (C) 2017 g10 Code GmbH
* Copyright (C) 2017, 2019 g10 Code GmbH
* Author: NIIBE Yutaka <gniibe@fsij.org>
*
* This file is a part of Chopstx, a thread library for embedded.
@@ -104,6 +104,7 @@ static const struct brr_setting brr_table[] = {
{ B230400, ( 9 << 4)|12},
{ B460800, ( 4 << 4)|14},
{ B921600, ( 2 << 4)|7},
{ BSCARD, ( 232 << 4)|8},
};
static void *usart_main (void *arg);
@@ -111,6 +112,18 @@ static void *usart_main (void *arg);
static struct usart_stat usart2_stat;
static struct usart_stat usart3_stat;
void
usart_config_clken (uint8_t dev_no, int on)
{
struct USART *USARTx = get_usart_dev (dev_no);
if (on)
USARTx->CR2 |= (1 << 11);
else
USARTx->CR2 &= ~(1 << 11);
}
int
usart_config (uint8_t dev_no, uint32_t config_bits)
{
@@ -136,9 +149,9 @@ usart_config (uint8_t dev_no, uint32_t config_bits)
return -1;
if ((config_bits & PARENB) == 0)
cr1_config &= ~USART_CR1_PCE;
cr1_config &= ~(USART_CR1_PCE | USART_CR1_PEIE);
else
cr1_config |= USART_CR1_PCE;
cr1_config |= (USART_CR1_PCE | USART_CR1_PEIE);
if ((config_bits & PARODD) == 0)
cr1_config &= ~USART_CR1_PS;
@@ -169,6 +182,21 @@ usart_config (uint8_t dev_no, uint32_t config_bits)
USARTx->CR3 = 0;
USARTx->CR1 = cr1_config;
/* SCEN (smartcard enable) should be set _after_ CR1. */
if ((config_bits & MASK_MODE))
{
if ((config_bits & MASK_MODE) == MODE_SMARTCARD)
{
USARTx->GTPR = (16 << 8) | 5;
USARTx->CR3 |= ((1 << 5) | (1 << 4));
}
else if ((config_bits & MASK_MODE) == MODE_IRDA)
USARTx->CR3 |= (1 << 1);
else if ((config_bits & MASK_MODE) == MODE_IRDA_LP)
USARTx->CR3 |= (1 << 2) | (1 << 1);
}
return 0;
}
@@ -187,8 +215,6 @@ usart_init (uint16_t prio, uintptr_t stack_addr, size_t stack_size,
RCC->APB1RSTR = ((1 << 18) | (1 << 17));
RCC->APB1RSTR = 0;
usart_config (2, B115200 | CS8 | STOP1B);
usart_config (3, B115200 | CS8 | STOP1B);
chopstx_create (prio, stack_addr, stack_size, usart_main, NULL);
}

View File

@@ -11,6 +11,7 @@
#define B230400 26
#define B460800 27
#define B921600 28
#define BSCARD 63
#define MASK_BAUD 0x3f
/* POSIX supports 5, 6. USB suppots 16 */
@@ -43,6 +44,13 @@ PAR_BITS 3
/* USB: SERIAL_STATE
DSR DCD RI */
/* non-POSIX, non-USB-CDC configs */
#define MODE_SMARTCARD (1 << 30)
#define MODE_IRDA (2UL << 30)
#define MODE_IRDA_LP (3UL << 30)
#define MASK_MODE (0x3UL << 30)
/* 0: standard, 1: smartcard, 2: IrDA, 3: IrDA-LP */
struct usart_stat {
uint8_t dev_no;
@@ -64,3 +72,4 @@ int usart_read (uint8_t dev_no, char *buf, uint16_t buflen);
int usart_write (uint8_t dev_no, char *buf, uint16_t buflen);
const struct usart_stat *usart_stat (uint8_t dev_no);
int usart_send_break (uint8_t dev_no);
void usart_config_clken (uint8_t dev_no, int on);

View File

@@ -1,7 +1,7 @@
\input texinfo @c -*-texinfo-*-
@c %**start of header
@setfilename chopstx.info
@set VERSION 1.12
@set VERSION 1.14
@settitle Chopstx Reference Manual
@c Unify some of the indices.
@syncodeindex tp fn

View File

@@ -74,6 +74,9 @@ eventflag_prepare_poll (struct eventflag *ev, chopstx_poll_cond_t *poll_desc)
}
/* When multiple events are marked, event with lower bit has precedence.
Because __builtin_ffs returns the least significant 1-bit. */
eventmask_t
eventflag_get (struct eventflag *ev)
{

View File

@@ -1,12 +1,19 @@
This is an application example using ST Nucleo F103 board.
SB62 and SB63 should be soldered.
ST-Link/V2 is disconnected (SB13 and SB14).
SB62 and SB63 should be soldered to connect PA2 and PA3 to CN10
connector.
ST-Link/V2's TX and RX are disconnected (by removing SB13 and SB14).
Smartcard can be connected, by using USART3_TX for I/O and USART3_CK
for CLK, PB6 for Vcc, PB8 for Vpp, and PB9 for RST.
NOTE:
Using the USB CDC-ACM for serial communication is a kind of wrong,
because it's designed for modem; No way to control CTSRTS.
because it's designed for modem; In the protocol, we have no way to
control the CTSRTS signal.
TIOCGICOUNT
@@ -19,4 +26,4 @@ TODO:
* stats report control
By vendor specific control?
* Half-duplex support
* Support of other communication mode: smartcard, IrDA, etc.
* Support of other communication mode: IrDA, etc.

View File

@@ -238,6 +238,9 @@ main (int argc, const char *argv[])
usart_init (PRIO_USART, STACK_ADDR_USART, STACK_SIZE_USART, ss_notify);
usart_config (2, B115200 | CS8 | STOP1B);
usart_config (3, B115200 | CS8 | STOP1B);
cdc_usart0.cdc = cdc_open (0);
cdc_usart0.dev_no = 2;
cdc_usart1.cdc = cdc_open (1);

View File

@@ -23,14 +23,14 @@
#include "board.h"
#define ADDR_VECTORS (0x00000900)
#define ADDR_SCR_VTOR 0xe000ed08
#define ADDR_SCB_VTOR 0xe000ed08
static void __attribute__ ((naked,section(".fixed_function.reset")))
reset (void)
{
uint32_t r3 = ADDR_SCR_VTOR;
uint32_t r3 = ADDR_SCB_VTOR;
asm volatile ("str %2, [%0]\n\t" /* Set SCR->VTOR */
asm volatile ("str %2, [%0]\n\t" /* Set SCB->VTOR */
"ldr %0, [%2]\n\t" /* Stack address */
"msr MSP, %0\n\t" /* Exception handler stack. */
"ldr %0, [%2, #4]\n\t" /* The entry address */

View File

@@ -340,13 +340,13 @@ reset (void)
#else
extern const uint32_t FT0[256], FT1[256], FT2[256];
asm volatile ("cpsid i\n\t" /* Mask all interrupts. */
"ldr r0, 1f\n\t" /* r0 = SCR */
"ldr r0, 1f\n\t" /* r0 = SCB start */
"mov r1, pc\n\t" /* r1 = (PC + 0x1000) & ~0x0fff */
"mov r2, #0x1000\n\t"
"add r1, r1, r2\n\t"
"sub r2, r2, #1\n\t"
"bic r1, r1, r2\n\t"
"str r1, [r0, #8]\n\t" /* Set SCR->VCR */
"str r1, [r0, #8]\n\t" /* Set SCB->VTOR */
"ldr r0, [r1], #4\n\t"
"msr MSP, r0\n\t" /* Main (exception handler) stack. */
"ldr r0, [r1]\n\t" /* Reset handler. */

View File

@@ -321,13 +321,13 @@ reset (void)
* So, we take the address from PC.
*/
asm volatile ("cpsid i\n\t" /* Mask all interrupts. */
"ldr r0, 1f\n\t" /* r0 = SCR */
"ldr r0, 1f\n\t" /* r0 = SCB start */
"mov r1, pc\n\t" /* r1 = (PC + 0x1000) & ~0x0fff */
"mov r2, #0x1000\n\t"
"add r1, r1, r2\n\t"
"sub r2, r2, #1\n\t"
"bic r1, r1, r2\n\t"
"str r1, [r0, #8]\n\t" /* Set SCR->VCR */
"str r1, [r0, #8]\n\t" /* Set SCB->VTOR */
"ldr r0, [r1], #4\n\t"
"msr MSP, r0\n\t" /* Main (exception handler) stack. */
"ldr r0, [r1]\n\t" /* Reset handler. */

View File

@@ -1,7 +1,7 @@
/*
* usb-mkl27z.c - USB driver for MKL27Z
*
* Copyright (C) 2016 Flying Stone Technology
* Copyright (C) 2016, 2018 Flying Stone Technology
* Author: NIIBE Yutaka <gniibe@fsij.org>
*
* This file is a part of Chopstx, a thread library for embedded.

View File

@@ -1,7 +1,7 @@
/*
* usb-stm32f103.c - USB driver for STM32F103
*
* Copyright (C) 2016, 2017 Flying Stone Technology
* Copyright (C) 2016, 2017, 2018 Flying Stone Technology
* Author: NIIBE Yutaka <gniibe@fsij.org>
*
* This file is a part of Chopstx, a thread library for embedded.

View File

@@ -1,7 +1,7 @@
/*
* usb-usbip.c - USB Device Emulation (server side) by USBIP
*
* Copyright (C) 2017 g10 Code GmbH
* Copyright (C) 2017, 2018 g10 Code GmbH
* Author: NIIBE Yutaka <gniibe@fsij.org>
*
* This file is a part of Chopstx, a thread library for embedded.