Hack the PHS Shield, serial bridge

Dependencies:   mbed

Fork of PHSShield_F405hack by phs fan

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers phs_f405.cpp Source File

phs_f405.cpp

00001 #include "phs_f405.h"
00002 #include "stm32f4xx_hal.h"
00003 #include <cstdarg>
00004 
00005 
00006 void PhsReset::write (int value) {
00007     __GPIOB_CLK_ENABLE();
00008     if (value) {
00009         GPIOB->MODER &= ~(3<<22); // PB_11 input
00010         GPIOB->PUPDR = (GPIOB->PUPDR & ~(3<<22)) | (1<<22); // pullup
00011         GPIOB->OTYPER &= ~(1<<11); // push pull
00012         GPIOB->BSRRL = (1<<11); // reser=1
00013     } else {
00014         GPIOB->MODER = (GPIOB->MODER & ~(3<<22)) | (1<<22); // PB_11 output
00015         GPIOB->PUPDR &= ~(3<<22); // pull none
00016         GPIOB->OTYPER |= (1<<11); // open drain
00017         GPIOB->BSRRH = (1<<11); // reser=0
00018     }
00019 }
00020 
00021 int PhsReset::read () {
00022     int status;
00023     status = (GPIOB->ODR & (1<<11)) ? 1 : 0;
00024     return status;
00025 }
00026 
00027 
00028 #define STRING_STACK_LIMIT 256
00029 #define UART5_BASE            (APB1PERIPH_BASE + 0x5000)
00030 #define UART5               ((USART_TypeDef *) UART5_BASE)
00031 #define  RCC_APB1ENR_UART5EN                 ((uint32_t)0x00100000)
00032 #define __UART5_CLK_ENABLE()   (RCC->APB1ENR |= (RCC_APB1ENR_UART5EN))
00033 #define GPIO_AF8_UART5         ((uint8_t)0x08) 
00034 
00035 ShieldSerial::ShieldSerial (int baud) {
00036     __UART5_CLK_ENABLE();
00037 
00038     UartHandle.Instance = (USART_TypeDef *)UART5;
00039     UartHandle.Init.BaudRate   = baud;
00040     UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
00041     UartHandle.Init.StopBits   = UART_STOPBITS_1;
00042     UartHandle.Init.Parity     = UART_PARITY_NONE;
00043     UartHandle.Init.HwFlowCtl  = UART_HWCONTROL_NONE;
00044     UartHandle.Init.Mode = UART_MODE_TX_RX;
00045     if (HAL_UART_Init(&UartHandle) != HAL_OK) {
00046         mbed_die();
00047     }
00048 
00049     __GPIOC_CLK_ENABLE();
00050     __GPIOD_CLK_ENABLE();
00051     GPIO_InitTypeDef  GPIO_InitStruct;
00052     GPIO_InitStruct.Mode      = GPIO_MODE_AF_PP;
00053     GPIO_InitStruct.Pull      = GPIO_PULLUP;
00054     GPIO_InitStruct.Speed     = GPIO_SPEED_FAST;
00055     GPIO_InitStruct.Pin       = GPIO_PIN_12;
00056     GPIO_InitStruct.Alternate = GPIO_AF8_UART5;
00057     HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); // TX
00058     GPIO_InitStruct.Pin = GPIO_PIN_2;
00059     GPIO_InitStruct.Alternate = GPIO_AF8_UART5;
00060     HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); // RX
00061 }
00062 
00063 int ShieldSerial::getc () {
00064     while (!readable());
00065     return (int)(UartHandle.Instance->DR & 0x1FF);
00066 }
00067 
00068 int ShieldSerial::putc (int c) {
00069     while (!writeable());
00070     UartHandle.Instance->DR = (uint32_t)(c & 0x1FF);
00071     return 0;
00072 }
00073 
00074 int ShieldSerial::readable () {
00075     int status;
00076     status = ((__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_RXNE) != RESET) ? 1 : 0);
00077     return status;
00078 }
00079 
00080 int ShieldSerial::writeable () {
00081     int status;
00082     status = ((__HAL_UART_GET_FLAG(&UartHandle, UART_FLAG_TXE) != RESET) ? 1 : 0);
00083     return status;
00084 }