Skip to content

Commit

Permalink
Merge pull request #461 from microbit-foundation/autodetect_mb_board
Browse files Browse the repository at this point in the history
microbit: Autodetect board type on runtime
  • Loading branch information
brianesquilona authored Aug 1, 2018
2 parents 92bd0a4 + b51bb9d commit 079001e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
50 changes: 48 additions & 2 deletions source/board/microbit.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

const char *board_id = "9900";
#include "fsl_device_registers.h"
#include "IO_Config.h"

// URL_NAME and DRIVE_NAME must be 11 characters excluding
// the null terminated character
Expand All @@ -30,3 +30,49 @@ __attribute__((aligned(4)))
const char daplink_drive_name[11] = "MICROBIT ";
__attribute__((aligned(4)))
const char *const daplink_target_url = "https://microbit.org/device/?id=@B&v=@V";

const char *board_id = "";
const char * const board_id_mb_1_3 = "9900";
const char * const board_id_mb_1_5 = "9901";

typedef enum {
BOARD_VERSION_1_3 = 0,
BOARD_VERSION_1_5 = 1,
} mb_version_t;

// Enables Board Type Pin, reads it and disables it
// Depends on gpio_init() to have been executed already
uint8_t read_board_type_pin(void) {
uint8_t pin_state = 0;
// GPIO mode, Pull enable, pull down, input
PIN_BOARD_TYPE_PORT->PCR[PIN_BOARD_TYPE_BIT] = PORT_PCR_MUX(1) | PORT_PCR_PE(1) | PORT_PCR_PS(0);
PIN_BOARD_TYPE_GPIO->PDDR &= ~PIN_BOARD_TYPE;
// Wait to stabilise, based on gpio.c busy_wait(), at -O2 10000 iterations delay ~850us
for (volatile uint32_t i = 10000; i > 0; i--);
// Read pin
pin_state = (PIN_BOARD_TYPE_GPIO->PDIR & PIN_BOARD_TYPE);
// Revert and disable
PIN_BOARD_TYPE_PORT->PCR[PIN_BOARD_TYPE_BIT] = PORT_PCR_MUX(0) | PORT_PCR_PE(0);
return pin_state;
}

void set_board_id(mb_version_t board_version) {
switch (board_version) {
case BOARD_VERSION_1_3:
board_id = board_id_mb_1_3;
break;
case BOARD_VERSION_1_5:
board_id = board_id_mb_1_5;
break;
default:
board_id = board_id_mb_1_5;
break;
}
}

// Called in main_task() to init before USB and files are configured
void prerun_board_config(void) {
// With only two boards the digital pin read maps directly to the type
mb_version_t board_version = (mb_version_t)read_board_type_pin();
set_board_id(board_version);
}
6 changes: 6 additions & 0 deletions source/hic_hal/freescale/kl26z/IO_Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ COMPILER_ASSERT(DAPLINK_HIC_ID == DAPLINK_HIC_ID_KL26);
#define PIN_SW_RESET_BIT (1)
#define PIN_SW_RESET (1<<PIN_SW_RESET_BIT)

// BOARD TYPE
#define PIN_BOARD_TYPE_PORT PORTB
#define PIN_BOARD_TYPE_GPIO PTB
#define PIN_BOARD_TYPE_BIT (0)
#define PIN_BOARD_TYPE (1<<PIN_BOARD_TYPE_BIT)

// Connected LED Not available

// Target Running LED Not available
Expand Down

0 comments on commit 079001e

Please sign in to comment.