34 #if defined(PIOS_INCLUDE_USART)
39 static void PIOS_USART_ChangeBaud(uintptr_t usart_id, uint32_t baud);
40 static void PIOS_USART_RegisterRxCallback(uintptr_t usart_id,
pios_com_callback rx_in_cb, uintptr_t context);
41 static void PIOS_USART_RegisterTxCallback(uintptr_t usart_id,
pios_com_callback tx_out_cb, uintptr_t context);
42 static void PIOS_USART_TxStart(uintptr_t usart_id, uint16_t tx_bytes_avail);
43 static void PIOS_USART_RxStart(uintptr_t usart_id, uint16_t rx_bytes_avail);
47 .tx_start = PIOS_USART_TxStart,
48 .rx_start = PIOS_USART_RxStart,
49 .bind_tx_cb = PIOS_USART_RegisterTxCallback,
50 .bind_rx_cb = PIOS_USART_RegisterRxCallback,
53 enum pios_usart_dev_magic {
54 PIOS_USART_DEV_MAGIC = 0x11223344,
57 struct pios_usart_dev {
58 enum pios_usart_dev_magic
magic;
62 uintptr_t rx_in_context;
64 uintptr_t tx_out_context;
69 static bool PIOS_USART_validate(
struct pios_usart_dev * usart_dev)
71 return (usart_dev->magic == PIOS_USART_DEV_MAGIC);
74 static struct pios_usart_dev * PIOS_USART_alloc(
void)
76 struct pios_usart_dev * usart_dev;
78 usart_dev = (
struct pios_usart_dev *)
PIOS_malloc(
sizeof(*usart_dev));
79 if (!usart_dev)
return(NULL);
81 memset(usart_dev, 0,
sizeof(*usart_dev));
82 usart_dev->magic = PIOS_USART_DEV_MAGIC;
92 static void PIOS_USART_generic_irq_handler(uintptr_t usart_id);
94 static uintptr_t PIOS_USART_1_id;
96 static
void PIOS_USART_1_irq_handler (
void)
98 PIOS_USART_generic_irq_handler (PIOS_USART_1_id);
101 static uintptr_t PIOS_USART_2_id;
103 static
void PIOS_USART_2_irq_handler (
void)
105 PIOS_USART_generic_irq_handler (PIOS_USART_2_id);
108 static uintptr_t PIOS_USART_3_id;
110 static
void PIOS_USART_3_irq_handler (
void)
112 PIOS_USART_generic_irq_handler (PIOS_USART_3_id);
123 struct pios_usart_dev * usart_dev;
125 usart_dev = (
struct pios_usart_dev *) PIOS_USART_alloc();
126 if (!usart_dev)
goto out_fail;
129 usart_dev->cfg =
cfg;
132 if (usart_dev->cfg->remap) {
133 GPIO_PinRemapConfig(usart_dev->cfg->remap, ENABLE);
137 GPIO_Init(usart_dev->cfg->rx.gpio, (GPIO_InitTypeDef*)&usart_dev->cfg->rx.init);
138 GPIO_Init(usart_dev->cfg->tx.gpio, (GPIO_InitTypeDef*)&usart_dev->cfg->tx.init);
141 switch ((uint32_t)usart_dev->cfg->regs) {
142 case (uint32_t)USART1:
143 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
145 case (uint32_t)USART2:
146 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
148 case (uint32_t)USART3:
149 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
154 USART_Init(usart_dev->cfg->regs, (USART_InitTypeDef*)¶ms->
init);
156 *usart_id = (uintptr_t)usart_dev;
159 switch ((uint32_t)usart_dev->cfg->regs) {
160 case (uint32_t)USART1:
161 PIOS_USART_1_id = (uintptr_t)usart_dev;
163 case (uint32_t)USART2:
164 PIOS_USART_2_id = (uintptr_t)usart_dev;
166 case (uint32_t)USART3:
167 PIOS_USART_3_id = (uintptr_t)usart_dev;
170 NVIC_Init((NVIC_InitTypeDef*)&usart_dev->cfg->irq.init);
171 USART_ITConfig(usart_dev->cfg->regs, USART_IT_RXNE, ENABLE);
172 USART_ITConfig(usart_dev->cfg->regs, USART_IT_TXE, ENABLE);
175 USART_Cmd(usart_dev->cfg->regs, ENABLE);
183 static void PIOS_USART_RxStart(uintptr_t usart_id, uint16_t rx_bytes_avail)
185 struct pios_usart_dev * usart_dev = (
struct pios_usart_dev *)usart_id;
187 bool valid = PIOS_USART_validate(usart_dev);
190 USART_ITConfig(usart_dev->cfg->regs, USART_IT_RXNE, ENABLE);
192 static void PIOS_USART_TxStart(uintptr_t usart_id, uint16_t tx_bytes_avail)
194 struct pios_usart_dev * usart_dev = (
struct pios_usart_dev *)usart_id;
196 bool valid = PIOS_USART_validate(usart_dev);
199 USART_ITConfig(usart_dev->cfg->regs, USART_IT_TXE, ENABLE);
207 static void PIOS_USART_ChangeBaud(uintptr_t usart_id, uint32_t baud)
209 struct pios_usart_dev * usart_dev = (
struct pios_usart_dev *)usart_id;
211 bool valid = PIOS_USART_validate(usart_dev);
214 USART_InitTypeDef USART_InitStructure;
217 USART_InitStructure.USART_BaudRate = baud;
220 USART_InitStructure.USART_WordLength = usart_dev->cfg->regs->CR1 & (uint32_t)USART_CR1_M;
221 USART_InitStructure.USART_Parity = usart_dev->cfg->regs->CR1 & ((uint32_t)USART_CR1_PCE | (uint32_t)USART_CR1_PS);
222 USART_InitStructure.USART_StopBits = usart_dev->cfg->regs->CR2 & (uint32_t)USART_CR2_STOP;
223 USART_InitStructure.USART_HardwareFlowControl = usart_dev->cfg->regs->CR3 & ((uint32_t)USART_CR3_CTSE | (uint32_t)USART_CR3_RTSE);
224 USART_InitStructure.USART_Mode = usart_dev->cfg->regs->CR1 & ((uint32_t)USART_CR1_TE | (uint32_t)USART_CR1_RE) ;
227 USART_Init(usart_dev->cfg->regs, &USART_InitStructure);
230 static void PIOS_USART_RegisterRxCallback(uintptr_t usart_id,
pios_com_callback rx_in_cb, uintptr_t context)
232 struct pios_usart_dev * usart_dev = (
struct pios_usart_dev *)usart_id;
234 bool valid = PIOS_USART_validate(usart_dev);
241 usart_dev->rx_in_context = context;
242 usart_dev->rx_in_cb = rx_in_cb;
245 static void PIOS_USART_RegisterTxCallback(uintptr_t usart_id,
pios_com_callback tx_out_cb, uintptr_t context)
247 struct pios_usart_dev * usart_dev = (
struct pios_usart_dev *)usart_id;
249 bool valid = PIOS_USART_validate(usart_dev);
256 usart_dev->tx_out_context = context;
257 usart_dev->tx_out_cb = tx_out_cb;
260 static void PIOS_USART_generic_irq_handler(uintptr_t usart_id)
263 struct pios_usart_dev * usart_dev = (
struct pios_usart_dev *)usart_id;
265 bool valid = PIOS_USART_validate(usart_dev);
269 volatile uint16_t sr = usart_dev->cfg->regs->SR;
270 volatile uint8_t dr = usart_dev->cfg->regs->DR;
273 bool rx_need_yield =
false;
274 if (sr & USART_SR_RXNE) {
276 if (usart_dev->rx_in_cb) {
278 rc = (usart_dev->rx_in_cb)(usart_dev->rx_in_context, &byte, 1, NULL, &rx_need_yield);
281 usart_dev->rx_dropped += 1;
287 bool tx_need_yield =
false;
288 if (sr & USART_SR_TXE) {
289 if (usart_dev->tx_out_cb) {
291 uint16_t bytes_to_send;
293 bytes_to_send = (usart_dev->tx_out_cb)(usart_dev->tx_out_context, &b, 1, NULL, &tx_need_yield);
295 if (bytes_to_send > 0) {
297 usart_dev->cfg->regs->DR = b;
300 USART_ITConfig(usart_dev->cfg->regs, USART_IT_TXE, DISABLE);
304 USART_ITConfig(usart_dev->cfg->regs, USART_IT_TXE, DISABLE);
Main PiOS header to include all the compiled in PiOS options.
void USART1_IRQHandler(void)
#define PIOS_DEBUG_Assert(test)
#define PIOS_IRQ_Epilogue()
void * PIOS_malloc(size_t size)
USART private definitions.
static struct flyingpicmd_cfg_fa cfg
const struct pios_com_driver pios_usart_com_driver
#define USART2_IRQHandler
#define USART3_IRQHandler
int32_t PIOS_USART_Init(uintptr_t *usart_id, const struct pios_usart_cfg *cfg, struct pios_usart_params *params)
#define PIOS_IRQ_Prologue()
#define PIOS_Assert(test)
uint16_t(* pios_com_callback)(uintptr_t context, uint8_t *buf, uint16_t buf_len, uint16_t *headroom, bool *task_woken)
void(* set_baud)(uintptr_t id, uint32_t baud)